Answer a turn's outcome in one pass, and build the clock format once
computeInputOutcomes scanned the transcript once per input, and it runs on every timeline change: quadratic, and at 4800 records the largest single cost left in the console (26% + 13% + 12% of sampled main-thread time). It is now one walk that gathers each turn's evidence as it finds the segments, it lives in the data layer where it can be tested, and a differential test pins it to the algorithm it replaced. An Intl.DateTimeFormat was also constructed per rendered row per render (8% of main-thread time at 4800 records); it is built once. One pass reads each record twice whatever the length; the old scan read 2, 52, 102, 199, 398 times per record as the transcript doubled. 1440 records: 4.0s -> 1.1s. 4800 records: 34.3s -> 5.5s (from 120.3s before either change).
6 unmodified lines7891011121314152 unmodified lines1819202121222223242526214 unmodified linesExpand all241242243244245246247248249250246251252253254255256257258259255256257258259260261262263264265266267268269270271272273274275276260278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320261262263264235 unmodified linesExpand all6 unmodified lines import type { StreamingClientV2, TimelineItem } from "../lib/streaming-v2"; import { Activity, ChevronDown, ChevronRight, CircleAlert, ShieldAlert, Terminal } from "lucide-svelte"; import { buildRows, computeInputOutcomes, filterItems, groupTurns, isCellRun, isLifecycleRun,2 unmodified lines type LifecycleRun, type Row, type TurnChild, } from "../lib/turns"; import type { InputOutcome, WorkItemDiagnostic } from "../lib/protocol-v2";
export let client: StreamingClientV2; /** Which lanes this view shows: a set, not a single mode. */ export let selection: LaneSelection = ["conversation"]; export let atLive = true;214 unmodified linesExpand all const cell = streamState[cellId] ?? {}; streamState = { ...streamState, [cellId]: { ...cell, [index]: !cell[index] } }; }
/** * Built once. An `Intl.DateTimeFormat` is a locale lookup plus a compiled pattern, and constructing one * per rendered row per render was, at 4800 records, the largest single cost left in the console. */ const clockFormat = new Intl.DateTimeFormat(undefined, { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }); function clock(ms: number): string { return new Intl.DateTimeFormat(undefined, { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }).format(new Date(ms)); return clockFormat.format(ms); }
function duration(ms?: number): string { if (!ms) return ""; if (ms < 1000) return `${ms}ms`; return `${(ms / 1000).toFixed(1)}s`; }
// input outcomes are derived from the turn that followed them; unchanged from the working version function computeInputOutcomes(items: TimelineItem[], work: WorkItemDiagnostic[]): Map<string, InputOutcome> { const outcomes = new Map<string, InputOutcome>(); const exactBySeq = new Map<number, InputOutcome>(); for (const item of items) { if (item.lane !== "lifecycle") continue; const kind = (item.data as { kind?: string }).kind; if (kind !== "turn.outcome" && kind !== "turn.failed") continue; const data = (item.data as { data?: Record<string, unknown> }).data ?? {}; const ids = Array.isArray(data.input_event_ids) ? (data.input_event_ids as number[]) : []; const status = String(data.status ?? (kind === "turn.failed" ? "failed" : "")); const reason = String(data.error ?? data.reason ?? (item.data as { reason?: string }).reason ?? ""); const outcome: InputOutcome = kind === "turn.failed" || status === "failed" ? { status: "failed", reason: reason || "the turn failed" } : status === "held" ? { status: "held", reason: reason || "held by delivery review" } : status === "replied" ? { status: "replied" } : { status: "no_reply", reason: "the turn completed without speech" }; for (const id of ids) exactBySeq.set(id, outcome); }
const inputs = items.filter((it): it is Extract<TimelineItem, { lane: "input" }> => it.lane === "input"); inputs.forEach((inputItem, index) => { if (inputItem.seq !== undefined && exactBySeq.has(inputItem.seq)) { outcomes.set(inputItem.id, exactBySeq.get(inputItem.seq)!); return; } const start = items.findIndex((it) => it.id === inputItem.id); const nextInput = inputs[index + 1]; const end = nextInput ? items.findIndex((it) => it.id === nextInput.id) : items.length; const turnItems = items.slice(start + 1, end);
const delivered = turnItems.find((it) => it.lane === "speech" && it.data.receipt?.status === "confirmed"); if (delivered) { outcomes.set(inputItem.id, { status: "replied" }); return; }
const held = turnItems.find((it) => it.lane === "speech" && it.data.receipt?.status === "held"); if (held && held.lane === "speech") { outcomes.set(inputItem.id, { status: "held", reason: held.data.receipt.reason || "held by delivery review" }); return; }
const failedCell = turnItems.find((it) => it.lane === "cell" && (it.data.status === "error" || (it.data.exit_code ?? 0) !== 0)); const failedLifecycle = turnItems.find((it) => it.lane === "lifecycle" && String((it.data as { kind?: string }).kind) === "failed"); const failedWork = work?.find((w) => w.status === "failed" && inputItem.seq !== undefined && w.id.includes(`input-${inputItem.seq}`)); if (failedCell || failedLifecycle || failedWork) { const stderr = failedCell && failedCell.lane === "cell" ? failedCell.data.outputs.find((o) => o.stream === "stderr")?.text : undefined; const reason = failedCell && failedCell.lane === "cell" ? stderr?.split("\n").filter(Boolean).slice(-1)[0] || `the cell exited ${failedCell.data.exit_code ?? "non-zero"}` : failedWork?.title || "the turn failed"; outcomes.set(inputItem.id, { status: "failed", reason }); return; }
if (index < inputs.length - 1) { outcomes.set(inputItem.id, { status: "no_reply", reason: "the turn completed without speech" }); return; } outcomes.set(inputItem.id, { status: "running" }); }); return outcomes; }
$: inputOutcomes = computeInputOutcomes(timelineItems, workItems);
/** `hook.decision` is a machine identifier, not a phrase: the console says "hook decision". */ function humanKind(kind: string): string {235 unmodified linesExpand all123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235import { describe, test, expect } from "bun:test";import { computeInputOutcomes } from "./turns";import type { TimelineItem } from "./streaming-v2";import type { InputOutcome, WorkItemDiagnostic } from "./protocol-v2";
// Fixtures for the outcomes of a turn: each builder produces the record the runtime would send.const input = (seq: number, id?: string): TimelineItem => ({ lane: "input", id: id ?? `in-${seq}`, seq, created_ms: seq * 10, data: { source: "operator", text: `turn ${seq}` },} as unknown as TimelineItem);
const speech = (seq: number, status: string, reason?: string): TimelineItem => ({ lane: "speech", id: `sp-${seq}`, seq, created_ms: seq * 10, data: { text: "words", receipt: { status, reason } },} as unknown as TimelineItem);
const cell = ( seq: number, { status, exit_code, stderr }: { status: string; exit_code: number; stderr?: string }): TimelineItem => ({ lane: "cell", id: `cell-${seq}`, seq, created_ms: seq * 10, data: { type: "completed", execution_id: `exec-${seq}`, status, exit_code, outputs: stderr === undefined ? [{ stream: "stdout", text: "ok\n" }] : [{ stream: "stderr", text: stderr }], },} as unknown as TimelineItem);
const lifecycle = (seq: number, kind: string, data?: Record<string, unknown>): TimelineItem => ({ lane: "lifecycle", id: `life-${seq}`, seq, created_ms: seq * 10, data: { kind, details: kind, reason: data?.reason, data },} as unknown as TimelineItem);
const workItem = (id: string, status: WorkItemDiagnostic["status"], title = "repair"): WorkItemDiagnostic => ({ id, title, purpose: "repair", status, origin: "human", created_ms: 1,});
/** * The implementation this replaced, kept here as the reference: evidence gathered by scanning the * transcript once per input. The rewrite has to answer exactly what this answers. */function referenceOutcomes(items: TimelineItem[], work: WorkItemDiagnostic[]): Map<string, InputOutcome> { const outcomes = new Map<string, InputOutcome>(); const exactBySeq = new Map<number, InputOutcome>(); for (const item of items) { if (item.lane !== "lifecycle") continue; const kind = (item.data as { kind?: string }).kind; if (kind !== "turn.outcome" && kind !== "turn.failed") continue; const data = (item.data as { data?: Record<string, unknown> }).data ?? {}; const ids = Array.isArray(data.input_event_ids) ? (data.input_event_ids as number[]) : []; const status = String(data.status ?? (kind === "turn.failed" ? "failed" : "")); const reason = String(data.error ?? data.reason ?? (item.data as { reason?: string }).reason ?? ""); const outcome: InputOutcome = kind === "turn.failed" || status === "failed" ? { status: "failed", reason: reason || "the turn failed" } : status === "held" ? { status: "held", reason: reason || "held by delivery review" } : status === "replied" ? { status: "replied" } : { status: "no_reply", reason: "the turn completed without speech" }; for (const id of ids) exactBySeq.set(id, outcome); }
const inputs = items.filter((it): it is Extract<TimelineItem, { lane: "input" }> => it.lane === "input"); inputs.forEach((inputItem, index) => { if (inputItem.seq !== undefined && exactBySeq.has(inputItem.seq)) { outcomes.set(inputItem.id, exactBySeq.get(inputItem.seq)!); return; } const start = items.findIndex((it) => it.id === inputItem.id); const nextInput = inputs[index + 1]; const end = nextInput ? items.findIndex((it) => it.id === nextInput.id) : items.length; const turnItems = items.slice(start + 1, end);
const delivered = turnItems.find((it) => it.lane === "speech" && it.data.receipt?.status === "confirmed"); if (delivered) { outcomes.set(inputItem.id, { status: "replied" }); return; }
const held = turnItems.find((it) => it.lane === "speech" && it.data.receipt?.status === "held"); if (held && held.lane === "speech") { outcomes.set(inputItem.id, { status: "held", reason: held.data.receipt.reason || "held by delivery review" }); return; }
const failedCell = turnItems.find((it) => it.lane === "cell" && (it.data.status === "error" || (it.data.exit_code ?? 0) !== 0)); const failedLifecycle = turnItems.find((it) => it.lane === "lifecycle" && String((it.data as { kind?: string }).kind) === "failed"); const failedWork = work?.find((w) => w.status === "failed" && inputItem.seq !== undefined && w.id.includes(`input-${inputItem.seq}`)); if (failedCell || failedLifecycle || failedWork) { const stderr = failedCell && failedCell.lane === "cell" ? failedCell.data.outputs.find((o) => o.stream === "stderr")?.text : undefined; const reason = failedCell && failedCell.lane === "cell" ? stderr?.split("\n").filter(Boolean).slice(-1)[0] || `the cell exited ${failedCell.data.exit_code ?? "non-zero"}` : failedWork?.title || "the turn failed"; outcomes.set(inputItem.id, { status: "failed", reason }); return; }
if (index < inputs.length - 1) { outcomes.set(inputItem.id, { status: "no_reply", reason: "the turn completed without speech" }); return; } outcomes.set(inputItem.id, { status: "running" }); }); return outcomes;}
/** Deterministic corpora: a fixed seed, so a failure is reproducible rather than a flake. */function seed(seedValue: number) { let x = seedValue >>> 0; return () => { x = (x * 1664525 + 1013904223) >>> 0; return x / 4294967296; };}
function transcript(turns: number, seedValue = 7): { items: TimelineItem[]; work: WorkItemDiagnostic[] } { const next = seed(seedValue); const items: TimelineItem[] = []; const work: WorkItemDiagnostic[] = []; let seq = 0; // records before any input: a session that started itself items.push(lifecycle(++seq, "session.activated")); items.push(speech(++seq, "confirmed")); for (let t = 1; t <= turns; t++) { const inputSeq = ++seq; items.push(input(inputSeq)); const shape = Math.floor(next() * 8); if (shape === 0) items.push(speech(++seq, "confirmed")); else if (shape === 1) items.push(speech(++seq, "held", next() < 0.5 ? "awaiting delivery review" : undefined)); else if (shape === 2) items.push(cell(++seq, { status: "error", exit_code: 101, stderr: "assertion failed\nleft == right" })); else if (shape === 3) items.push(cell(++seq, { status: "error", exit_code: 0, stderr: "" })); else if (shape === 4) items.push(cell(++seq, { status: "ok", exit_code: 0 })); else if (shape === 5) items.push(lifecycle(++seq, "failed")); else if (shape === 6) { items.push(speech(++seq, "held", "held at the boundary")); items.push(speech(++seq, "confirmed")); } else { items.push(lifecycle(++seq, "turn.outcome", { status: "held", reason: "delivery review refused" })); items.push(cell(++seq, { status: "ok", exit_code: 0 })); } if (next() < 0.3) work.push(workItem(`input-${inputSeq}`, "failed", `repair for turn ${t}`)); if (next() < 0.2) work.push(workItem(`input-${inputSeq}-extra`, "pending")); } // a second, later answer for an input that already had one const targets = items.filter((it) => it.lane === "input").slice(0, 3); for (const target of targets) { if (target.seq === undefined) continue; items.push(lifecycle(++seq, "turn.outcome", { status: "replied", input_event_ids: [target.seq] })); } return { items, work };}
describe("computeInputOutcomes", () => { test("answers what the scan-per-input implementation answered", () => { for (const size of [1, 2, 5, 40, 200]) { const { items, work } = transcript(size, size * 13 + 1); const mine = computeInputOutcomes(items, work); const reference = referenceOutcomes(items, work); expect([...mine.entries()]).toEqual([...reference.entries()]); } });
test("names the outcome of a turn by the evidence in it", () => { const items = [ input(1), speech(2, "confirmed"), // replied input(3), cell(4, { status: "error", exit_code: 101, stderr: "one\ntwo\nboom" }), // failed input(5), speech(6, "held", "awaiting delivery review"), // held input(7), cell(8, { status: "ok", exit_code: 0 }), // no reply input(9), cell(10, { status: "ok", exit_code: 0 }), // running (last, nothing said yet) ]; const outcomes = computeInputOutcomes(items, []); expect(outcomes.get("in-1")).toEqual({ status: "replied" }); expect(outcomes.get("in-3")).toEqual({ status: "failed", reason: "boom" }); expect(outcomes.get("in-5")).toEqual({ status: "held", reason: "awaiting delivery review" }); expect(outcomes.get("in-7")).toEqual({ status: "no_reply", reason: "the turn completed without speech" }); expect(outcomes.get("in-9")).toEqual({ status: "running" }); expect(outcomes.size).toBe(5); });
test("an explicit outcome for an input outranks the evidence that followed it", () => { const items = [ input(1), cell(2, { status: "error", exit_code: 101, stderr: "boom" }), speech(3, "confirmed"), // the runtime names input 1's own event id: that is the answer, not the failing cell beside it lifecycle(4, "turn.outcome", { status: "replied", input_event_ids: [1] }), ]; expect(computeInputOutcomes(items, []).get("in-1")).toEqual({ status: "replied" }); });
test("a failed work item tied to the input's sequence is a failure, with its title as the reason", () => { const items = [input(1), cell(2, { status: "ok", exit_code: 0 }), input(3)]; const work = [workItem("input-1", "failed", "repair the scheduler")]; expect(computeInputOutcomes(items, work).get("in-1")).toEqual({ status: "failed", reason: "repair the scheduler" }); });
test("reads the transcript a fixed number of times per record, not once per record per input", () => { const countReads = (turns: number) => { const { items, work } = transcript(turns, 99); let reads = 0; const counted = new Proxy(items, { get(target, prop, receiver) { if (typeof prop === "string" && /^\d+$/.test(prop)) reads += 1; return Reflect.get(target, prop, receiver); }, }); computeInputOutcomes(counted as unknown as TimelineItem[], work); return { reads, items: items.length }; }; const small = countReads(100); const large = countReads(200); // One pass reads each record for what it is, at every length: 2 reads per record measured at 50, 100, // 200 and 400 turns. The scan-per-input version doubled its reads per record every time the // transcript doubled (52, 102, 199, 398 at those same sizes) — 199x the reads at the largest. expect(large.reads / large.items).toBeLessThan(small.reads / small.items * 1.6); });});123456172 unmodified linesExpand all17918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229396 unmodified linesimport type { TimelineItem } from "./streaming-v2";import type { InputOutcome, WorkItemDiagnostic } from "./protocol-v2";
export type InputItem = Extract<TimelineItem, { lane: "input" }>;export type CellItem = Extract<TimelineItem, { lane: "cell" }>;export type SpeechItem = Extract<TimelineItem, { lane: "speech" }>;172 unmodified linesExpand all return children;}
/** * What became of each thing the operator said. The answer for a turn is the evidence between it and the * next input — the delivered speech, the held speech, the failing cell — and one exact answer that * outranks all of it: a `turn.outcome` record naming the input's own event ids. * * The evidence is gathered in the single walk that finds the segments, not by scanning the transcript * once per input. It used to scan per input, and this runs on every timeline change: with a few thousand * records that scan was the largest single cost in the console, larger than the whole render. */export function computeInputOutcomes( items: TimelineItem[], work: WorkItemDiagnostic[] = []): Map<string, InputOutcome> { // First pass: the answers that are stated outright, keyed by the input sequence they name. const exactBySeq = new Map<number, InputOutcome>(); for (const item of items) { if (item.lane !== "lifecycle") continue; const kind = (item.data as { kind?: string }).kind; if (kind !== "turn.outcome" && kind !== "turn.failed") continue; const data = (item.data as { data?: Record<string, unknown> }).data ?? {}; const ids = Array.isArray(data.input_event_ids) ? (data.input_event_ids as number[]) : []; const status = String(data.status ?? (kind === "turn.failed" ? "failed" : "")); const reason = String(data.error ?? data.reason ?? (item.data as { reason?: string }).reason ?? ""); const outcome: InputOutcome = kind === "turn.failed" || status === "failed" ? { status: "failed", reason: reason || "the turn failed" } : status === "held" ? { status: "held", reason: reason || "held by delivery review" } : status === "replied" ? { status: "replied" } : { status: "no_reply", reason: "the turn completed without speech" }; for (const id of ids) exactBySeq.set(id, outcome); }
// Second pass: walk the transcript, keeping the segment started by the open input. Each input is // decided when the next one closes it, so every record is read exactly once. const failedWork = work.filter((item) => item.status === "failed"); const outcomes = new Map<string, InputOutcome>(); let open: InputItem | null = null; let delivered = false; let heldReason: string | null = null; let failedCell: CellItem | null = null; let failedLifecycle = false;
const close = (last: boolean) => { const input = open; if (!input) return; open = null; if (input.seq !== undefined && exactBySeq.has(input.seq)) { outcomes.set(input.id, exactBySeq.get(input.seq)!); } else if (delivered) { outcomes.set(input.id, { status: "replied" }); } else if (heldReason !== null) { outcomes.set(input.id, { status: "held", reason: heldReason }); } else { const failed = failedCell ? failedCell.data.outputs.find((output) => output.stream === "stderr")?.text ?.split("\n") .filter(Boolean) .slice(-1)[0] || `the cell exited ${failedCell.data.exit_code ?? "non-zero"}` : failedWork.find((item) => input.seq !== undefined && item.id.includes(`input-${input.seq}`))?.title; if (failed || failedLifecycle) { outcomes.set(input.id, { status: "failed", reason: failed || "the turn failed" }); } else if (last) { outcomes.set(input.id, { status: "running" }); } else { outcomes.set(input.id, { status: "no_reply", reason: "the turn completed without speech" }); } } delivered = false; heldReason = null; failedCell = null; failedLifecycle = false; };
for (const item of items) { if (item.lane === "input") { close(false); open = item; continue; } if (!open) continue; switch (item.lane) { case "speech": { const receipt = item.data.receipt; if (receipt?.status === "confirmed") delivered = true; else if (receipt?.status === "held" && heldReason === null) { heldReason = receipt.reason || "held by delivery review"; } break; } case "cell": if (failedCell === null && (item.data.status === "error" || (item.data.exit_code ?? 0) !== 0)) { failedCell = item; } break; case "lifecycle": if (String((item.data as { kind?: string }).kind) === "failed") failedLifecycle = true; break; default: break; } } close(true); return outcomes;}
/** * A turn is one human input and everything the runtime did about it. Records that arrive before any * input (session activation, a model-driven start) form a leading turn of their own, so nothing is * dropped and nothing is invented. */96 unmodified lines