Skip to content

refactor(run-engine): route TTL expiration through the batch path only#3554

Open
ericallam wants to merge 2 commits into
mainfrom
feat/remove-old-ttl-system
Open

refactor(run-engine): route TTL expiration through the batch path only#3554
ericallam wants to merge 2 commits into
mainfrom
feat/remove-old-ttl-system

Conversation

@ericallam
Copy link
Copy Markdown
Member

Summary

TTL expiration on queued runs was being scheduled twice: once via a per-run expireRun worker job (the original implementation) and once via the batch TTL system (added more recently). Both paths attempt to flip the same run to EXPIRED. The per-run job almost always won the race, leaving the batch consumer to observe runs already expired by the older path.

This collapses TTL expiration onto the batch path so every queued TTLed run goes through a single Redis-backed sorted set + batch consumer instead of also getting its own scheduled redis-worker job.

Design

engine.trigger and delayedRunSystem.enqueueDelayedRun no longer call ttlSystem.scheduleExpireRun. The remaining enqueueSystem.enqueueRun({ includeTtl: true }) already adds the run to the TTL sorted set; TtlSystem.expireRunsBatch flips it to EXPIRED when the TTL fires.

Delayed runs get the same coverage by passing includeTtl: true on their post-delay enqueue, so the TTL is armed from the moment the run enters the queue (matching how the old job behaved — parseNaturalLanguageDuration is evaluated at enqueue time).

The new path explicitly does not re-expire runs once they have been allocated a concurrency slot. That is intentional: TTL is for runs that are queued and have never started. Once a run has a slot it is on its way to executing.

Test plan

  • pnpm run test --filter @internal/run-engine ./src/engine/tests/ttl.test.ts — 15 tests, including a new "Re-enqueued runs are not expired by TTL once they have started" that locks in the queued-and-never-started contract.
  • pnpm run test --filter @internal/run-engine ./src/engine/tests/delays.test.ts — 5 tests, including "Delayed run with a ttl" which now also asserts the TTL is armed from queue-enter time, not createdAt.
  • pnpm run test --filter @internal/run-engine ./src/engine/tests/lazyWaitpoint.test.ts — 12 tests.
  • pnpm run typecheck --filter @internal/run-engine.

TTL expiration on queued runs was being scheduled twice: once via a
per-run expireRun worker job (the original implementation) and once via
the batch TTL system (added more recently). The per-run job almost
always won the race, leaving the batch consumer to observe runs already
expired by the older path.

Collapse onto the batch path. engine.trigger and
delayedRunSystem.enqueueDelayedRun no longer schedule the per-run
expireRun job. The TTL sorted set + batch consumer is now the only
mechanism that flips queued runs to EXPIRED.

Delayed runs get the same coverage by passing includeTtl: true on their
post-delay enqueue, so the TTL is armed from the moment the run enters
the queue (matching how the old job behaved). The new path
intentionally does not re-expire runs that have already been allocated
a concurrency slot — TTL is for runs that are queued and have never
started.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 11, 2026

⚠️ No Changeset found

Latest commit: aaf2a93

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

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: ad5e49df-ad99-4f32-9982-08977cd655f1

📥 Commits

Reviewing files that changed from the base of the PR and between b208b49 and aaf2a93.

📒 Files selected for processing (5)
  • internal-packages/run-engine/src/engine/index.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal-packages/run-engine/src/engine/index.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (29)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
  • GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
  • GitHub Check: units / e2e-webapp / 🧪 E2E Tests: Webapp
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: sdk-compat / Node.js 22.12 (ubuntu-latest)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: sdk-compat / Bun Runtime
  • GitHub Check: sdk-compat / Deno Runtime
  • GitHub Check: sdk-compat / Cloudflare Workers
  • GitHub Check: sdk-compat / Node.js 20.20 (ubuntu-latest)
  • GitHub Check: Analyze (javascript-typescript)
🧰 Additional context used
📓 Path-based instructions (12)
**/*.{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 instead

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use function declarations instead of default exports

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.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:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
internal-packages/run-engine/src/engine/systems/**/*.ts

📄 CodeRabbit inference engine (internal-packages/run-engine/CLAUDE.md)

Integrate OpenTelemetry tracer and meter instrumentation in RunEngine systems for observability

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
{apps,internal-packages}/**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (CLAUDE.md)

Use pnpm run typecheck to verify changes in apps and internal packages (apps/*, internal-packages/*) instead of build, which proves almost nothing about correctness

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
{package.json,**/*.{ts,tsx,js}}

📄 CodeRabbit inference engine (CLAUDE.md)

Pin Zod to version 3.25.76 exactly across the entire monorepo - never use a different version or version range

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx,js}: Import from @trigger.dev/core using subpaths only, never the root export
Always import tasks from @trigger.dev/sdk, never from @trigger.dev/sdk/v3 or deprecated client.defineJob
Add crumbs to code using // @Crumbs comments or `// `#region` `@crumbs blocks for debug tracing during development

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
**/*.{ts,tsx,js,jsx,json,md,css,scss}

📄 CodeRabbit inference engine (AGENTS.md)

Code formatting is enforced using Prettier. Run pnpm run format before committing

Files:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
**/*.{test,spec}.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use vitest for all tests in the Trigger.dev repository

Files:

  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
internal-packages/run-engine/src/engine/tests/**/*.test.ts

📄 CodeRabbit inference engine (internal-packages/run-engine/CLAUDE.md)

Implement tests for RunEngine in src/engine/tests/ using testcontainers for Redis and PostgreSQL containerization

Files:

  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
**/*.test.{ts,tsx,js}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.test.{ts,tsx,js}: Use vitest exclusively for testing and never mock anything - use testcontainers instead
Place test files next to source files using the pattern MyService.ts -> MyService.test.ts

**/*.test.{ts,tsx,js}: Use vitest for unit testing and run tests with pnpm run test
Test files should live beside the files under test with descriptive describe and it blocks
Tests should avoid mocks or stubs and use helpers from @internal/testcontainers when Redis or Postgres are needed

Files:

  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
**/*.test.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Use testcontainers with redisTest, postgresTest, or containerTest from @internal/testcontainers for testing with Redis/PostgreSQL dependencies

Files:

  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
🧠 Learnings (2)
📚 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:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.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:

  • internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts
  • internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts
  • internal-packages/run-engine/src/engine/tests/ttl.test.ts
  • internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts
🔇 Additional comments (4)
internal-packages/run-engine/src/engine/systems/pendingVersionSystem.ts (1)

101-106: LGTM!

internal-packages/run-engine/src/engine/systems/delayedRunSystem.ts (1)

147-175: LGTM!

internal-packages/run-engine/src/engine/tests/pendingVersion.test.ts (1)

313-427: LGTM!

internal-packages/run-engine/src/engine/tests/ttl.test.ts (1)

4-4: LGTM!

Also applies to: 32-33, 63-70, 166-167, 245-246, 317-433, 435-530, 552-553, 578-583, 621-624, 689-690, 778-779, 804-809, 878-881, 897-898, 1028-1029, 1493-1494, 1520-1529


Walkthrough

This PR consolidates TTL expiration handling so TTL is armed when a run is enqueued (includeTtl: true) and the TTL consumer batch path is the primary mechanism that marks runs EXPIRED. Legacy per-run expire jobs are retained only for DEVELOPMENT runtime. Tests are updated with ttlSystem.batchMaxWaitMs and concurrency controls and a new contract test confirms re-enqueued runs without TTL arming do not expire.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'refactor(run-engine): route TTL expiration through the batch path only' directly and clearly summarizes the main change: consolidating TTL expiration to use only the batch path instead of the previous dual approach.
Description check ✅ Passed The pull request description comprehensively covers the summary of changes, design rationale, and test plan as required. All key aspects of the refactor are explained clearly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feat/remove-old-ttl-system

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 and usage tips.

@ericallam ericallam marked this pull request as ready for review May 11, 2026 19:09
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

Devin Review found 0 potential issues.

View 3 additional findings in Devin Review.

Open in Devin Review

…endingVersion re-enqueue

Two gaps in the previous commit's collapse onto the batch TTL path:

- DEV runs are fast-pathed straight to the worker queue and never enter
  the TTL sorted set. If the dev CLI isn't running, those runs would sit
  on the worker queue forever without the batch path ever seeing them.
  In non-DEV environments runs always execute once they're on the worker
  queue, so this isn't a concern there. Restore the legacy per-run
  expireRun job, gated on environment.type === "DEVELOPMENT".

- pendingVersionSystem.enqueueRunsForBackgroundWorker re-enqueues runs
  that were waiting for a matching worker version. That re-enqueue is
  the first time the run is actually queued for a worker, so pass
  includeTtl: true to arm TTL on the new message.

Adds a DEV TTL test that exercises the fast-path coverage and a
pendingVersion test that asserts ttlExpiresAt is present after
re-enqueue.
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.

1 participant