FORGE-88: guard work-item transitions, rollbacks and Stripe sync (Phase 3 step 4a) #737

Merged
gmackie merged 1 commit from FORGE-88-guards-a into main 2026-10-12 07:55:41 +00:00
Owner

FORGE-88 step 4a (FORGE-69 D1 migration, Phase 3): GUARD sites that fix pre-existing races. Stacked on #736 (step 3), so review only the last commit.

Every fix has a test that runs the operation concurrently on real Postgres, and every one of those tests fails with the guard removed.

  • T3 transitionWorkItem. It validates against the read status, then runs one guarded batch: touch, activity INSERT … SELECT, UPDATE … RETURNING. Each statement is conditioned on status still being the validated one. Zero rows means re-read and revalidate. Bug fixed: the UPDATE was unconditional. With 6 racing writers, 6 transitions applied before; 1 applies now.
  • T22 rollback updateStatus. Metadata is merged in SQL (coalesce(metadata,'{}') || patch), and the event, environment state and intervention are written in the same batch. Bug fixed: the JS read-merge-write lost keys. With 6 racing updates, 1 key survived before; all 6 survive now.
  • T17 triggerRollback. A guarded batch: assert the deployment is still failed, then INSERT … ON CONFLICT DO NOTHING. A new deployments.rollback_of_deployment_id column has a partial unique index, so one failure gets at most one rollback; a repeated run returns the existing one. Migration 0121 adds the column and the index (CONCURRENTLY; the column is new, so it has no data).
  • T14 Stripe sync. The mutation is planned from a read, then: insert the ledger row with a pre-generated id ON CONFLICT DO NOTHING, assert our id is the recorded one, write the subscription. A replay or a concurrent duplicate delivery fails the assertion and reports duplicate. New unique indexes on subscriptions(user_id) and (stripe_subscription_id) (production duplicates counted read-only: 0 and 0), so a racing first insert becomes an update. Migration 0122 (CONCURRENTLY).

Both migrations are additive and idempotent. I'll apply each to production with fg db migrate right after merge.

transitionWorkItem's mock-transaction unit tests are replaced by database tests against the real batch.

🤖 Generated with Claude Code

FORGE-88 step 4a (FORGE-69 D1 migration, Phase 3): GUARD sites that fix pre-existing races. **Stacked on #736 (step 3)**, so review only the last commit. Every fix has a test that runs the operation concurrently on real Postgres, and every one of those tests fails with the guard removed. - **T3 transitionWorkItem.** It validates against the read status, then runs one guarded batch: touch, activity `INSERT … SELECT`, `UPDATE … RETURNING`. Each statement is conditioned on `status` still being the validated one. Zero rows means re-read and revalidate. Bug fixed: the UPDATE was unconditional. With 6 racing writers, 6 transitions applied before; 1 applies now. - **T22 rollback updateStatus.** Metadata is merged in SQL (`coalesce(metadata,'{}') || patch`), and the event, environment state and intervention are written in the same batch. Bug fixed: the JS read-merge-write lost keys. With 6 racing updates, 1 key survived before; all 6 survive now. - **T17 triggerRollback.** A guarded batch: assert the deployment is still `failed`, then `INSERT … ON CONFLICT DO NOTHING`. A new `deployments.rollback_of_deployment_id` column has a partial unique index, so one failure gets at most one rollback; a repeated run returns the existing one. Migration **0121** adds the column and the index (CONCURRENTLY; the column is new, so it has no data). - **T14 Stripe sync.** The mutation is planned from a read, then: insert the ledger row with a pre-generated id `ON CONFLICT DO NOTHING`, assert our id is the recorded one, write the subscription. A replay or a concurrent duplicate delivery fails the assertion and reports `duplicate`. New unique indexes on `subscriptions(user_id)` and `(stripe_subscription_id)` (production duplicates counted read-only: 0 and 0), so a racing first insert becomes an update. Migration **0122** (CONCURRENTLY). Both migrations are additive and idempotent. I'll apply each to production with `fg db migrate` right after merge. `transitionWorkItem`'s mock-transaction unit tests are replaced by database tests against the real batch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-88: guard work-item transitions, rollbacks and Stripe sync; merge rollback metadata in SQL
Some checks failed
CI / gitleaks (pull_request) Successful in 13s
CI / unit-api (pull_request) Successful in 2m29s
CI / unit-web (pull_request) Successful in 5m19s
CI / storybook (pull_request) Successful in 2m8s
CI / web-build (pull_request) Successful in 2m50s
CI / go (pull_request) Successful in 1m28s
CI / checks (pull_request) Successful in 3m10s
CI / database (pull_request) Failing after 2m57s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 56s
4be1ce9b7b
Phase 3 step 4a (FORGE-69 inventory T3, T14, T17, T22). Each one fixes a
pre-existing race and has a test that runs it concurrently on real Postgres
and fails without the guard.

- T3 transitionWorkItem: validate against the read status, then one guarded
  batch [touch; activity INSERT … SELECT; UPDATE … RETURNING], each statement
  conditioned on status = the validated status. On zero rows it re-reads and
  revalidates. Bug: the UPDATE was unconditional, so two transitions from one
  status both applied (6 racing writers: 6 applied before, 1 now).
- T22 rollback updateStatus: metadata is merged in SQL
  (coalesce(metadata,'{}') || patch); the batch also writes the event,
  environment state and intervention. Bug: the JS read-merge-write lost
  concurrent keys (6 racing updates kept 1 key before, all 6 now).
- T17 triggerRollback: a guarded batch [assert still failed; insert … ON
  CONFLICT DO NOTHING] with a new deployments.rollback_of_deployment_id and a
  partial unique index, so one failure gets at most one rollback; a repeated
  run returns the existing one. Migration 0121 (column + CONCURRENTLY index).
- T14 Stripe sync: the mutation is planned from a read, then [ledger insert
  with a pre-generated id ON CONFLICT DO NOTHING; assert our id is the
  recorded one; subscription write]. A replay or concurrent duplicate fails
  the assertion and reports duplicate. New unique indexes on
  subscriptions(user_id) and (stripe_subscription_id) (0 duplicates in
  production); a racing first insert becomes an update. Migration 0122.

transitionWorkItem's mock-transaction unit tests are replaced by database
tests against the real batch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie force-pushed FORGE-88-guards-a from 4be1ce9b7b
Some checks failed
CI / gitleaks (pull_request) Successful in 13s
CI / unit-api (pull_request) Successful in 2m29s
CI / unit-web (pull_request) Successful in 5m19s
CI / storybook (pull_request) Successful in 2m8s
CI / web-build (pull_request) Successful in 2m50s
CI / go (pull_request) Successful in 1m28s
CI / checks (pull_request) Successful in 3m10s
CI / database (pull_request) Failing after 2m57s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 56s
to b412baaac6
Some checks failed
CI / gitleaks (pull_request) Successful in 25s
CI / go (pull_request) Successful in 1m25s
CI / storybook (pull_request) Successful in 2m13s
CI / checks (pull_request) Successful in 2m28s
CI / unit-web (pull_request) Successful in 2m32s
CI / web-build (pull_request) Successful in 2m49s
CI / unit-api (pull_request) Successful in 2m54s
CI / database (pull_request) Successful in 3m20s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 45s
CI / database-d1 (pull_request) Failing after 8m32s
2026-10-12 06:53:07 +00:00
Compare
gmackie force-pushed FORGE-88-guards-a from b412baaac6
Some checks failed
CI / gitleaks (pull_request) Successful in 25s
CI / go (pull_request) Successful in 1m25s
CI / storybook (pull_request) Successful in 2m13s
CI / checks (pull_request) Successful in 2m28s
CI / unit-web (pull_request) Successful in 2m32s
CI / web-build (pull_request) Successful in 2m49s
CI / unit-api (pull_request) Successful in 2m54s
CI / database (pull_request) Successful in 3m20s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 45s
CI / database-d1 (pull_request) Failing after 8m32s
to 0a39603720
Some checks failed
CI / superseded (pull_request) Successful in 1s
CF executor smoke / smoke (pull_request) Successful in 15s
CI / go (pull_request) Successful in 1m26s
CI / gitleaks (pull_request) Successful in 17s
CI / storybook (pull_request) Successful in 2m9s
CI / web-build (pull_request) Successful in 2m40s
CI / checks (pull_request) Successful in 2m43s
CI / database (pull_request) Successful in 3m38s
CI / unit-api (pull_request) Successful in 2m45s
CI / unit-web (pull_request) Successful in 2m46s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 35s
CI / database-d1 (pull_request) Failing after 7m5s
2026-10-12 07:42:47 +00:00
Compare
gmackie changed target branch from FORGE-88-read-evidence to main 2026-10-12 07:55:21 +00:00
Sign in to join this conversation.
No description provided.