Skip to content

feat(pi): drain and resume pi() runs - #327

Merged
eersnington merged 4 commits into
stack/chore-pi-bump-pi-coding-agent-to-1-0-2-tvmyommkfrom
stack/feat-pi-drain-and-resume-pi-runs-rklvtvuy
Oct 5, 2026
Merged

eersnington merged 4 commits into
stack/chore-pi-bump-pi-coding-agent-to-1-0-2-tvmyommkfrom
stack/feat-pi-drain-and-resume-pi-runs-rklvtvuy

Conversation

@eersnington

@eersnington eersnington commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

pi() gets the same deploy behavior as piDurable(): live runs drain, and a run cut off by a stop resumes on wake when Pi can continue it.

0.1.0 This PR
Actor stops mid-run session.abort(), the run ends as aborted waits for the run to end, then closes, inside sleepGracePeriod (15 min default)
Run outlives the drain aborted aborted in memory with nothing aborted stored, and resumes on the next wake if Pi accepts the transcript
Generation lost (the engine moved the actor) aborted through c.abortSignal aborted in memory at once, nothing stored; the next generation resumes it
prompt times out, or its caller leaves run aborted prompt rejects, and the run keeps going. Only the abort action stops it
Destroy abort abort

Drain and lost generations

  • The drain deadline is taken before the app's own onSleep and keeps time back for closing (drainDeadline in src/drain.ts), so the final save and the sandbox suspend run before RivetKit's grace deadline.
  • A lost generation skips onSleep, so stopRunWhenLost listens on the actor's abort signal from onWake and aborts the run when the context reports isLost. Otherwise the old process keeps calling the model and running tools next to the generation that resumed the run. RivetKit releases without lost generations still run onSleep, and the drain handles them.

When a run resumes

Resume never writes an entry Pi would not write. It continues only when Pi's own Agent.continue() accepts the stored transcript: the last message is a user or tool-result message.

Stopped while Transcript ends with Resumes?
the model was answering the user message, or the last tool results yes. The model is asked again, and the partial answer is lost
parallel tools ran and at least one finished a tool result yes. Unfinished calls get pi-ai's "No result provided"
a tool ran and none finished the assistant message with the tool calls no
Pi's own abort ran an aborted assistant message no, which is what an abort means

A chat that does not resume is left as after an abort, and the next message continues it.

How

  • Migration 2 adds run_state and queued_json to pi_session. A run start records running and the queued steer and follow-up messages; the end records idle.
  • On wake, after the app's onWake, resumeInterruptedRun replays the queue and drives AgentSession's own run loop, so retry, compaction, and queued messages work as in any run:
for (const text of queue.steering) await internals._queueSteer(text);
for (const text of queue.followUp) await internals._queueFollowUp(text);
// The loop starts with agent.prompt; for this one call it continues instead.
Object.defineProperty(agent, "prompt", { configurable: true, value: async () => {
	Reflect.deleteProperty(agent, "prompt");
	await agent.continue();
} });
await internals._runAgentPrompt([]);
  • These are private methods. pi-coding-agent is pinned exactly, and the resume test fails if an upgrade changes them.
  • A resumed run runs in the background under c.keepAwake. A client that was waiting on prompt when the actor stopped got an error; it sees the finished run through events or getMessages().

This is part 8 of 11 in a stack:

@eersnington
eersnington force-pushed the stack/chore-pi-bump-pi-coding-agent-to-1-0-2-tvmyommk branch from 6e58b4b to f2bb142 Compare October 5, 2026 18:53
@eersnington
eersnington force-pushed the stack/feat-pi-drain-and-resume-pi-runs-rklvtvuy branch from e52d027 to 3f69319 Compare October 5, 2026 18:53
@eersnington
eersnington merged commit 90587b2 into stack/chore-pi-bump-pi-coding-agent-to-1-0-2-tvmyommk Oct 5, 2026
1 of 3 checks passed
@eersnington
eersnington deleted the stack/feat-pi-drain-and-resume-pi-runs-rklvtvuy branch October 5, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant