Multiplayer agent harness for work.
Skip the stopped assistant entry when shutdown aborts a turn (#2062)
Regan Bell445bf2ff0f04
diff --git a/src/harness/claude-harness.ts b/src/harness/claude-harness.tsindex 2cfc1692f..3c0e592be 100644--- a/src/harness/claude-harness.ts+++ b/src/harness/claude-harness.ts@@ -47,6 +47,7 @@ import {oneShotRunner,tapeReplyCheckpoint,recordSteerIntake,+ recordStoppedReply,type SteerIntake,transitionTask,type HarnessToolPlumbing,@@ -749,11 +750,7 @@ export function createClaudeHarness(opts: ClaudeHarnessOptions = {}): Harness {const terminal = ref.runtimeHandoff || ref.silentRequested || ref.pausedOnApproval;const text = message.subtype === "success" && !terminal ? message.result.trim() : "";if (text) {- const finalEntry = await turn.emit({- type: "assistant",- payload: { text, ...(stopped ? { stopped: true } : {}) },- scopeLabel: turn.scopeLabel,- });+ const finalEntry = await turn.emit({ type: "assistant", payload: { text }, scopeLabel: turn.scopeLabel });await tapeReplyCheckpoint(turn, finalEntry);}streamedText = "";@@ -787,12 +784,8 @@ export function createClaudeHarness(opts: ClaudeHarnessOptions = {}): Harness {const reply = terminal ? "" : streamedText.trim();await flushThinking();if (reply && !terminal) {- const finalEntry = await turn.emit({- type: "assistant",- payload: { text: reply, stopped: true },- scopeLabel: turn.scopeLabel,- });- await tapeReplyCheckpoint(turn, finalEntry);+ const finalEntry = await recordStoppedReply(turn, reply);+ if (finalEntry) await tapeReplyCheckpoint(turn, finalEntry);}return {reply,diff --git a/src/harness/codex-harness.ts b/src/harness/codex-harness.tsindex cb39c47cb..47e2de34b 100644--- a/src/harness/codex-harness.ts+++ b/src/harness/codex-harness.ts@@ -33,6 +33,7 @@ import {oneShotRunner,tapeReplyCheckpoint,recordSteerIntake,+ recordStoppedReply,type SteerIntake,transitionTask,type BridgedTool,@@ -1189,11 +1190,7 @@ export function createCodexHarness(opts: CodexHarnessOptions = {}): Harness {scopeLabel: turn.scopeLabel,});}- await turn.emit({- type: "assistant",- payload: { text: state.stoppedReply ?? "", stopped: true },- scopeLabel: turn.scopeLabel,- });+ await recordStoppedReply(turn, state.stoppedReply ?? "");})();return stoppedReplySaved;};@@ -1330,14 +1327,13 @@ export function createCodexHarness(opts: CodexHarnessOptions = {}): Harness {if (reply && !terminal) {const finalEntry = await turn.emit({type: "assistant",- payload: { text: reply, ...(state.stopped ? { stopped: true } : {}) },+ payload: { text: reply },scopeLabel: turn.scopeLabel,});await tapeReplyCheckpoint(turn, finalEntry);}turnResult = {reply,- ...(state.stopped ? { stopped: true as const } : {}),...(ref.runtimeHandoff ? { runtimeHandoff: ref.runtimeHandoff } : {}),...(ref.silentRequested ? { silent: true } : {}),...(ref.pendingApprovals?.length ? { pendingApprovals: ref.pendingApprovals } : {}),diff --git a/src/harness/harness-shared.ts b/src/harness/harness-shared.tsindex e8937e52e..79aece06d 100644--- a/src/harness/harness-shared.ts+++ b/src/harness/harness-shared.ts@@ -85,6 +85,11 @@ export async function tapeReplyCheckpoint(});}+export async function recordStoppedReply(turn: HarnessTurnInput, text: string): Promise<SessionEntry | null> {+ if (turn.shutdown?.aborted) return null;+ return turn.emit({ type: "assistant", payload: { text, stopped: true }, scopeLabel: turn.scopeLabel });+}+export function withTapedEntryMirrors(turn: HarnessTurnInput): HarnessTurnInput {const tape = turn.tape;if (!tape) return turn;diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.tsindex 0393d845c..8befbc9d6 100644--- a/src/harness/mock-harness.ts+++ b/src/harness/mock-harness.ts@@ -11,6 +11,7 @@ import { NonRetryableTurnError, TitleRejected } from "../core/turn-error.ts";import { NeedsApproval } from "../tools/primitives.ts";import { deterministicCompactSummary, estimateHistoryTokens } from "./context-compaction.ts";import { countTokens } from "../util/tokens.ts";+import { recordStoppedReply } from "./harness-shared.ts";import {SECURITY_SCREEN_STEP,SECURITY_SCREEN_SYSTEM_PROMPT,@@ -84,6 +85,7 @@ export function createMockHarness(): Harness {const shedSessions = new Set<string>();const boomAlwaysSessions = new Set<string>();const resumePostSessions = new Map<string, string>();+ const shutdownResumeSessions = new Set<string>();const flakyScreens = new Set<string>();return defineHarness({@@ -196,6 +198,27 @@ export function createMockHarness(): Harness {});usedTool = true;reply = r.ok ? "(posted after resume)" : `[not sent] ${r.message ?? "failed"}`;+ } else if (command0.startsWith("(system note:") && shutdownResumeSessions.has(turn.session.id)) {+ shutdownResumeSessions.delete(turn.session.id);+ reply = "finished after resume";+ } else if (command0 === "!work-until-shutdown") {+ await turn.emit({+ type: "tool_call",+ payload: { tool: "execute", callId: "build", command: "make build" },+ scopeLabel: turn.scopeLabel,+ });+ await turn.emit({+ type: "tool_result",+ payload: { tool: "execute", callId: "build", ok: true },+ scopeLabel: turn.scopeLabel,+ });+ shutdownResumeSessions.add(turn.session.id);+ await new Promise<void>((resolve) => {+ if (!turn.cancel || turn.cancel.aborted) resolve();+ else turn.cancel.addEventListener("abort", () => resolve(), { once: true });+ });+ await recordStoppedReply(turn, "(stopped)");+ return { reply: "(stopped)", stopped: true, modelCalls: 1 };} else if (command0.startsWith("!post-lost-result ")) {const msg = cmd.slice(cmd.indexOf("!post-lost-result ") + "!post-lost-result ".length);await turn.emit({diff --git a/src/harness/opencode-harness.ts b/src/harness/opencode-harness.tsindex fa6970cec..d2d4ab639 100644--- a/src/harness/opencode-harness.ts+++ b/src/harness/opencode-harness.ts@@ -45,6 +45,7 @@ import {oneShotRunner,tapeReplyCheckpoint,recordSteerIntake,+ recordStoppedReply,type SteerIntake,type BridgedTool,type HarnessToolPlumbing,@@ -1169,17 +1170,17 @@ export function createOpenCodeHarness(opts: OpenCodeHarnessOptions = {}): Harnesfor (const thinking of reasoningFromParts(parts))await turn.emit({ type: "thinking", payload: thinking, scopeLabel: turn.scopeLabel });const reply = ref.runtimeHandoff || ref.silentRequested ? "" : textFromParts(parts);+ const interrupted = state.stopped || turn.cancel?.aborted === true;if (reply) {- const finalEntry = await turn.emit({- type: "assistant",- payload: { text: reply, ...(state.stopped ? { stopped: true } : {}) },- scopeLabel: turn.scopeLabel,- });- await tapeReplyCheckpoint(turn, finalEntry);+ const finalEntry = interrupted+ ? await recordStoppedReply(turn, reply)+ : await turn.emit({ type: "assistant", payload: { text: reply }, scopeLabel: turn.scopeLabel });+ if (finalEntry) await tapeReplyCheckpoint(turn, finalEntry);}return {reply,- ...(state.stopped ? { stopped: true as const, stoppedByUser: true as const } : {}),+ ...(interrupted ? { stopped: true as const } : {}),+ ...(state.stopped ? { stoppedByUser: true as const } : {}),...(ref.runtimeHandoff ? { runtimeHandoff: ref.runtimeHandoff } : {}),...(ref.silentRequested ? { silent: true } : {}),...(ref.pendingApprovals?.length ? { pendingApprovals: ref.pendingApprovals } : {}),diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.tsindex 27dd6e326..d0822f425 100644--- a/src/harness/pi-harness.ts+++ b/src/harness/pi-harness.ts@@ -1,4 +1,11 @@-import { recordSteerIntake, resumeInterruptedToolCall, type BridgedTool, type SteerIntake } from "./harness-shared.ts";+import {+ recordSteerIntake,+ recordStoppedReply,+ resumeInterruptedToolCall,+ tapeReplyCheckpoint,+ type BridgedTool,+ type SteerIntake,+} from "./harness-shared.ts";import { withDocumentInputs, type DocumentModel } from "./document-inputs.ts";import { gatewayModelsJson, gatewayModelsVersion } from "../model/gateway-models.ts";import { Type } from "typebox";@@ -54,7 +61,7 @@ import type {NewTapeRecord,TapeRecord,} from "../sessions/session-store.ts";-import { tapeCheckpointPayload, tapeEntryMirrorRecord } from "../sessions/session-store.ts";+import { tapeEntryMirrorRecord } from "../sessions/session-store.ts";import { NonRetryableTurnError, ProviderTurnError, TitleRejected } from "../core/turn-error.ts";import { MAX_LLM_REQUEST_BYTES } from "../core/attachments.ts";import { asError, swallow, swallowAs } from "../util/errors.ts";@@ -2161,22 +2168,6 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {if (!turn.tape) return;await turn.tape(tapeEntryMirrorRecord(mirrored));};- const checkpointSubturn = async (- finalEntry: { seq: number; createdAt: number },- reply: string,- ): Promise<void> => {- if (!turn.tape) return;- await turn.tape({- kind: "annotation",- payload: tapeCheckpointPayload("subturnEnd", {- type: "assistant",- payload: { text: reply },- at: finalEntry.createdAt,- }),- scopeLabel: turn.scopeLabel,- entrySeq: finalEntry.seq,- });- };let wallClock!: TurnWallClockOutcome;const messagesBefore = entry.agentSession.messages.length;const freshAssistantStopReason = (): string | undefined => {@@ -2493,12 +2484,8 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {await tapeEntryMirror(goalEntry);if (g.status === "complete") entry.ref.goal = null;}- const finalEntry = await turn.emit({- type: "assistant",- payload: { text: reply, stopped: true },- scopeLabel: turn.scopeLabel,- });- const stoppedPartial = stoppedPartialTapeMessage(freshMessages, reply, finalEntry.createdAt);+ const finalEntry = await recordStoppedReply(turn, reply);+ const stoppedPartial = finalEntry && stoppedPartialTapeMessage(freshMessages, reply, finalEntry.createdAt);if (turn.tape && stoppedPartial) {await turn.tape({kind: "message",@@ -2507,7 +2494,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {scopeLabel: turn.scopeLabel,});}- await checkpointSubturn(finalEntry, reply);+ if (finalEntry) await tapeReplyCheckpoint(turn, finalEntry);const cacheUsage = sumCacheUsage(callStats);const base = {reply,@@ -2538,7 +2525,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {payload: { text: reply },scopeLabel: turn.scopeLabel,});- await checkpointSubturn(finalEntry, reply);+ await tapeReplyCheckpoint(turn, finalEntry);const pendingApprovals = entry.ref.pendingApprovals ?? [];const modelCalls = entry.ref.modelCalls ?? 0;const cacheUsage = sumCacheUsage(callStats);diff --git a/src/harness/tape-fold.ts b/src/harness/tape-fold.tsindex 52aa7c4aa..930f4d73a 100644--- a/src/harness/tape-fold.ts+++ b/src/harness/tape-fold.ts@@ -415,8 +415,10 @@ export function lintFold(messages: readonly unknown[]): FoldLint {}}}- if (msg.role === "assistant" && Array.isArray(msg.content)) {- for (const block of msg.content) {+ if (msg.role === "assistant") {+ if (openCalls.size && !assistantDroppedAtReplay(m))+ problems.push(`#${i}: assistant message while ${openCalls.size} tool call(s) await results`);+ for (const block of Array.isArray(msg.content) ? msg.content : []) {const b = block as { type?: string; id?: string };if (b?.type === "toolCall" && typeof b.id === "string") {if (seenCallIds.has(b.id)) problems.push(`#${i}: duplicate tool call id ${b.id}`);diff --git a/test/claude-harness-turn.test.ts b/test/claude-harness-turn.test.tsindex 348b2ced6..a473eafba 100644--- a/test/claude-harness-turn.test.ts+++ b/test/claude-harness-turn.test.ts@@ -5,7 +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";+import { findTrailingPartialTurn, resumeNote, resumeStrategy, turnAtSeq } from "../src/core/turn-resume.ts";type FakeSdkMessage = Record<string, unknown>;type Script = (prompts: AsyncIterable<{ message: { content: unknown } }>) => AsyncGenerator<FakeSdkMessage>;@@ -755,3 +755,51 @@ test("Claude ignores continueTurn and still prompts the resume note as a user meassert.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");});++for (const shutdown of [false, true]) {+ test(`Claude ${shutdown ? "skips" : "records"} the stopped partial when cancelled mid-turn ${shutdown ? "by shutdown" : "by the worker"}`, async () => {+ const waiting = Promise.withResolvers<void>();+ const release = Promise.withResolvers<void>();+ const cancel = new AbortController();+ const shutdownSignal = new AbortController();+ currentScript = async function* (prompts) {+ await prompts[Symbol.asyncIterator]().next();+ await toolHandlers.get("memory")!({ action: "read" });+ yield {+ type: "stream_event",+ event: { type: "content_block_delta", delta: { type: "text_delta", text: "Visible partial" } },+ };+ waiting.resolve();+ await release.promise;+ yield resultMessage("replacement after stop");+ };+ const harness = createClaudeHarness({});+ const { turn, entries } = harnessTurn({+ cancel: cancel.signal,+ shutdown: shutdownSignal.signal,+ readOnly: false,+ tools: { memoryRead: async () => "remembered facts" } as unknown as HarnessTurnInput["tools"],+ });+ const running = harness.turns.runTurn(turn);+ await waiting.promise;+ if (shutdown) shutdownSignal.abort();+ cancel.abort();+ release.resolve();+ const result = await running;+ assert.equal(result.stopped, true);+ assert.equal(result.stoppedByUser, undefined);+ assert.deepEqual(+ entries.map((entry) => entry.type).filter((type) => type !== "thinking"),+ shutdown ? ["user", "tool_call", "tool_result"] : ["user", "tool_call", "tool_result", "assistant"],+ );+ const partial = findTrailingPartialTurn(entries, turn.input);+ if (shutdown) {+ assert.ok(partial);+ assert.notEqual(resumeStrategy(entries, partial).kind, "restart");+ assert.equal(turnAtSeq(entries, partial.userSeq)?.answer, undefined);+ } else {+ assert.deepEqual(entries.at(-1)!.payload, { text: "Visible partial", stopped: true });+ assert.equal(partial, null);+ }+ });+}diff --git a/test/codex-harness.test.ts b/test/codex-harness.test.tsindex 270e034d6..f8ca7bbbe 100644--- a/test/codex-harness.test.ts+++ b/test/codex-harness.test.ts@@ -1,4 +1,4 @@-import { resumeNote } from "../src/core/turn-resume.ts";+import { resumeNote, turnAtSeq } from "../src/core/turn-resume.ts";import test from "node:test";import assert from "node:assert/strict";import {@@ -2170,3 +2170,62 @@ readline.createInterface({ input: process.stdin }).on('line', (line) => {assert.equal(result.reply, "denied");assert.equal(shared, false);});++test("Codex skips the stopped reply when shutdown cancels the turn, leaving it open for resume", async (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-codex-shutdown-stop-"));+ const harness = createCodexHarness({+ binaryPath: stopReportsFailedCodexBinary(dir, true, true),+ env: testHarnessEnv(dir),+ turnWallClockMs: 5_000,+ });+ t.after(async () => {+ await harness.turns.close?.();+ rmSync(dir, { recursive: true, force: true });+ });+ const cancel = new AbortController();+ const shutdown = new AbortController();+ const received = Promise.withResolvers<void>();+ const entries: SessionEntry[] = [];+ const scope = "personal:test" as ScopeId;+ const running = harness.turns.runTurn({+ session: { id: "shutdown-stop" } as Session,+ runId: "shutdown-stop-run",+ input: "check",+ systemPrompt: "be concise",+ history: [],+ tools: {} as HarnessTurnInput["tools"],+ scopeLabel: scope,+ orgScopeId: scope,+ cancel: cancel.signal,+ shutdown: shutdown.signal,+ emit: async (entry) => {+ const saved = {+ ...entry,+ sessionId: "shutdown-stop",+ seq: entries.length + 1,+ createdAt: Date.now(),+ } as SessionEntry;+ entries.push(saved);+ return saved;+ },+ recordModelCall: () => {},+ onDelta: (text) => {+ if (text === "Partial answer") received.resolve();+ },+ });+ await received.promise;+ shutdown.abort();+ cancel.abort();+ const result = await running;+ assert.equal(result.stopped, true);+ assert.equal(result.reply, "Partial answer");+ assert.deepEqual(+ entries.filter((entry) => entry.type === "assistant"),+ [],+ );+ assert.deepEqual(+ entries.filter((entry) => entry.type === "text").map((entry) => entry.payload),+ [{ text: "Checking.", phase: "commentary" }],+ );+ assert.equal(turnAtSeq(entries, entries[0]!.seq)?.answer, undefined);+});diff --git a/test/opencode-harness.test.ts b/test/opencode-harness.test.tsindex 5818dad02..3f83a6557 100644--- a/test/opencode-harness.test.ts+++ b/test/opencode-harness.test.ts@@ -760,3 +760,55 @@ test("OpenCode's fixed tool list includes the web-only sessions tool", () => {);assert.doesNotMatch(definitions.find((tool) => tool.name === "subagents")!.description, /use sessions/);});++for (const shutdown of [false, true]) {+ test(`OpenCode ${shutdown ? "skips" : "records"} the stopped partial when ${shutdown ? "shutdown" : "the worker"} cancels mid-turn`, async (t) => {+ const dir = mkdtempSync(join(tmpdir(), "qm-opencode-shutdown-stop-"));+ const cancel = new AbortController();+ const shutdownSignal = new AbortController();+ const partialAssistant = okAssistant.replace("hello from fake", "Partial answer");+ const harness = createOpenCodeHarness({+ binaryPath: fakeSidecar(+ dir,+ "shutdown-stop",+ `+ if (req.method === "POST" && message) {+ await readBody(req);+ globalThis.pendingPrompt = res;+ require("node:fs").writeFileSync(${JSON.stringify(join(dir, "started"))}, "1");+ return;+ }+ if (req.method === "POST" && url.pathname.endsWith("/abort")) {+ if (globalThis.pendingPrompt) { json(globalThis.pendingPrompt, ${partialAssistant}); globalThis.pendingPrompt = null; }+ return json(res, true);+ }+ if (req.method === "GET" && message) return json(res, []);+ `,+ ),+ turnWallClockMs: 5_000,+ });+ t.after(async () => {+ await harness.turns.close?.();+ rmSync(dir, { recursive: true, force: true });+ });+ const entries: SessionEntry[] = [];+ const running = harness.turns.runTurn(+ turnInput(entries, [], { runId: "shutdown-stop", cancel: cancel.signal, shutdown: shutdownSignal.signal }),+ );+ const deadline = Date.now() + 4_000;+ while (!existsSync(join(dir, "started"))) {+ if (Date.now() > deadline) throw new Error("mock OpenCode never started");+ await new Promise((resolve) => setTimeout(resolve, 10));+ }+ if (shutdown) shutdownSignal.abort();+ cancel.abort();+ const result = await running;+ assert.equal(result.stopped, true);+ assert.equal(result.stoppedByUser, undefined);+ assert.equal(result.reply, "Partial answer");+ assert.deepEqual(+ entries.filter((entry) => entry.type === "assistant").map((entry) => entry.payload),+ shutdown ? [] : [{ text: "Partial answer", stopped: true }],+ );+ });+}diff --git a/test/pi-harness-cancel-exit.test.ts b/test/pi-harness-cancel-exit.test.tsindex ac4cdf6b6..4b41db6b9 100644--- a/test/pi-harness-cancel-exit.test.ts+++ b/test/pi-harness-cancel-exit.test.ts@@ -1,5 +1,6 @@import { test } from "node:test";import assert from "node:assert/strict";+import { findTrailingPartialTurn, resumeStrategy, turnAtSeq } from "../src/core/turn-resume.ts";import { createGoalRecord, latestGoalRecord } from "../src/harness/goal.ts";import { createPiHarness } from "../src/harness/pi-harness.ts";import type { HarnessTurnInput } from "../src/harness/harness.ts";@@ -10,6 +11,7 @@ function cancelTurn(sessionId: string,cancel: AbortSignal,sink: { entries: Array<{ seq: number; type: string; payload: unknown }>; tape: NewTapeRecord[] },+ overrides: Partial<HarnessTurnInput> = {},): HarnessTurnInput {let seq = 0;return {@@ -30,6 +32,7 @@ function cancelTurn(sink.tape.push(rec);},recordModelCall: () => {},+ ...overrides,};}@@ -264,3 +267,89 @@ test("Pi rehydrates a durable receipt and preserves its active goal when the worglobalThis.fetch = realFetch;}});++function toolUseEvents(): Array<Record<string, unknown>> {+ const events = textReplyEvents("");+ events[1] = {+ type: "content_block_start",+ index: 0,+ content_block: { type: "tool_use", id: "read-memory", name: "memory", input: {} },+ };+ events[2] = {+ type: "content_block_delta",+ index: 0,+ delta: { type: "input_json_delta", partial_json: JSON.stringify({ action: "read" }) },+ };+ events[4] = { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 3 } };+ return events;+}++for (const shutdown of [false, true]) {+ test(`a cancel mid-tool ${shutdown ? "from shutdown leaves the turn open for resume" : "without shutdown records the stop marker"}`, async () => {+ const harness = createPiHarness({ apiKey: "sk-test" });+ const cancel = new AbortController();+ const shutdownSignal = new AbortController();+ const sink = { entries: [] as Array<{ seq: number; type: string; payload: unknown }>, tape: [] as NewTapeRecord[] };+ const realFetch = globalThis.fetch;+ let calls = 0;+ globalThis.fetch = ((_url: string | URL | Request, init?: RequestInit) => {+ if (++calls === 1) return Promise.resolve(sse(toolUseEvents()));+ return new Promise((_resolve, reject) => {+ const signal = init?.signal;+ if (signal?.aborted) {+ reject(abortShapedError());+ return;+ }+ signal?.addEventListener("abort", () => reject(abortShapedError()), { once: true });+ });+ }) as typeof globalThis.fetch;+ try {+ const turn = cancelTurn("cancel-mid-tool", cancel.signal, sink, {+ readOnly: false,+ shutdown: shutdownSignal.signal,+ tools: {+ memoryRead: async () => {+ if (shutdown) shutdownSignal.abort();+ cancel.abort();+ await new Promise((resolve) => setTimeout(resolve, 20));+ return "remembered facts";+ },+ } as unknown as HarnessTurnInput["tools"],+ });+ const result = await harness.turns.runTurn(turn);+ assert.equal(result.stopped, true);+ assert.equal(result.stoppedTapeComplete, true);+ const entries = sink.entries as unknown as SessionEntry[];+ const types = entries.map((entry) => entry.type);+ assert.deepEqual(types.slice(0, 3), ["user", "tool_call", "tool_result"]);+ const stoppedEntries = entries.filter((entry) => entry.type === "assistant");+ const stoppedTape = sink.tape.filter(+ (rec) =>+ rec.kind === "message" &&+ (rec.payload as { role?: string; stopReason?: string }).role === "assistant" &&+ (rec.payload as { stopReason?: string }).stopReason === "stop",+ );+ const checkpoints = sink.tape.filter(+ (rec) => rec.kind === "annotation" && (rec.payload as { subturnEnd?: unknown }).subturnEnd === true,+ );+ const partial = findTrailingPartialTurn(entries, "do the thing");+ if (shutdown) {+ assert.deepEqual(stoppedEntries, [], "shutdown must not close the turn with a stop marker");+ assert.deepEqual(stoppedTape, [], "…nor re-tape one for replay");+ assert.deepEqual(checkpoints, []);+ assert.equal((entries[2]!.payload as { interrupted?: unknown }).interrupted, true);+ assert.ok(partial, "the resumed attempt finds the open turn");+ assert.notEqual(resumeStrategy(entries, partial).kind, "restart");+ assert.equal(turnAtSeq(entries, partial.userSeq)?.answer, undefined);+ } else {+ assert.equal(stoppedEntries.length, 1);+ assert.equal((stoppedEntries[0]!.payload as { text?: unknown }).text, "(stopped)");+ assert.equal(stoppedTape.length, 1);+ assert.equal(checkpoints.length, 1);+ assert.equal(partial, null, "a user-facing stop closes the turn");+ }+ } finally {+ globalThis.fetch = realFetch;+ }+ });+}diff --git a/test/tape-fold.test.ts b/test/tape-fold.test.tsindex 4aa79a0df..c1b3335c7 100644--- a/test/tape-fold.test.ts+++ b/test/tape-fold.test.ts@@ -828,3 +828,15 @@ test("a later real result for the same call supersedes the interrupted one in pl"patching a served fold with the re-run result lands it in the same place",);});++test("lintFold flags an assistant message recorded between a tool call and its result", () => {+ const call = { role: "assistant", content: [{ type: "toolCall", id: "c1", name: "t", arguments: {} }] };+ const result = { role: "toolResult", toolCallId: "c1", toolName: "t", content: [{ type: "text", text: "r" }] };+ const stopped = { role: "assistant", content: [{ type: "text", text: "(stopped)" }], stopReason: "stop" };+ const aborted = { role: "assistant", content: [], stopReason: "aborted" };+ const lint = lintFold([{ role: "user", content: [] }, call, stopped, result]);+ assert.ok(!lint.ok);+ assert.ok(lint.problems.some((p) => p.includes("assistant message while 1 tool call(s) await results")));+ assert.ok(lintFold([{ role: "user", content: [] }, call, aborted, result]).ok, "pi drops aborted partials at replay");+ assert.ok(lintFold([{ role: "user", content: [] }, call, result, stopped]).ok);+});diff --git a/test/worker-reaper.test.ts b/test/worker-reaper.test.tsindex 1255f1ec2..8ed5b2dc0 100644--- a/test/worker-reaper.test.ts+++ b/test/worker-reaper.test.ts@@ -378,6 +378,56 @@ test("shutdown aborts the blocked step before the handback, and the next workerassert.deepEqual(events, ["step1", "abort", "release", "resume@2", "step2"]);});+test("shutdown mid-turn through the harness leaves no stop marker, and the resumed attempt finishes the work", async () => {+ const built = buildApp(+ testConfig({ dataDir: mkdtempSync(join(tmpdir(), "harness-resume-")), workers: 1, shutdownDrainMs: 30 }),+ );+ const threadRef = "harness-shutdown-resume";+ const queued = await built.app.turn({+ surface: "test",+ actor: { externalId: "U1" },+ conversation: { kind: "dm", threadRef },+ text: "!work-until-shutdown",+ async: true,+ });+ const entriesNow = async () => {+ const session = await built.sessions.getByThread(threadRef);+ return session ? built.sessions.getEntries(session.id) : [];+ };+ built.runtime.start();+ try {+ const deadline = Date.now() + 5_000;+ while (!(await entriesNow()).some((entry) => entry.type === "tool_result")) {+ if (Date.now() > deadline) throw new Error("the first attempt never recorded its tool work");+ await sleep(10);+ }+ await built.runtime.stopBackground();+ await built.runtime.releaseInFlightRuns();+ const handedBack = (await built.runs.get(queued.runId!))!;+ assert.equal(handedBack.status, "pending");+ assert.equal(handedBack.attempts, 1);+ assert.equal(handedBack.leaseToken, null);+ const interrupted = await entriesNow();+ assert.deepEqual(+ interrupted.filter((entry) => entry.type === "assistant"),+ [],+ "the shutdown-cancelled attempt must not close the turn",+ );+ built.runtime.start();+ const done = await built.runs.waitFor(queued.runId!, 5_000);+ assert.equal(done.status, "done");+ assert.equal(done.attempts, 2);+ assert.equal(done.result?.reply, "finished after resume");+ const replies = (await entriesNow()).filter((entry) => entry.type === "assistant");+ assert.deepEqual(+ replies.map((entry) => (entry.payload as { text: string }).text),+ ["finished after resume"],+ );+ } finally {+ await built.runtime.stop();+ }+});+test("a thrown claim neither kills the worker loop nor blocks the drain handback", async () => {const store = createMemoryRunStore();let explode = 2;