FORGE-88: guarded outbox/readiness claims and resource_locks lease (Phase 3 step 2) #729

Merged
gmackie merged 1 commit from FORGE-88-outbox-lease into main 2026-10-12 06:31:54 +00:00
Owner

FORGE-88 step 2 of 7 (FORGE-69 D1 migration, Phase 3). This covers inventory T6, T9 and T10 and the three lock sites in notification-outbox.ts:23, readiness-webhook.ts:114 and node-access-db.ts:84. Step 1 (#720, atomic adapter) is merged.

What

  • Outbox drain (T6). FOR UPDATE SKIP LOCKED plus per-row updates in a transaction become one statement: UPDATE notification_outbox SET lease… WHERE id IN (SELECT … ORDER BY next_attempt_at, id LIMIT n) AND <eligible> RETURNING *. Repeating the eligibility predicate on the outer UPDATE is what keeps the claim exclusive on Postgres. A drainer blocked on a row re-checks it after the winner commits, and skips it because the lease is no longer expired. Acks stay fenced by lease_token.
  • Readiness reconciler (T9). Same shape. A link visited within READINESS_VISIT_COOLDOWN_MS (5 s) is not reclaimed, so each link is visited by one reconciler per round. It now also returns linkIds.
  • Node-access owner lock (T10). pg_try_advisory_xact_lock becomes a resource_locks lease (new @forgegraph/api/lib/resource-lease).
    • Acquire is one statement: INSERT … ON CONFLICT (key) DO UPDATE … WHERE expires_at < now RETURNING token. Contenders fail fast with operation_in_progress, as before.
    • The lease is renewed every ttl/3 (default ttl 60 s) while the Cloudflare calls run, and released by token.
    • It no longer needs a transaction or a dedicated connection, so the handler stops opening one. The node_access_resources lease CAS stays the write fence.
  • Migration 0120_resource_locks.sql. Additive and idempotent, and grants to forgegraph_dbcooper. I'll apply it to production right after merge with fg db migrate.

Concurrency tests (rewritten to the new semantics)

  • notification-outbox.concurrency.test.ts:
    • The existing intent/retry test is kept.
    • New: 4 drainers on separate connections deliver 30 due rows exactly once each (attempts = 1). With the outer guard removed this test fails with 40 deliveries for 30 rows.
    • New: 3 concurrent reconcilers visit each link once, and an immediate re-run revisits none.
  • node-access.concurrency.test.ts:
    • The original scenario is kept: overlap fails fast, an independent key works, a retry after release works, and durable cleanup state is not rolled back. It also checks that the lease row is released.
    • New: 8 callers race one lease and exactly one wins.
    • New: a 300 ms lease renewed every 50 ms holds through 900 ms of work.
    • New: an abandoned lease blocks until it expires, then is taken over.
  • registry-leases and review-concurrency are unchanged; review moves with step 5.

Local, on real Postgres 17: concurrency lane 18/18, readiness and kanbanger database tests 15/15, full api database lane green earlier, and api tsc clean.

Remaining FORGE-88 steps: 3 (BATCH/READ sites; branches FORGE-88-batch-routers, FORGE-88-batch-web and FORGE-88-read-evidence are ready behind this), 4 GUARD sites, 5 RepositoryCoordinator DO, 6 deploy route guards, 7 lint at error.

🤖 Generated with Claude Code

FORGE-88 step 2 of 7 (FORGE-69 D1 migration, Phase 3). This covers inventory T6, T9 and T10 and the three lock sites in notification-outbox.ts:23, readiness-webhook.ts:114 and node-access-db.ts:84. Step 1 (#720, atomic adapter) is merged. ## What - **Outbox drain (T6).** `FOR UPDATE SKIP LOCKED` plus per-row updates in a transaction become one statement: `UPDATE notification_outbox SET lease… WHERE id IN (SELECT … ORDER BY next_attempt_at, id LIMIT n) AND <eligible> RETURNING *`. Repeating the eligibility predicate on the outer UPDATE is what keeps the claim exclusive on Postgres. A drainer blocked on a row re-checks it after the winner commits, and skips it because the lease is no longer expired. Acks stay fenced by `lease_token`. - **Readiness reconciler (T9).** Same shape. A link visited within `READINESS_VISIT_COOLDOWN_MS` (5 s) is not reclaimed, so each link is visited by one reconciler per round. It now also returns `linkIds`. - **Node-access owner lock (T10).** `pg_try_advisory_xact_lock` becomes a `resource_locks` lease (new `@forgegraph/api/lib/resource-lease`). - Acquire is one statement: `INSERT … ON CONFLICT (key) DO UPDATE … WHERE expires_at < now RETURNING token`. Contenders fail fast with `operation_in_progress`, as before. - The lease is renewed every ttl/3 (default ttl 60 s) while the Cloudflare calls run, and released by token. - It no longer needs a transaction or a dedicated connection, so the handler stops opening one. The `node_access_resources` lease CAS stays the write fence. - **Migration `0120_resource_locks.sql`.** Additive and idempotent, and grants to forgegraph_dbcooper. I'll apply it to production right after merge with `fg db migrate`. ## Concurrency tests (rewritten to the new semantics) - `notification-outbox.concurrency.test.ts`: - The existing intent/retry test is kept. - New: 4 drainers on separate connections deliver 30 due rows exactly once each (attempts = 1). With the outer guard removed this test fails with 40 deliveries for 30 rows. - New: 3 concurrent reconcilers visit each link once, and an immediate re-run revisits none. - `node-access.concurrency.test.ts`: - The original scenario is kept: overlap fails fast, an independent key works, a retry after release works, and durable cleanup state is not rolled back. It also checks that the lease row is released. - New: 8 callers race one lease and exactly one wins. - New: a 300 ms lease renewed every 50 ms holds through 900 ms of work. - New: an abandoned lease blocks until it expires, then is taken over. - `registry-leases` and `review-concurrency` are unchanged; review moves with step 5. Local, on real Postgres 17: concurrency lane 18/18, readiness and kanbanger database tests 15/15, full api database lane green earlier, and api `tsc` clean. Remaining FORGE-88 steps: 3 (BATCH/READ sites; branches FORGE-88-batch-routers, FORGE-88-batch-web and FORGE-88-read-evidence are ready behind this), 4 GUARD sites, 5 RepositoryCoordinator DO, 6 deploy route guards, 7 lint at error. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-88: claim outbox and readiness rows with one guarded UPDATE; lease node-access owners
All checks were successful
CI / gitleaks (pull_request) Successful in 19s
CI / unit-api (pull_request) Successful in 2m49s
CI / unit-web (pull_request) Successful in 3m52s
CI / go (pull_request) Successful in 1m7s
CI / storybook (pull_request) Successful in 1m53s
CI / web-build (pull_request) Successful in 2m47s
CI / database (pull_request) Successful in 3m5s
CI / checks (pull_request) Successful in 4m39s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 45s
2ccdb2ca7b
Phase 3 step 2 (FORGE-69 inventory T6, T9, T10 and the three lock sites).

- notification-outbox drain (T6): FOR UPDATE SKIP LOCKED + per-row updates in a
  transaction become one UPDATE … WHERE id IN (SELECT … ORDER BY … LIMIT n)
  AND <eligible> RETURNING. Repeating the eligibility predicate on the outer
  UPDATE is what keeps it exclusive on Postgres: a drainer blocked on a row
  re-checks it after the winner commits and skips it.
- readiness reconciler (T9): the same shape. A link visited within
  READINESS_VISIT_COOLDOWN_MS (5 s) is not reclaimed, so each link is visited
  by one reconciler per round. It now also returns the visited link ids.
- node-access owner lock (T10): pg_try_advisory_xact_lock is replaced by a
  resource_locks lease (new table, migration 0120). Acquire is one INSERT … ON
  CONFLICT DO UPDATE … WHERE expired RETURNING; the lease is renewed every
  ttl/3 while the Cloudflare calls run and released by token. The handler no
  longer opens a second connection for the lock.

Concurrency tests rewritten to the new semantics: concurrent drainers deliver
each due row exactly once (fails with the guard removed: 40 deliveries for 30
rows), concurrent reconcilers visit each link once, 8 callers race one lease
and one wins, renewal holds a 300 ms lease through 900 ms of work, and an
abandoned lease is taken over only after expiry.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.