FORGE-88: atomic() adapter for D1-portable writes (Phase 3 step 1) #720

Merged
gmackie merged 1 commit from FORGE-88-atomic-adapter into main 2026-10-12 06:13:21 +00:00
Owner

FORGE-88 step 1 of 7 (FORGE-69 D1 migration, Phase 3): the atomic() adapter every later concurrency rewrite lands on.

Issue: https://tasks.gmac.io (FORGE-88). Design: docs/plans/2026-10-11-forgegraph-d1-migration.html, "Concurrency patterns" + Phase 3.

What

  • @forgegraph/db/atomic exports atomic(db, (b) => [...]). On Postgres it is one transaction that executes exactly the pre-built statements, in order, with no interleaved reads. On any drizzle database with batch (D1) it is db.batch(). Result types are inferred per statement for both dialects.
  • assertInBatch(b, predicate, label) aborts the whole batch. It inserts (0, label) into the new, always-empty batch_assert table only when the predicate is false or NULL; CHECK (ok = 1) rolls the batch back on both PG and D1. atomic() rethrows AtomicAssertionError { code: "atomic_assertion_failed", label }.
  • D1's 100-bound-parameter limit is enforced on both dialects before sending, so Postgres tests catch D1 overflows. chunkByParameters() splits multi-row inserts; inList() passes long IN lists as one JSON parameter (json_each on SQLite, jsonb_array_elements_text with a column-type cast on PG).
  • Works around a drizzle-orm 1.0.0-rc.3 bug: D1 batch() crashes on parameterized raw statements (db.run(sql\…`)`, "reading 'bind'"), which nearly every GUARD rewrite uses.
  • @forgegraph/db/testing/d1-sqlite: a D1 stand-in on node:sqlite driven through the real drizzle-orm/d1 driver. It reproduces batch atomicity, D1's 7500 rejection of BEGIN/SAVEPOINT, the 100-parameter limit, D1 bind rules and always-on FKs. No new dependency. FORGE-89's SQLite lane can reuse it.
  • Migration 0118_batch_assert.sql: additive and idempotent, with the grant to forgegraph_dbcooper. I'll apply it to production right after merge with fg db migrate. That is required so the drift gate stays green.

Not changed: packages/api/src/ci-control/atomic.ts (FORGE-77's local copy) stays as is to avoid a cross-agent conflict; it can switch to this shared adapter in a follow-up.

Tests

  • packages/db/src/atomic.test.ts (13, D1 shim): ordering/results, rollback on failure, guarded transition token, two concurrent transitions → exactly one effect, assertion abort + label, NULL predicate fails, parameter limit, chunked 250-row insert, json_each IN with text and integer columns, refuses started promises, D1 rejects transaction().
  • packages/api/src/__tests__/atomic.database.test.ts (7, Postgres): the same contract on PG, including the PG detail-derived label with two asserts in one batch.
  • packages/api/src/__tests__/atomic.concurrency.test.ts (new, real PG): 8 connections race one guarded transition; exactly one commits, 7 fail with AtomicAssertionError, one event row.
  • Local runs: db unit 115/115, api database lane (atomic) on PGlite and on real PG 17, concurrency lane 10/10 (including the existing 4 files + control-plane), tsc for db and api clean.

Remaining FORGE-88 steps

2 outbox/readiness claims + resource_locks lease; 3 BATCH/READ sites; 4 GUARD sites + bug fixes; 5 RepositoryCoordinator DO; 6 deploy route guards; 7 lint at error.

🤖 Generated with Claude Code

FORGE-88 step 1 of 7 (FORGE-69 D1 migration, Phase 3): the `atomic()` adapter every later concurrency rewrite lands on. Issue: https://tasks.gmac.io (FORGE-88). Design: docs/plans/2026-10-11-forgegraph-d1-migration.html, "Concurrency patterns" + Phase 3. ## What - `@forgegraph/db/atomic` exports `atomic(db, (b) => [...])`. On Postgres it is one transaction that executes exactly the pre-built statements, in order, with no interleaved reads. On any drizzle database with `batch` (D1) it is `db.batch()`. Result types are inferred per statement for both dialects. - `assertInBatch(b, predicate, label)` aborts the whole batch. It inserts `(0, label)` into the new, always-empty `batch_assert` table only when the predicate is false or NULL; `CHECK (ok = 1)` rolls the batch back on both PG and D1. atomic() rethrows `AtomicAssertionError { code: "atomic_assertion_failed", label }`. - D1's 100-bound-parameter limit is enforced on both dialects before sending, so Postgres tests catch D1 overflows. `chunkByParameters()` splits multi-row inserts; `inList()` passes long IN lists as one JSON parameter (`json_each` on SQLite, `jsonb_array_elements_text` with a column-type cast on PG). - Works around a drizzle-orm 1.0.0-rc.3 bug: D1 `batch()` crashes on parameterized raw statements (`db.run(sql\`…\`)`, "reading 'bind'"), which nearly every GUARD rewrite uses. - `@forgegraph/db/testing/d1-sqlite`: a D1 stand-in on `node:sqlite` driven through the real `drizzle-orm/d1` driver. It reproduces batch atomicity, D1's 7500 rejection of BEGIN/SAVEPOINT, the 100-parameter limit, D1 bind rules and always-on FKs. No new dependency. FORGE-89's SQLite lane can reuse it. - Migration `0118_batch_assert.sql`: additive and idempotent, with the grant to forgegraph_dbcooper. **I'll apply it to production right after merge** with `fg db migrate`. That is required so the drift gate stays green. Not changed: `packages/api/src/ci-control/atomic.ts` (FORGE-77's local copy) stays as is to avoid a cross-agent conflict; it can switch to this shared adapter in a follow-up. ## Tests - `packages/db/src/atomic.test.ts` (13, D1 shim): ordering/results, rollback on failure, guarded transition token, two concurrent transitions → exactly one effect, assertion abort + label, NULL predicate fails, parameter limit, chunked 250-row insert, json_each IN with text and integer columns, refuses started promises, D1 rejects `transaction()`. - `packages/api/src/__tests__/atomic.database.test.ts` (7, Postgres): the same contract on PG, including the PG detail-derived label with two asserts in one batch. - `packages/api/src/__tests__/atomic.concurrency.test.ts` (new, real PG): 8 connections race one guarded transition; exactly one commits, 7 fail with `AtomicAssertionError`, one event row. - Local runs: db unit 115/115, api database lane (atomic) on PGlite and on real PG 17, concurrency lane 10/10 (including the existing 4 files + control-plane), `tsc` for db and api clean. ## Remaining FORGE-88 steps 2 outbox/readiness claims + resource_locks lease; 3 BATCH/READ sites; 4 GUARD sites + bug fixes; 5 RepositoryCoordinator DO; 6 deploy route guards; 7 lint at error. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-88: add the atomic() adapter for D1-portable writes
Some checks failed
CI / gitleaks (pull_request) Successful in 14s
CI / go (pull_request) Successful in 1m5s
CI / storybook (pull_request) Successful in 2m34s
CI / unit-api (pull_request) Successful in 4m23s
CI / database (pull_request) Successful in 2m47s
CI / web-build (pull_request) Successful in 3m48s
CI / checks (pull_request) Successful in 4m12s
CI / unit-web (pull_request) Failing after 15m40s
CI / ci (pull_request) Has been cancelled
57955a6ab7
packages/db/src/atomic.ts is the FORGE-69 Phase 3 adapter. atomic(db, (b) => [...])
runs pre-built statements all-or-nothing: one Postgres transaction with nothing
interleaved, or db.batch() on any drizzle database with batch (D1).

- assertInBatch(b, predicate, label) aborts the whole batch through a new,
  always-empty batch_assert table (CHECK ok = 1) and surfaces as
  AtomicAssertionError with the label. NULL predicates fail closed.
- Every statement is checked against D1's 100-bound-parameter limit on both
  dialects; chunkByParameters() splits multi-row inserts and inList() passes
  long IN lists as one JSON parameter (json_each / jsonb_array_elements_text).
- Works around drizzle-orm 1.0.0-rc.3's D1 batch crash on parameterized raw
  statements (db.run(sql`...`)), which every GUARD rewrite needs.
- packages/db/src/testing/d1-sqlite.ts: a D1 stand-in over node:sqlite for the
  real drizzle-orm/d1 driver (batch atomicity, 7500 on BEGIN, 100-param limit,
  D1 bind rules, FKs on), shared with the FORGE-89 SQLite lane.
- Migration 0118_batch_assert.sql (additive, idempotent).

Tests: the same contract on D1 (packages/db, 13) and Postgres (api database
lane, 7), plus a concurrency test where 8 connections race one guarded
transition and exactly one applies.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie force-pushed FORGE-88-atomic-adapter from 57955a6ab7
Some checks failed
CI / gitleaks (pull_request) Successful in 14s
CI / go (pull_request) Successful in 1m5s
CI / storybook (pull_request) Successful in 2m34s
CI / unit-api (pull_request) Successful in 4m23s
CI / database (pull_request) Successful in 2m47s
CI / web-build (pull_request) Successful in 3m48s
CI / checks (pull_request) Successful in 4m12s
CI / unit-web (pull_request) Failing after 15m40s
CI / ci (pull_request) Has been cancelled
to 91b301d2d1
All checks were successful
CI / gitleaks (pull_request) Successful in 18s
CF executor smoke / smoke (pull_request) Successful in 22s
CI / unit-web (pull_request) Successful in 3m1s
CI / go (pull_request) Successful in 1m12s
CI / storybook (pull_request) Successful in 2m34s
CI / web-build (pull_request) Successful in 2m28s
CI / unit-api (pull_request) Successful in 4m21s
CI / checks (pull_request) Successful in 2m32s
CI / database (pull_request) Successful in 2m50s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 57s
2026-10-12 06:06:07 +00:00
Compare
Sign in to join this conversation.
No description provided.