Repository navigation
feat(server): hand a remote Planner's running turn to the next process at a deploy, and pick up a cut one once - #1044
Merged
Merged
Conversation
…he next process (turn-handover)
A new optional feature, turn-handover, offered beside resumable-sessions:
each tools, part, host-tool-call and turn-end frame carries seq (1, 2, ...
per turn); a restarting server may send turn-handover {session, turn,
after, withinMs} instead of aborting a running turn; resume names the
turns a client still runs from one (turns), and resumed lists the turns
the server adopted with its cursor (turns[].after).
Assistant-model: Claude Opus 5.5
Assistant-verification: bun test packages/agent-link (71 pass)
…s at a deploy
A client that offered turn-handover (beside resumable-sessions) keeps its
turn running across a restart, and the next process takes it back once:
- The old process suspends the turn instead of aborting it: its frames are
dropped unanswered from then on, its host tools are stopped so a write
already asked for settles applied or refused, and only then is the client
sent turn-handover {after, withinMs}. Chat leaves the reply and its rows
as they are and commits them.
- The checkpoint (cursor, every write's outcome, owed reads, Chat's ids and
a digest of the turn's entries) goes into the session's entry of
planner_link_holds, written while the process still serves and holds the
writer lease; one that cannot be written makes the process end the turn
itself, saying so, and tell the client to abort it.
- The next process rewrites the row without the checkpoint before it shows
the hold, opens the document so Chat holds the turn open (busy, so its
line waits), answers a resume naming it only then, for the same account
and machine, with its cursor, sends each write's outcome again, runs owed
reads again, and applies each frame after the cursor once. A frame sent
again is dropped; one that skips a number or repeats a call fails the turn.
- A turn nobody takes back by max(window, withinMs), whose client comes back
without it, or whose transcript is not the one it was handed over beside,
ends with a notice and each write's real outcome. A process that stops
before the client returns writes the checkpoint back.
- Chat.restore now says a restart interrupted a reply it ends mid-way.
Assistant-model: Claude Opus 5.5
Assistant-verification: bun test src/chat/turn-handover.test.ts (20 pass); bun test src/chat src/harness src/handover (1856 pass before the last fixes); tsgo; oxlint --no-ignore on changed files
…a handed-over write half way A turn a restart cuts off that cannot be handed over (one run in Chopin, or on a client without turn-handover) still ends with a notice, and a person's message whose reply it cut waits in line again, at the place it was sent, with a record on its entry (server-only, never sent to a client) that the next process picks it up once: - run again as at first, its cut attempt and notice taken out, when the attempt called nothing but reads (READ_TOOLS; any other tool, one it does not name included, counts as a write, applied yet or not); - gone on with from the saved Atomic session when it called a write, the agent told what each call truly came to, as its rows say; - otherwise not at all: the notice and its Retry stand. A message is picked up once however many restarts follow, and a background job a restart cuts off goes back to wait, its attempts unspent. Hand-over: the old process no longer stops the turn's host tools, since a write stopped half way can report an outcome that is not what it did; it waits, a bound at most, for every write to finish. A turn the next process took back whose run fails before its harness takes it is aborted on its client, so nothing waits on a harness that will not come. Docs: the turn-handover feature and its rules in agent-connection.md, the deploy behaviour in zero-downtime-deploys.md, and AGENTS.md. Assistant-model: Claude Opus 5.5 Assistant-verification: bun test src/chat/turn-handover.test.ts src/chat/restart-recovery.test.ts src/chat/stopping-turns.test.ts (all pass); bun run fix, ci, types; oxlint --no-ignore on changed files (2 warnings, both on main)
…and how a cut turn is picked up Assistant-model: Claude Opus 5.5 Assistant-verification: bun run ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A Planner turn running on a person's machine now survives a deploy, and a turn that cannot is cut honestly and picked up once by the next process.
Client side: #1043. Both PRs are needed to hand a turn over end to end. Each one also works on its own: old clients and old servers behave exactly as they do today.
Wire (
turn-handover, offered only withresumable-sessions)tools,part,host-tool-callandturn-endframe carriesseq(1, 2, … per turn). A link with the feature must number every frame; a link without it must number none.turn-handover {session, turn, after, withinMs}instead ofabort.resume.turnsnames the turns the client still runs, andresumed.turnslists the ones the server adopted, each with the server's cursor (after).Remote turn handed over (old process)
When the turn-wait runs out, a turn whose client offered the feature is suspended, not aborted:
EDIT_SETTLE_MS+ 1 s, so no write is cut half way and each one records its real outcome. A turn whose writes don't finish in time is cut.turn-handover.The checkpoint is: the cursor, every write's outcome (including writes an earlier process decided), the reads to run again, Chat's ids, and a digest of the turn's entries. It goes into the session's entry in
planner_link_holdswhile the process still serves and still holds the writer lease. No sidecar key and no migration are added.If that write fails, the process ends the turn itself: it posts a notice, labels each write with its real outcome, and sends
aborton the still-open link.Next process
Chat.restoreholds the turn open, and the Chat stays busy so nothing in its line starts. This happens only if the entries still match the digest; otherwise the turn is ended with real labels for each write.resumeonly after that. It must come from the same account and machine, and name the same document, session and turn.resumedthe server:seq = cursor + 1. Repeated frames are dropped. A skipped frame, or a reused call id, fails the turn.The turn ends with a notice saying why if:
max(window, withinMs)(30 s);A process that drains with an adoption still pending writes the checkpoint back and answers no resume. An adopted run that fails before the harness takes the turn aborts the turn on the client.
Other turns (in-process, or a client without the feature)
The turn is cut with a notice. If the turn was a person's message, the message goes back into the line at the place it was sent, with a server-only recovery record on the entry. The next process picks it up once:
A background job that is cut goes back to pending, with no attempt spent.
Chat.restorenow posts a notice for every reply it ends mid-way, including after a crash.Tests
apps/server/src/chat/turn-handover.test.ts(22 tests). These run two or three server instances over one store with a raw numbered client. They cover:seqrules;Each guard was checked by removing it.
apps/server/src/chat/restart-recovery.test.ts(8 tests) covers retry, continue, Retry-only, once only, place in line, a write that was called but not reported, and jobs.Results:
On head 27f0405 (rebased on main):
bun test(apps/server) chat, harness, handover, ai: 2080 pass, 0 fail; packages/agent-link: 71 pass. Before the rebase, also plan, questions, storage, agent: 3347 pass.bun run fix,ci,typespass;oxlint --no-ignoreon changed files: only the 2 warnings also on main.bun run fix,ci,types: pass.oxlint --no-ignoreon the changed files: only the 2 warnings that are also on main.With feat(agent-link): keep a handed-over turn running across a restart in the reference client #1043 merged into a scratch branch: 515 pass across the harness/remote, remote-planner, turn-handover, testing and agent-link suites. A scratch test using the real
connectAgentacross a deploy also passes: the turn keeps running and the reply ends once, whole. That test is not committed because it needs feat(agent-link): keep a handed-over turn running across a restart in the reference client #1043.e2e/handover.e2e.ts: in this environment 10 of 13 fail identically on
origin/main(the machine link "stepped away" before the first turn starts). No new e2e was added here. A two-process e2e for adoption needs feat(agent-link): keep a handed-over turn running across a restart in the reference client #1043's client.Scope and risks