diff --git a/src/adapters/claude.ts b/src/adapters/claude.ts index b7293039..80976625 100644 --- a/src/adapters/claude.ts +++ b/src/adapters/claude.ts @@ -63,6 +63,59 @@ export class Claude extends DirectAdapter implements Adapter { private requests = new Map(); private session: string; private messageIds = new Map(); + // Assistant item ids already emitted for child-session (Task tool) streams. + // Keyed by the item id, because a stream's id can switch before + // `message_start` arrives. An id already emitted must never be established + // again: the projector replaces an item by id, so an empty re-established + // item would overwrite the child's saved text. Cleared at the result and + // again at the next new user turn, so a delayed delta re-establishes its + // commentary item in the fresh projector window. + private childItems = new Set(); + // Child item ids whose stream completed in an earlier turn. A completed + // child stream is frozen: a late delta or assistant message for it must + // never re-create its item in a later run (that would move or overwrite + // the earlier run's native row and misattribute the output), so such + // events are ignored entirely. Ids are harvested at each turn's result + // and kept for the adapter's lifetime — they are ids only, and evicting + // one would let a late event treat a completed stream as new. + private priorChildIds = new Set(); + // Last-known text per child item id for the current (or upcoming) turn: + // the full assistant message (written whether a turn is open or not) plus + // any deltas seen before the next prompt. Kept across prompt() (unlike + // childItems) so a re-established item in a fresh window carries the known + // text instead of replacing it with empty; cleared at the result, where + // its ids move into priorChildIds. Each entry is capped at the projector's + // display-preview limit and the map is bounded by both bytes and entry + // count, so a long session cannot grow adapter memory with copies of + // child output. + private childText = new Map(); + private childTextBytes = 0; + private rememberChildText(id: string, text: string) { + // Keep only a display-sized tail, mirroring the projector's own preview + // bound, so one long-lived stream cannot grow its entry without limit. + if (Buffer.byteLength(text) > 64000) { + let low = 0; + let high = text.length; + while (low < high) { + const keep = Math.ceil((low + high) / 2); + if (Buffer.byteLength(text.slice(-keep)) <= 64000) low = keep; + else high = keep - 1; + } + text = text.slice(-low); + } + const previous = this.childText.get(id); + const previousBytes = previous ? Buffer.byteLength(previous) : 0; + this.childTextBytes += Buffer.byteLength(text) - previousBytes; + this.childText.set(id, text); + while ( + (this.childTextBytes > 4 * 1024 * 1024 || this.childText.size > 512) && + this.childText.size > 1 + ) { + const oldest = this.childText.keys().next().value!; + this.childTextBytes -= Buffer.byteLength(this.childText.get(oldest)!); + this.childText.delete(oldest); + } + } private interrupted = false; private active = false; private steering = new ClaudeSteering(); @@ -299,6 +352,17 @@ export class Claude extends DirectAdapter implements Adapter { async prompt(text: string, beforeDispatch?: () => void) { beforeDispatch?.(); this.interrupted = false; + // A new user turn clears the child ids: attaching the run resets the + // projector's item window, and an item folded between runs has no run, + // so a post-dispatch delta must re-establish its commentary item (seeded + // from childText) or the projector would create a phase-less fragment + // that is eligible as the answer. Mid-turn steering follow-ups also call + // prompt() while a turn is active; they keep the ids so their deltas + // still merge into the open items. Trade-off: if a stream's seed was + // evicted from childText (over 512 streams or 4 MiB in one turn cycle), + // the re-established item carries only the new fragment, so that + // stream's progress preview loses earlier text. It is never phase-less. + if (!this.active) this.childItems.clear(); this.active = true; await this.native.write({ type: "user", @@ -526,12 +590,65 @@ export class Claude extends DirectAdapter implements Adapter { if (this.messageIds.size > 128) this.messageIds.delete(this.messageIds.keys().next().value!); } - if (e.type === "content_block_delta" && e.delta.type === "text_delta") - this.emit({ - type: "delta", - id: this.messageIds.get(stream) ?? m.uuid, - text: e.delta.text, - }); + if (e.type === "content_block_delta" && e.delta.type === "text_delta") { + const id = this.messageIds.get(stream) ?? m.uuid; + // A child stream whose turn completed is frozen; a late delta must + // not re-create its item in this run (it would move or overwrite + // the earlier run's native row), so ignore it entirely. + if (m.parent_tool_use_id && this.priorChildIds.has(id)) return; + if (m.parent_tool_use_id && !this.active) { + // Between turns the projector may drop a bare delta (no run) or + // attach the next run during recall before active flips true. In + // both cases a bare delta could leave a phase-less item that is + // selectable as the answer, so fold the delta into a single + // commentary item carrying the accumulated seed: the item is + // never phase-less and its text preserves the stream (including + // an evicted seed's id — the item then carries the fragment, not + // an empty seed). No separate delta is emitted here. + this.rememberChildText( + id, + (this.childText.get(id) ?? "") + e.delta.text, + ); + this.emit({ + type: "item", + item: { + id, + kind: "assistant", + text: this.childText.get(id) ?? "", + phase: "commentary", + }, + }); + } else if (m.parent_tool_use_id && !this.childItems.has(id)) { + this.childItems.add(id); + this.emit({ + type: "item", + item: { + id, + kind: "assistant", + // Seed with any text already known for this id (a full child + // assistant message or deltas seen earlier); the projector + // replaces by id, so an empty item would drop it. + text: this.childText.get(id) ?? "", + phase: "commentary", + }, + }); + } + // Keep the seed current with every active delta (the inactive fold + // above already accumulated its delta), so a later re-established + // item carries everything the stream has said so far and a + // subsequent delta appends instead of overwriting it. Empty deltas + // contribute nothing, so they do not add a cache entry. + if (m.parent_tool_use_id && this.active && e.delta.text) + this.rememberChildText( + id, + (this.childText.get(id) ?? "") + e.delta.text, + ); + // Root (parent) streams always emit deltas; child streams emit them + // only while a turn is open (while inactive their delta was folded + // into the commentary item above). + if (!m.parent_tool_use_id || this.active) + this.emit({ type: "delta", id, text: e.delta.text }); + } if ( e.type === "content_block_start" && e.content_block.type === "tool_use" @@ -558,11 +675,31 @@ export class Claude extends DirectAdapter implements Adapter { .filter((b: any) => b.type === "text") .map((b: any) => b.text) .join("\n"); - if (text) + // A child stream whose turn completed is frozen: a late assistant + // message for it must not re-create its item, but its tool updates are + // still emitted below so the trace keeps the tool arguments. + const frozen = + m.parent_tool_use_id && this.priorChildIds.has(m.message.id); + if (text && !frozen) { + // Remember the emitted id and its text so a later delta merges into + // this item instead of establishing an empty one that replaces it. + if (m.parent_tool_use_id) { + this.childItems.add(m.message.id); + this.rememberChildText(m.message.id, text); + } this.emit({ type: "item", - item: { id: m.message.id, kind: "assistant", text }, + item: { + id: m.message.id, + kind: "assistant", + text, + // A Task tool (subagent) session's assistant message must not + // become the parent run's answer; commentary keeps it visible + // as progress while the projector excludes it from selection. + ...(m.parent_tool_use_id ? { phase: "commentary" as const } : {}), + }, }); + } for (const b of content) if (b.type === "tool_use") { this.activityInputs.set(b.id, { name: b.name, input: b.input }); @@ -604,7 +741,17 @@ export class Claude extends DirectAdapter implements Adapter { if (usage) this.emit({ type: "usage", usage }); if (this.steering.result()) return; this.active = false; + // The turn's child streams are now complete; freeze their ids so late + // events for them cannot move or overwrite this run's native history. + // The set is ids-only and kept for the adapter's lifetime: evicting an + // id would let a late event treat a completed stream as new and + // overwrite its recorded history. + for (const id of this.childItems) this.priorChildIds.add(id); + for (const id of this.childText.keys()) this.priorChildIds.add(id); this.messageIds.clear(); + this.childItems.clear(); + this.childText.clear(); + this.childTextBytes = 0; for (const id of this.requests.keys()) this.emit({ type: "resolved", id }); this.requests.clear(); diff --git a/src/server/native/projector.ts b/src/server/native/projector.ts index 88e0138f..02777324 100644 --- a/src/server/native/projector.ts +++ b/src/server/native/projector.ts @@ -536,6 +536,9 @@ export class NativeProjector { live.binding.conversation_id, run.id, ); + // assistantHistory's SQL excludes phase "commentary" (and this in-memory + // pass already excluded anything still in the window), so this fallback + // only ever sees evicted non-commentary assistant items. for (const row of stored) { const item = JSON.parse(row.item) as Item; // A pending in-memory replacement may have cleared this saved reply diff --git a/test/fixtures/harness.mjs b/test/fixtures/harness.mjs index 9cb54b75..c8e5ab0b 100755 --- a/test/fixtures/harness.mjs +++ b/test/fixtures/harness.mjs @@ -155,9 +155,45 @@ function finish(status = "completed", failure = "Fixture native turn failed") { type: "assistant", message: { id: active, - content: [{ type: "text", text: "Fixture done" }], + content: process.env.LOOP_TEST_CLAUDE_PARENT_BLANK + ? [] + : [{ type: "text", text: "Fixture done" }], }, }); + // Child-session (Task tool) output between the parent's final assistant + // message and the parent result. LOOP_TEST_CLAUDE_CHILD_TEXT also sends + // the full child assistant message; LOOP_TEST_CLAUDE_CHILD_DELTA_ONLY + // sends only the child stream, which can outlive its assistant message. + const childText = process.env.LOOP_TEST_CLAUDE_CHILD_TEXT; + const childDeltaOnly = process.env.LOOP_TEST_CLAUDE_CHILD_DELTA_ONLY; + if (childText || childDeltaOnly) { + const childId = "child-" + count; + const childParent = "task-" + count; + const text = childText || childDeltaOnly; + out({ + type: "stream_event", + parent_tool_use_id: childParent, + event: { type: "message_start", message: { id: childId } }, + }); + out({ + type: "stream_event", + parent_tool_use_id: childParent, + event: { + type: "content_block_delta", + delta: { type: "text_delta", text }, + }, + }); + if (childText) + out({ + type: "assistant", + parent_tool_use_id: childParent, + message: { + id: childId, + role: "assistant", + content: [{ type: "text", text: childText }], + }, + }); + } out({ type: "result", subtype: "success", @@ -789,13 +825,14 @@ if (kind !== "opencode") { type: "stream_event", event: { type: "message_start", message: { id: active } }, }); - out({ - type: "stream_event", - event: { - type: "content_block_delta", - delta: { type: "text_delta", text: "Fixture " }, - }, - }); + if (!process.env.LOOP_TEST_CLAUDE_PARENT_BLANK) + out({ + type: "stream_event", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "Fixture " }, + }, + }); out({ type: "assistant", message: { diff --git a/test/native-activity-ingress.test.ts b/test/native-activity-ingress.test.ts index 27e7007a..57e80c72 100644 --- a/test/native-activity-ingress.test.ts +++ b/test/native-activity-ingress.test.ts @@ -263,7 +263,7 @@ it("records native turn tokens through adapter ingress without replay or cross-t expect(() => conversationUsage("intruder", "conv")).toThrow(); }); -it("records Claude result usage including cached inputs, excluding foreign results", () => { +it("records Claude result usage including cached inputs, excluding foreign results", async () => { const run = turn("conv", "usage-claude"); const sessions = new NativeSessions(); const current = start(sessions, "usage-claude-generation", run); @@ -305,7 +305,969 @@ it("records Claude result usage including cached inputs, excluding foreign resul output_tokens: 20, cache_read_tokens: 50, }); + await sessions.close(); }); + +// The projector picks the last non-commentary assistant item with text as a +// completed run's answer (projectOutput), so child-session output must carry +// phase "commentary" at ingress to stay out of that selection. +const retainedAnswer = (live: Live) => + [...live.items.values()].findLast( + (i) => i.kind === "assistant" && i.phase !== "commentary" && i.text.trim(), + ); + +it("marks Claude child-session assistant messages as commentary so they cannot become the run's answer", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-child-commentary"); + const current = start(sessions, "claude-child-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-child-commentary", + }, + current.emit, + ) as any; + adapter.active = true; + const send = (m: any) => + adapter.message({ session_id: "claude-child-commentary", ...m }); + send({ + type: "assistant", + parent_tool_use_id: null, + message: { + id: "parent-msg", + role: "assistant", + content: [{ type: "text", text: "Parent final" }], + }, + }); + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-msg", + role: "assistant", + content: [{ type: "text", text: "Child text" }], + }, + }); + // Parent-session items keep their exact shape; the child is commentary. + expect(current.live.items.get("parent-msg")).toEqual({ + id: "parent-msg", + kind: "assistant", + text: "Parent final", + }); + expect(current.live.items.get("child-msg")).toEqual({ + id: "child-msg", + kind: "assistant", + text: "Child text", + phase: "commentary", + }); + expect(retainedAnswer(current.live)?.id).toBe("parent-msg"); + await sessions.close(); +}); + +it("establishes a commentary item before a child stream's first delta", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-child-delta"); + const current = start(sessions, "claude-child-delta-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-child-delta", + }, + current.emit, + ) as any; + adapter.active = true; + const send = (m: any) => + adapter.message({ session_id: "claude-child-delta", ...m }); + send({ + type: "stream_event", + parent_tool_use_id: "task-2", + event: { type: "message_start", message: { id: "child-stream" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-2", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "Child stream text" }, + }, + }); + // The delta merged into the commentary item the adapter established first; + // a turn ending here (no parent text) must not select the child stream. + expect(current.live.items.get("child-stream")).toEqual({ + id: "child-stream", + kind: "assistant", + text: "Child stream text", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("merges a child delta into an earlier child assistant message instead of replacing its text", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-child-late-delta"); + const current = start(sessions, "claude-child-late-delta-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-child-late-delta", + }, + current.emit, + ) as any; + adapter.active = true; + const send = (m: any) => + adapter.message({ session_id: "claude-child-late-delta", ...m }); + // Full child assistant message arrives first, then its stream's delta. + send({ + type: "assistant", + parent_tool_use_id: "task-3", + message: { + id: "child-late", + role: "assistant", + content: [{ type: "text", text: "Full child text" }], + }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-3", + event: { type: "message_start", message: { id: "child-late" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-3", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " and more" }, + }, + }); + // The delta merged into the existing commentary item; the earlier text is + // not overwritten by an empty re-established item. + expect(current.live.items.get("child-late")).toEqual({ + id: "child-late", + kind: "assistant", + text: "Full child text and more", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("ignores late child events for a stream whose turn already completed", async () => { + const sessions = new NativeSessions(); + const first = turn("conv", "claude-cross-turn-1"); + const current = start(sessions, "claude-cross-turn-generation", first); + sessions.state.startTurn(current.live, first); + const emitted: AdapterEvent[] = []; + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-cross-turn", + }, + (e) => { + emitted.push(e); + current.emit(e); + }, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-cross-turn", ...m }); + // Turn 1: the child session emits an assistant message, then the turn ends. + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-cross", + role: "assistant", + content: [{ type: "text", text: "Child text one" }], + }, + }); + adapter.message({ + type: "result", + session_id: "claude-cross-turn", + is_error: false, + }); + // Between turns the child re-emits content for the completed stream, and + // after the next prompt a delayed delta arrives. Both are late events for + // a frozen stream and must be ignored: re-creating the item would move or + // overwrite turn 1's native row and misattribute the output. + emitted.length = 0; + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-cross", + role: "assistant", + content: [{ type: "text", text: "Child text two" }], + }, + }); + await adapter.prompt("next"); + sessions.state.startTurn(current.live, turn("conv", "claude-cross-turn-2")); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-cross" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " late" }, + }, + }); + expect( + emitted.filter((e) => e.type === "item" || e.type === "delta"), + ).toEqual([]); + expect(current.live.items.get("child-cross")).toBeUndefined(); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("does not overwrite a recall-window child message with a later delta's empty item", async () => { + const sessions = new NativeSessions(); + const first = turn("conv", "claude-recall-1"); + const current = start(sessions, "claude-recall-generation", first); + sessions.state.startTurn(current.live, first); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-recall", + }, + current.emit, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-recall", ...m }); + // Turn 1 ends. + adapter.message({ + type: "result", + session_id: "claude-recall", + is_error: false, + }); + // The next run attaches; a delayed full child message arrives during the + // recall window, before the next prompt is dispatched. + sessions.state.startTurn(current.live, turn("conv", "claude-recall-2")); + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-recall", + role: "assistant", + content: [{ type: "text", text: "Full recall text" }], + }, + }); + // A first delta also arrives during recall, before the prompt is sent. + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-recall" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " A" }, + }, + }); + await adapter.prompt("next"); + // The child stream's second delta arrives after the new prompt. + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " B" }, + }, + }); + // The re-established item carried the recall text including its first + // delta, so the second delta appended instead of overwriting either. + expect(current.live.items.get("child-recall")).toEqual({ + id: "child-recall", + kind: "assistant", + text: "Full recall text A B", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("caps a single recall stream's cached seed at the display limit", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-cache-cap"); + const current = start(sessions, "claude-cache-cap-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-cache-cap", + }, + current.emit, + ) as any; + adapter.active = true; + const send = (m: any) => + adapter.message({ session_id: "claude-cache-cap", ...m }); + // Turn 1 ends; one long-lived child stream keeps streaming while no turn + // is open. + adapter.message({ + type: "result", + session_id: "claude-cache-cap", + is_error: false, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-big" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "x".repeat(200_000) }, + }, + }); + const stored = adapter.childText.get("child-big") as string; + expect(Buffer.byteLength(stored)).toBeLessThanOrEqual(64000); + expect(stored.endsWith("x".repeat(1000))).toBe(true); + await sessions.close(); +}); + +it("bounds the child text cache so a long session cannot accumulate child output", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-cache-bound"); + const current = start(sessions, "claude-cache-bound-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-cache-bound", + }, + current.emit, + ) as any; + // Enough 64,000-byte entries to exceed the 4 MiB budget, so the oldest + // ids are evicted and the cache stays bounded for the adapter's lifetime. + for (let i = 0; i < 70; i++) + adapter.rememberChildText("bound-" + i, "x".repeat(64000)); + expect(adapter.childText.has("bound-0")).toBe(false); + expect(adapter.childText.size).toBeLessThanOrEqual(66); + expect(adapter.childTextBytes).toBeLessThanOrEqual(4 * 1024 * 1024 + 64000); + await sessions.close(); +}); + +it("bounds the child text cache by entry count even for empty-text streams", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-cache-count"); + const current = start(sessions, "claude-cache-count-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-cache-count", + }, + current.emit, + ) as any; + // Many distinct ids whose text is empty or near-empty: the byte counter + // never moves, so eviction must also bound the entry count. + for (let i = 0; i < 600; i++) adapter.rememberChildText("empty-" + i, ""); + expect(adapter.childText.size).toBeLessThanOrEqual(512); + await sessions.close(); +}); + +it("keeps a post-prompt delta commentary even when its recall seed was evicted", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-seed-lost"); + const current = start(sessions, "claude-seed-lost-generation", run); + sessions.state.startTurn(current.live, run); + const emitted: AdapterEvent[] = []; + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-seed-lost", + }, + (e) => { + emitted.push(e); + current.emit(e); + }, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-seed-lost", ...m }); + // Turn 1 ends; the next run attaches and, during recall, the child's full + // message is retained in this run's window. + adapter.message({ + type: "result", + session_id: "claude-seed-lost", + is_error: false, + }); + sessions.state.startTurn(current.live, turn("conv", "claude-seed-lost-2")); + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-seed-lost", + role: "assistant", + content: [{ type: "text", text: "Retained text" }], + }, + }); + // The recall seed is evicted here (as after extreme cache pressure), but + // the retained commentary item in this run's window still holds the text. + adapter.childText.delete("child-seed-lost"); + await adapter.prompt("next"); + emitted.length = 0; + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-seed-lost" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " more" }, + }, + }); + // A new turn cleared the child ids, so the delta re-establishes its + // commentary item before appending. With the seed evicted, the item + // carries only the fragment (the documented eviction trade-off: the + // progress preview loses earlier text), but it is never phase-less, so it + // cannot become the run's answer. + expect(emitted).toEqual([ + { + type: "item", + item: { + id: "child-seed-lost", + kind: "assistant", + text: "", + phase: "commentary", + }, + }, + { type: "delta", id: "child-seed-lost", text: " more" }, + ]); + expect(current.live.items.get("child-seed-lost")).toEqual({ + id: "child-seed-lost", + kind: "assistant", + text: " more", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("keeps an active-turn child message's text when a delayed delta arrives in the next turn", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-delayed-delta"); + const current = start(sessions, "claude-delayed-delta-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-delayed-delta", + }, + current.emit, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-delayed-delta", ...m }); + // Turn 1, while active: the child's full assistant message arrives. + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-old", + role: "assistant", + content: [{ type: "text", text: "Full active text" }], + }, + }); + // Turn 1 ends with no further child content; turn 2 starts fresh. + adapter.message({ + type: "result", + session_id: "claude-delayed-delta", + is_error: false, + }); + await adapter.prompt("next"); + sessions.state.startTurn( + current.live, + turn("conv", "claude-delayed-delta-2"), + ); + // A delayed delta for the same child message id finally arrives. The + // stream's turn completed, so the event is ignored: re-creating the item + // would move turn 1's native row to this run and misattribute its text. + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-old" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " more" }, + }, + }); + // Turn 1's recorded child text is left alone; nothing is attributed to + // this run for the completed stream. + expect(current.live.items.get("child-old")).toBeUndefined(); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("keeps an active-turn child stream's text when a delayed delta arrives in the next turn", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-active-delta"); + const current = start(sessions, "claude-active-delta-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-active-delta", + }, + current.emit, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-active-delta", ...m }); + // Turn 1, while active: a delta-only child stream (no full message). + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-delta" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "A" }, + }, + }); + adapter.message({ + type: "result", + session_id: "claude-active-delta", + is_error: false, + }); + // A delayed delta for the same stream arrives. The stream's turn + // completed, so the event is ignored: the earlier text stays with its own + // run instead of being attributed to this one. + await adapter.prompt("next"); + sessions.state.startTurn(current.live, turn("conv", "claude-active-delta-2")); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-delta" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "B" }, + }, + }); + expect(current.live.items.get("child-delta")).toBeUndefined(); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("marks a delta-only child stream commentary when it arrives during recall", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-recall-delta"); + const current = start(sessions, "claude-recall-delta-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-recall-delta", + }, + current.emit, + ) as any; + adapter.active = true; + // Turn 1 ends; the next run attaches during recall, before prompt() would + // mark the adapter active again. + adapter.message({ + type: "result", + session_id: "claude-recall-delta", + is_error: false, + }); + sessions.state.startTurn(current.live, turn("conv", "claude-recall-delta-2")); + // A delta-only child stream arrives in that recall window (no full message). + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "recall-delta" } }, + }); + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "Recall fragment" }, + }, + }); + // The item is commentary, never phase-less: it cannot become the answer + // even though the run is already attached and the adapter is inactive. + expect(current.live.items.get("recall-delta")).toEqual({ + id: "recall-delta", + kind: "assistant", + text: "Recall fragment", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("keeps between-run child deltas commentary even across a window reset", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-between-2"); + const current = start(sessions, "claude-between-2-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-between-2", + }, + current.emit, + ) as any; + adapter.active = true; + // Turn 1 ends; the first child delta arrives between runs. + adapter.message({ + type: "result", + session_id: "claude-between-2", + is_error: false, + }); + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "between-2" } }, + }); + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "A" }, + }, + }); + // The next run attaches, resetting the projector's item window, and the + // second child delta arrives pre-prompt. + sessions.state.startTurn(current.live, turn("conv", "claude-between-2b")); + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "between-2" } }, + }); + adapter.message({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "B" }, + }, + }); + // Each between-run delta produced a commentary item; the fragment joined + // and is never phase-less, so it cannot become the run's answer. + expect(current.live.items.get("between-2")).toEqual({ + id: "between-2", + kind: "assistant", + text: "AB", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("re-establishes a between-run child stream as commentary after the next prompt", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-between-3"); + const current = start(sessions, "claude-between-3-generation", run); + sessions.state.startTurn(current.live, run); + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-between-3", + }, + current.emit, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-between-3", ...m }); + // Turn 1 ends; the child's first delta arrives between runs and is folded + // into a commentary item that no run owns. + adapter.message({ + type: "result", + session_id: "claude-between-3", + is_error: false, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "between-3" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "A" }, + }, + }); + // The next run attaches (resetting the item window) and is dispatched; + // only then does the child's next delta arrive. + sessions.state.startTurn(current.live, turn("conv", "claude-between-3b")); + await adapter.prompt("next"); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: "B" }, + }, + }); + // The delta re-established its seeded commentary item in the new window + // instead of leaving a phase-less fragment eligible as the answer. + expect(current.live.items.get("between-3")).toEqual({ + id: "between-3", + kind: "assistant", + text: "AB", + phase: "commentary", + }); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("drops late events for a completed child stream even after thousands of other ids", async () => { + const sessions = new NativeSessions(); + const run = turn("conv", "claude-prior-many"); + const current = start(sessions, "claude-prior-many-generation", run); + sessions.state.startTurn(current.live, run); + const emitted: AdapterEvent[] = []; + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-prior-many", + }, + (e) => { + emitted.push(e); + current.emit(e); + }, + ) as any; + adapter.active = true; + adapter.native = { write: async () => undefined }; + const send = (m: any) => + adapter.message({ session_id: "claude-prior-many", ...m }); + // One turn completes thousands of child streams; the first id must stay + // frozen no matter how many others complete after it. + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-first", + role: "assistant", + content: [{ type: "text", text: "First child text" }], + }, + }); + for (let i = 0; i < 6005; i++) + send({ + type: "assistant", + parent_tool_use_id: "task-" + (2 + i), + message: { + id: "overflow-" + i, + role: "assistant", + content: [{ type: "text", text: "x" }], + }, + }); + adapter.message({ + type: "result", + session_id: "claude-prior-many", + is_error: false, + }); + await adapter.prompt("next"); + sessions.state.startTurn(current.live, turn("conv", "claude-prior-many-2")); + emitted.length = 0; + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { type: "message_start", message: { id: "child-first" } }, + }); + send({ + type: "stream_event", + parent_tool_use_id: "task-1", + event: { + type: "content_block_delta", + delta: { type: "text_delta", text: " late" }, + }, + }); + // The completed stream stays frozen — its id is never evicted from the + // prior-id set, so the late delta cannot re-create or overwrite it. + expect( + emitted.filter((e) => e.type === "item" || e.type === "delta"), + ).toEqual([]); + expect(current.live.items.get("child-first")).toBeUndefined(); + expect(retainedAnswer(current.live)).toBeUndefined(); + await sessions.close(); +}); + +it("still emits a frozen child frame's tool update while suppressing its text", async () => { + const conversation = "claude-frozen-tool"; + seedConversation(db, { + id: conversation, + owner_id: "owner", + title: "t", + created_at: 0, + }); + db.prepare( + "INSERT INTO companion_sessions(worktree_agent_id,conversation_id,harness,project,updated_at) VALUES('fixture-agent',?,'codex',?,0)", + ).run(conversation, dir); + const sessions = new NativeSessions(); + const run = turn(conversation, "claude-frozen-tool"); + const current = start( + sessions, + "claude-frozen-tool-generation", + run, + conversation, + ); + sessions.state.startTurn(current.live, run); + const emitted: AdapterEvent[] = []; + const adapter = new Claude( + { + harness: "claude", + executable: "", + project: dir, + version: "fixture", + billing: "existing", + resume: "claude-frozen-tool", + }, + (e) => { + emitted.push(e); + current.emit(e); + }, + ) as any; + adapter.active = true; + const send = (m: any) => + adapter.message({ session_id: "claude-frozen-tool", ...m }); + // Turn 1 records the completed stream; a late frame re-emits it WITH a + // tool_use block alongside its text. + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-tool", + role: "assistant", + content: [{ type: "text", text: "Late text" }], + }, + }); + adapter.message({ + type: "result", + session_id: "claude-frozen-tool", + is_error: false, + }); + emitted.length = 0; + send({ + type: "assistant", + parent_tool_use_id: "task-1", + message: { + id: "child-tool", + role: "assistant", + content: [ + { type: "text", text: "Late text again" }, + { + type: "tool_use", + id: "tool-late", + name: "Read", + input: { file_path: "README.md" }, + }, + ], + }, + }); + // The frozen stream's text item is suppressed, but the frame's tool_use + // still yields its running item so the trace keeps the tool arguments. + expect( + emitted.filter((e) => e.type === "item" && e.item.kind === "assistant"), + ).toEqual([]); + expect(emitted).toContainEqual({ + type: "item", + item: { + id: "tool-late", + kind: "tool", + title: "Read", + text: JSON.stringify({ file_path: "README.md" }, null, 2), + status: "running", + }, + }); + await sessions.close(); +}); + const option = (id: string, confirmed: string) => ({ id, label: id, diff --git a/test/native-turn-recovery.test.ts b/test/native-turn-recovery.test.ts index 8ebad8a9..fbb55e3b 100644 --- a/test/native-turn-recovery.test.ts +++ b/test/native-turn-recovery.test.ts @@ -26,6 +26,8 @@ seedAgent(db, { const { enqueue } = await import("../src/server/queue"); const { tick, execute } = await import("../src/server/worker"); const { nativeSessions } = await import("../src/server/native-sessions"); +const { publishCompletedRunResult } = + await import("../src/server/result-publication"); for (const harness of ["claude", "codex"]) { db.prepare("INSERT INTO harnesses VALUES(?,?,'{}','{}','[]',0)").run( harness, @@ -51,6 +53,9 @@ for (const harness of ["claude", "codex"]) { afterEach(() => { delete process.env.LOOP_TEST_HARNESS; delete process.env.LOOP_TEST_CLAUDE_INJECTED_RESULTS; + delete process.env.LOOP_TEST_CLAUDE_CHILD_TEXT; + delete process.env.LOOP_TEST_CLAUDE_CHILD_DELTA_ONLY; + delete process.env.LOOP_TEST_CLAUDE_PARENT_BLANK; delete process.env.LOOP_TEST_CODEX_WRITER_BUSY; delete process.env.LOOP_TEST_AUDIT; }); @@ -130,6 +135,81 @@ it("keeps the user run open across injected Claude results and saves its actual expect(readFileSync(audit, "utf8")).toBe("prompt\n"); }); +// Child-session (Task tool) assistant output arrives between the parent's +// final assistant message and the parent `result`. It must stay visible as +// progress but never become the run's answer or the sealed Result body. +function sealedBody(runId: string) { + return publishCompletedRunResult(runId, "local-workspace", db)!.outcome.body; +} +function progressContent(runId: string, key: string) { + return ( + db + .prepare("SELECT content FROM messages WHERE run_id=? AND update_key=?") + .get(runId, key) as { content: string } | undefined + )?.content; +} + +it("seals the parent's text as the answer when a child assistant message follows it", async () => { + process.env.LOOP_TEST_HARNESS = "claude"; + process.env.LOOP_TEST_CLAUDE_CHILD_TEXT = "Subagent result text"; + const run = submit("claude-child-answer", "claude", "claude-child-answer"); + expect(await tick(execute)).toBe(true); + expect( + db.prepare("SELECT state,result,error FROM runs WHERE id=?").get(run.id), + ).toEqual({ state: "completed", result: "Fixture done", error: null }); + expect(sealedBody(run.id)).toBe("Fixture done"); + // The child's text is still recorded as a progress update on the run. + expect(progressContent(run.id, "native-message:child-1")).toBe( + "Subagent result text", + ); +}); + +it("does not seal child stream deltas when the child assistant message never arrives", async () => { + process.env.LOOP_TEST_HARNESS = "claude"; + process.env.LOOP_TEST_CLAUDE_CHILD_DELTA_ONLY = "Subagent stream text"; + const run = submit( + "claude-child-delta-only", + "claude", + "claude-child-delta-only", + ); + expect(await tick(execute)).toBe(true); + expect( + db.prepare("SELECT state,result,error FROM runs WHERE id=?").get(run.id), + ).toEqual({ state: "completed", result: "Fixture done", error: null }); + expect(sealedBody(run.id)).toBe("Fixture done"); + // The child stream's deltas still surface as progress on the run. + expect(progressContent(run.id, "native-message:child-1")).toBe( + "Subagent stream text", + ); +}); + +it("seals no result when the parent's final message has no text and only a child message does", async () => { + process.env.LOOP_TEST_HARNESS = "claude"; + process.env.LOOP_TEST_CLAUDE_PARENT_BLANK = "1"; + process.env.LOOP_TEST_CLAUDE_CHILD_TEXT = "Only the subagent speaks"; + const run = submit("claude-child-blank", "claude", "claude-child-blank"); + expect(await tick(execute)).toBe(true); + // No parent text is eligible, so the run's answer is empty and nothing is + // sealed from the child's text. + expect( + db.prepare("SELECT state,result FROM runs WHERE id=?").get(run.id), + ).toEqual({ + state: "completed", + result: "", + }); + expect(publishCompletedRunResult(run.id, "local-workspace", db)).toBeNull(); +}); + +it("keeps the same answer for a Claude turn without child sessions", async () => { + process.env.LOOP_TEST_HARNESS = "claude"; + const run = submit("claude-plain", "claude", "claude-plain"); + expect(await tick(execute)).toBe(true); + expect( + db.prepare("SELECT state,result,error FROM runs WHERE id=?").get(run.id), + ).toEqual({ state: "completed", result: "Fixture done", error: null }); + expect(sealedBody(run.id)).toBe("Fixture done"); +}); + it("reports a Codex writer conflict without dispatch, takeover or raw native error disclosure", async () => { process.env.LOOP_TEST_HARNESS = "codex"; process.env.LOOP_TEST_CODEX_WRITER_BUSY = "1";