perf(server): runs finish when the provider turn ends, not after checkpoint capture #41362
t3/remove-turn-changed-files-card fromrefs/pull/41362/head·merged by juliusmarminge View on GitHubProblem#
A run counted as finished about 1.5 s after the model's last byte. About 700 ms of that is turn-end checkpoint capture (about 10 sequential git commands, then ref lookups, the diff and the authored paths). RunExecutionService wrote a finished turn as waiting and flipped it to completed only after capture. Clients treat waiting as active, so the run looked busy throughout capture. With the files-changed card gone (#41317), nothing in the timeline needs the checkpoint when the turn ends.
Change#
- Run completion: a turn that ends normally is written as
completed, withcompletedAtset to the turn end, in the same commit that enqueues thecheckpoint.captureoutbox effect. Interrupted and cancelled runs already worked this way. - Capture: capture accepts completed runs and only attaches the checkpoint to them. It adds the checkpoint, its turn item, and
checkpointIdon the run. It no longer moves status orcompletedAt, and it no longer touches the root node. Runs stored aswaitingby older servers still use the old flip. - Ordering: commands still order behind the capture. Under
main,waitingwas a barrier: steers, queued turns and hand-off checks saw an active run until capture committed, so steer deliveries already queued on the thread's effect lane settled first. To keep that:readCommandProjectionpresents the latest run aswaitingto command decisions while its capture effect is pending or running.requeueUnreadSteersand the run-end reactor wait for the capture commit, which repeats the run update.- Clients and projections only ever see
completed.
- Lane: the per-thread effect lane already serializes the next turn's start (and its baseline checkpoint), rollback and restore behind the capture. A new test pins it: a start enqueued while a capture is running cannot be claimed until that capture finishes.
Checkpoint readers while capture is pending#
- Diff panel, rewind targets, mobile review: built from checkpoint records, so a pending turn simply has no entry yet. Same as during
waitingundermain. getTurnDiff: only receives turn counts that clients read from existing checkpoints.- Proactive diff panel: already defers until the checkpoint arrives.
- Timeline (web and mobile): skips
checkpointturn items, so nothing shifts when capture lands. - MCP thread read: lists the checkpoint turn item once it exists. No MCP tool reads refs.
- Rollback: its effect runs on the same lane, after the capture.
- Fork and merge-back from
latest_stable: this source needs a run with a checkpoint. The dispatch now waits for the source's pending capture before it takes any thread lock (capture commits under that lock), so a fork right after a turn starts from that turn.
Behavior changes#
t3_thread_waitanddelegate_tasksettle at turn end, before capture.- A delegated child whose turn ended reports
waiting_for_children(notworking) while its capture runs. - Cancelling a task whose child turn has ended settles it in the cancel's own commit.
- A send that arrives during the capture window is queued behind the capture, as it was while the run was
waiting. - If a capture fails permanently, the run stays
completedwithout a checkpoint and the queue stays held. Undermainthe run stayedwaiting.
Scope#
Product decision by Julius and Maria: remove the files-changed card and capture turn-end checkpoints asynchronously. Layer 2 of 2, stacked on #41317.
Verification#
- Pipeline A/B (
node scripts/server-metrics.ts ab --only pipeline --rounds 3, harness at #41282 head 996169f, A =main65cd644, B = this branch b46422c; the later fork-wait commit does not touch the pipeline path, cups linux-x64):
| metric | main | this PR | change | 95% interval |
|---|---|---|---|---|
pipeline.turnEndMs.median (API stream end → finished run) | 1335 ms | 929 ms | −30% | −472 to −148 ms |
pipeline.msPerTurn | 4506 | 4542 | +1% | −406 to 511 |
pipeline.gcPauseMs | 1343 | 2009 | +50% | 391 to 1216 (flagged worse) |
gcPauseMs is flagged worse with 3 rounds a side. I have not explained it; capture now overlaps the harness's next send. Per-turn time is unchanged.
- Real web client: dev server with the Replay (dev) provider, in a throwaway git repo with 20k untracked files to make capture slow. The run's
completedAtwas 08:23:25.563. The client dropped "Stop generation" at 25.977 (about 0.4 s of client latency). Capture then ran for about 30 s in the background and hit the existing 30 s VCS timeout (anerrorcheckpoint, old behavior). The client recorded no timeline layout change after the completion frame. - Tests: a new lane-ordering test in
RunFinalizationQueue.test.ts. The capture tests now cover a completed run (status andcompletedAtkept, nonode.updated); the legacywaitingcases still cover the flip. Tests and replay-fixture assertions that waited for thewaitingevent now wait forcompleted. The replay scenario harness counts a completed run as busy until its checkpoint lands. 18 affected server files pass in an isolated PID namespace (774 tests).vp run check:changedpasses: format, lint, knip, typecheck (9 packages), tests (26 files) and the react compiler check. - Docs:
docs/internals/overview.md"Turn completion and checkpoints" now describes completion before capture and the command-side hold.
Model: Claude Opus 5.5 in Claude Code (T3 Code).
🤖 Generated with Claude Code