Skip to content

improvement(execution): cut the time between a workflow trigger and its first block - #8434

Merged
waleedlatif1 merged 4 commits into
stagingfrom
perf/execution-preprocessing
Sep 29, 2026
Merged

waleedlatif1 merged 4 commits into
stagingfrom
perf/execution-preprocessing

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Measured where time goes between a trigger (webhook, schedule, API) and the first block running: preprocessing itself is fast; the cost is ~40 sequential DB round trips plus shipping the full deployed state (hundreds of KB for large workflows) up and back several times per run
  • Schedule dispatch: TriggerDevJobQueue.getJob called runs.retrieve with caller-chosen ids (schedule_…, workflow-execution:…), which Trigger.dev takes ~10 s to 404 (vs <1 s for a real run id). Every schedule tick stalled ~9.6 s on this before enqueueing anything. Only run_ ids are retrieved directly now; others go straight to the jobId: tag lookup
  • Log start: snapshot id resolved by hash (cached per process once a log row references it) and the log inserted with ON CONFLICT (execution_id) DO NOTHING — replaces select-existing-log + full-state upsert RETURNING * + insert. Orphan-cleanup race handled by re-resolving on FK violation. Child workflow snapshots use the same id-only resolution
  • Preprocessing: payer attribution starts alongside the workflow read when the workspace is known; ban/usage/subscription gate reads start as soon as actor + payer are known and are applied in the same fixed precedence; the actor subscription is only read when rate limiting runs (or the caller asks — table cells)
  • Execution core: env + PII policy reads start before the custom-block overlay, custom blocks and outbound context load in parallel, and the webhook job's already-loaded environment is reused instead of loaded/decrypted twice
  • Webhooks: lookups compute trigger-block deployment from the active version they already join (no second query, no full-state fetch); webhook job starts its env load right after preprocessing
  • Fewer round trips in shared lookups: getHighestPrioritySubscription (3 → 1 stage), custom-block rows (rows first, entitlement only when rows exist), env suspension check runs alongside access/env reads
  • Versioned deployment loads serve the materialized-state cache before querying (entries now bound to their workflow)

Results

Opt-in harness driving the real webhook route in-process (inline backend, real Postgres/Redis, 1 ms injected DB latency, ~390 KB workflow, billing on), baseline and branch alternated:

  • delivery → first block: p50 194–235 ms → 85–108 ms, p90 241–266 → 116–128 ms
  • job start → first block: 132–160 ms → 48–60 ms
  • route response: 58–67 ms → 32–42 ms
  • no injected latency: 49 → 24 ms p50

Type of Change

  • Improvement / performance

Testing

  • New real-Postgres integration tests: start-execution (dedup, idempotent start, cleanup-deleted snapshot recovery, concurrent starts), execution-environment (owner/actor identities, suspension, lapsed access), trigger-block-deployment (lookup flag vs standalone check, cache bound to workflow), workspace-rows (entitlement), plan (tier priority, org-over-personal tie-break); key guards shown red by mutation
  • trigger-dev.test.ts regression for caller-chosen job ids (fails on old code); preprocessing precedence tests for the early gate reads
  • Full apps/sim unit suite (34,580 passed), full bun run test:integration (1,113 + 106 passed), bun run type-check, bun run lint, bun run check:audits (52), docs-manifest:check
  • Independent review of the diff; findings addressed (snapshot cache only remembers referenced ids, FK retry scoped to the insert, comment fixes)

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…ts first block

- Trigger.dev getJob only retrieves real run ids; caller-chosen ids (schedule_…,
  workflow-execution:…) go straight to the tag lookup instead of a ~10 s 404
- Execution-log start resolves the snapshot id (cached once referenced) and inserts
  with ON CONFLICT instead of select + full-state upsert RETURNING * + insert
- Preprocessing starts payer attribution and the ban/usage/subscription gate reads as
  soon as their inputs are known; subscription is only read when needed
- Execution core prefetches env + PII policy alongside custom blocks and reuses the
  webhook job's already-loaded environment
- Webhook lookups answer trigger-block deployment from the join they already do;
  versioned deployment loads serve the materialized cache first
- getHighestPrioritySubscription, custom-block rows, and env suspension lookups drop
  sequential round trips
@vercel

vercel Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 29, 2026 10:12pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 29 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/start-execution.integration.ts Outdated
Comment thread apps/sim/lib/environment/utils.ts Outdated
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Refactors workflow execution environment loading and snapshot resolution.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR reduces work between a trigger and its first workflow block.

  • Caller-chosen Trigger.dev job IDs use tag lookup instead of a slow failed run-ID retrieval.
  • Execution admission, environment, and policy reads begin earlier; webhook jobs reuse their resolved environment.
  • Snapshot and deployment-state caching reduce repeated database reads and transfers, with integration coverage for key persistence and access paths.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  T[Trigger] --> A[Admission reads]
  A --> Q[Queued or direct execution]
  Q --> E[Environment and deployment state]
  E --> L[Idempotent log and snapshot]
  L --> B[First block]
Loading

Reviews (4) · Last reviewed commit: "improvement(environment): reuse the acto..."

Comment thread apps/sim/lib/core/async-jobs/backends/trigger-dev.test.ts Outdated
Comment thread apps/sim/lib/logs/execution/logger.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai 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.

No issues found across 29 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 29 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/environment/utils.ts
Comment thread apps/sim/lib/logs/execution/snapshot/service.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai 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.

No issues found across 29 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit d3f2830 into staging Sep 29, 2026
33 checks passed
@waleedlatif1
waleedlatif1 deleted the perf/execution-preprocessing branch September 29, 2026 22:31

This branch was previously deployed

1 inactive deployment
Preview — ee42ccfe Deployed Sep 29, 2026 by vercel[bot]
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