From ce550b5460af572577f514d8c6dd3e68aedfa4ed Mon Sep 17 00:00:00 2001 From: garrytan Date: Tue, 3 Nov 2026 09:23:15 +0000 Subject: [PATCH] gstack-evidence carried: list a CI row only when check's grading calls it FRESH (TRUT-7 stray ci_carried entry) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-evidence | 123 ++++++++++++----------- changelog.d/gsta258-l6-label-sanitize.md | 2 + test/ship-ci-carry.test.ts | 14 +++ 3 files changed, 82 insertions(+), 57 deletions(-) diff --git a/bin/gstack-evidence b/bin/gstack-evidence index 62ad8d381..39059086c 100755 --- a/bin/gstack-evidence +++ b/bin/gstack-evidence @@ -95,7 +95,7 @@ import { isUnavailable, loadPolicyFromBase, resolveBaseBranch } from "../lib/shi import { versionAtRef } from "../lib/restamp"; import { bashCommandInvocation, runScriptSync, wtreeFingerprint } from "../lib/script-invocation"; import { findCarry, laneMapping, mappingCoverage, perFileCommand } from "../lib/ci-carry"; -import { runChanged } from "../lib/run-evidence"; +import { DEFAULT_MAX_AGE_HOURS, runChanged } from "../lib/run-evidence"; import { isConfigUnavailable, loadConfigFromBase } from "../lib/pregate/config"; import { buildRunCheck } from "../lib/pregate/run-check"; @@ -758,13 +758,76 @@ function cmdCarried(argv: string[]): number { const wtree = currentWtree(); const latest = new Map(); for (const r of records) latest.set(r.label, r); - const carried = [...latest.values()].filter((r) => r.source === "ci" && !!wtree && r.wtree === wtree && Array.isArray(r.runs) && typeof r.policy === "string" && r.policy.startsWith(policyRef)) + const carried = [...latest.values()].filter((r) => r.source === "ci" && !!wtree && r.wtree === wtree && Array.isArray(r.runs) && typeof r.policy === "string" && r.policy.startsWith(policyRef) + && gradeRecord(r, { maxAgeHours: DEFAULT_MAX_AGE_HOURS, allowPaths: [], wtreeNow: wtree }).verdict === "FRESH") .flatMap((r) => r.runs!.map((ref) => { const [run, attempt] = ref.split("/").map(Number); return { lane: r.label, run, attempt, tree: r.wtree! }; })); if (argv.includes("--json")) console.log(JSON.stringify(carried)); else for (const c of carried) console.log(`CI_CARRIED: ${c.lane} run=${c.run}/${c.attempt} tree=${c.tree.slice(0, 12)}`); return 0; } +/** check's verdict for one ledger row; `carried` lists a CI row only when it grades FRESH here, so a carry that never reached FRESH leaves no receipt entry. */ +function gradeRecord(latest: EvidenceRecord, o: { maxAgeHours?: number; expectCmd?: string; allowPaths: string[]; wtreeNow: string | undefined }): { verdict: "FRESH" | "STALE" | "ZERO-RUN"; reason: string } { + const { maxAgeHours, expectCmd, allowPaths, wtreeNow } = o; + const testsRan = typeof latest.tests_ran === "number" || latest.tests_ran === "n/a" ? latest.tests_ran : "unknown"; + const changedDuringRun = Array.isArray(latest.wtree_changed) ? latest.wtree_changed.filter((p): p is string => typeof p === "string" && p !== "" && !/[\u0000-\u001f\u007f]/.test(p)).slice(0, 5) : []; + let verdict: "FRESH" | "STALE" | "ZERO-RUN" = "FRESH"; + let reason = ""; + if (latest.exit !== 0) { + verdict = "STALE"; + reason = "recorded run failed"; + } else if (testsRan === 0) { + // Zero-run green: exit 0 with nothing executed (all skipped, empty + // selection, "No tests found"). Graded like STALE under its own label; + // "unknown" (no recognised runner summary) stays non-blocking. + verdict = "ZERO-RUN"; + reason = "runner reported 0 executed tests"; + } else if (maxAgeHours !== undefined) { + const ageMs = Date.now() - Date.parse(latest.ts); + if (!(ageMs >= 0 && ageMs <= maxAgeHours * 3600 * 1000)) { + verdict = "STALE"; + reason = `older than ${maxAgeHours}h`; + } + } + if (verdict === "FRESH" && expectCmd !== undefined && sha256(expectCmd) !== latest.cmd_sha256) { + verdict = "STALE"; + reason = "command changed (cmd_sha256 mismatch)"; + } + if (verdict === "FRESH") { + // Content binding: identical working-tree fingerprint, or a diff confined + // to the allow-list. Any git failure (gc'd tree, not a repo) → STALE — + // never an error into the calling flow. + if (!latest.wtree || !/^[0-9a-f]{40}$/.test(latest.wtree) || !wtreeNow) { + // Stored fingerprints are re-validated before reaching git argv — a + // forged/corrupt ledger line must degrade, never inject options. + verdict = "STALE"; + reason = !latest.wtree + ? changedDuringRun.length + ? `content changed during the run: ${changedDuringRun.join(", ")}` + : "record has no content fingerprint" + : !/^[0-9a-f]{40}$/.test(latest.wtree) + ? "record has malformed fingerprint" + : "current fingerprint unavailable"; + } else if (latest.wtree !== wtreeNow) { + const diff = git(["diff", "--name-only", latest.wtree, wtreeNow]); + if (diff === undefined) { + verdict = "STALE"; + reason = "content changed (fingerprint diff unavailable)"; + } else { + const changed = diff.split("\n").map((s) => s.trim()).filter(Boolean); + const outside = changed.filter((f) => !allowPaths.some((a) => f === a || f.startsWith(a.replace(/\/$/, "") + "/"))); + if (changed.length === 0 || outside.length === 0) { + reason = changed.length ? `diff confined to allow-paths (${changed.length} file(s))` : ""; + } else { + verdict = "STALE"; + reason = `content changed: ${outside.slice(0, 5).join(", ")}${outside.length > 5 ? ", ..." : ""}`; + } + } + } + } + return { verdict, reason }; +} + function cmdCheck(argv: string[]): number { // Parse: repeated --label, each optionally followed (anywhere later) by its // own --expect-cmd; pairing is positional — an --expect-cmd binds to the most @@ -824,63 +887,9 @@ function cmdCheck(argv: string[]): number { continue; } const testsRan = typeof latest.tests_ran === "number" || latest.tests_ran === "n/a" ? latest.tests_ran : "unknown"; - const changedDuringRun = Array.isArray(latest.wtree_changed) ? latest.wtree_changed.filter((p): p is string => typeof p === "string" && p !== "" && !/[\u0000-\u001f\u007f]/.test(p)).slice(0, 5) : []; const ci = latest.source === "ci" && Array.isArray(latest.runs) ? ` source=ci runs=${latest.runs.filter((s) => /^\d+\/\d+$/.test(s)).join(",")}` : ""; const detail = `label=${label} exit=${latest.exit} tests_ran=${testsRan}${latest.tests_unit === "files" ? " unit=files" : ""} ts=${latest.ts}${latest.log_path ? ` log=${latest.log_path}` : ""}${ci}`; - let verdict: "FRESH" | "STALE" | "ZERO-RUN" = "FRESH"; - let reason = ""; - if (latest.exit !== 0) { - verdict = "STALE"; - reason = "recorded run failed"; - } else if (testsRan === 0) { - // Zero-run green: exit 0 with nothing executed (all skipped, empty - // selection, "No tests found"). Graded like STALE under its own label; - // "unknown" (no recognised runner summary) stays non-blocking. - verdict = "ZERO-RUN"; - reason = "runner reported 0 executed tests"; - } else if (maxAgeHours !== undefined) { - const ageMs = Date.now() - Date.parse(latest.ts); - if (!(ageMs >= 0 && ageMs <= maxAgeHours * 3600 * 1000)) { - verdict = "STALE"; - reason = `older than ${maxAgeHours}h`; - } - } - if (verdict === "FRESH" && expectCmd !== undefined && sha256(expectCmd) !== latest.cmd_sha256) { - verdict = "STALE"; - reason = "command changed (cmd_sha256 mismatch)"; - } - if (verdict === "FRESH") { - // Content binding: identical working-tree fingerprint, or a diff confined - // to the allow-list. Any git failure (gc'd tree, not a repo) → STALE — - // never an error into the calling flow. - if (!latest.wtree || !/^[0-9a-f]{40}$/.test(latest.wtree) || !wtreeNow) { - // Stored fingerprints are re-validated before reaching git argv — a - // forged/corrupt ledger line must degrade, never inject options. - verdict = "STALE"; - reason = !latest.wtree - ? changedDuringRun.length - ? `content changed during the run: ${changedDuringRun.join(", ")}` - : "record has no content fingerprint" - : !/^[0-9a-f]{40}$/.test(latest.wtree) - ? "record has malformed fingerprint" - : "current fingerprint unavailable"; - } else if (latest.wtree !== wtreeNow) { - const diff = git(["diff", "--name-only", latest.wtree, wtreeNow]); - if (diff === undefined) { - verdict = "STALE"; - reason = "content changed (fingerprint diff unavailable)"; - } else { - const changed = diff.split("\n").map((s) => s.trim()).filter(Boolean); - const outside = changed.filter((f) => !allowPaths.some((a) => f === a || f.startsWith(a.replace(/\/$/, "") + "/"))); - if (changed.length === 0 || outside.length === 0) { - reason = changed.length ? `diff confined to allow-paths (${changed.length} file(s))` : ""; - } else { - verdict = "STALE"; - reason = `content changed: ${outside.slice(0, 5).join(", ")}${outside.length > 5 ? ", ..." : ""}`; - } - } - } - } + const { verdict, reason } = gradeRecord(latest, { maxAgeHours, expectCmd, allowPaths, wtreeNow }); console.log(`EVIDENCE: ${verdict} ${detail}${reason ? ` reason=${reason}` : ""}`); if (verdict !== "FRESH") allFresh = false; } diff --git a/changelog.d/gsta258-l6-label-sanitize.md b/changelog.d/gsta258-l6-label-sanitize.md index 5f88f42b8..30ea89d3b 100644 --- a/changelog.d/gsta258-l6-label-sanitize.md +++ b/changelog.d/gsta258-l6-label-sanitize.md @@ -1,4 +1,6 @@ - A lane label with a space or a colon, such as `pnpm lint` or `db:test`, no longer goes MISSING right after it carries (TRUT-7, truther#1170). `gstack-evidence` writes a label to the ledger with unsafe characters turned into `_`, but `check`, which `gstack-ship-release finish` and `accept` call, looked it up under the raw label, so /ship stopped with "not FRESH at the pushed head". `run`, `record-ci` and `check` now share one label spelling. +- A carry that doesn't reach FRESH no longer leaves a `ci_carried` entry in the ship receipt (TRUT-7: truther's receipt kept `pnpm_lint:41603968341/1` from the attempt that stopped). `gstack-evidence carried` now lists a CI row only when `check`'s own grading, at the 24-hour age /ship uses, calls it FRESH. ### Tests - `test/ship-ci-carry.test.ts`: `accept` with the lanes `pnpm lint` and `db:test` carries both and checks them FRESH, and `gstack-evidence check` finds both. It failed before the fix (`EVIDENCE: MISSING label=pnpm lint`). +- `test/ship-ci-carry.test.ts`: a carried `pnpm lint` row aged past 24 hours grades STALE and leaves `carried` empty. It failed before the fix (the stale row was still listed). diff --git a/test/ship-ci-carry.test.ts b/test/ship-ci-carry.test.ts index 9f5c998bd..df4a1b4b0 100644 --- a/test/ship-ci-carry.test.ts +++ b/test/ship-ci-carry.test.ts @@ -134,6 +134,20 @@ describe('finish, push --early and accept (A1)', () => { for (const label of ['pnpm lint', 'db:test']) expect(f.run(path.join(BIN, 'gstack-evidence'), ['check', '--label', label]).stdout).toMatch(/^EVIDENCE: FRESH /m); }); + test('a carried row that no longer grades FRESH leaves no ci_carried entry (TRUT-7: pnpm lint)', () => { + const f = fx(); + world(f); + expect(f.run(RELEASE, ['push', '--branch', 'feat', '--early', '--base', 'main'], unattended).status).toBe(0); + expect(f.run(RELEASE, ['accept', '--base', 'main', '--lane', `pnpm lint=${FREE}`, '--wait', '0']).status).toBe(0); + const carried = () => JSON.parse(f.run(path.join(BIN, 'gstack-evidence'), ['carried', '--base', 'main', '--json']).stdout); + expect(carried()).toEqual([expect.objectContaining({ lane: 'pnpm_lint', run: 7, attempt: 1 })]); + const [ledger] = findFilesBySuffix(path.join(f.state, 'projects'), '-evidence.jsonl'); + const rows = fs.readFileSync(ledger!, 'utf8').trim().split('\n').map(l => JSON.parse(l)); + fs.writeFileSync(ledger!, rows.map(r => JSON.stringify(r.source === 'ci' ? { ...r, ts: at(30 * 60) } : r)).join('\n') + '\n'); + expect(f.run(path.join(BIN, 'gstack-evidence'), ['check', '--label', 'pnpm lint', '--max-age', '24']).stdout).toMatch(/^EVIDENCE: STALE .* reason=older than 24h/m); + expect(carried()).toEqual([]); + }); + test('finish defers the mapped lane; accept carries it from the green run at the pushed head', () => { const f = fx(); world(f);