Multiplayer agent harness for work.
Continue the Pi assistant turn on resume instead of prompting a note (#2058)
Regan Bell4d5cfd3dc17d
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex fb97f928a..c84feb4c0 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -3290,8 +3290,12 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {let turnInput = resumePlan? resumeNote({ strategy: resumePlan, backgroundJobs: !!backgroundBroker }): baseText;- if (partial && !history.some((entry) => entry.seq === partial.userSeq))+ const requestInHistory = !!partial && history.some((entry) => entry.seq === partial.userSeq);+ const continueTurn =+ (resumePlan?.kind === "continue" || resumePlan?.kind === "retry") && requestInHistory && !releasedToolOutput;+ if (partial && !requestInHistory)turnInput += `\nCurrent request (continue from recorded work; do not restart):\n${baseText}`;+ if (partial) spine.turnUserEntrySeq = partial.userSeq;if (releasedToolOutput) {turnInput = `The human released quarantined tool output recorded in the conversation. Continue the original task using that output. The tool action already ran; do not repeat it. Original task: ${baseText}`;}@@ -3747,6 +3751,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {...(codexTurnAuth ? { codexAuth: codexTurnAuth } : {}),...(input.runId ? { runId: input.runId } : {}),cancel: turnAbort.signal,+ ...(continueTurn && !continuation ? { continueTurn: true } : {}),input: harnessInput,...(!partial && messageTs ? { triggerTs: messageTs } : {}),...(!partial && entryTs ? { entryTs } : {}),@@ -4238,7 +4243,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {const turnCompleted = outcome.completed;const pausing = outcome.paused;if (input.runId && !pausing && reply && reply.trim()) deps.turnStream?.markReplyDone(input.runId);- const turnUserSeq = emittedEntries.find((e) => e.type === "user")?.seq;+ const turnUserSeq = partial?.userSeq ?? emittedEntries.find((e) => e.type === "user")?.seq;let metricProvisionMs: number | undefined;if (box.provisionMs !== undefined) metricProvisionMs = box.provisionMs;else if (scratchBox.provisionMs !== undefined) metricProvisionMs = scratchBox.provisionMs;@@ -4389,7 +4394,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {};let finalResult: TurnResult;- const sourceUserSeq = partial?.userSeq ?? emittedEntries.find((e) => e.type === "user")?.seq;+ const sourceUserSeq = turnUserSeq;const sourceAssistantEntrySeq = [...emittedEntries].reverse().find((e) => e.type === "assistant")?.seq;if (cancelStopped && !result.pendingApprovals?.length) {finalResult = { status: "silent", sessionId: session.id, stopped: true };diff --git a/src/core/turn-resume.ts b/src/core/turn-resume.tsindex 4b6b16225..2e4e89440 100644--- a/src/core/turn-resume.ts+++ b/src/core/turn-resume.ts@@ -16,6 +16,8 @@ export function isResumeNote(text: string): boolean {return text.trimStart().startsWith(NOTE_HEAD);}+const MODEL_TURN_ENTRY_TYPES = new Set<SessionEntry["type"]>(["user", "assistant", "tool_call", "tool_result"]);+export function findTrailingPartialTurn(entries: readonly SessionEntry[], inputText: string): PartialTurn | null {const text = inputText.trim();if (!text) return null;@@ -76,7 +78,8 @@ export interface ResumableToolCall {input: Record<string, unknown>;}-export type ResumeStrategy = { kind: "restart" } | { kind: "note" } | { kind: "retry"; call: ResumableToolCall };+export type ResumeStrategy =+ { kind: "restart" } | { kind: "note" } | { kind: "continue" } | { kind: "retry"; call: ResumableToolCall };export function resumeStrategy(entries: readonly SessionEntry[], partial: PartialTurn): ResumeStrategy {if (partial.workEntries === 0) return { kind: "restart" };@@ -89,6 +92,10 @@ export function resumeStrategy(entries: readonly SessionEntry[], partial: Partiaconst dangling = turn.filter((e) => e.type === "tool_call" && !answered.has((e.payload as { callId?: unknown } | null)?.callId),);+ if (dangling.length === 0) {+ const last = turn.findLast((e) => MODEL_TURN_ENTRY_TYPES.has(e.type));+ return last?.type === "tool_result" ? { kind: "continue" } : { kind: "note" };+ }if (dangling.length !== 1) return { kind: "note" };const payload = dangling[0]!.payload as {callId?: unknown;diff --git a/src/harness/harness-router.ts b/src/harness/harness-router.tsindex d822e6de7..0a828b8ce 100644--- a/src/harness/harness-router.ts+++ b/src/harness/harness-router.ts@@ -86,6 +86,7 @@ async function runTurnEnforcingGoal(const remaining = remainingWallMs();result = await adapter.turns.runTurn({...dispatched,+ continueTurn: undefined,input: note,history: [...input.history, ...emitted],goal,diff --git a/src/harness/harness.ts b/src/harness/harness.tsindex 33d9183a1..31673bce4 100644--- a/src/harness/harness.ts+++ b/src/harness/harness.ts@@ -103,6 +103,7 @@ export interface HarnessTurnInput {session: Session;runId?: string;cancel?: AbortSignal;+ continueTurn?: boolean;input: string;triggerTs?: string;entryTs?: string;diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.tsindex 70cb3267b..c7a3b2268 100644--- a/src/harness/pi-harness.ts+++ b/src/harness/pi-harness.ts@@ -102,7 +102,7 @@ import {type SeededMessage,} from "./replay.ts";import { assistantDroppedAtReplay, ELIDED_IMAGE_TEXT, planTapeSeed } from "./tape-fold.ts";-import { estimateHistoryTokens } from "./context-compaction.ts";+import { estimateHistoryTokens, INTERRUPTED_TOOL_RESULT } from "./context-compaction.ts";import { summarizeHistory } from "./history-summary.ts";import { countTokens } from "../util/tokens.ts";import {@@ -670,6 +670,15 @@ interface PiAgentWithPayloadHook {subscribe?(listener: (event: unknown) => Promise<void> | void): () => void;}+export function endsAtRecordedToolResult(messages: readonly unknown[]): boolean {+ const last = messages.at(-1) as { role?: unknown; content?: unknown } | undefined;+ return last?.role === "toolResult" && textFromContent(last.content) !== INTERRUPTED_TOOL_RESULT;+}++function continueAssistantTurn(session: AgentSession): Promise<void> {+ return (session as unknown as { _runAgentPrompt(messages: readonly unknown[]): Promise<void> })._runAgentPrompt([]);+}+interface PiSeedTarget {agent?: { state?: { messages?: unknown[] } };sessionManager?: { appendMessage?: (message: unknown) => unknown };@@ -1912,16 +1921,25 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {};turn.onGapWork?.(collectGapWork);entry.ref.onGapWork = collectGapWork;- const userEntry = await turn.emit({- type: "user",- payload: {- text: turn.input,- ...(turn.environment ? { environment: turn.environment } : {}),- ...((turn.triggerTs ?? turn.entryTs) ? { ts: turn.triggerTs ?? turn.entryTs } : {}),- ...(turn.attachments?.length ? { attachments: turn.attachments } : {}),- },- scopeLabel: turn.scopeLabel,- });+ const continueTurn = !!turn.continueTurn && endsAtRecordedToolResult(entry.agentSession.agent.state.messages);+ if (turn.continueTurn && !continueTurn)+ console.log(+ `[pi] resume cannot continue the assistant turn — seeded context does not end at a recorded tool result; prompting the resume note session=${turn.session.id}`,+ );+ const userEntry = continueTurn+ ? turn.history.find(+ (e) => e.type === "user" && (e.payload as { runId?: unknown } | null)?.runId === turn.runId,+ )+ : await turn.emit({+ type: "user",+ payload: {+ text: turn.input,+ ...(turn.environment ? { environment: turn.environment } : {}),+ ...((turn.triggerTs ?? turn.entryTs) ? { ts: turn.triggerTs ?? turn.entryTs } : {}),+ ...(turn.attachments?.length ? { attachments: turn.attachments } : {}),+ },+ scopeLabel: turn.scopeLabel,+ });const grindMeter = createGrindMeter();if (!entry.ref.goal) entry.ref.goal = rehydrateOpenGoal(turn.history);@@ -1942,7 +1960,10 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {entry.ref.pendingTransformContext = undefined;turn.recordModelCall({model: effectiveModel,- inputTokens: entry.composedPromptTokens + estimateHistoryTokens(turn.history) + countTokens(modelPrompt),+ inputTokens:+ entry.composedPromptTokens ++ estimateHistoryTokens(turn.history) ++ (continueTurn ? 0 : countTokens(modelPrompt)),entryCount: turn.history.length,});@@ -1953,7 +1974,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {const stepWindows: Array<{ gapStart?: number; gapEnd: number }> = [];let thinkTail: Promise<unknown> = Promise.resolve();let tapeError: Error | undefined;- let tapedTriggerUser = false;+ let tapedTriggerUser = continueTurn;const toolAbort = new AbortController();const pendingSteerTapeMeta: Array<SteerIntake & {@@ -1984,7 +2005,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {harness: "pi",payload: stripImageBytes(message, isTrigger ? turn.images : steer?.images),scopeLabel: resultScope ?? turn.scopeLabel,- ...(isTrigger+ ...(isTrigger && userEntry? {entrySeq: userEntry.seq,meta: {@@ -2101,7 +2122,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {: undefined;try {await turn.recordLlmRequest({- turnSeq: userEntry.seq,+ turnSeq: userEntry?.seq ?? null,step,model: captured[step]!.transport?.modelId ?? effectiveModel,promptEnvelope: captured[step]!.envelope,@@ -2273,7 +2294,9 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {? turn.images.map((i) => ({ type: "image" as const, data: i.dataBase64, mimeType: i.mimeType })): undefined;wallClock = await raceTurnWallClock(- entry.agentSession.prompt(modelPrompt, images ? { images } : undefined),+ continueTurn+ ? continueAssistantTurn(entry.agentSession)+ : entry.agentSession.prompt(modelPrompt, images ? { images } : undefined),{capMs: raceCapMs(),extendMs: extendCapMs,diff --git a/test/claude-harness-turn.test.ts b/test/claude-harness-turn.test.tsindex 6b0c213d8..348b2ced6 100644--- a/test/claude-harness-turn.test.ts+++ b/test/claude-harness-turn.test.ts@@ -5,6 +5,7 @@ import { createMemoryRunSignalStore } from "../src/runs/run-signal-store.ts";import type { HarnessLlmRequestRecord, HarnessTurnInput } from "../src/harness/harness.ts";import type { NewEntry } from "../src/sessions/session-store.ts";import type { ScopeId, SessionEntry } from "../src/types.ts";+import { resumeNote } from "../src/core/turn-resume.ts";type FakeSdkMessage = Record<string, unknown>;type Script = (prompts: AsyncIterable<{ message: { content: unknown } }>) => AsyncGenerator<FakeSdkMessage>;@@ -734,3 +735,23 @@ test("Claude retains a queued message which the SDK never consumes", async () =>assert.equal(entries.filter((entry) => entry.type === "user").length, 1);assert.equal((await signals.pending("no-echo"))[0]?.signal.ts, "pending");});++test("Claude ignores continueTurn and still prompts the resume note as a user message", async () => {+ const prompted: string[] = [];+ currentScript = async function* (prompts) {+ for await (const prompt of prompts) {+ prompted.push(JSON.stringify(prompt.message.content));+ break;+ }+ yield assistantMessage("msg_A", "picking up", { input_tokens: 3, output_tokens: 2 });+ yield resultMessage("picking up");+ };+ const note = resumeNote({ strategy: { kind: "continue" } });+ const harness = createClaudeHarness({});+ const { turn, entries } = harnessTurn({ input: note, continueTurn: true, runId: "run-resume" });+ const result = await harness.turns.runTurn(turn);+ assert.equal(result.reply, "picking up");+ assert.equal(entries[0]?.type, "user");+ assert.equal((entries[0]!.payload as { text: string }).text, note);+ assert.ok(prompted[0]!.includes("(system note:"), "the note reaches the model as a user prompt");+});diff --git a/test/codex-harness.test.ts b/test/codex-harness.test.tsindex 214100696..270e034d6 100644--- a/test/codex-harness.test.ts+++ b/test/codex-harness.test.ts@@ -1,3 +1,4 @@+import { resumeNote } from "../src/core/turn-resume.ts";import test from "node:test";import assert from "node:assert/strict";import {@@ -531,6 +532,39 @@ test("Codex harness drives app-server JSON-RPC with a read-only jail", async (t));});+test("Codex ignores continueTurn and still records the resume note as this attempt's user entry", async (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-test-"));+ const harness = createCodexHarness({ binaryPath: fakeCodexBinary(dir), env: testHarnessEnv(dir) });+ t.after(async () => {+ await harness.turns.close?.();+ rmSync(dir, { recursive: true, force: true });+ });+ const entries: SessionEntry[] = [];+ const scope = { kind: "org", id: "test" } as unknown as ScopeId;+ const session = { id: "session-1" } as Session;+ const note = resumeNote({ strategy: { kind: "continue" } });+ const result = await harness.turns.runTurn({+ session,+ runId: "run-resume",+ continueTurn: true,+ input: note,+ systemPrompt: "be concise",+ history: [],+ tools: {} as HarnessTurnInput["tools"],+ scopeLabel: scope,+ orgScopeId: scope,+ emit: async (entry) => {+ const saved = { ...entry, sessionId: session.id, seq: entries.length + 1, createdAt: Date.now() } as SessionEntry;+ entries.push(saved);+ return saved;+ },+ recordModelCall: () => {},+ });+ assert.equal(result.reply, "hello");+ assert.equal(entries[0]?.type, "user");+ assert.equal((entries[0]!.payload as { text: string }).text, note);+});+test("Codex task titles stay concise when the provider includes the parent request", () => {assert.equal(codexTaskTitle("The user asked for two workers. You are the WEST subagent. Return a useful summary."),diff --git a/test/opencode-harness.test.ts b/test/opencode-harness.test.tsindex ec15aa6ea..5818dad02 100644--- a/test/opencode-harness.test.ts+++ b/test/opencode-harness.test.ts@@ -1,3 +1,4 @@+import { resumeNote } from "../src/core/turn-resume.ts";import test from "node:test";import { createMemoryRunSignalStore } from "../src/runs/run-signal-store.ts";import assert from "node:assert/strict";@@ -81,7 +82,11 @@ const okAssistant = `{parts: [{ id: "prt_1", sessionID: "ses_main", messageID: "msg_1", type: "text", text: "hello from fake" }],}`;-function turnInput(entries: SessionEntry[], llmRows: HarnessLlmRequestRecord[]): HarnessTurnInput {+function turnInput(+ entries: SessionEntry[],+ llmRows: HarnessLlmRequestRecord[],+ overrides: Partial<HarnessTurnInput> = {},+): HarnessTurnInput {const scope = { kind: "org", id: "test" } as unknown as ScopeId;const session = { id: "session-1" } as Session;return {@@ -102,9 +107,27 @@ function turnInput(entries: SessionEntry[], llmRows: HarnessLlmRequestRecord[]):recordLlmRequest: async (rec) => {llmRows.push(rec);},+ ...overrides,};}+test("OpenCode ignores continueTurn and still records the resume note as this attempt's user entry", async (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-opencode-test-"));+ const harness = createOpenCodeHarness({ binaryPath: fakeSidecar(dir, "resume", promptHandlers(okAssistant)) });+ t.after(async () => {+ await harness.turns.close?.();+ rmSync(dir, { recursive: true, force: true });+ });+ const entries: SessionEntry[] = [];+ const note = resumeNote({ strategy: { kind: "continue" } });+ const result = await harness.turns.runTurn(+ turnInput(entries, [], { input: note, continueTurn: true, runId: "run-resume" }),+ );+ assert.equal(result.reply, "hello from fake");+ assert.equal(entries[0]?.type, "user");+ assert.equal((entries[0]!.payload as { text: string }).text, note);+});+test("OpenCode surfaces a provider error as a non-retryable failure, never a successful empty reply", async (t) => {const dir = mkdtempSync(join(tmpdir(), "qm-opencode-test-"));const harness = createOpenCodeHarness({diff --git a/test/pi-harness-continue.test.ts b/test/pi-harness-continue.test.tsnew file mode 100644index 000000000..3b57b719c--- /dev/null+++ b/test/pi-harness-continue.test.ts@@ -0,0 +1,194 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { createPiHarness, endsAtRecordedToolResult } from "../src/harness/pi-harness.ts";+import type { HarnessTurnInput } from "../src/harness/harness.ts";+import { INTERRUPTED_TOOL_RESULT } from "../src/harness/context-compaction.ts";+import { resumeNote } from "../src/core/turn-resume.ts";+import type { NewEntry, NewTapeRecord } from "../src/sessions/session-store.ts";+import type { ToolContext } from "../src/tools/primitives.ts";+import type { SessionEntry } from "../src/types.ts";++type Sink = {+ entries: Array<{ seq: number; type: string; payload: unknown }>;+ tape: NewTapeRecord[];+};++type RequestBody = { messages: Array<{ role: string; content: unknown }> };++function entry(type: SessionEntry["type"], payload: unknown, seq: number): SessionEntry {+ return { sessionId: "s", seq, parentSeq: null, type, payload, scopeLabel: "personal:tester", createdAt: seq };+}++const ASK = entry("user", { text: "list the files", runId: "run-1" }, 1);+const CALL = entry("tool_call", { tool: "execute", callId: "c1", command: "ls" }, 2);+const RESULT = entry("tool_result", { callId: "c1", result: "a.txt\nb.txt" }, 3);+const SAFE_CALL = entry(+ "tool_call",+ {+ tool: "history",+ query: "budget",+ callId: "c-budget",+ retrySafe: true,+ rerun: { tool: "history", input: { query: "budget" } },+ },+ 2,+);++function turnInput(sink: Sink, overrides: Partial<HarnessTurnInput>): HarnessTurnInput {+ let seq = 10;+ return {+ session: { id: "resume" } as HarnessTurnInput["session"],+ runId: "run-1",+ input: resumeNote(),+ systemPrompt: "BASE",+ history: [],+ tools: {} as HarnessTurnInput["tools"],+ scopeLabel: "personal:tester" as HarnessTurnInput["scopeLabel"],+ orgScopeId: "org:test" as HarnessTurnInput["orgScopeId"],+ emit: async (e: NewEntry) => {+ const appended = { ...e, seq: seq++, createdAt: Date.now() };+ sink.entries.push({ seq: appended.seq, type: e.type, payload: e.payload });+ return appended as unknown as SessionEntry;+ },+ tape: async (rec: NewTapeRecord) => {+ sink.tape.push(rec);+ },+ recordModelCall: () => {},+ ...overrides,+ };+}++function sse(text: string): Response {+ const events: Array<Record<string, unknown>> = [+ {+ type: "message_start",+ message: {+ id: "msg_1",+ type: "message",+ role: "assistant",+ content: [],+ model: "claude-test",+ stop_reason: null,+ usage: { input_tokens: 5, output_tokens: 0 },+ },+ },+ { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },+ { type: "content_block_delta", index: 0, delta: { type: "text_delta", text } },+ { type: "content_block_stop", index: 0 },+ { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } },+ { type: "message_stop" },+ ];+ const body = events.map((event) => `event: ${event.type as string}\ndata: ${JSON.stringify(event)}\n\n`).join("");+ return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } });+}++async function runResume(history: SessionEntry[], overrides: Partial<HarnessTurnInput> = {}) {+ const harness = createPiHarness({ apiKey: "sk-test" });+ const sink: Sink = { entries: [], tape: [] };+ const requests: RequestBody[] = [];+ const llmTurnSeqs: Array<number | null> = [];+ const realFetch = globalThis.fetch;+ globalThis.fetch = (async (_url: string | URL | Request, init?: RequestInit) => {+ requests.push(JSON.parse(String(init?.body ?? "{}")) as RequestBody);+ return sse("done: two files");+ }) as typeof globalThis.fetch;+ try {+ const result = await harness.turns.runTurn(+ turnInput(sink, {+ history,+ recordLlmRequest: (rec) => {+ llmTurnSeqs.push(rec.turnSeq);+ },+ ...overrides,+ }),+ );+ return { result, sink, requests, llmTurnSeqs };+ } finally {+ globalThis.fetch = realFetch;+ }+}++function promptedNote(requests: RequestBody[]): boolean {+ return JSON.stringify(requests[0]!.messages).includes("(system note:");+}++test("a genuine resume whose context ends at a recorded tool result continues the assistant turn silently", async () => {+ const { result, sink, requests, llmTurnSeqs } = await runResume([ASK, CALL, RESULT], { continueTurn: true });+ assert.equal(result.reply, "done: two files");+ assert.deepEqual(llmTurnSeqs, [ASK.seq], "model requests are attributed to the run's original user entry");+ assert.equal(requests.length, 1);+ const last = requests[0]!.messages.at(-1)!;+ assert.equal(last.role, "user");+ assert.ok(+ (last.content as Array<{ type: string }>).some((block) => block.type === "tool_result"),+ "the model resumes right after the recorded tool result",+ );+ assert.equal(promptedNote(requests), false, "no resume note reaches the model");+ assert.deepEqual(+ sink.entries.map((e) => e.type),+ ["assistant"],+ "no user entry is appended for the resumed attempt",+ );+ assert.deepEqual(+ sink.tape.filter((rec) => rec.kind === "message").map((rec) => (rec.payload as { role: string }).role),+ ["assistant"],+ "no trigger user row is taped",+ );+});++test("a resume that re-runs a retry-safe call continues the assistant turn right after the re-run result", async () => {+ const queries: string[] = [];+ const { result, sink, requests } = await runResume([ASK, SAFE_CALL], {+ continueTurn: true,+ resumeToolCall: { callId: "c-budget", tool: "history", input: { query: "budget" } },+ tools: {+ async history(q: string) {+ queries.push(q);+ return ["user#1: the budget doc is in shared/q2.md"];+ },+ } as unknown as ToolContext,+ });+ assert.equal(result.reply, "done: two files");+ assert.deepEqual(queries, ["budget"], "the interrupted call ran exactly once");+ assert.deepEqual(+ sink.entries.map((e) => e.type),+ ["tool_result", "assistant"],+ "no note entry follows the re-run",+ );+ assert.deepEqual(+ sink.tape.filter((rec) => rec.kind === "message").map((rec) => (rec.payload as { role: string }).role),+ ["toolResult", "assistant"],+ );+ const flattened = JSON.stringify(requests[0]!.messages);+ assert.ok(flattened.includes("shared/q2.md"), "the model sees the re-run result");+ assert.equal(promptedNote(requests), false);+ assert.ok(!flattened.includes("[interrupted"));+});++test("a resume whose last tool call never recorded a result keeps the note path", async () => {+ const { sink, requests } = await runResume([ASK, CALL], { continueTurn: true });+ assert.equal(promptedNote(requests), true, "an unknown outcome needs the note, not a silent continue");+ assert.ok(JSON.stringify(requests[0]!.messages).includes(INTERRUPTED_TOOL_RESULT));+ assert.equal(sink.entries[0]?.type, "user", "the note is recorded as the attempt's user entry");+});++test("without the resume signal a context ending at a tool result is prompted normally", async () => {+ const { sink, requests } = await runResume([ASK, CALL, RESULT], { input: "follow-up question" });+ assert.equal(JSON.stringify(requests[0]!.messages).includes("follow-up question"), true);+ assert.equal(sink.entries[0]?.type, "user");+ assert.equal(+ sink.tape.filter((rec) => rec.kind === "message" && (rec.payload as { role: string }).role === "user").length,+ 1,+ "the trigger user row is taped as usual",+ );+});++test("endsAtRecordedToolResult accepts only a real trailing tool result", () => {+ const result = { role: "toolResult", content: [{ type: "text", text: "ok" }] };+ const healed = { role: "toolResult", content: [{ type: "text", text: INTERRUPTED_TOOL_RESULT }] };+ assert.equal(endsAtRecordedToolResult([{ role: "user" }, result]), true);+ assert.equal(endsAtRecordedToolResult([{ role: "user" }, healed]), false);+ assert.equal(endsAtRecordedToolResult([{ role: "user" }, { role: "assistant" }]), false);+ assert.equal(endsAtRecordedToolResult([{ role: "user" }]), false);+ assert.equal(endsAtRecordedToolResult([]), false);+});diff --git a/test/tape-nudge-continuation.test.ts b/test/tape-nudge-continuation.test.tsindex b0236efcb..fc7384ba6 100644--- a/test/tape-nudge-continuation.test.ts+++ b/test/tape-nudge-continuation.test.ts@@ -62,9 +62,11 @@ async function runScenario(staleNudgeRead?: boolean;stoppedPartial?: boolean;stoppedTapeComplete?: boolean;+ resumedPrimary?: boolean;} = {},) {const modes: Array<"shadow" | "serve" | undefined> = [];+ const continued: Array<boolean | undefined> = [];const folds: unknown[][] = [];const harness = defineHarness({@@ -75,9 +77,13 @@ async function runScenario(capabilities: new Set(),},{- async runTurn(turn) {- modes.push(turn.tapeMode);- folds.push(turn.tapeFold ?? []);+ async runTurn(dispatched) {+ modes.push(dispatched.tapeMode);+ folds.push(dispatched.tapeFold ?? []);+ continued.push(dispatched.continueTurn);+ const turn = dispatched.input.startsWith("(system note:")+ ? { ...dispatched, input: "needs nudge" }+ : dispatched;if (options.nudgeCrash && turn.input.startsWith("[system] You were addressed")) {throw new Error("fetch failed");}@@ -287,11 +293,26 @@ async function runScenario(});await orchestrator.handleTurn(input("prime"));+ if (options.resumedPrimary) {+ const primed = (await sessions.getByThread(conversation.threadRef))!;+ const { lease } = await sessions.acquireLease(primed.id);+ try {+ for (const entry of [+ { type: "user" as const, payload: { text: "needs nudge" } },+ { type: "tool_call" as const, payload: { tool: "execute", callId: "c1", command: "ls" } },+ { type: "tool_result" as const, payload: { callId: "c1", result: "ok" } },+ ])+ await sessions.append(lease!, { ...entry, scopeLabel: primed.scopeId });+ } finally {+ await sessions.releaseLease(lease!);+ }+ }const second = orchestrator.handleTurn(input("needs nudge", {addressed: true,surfaceTools: true,deliveryTarget: "slack:C1:tape-nudge",+ ...(options.resumedPrimary ? { attempt: 2 } : {}),}),);if (options.nudgeCrash) {@@ -304,9 +325,23 @@ async function runScenario(const session = await sessions.getByThread(conversation.threadRef);const entries = await sessions.getEntries(session!.id);assert.equal(scope, session!.scopeId);- return { modes, folds, deliveries, sessions, session: session!, entries, orchestrator, input };+ return { modes, folds, continued, deliveries, sessions, session: session!, entries, orchestrator, input };}+test("only a resumed primary turn continues the assistant turn; its reply-or-decline nudge never does", async () => {+ const { continued, deliveries } = await runScenario({ resumedPrimary: true });+ assert.deepEqual(continued, [undefined, true, undefined]);+ assert.equal(+ (await deliveries.pending("slack")).some((delivery) => delivery.text === "nudged from tape"),+ true,+ );+});++test("an ordinary primary turn and its nudge never continue a prior assistant turn", async () => {+ const { continued } = await runScenario();+ assert.deepEqual(continued, [undefined, undefined, undefined]);+});+test("an exact first sub-turn continues its reply-or-decline nudge from the refreshed tape", async () => {const { modes, folds, deliveries, sessions, session, entries } = await runScenario();assert.deepEqual(modes, ["shadow", "serve", "serve"]);diff --git a/test/turn-resume.test.ts b/test/turn-resume.test.tsindex b9a27591d..b4a66b8c2 100644--- a/test/turn-resume.test.ts+++ b/test/turn-resume.test.ts@@ -234,10 +234,8 @@ test("resumeStrategy falls back to the interrupted note when the dangling call iassert.deepEqual(resumeStrategy(unmarked, { userSeq: 1, workEntries: 1 }), { kind: "note" });const twoDangling = [user("build and deploy the release", 1), safeCall(2, true), safeCall(3, true)];assert.deepEqual(resumeStrategy(twoDangling, { userSeq: 1, workEntries: 2 }), { kind: "note" });- const answered = [user("build and deploy the release", 1), safeCall(2, true), toolResult(3)];- assert.deepEqual(resumeStrategy(answered, { userSeq: 1, workEntries: 2 }), { kind: "note" });const priorTurnCall = [safeCall(0, true), user("build and deploy the release", 1), toolCall(2), toolResult(3)];- assert.deepEqual(resumeStrategy(priorTurnCall, { userSeq: 1, workEntries: 2 }), { kind: "note" });+ assert.deepEqual(resumeStrategy(priorTurnCall, { userSeq: 1, workEntries: 2 }), { kind: "continue" });});test("resumeStrategy restarts when the dead attempt recorded no work", () => {@@ -269,3 +267,32 @@ test("a tool_result marked interrupted does not answer its call, so the call isconst unsafe = [user("build and deploy the release", 1), safeCall(4, false), killed];assert.deepEqual(resumeStrategy(unsafe, { userSeq: 1, workEntries: 2 }), { kind: "note" });});++test("resumeStrategy continues silently only when every call is answered and the ledger ends at a real tool result", () => {+ const thinking = (seq: number) => ent("thinking", { text: "hm" }, seq);+ const killed = (seq: number) =>+ ent("tool_result", { callId: `c${seq - 1}`, result: "[exit 143]", interrupted: true }, seq);+ const partial = { userSeq: 1, workEntries: 2 };+ assert.deepEqual(resumeStrategy([user("go", 1), toolCall(2), toolResult(3)], partial), { kind: "continue" });+ assert.deepEqual(resumeStrategy([user("go", 1), safeCall(2, true), toolResult(3)], partial), { kind: "continue" });+ assert.deepEqual(resumeStrategy([user("go", 1), toolCall(2), toolResult(3), thinking(4)], partial), {+ kind: "continue",+ });+ assert.deepEqual(resumeStrategy([user("go", 1), toolCall(2), toolResult(3), steer("also", 4)], partial), {+ kind: "note",+ });+ assert.deepEqual(resumeStrategy([user("go", 1), toolCall(2), toolResult(3), user(resumeNote(), 4)], partial), {+ kind: "note",+ });+ assert.deepEqual(resumeStrategy([user("go", 1), toolCall(2), killed(3)], partial), { kind: "note" });+ assert.deepEqual(resumeStrategy([safeCall(0, true), user("go", 1), toolCall(2)], { userSeq: 1, workEntries: 1 }), {+ kind: "note",+ });+ assert.deepEqual(+ resumeStrategy([user("go", 1), toolCall(2), toolResult(3), safeCall(4, true), killed(5)], {+ userSeq: 1,+ workEntries: 4,+ }).kind,+ "retry",+ );+});diff --git a/test/turn-retry-replay.test.ts b/test/turn-retry-replay.test.tsindex 942c7fa36..29407bee7 100644--- a/test/turn-retry-replay.test.ts+++ b/test/turn-retry-replay.test.ts@@ -52,6 +52,7 @@ function fakeSandbox(): Sandbox {function buildScenario() {const turns: string[] = [];+ const continued: Array<boolean | undefined> = [];const harness = defineHarness({id: "pi",@@ -63,6 +64,7 @@ function buildScenario() {{async runTurn(turn) {turns.push(turn.input);+ continued.push(turn.continueTurn);const userEntry = await turn.emit({type: "user",payload: { text: turn.input },@@ -144,7 +146,7 @@ function buildScenario() {(e) => e.type === "user" && String((e.payload as { text?: string }).text ?? "").startsWith(ASK),);};- return { orchestrator, sessions, runs, turns, input, asks };+ return { orchestrator, sessions, runs, turns, continued, input, asks };}test("a retry replays the answer the previous attempt recorded instead of asking again", async () => {@@ -236,7 +238,7 @@ test("the replay does not need the session lease, so a busy session cannot requeasync function seedTurn(sessions: ReturnType<typeof buildScenario>["sessions"],- entries: Array<{ type: "user" | "assistant" | "tool_call"; payload: Record<string, unknown> }>,+ entries: Array<{ type: "user" | "assistant" | "tool_call" | "tool_result"; payload: Record<string, unknown> }>,): Promise<number> {const session = await sessions.getOrCreateByThread(conversation.threadRef, "dm", "personal:U1");const { lease } = await sessions.acquireLease(session.id);@@ -284,3 +286,43 @@ test("a turn that recorded no reply replays as silent, not as an empty answer",assert.equal(retry.reply, undefined);assert.equal(turns.length, 0);});++test("a resume whose recorded work ends at a tool result asks the harness to continue the assistant turn", async () => {+ const { orchestrator, sessions, runs, turns, continued, input, asks } = buildScenario();+ const run = (await runs.enqueue({ sessionId: conversation.threadRef, request: input(ASK) })).run;+ const marker = await seedTurn(sessions, [+ { type: "user", payload: { text: ASK } },+ { type: "tool_call", payload: { tool: "execute", callId: "c1", command: "ls" } },+ { type: "tool_result", payload: { callId: "c1", result: "a.txt" } },+ ]);+ await runs.noteTurnUserSeq(run.id, marker);++ const retry = await orchestrator.handleTurn(input(ASK, { runId: run.id, attempt: 2 }));+ assert.equal(retry.status, "ok");+ assert.deepEqual(continued, [true], "the harness is told the tape ends at a recorded tool result");+ assert.match(turns[0]!, /^\(system note:/, "the note still travels as the fallback input");+ assert.equal((await asks()).length, 1);+});++test("a resume whose last tool call has no recorded result is not asked to continue", async () => {+ const { orchestrator, sessions, runs, continued, input } = buildScenario();+ const run = (await runs.enqueue({ sessionId: conversation.threadRef, request: input(ASK) })).run;+ const marker = await seedTurn(sessions, [+ { type: "user", payload: { text: ASK } },+ { type: "tool_call", payload: { tool: "execute", callId: "c1", command: "ls" } },+ ]);+ await runs.noteTurnUserSeq(run.id, marker);++ const retry = await orchestrator.handleTurn(input(ASK, { runId: run.id, attempt: 2 }));+ assert.equal(retry.status, "ok");+ assert.deepEqual(continued, [undefined]);+});++test("a first attempt and a no-work retry are never asked to continue", async () => {+ const { orchestrator, runs, continued, input } = buildScenario();+ const run = (await runs.enqueue({ sessionId: conversation.threadRef, request: input(ASK) })).run;+ await orchestrator.handleTurn(input(ASK, { runId: run.id, attempt: 1 }));+ const again = (await runs.enqueue({ sessionId: conversation.threadRef, request: input(ASK) })).run;+ await orchestrator.handleTurn(input(ASK, { runId: again.id, attempt: 2 }));+ assert.deepEqual(continued, [undefined, undefined]);+});