FORGE-85: assign deployment_events.seq as max(seq) + 1 #713

Merged
gmackie merged 1 commit from FORGE-85-seq-max-plus-one into main 2026-10-12 07:48:43 +00:00
Owner

Part 7b of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #745 (D1 constraint-message fix). #712's index is applied and valid in production (migration 0121).

  • appendDeploymentEvent assigns seq in the insert: INSERT … SELECT coalesce(max(seq), 0) + 1 FROM deployment_events, retried with jittered backoff (bound 100) on a collision with deployment_event_seq_unique_idx. No writer uses the serial sequence; the PG default stays until the D1 schema replaces it.
  • Cursor safety: a writer can only pick k + 1 once k is committed and visible, so events commit in seq order and getDeploymentEvents(…, afterSeq) never skips a late-committing event, which a sequence allowed. On D1 each statement runs alone on the single writer, so the retry never fires.
  • appendDeploymentEvents appends a batch in order. The agent report route uses it instead of Promise.all, whose parallel appends would only collide and whose order was arbitrary.
  • payload and created_at are bound through the column encoders and typed by the INSERT target (no ::jsonb), so the identical statement runs on D1; deployment-events-seq-index leaves d1-known-failures.txt. created_at via the encoder also matters because; a raw Date in sql isn't serialised by postgres-js once the server infers a timestamp.

Verification

  • deployment-events-seq.concurrency.test.ts (real Postgres): 80 concurrent appends across two deployments over a 20-connection pool, with a cursor reader polling throughout. It asserts distinct, gap-free seqs and that the reader saw every event exactly once, in order. With retries disabled it fails on deployment_event_seq_unique_idx, so the contention is real. CI caught that a fixed 20-attempt bound could starve one writer under 80-way contention; the jittered backoff fixes it. Passes repeatedly on PG16, including a 300-writer stress run.
  • deployment-events-seq.database.test.ts: ordered appends, payloads, cursor reads (PGlite + PG16). 7a's lag test passes against the new writer.
  • D1 lane (test:d1) green including both seq test files; api/web tsc clean; agent and deploy route suites (142) green.

🤖 Generated with Claude Code

Part 7b of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #745 (D1 constraint-message fix). #712's index is applied and valid in production (migration 0121). - `appendDeploymentEvent` assigns seq in the insert: `INSERT … SELECT coalesce(max(seq), 0) + 1 FROM deployment_events`, retried with jittered backoff (bound 100) on a collision with `deployment_event_seq_unique_idx`. No writer uses the serial sequence; the PG default stays until the D1 schema replaces it. - Cursor safety: a writer can only pick k + 1 once k is committed and visible, so events commit in seq order and `getDeploymentEvents(…, afterSeq)` never skips a late-committing event, which a sequence allowed. On D1 each statement runs alone on the single writer, so the retry never fires. - `appendDeploymentEvents` appends a batch in order. The agent report route uses it instead of `Promise.all`, whose parallel appends would only collide and whose order was arbitrary. - `payload` and `created_at` are bound through the column encoders and typed by the INSERT target (no `::jsonb`), so the identical statement runs on D1; `deployment-events-seq-index` leaves d1-known-failures.txt. `created_at` via the encoder also matters because; a raw Date in `sql` isn't serialised by postgres-js once the server infers a timestamp. Verification - `deployment-events-seq.concurrency.test.ts` (real Postgres): 80 concurrent appends across two deployments over a 20-connection pool, with a cursor reader polling throughout. It asserts distinct, gap-free seqs and that the reader saw every event exactly once, in order. With retries disabled it fails on `deployment_event_seq_unique_idx`, so the contention is real. CI caught that a fixed 20-attempt bound could starve one writer under 80-way contention; the jittered backoff fixes it. Passes repeatedly on PG16, including a 300-writer stress run. - `deployment-events-seq.database.test.ts`: ordered appends, payloads, cursor reads (PGlite + PG16). 7a's lag test passes against the new writer. - D1 lane (`test:d1`) green including both seq test files; api/web tsc clean; agent and deploy route suites (142) green. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-85: assign deployment_events.seq as max(seq) + 1
All checks were successful
CI / gitleaks (pull_request) Successful in 12s
CI / storybook (pull_request) Successful in 1m28s
CI / go (pull_request) Successful in 1m6s
CI / unit-web (pull_request) Successful in 2m0s
CI / unit-api (pull_request) Successful in 2m45s
CI / web-build (pull_request) Successful in 3m40s
CI / database (pull_request) Successful in 3m33s
CI / checks (pull_request) Successful in 4m28s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 41s
034d9c7d3a
appendDeploymentEvent now assigns seq in the insert itself
(INSERT ... SELECT coalesce(max(seq), 0) + 1 FROM deployment_events),
retrying when another writer took the value (unique index from 0116).
No writer uses the serial sequence any more; the column default stays on
Postgres and goes with the D1 schema.

Writers can only pick k + 1 once k is committed and visible, so events
commit in seq order and a cursor reader polling seq > afterSeq never skips
an event that commits late, which the sequence allowed.

The agent report route appends a report's events in order through the new
appendDeploymentEvents instead of Promise.all: parallel appends to one
cursor only collide, and their relative order was arbitrary.

Tests: ordered appends and cursor reads; a parallel-insert test on real
Postgres (80 concurrent appends over a 12-connection pool with a cursor
reader polling throughout) checks gap-free distinct seqs and that the
reader sees every event exactly once, in order. With retries disabled it
fails with seq collisions, so the contention is real.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Author
Owner

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

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

Preview environment is live: https://pr-713-forgegraph.forgegraf.com Deployed `034d9c7d` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
Author
Owner

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

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

Preview environment is live: https://pr-713-forgegraph.forgegraf.com Deployed `034d9c7d` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
gmackie force-pushed FORGE-85-seq-max-plus-one from 034d9c7d3a
All checks were successful
CI / gitleaks (pull_request) Successful in 12s
CI / storybook (pull_request) Successful in 1m28s
CI / go (pull_request) Successful in 1m6s
CI / unit-web (pull_request) Successful in 2m0s
CI / unit-api (pull_request) Successful in 2m45s
CI / web-build (pull_request) Successful in 3m40s
CI / database (pull_request) Successful in 3m33s
CI / checks (pull_request) Successful in 4m28s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 41s
to a4ec761370
Some checks failed
CI / gitleaks (pull_request) Successful in 11s
CF executor smoke / smoke (pull_request) Successful in 24s
CI / unit-api (pull_request) Successful in 3m31s
CI / unit-web (pull_request) Successful in 4m2s
CI / storybook (pull_request) Successful in 2m3s
CI / database (pull_request) Failing after 2m33s
CI / go (pull_request) Successful in 1m46s
CI / web-build (pull_request) Successful in 3m42s
CI / checks (pull_request) Successful in 3m57s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 54s
2026-10-12 05:26:26 +00:00
Compare
gmackie force-pushed FORGE-85-seq-max-plus-one from a4ec761370
Some checks failed
CI / gitleaks (pull_request) Successful in 11s
CF executor smoke / smoke (pull_request) Successful in 24s
CI / unit-api (pull_request) Successful in 3m31s
CI / unit-web (pull_request) Successful in 4m2s
CI / storybook (pull_request) Successful in 2m3s
CI / database (pull_request) Failing after 2m33s
CI / go (pull_request) Successful in 1m46s
CI / web-build (pull_request) Successful in 3m42s
CI / checks (pull_request) Successful in 3m57s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 54s
to 8ee8c5617c
Some checks failed
CI / gitleaks (pull_request) Successful in 22s
CI / storybook (pull_request) Successful in 2m44s
CI / unit-api (pull_request) Successful in 3m37s
CI / go (pull_request) Successful in 1m48s
CI / web-build (pull_request) Successful in 2m26s
CI / checks (pull_request) Successful in 2m37s
CI / database (pull_request) Failing after 2m34s
CI / unit-web (pull_request) Successful in 5m41s
CI / ci (pull_request) Failing after 1m10s
2026-10-12 06:08:22 +00:00
Compare
gmackie force-pushed FORGE-85-seq-max-plus-one from 8ee8c5617c
Some checks failed
CI / gitleaks (pull_request) Successful in 22s
CI / storybook (pull_request) Successful in 2m44s
CI / unit-api (pull_request) Successful in 3m37s
CI / go (pull_request) Successful in 1m48s
CI / web-build (pull_request) Successful in 2m26s
CI / checks (pull_request) Successful in 2m37s
CI / database (pull_request) Failing after 2m34s
CI / unit-web (pull_request) Successful in 5m41s
CI / ci (pull_request) Failing after 1m10s
to e69fea2b56
Some checks failed
CI / gitleaks (pull_request) Successful in 20s
CF executor smoke / smoke (pull_request) Successful in 30s
CI / unit-api (pull_request) Successful in 2m0s
CI / unit-web (pull_request) Successful in 5m42s
CI / storybook (pull_request) Successful in 1m44s
CI / go (pull_request) Successful in 1m8s
CI / web-build (pull_request) Successful in 2m26s
CI / database (pull_request) Failing after 2m26s
CI / checks (pull_request) Successful in 2m34s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 56s
2026-10-12 06:15:15 +00:00
Compare
gmackie force-pushed FORGE-85-seq-max-plus-one from e69fea2b56
Some checks failed
CI / gitleaks (pull_request) Successful in 20s
CF executor smoke / smoke (pull_request) Successful in 30s
CI / unit-api (pull_request) Successful in 2m0s
CI / unit-web (pull_request) Successful in 5m42s
CI / storybook (pull_request) Successful in 1m44s
CI / go (pull_request) Successful in 1m8s
CI / web-build (pull_request) Successful in 2m26s
CI / database (pull_request) Failing after 2m26s
CI / checks (pull_request) Successful in 2m34s
forgegraph/ci CI failed
CI / ci (pull_request) Failing after 56s
to 97531688cb
All checks were successful
CI / gitleaks (pull_request) Successful in 20s
CI / unit-api (pull_request) Successful in 2m6s
CI / unit-web (pull_request) Successful in 2m59s
CI / storybook (pull_request) Successful in 2m10s
CI / go (pull_request) Successful in 1m27s
CI / checks (pull_request) Successful in 2m46s
CI / database (pull_request) Successful in 3m12s
CI / web-build (pull_request) Successful in 3m54s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 55s
2026-10-12 06:33:02 +00:00
Compare
Author
Owner

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

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

Preview environment is live: https://pr-713-forgegraph.forgegraf.com Deployed `97531688` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
gmackie changed target branch from FORGE-85-seq-unique-index to FORGE-85-d1-lane-fixes 2026-10-12 07:35:47 +00:00
gmackie force-pushed FORGE-85-seq-max-plus-one from 97531688cb
All checks were successful
CI / gitleaks (pull_request) Successful in 20s
CI / unit-api (pull_request) Successful in 2m6s
CI / unit-web (pull_request) Successful in 2m59s
CI / storybook (pull_request) Successful in 2m10s
CI / go (pull_request) Successful in 1m27s
CI / checks (pull_request) Successful in 2m46s
CI / database (pull_request) Successful in 3m12s
CI / web-build (pull_request) Successful in 3m54s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 55s
to 1f6e651329
All checks were successful
CI / superseded (pull_request) Successful in 1s
CI / gitleaks (pull_request) Successful in 19s
CI / storybook (pull_request) Successful in 2m19s
CI / web-build (pull_request) Successful in 2m56s
CI / checks (pull_request) Successful in 2m14s
CI / go (pull_request) Successful in 1m28s
CI / database (pull_request) Successful in 3m8s
CI / unit-web (pull_request) Successful in 2m31s
CI / unit-api (pull_request) Successful in 2m33s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 38s
CI / database-d1 (pull_request) Successful in 6m56s
2026-10-12 07:35:49 +00:00
Compare
Author
Owner

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

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

Preview environment is live: https://pr-713-forgegraph.forgegraf.com Deployed `1f6e6513` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
Author
Owner

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

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

Preview environment is live: https://pr-713-forgegraph.forgegraf.com Deployed `1f6e6513` with the beta stage's environment. It redeploys on every push and is destroyed when this PR closes.
gmackie changed target branch from FORGE-85-d1-lane-fixes to main 2026-10-12 07:48:34 +00:00
Sign in to join this conversation.
No description provided.