feat(agentexec): record each agent run's transcript and show it per step (#1718)
The loop runner hooks every model call and tool call and reports ordered,
redacted, clipped entries. Each is persisted as an agent_transcript task
event through InsertEvent, left out of the timeline read, and served per
attempt at /api/tasks/{id}/transcript. The step run view shows it with Trace.
This commit is contained in:
35 files changed
+791
-68
No files matched your search
@@ -0,0 +1,218 @@
|
||||
<script setup lang="ts">
|
||||
import { CircleAlertIcon, CircleCheckIcon, FoldVerticalIcon, ScrollTextIcon, SparklesIcon, TargetIcon } from "@lucide/vue";
|
||||
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";
|
||||
|
||||
/** 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 Usage { prompt_tokens?: number; completion_tokens?: number; cached_tokens?: number }
|
||||
interface Entry {
|
||||
seq: number;
|
||||
kind: string;
|
||||
text?: string;
|
||||
text_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?: Usage;
|
||||
latency_ms?: number;
|
||||
}
|
||||
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 };
|
||||
|
||||
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 withClip(text: string | undefined, bytes: number | undefined): string {
|
||||
if (!bytes) return text ?? "";
|
||||
return `${text ?? ""}\n… clipped, ${(bytes / 1024).toFixed(1)} KB in full`;
|
||||
}
|
||||
|
||||
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(" · ");
|
||||
}
|
||||
|
||||
// The row names the call by its main value, as "read src/main.go" rather than
|
||||
// its JSON: the first string argument, else nothing.
|
||||
function argument(entry: Entry): string {
|
||||
try {
|
||||
const args: unknown = JSON.parse(entry.args ?? "");
|
||||
if (args && typeof args === "object" && !Array.isArray(args)) {
|
||||
const first = Object.values(args).find((v) => typeof v === "string" && v);
|
||||
if (typeof first === "string") return first.split("\n", 1)[0] ?? "";
|
||||
}
|
||||
} catch {
|
||||
// Clipped or not JSON.
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Tool results sit under the step whose response asked for them.
|
||||
function rows(entries: Entry[]): Row[] {
|
||||
const out: Row[] = [];
|
||||
const owner = new Map<string, Step>();
|
||||
let prompted = false;
|
||||
let steps = 0;
|
||||
for (const entry of entries) {
|
||||
if (entry.kind === "system") out.push({ kind: "prompt", title: "System prompt", entry });
|
||||
else if (entry.kind === "user") {
|
||||
out.push(prompted ? { kind: "user", entry } : { kind: "prompt", title: "Mission", entry });
|
||||
prompted = true;
|
||||
} else if (entry.kind === "compaction") out.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);
|
||||
out.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 out.push({ kind: "step", number: ++steps, step: { model: { seq: entry.seq, kind: "model" }, tools: [entry] } });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A step can hold several agents (parallel branches, retries); each 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) => ({ key: `${label}#${i}`, label, rows: rows(entries) })));
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<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>
|
||||
<div v-for="conversation in conversations" :key="conversation.key" class="flex flex-col gap-4">
|
||||
<h4 v-if="conversations.length > 1" class="font-mono text-xs">{{ conversation.label }}</h4>
|
||||
<template v-for="row in conversation.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">
|
||||
<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>
|
||||
<TraceCollapsible v-for="tool in row.step.tools" :key="tool.seq" :default-open="!!tool.error">
|
||||
<TraceCollapsibleTrigger>
|
||||
<TraceLine :status="tool.error ? 'error' : undefined">
|
||||
<TraceIcon><CircleAlertIcon v-if="tool.error" /><CircleCheckIcon v-else /></TraceIcon>
|
||||
<TraceTitle>{{ tool.error ? 'Failed to run' : 'Ran' }} <span class="font-mono">{{ tool.tool }}</span> {{ argument(tool) }}</TraceTitle>
|
||||
<TraceDescription v-if="tool.latency_ms">{{ duration(tool.latency_ms) }}</TraceDescription>
|
||||
</TraceLine>
|
||||
</TraceCollapsibleTrigger>
|
||||
<TraceCollapsibleContent class="flex flex-col gap-2">
|
||||
<CodeBlock 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">{{ withClip(tool.output, tool.output_bytes) }}</CodeBlockContent>
|
||||
</CodeBlock>
|
||||
</TraceCollapsibleContent>
|
||||
</TraceCollapsible>
|
||||
</Trace>
|
||||
</MessageContent>
|
||||
</Message>
|
||||
</template>
|
||||
</div>
|
||||
</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">
|
||||
|
||||
Reference in new issue
Block a user