Skip to content

feat(agent-link): keep a handed-over turn running across a restart in the reference client - #1043

Merged
lavaman131 merged 3 commits into
mainfrom
feat/turn-handover-client
Dec 1, 2026
Merged

lavaman131 merged 3 commits into
mainfrom
feat/turn-handover-client

Conversation

@lavaman131

@lavaman131 lavaman131 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Client half of turn-handover: the reference agent-link client (packages/agent-link/src/client.ts) keeps a running Planner turn alive across a server restart, so the next process can take it over instead of the turn being cut.

Pairs with the server side, #1044, now on main; this branch is rebased onto it.

What the client does

  • Asks for turn-handover together with resumable-sessions (the default). With a server that does not list it, nothing changes: no frame carries seq, and a close ends the turn as before.
  • Numbers every turn frame. tools, part, host-tool-call and turn-end carry seq from 1 per turn, numbered as they are sent. A frame too large to send uses no number. Each frame is kept until a server gives a cursor.
  • On turn-handover, the client stops sending that turn's frames to the old process. When the link closes, the turn keeps running: its pending host calls stay open (they are not failed with "The agent connection closed."), and new frames, host calls included, are queued. The resumption carries the turn.
  • On reconnect, resume names the turn in turns. If resumed lists it with after, the client resends every kept frame with seq > after, in order, from the server's cursor and never from what it last sent. It then goes on live on the new link. If resumed leaves the turn out, the client aborts it locally and sends nothing.
  • withinMs starts at the close. If no link takes the turn back in that time, the turn is aborted. If a resume had already named it and resumed still lists it, the client sends exactly one turn-end {status: "aborted", seq: after + 1} and none of the kept frames.
  • A handover is single-use. After resumed takes the turn back, a later close without a new handover ends it. A second handover sends only what follows its own cursor.
  • An ended turn keeps its frames, turn-end included, until its session's next turn, an abort or destroy of it, or the close. A handover that arrives before the server read the end can still pass the end on.
  • The client gives the turn up if the server aborts or destroys it, the client detaches, the resume is refused for good, or the client reconnects to a server without the feature.
  • Kept frames are capped at 32 MiB, counted as UTF-8 bytes. A turn-handover or resumed drops the frames through its cursor, so only the rest count. A turn past the cap is no longer handed over: its frames are dropped and it is left out of the next resume. On an open link it ends at the close as before the feature: pending host calls fail with "The agent connection closed." and the turn is aborted. If its link has already closed, it ends that way at once. Frames are still numbered past the cap.

Tests

client.test.ts adds 14 tests in "Agent connection client handing a turn over". They cover numbering with and without the feature, a turn surviving the close with its calls open and its frames held, resending from the server's cursor, single use, a second handover, an ended turn's frames, the next turn dropping them, the withinMs timer from the close, the aborted end at after + 1, a turn the server does not list, rollback, refusal, abort, destroy and detach, and the cap: crossed before a handover, after it on the open link, after the close, and after the resume named the turn, with only frames after the cursor counted.

Each line of the new client logic was removed or altered in turn (40 mutations), and every one makes at least one test fail.

apps/server/src/chat/turn-handover.test.ts adds "the reference client across a deploy". A real connectAgent turn has edit_plan applied by the first process and is still running when that process hands it over and closes. The client reconnects to the next process with resume and finishes the reply there. The test checks that the edit was applied once, that the reply reads "Writing more." exactly once, and that Chat says nothing of a restart. It fails with a client that ends its turns when the link closes.

Verification on 8f8fd3d4 (main 10417854 + 3 commits):

  • the interop test, run 3 times: 1 pass each
  • bun test packages/agent-link: 85 pass
  • bun test of the server's remote-link, testing, remote-planner, turn-handover, threads and MCP link tests: 475 pass
  • bun run fix, bun run ci and bun run types pass, and oxlint --no-ignore reports nothing on the changed files

Not run: E2E.

@lavaman131
lavaman131 force-pushed the feat/turn-handover-client branch from ddceaf5 to 8f8fd3d Compare November 30, 2026 23:53
@lavaman131
lavaman131 marked this pull request as ready for review November 30, 2026 23:54
lavaman131 added a commit that referenced this pull request Dec 1, 2026
The coarse-pointer fix (0b678f8) moved one test off a loop of calls per
element; the same loops kept others near their minute in CI. "A copied
element is as many pixels as its Tailwind classes say" checked 128
elements with five or six traced calls each, and timed out on main at
1041785 (run 46540012615) and on #1043 (run 46540622380). In CI it took
42 to 60s, and the message box took 47 to 49s, re-reading every
control's visibility for each control in turn. The icon family test took
39 to 46s.

Each such loop now reads all its elements in one evaluateAll and asserts
on the result, with the same assertions and the same messages: the
element's label, index or classes, and the property.

- assistant-ui-geometry: expectClassGeometry checks every element a
  locator finds in two calls and returns how many it checked; the handoff
  pills and subagent cards, the message actions, toolbar and reaction
  pills, the gallery tiles, the message box's controls and icons, and the
  staged chips are read the same way.
- fixture: shown, Playwright's isVisible for an element that is not
  display: contents, to ask of every element in one call.
- emoji (the icon family), touch-targets (rows, their targets and where
  a finger lands between them), connecting-indicator (the dots) and
  chat-media (toolbar buttons and the file icons' inks) likewise.

Assistant-model: Claude Opus 5.5
Assistant-verification: bun run design:behavior on origin/main and on this change, both in full: 123 passed, 3 skipped; locally the Tailwind classes test went from 21.6s to 11.1s (wide) and 21.6s to 9.6s (narrow), the message box from 20.3s to 4.8s and 20.0s to 4.3s, the icon family from 19.6s to 5.0s and 18.4s to 5.0s, the media colours from 8.9-10.1s to 6.0-6.3s, the message actions from 7.1s to 5.0s and 6.4s to 4.8s; an injected 3px border on a gallery tile fails with its label, classes and "border widths"; bun run fix (its oxlint autofixes to two unrelated connector files reverted), ci and types
… the reference client

The reference client now asks for turn-handover beside resumable-sessions.
On a link that accepted it, every tools, part, host-tool-call and turn-end
frame of a turn carries seq from 1, numbered as it is sent (a frame too
large to send uses no number) and kept until a server gives a cursor.

On turn-handover the client stops sending that turn's frames to the old
process, and when the link closes it keeps the turn running, its host calls
open (no LINK_CLOSED, no abort) and new frames, host calls included,
queued. The resumption carries the turn; the next link names it in resume.
A resumed that lists it resends every kept frame after the server's cursor,
in order, then goes on live; one that leaves it out aborts it locally and
sends nothing. A turn not taken back within withinMs of the close is
aborted; if a resume had already named it and resumed lists it, the client
sends one turn-end {status: aborted, seq: after + 1} and nothing else. A
handover is used once: the next close without a new one ends the turn. An
abort, destroy or detach, a refused resume, or a server without the feature
gives it up.

An ended turn keeps its frames, its turn-end included, until its session's
next turn, an abort or destroy, or the close, so a handover that arrives
before the server read the end can still pass it on. A server without the
feature sees exactly what it saw before: no seq, and a close ends the turn.

Assistant-model: Claude Opus 5.5
Assistant-verification: bun test packages/agent-link (82 pass; 26 single-line mutations of the new client code each fail a test); bun test of apps/server harness/remote, testing, chat/remote-planner, chat/threads, mcp link tests (452 pass); bun run fix, ci and types; oxlint --no-ignore on the changed files
A turn kept every frame since the last cursor a server gave, without
bound. It now counts the UTF-8 bytes of the frames it keeps. A
turn-handover, like a resumed, drops the frames through its cursor and
counts only the rest. Once the kept frames pass 32 MiB the turn can no
longer be handed over: its frames are dropped and it is left out of the
next resume. On an open link it ends at the close as before the
feature, with its pending host calls failing with "The agent connection
closed." and the turn aborted, and a turn-handover for it is ignored.
On a link that has already closed it ends that way at once; if a resume
already named it, the resumed that lists it gets the one aborted
turn-end at its cursor. Frames are still numbered past the cap, because
the server requires seq on every frame.

Assistant-model: Claude Opus 5.5
Assistant-verification: bun test packages/agent-link (85 pass, 5 runs); 40 single-line mutations of the client's handover code each fail a test; bun test of apps/server harness/remote, testing, chat/remote-planner, chat/threads, mcp link tests (452 pass); bun run fix, ci and types; oxlint --no-ignore on the changed files
The turn-handover tests drove the server with raw frames; this one runs a
real connectAgent client against it. Its turn writes "Writing", has
edit_plan applied by the first process, and is still running when that
process hands it over and closes. The client keeps it in its resumption,
connects to the next process with resume, and finishes the reply there.
The edit is applied once, the reply reads "Writing more." exactly once,
and Chat says nothing of a restart. It fails with a client that ends its
turns when the link closes. takeOver now takes just the linked document,
so the test needs no Deployed of the raw-frame helper.

Assistant-model: Claude Opus 5.5
Assistant-verification: bun test apps/server/src/chat/turn-handover.test.ts -t "reference client" (3 runs, 1 pass each; fails with the client's close-keeps-handover branch removed); bun test packages/agent-link (85 pass); apps/server harness/remote, testing, chat/remote-planner, chat/turn-handover, chat/threads, mcp link tests (475 pass); bun run fix, ci and types
@lavaman131
lavaman131 force-pushed the feat/turn-handover-client branch from 8f8fd3d to f5c63ae Compare December 1, 2026 00:55
@lavaman131
lavaman131 merged commit d37df7c into main Dec 1, 2026
26 checks passed
@lavaman131
lavaman131 deleted the feat/turn-handover-client branch December 1, 2026 01:10
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