Skip to content

fix(run-store,webapp): correct split-database read routing, write residency, and batches list ordering#4272

Merged
d-cs merged 13 commits into
mainfrom
feature/tri-12163-perfrun-store-route-run-keyed-waitpointsnapshot-reads
Jul 17, 2026
Merged

fix(run-store,webapp): correct split-database read routing, write residency, and batches list ordering#4272
d-cs merged 13 commits into
mainfrom
feature/tri-12163-perfrun-store-route-run-keyed-waitpointsnapshot-reads

Conversation

@d-cs

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

Copy link
Copy Markdown
Collaborator

Summary

Correctness and performance fixes for deployments that split run data across more than one database. Single-database / self-hosted deployments are unaffected (they collapse to a single read/write path).

  • Batches list (dashboard): for some organizations the Batches list could hide older batches or show them out of order. It now orders and paginates by creation time (with the id as a stable tiebreak), so every batch appears exactly once, newest first. The pagination cursor format changes; older in-flight cursors simply restart from the first page.
  • Reads: waitpoint and snapshot lookups that are keyed by a single run now read only the database that holds that run instead of querying both, removing redundant queries on hot paths (unblock, snapshot reads).
  • Writes: environment-scoped writes with no owning run (standalone wait tokens, waitpoint tags, idempotency-key resets) now land in the same database as that environment's runs, rather than defaulting to the other one. An idempotency-key reset also falls back to the other database when it matches nothing, so a reset still clears the key wherever the run actually lives.

Notes

Verified end-to-end against multi-database setups: run-keyed reads and env-scoped writes land on the correct database with no cross-database writes, and the batches list surfaces every batch in creation order. New tests cover the batches ordering/reachability and the write-residency routing.

…ads by run id

In split mode these reads fanned out to both run-ops databases on every call.
Route them by the run id already in scope so the non-owning database is no
longer queried; only genuinely cross-tree waitpoints still need both.
@changeset-bot

changeset-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6e9ab5c

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 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 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: a82a1ac7-e83b-489c-ab9e-86d82b48260f

📥 Commits

Reviewing files that changed from the base of the PR and between 082f31f and 70b2f86.

📒 Files selected for processing (1)
  • apps/webapp/test/batchListPresenter.readroute.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/webapp/test/batchListPresenter.readroute.test.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout. (18)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (12, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (10, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (11, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (9, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (3, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (4, 12)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: internal / 🧪 Unit Tests: Internal
  • GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp
  • GitHub Check: code-quality / code-quality
  • GitHub Check: 🛡️ E2E Auth Tests (full)
  • GitHub Check: Analyze (javascript-typescript)

Walkthrough

The change adds run-scoped waitpoint and snapshot routing with presence-aware pending counts and partitioned fallback reads. Standalone waitpoints, tags, and idempotency resets now support explicit residency routing. Batch listing uses merged split-store reads with createdAt/id composite keyset pagination. Tests cover routing, fallback, residency precedence, transaction handling, and pagination across residency boundaries.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description misses the required template sections, including Closes #, checklist, Testing, Changelog, and Screenshots. Rewrite the PR description to match the repository template and add Closes #, checklist items, Testing steps, Changelog, and Screenshots sections.
Docstring Coverage ⚠️ Warning Docstring coverage is 70.00% 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 main split-database routing, residency, and batch ordering changes.
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 feature/tri-12163-perfrun-store-route-run-keyed-waitpointsnapshot-reads

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

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

trigger.dev

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

@trigger.dev/core

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

@trigger.dev/python

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

@trigger.dev/react-hooks

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

@trigger.dev/redis-worker

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

@trigger.dev/rsc

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

@trigger.dev/schema-to-json

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

@trigger.dev/sdk

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

commit: 555be99

d-cs added 5 commits July 16, 2026 20:15
…ns on the run-ops DB by env mint kind

A token created with no owning run (wait.createToken) mints a cuid id and routed to the legacy
database by id-shape, so a fully-minted-new environment wrote its tokens to the draining legacy
database instead of the run-ops database. Standalone tokens now read the env mint kind and pin to
the run-ops database; co-located waitpoints still inherit their owning run's residency.
…y-key resets to the run-ops DB when the env mints run-ops ids

Two env-scoped writes with no owning run to route by defaulted to the legacy database in a
fully-minted-new environment: waitpoint tags (createWaitpointTag minted no id, so id-shape sent
them to legacy) and idempotency-key resets (clearIdempotencyKey by predicate fanned out to both
databases). Both now read the env mint kind and pin to the run-ops database, so they no longer
write to the draining legacy database; a predicate reset with no mint signal still fans out.
…the new DB matches nothing

Routing a predicate reset to the new database ONLY broke resets during the rollout window: a key
minted before an org flipped still lives on a legacy-resident run (idempotency TTL up to 30 days),
so a new-only reset matched zero rows, returned 404, and left the stale key deduping child triggers.
The reset now checks the new database first and falls back to legacy when nothing matched, so it
clears the key wherever the run lives; a fully-drained env still never touches legacy.
… some orgs

The dashboard Batches list ordered and paginated by batch id, but batch id no longer reflects
creation order for every batch, so some organizations could see older batches missing from the
list or sorted above newer ones. The list now orders and paginates by creation time (with id as a
stable tiebreak) and always reads both backing stores, so every batch appears exactly once,
newest first.
@d-cs d-cs changed the title perf(run-store,run-engine): route run-keyed waitpoint and snapshot reads by run id fix(run-store,webapp): correct split-database read routing, write residency, and batches list ordering Jul 17, 2026
coderabbitai[bot]

This comment was marked as resolved.

d-cs added 3 commits July 17, 2026 13:21
… loaded on the env

Guards against a missing environment.organization so the reset no longer crashes, falling back to
the two-store reset in that case. The authenticated API path always loads the org, so behavior is
unchanged in production.
…id date

A malformed URL cursor could pass the finite-number check but still produce an invalid Date; it now
falls back to the first page instead of reaching the query with an invalid date.
…edup-overlap regression, fix a routing comment
@d-cs

d-cs commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Addressing the three points:

1. Refill-after-dedup (BatchListPresenter#scanBatchTaskRun). I worked through this and the current merge is correct without a refill loop. Each store scan returns its top pageSize + 1 by (createdAt, id) after the cursor, and the union of both stores' top-(pageSize+1) always contains the global top-(pageSize+1): any global row has at most pageSize rows above it, hence at most pageSize from its own store, so it is within that store's LIMIT. De-dup only removes a duplicate id (a batch copied to both DBs); the surviving copy stays, so it can't drop a row the page needs. The merged-unique count can fall below pageSize + 1 only when BOTH stores return fewer than pageSize + 1 (both exhausted), where hasMore = false is correct; when either store hits its LIMIT, that store alone contributes pageSize + 1 distinct rows so the page fills. And the next page re-queries both stores from the cursor, so nothing is skipped across pagination. I added an overlap-heavy regression (three batches duplicated on both stores + two legacy-only) that walks every page and asserts each batch appears exactly once, newest-first.

2. Assert the complete composite cursor. Done. Both call sites now assert the exact ${createdAt.getTime()}_${id} value instead of endsWith.

3. Routing comment in runOpsStore. Corrected to match the implementation: residency hint first, then a minted id-shape, then fall back to legacy.

4. The decodeBatchCursor invalid-Date catch is fixed and that thread is resolved.

coderabbitai[bot]

This comment was marked as resolved.

Use one createdAt per duplicated batch so both DB copies match exactly; two Date.now() calls could
drift by a millisecond and the older copy would re-surface across pages under the createdAt keyset.
@d-cs
d-cs marked this pull request as ready for review July 17, 2026 15:05
@d-cs
d-cs enabled auto-merge (squash) July 17, 2026 15:11

@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 internal-packages/run-engine/src/engine/systems/waitpointSystem.ts
Comment thread apps/webapp/app/presenters/v3/BatchListPresenter.server.ts
@d-cs
d-cs disabled auto-merge July 17, 2026 15:14
@d-cs
d-cs merged commit 8219721 into main Jul 17, 2026
40 checks passed
@d-cs
d-cs deleted the feature/tri-12163-perfrun-store-route-run-keyed-waitpointsnapshot-reads branch July 17, 2026 15:26
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