perf(server): per-thread settlement and PR checks no longer rebuild the whole thread list #13691
main fromrefs/pull/13691/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubOn a long-running server with about 5,000 threads and 130 projects, the settlement and PR discovery reactors read the full shell snapshot for every per-thread event. One full read loads every active thread, runs 6 queries, and resolves repository identity for every project. One turn end queued about 3 of these full reads, and the drain worker does not merge duplicates. This came from a long-uptime slowdown report (0.0.40, 9 days of uptime).
Fix#
- New helper
readSweepSnapshot(snapshots, threadId | null)inThreadPullRequestReactor.ts.- With
null, it callsgetShellSnapshot()as before. The 1-minute sweeps, merge sweeps, and settings sweeps do not change. - With a thread id, it reads
getSnapshotSequence, thengetThreadShellById, thengetProjectShellsfor the thread's project and the project its saved PR names. Settlement checks that second project. - The sequence is read first, so the thread is at least as new as the sequence that guards
thread.auto-settleandthread.pull-request.sync. A race can only cause a rejected command. It cannot hide a change.
- With
ThreadSettlementReactorandThreadPullRequestReactoruse this helper for all sweeps.- PR discovery now prunes pending backfills only for the thread it read. A one-thread read cannot show which other threads are gone.
- The per-thread
settleThreaddecision no longer opens a span. A full sweep made one span per candidate thread. PullRequestSyncReactordoes not change here. #13704 already moved it to a query that reads only threads with linked PRs.
Related: #12161 added the per-thread settlement triggers.
Verification#
- A real SQLite test in
ProjectionSnapshotQuery.test.tsseeds a thread with a linked PR, a latest turn, and a session, plus a thread whose branch PR names another project. It asserts thatreadSweepSnapshotfor each thread returns the same sequence, thread, and project shells asgetShellSnapshot. It fails if the helper drops the PR's project or ifgetThreadShellByIddrifts from the full snapshot. - The reactor tests record each shell read as a thread id or as a full read. They send per-thread events (session-set ready, PR sync, turn-diff-completed) and assert a one-thread read. If the reactors go back to the full read, they fail with an assertion, not a timeout.
- A new check sends an event for another thread during a pending backfill retry. It fails if a one-thread read prunes other pending backfills.
vp test runonLayers/ProjectionSnapshotQuery.test.ts,ThreadPullRequestReactor.test.ts,PullRequestSyncReactor.test.ts, andLayers/OrchestrationReactor.test.ts: 64 passed.vp test run ThreadSettlementReactor.test.ts -t ThreadSettlementReactor: 20 passed. The "storage cleanup" tests in this file also fail on unchanged main on this machine. They pass in CI.vp lintandvp fmton the changed files, andvp run --filter t3 typecheck.
Made by Claude Opus 5.5 (1M context) in Claude Code, running in T3 Code.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Improvements
- Thread-specific pull request and settlement updates now use data scoped to the affected thread and its related projects, while full sweeps continue to use the complete snapshot.
- Updates check the affected thread when handling pending backfills, and missing threads are looked up before a retry.
- These changes preserve full-sweep behavior while reducing unnecessary full-snapshot reads for thread-specific updates.
- 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
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
Review profile: CHILL
Plan: Team
Run ID:
714f841e-1b44-49d9-95bd-530c1e3a76ff📥 Commits
Reviewing files that changed from the base of the PR and between 0fcc5b0c3ac435e06f4debadda6a5278d5690118 and bebf1efa5dfee3e697ceef27cf1c3fb8693e2865.
📒 Files selected for processing (1)
apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.
📝 Walkthrough
Walkthrough#
Single-thread sweeps in the pull-request and settlement reactors now read thread-scoped snapshots. Full sweeps continue to read full snapshots. The tests track thread-specific reads and verify requests for the expected thread, including a missing-thread backfill case.
Changes#
Thread-scoped reactor snapshots
Layer / File(s) Summary Read and use thread-scoped snapshots
apps/server/src/orchestration/ThreadPullRequestReactor.ts,apps/server/src/orchestration/ThreadSettlementReactor.ts,apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.tsA shared helper reads the requested thread and related projects for single-thread sweeps. The reactors use that snapshot. Pull-request backfill pruning checks only the requested thread during a single-thread sweep. settleThreadnow usesEffect.fnUntraced. The parity test checks targeted snapshots against shell snapshots.Verify thread-specific reads
apps/server/src/orchestration/ThreadPullRequestReactor.test.ts,apps/server/src/orchestration/ThreadSettlementReactor.test.tsThe test harnesses track full and thread-specific reads and provide thread and project snapshot queries. Tests verify reads for the expected thread, including a missing-thread backfill case. Priority: ⬇️ Low
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)#
sequenceDiagram participant Reactor as ThreadPullRequestReactor participant SnapshotReader as readSweepSnapshot participant Query as ProjectionSnapshotQuery Reactor->>SnapshotReader: Request snapshot for threadId SnapshotReader->>Query: Read snapshot sequence SnapshotReader->>Query: Look up thread by ID SnapshotReader->>Query: Load related project shells SnapshotReader-->>Reactor: Return targeted snapshotSuggested reviewers:
bil0000Merge Risk: ⚪ Minimal · up to
bebf1No actionable merge-blocking risk is identified; merge after normal checks.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold. ✅ Passed checks (4 passed)
Check name Status Explanation Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request. Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request. Title check ✅ Passed The title clearly and concisely describes the main performance change: per-thread settlement and pull-request checks no longer rebuild the full thread list. Description check ✅ Passed The description explains what changed, why it changed, implementation details, verification coverage, and test results. It does not use the template headings exactly and omits the checklist, but the r… - Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
- Commit to this branch
- Create a new PR
Comment `@coderabbitai help` to get the list of available commands.Approvability#
Verdict: Approved at
bebf1efMacroscope's review found this PR approvable — This is a contained server performance refactor that preserves full-sweep behavior and narrows only already thread-scoped reads, with parity and reactor tests covering the changed paths. It introduces no schema, deployment, security, billing, authentication, product-default, or static-analysis-suppression changes.
Notes:
- All code in this push has already been reviewed. Approvability was decided on eligibility alone.
- @@ -69,3 +72,9 @@6972 );7073 }717475+/**76+ * Read the shell state for a discovery or settlement sweep. A sweep for one77+ * thread reads that thread and the projects it names, not every thread.78+ */79+export const readSweepSnapshot = (80+ snapshots: ProjectionSnapshotQuery.ProjectionSnapshotQueryShape,
This production Effect helper accepts
Posted via Macroscope — Effect Service ConventionsProjectionSnapshotQueryShapeas an explicit service dependency. Could it acquireProjectionSnapshotQuery.ProjectionSnapshotQuerywithyield*instead, and have both reactors and the parity test provide the query through their layers? That keeps service dependencies in the Effect environment rather than passing service instances between production modules.Note
🤖 Claude Opus 5.5 responding on behalf of Theo
Keeping the argument.
readSweepSnapshotis a read helper, not a service factory: both reactors already acquireProjectionSnapshotQuerywithyield*inmake(ThreadPullRequestReactor.ts:107, ThreadSettlementReactor.ts:83) and pass that instance down. Reading it from the environment inside the helper would leak the requirement intoThreadSettlementReactor.start, which callsrunSweepdirectly for merge events (ThreadSettlementReactor.ts:378), so it would need an extraEffect.provideServiceto keep itsEffect<void, never, Scope>type. macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
6530de0· PR result:bebf1ef· 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.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.