Skip to content

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
lavaman131 merged 4 commits into
mainfrom
feat/turn-handover-server
Nov 30, 2026
Merged

lavaman131 merged 4 commits into
mainfrom
feat/turn-handover-server

Conversation

@lavaman131

@lavaman131 lavaman131 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

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 with resumable-sessions)

  • Every tools, part, host-tool-call and turn-end frame carries seq (1, 2, … per turn). A link with the feature must number every frame; a link without it must number none.
  • The server can send turn-handover {session, turn, after, withinMs} instead of abort. resume.turns names the turns the client still runs, and resumed.turns lists 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:

  • From then on its frames are dropped and never answered.
  • Its host tools are not stopped. Every call that may write finishes on its own, within 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.
  • After that, the client is sent 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_holds while 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 abort on the still-open link.

Next process

  • It rewrites the hold row without the checkpoint (fenced) before it shows the hold. A failed rewrite means the turn is only ended, never adopted.
  • Chat.restore holds 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.
  • The server answers resume only after that. It must come from the same account and machine, and name the same document, session and turn.
  • After resumed the server:
    • sends each write's result again;
    • re-runs owed reads (reads never answered, or answered with no frame applied after the result);
    • applies only 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:

  • no client takes it by max(window, withinMs) (30 s);
  • the client comes back without it;
  • the transcript doesn't match the digest.

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:

  • Run again as at first (attempt and notice removed, Retry semantics) if the attempt called only reads. Calling any other tool counts as a write, whether or not it was applied.
  • Continue from the saved Atomic session if it called a write. The agent is told what each call actually came to.
  • Otherwise: the notice and Retry stay.

A background job that is cut goes back to pending, with no attempt spent. Chat.restore now 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:

    • frames dropped unanswered;
    • writes decided before the hand-over;
    • the gate (a failed hold write);
    • every write is carried, including those from an earlier process;
    • owed reads;
    • write-back;
    • consume-once;
    • the digest guard;
    • session and turn identity;
    • the line waiting;
    • the cursor, gaps and repeated ids;
    • the deadline;
    • the restore notice;
    • old clients;
    • the endpoint;
    • seq rules;
    • a write never stopped half way;
    • a failed adopted run.

    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, types pass; oxlint --no-ignore on changed files: only the 2 warnings also on main.

    • bun run fix, ci, types: pass.
    • oxlint --no-ignore on 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 connectAgent across 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

…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
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