Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): stop creating a SQLite temp file for every orchestration command #14403

Closedhaseeb-asad(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/14403/headopened View on GitHub

What Changed

projectEventDeferred no longer opens its own transaction. It runs in the caller's, which is what its contract already says and what the engine, its only production caller, does. projectEvent has no caller transaction, so it now opens one around the deferred call and keeps its rollback behavior.

Why

Every orchestration command created, wrote and deleted a SQLite temp file.

The engine appends an event and projects it inside one command transaction, so the projection's sql.withTransaction was nested, which makes it a savepoint. Effect SQL runs nothing when a nested transaction succeeds, so that savepoint stayed open until the command's COMMIT. SQLite journals every page first written under an open savepoint, and an ordinary command writes enough pages (activity row, thread row, cursors, receipt, and their indexes) to push that journal past the 64 KiB limit where SQLite moves it into a temp file.

On main, lsof on the server process during the benchmark shows a different 65,600-byte etilqs_* file in 8 of 40 samples. With this change it shows none in 40. Per-statement timing puts the command receipt insert, the statement that crosses the limit, at about 170 µs on main and 8 µs here.

A projector failure still fails the whole command: before, it rolled back to the savepoint and then the outer transaction rolled back as well.

Related to the performance audit in #9661. This is not one of its listed items; it sits on the same per-event write path as S10 (#9671).

Measurements

CPU time per thread.activity.append command through the real OrchestrationEngine on an on-disk database. Each run is 500 warmup commands, then 5 rounds of 4,000 commands, taking the median round. Each side ran three times in alternating order, and the table shows the middle run.

Activity payloadmainThis PRChange
300 B813 µs650 µs20% less
1.7 kB895 µs664 µs26% less
8 kB1,052 µs720 µs32% less

Kernel time per command roughly halves (406 µs to 209 µs at 1.7 kB). On a copy of a real 897 MB database the same 1.7 kB workload went from 947 to 1,240 µs down to 655 to 693 µs.

macOS on Apple silicon, Node 24.21.0, SQLite 3.53.4. The machine was not idle, so treat the absolute numbers as rough; the direction held in every pair. The packaged desktop runtime reports the same SQLite build options (TEMP_STORE=1). Linux, Windows and UI latency were not measured.

Verification

  • The new test counts sql.transaction spans for a projection run inside a caller's transaction. It fails on main with 2 and passes here with 1.
  • 65 tests pass in ProjectionPipeline.test.ts and OrchestrationEngine.test.ts, and 295 in server.test.ts and ProviderRuntimeIngestion.test.ts.
  • vp test run src/orchestration src/persistence passes 685 tests. The 37 failures are all in ThreadSettlementReactor.test.ts and fail the same way on unmodified main on my machine.
  • Server tsc --noEmit, targeted lint and format check pass.

Checklist

  • This PR is small and focused
  • I explained what changed and why

No UI changes.

Model: Claude Fable 5.1. Harness: Claude Code in T3 Code.

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

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

      Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      ⚙️ Run configuration

      Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: 4528d498-bfe2-4c04-a172-8424da610d86

      📥 Commits

      Reviewing files that changed from the base of the PR and between 6b0a78156922d5e96f7621a8c41543ec27e15e1f and 232f245fd4457b276404051c936386b795162eac.

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      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: Advanced

      Run ID: d310b6d8-028f-48fd-a4bc-cd739513b948

      📥 Commits

      Reviewing files that changed from the base of the PR and between c18e5ea6ed741443a8ec4a5d22d4b6939b0ecd21 and 6b0a78156922d5e96f7621a8c41543ec27e15e1f.

      📒 Files selected for processing (3)
      • apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
      • apps/server/src/orchestration/Layers/ProjectionPipeline.ts
      • apps/server/src/orchestration/Services/ProjectionPipeline.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      projectEventDeferred no longer opens its own transaction. projectEvent wraps the deferred projection in a transaction. A test checks that deferred projection uses a caller-owned transaction.

      Changes

      Projection transactions

      Layer / File(s)Summary
      Projection transaction boundary
      apps/server/src/orchestration/Services/ProjectionPipeline.ts, apps/server/src/orchestration/Layers/ProjectionPipeline.ts, apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
      The documentation states that projectEventDeferred opens no transaction. The deferred method applies projectors and updates cursors without opening one, while projectEvent wraps that call in a transaction. The test checks that the projected row is visible inside the caller’s transaction and that one transaction span occurs.

      Priority: ➖ Normal

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Suggested reviewers: t3dotgg

      Merge Risk: ⚪ Minimal · up to 6b0a7

      Deferred projection now uses the caller’s transaction while standard projection retains atomicity. No actionable merge-blocking risk is established; merge after normal checks.

      Security Architecture Review

      Security architecture risk: 🔵 Low · up to 6b0a7

      The production command path retains one transaction covering all database changes, and the standalone projection method still provides its own transaction. No introduced security issue was identified in the inspected paths. Deferred projection now depends explicitly on callers honoring its transaction requirement.

      Retained concerns No architecture-level concerns identified.

      Security review details

      Security Blast Radius

      • inferred — The demonstrated exposure is the server command's event store, projected state, cursors, receipt, and returned attachment-cleanup effects. These database writes retain the existing command transaction boundary; broader tenant or environment exposure is not established by the supplied evidence.

      Trust Boundaries and Controls

      • inferred — The inspected change does not give command input a new route around the engine's transaction control. Deferred projection is an internal service operation whose atomicity precondition is satisfied by its production caller; callers outside that boundary must supply their own transaction.
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Title check✅ PassedThe title clearly describes the primary performance change: preventing SQLite temporary-file creation during orchestration commands.
      Description check✅ PassedThe description includes complete What Changed, Why, verification, measurements, scope, and checklist information. It also states that UI changes are not applicable.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      Comment `@coderabbitai help` to get the list of available commands.
    3. juliusmarminge added labelsmacroscope-review

    4. Approvability

      Verdict: Approved at 6b0a781

      Macroscope's review found this PR approvable — This is a focused transaction-boundary performance fix: deferred projections reuse the caller’s existing transaction, while standalone projections retain atomicity through their own transaction. The change is isolated, tested, and introduces no product-default, schema, security, or static-analysis configuration changes.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Thanks for working on this. We merged the orchestrator V2 rewrite in #2829, and we are closing this PR as part of that transition.

      This change touches ProjectionPipeline.ts, ProjectionPipeline.ts, which the V2 merge removed. V2 persists and reads its own projections instead of the V1 read model.

      Sorry for the extra work this creates. If the change is still needed on V2, please rebuild it on current main, verify it there, and open a new PR linking back here. We're closing the current implementation without assuming the underlying request is resolved.

    6. juliusmarminge closed this

Sign in to join this conversation.