Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): history compaction drops ~90% of provider-turn updates #22548

Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromfix/compact-provider-turn-updatesopened View on GitHub

Server 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 (upsertProviderTurn and 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:

StepTime
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
Migration28 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 indexSize
orchestration_events7.1 GB (1.68M rows)
event indexes1.6 GB
projection_turn_items1.9 GB
projection_nodes0.96 GB

Events by type:

Event typeEventsEntities
turn-item.updated696k336k
node.updated433k242k
provider-turn.updated316k8.6k
provider-thread.updated68k609

Things that do not help much:

  • VACUUM: shrinks the snapshot only from 12.78 to 12.33 GB. dbstat reports 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 for t3 doctor or 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) and reasoning (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. juliusmarminge added labelsmacroscope-review

    2. 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).
    3. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded20.9 KiB20.9 KiB0 B (0.0%)29.3 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB0 B (0.0%)29.3 KiB✅
      ClaudeLive turn messages220 (0.0%)8✅

      Baseline: 5d27ecd · PR result: ee6017c · Source CI: success

      Scenario 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.

    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      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.ts
      • apps/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.ts
      Compaction 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.ts
      The 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: Passed 3 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Description checkWarningThe 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 approval section. 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 Passed checks: 3 passed
      Check nameStatusExplanation
      Title checkPassedThe title is concise, conventional, and accurately describes the main change: reducing provider-turn updates during server history compaction.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck 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 approval section. 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
      Comment `@coderabbitai help` to get the list of available commands.
    5. 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.

    6. 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.

    7. juliusmarminge closed this

Sign in to join this conversation.