import { AGGREGATE_REPORT, type PipelineReport, joinLiveRun, ruleReportName, supersedingRun, writePipelineReport, } from "../reporters/rule.reporter.ts"; import { BYPASSED_RUN, JOINED, STREAMED, SUPERSEDED, UNATTRIBUTED, claimedUnwritten, covered, healingHeld, narrowedRun, notMeasured, repairedWhileReading, runContended, scopeUnresolved, wroteOutsideScope, } from "../strings/pipeline.strings.ts"; import { GENERATED_DIR, NO_FIX_FLAG } from "../constants/path.constants.ts"; import { STAGES, type Stage } from "../types/rule.types.ts"; import { claimStanding, releaseStanding } from "../registries/claim.registry.ts"; import { projectRoot } from "../../../config/surface.config.ts"; import { runPipeline } from "../orchestrators/pipeline.orchestrator.ts"; import { toPosix } from "../iterators/file.iterator.ts"; const REPO_ROOT = projectRoot(); export const RESTATES: readonly string[] = [ "one_entry_point_staged_pipeline", "healing_is_default_in_every_entrypoint", "a_run_that_cannot_replace_the_aggregate_streams", "report_is_the_state", ]; const argValue = function argValue(argv: readonly string[], flag: string): string | null { const index = argv.indexOf(flag); if (index === -1) { return null; } return argv[index + 1] ?? null; }; const argList = function argList(argv: readonly string[], flag: string): string[] { const out: string[] = []; for (let i = 0; i < argv.length; i += 1) { if (argv[i] !== flag) { continue; } const value = argv[i + 1]; if (value === undefined) { continue; } let current = ""; for (let k = 0; k < value.length; k += 1) { if (value[k] === ",") { if (current.length > 0) { out.push(current); } current = ""; continue; } current += value[k]; } if (current.length > 0) { out.push(current); } } return out; }; const main = async function main(): Promise { const argv = process.argv.slice(2); const stageArg = argValue(argv, "--stage"); const stage = stageArg !== null && STAGES.includes(stageArg as Stage) ? (stageArg as Stage) : null; const asked = !argv.includes(NO_FIX_FLAG); const declaredCaller = argValue(argv, "--agent"); const runAgent = declaredCaller ?? "an undeclared caller"; const runAt = Date.now(); if (declaredCaller === null) { process.stdout.write(UNATTRIBUTED); } const standing = claimStanding(REPO_ROOT, argValue(argv, "--scope") ?? "whole", runAt, runAgent); if (standing.message !== null) { process.stdout.write(`OTHER RUNS ${standing.message}\n`); } const heal = asked && standing.overlapping.length === 0; if (asked && !heal) { process.stdout.write(healingHeld(standing.overlapping.length, standing.overlapping)); } if (standing.decision === "yield") { releaseStanding(REPO_ROOT, runAgent, runAt); process.stdout.write(JOINED); const joined = await joinLiveRun(REPO_ROOT, runAt); process.stdout.write(joined.message); process.exit(joined.code); } if (standing.covering.length > 0 && !heal) { releaseStanding(REPO_ROOT, runAgent, runAt); process.stdout.write(covered(standing.covering.length, standing.covering)); const joined = await joinLiveRun(REPO_ROOT, runAt); process.stdout.write(joined.message); process.exit(joined.code); } const result = await runPipeline({ bypass: argList(argv, "--bypass"), fix: heal, repoRoot: REPO_ROOT, ruleId: argValue(argv, "--rule"), scope: argValue(argv, "--scope"), stage, }); const report: PipelineReport = { agent: runAgent, at: runAt, authoritative: result.authoritative, bypassed: result.bypassedAny, derivations: { healingHeldBy: standing.overlapping, movedByThisRun: result.movedByThisRun, priorRunIncomplete: standing.incomplete, stagesBypassed: result.stages.filter((s) => s.bypassed).map((s) => s.rule), stagesWalked: result.stages.filter((s) => !s.bypassed).map((s) => s.rule), }, escaped: result.escaped, findings: result.findings, mode: heal ? "healing" : "held", moved: result.moved, mutated: heal, registered: result.registered, reports: result.stages.filter((s) => !s.bypassed).map((s) => `${GENERATED_DIR}/${ruleReportName(s.rule)}`), scanned: result.scanned, scope: result.scope, stages: result.stages, tool: "govern", unfulfilled: result.unfulfilled, verdict: result.findings.length === 0 ? "pass" : "fail", written: result.written, }; if (result.escaped.length > 0) { process.stdout.write(wroteOutsideScope(result.escaped)); } if (result.unfulfilled.length > 0) { process.stdout.write(claimedUnwritten(result.unfulfilled)); } if (result.unresolvedScope !== undefined) { releaseStanding(REPO_ROOT, runAgent, runAt); process.stdout.write(scopeUnresolved(result.unresolvedScope)); process.exit(2); } const superseded = report.authoritative ? supersedingRun(REPO_ROOT, report.at) : 0; const target = writePipelineReport(REPO_ROOT, report); const lines = result.stages.map((s) => { const mark = s.bypassed ? "BYPASS" : (s.findings === 0 ? " pass" : " FAIL"); const detail = s.bypassed ? "skipped" : `${s.findings} findings, ${s.healed} healed`; return ` ${mark} ${s.stage}/${s.rule} ${detail}`; }); process.stdout.write( `${report.verdict.toUpperCase()} registered=${report.registered} scanned=${report.scanned} ` + `findings=${report.findings.length}\n${lines.length > 0 ? `${lines.join("\n")}\n` : ""}${ report.bypassed ? BYPASSED_RUN : "" }${result.movedByThisRun.length === 0 ? "" : repairedWhileReading(result.movedByThisRun)}${ result.moved.length === 0 ? "" : runContended(result.moved.length, result.moved) }${report.authoritative ? "" : narrowedRun(report.scope, AGGREGATE_REPORT)}${ result.incomparable.length === 0 ? "" : notMeasured(result.incomparable) }${ superseded === 0 ? "" : SUPERSEDED }${target === null ? STREAMED : `report: ${toPosix(REPO_ROOT, target)}\n`}`, ); const unidentified = releaseStanding(REPO_ROOT, runAgent, runAt); if (unidentified !== null) { process.stdout.write(`${unidentified}\n`); } process.exit(report.verdict === "pass" ? 0 : 1); }; void main();