Multiplayer agent harness for work.
Stream reasoning summaries as they arrive
Regan Bell79207bc3cd08
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.tsindex 1988d1465..cfaba99f9 100644--- a/plugins/web-ui/src/chat.ts+++ b/plugins/web-ui/src/chat.ts@@ -126,6 +126,7 @@ import {} from "./core-bridge";import {buildTimeline,+ thinkingStepKey,messageWorkTimeline,workTimelineSegments,streamedAnswer,@@ -436,6 +437,7 @@ export function createChatSurface(let ctaText = CHAT_CTAS[0]!;let workTicker: ReturnType<typeof setInterval> | null = null;let liveWorkExpanded = false;+ const openThinking = new Set<string>();function notePendingSessionOnSend(): void {if (!chatState.threadRef || chatState.sessionId !== null) return;@@ -588,6 +590,7 @@ export function createChatSurface(clearLiveWork();resetBackgroundPanel();chatState.resolvingApprovals.clear();+ openThinking.clear();const onWork = observeLiveWork(agent);const onSendIssues = (issues: string[], retryable: Attachment[]): void => {if (agent !== chatState.agent || !issues.length) return;@@ -2965,6 +2968,8 @@ export function createChatSurface(function timelineKey(item: TimelineItem): string {if (item.kind === "tool") return `tool:${item.row.call?.seq ?? item.row.result?.seq ?? item.row.approval?.seq}`;if (item.kind === "approval") return `approval:${item.approval.requestId}`;+ const step = item.kind === "thinking" ? thinkingStepKey(item.activity.payload) : null;+ if (step) return `thinking:${step}`;return `${item.kind}:${item.activity.seq}`;}@@ -3018,6 +3023,7 @@ export function createChatSurface([work,work.activity,+ work.reasoning,work.status,work.stale,work.pendingApprovals,@@ -3130,7 +3136,15 @@ export function createChatSurface(}if (item.kind === "thinking") {const thought = thinkingPresentation((item.activity.payload as { thinking?: string }).thinking ?? "");- return html`<details class="thinking-row">+ const key = timelineKey(item);+ return html`<details+ class="thinking-row"+ ?open=${openThinking.has(key)}+ @toggle=${(event: Event) => {+ if ((event.currentTarget as HTMLDetailsElement).open) openThinking.add(key);+ else openThinking.delete(key);+ }}+ ><summary class="thinking-summary"><span class="tool-icon">${icon(Brain, 15)}</span><span class="thinking-title">${thought.title}</span><span class="activity-chevron">${icon(ChevronRight, 14)}</span>diff --git a/plugins/web-ui/src/core-bridge.ts b/plugins/web-ui/src/core-bridge.tsindex a775b9b3b..c95db2d21 100644--- a/plugins/web-ui/src/core-bridge.ts+++ b/plugins/web-ui/src/core-bridge.ts@@ -445,11 +445,19 @@ export interface WorkBlock {finishedAt?: number;stale?: boolean;activity: ToolActivity[];+ reasoning?: LiveReasoning[];+ attempt?: number;pendingApprovals?: PendingApproval[];goal?: GoalStripState;runtime?: TurnRuntime;}+interface LiveReasoning {+ step: string;+ index: number;+ text: string;+}+export interface TurnRuntime {harnessId: string;modelId: string;@@ -527,6 +535,8 @@ export interface RunPoll {stale?: boolean;replyComplete?: boolean;activity?: unknown[];+ reasoning?: LiveReasoning[];+ attempt?: number;startedAt?: number | null;finishedAt?: number | null;}@@ -1455,6 +1465,21 @@ function parseActivity(raw: unknown): ToolActivity[] {return out;}+function mergeReasoning(work: WorkBlock | undefined, block: LiveReasoning, offset = 0): boolean {+ if (!work || typeof block.step !== "string" || !Number.isSafeInteger(block.index) || typeof block.text !== "string")+ return false;+ const blocks = work.reasoning ?? [];+ const at = blocks.findIndex((b) => b.step === block.step && b.index === block.index);+ const current = at >= 0 ? blocks[at]!.text : "";+ if (!Number.isSafeInteger(offset) || offset < 0 || offset > current.length) return false;+ const text = current.slice(0, offset) + block.text;+ if (text.length <= current.length) return false;+ const next = { step: block.step, index: block.index, text };+ work.reasoning =+ at >= 0 ? blocks.map((b, i) => (i === at ? next : b)) : [...blocks.filter((b) => b.step === block.step), next];+ return true;+}+function setWorkStale(work: WorkBlock | undefined, stale: boolean, notify?: () => void): void {if (!work || (work.stale ?? false) === stale) return;work.stale = stale;@@ -1493,6 +1518,12 @@ function applyRun(if ((work?.activity.length ?? 0) > beforeActivity) st.lastProgressAt = now();notify?.();}+ if (work && typeof run.attempt === "number" && work.attempt !== run.attempt) {+ if (work.attempt !== undefined && work.reasoning?.length) work.reasoning = [];+ work.attempt = run.attempt;+ }+ if (Array.isArray(run.reasoning) && run.reasoning.map((block) => mergeReasoning(work, block)).some(Boolean))+ notify?.();setWorkStale(work, run.stale === true, notify);if (work && run.goal && JSON.stringify(run.goal) !== JSON.stringify(work.goal)) {work.goal = run.goal;@@ -1688,6 +1719,13 @@ async function streamRunViaSse(return "fallback";if (value.offset > Math.max(st.startOffset + st.acc.length, value.startOffset ?? 0)) return "fallback";applyStreamText(stream, partial, st, value.delta, value.offset, value.startOffset ?? 0);+ } else if (event.name === "reasoning") {+ const value = event.value as { step: string; index: number; offset: number; delta: string };+ const work = (partial as AssistantWork).work;+ if (mergeReasoning(work, { step: value.step, index: value.index, text: value.delta }, value.offset)) {+ st.lastProgressAt = now();+ notify?.();+ }} else if (event.name === "run") {const run = event.value as RunPoll;// Completion must carry the final result, including files and approvals.diff --git a/plugins/web-ui/src/timeline.ts b/plugins/web-ui/src/timeline.tsindex 3f2cf71b3..f8a41644f 100644--- a/plugins/web-ui/src/timeline.ts+++ b/plugins/web-ui/src/timeline.ts@@ -156,6 +156,7 @@ const timelineMemo = new WeakMap<WorkBlock,{activity: WorkBlock["activity"];+ reasoning: WorkBlock["reasoning"];status: WorkBlock["status"];approvals: WorkBlock["pendingApprovals"];items: TimelineItem[];@@ -164,12 +165,19 @@ const timelineMemo = new WeakMap<export function buildTimeline(work: WorkBlock): TimelineItem[] {const hit = timelineMemo.get(work);- if (hit && hit.activity === work.activity && hit.status === work.status && hit.approvals === work.pendingApprovals) {+ if (+ hit &&+ hit.activity === work.activity &&+ hit.reasoning === work.reasoning &&+ hit.status === work.status &&+ hit.approvals === work.pendingApprovals+ ) {return hit.items;}const items = buildTimelineUncached(work);timelineMemo.set(work, {activity: work.activity,+ reasoning: work.reasoning,status: work.status,approvals: work.pendingApprovals,items,@@ -221,6 +229,20 @@ function buildTimelineUncached(work: WorkBlock): TimelineItem[] {open = null;}}+ if (!isTerminalWorkStatus(work.status)) {+ liveReasoning(work).forEach((block, i) =>+ items.push({+ kind: "thinking",+ activity: {+ seq: -1 - i,+ parentSeq: null,+ type: "thinking",+ payload: { thinking: block.text, step: { id: block.step, index: block.index } },+ createdAt: 0,+ },+ }),+ );+ }const collapsed = collapseToolItems(items, work.status);for (const item of collapsed) {if (item.kind !== "tool") continue;@@ -234,6 +256,16 @@ function buildTimelineUncached(work: WorkBlock): TimelineItem[] {return collapsed;}+export function thinkingStepKey(payload: unknown): string | null {+ const step = (payload as { step?: { id?: unknown; index?: unknown } } | null)?.step;+ return step ? `${String(step.id)}:${String(step.index)}` : null;+}++function liveReasoning(work: WorkBlock): NonNullable<WorkBlock["reasoning"]> {+ const persisted = new Set(work.activity.map((a) => (a.type === "thinking" ? thinkingStepKey(a.payload) : null)));+ return (work.reasoning ?? []).filter((b) => b.text.trim() && !persisted.has(`${b.step}:${b.index}`));+}+function collapseToolItems(items: TimelineItem[], status: WorkBlock["status"]): TimelineItem[] {if (!isTerminalWorkStatus(status)) return items;const out: TimelineItem[] = [];diff --git a/plugins/web-ui/test/run-connection.test.ts b/plugins/web-ui/test/run-connection.test.tsindex 26698e053..c527b4a22 100644--- a/plugins/web-ui/test/run-connection.test.ts+++ b/plugins/web-ui/test/run-connection.test.ts@@ -341,3 +341,75 @@ test("a resume snapshot replaces whitespace-normalized transcript seeds", asyncconst stream = await fn(model, {} as Context, {});assert.deepEqual((await stream.result()).content, [{ type: "text", text: "\nChecking.\n\nNext part" }]);});++test("reasoning frames and snapshots build live thinking blocks, deduped by per-block offset", async (t) => {+ const state = streamingFetch(t);+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});+ await flush();+ state.send({+ type: "CUSTOM",+ name: "reasoning",+ value: { step: "s1", index: 0, offset: 0, delta: "**Checking** the " },+ });+ state.send({+ type: "CUSTOM",+ name: "reasoning",+ value: { step: "s1", index: 0, offset: 0, delta: "**Checking** the " },+ });+ state.send({ type: "CUSTOM", name: "reasoning", value: { step: "s1", index: 0, offset: 17, delta: "cache" } });+ state.send({ type: "CUSTOM", name: "reasoning", value: { step: "s1", index: 0, offset: 99, delta: "gap" } });+ state.send({+ type: "CUSTOM",+ name: "run",+ value: {+ status: "running",+ result: null,+ partial: "hello",+ reasoning: [+ { step: "s1", index: 0, text: "**Checking** the cache and queue" },+ { step: "s1", index: 2, text: "second" },+ ],+ },+ });+ state.send({ type: "CUSTOM", name: "delta", value: { offset: 5, delta: " world" } });+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok" } } });+ const message = await stream.result();+ assert.deepEqual(message.content, [{ type: "text", text: "hello world" }]);+ assert.deepEqual((message as unknown as { work: { reasoning: unknown } }).work.reasoning, [+ { step: "s1", index: 0, text: "**Checking** the cache and queue" },+ { step: "s1", index: 2, text: "second" },+ ]);+});++test("a new step's reasoning replaces unpersisted blocks, so a retried attempt never shows the dead attempt's", async (t) => {+ const state = streamingFetch(t);+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});+ await flush();+ state.send({+ type: "CUSTOM",+ name: "reasoning",+ value: { step: "dead", index: 0, offset: 0, delta: "half a thought" },+ });+ state.send({ type: "CUSTOM", name: "reasoning", value: { step: "retry", index: 0, offset: 0, delta: "fresh" } });+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok", reply: "ok" } } });+ const message = await stream.result();+ assert.deepEqual((message as unknown as { work: { reasoning: unknown } }).work.reasoning, [+ { step: "retry", index: 0, text: "fresh" },+ ]);+});++test("a new run attempt drops the previous attempt's unpersisted reasoning", async (t) => {+ const state = streamingFetch(t);+ const stream = await makeRunResumeStreamFn("r")(model, {} as Context, {});+ await flush();+ state.send({ type: "CUSTOM", name: "run", value: { status: "running", result: null, attempt: 1 } });+ state.send({+ type: "CUSTOM",+ name: "reasoning",+ value: { step: "dead", index: 0, offset: 0, delta: "half a thought" },+ });+ state.send({ type: "CUSTOM", name: "run", value: { status: "running", result: null, attempt: 2 } });+ state.send({ type: "CUSTOM", name: "run", value: { status: "done", result: { status: "ok", reply: "ok" } } });+ const message = await stream.result();+ assert.deepEqual((message as unknown as { work: { reasoning: unknown } }).work.reasoning, []);+});diff --git a/plugins/web-ui/test/timeline.test.ts b/plugins/web-ui/test/timeline.test.tsindex 4b1228036..a9c7e92ab 100644--- a/plugins/web-ui/test/timeline.test.ts+++ b/plugins/web-ui/test/timeline.test.ts@@ -553,3 +553,36 @@ test("recorded commentary is not repeated after its live window is trimmed", ()});assert.equal(streamedAnswer("ipped word here", work), "");});++test("live reasoning renders as a trailing thinking row until its persisted entry arrives or work ends", () => {+ const reasoning = [+ { step: "s1", index: 0, text: "**Checking** the cache" },+ { step: "s1", index: 2, text: " " },+ ];+ const work: WorkBlock = {+ status: "working",+ activity: [act(1, "thinking", { thinking: "earlier", step: { id: "s0", index: 0 } })],+ reasoning,+ };+ const live = buildTimeline(work);+ assert.deepEqual(+ live.map((item) =>+ item.kind === "thinking" ? (item.activity.payload as { thinking: string }).thinking : item.kind,+ ),+ ["earlier", "**Checking** the cache"],+ );+ const persisted: WorkBlock = {+ ...work,+ activity: [+ ...work.activity,+ act(2, "thinking", { thinking: "**Checking** the cache", step: { id: "s1", index: 0 } }),+ ],+ };+ assert.equal(buildTimeline(persisted).length, 2, "the persisted entry replaces the live row instead of doubling it");+ const otherBlock: WorkBlock = {+ ...work,+ activity: [...work.activity, act(2, "thinking", { thinking: "other", step: { id: "s1", index: 1 } })],+ };+ assert.equal(buildTimeline(otherBlock).length, 3, "a different block of the same step does not hide this one");+ assert.equal(buildTimeline({ ...work, status: "complete" }).length, 1, "finished work shows only durable rows");+});diff --git a/src/api/app-turn.ts b/src/api/app-turn.tsindex 97730ea69..752e1a0ab 100644--- a/src/api/app-turn.ts+++ b/src/api/app-turn.ts@@ -722,8 +722,8 @@ export function createTurnMethods();},- syncRunStream(runId, offset) {- deps.runStreamEvents?.emit({ runId, kind: "sync", offset });+ syncRunStream(runId, offset, reasoning) {+ deps.runStreamEvents?.emit({ runId, kind: "sync", offset, reasoning });},async getRun(runId, viewer) {@@ -739,11 +739,13 @@ export function createTurnMethods(const replying = deps.turnStream?.replying(runId) ?? false;const replyComplete = deps.turnStream?.isReplyDone(runId) ?? false;const activity = await deps.runActivity?.list(runId);+ const reasoning = deps.turnStream?.reasoning(runId) ?? [];const stale =run.status === "pending" ? run.attempts > 0 : !alive && leaseLapsed(run, Date.now() - STALE_LEASE_GRACE_MS);return {desktopClientId: desktopClientForTurn({ ...run.request, attempt: run.attempts }),status: run.status,+ attempt: run.attempts,result: run.result ? await withAdminLink(run.result) : run.result,...(run.request.surface === "web" &&!run.request.approval &&@@ -782,6 +784,7 @@ export function createTurnMethods(...(replying ? { replying: true } : {}),...(replyComplete ? { replyComplete: true } : {}),...(activity && activity.length ? { activity } : {}),+ ...(reasoning.length ? { reasoning } : {}),};},diff --git a/src/api/app-types.ts b/src/api/app-types.tsindex 422c3bd78..3f59c4ab2 100644--- a/src/api/app-types.ts+++ b/src/api/app-types.ts@@ -34,7 +34,7 @@ import type { MonitorStore } from "../monitors/monitor-store.ts";import type { EngagedRegistry } from "../wake/engaged-registry.ts";import type { Orchestrator } from "../core/orchestrator.ts";import type { Run, RunDeliveryState, RunStore } from "../runs/run-store.ts";-import type { GoalView, TurnStream } from "../runs/turn-stream.ts";+import type { GoalView, ReasoningBlock, TurnStream } from "../runs/turn-stream.ts";import type { SessionStateBus, SessionStateEvent } from "../runs/session-state-bus.ts";import type { LedgerEventBus, OwnedLedgerEvent } from "../loops/ledger-events.ts";import type { SubscribeOptions } from "../util/event-bus.ts";@@ -283,12 +283,13 @@ export interface App {listSessionApprovals(sessionId: string, viewer: string): Promise<PendingApproval[]>;pendingApprovalForThread(threadRef: string, viewer?: string): Promise<TurnResult | null>;subscribeRun(runId: string, listener: (event: RunStreamEvent) => void, onResync?: () => void): () => void;- syncRunStream(runId: string, offset: number): void;+ syncRunStream(runId: string, offset: number, reasoning: Record<string, number>): void;getRun(runId: string,viewer?: string,): Promise<{status: Run["status"];+ attempt: number;result: TurnResult | null;input?: {runId: string;@@ -309,6 +310,7 @@ export interface App {firstBlockClosed?: boolean;surfacePosted?: boolean;activity?: RunActivityEntry[];+ reasoning?: ReasoningBlock[];startedAt: number | null;finishedAt: number | null;} | null>;diff --git a/src/api/routes/run-events.ts b/src/api/routes/run-events.tsindex f4d3625de..7fa6a8392 100644--- a/src/api/routes/run-events.ts+++ b/src/api/routes/run-events.ts@@ -1,4 +1,7 @@import { sendJson } from "../http.ts";+import { LRUCache } from "lru-cache";+import { reasoningKey } from "../../runs/run-stream-events.ts";+import { REASONING_MAX_BLOCKS } from "../../runs/turn-stream.ts";import type { SessionEntry } from "../../types.ts";import type { ApiCtx, Route } from "./route.ts";@@ -43,6 +46,7 @@ async function streamRun(ctx: ApiCtx): Promise<void> {let refreshing = false;let refreshAgain = false;const sentToolEvents = new Set<string>();+ const sentReasoning = new LRUCache<string, number>({ max: REASONING_MAX_BLOCKS });let toolCursor: number | undefined;const cleanup = (): void => {closed = true;@@ -71,7 +75,7 @@ async function streamRun(ctx: ApiCtx): Promise<void> {if (syncing || closed) return;syncing = true;queueMicrotask(() => {- if (!closed) app.syncRunStream(runId, offset);+ if (!closed) app.syncRunStream(runId, offset, Object.fromEntries(sentReasoning.entries()));});};const sendDrained = async (data: unknown, id?: string): Promise<void> => {@@ -82,6 +86,10 @@ async function streamRun(ctx: ApiCtx): Promise<void> {const toolEntries = await app.getRunToolEntries(runId, actor?.p, toolCursor);if (closed) return;offset = Math.max(offset, (run.partialStartOffset ?? 0) + (run.partial?.length ?? 0));+ for (const block of run.reasoning ?? []) {+ const key = reasoningKey(block);+ sentReasoning.set(key, Math.max(sentReasoning.get(key) ?? 0, block.text.length));+ }for (const entry of toolEntries) {const event = toolEvent(entry);if (event && !sentToolEvents.has(event.id)) {@@ -135,6 +143,25 @@ async function streamRun(ctx: ApiCtx): Promise<void> {void refresh();return;}+ if (event.kind === "reasoning") {+ if (refreshing) return;+ const key = reasoningKey(event);+ const sent = sentReasoning.get(key) ?? 0;+ if (event.offset > sent) {+ sync();+ return;+ }+ const delta = event.text.slice(sent - event.offset);+ if (!delta) return;+ syncing = false;+ send({+ type: "CUSTOM",+ name: "reasoning",+ value: { step: event.step, index: event.index, offset: sent, delta },+ });+ sentReasoning.set(key, sent + delta.length);+ return;+ }if (event.kind !== "delta" || refreshing) return;const startOffset = event.startOffset ?? 0;offset = Math.max(offset, startOffset);diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex b9de3bebc..0a184d735 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -640,18 +640,27 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {}).catch(swallowAs("orchestrator: run-activity append", undefined));} else if (input.runId && deps.runActivity && appended.type === "thinking") {- const block = appended.payload as { thinking?: string; redacted?: boolean };+ const block = appended.payload as {+ thinking?: string;+ redacted?: boolean;+ step?: { id?: unknown; index?: unknown };+ };+ const step =+ typeof block.step?.id === "string" && typeof block.step.index === "number"+ ? { id: block.step.id, index: block.step.index }+ : undefined;if (!block.redacted && block.thinking?.trim()) {await deps.runActivity.append(input.runId, {seq: appended.seq,parentSeq: appended.parentSeq,type: appended.type,- payload: { thinking: block.thinking },+ payload: { thinking: block.thinking, ...(step ? { step } : {}) },createdAt: appended.createdAt,}).catch(swallowAs("orchestrator: run-activity append", undefined));}+ if (step) deps.turnStream?.settleReasoning(input.runId, step.id, step.index);}};if (!(await managedRosterIsCurrent())) {@@ -3641,6 +3650,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {if (input.surfaceTools && spineFirstBlockOpen && spineFirstBlock.length < FIRST_BLOCK_CAPTURE_MAX_CHARS)spineFirstBlock += chunk;},+ onReasoningDelta: (step: string, index: number, chunk: string) => {+ if (input.runId && deps.turnStream && !input.surfaceTools)+ deps.turnStream.publishReasoning(input.runId, step, index, chunk);+ },onToolCallStart: (name: string) => {if (!earlyTaskAck || name === surfaceName) return;if (input.surfaceTools) publishFirstAck(false);diff --git a/src/harness/harness.ts b/src/harness/harness.tsindex e26f80195..fd52b3ab5 100644--- a/src/harness/harness.ts+++ b/src/harness/harness.ts@@ -136,6 +136,7 @@ export interface HarnessTurnInput {onProgress?(p: { toolCalls: number; tokens?: number }): void;onGapWork?(sink: (work: GapWork) => void): void;onDelta?(chunk: string): void;+ onReasoningDelta?(step: string, index: number, chunk: string): void;onTextBlockStart?(phase?: "commentary" | "final_answer"): void | Promise<void>;onToolCallStart?(name: string): void;screenToolResult?(input: ToolResultScreenInput): Promise<ToolResultScreen>;diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.tsindex 187a28ba7..a6d24a156 100644--- a/src/harness/mock-harness.ts+++ b/src/harness/mock-harness.ts@@ -1,3 +1,4 @@+import { randomUUID } from "node:crypto";import {defineHarness,type Harness,@@ -383,9 +384,12 @@ export function createMockHarness(): Harness {} else if (command0 === "!surfacename") {reply = `surface:${turn.surfaceName ?? "none"}`;} else if (command0.startsWith("!think ")) {+ const thinking = command0.slice("!think ".length);+ const step = { id: randomUUID(), index: 0 };+ for (const chunk of streamChunks(thinking)) turn.onReasoningDelta?.(step.id, step.index, chunk);await turn.emit({type: "thinking",- payload: { thinking: command0.slice("!think ".length), thinkingSignature: "OPAQUE-SIG-BYTES" },+ payload: { thinking, thinkingSignature: "OPAQUE-SIG-BYTES", step },scopeLabel: turn.scopeLabel,});reply = "thought about it";diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.tsindex c9aeb1187..668bfc362 100644--- a/src/harness/pi-harness.ts+++ b/src/harness/pi-harness.ts@@ -1915,6 +1915,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {});const unsubscribe = entry.agentSession.subscribe((event) => {if (event.type === "message_start" && (event.message as { role?: string }).role === "assistant") {+ steps.startStreaming();curStart = Date.now();curFirst = undefined;try {@@ -1932,6 +1933,11 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {} else if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {if (curFirst === undefined) curFirst = Date.now();turn.onDelta?.(event.assistantMessageEvent.delta);+ } else if (event.type === "message_update" && event.assistantMessageEvent.type === "thinking_delta") {+ const { contentIndex, delta, partial } = event.assistantMessageEvent;+ const block = partial.content[contentIndex];+ if (block?.type === "thinking" && !block.redacted)+ turn.onReasoningDelta?.(steps.streamingStepId(), contentIndex, delta);} else if (event.type === "message_end" && (event.message as { role?: string }).role === "assistant") {const end = Date.now();const u = (event.message as { usage?: Partial<Usage> }).usage;diff --git a/src/harness/pi-step-recorder.ts b/src/harness/pi-step-recorder.tsindex eb3fe524c..7a2eefbfb 100644--- a/src/harness/pi-step-recorder.ts+++ b/src/harness/pi-step-recorder.ts@@ -67,6 +67,7 @@ export function createStepRecorder(turn: Pick<HarnessTurnInput, "emit" | "scopeLconst executing = new Map<string, RecordedEntry>();let pending: { step: ReplayStep; parts: Positioned[] } | null = null;let last: ReplayStep | undefined;+ let streamingStep: string | undefined;const write = (entry: RecordedEntry) => persist(() => turn.emit(entry as TurnEntry));const flush = (): void => {if (!pending) return;@@ -122,7 +123,8 @@ export function createStepRecorder(turn: Pick<HarnessTurnInput, "emit" | "scopeLassistant(message: AssistantMessage): void {flush();const { api, provider, model, stopReason } = message;- const step: ReplayStep = { id: randomUUID(), api, provider, model, stopReason };+ const step: ReplayStep = { id: streamingStep ?? randomUUID(), api, provider, model, stopReason };+ streamingStep = undefined;last = step;const parts: Positioned[] = [];message.content.forEach((block, index) => {@@ -136,6 +138,12 @@ export function createStepRecorder(turn: Pick<HarnessTurnInput, "emit" | "scopeLpending = { step, parts };if (message.content.some((block) => block.type === "toolCall")) flush();},+ startStreaming(): void {+ streamingStep = undefined;+ },+ streamingStepId(): string {+ return (streamingStep ??= randomUUID());+ },toolReturned: recordResult,toolResult(message: ToolResultMessage): void {recordResult(message.toolCallId, message.content, message.isError);diff --git a/src/runs/run-stream-events.ts b/src/runs/run-stream-events.tsindex 1ceeefa9c..a11261ec4 100644--- a/src/runs/run-stream-events.ts+++ b/src/runs/run-stream-events.ts@@ -2,7 +2,8 @@ import type { EventBus } from "../util/event-bus.ts";export type RunStreamEvent =| { runId: string; kind: "delta"; offset: number; text: string; startOffset: number }- | { runId: string; kind: "sync"; offset: number }+ | { runId: string; kind: "reasoning"; step: string; index: number; offset: number; text: string }+ | { runId: string; kind: "sync"; offset: number; reasoning?: Record<string, number> }| { runId: string; kind: "refresh" };export function emitRunText(@@ -12,8 +13,26 @@ export function emitRunText(offset: number,startOffset: number,): void {+ for (const [at, chunk] of notifyChunks(text))+ bus.emit({ runId, kind: "delta", offset: offset + at, text: chunk, startOffset });+}++export function emitRunReasoning(+ bus: EventBus<RunStreamEvent>,+ runId: string,+ block: { step: string; index: number },+ text: string,+ offset: number,+): void {+ for (const [at, chunk] of notifyChunks(text))+ bus.emit({ runId, kind: "reasoning", step: block.step, index: block.index, offset: offset + at, text: chunk });+}++export function reasoningKey(block: { step: string; index: number }): string {+ return `${block.step}:${block.index}`;+}++function* notifyChunks(text: string): Generator<[number, string]> {// JSON escaping can cost six bytes per UTF-16 unit; stay below NOTIFY's byte limit.- for (let at = 0; at < text.length; at += 1_000) {- bus.emit({ runId, kind: "delta", offset: offset + at, text: text.slice(at, at + 1_000), startOffset });- }+ for (let at = 0; at < text.length; at += 1_000) yield [at, text.slice(at, at + 1_000)];}diff --git a/src/runs/turn-stream.ts b/src/runs/turn-stream.tsindex 5d8c3bc79..e00036d6f 100644--- a/src/runs/turn-stream.ts+++ b/src/runs/turn-stream.ts@@ -10,11 +10,20 @@ export interface GoalView {updatedAt: number;}+export interface ReasoningBlock {+ step: string;+ index: number;+ text: string;+}+export interface TurnStream {begin(runId: string): void;replying(runId: string): boolean;publish(runId: string, delta: string): void;publishBlockStart(runId: string): void;+ publishReasoning(runId: string, step: string, index: number, delta: string): void;+ settleReasoning(runId: string, step: string, index: number): void;+ reasoning(runId: string): ReasoningBlock[];noteToolCall(runId: string): void;noteGoal(runId: string, goal: GoalView): void;goal(runId: string): GoalView | null;@@ -44,6 +53,7 @@ interface Entry {firstBlock: string;firstBlockOpen: boolean;firstBlockClosed: boolean;+ reasoning: ReasoningBlock[];surfacePosted: boolean;replying: boolean;replyDone: boolean;@@ -54,6 +64,7 @@ export interface TurnStreamOptions {maxChars?: number;graceMs?: number;onDelta?(runId: string, text: string, offset: number, startOffset: number): void;+ onReasoning?(runId: string, block: Omit<ReasoningBlock, "text">, text: string, offset: number): void;onChange?(runId: string): void;}@@ -61,6 +72,8 @@ const DEFAULT_MAX_CHARS = 200_000;const DEFAULT_GRACE_MS = 30_000;const FIRST_BLOCK_MAX_CHARS = 20_000;const BLOCK_JOIN = "\n\n";+export const REASONING_MAX_BLOCKS = 8;+const REASONING_MAX_CHARS = 20_000;function makeEntry(partial: Partial<Entry> = {}): Entry {return {@@ -71,6 +84,7 @@ function makeEntry(partial: Partial<Entry> = {}): Entry {firstBlock: "",firstBlockOpen: true,firstBlockClosed: false,+ reasoning: [],surfacePosted: false,replying: true,replyDone: false,@@ -106,8 +120,12 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {return {begin(runId) {const entry = runs.get(runId);- if (entry) entry.replying = true;- else runs.set(runId, makeEntry());+ if (entry) {+ entry.replying = true;+ entry.reasoning = [];+ if (entry.timer) clearTimeout(entry.timer);+ entry.timer = null;+ } else runs.set(runId, makeEntry());opts.onChange?.(runId);},@@ -136,6 +154,29 @@ export function createTurnStream(opts: TurnStreamOptions = {}): TurnStream {append(runId, entry, BLOCK_JOIN);},+ publishReasoning(runId, step, index, delta) {+ const entry = ensure(runId);+ let block = entry.reasoning.find((b) => b.step === step && b.index === index);+ if (!block) {+ block = { step, index, text: "" };+ entry.reasoning = [...entry.reasoning, block].slice(-REASONING_MAX_BLOCKS);+ }+ const piece = delta.slice(0, Math.max(0, REASONING_MAX_CHARS - block.text.length));+ if (!piece) return;+ const offset = block.text.length;+ block.text += piece;+ opts.onReasoning?.(runId, { step, index }, piece, offset);+ },++ settleReasoning(runId, step, index) {+ const entry = runs.get(runId);+ if (entry) entry.reasoning = entry.reasoning.filter((b) => b.step !== step || b.index !== index);+ },++ reasoning(runId) {+ return (runs.get(runId)?.reasoning ?? []).map((b) => ({ ...b }));+ },+noteToolCall(runId) {const entry = ensure(runId);if (!entry.firstBlockOpen) return;diff --git a/src/wiring.ts b/src/wiring.tsindex 2e0843262..49f7ff458 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -27,7 +27,7 @@ import { createFlyTunnelManager, parseFlyWireguardPeers } from "./deploy/fly-tunimport type { FlyPeerClaim } from "./deploy/fly-peer-claims.ts";import { createMemoryEventBus } from "./util/event-bus.ts";import { createPostgresNotifyBus } from "./persistence/postgres-notify-bus.ts";-import { emitRunText, type RunStreamEvent } from "./runs/run-stream-events.ts";+import { emitRunReasoning, emitRunText, reasoningKey, type RunStreamEvent } from "./runs/run-stream-events.ts";import { createPostgresResourceSearch } from "./search/resource-search.ts";import { createSessionMailbox, type SessionMessage } from "./sessions/session-mailbox.ts";import type { TaskAckState } from "./slack/task-ack.ts";@@ -1700,6 +1700,7 @@ export function buildApp(const refreshRunStream = (runId: string): void => runStreamEvents.emit({ runId, kind: "refresh" });const turnStream = createTurnStream({onDelta: (runId, text, offset, startOffset) => emitRunText(runStreamEvents, runId, text, offset, startOffset),+ onReasoning: (runId, block, text, offset) => emitRunReasoning(runStreamEvents, runId, block, text, offset),onChange: refreshRunStream,});const stopStreamSync = runStreamEvents.subscribe((event) => {@@ -1708,6 +1709,10 @@ export function buildApp(const startOffset = turnStream.startOffset(event.runId);const offset = Math.max(event.offset, startOffset);if (text) emitRunText(runStreamEvents, event.runId, text.slice(offset - startOffset), offset, startOffset);+ for (const block of turnStream.reasoning(event.runId)) {+ const sent = event.reasoning?.[reasoningKey(block)] ?? 0;+ if (block.text.length > sent) emitRunReasoning(runStreamEvents, event.runId, block, block.text.slice(sent), sent);+ }});runs.onTerminal((run) => refreshRunStream(run.id));const sessionStateBus: SessionStateBus = config.databaseUrldiff --git a/test/orchestrator.test.ts b/test/orchestrator.test.tsindex 1181bd6d6..40531764c 100644--- a/test/orchestrator.test.ts+++ b/test/orchestrator.test.ts@@ -3776,7 +3776,13 @@ test("thinking always reaches the run activity feed, with the model-bound signatconst onView = await runViewFor("!think open plan");const published = (onView?.activity ?? []).filter((a) => a.type === "thinking");assert.equal(published.length, 1, "thinking is in the live feed");- assert.deepEqual(published[0]!.payload, { thinking: "open plan" }, "signature stripped from the published payload");+ const step = (entries[0]!.payload as { step: { id: string; index: number } }).step;+ assert.deepEqual(+ published[0]!.payload,+ { thinking: "open plan", step },+ "signature stripped; the step identifies the live block it replaces",+ );+ assert.equal(onView?.reasoning, undefined, "the streamed block is settled once its entry is published");});test("channel session for an all-internal audience runs and is channel-scoped", async () => {diff --git a/test/pi-harness-cancel-exit.test.ts b/test/pi-harness-cancel-exit.test.tsindex 00522c1b1..83e44b0c4 100644--- a/test/pi-harness-cancel-exit.test.ts+++ b/test/pi-harness-cancel-exit.test.ts@@ -295,3 +295,61 @@ for (const shutdown of [false, true]) {}});}++function thinkingThenStall(signal: AbortSignal | null | undefined): Response {+ const events = textReplyEvents("Cache is warm.").slice(0, 1);+ events.push(+ { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },+ { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "**Checking** the " } },+ { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "cache" } },+ );+ const body = events.map((event) => `event: ${event.type as string}\ndata: ${JSON.stringify(event)}\n\n`).join("");+ const stream = new ReadableStream<Uint8Array>({+ start(controller) {+ signal?.addEventListener("abort", () => controller.error(abortShapedError()), { once: true });+ controller.enqueue(new TextEncoder().encode(body));+ },+ });+ return new Response(stream, { status: 200, headers: { "content-type": "text/event-stream" } });+}++test("reasoning streams before its thinking entry is persisted, and a cancel mid-reasoning persists it under the streamed id", async () => {+ const harness = createPiHarness({ apiKey: "sk-test" });+ const controller = new AbortController();+ const sink = { entries: [] as Array<{ seq: number; type: string; payload: unknown }> };+ const streamed: Array<{ step: string; index: number; chunk: string; persistedSoFar: number }> = [];+ const realFetch = globalThis.fetch;+ globalThis.fetch = (async (_url: string | URL | Request, init?: RequestInit) =>+ thinkingThenStall(init?.signal)) as typeof globalThis.fetch;+ try {+ const result = await harness.turns.runTurn(+ cancelTurn("reasoning-stream", controller.signal, sink, {+ onReasoningDelta: (step, index, chunk) => {+ streamed.push({+ step,+ index,+ chunk,+ persistedSoFar: sink.entries.filter((e) => e.type === "thinking").length,+ });+ if (chunk === "cache") controller.abort();+ },+ }),+ );+ assert.equal(result.stopped, true);+ assert.equal(streamed.map((s) => s.chunk).join(""), "**Checking** the cache");+ assert.ok(+ streamed.every((s) => s.persistedSoFar === 0),+ "reasoning is visible before the block is persisted",+ );+ const persisted = sink.entries.filter((e) => e.type === "thinking");+ assert.equal(persisted.length, 1);+ const payload = persisted[0]!.payload as { thinking: string; step: { id: string; index: number } };+ assert.equal(payload.thinking, "**Checking** the cache");+ assert.deepEqual(+ new Set(streamed.map((s) => `${s.step}:${s.index}`)),+ new Set([`${payload.step.id}:${payload.step.index}`]),+ );+ } finally {+ globalThis.fetch = realFetch;+ }+});diff --git a/test/run-events.test.ts b/test/run-events.test.tsindex a96b1974a..bf74e148e 100644--- a/test/run-events.test.ts+++ b/test/run-events.test.ts@@ -6,7 +6,7 @@ import { runEventRoutes } from "../src/api/routes/run-events.ts";import type { ApiCtx } from "../src/api/routes/route.ts";import type { App } from "../src/api/app.ts";import { createMemoryEventBus } from "../src/util/event-bus.ts";-import { emitRunText, type RunStreamEvent } from "../src/runs/run-stream-events.ts";+import { emitRunReasoning, emitRunText, type RunStreamEvent } from "../src/runs/run-stream-events.ts";import type { SessionEntry } from "../src/types.ts";const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));@@ -23,6 +23,7 @@ test("run events push deltas without snapshot polling and recover a separate worreads++;return {status,+ attempt: 1,startedAt: 0,finishedAt: null,result: status === "done" ? { status: "ok", reply: workerText } : null,@@ -73,7 +74,7 @@ test("run events push deltas without snapshot polling and recover a separate wor}assert.equal(syncCalls - beforeSync, 1, "later gaps must not start overlapping catch-up replays");holdSync = false;- app.syncRunStream("run", 14);+ app.syncRunStream("run", 14, {});await sleep(50);assert.match(wire, /lost then found/);status = "done";@@ -105,6 +106,7 @@ test("run events deliver a pending activity snapshot before subsequent text and}return {status,+ attempt: 1,startedAt: 0,finishedAt: null,result: status === "done" ? { status: "ok", reply: workerText } : null,@@ -201,6 +203,7 @@ async function streamToolRun(entries: SessionEntry[], state: { status: "running"async getRun() {return {status: state.status,+ attempt: 1,startedAt: 0,finishedAt: null,result: state.status === "done" ? { status: "ok", reply: "done" } : null,@@ -411,3 +414,98 @@ test(}},);++test("run events forward reasoning per block, dedupe overlap, and catch up gaps through sync", async () => {+ const bus = createMemoryEventBus<RunStreamEvent>("test");+ const worker = { step: "s1", index: 0, text: "**Checking** the cache" };+ const syncs: Array<Record<string, number> | undefined> = [];+ const app = {+ async getRun() {+ return { status: "running", attempt: 1, startedAt: 0, finishedAt: null, result: null };+ },+ subscribeRun(_runId, listener, onResync) {+ return bus.subscribe(listener, { onResync });+ },+ syncRunStream(runId, _offset, reasoning) {+ syncs.push(reasoning);+ const sent = reasoning?.["s1:0"] ?? 0;+ if (worker.text.length > sent) emitRunReasoning(bus, runId, worker, worker.text.slice(sent), sent);+ },+ async getRunToolEntries() {+ return [];+ },+ } as Pick<App, "getRun" | "getRunToolEntries" | "subscribeRun" | "syncRunStream">;+ const server = createServer((req, res) => {+ void runEventRoutes[0]!.handle({ app, req, res, params: { id: "run" } } as unknown as ApiCtx);+ });+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ const controller = new AbortController();+ try {+ const response = await fetch(`http://127.0.0.1:${(server.address() as AddressInfo).port}`, {+ signal: controller.signal,+ });+ const reader = response.body!.getReader();+ let wire = "";+ void (async () => {+ while (true) {+ const next = await reader.read();+ if (next.done) return;+ wire += new TextDecoder().decode(next.value);+ }+ })().catch(() => {});+ await sleep(100);+ const frames = () =>+ wire+ .split("\n\n")+ .map((chunk) => chunk.split("\n").find((line) => line.startsWith("data: ")))+ .filter((line): line is string => !!line)+ .map((line) => JSON.parse(line.slice(6)) as { name?: string; value?: Record<string, unknown> })+ .filter((frame) => frame.name === "reasoning")+ .map((frame) => frame.value!);+ assert.equal(+ frames()+ .map((v) => v.delta)+ .join(""),+ worker.text,+ "connect-time sync replays the live block",+ );+ bus.emit({ runId: "run", kind: "reasoning", step: "s1", index: 0, offset: 12, text: " the cache" });+ worker.text += " and the queue";+ bus.emit({ runId: "run", kind: "reasoning", step: "s1", index: 0, offset: 22, text: " and the queue" });+ await sleep(50);+ assert.equal(+ frames()+ .map((v) => v.delta)+ .join(""),+ worker.text,+ "overlapping reasoning is sent once",+ );+ assert.deepEqual(+ frames().map((v) => [v.step, v.index, v.offset]),+ [+ ["s1", 0, 0],+ ["s1", 0, 22],+ ],+ );+ syncs.length = 0;+ worker.text += " then more";+ bus.emit({ runId: "run", kind: "reasoning", step: "s1", index: 0, offset: 40, text: "more" });+ await sleep(50);+ assert.deepEqual(syncs, [{ "s1:0": 36 }], "a gap asks the owner for exactly what this stream lacks");+ assert.equal(+ frames()+ .map((v) => v.delta)+ .join(""),+ worker.text,+ );+ for (let i = 1; i <= 10; i++)+ bus.emit({ runId: "run", kind: "reasoning", step: `s${i + 1}`, index: 0, offset: 0, text: "x" });+ syncs.length = 0;+ bus.emit({ runId: "run", kind: "reasoning", step: "s12", index: 0, offset: 5, text: "gap" });+ await sleep(50);+ assert.equal(Object.keys(syncs[0] ?? {}).length, 8, "sync requests stay small however many blocks a run streams");+ } finally {+ controller.abort();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ }+});diff --git a/test/turn-stream.test.ts b/test/turn-stream.test.tsindex 855501261..f475c736e 100644--- a/test/turn-stream.test.ts+++ b/test/turn-stream.test.ts@@ -446,3 +446,36 @@ test("monitor wakes persist engagement after the reply gate", async () => {await built.runtime.stop();}});++test("reasoning streams per block beside the reply text, capped, and settles when persisted", () => {+ const published: Array<{ step: string; index: number; text: string; offset: number }> = [];+ const s = createTurnStream({+ onReasoning: (_runId, block, text, offset) => published.push({ ...block, text, offset }),+ });+ s.publishReasoning("r1", "step-a", 0, "**Plan**");+ s.publishReasoning("r1", "step-a", 0, " the fix");+ s.publishReasoning("r1", "step-a", 2, "second block");+ s.publish("r1", "Answer");+ assert.equal(s.snapshot("r1"), "Answer", "reasoning never enters the reply text");+ assert.deepEqual(s.reasoning("r1"), [+ { step: "step-a", index: 0, text: "**Plan** the fix" },+ { step: "step-a", index: 2, text: "second block" },+ ]);+ assert.deepEqual(+ published.map(({ index, offset }) => [index, offset]),+ [+ [0, 0],+ [0, 8],+ [2, 0],+ ],+ );+ s.settleReasoning("r1", "step-a", 0);+ assert.deepEqual(s.reasoning("r1"), [{ step: "step-a", index: 2, text: "second block" }]);+ s.publishReasoning("r1", "step-b", 0, "x".repeat(25_000));+ s.publishReasoning("r1", "step-b", 0, "more");+ assert.equal(s.reasoning("r1").find((b) => b.step === "step-b")?.text.length, 20_000);+ assert.equal(published.at(-1)?.text.length, 20_000, "nothing is published past the cap");+ s.end("r1");+ s.begin("r1");+ assert.deepEqual(s.reasoning("r1"), [], "a retried attempt never serves the previous attempt's blocks");+});