perf(server): store a subagent's record and its item in one commit #37624
main fromrefs/pull/37624/head·opened View on GitHubProblem#
While a subagent runs, each of its progress updates reaches the server as subagent.updated, immediately followed by the subagent's turn_item.updated. main stores them as two SQLite commits: one holds only the subagent.updated row, the next only its turn_item.updated. Every commit pays the same fixed set of hot pages: the thread row and its index, projection metadata, sqlite_sequence, page 1, and the event log's index leaves. In a Claude turn with two background subagents that is 10 extra commits per turn, and joining each pair cuts the turn's WAL frames per event by 4–5%. On a real database these pairs are 14–18% of all events. This is one of the parts of #37600.
Change#
subagent.updatedfrom an adapter can carry its item (turnItem) when nothing is emitted between the record and the item:- Claude for progress-only updates;
- Codex, Pi, Cursor, OpenCode and ACP for every record their item follows.
Muse emits
node.updatedbetween the two, so it is unchanged.providerEventMembersinprovider-coresplits such an event back into the two events. The session manager and the run consumer give each member the handling of a standalone event: routing, the delivery filter, child and background-item tracking, and ingestion.The run ingests both members through
ProviderEventIngestor.ingestTogether, in one transaction. Work that must wait for the commit goes into an after-commit queue, and nothing in it runs if the transaction fails:- The run's tracking of open subagents, nodes and items. It cannot fail, so all of it applies right after the commit, before anything else, and without an interruption between the commit and it. A pair that rolls back therefore leaves the open work that failure finalization ends exactly as stored. A failed write after the commit still leaves both committed members tracked.
- The child thread's model sync. It takes a thread lock, and taking a thread lock inside the write transaction would deadlock with a command that holds the lock and waits to write. It runs after the tracking, still on the consumer, before the next event.
Clients receive the same two events, with the same contents, in the same order. When the child's model needs a sync, the child thread's thread.model-selection-updated now follows the parent's item instead of preceding it. That event is on another thread's stream, and every reader of it replaces the child thread row from its payload.
Scope#
A performance bug: two events that the adapter emits back to back for one update are written as two commits. It is one part of #37600, which measures the write amplification these commits cause.
Verification#
What shows the problem on main. Attribute each new event row to the WAL commit it first appears in, as the harness below does, and look at the commits that hold a subagent.updated row. The counts are the same in every run and do not depend on timing or host load:
| Revision, runs 1–3 | Commits with a subagent.updated row | Holding only that row, then a commit holding only its turn_item.updated | Holding the row and its turn_item.updated |
|---|---|---|---|
main | 14, 14, 14 | 10, 10, 10 | 0, 0, 0 |
| this PR | 14, 14, 14 | 0, 0, 0 | 10, 10, 10 |
The other 4 per turn are the two subagents' start and end records, which no item follows; they are alone on both revisions. Frames per event below are what those 10 commits cost.
Live turns. A scratch server on this branch and on main at 760f9cbe64 ran three live Claude turns each (claude-haiku-5-5, two background subagents running five Bash steps each), interleaved. A harness (apps/server/scripts/bench-event-writes.ts on a branch of our fork, not part of this PR) read every WAL frame the server appended during each turn and attributed it to commits. It checks that every new event row appears in the frames it read. The host's load average was 9–26 during the runs; the counts measured here do not depend on timing.
| Run | Events | Event commits | Events per commit | WAL frames per event |
|---|---|---|---|---|
main 1 | 172 | 122 | 1.41 | 20.67 |
main 2 | 204 | 139 | 1.47 | 20.37 |
main 3 | 203 | 138 | 1.47 | 20.33 |
| this PR 1 | 204 | 127 | 1.61 | 19.47 |
| this PR 2 | 205 | 128 | 1.60 | 19.46 |
| this PR 3 | 203 | 126 | 1.61 | 19.30 |
Across runs of the same size, event commits fell 8–9% and WAL frames per event fell 4.2–5.3%. The first main run did fewer steps, so it has fewer events.
Replay of recorded windows, measured on this branch's first version against main of November 12, through the server's own EventSink, on a copy of a real database, where counts repeat exactly between runs:
| Window | Commits | WAL frames per event | Checkpoint pages per event |
|---|---|---|---|
| 5,417 events | 5,030 → 4,472 | 29.84 → 28.55 (−4.3%) | 11.33 → 11.13 |
| 418 events | 392 → 344 | 31.78 → 30.31 (−4.6%) | 12.69 → 12.46 |
| a Claude turn without subagents | 44 → 44 | unchanged | unchanged |
Tests:
ProviderEventIngestor.test.ts:- "commits a subagent record and its item at once, then syncs the child's model after it";
- "restores a native subagent's model that a command changed while its update was queued".
RunExecutionService.test.ts:- "handles a subagent record and its paired item as two events in one write", with and without the commit coordinator;
- "ends a subagent whose terminal record was rolled back with its paired item";
- "keeps a committed subagent pair settled when its child model sync fails".
The last two fail if the tracking runs inside the transaction or after the model sync.
The adapter tests for Claude ("streams a background subagent's work while the root turn is idle"), Pi, OpenCode and ACP check the paired emissions. Codex and Cursor already emitted the record and its item from one place, which now emits one event.
vp run check:changedpasses: format, lint, knip, graphql, typecheck and tests.
Model and harness: Claude Opus 5.5, Claude Code in T3 Code
- 1
- 2
Pushed011be97
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-rebase
Closing this PR rather than rebasing its 19-file cross-adapter diff after #38544 and #39502 landed provider transaction batching on
main.A deterministic replay of the recorded 172-event workload against
main(fce54e67a6) shows a schedule-dependent trade-off rather than a clear write win:- Pairing the 9 post-terminal subagent progress pairs saves 9 of 119 commits (−7.56%) and 133 of 3,696 WAL frames (−3.60%, −548 KB).
- Pairing pre-terminal pairs saves 0 commits and adds +3 WAL frames (+12 KB), and in burst snapshot-batching mode increases WAL bytes by +4.69% because pulling
turn_item.updatedinto the control transaction removes it from an adjacent snapshot batch.
That remaining post-terminal delta does not justify carrying a 19-file cross-adapter contract change, and closing this frees a PR slot.
Model and harness: Gemini 4 Neon, Antigravity in T3 Code; executor and advisor: GPT-6.2 Astra (Codex)
Vantrongs(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
This PR conflicts with
main. Rebase it or mergemaininto it. If it still conflicts after 7 days with no activity, it will be closed automatically.See the conflict window.