fix(webapp,run-engine): honor per-queue length cap on concurrency-key queues#3558
fix(webapp,run-engine): honor per-queue length cap on concurrency-key queues#3558ericallam wants to merge 4 commits into
Conversation
|
WalkthroughThis PR fixes a bug where per-queue length limits and dashboard metrics for queues using concurrency keys (CK) incorrectly reported 0, allowing the per-queue cap to be bypassed. The fix introduces per-base-queue counter keys and tracked Redis Lua commands. Query methods now aggregate base set/zset cardinality with GET-backed counters. CK enqueue, dequeue, message read, acknowledge, nack, dead-letter, release, and TTL expiry paths were updated to atomically maintain length and running counters with floored DECR guards. Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal-packages/run-engine/src/run-queue/index.ts`:
- Around line 420-424: The code is incorrectly treating Redis command errors as
missing keys by converting baseErr/ctrErr to 0; update the logic around the
results tuple (baseErr, baseVal, ctrErr, ctrVal) so that if baseErr or ctrErr is
truthy you surface/propagate that error (throw or return it) instead of coercing
to 0, while still treating a null/undefined value as 0 (i.e., keep the baseVal
== null ? 0 behavior). Locate the block using results[0]/results[1] and
variables baseErr, baseVal, ctrErr, ctrVal, compute baseCount/ctrCount only when
no error is present, and return or rethrow the encountered error so callers can
handle Redis command failures rather than silently undercounting.
In `@internal-packages/run-engine/src/run-queue/tests/ckCounters.test.ts`:
- Line 4: The test imports describe from node:test which conflicts with the
Vitest runner; replace the import so the test uses Vitest's API (i.e., import
describe from "vitest" or use Vitest globals) so the file ckCounters.test.ts
runs under Vitest; update any other test helpers (e.g., it/expect) in the same
file to use Vitest symbols if needed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f0aabc18-c004-4d2b-894b-a0397acc1811
⛔ Files ignored due to path filters (1)
references/hello-world/src/trigger/ckCounters.tsis excluded by!references/**
📒 Files selected for processing (5)
.server-changes/fix-ck-queue-length-cap-and-dashboard.mdinternal-packages/run-engine/src/run-queue/index.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/types.ts
📜 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). (5)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
- GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
- GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 8)
- GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
🧰 Additional context used
📓 Path-based instructions (10)
**/*.{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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.ts
{apps,internal-packages}/**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (CLAUDE.md)
Use
pnpm run typecheckto verify changes in apps and internal packages (apps/*,internal-packages/*) instead ofbuild, which proves almost nothing about correctness
Files:
internal-packages/run-engine/src/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.ts
**/*.{ts,tsx,js}
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.{ts,tsx,js}: Import from@trigger.dev/coreusing subpaths only, never the root export
Always import tasks from@trigger.dev/sdk, never from@trigger.dev/sdk/v3or deprecatedclient.defineJob
Add crumbs to code using//@Crumbscomments or `// `#region` `@crumbsblocks for debug tracing during development
Files:
internal-packages/run-engine/src/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.ts
**/*.{ts,tsx,js,jsx,json,md,css,scss}
📄 CodeRabbit inference engine (AGENTS.md)
Code formatting is enforced using Prettier. Run
pnpm run formatbefore committing
Files:
internal-packages/run-engine/src/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.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/run-queue/tests/ckCounters.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 patternMyService.ts->MyService.test.ts
**/*.test.{ts,tsx,js}: Use vitest for unit testing and run tests withpnpm run test
Test files should live beside the files under test with descriptivedescribeanditblocks
Tests should avoid mocks or stubs and use helpers from@internal/testcontainerswhen Redis or Postgres are needed
Files:
internal-packages/run-engine/src/run-queue/tests/ckCounters.test.ts
**/*.test.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use testcontainers with
redisTest,postgresTest, orcontainerTestfrom@internal/testcontainersfor testing with Redis/PostgreSQL dependencies
Files:
internal-packages/run-engine/src/run-queue/tests/ckCounters.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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.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/run-queue/types.tsinternal-packages/run-engine/src/run-queue/keyProducer.tsinternal-packages/run-engine/src/run-queue/tests/ckCounters.test.tsinternal-packages/run-engine/src/run-queue/index.ts
| redis.call('INCR', lengthCounterKey) | ||
| end | ||
|
|
||
| -- Rebalance CK index |
There was a problem hiding this comment.
suggestion: Keep these comments as it helps readers understand intent
Summary
Queues that use concurrency keys can no longer bypass the per-queue length cap, and the "Queued | Running" columns in the dashboard now show the true total across all CK variants instead of 0.
The cap and the dashboard both relied on
ZCARDof the base queue key, but CK-keyed runs live under<base>:ck:<variant>keys. Any queue that used concurrency keys read 0 — letting a single CK variant grow unbounded past the user's configured cap.Fix
Two per-base-queue counters are maintained inside the CK Lua scripts:
<base>:lengthCounterand<base>:runningCounter. Non-CK enqueue/dequeue paths are untouched.Counters are lazy-initialized the first time a CK enqueue (or nack) lands on a queue: the Lua script sums
ZCARDacross the variants tracked byckIndex, sets the counter, thenINCRs. Pre-existing CK backlog on already-populated queues is captured automatically — no batch migration required.INCR/DECRis gated onZADD/SADDreturning 1 (a new entry vs an idempotent no-op), so duplicate enqueues or re-dequeues don't inflate the counter.The counter is
SETwith a 24-hour TTL on init.INCR/DECRdo not extend the TTL, so the counter expires daily and the next CK operation re-seeds it fromckIndex. This bounds any drift that accumulates during the rolling-deploy overlap window — where old (un-Tracked) and new (Tracked) webapp instances briefly coexist — to ≤24 hours, with no admin sweep or background reconciler needed.Read paths pipeline
ZCARD/SCARDon the base key +GETon the counter and sum. A missing counter is treated as 0, so pure non-CK queues see the same answer as before.The counter-aware scripts ship alongside the originals with a
Trackedsuffix for rolling-deploy safety; a follow-up PR will drop the originals once this has rolled out.Test plan
pnpm run test --filter @internal/run-engine— 116 tests pass, including a newckCounters.test.tscovering lazy init from pre-existing backlog, churn, floor-at-zero, the non-CK regression case, mixed CK + non-CK on the same base queue, idempotent re-enqueue (ZADD-already-exists), 24h TTL on the counter, and nack re-seeding after counter expiry.lengthCounter=16,runningCounter=8, dashboard showed Queued=16 / Running=8 for the CK queue.QueueSizeLimitExceededError.ckIndexsum, then INCR'd.