Commit b462755
OpenCode Agent
fix(concurrency): centralize write transactions with bounded SQLITE_BUSY retry
Replaces 9 hand-rolled BEGIN IMMEDIATE / COMMIT / finally-ROLLBACK blocks
with a single shared runWriteTransaction helper that adds a bounded
SQLITE_BUSY retry loop (4 attempts, 100ms backoff). This is a better
concurrency solution than the previous per-site pattern because:
1. Centralized retry policy: transient SQLITE_BUSY from cross-process WAL
writer lock contention (sibling migration, dreamer run, Channel-2 bulk
delivery) now waits-and-retries instead of propagating up and disabling
Magic Context for the run. Previously each site either swallowed,
propagated, or retried inconsistently.
2. Composition-safe: if called inside an existing db.transaction() or
another runWriteTransaction, the body runs inline WITHOUT issuing a
nested BEGIN (SQLite doesn't allow nested BEGIN; the outer transaction
already holds the writer lock). Detected via the bun:sqlite
inTransaction flag or the node:sqlite shim's isTransaction flag.
3. One correct transaction plumbing pattern instead of 9 slightly-different
copies. Several sites were missing the ROLLBACK path or had subtle
ordering bugs at the edges.
Sites converted (all sync write paths with hand-rolled BEGIN IMMEDIATE):
- dreamer/lease.ts (acquireLease, renewLease, releaseLease)
- git-commits/sweep-coordinator.ts (acquireGitSweepLease, renewGitSweepLease,
markGitSweepSuccessAndRelease)
- message-index.ts (indexMessagesAfterOrdinal — cross-process FTS dedup)
- workspaces.ts (bumpEpochsForWorkspaceMembers, bumpEpochsForWorkspaceMemberSet)
- compartment-storage.ts (replaceAllCompartmentStateAndBumpDepth,
promoteRecompStaging)
- key-files/project-key-files.ts (replaceAllKeyFiles)
- key-files/identify-key-files.ts (commitKeyFilesUnderLease)
- hooks/compartment-runner-recomp.ts (promoteRecompStagingWithM0Mutation)
- hooks/compartment-runner-incremental.ts (historian publish path)
inject-compartments.ts is intentionally NOT converted: its two BEGIN IMMEDIATE
blocks have complex early-return-with-rollback-and-fallback-read patterns
(Phase 3 materialization contention retry, soft-refresh cache replay) that
don't map cleanly onto the shared helper without significant restructuring.
The helper is still a net win across the 9 simpler sites.
Verification:
- bun run typecheck (tsc --noEmit + scripts) passes
- bun run lint (biome check) passes
- 12 new unit tests for runWriteTransaction/runWriteTransactionAsync pass
(BEGIN/COMMIT, rollback on throw, transient BUSY retry, max-attempts
give-up, nested-transaction composition, non-transient error passthrough)
- All existing tests in the converted files' test suites pass:
dreamer/lease.test.ts (7), git-commits (19), compartment-storage-atomic
(8), compartment-storage-v6, compartment-lease, compartment-runner-recomp-fk
(4), compartment-runner-partial-recomp, key-files (14), workspaces,
storage-db (11), message-index
Net: -199 lines / +40 lines across the 9 converted sites, +201 lines for
the shared helper + 156 lines for its tests.1 parent db3213e commit b462755
11 files changed
Lines changed: 392 additions & 199 deletions
File tree
- packages/plugin/src
- features/magic-context
- dreamer
- git-commits
- key-files
- hooks/magic-context
Lines changed: 5 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
379 | 380 | | |
380 | 381 | | |
381 | 382 | | |
382 | | - | |
383 | | - | |
384 | | - | |
| 383 | + | |
385 | 384 | | |
386 | | - | |
387 | | - | |
388 | 385 | | |
389 | 386 | | |
390 | 387 | | |
| |||
402 | 399 | | |
403 | 400 | | |
404 | 401 | | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | 402 | | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
| 403 | + | |
418 | 404 | | |
419 | 405 | | |
420 | 406 | | |
| |||
587 | 573 | | |
588 | 574 | | |
589 | 575 | | |
590 | | - | |
591 | | - | |
592 | | - | |
| 576 | + | |
593 | 577 | | |
594 | | - | |
595 | | - | |
596 | 578 | | |
597 | 579 | | |
598 | 580 | | |
599 | 581 | | |
600 | 582 | | |
601 | | - | |
602 | | - | |
603 | 583 | | |
604 | 584 | | |
605 | 585 | | |
| |||
615 | 595 | | |
616 | 596 | | |
617 | 597 | | |
618 | | - | |
619 | | - | |
620 | 598 | | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
| 599 | + | |
630 | 600 | | |
631 | 601 | | |
632 | 602 | | |
| |||
Lines changed: 4 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | 47 | | |
65 | 48 | | |
66 | | - | |
| 49 | + | |
67 | 50 | | |
68 | 51 | | |
69 | 52 | | |
| |||
80 | 63 | | |
81 | 64 | | |
82 | 65 | | |
83 | | - | |
| 66 | + | |
84 | 67 | | |
85 | 68 | | |
86 | 69 | | |
| |||
93 | 76 | | |
94 | 77 | | |
95 | 78 | | |
96 | | - | |
| 79 | + | |
97 | 80 | | |
98 | 81 | | |
99 | 82 | | |
| |||
Lines changed: 5 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 63 | | |
82 | 64 | | |
83 | 65 | | |
| |||
110 | 92 | | |
111 | 93 | | |
112 | 94 | | |
113 | | - | |
| 95 | + | |
114 | 96 | | |
115 | 97 | | |
116 | 98 | | |
| |||
173 | 155 | | |
174 | 156 | | |
175 | 157 | | |
176 | | - | |
| 158 | + | |
177 | 159 | | |
178 | 160 | | |
179 | 161 | | |
| |||
194 | 176 | | |
195 | 177 | | |
196 | 178 | | |
197 | | - | |
| 179 | + | |
198 | 180 | | |
199 | 181 | | |
200 | 182 | | |
| |||
212 | 194 | | |
213 | 195 | | |
214 | 196 | | |
215 | | - | |
| 197 | + | |
216 | 198 | | |
217 | 199 | | |
218 | 200 | | |
| |||
Lines changed: 3 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
423 | 424 | | |
424 | 425 | | |
425 | 426 | | |
426 | | - | |
427 | | - | |
428 | | - | |
| 427 | + | |
429 | 428 | | |
430 | 429 | | |
431 | 430 | | |
| |||
440 | 439 | | |
441 | 440 | | |
442 | 441 | | |
443 | | - | |
444 | | - | |
445 | 442 | | |
446 | 443 | | |
447 | 444 | | |
448 | 445 | | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
| 446 | + | |
458 | 447 | | |
459 | 448 | | |
460 | 449 | | |
| |||
Lines changed: 4 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
143 | | - | |
144 | | - | |
145 | | - | |
| 144 | + | |
146 | 145 | | |
147 | 146 | | |
148 | 147 | | |
| |||
152 | 151 | | |
153 | 152 | | |
154 | 153 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 154 | + | |
| 155 | + | |
168 | 156 | | |
169 | 157 | | |
170 | 158 | | |
| |||
Lines changed: 4 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
218 | 219 | | |
219 | 220 | | |
220 | 221 | | |
221 | | - | |
222 | | - | |
223 | | - | |
| 222 | + | |
224 | 223 | | |
225 | 224 | | |
226 | 225 | | |
| |||
245 | 244 | | |
246 | 245 | | |
247 | 246 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 247 | + | |
| 248 | + | |
260 | 249 | | |
261 | 250 | | |
262 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
271 | 272 | | |
272 | 273 | | |
273 | 274 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | 275 | | |
280 | 276 | | |
281 | 277 | | |
| |||
313 | 309 | | |
314 | 310 | | |
315 | 311 | | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
| 312 | + | |
332 | 313 | | |
333 | 314 | | |
334 | 315 | | |
| |||
337 | 318 | | |
338 | 319 | | |
339 | 320 | | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
| 321 | + | |
356 | 322 | | |
0 commit comments