FORGE-88: guard work-item transitions, rollbacks and Stripe sync (Phase 3 step 4a) #737
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
gmackie/ForgeGraph!737
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "FORGE-88-guards-a"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
INSERT … SELECT,UPDATE … RETURNING. Each statement is conditioned onstatusstill 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.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.failed, thenINSERT … ON CONFLICT DO NOTHING. A newdeployments.rollback_of_deployment_idcolumn 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).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 reportsduplicate. New unique indexes onsubscriptions(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 migrateright after merge.transitionWorkItem's mock-transaction unit tests are replaced by database tests against the real batch.🤖 Generated with Claude Code
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>4be1ce9b7bb412baaac6b412baaac60a39603720