feat(agentexec): record each agent run's transcript and show it per step (#1718) #1724
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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" +
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 ...".
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
@@ -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"},
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
@@ -275,6 +275,8 @@ export const api = {
|
||||
request<T>(`/api/tasks/${encodeURIComponent(id)}/changes` + qs(params)),
|
||||
taskDebug: <T = unknown>(id: string, params?: QueryParams) =>
|
||||
request<T>(`/api/tasks/${encodeURIComponent(id)}/debug` + qs(params)),
|
||||
taskTranscript: <T = unknown>(id: string, params?: QueryParams) =>
|
||||
request<T>(`/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
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
<script setup lang="ts">
|
||||
import { BotIcon, BrainIcon, CircleAlertIcon, FoldVerticalIcon, ScrollTextIcon, SparklesIcon, TargetIcon } from "@lucide/vue";
|
||||
import { createReusableTemplate } from "@vueuse/core";
|
||||
import { computed, onUnmounted, ref, watch } from "vue";
|
||||
|
||||
import { Bubble, BubbleContent } from "@/components/ui/bubble";
|
||||
import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block";
|
||||
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
||||
import { Message, MessageContent } from "@/components/ui/message";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import {
|
||||
Trace,
|
||||
TraceCollapsible,
|
||||
TraceCollapsibleContent,
|
||||
TraceCollapsibleTrigger,
|
||||
TraceDescription,
|
||||
TraceIcon,
|
||||
TraceLine,
|
||||
TraceTitle,
|
||||
} from "@/components/ui/trace";
|
||||
import ChatMarkdown from "@/chat/ChatMarkdown.vue";
|
||||
import { api } from "@/lib/api";
|
||||
import { useLiveResource } from "@/stores/session";
|
||||
import { duration } from "@/tasks/timeline-event";
|
||||
import type { TaskEvent } from "@/tasks/task-run";
|
||||
import TranscriptTool from "./TranscriptTool.vue";
|
||||
import { withClip, type TranscriptEntry as Entry } from "./transcript";
|
||||
|
||||
/** One step's agent conversations for one attempt, oldest first. */
|
||||
const props = defineProps<{ taskId: number | string; attempt: number; match: (event: TaskEvent) => boolean; running: boolean }>();
|
||||
|
||||
interface Step { model: Entry; tools: Entry[] }
|
||||
type Row =
|
||||
| { kind: "prompt"; title: string; entry: Entry }
|
||||
| { kind: "user"; entry: Entry }
|
||||
| { kind: "compaction"; entry: Entry }
|
||||
| { kind: "step"; number: number; step: Step };
|
||||
interface Conversation { key: string; label: string; rows: Row[]; steps: number; tools: number; failed: number }
|
||||
|
||||
const [DefineConversation, ConversationView] = createReusableTemplate<{ conversation: Conversation }>();
|
||||
|
||||
const events = ref<TaskEvent[]>();
|
||||
const failed = ref(false);
|
||||
let generation = 0;
|
||||
let timer: ReturnType<typeof setInterval> | undefined;
|
||||
|
||||
async function load(): Promise<void> {
|
||||
const current = ++generation;
|
||||
try {
|
||||
const view = await api.taskTranscript<{ entries?: TaskEvent[] }>(String(props.taskId), { attempt: props.attempt });
|
||||
if (current !== generation) return;
|
||||
events.value = view.entries ?? [];
|
||||
failed.value = false;
|
||||
} catch {
|
||||
if (current === generation) failed.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
watch(() => [props.taskId, props.attempt], () => { events.value = undefined; void load(); }, { immediate: true });
|
||||
watch(() => props.running, (running) => {
|
||||
clearInterval(timer);
|
||||
if (running) timer = setInterval(() => void load(), 3000);
|
||||
}, { immediate: true });
|
||||
useLiveResource("tasks", () => void load(), 1000);
|
||||
onUnmounted(() => { generation++; clearInterval(timer); });
|
||||
|
||||
function usage(entry: Entry): string {
|
||||
const u = entry.usage;
|
||||
const parts = [];
|
||||
if (u?.prompt_tokens) parts.push(`${u.prompt_tokens.toLocaleString()} in`);
|
||||
if (u?.cached_tokens) parts.push(`${u.cached_tokens.toLocaleString()} cached`);
|
||||
if (u?.completion_tokens) parts.push(`${u.completion_tokens.toLocaleString()} out`);
|
||||
if (entry.latency_ms) parts.push(duration(entry.latency_ms));
|
||||
return parts.join(" · ");
|
||||
}
|
||||
|
||||
function plural(n: number, word: string): string {
|
||||
return `${n} ${word}${n === 1 ? "" : "s"}`;
|
||||
}
|
||||
|
||||
// Tool results sit under the step whose response asked for them.
|
||||
function conversation(key: string, label: string, entries: Entry[]): Conversation {
|
||||
const rows: Row[] = [];
|
||||
const owner = new Map<string, Step>();
|
||||
let prompted = false;
|
||||
let steps = 0;
|
||||
for (const entry of entries) {
|
||||
if (entry.kind === "system") rows.push({ kind: "prompt", title: "System prompt", entry });
|
||||
else if (entry.kind === "user") {
|
||||
rows.push(prompted ? { kind: "user", entry } : { kind: "prompt", title: "Mission", entry });
|
||||
prompted = true;
|
||||
} else if (entry.kind === "compaction") rows.push({ kind: "compaction", entry });
|
||||
else if (entry.kind === "model") {
|
||||
const step: Step = { model: entry, tools: [] };
|
||||
for (const call of entry.tool_calls ?? []) if (call.id) owner.set(call.id, step);
|
||||
rows.push({ kind: "step", number: ++steps, step });
|
||||
} else if (entry.kind === "tool") {
|
||||
const step = entry.call_id ? owner.get(entry.call_id) : undefined;
|
||||
if (step) step.tools.push(entry);
|
||||
else rows.push({ kind: "step", number: ++steps, step: { model: { seq: entry.seq, kind: "model" }, tools: [entry] } });
|
||||
}
|
||||
}
|
||||
const tools = entries.filter((entry) => entry.kind === "tool");
|
||||
return { key, label, rows, steps, tools: tools.length, failed: tools.filter((tool) => tool.error).length };
|
||||
}
|
||||
|
||||
// Each agent in the step (a fan-out branch, or a retry) keeps its own
|
||||
// conversation, numbered from 1.
|
||||
const conversations = computed(() => {
|
||||
const byAgent = new Map<string, Entry[][]>();
|
||||
for (const event of (events.value ?? []).filter(props.match)) {
|
||||
const key = [event.data?.branch, event.stage].filter(Boolean).join("/");
|
||||
const entry = event.data as unknown as Entry;
|
||||
const runs = byAgent.get(key) ?? [];
|
||||
if (!runs.length || entry.seq === 1) runs.push([]);
|
||||
runs.at(-1)!.push(entry);
|
||||
byAgent.set(key, runs);
|
||||
}
|
||||
return [...byAgent.entries()].flatMap(([label, runs]) => runs.map((entries, i) => conversation(`${label}#${i}`, label, entries)));
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DefineConversation v-slot="{ conversation: c }">
|
||||
<div class="flex flex-col gap-4">
|
||||
<template v-for="row in c.rows" :key="`${row.kind}-${row.kind === 'step' ? row.number : row.entry.seq}`">
|
||||
<Trace v-if="row.kind === 'prompt' || row.kind === 'compaction'">
|
||||
<TraceCollapsible>
|
||||
<TraceCollapsibleTrigger>
|
||||
<TraceLine>
|
||||
<TraceIcon>
|
||||
<FoldVerticalIcon v-if="row.kind === 'compaction'" />
|
||||
<TargetIcon v-else-if="row.title === 'Mission'" />
|
||||
<ScrollTextIcon v-else />
|
||||
</TraceIcon>
|
||||
<TraceTitle>{{ row.kind === 'compaction' ? 'Compacted the history' : row.title }}</TraceTitle>
|
||||
<TraceDescription v-if="row.kind === 'compaction' && usage(row.entry)">{{ usage(row.entry) }}</TraceDescription>
|
||||
</TraceLine>
|
||||
</TraceCollapsibleTrigger>
|
||||
<TraceCollapsibleContent>
|
||||
<CodeBlock size="sm">
|
||||
<CodeBlockContent wrap class="max-h-96">{{ withClip(row.entry.text || row.entry.error, row.entry.text_bytes) }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
</TraceCollapsibleContent>
|
||||
</TraceCollapsible>
|
||||
</Trace>
|
||||
<Message v-else-if="row.kind === 'user'" align="end">
|
||||
<MessageContent>
|
||||
<Bubble align="end" variant="muted">
|
||||
<BubbleContent class="whitespace-pre-wrap [overflow-wrap:anywhere]">{{ withClip(row.entry.text, row.entry.text_bytes) }}</BubbleContent>
|
||||
</Bubble>
|
||||
</MessageContent>
|
||||
</Message>
|
||||
<Message v-else>
|
||||
<MessageContent class="gap-2">
|
||||
<Trace v-if="row.step.model.reasoning">
|
||||
<TraceCollapsible>
|
||||
<TraceCollapsibleTrigger>
|
||||
<TraceLine>
|
||||
<TraceIcon><BrainIcon /></TraceIcon>
|
||||
<TraceTitle>Thought</TraceTitle>
|
||||
</TraceLine>
|
||||
</TraceCollapsibleTrigger>
|
||||
<TraceCollapsibleContent class="whitespace-pre-wrap">{{ withClip(row.step.model.reasoning, row.step.model.reasoning_bytes) }}</TraceCollapsibleContent>
|
||||
</TraceCollapsible>
|
||||
</Trace>
|
||||
<ChatMarkdown v-if="row.step.model.text" :text="withClip(row.step.model.text, row.step.model.text_bytes)" />
|
||||
<Trace>
|
||||
<TraceLine :status="row.step.model.error ? 'error' : undefined">
|
||||
<TraceIcon><CircleAlertIcon v-if="row.step.model.error" /><SparklesIcon v-else /></TraceIcon>
|
||||
<TraceTitle>{{ row.step.model.error ? `Step ${row.number} failed: ${row.step.model.error}` : `Step ${row.number}` }}</TraceTitle>
|
||||
<TraceDescription v-if="usage(row.step.model)">{{ usage(row.step.model) }}</TraceDescription>
|
||||
</TraceLine>
|
||||
<TranscriptTool v-for="tool in row.step.tools" :key="tool.seq" :tool="tool" />
|
||||
</Trace>
|
||||
</MessageContent>
|
||||
</Message>
|
||||
</template>
|
||||
</div>
|
||||
</DefineConversation>
|
||||
|
||||
<p v-if="failed" role="alert" class="text-danger">Could not load the transcript</p>
|
||||
<Spinner v-else-if="!events" />
|
||||
<Empty v-else-if="!conversations.length"><EmptyHeader><EmptyTitle>No transcript recorded</EmptyTitle></EmptyHeader></Empty>
|
||||
<ConversationView v-else-if="conversations.length === 1" :conversation="conversations[0]!" />
|
||||
<Trace v-else>
|
||||
<TraceCollapsible v-for="c in conversations" :key="c.key">
|
||||
<TraceCollapsibleTrigger>
|
||||
<TraceLine :status="c.failed ? 'error' : undefined">
|
||||
<TraceIcon><BotIcon /></TraceIcon>
|
||||
<TraceTitle class="font-mono">{{ c.label }}</TraceTitle>
|
||||
<TraceDescription>{{ plural(c.steps, 'step') }} · {{ plural(c.tools, 'tool call') }}{{ c.failed ? `, ${c.failed} failed` : '' }}</TraceDescription>
|
||||
</TraceLine>
|
||||
</TraceCollapsibleTrigger>
|
||||
<TraceCollapsibleContent>
|
||||
<ConversationView :conversation="c" />
|
||||
</TraceCollapsibleContent>
|
||||
</TraceCollapsible>
|
||||
</Trace>
|
||||
</template>
|
||||
@@ -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<LogState | null>();
|
||||
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<typeof setInterval> | 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
|
||||
<p v-if="group.reports.length" class="text-xs text-fg-muted">{{ group.reports.map((report) => describeTimelineEvent(report).detail).filter(Boolean).join(' · ') }}</p>
|
||||
<p v-else-if="group.agents.length" class="text-xs text-fg-muted">{{ group.agents.reduce((sum, agent) => sum + agent.tokens_used, 0).toLocaleString() }} tokens recorded</p>
|
||||
</section>
|
||||
<section v-if="stage.agents?.length || groups.some((group) => group.reports.length)" class="space-y-3">
|
||||
<Button type="button" variant="link" size="xs" class="px-0 text-caption font-medium uppercase text-fg-muted" :aria-expanded="transcriptOpen" @click="transcriptOpen = !transcriptOpen">Transcript</Button>
|
||||
<AgentTranscript v-if="transcriptOpen" :task-id="task.id" :attempt="attempt" :match="inStep" :running="stage.status === 'running'" />
|
||||
</section>
|
||||
<section v-if="recorded.length" class="space-y-1">
|
||||
<h3 class="text-caption font-medium uppercase text-fg-muted">Not delivered in a test run</h3>
|
||||
<ul class="divide-y divide-border rounded-md border border-border">
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
<script setup lang="ts">
|
||||
import { ArrowUpRightIcon, CircleAlertIcon, CircleCheckIcon, FlagIcon, FlaskConicalIcon, GlobeIcon, SquareTerminalIcon } from "@lucide/vue";
|
||||
import { computed } from "vue";
|
||||
|
||||
import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block";
|
||||
import {
|
||||
Trace,
|
||||
TraceCollapsible,
|
||||
TraceCollapsibleContent,
|
||||
TraceCollapsibleTrigger,
|
||||
TraceDescription,
|
||||
TraceIcon,
|
||||
TraceLine,
|
||||
TraceTitle,
|
||||
} from "@/components/ui/trace";
|
||||
import { duration } from "@/tasks/timeline-event";
|
||||
import { withClip, type TranscriptEntry } from "./transcript";
|
||||
|
||||
/**
|
||||
* One tool call, drawn as the Trace pattern its tool calls for: a shell
|
||||
* command as a terminal, the gate as a test run, a fetch as a source, finish
|
||||
* as the run's own verdict, anything else as input then output.
|
||||
*/
|
||||
const props = defineProps<{ tool: TranscriptEntry }>();
|
||||
|
||||
const args = computed<Record<string, unknown>>(() => {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(props.tool.args ?? "");
|
||||
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed as Record<string, unknown> : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
});
|
||||
const failed = computed(() => !!props.tool.error);
|
||||
const kind = computed(() => {
|
||||
switch (props.tool.tool) {
|
||||
case "shell": return "terminal";
|
||||
case "check": return "tests";
|
||||
case "web_fetch": return "source";
|
||||
case "finish": return "finish";
|
||||
default: return "tool";
|
||||
}
|
||||
});
|
||||
const command = computed(() => String(args.value.command ?? ""));
|
||||
const url = computed(() => String(args.value.url ?? ""));
|
||||
const host = computed(() => {
|
||||
try {
|
||||
return new URL(url.value).hostname;
|
||||
} catch {
|
||||
return url.value;
|
||||
}
|
||||
});
|
||||
// The row names a plain call by its main value, as "read src/main.go" rather
|
||||
// than its JSON: the first string argument.
|
||||
const argument = computed(() => {
|
||||
const first = Object.values(args.value).find((v) => typeof v === "string" && v);
|
||||
return typeof first === "string" ? first.split("\n", 1)[0] : "";
|
||||
});
|
||||
const title = computed(() => {
|
||||
switch (kind.value) {
|
||||
case "terminal": return `${failed.value ? "Failed to run" : "Ran"} ${command.value.split("\n", 1)[0]}`;
|
||||
case "tests": return failed.value ? "Gate failed" : "Ran the gate";
|
||||
case "source": return `${failed.value ? "Failed to read" : "Read"} ${host.value}`;
|
||||
case "finish": return `Finished as ${String(args.value.status ?? "unknown")}`;
|
||||
default: return `${failed.value ? "Failed to run" : "Ran"} ${props.tool.tool} ${argument.value}`;
|
||||
}
|
||||
});
|
||||
const output = computed(() => withClip(props.tool.output, props.tool.output_bytes));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<TraceCollapsible :default-open="failed">
|
||||
<TraceCollapsibleTrigger>
|
||||
<TraceLine :status="failed ? 'error' : undefined">
|
||||
<TraceIcon>
|
||||
<CircleAlertIcon v-if="failed" />
|
||||
<SquareTerminalIcon v-else-if="kind === 'terminal'" />
|
||||
<FlaskConicalIcon v-else-if="kind === 'tests'" />
|
||||
<GlobeIcon v-else-if="kind === 'source'" />
|
||||
<FlagIcon v-else-if="kind === 'finish'" />
|
||||
<CircleCheckIcon v-else />
|
||||
</TraceIcon>
|
||||
<TraceTitle :class="kind === 'terminal' && 'font-mono'">{{ title }}</TraceTitle>
|
||||
<TraceDescription v-if="tool.latency_ms">{{ duration(tool.latency_ms) }}</TraceDescription>
|
||||
</TraceLine>
|
||||
</TraceCollapsibleTrigger>
|
||||
<TraceCollapsibleContent class="flex flex-col gap-2">
|
||||
<template v-if="kind === 'terminal'">
|
||||
<CodeBlock size="sm" :variant="failed ? 'destructive' : 'default'">
|
||||
<CodeBlockContent wrap class="max-h-64 font-mono">$ {{ command }}
|
||||
{{ output }}{{ tool.error ? `\n${tool.error}` : '' }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
</template>
|
||||
<template v-else-if="kind === 'finish'">
|
||||
<p class="whitespace-pre-wrap">{{ String(args.summary ?? '') }}</p>
|
||||
</template>
|
||||
<template v-else>
|
||||
<Trace v-if="kind === 'source' && url">
|
||||
<a :href="url" target="_blank" rel="noreferrer">
|
||||
<TraceLine>
|
||||
<TraceIcon><GlobeIcon /></TraceIcon>
|
||||
<TraceTitle>{{ url }}<span class="sr-only"> (opens in a new tab)</span></TraceTitle>
|
||||
<TraceIcon><ArrowUpRightIcon /></TraceIcon>
|
||||
</TraceLine>
|
||||
</a>
|
||||
</Trace>
|
||||
<CodeBlock v-if="kind === 'tool'" size="sm">
|
||||
<CodeBlockContent wrap class="max-h-48">{{ withClip(tool.args, tool.args_bytes) }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
<CodeBlock v-if="tool.error" size="sm" variant="destructive">
|
||||
<CodeBlockContent wrap class="max-h-64">{{ tool.error }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
<CodeBlock v-if="tool.output || !tool.error" size="sm">
|
||||
<CodeBlockContent wrap class="max-h-64">{{ output }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
</template>
|
||||
</TraceCollapsibleContent>
|
||||
</TraceCollapsible>
|
||||
</template>
|
||||
@@ -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`;
|
||||
}
|
||||
Reference in new issue
Block a user