fix(server): V2 event writes no longer fail when another process commits mid-transaction #14365
main fromrefs/pull/14365/head·opened View on GitHubWhat Changed#
V2 EventSink write transactions now take the SQLite write lock before their first read (withWriteTransaction, in apps/server/src/orchestration-v2/writeTransaction.ts). All seven sql.withTransaction sites in EventSink.ts use it. Read-only transactions elsewhere are untouched.
Why#
Users see a red "Provider turn failed." on a run while the provider keeps working and later finishes normally. The run is marked failed (class unknown), and the real provider turn is left running.
EventSink writes read first (ownership check, pending-input guard, next stream_version) and write second, under a deferred BEGIN. When another connection holds or has just taken the write lock, upgrading that read transaction to a write fails immediately with "database is locked": SQLITE_BUSY (SQLite skips the busy handler here to avoid deadlock) or SQLITE_BUSY_SNAPSHOT (the snapshot went stale). busy_timeout is never consulted on this path. The SqlError ends the run's ingestion stream, and the catchCause in RunExecutionService records the generic failure.
Captured from a live failure (server trace, desktop server on the shared statev2.sqlite):
sql.execute INSERT INTO orchestration_events ... durationMs: 0.258
at orchestrationV2.EventSink.writeIfRunCurrent
at orchestrationV2.providerTurnStart.start
[cause]: SqlError/UnknownError: Failed to execute statement
[cause]: Error: database is locked
The competing writer was a second T3 server on the same database (boot-service t3 serve plus the desktop app's embedded server), which was writing thread.pull-request-synced events continuously (~1,500/hour) at the time.
I scoped this to the write path rather than switching the SQLite client to BEGIN IMMEDIATE globally. An independent review pointed out that a global switch would make read-only snapshot queries (getSnapshot, getShellSnapshot, ...) contend for the writer lock and block the event loop up to busy_timeout.
This does not stop two servers from sharing one home. It makes the event write path tolerate it: writers now wait for the lock (up to busy_timeout) instead of failing.
Verification#
Refreshed onto current target 8297ccf. The three-file fix is byte-identical to the original contribution. Current candidate: f9c1480.
- Focused
writeTransaction,RunExecutionService, replay fixtures and replay recovery suites: 147 tests passed across four files, exit 0, on the refreshed base. - Scoped lint and server
tsc --noEmit: exit 0. Commit formatting hook passed without changing the reviewed tree. - Fresh independent Claude Fable 5.1 review: no actionable findings. Its separate two-process SQLite probe reproduced immediate deferred-transaction failures (codes 5/517) and confirmed that taking the write lock first waits for the competing writer and commits.
- The original author's 9,000-transaction stress run was not repeated in this audit. The fresh review probe used Node 24.12.0 / SQLite 3.50.4.
- No UI change. Current-head CI is tracked separately from these local results.
Original implementation: Claude Code. Readiness audit and base refresh: GPT-6.1 Sol / GPT-6 Astra through Codex in T3 Code. Independent review: Claude Fable 5.1 through Claude Code in T3 Code.
- 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
Approvability#
Verdict: Approved at
e916ab0Macroscope's review found this PR approvable — This is a narrowly scoped SQLite concurrency bug fix that changes lock acquisition timing for existing V2 write transactions while leaving read-only paths and stored data unchanged. The added focused tests cover the competing-process behavior, with no product-default, schema, security, or deployment changes.
Notes:
- Diff unchanged. Approvability was decided on eligibility alone.
juliusmarminge added labelsmacroscope-review
juliusmarminge closed this
juliusmarminge added labelstriage:keep-open
juliusmarminge reopened this
Macroscope skipped reviewing this pull request. Per-review cost limit exceeded (workspace setting).
This review would cost an estimated $16.03, which exceeds your per-review limit of $15.00.
The top 3 files driving up this estimate:
File Diff Size Estimate packages/contracts/src/orchestrationV2.ts127.11KB $2.19 apps/server/src/mcp/OrchestratorMcpService.ts75.70KB $1.30 apps/server/src/orchestration-v2/EventSink.ts34.08KB $0.59 [Turn off this reminder going forward](https://app.macroscope.com/pingdotgg/settings#Personal)Tip
To get this pull request reviewed, you can:
- Comment
@macroscope-appon this PR to request a manual review (monthly spend limits still apply). - Exclude the file(s) above from review by adding a pattern to your
.macroscope/ignore.md— note that creating this file replaces Macroscope's built-in default ignores rather than extending them. - Raise your cost limit in your workspace billing settings.
- Comment
Warning
Review limit reached#
Only developers with an assigned seat can use this organization's usage-based review budget, and seats here are assigned manually. Ask an admin to assign a seat, or change the review continuation mode in Billing.
Next included review available in 45 minutes.
View limit details
Limit details: You’ve used all 10 included reviews currently available.
Review configuration:
⚙️ Run configuration
Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
2adbec60-a526-4511-9980-4bf97ab00a9d📥 Commits
Reviewing files that changed from the base of the PR and between cc1e634bfa62edd56ff792eea666e436fdef788f and e916ab0b1aab3bdffe4f9a2aba937e520fc6a2ca.
📒 Files selected for processing (3)
apps/server/src/orchestration-v2/EventSink.tsapps/server/src/orchestration-v2/writeTransaction.test.tsapps/server/src/orchestration-v2/writeTransaction.ts
- Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Autopilot is currently an internal CodeRabbit preview.
Comment `@coderabbitai help` to get the list of available commands.macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Note
Grok responding on behalf of Julius.
Superseded by #15488, which landed on
mainand fixes the same deferred-BEGIN/SQLITE_BUSY_SNAPSHOTfailure atpackages/shared/src/nodeSqliteClient.tsby starting writable transactions withBEGIN IMMEDIATE(plus correcterrcode→errnoclassification).That covers the EventSink write path this PR scoped with
withWriteTransaction, so this branch is no longer needed. Thanks for the investigation and the focused repro — the client-level fix is the direction we took.juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed