Skip to content

improvement(cron): only start outbox and file-search passes when work is due, via a shared scheduled-pass helper - #8713

Merged
waleedlatif1 merged 7 commits into
stagingfrom
improvement/outbox-work-gate
Oct 7, 2026
Merged

waleedlatif1 merged 7 commits into
stagingfrom
improvement/outbox-work-gate

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The per-minute outbox and workspace-file-search crons started a Trigger.dev run every tick even with nothing to do. Both now check first, and all three gated scheduled passes share one helper.

Shared helper — lib/core/async-jobs/scheduled-pass.ts

  • triggerScheduledPass: the windowed tasks.trigger (idempotency key ${keyPrefix ?? taskId}:${window}, idempotencyKeyTTL: '5m', region) that outbox, file search, and knowledge projection each hand-rolled. Keys and options are byte-identical to before, so in-flight dedupe is unaffected across the deploy
  • startScheduledPass: due → inline or Trigger.dev → shared ScheduledPassResult ({ triggered, backend, jobId }), replacing KnowledgeProjectionSweepResult / the file-search result type
  • Each call site keeps its policy explicit by computing due itself: outbox maintenance window + fail-open, file search fail-open, knowledge projection fail-closed (unchanged). Outbox keeps its synchronous self-hosted branch local because the route reports its output. cleanup-stale-executions uses the job-queue path and is left as is

Outbox gate — starts a run only when:

  • a pending event is due — the claim phase's own readyEventTypesQuery, so gate and claimer share one predicate
  • a processing lease is stale — the reaper's predicate, now shared via isStuckProcessing / STUCK_PROCESSING_THRESHOLD_MS
  • or it's a maintenance tick (every 5th minute), so document recovery, background-work reaping, and pruning keep running on an idle queue

File-search dispatch gate — starts a run only when one of the dispatcher's own phases would act, each sharing its predicate with that phase: backfill page due (isBackfillPageDue), expired build (fileSearchBuildExpired), stale claim (staleClaim), or a queued workspace. Reconcile interval unchanged (hourly) since db:push environments rely on that walk. enqueue-dispatch.ts now uses import type for the task instead of a runtime import

Both gates fail open (a check error starts the run as before — billing, seat sync, invitations, and KB/file indexing ride these crons) and cover both the Trigger.dev and inline backends. Cron routes return 200 triggered: false on an idle tick; response bodies otherwise unchanged. Latency for due work stays ≤ 1 tick.

Supersedes #8714 (folded in so both gates can share the helper).

Type of Change

  • Improvement

Testing

  • Integration on real Postgres:
    • outbox: future pending / fresh lease / completed / dead-letter → no work; event due exactly at now → work; lease exactly at the stale threshold → work; plan has no Seq Scan and stays under 100 buffers on a 100k-row backlog
    • file search: each work leg reports work; the idle case reports no work and prepareWorkspaceFileSearchDispatch does nothing on the same data; no Seq Scan
  • Unit: each backend's own result for idle / maintenance / check-failure ticks; exact tasks.trigger call pinned for all three passes (keys, TTLs, maxDuration, region)
  • Every guard and the helper's branches shown red by reverting them (maintenance window, fail-open, window instant, key prefix, inline path, due)
  • bun run lint, bun run type-check, bun run check:audits, check:test-patterns — green; 26 files / 309 unit tests; 2 integration suites / 40 tests

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)

@vercel

vercel Bot commented Oct 7, 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 Oct 7, 2026 2:17am 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 9 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/core/outbox/retention.ts Outdated
@greptile-apps

greptile-apps Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Refactors cron job scheduling with new shared helper and database queries.

The PR appears safe to merge; no new actionable issue was found.

What we checked:

  • Different tasks share a fake run: JSON.stringify([taskId, key]) keeps both strings separate. Repeated pairs still find the same saved run.

Summary

This PR skips idle outbox and file-search runs and shares scheduled-pass dispatch with knowledge projection.

  • Outbox keeps its maintenance ticks and runs when the work check fails.
  • File search checks the same conditions used by its dispatch phases.
  • Since the last review, only the test fake’s key encoding changed. No new issue was found.
  • The earlier findings are addressed: tests check returned results, cleanup runs without the partial mock, and the query-plan test uses production indexes without forcing the plan.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Tick[Cron tick] --> Due{Pass due?}
  Due -->|No| Idle[Return triggered false]
  Due -->|Yes| Backend{Trigger.dev enabled?}
  Backend -->|Yes| Trigger[Start window-keyed run]
  Backend -->|No| Inline[Run inline]
  Trigger --> Accepted[Return accepted job ID]
Loading

Reviews (6) · Last reviewed commit: "test(testing): key the idempotent Trigge..." · Reviewed by Greptile

Comment thread apps/sim/lib/core/outbox/enqueue.test.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 Oct 7, 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 9 files

Confidence score: 5/5

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

Turn on auto-fix | Re-trigger cubic

@waleedlatif1 waleedlatif1 changed the title improvement(outbox): only start the processor when work is due or maintenance is scheduled improvement(cron): only start outbox and file-search passes when work is due, via a shared scheduled-pass helper Oct 7, 2026
@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 Oct 7, 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 17 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/workspace-files/search/dispatcher.integration.ts Outdated
Comment thread apps/sim/lib/workspace-files/search/enqueue-dispatch.test.ts Outdated
Comment thread apps/sim/lib/workspace-files/search/dispatcher.integration.ts Outdated
Comment thread apps/sim/lib/workspace-files/search/dispatcher.integration.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 Oct 7, 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 17 files

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Turn on auto-fix | 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 Oct 7, 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 18 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread packages/testing/src/mocks/trigger-sdk.mock.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 Oct 7, 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 18 files

Confidence score: 5/5

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

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 18ad8b8 into staging Oct 7, 2026
33 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/outbox-work-gate branch October 7, 2026 05:48

This branch was previously deployed

1 inactive deployment
Preview — e4482798 Deployed Oct 7, 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