Skip to content

Latest commit

 

History

History
269 lines (203 loc) · 19 KB

03-trigger-system.md

File metadata and controls

269 lines (203 loc) · 19 KB

Trigger System

The trigger system routes webhook events to the appropriate agent. When a webhook arrives, the router builds a TriggerContext and calls TriggerRegistry.dispatch() to find the first matching handler. The matched handler returns a TriggerResult specifying which agent to run and with what input.

TriggerRegistry

src/triggers/registry.ts

A simple ordered list of handlers with first-match-wins dispatch:

class TriggerRegistry {
  register(handler: TriggerHandler): void;
  dispatch(ctx: TriggerContext): Promise<TriggerResult | null>;
  getHandlers(): TriggerHandler[];
}

dispatch() iterates handlers in registration order. For each handler:

  1. Call matches(ctx) — if false, skip
  2. Call handle(ctx) — if it returns a TriggerResult, return it
  3. If handle() returns null, continue to next handler

That makes dispatch first-match-wins for non-null results. A handler should return bare null only when it does not claim the event and later handlers should still get a chance. If a handler claims the event but decides not to run an agent, it returns a structured TriggerResult with agentType: null instead.

TriggerHandler

src/triggers/types.ts

interface TriggerHandler {
  name: string;
  description: string;
  matches(ctx: TriggerContext): boolean;
  handle(ctx: TriggerContext): Promise<TriggerResult | null>;
}

TriggerContext

interface TriggerContext {
  project: ProjectConfig;
  source: TriggerSource;          // 'trello' | 'github' | 'jira' | 'linear' | 'sentry'
  payload: unknown;                // Raw webhook payload
  personaIdentities?: PersonaIdentities;  // GitHub bot identities
}

TriggerResult

interface TriggerResult {
  agentType: string | null;        // Which agent to run
  agentInput: AgentInput;          // Input data for the agent
  workItemId?: string;
  workItemUrl?: string;
  workItemTitle?: string;
  prNumber?: number;
  prUrl?: string;
  prTitle?: string;
  onBlocked?: () => void;          // Cleanup if job can't be enqueued
  skipReason?: {
    handler: string;
    message: string;
  };
  lockKey?: string;                 // Optional work-item lock override
  coalesceKey?: string;             // Optional PM dispatch coalescing key
  deferredRecheck?: {
    delayMs: number;
    coalesceKey: string;
  };                               // Schedule a bare delayed re-dispatch
}

Built-in Triggers

Registration happens in src/triggers/builtins.ts. PM providers (Trello, JIRA, Linear) contribute triggers via the manifest registry; SCM and alerting providers use their own register.ts functions:

function registerBuiltInTriggers(registry: TriggerRegistry): void {
  // PM providers register via the manifest registry (spec 006/009 pattern)
  for (const manifest of listPMProviders()) {
    for (const handler of manifest.triggerHandlers) {
      registry.register(handler);
    }
  }
  registerGitHubTriggers(registry);
  registerSentryTriggers(registry);
}

Trello triggers (src/triggers/trello/)

Handler Event Agent
TrelloCommentMentionTrigger Bot mentioned in comment Varies by context
TrelloStatusChangedSplittingTrigger Card → Splitting list splitting
TrelloStatusChangedPlanningTrigger Card → Planning list planning
TrelloStatusChangedTodoTrigger Card → Todo list implementation
TrelloStatusChangedBacklogTrigger Card → Backlog list backlog-manager
TrelloStatusChangedMergedTrigger Card → Merged list backlog-manager
ReadyToProcessLabelTrigger "cascade-ready" label added splitting

JIRA triggers (src/triggers/jira/)

Handler Event Agent
JiraCommentMentionTrigger Bot mentioned in comment Varies
JiraStatusChangedTrigger Issue status transition Per-status mapping
JiraLabelAddedTrigger "cascade-ready" label added splitting

GitHub triggers (src/triggers/github/)

Handler Event Agent
CheckSuiteSuccessTrigger CI passed review (with authorMode param)
CheckSuiteFailureTrigger CI failed respond-to-ci (with authorMode param)
PrReviewSubmittedTrigger Review with changes_requested respond-to-review
ReviewRequestedTrigger Bot requested as reviewer review
PrOpenedTrigger PR opened review
PrCommentMentionTrigger Bot @mentioned in PR comment respond-to-pr-comment
PrMergedTrigger PR merged PM status update (no agent)
PrReadyToMergeTrigger PR approved + checks pass PM status update (no agent)
PrConflictDetectedTrigger Merge conflict on PR resolve-conflicts (with authorMode param)

The authorMode parameter (own / external / all, default own) filters PRs by author type for CheckSuiteSuccessTrigger, CheckSuiteFailureTrigger, and PrConflictDetectedTrigger. own restricts dispatch to CASCADE-authored PRs (the historical behavior); external/all extend it to human-authored PRs. The shared evaluator lives in src/triggers/shared/author-mode.ts. Because respond-to-ci and resolve-conflicts push commits and CASCADE has no write access to a contributor's fork, both apply a fork write-access skip (gateForkWriteAccess) — a fork PR under external/all produces a clean, self-explanatory skip instead of failing mid-run at git push.

Linear triggers (src/triggers/linear/)

Handler Event Agent
LinearCommentMentionTrigger Bot @mentioned in issue comment respond-to-planning-comment
LinearStatusChangedTrigger Issue state transition Per-status mapping
LinearReadyToProcessLabelTrigger "cascade-ready" label added splitting

Sentry triggers (src/triggers/sentry/)

Handler Event Agent
AlertingIssueTrigger Sentry event_alert resource alerting
AlertingMetricTrigger Sentry metric_alert resource alerting
SentryIssueLifecycleTrigger Sentry issue lifecycle resource alerting

Trigger Configuration

Event format

Triggers use category-prefixed events from src/triggers/shared/events.ts. TRIGGER_EVENTS is the canonical catalog used by handlers, result builders, trigger configuration, and static tests:

  • PM: pm:status-changed, pm:label-added, pm:comment-mention
  • SCM: scm:check-suite-success, scm:check-suite-failure, scm:pr-review-submitted, scm:review-requested, scm:pr-opened, scm:pr-comment-mention, scm:pr-merged, scm:pr-ready-to-merge, scm:pr-conflict-detected
  • Alerting: alerting:issue-alert, alerting:metric-alert, alerting:issue-lifecycle
  • Internal: internal:auto-chain

New handlers should import TRIGGER_EVENTS instead of adding raw string literals. The static guard in tests/unit/triggers/trigger-event-consistency.test.ts fails when a handler gates on one event string and emits a different agentInput.triggerEvent.

Result builders

Shared builders live in src/triggers/shared/result-builders.ts, src/triggers/shared/pm-status.ts, src/triggers/shared/pm-label.ts, and src/triggers/github/result-builders.ts.

Use them for new handlers unless there is a concrete reason not to:

  • buildPMDispatchResult, buildPMStatusDispatchResult, and buildPMLabelDispatchResult attach canonical PM trigger events, work-item fields, and PM coalescing keys.
  • buildGitHubPRDispatchResult and the GitHub-specific wrappers attach PR metadata, optional linked PM work-item metadata, and normalized agent input for PR agents.
  • buildNoAgentResult represents a matched trigger whose side effect is complete without spawning an agent, such as PM status updates after a PR merge.
  • buildSkipResult or skip() represents a matched trigger that intentionally stops dispatch with a human-readable reason.
  • buildDeferredRecheckResult represents a delayed bare-job re-dispatch.

Structured skip vs bare null

Bare null means "this handler did not handle the event; continue registry dispatch." Structured skip means "this handler did handle the event; stop dispatch and record why no agent was queued."

The router preserves structured skips in webhook logs with Trigger <handler> skipped: <message>. Use structured skip for disabled trigger config, author-mode gates, self-loop gates, incomplete aggregate check-suite state, missing PR/work-item prerequisites, and similar expected non-dispatch outcomes.

Self-loop gates are intentionally narrow. ReviewRequestedTrigger exempts a self-directed request (sender === requested_reviewer, both a CASCADE persona) and dispatches review, because a human using the shared GITHUB_TOKEN_REVIEWER can re-request their own review; only cross-persona review_requested events (sender ≠ requested_reviewer) are skipped as bot loops. This is safe because CASCADE never programmatically calls GitHub's "request reviewers" API and review submissions emit pull_request_review, not review_requested.

Deferred re-checks

Handlers that cannot make a final decision yet can return deferredRecheck: { delayMs, coalesceKey, recheckKind? } with agentType: null. The router schedules a coalesced delayed BullMQ job and exits without spawning an agent.

The bare re-dispatch on job fire is currently GitHub-only: GitHubRouterAdapter.buildJob() strips triggerResult and stamps the right re-check field based on the optional recheckKind discriminator. Two re-check kinds exist:

  • Mergeability re-check (recheckKind absent) — mergeabilityRecheckAttempt: 1 is set on the job. The GitHub worker re-dispatches through the registry for fresh provider state. If the re-check still cannot resolve state, the worker Sentry-captures under mergeability_recheck_exhausted and stops (one-shot — no further rescheduling).
  • Check-suite re-check (recheckKind: 'check-suite') — checkSuiteRecheckAttempt: 1 is set on the job. If the Actions API is still stale when the job fires, the worker reschedules another coalesced delayed job instead of exhausting, so review/respond-to-ci dispatch stays alive until the API catches up. Used by check-suite-success and check-suite-failure for the Actions-API-lag case (ucho PR #394/MNG-683, 2026-05-11).

Non-GitHub adapters (Trello, JIRA, Linear, Sentry) embed triggerResult in the job regardless of deferredRecheck; resolveTriggerResult() returns the pre-resolved result directly, skipping registry dispatch. A non-GitHub handler returning buildDeferredRecheckResult would therefore schedule a job that reuses the same agentType: null result rather than re-evaluating provider state. See src/triggers/README.md for the full authoring contract.

Config resolution

src/triggers/config-resolver.ts

Each trigger handler calls isTriggerEnabled() to check if it should fire. Resolution follows a three-tier cascade:

  1. Database overrides — agent_trigger_configs table entries per project/agent/event
  2. Definition defaults — defaultEnabled and default parameters from YAML definitions
  3. Legacy fallback — project_integrations.triggers JSONB (migrated automatically)

Context pipeline

Each trigger in a YAML agent definition can declare a contextPipeline — an ordered list of context-fetching steps that run before the agent starts:

Step Purpose
directoryListing List repository file structure
contextFiles Read key project files (README, etc.)
workItem Fetch work item details from PM tool
prepopulateTodos Pre-populate todo list from work item checklists
prContext Fetch PR details, GitHub changed-file metadata, locally verified compact per-file diffs from origin/<base>...HEAD, and CI checks; emit a SKIPPED FILES injection when files are omitted (over budget, deleted, binary/no patch, or local diff unavailable)
prConversation Fetch PR comments and review threads
pipelineSnapshot Fetch PM workflow/pipeline state and emit the single authoritative PipelineSnapshotSummary JSON context for backlog-manager
alertingIssue Fetch Sentry issue and event details

prContext budget and debugging

The review agent receives a compact per-file diff context, not full file contents. REVIEW_DIFF_CONTEXT_TOKEN_LIMIT (src/config/reviewConfig.ts, 200k tokens) caps the whole context and each file gets at most 10 % of it. GitHub's changed-file API supplies the file list and change counts; patch bodies come from the checked-out workspace via git diff origin/<base>...HEAD. Files that cannot fit or cannot be locally verified (deleted, binary/no text patch, local diff failure or empty patch, oversized patch, budget exhausted) are listed under SKIPPED FILES with instructions to fetch on demand via cascade-tools scm get-pr-diff --prNumber <N> --path <path> (add --outputFile <path> for large or one-line JSON diffs that would truncate stdout), Read, or Grep.

When a review misses something, check the PR context prepared log entry: included / skipped / skipReasons, patchSources, totalDiffTokens, perFileTokenCap, and localGitMismatches (GitHub's API patch differed from the local one). Also check the context-offload logs if the diff was written under .cascade/context/.

CI check status is informational, not fatal: fetchPRContextStep wraps only getCheckSuiteStatus in a try/catch. If the reviewer PAT lacks the Actions: Read permission, the Actions API returns 403 and the GetPRChecks injection degrades to an explicit "CI check status UNAVAILABLE" message (deliberately distinct from "No CI checks configured") plus a WARN CI check status unavailable log. getPR and getPRDiff stay fatal — a review without the PR itself is meaningless.

Shared Agent Execution

src/triggers/shared/agent-execution.ts

After a trigger matches, the shared execution layer handles the agent lifecycle. runAgentExecutionPipeline() is intentionally a thin facade: it keeps the source-compatible call signature used by PM, GitHub, Sentry, and manual paths, while delegating each execution concern to helper modules under src/triggers/shared/.

flowchart TD
    A[Trigger matched] --> B[Guard and context setup]
    B --> C[Validation and budget preflight]
    C -->|Blocked| D[Notify PM/callbacks and stop]
    C -->|Allowed| FG[Implementation freshness gate]
    FG -->|Blocked| FGSKIP[Post durable PM skip comment and stop]
    FG -->|Passed| E[Persist work-item and PR links]
    E --> F[PM lifecycle: prepareForAgent]
    F --> G[Run agent via engine]
    G --> H[Post-run side effects]
    H --> I[PM lifecycle cleanup and success/failure]
    I --> J[Source callbacks]
    J --> K[Follow-up dispatch]
    K --> L[Auto-debug if eligible]
Loading

This includes:

  • Context setup in agent-execution-runtime.ts: build the PMLifecycleManager, load agent lifecycle hooks, and re-resolve workItemId from PR links when a webhook arrived before the DB mapping existed.
  • Validation and lifecycle preflight in agent-execution-lifecycle.ts: validate PM/SCM integrations, notify PM/callbacks on validation failure, check workItemBudgetUsd, and run prepareForAgent.
  • Implementation freshness gate in implementation-freshness-gate.ts (MNG-1053): only fires for agentType === 'implementation' with a resolved workItemId. Reloads the live PM work item, terminal checklists (Implementation Steps, Acceptance Criteria), and agent_runs ownership state, then verifies any linked PRs through GitHub using the implementer persona token. Open or merged PRs, fully-completed terminal checklists, and active same-type runs short-circuit dispatch with a stable Implementation not started: PM comment (updating the existing ack comment when present, posting a new comment otherwise). Checklist read uncertainty always falls into needs_human_reconciliation; PR lookup uncertainty does the same when a DB/run-linked PR candidate exists. Closed-unmerged PRs do NOT permanently block reimplementation.
  • Work-item and PR traceability in agent-work-items.ts: create/update work-item records, maintain PR/work-item links before and after execution, fetch PR titles, and backfill run PR numbers.
  • Agent execution in agent-execution-runtime.ts: call runAgent() with the resolved input plus project, config, and remaining budget.
  • Post-run PM behavior in agent-pm-summary.ts and agent-execution-lifecycle.ts: post review/output summaries to the PM work item, handle artifacts, post budget warnings, clean up processing state, and call handleSuccess or handleFailure.
  • Follow-up dispatch in agent-execution-followups.ts: dispatch review after a successful implementation PR once CI is passing and the review dedup key is claimed (claimReviewDispatch — the same key the check-suite-success trigger uses, so the two paths cannot double-enqueue; this fires before the container exits, so review dispatch does not depend on GitHub webhook timing), and chain backlog-manager after a successful splitting run when the auto label/capacity checks allow it.
  • Auto-debug in agent-auto-debug.ts: fire-and-forget debug analysis for eligible failed or timed-out runs after callbacks and follow-up dispatch complete. It calls the shared triggerDebugAnalysis() runner, whose running/failed lifecycle is durable and cross-process — see Debug-analysis status below.

Credential scoping still happens before the facade runs. PM webhook handling enters provider credentials and PM provider scope before dispatch; GitHub and Sentry use webhook-execution.ts / credential-scope.ts to inject LLM keys, PM credentials, PM provider scope, and GitHub persona tokens as needed.

Debug-analysis status (durable, cross-process)

Post-mortem debug analysis runs in a separate worker container, so its running/failed lifecycle is tracked in the durable, cross-process debug_analysis_status table (see the debug_analysis_status table) rather than an in-process flag invisible to the dashboard process. Both entry points — the automatic agent-auto-debug.ts path above and the manual dashboard "Run Analysis" button — drive the shared triggerDebugAnalysis() runner, which marks running around the analysis, clears the row on success (a persisted debug_analyses row is then the completed signal), and marks failed on a catchable in-process error.

runs.getDebugAnalysisStatus reads this table uniformly in queue mode and local dev with precedence active running → completed → failed → idle; a running row older than DEBUG_ANALYSIS_RUNNING_STALE_MS (2h) self-heals to idle so a crashed worker never wedges the run. The deterministic debug-analysis-<runId> dashboard job (queue mode) provides idempotent re-enqueue and double-trigger dedup — not the status signal, since a BullMQ job reaches completed at container spawn rather than at analysis completion. An earlier in-memory Set (debug-status.ts) was removed because it was never visible to the dashboard process (MNG-1667). See Cross-process debug-analysis status for the service-level view.