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");