Skip to content

fix: read-your-writes + global-scope idempotency correctness under the run-ops split#4284

Merged
d-cs merged 3 commits into
mainfrom
fix/wait-until-idempotency-retry-rewait
Jul 19, 2026
Merged

fix: read-your-writes + global-scope idempotency correctness under the run-ops split#4284
d-cs merged 3 commits into
mainfrom
fix/wait-until-idempotency-retry-rewait

Conversation

@d-cs

@d-cs d-cs commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

What & why

Two related correctness fixes for the run-ops DB split. Under the split, run-store reads can route to a lagging read replica; a just-written run/waitpoint/batch can then be missed, causing a wrong decision.

1. Read-your-writes → owning primary. Surfaced first as an intermittent wait.until({ idempotencyKey }) re-wait on retry. Auditing the run-store read surface found the same class at sibling sites (some gating mutations or returning spurious 404s, others tolerable/self-healing). Reads that must observe their own writes now route to the owning primary (findRun/findWaitpoint/findBatchTaskRunByFriendlyId*OnPrimary, a primary re-read on a miss, or a retryable 404 where the SDK polls). Read-view reads stay on the replica. All additive — the happy path is unchanged.

2. Global-scope idempotency across the split. A global-scope key carries no per-run salt, so the same (env, task, key) triggered concurrently from parents resident on different run-ops DBs could dedup-miss on each DB and create a duplicate (the per-DB unique index can't enforce cross-DB uniqueness). Such triggers (global scope, or scope-absent, while split is active) are serialized through the existing Redis idempotency claim, the loser resolves the winner by id across both DBs, and the claim is reacquired on the expired/failed clear-and-recreate path. run/attempt scope embed the run id and never contend.

Stacked for review

This is the base of a 2-PR stack, split so review is easier:

Validation

Local run-ops split, both 2-DB and 3-DB, fresh boot on this branch: SDK canary 64/71 (only the known concurrency/input-streams/s3 failures), quarantine sweep 0 unexpected (340 pass / 16 known / 4 local) in each topology, dashboard e2e 0 failed. No product regressions.

@changeset-bot

changeset-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1c625e1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@d-cs d-cs self-assigned this Jul 18, 2026
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ed866f96-8518-4d1b-9b7f-4900205c3b18

📥 Commits

Reviewing files that changed from the base of the PR and between 568b3bf and 1c625e1.

📒 Files selected for processing (10)
  • apps/webapp/app/env.server.ts
  • apps/webapp/app/routes/realtime.v1.batches.$batchId.ts
  • apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts
  • apps/webapp/app/runEngine/services/triggerTask.server.ts
  • apps/webapp/app/v3/mollifier/claimTtl.ts
  • apps/webapp/app/v3/mollifier/idempotencyClaim.server.ts
  • apps/webapp/app/v3/realtime/resolveBatchForRealtime.server.ts
  • apps/webapp/test/mollifierIdempotencyClaim.test.ts
  • internal-packages/run-store/src/runOpsStore.residencyMismatchFallback.test.ts
  • internal-packages/run-store/src/runOpsStore.ts
💤 Files with no reviewable changes (1)
  • internal-packages/run-store/src/runOpsStore.residencyMismatchFallback.test.ts
🚧 Files skipped from review as they are similar to previous changes (9)
  • apps/webapp/app/v3/mollifier/claimTtl.ts
  • apps/webapp/app/routes/realtime.v1.batches.$batchId.ts
  • apps/webapp/test/mollifierIdempotencyClaim.test.ts
  • apps/webapp/app/env.server.ts
  • apps/webapp/app/v3/realtime/resolveBatchForRealtime.server.ts
  • apps/webapp/app/runEngine/services/triggerTask.server.ts
  • apps/webapp/app/v3/mollifier/idempotencyClaim.server.ts
  • internal-packages/run-store/src/runOpsStore.ts
  • apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout. (24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (10, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (11, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (12, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (4, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (9, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (3, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 12)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (1, 3)
  • GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp
  • GitHub Check: packages / 🧪 Unit Tests: Packages (2, 3)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (3, 3)
  • GitHub Check: internal / 🧪 Unit Tests: Internal
  • GitHub Check: typecheck / typecheck
  • GitHub Check: runops-guard / runops-guard
  • GitHub Check: code-quality / code-quality
  • GitHub Check: audit
  • GitHub Check: 🛡️ E2E Auth Tests (full)
  • GitHub Check: audit
  • GitHub Check: Analyze (javascript-typescript)

Walkthrough

The changes add primary fallbacks for replica-miss run, batch, waitpoint, and session lookups across API, realtime, resource, presenter, routing, and execution paths. Batch routes also mark not-found responses as retryable. Run-store routing now uses owning-store reads for classifiable runs and colocated dedup probes. Split-mode global idempotency handling adds claim TTL calculation, compare-and-delete reset, cross-database winner resolution, bounded cleared-winner reacquisition, and explicit claim publish outcomes.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is useful, but it doesn't follow the required template and is missing Closes #, checklist items, Testing, Changelog, and Screenshots sections. Add the repo's required template sections: Closes #issue, checklist, Testing steps, Changelog summary, and Screenshots placeholder.
Docstring Coverage ⚠️ Warning Docstring coverage is 38.02% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the two main fixes: read-your-writes consistency and global-scope idempotency under the run-ops split.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/wait-until-idempotency-retry-rewait

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]

This comment was marked as resolved.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

coderabbitai[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Jul 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@e4c9b5c

trigger.dev

npm i https://pkg.pr.new/trigger.dev@e4c9b5c

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@e4c9b5c

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@e4c9b5c

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@e4c9b5c

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@e4c9b5c

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@e4c9b5c

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@e4c9b5c

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@e4c9b5c

commit: e4c9b5c

coderabbitai[bot]

This comment was marked as resolved.

…bal-scope idempotency correctness under the run-ops split

Production code only — the guarding tests are in the stacked PR.

1) Read-your-writes: run-store reads that gate a mutation or feed a public GET /
   realtime response were routed to a lagging read replica, so a just-written
   run/waitpoint/batch could spuriously miss under replica lag. Route those reads
   to the owning primary (findRun/findWaitpoint/findBatchTaskRunByFriendlyId ->
   *OnPrimary, a primary re-read on a miss, or a retryable 404 where the SDK polls).
   Additive: the happy path is unchanged; a primary read happens only on a miss.

2) Global-scope idempotency across the split: a global-scope key carries no per-run
   salt, so the same (env, task, key) triggered concurrently from parents resident
   on different run-ops DBs could dedup-miss on each DB and create a duplicate run
   (the per-DB unique index can't enforce cross-DB uniqueness). Serialize such
   triggers (global scope, or scope-absent, while split is active) through the
   existing Redis idempotency claim, resolve the winner by id across both DBs, and
   reacquire the claim on the expired/failed clear-and-recreate path. run/attempt
   scope embed the run id in their hash and never contend.
@d-cs
d-cs force-pushed the fix/wait-until-idempotency-retry-rewait branch from bf4ee4f to e4c9b5c Compare July 18, 2026 16:26
@d-cs d-cs changed the title fix(run-engine,webapp): read-your-writes primary routing for run-ops replica-lag reads fix: read-your-writes + global-scope idempotency correctness under the run-ops split Jul 18, 2026
Dead since #4272 inlined per-write residency routing at each call site — the
private helper had zero callers (only doc-comments referenced it by name),
tripping eslint no-unused-private-class-members. Remove it and reword the
comments that referenced it.
@d-cs
d-cs marked this pull request as ready for review July 18, 2026 16:37

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the read-your-writes routing and the global-scope idempotency claim, plus a completeness sweep of run/waitpoint/batch/snapshot read-after-write paths across the engine and webapp. Core routing looks correct and complete — no read that must observe its own write still reads the replica, and the readOnlyPrisma → prisma change in runAttemptSystem is a genuine correctness fix. Four inline notes: two edges worth tightening (both re-open the duplicate/stale-read class this PR closes) and two minors. None a blocker.

(Note: the third inline note is attached as a file-level comment on internal-packages/run-store/src/runOpsStore.ts because its target — the findRunOnPrimary() classifiable-id path around lines 811-821 — is unchanged code outside the PR's diff hunks.)

Comment thread apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts Outdated
Comment thread apps/webapp/app/routes/realtime.v1.batches.$batchId.ts
Comment thread internal-packages/run-store/src/runOpsStore.ts
Comment thread apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts Outdated
@d-cs
d-cs force-pushed the fix/wait-until-idempotency-retry-rewait branch from 1c6e6ff to 2941d71 Compare July 19, 2026 14:06

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 new potential issues.

Open in Devin Review

Comment thread apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts Outdated
Comment thread apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts
@d-cs
d-cs force-pushed the fix/wait-until-idempotency-retry-rewait branch from 2941d71 to 6ae9b8a Compare July 19, 2026 14:13

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Open in Devin Review

Comment thread internal-packages/run-store/src/runOpsStore.ts
…me batch hardening

Production follow-ups from the CodeRabbit/Claude/Devin review. Caller-driven guard tests are in the
stacked tests PR; the two test edits here are coupled to the production change (the publishClaim
signature change invalidates a main assertion; the fan-out removal obsoletes a main test).

- Claim TTL floor (TRIGGER_MOLLIFIER_CLAIM_MIN_TTL_SECONDS, default 5) independent of the customer key
  TTL, so a short key TTL can't expire the claim mid-pipeline and let a loser re-claim.
- publishClaim returns the buffer CAS result; the trigger success path detects + logs a no-op'd publish.
- reacquireClearedGlobalWinner fails closed with a retryable 503 (exhaustion / unfindable winner); the
  expired/failed clear-and-recreate path routes through it too (Devin), so that recreate is serialised.
- Realtime batch route re-reads the owning primary on a replica miss (backend-agnostic; closes the
  Electric ShapeStream permanent-404 for self-hosters).
- Remove the classifiable-id cross-store fan-out from findRun: a run's id-shape fixes its residency for
  life, so the single-store read is correct and the fan-out was dead code.
@d-cs
d-cs force-pushed the fix/wait-until-idempotency-retry-rewait branch from 568b3bf to 1c625e1 Compare July 19, 2026 15:10
@d-cs
d-cs merged commit ae96b6c into main Jul 19, 2026
36 checks passed
@d-cs
d-cs deleted the fix/wait-until-idempotency-retry-rewait branch July 19, 2026 16:57
d-cs added a commit that referenced this pull request Jul 19, 2026
… (#4285)

## Stacked on #4284 — tests only

This PR contains **only the tests** that guard the production fixes in
#4284 (its base). Review #4284 first; this branch adds no production
code.

## What

Caller-driven replica-lag and idempotency guards for every fixed site:
- Each guard **drives the real exported caller** (route loader/action,
presenter `.call()`, service, or engine method) against a **real
Postgres** with the owning replica frozen via the shared
`laggingReplica` testcontainer primitive — never a store-seam
reimplementation.
- For a **fixed** site the guard goes **RED when the production change
is reverted**; for a **tolerated read-view** site it's a caller-driven
**GREEN** proof the miss self-heals (returns null/empty, no mutation,
row live on primary).
- The **global-scope idempotency** guard drives the real dedup + claim
path through a **real `MollifierBuffer` over a Redis testcontainer**
(real SETNX/poll/publish), and covers the cross-DB **andWait** waitpoint
wiring and the **expired/failed clear-and-recreate** reacquire cases.

Run with `vitest --no-file-parallelism` (testcontainers). Verified
GREEN, and revert→RED verified per fixed site.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants