perf(server): history compaction drops ~90% of provider-turn updates #22548
main fromfix/compact-provider-turn-updates·opened View on GitHubServer updates on a big install are slow, and the database size drives it. On one Linux box, statev2.sqlite is 12.7 GB. Each update makes a full copy of it before the new server starts, and the server is down during the copy.
This PR is one easy win from looking into it. Compaction now also drops superseded provider-turn.updated events. Below that are the findings for the rest.
The change#
Provider turns get an update for every token usage report, about 36 per turn. Compaction already drops superseded updates for nodes and messages, but it kept every provider-turn update. Now it keeps three per turn:
- the first update, because client reducers append an entity where they first see it
- the newest update
- the newest update that carries
tokenUsage, because updates without usage inherit it from earlier ones (upsertProviderTurnand the client reducer both do this)
run.updated and provider-thread.updated stay as they are. CodexUsageHistory reads intermediate run.updated events, and provider-thread.updated moves rows between threads.
Measured with this branch's DatabaseMaintenance.compact on a clone of the real database: 11.56 GiB → 10.27 GiB (−11%). It removed 291,936 events, and provider-turn.updated dropped from 317,681 to 25,761. Projections verified as valid before and after. For comparison, the existing doctor compact had just taken the same database from 11.97 to 11.56 GiB. This also stops the fastest-growing compactable event type from piling up.
Findings: what a 37s update spends its time on#
Timeline from boot-service.log and the server traces, for an update with a 6 ms migration:
| Step | Time |
|---|---|
Download, tar, version check (server still up) | 5.7s |
| Handoff delay, then old server shutdown | ~6s |
Launcher copies the database (cp --reflink=auto on ext4 falls back to a full copy, then fsync) | 29.5s |
| Migration | 28 ms |
| New server listening, then tunnel reachable | +13s |
| Desktop client over T3 Connect sends its first request | +26s after listening |
What is in the 12.7 GB:
| Table or index | Size |
|---|---|
orchestration_events | 7.1 GB (1.68M rows) |
| event indexes | 1.6 GB |
projection_turn_items | 1.9 GB |
projection_nodes | 0.96 GB |
Events by type:
| Event type | Events | Entities |
|---|---|---|
turn-item.updated | 696k | 336k |
node.updated | 433k | 242k |
provider-turn.updated | 316k | 8.6k |
provider-thread.updated | 68k | 609 |
Things that do not help much:
- VACUUM: shrinks the snapshot only from 12.78 to 12.33 GB.
dbstatreports 3.15 GB as unused, but most of it is page slack, not free pages: event rows are 3–4 KB on 4 KB pages. A rebuild with 16 KB pages brings it to 10.51 GB. That is a possible follow-up fort3 doctoror for new databases, but it needs its own performance check. - Turn items: these are real history, about 2 updates per item. Most of the bytes are
command_execution(1.0 GB) andreasoning(0.56 GB).
Follow-ups this PR does not cover:
- Launcher version: the systemd unit runs the launcher from the runtime it was installed with (0.0.46, 17 days old). Launcher fixes never reach that machine. See #19652.
- Backup during downtime: the copy happens while the server is down, and nothing logs it. #18633 adds progress reporting but keeps the cost. Copying while the old server still runs, then copying only the WAL file during downtime, would remove most of the outage on filesystems without reflink.
- Reconnect delay: client backoff doubles to 16–32s within a 30s outage. Only the client that starts the update nudges every 1s, so other devices can wait up to about 30s after the server is back. The server sends no "restarting for update" signal clients could act on. The 26s gap above needs client-side logs to fully explain.
Created with Claude Opus 5.5 in Claude Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge added labelsmacroscope-review
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 20.9 KiB 20.9 KiB 0 B (0.0%) 29.3 KiB ✅ Codex Live turn messages 2 2 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.2 KiB 21.2 KiB 0 B (0.0%) 29.3 KiB ✅ Claude Live turn messages 2 2 0 (0.0%) 8 ✅ Baseline:
5d27ecd· PR result:ee6017c· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Team
- Run ID:
da85fd38-8740-41d8-8961-ba92fd663774
📥 Commits
-
Reviewing files that changed from the base of the PR and between 5d27ecd79c33a642efcdf259e89e3eba5a560247 and ee6017c453ef3a9616a2bf796ee34811e7e5c0eb.
📒 Files selected for processing: 2
-
apps/server/src/orchestration-v2/FoundationPersistence.test.tsapps/server/src/orchestration-v2/ProjectionMaintenance.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
-
📝 Walkthrough
-
Event-store compaction now includes provider-turn updates. It retains the oldest provider-turn event and the newest event that includes token usage. The compaction test covers these retention rules.
Changes: Provider-turn compaction
Layer / File(s) Summary Provider-turn retention rules
apps/server/src/orchestration-v2/ProjectionMaintenance.tsCompaction extracts provider-turn keys and identifies updates with tokenUsage. It retains the newest usage-bearing event along with the oldest event.Retention test coverage
apps/server/src/orchestration-v2/FoundationPersistence.test.tsThe compaction scenario adds provider-turn events with and without token usage. Assertions check the retained event ordering and deletion count. Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Feature
Suggested reviewers:
juliusmarminge
Merge Risk: ⚪ Minimal · up to
ee601-
Provider-turn compaction preserves the inspected replay behavior, and no actionable issue remains before merge.
Pre-merge checks:
3 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Description check The description clearly explains the problem, the provider-turn compaction change, measured results, verification evidence, out-of-scope follow-ups, and the agent used. It does not include the require… Add a ## Scope and approvalsection. Link the triaged issue or discussion with explicit maintainer approval and the approval comment. If no prior approval is required, explain why this is a small, focused fix of an obvious bug and state h… Passed checks: 3 passed-
Check name Status Explanation Title check The title is concise, conventional, and accurately describes the main change: reducing provider-turn updates during server history compaction. Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request.
Full details: Description check
-
Explanation
The description clearly explains the problem, the provider-turn compaction change, measured results, verification evidence, out-of-scope follow-ups, and the agent used. It does not include the required Scope and approval section or provide a triaged issue, maintainer approval, or an explanation for why this focused fix is exempt.
Resolution
Add a
## Scope and approvalsection. Link the triaged issue or discussion with explicit maintainer approval and the approval comment. If no prior approval is required, explain why this is a small, focused fix of an obvious bug and state how its scope remains limited.
-
✨ Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
A rebased port is ready at https://github.com/pingdotgg/t3code/pull/28109, with credit to this implementation. It also keeps command-associated provider-turn snapshots for command-result replay. On an isolated 95-thread subset, provider-turn events fell from 9,704 to 2,357 and all thread projections matched before and after rebuild. Maintainers can choose either PR.
Thanks, Theo, for investigating and implementing this. #29806 (550c5fa34b) now prunes old provider-turn updates along with other projection-covered events, addressing the same database growth. It deliberately retains at least a day of history rather than compacting the recent tail. If reducing that recent tail still matters, please propose it separately against the new retention model.
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — This server change substantially alters automatic history compaction by deleting most previously retained provider-turn updates, while preserving selected state and token usage. Because the effect is production-facing and irreversible, it warrants review beyond the added test coverage.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).