Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 2x 42x 42x 42x 42x 42x 42x 41x 41x 40x 40x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 2x 42x 42x 42x 40x 40x 2x 38x 1x 37x 37x 35x 35x 37x 42x 42x 42x 5x 5x 5x 5x 5x 5x 5x 5x | /**
* Trigger Executor
*
* Executes a single trigger and manages associated GitHub check runs.
* Extracted from main.ts to enable isolated testing and clearer dependencies.
*/
import type { Octokit } from '@octokit/rest';
import { Sentry } from '../../sentry.js';
import { ActionFailedError } from '../workflow/base.js';
import type { ResolvedTrigger } from '../../config/loader.js';
import type { EventContext, SkillReport, SeverityThreshold, ConfidenceThreshold } from '../../types/index.js';
import type { RenderResult } from '../../output/types.js';
import type { OutputMode } from '../../cli/output/tty.js';
import { resolveSkillAsync } from '../../skills/loader.js';
import { filterContextByPaths } from '../../triggers/matcher.js';
import { runSkillTask, createDefaultCallbacks } from '../../cli/output/tasks.js';
import type { SkillTaskOptions } from '../../cli/output/tasks.js';
import { renderSkillReport } from '../../output/renderer.js';
import {
createSkillCheck,
updateSkillCheck,
failSkillCheck,
} from '../../output/github-checks.js';
import { logGroup, logGroupEnd } from '../workflow/base.js';
import { DEFAULT_FILE_CONCURRENCY } from '../../sdk/types.js';
import { SkillRunnerError } from '../../sdk/errors.js';
import type { Semaphore } from '../../utils/index.js';
import { Verbosity } from '../../cli/output/verbosity.js';
import type { ProviderFailureCircuitBreaker } from '../../sdk/circuit-breaker.js';
import { assertValidPiModelSelectors } from '../../sdk/runtimes/model-selectors.js';
import { captureActionTriggerError } from '../error-reporting.js';
/** Log-mode output for CI: no TTY, no color. */
const CI_OUTPUT_MODE: OutputMode = { isTTY: false, supportsColor: false, columns: 120 };
// -----------------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------------
/**
* Dependencies required for trigger execution.
* Making these explicit enables testing with mock implementations.
*/
export interface TriggerExecutorDeps {
octokit: Octokit;
context: EventContext;
anthropicApiKey: string;
claudePath?: string;
/** Global fail-on from action inputs (trigger-specific takes precedence) */
globalFailOn?: SeverityThreshold;
/** Global report-on from action inputs (trigger-specific takes precedence) */
globalReportOn?: SeverityThreshold;
/** Global max-findings from action inputs (trigger-specific takes precedence) */
globalMaxFindings: number;
/** Global request-changes from action inputs (trigger-specific takes precedence) */
globalRequestChanges?: boolean;
/** Global fail-check from action inputs (trigger-specific takes precedence) */
globalFailCheck?: boolean;
/** Global semaphore for limiting concurrent file analyses across triggers */
semaphore?: Semaphore;
/** Shared controller for stopping the whole action run */
abortController?: AbortController;
/** Shared circuit breaker for auth/provider failures */
circuitBreaker?: ProviderFailureCircuitBreaker;
}
/**
* Result from executing a single trigger.
*/
export interface TriggerResult {
triggerName: string;
report?: SkillReport;
renderResult?: RenderResult;
failOn?: SeverityThreshold;
reportOn?: SeverityThreshold;
minConfidence?: ConfidenceThreshold;
reportOnSuccess?: boolean;
requestChanges?: boolean;
failCheck?: boolean;
checkRunUrl?: string;
maxFindings?: number;
error?: unknown;
}
// -----------------------------------------------------------------------------
// Executor
// -----------------------------------------------------------------------------
/**
* Execute a single trigger and return results.
*
* Handles:
* - Creating/updating GitHub check runs
* - Running the skill via Claude Code SDK
* - Rendering results for GitHub review
*/
export async function executeTrigger(
trigger: ResolvedTrigger,
deps: TriggerExecutorDeps
): Promise<TriggerResult> {
return Sentry.startSpan(
{ op: 'trigger.execute', name: `execute ${trigger.name}` },
async (span) => {
span.setAttribute('gen_ai.agent.name', trigger.skill);
span.setAttribute('warden.trigger.name', trigger.name);
const { octokit, context, anthropicApiKey, claudePath } = deps;
logGroup(`Running trigger: ${trigger.name} (skill: ${trigger.skill})`);
// Create skill check (only for PRs)
let skillCheckId: number | undefined;
let skillCheckUrl: string | undefined;
if (context.pullRequest) {
try {
const skillCheck = await createSkillCheck(octokit, trigger.skill, {
owner: context.repository.owner,
repo: context.repository.name,
headSha: context.pullRequest.headSha,
});
skillCheckId = skillCheck.checkRunId;
skillCheckUrl = skillCheck.url;
} catch (error) {
console.error(`::warning::Failed to create skill check for ${trigger.skill}: ${error}`);
}
}
const failOn = trigger.failOn ?? deps.globalFailOn;
const reportOn = trigger.reportOn ?? deps.globalReportOn;
const minConfidence = trigger.minConfidence ?? 'medium';
const requestChanges = trigger.requestChanges ?? deps.globalRequestChanges;
const failCheck = trigger.failCheck ?? deps.globalFailCheck;
const skillRoot = trigger.useBuiltinSkill ? undefined : (trigger.skillRoot ?? context.repoPath);
try {
assertValidPiModelSelectors([trigger]);
const taskOptions: SkillTaskOptions = {
name: trigger.name,
displayName: trigger.skill,
triggerName: trigger.name,
failOn,
resolveSkill: () => resolveSkillAsync(trigger.skill, skillRoot, {
remote: trigger.remote,
}),
context: filterContextByPaths(context, trigger.filters),
runnerOptions: {
apiKey: anthropicApiKey,
model: trigger.model,
runtime: trigger.runtime,
auxiliaryModel: trigger.auxiliaryModel,
synthesisModel: trigger.synthesisModel,
maxTurns: trigger.maxTurns,
batchDelayMs: trigger.batchDelayMs,
maxContextFiles: trigger.maxContextFiles,
pathToClaudeCodeExecutable: claudePath,
auxiliaryMaxRetries: trigger.auxiliaryMaxRetries,
verifyFindings: trigger.verifyFindings,
abortController: deps.abortController,
circuitBreaker: deps.circuitBreaker,
},
};
const callbacks = createDefaultCallbacks([taskOptions], CI_OUTPUT_MODE, Verbosity.Normal);
const fileConcurrency = deps.semaphore ? Number.MAX_SAFE_INTEGER : DEFAULT_FILE_CONCURRENCY;
const result = await runSkillTask(taskOptions, fileConcurrency, callbacks, deps.semaphore);
const report = result.report;
if (!report) {
throw result.error ?? new Error('Skill task returned no report');
}
// runSkillTask now synthesizes a report even on failure so the CLI
// can log it as JSONL. The action's fail-check path still expects a
// thrown error, so re-throw when the report carries one. Preserve
// the ErrorCode in the fallback so Sentry / failSkillCheck see a
// typed error.
if (report.error) {
throw (
result.error ??
new SkillRunnerError(report.error.message, { code: report.error.code })
);
}
console.log(`Found ${report.findings.length} findings`);
// Update skill check with results
if (skillCheckId && context.pullRequest) {
try {
await updateSkillCheck(octokit, skillCheckId, report, {
owner: context.repository.owner,
repo: context.repository.name,
headSha: context.pullRequest.headSha,
failOn,
reportOn,
minConfidence,
failCheck,
});
} catch (error) {
console.error(`::warning::Failed to update skill check for ${trigger.skill}: ${error}`);
}
}
const maxFindings = trigger.maxFindings ?? deps.globalMaxFindings;
const renderResult =
reportOn !== 'off'
? renderSkillReport(report, {
maxFindings,
reportOn,
minConfidence,
failOn,
requestChanges,
checkRunUrl: skillCheckUrl,
totalFindings: report.findings.length,
})
: undefined;
logGroupEnd();
return {
triggerName: trigger.name,
report,
renderResult,
failOn,
reportOn,
minConfidence,
reportOnSuccess: trigger.reportOnSuccess,
requestChanges,
failCheck,
checkRunUrl: skillCheckUrl,
maxFindings,
};
} catch (error) {
Iif (error instanceof ActionFailedError) throw error;
captureActionTriggerError(error, {
triggerName: trigger.name,
skillName: trigger.skill,
});
// Mark skill check as failed
Eif (skillCheckId && context.pullRequest) {
try {
await failSkillCheck(octokit, skillCheckId, error, {
owner: context.repository.owner,
repo: context.repository.name,
headSha: context.pullRequest.headSha,
});
} catch (checkError) {
console.error(`::warning::Failed to mark skill check as failed: ${checkError}`);
}
}
console.error(`::warning::Trigger ${trigger.name} failed: ${error}`);
logGroupEnd();
return { triggerName: trigger.name, error };
}
},
);
}
|