From 3e72d97be6dc03727e3c1d69725de4e4f62ce48e Mon Sep 17 00:00:00 2001 From: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Date: Fri, 4 Dec 2026 04:38:32 +0000 Subject: [PATCH] fix(runner): keep room to retire a session that filled its command journal The Core command journal stops at 500 commands. A long run journals one semantic tool result per tool call, so its journal can reach that bound. The next run then cannot queue turn.stop, runner.drain, runner.suspend or session.close to retire that session. Every successor run fails with native_predecessor_retirement_unproven (command_journal_full). Work commands still stop at 500. Retirement commands now have a separate bounded reserve of 64 slots. State and checkpoint validation accept the reserve so that the retired journal restores. Co-Authored-By: Claude Opus 5.5 --- .../durable-prp-control-plane.test.ts | 50 +++++++++++++++++++ .../durable-prp-control-plane.ts | 16 ++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.test.ts b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.test.ts index 2bd82634631..01695a68a53 100644 --- a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.test.ts +++ b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.test.ts @@ -95,6 +95,56 @@ it("admits only source maintenance while the authenticated owner differs", async } finally { await core.stop(); rmSync(root, { recursive: true, force: true }); } }); +function fillAndRetireCommandJournal(core: DurablePrpControlPlane): void { + for (let index = 0; index < 500; index += 1) { + core.queueCommand("semantic_tool.result", { callId: `call-${index}`, result: { ok: true }, isError: false }, + `command_tool_full_${index}`); + } + expect(() => core.queueCommand("turn.start", { text: "more work" })).toThrow("command_journal_full"); + expect(() => core.queueCommand("session.snapshot")).toThrow("command_journal_full"); + for (const type of ["turn.stop", "runner.drain", "runner.suspend", "session.close"]) { + expect(core.queueCommand(type, {}, `command_retire_${type.replace(".", "_")}`).type).toBe(type); + } +} + +it("keeps room to retire a session whose work filled the command journal", async () => { + // A long run can journal one semantic tool result per call until the work + // bound is reached. The next run must still be able to stop, drain, suspend + // and close that session, or every successor fails predecessor retirement. + const root = mkdtempSync(resolve(tmpdir(), "runner-full-journal-retirement-")); + const core = new DurablePrpControlPlane({ stateDirectory: root, identity, expectedRunnerVersion, expectedRunnerDigest }); + let reopened: DurablePrpControlPlane | undefined; + try { + fillAndRetireCommandJournal(core); + expect(core.store.state.commands).toHaveLength(504); + await core.stop(); + reopened = new DurablePrpControlPlane({ stateDirectory: root, identity, expectedRunnerVersion, expectedRunnerDigest }); + expect(reopened.store.state.commands).toHaveLength(504); + expect(reopened.queueCommand("runner.suspend", {}, "command_retire_again").type).toBe("runner.suspend"); + } finally { + await reopened?.stop(); + await core.stop(); + rmSync(root, { recursive: true, force: true }); + } +}); + +it("checkpoints a retired session whose work filled the command journal", async () => { + const root = mkdtempSync(resolve(tmpdir(), "runner-full-journal-checkpoint-")); + const core = new DurablePrpControlPlane({ stateDirectory: root, identity, expectedRunnerVersion, expectedRunnerDigest }); + let checkpoint!: DurablePrpCheckpoint; + core.setCheckpointPersistence(async value => { checkpoint = value; }); + try { + fillAndRetireCommandJournal(core); + await core.flushCheckpoint(); + expect(core.store.state.commands).toHaveLength(504); + const restored = decodeDurablePrpCheckpoint(checkpoint.records); + expect([...restored.records.keys()].filter(key => key.startsWith("command:"))).toHaveLength(504); + } finally { + await core.stop(); + rmSync(root, { recursive: true, force: true }); + } +}); + it("keeps already-dispatching target work independent of predecessor completion", async () => { const root = mkdtempSync(resolve(tmpdir(), "runner-target-dispatch-")); const core = new DurablePrpControlPlane({ stateDirectory: root, identity, expectedRunnerVersion, expectedRunnerDigest, diff --git a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts index 3cfe779859c..17dadd7e85a 100644 --- a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts +++ b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts @@ -67,6 +67,15 @@ const maxFrameBytes = 1024 * 1024; const maxCommandBytes = Math.floor((maxFrameBytes - 4 * 1024) / 2); const RUN_ATTACH_ENVELOPE_BYTES = 32 * 1024; const maxCommands = 500; +// Work commands (tool results, turns, reads) stop at maxCommands. A session +// whose work fills that bound must still be stoppable and retirable by the +// next run, so these commands keep a separate bounded reserve. Each close +// attempt issues at most a stop, a drain and a few suspends. +const retirementCommandTypes = new Set([ + "turn.stop", "runner.drain", "runner.suspend", "runner.shutdown", "session.close", "session.destroy", "run.cancel", +]); +const retirementCommandReserve = 64; +const maxJournalCommands = maxCommands + retirementCommandReserve; const maxCommandsAtAttach = 32; const maxEventsPerAck = 64; class CommandJournalLimitError extends Error { @@ -608,7 +617,7 @@ function checkpointState(records: ReadonlyMap) { .sort((a, b) => Number(a.slice(prefix.length)) - Number(b.slice(prefix.length))); const commandKeys = v2 ? keys("command:") : header.commands; const eventKeys = v2 ? keys("event:") : header.events; - if (!Array.isArray(commandKeys) || commandKeys.length > maxCommands + if (!Array.isArray(commandKeys) || commandKeys.length > maxJournalCommands || !Array.isArray(eventKeys) || eventKeys.length > maxCommittedEventWindow || commandKeys.some(key => typeof key !== "string" || !/^command:\d+$/.test(key)) || eventKeys.some(key => typeof key !== "string" || !/^event:\d+$/.test(key)) @@ -1209,7 +1218,7 @@ function isStoredCoreState( !isRecord(value.tickets) || !isRecord(value.leases) || !Array.isArray(commands) || - commands.length > maxCommands || + commands.length > maxJournalCommands || !Array.isArray(events) || events.length > maxCommittedEventWindow || !Number.isSafeInteger(value.ackedSourceSeq) || @@ -3160,7 +3169,8 @@ export class DurablePrpControlPlane { status: "pending", result: null, }; - if (this.#store.state.commands.length >= maxCommands) + const journalLength = this.#store.state.commands.length; + if (journalLength >= maxJournalCommands || (journalLength >= maxCommands && !retirementCommandTypes.has(type))) throw new CommandJournalLimitError("command_journal_full"); if (Buffer.byteLength(JSON.stringify(command)) > maxCommandBytes) throw new CommandJournalLimitError("command_payload_too_large");