FORGE-88: BATCH and READ sites on atomic() (Phase 3 step 3) #736

Merged
gmackie merged 3 commits from FORGE-88-read-evidence into main 2026-10-12 07:54:40 +00:00
Owner

FORGE-88 step 3 of 7 (FORGE-69 D1 migration, Phase 3): the BATCH and READ sites move from db.transaction() to atomic() (#720). There are three commits, grouped by area; review them commit by commit.

  1. api routers (T11, T16, T18, T21): workspace delete in FK order, the fleet rolling-update insert chunked by insertInChunks(), work-item addArtifact and rollback create with pre-generated ids.
  2. web routes (T8, T34, T38, T40, T43): auto-deploy insert chunked, deploys/complete demote+insert, work-items PATCH status, artifacts POST, node-access grants. T38 also fixes a race: the new overrideWorkItemStatus() touches the row first (a row lock on Postgres), so the activity's fromValue is the status actually overwritten. In the race test, 8 concurrent overrides produce one consistent chain; with the touch removed, all 8 record fromValue = draft.
  3. evidence (T1, T27, T28):
    • loadEffectiveEvidence reads its nine queries as one snapshot batch: atomic(..., { snapshot: true }), which is REPEATABLE READ, READ ONLY on Postgres. The latest-per-group reads use the new lazy latestPerGroupQuery(), and id lists go through inList(), so a page over 100 ids stays one parameter on D1.
    • The evidence route's build and test-run upserts, artifacts and timeline event commit in one batch. The id is read first and asserted in the batch, with retryOnAssertion() for a concurrent first insert.

Adapter additions: insertInChunks, rawStatement, rawRows, retryOnAssertion, atomic(..., { snapshot }). atomic() now accepts any thenable, so route test doubles keep working.

Unique indexes for T18/T40 (one current artifact) and T28 (test_artifacts) need production duplicate checks and land with the GUARD/UNIQUE step. work_item_artifacts has 1 duplicate group, and its repair is awaiting approval.

Tests

  • Database: atomic-batch-sites.database.test.ts (T11, T16 with 40 nodes and an all-or-nothing failure, T18, T21, T1 with 151 ids) and evidence-batch.database.test.ts (T27, plus T28 with 30 chunked artifacts).
  • Concurrency: work-item-status.concurrency.test.ts (T38).
  • Unit tests for deploys/complete, auto-deploy and node-access grants were adapted to pre-generated ids.
  • Local: api unit 1550 passed, web unit 1750 passed (the one failure is an environment-only jj check), and api, db and web tsc are clean.

Remaining: step 4 GUARD sites (4a stacked on this PR), 5 RepositoryCoordinator DO, 6 deploy route guards, 7 lint.

🤖 Generated with Claude Code

FORGE-88 step 3 of 7 (FORGE-69 D1 migration, Phase 3): the BATCH and READ sites move from `db.transaction()` to `atomic()` (#720). There are three commits, grouped by area; review them commit by commit. 1. **api routers** (T11, T16, T18, T21): workspace delete in FK order, the fleet rolling-update insert chunked by `insertInChunks()`, work-item `addArtifact` and rollback `create` with pre-generated ids. 2. **web routes** (T8, T34, T38, T40, T43): auto-deploy insert chunked, deploys/complete demote+insert, work-items PATCH status, artifacts POST, node-access grants. **T38 also fixes a race**: the new `overrideWorkItemStatus()` touches the row first (a row lock on Postgres), so the activity's `fromValue` is the status actually overwritten. In the race test, 8 concurrent overrides produce one consistent chain; with the touch removed, all 8 record `fromValue = draft`. 3. **evidence** (T1, T27, T28): - `loadEffectiveEvidence` reads its nine queries as one snapshot batch: `atomic(..., { snapshot: true })`, which is REPEATABLE READ, READ ONLY on Postgres. The latest-per-group reads use the new lazy `latestPerGroupQuery()`, and id lists go through `inList()`, so a page over 100 ids stays one parameter on D1. - The evidence route's build and test-run upserts, artifacts and timeline event commit in one batch. The id is read first and asserted in the batch, with `retryOnAssertion()` for a concurrent first insert. Adapter additions: `insertInChunks`, `rawStatement`, `rawRows`, `retryOnAssertion`, `atomic(..., { snapshot })`. `atomic()` now accepts any thenable, so route test doubles keep working. Unique indexes for T18/T40 (one current artifact) and T28 (test_artifacts) need production duplicate checks and land with the GUARD/UNIQUE step. work_item_artifacts has 1 duplicate group, and its repair is awaiting approval. ## Tests - Database: `atomic-batch-sites.database.test.ts` (T11, T16 with 40 nodes and an all-or-nothing failure, T18, T21, T1 with 151 ids) and `evidence-batch.database.test.ts` (T27, plus T28 with 30 chunked artifacts). - Concurrency: `work-item-status.concurrency.test.ts` (T38). - Unit tests for deploys/complete, auto-deploy and node-access grants were adapted to pre-generated ids. - Local: api unit 1550 passed, web unit 1750 passed (the one failure is an environment-only jj check), and api, db and web `tsc` are clean. Remaining: step 4 GUARD sites (4a stacked on this PR), 5 RepositoryCoordinator DO, 6 deploy route guards, 7 lint. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Phase 3 step 3a (FORGE-69 inventory T11, T16, T18, T21). Each
db.transaction() that only existed for all-or-nothing now runs as one
atomic() batch with ids generated in JS:

- T11 workspace delete: the three non-cascading history deletes and the
  workspace delete, in FK order.
- T16 fleet rolling update: deployments inserted through insertInChunks(), so
  a large fleet stays under D1's 100-parameter limit and still commits as one
  batch.
- T18 work-item addArtifact: supersede, insert, activity; the activity names a
  pre-generated artifact id instead of a RETURNING value.
- T21 rollback create: rollback and its rollback_requested event share a
  pre-generated id.

insertInChunks(rows, build) joins the adapter. It measures per-row and
per-statement parameters from the built statements, so defaults, $defaultFn
columns and ON CONFLICT values are counted exactly.

The partial unique index for T18 (one current artifact) needs a production
duplicate count first and lands with the GUARD step.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Phase 3 step 3b (FORGE-69 inventory T8, T34, T38, T40, T43).

- T8 auto-deploy: every deployment planned for a push is inserted in one
  atomic() batch, chunked by insertInChunks(); ids are generated in JS and
  returned without RETURNING.
- T34 deploys/complete: demote the previous server and insert the new Worker
  deployment in one batch with a pre-generated id.
- T38 work-items PATCH status: moved to overrideWorkItemStatus() in
  @forgegraph/api/lib/work-item-status. The batch is [touch; activity INSERT …
  SELECT status; UPDATE]. The touch takes the row lock on Postgres, so the
  activity's fromValue is the status this write overwrote. Before, two
  concurrent PATCHes could both record the same fromValue.
- T40 work-item artifacts POST and T43 node-access grants: batches with
  pre-generated ids.

Adapter changes: rawStatement(b, sql) runs a raw guarded statement on either
dialect (drizzle's insert().select() needs every table column), and
insertInChunks() builds one statement when the rows fit. atomic() now accepts
any thenable, so route test doubles keep working.

Race test: 8 concurrent overrides on real Postgres leave one activity chain
from the initial status to the final one (with the touch removed, all 8 record
fromValue = draft).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
FORGE-88: read effective evidence as one snapshot batch; batch evidence upserts
All checks were successful
CI / gitleaks (pull_request) Successful in 23s
CI / unit-api (pull_request) Successful in 2m35s
CI / unit-web (pull_request) Successful in 3m44s
CI / storybook (pull_request) Successful in 2m1s
CI / go (pull_request) Successful in 1m9s
CI / web-build (pull_request) Successful in 2m39s
CI / checks (pull_request) Successful in 3m9s
CI / database (pull_request) Successful in 3m50s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 46s
0bb6c52530
Phase 3 step 3c (FORGE-69 inventory T1, T27, T28).

- T1 loadEffectiveEvidence: the nine reads are one atomic() batch with the
  new { snapshot: true } option (REPEATABLE READ, READ ONLY on Postgres; a
  batch is already a snapshot on D1). latestHeadBuilds/TestRuns/Reviews use
  latestPerGroupQuery(), the unexecuted form of latestPerGroup(), so they can
  join the batch; awaiting them still works. Changeset id lists go through
  inList(), so a page over 100 ids stays one parameter on D1.
- T27/T28 evidence route: build (by run_id) and test-run (by source +
  external_run_id) upserts, their artifacts and the timeline event commit in
  one batch. A conflict keeps the existing row id, so the id is read first and
  asserted inside the batch; a concurrent first insert fails the assertion
  and retryOnAssertion() re-reads the winner's id. Artifacts are chunked by
  insertInChunks(); recordActivity() runs after the commit, as it always
  wrote outside the transaction.

Adapter: atomic(db, build, { snapshot }), rawRows(b, sql) for raw statements
whose rows you need, and retryOnAssertion().

The test_artifacts unique index (T28 bug) needs a production duplicate count
first and lands with the GUARD/UNIQUE step.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie force-pushed FORGE-88-read-evidence from 0bb6c52530
All checks were successful
CI / gitleaks (pull_request) Successful in 23s
CI / unit-api (pull_request) Successful in 2m35s
CI / unit-web (pull_request) Successful in 3m44s
CI / storybook (pull_request) Successful in 2m1s
CI / go (pull_request) Successful in 1m9s
CI / web-build (pull_request) Successful in 2m39s
CI / checks (pull_request) Successful in 3m9s
CI / database (pull_request) Successful in 3m50s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 46s
to 4d9d75cb1c
Some checks failed
CI / gitleaks (pull_request) Successful in 20s
CI / storybook (pull_request) Successful in 2m27s
CI / unit-web (pull_request) Successful in 2m47s
CI / unit-api (pull_request) Successful in 2m51s
CI / go (pull_request) Successful in 1m22s
CI / web-build (pull_request) Successful in 2m23s
CI / checks (pull_request) Successful in 3m12s
CI / database (pull_request) Successful in 3m13s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 48s
CI / database-d1 (pull_request) Failing after 6m46s
2026-10-12 06:53:13 +00:00
Compare
gmackie force-pushed FORGE-88-read-evidence from 4d9d75cb1c
Some checks failed
CI / gitleaks (pull_request) Successful in 20s
CI / storybook (pull_request) Successful in 2m27s
CI / unit-web (pull_request) Successful in 2m47s
CI / unit-api (pull_request) Successful in 2m51s
CI / go (pull_request) Successful in 1m22s
CI / web-build (pull_request) Successful in 2m23s
CI / checks (pull_request) Successful in 3m12s
CI / database (pull_request) Successful in 3m13s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 48s
CI / database-d1 (pull_request) Failing after 6m46s
to 79bdb54e2b
Some checks failed
CF executor smoke / smoke (pull_request) Successful in 16s
CI / superseded (pull_request) Successful in 0s
CI / go (pull_request) Successful in 1m3s
CI / storybook (pull_request) Successful in 2m9s
CI / gitleaks (pull_request) Successful in 16s
CI / checks (pull_request) Successful in 2m7s
CI / unit-api (pull_request) Successful in 2m10s
CI / web-build (pull_request) Successful in 4m4s
CI / database (pull_request) Successful in 4m7s
CI / unit-web (pull_request) Successful in 3m41s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 30s
CI / database-d1 (pull_request) Failing after 8m11s
2026-10-12 07:42:55 +00:00
Compare
Author
Owner

Preview environment is live: https://pr-736-forgegraph.forgegraf.com

Deployed 79bdb54e with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.

Preview environment is live: https://pr-736-forgegraph.forgegraf.com Deployed `79bdb54e` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
Sign in to join this conversation.
No description provided.