import { buildEvidenceManifest } from "./evidence-manifest.ts"; import { selectSpecialists } from "./orchestration-route.ts"; import { recordRoutingObservation } from "./routing-observation.ts"; import { closeReview } from "./review-closure.ts"; import { chooseReviewerRerun } from "./rerun-policy.ts"; // Only recognize a closed, sensitive-object prohibition grammar. Unknown wording // remains sensitive; never erase an entire mixed sentence or affirmative clause. function sensitiveWorkSignals(task: string): { secret: boolean; cloud: boolean } { const object = "(?:secrets?|credentials?|cloud|aws)"; const objects = `${object}(?:\\s*(?:,|or|and)\\s*${object})*`; // A closed list of prohibited operations, not arbitrary prose after "no". const operation = `(?:${object}(?:\\s+(?:operations|calls|writes))?|deploy|sql(?:\\s+execution)?|commits|localserve|version bumps|package downloads)`; const operations = `${operation}(?:\\s*(?:/|,|or|and)\\s*${operation})*`; const gerund = "(?:reading|accessing|exposing|using|modifying|rotating|printing|copying)"; const prohibition = new RegExp(`^(?:do not (?:read|access|expose|use|modify|rotate|perform) ${objects}(?: operations)?|no ${operations}|without ${gerund}(?:\\s+(?:or|and)\\s+${gerund})* ${objects})$`, "i"); const remaining = task.split(/[;.\n]/).filter(clause => { const directive = clause.trim().replace(/^(?:[-*]|\d+[)])\s+/, "").replace(/!+$/, "").trim(); return !prohibition.test(directive); }).join(";"); return { secret: /secret|credential/i.test(remaining), cloud: /\b(cloud|aws)\b/i.test(remaining) }; } export function activeOrchestrationHandoff(input: { agent: string; task: string; scope: string[]; revision: string; changes: string[]; exitCode: number; command?: string; testsExecuted?: number; model?: string; elapsedMs?: number; questions: string[]; evidenceRefs: string[]; approvedPlan: boolean; risk: "low" | "high"; scopeBreadth: "narrow" | "wide"; sharedCode: boolean; }) { const route = selectSpecialists({ scopeBreadth: input.scopeBreadth, risk: input.risk, reversible: true, approvedPlan: input.approvedPlan, unresolvedForks: input.questions, selfDeclaredSmall: /small/i.test(input.task), }, sensitiveWorkSignals(input.task)); const manifest = buildEvidenceManifest({ revision: input.revision || "unknown", scope: input.scope, changes: input.changes, commands: input.command ? [{ command: input.command, exitCode: input.exitCode }] : [], tests: { executed: input.testsExecuted ?? 0, passed: 0, failed: 0 }, evidenceRefs: input.evidenceRefs, openAssertions: input.questions, observedRevision: input.revision || "unknown", }); const closure = closeReview({ revision: input.revision || "unknown", scope: input.scope, prose: input.agent.includes("review") ? "PASS" : "", origin: input.evidenceRefs.length ? "declared" : "missing", structuredReturn: input.evidenceRefs.length ? { verdict: "PASS", evidenceRefs: input.evidenceRefs } : undefined, }); const rerun = chooseReviewerRerun({ impact: input.scopeBreadth === "wide" ? "wide" : "narrow", sharedCode: input.sharedCode, selector: input.scope[0] ?? "", flaky: false, policyMandate: false, builderPassed: input.exitCode === 0, reviewerRan: input.agent.includes("review"), }); const observation = recordRoutingObservation({ taskClass: route.stages.join("-"), model: input.model ?? "unknown", wallMs: input.elapsedMs ?? 0, retries: 0, refusals: 0, contractCompliant: closure.status !== "unproven", accepted: closure.status === "closed", }); return { route, manifest, closure, rerun, observation, routingChanged: false as const }; } export function reviewMayClose(input: { revision: string; scope: string[]; output: string; evidenceRef?: string; declared: boolean; wide: boolean }): boolean { const rerun = chooseReviewerRerun({ impact: input.wide ? "wide" : "narrow", sharedCode: input.wide, selector: input.scope[0] ?? "", flaky: false, policyMandate: false, builderPassed: true, reviewerRan: true, }); if (!rerun.accepted) return false; if (rerun.mode === "full" && !/full regression|tests_run:\s*full/i.test(input.output)) return false; return closeReview({ revision: input.revision, scope: input.scope, prose: input.output, origin: input.declared ? "declared" : input.evidenceRef ? "extracted" : "missing", structuredReturn: /APPROVE/i.test(input.output) ? { verdict: "PASS", evidenceRefs: input.evidenceRef ? [input.evidenceRef] : [] } : undefined, evidenceComplete: !!input.evidenceRef, evidenceRevision: input.revision, }).status === "closed"; }