Problem#
Claude Code reports a background task's start both as task_started and as a background_tasks_changed level, and its end both as a level and as task_notification, in either order (the recorded fixtures claude_background_task_after_root, claude_background_task_wake and claude_background_monitor_wake under apps/server/src/orchestration-v2/testkit/fixtures/ show both orders). A level also lists subagents (local_agent), which parseClaudeBackgroundTaskEntry leaves out of the roster. applyBackgroundTaskRosterMessage in ClaudeAdapterV2.ts treats every level and every backgrounded task_started as a change, so emitProviderThreadRoster publishes the same roster two or three times, each with a new updatedAt.
Every copy becomes a persisted provider-thread.updated event. Each one appends an event row with its indexes, rewrites the provider thread projection and its indexes, and bumps the thread row: about 30 SQLite pages per event on a real database (#37600 has the breakdown by b-tree).
On a Linux desktop install (0.1.9-nightly.20261111.4501), one Claude thread that ran background shells and subagents for half an hour produced 496 provider-thread.updated events. 108 were byte-identical to the previous event for the same provider thread, written 1–2 ms after it, and 45 more differed only in updatedAt. 104 of these repeats came right after an event that added or removed a task.
Change#
emitProviderThreadRoster compares the event it would send with the last provider_thread.updated the adapter sent for that provider thread, from any emitter. It ignores updatedAt, counts pendingBackgroundWork, and skips the event when nothing else changed. The adapter's internal roster bookkeeping still runs for every message; only the repeated publication is dropped.
emitProviderEvent records the last provider_thread.updated per provider thread after it adds the attachment budget, so the comparison sees the event as sent. The budget step moved into a helper, withAttachmentBudget, for that.
startTurn forgets the record for its provider thread. Provider events reach the server only while a run is subscribed, so an event sent between runs may have been dropped and must not stand in for the next one.
- Every caller of
emitProviderThreadRoster (roster messages, awaitBackgroundTasks, the wake retire path, resetBackgroundTaskStateForNativeThreadProcess) goes through the same check.
- The turn-finalize, goal and attachment-budget emissions are unchanged. A finalize equal to the previous event can still be needed after the server writes the provider thread itself.
Scope#
One part of the write amplification in #37600: the same state is written to the database two or three times per background task change. #36233 shrank the thread.visited payload; this PR removes whole events, and each event is its own commit of about 30 pages whatever its size, so fewer events is what lowers the write rate. The change is limited to the Claude adapter's roster publication; nothing T3 stores or shows changes.
Verification#
What shows the problem on main. The test below, "publishes each background roster change once", fails on main with three extra publications of the same roster and passes on this branch. In the capture's own scenario on main (5dd8cde083), three provider-thread.updated events were byte-identical to the previous event for the same provider thread. Each came 3 ms after it, while the thread was idle and a background command had just ended; this branch wrote none (full run). Look for consecutive provider-thread.updated events of one provider thread with equal payloads. How many a short scenario shows depends on when Haiku wakes the thread; long sessions show more: 20% of 6,562 such events over 48 hours on one desktop install.
- New test in
ClaudeAdapterV2.test.ts, "ClaudeAdapterV2 background wake turns" › "publishes each background roster change once". It feeds a Bash run_in_background launch, task_started, levels [shell], [shell, local_agent], [shell], [], task_notification, then a new task. It asserts the (task ids, pendingBackgroundWork) of every provider_thread.updated:
- on
main it fails with three extra ["bdqirlcyw"], false entries (expected [ [ [], false ], …(6) ] to deeply equal [ [ [], false ], …(3) ]);
- with the change it passes with
[[], false], [[bdqirlcyw], false], [[bdqirlcyw], true], [[b2], false], so the task's end and the new task are still published.
- The 11 recorded Claude background replay scenarios stored 208
provider-thread.updated events with 65 repeats before the change and 191 with 49 repeats after. The remaining repeats come from the finalize and attachment-budget emissions, which this PR leaves alone.
vp run check:changed: format, lint, knip, typecheck and the two touched test files (402 tests) pass, rebased on 1e438c8f80. src/orchestration-v2/Adapters/ and testkit/: 1186/1186 pass.
- Not measured: the write volume on a live install with the change applied.
Model and harness: Claude Opus 5.5, Claude Code in T3 Code
coderabbitai[bot](GitHub profile, opens in a new tab) reviewed
Actionable comments posted: 1
da0fc35a-eda4-45bf-8064-3db8ff6fd058Reviewing files that changed from the base of the PR and between ac0e8c935f9d5a729e8a677ddb8b1c2c55f2bbd4 and ebb0ba4dcca7f4434baf15060bd9aa535980d1a4.
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.tsapps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.