fix(server): compact superseded turn-item snapshots #20990
main fromrefs/pull/20990/head·merged by Yash-Singh1(GitHub profile, opens in a new tab) View on GitHubProblem#
Repeated full turn-item snapshots survive every background compaction pass. A synthetic stream of nine command, reasoning, and todo items with 200 updates each retains all 1,800 rows and 25.29 MB of payload, even though only nine items remain current.
Change#
Apply the existing first/latest retention rule to non-subagent turn items. The first snapshot preserves replay ordering and the latest keeps the complete output. Keep command-associated snapshots for command-result replay, and keep all subagent snapshots because later events can reference their prompt anchors.
Scope and approval#
Submitted under the focused obvious-bug exception. This extends the compactor shipped in #19435 to a skipped class of full snapshots, using its existing entity keys, scan bounds, and deletion algorithm. Related #19451 proposed a larger retirement mechanism; this change adds no migration or write-time deletion.
Incoming writes, streaming cadence, the six-hour maintenance schedule, and compactor pacing are unchanged. #20877 separately addresses discovery blocking and pacing. This patch bounds retained eligible snapshots after a pass; it does not promise lower incoming device writes or immediate file shrinkage.
Verification#
Runnable synthetic reproduction and raw results. A fresh synthetic SQLite database runs through the real event sink, event store, projections, and compactor. Baseline is 13aa882c22d584de5388dfce3b3ca413387ba059.
| After one compaction pass | Base | Fix |
|---|---|---|
| Turn-item event rows | 1,800 | 18 |
| Turn-item payload bytes | 25,289,625 | 253,119 |
| Reusable free-page bytes | 24,576 | 26,808,320 |
| Allocated database bytes | 28,151,808 | 28,151,808 |
Latest full outputs and rebuilt projections match. The global high-water stays at 1,801 and a second pass deletes nothing. Both runs use 37 discovery queries, each limited to 50 rows. On this small, warm fixture, the fixed pass took 68 ms, with a maximum discovery query of 11.72 ms and delete query of 1.15 ms. These are local measurements, not a latency bound for large databases. The existing cooperative yields provide no timed duty-cycle cap.
Focused verification, with one test worker:
vp test run apps/server/src/orchestration-v2/FoundationPersistence.test.ts apps/server/src/persistence/OrchestrationEventStore.test.ts apps/server/src/persistence/OrchestrationEventStore.sequence.test.ts --maxWorkers=1
vp test run apps/server/src/orchestration-v2/TurnItemCompaction.test.ts --maxWorkers=1
The two runs passed 61 and 2 tests. The added middle-snapshot regression failed on the base. Coverage includes complete command/reasoning/todo payloads, distinct items, replay from a deleted cursor through the actual partial-timeline reducer, rebuilt positions, command-result replay, changed subagent prompt anchors, and live appends during a pass. All three post-bound snapshots survive the active pass and compact on the next one.
Server tsc-rs --noEmit passes, as do scoped formatting and lint. Only synthetic data was used; no live-installation improvement is claimed.
Independent review found no material issues in the source or regression coverage. The final review accepted candidate bcf0d667f8.
Independent review: GPT 6.1 Sol xhigh (Opus unavailable: rate-limited at 2026-10-17 15:47:05 UTC)
Implemented with GPT 6 Astra and GPT 6 Luna through the Codex harness in T3 Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
Approvability#
Verdict: Approved at
bcf0d66Macroscope's review found this PR approvable — This is a narrowly scoped extension of the existing event-compaction maintenance path, with no schema, deployment, default, or static-analysis changes. Extensive tests verify replay fidelity, command lookup, prompt preservation, projection rebuilding, and concurrent writes while intermediate turn-item snapshots are removed.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.- Last reviewed commit: 4c4fb2b9be0515e3fd4a5d5feddf9bc9cb5ee051
- Latest commit, not reviewed: f6782a02c552fbe5446958396149f1bc2fc1e94f
Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.
Use the checkboxes below for quick actions:
- ▶️ Resume reviews
- 🔍 Trigger review
No actionable comments were generated in the recent review.
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
280e6a4b-1ba8-49a8-9960-314ed0b4d273
📥 Commits
-
Reviewing files that changed from the base of the PR and between 1f52463105ecaabf2fac3f19d23fa7f446e4f943 and 4c4fb2b9be0515e3fd4a5d5feddf9bc9cb5ee051.
📒 Files selected for processing: 2
-
apps/server/src/orchestration-v2/FoundationPersistence.test.tsapps/server/src/orchestration-v2/ProjectionMaintenance.ts
🚧 Files skipped from review as they are similar to previous changes: 1
-
- 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; 7 remain after this review.
-
Walkthrough
-
Event-store compaction now removes intermediate uncommanded turn-item updates by entity key. It retains the oldest and newest updates, commanded updates, and subagent updates. Tests cover ordinary compaction and appends during a compaction scan.
Changes: Turn-item compaction
Priority:Layer / File(s) Summary Select and compact eligible turn-item events
apps/server/src/orchestration-v2/ProjectionMaintenance.tsThe event query selects uncommanded turn-item updates without a subagent payload for keyed compaction. The compactor retains the oldest event and marks intermediate events obsolete. Validate replay, retention, and concurrent appends
apps/server/src/orchestration-v2/TurnItemCompaction.test.ts,apps/server/src/orchestration-v2/FoundationPersistence.test.tsTests cover retained events, replay and projection behavior, maintenance verification, item positions, and appends during compaction. The persistence fixture includes an intermediate streaming update. Pending latest changesEstimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers:
juliusmarminge
Merge Risk:
Minimal · up to4c4fb-
No concrete merge-blocking risk remains from this change.
Architecture risk:
Low · up to4c4fb-
The change affects 1 system.
Changed systems:
apps/serverArchitecture concerns No architecture-level concerns identified.
Review details
-
Systems and components
- observed — apps/server (service) was modified; 3 changed files map to changed impact.
Before / after behavior
- observed — Modified behavior in apps/server/src/orchestration-v2/TurnItemCompaction.test.ts: Adds test setup and event fixtures: an in-memory SQLite-backed layer, thread and item event builders, and subagent events with revision-dependent prompts and progress. Item revision 69 is completed; earlier revisions are running.
- observed — Modified behavior in apps/server/src/orchestration-v2/TurnItemCompaction.test.ts: Adds an ordinary-compaction test that writes 70 revisions per item, varies item ordering, and associates a middle snapshot with a command. It checks retained replay IDs and deleted-row counts, reduced payload bytes with an unchanged high-water sequence, command lookup, subagent prompt compaction, cursor replay, client projection equivalence, valid verification and rebuild, and preserved item positions.
- observed — Modified behavior in apps/server/src/orchestration-v2/TurnItemCompaction.test.ts: Adds a race test that blocks compaction during its first descending event scan, appends three snapshots, and then releases the scan. It checks that the first pass deletes one event while retaining and streaming all three appends, and that a second compaction leaves only the first and latest item snapshots.
- observed — Modified behavior in apps/server/src/orchestration-v2/FoundationPersistence.test.ts: The compaction fixture now adds an intermediate streaming turn-item update between the first streaming item and the larger history sequence.
-
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly and concisely describes the primary change: compacting superseded turn-item snapshots on the server. Description check The description covers the problem, implementation, scope rationale, verification steps, observed results, limitations, and agent usage. It explains the focused obvious-bug exception and provides deta… 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 covers the problem, implementation, scope rationale, verification steps, observed results, limitations, and agent usage. It explains the focused obvious-bug exception and provides detailed test evidence.
-
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Yash-Singh1(GitHub profile, opens in a new tab) approved these changes
Yash-Singh1(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes