perf(server): batch projector cursor writes #9671
main fromrefs/pull/9671/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubEvery orchestration event issued nine separate SQL statements to advance projector cursors. Ordinary tool activity paid that cost even when most projectors had nothing to change.
Batch the nine cursor rows into one upsert after all projectors finish, inside the existing transaction. Keep separate cursors, bootstrap order, and rollback behavior.
Part of the performance audit. This PR only batches cursor statements per event. It does not change thread-row updates or combine events across commands.
Verified:
- 51 tests passed with
vp test run src/orchestration/Layers/ProjectionPipeline.test.ts src/orchestration/Layers/OrchestrationEngine.test.tsfromapps/server. - Tests cover the two-statement project projection, cursor rollback on write failure, replay after failure, and outer command-receipt rollback.
- Targeted lint passed for all four changed TypeScript files.
vp run --filter t3 typecheckpassed.- The audit's in-memory source harness now runs 100 activity commands in 700 SQL statements, including 100 cursor statements. Before this change, the same workload used 1,500 statements, including 900 cursor statements. These counts exclude transaction-control spans. The nine cursor rows still persist together.
No browsers or device tests were run, as requested.
Created with GPT-6 Astra (preview) in Codex.
Note
Batch projector cursor writes into one upsertMany call in ProjectionPipeline#
- Adds
ProjectionStateRepository.upsertManyto the persistence and service layers, generating a singleINSERT ... ON CONFLICTstatement for multiple projection-state rows and treating empty input as a no-op. - Replaces per-projector cursor updates in
projectEventDeferredwith a singleupsertManycall after all projectors run, so every cursor persists in one batch within the same transaction. - Updates integration tests to assert that failed projections or engine dispatches leave all cursors unchanged, and that successful retries advance every cursor.
- Behavioral Change: cursor updates now commit or roll back atomically with the projection transaction; any code expecting individual per-projector cursor writes will see a single batched statement instead.
Note
Medium Risk Changes transactional projection cursor semantics (all-or-nothing per event); incorrect batching or rollback could desync replay/bootstrap, though tests target failure and replay paths.
Overview Cuts per-event SQL overhead by advancing all orchestration projector cursors in one batched upsert instead of nine separate statements.
ProjectionStateRepository gains upsertMany, issuing a single INSERT … ON CONFLICT for multiple projection_state rows (empty input is a no-op). In projectEventDeferred, projectors still run sequentially with projector.apply, but cursor persistence moves to upsertMany after every projector finishes, still inside the existing transaction. Bootstrap keeps advancing cursors per projector via the existing path.
Tests add a two-statement projection check (projection work + batched cursors), and tighten rollback/replay coverage so failed projections or engine dispatches leave all cursors unchanged while successful retries advance them together.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit dbaede16ed9893b1a7b8bebcc9aa6151017f5e10. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).- 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
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 13.5 KiB 13.6 KiB +88 B (+0.6%) 15.1 KiB ✅ Codex Thread snapshot wire 7.0 KiB 7.0 KiB +8 B (+0.1%) 7.3 KiB ✅ Codex Live turn WebSocket wire 6.5 KiB 6.6 KiB +80 B (+1.2%) 7.8 KiB ✅ Codex Live turn WebSocket decoded 57.0 KiB 57.1 KiB +88 B (+0.2%) 66.4 KiB ✅ Codex Live turn messages 8 10 +2 (+25.0%) 21 ✅ Claude Total thread wire 13.5 KiB 13.6 KiB +54 B (+0.4%) 15.1 KiB ✅ Claude Thread snapshot wire 7.0 KiB 7.0 KiB 0 B (0.0%) 7.3 KiB ✅ Claude Live turn WebSocket wire 6.5 KiB 6.6 KiB +54 B (+0.8%) 7.8 KiB ✅ Claude Live turn WebSocket decoded 57.8 KiB 57.9 KiB +88 B (+0.1%) 66.4 KiB ✅ Claude Live turn messages 8 10 +2 (+25.0%) 21 ✅ Baseline:
8e3aa32· PR result:dbaede1· 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: 113.8 KiB
- Claude decoded thread snapshot: 114.5 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
Note
🤖 GPT-6 Astra (preview) responding on behalf of Theo
bugbot run
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Approvability#
Verdict: Approved at
c1b1495Macroscope's review found this PR approvable — This is a narrowly scoped server performance optimization that batches existing projector cursor upserts without changing projection order, persisted data shape, transaction boundaries, or user-facing behavior. Tests cover statement reduction, rollback, replay, and command-level transaction handling.
No code changes detected at
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).dbaede1. Prior analysis still applies.