feat: route new task runs to a parallel task_run_v2 table#4000
Conversation
Add an isomorphic generateKsuid() and an isKsuidId() format check to the run-id scheme, so a run's id can encode which table it belongs to. Additive groundwork: nothing mints KSUIDs yet, and generate() (cuid) is unchanged.
Add task_run_v2 as a scalar clone of TaskRun with no foreign-key constraints, plus a (createdAt, id) index for keyset pagination. Unused for now; new runs are routed to it by id format in a later change.
Drop the 14 child-table foreign keys that referenced TaskRun.id so a child row can reference a run in either the legacy or the new run table by plain scalar. Run integrity moves to app code, symmetric with TaskRun's already dropped outgoing foreign keys. Relations stay in the Prisma schema.
Give TaskRunV2 the same relation surface as TaskRun (belongs-to plus child collections, with child relations sharing the existing scalar fields) so run reads through the store can include relations regardless of table. No DB foreign keys: stripped in production migrations and in the test harness.
Production drops the foreign keys on and referencing the run tables, but the test harness builds via prisma db push, which recreates them from the schema relations. Drop them after the push so test databases match production and a run can live in either run table.
Select the TaskRun or task_run_v2 table per operation from the run id's format (KSUID routes to v2, anything else to legacy) via a runModel helper, so a run is read and written in its own table. Batch and predicate-keyed operations span both tables. Behavior-preserving for legacy runs.
findRuns now queries both TaskRun and task_run_v2 and merges the two ordered streams into one result. Ordered, limited reads require a time-based key (createdAt) because cuid and ksuid ids do not sort into a shared range, so id alone cannot order the union.
runsBackfiller paginates on a (createdAt, id) keyset instead of id alone. The ClickHouse runs list restores ClickHouse ranking in memory after hydrating rows by id, since a single SQL order cannot span the two tables.
findRun and findRunOrThrow route by the id/friendlyId in the predicate. A lookup that carries neither (the idempotency-key dedup, or an "are there any runs in this environment" check) previously defaulted to the legacy table and would miss a match that lives in task_run_v2. Such predicates now query both tables in parallel and return the first match, so a reused idempotency key is found wherever its run lives and no duplicate is created.
Adds a per-org runTableV2 feature flag, read in memory at the single run-id mint site in the trigger path. When on, the org mints a KSUID id for new runs (routing them to task_run_v2); off, the default, keeps minting legacy ids. The read is a pure lookup on the org featureFlags already loaded at auth, so the trigger path adds no query. RunStore routes purely by id format and never sees this flag.
…g publications LogicalReplicationClient gains an optional additionalTables option. These are published alongside the primary table in the same publication, and their WAL events stream through the same data handler. When the publication already exists, missing tables are added via ALTER PUBLICATION ADD TABLE (online, slot-preserving) instead of erroring, so a publication can gain a table without a drop and recreate.
runsReplicationService co-publishes task_run_v2 alongside TaskRun. It is a column-identical clone, so its WAL rows flow through the same transform into the same ClickHouse table, keeping the mirror complete once orgs cut over to v2 run ids. task_run_v2 needs REPLICA IDENTITY FULL, applied the same out-of-band way as TaskRun, so update and delete events carry the old row.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal-packages/run-store/src/PostgresRunStore.ts (1)
835-840:⚠️ Potential issue | 🟠 Major | ⚡ Quick win
skipis applied per-table, breaking cross-table pagination semantics.The method accepts
skipbut applies it to each table'sfindManyindividually. When both tables have data,skip: NskipsNrows from each table rather thanNfrom the combined sorted result.For example,
{ skip: 10, take: 5, orderBy: { createdAt: 'desc' } }would:
- Skip 10 from
TaskRun, take 5- Skip 10 from
task_run_v2, take 5- Merge to 5 rows
But the caller expects: merge all, sort, skip 10 from combined, take 5.
The code explicitly rejects
cursorwith a helpful error because it "cannot address two tables" —skiphas the same semantic problem. Either rejectskipwith a similar error (like cursor), or omitskipfrom the per-table queries and apply it post-merge (expensive for large skips).Suggested fix: reject skip on cross-table reads
if (ordered.length > 0 && args.take !== undefined) { if (args.cursor !== undefined) { throw new Error( "RunStore.findRuns: a Prisma `cursor` cannot address two tables on an ordered+limited read. " + "Use a where-based keyset (e.g. `where: { createdAt: { lt: X } }`) instead." ); } + if (args.skip !== undefined && args.skip > 0) { + throw new Error( + "RunStore.findRuns: `skip` cannot paginate correctly across two tables on an ordered+limited read. " + + "Use a where-based keyset (e.g. `where: { createdAt: { lt: X } }`) instead." + ); + }Also applies to: 880-880, 897-905
🧹 Nitpick comments (2)
internal-packages/replication/src/client.ts (1)
463-475: 💤 Low valueStatic analysis flags SQL injection — acceptable given controlled inputs.
The static analysis tools correctly identify string interpolation in SQL, but the threat model here is low:
publicationNameand table names come from constructor options set by developers (hardcoded strings like"task_runs_to_clickhouse_v1_publication"and["task_run_v2"])- PostgreSQL DDL statements don't support parameterized queries for identifiers — interpolation is the standard approach
- This is internal infrastructure code, not API-facing
If you want defense-in-depth, you could validate that identifier names match a safe pattern (e.g.,
^[a-zA-Z_][a-zA-Z0-9_]*$) at construction time. However, given the controlled nature of the call sites, this is optional.Also applies to: 514-518, 542-546
Source: Linters/SAST tools
apps/webapp/test/runsReplicationService.taskRunV2.test.ts (1)
16-23: ⚡ Quick winAdd an UPDATE/DELETE assertion for
task_run_v2replication.This test validates INSERT replication, but the new
REPLICA IDENTITY FULLsetup is mainly needed for UPDATE/DELETE old-row transforms. Adding one update/delete check would protect that contract directly.Also applies to: 73-123
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: afe68b46-a4dd-4596-8812-890b61bb3367
📒 Files selected for processing (18)
apps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsBackfiller.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/test/runTableV2.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsapps/webapp/test/utils/replicationUtils.tsinternal-packages/database/prisma/migrations/20260616151544_create_task_run_v2/migration.sqlinternal-packages/database/prisma/migrations/20260619120042_drop_taskrun_incoming_fks/migration.sqlinternal-packages/database/prisma/schema.prismainternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.tsinternal-packages/testcontainers/src/utils.tspackages/core/src/v3/isomorphic/friendlyId.test.tspackages/core/src/v3/isomorphic/friendlyId.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (13)
internal-packages/database/**/prisma/migrations/*/*.sql
📄 CodeRabbit inference engine (internal-packages/database/CLAUDE.md)
internal-packages/database/**/prisma/migrations/*/*.sql: Clean up generated Prisma migrations by removing extraneous lines for junction tables (_BackgroundWorkerToBackgroundWorkerFile,_BackgroundWorkerToTaskQueue,_TaskRunToTaskRunTag,_WaitpointRunConnections,_completedWaitpoints) and indexes (SecretStore_key_idx, variousTaskRunindexes) unless explicitly added
When adding indexes to existing tables, useCREATE INDEX CONCURRENTLY IF NOT EXISTSto avoid table locks in production, and place each concurrent index in its own separate migration file
Indexes on newly created tables can useCREATE INDEXwithout CONCURRENTLY and can be combined in the same migration file as theCREATE TABLEstatement
When adding an index on a new column in an existing table, use two separate migrations: first forALTER TABLE ... ADD COLUMN IF NOT EXISTS ..., then forCREATE INDEX CONCURRENTLY IF NOT EXISTS ...in its own file
Files:
internal-packages/database/prisma/migrations/20260619120042_drop_taskrun_incoming_fks/migration.sqlinternal-packages/database/prisma/migrations/20260616151544_create_task_run_v2/migration.sql
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{ts,tsx}: Use types over interfaces for TypeScript
Avoid using enums; prefer string unions or const objects insteadImport from
@trigger.dev/sdkwhen writing Trigger.dev tasks. Never use@trigger.dev/sdk/v3or deprecatedclient.defineJob
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
{packages/core,apps/webapp}/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use zod for validation in packages/core and apps/webapp
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsapps/webapp/app/services/runsBackfiller.server.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use function declarations instead of default exports
**/*.{ts,tsx,js,jsx}: Prefer static imports over dynamic imports. Only use dynamicimport()when circular dependencies cannot be resolved, code splitting is needed for performance, or the module must be loaded conditionally at runtime
Import subpaths only frompackages/core(@trigger.dev/core), never import from the root
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
**/*.{test,spec}.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use vitest for all tests in the Trigger.dev repository
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsinternal-packages/run-store/src/PostgresRunStore.test.ts
**/*.ts
📄 CodeRabbit inference engine (.cursor/rules/otel-metrics.mdc)
**/*.ts: When creating or editing OTEL metrics (counters, histograms, gauges), ensure metric attributes have low cardinality by using only enums, booleans, bounded error codes, or bounded shard IDs
Do not use high-cardinality attributes in OTEL metrics such as UUIDs/IDs (envId, userId, runId, projectId, organizationId), unbounded integers (itemCount, batchSize, retryCount), timestamps (createdAt, startTime), or free-form strings (errorMessage, taskName, queueName)
When exporting OTEL metrics via OTLP to Prometheus, be aware that the exporter automatically adds unit suffixes to metric names (e.g., 'my_duration_ms' becomes 'my_duration_ms_milliseconds', 'my_counter' becomes 'my_counter_total'). Account for these transformations when writing Grafana dashboards or Prometheus queries
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
apps/webapp/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/webapp.mdc)
apps/webapp/**/*.{ts,tsx}: Access environment variables through theenvexport ofenv.server.tsinstead of directly accessingprocess.env
Use subpath exports from@trigger.dev/corepackage instead of importing from the root@trigger.dev/corepathUse named constants for sentinel/placeholder values (e.g.
const UNSET_VALUE = '__unset__') instead of raw string literals scattered across comparisons
Files:
apps/webapp/test/runTableV2.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsapps/webapp/app/services/runsBackfiller.server.ts
apps/webapp/**/*.test.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/webapp.mdc)
Do not import
env.server.tsdirectly or indirectly into test files; instead pass environment-dependent values through options/parameters to make code testableFor testable code, never import
env.server.tsin test files. Pass configuration as options instead (e.g.,realtimeClient.server.tstakes config as constructor arg,realtimeClientGlobal.server.tscreates singleton with env config)
Files:
apps/webapp/test/runTableV2.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.ts
**/*.test.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.test.{ts,tsx}: Never mock anything in tests - use testcontainers instead
Test files should be placed next to source files (e.g.,MyService.ts->MyService.test.ts)
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsinternal-packages/run-store/src/PostgresRunStore.test.ts
**/*.{js,ts,tsx,jsx,css,json,md}
📄 CodeRabbit inference engine (AGENTS.md)
Use Prettier for code formatting and run
pnpm run formatbefore committing
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
**/*.test.{js,ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.test.{js,ts,tsx}: Test files should live beside the files under test and use descriptivedescribeanditblocks
Use vitest for unit testing
Tests should avoid mocks or stubs and use helpers from@internal/testcontainerswhen Redis or Postgres are needed
Files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsinternal-packages/run-store/src/PostgresRunStore.test.ts
packages/core/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (packages/core/CLAUDE.md)
Never import the root package (
@trigger.dev/core). Always use subpath imports such as@trigger.dev/core/v3,@trigger.dev/core/v3/utils,@trigger.dev/core/logger, or@trigger.dev/core/schemas
Files:
packages/core/src/v3/isomorphic/friendlyId.test.tspackages/core/src/v3/isomorphic/friendlyId.ts
apps/webapp/**/*.server.ts
📄 CodeRabbit inference engine (apps/webapp/CLAUDE.md)
apps/webapp/**/*.server.ts: Never userequest.signalfor detecting client disconnects. UsegetRequestAbortSignal()fromapp/services/httpAsyncStorage.server.tsinstead, which is wired directly to Expressres.on('close')and fires reliably
Access environment variables viaenvexport fromapp/env.server.ts. Never useprocess.envdirectly
Always usefindFirstinstead offindUniquein Prisma queries.findUniquehas an implicit DataLoader that batches concurrent calls and has active bugs even in Prisma 6.x (uppercase UUIDs returning null, composite key SQL correctness issues, 5-10x worse performance).findFirstis never batched and avoids this entire class of issues
Files:
apps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/app/services/runsBackfiller.server.ts
🧠 Learnings (25)
📚 Learning: 2026-02-03T18:48:31.790Z
Learnt from: 0ski
Repo: triggerdotdev/trigger.dev PR: 2994
File: internal-packages/database/prisma/migrations/20260129162810_add_integration_deployment/migration.sql:14-18
Timestamp: 2026-02-03T18:48:31.790Z
Learning: For Prisma migrations targeting PostgreSQL: - When adding indexes to existing tables, create the index in a separate migration file and include CONCURRENTLY to avoid locking the table. - For indexes on newly created tables (in CREATE TABLE statements), you can create the index in the same migration file without CONCURRENTLY. This reduces rollout complexity for new objects while protecting uptime for existing structures.
Applied to files:
internal-packages/database/prisma/migrations/20260619120042_drop_taskrun_incoming_fks/migration.sqlinternal-packages/database/prisma/migrations/20260616151544_create_task_run_v2/migration.sql
📚 Learning: 2026-03-22T13:49:20.068Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3244
File: internal-packages/database/prisma/migrations/20260318114244_add_prompt_friendly_id/migration.sql:5-5
Timestamp: 2026-03-22T13:49:20.068Z
Learning: For Prisma migration SQL files under `internal-packages/database/prisma/migrations/`, it is acceptable to create indexes with `CREATE INDEX` / `CREATE UNIQUE INDEX` (i.e., without `CONCURRENTLY`) when the parent table is introduced in the same PR and has no existing production rows yet. Only require `CREATE INDEX CONCURRENTLY` (or otherwise account for existing production data/locks) when the table already exists in production with data.
Applied to files:
internal-packages/database/prisma/migrations/20260619120042_drop_taskrun_incoming_fks/migration.sqlinternal-packages/database/prisma/migrations/20260616151544_create_task_run_v2/migration.sql
📚 Learning: 2026-03-22T13:26:12.060Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3244
File: apps/webapp/app/components/code/TextEditor.tsx:81-86
Timestamp: 2026-03-22T13:26:12.060Z
Learning: In the triggerdotdev/trigger.dev codebase, do not flag `navigator.clipboard.writeText(...)` calls for `missing-await`/`unhandled-promise` issues. These clipboard writes are intentionally invoked without `await` and without `catch` handlers across the project; keep that behavior consistent when reviewing TypeScript/TSX files (e.g., usages like in `apps/webapp/app/components/code/TextEditor.tsx`).
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-03-22T19:24:14.403Z
Learnt from: matt-aitken
Repo: triggerdotdev/trigger.dev PR: 3187
File: apps/webapp/app/v3/services/alerts/deliverErrorGroupAlert.server.ts:200-204
Timestamp: 2026-03-22T19:24:14.403Z
Learning: In the triggerdotdev/trigger.dev codebase, webhook URLs are not expected to contain embedded credentials/secrets (e.g., fields like `ProjectAlertWebhookProperties` should only hold credential-free webhook endpoints). During code review, if you see logging or inclusion of raw webhook URLs in error messages, do not automatically treat it as a credential-leak/secrets-in-logs issue by default—first verify the URL does not contain embedded credentials (for example, no username/password in the URL, no obvious secret/token query params or fragments). If the URL is credential-free per this project’s conventions, allow the logging.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-05-18T08:21:27.694Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3632
File: apps/webapp/sentry.server.ts:4-21
Timestamp: 2026-05-18T08:21:27.694Z
Learning: When handling Prisma error P1001 ("Can't reach database server") in TypeScript, don’t assume a single error shape. Prisma can surface P1001 via two different error classes/fields: `PrismaClientKnownRequestError` exposes it as `err.code === "P1001"` (common during mid-query connection drops), while `PrismaClientInitializationError` exposes it as `err.errorCode === "P1001"` (common on client startup failure). Therefore, predicates should use `err.code === "P1001" || err.errorCode === "P1001"`. Do not flag `err.code === "P1001"` as “unreachable/never matches,” as it is expected in production.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-05-18T08:21:27.694Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3632
File: apps/webapp/sentry.server.ts:4-21
Timestamp: 2026-05-18T08:21:27.694Z
Learning: When handling Prisma errors for P1001 ("Can't reach database server"), do not assume it only appears under a single property name. Prisma may surface P1001 via either `PrismaClientKnownRequestError` (`err.code === "P1001"`, e.g., mid-query connection drops) or `PrismaClientInitializationError` (`err.errorCode === "P1001"`, e.g., client startup connection failure). To reliably detect the condition, check `err.code === "P1001" || err.errorCode === "P1001"`, and avoid review rules that would incorrectly flag `err.code === "P1001"` as unreachable/never-matching.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-06-13T19:53:13.759Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3937
File: packages/trigger-sdk/skills/realtime-and-frontend/SKILL.md:258-260
Timestamp: 2026-06-13T19:53:13.759Z
Learning: When reviewing code that uses `trigger.dev/react-hooks`’s `useRealtimeRun`, preserve the call signature where the first argument is the full realtime handle object (not `handle.id`). This is intentional to maintain type-safety and is consistent with the official docs; do not suggest changing the first argument from the handle object to `handle.id`.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-06-17T17:13:49.929Z
Learnt from: matt-aitken
Repo: triggerdotdev/trigger.dev PR: 3948
File: apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.bulk-actions.$bulkActionParam/route.tsx:48-62
Timestamp: 2026-06-17T17:13:49.929Z
Learning: In triggerdotdev/trigger.dev, within `dashboardLoader`/`dashboardAction` (or similar context resolver code) whenever you resolve an organization ID from an organization slug for RBAC/enterprise authorization scope, always read from the primary Prisma client (`prisma`), not `$replica`. Using `$replica` can hit replica-lag and cause the RBAC lookup/authorization to run without the correct org scope (bypassing intended role enforcement). Implement the slug→org lookup with `prisma.organization.findFirst(...)` (or equivalent primary-client query) and add an inline comment documenting why the primary client is required (replica lag could lead to unscoped RBAC checks).
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-05-07T12:25:18.271Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3531
File: apps/webapp/test/sentryTraceContext.server.test.ts:9-47
Timestamp: 2026-05-07T12:25:18.271Z
Learning: In the triggerdotdev/trigger.dev webapp test suite, it is acceptable to leave `createInMemoryTracing()` calls that register a global `NodeTracerProvider` without `afterEach`/`afterAll` teardown. Do not flag this as a test-ordering risk when the code follows the established pattern used across webapp tests (e.g., replication service/benchmark/backfiller tests). This is considered safe because `trace.getActiveSpan()` when called outside a `context.with(...)` block reads `AsyncLocalStorage.getStore()` (undefined when no `run()` scope exists), so it falls back to `ROOT_CONTEXT` with no attached span—regardless of which provider is registered.
Applied to files:
apps/webapp/test/runTableV2.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/test/runsReplicationService.taskRunV2.test.ts
📚 Learning: 2026-05-28T20:02:10.647Z
Learnt from: myftija
Repo: triggerdotdev/trigger.dev PR: 3772
File: apps/webapp/test/findOrCreateBackgroundWorker.test.ts:1-1
Timestamp: 2026-05-28T20:02:10.647Z
Learning: In the triggerdotdev/trigger.dev monorepo, for the `apps/webapp` package use the established convention of storing Vitest tests (unit, integration, and e2e) under `apps/webapp/test/` rather than colocating them next to source files. Do not flag files located in `apps/webapp/test/` as violating any rule that says to colocate tests with source.
Applied to files:
apps/webapp/test/runTableV2.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/test/runsReplicationService.taskRunV2.test.ts
📚 Learning: 2026-05-12T21:04:05.815Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3542
File: apps/webapp/app/components/sessions/v1/SessionStatus.tsx:1-3
Timestamp: 2026-05-12T21:04:05.815Z
Learning: In this Remix + TypeScript codebase, do not flag a server/client boundary violation when a file imports only types from a module matching `*.server`.
Specifically, it’s safe to import types using `import type { Foo } from "*.server"` or `import { type Foo } from "*.server"` because TypeScript erases type-only imports at compile time and they emit no JavaScript, so they won’t cross the Remix server/client bundle boundary.
Only raise the boundary concern for value imports (e.g., `import { Foo }` without `type`, or `import Foo`), since those produce JavaScript output.
Applied to files:
apps/webapp/test/runTableV2.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsapps/webapp/app/services/runsBackfiller.server.ts
📚 Learning: 2026-05-18T14:40:02.173Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3658
File: packages/core/src/v3/realtimeStreams/manager.test.ts:1-147
Timestamp: 2026-05-18T14:40:02.173Z
Learning: In the triggerdotdev/trigger.dev repo, the policy “Never mock anything — use testcontainers instead” should only be enforced for integration tests that interact with real external services (e.g., Redis, Postgres) via actual infrastructure. For unit tests that exercise pure in-memory logic (e.g., cache semantics) it is OK to stub collaborators such as `ApiClient` using Vitest (`vi.fn()`) to assert call counts or control behavior. Do not flag `vi.fn()`-based `ApiClient` stubs in unit tests as violations of the testcontainers policy.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsinternal-packages/run-store/src/PostgresRunStore.test.ts
📚 Learning: 2026-06-04T18:16:35.386Z
Learnt from: nicktrn
Repo: triggerdotdev/trigger.dev PR: 3836
File: apps/supervisor/src/backpressure/backpressureMonitor.ts:3-5
Timestamp: 2026-06-04T18:16:35.386Z
Learning: When reviewing TypeScript in this repo, apply the rule “prefer type aliases over interfaces” only to data/object shapes and union/intersection type modeling. If an interface is being used as a behavioral contract for collaborators to implement (e.g., method-shape interfaces that define required behavior, such as `BackpressureLogger` / `BackpressureSignalSource` in `apps/supervisor/src/backpressure/backpressureMonitor.ts`), keep it as an `interface` and do not flag it as a type-alias-vs-interface violation.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-06-09T17:58:04.699Z
Learnt from: 0ski
Repo: triggerdotdev/trigger.dev PR: 3879
File: apps/webapp/app/models/vercelIntegration.server.ts:619-630
Timestamp: 2026-06-09T17:58:04.699Z
Learning: In this codebase, outbound raw `fetch` calls should typically rely on Node/undici’s default request timeout (about ~300s) rather than adding a per-call `AbortController` + `setTimeout` wrapper inside individual functions (e.g. in files like `apps/webapp/app/models/vercelIntegration.server.ts`). During code review, do not flag the absence of a per-call timeout on a single `fetch` as an issue; if per-call timeouts are needed, they should be implemented via a codebase-wide convention (e.g., a shared fetch wrapper or documented pattern) rather than ad-hoc per-function changes.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/utils/replicationUtils.tsapps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tspackages/core/src/v3/isomorphic/friendlyId.tsinternal-packages/testcontainers/src/utils.tsapps/webapp/app/services/runsBackfiller.server.tsinternal-packages/replication/src/client.tsinternal-packages/run-store/src/PostgresRunStore.test.tsinternal-packages/run-store/src/PostgresRunStore.ts
📚 Learning: 2026-06-16T09:19:47.637Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3960
File: apps/webapp/test/prismaInfrastructureErrorCapture.test.ts:0-0
Timestamp: 2026-06-16T09:19:47.637Z
Learning: In this repo’s Vitest setup, `vitest.config.ts` uses `globals: true`, so identifiers like `vi`, `describe`, `it`, and `expect` are available as globals in Vitest test files. During code review, do not flag missing `vi`/`describe`/`it`/`expect` imports as a runtime error or correctness issue when they’re used in `*.test.ts/tsx` or `*.spec.ts/tsx` files. Explicit imports are still preferred for consistency, but they’re not required for runtime behavior.
Applied to files:
apps/webapp/test/runTableV2.test.tspackages/core/src/v3/isomorphic/friendlyId.test.tsapps/webapp/test/runsReplicationService.taskRunV2.test.tsinternal-packages/run-store/src/PostgresRunStore.test.ts
📚 Learning: 2026-05-18T14:40:02.173Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3658
File: packages/core/src/v3/realtimeStreams/manager.test.ts:1-147
Timestamp: 2026-05-18T14:40:02.173Z
Learning: In this repo’s trigger.dev codebase, the “never mock — use testcontainers” guideline should only be applied to integration tests that talk to real external services (e.g., Redis, Postgres, S2). For unit tests that validate in-memory logic (e.g., deduplication/cache behavior in StandardRealtimeStreamsManager and similar module-boundary call counting), it is allowed to use Vitest mocks like `vi.fn()` and to stub/mock `ApiClient` objects to count calls or simulate in-process collaborators. Do not flag `vi.fn()`-based mocks as policy violations in these unit-test scenarios; reserve the rule for true external-service integration tests.
Applied to files:
packages/core/src/v3/isomorphic/friendlyId.test.ts
📚 Learning: 2026-03-29T19:16:28.864Z
Learnt from: nicktrn
Repo: triggerdotdev/trigger.dev PR: 3291
File: apps/webapp/app/v3/featureFlags.ts:53-65
Timestamp: 2026-03-29T19:16:28.864Z
Learning: When reviewing TypeScript code that uses Zod v3, treat `z.coerce.*()` schemas as their direct Zod type (e.g., `z.coerce.boolean()` returns a `ZodBoolean` with `_def.typeName === "ZodBoolean"`) rather than a `ZodEffects`. Only `.preprocess()`, `.refine()`/`.superRefine()`, and `.transform()` are expected to wrap schemas in `ZodEffects`. Therefore, in reviewers’ logic like `getFlagControlType`, do not flag/unblock failures that require unwrapping `ZodEffects` when the input schema is a `z.coerce.*` schema.
Applied to files:
apps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.ts
📚 Learning: 2026-06-09T16:27:26.195Z
Learnt from: myftija
Repo: triggerdotdev/trigger.dev PR: 3878
File: apps/webapp/app/v3/services/computeTemplateCreation.server.ts:0-0
Timestamp: 2026-06-09T16:27:26.195Z
Learning: When working in triggerdotdev/trigger.dev code related to worker-group/region default resolution (e.g., defaultWorkerInstanceGroupId handling used by getGlobalDefaultWorkerGroup, getDefaultWorkerGroupForProject, and RegionsPresenter), do NOT add org-level featureFlags overrides in only one resolution site. That can cause template creation routing/decisions to diverge from actual run routing. If org-level override of the default region/worker group is required, it must be centralized in getGlobalDefaultWorkerGroup so every resolution path remains aligned.
Applied to files:
apps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/v3/featureFlags.ts
📚 Learning: 2026-05-05T09:38:02.512Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3523
File: apps/webapp/app/routes/api.v3.batches.ts:178-181
Timestamp: 2026-05-05T09:38:02.512Z
Learning: When reviewing code that catches `ServiceValidationError` in `*.server.ts` files, do not blindly forward `error.status` to HTTP responses, because SVEs may be thrown with non-default statuses (e.g., 400/500) and forwarding them can cause client-visible behavioral regressions (e.g., surfacing 500s to clients). Prefer a safe default response status of `error.status ?? 422`, but only after confirming via the reachable call graph that the caught `ServiceValidationError` instances are expected to carry those non-default statuses; otherwise, normalize to `422` to avoid unexpected client-visible 5xx behavior.
Applied to files:
apps/webapp/app/v3/runTableV2.server.tsapps/webapp/app/runEngine/services/triggerTask.server.tsapps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/app/services/runsBackfiller.server.ts
📚 Learning: 2026-05-14T08:21:07.614Z
Learnt from: d-cs
Repo: triggerdotdev/trigger.dev PR: 3614
File: apps/webapp/app/v3/mollifier/mollifierGate.server.ts:48-52
Timestamp: 2026-05-14T08:21:07.614Z
Learning: When using Trigger.dev v3 feature flags in the webapp, prefer the existing per-org gating mechanism supported by `flag()` via the `overrides` argument. Pass `Organization.featureFlags` (from `environment.organization.featureFlags`) as the `overrides` value; overrides must take precedence over the global `featureFlag` row. Do not require schema changes or add an `orgId` field to `FlagsOptions` for per-org gating—use the overrides pattern consistently (e.g., in gate flows like `resolveOrgFlag` and any server code that threads `environment.organization.featureFlags` into the gate call).
Applied to files:
apps/webapp/app/v3/runTableV2.server.ts
📚 Learning: 2026-05-22T11:50:56.079Z
Learnt from: matt-aitken
Repo: triggerdotdev/trigger.dev PR: 3333
File: apps/webapp/app/runEngine/services/triggerFailedTask.server.ts:76-80
Timestamp: 2026-05-22T11:50:56.079Z
Learning: When reviewing changes related to org-scoped ClickHouse / org reassignment, treat ClickHouse as the source of truth for the affected read paths (e.g., run lists, span detail, logs) with no Postgres fallback. Reassigning an org from one ClickHouse cluster to another must be done by migrating that org’s existing ClickHouse data between clusters first—do not assume it’s sufficient to update only the OrganizationDataStore entry. If any implementation/change would make org reassignment possible, ensure the required migration design/implementation is included (work tracked under Linear TRI-9659, sub-issue of TRI-7994). During the initial rollout of org-scoped ClickHouse (PR `#3333`), no production org has an override configured, so the limitation is not yet reachable in production; don’t introduce production-dependent assumptions that rely on overrides being active.
Applied to files:
apps/webapp/app/runEngine/services/triggerTask.server.ts
📚 Learning: 2026-03-26T09:02:07.973Z
Learnt from: myftija
Repo: triggerdotdev/trigger.dev PR: 3274
File: apps/webapp/app/services/runsReplicationService.server.ts:922-924
Timestamp: 2026-03-26T09:02:07.973Z
Learning: When parsing Trigger.dev task run annotations in server-side services, keep `TaskRun.annotations` strictly conforming to the `RunAnnotations` schema from `trigger.dev/core/v3`. If the code already uses `RunAnnotations.safeParse` (e.g., in a `#parseAnnotations` helper), treat that as intentional/necessary for atomic, schema-accurate annotation handling. Do not recommend relaxing the annotation payload schema or using a permissive “passthrough” parse path, since the annotations are expected to be written atomically in one operation and should not contain partial/legacy payloads that would require a looser parser.
Applied to files:
apps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.tsapps/webapp/app/services/runsReplicationService.server.tsapps/webapp/app/services/runsBackfiller.server.ts
📚 Learning: 2026-04-20T14:50:16.440Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3417
File: apps/webapp/app/services/sessionsReplicationService.server.ts:224-231
Timestamp: 2026-04-20T14:50:16.440Z
Learning: In Trigger.dev’s replication services (e.g., sessionsReplicationService.server.ts and runsReplicationService.server.ts), the “acknowledge-before-flush” behavior is intentional. The `_latestCommitEndLsn` should be updated at Postgres commit time and acknowledged on a periodic interval (via methods like `#acknowledgeLatestTransaction`) without waiting for ClickHouse batch flush to complete. Reviewers should not flag this as a durability/ordering bug; it is an established project-wide at-least-once delivery trade-off used across both runs and sessions replication services.
Applied to files:
apps/webapp/app/services/runsReplicationService.server.ts
📚 Learning: 2026-04-20T15:08:49.959Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3417
File: apps/webapp/app/services/sessionsReplicationService.server.ts:204-215
Timestamp: 2026-04-20T15:08:49.959Z
Learning: For replication services in `apps/webapp/app/services/*ReplicationService.server.ts`, keep the `ConcurrentFlushScheduler` deduplication key shape consistent across the related services (e.g., sessions vs runs) by using the same `${item.event}_${item.session.id}` / `${item.event}_${item.run.id}` pattern. If the key format ever needs to change (such as keying only by session/run id), make the update in all related replication services together—never in just one—so deduplication behavior stays aligned across services.
Applied to files:
apps/webapp/app/services/runsReplicationService.server.ts
📚 Learning: 2026-04-30T21:28:35.705Z
Learnt from: samejr
Repo: triggerdotdev/trigger.dev PR: 3473
File: internal-packages/database/prisma/schema.prisma:59-60
Timestamp: 2026-04-30T21:28:35.705Z
Learning: When reviewing Prisma schema files in this repository, do not suggest using Prisma’s `@check` model/table-level attribute or any native Prisma schema syntax for CHECK constraints. Prisma does not implement CHECK constraints (see prisma/prisma#3388). If a CHECK constraint is required, add it only via raw SQL in a handwritten migration (e.g., `ALTER TABLE ... ADD CONSTRAINT ... CHECK (...)`).
Applied to files:
internal-packages/database/prisma/schema.prisma
🪛 ast-grep (0.43.0)
internal-packages/replication/src/client.ts
[error] 514-518: Avoid SQL injection
Context: this.client.query(
SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = '${this.options.publicationName}';
)
Note: [CWE-89].
(sql-injection-typescript)
🪛 OpenGrep (1.22.0)
internal-packages/replication/src/client.ts
[ERROR] 515-519: SQL query built via string concatenation or template literal passed to query()/execute(). Use parameterized queries instead.
(coderabbit.sql-injection.raw-query-concat-js)
[ERROR] 543-545: SQL query built via string concatenation or template literal passed to query()/execute(). Use parameterized queries instead.
(coderabbit.sql-injection.raw-query-concat-js)
🔇 Additional comments (27)
internal-packages/replication/src/client.ts (3)
26-33: LGTM!
418-425: LGTM!
527-551: Publication reconciliation logic handles race conditions correctly.The approach of treating
42710(duplicate_object) as a benign race is sound — multiple leader candidates could attemptADD TABLEconcurrently, and the first to succeed wins while others observe an already-present table. The error handling and logging are appropriate.apps/webapp/app/services/runsReplicationService.server.ts (1)
230-234: LGTM!internal-packages/database/prisma/migrations/20260616151544_create_task_run_v2/migration.sql (1)
1-122: LGTM!internal-packages/database/prisma/migrations/20260619120042_drop_taskrun_incoming_fks/migration.sql (1)
1-17: LGTM!internal-packages/database/prisma/schema.prisma (1)
315-402: LGTM!Also applies to: 425-488, 536-583, 673-727, 735-777, 1104-1338, 1440-1501, 1557-1633, 1647-1679, 1810-1827, 1830-1856, 1878-1927, 2115-2190, 2201-2224, 2293-2326, 2333-2365, 2611-2641, 2799-2829
packages/core/src/v3/isomorphic/friendlyId.ts (1)
14-90: LGTM!Also applies to: 150-158
packages/core/src/v3/isomorphic/friendlyId.test.ts (1)
1-99: LGTM!apps/webapp/app/v3/featureFlags.ts (1)
19-19: LGTM!Also applies to: 47-52
apps/webapp/app/v3/runTableV2.server.ts (1)
1-28: LGTM!apps/webapp/test/runTableV2.test.ts (1)
1-28: LGTM!apps/webapp/app/runEngine/services/triggerTask.server.ts (1)
28-28: LGTM!Also applies to: 155-165
apps/webapp/app/services/runsRepository/clickhouseRunsRepository.server.ts (1)
169-234: LGTM!apps/webapp/app/services/runsBackfiller.server.ts (2)
44-74: LGTM!
109-137: LGTM!internal-packages/run-store/src/PostgresRunStore.ts (9)
52-63: LGTM!The
runModelhelper cleanly encapsulates the routing decision based on ID format. The type castas unknown as typeof client.taskRunis appropriate sincetaskRunV2mirrorstaskRun's schema.
65-103: LGTM!
#routingKeyOfcorrectly extracts the routing discriminator fromwhereclauses, and#findFirstAcrossTablesimplements the documented "legacy preferred" semantics for non-id predicates.
105-186: LGTM!Create methods (
createRun,createCancelledRun,createFailedRun) correctly route viarunModel(client, params.data.id).
188-329: LGTM!Single-run update methods (
startAttempt,completeAttemptSuccess,recordRetryOutcome,requeueRun,recordBulkActionMembership,cancelRun) correctly route byrunId.
331-438: LGTM!
failRunPermanently,expireRunroute viarunModel.expireRunsBatchcorrectly partitions IDs by format and issues separate raw UPDATEs per table — handles the empty-array edge case and sums counts.
440-614: LGTM!Remaining update methods (
lockRunToWorker,parkPendingVersion,promotePendingVersionRuns,suspendForCheckpoint,resumeFromCheckpoint,rescheduleRun,enqueueDelayedRun,rewriteDebouncedRun) all route correctly viarunModel.
616-730: LGTM!
updateMetadata,clearIdempotencyKey,pushTags, andpushRealtimeStreamhandle routing correctly.clearIdempotencyKeyproperly handles all three branches:byId(single-table),byPredicate(both tables), andbyFriendlyIds(partitioned by format).
732-798: LGTM!
findRunandfindRunOrThrowcorrectly route to a single table whenid/friendlyIdis present, and fall back to#findFirstAcrossTablesfor non-id predicates. The throw-on-miss contract is enforced manually for the cross-table path.
862-888: LGTM!The cross-table merge implementation is well-designed:
- Explicit rejection of Prisma
cursorwith helpful error message- Requires time-based ordering key for total order correctness
- Automatic
idtiebreak addition for determinism- Projection augmentation to include sort keys, with post-merge stripping
- Bounded 2-way merge algorithm is O(take) and correct
Also applies to: 926-1138
apps/webapp/test/utils/replicationUtils.ts (1)
20-23: LGTM!internal-packages/run-store/src/PostgresRunStore.test.ts (1)
2-2: LGTM!Also applies to: 1777-2809
| await runsReplicationService.start(); | ||
|
|
There was a problem hiding this comment.
Always stop replication service in finally.
If any assertion throws before Line 122, the service keeps running and can interfere with later tests in the same worker.
Proposed fix
- await runsReplicationService.start();
-
- const organization = await prisma.organization.create({
+ await runsReplicationService.start();
+ try {
+ const organization = await prisma.organization.create({
data: { title: "test", slug: "test" },
- });
+ });
// ...existing test body...
-
- await runsReplicationService.stop();
+ } finally {
+ await runsReplicationService.stop();
+ }Also applies to: 122-123
| await setTimeout(1000); | ||
|
|
||
| const queryRuns = clickhouse.reader.query({ | ||
| name: "runs-replication", | ||
| query: "SELECT * FROM trigger_dev.task_runs_v2 WHERE run_id = {runId: String}", | ||
| schema: z.any(), | ||
| params: z.object({ runId: z.string() }), | ||
| }); | ||
|
|
||
| const [queryError, result] = await queryRuns({ runId: run.id }); | ||
|
|
There was a problem hiding this comment.
Replace fixed sleep with bounded polling to avoid flaky replication timing.
Line 96 uses a fixed setTimeout(1000) and then a single read. Replication lag variance can intermittently fail this test even when behavior is correct.
Proposed fix
- await setTimeout(1000);
-
const queryRuns = clickhouse.reader.query({
name: "runs-replication",
query: "SELECT * FROM trigger_dev.task_runs_v2 WHERE run_id = {runId: String}",
schema: z.any(),
params: z.object({ runId: z.string() }),
});
- const [queryError, result] = await queryRuns({ runId: run.id });
+ let queryError: unknown = null;
+ let result: unknown[] | undefined;
+ const deadline = Date.now() + 10_000;
+
+ do {
+ [queryError, result] = await queryRuns({ runId: run.id });
+ if (!queryError && result?.length === 1) break;
+ await setTimeout(200);
+ } while (Date.now() < deadline);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| await setTimeout(1000); | |
| const queryRuns = clickhouse.reader.query({ | |
| name: "runs-replication", | |
| query: "SELECT * FROM trigger_dev.task_runs_v2 WHERE run_id = {runId: String}", | |
| schema: z.any(), | |
| params: z.object({ runId: z.string() }), | |
| }); | |
| const [queryError, result] = await queryRuns({ runId: run.id }); | |
| const queryRuns = clickhouse.reader.query({ | |
| name: "runs-replication", | |
| query: "SELECT * FROM trigger_dev.task_runs_v2 WHERE run_id = {runId: String}", | |
| schema: z.any(), | |
| params: z.object({ runId: z.string() }), | |
| }); | |
| let queryError: unknown = null; | |
| let result: unknown[] | undefined; | |
| const deadline = Date.now() + 10_000; | |
| do { | |
| [queryError, result] = await queryRuns({ runId: run.id }); | |
| if (!queryError && result?.length === 1) break; | |
| await setTimeout(200); | |
| } while (Date.now() < deadline); |
| WHERE contype = 'f' | ||
| AND (confrelid IN ('"TaskRun"'::regclass, 'task_run_v2'::regclass) | ||
| OR conrelid IN ('"TaskRun"'::regclass, 'task_run_v2'::regclass)) | ||
| LOOP |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Current helper predicate in testcontainers utils ==="
rg -n "confrelid|conrelid|TaskRun|task_run_v2" internal-packages/testcontainers/src/utils.ts -C 2
echo
echo "=== Migration SQL that touches TaskRun/task_run_v2 constraints ==="
fd -p migration.sql internal-packages/database/prisma/migrations | while read -r f; do
if rg -n "TaskRun|task_run_v2|FOREIGN KEY|DROP CONSTRAINT|REFERENCES" "$f" >/dev/null; then
echo "---- $f ----"
rg -n "TaskRun|task_run_v2|FOREIGN KEY|DROP CONSTRAINT|REFERENCES" "$f"
fi
doneRepository: triggerdotdev/trigger.dev
Length of output: 50382
🏁 Script executed:
#!/bin/bash
# Look for explicit DROP CONSTRAINT statements involving TaskRun or task_run_v2
# particularly where these are the source table (not target)
echo "=== Searching for explicit DROP CONSTRAINT on TaskRun/task_run_v2 in migrations ==="
rg -n 'DROP CONSTRAINT.*"TaskRun"|DROP CONSTRAINT.*task_run_v2' internal-packages/database/prisma/migrations/
echo
echo "=== Searching for migration references to task_run_v2 created/modified ==="
rg -n 'task_run_v2' internal-packages/database/prisma/migrations/ | head -30
echo
echo "=== Looking for any migration that explicitly handles the TaskRun FK issue ==="
fd migration.sql internal-packages/database/prisma/migrations | xargs rg -l 'TaskRun.*runId|task_run_v2.*runId' | head -5 | while read f; do
echo "---- $f ----"
rg -n -C 3 'DROP|TaskRun|task_run_v2|runId' "$f" | head -20
doneRepository: triggerdotdev/trigger.dev
Length of output: 5820
Scope mismatch: testcontainers helper drops both incoming and outgoing TaskRun FKs, but production migrations drop only incoming.
The production migration 20260619120042_drop_taskrun_incoming_fks explicitly drops only FKs pointing to TaskRun (via confrelid), not FKs defined on TaskRun/task_run_v2 (via conrelid). The testcontainers helper at lines 84–85 uses OR, removing both scopes. This means the test database may end up with a different FK footprint than production. Verify whether outgoing TaskRun FKs exist in the schema or this difference is inconsequential.
Summary
New task runs can be routed to a parallel
task_run_v2Postgres table instead of the mainTaskRuntable, decided per-org by a feature flag and keyed purely by the run id's format. Existing runs stay inTaskRun, with no backfill. The flag ships off, so behavior is unchanged until an org is opted in.This builds on the RunStore adapter that already funnels all Postgres
TaskRunaccess through one place (writes in #3981, reads in #3990). RunStore now routes each run to its physical table by id format: a KSUID id meanstask_run_v2, anything else (including legacy cuids) meansTaskRun.Design
runTableV2flag on; everyone else keeps minting legacy ids. The flag is read in memory at the single mint site in the trigger path, so the hot path adds no query. RunStore never sees the flag: it routes purely byisKsuidId(id), and a malformed id falls back to legacy.findRunsdoes a bounded two-way merged keyset cursor (ordered reads standardize on a(createdAt, id)keyset, since cuid and KSUID do not share a sort range), and a non-idfindRun(idempotency-key dedup, or "are there any runs in this environment") queries both tables. Both apply identical scoping to each table, so a merge cannot leak a run across an auth boundary.TaskRun,task_run_v2, and the mollifier buffer, so a reused key is always found and never produces a duplicate run.task_run_v2from the start (empty until orgs cut over), streaming its WAL rows through the same transform into the same ClickHouse table.task_run_v2carries the same relations asTaskRun, and the incoming foreign keys pointing atTaskRunare dropped so the two tables are not coupled by constraints.Stacked on #3990 (its base), so this PR shows only the routing commits on top of the read adapter.
Before enabling the flag for any org,
task_run_v2needsREPLICA IDENTITY FULLapplied the same out-of-band way asTaskRun, so its update and delete events stream to ClickHouse with the old row.