diff --git a/internal/agentexec/harness_adapters.go b/internal/agentexec/harness_adapters.go index 92a5a745b..fc644306e 100644 --- a/internal/agentexec/harness_adapters.go +++ b/internal/agentexec/harness_adapters.go @@ -52,7 +52,7 @@ type HarnessStages struct { Spec agentrun.HarnessSpec } -func (r HarnessStages) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.ToolCallReporter) (agentrun.Result, error) { +func (r HarnessStages) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { spec := r.Spec req.Harness = &spec return r.Runner.Run(ctx, workspace, req, report) diff --git a/internal/agentexec/harness_mcp_test.go b/internal/agentexec/harness_mcp_test.go index da4f0fa88..39f342e2e 100644 --- a/internal/agentexec/harness_mcp_test.go +++ b/internal/agentexec/harness_mcp_test.go @@ -34,7 +34,7 @@ func TestHarnessRequiresMCPInitialization(t *testing.T) { if !central { req.CaptureTools = []agentrun.CaptureTool{{Name: "capture_result", Parameters: json.RawMessage(`{"type":"object"}`)}} } - result, err := NewHarnessRunner(nil).Run(t.Context(), workspace, req, nil) + result, err := NewHarnessRunner(nil).Run(t.Context(), workspace, req, agentrun.Reporter{}) if err != nil { t.Fatal(err) } diff --git a/internal/agentexec/harness_output_claude.go b/internal/agentexec/harness_output_claude.go index a05d4a207..4c28c6fb6 100644 --- a/internal/agentexec/harness_output_claude.go +++ b/internal/agentexec/harness_output_claude.go @@ -28,7 +28,7 @@ func newClaudeCodeHarnessOutput() *claudeCodeHarnessOutput { } } -func (o *claudeCodeHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { +func (o *claudeCodeHarnessOutput) Line(line []byte, report agentrun.Reporter) { var event struct { Type string `json:"type"` SessionID string `json:"session_id"` @@ -55,7 +55,7 @@ func (o *claudeCodeHarnessOutput) Line(line []byte, report agentrun.ToolCallRepo } } -func (o *claudeCodeHarnessOutput) assistant(raw json.RawMessage, _ agentrun.ToolCallReporter) { +func (o *claudeCodeHarnessOutput) assistant(raw json.RawMessage, _ agentrun.Reporter) { var message struct { ID string `json:"id"` Usage json.RawMessage `json:"usage"` @@ -79,7 +79,7 @@ func (o *claudeCodeHarnessOutput) assistant(raw json.RawMessage, _ agentrun.Tool } } -func (o *claudeCodeHarnessOutput) user(raw json.RawMessage, report agentrun.ToolCallReporter) { +func (o *claudeCodeHarnessOutput) user(raw json.RawMessage, report agentrun.Reporter) { var message struct { Content []struct { Type string `json:"type"` @@ -100,12 +100,10 @@ func (o *claudeCodeHarnessOutput) user(raw json.RawMessage, report agentrun.Tool continue } delete(o.pending, block.ToolUseID) - if report != nil { - report(agentrun.ToolCallReport{ - Tool: tool, Detail: ClipToolCallDetail(claudeResultContent(block.Content)), - Failed: block.IsError, - }) - } + report.ReportToolCall(agentrun.ToolCallReport{ + Tool: tool, Detail: ClipToolCallDetail(claudeResultContent(block.Content)), + Failed: block.IsError, + }) } } diff --git a/internal/agentexec/harness_output_codex.go b/internal/agentexec/harness_output_codex.go index f21cf7b4c..656ec453e 100644 --- a/internal/agentexec/harness_output_codex.go +++ b/internal/agentexec/harness_output_codex.go @@ -15,7 +15,7 @@ type codexHarnessOutput struct { usage agentrun.Usage } -func (o *codexHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { +func (o *codexHarnessOutput) Line(line []byte, report agentrun.Reporter) { var event struct { Type string `json:"type"` ThreadID string `json:"thread_id"` @@ -43,8 +43,8 @@ func (o *codexHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) }) } case "item.completed": - if call, ok := event.Item.report(); ok && report != nil { - report(call) + if call, ok := event.Item.report(); ok { + report.ReportToolCall(call) } } } diff --git a/internal/agentexec/harness_output_copilot.go b/internal/agentexec/harness_output_copilot.go index 2488ee861..5afc2e27f 100644 --- a/internal/agentexec/harness_output_copilot.go +++ b/internal/agentexec/harness_output_copilot.go @@ -18,7 +18,7 @@ func newCopilotHarnessOutput() *copilotHarnessOutput { return &copilotHarnessOutput{pending: make(map[string]string)} } -func (o *copilotHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { +func (o *copilotHarnessOutput) Line(line []byte, report agentrun.Reporter) { var event struct { Type string `json:"type"` SessionID string `json:"sessionId"` @@ -52,9 +52,7 @@ func (o *copilotHarnessOutput) Line(line []byte, report agentrun.ToolCallReporte return } delete(o.pending, event.Data.ToolCallID) - if report != nil { - report(agentrun.ToolCallReport{Tool: tool, Detail: ClipToolCallDetail(event.Data.Result.Content), Failed: !event.Data.Success}) - } + report.ReportToolCall(agentrun.ToolCallReport{Tool: tool, Detail: ClipToolCallDetail(event.Data.Result.Content), Failed: !event.Data.Success}) } } diff --git a/internal/agentexec/harness_output_pi.go b/internal/agentexec/harness_output_pi.go index a853c7816..df2a0f611 100644 --- a/internal/agentexec/harness_output_pi.go +++ b/internal/agentexec/harness_output_pi.go @@ -15,7 +15,7 @@ type piHarnessOutput struct { usage agentrun.Usage } -func (o *piHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { +func (o *piHarnessOutput) Line(line []byte, report agentrun.Reporter) { var event struct { Type string `json:"type"` ID string `json:"id"` @@ -49,7 +49,7 @@ func (o *piHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { o.usage = addUsage(o.usage, usageFromFields(u.Input, u.Output, u.CacheRead, u.CacheWrite)) } case "tool_execution_end": - if event.ToolName == "" || report == nil { + if event.ToolName == "" { return } texts := make([]string, 0, len(event.Result.Content)) @@ -58,7 +58,7 @@ func (o *piHarnessOutput) Line(line []byte, report agentrun.ToolCallReporter) { texts = append(texts, block.Text) } } - report(agentrun.ToolCallReport{Tool: event.ToolName, Detail: ClipToolCallDetail(strings.Join(texts, "\n")), Failed: event.IsError}) + report.ReportToolCall(agentrun.ToolCallReport{Tool: event.ToolName, Detail: ClipToolCallDetail(strings.Join(texts, "\n")), Failed: event.IsError}) } } diff --git a/internal/agentexec/harness_runner.go b/internal/agentexec/harness_runner.go index 8954448a6..db48ccd72 100644 --- a/internal/agentexec/harness_runner.go +++ b/internal/agentexec/harness_runner.go @@ -45,7 +45,7 @@ const ( // knows its CLI's stream format; it reports tool calls as they complete and // the session to resume. type HarnessOutput interface { - Line(line []byte, report agentrun.ToolCallReporter) + Line(line []byte, report agentrun.Reporter) SessionID() string Usage() agentrun.Usage } @@ -67,7 +67,7 @@ func NewHarnessRunner(newOutput func() HarnessOutput) *HarnessRunner { return &HarnessRunner{newOutput: newOutput} } -func (r *HarnessRunner) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.ToolCallReporter) (agentrun.Result, error) { +func (r *HarnessRunner) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { if err := validateHarness(req); err != nil { return agentrun.Result{}, err } @@ -95,7 +95,7 @@ func (r *HarnessRunner) Run(ctx context.Context, workspace string, req agentrun. return res, err } -func (r *HarnessRunner) run(ctx context.Context, workspace string, req agentrun.Request, mcpConfig string, report agentrun.ToolCallReporter) (agentrun.Result, error) { +func (r *HarnessRunner) run(ctx context.Context, workspace string, req agentrun.Request, mcpConfig string, report agentrun.Reporter) (agentrun.Result, error) { res := agentrun.Result{Version: agentrun.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt, Stage: req.Stage} before, err := snapshotRepo(workspace) if err != nil { @@ -244,7 +244,7 @@ func resumeVerb(h *agentrun.HarnessSpec, sessionID string) []string { // invoke runs one harness invocation in its own process group, feeding // stdout to out line by line. Cancelling ctx kills the whole group, so a // CLI's children cannot outlive the step. -func invoke(ctx context.Context, workspace string, h *agentrun.HarnessSpec, argv []string, out HarnessOutput, report agentrun.ToolCallReporter) error { +func invoke(ctx context.Context, workspace string, h *agentrun.HarnessSpec, argv []string, out HarnessOutput, report agentrun.Reporter) error { cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) cmd.Dir = workspace cmd.WaitDelay = killGrace @@ -467,9 +467,9 @@ func policyViolation(before, after repoState, changes []string, req agentrun.Req type silentOutput struct{} -func (silentOutput) Line([]byte, agentrun.ToolCallReporter) {} -func (silentOutput) SessionID() string { return "" } -func (silentOutput) Usage() agentrun.Usage { return agentrun.Usage{} } +func (silentOutput) Line([]byte, agentrun.Reporter) {} +func (silentOutput) SessionID() string { return "" } +func (silentOutput) Usage() agentrun.Usage { return agentrun.Usage{} } // tailBuffer keeps the last stderrTailBytes written to it. type tailBuffer struct{ buf bytes.Buffer } diff --git a/internal/agentexec/harness_runner_test.go b/internal/agentexec/harness_runner_test.go index 2d3127556..55826ad84 100644 --- a/internal/agentexec/harness_runner_test.go +++ b/internal/agentexec/harness_runner_test.go @@ -18,7 +18,7 @@ func TestHarnessLiveOutput(t *testing.T) { if os.Geteuid() == 0 { user = "65534" } - err := invoke(t.Context(), "/tmp", &agentrun.HarnessSpec{User: user}, []string{"sh", "-c", `printf '%s\n' '{"type":"thread.started","thread_id":"live-thread"}'; printf '%s\n' 'live diagnostic' >&2`}, adapter, nil) + err := invoke(t.Context(), "/tmp", &agentrun.HarnessSpec{User: user}, []string{"sh", "-c", `printf '%s\n' '{"type":"thread.started","thread_id":"live-thread"}'; printf '%s\n' 'live diagnostic' >&2`}, adapter, agentrun.Reporter{}) if err != nil { t.Fatal(err) } diff --git a/internal/agentexec/modelloop/loop_runner.go b/internal/agentexec/modelloop/loop_runner.go index d0a67b6ce..4eaecb4fa 100644 --- a/internal/agentexec/modelloop/loop_runner.go +++ b/internal/agentexec/modelloop/loop_runner.go @@ -53,7 +53,7 @@ func NewLoopRunner( // work on the chat workspace and bypass ReadOnly and ProtectPaths. const workspaceToolset = "workspace" -func (r *LoopRunner) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.ToolCallReporter) (agentrun.Result, error) { +func (r *LoopRunner) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { if err := req.Validate(); err != nil { return agentrun.Result{}, err } @@ -67,12 +67,18 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req agentrun.Req notes := &memoryNotes{initial: req.Notes} captures := make(map[string][]json.RawMessage) toolOpts := r.Limits.Options(workspaceToolset) - toolOpts.OnToolCall = report + toolOpts.OnToolCall = report.ToolCall centralTools, err := BuildToolSet(r.tools, toolOpts) if err != nil { return agentrun.Result{}, fmt.Errorf("build central tool set: %w", err) } captureTools := captureToolSet(req.CaptureTools, captures) + var modelHooks []core.ModelHook + var toolHooks []core.ToolHook + if report.Transcript != nil { + recorder := newTranscript(report.Transcript) + modelHooks, toolHooks = []core.ModelHook{recorder}, []core.ToolHook{recorder} + } res, err := r.run(ctx, agentloop.Config{ Runtime: r.runtime, ModelRef: req.Model, @@ -101,7 +107,9 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req agentrun.Req allowedTools(centralTools, r.AllowTools), captureTools, ), - Logger: r.logger(req), + ModelHooks: modelHooks, + ToolHooks: toolHooks, + Logger: r.logger(req), }) result := resultFromRun(req, res, notes.appended, captures) if cause := context.Cause(ctx); cause != nil { diff --git a/internal/agentexec/modelloop/toolset.go b/internal/agentexec/modelloop/toolset.go index f07cb7928..86393b760 100644 --- a/internal/agentexec/modelloop/toolset.go +++ b/internal/agentexec/modelloop/toolset.go @@ -74,7 +74,7 @@ type ToolSetOptions struct { // OnToolCall, when non-nil, is notified once per completed tool call. // LoopRunner populates it so a workflow stage can surface tool activity on // the task timeline. Nil means no one is listening. - OnToolCall agentrun.ToolCallReporter + OnToolCall func(agentrun.ToolCallReport) } // reportToolCallCompletion notifies opts.OnToolCall, if set, of one diff --git a/internal/agentexec/modelloop/transcript.go b/internal/agentexec/modelloop/transcript.go new file mode 100644 index 000000000..3ea93618d --- /dev/null +++ b/internal/agentexec/modelloop/transcript.go @@ -0,0 +1,187 @@ +package modelloop + +import ( + "context" + "encoding/json" + "strings" + "sync" + "time" + "unicode/utf8" + + "github.com/samcharles93/ai-sdk/chat" + "github.com/samcharles93/ai-sdk/core" + + "github.com/samcharles93/archie-core/internal/domain/agentrun" + "github.com/samcharles93/archie-core/internal/webhookguard" +) + +// transcriptFieldLimit caps each recorded text field, in bytes. +const transcriptFieldLimit = 32 << 10 + +// compactionPrefix starts the instruction agentloop sends to summarise the +// history, which marks a model call as a compaction rather than a step. +const compactionPrefix = "Compact the conversation history above" + +// transcript records one agent run's conversation through agentloop's model +// and tool hooks. Tool hooks run concurrently, so entries are numbered and +// reported under one lock to keep their order. +type transcript struct { + report func(agentrun.TranscriptEntry) + + mu sync.Mutex + seq int + prompted bool + lastUser string + started map[string]time.Time +} + +func newTranscript(report func(agentrun.TranscriptEntry)) *transcript { + return &transcript{report: report, started: map[string]time.Time{}} +} + +func (t *transcript) add(entry agentrun.TranscriptEntry) { + t.seq++ + entry.Seq = t.seq + t.report(entry) +} + +func (t *transcript) ModelCallStarted(context.Context, chat.Request) {} + +func (t *transcript) ModelCallFinished(_ context.Context, req chat.Request, resp *chat.Response, err error, latency time.Duration, usage *chat.Usage) { + t.mu.Lock() + defer t.mu.Unlock() + entry := agentrun.TranscriptEntry{Kind: agentrun.TranscriptModel, LatencyMS: latency.Milliseconds(), Usage: transcriptUsage(usage)} + if isCompaction(req) { + entry.Kind = agentrun.TranscriptCompaction + } else { + t.recordInput(req.Messages) + } + if err != nil { + entry.Error = clip(err.Error(), nil) + } + if resp != nil { + entry.Text = clip(resp.Content, &entry.TextBytes) + entry.Reasoning = clip(reasoning(resp.Parts), &entry.ReasoningBytes) + entry.FinishReason = resp.FinishReason + for _, call := range resp.ToolCalls { + recorded := agentrun.TranscriptToolCall{ID: call.ID, Name: call.Name} + recorded.Args = clipArgs(call.Arguments, &recorded.ArgsBytes) + entry.ToolCalls = append(entry.ToolCalls, recorded) + } + } + t.add(entry) +} + +// recordInput records the prompt on the first call, and after that each user +// message the loop appended since the model last answered: gate feedback, +// recovery nudges. Assistant and tool messages are recorded from the response +// and the tool hooks instead. +func (t *transcript) recordInput(messages []chat.Message) { + if !t.prompted { + t.prompted = true + for _, message := range messages { + t.recordMessage(message) + } + return + } + start := len(messages) + for start > 0 && messages[start-1].Role == chat.RoleUser { + start-- + } + for _, message := range messages[start:] { + if message.Content != t.lastUser { + t.recordMessage(message) + } + } +} + +func (t *transcript) recordMessage(message chat.Message) { + kind := agentrun.TranscriptUser + switch message.Role { + case chat.RoleSystem: + kind = agentrun.TranscriptSystem + case chat.RoleUser: + t.lastUser = message.Content + default: + return + } + entry := agentrun.TranscriptEntry{Kind: kind} + entry.Text = clip(message.Content, &entry.TextBytes) + t.add(entry) +} + +func (t *transcript) BeforeToolExecute(_ context.Context, call *core.ToolCall) (*core.Skip, error) { + t.mu.Lock() + t.started[call.ToolCallID] = time.Now() + t.mu.Unlock() + return nil, nil +} + +func (t *transcript) AfterToolExecute(_ context.Context, call core.ToolCall, result *core.ToolResult) { + t.mu.Lock() + defer t.mu.Unlock() + entry := agentrun.TranscriptEntry{Kind: agentrun.TranscriptTool, CallID: call.ToolCallID, Tool: call.ToolName} + if started, ok := t.started[call.ToolCallID]; ok { + entry.LatencyMS = time.Since(started).Milliseconds() + delete(t.started, call.ToolCallID) + } + entry.Args = clipArgs(call.Input, &entry.ArgsBytes) + entry.Output = clip(result.Output, &entry.OutputBytes) + entry.Error = clip(result.Error, nil) + t.add(entry) +} + +func reasoning(parts chat.Parts) string { + var texts []string + for _, part := range parts { + if thought, ok := part.(chat.ReasoningPart); ok && thought.Text != "" { + texts = append(texts, thought.Text) + } + } + return strings.Join(texts, "\n\n") +} + +func isCompaction(req chat.Request) bool { + n := len(req.Messages) + return n > 0 && req.Messages[n-1].Role == chat.RoleUser && strings.HasPrefix(req.Messages[n-1].Content, compactionPrefix) +} + +func transcriptUsage(usage *chat.Usage) *agentrun.Usage { + if usage == nil { + return nil + } + return &agentrun.Usage{ + PromptTokens: usage.PromptTokens, + CompletionTokens: usage.CompletionTokens, + TotalTokens: usage.TotalTokens, + CachedTokens: usage.CachedTokens, + CacheCreationTokens: usage.CacheCreationTokens, + } +} + +// clipArgs redacts tool arguments by key when they are JSON, then clips them. +func clipArgs(args string, full *int) string { + if json.Valid([]byte(args)) { + if redacted, err := webhookguard.RedactPayload([]byte(args)); err == nil { + args = string(redacted) + } + } + return clip(args, full) +} + +// clip redacts secret-shaped spans and cuts text to transcriptFieldLimit on a +// rune boundary, storing the original size in full when it cuts. +func clip(text string, full *int) string { + text = webhookguard.RedactText(text) + if len(text) <= transcriptFieldLimit { + return text + } + if full != nil { + *full = len(text) + } + cut := transcriptFieldLimit + for cut > 0 && !utf8.RuneStart(text[cut]) { + cut-- + } + return text[:cut] +} diff --git a/internal/agentexec/modelloop/transcript_test.go b/internal/agentexec/modelloop/transcript_test.go new file mode 100644 index 000000000..05950d65e --- /dev/null +++ b/internal/agentexec/modelloop/transcript_test.go @@ -0,0 +1,71 @@ +package modelloop + +import ( + "strings" + "testing" + + "github.com/samcharles93/ai-sdk/core" + + "github.com/samcharles93/archie-core/internal/domain/agentrun" +) + +func TestTranscriptToolEntryRedactsAndClips(t *testing.T) { + const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcmNoaWUifQ.c2lnbmF0dXJlc2lnbmF0dXJl" + const pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIEow\n-----END RSA PRIVATE KEY-----" + long := strings.Repeat("x", transcriptFieldLimit+10) + tests := []struct { + name string + input string + output string + wantArgs string + wantOutput string + wantBytes int + }{ + { + name: "secret key in arguments, token inside output", + input: `{"path":"a.txt","api_key":"plain-value"}`, + output: "curl -H 'Authorization: Bearer " + jwt + "' done", + wantArgs: `{"api_key":"[redacted]","path":"a.txt"}`, + wantOutput: "curl -H 'Authorization: Bearer [redacted]' done", + }, + { + name: "private key block inside output", + input: `{}`, + output: "before\n" + pem + "\nafter", + wantArgs: `{}`, + wantOutput: "before\n[redacted]\nafter", + }, + { + name: "oversized output keeps its full size", + input: `{}`, + output: long, + wantArgs: `{}`, + wantOutput: long[:transcriptFieldLimit], + wantBytes: len(long), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var got []agentrun.TranscriptEntry + recorder := newTranscript(func(entry agentrun.TranscriptEntry) { got = append(got, entry) }) + call := core.ToolCall{ToolCallID: "c1", ToolName: "shell", Input: tt.input} + if _, err := recorder.BeforeToolExecute(t.Context(), &call); err != nil { + t.Fatal(err) + } + recorder.AfterToolExecute(t.Context(), call, &core.ToolResult{Output: tt.output}) + if len(got) != 1 { + t.Fatalf("entries = %d, want 1", len(got)) + } + entry := got[0] + if entry.Args != tt.wantArgs { + t.Errorf("args = %q, want %q", entry.Args, tt.wantArgs) + } + if entry.Output != tt.wantOutput { + t.Errorf("output = %q, want %q", entry.Output, tt.wantOutput) + } + if entry.OutputBytes != tt.wantBytes { + t.Errorf("output bytes = %d, want %d", entry.OutputBytes, tt.wantBytes) + } + }) + } +} diff --git a/internal/app/agentworker/persistent_runner.go b/internal/app/agentworker/persistent_runner.go index b9b9b8ed8..4d90a9598 100644 --- a/internal/app/agentworker/persistent_runner.go +++ b/internal/app/agentworker/persistent_runner.go @@ -44,7 +44,7 @@ func readProjectMemory(path string) (string, error) { return "Project memory from earlier sessions:\n" + strings.Join(entries, "\n"), nil } -func (r persistentRunner) Run(ctx context.Context, workspace string, request agentrun.Request, report agentrun.ToolCallReporter) (agentrun.Result, error) { +func (r persistentRunner) Run(ctx context.Context, workspace string, request agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { if !r.enabled { return r.Runner.Run(ctx, workspace, request, report) } diff --git a/internal/contracts/state/v1/task_event.pb.go b/internal/contracts/state/v1/task_event.pb.go index 11b412264..19435ecae 100644 --- a/internal/contracts/state/v1/task_event.pb.go +++ b/internal/contracts/state/v1/task_event.pb.go @@ -211,8 +211,12 @@ func (x *EventsSinceResponse) GetEvents() []*Event { } type TaskEventsRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + // transcript selects the agent_transcript entries of one attempt instead of + // the timeline, which leaves them out. + Transcript bool `protobuf:"varint,2,opt,name=transcript,proto3" json:"transcript,omitempty"` + Attempt int64 `protobuf:"varint,3,opt,name=attempt,proto3" json:"attempt,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -254,6 +258,20 @@ func (x *TaskEventsRequest) GetTaskId() int64 { return 0 } +func (x *TaskEventsRequest) GetTranscript() bool { + if x != nil { + return x.Transcript + } + return false +} + +func (x *TaskEventsRequest) GetAttempt() int64 { + if x != nil { + return x.Attempt + } + return 0 +} + type TaskEventsResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Events []*Event `protobuf:"bytes,1,rep,name=events,proto3" json:"events,omitempty"` @@ -311,9 +329,13 @@ const file_state_v1_task_event_proto_rawDesc = "" + "\bsince_id\x18\x01 \x01(\tR\asinceId\x12\x14\n" + "\x05limit\x18\x02 \x01(\x03R\x05limit\">\n" + "\x13EventsSinceResponse\x12'\n" + - "\x06events\x18\x01 \x03(\v2\x0f.state.v1.EventR\x06events\",\n" + + "\x06events\x18\x01 \x03(\v2\x0f.state.v1.EventR\x06events\"f\n" + "\x11TaskEventsRequest\x12\x17\n" + - "\atask_id\x18\x01 \x01(\x03R\x06taskId\"=\n" + + "\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x1e\n" + + "\n" + + "transcript\x18\x02 \x01(\bR\n" + + "transcript\x12\x18\n" + + "\aattempt\x18\x03 \x01(\x03R\aattempt\"=\n" + "\x12TaskEventsResponse\x12'\n" + "\x06events\x18\x01 \x03(\v2\x0f.state.v1.EventR\x06events2\xf3\x01\n" + "\x10TaskEventService\x12J\n" + diff --git a/internal/domain/agentrun/runner.go b/internal/domain/agentrun/runner.go index 39df40658..484db1f20 100644 --- a/internal/domain/agentrun/runner.go +++ b/internal/domain/agentrun/runner.go @@ -4,12 +4,13 @@ import "context" // Runner executes one autonomous stage against an already prepared workspace. type Runner interface { - // report, when non-nil, is notified once per completed tool call. - Run(ctx context.Context, workspace string, req Request, report ToolCallReporter) (Result, error) + // report receives what the run does as it happens; its zero value + // discards everything. + Run(ctx context.Context, workspace string, req Request, report Reporter) (Result, error) } // ToolCallReport is one completed tool invocation, reported through a -// ToolCallReporter. +// Reporter. type ToolCallReport struct { Tool string // Detail is a clipped one-line summary: the tool's result, or @@ -18,6 +19,67 @@ type ToolCallReport struct { Failed bool } -// ToolCallReporter receives one ToolCallReport per completed tool call. A nil -// reporter is valid and reports nothing. -type ToolCallReporter func(ToolCallReport) +// Reporter receives a run's activity. Either func may be nil. +type Reporter struct { + // ToolCall is notified once per completed tool call. + ToolCall func(ToolCallReport) + // Transcript is notified of each transcript entry, in order. + Transcript func(TranscriptEntry) +} + +// ReportToolCall forwards report to r.ToolCall, if set. +func (r Reporter) ReportToolCall(report ToolCallReport) { + if r.ToolCall != nil { + r.ToolCall(report) + } +} + +// ReportTranscript forwards entry to r.Transcript, if set. +func (r Reporter) ReportTranscript(entry TranscriptEntry) { + if r.Transcript != nil { + r.Transcript(entry) + } +} + +// Transcript entry kinds. +const ( + TranscriptSystem = "system" // Text: the system prompt + TranscriptUser = "user" // Text: the mission, or input the loop added later + TranscriptModel = "model" // Reasoning, Text and ToolCalls: one model response + TranscriptTool = "tool" // one tool call's arguments and result + TranscriptCompaction = "compaction" // Text: the summary that replaced the history +) + +// TranscriptEntry is one ordered item of an agent run's conversation. Text +// fields are redacted and clipped; a *Bytes field holds the full size when its +// field was clipped. +type TranscriptEntry struct { + Seq int `json:"seq"` + Kind string `json:"kind"` + Text string `json:"text,omitempty"` + TextBytes int `json:"text_bytes,omitempty"` + ToolCalls []TranscriptToolCall `json:"tool_calls,omitempty"` + + Reasoning string `json:"reasoning,omitempty"` + ReasoningBytes int `json:"reasoning_bytes,omitempty"` + + CallID string `json:"call_id,omitempty"` + Tool string `json:"tool,omitempty"` + Args string `json:"args,omitempty"` + ArgsBytes int `json:"args_bytes,omitempty"` + Output string `json:"output,omitempty"` + OutputBytes int `json:"output_bytes,omitempty"` + Error string `json:"error,omitempty"` + + FinishReason string `json:"finish_reason,omitempty"` + Usage *Usage `json:"usage,omitempty"` + LatencyMS int64 `json:"latency_ms,omitempty"` +} + +// TranscriptToolCall is one tool call a model response requested. +type TranscriptToolCall struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Args string `json:"args,omitempty"` + ArgsBytes int `json:"args_bytes,omitempty"` +} diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index cda7ccdde..be7ed383e 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -153,7 +153,10 @@ type TaskQueries interface { type TaskEvents interface { InsertEvent(ctx context.Context, e events.Event) (int64, error) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) + // TaskEvents returns a task's timeline, without agent transcripts. TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) + // TaskTranscript returns one attempt's agent transcript entries in order. + TaskTranscript(ctx context.Context, taskID int64, attempt int) ([]events.Event, error) WorkflowStats(ctx context.Context) ([]WorkflowStat, error) StageStats(ctx context.Context) ([]StageStat, error) TokensByDay(ctx context.Context, days int) ([]DayTokens, error) diff --git a/internal/domain/workflow/agent.go b/internal/domain/workflow/agent.go index 8ede4e01d..e9c89cd1e 100644 --- a/internal/domain/workflow/agent.go +++ b/internal/domain/workflow/agent.go @@ -71,7 +71,7 @@ func (a AgentStage) Stage() Stage { // The agent call is a child of the stage's recorded step: the store // says what the stage's runtime did, not just what the stage did. res, runErr := tc.RunAgentChild(ctx, a.Name, a.Model, modelRef, func() (agentrun.Result, error) { - return tc.Agent.Run(ctx, tc.Dir, req, tc.toolCallReporter(ctx, a.Name)) + return tc.Agent.Run(ctx, tc.Dir, req, tc.reporter(ctx, a.Name)) }) return a.handleResult(ctx, tc, req, res, runErr, modelRef) }} diff --git a/internal/domain/workflow/agent_budget_test.go b/internal/domain/workflow/agent_budget_test.go index a3bea04d3..82bf1637a 100644 --- a/internal/domain/workflow/agent_budget_test.go +++ b/internal/domain/workflow/agent_budget_test.go @@ -15,7 +15,7 @@ import ( // fakeBudgetRunner is an agent runner whose stage exhausted its budget. type fakeBudgetRunner struct{ res agentrun.Result } -func (f fakeBudgetRunner) Run(context.Context, string, agentrun.Request, agentrun.ToolCallReporter) (agentrun.Result, error) { +func (f fakeBudgetRunner) Run(context.Context, string, agentrun.Request, agentrun.Reporter) (agentrun.Result, error) { return f.res, nil } diff --git a/internal/domain/workflow/implement.go b/internal/domain/workflow/implement.go index 225e76dbd..d90132133 100644 --- a/internal/domain/workflow/implement.go +++ b/internal/domain/workflow/implement.go @@ -97,7 +97,7 @@ func runBaselineFix(ctx context.Context, tc *TaskContext, argv []string, out []b } res, agentErr := tc.RunAgentChild(ctx, "baseline-fix", "", modelRef, func() (agentrun.Result, error) { - return tc.Agent.Run(ctx, tc.Dir, req, tc.toolCallReporter(ctx, "baseline-fix")) + return tc.Agent.Run(ctx, tc.Dir, req, tc.reporter(ctx, "baseline-fix")) }) if agentErr != nil && res.Version == 0 { return baselineFixFailedError(argv, string(out), fmt.Sprintf("the builder could not run: %v", agentErr)) diff --git a/internal/domain/workflow/prreview_stages.go b/internal/domain/workflow/prreview_stages.go index 40b72fbd5..bd94fb341 100644 --- a/internal/domain/workflow/prreview_stages.go +++ b/internal/domain/workflow/prreview_stages.go @@ -579,7 +579,7 @@ func runReviewerAgent( if err != nil { return agentrun.Result{}, err } - res, runErr := tc.Agent.Run(ctx, tc.prReview.snapshotDir, req, tc.toolCallReporter(ctx, name)) + res, runErr := tc.Agent.Run(ctx, tc.prReview.snapshotDir, req, tc.reporter(ctx, name)) tc.prReview.tokensSpent.Add(int64(res.TokensUsed)) to, detail := taskstate.StepSucceeded, res.Summary switch { @@ -805,7 +805,7 @@ func runPRReviewAgentRecorded( CaptureTools: captureTools, } res, err := tc.RunAgentChild(ctx, name, "", modelRef, func() (agentrun.Result, error) { - return tc.Agent.Run(ctx, workspace, req, tc.toolCallReporter(ctx, name)) + return tc.Agent.Run(ctx, workspace, req, tc.reporter(ctx, name)) }) tc.prReview.tokensSpent.Add(int64(res.TokensUsed)) return res, err diff --git a/internal/domain/workflow/triage_repo_optional_test.go b/internal/domain/workflow/triage_repo_optional_test.go index 39f9f3841..154b22999 100644 --- a/internal/domain/workflow/triage_repo_optional_test.go +++ b/internal/domain/workflow/triage_repo_optional_test.go @@ -64,7 +64,7 @@ type fakePromptRunner struct { mission string } -func (f *fakePromptRunner) Run(_ context.Context, workspace string, req agentrun.Request, _ agentrun.ToolCallReporter) (agentrun.Result, error) { +func (f *fakePromptRunner) Run(_ context.Context, workspace string, req agentrun.Request, _ agentrun.Reporter) (agentrun.Result, error) { f.workspace = workspace f.mission = req.Mission return f.res, nil diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 8fa55f430..7896c7742 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -206,7 +206,7 @@ func (tc *TaskContext) Emit(ctx context.Context, kind, stage, detail string, dat // Activity belongs to the recorded stage, even when an agent uses its own name. func (tc *TaskContext) activityData(kind string, data map[string]any) map[string]any { - if tc.StepID == 0 || (kind != events.KindToolCall && kind != events.KindAgentFinish) { + if tc.StepID == 0 || (kind != events.KindToolCall && kind != events.KindAgentFinish && kind != events.KindAgentTranscript) { return data } out := make(map[string]any, len(data)+2) @@ -239,18 +239,41 @@ func (tc *TaskContext) EmitDurable(ctx context.Context, kind, stage, detail stri return nil } -// toolCallReporter builds the agentrun.ToolCallReporter an agent stage passes -// to tc.Agent.Run, so every completed tool call during that run surfaces as a -// tool_call event on the task timeline. Safe to call on a nil Bus: Emit no-ops. -func (tc *TaskContext) toolCallReporter(ctx context.Context, stage string) agentrun.ToolCallReporter { - return func(report agentrun.ToolCallReport) { - tc.Emit(ctx, events.KindToolCall, stage, report.Detail, map[string]any{ - "tool": report.Tool, - "failed": report.Failed, - }) +// reporter builds the agentrun.Reporter an agent stage passes to +// tc.Agent.Run: every completed tool call surfaces as a tool_call event on the +// task timeline, and every transcript entry is persisted as an +// agent_transcript event so it outlives the container. A transcript entry +// that cannot be persisted is logged and dropped; it never fails the run. +func (tc *TaskContext) reporter(ctx context.Context, stage string) agentrun.Reporter { + return agentrun.Reporter{ + ToolCall: func(report agentrun.ToolCallReport) { + tc.Emit(ctx, events.KindToolCall, stage, report.Detail, map[string]any{ + "tool": report.Tool, + "failed": report.Failed, + }) + }, + Transcript: func(entry agentrun.TranscriptEntry) { + data, err := transcriptData(entry) + if err == nil { + err = tc.EmitDurable(ctx, events.KindAgentTranscript, stage, entry.Kind, data) + } + if err != nil && tc.Log != nil { + tc.Log.WarnContext(ctx, "agent transcript entry not recorded", + "task", tc.Task.ID, "stage", stage, "seq", entry.Seq, "err", err) + } + }, } } +func transcriptData(entry agentrun.TranscriptEntry) (map[string]any, error) { + encoded, err := json.Marshal(entry) + if err != nil { + return nil, err + } + var data map[string]any + return data, json.Unmarshal(encoded, &data) +} + // Outcome is a stage's terminal decision for the whole workflow. Stages // that don't end the workflow leave it zero. type Outcome struct { diff --git a/internal/events/events.go b/internal/events/events.go index b0f1bff16..3e9c7c552 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -22,8 +22,12 @@ const ( KindAgentFinish = "agent_finish" // data: status, stop_reason, tokens, iterations, model, prompt_tokens, completion_tokens, cached_tokens, cache_creation_tokens // KindToolCall marks one completed tool invocation during an agent stage // run. Only emitted for runners that execute in the same process as their - // tools; see agentexec.ToolCallReporter. + // tools; see agentrun.Reporter. KindToolCall = "tool_call" // data: tool, failed + // KindAgentTranscript is one entry of an agent run's conversation: a + // prompt, a model response, a tool call or a compaction. detail: the + // entry kind; data: agentrun.TranscriptEntry's fields. + KindAgentTranscript = "agent_transcript" // KindDeliveryRecorded records one outward delivery a test run did not // perform: a forge write, a push, an HTTP request, a central tool call. // detail: what it would have done, phrased "would ...". diff --git a/internal/infrastructure/postgres/events.go b/internal/infrastructure/postgres/events.go index 38f75a066..70411ca17 100644 --- a/internal/infrastructure/postgres/events.go +++ b/internal/infrastructure/postgres/events.go @@ -90,7 +90,17 @@ func eventsFromRows(rows []postgresdb.Event) []events.Event { return out } -// TaskEvents returns a task's full timeline, oldest first. +// TaskTranscript returns one attempt's agent transcript entries, oldest first. +func (s *Store) TaskTranscript(ctx context.Context, taskID int64, attempt int) ([]events.Event, error) { + rows, err := s.queries().TaskTranscript(ctx, postgresdb.TaskTranscriptParams{TaskID: taskID, Attempt: int64(attempt), ScopeOrg: scopeOrg(ctx)}) + if err != nil { + return nil, err + } + return eventsFromRows(rows), nil +} + +// TaskEvents returns a task's timeline without agent transcripts, oldest +// first. func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) { rows, err := s.queries().TaskEventsByID(ctx, postgresdb.TaskEventsByIDParams{TaskID: taskID, ScopeOrg: scopeOrg(ctx)}) if err != nil { diff --git a/internal/infrastructure/postgres/postgresdb/events.sql.go b/internal/infrastructure/postgres/postgresdb/events.sql.go index d5ff8d36b..4e7c929b3 100644 --- a/internal/infrastructure/postgres/postgresdb/events.sql.go +++ b/internal/infrastructure/postgres/postgresdb/events.sql.go @@ -228,7 +228,7 @@ func (q *Queries) StageStats(ctx context.Context, scopeOrg pgtype.Text) ([]Stage const taskEventsByID = `-- name: TaskEventsByID :many SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id FROM events -WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) +WHERE task_id = $1 AND kind <> 'agent_transcript' AND ($2::text IS NULL OR org_id = $2) ORDER BY id ` @@ -274,6 +274,56 @@ func (q *Queries) TaskEventsByID(ctx context.Context, arg TaskEventsByIDParams) return items, nil } +const taskTranscript = `-- name: TaskTranscript :many +SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id +FROM events +WHERE task_id = $1 AND attempt = $2 AND kind = 'agent_transcript' AND ($3::text IS NULL OR org_id = $3) +ORDER BY id +` + +type TaskTranscriptParams struct { + TaskID int64 + Attempt int64 + ScopeOrg pgtype.Text +} + +func (q *Queries) TaskTranscript(ctx context.Context, arg TaskTranscriptParams) ([]Event, error) { + rows, err := q.db.Query(ctx, taskTranscript, arg.TaskID, arg.Attempt, arg.ScopeOrg) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Event + for rows.Next() { + var i Event + if err := rows.Scan( + &i.ID, + &i.At, + &i.Kind, + &i.TaskID, + &i.Repo, + &i.Issue, + &i.Workflow, + &i.Stage, + &i.Attempt, + &i.ActorID, + &i.ActorKind, + &i.PrincipalID, + &i.Detail, + &i.Data, + &i.OrgID, + &i.WorkspaceID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const tokensByDay = `-- name: TokensByDay :many WITH event_totals AS ( SELECT task_id, to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day, diff --git a/internal/infrastructure/postgres/queries/events.sql b/internal/infrastructure/postgres/queries/events.sql index 35bceff11..aec052ea5 100644 --- a/internal/infrastructure/postgres/queries/events.sql +++ b/internal/infrastructure/postgres/queries/events.sql @@ -35,7 +35,13 @@ LIMIT sqlc.arg(page_limit); -- name: TaskEventsByID :many SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id FROM events -WHERE task_id = sqlc.arg(task_id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) +WHERE task_id = sqlc.arg(task_id) AND kind <> 'agent_transcript' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) +ORDER BY id; + +-- name: TaskTranscript :many +SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id +FROM events +WHERE task_id = sqlc.arg(task_id) AND attempt = sqlc.arg(attempt) AND kind = 'agent_transcript' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY id; -- name: WorkflowStats :many diff --git a/internal/infrastructure/staterpc/task_events.go b/internal/infrastructure/staterpc/task_events.go index 27e33b113..a2596b101 100644 --- a/internal/infrastructure/staterpc/task_events.go +++ b/internal/infrastructure/staterpc/task_events.go @@ -29,7 +29,13 @@ func (s *taskEventServer) EventsSince(ctx context.Context, r *pb.EventsSinceRequ } func (s *taskEventServer) TaskEvents(ctx context.Context, r *pb.TaskEventsRequest) (*pb.TaskEventsResponse, error) { - evs, err := s.deps.Tasks.TaskEvents(ctx, r.TaskId) + var evs []events.Event + var err error + if r.Transcript { + evs, err = s.deps.Tasks.TaskTranscript(ctx, r.TaskId, int(r.Attempt)) + } else { + evs, err = s.deps.Tasks.TaskEvents(ctx, r.TaskId) + } if err != nil { return nil, s.logErr("TaskEvents", err) } @@ -59,3 +65,11 @@ func (c *Client) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, } return mapValues(r.Events, eventValue), nil } + +func (c *Client) TaskTranscript(ctx context.Context, taskID int64, attempt int) ([]events.Event, error) { + r, err := c.taskEvent.TaskEvents(ctx, &pb.TaskEventsRequest{TaskId: taskID, Transcript: true, Attempt: int64(attempt)}) + if err != nil { + return nil, unmapError(err) + } + return mapValues(r.Events, eventValue), nil +} diff --git a/internal/webhookguard/redact.go b/internal/webhookguard/redact.go index e76134c6f..181fc41ff 100644 --- a/internal/webhookguard/redact.go +++ b/internal/webhookguard/redact.go @@ -52,6 +52,20 @@ func RedactString(value string) string { return value } +// pemPrivateKeyRe matches a PEM private-key block, header to footer. +var pemPrivateKeyRe = regexp.MustCompile(`-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----`) + +// RedactText replaces each secret-shaped span inside free text, keeping the +// rest, for long text such as tool output where blanking the whole value would +// hide everything around the secret. +func RedactText(value string) string { + if len(value) < 20 { + return value + } + value = jwtTokenRe.ReplaceAllString(value, redactedValue) + return pemPrivateKeyRe.ReplaceAllString(value, redactedValue) +} + func redactValue(value any) any { switch typed := value.(type) { case map[string]any: diff --git a/internal/webui/api_tasks_transcript.go b/internal/webui/api_tasks_transcript.go new file mode 100644 index 000000000..c57f498c4 --- /dev/null +++ b/internal/webui/api_tasks_transcript.go @@ -0,0 +1,31 @@ +package webui + +import ( + "net/http" + + "github.com/samcharles93/archie-core/internal/events" +) + +// taskTranscriptView is GET /api/tasks/{id}/transcript: one attempt's agent +// transcript entries, oldest first. +type taskTranscriptView struct { + TaskID int64 `json:"task_id"` + Attempt int `json:"attempt"` + Entries []events.Event `json:"entries"` +} + +func (s *Server) handleTaskTranscript(w http.ResponseWriter, r *http.Request) { + t, attempt, ok := s.taskAttemptTarget(w, r) + if !ok { + return + } + entries, err := s.Store.TaskTranscript(r.Context(), t.ID, attempt) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if entries == nil { + entries = []events.Event{} + } + writeJSON(w, taskTranscriptView{TaskID: t.ID, Attempt: attempt, Entries: entries}) +} diff --git a/internal/webui/authorization_surfaces_test.go b/internal/webui/authorization_surfaces_test.go index 2af5f28a2..84b7dcde2 100644 --- a/internal/webui/authorization_surfaces_test.go +++ b/internal/webui/authorization_surfaces_test.go @@ -86,6 +86,7 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { {"GET", "/api/tasks/7/logs"}, {"GET", "/api/tasks/7/logs/download"}, {"GET", "/api/tasks/7/sandbox"}, + {"GET", "/api/tasks/7/transcript"}, {"GET", "/api/tokens"}, {"GET", "/api/version"}, {"GET", "/api/workflows"}, diff --git a/internal/webui/server.go b/internal/webui/server.go index 85b21531e..c824b6550 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -320,6 +320,7 @@ func (s *Server) registerTaskRoutes(mux *http.ServeMux) { mux.HandleFunc("GET /api/tasks/{id}/changes", s.handleTaskChanges) mux.HandleFunc("GET /api/tasks/{id}/debug", s.handleTaskDebug) mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs) + mux.HandleFunc("GET /api/tasks/{id}/transcript", s.handleTaskTranscript) mux.HandleFunc("GET /api/tasks/{id}/sandbox", s.handleTaskSandbox) mux.HandleFunc("GET /api/tasks/{id}/logs/download", s.handleTaskLogDownload) } diff --git a/proto/state/v1/task_event.proto b/proto/state/v1/task_event.proto index 7f851e938..01b8fcd42 100644 --- a/proto/state/v1/task_event.proto +++ b/proto/state/v1/task_event.proto @@ -22,5 +22,11 @@ message EventsSinceRequest { int64 limit = 2; } message EventsSinceResponse { repeated Event events = 1; } -message TaskEventsRequest { int64 task_id = 1; } +message TaskEventsRequest { + int64 task_id = 1; + // transcript selects the agent_transcript entries of one attempt instead of + // the timeline, which leaves them out. + bool transcript = 2; + int64 attempt = 3; +} message TaskEventsResponse { repeated Event events = 1; } diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index ff55434fe..31eee7e7e 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -275,6 +275,8 @@ export const api = { request(`/api/tasks/${encodeURIComponent(id)}/changes` + qs(params)), taskDebug: (id: string, params?: QueryParams) => request(`/api/tasks/${encodeURIComponent(id)}/debug` + qs(params)), + taskTranscript: (id: string, params?: QueryParams) => + request(`/api/tasks/${encodeURIComponent(id)}/transcript` + qs(params)), // taskAction posts an operator action. A review-gate answer may carry a // payload: the instructions a rereview requires, and the finding keys an // approve posts (an absent selection means every offered finding). A retry diff --git a/ui/src/workflows/AgentTranscript.vue b/ui/src/workflows/AgentTranscript.vue new file mode 100644 index 000000000..687d0acf7 --- /dev/null +++ b/ui/src/workflows/AgentTranscript.vue @@ -0,0 +1,200 @@ + + + diff --git a/ui/src/workflows/StepRun.vue b/ui/src/workflows/StepRun.vue index 4adc1631b..3499b210a 100644 --- a/ui/src/workflows/StepRun.vue +++ b/ui/src/workflows/StepRun.vue @@ -7,6 +7,7 @@ import { useLiveResource } from "@/stores/session"; import { Badge } from "@/components/ui/badge"; import ChangeCapture from "@/tasks/ChangeCapture.vue"; import TaskLogs from "@/tasks/TaskLogs.vue"; +import AgentTranscript from "./AgentTranscript.vue"; import { stageStatusMeta } from "@/tasks/stage-rail"; import { describeTimelineEvent, duration } from "@/tasks/timeline-event"; import type { ChangesState, LogState, TaskEvent, TaskRecord } from "@/tasks/task-run"; @@ -19,6 +20,7 @@ const logs = ref(); const activityError = ref(""); const changesError = ref(""); const expanded = ref(false); +const transcriptOpen = ref(false); const key = computed(() => `${props.task.id}/${props.attempt}/${props.stage.id ?? props.stage.name}`); let generation = 0; let timer: ReturnType | undefined; @@ -58,6 +60,7 @@ watch(key, () => { activityError.value = ""; changesError.value = ""; expanded.value = false; + transcriptOpen.value = false; void refresh(); }, { immediate: true }); watch(() => props.stage.status, (status) => { @@ -77,11 +80,12 @@ function inWindow(at: string | number | Date | undefined): boolean { const time = new Date(at).getTime(); return time >= Date.parse(props.stage.started_at) && (!props.stage.finished_at || time <= Date.parse(props.stage.finished_at)) && (!nextStart.value || time < Date.parse(nextStart.value)); } -const activity = computed(() => events.value.filter((event) => { +function inStep(event: TaskEvent): boolean { if (Number(event.attempt) !== props.attempt) return false; if (event.data?.step_id != null) return Number(event.data.step_id) === props.stage.id; return names.value.has(event.stage ?? "") && inWindow(event.at); -}).sort((a, b) => String(a.at ?? "").localeCompare(String(b.at ?? "")))); +} +const activity = computed(() => events.value.filter(inStep).sort((a, b) => String(a.at ?? "").localeCompare(String(b.at ?? "")))); const recorded = computed(() => activity.value.filter((event) => event.kind === "delivery_recorded")); const groups = computed(() => { const agents = props.stage.agents ?? []; @@ -130,6 +134,10 @@ const scopedLogs = computed(() => logs.value ? { ...logs.value, entries: (logs.v

{{ group.reports.map((report) => describeTimelineEvent(report).detail).filter(Boolean).join(' · ') }}

{{ group.agents.reduce((sum, agent) => sum + agent.tokens_used, 0).toLocaleString() }} tokens recorded

+
+ + +

Not delivered in a test run

    diff --git a/ui/src/workflows/TranscriptTool.vue b/ui/src/workflows/TranscriptTool.vue new file mode 100644 index 000000000..e0611a83d --- /dev/null +++ b/ui/src/workflows/TranscriptTool.vue @@ -0,0 +1,119 @@ + + + diff --git a/ui/src/workflows/transcript.ts b/ui/src/workflows/transcript.ts new file mode 100644 index 000000000..d49970cce --- /dev/null +++ b/ui/src/workflows/transcript.ts @@ -0,0 +1,26 @@ +/** One agent_transcript entry, as the State Store records it. */ +export interface TranscriptEntry { + seq: number; + kind: string; + text?: string; + text_bytes?: number; + reasoning?: string; + reasoning_bytes?: number; + tool_calls?: { id?: string; name: string }[]; + call_id?: string; + tool?: string; + args?: string; + args_bytes?: number; + output?: string; + output_bytes?: number; + error?: string; + finish_reason?: string; + usage?: { prompt_tokens?: number; completion_tokens?: number; cached_tokens?: number }; + latency_ms?: number; +} + +/** The text as recorded, with a note when it was clipped. */ +export function withClip(text: string | undefined, bytes: number | undefined): string { + if (!bytes) return text ?? ""; + return `${text ?? ""}\n… clipped, ${(bytes / 1024).toFixed(1)} KB in full`; +}