import { hasCommitsInLast24h } from './github-port.ts' import { resolvePrompt } from './prompts.ts' import { isScopedAutomation } from './scope-core.ts' import { runScoped } from './scoped-run.ts' import { isAutomationId, unknownAutomationMessage, normalizeRepoSlug, type AutomationId, type DevinMode, } from './shared.ts' import { findCiDedupe, getAutomation } from './storage-port.ts' export type RunNowInput = { automation: AutomationId /** Limit to one repo; default = all configured repos for the automation. */ repo?: string dryRun?: boolean /** Bypass automation.enabled check (jobs still respect platform enabled). */ force?: boolean /** Bypass only the automation.enabled check (does not skip the CI per-SHA dedupe). */ ignoreEnabled?: boolean /** Optional CI context for prod-ci-failure. */ ci?: { headSha?: string conclusion?: string htmlUrl?: string | null checkName?: string | null workflowName?: string | null event?: string } /** Skip the 24h commit gate even when configured (non-scoped automations). */ skipCommitGate?: boolean /** docs-freshness / keep-tests-tight: skip the weekly full sweep even on its weekday. */ scopedOnly?: boolean /** docs-freshness / keep-tests-tight: run the full-repo sweep now. */ fullSweep?: boolean } export type SpawnOrPreviewInput = { automation: AutomationId repo: string branch: string model: { id: string; params?: Array<{ id: string; value: string }> } prompt: string dryRun: boolean headSha: string | null skipped: string | null devinMode: DevinMode maxAcuLimit: number | null outcome?: string | null files?: string[] | null } export type SpawnOrPreviewResult = { ok: boolean automation: AutomationId repo?: string branch?: string dryRun?: boolean skipped?: string | null [key: string]: unknown } export type SpawnOrPreview = (input: SpawnOrPreviewInput) => Promise function requireAutomationId(value: unknown): AutomationId { if (!isAutomationId(value)) { throw new Error(unknownAutomationMessage()) } return value } function buildExtra(automation: AutomationId, ci: RunNowInput['ci']) { if (automation !== 'prod-ci-failure' || !ci) return '' const lines = [ '## Failure context', ci.event ? `- Event: ${ci.event}` : null, ci.conclusion ? `- Conclusion: ${ci.conclusion}` : null, ci.headSha ? `- Head SHA: \`${ci.headSha}\`` : null, ci.checkName ? `- Check: ${ci.checkName}` : null, ci.workflowName ? `- Workflow: ${ci.workflowName}` : null, ci.htmlUrl ? `- URL: ${ci.htmlUrl}` : null, ].filter(Boolean) return lines.join('\n') } /** * Run one automation now against its configured repos (or a single repo). * Pass `dryRun: true` to resolve prompts without spawning Devin sessions * or Cursor agents (Devin automations return the exact create-session request). */ export function createRunNow(spawnOrPreview: SpawnOrPreview) { return async function runNow(input: RunNowInput) { const automation = requireAutomationId(input?.automation) const dryRun = input.dryRun === true const force = input.force === true const ignoreEnabled = input.ignoreEnabled === true const config = await getAutomation(automation) if (!config.enabled && !dryRun && !force && !ignoreEnabled) { return { ok: true, automation, skipped: 'automation-disabled', hint: 'Set enabled:true via config.set, or pass force:true / dryRun:true', } } const repos = input.repo ? [normalizeRepoSlug(input.repo)] : config.repos.map((r) => normalizeRepoSlug(r)) if (repos.length === 0) { return { ok: true, automation, skipped: 'no-repos' } } const results = [] for (const repo of repos) { if (isScopedAutomation(automation)) { try { results.push( await runScoped({ automation, repo, config, dryRun, scopedOnly: input.scopedOnly, fullSweep: input.fullSweep, spawn: (launch) => spawnOrPreview({ automation, repo, branch: config.branch, model: config.model, prompt: launch.prompt, dryRun, headSha: null, skipped: launch.skipped, devinMode: config.devinMode, maxAcuLimit: config.maxAcuLimit, outcome: launch.outcome, files: launch.files, }), }), ) } catch (error) { results.push({ ok: false, automation, repo, error: error instanceof Error ? error.message : String(error) }) } continue } const headSha = input.ci?.headSha ?? null if (automation === 'prod-ci-failure' && headSha && !dryRun && !force) { const existing = await findCiDedupe(repo, headSha) if (existing) { results.push({ ok: true, automation, repo, branch: config.branch, skipped: 'already-launched-for-sha', agentId: existing.agent_id, agentUrl: existing.agent_url, headSha, }) continue } } let skipped: string | null = null if (config.skipIfNoCommits24h && !input.skipCommitGate) { try { const hasCommits = await hasCommitsInLast24h({ repo, branch: config.branch, }) if (!hasCommits) skipped = 'no-commits-24h' } catch (error) { const message = error instanceof Error ? error.message : String(error) skipped = `commit-check-failed:${message.slice(0, 120)}` } } const prompt = resolvePrompt({ automation, repo, branch: config.branch, extra: buildExtra(automation, input.ci), notify: config.notify, }) results.push( await spawnOrPreview({ automation, repo, branch: config.branch, model: config.model, prompt, dryRun, headSha, skipped, devinMode: config.devinMode, maxAcuLimit: config.maxAcuLimit, }), ) } return { ok: true, automation, dryRun, results, } } }