Skip to content

perf(run-engine): optimize waitpoint mapping in executionSnapshotSystem - #4467

Closed
deepshekhardas wants to merge 4 commits into
triggerdotdev:mainfrom
deepshekhardas:feat/mythic-algorithm-optimization
Closed

perf(run-engine): optimize waitpoint mapping in executionSnapshotSystem#4467
deepshekhardas wants to merge 4 commits into
triggerdotdev:mainfrom
deepshekhardas:feat/mythic-algorithm-optimization

Conversation

@deepshekhardas

Copy link
Copy Markdown

Performance optimization for waitpoint mapping in executionSnapshotSystem.

deepshekhardas added 4 commits February 14, 2026 07:14
triggerdotdev#2796)

When a user removes the machine configuration from a task and redeploys, task.machine becomes undefined. Prisma's create() silently skips undefined fields for Json columns rather than setting them to NULL. This change uses the nullish coalescing operator to explicitly pass null, ensuring the machineConfig column is cleared in the database.
…undWorkerTask create

Applied the fix pattern to ensure retryConfig, queueConfig, and payloadSchema are also explicitly cleared when removed from task definition, as suggested in PR feedback.
Efficiently select and map batch relation in AttemptForExecutionGetPayload and _executionFromAttempt to restore batch context during dequeue. Part of Legend Rank mission.
Refactor enhanceExecutionSnapshotWithWaitpoints to use an Index Map for O(N+M) complexity, replacing a quadratic nested loop. Improves performance for runs with large numbers of waitpoints. Part of Mythic Rank mission.
@changeset-bot

changeset-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 4b1f911

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

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Hi @deepshekhardas, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this Aug 2, 2026
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d250add0-4fcc-44f4-9449-1d56279bb3cd

📥 Commits

Reviewing files that changed from the base of the PR and between 14824b0 and 4b1f911.

📒 Files selected for processing (3)
  • apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts
  • apps/webapp/app/v3/services/createBackgroundWorker.server.ts
  • internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts

Walkthrough

The queue consumer now retrieves task run batch identifiers and includes batch metadata in returned executions. Background worker task creation stores absent configuration and payload schema values as null. Execution snapshot processing precomputes completed waitpoint indexes and preserves duplicate and fallback behavior. Several formatting-only changes update callbacks, deployment selection indentation, return type formatting, and metadata mappings.

✨ Finishing Touches 💡 1
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch feat/mythic-algorithm-optimization
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Warning

⚠️ This pull request shows signs of AI-generated slop (description_diff_mismatch). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

@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 4 potential issues.

Open in Devin Review

Comment on lines +277 to +284
retryConfig: task.retry ?? null,
queueConfig: task.queue ?? null,
machineConfig: task.machine ?? null,
triggerSource: task.triggerSource === "schedule" ? "SCHEDULED" : "STANDARD",
fileId: tasksToBackgroundFiles?.get(task.id) ?? null,
maxDurationInSeconds: task.maxDuration ? clampMaxDuration(task.maxDuration) : null,
queueId: queue.id,
payloadSchema: task.payloadSchema as any,
payloadSchema: (task.payloadSchema as any) ?? null,

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.

🔴 Deployed task registration fails when a task has no retry, queue, machine or schema settings

Task records are created with explicit empty values (task.retry ?? null at apps/webapp/app/v3/services/createBackgroundWorker.server.ts:277-279 and 284) instead of simply leaving those settings out, and the database layer rejects that empty value, so registering such tasks silently fails.
Impact: Tasks that don't declare retry/queue/machine/payload-schema options can fail to be registered during deploy or dev registration, and the failure is only logged, leaving those tasks missing from the deployed worker.

Prisma rejects plain null for nullable Json fields

retryConfig, queueConfig, machineConfig and payloadSchema are Json? columns (internal-packages/database/prisma/schema.prisma:559-570). For nullable Json fields Prisma's create input is NullableJsonNullValueInput | InputJsonValue, i.e. Prisma.DbNull / Prisma.JsonNull — passing a bare null is a type error and, at runtime, produces a PrismaClientValidationError ("Invalid value provided. Expected NullableJsonNullValueInput or Json, provided null"). That error is swallowed by the surrounding try/catch in createWorkerTask (apps/webapp/app/v3/services/createBackgroundWorker.server.ts:287-322), which only logs, so the backgroundWorkerTask row is never created.

Also note this is a create, not an update: omitting the field (the previous undefined behaviour) already stores SQL NULL, so the ?? null change provides no benefit.

Suggested change
retryConfig: task.retry ?? null,
queueConfig: task.queue ?? null,
machineConfig: task.machine ?? null,
triggerSource: task.triggerSource === "schedule" ? "SCHEDULED" : "STANDARD",
fileId: tasksToBackgroundFiles?.get(task.id) ?? null,
maxDurationInSeconds: task.maxDuration ? clampMaxDuration(task.maxDuration) : null,
queueId: queue.id,
payloadSchema: task.payloadSchema as any,
payloadSchema: (task.payloadSchema as any) ?? null,
retryConfig: task.retry,
queueConfig: task.queue,
machineConfig: task.machine,
triggerSource: task.triggerSource === "schedule" ? "SCHEDULED" : "STANDARD",
fileId: tasksToBackgroundFiles?.get(task.id) ?? null,
maxDurationInSeconds: task.maxDuration ? clampMaxDuration(task.maxDuration) : null,
queueId: queue.id,
payloadSchema: task.payloadSchema as any,
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

console.log("✅ Started the SharedQueueConsumer");

this.#doWork().finally(() => {});
this.#doWork().finally(() => { });

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.

🟡 Several touched files are no longer formatted per the repository's required formatter

Code in the changed files is re-indented into a style the project's mandated formatter does not produce (e.g. () => { } at apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts:265), so the required formatting check is violated.
Impact: The repository's formatting requirement is broken, producing noisy unrelated diffs and failing format checks.

Prettier-enforced style per AGENTS.md

AGENTS.md ("Coding style") states: "Formatting is enforced using Prettier. Run pnpm run format before committing." The PR introduces non-Prettier output in multiple places: .finally(() => { }) at apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts:265 and :420, the re-indented nested ternary at apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts:623-624 and :1763-1767, the return-type object at :1913-1915, and the object-literal re-indentation in internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts:89-104 and :233-245. Running pnpm run format will revert these.

Suggested change
this.#doWork().finally(() => { });
this.#doWork().finally(() => {});
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1653 to +1658
batch: {
select: {
id: true,
friendlyId: true,
},
},

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.

🔍 PR contains three unrelated changes across two subsystems

The stated scope is a waitpoint-mapping optimization in the run engine, but the branch also re-adds batch context to the legacy shared-queue execution payload (apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts:1653-1658, :1763-1767) and changes nullable Json handling in apps/webapp/app/v3/services/createBackgroundWorker.server.ts:277-284. CONTRIBUTING.md explicitly asks for one issue per PR; splitting would also make the batch-relation change (which reverses a deliberate // TODO: Removing this for now until we can do it more efficiently) reviewable on its own merits, including the extra join cost per dequeue.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1763 to +1767
batch: attempt.taskRun.batch
? {
id: attempt.taskRun.batch.friendlyId,
}
: undefined,

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.

🔍 Batch id semantics in the restored execution payload match the attempt-creation path

batch.id is populated from attempt.taskRun.batch.friendlyId, which matches the convention used when the execution payload is built at attempt-creation time (apps/webapp/app/v3/services/createTaskRunAttempt.server.ts:244-246 uses batchTaskRun.friendlyId as id). One difference worth noting: that path resolves the batch through batchItems/BatchTaskRunItem, while this one uses the direct TaskRun.batchId relation, so runs whose batch association only exists via BatchTaskRunItem would report batch: undefined here, producing inconsistent ctx.batch between initial execution and resume.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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