import { parseIssueLabels, parseRedFirst, testIntegrityBlockers, type RedFirstReceipt, type TestIntegrity } from "./test-integrity.ts"; import { planConformanceBlockers, type PlanConformance } from "./plan-conformance.ts"; import { controlWriteSpan } from "./control-write-trace.ts"; import { DEFAULT_MAX_NITS, findingsForHead, findingHead, findingTally, type ReviewFindingSet, type ReviewFindingTally } from "./review-findings.ts"; import type { CostPlan } from "./cost-governance.ts"; import { bandsConfig, type BandsConfig } from "./sdlc-metrics.ts"; import { securityPassBlockers, securityPolicy, type SecurityPassView, type SecurityPolicy } from "./security-pass.ts"; import { configPathList, evalGapBlockers, evalGateView, evalRequiredChecks, type EvalGate, type EvalResult } from "./eval-gate.ts"; import { SECURITY_SOURCES, type SecuritySource } from "./security-baseline.ts"; import { criteriaReceipt, type CriteriaReceipt, type CriteriaView, type LegacyPhaseReceipt } from "./workflow.ts"; import { constants, openSync, fstatSync, readSync, closeSync } from "node:fs"; import { join } from "node:path"; import { spawn } from "node:child_process"; import { childEnv, stopOwnedProcess } from "./child-process.ts"; import { hostExecutable } from "./host-executable.ts"; import { readBranchCi } from "./sdlc-branch-ci.ts"; /** The off-mode default (#257). Off output, receipts and schemas keep this shape. */ export const SDLC_STAGES = ["spec", "plan", "implement", "review", "verify", "merge", "release"] as const; /** The enabled default (#481 S0): the AI-Native SDLC Playbook stage model. */ export const PLAYBOOK_STAGES = ["intent", "spec", "plan", "build", "test", "review", "merge", "release", "maintain"] as const; export type SdlcBaseStage = typeof SDLC_STAGES[number] | typeof PLAYBOOK_STAGES[number]; const BASES: readonly string[] = [...new Set([...SDLC_STAGES, ...PLAYBOOK_STAGES])]; /** Legacy ids read as their playbook stage; a receipt written with one stores the canonical id. */ const ALIASES: Record = { implement: "build", verify: "test" }; export const canonicalStage = (stage: string): string => ALIASES[stage] ?? stage; const PLAYBOOK_LABEL: Record = { intent: "Plan", spec: "Design", plan: "Build", build: "Build", test: "Test", review: "Deploy", merge: "Deploy", release: "Deploy", maintain: "Maintain" }; export const playbookStage = (base: string): string | undefined => PLAYBOOK_LABEL[canonicalStage(base)]; /** #481 S8: `workflow` and the `phase` gate/receipt name load with a warning before this hub version and refuse from it. */ export const RETIRED_WORKFLOW_FROM = "0.13.0"; const olderThan = (version: string, limit: string): boolean => { const a = version.split(/[.-]/).slice(0, 3).map(Number), b = limit.split(".").map(Number); for (let i = 0; i < 3; i++) if ((a[i] ?? 0) !== (b[i] ?? 0)) return (a[i] ?? 0) < (b[i] ?? 0); return false; }; export type SdlcStage = string; export type StageGate = "issue" | "pr" | "local" | "review" | "checks" | "criteria"; export interface StageDefinition { base: SdlcBaseStage; actor: string; entry_gates: StageGate[]; exit_gates: StageGate[]; after: string[]; } export type SdlcMode = "off" | "advise" | "enforce"; /** `workflow: "gsd-lite"` marks the retired setting during its window (#481 S8): every task then needs a criteria checklist. */ /** #786: `off` never merges, `dry-run` reports the decision and runs nothing, `on` runs the guarded `gh pr merge`. */ export type AutoMergeMode = "off" | "dry-run" | "on"; export const AUTO_MERGE_MODES: readonly AutoMergeMode[] = ["off", "dry-run", "on"]; /** #481 S7: which GitHub alert sources the scan reads, how often, and for which repository (absent: the project repository). */ export interface SecurityScanConfig { everyH: number; sources: SecuritySource[]; repo?: string } /** #786: one repository's own gate: its required checks (before the top-level list) and its own merge mode. */ export interface RepoMergePolicy { requiredChecks?: string[]; autoMerge?: AutoMergeMode } export interface SdlcConfig { workflow?: "gsd-lite"; maxNits?: number; /** #481 S7: optional control bands per metric; absent means none. */ bands?: BandsConfig; /** #481 S7: the scheduled security-scan baseline; absent means none is scheduled. */ securityScan?: SecurityScanConfig; /** #481 S5: the one check a configuration PR needs; no default, the person names it. */ evalCheck?: string; /** #481 S5: patterns that mark a PR as a configuration change; absent reads as the defaults in eval-gate.ts, an empty list turns the gate off. */ configPaths?: string[]; /** #1403 A2: sensitive paths that need a recorded security pass, and the review companions that apply; absent means the feature is off. */ security?: SecurityPolicy; mode: SdlcMode; requiredChecks: string[]; /** #786: absent reads as "off"; written only when the file names it. */ autoMerge?: AutoMergeMode; /** #786: per-repository tables, keyed by lowercase owner/name. */ repos?: Record; stages: SdlcStage[]; definitions?: Record; deprecations?: string[]; } export interface PlanReviewReceipt { planner: string; planAuthor?: string | null; selfReview?: true; planningRevision: number; recordedAt: number; scope?: CostPlan; issue?: PlanIssueMetadata; } /** Host-observed guarded merge evidence. Never accepted from a tool or restored across an artifact relink. */ export interface MergeRecord { repo: string; pr: number; head: string; at: number; state: "merged" | "not_merged" | "unknown"; exit: number; mergeCommit?: string; cleanup: "not_started" | "claimed" | "deleted" | "already_gone" | "skipped" | "unknown"; reason?: "not_merged" | "missing_commit" | "metadata_unknown" | "identity_changed" | "protected_branch" | "default_branch" | "head_changed" | "authority_changed" | "delete_unconfirmed"; ownerNotice: "not_started" | "claimed" | "sent" | "unknown"; /** #481 S7: the merged PR's review and CI figures, read once at merge by the host. Absent when the PR could not be read; fields inside are null when unknown. */ cost?: import("./cost-governance.ts").MergeCost; } /** A merge observed by readback, with no locally executed merge or cleanup. */ export interface ReadbackMergeRecord { repo: string; pr: number; head: string; at: number; state: "merged"; mergeCommit?: string; cost?: import("./cost-governance.ts").MergeCost; } export interface SdlcReceipt { merge?: MergeRecord; mergeReadback?: ReadbackMergeRecord; stage: SdlcStage; planReview?: PlanReviewReceipt; criteria?: CriteriaReceipt; /** Stored before #481 S8 and read as `criteria`; never written again. */ phase?: LegacyPhaseReceipt; /** The person's spec acceptance (#481 S1): set only by Tasks.acceptSpec, never by hub_sdlc_record. */ acceptance?: SpecAcceptance; repo?: string; issue?: number; pr?: number; head?: string; reviewedHead?: string; localHead?: string; localResult?: "passed" | "failed" | "unknown"; observed?: number; redFirst?: RedFirstReceipt; } export interface PrEvidence { head: string; state: "OPEN" | "MERGED" | "CLOSED"; checks: { name: string; state: "passed" | "failed" | "pending" | "unknown" }[]; review: "approved" | "changes_requested" | "unknown"; unresolved: number | null; pendingReviews: number | null; paths: string[]; fileChanges?: { path: string; change: string }[]; issueLabels?: { issue: number; labels: string[] | null }; pathsComplete?: boolean; /** All affected path identities are represented; rename/copy destinations alone cannot certify S2. */ typesComplete?: boolean; draft?: boolean; base?: string; baseHead?: string; mergeCommit?: string; /** #786: the PR's head branch name, read so a release branch is refused; never used as an identity. */ headBranch?: string; } const sha = (value: unknown): value is string => typeof value === "string" && /^[0-9a-f]{40}$/.test(value); const repo = (value: unknown): value is string => typeof value === "string" && /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(value) && !value.startsWith("-"); const id = (value: unknown): value is number => typeof value === "number" && Number.isSafeInteger(value) && value > 0; const STAGE_ID = /^[a-z][a-z0-9_-]{0,47}$/; // #451 froze this literal as non-peer gate names ("local" is the local-check gate). "phase" is read as criteria (#481 S8). const STAGE_GATES = ["issue", "pr", "local", "review", "checks", "phase"]; const GATES = STAGE_GATES.map(gate => gate === "phase" ? "criteria" : gate) as StageGate[]; const baseActor = (base: string) => base === "review" || canonicalStage(base) === "test" ? "reviewer" : base === "merge" || base === "release" ? "person" : "owner/planner"; /** A receipt's checklist under either stored name. */ export const checklistOf = (receipt: SdlcReceipt | undefined): CriteriaReceipt | undefined => receipt?.criteria ?? receipt?.phase; export function stageDefinition(config: SdlcConfig, stage: string): StageDefinition | undefined { if (!config.stages.includes(stage)) return stage !== canonicalStage(stage) ? stageDefinition(config, canonicalStage(stage)) : undefined; if (config.definitions && Object.hasOwn(config.definitions, stage)) return config.definitions[stage]; if (!BASES.includes(stage)) return undefined; const base = stage as SdlcBaseStage; return { base, actor: baseActor(base), entry_gates: [], exit_gates: [], after: [] }; } export function lifecycleStage(config: SdlcConfig, base: SdlcBaseStage): string { return config.stages.find(stage => stageDefinition(config, stage)?.base === base) ?? base; } function definitions(raw: unknown, stages: unknown, phaseAlias: { allowed: boolean; used: boolean }): { stages: string[]; definitions: Record } { if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).length > 32) throw new Error("sdlc.definitions: expected at most 32 stage definitions"); if (!Array.isArray(stages) || !stages.length || stages.length > 32 || stages.some(s => typeof s !== "string" || !STAGE_ID.test(s)) || new Set(stages).size !== stages.length) throw new Error("sdlc.stages: expected 1..32 distinct bounded stage ids"); const result: Record = Object.create(null); for (const [id, value] of Object.entries(raw)) { if (!STAGE_ID.test(id) || !stages.includes(id) || !value || typeof value !== "object" || Array.isArray(value)) throw new Error("sdlc.definitions: invalid or unlisted stage"); const def = value as Record; if (Object.keys(def).some(k => !["base", "actor", "entry_gates", "exit_gates", "after"].includes(k)) || !BASES.includes(def.base as string)) throw new Error("sdlc.definitions: unknown field or base"); const base = def.base as SdlcBaseStage; if ((SDLC_STAGES as readonly string[]).includes(id) && id !== base) throw new Error("sdlc.definitions: built-in stage base cannot change"); if (!(SDLC_STAGES as readonly string[]).includes(id) && (PLAYBOOK_STAGES as readonly string[]).includes(id) && canonicalStage(id) !== canonicalStage(base)) throw new Error("sdlc.definitions: built-in playbook stage base cannot change"); const actor = def.actor ?? baseActor(base); if (typeof actor !== "string" || actor !== baseActor(base) && !["owner", "reviewer", "person", "planner"].includes(actor)) throw new Error("sdlc.definitions.actor: unknown actor"); const gates = (key: string): StageGate[] => { const given = def[key] ?? []; const retired = Array.isArray(given) && given.includes("phase"); if (retired && !phaseAlias.allowed) throw new Error(`sdlc.definitions.${key}: the phase gate was retired in ${RETIRED_WORKFLOW_FROM} (#481); name it criteria`); if (retired) phaseAlias.used = true; const list = Array.isArray(given) ? given.map(g => g === "phase" ? "criteria" : g) : given; if (!Array.isArray(list) || list.length > 6 || list.some(g => !GATES.includes(g)) || new Set(list).size !== list.length) throw new Error(`sdlc.definitions.${key}: unknown or duplicate gate`); return [...list] as StageGate[]; }; const after = def.after ?? []; if (!Array.isArray(after) || after.length > 8 || after.some(a => typeof a !== "string" || !stages.includes(a) || stages.indexOf(a) >= stages.indexOf(id)) || new Set(after).size !== after.length) throw new Error("sdlc.definitions.after: dependencies must be distinct earlier stages (no missing, self, forward or cyclic edge)"); result[id] = { base, actor: actor as string, entry_gates: gates("entry_gates"), exit_gates: gates("exit_gates"), after: [...after] as string[] }; } const config = { stages, definitions: result } as SdlcConfig; if (stages.some(s => !stageDefinition(config, s)) || !["review", "merge"].every(base => stages.some(s => stageDefinition(config, s)?.base === base))) throw new Error("sdlc.stages: define every custom id and include review and merge base destinations"); return { stages: [...stages], definitions: result }; } const checkList = (value: unknown, what: string): string[] => { if (!Array.isArray(value) || value.length > 32 || value.some(c => typeof c !== "string" || !c.trim() || c.length > 120 || /[\r\n\x00-\x1f]/.test(c)) || new Set(value).size !== value.length) throw new Error(`${what}: expected at most 32 unique nonempty names`); return [...value] as string[]; }; const evalCheckName = (value: unknown): string => { if (typeof value !== "string" || !value.trim() || value.length > 120 || /[\r\n\x00-\x1f]/.test(value)) throw new Error("sdlc.eval_check: expected one nonempty check name"); return value; }; const securityScanConfig = (value: unknown): SecurityScanConfig => { const fail = (path: string, why: string): never => { throw new Error(`sdlc.security_scan${path}: ${why}`); }; if (!value || typeof value !== "object" || Array.isArray(value)) return fail("", "expected a table"); const raw = value as Record; for (const key of Object.keys(raw)) if (!["every_h", "sources", "repo"].includes(key)) fail(`.${key}`, "unknown field"); const everyH = raw.every_h === undefined ? 24 : raw.every_h; if (typeof everyH !== "number" || !Number.isSafeInteger(everyH) || everyH < 1 || everyH > 720) fail(".every_h", "expected an integer number of hours from 1 to 720"); const sources = raw.sources; if (!Array.isArray(sources) || !sources.length || sources.length > SECURITY_SOURCES.length || new Set(sources).size !== sources.length || sources.some(s => !SECURITY_SOURCES.includes(s as SecuritySource))) fail(".sources", `expected a nonempty list of unique names from ${SECURITY_SOURCES.join(", ")}; the scan has no default source`); if (raw.repo !== undefined && !repo(raw.repo)) fail(".repo", "expected owner/repository"); return { everyH: everyH as number, sources: [...sources as SecuritySource[]], ...(raw.repo === undefined ? {} : { repo: raw.repo as string }) }; }; const autoMergeMode = (value: unknown, what: string): AutoMergeMode => { if (!AUTO_MERGE_MODES.includes(value as AutoMergeMode)) throw new Error(`${what}: expected off, dry-run or on`); return value as AutoMergeMode; }; /** #786: `[repos."owner/name"]` tables. Unknown keys are refused like everywhere else in this file. */ function mergeRepos(value: unknown): Record | undefined { if (value === undefined) return undefined; if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length > 16) throw new Error("sdlc.repos: expected at most 16 tables named owner/repository"); const result: Record = Object.create(null); for (const [name, table] of Object.entries(value)) { if (!repo(name)) throw new Error(`sdlc.repos: "${String(name).slice(0, 60)}" is not an owner/repository name`); if (!table || typeof table !== "object" || Array.isArray(table)) throw new Error(`sdlc.repos.${name}: expected a table`); const row = table as Record; for (const key of Object.keys(row)) if (!["required_checks", "auto_merge"].includes(key)) throw new Error(`sdlc.repos.${name}.${key}: unknown field`); const key = name.toLowerCase(); if (result[key]) throw new Error(`sdlc.repos.${name}: repeats another table (names are case-insensitive)`); result[key] = { ...(row.required_checks === undefined ? {} : { requiredChecks: checkList(row.required_checks, `sdlc.repos.${name}.required_checks`) }), ...(row.auto_merge === undefined ? {} : { autoMerge: autoMergeMode(row.auto_merge, `sdlc.repos.${name}.auto_merge`) }) }; } return result; } /** `version` is the running hub's, which closes the #481 S8 window; the daemon and Tasks pass it. Without one the window is open. */ export function sdlcConfig(value: unknown, version?: string): SdlcConfig { if (value === undefined) return { mode: "off", requiredChecks: [], stages: [...SDLC_STAGES] }; if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("sdlc: expected a table"); const raw = value as Record; for (const key of Object.keys(raw)) if (!["mode", "required_checks", "stages", "workflow", "definitions", "max_nits", "bands", "auto_merge", "repos", "eval_check", "config_paths", "security_scan", "security"].includes(key)) throw new Error(`sdlc.${key}: unknown field`); const maxNits = raw.max_nits === undefined ? DEFAULT_MAX_NITS : raw.max_nits; if (typeof maxNits !== "number" || !Number.isSafeInteger(maxNits) || maxNits < 0 || maxNits > 64) throw new Error("sdlc.max_nits: expected an integer from 0 to 64"); const bands = bandsConfig(raw.bands), bandConfig = Object.keys(bands).length ? { bands } : {}; const evalConfig = { ...(raw.eval_check === undefined ? {} : { evalCheck: evalCheckName(raw.eval_check) }), ...(raw.config_paths === undefined ? {} : { configPaths: configPathList(raw.config_paths, "sdlc.config_paths") }), }; const findingConfig = { ...(raw.max_nits === undefined ? {} : { maxNits }), ...bandConfig, ...evalConfig, ...(raw.security_scan === undefined ? {} : { securityScan: securityScanConfig(raw.security_scan) }), ...(raw.security === undefined ? {} : { security: securityPolicy(raw.security) }) }; const mode = raw.mode ?? "off"; if (mode !== "off" && mode !== "advise" && mode !== "enforce") throw new Error("sdlc.mode: expected off, advise or enforce"); // ponytail: the #481 S8 window is a fixed version; delete `workflow`, the phase aliases and their tests once it passes. const window = version === undefined || olderThan(version, RETIRED_WORKFLOW_FROM); if (raw.workflow !== undefined && !window) throw new Error(`sdlc.workflow: retired in ${RETIRED_WORKFLOW_FROM} (#481); remove it and record a criteria checklist instead`); const workflow = raw.workflow ?? "none"; if (workflow !== "none" && workflow !== "gsd-lite") throw new Error("sdlc.workflow: expected none or gsd-lite"); const notes = workflow === "gsd-lite" ? [`sdlc.workflow = "gsd-lite" is deprecated and stops loading in ${RETIRED_WORKFLOW_FROM} (#481): remove it; every task then needs a criteria checklist recorded with hub_sdlc_record`] : []; const phaseAlias = { allowed: window, used: false }; const checks = checkList(raw.required_checks ?? [], "sdlc.required_checks"); // #786: absent keys add nothing to the config, so the off-mode default keeps its #257 shape byte for byte. const mergeConfig = { ...(raw.auto_merge === undefined ? {} : { autoMerge: autoMergeMode(raw.auto_merge, "sdlc.auto_merge") }), ...(raw.repos === undefined ? {} : { repos: mergeRepos(raw.repos)! }) }; if (raw.definitions !== undefined) { const custom = definitions(raw.definitions, raw.stages, phaseAlias); if (phaseAlias.used) notes.push(`the sdlc phase gate is deprecated and stops loading in ${RETIRED_WORKFLOW_FROM} (#481): name it criteria`); return { mode, ...findingConfig, ...mergeConfig, requiredChecks: [...checks] as string[], ...custom, ...(workflow === "gsd-lite" ? { workflow } : {}), ...(notes.length ? { deprecations: notes } : {}) }; } // Off keeps the #257 seven stages byte for byte; an enabled lifecycle defaults to the playbook (#481 S0). const given = raw.stages ?? (mode === "off" ? [...SDLC_STAGES] : [...PLAYBOOK_STAGES]); const exactly = (list: readonly string[]) => Array.isArray(given) && given.length === list.length && given.every((stage, i) => stage === list[i]); // An enabled #257 list keeps its order under canonical ids, so implement/verify stay read-only aliases there too. const stages = mode !== "off" && exactly(SDLC_STAGES) ? SDLC_STAGES.map(canonicalStage) : given; if (!exactly(SDLC_STAGES) && !exactly(PLAYBOOK_STAGES)) throw new Error(`sdlc.stages: use ${PLAYBOOK_STAGES.join(", ")} in that order (or the #257 list ${SDLC_STAGES.join(", ")}); other lists need definitions`); return { mode, ...findingConfig, ...mergeConfig, requiredChecks: [...checks] as string[], stages: [...stages as string[]], ...(workflow === "gsd-lite" ? { workflow } : {}), ...(notes.length ? { deprecations: notes } : {}) }; } export function loadSdlc(cwd: string, version?: string): SdlcConfig { try { const fd = openSync(join(cwd, ".agenthub", "sdlc.toml"), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); try { const before = fstatSync(fd), cap = 64 * 1024; if (!before.isFile() || before.nlink !== 1 || before.size > cap) throw new Error("sdlc.toml must be a regular single-link file within 64 KiB"); const bytes = Buffer.alloc(cap + 1); let length = 0, n = 0; while ((n = readSync(fd, bytes, length, bytes.length - length, null)) > 0) { length += n; if (length > cap) throw new Error("sdlc.toml exceeds 64 KiB"); } const after = fstatSync(fd); if (before.size !== after.size || before.mtimeMs !== after.mtimeMs || length !== after.size) throw new Error("sdlc.toml changed during read"); return sdlcConfig(Bun.TOML.parse(bytes.subarray(0, length).toString("utf8")), version); } finally { closeSync(fd); } } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return sdlcConfig(undefined); throw error; } } /** Identities and claimed local receipts only. External facts are read, never accepted as input. */ export function sdlcReceipt(value: unknown, previous: SdlcReceipt | undefined = undefined, config: SdlcConfig = sdlcConfig(undefined), version?: string): SdlcReceipt { if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("sdlc receipt: expected an object"); const raw = value as Record; for (const key of Object.keys(raw)) if (!["stage", "repo", "issue", "pr", "reviewedHead", "localHead", "localResult", "criteria", "phase", ...(config.mode !== "off" ? ["redFirst"] : [])].includes(key)) throw new Error(`sdlc receipt.${key}: unsupported or externally observed field`); if (raw.criteria !== undefined && raw.phase !== undefined) throw new Error("sdlc receipt: give criteria only; phase is its retired name"); if (raw.phase !== undefined && version !== undefined && !olderThan(version, RETIRED_WORKFLOW_FROM)) throw new Error(`sdlc receipt.phase: retired in ${RETIRED_WORKFLOW_FROM} (#481); use criteria`); previous ??= { stage: config.stages[0] ?? "spec" }; const next = { ...previous }; const checklist = raw.criteria ?? raw.phase; // Off keeps the #257 receipt bytes for a phase write (task import stays retired); everything else stores criteria. if (raw.phase !== undefined && config.mode === "off" && !config.workflow) { const legacy = criteriaReceipt(raw.phase); next.phase = { id: legacy.id, ...(legacy.head ? { head: legacy.head } : {}), tasks: [], criteria: legacy.criteria }; delete next.criteria; } else if (checklist !== undefined) { next.criteria = criteriaReceipt(checklist); delete next.phase; } if (raw.stage !== undefined) { if (typeof raw.stage !== "string" || !stageDefinition(config, raw.stage)) throw new Error("sdlc receipt.stage: unknown stage"); // A legacy id is accepted from older clients but stored under the stage the config actually lists (#481 S0). next.stage = (config.stages.includes(raw.stage) ? raw.stage : canonicalStage(raw.stage)) as SdlcStage; } if (raw.repo !== undefined) { if (!repo(raw.repo)) throw new Error("sdlc receipt.repo: expected owner/repository"); next.repo = raw.repo; } for (const key of ["issue", "pr"] as const) if (raw[key] !== undefined) { if (!id(raw[key])) throw new Error(`sdlc receipt.${key}: expected a positive integer`); next[key] = raw[key]; } for (const key of ["reviewedHead", "localHead"] as const) if (raw[key] !== undefined) { if (!sha(raw[key])) throw new Error(`sdlc receipt.${key}: expected a full lowercase SHA`); next[key] = raw[key]; } if (raw.localResult !== undefined) { if (!["passed", "failed", "unknown"].includes(String(raw.localResult))) throw new Error("sdlc receipt.localResult: expected passed, failed or unknown"); next.localResult = raw.localResult as SdlcReceipt["localResult"]; } if (raw.redFirst !== undefined) next.redFirst = parseRedFirst(raw.redFirst); return receiptIdentity(next, previous, raw); } function receiptIdentity(next: SdlcReceipt, previous: SdlcReceipt, raw: Record): SdlcReceipt { const checklist = raw.criteria ?? raw.phase; if (next.repo !== previous.repo || next.pr !== previous.pr || next.head !== previous.head || next.reviewedHead !== previous.reviewedHead || next.localHead !== previous.localHead) { delete next.merge; delete next.mergeReadback; } // A test receipt never carries across a different linked issue or artifact. if (raw.redFirst === undefined && (next.repo !== previous.repo || next.issue !== previous.issue || next.pr !== previous.pr)) delete next.redFirst; // An acceptance names one spec issue; relinking the receipt to another drops it (#481 S1). if (next.repo !== previous.repo || next.issue !== previous.issue) delete next.acceptance; // A new artifact cannot inherit another PR's review or local gate. if (next.repo !== previous.repo || next.pr !== previous.pr) { delete next.head; delete next.observed; if (checklist === undefined) { delete next.criteria; delete next.phase; } if (raw.reviewedHead === undefined) delete next.reviewedHead; if (raw.localHead === undefined) delete next.localHead; if (raw.localResult === undefined) delete next.localResult; } return next; } export interface PlanningIssueIdentity { repo?: string | null; issue?: number | null } /** Planning can change only the linked issue identity, never lifecycle evidence. */ export function planningIssueIdentity(value: unknown): PlanningIssueIdentity { if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("planning sdlc identity must be an object"); const raw = value as Record, keys = Object.keys(raw); if (!keys.length || keys.some(key => key !== "repo" && key !== "issue")) throw new Error("planning sdlc identity takes repo/issue only"); const result: PlanningIssueIdentity = {}; if (Object.hasOwn(raw, "repo")) { if (raw.repo !== null && (!repo(raw.repo) || raw.repo.length > 400)) throw new Error("planning sdlc.repo: expected owner/repository or null"); result.repo = raw.repo as string | null; } if (Object.hasOwn(raw, "issue")) { if (raw.issue !== null && !id(raw.issue)) throw new Error("planning sdlc.issue: expected a positive integer or null"); result.issue = raw.issue as number | null; } return result; } export function planningIssueReceipt(value: unknown, previous: SdlcReceipt | undefined, config: SdlcConfig): SdlcReceipt { const identity = planningIssueIdentity(value); if (identity.repo !== null && identity.issue !== null) return sdlcReceipt(identity, previous, config); const old = previous ?? { stage: config.stages[0] ?? "spec" }, next = { ...old }; if (Object.hasOwn(identity, "repo")) { if (identity.repo === null) delete next.repo; else next.repo = identity.repo!; } if (Object.hasOwn(identity, "issue")) { if (identity.issue === null) delete next.issue; else next.issue = identity.issue!; } return receiptIdentity(next, old, {}); } /** * #1490 AC5: one head can carry the check rows of several runs of one workflow: the draft run `gh pr ready` replaces (its macOS aggregate failed on purpose, its other jobs were * cancelled), a rerun, or a run a newer push cancelled. The newest run of a workflow is the one that says what the head's CI is, so an older row of the same check name is dropped * from the evidence, never read as a failure to fix. Only when it is certain: every row of the name has a start time and the same workflow identity, and exactly one row is newest. * Anything else (a different workflow with the same job name, a missing time, a tie) keeps every row, so `requiredCiBlocker` still says `duplicate` and unknown stays unknown. * Callers apply it before they index the raw rows beside the parsed checks. */ export function latestCheckRuns(rows: readonly T[]): T[] { const field = (row: T) => row as unknown as { name?: unknown; context?: unknown; startedAt?: unknown; workflowName?: unknown; checkSuite?: { workflowRun?: { workflow?: { resourcePath?: unknown } } } } | null; const workflowOf = (row: T): string | undefined => { const value = field(row); const named = value?.workflowName ?? value?.checkSuite?.workflowRun?.workflow?.resourcePath; return typeof named === "string" && named ? named : undefined; }; const groups = new Map(); for (const row of rows) { const name = field(row)?.name ?? field(row)?.context; if (typeof name !== "string") continue; const group = groups.get(name); if (group) group.push(row); else groups.set(name, [row]); } const older = new Set(); for (const group of groups.values()) { if (group.length < 2) continue; const workflows = new Set(group.map(workflowOf)); const times = group.map(row => typeof field(row)?.startedAt === "string" ? Date.parse(field(row)!.startedAt as string) : NaN); if (workflows.size !== 1 || workflows.has(undefined) || times.some(time => !Number.isFinite(time))) continue; const newest = Math.max(...times); if (times.filter(time => time === newest).length !== 1) continue; group.forEach((row, index) => { if (times[index] !== newest) older.add(row); }); } return older.size ? rows.filter(row => !older.has(row)) : [...rows]; } export function parsePrEvidence(value: unknown): PrEvidence { const p = value as Record; if (!p || !sha(p.headRefOid) || !["OPEN", "CLOSED", "MERGED"].includes(p.state) || !Array.isArray(p.statusCheckRollup) || !Array.isArray(p.files) || p.files.length > 1000) throw new Error("PR readback: invalid or incomplete structured evidence"); const checks = p.statusCheckRollup.map((c: Record) => { const name = c.name ?? c.context; if (typeof name !== "string" || !name || name.length > 120) throw new Error("PR readback: invalid check identity"); const native = c.conclusion ?? c.state; const state = ["PENDING", "QUEUED", "IN_PROGRESS"].includes(String(c.status ?? native)) ? "pending" : native === "SUCCESS" ? "passed" : ["FAILURE", "ERROR", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED"].includes(String(native)) ? "failed" : "unknown"; return { name, state } as PrEvidence["checks"][number]; }); const paths = p.files.map((f: Record) => { if (typeof f.path !== "string" || !f.path || f.path.length > 1000) throw new Error("PR readback: invalid file identity"); return f.path; }); const completeTypes = p.files.every((file: Record) => typeof file.changeType === "string" && ["ADDED", "DELETED", "MODIFIED", "CHANGED"].includes(file.changeType)); // Rename/copy metadata exposes only its destination, never a complete inventory of affected identities. return { head: p.headRefOid, state: p.state, checks, paths, fileChanges: p.files.map((file: Record) => ({ path: file.path as string, change: typeof file.changeType === "string" ? file.changeType : "UNKNOWN" })), pathsComplete: Number.isSafeInteger(p.changedFiles) && p.changedFiles >= 0 && p.changedFiles === paths.length, typesComplete: completeTypes, ...(typeof p.isDraft === "boolean" ? { draft: p.isDraft } : {}), ...(typeof p.baseRefName === "string" && p.baseRefName && p.baseRefName.length <= 200 && !/[\x00-\x1f\x7f]/.test(p.baseRefName) ? { base: p.baseRefName } : {}), ...(sha(p.baseRefOid) ? { baseHead: p.baseRefOid } : {}), review: p.reviewDecision === "APPROVED" ? "approved" : p.reviewDecision === "CHANGES_REQUESTED" ? "changes_requested" : "unknown", pendingReviews: Array.isArray(p.reviewRequests) && p.reviewRequests.length <= 100 && p.reviewRequests.every((r: unknown) => !!r && typeof r === "object") ? p.reviewRequests.length : null, unresolved: Number.isSafeInteger(p.unresolvedThreads) && p.unresolvedThreads >= 0 ? p.unresolvedThreads : null, ...(sha(p.mergeCommit?.oid) ? { mergeCommit: p.mergeCommit.oid } : {}), ...(typeof p.headRefName === "string" && p.headRefName && p.headRefName.length <= 200 && !/[\x00-\x1f\x7f]/.test(p.headRefName) ? { headBranch: p.headRefName } : {}) }; } /** Issue identity and edit times only; no body is fetched or stored. `lastEditedAt` moves on a body edit, not on comments. */ export interface SpecEdit { lastEditedAt: string | null; updatedAt: string } export interface SpecAcceptance extends SpecEdit { repo: string; issue: number; acceptedAt: number } /** Consent references are complete local task tokens, never numbers extracted from prose. */ export function localSupersedingTask(value: unknown): number | undefined { if (typeof value !== "string") return undefined; const match = /^(?:task #|#)?([1-9][0-9]*)$/.exec(value); const task = match?.[0] === value ? Number(match[1]) : NaN; return Number.isSafeInteger(task) ? task : undefined; } /** Validate host-stored consent without importing any other source receipt fields. */ export function storedSpecAcceptance(value: unknown): SpecAcceptance | undefined { if (!value || typeof value !== "object" || Array.isArray(value)) return undefined; const record = value as Record; const stamp = (v: unknown): v is string => typeof v === "string" && v.length <= 40 && !Number.isNaN(Date.parse(v)); if (!repo(record.repo) || !id(record.issue) || !stamp(record.updatedAt) || record.lastEditedAt !== null && !stamp(record.lastEditedAt) || typeof record.acceptedAt !== "number" || !Number.isFinite(record.acceptedAt)) return undefined; return { repo: record.repo, issue: record.issue, updatedAt: record.updatedAt, lastEditedAt: record.lastEditedAt as string | null, acceptedAt: record.acceptedAt }; } /** Only a host observation of the same issue/body permits copying existing person consent. */ export function carriedSpecAcceptance(source: SpecAcceptance, destination: { repo: string; issue: number }, edit: SpecEdit | undefined): SpecAcceptance | undefined { if (!edit || source.repo.toLowerCase() !== destination.repo.toLowerCase() || source.issue !== destination.issue || source.lastEditedAt !== edit.lastEditedAt) return undefined; return { repo: destination.repo, issue: destination.issue, lastEditedAt: source.lastEditedAt, updatedAt: source.updatedAt, acceptedAt: source.acceptedAt }; } export type SpecState = "missing" | "current" | "changed" | "unknown"; /** Whether the receipt's spec issue is accepted at its current body. `edit` undefined means the readback failed. */ export function specState(receipt: SdlcReceipt | undefined, edit: SpecEdit | undefined): SpecState { const accepted = receipt?.acceptance; if (!accepted || accepted.repo !== receipt?.repo || accepted.issue !== receipt?.issue) return "missing"; if (!edit) return "unknown"; return edit.lastEditedAt === accepted.lastEditedAt ? "current" : "changed"; } export const SPEC_STATE_TEXT: Record, string> = { missing: "spec acceptance missing: the person runs ahub sdlc accept --issue ", changed: "spec changed after acceptance: the person re-accepts with ahub sdlc accept --issue ", unknown: "spec edit time unknown: the issue could not be read", }; export interface SdlcStatus { /** #1403 A2: shown only for an enabled lifecycle that configures `[security]`. */ securityPass?: SecurityPassView; evalGate?: EvalGate; testIntegrity?: TestIntegrity; planConformance?: PlanConformance & { accepted?: boolean; earlierPushes?: "unknown"; revisionsAfterFirstObservedPush?: number }; findings?: ReviewFindingTally; spec?: { issue: number; state: SpecState }; playbook?: string; criteria?: CriteriaView; mode: SdlcMode; stage: SdlcStage; definition?: StageDefinition; entry?: string[]; exit?: string[]; blockers: string[]; ready: boolean; actor: string; next: string; } /** The acceptance-criteria checklist (#481 S8, Compliance pass) counts once recorded, and on every task under the retired gsd-lite setting. Off keeps #257's output. */ export const criteriaRequired = (config: SdlcConfig, receipt: SdlcReceipt | undefined): boolean => config.workflow === "gsd-lite" || (config.mode !== "off" && !!checklistOf(receipt)); export function sdlcStatus(config: SdlcConfig, receipt: SdlcReceipt | undefined, evidence?: PrEvidence, criteria?: CriteriaView, specEdit?: SpecEdit, sets: readonly ReviewFindingSet[] = [], commit?: string, plan?: SdlcStatus["planConformance"], integrity?: TestIntegrity, evalResult?: EvalResult, securityPass?: SecurityPassView): SdlcStatus { const stage = receipt?.stage ?? config.stages[0] ?? "spec", blockers: string[] = []; const definition = stageDefinition(config, stage); const evalGate = evidence ? evalGateView(config, evidence, evalResult) : undefined; const items = findingsForHead(sets, findingHead(receipt, evidence, commit)); const findings = items.length ? findingTally(items) : undefined; if (config.mode === "enforce" && findings?.openImportant) blockers.push("open important review findings: " + findings.openImportant); if (config.mode === "enforce") blockers.push(...planConformanceBlockers(plan), ...testIntegrityBlockers(integrity)); // #1403 A2: a required pass that is not recorded, or an applicability the inventory cannot settle, blocks only in enforce; advise shows it in `securityPass` and the advice notice. const security = config.mode !== "off" ? securityPass : undefined; if (config.mode === "enforce") blockers.push(...securityPassBlockers(security)); if (!definition) blockers.push("lifecycle stage definition unknown"); if (!receipt?.repo || !receipt.issue) blockers.push("issue identity missing"); if (!receipt?.pr) blockers.push("PR identity missing"); if (!evidence) blockers.push("current PR evidence unknown"); else { if (evidence.state !== "OPEN") blockers.push("PR is not open"); if (receipt?.reviewedHead !== evidence.head) blockers.push("current head has not been reviewed"); if (receipt?.localHead !== evidence.head || receipt.localResult !== "passed") blockers.push("local gate on current head missing or failed"); if (!config.requiredChecks.length) blockers.push("required check set unknown"); // #481 S5: a configuration PR also needs the configured eval check; a code-only PR is unaffected. for (const name of [...new Set([...config.requiredChecks, ...evalRequiredChecks(config, evidence)])]) { const matches = evidence.checks.filter(c => c.name === name); if (!matches.length || matches.some(c => c.state !== "passed")) blockers.push(`required check ${name}: not successful on current head`); } // Gaps in the eval evidence block only in enforce; advise shows them in `evalGate` and the advice notice (never a silent pass, never an advise block). if (config.mode === "enforce") blockers.push(...evalGapBlockers(evalGate)); if (evidence.review !== "approved") blockers.push("GitHub review approval missing or changes requested"); if (evidence.pendingReviews !== 0) blockers.push("review requests pending or unknown"); if (evidence.unresolved !== 0) blockers.push("review threads unresolved or unknown"); } const viewBlockers = criteria?.blockers ?? ["criteria board evidence unknown"]; const staleHead = !evidence || checklistOf(receipt)?.head !== evidence.head ? ["criteria checklist head missing or stale"] : []; if (criteriaRequired(config, receipt)) { blockers.unshift(...viewBlockers); blockers.push(...staleHead); } const custom = config.definitions && definition ? stageGates(definition, blockers, [...viewBlockers, ...staleHead]) : undefined; if (custom) for (const blocker of [...custom.entry, ...custom.exit]) if (!blockers.includes(blocker)) blockers.push(blocker); const shown = criteria && (criteriaRequired(config, receipt) || definition && [...definition.entry_gates, ...definition.exit_gates].includes("criteria")); // Shown, not counted as a readiness blocker: the spec gate is build entry (#481 S1). const spec = config.mode !== "off" && receipt?.issue ? { spec: { issue: receipt.issue, state: specState(receipt, specEdit) } } : {}; return { ...spec, ...(security ? { securityPass: security } : {}), ...(evalGate ? { evalGate } : {}), ...(config.mode !== "off" && integrity ? { testIntegrity: integrity } : {}), ...(config.mode !== "off" && plan ? { planConformance: plan } : {}), ...(findings ? { findings } : {}), ...(config.mode !== "off" && definition && playbookStage(definition.base) ? { playbook: playbookStage(definition.base) } : {}), ...(shown ? { criteria } : {}), ...(custom ? { definition, ...custom } : {}), mode: config.mode, stage, blockers, ready: blockers.length === 0, actor: !definition ? "person" : shown && criteria.blockers.length ? "reviewer/person" : config.definitions ? definition.actor : baseActor(definition.base), // #694: the identity blocker keeps its exact text for gate matching; the next step says how to supply it. next: blockers[0] === "issue identity missing" ? "issue identity missing: name one issue (title #N, refs or hub_sdlc_record)" : blockers[0] ?? "person may inspect merge readiness; no automatic merge" }; } function stageGates(def: StageDefinition, blockers: string[], checklist: string[]): { entry: string[]; exit: string[] } { const select = (gates: StageGate[]) => [...new Set(gates.flatMap(gate => { if (gate === "criteria") return checklist; const matching = (text: string) => gate === "issue" ? text === "issue identity missing" : gate === "pr" ? ["PR identity missing", "current PR evidence unknown", "PR is not open"].includes(text) : gate === "local" ? ["current PR evidence unknown", "local gate on current head missing or failed"].includes(text) : gate === "checks" ? text === "current PR evidence unknown" || text === "required check set unknown" || text.startsWith("required check ") : ["current PR evidence unknown", "current head has not been reviewed", "GitHub review approval missing or changes requested", "review requests pending or unknown", "review threads unresolved or unknown"].includes(text); return blockers.filter(matching); }))]; return { entry: select(def.entry_gates), exit: select(def.exit_gates) }; } export function touchingPrs(prs: { pr: number; paths: string[] }[]): { before: number; after: number; paths: string[] }[] { const conflicts: ReturnType = []; for (let i = 0; i < prs.length; i++) for (const right of prs.slice(i + 1)) { const left = prs[i]!, paths = [...new Set(left.paths.filter(path => right.paths.includes(path)))].sort(); if (paths.length) conflicts.push({ before: left.pr, after: right.pr, paths }); } return conflicts; } export type SdlcGh = (args: string[], timeoutMs?: number, cwd?: string, maxOutputBytes?: number) => Promise<{ code: number; stdout: string }>; /** Fixed argv and bounded output and time; diagnostics never expose credentials or remote text. Reads, except the one guarded `gh pr merge` that `sdlc-merge.ts` builds (#786). */ export const sdlcGh: SdlcGh = (args, timeoutMs = 10_000, cwd, maxOutputBytes = 256 * 1024) => controlWriteSpan("external", () => new Promise<{ code: number; stdout: string }>((resolve) => { const outputLimit = Number.isSafeInteger(maxOutputBytes) ? Math.min(2 * 1024 * 1024, Math.max(1, maxOutputBytes)) : 256 * 1024; let child: ReturnType; try { child = spawn(hostExecutable("gh", cwd ?? process.cwd()), args, { ...(cwd ? { cwd } : {}), env: childEnv(), detached: true, stdio: ["ignore", "pipe", "ignore"] }); } catch { resolve({ code: 1, stdout: "" }); return; } let stdout = "", failed = false; const stop = () => { failed = true; void stopOwnedProcess(child, { group: true }).catch(() => {}); }; const timer = setTimeout(stop, Math.max(1, Math.min(10_000, timeoutMs))); child.stdout?.setEncoding("utf8"); child.stdout?.on("data", chunk => { if (failed) return; stdout += chunk.toString(); if (Buffer.byteLength(stdout) > outputLimit) { stdout = ""; stop(); } }); child.once("error", () => { failed = true; clearTimeout(timer); resolve({ code: 1, stdout: "" }); }); child.once("close", code => { clearTimeout(timer); resolve({ code: failed ? 1 : code ?? 1, stdout: failed ? "" : stdout }); }); })); /** Read-only, fixed argv: the issue's edit times, never its body. */ export async function readSpecEdit(repoName: string, issue: number, run: SdlcGh = sdlcGh): Promise { if (!repo(repoName) || !id(issue)) return undefined; const [owner, name] = repoName.split("/"); try { const out = await run(["api", "graphql", "-f", "query=query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){issue(number:$number){number lastEditedAt updatedAt}}}", "-f", `owner=${owner}`, "-f", `name=${name}`, "-F", `number=${issue}`]); if (out.code !== 0) return undefined; const found = JSON.parse(out.stdout)?.data?.repository?.issue; const stamp = (v: unknown) => typeof v === "string" && v.length <= 40 && !Number.isNaN(Date.parse(v)); if (found?.number !== issue || !stamp(found.updatedAt) || (found.lastEditedAt !== null && !stamp(found.lastEditedAt))) return undefined; return { lastEditedAt: found.lastEditedAt, updatedAt: found.updatedAt }; } catch { return undefined; } } /** * #694: the one open PR whose head is `commit`, or, with no commit, whose branch is `branch`. Zero or several matches, a * failed read or malformed output give undefined: the hub links only what it verified, never text an agent wrote. */ export async function openPrFor(repository: string, head: { commit?: string; branch?: string }, run: SdlcGh = sdlcGh): Promise { if (!repo(repository) || head.commit !== undefined && !sha(head.commit) || !head.commit && !head.branch) return undefined; try { const listed = await run(["pr", "list", "--repo", repository, "--state", "open", "--limit", "100", "--json", "number,headRefOid,headRefName"], 10_000, undefined, 256 * 1024); if (listed.code !== 0) return undefined; const rows: unknown = JSON.parse(listed.stdout); // A full page may be truncated: uniqueness is unknown, so nothing is linked. Every row must be well formed. if (!Array.isArray(rows) || rows.length >= 100) return undefined; if (!rows.every((row: { number?: unknown; headRefOid?: unknown; headRefName?: unknown }) => id(row?.number) && sha(row.headRefOid) && typeof row.headRefName === "string")) return undefined; const hits = rows.filter((row: { headRefOid: string; headRefName: string }) => head.commit ? row.headRefOid === head.commit : row.headRefName === head.branch); return hits.length === 1 ? (hits[0] as { number: number }).number : undefined; } catch { return undefined; } } /** #694: the first link of an issue or PR is not a relink: it keeps everything the receipt already holds. */ export function linkedReceipt(previous: SdlcReceipt | undefined, link: { repo: string; issue?: number; pr?: number }, config: SdlcConfig): SdlcReceipt | undefined { if (!repo(link.repo) || link.issue !== undefined && !id(link.issue) || link.pr !== undefined && !id(link.pr)) return undefined; if (previous?.repo && previous.repo.toLowerCase() !== link.repo.toLowerCase()) return undefined; if (link.issue !== undefined && previous?.issue !== undefined || link.pr !== undefined && previous?.pr !== undefined) return undefined; return { ...(previous ?? { stage: config.stages[0] ?? "spec" }), repo: previous?.repo ?? link.repo, ...(link.issue !== undefined ? { issue: link.issue } : {}), ...(link.pr !== undefined ? { pr: link.pr } : {}) }; } export async function readSdlcPr(receipt: SdlcReceipt, run: SdlcGh = sdlcGh, withIntegrity = false): Promise { if (!repo(receipt.repo) || !id(receipt.pr)) return undefined; try { const view = await run(["pr", "view", String(receipt.pr), "--repo", receipt.repo, "--json", "headRefOid,state,statusCheckRollup,reviewDecision,reviewRequests,files,changedFiles,mergeCommit,isDraft,baseRefName,baseRefOid,headRefName"]); if (view.code !== 0) return undefined; const [owner, name] = receipt.repo.split("/"); // The enabled S4 label read shares the existing thread snapshot, never a second readback for a known non-bug. const labels = withIntegrity && id(receipt.issue) ? { issue: receipt.issue, labels: null as string[] | null } : undefined; const query = `query($owner:String!,$name:String!,$number:Int!${labels ? ",$issue:Int!" : ""}){repository(owner:$owner,name:$name){pullRequest(number:$number){headRefOid reviewThreads(first:100){nodes{isResolved} pageInfo{hasNextPage}}}${labels ? " issue(number:$issue){number labels(first:100){nodes{name} pageInfo{hasNextPage}}}" : ""}}}`; const threads = await run(["api", "graphql", "-f", `query=${query}`, "-f", `owner=${owner}`, "-f", `name=${name}`, ...(labels ? ["-F", `issue=${labels.issue}`] : []), "-F", `number=${receipt.pr}`]); const p = JSON.parse(view.stdout); if (Array.isArray(p.statusCheckRollup)) p.statusCheckRollup = latestCheckRuns(p.statusCheckRollup); delete p.unresolvedThreads; // only the complete thread query can establish this fact if (threads.code === 0) { const response = JSON.parse(threads.stdout); const current = response?.data?.repository?.pullRequest; if (labels && response.errors === undefined) labels.labels = parseIssueLabels(response?.data?.repository?.issue, labels.issue) ?? null; const nodes = current?.reviewThreads?.nodes; // Paginated or malformed thread data is unknown, never a clean review. if (current?.headRefOid === p.headRefOid && current?.reviewThreads?.pageInfo?.hasNextPage === false && Array.isArray(nodes) && nodes.every(n => typeof n?.isResolved === "boolean")) p.unresolvedThreads = nodes.filter(n => !n.isResolved).length; else if (current?.headRefOid && current.headRefOid !== p.headRefOid) return undefined; } return { ...parsePrEvidence(p), ...(labels ? { issueLabels: labels } : {}) }; } catch { return undefined; } } /** Read only the current PR head when broader PR evidence is unavailable. */ export async function readSdlcPrHead(receipt: SdlcReceipt, run: SdlcGh = sdlcGh): Promise { if (!repo(receipt.repo) || !id(receipt.pr)) return undefined; try { const result = await run(["pr", "view", String(receipt.pr), "--repo", receipt.repo, "--json", "headRefOid"], 10_000, undefined, 1024); if (result.code !== 0 || Buffer.byteLength(result.stdout) > 1024) return undefined; const value: unknown = JSON.parse(result.stdout); const head = (value as { headRefOid?: unknown } | null)?.headRefOid; return sha(head) ? head : undefined; } catch { return undefined; } } export interface PlanIssueMetadata { repo: string; issue: number; state: "OPEN" | "CLOSED"; /** Body edit time; comment activity must never invalidate a plan review. */ editedAt: string; /** Nullable GitHub body edit field, retained to compare independent S1 acceptance evidence. */ lastEditedAt?: string | null; } /** Metadata only: never request issue bodies or comments for a planning receipt. */ export async function readPlanIssueMetadata(repository: string, number: number, run: SdlcGh = sdlcGh): Promise { if (!repo(repository) || !id(number)) return undefined; const [owner, name] = repository.split("/"); try { const result = await run(["api", "graphql", "-f", "query=query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){issue(number:$number){number state createdAt lastEditedAt}}}", "-f", `owner=${owner}`, "-f", `name=${name}`, "-F", `number=${number}`], 10_000, undefined, 8 * 1024); if (result.code !== 0 || Buffer.byteLength(result.stdout) > 8 * 1024) return undefined; const response = JSON.parse(result.stdout); if (response.errors !== undefined) return undefined; const issue = response.data?.repository?.issue; if (!issue || issue.number !== number || !["OPEN", "CLOSED"].includes(issue.state)) return undefined; const editedAt = issue.lastEditedAt === null ? issue.createdAt : issue.lastEditedAt; if (typeof editedAt !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/.test(editedAt) || !Number.isFinite(Date.parse(editedAt))) return undefined; return { repo: repository, issue: number, state: issue.state, editedAt, lastEditedAt: issue.lastEditedAt }; } catch { return undefined; } } /** The native CI floor belongs only to this repository; other projects must name their own checks. */ export function requiredCheckNames(repository: string | undefined, required: readonly string[]): string[] { return required.length ? [...new Set(required)] : repository?.toLowerCase() === "staixbwlb/agent-hub" ? ["check (ubuntu-latest)", "check (macos-latest)", "seeded guards"] : []; } /** Names each non-green required check. Incomplete inventories cannot prove either presence or success. */ export function requiredCiBlocker(required: readonly string[], checks: PrEvidence["checks"], complete = true): string | undefined { const problems = required.flatMap(name => { const matches = checks.filter(check => check.name === name); const state = !complete ? "unknown" : !matches.length ? "missing" : matches.some(check => check.state === "failed") ? "failed" : matches.some(check => check.state === "pending") ? "pending" : matches.length !== 1 ? "duplicate" : matches[0]!.state !== "passed" ? "unknown" : undefined; return state ? [`${state} ${name}`] : []; }); return problems.length ? `exact-head full CI is not green: ${problems.join("; ")}` : undefined; } /** A complete resolved-thread snapshot already proves there is no pending unanswered bot thread. */ export function reviewReadinessFromEvidence(evidence: PrEvidence | undefined, expected: string | undefined, required: readonly string[], repository?: string): ReviewReadiness | undefined { if (!evidence || evidence.unresolved !== 0) return undefined; // A draft's failed aggregate needs job-step evidence, which this compact snapshot does not carry. if (evidence.draft === true && evidence.checks.some(check => check.name === "check (macos-latest)" && check.state === "failed")) return undefined; const checks = requiredCheckNames(repository, required); if (!checks.length) return { ready: false, blockers: ["required checks unknown: set required_checks in .agenthub/sdlc.toml"], checks }; if (!checks.every(name => evidence.checks.some(check => check.name === name)) || evidence.checks.some(check => check.state === "unknown")) return undefined; const blockers: string[] = []; if (!expected || !sha(expected)) blockers.push("review head missing"); else if (evidence.head !== expected) blockers.push("review head changed"); if (evidence.state !== "OPEN") blockers.push("review PR is not open"); const ciBlocker = requiredCiBlocker(checks, evidence.checks); if (ciBlocker) blockers.push(ciBlocker); return { head: evidence.head, ready: blockers.length === 0, blockers, checks }; } /** `checks` (#824) names the required checks that were judged, so a fresh reviewer can be told which ones passed at this head; absent when the read was unknown. */ export interface ReviewReadiness { head?: string; ready: boolean; blockers: string[]; reusedRun?: number; checks?: string[]; deferred?: string[]; draftApprovalEligible?: boolean } /** Only this repository's actual draft-deferral step can explain its failed macOS aggregate. */ async function draftMacosDeferred(repository: string, head: string, node: any, run: SdlcGh): Promise { if (repository.toLowerCase() !== "staixbwlb/agent-hub" || node?.name !== "check (macos-latest)" || node.status !== "COMPLETED" || node.conclusion !== "FAILURE" || node.checkSuite?.workflowRun?.workflow?.resourcePath !== "/STAIxBWLB/agent-hub/actions/workflows/check.yml") return false; const url = typeof node.detailsUrl === "string" ? node.detailsUrl : ""; const match = /^https:\/\/github\.com\/STAIxBWLB\/agent-hub\/actions\/runs\/(\d+)\/job\/(\d+)$/.exec(url); if (!match) return false; const result = await run(["api", `repos/${repository}/actions/jobs/${match[2]}`], 10_000, undefined, 64 * 1024); if (result.code !== 0 || Buffer.byteLength(result.stdout) > 64 * 1024) return false; try { const job = JSON.parse(result.stdout), steps = job.steps; if (String(job.id) !== match[2] || String(job.run_id) !== match[1] || job.head_sha !== head || job.name !== node.name || job.status !== "completed" || job.conclusion !== "failure" || !Array.isArray(steps)) return false; const deferred = steps.filter((step: any) => step?.name === "macOS shards deferred for a draft PR"); const stacked = steps.filter((step: any) => step?.name === "macOS shards deferred for a stacked PR"); return deferred.length === 1 && deferred[0].status === "completed" && deferred[0].conclusion === "failure" && stacked.length === 1 && stacked[0].conclusion === "skipped" && steps.filter((step: any) => step?.conclusion === "failure").length === 1 && steps.every((step: any) => step?.status === "completed" && ["success", "skipped", "failure"].includes(step.conclusion)); } catch { return false; } } /** Metadata-only pre-review admission; no remote body or comment text is requested. */ export async function readReviewReadiness(receipt: SdlcReceipt, expected: string | undefined, run: SdlcGh = sdlcGh, requiredChecks: readonly string[] = []): Promise { const unknown = (): ReviewReadiness => ({ ready: false, blockers: ["review readiness unavailable"] }); if (!repo(receipt.repo) || !id(receipt.pr)) return unknown(); const full = requiredCheckNames(receipt.repo, requiredChecks); if (!full.length) return { ready: false, blockers: ["required checks unknown: set required_checks in .agenthub/sdlc.toml"], checks: full }; const [owner, name] = receipt.repo.split("/"); const query = "query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){headRefOid state isDraft reviewDecision reviewRequests(first:100){nodes{__typename} pageInfo{hasNextPage}} commits(last:1){nodes{commit{oid tree{oid} statusCheckRollup{contexts(first:100){nodes{... on CheckRun{name status conclusion startedAt detailsUrl checkSuite{workflowRun{workflow{resourcePath}}}} ... on StatusContext{context state}} pageInfo{hasNextPage}}}}}} reviewThreads(first:100){nodes{isResolved comments(first:100){nodes{author{__typename} createdAt} pageInfo{hasNextPage}}} pageInfo{hasNextPage}}}}}"; try { const deadline = performance.now() + 20_000; const bounded: SdlcGh = (args, timeout, cwd, cap) => performance.now() < deadline ? run(args, Math.max(1, Math.min(timeout ?? 10_000, deadline - performance.now())), cwd, cap) : Promise.resolve({ code: 1, stdout: "" }); const args = ["api", "graphql", "-f", `query=${query}`, "-f", `owner=${owner}`, "-f", `name=${name}`, "-F", `number=${receipt.pr}`]; const response = await bounded(args, 10_000, undefined, 512 * 1024); if (response.code !== 0 || Buffer.byteLength(response.stdout) > 512 * 1024) return unknown(); const parsed = JSON.parse(response.stdout), pr = parsed.data?.repository?.pullRequest; if (parsed.errors || !pr || !sha(pr.headRefOid)) return unknown(); const blockers: string[] = []; if (!expected || !sha(expected)) blockers.push("review head missing"); else if (pr.headRefOid !== expected) blockers.push("review head changed"); if (pr.state !== "OPEN") blockers.push("review PR is not open"); const commit = pr.commits?.nodes?.[0]?.commit; if (!commit || commit.oid !== pr.headRefOid) blockers.push("review head changed"); const contexts = commit?.statusCheckRollup?.contexts; const nodes: unknown[] | undefined = contexts?.nodes; let reusedRun: number | undefined; const completeChecks = !(contexts?.pageInfo?.hasNextPage !== false || !Array.isArray(nodes) || nodes.length > 100); const requiredNodes = completeChecks ? latestCheckRuns(nodes!).filter(node => { const raw = node as { name?: unknown; context?: unknown } | null | undefined; return full.some(name => (raw?.name ?? raw?.context) === name); }) : []; const checkEvidence = completeChecks ? parsePrEvidence({ headRefOid: pr.headRefOid, state: pr.state, files: [], statusCheckRollup: requiredNodes }).checks.map((check, index) => { const raw = requiredNodes[index] as { name?: unknown; status?: unknown; conclusion?: unknown; state?: unknown } | undefined; // Fresh GraphQL admission keeps its original success proof, even when common normalization accepts a legacy shape. const success = raw?.name ? raw.status === "COMPLETED" && raw.conclusion === "SUCCESS" : raw?.state === "SUCCESS"; return check.state === "passed" && !success ? { ...check, state: "unknown" as const } : check; }) : []; const ciBlocker = requiredCiBlocker(full, checkEvidence, completeChecks); if (ciBlocker) blockers.push(ciBlocker); let deferred: string[] | undefined; const mac = "check (macos-latest)"; const macNodes = requiredNodes.filter(node => (node as { name?: unknown })?.name === mac); if (pr.isDraft === true && !blockers.some(blocker => blocker !== ciBlocker) && ciBlocker && [mac, "check (ubuntu-latest)", "seeded guards"].every(name => full.includes(name)) && macNodes.length === 1 && !requiredCiBlocker(full.filter(name => name !== mac), checkEvidence, completeChecks) && checkEvidence.filter(check => check.name === mac).length === 1 && checkEvidence.find(check => check.name === mac)?.state === "failed" && await draftMacosDeferred(receipt.repo, pr.headRefOid, macNodes[0], bounded)) { // The proof is fetched separately; the PR may have changed while it was read. const after = await bounded(args, 10_000, undefined, 512 * 1024); try { const refreshed = JSON.parse(after.stdout); if (after.code === 0 && !refreshed.errors && JSON.stringify(refreshed.data?.repository?.pullRequest) === JSON.stringify(pr)) { blockers.splice(blockers.indexOf(ciBlocker), 1); deferred = [mac]; } } catch { /* Incomplete evidence never admits review. */ } } if (ciBlocker && contexts?.pageInfo?.hasNextPage === false && Array.isArray(nodes) && full.every(name => { const matches = (nodes as { name?: string; conclusion?: string }[]).filter(node => node?.name === name); return matches.length === 1 && matches[0]?.conclusion === "SKIPPED"; }) && sha(commit?.tree?.oid)) { reusedRun = await identicalTreeGate(receipt.repo, commit.tree.oid, bounded, full); if (reusedRun !== undefined) { // Proof reads can await several API calls: recheck the complete head/check/thread snapshot before admission. const after = await bounded(args, 10_000, undefined, 512 * 1024); try { if (after.code !== 0 || JSON.stringify(JSON.parse(after.stdout)?.data?.repository?.pullRequest) !== JSON.stringify(pr)) reusedRun = undefined; } catch { reusedRun = undefined; } if (reusedRun !== undefined) blockers.splice(blockers.indexOf(ciBlocker!), 1); } } const threads = pr.reviewThreads; if (threads?.pageInfo?.hasNextPage !== false || !Array.isArray(threads.nodes) || threads.nodes.length > 100) blockers.push("bot review thread evidence unavailable"); else for (const thread of threads.nodes) { if (thread?.isResolved === true) continue; if (thread?.isResolved !== false) { blockers.push("bot review thread evidence unavailable"); continue; } const comments = thread?.comments; if (comments?.pageInfo?.hasNextPage !== false || !Array.isArray(comments.nodes) || comments.nodes.length > 100 || comments.nodes.some((comment: { createdAt?: string }) => !Number.isFinite(Date.parse(comment?.createdAt ?? "")))) { blockers.push("bot review thread evidence unavailable"); continue; } // #230 item 3: only a `User` comment answers. Every other or missing author kind (Bot, null, Organization, Mannequin, // EnterpriseUserAccount, a kind GitHub adds later) is a pending bot comment, so an unresolved thread settles by // resolution or a later User comment. No weaker than before, and still no body or login is read. let unanswered = false; for (const comment of comments.nodes as { author?: { __typename?: string | null } | null }[]) { unanswered = comment?.author?.__typename !== "User"; } if (unanswered) blockers.push("bot review threads unanswered"); } const requests = pr.reviewRequests; const draftApprovalEligible = !!deferred && Object.hasOwn(pr, "reviewDecision") && [null, "APPROVED", "REVIEW_REQUIRED"].includes(pr.reviewDecision) && requests?.pageInfo?.hasNextPage === false && parsePrEvidence({ headRefOid: pr.headRefOid, state: pr.state, files: [], statusCheckRollup: [], reviewRequests: requests.nodes }).pendingReviews !== null && threads?.pageInfo?.hasNextPage === false && Array.isArray(threads.nodes) && threads.nodes.every((thread: { isResolved?: unknown }) => thread?.isResolved === true); return { head: pr.headRefOid, ready: blockers.length === 0, blockers: [...new Set(blockers)], checks: full.filter(name => !deferred?.includes(name)), ...(deferred ? { deferred, draftApprovalEligible } : {}), ...(reusedRun !== undefined ? { reusedRun } : {}) }; } catch { return unknown(); } } /** Same proof floor as scripts/ci-gates.mjs: the identical tree includes the workflow itself. No skipped proof. */ async function identicalTreeGate(repository: string, tree: string, run: SdlcGh, required: readonly string[]): Promise { try { const list = await run(["api", `repos/${repository}/actions/workflows/check.yml/runs?status=success&per_page=20`, "--jq", "{total_count,workflow_runs:[.workflow_runs[]|{id,event,conclusion,head_sha}]}"], 10_000, undefined, 64 * 1024); if (list.code !== 0) return; const runs = JSON.parse(list.stdout)?.workflow_runs; if (!Array.isArray(runs) || runs.length > 20) return; for (const candidate of runs) { if (!id(candidate?.id) || candidate.conclusion !== "success" || !["pull_request", "push"].includes(candidate.event) || !sha(candidate.head_sha)) continue; const commit = await run(["api", `repos/${repository}/git/commits/${candidate.head_sha}`, "--jq", "{tree:{sha:.tree.sha}}"], 10_000, undefined, 4096); if (commit.code !== 0 || JSON.parse(commit.stdout)?.tree?.sha !== tree) continue; const jobs = await run(["api", `repos/${repository}/actions/runs/${candidate.id}/jobs?per_page=100`, "--jq", '{total_count,jobs:[.jobs[]|{name,conclusion,reused:any(.steps[]?; .name=="Reuse verified identical-tree full gate" and .conclusion=="success")}]}'], 10_000, undefined, 32 * 1024); if (jobs.code !== 0) continue; const proof = JSON.parse(jobs.stdout); if (!Array.isArray(proof.jobs) || proof.jobs.length > 100 || proof.total_count !== proof.jobs.length) continue; if (required.every(name => { const found = proof.jobs.filter((job: { name?: string }) => job?.name === name); return found.length === 1 && found[0]?.conclusion === "success" && found[0]?.reused !== true; })) return candidate.id; } } catch { return; } } /** PR cost evidence excludes titles, bodies and comment text; unknown/truncated coverage never becomes zero. */ export async function readPrCostHistory(repository: string, number: number, run: SdlcGh = sdlcGh, snapshot?: unknown, expectedHead?: string, firstSubmittedHead?: string, recordedHeadInventory = false): Promise { if (!repo(repository) || !id(number)) return; const [owner, name] = repository.split("/"); try { const query = "query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){number state headRefOid mergedAt commits(first:100){nodes{commit{oid}} pageInfo{hasNextPage}} reviews(first:100){nodes{databaseId commit{oid}} pageInfo{hasNextPage}}}}}"; let pr: any; if (snapshot !== undefined) pr = snapshot; else { const reply = await run(["api", "graphql", "-f", `query=${query}`, "-f", `owner=${owner}`, "-f", `name=${name}`, "-F", `number=${number}`], 10_000, undefined, 128 * 1024); if (reply.code !== 0) return; const parsed = JSON.parse(reply.stdout); if (parsed.errors) return; pr = parsed.data?.repository?.pullRequest; } if (pr?.number !== number || !["OPEN", "CLOSED", "MERGED"].includes(pr.state) || (expectedHead !== undefined && pr.headRefOid !== expectedHead)) return; const result: import("./cost-governance.ts").CostPr = { repo: repository, pr: number, state: pr.state, complete: false, ...(typeof pr.mergedAt === "string" && Number.isFinite(Date.parse(pr.mergedAt)) ? { mergedAt: Date.parse(pr.mergedAt) } : {}) }; if (pr.state !== "MERGED") return result; // Cost metrics count merged PRs, not an active work queue. const reviews = pr.reviews, commits = pr.commits; const reviewComplete = reviews?.pageInfo?.hasNextPage === false && Array.isArray(reviews.nodes) && reviews.nodes.length <= 100 && reviews.nodes.every((node: { databaseId?: number }) => id(node?.databaseId)); const commitComplete = commits?.pageInfo?.hasNextPage === false && Array.isArray(commits.nodes) && commits.nodes.length <= 100 && sha(pr.headRefOid) && commits.nodes.every((node: { commit?: { oid?: string } }) => sha(node?.commit?.oid)); if (reviewComplete) result.reviews = new Set(reviews.nodes.map((review: { databaseId?: unknown }) => review.databaseId).filter((id: unknown) => Number.isSafeInteger(id))).size; const heads = new Set([pr.headRefOid, ...(commits?.nodes ?? []).map((node: { commit?: { oid?: string } }) => node?.commit?.oid), ...(reviews?.nodes ?? []).map((node: { commit?: { oid?: string } }) => node?.commit?.oid)].filter(sha)); const checks = await run(["api", `repos/${repository}/actions/workflows/check.yml/runs?per_page=100`, "--jq", "{total_count,workflow_runs:[.workflow_runs[]|{id,run_attempt,head_sha,pull_requests:[.pull_requests[].number]}]}"], 10_000, undefined, 128 * 1024); if (checks.code !== 0) return result; const runs = JSON.parse(checks.stdout); if (!Array.isArray(runs.workflow_runs) || runs.workflow_runs.length > 100 || !Number.isSafeInteger(runs.total_count) || runs.total_count < runs.workflow_runs.length) return result; const linked = runs.workflow_runs.filter((row: { head_sha?: string; pull_requests?: number[] }) => heads.has(row.head_sha ?? "") || Array.isArray(row.pull_requests) && row.pull_requests.includes(number)); if (linked.some((row: { id?: number; run_attempt?: number }) => !id(row.id) || !id(row.run_attempt))) return result; result.ciRuns = linked.map((row: { id: number; run_attempt: number }) => ({ id: row.id, attempt: row.run_attempt })); // The dogfood reader supplies a separately validated, complete recorded-head inventory for its bounded time window. if (recordedHeadInventory && runs.total_count === runs.workflow_runs.length) { result.complete = reviewComplete && commitComplete; return result; } const directComplete = runs.total_count === runs.workflow_runs.length && runs.workflow_runs.every((row: { pull_requests?: number[] }) => Array.isArray(row.pull_requests) && row.pull_requests.length > 0 && row.pull_requests.every(id)); if (directComplete) { result.ciRuns = runs.workflow_runs.filter((row: { pull_requests: number[] }) => row.pull_requests.includes(number)).map((row: { id: number; run_attempt: number }) => ({ id: row.id, attempt: row.run_attempt })); result.complete = reviewComplete && commitComplete; } const branch = await readBranchCi(repository, number, firstSubmittedHead, run); const sameDirectRuns = !directComplete || branch && result.ciRuns?.length === branch.runs.length && result.ciRuns.every(row => branch.runs.some(candidate => candidate.id === row.id)); if (branch && sameDirectRuns) { result.ciRuns = branch.runs; result.fullGateRuns = branch.fullGateRuns; result.cancelledGateRuns = branch.cancelledGateRuns; result.successfulGateRuns = branch.successfulGateRuns; result.failedGateRuns = branch.failedGateRuns; result.otherGateRuns = branch.otherGateRuns; result.macosShardRuns = branch.macosShardRuns; result.firstPass = branch.firstPass; result.complete = reviewComplete && commitComplete; } return result; } catch { return; } }