mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-03 15:27:56 +01:00
* fix(ship): base merges, adjacent base edits, test-only and audit-requested fixes no longer force a second review loop (GST-407, GST-384, GST-391) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship driver: trim two phrases for headroom with #3662 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship driver: keep the Next steps heading phrase the handoff check reads; trim elsewhere Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: regenerate ship goldens Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes comparePath returns same only when every hunk sits where the base change puts it (patch-ids ignore positions) and treats a file-mode change as changed; the scoped-review packet says when a fence is truncated; --docs-result must be the posted audit's result; the --context-reviewed record goes beside the packet, outside the worktree. Negative controls for each. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pass-2 review fixes A test edit routes as tests= only when it adds lines with no gate tag; a removed or changed line or an unreadable test diff goes through the full loop. Diffs skip textconv. A hunkless patch (binary, empty, mode-only) is same when its patch-id matches. Without --reviewed-base test files route as before. finish's packet path and attempt's state directory have fallbacks. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: Step 11 review fixes; a version-only stamp_files edit after review is release metadata (GBRA-251) gateEditedPaths reads removed lines only inside hunks; carry refuses when a path listing fails; docs freshness stops on a failed listing; attempt counts an unreadable state file. releasePaths includes stamp_files paths whose edit is a version swap under stamp_patterns. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: no test edit skips review; guard packet writes; exclusive attempt state A test-only edit with no gate tag gets the scoped fix-check of its test hunks instead of a lanes-only rerun. Packet write failures print a STOP or RECEIPT_CARRY_REFUSED. attempt's allowance uses an exclusive create. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: attempt prints gstack's install command; carry checks the product diff outside context paths The attempt STOP no longer echoes the audit child's fix text. A scoped-review carry now requires the product patch-id without the context paths to match at both heads. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: oversized packets take the full loop; exact --docs-requested match; name-only test globs only in test dirs A scoped-review packet section over 200 lines sends that path to the full loop instead of a truncated packet. The docs-result path match is exact. test_*.py, *Test.java and similar count as test code only under a test directory. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs(unattended): carry's scoped-review refusal and what it keeps Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: carry's scoped-review route in ship-policy, review-policy and troubleshooting (docs-a-1) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test(windows): curate ship-rereview-triggers out of the Windows lane like ship-release (run 41193336209) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(review-log): record a notes-only /ship pass clean; name the cause and repair when a pass's tree moved (GST-430) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(receipt): carry and write handle PRs with thousands of files; tree receipt fails closed on a failed diff (GST-448) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(ship): preflight routes RECEIPT_CARRY_SCOPED_REVIEW to the scoped review; carry --dry-run writes no packet (GST-442) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs(ship): Step 1.4 and Step 16 route the scoped-review refusal and the --docs-requested fix; trims keep the cap (GST-442, GST-444) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: /ship review pass 1: notes-only floor, scoped carry order, named temp and diff refusals - A notes-only pass is recorded clean only when every finding is fixed, or noted or deferred below the blocking floor (never CRITICAL or blocking), by its logger-stamped action, and the findings account for the claimed issue and critical counts (GST-430). - The preflight scoped carry route commits and pushes the base-merge fix before writing the packet (GST-442). - patchId refuses by name on an unwritable TMPDIR; the tree receipt names why its diff failed (GST-448). - Step 16 stage 3 spells out --docs-requested with its --docs-result (GST-444). Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: pin the Step 16 --docs-requested route with its --docs-result Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: a claimed clean review row with a logger-stamped open gate finding reads UNVERIFIED (red-team, pass 1) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: Step 11 findings: gate rows from --findings are stamped, one open rule, stale patch-id dirs swept - deriveFromFindings stamps gate rows with the session kind before deriving status, so a findings file cannot dispose of a gate finding the unattended logger forces open (outside-voice#1). - reviewFreshness reads a claimed row's findings with isOpenByAction, the rule the notes-only rewrite uses; noted and deferred clear a finding only when it is marked blocking: false and is not CRITICAL. - patchId sweeps gstack-patch-id-* directories over an hour old that a killed carry left in TMPDIR. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: Step 11 round 2: stamped-open gates win over status and disposition; resolved gate, skipped and below-floor actions stay CURRENT - isOpenFinding treats action 'open' (the logger's forced-open stamp) as open before a row's status or disposition. - reviewFreshness flags a claimed clean row only for an 'open' action or a CRITICAL or blocking deferral or note, so interactive kept and restored gates, skipped advisories and INFORMATIONAL deferrals read CURRENT as before. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: carry dry run offers the scoped route last; patch-id diff under the git dir; changelog matches the review-log floor Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: a claimed clean review row with any unresolved non-gate CRITICAL finding reads UNVERIFIED (pass 2 red-team) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: carry dry-run scoped route, patch-id directory, tree-receipt refusal (docs-d-1 audit) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(ship): Step 3 names merge conflicts in its STOP sentence again (ship-workflow-clarity pin) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(tree-receipt): keep the gated-to-current diff at the file helper timeout (60 s); pregate literals check Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test(carry): large-diff fixture sets core.longpaths so Git for Windows can add its long receipt paths (windows-free-shard 1, run 41210080055) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(review): a stale local base branch yields to its origin twin in gate-diff and diff-scope (GST-462) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(ship): Step 11 saves the targeted record and the receipt writes carry it; Step 9 clears it (GST-464) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: GST-464 and GST-462 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(review): pass 1: ahead and diverged labels, diff-scope needs shared history; preflight refreshes origin/<base> and stops on a diff far beyond the PR (GST-462, HARN-15) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pass-2 review fixes for base twin, prSized stop and targeted record Resolve the tracking twin by refs/remotes/<remote>/<ref> and only for a bare branch name, keep a local base that is ahead or diverged, quote the prSized stop's base and name the fork case, and have every Step 9 entry create, exclude and clear .gstack/tmp/targeted.json. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: origin's tracking base wins whenever related; prSized lower bound The fix-check found that keeping a local base ahead of origin left unpushed base commits out of the gate scan while review-plan listed them. The tracking branch now wins whenever it shares history with HEAD, an explicit origin/main reads refs/remotes/origin/main, the preflight also stops when the diff lists fewer files than the PR, and the Step 9 exclude line survives a missing .git/info and an exclude file without a final newline. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: the tracking-ref claim holds while the tracking branch exists Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: repin the two ship section briefs under no-loss replay GST-464 GST-464 brief-identity check: every reviewer brief lib/review-brief.ts cuts from ship/sections/review-army.md and adversarial.md stays byte-identical to the bodies the L and GST-257 replays admitted; only lead instructions outside the cut spans changed. The check is a test CI runs. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship: keep Step 9's PASS_HEAD sentence whole; repin review-army under GST-464 The targeted-record line follows the PASS_HEAD/RANGE_BASE sentence instead of splitting it (test/review-fix-range.test.ts pins that sentence). The brief-body check still holds: every reviewer brief is byte-identical. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: restore bin/gstack-ship-receipt after the main merge The merge commitb10e2dbwrote a truncated copy of the file. This is the three-way merge of both sides with the refused() union: main's codes plus RECEIPT_CARRY_SCOPED_REVIEW. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: narrow the consumer scan and give it one 300 s deadline (GST-544) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: keep the consumer scan deadline under the 180 s plan timeout (review fix) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: plain paths under --literal-pathspecs; whole-millisecond timeout override (review fix) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: whole-tree grep for non-UTF-8 names, no argument spread, name spawn errors (review fix) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: cap the consumer grep output at 256 MB across calls; per-scan deadline wording (review fix, cycle 4) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-collect: read a missing spawn status as null (Bun 1.4.2 returns undefined on ENOENT) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: review-collect whole-tree helper reads a null status as -1 (typecheck:test ratchet) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-bots: /review and /ship triage every configured review bot, not only Greptile (#3122) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes (gh before 2.48 pages without --slurp; driver routes Step 10; tests for guards, review-bot fence, REMOTE_SLUG owner) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: drop the review-bot fence test that could not fail (the tool-fence WARN degrade is unreachable: placeholder spans are skipped first) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: Step 11 review fixes (record by comment id so a filename never reaches a shell; lookup failures are errors; tojson pages; longest bot prefix wins) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pass-2 review fixes (record refuses * paths, colliding ids and a missing fetch dir; parent requires id and dir; botOwning doc) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review: classify the PR type and always check repo conventions (#356) The checklist now opens with a PR-type line (application, CI/infra, scripts/tooling, docs-only or mixed). Application and mixed diffs run every category; other types always run five categories, including the new Repo Conventions & Platform Fit, and report the rest as not applicable in one line instead of skipping silently. No-loss replay: GSTA-224 H1 preregistered no-loss run (run id gsta224-h1-20261031, Opus 5.5, 81 calls, $9.53) against main's checklist with a no-gstack arm; every preregistered bar passed. The review/checklist.md pin in test/review-packets-removed.test.ts moves to a32963ef with replay 'GSTA-224 H1'. Refs GSTA-224 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review-bots: allowlist the record placeholders and match Step 10's id rule in tests The record fence uses <comment-id>, <fix|fp|already-fixed> and <category>, each with an enum or numeric grammar; <greptile-dir> becomes <bot-dir>. review/greptile-triage.md also lists every REVIEW_BOTS skip reason. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: review-bot triage covers every configured bot (#3122) README, CONTRIBUTING, the contributor reference and standalone-binaries name gstack-review-bots and the review-bot fence; TODOS drops the #3122 item; the changelog fragment covers record-by-id and lookup errors. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: the PR-type test also pins the compose, Terraform and README/CHANGELOG phrases Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: follow the review-bot rename in the remote free tier's three reds The cross-skill consistency judge describes review-bot-history.md and its record helper; the tracker-guard exemption for the removed raw Greptile fetch in review/greptile-triage.md is dropped (the fetch now lives in gstack-review-bots, whose bodies still reach context only through gstack-issue-guard); the Step 10 PR-check test expects 'Review bots: not run'. HARNESS_VERSION stays 6: skill-fixture.ts and llm-judge.ts are recorded non-behavioral for every other case. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: keep the cross-skill judge's rubric to one renamed bullet Name review-bot-history.md beside the older greptile-history.md so the calibration corpus still matches, and keep the REMOTE_SLUG bullet. One live 3-sample panel at this tree: consistent 3/3, scores 4/4/4. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: H1 fragment passes prose lint Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: name the H1 fragment after its branch; TODOS drops #356 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: /review's PR type step in docs/skills.md (#356) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship-policy: review_exclude_paths key and loadPolicyAt (GST-508) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * review: apply the base policy's review_exclude_paths and the reviewer diff ceiling (GST-508) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * receipt: bind review_exclude in write, carry and the queue (GST-508) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs, tests and changelog for review_exclude_paths (GST-508) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes (binary by bytes, links kept, untracked files out of the selection) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: keep binary files at code paths under review_exclude_paths Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: name the reasons that keep a binary file under review_exclude_paths Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: keep every review_exclude_paths candidate when the gate scan is unknown; docs example names its version Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: keep the NUL fixture clear of the sync-spawn tripwire Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: split the ceiling sentence Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: keep a live binary file whose bytes cannot be read; tests render outside voices from a clean checkout and read the ship fixture text Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test(review-exclude): explicit +x for the census script; skip the chmod-000 read case on Windows and root Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * refactor: move loadPolicyAt to lib/ship-policy-at.ts so lib/ship-policy.ts stays under the 800-line module cap Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: refuse a base that contains HEAD; skip a self-pointing origin/HEAD (GST-774) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * chore: regenerate gstack-doctor-components.sh for BASE_CONTAINS_HEAD Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: land resumes a merged merge-only PR instead of refusing BASE_CONTAINS_HEAD (GST-774 review) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: changelog fragment for GST-774 Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: troubleshooting names diff-scope base_contains_head and the self-pointing no_base case (GST-774 docs audit) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: repin the reviewer briefs to GST-508's no-loss-tested bytes ship/sections/review-army.md, ship/sections/adversarial.md and lib/review-depth.ts now pin the bytes the GST-508 preregistered no-loss replay ran atef2760f(handoffs/GSTA-202/noloss-results.md in the GStack Minder drive): Arm V 33/41 vs R 33/41, delta 4, exact sign flip, p_NI 0.0279 (pass). The merge of main83ae96eleaves these three files byte-identical toef2760f, and an Arm P unit (3593) renders byte-identical briefs atef2760fand at the merge. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: GST-774 review round 1 — merged resume depth, aliased and detached PR checkouts, develop/trunk defaults Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship: Step 20 says a metrics row, keeping DRIVER.md at 12 KB after the main merge Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: ship-parts resend case leaves room for the first phase-3 part after the Step 9 targeted-record sentence Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: /ship delivers review/checklist.md in two parts so checklist v1 fits the 24576-byte part cap (#3670) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: drop the GST-407 and GST-430 fragments, already released on main (stacked history) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * wave review-53cfb5bfe011: lib/ship-policy.ts back to 800 lines (module-size ratchet after merging #3744 and #3795) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * wave review-53cfb5bfe011: /ship pass 1 fixes - lib/ship-receipt-depth.ts: a merged resume (land after a true merge) no longer recomputes the review_exclude digest over the emptied range, so it lands instead of RECEIPT_INVALID (failing-first land test with review_exclude_paths active). - bin/gstack-review-dispatch render: a standalone targeted render under an active review_exclude_paths refuses and points at the pass manifest voices stage, instead of writing a brief with no selection that leaves verify dirty (failing-first test). - test/default-base.test.ts: #3795 expectations follow main R51 depth (light for an 80-line code diff; runtime_lines=80 still proves the real base). - test/review-dispatch.test.ts: the GST-897 fingerprint test renders from the clean checkout, like its neighbor, because CI rebuilds the tracked lib/diagram-render/dist/diagram-render.html and the reviewer diff ceiling refused the ROOT render. * wave review-53cfb5bfe011: /ship pass 2 fixes - lib/review-exclude.ts: binaryContent reads each blob's first 8,000 bytes (git cat-file blob, 8 KB buffer) instead of one --batch read capped at 256 MB, so head mode (receipt, carry, queue) and the plan agree past 256 MB of blobs; an unreadable blob is kept, as in live mode. Failing-first test with two 270 MB blobs. - lib/review-bots.ts: a file-level bot review comment (subject_type file, no line position) is triaged instead of read as outdated. Failing-first test. - test/ship-queue-land.test.ts: the merged-resume exclusion test runs its gate scan under the fixture state root, not the real home (CI free-suite 6 home containment). * fix: merge follow-ups for main3d99c22: origin/* stays qualified in diff-scope's twin step, retro skeleton back under its cap, combined no-loss pin GST-508+R51+GST-911 * fix(review): receipt write gate-scans whenever the base declares review_exclude_paths; collectLive never re-qualifies origin/<base> (#3852 /ship red-team#1, outside-voice#1) * test: the docs-only receipt digest test keeps its gate scan out of the real home (free-suite shard 5 FREE_SUBSHARD_REJECTED) * fix(mergefc292e22f): tentative REVIEW_EXCLUDE_KEY_VERSION, classify targeted.json, render main's new outside-voice test from a clean checkout - lib/ship-policy.ts: REVIEW_EXCLUDE_KEY_VERSION 1.93.0.0 -> 1.94.0.0, the next-minor tentative form stamp_paths moves (docs/ship-policy.md, #3512); 1.93.0.0 is a released heading that never names the key. - test/gstack-clean-producers.test.ts (new from main): KEPT gains targeted.json, the wave's --review-targeted input. - test/review-dispatch.test.ts: main's wrong-tree test rendered the outside voice from ROOT; a CI build that dirties ROOT pushes that diff past the wave's REVIEW_DIFF_MAX_BYTES ceiling, so render refused and verify saw no brief (exit 3). It now runs in cleanCheckout like the wave's other outside-voice tests. * test(merge68410828f): GST-967's brief-body pin carries the wave's GST-508 placeholders The merged sectionTexts differ from main's by exactly the wave's GST-508 exclude-pathspec lines (merged minus main equals wave minusfc292e22f); the 1,440-case plan grid still hashes to main's MAIN_PLANS. Checks: GStack Minder drive handoffs/GSTA-202/merge-6841-checks.md. * ship: CI carries mapped lanes, --reuse tree cache, early push, finish/accept split (GSTA-258 A) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * ship: name the ship and pregate bins in prose so skill-bin-drift does not read a bin Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * size-ledger: re-record ship after the tests.md wording fix Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * pregate: a carried run clears the remote gate through remote_gate_runs, so a paid-only remainder is not a dead end Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * merge cleanup: drop wave fragments main already consumed and a duplicated KEPT entry Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(ship): record-ci verifies the CI run itself; finish's deferred preflight keeps pregate.json /ship pass 1 findings red-team#1 and specialist-batched#1 (GSTA-258 piece A). Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * feat(pregate): CI carry is gstack's default remote gate route; the VM is opt-in (plan choice F1) The uncleared remote_gate obligation names the CI route first when the base declares remote_gate_runs. docs/pregate.md says how gstack clears it, including the pull_request merge-ref tree mismatch. GSTA-258 piece A. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes (round 2) record-ci records the base policy that mapped the lane; the CI-first remote gate hint respects --no-ci-carry and reruns the whole pregate; changelog and docs say what the tool does. GSTA-258 piece A. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: CI carry in CONTRIBUTING, recommended-harness and the pregate usage line (ship docs audit) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
630 lines
46 KiB
TypeScript
630 lines
46 KiB
TypeScript
/**
|
||
* headless-artifacts — the one versioned contract for every artifact the
|
||
* unattended workflows write (plan B0): `tasks` (v2), `decisions`, `findings`,
|
||
* `timing`, the `run` manifest, `pregate` and `ship-receipt`. One JSON Schema
|
||
* per artifact is the source of truth for `gstack-artifact schema <name>` and
|
||
* for the row validation `validateRun` performs, so a parent on another
|
||
* machine reads the same contract this code enforces. Readers and writers:
|
||
* bin/gstack-artifact (validate | schema | ack | urgent), bin/gstack-gate
|
||
* (decisions), bin/gstack-autoplan-timing (timing), bin/gstack-review-log
|
||
* --findings (findings), and the B-runner/C-stamp tools that stack on them.
|
||
* Docs: docs/unattended.md. Error codes: lib/result-codes.ts.
|
||
*/
|
||
import { createHash } from 'node:crypto';
|
||
import * as fs from 'node:fs';
|
||
import * as path from 'node:path';
|
||
import type { ResultCodeName } from './result-codes';
|
||
import { withFileLock } from './spend-ledger';
|
||
|
||
export type Severity = 'Critical' | 'High' | 'Medium' | 'Low' | 'Informational';
|
||
export type Disposition = 'open' | 'accepted' | 'partially_accepted' | 'rejected' | 'deferred' | 'fixed';
|
||
export type Voice = 'native' | 'outside';
|
||
export type DecisionKind = 'auto' | 'approval' | 'user_challenge';
|
||
export type DecisionStatus = 'pending' | 'approved' | 'overridden' | 'rejected' | 'auto_kept';
|
||
export type TaskStatus = 'pending' | 'ready' | 'blocked_on_dependency' | 'blocked_on_decision' | 'in_progress' | 'done';
|
||
export type VoiceStatus = 'completed' | 'unavailable' | 'disabled' | 'skipped' | 'failed' | 'pending' | 'running';
|
||
export type ResultStatus = 'complete' | 'reviewed' | 'gate_pending' | 'amend_pending' | 'incomplete' | 'interrupted' | 'refused';
|
||
export type RunStatus = ResultStatus | 'awaiting_result' | 'running';
|
||
|
||
export interface FindingRow {
|
||
schema_version: 1; run: string; id: string; phase: string; voice: Voice; severity: Severity; title: string;
|
||
model?: string; file?: string; line?: number; disposition?: Disposition; resolution?: string;
|
||
plan_items?: string[]; native_counterpart?: string | null; source?: string; changes?: 'scope' | 'cost' | 'direction';
|
||
}
|
||
export interface DecisionRow {
|
||
schema_version: 1; run: string; id: string; title: string; options: string[]; recommended: string | null;
|
||
kind: DecisionKind; status: DecisionStatus; gate_rev: number; label?: string; phase?: string;
|
||
default_if_unanswered?: string | null; chosen?: string | null; answered_at?: string; reply?: string;
|
||
/** Who answered: `human` or `agent:<id>` (required in unattended runs). */
|
||
answered_by?: string; source?: string; cost?: string;
|
||
/** Why the recommendation (gate items built by gstack-autoplan), what a user challenge changes, and the carry-forward identity. */
|
||
recommended_basis?: string; changes?: string; origin?: string; carried_from?: string; chosen_text?: string;
|
||
/** An agent-relayed answer: the owner's verbatim words, and whether they parsed under the gate grammar. */
|
||
owner_reply?: string; owner_reply_parsed?: boolean; recommended_by?: string;
|
||
/** How the owner's own words answered (lib/gate-list.ts replyShape): their mode, whether they named this item, the rec_hash answered against, and who typed the reply. */
|
||
owner_mode?: 'rec' | 'rec_except' | 'tokens' | 'as_is' | 'unparsed'; owner_named?: boolean; rec_hash?: string; answered_role?: 'human' | 'agent';
|
||
/** An agent's own words (`--words-by agent:<id>`): never the owner's, so never carried or imported as the owner's. */
|
||
answer_words?: string; answer_words_parsed?: boolean; words_by?: string;
|
||
/** A whole-gate carry (lib/autoplan-carry.ts): the earlier p1 decision whose owner words this answer reuses, its gate_rev, why, who relayed them, items taken under "do it", and the delta check attempt. */
|
||
carried_gate_from?: string; carried_gate_rev?: number; carry_reason?: string; carried_answered_by?: string; carried_under?: string; carry_check?: string;
|
||
}
|
||
export interface TaskRow {
|
||
schema_version: 2; run: string; id: string; title: string; status: TaskStatus; depends_on: string[];
|
||
findings: string[]; blocked_by: string[]; acceptance: string; pr?: string; item?: string; tier?: number;
|
||
priority?: string; component?: string; files?: string[];
|
||
}
|
||
export interface TimingPhase {
|
||
phase: string; started_at?: string; ended_at?: string; wall_s?: number; outside_s?: number; native_s?: number;
|
||
amend_s?: number; note?: string; start_utc?: string; end_utc?: string;
|
||
}
|
||
export interface TimingFile {
|
||
schema_version: 1; run: string; phases: TimingPhase[]; started_at?: string; total_wall_s?: number;
|
||
session_kind?: string; estimate?: Record<string, number | null>; [extra: string]: unknown;
|
||
}
|
||
export interface VoiceOutcome { status: VoiceStatus; model?: string; provider?: string; output?: string; [extra: string]: unknown }
|
||
export interface RunPhase { snapshot?: string; native?: VoiceOutcome; outside?: VoiceOutcome }
|
||
export interface RunManifest {
|
||
schema_version: 1; run: string; status: RunStatus; required_phases: string[]; phases: Record<string, RunPhase>;
|
||
artifacts: Record<string, { path: string; sha256: string }>; counts?: Record<string, number>;
|
||
base?: string; host?: string; session_kind?: string; gate_rev?: number; gate?: Record<string, unknown>;
|
||
consumed_by?: Array<{ consumer: string; at: string }>; deviations?: string[]; [extra: string]: unknown;
|
||
}
|
||
export interface PregateCheck {
|
||
id: string; status: 'pass' | 'fail' | 'warn' | 'incomplete' | 'requires-remote' | 'not_installed'; ms?: number; detail?: string;
|
||
// C-pregate (C8): stage, the input identity the check ran against, the fix clause and result code, and per-item lines.
|
||
stage?: 'preflight' | 'tests'; inputs?: Record<string, string>; fix?: string; code?: string; lines?: string[];
|
||
// Lane D (GSTA-112): a non-pass names its cause; an environment cause carries its kind and evidence.
|
||
cause?: 'diff' | 'environment'; environment?: { kind: string; evidence: string; proof?: 'launch' | 'output' };
|
||
/** `regen`: the touched outputs the generator recreated byte-identically from a deleted copy (what a receipt carry may set aside as generated). */
|
||
proven?: string[];
|
||
/** `lanes` under `--from-run`: the run receipt and the selected local test files that run already passed on this tree (not run again here). */
|
||
from_run?: { receipt: string; files: string[] };
|
||
}
|
||
/** A remote_gate receipt accepted from a stamp-only predecessor tree (lib/pregate/remote-receipt.ts): the gated tree, the tree it was accepted for, the policy, and every differing path with its stamp rule. */
|
||
export interface PregateReceiptSubstitution { gated_tree: string; accepted_for: string; policy: string; files: Array<{ path: string; rule: string }> }
|
||
export interface PregateRemote { lane: string; platform: string; workflow: string; job: string; tests: string[]; receipt: string | null; cleared_by?: string; command?: string; substitution?: PregateReceiptSubstitution }
|
||
export interface PregateFile {
|
||
schema_version: 1; tree: string; checks: PregateCheck[]; run?: string; stage1_ms?: number; stage2_ms?: number;
|
||
// C-pregate (C8): head/base identity, the policy sources, persisted requires-remote obligations and the one-line verdict.
|
||
head?: string; base?: string; generated_at?: string; policy?: string; registry?: string; requires_remote?: PregateRemote[]; verdict?: string;
|
||
/** sha256 of the user-owned policy when `policy` is `user <path>`; verify re-checks it. */
|
||
policy_digest?: string;
|
||
/** Under the defaults, the user-owned policy path that was looked for and absent; verify checks it is still absent. */
|
||
policy_user_path?: string;
|
||
/** This run's scratch directory under the state root (logs, scan files). */
|
||
scratch?: string;
|
||
}
|
||
export interface ShipReceipt {
|
||
schema_version: 1; head: string; base: string; pr?: number; version?: string; gated_tree?: string;
|
||
gate?: Record<string, unknown>; ci?: Record<string, unknown>; spend_usd?: number | string; predecessor?: string;
|
||
/** GSTA-258: the lanes CI carried at this head's tree, one entry per run (gstack-evidence carried). */
|
||
ci_carried?: Array<{ lane: string; run: number; attempt: number; tree: string }>;
|
||
session_kind?: string; artifacts_consumed?: string; run?: string;
|
||
// C-stamp (C3/C5/C7): the tree receipt's verdicts and the policy provenance, all optional.
|
||
thread?: string; tree?: string; gate_reuse?: string; policy?: string; queue_mode?: string; preregistration?: string; history?: string;
|
||
// C-pregate (C8): the pre-gate's verdict against this head and its remote obligations (publication requires their receipts).
|
||
pregate?: string; requires_remote?: string[];
|
||
/** Approval classes (lib/approval-citations.ts): source, status, one record per declared class with hits, report-only built-in hits. */
|
||
approval?: Record<string, unknown>;
|
||
// C-pregate (C6): issue links the body carries, by intent.
|
||
// Related open PRs by other authors (Lane D): `supersedes` / `credit` only on the owner's confirmation.
|
||
issues?: { fixes: number[]; refs: number[]; flakes: Array<{ issue: number; run: string }>; related?: Array<{ pr: number; repo?: string; author: string; closes: number[]; supersedes?: boolean; credit?: boolean }> };
|
||
/** Harness lane E: the review loop's counters (gstack-review-scope summary) and the carry record when the receipt followed a head whose product diff was unchanged (gstack-ship-receipt carry). */
|
||
review_loop?: { passes: number; blocking_found: number; deferred: number; noted?: number; fixed?: number; scope?: string; fix_checks?: number; rounds?: number; dispatches?: Record<string, number>; est_input_tokens?: number; unrecorded?: number; ledgers_read?: number };
|
||
carry?: { from: string; reason: string; product_patch_id: string; changed_paths: string[]; merge_commits: number; range_diff: string; covers: string[]; reruns: string[]; trees?: string[]; ancestry?: 'linear' | 'rewritten' };
|
||
/** The reviewed head's tree and its product patch-id against the base (gstack-ship-receipt write; carry rebinds both): what a carry across rewritten history compares. */
|
||
head_tree?: string; product_patch_id?: string;
|
||
/** The gstack that wrote the receipt, and the review depth the reviewed head needs, recomputed at write (lib/ship-receipt-depth.ts receiptDepthAtHead). */
|
||
gstack_version?: string;
|
||
review_depth?: { depth: 'light' | 'standard' | 'full'; class: 'docs' | 'tests' | 'code' | 'gate'; runtime_lines: number; rules: string; base: string; registry: string | null; gate_scan: { tagged: number | null; head: string }; reasons: string[]; risk?: string[] };
|
||
/** The owner's acceptance of a stop after a fix-check edit under frozen scope (#3559); the PR body repeats it word for word. */
|
||
review_loop_exception?: { accepted_by: string; date: string; via: string; reason: string; fix_check: { head: string; findings: number; files?: string[] } };
|
||
/** The targeted pass on money, gate, auth and secret code (lib/review-targeted.ts), bound to the receipt's product_patch_id; `done` only with its output collected. */
|
||
review_targeted?: { classes: string[]; rules: string; brief_sha: string; product_patch_id: string; status: 'done' | 'failed' | 'not-run'; findings: number; raised_by: string };
|
||
/** Step 5 failures gstack-base-red proved are the base's own (lib/base-red.ts): a conditional receipt the queue refuses until /ship --resume rewrites it without this block. */
|
||
base_red?: { cases: string[]; files: string[]; base: string; lane: string; expires_at: string };
|
||
/** The base policy's review_exclude_paths at the reviewed head (lib/review-exclude.ts); absent when the base declares none. Carry and the queue compare `digest`. */
|
||
review_exclude?: { source: string; patterns: string[]; applied: string[]; excluded: number; excluded_bytes: number; kept: number; gate_tagged: string[]; patterns_sha256: string; list_sha256: string; digest: string };
|
||
}
|
||
|
||
export const RESULT_STATUSES: readonly ResultStatus[] = ['complete', 'reviewed', 'gate_pending', 'amend_pending', 'incomplete', 'interrupted', 'refused'];
|
||
const TERMINAL_RUN: readonly string[] = ['complete', 'reviewed', 'gate_pending', 'amend_pending', 'refused', 'incomplete'];
|
||
const TERMINAL_VOICE: readonly string[] = ['completed', 'unavailable', 'disabled', 'skipped', 'failed'];
|
||
const RESOLVED: readonly string[] = ['accepted', 'partially_accepted', 'rejected', 'deferred', 'fixed'];
|
||
|
||
/** Shared exit table for every workflow command; printed by each bin's --help. */
|
||
export const EXIT = { ok: 0, fail: 1, usage: 2, refused: 3 } as const;
|
||
export function exitTableHelp(): string {
|
||
return 'Exit codes: 0 ok · 1 fail · 2 usage · 3 refused or needs a flag';
|
||
}
|
||
|
||
/** `GSTACK_RESULT:` terminal line for workflow commands (never for pure queries). */
|
||
export function formatResult(skill: string, status: ResultStatus, run: string): string {
|
||
return `GSTACK_RESULT: skill=${skill} status=${status} run=${run}`;
|
||
}
|
||
export function parseResult(text: string): { skill: string; status: ResultStatus; run: string } | undefined {
|
||
const lines = text.split('\n').filter(l => l.startsWith('GSTACK_RESULT: '));
|
||
const m = /^GSTACK_RESULT: skill=(\S+) status=(\S+) run=(.+)$/.exec(lines[lines.length - 1] ?? '');
|
||
if (!m || !RESULT_STATUSES.includes(m[2] as ResultStatus)) return;
|
||
return { skill: m[1]!, status: m[2] as ResultStatus, run: m[3]! };
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// JSON Schemas (one per artifact; also the row validator's source of truth)
|
||
// ---------------------------------------------------------------------------
|
||
type Schema = {
|
||
type?: string | string[]; required?: string[]; properties?: Record<string, Schema>; enum?: readonly unknown[];
|
||
items?: Schema; const?: unknown; description?: string; minimum?: number;
|
||
};
|
||
const str: Schema = { type: 'string' };
|
||
const strList: Schema = { type: 'array', items: str };
|
||
const nullableStr: Schema = { type: ['string', 'null'] };
|
||
const num: Schema = { type: 'number' };
|
||
const voiceOutcome: Schema = {
|
||
type: 'object', required: ['status'],
|
||
properties: { status: { enum: ['completed', 'unavailable', 'disabled', 'skipped', 'failed', 'pending', 'running'] }, model: str, provider: str, output: { ...str, description: 'artifact key holding this voice’s immutable output; defaults to <phase>-<voice>.md' } },
|
||
};
|
||
|
||
export const ARTIFACT_SCHEMAS = {
|
||
findings: {
|
||
type: 'object', description: 'one row per finding in findings.jsonl; shares the review-log row shape (bindReview --findings derives status from it)',
|
||
required: ['schema_version', 'run', 'id', 'phase', 'voice', 'severity', 'title'],
|
||
properties: {
|
||
schema_version: { const: 1 }, run: str, id: { ...str, description: '<run>-<phase>-<voice>-<n>' }, phase: str,
|
||
voice: { enum: ['native', 'outside'] }, model: str, severity: { enum: ['Critical', 'High', 'Medium', 'Low', 'Informational'] },
|
||
title: str, file: str, line: { type: 'integer' }, disposition: { enum: ['open', 'accepted', 'partially_accepted', 'rejected', 'deferred', 'fixed'] },
|
||
resolution: str, plan_items: strList, native_counterpart: nullableStr, source: str,
|
||
changes: { enum: ['scope', 'cost', 'direction', 'packaging', 'ordering'] },
|
||
},
|
||
},
|
||
decisions: {
|
||
type: 'object', description: 'one row per decision in decisions.jsonl; gate items reference gate_rev (lib/gate-list.ts)',
|
||
required: ['schema_version', 'run', 'id', 'title', 'options', 'recommended', 'kind', 'status', 'gate_rev'],
|
||
properties: {
|
||
schema_version: { const: 1 }, run: str, id: str, label: str, title: str, options: strList, recommended: nullableStr,
|
||
kind: { enum: ['auto', 'approval', 'user_challenge'] }, status: { enum: ['pending', 'approved', 'overridden', 'rejected', 'auto_kept'] },
|
||
default_if_unanswered: nullableStr, gate_rev: { type: 'integer', minimum: 1 }, phase: str, chosen: nullableStr,
|
||
answered_at: str, reply: str, source: str, cost: str,
|
||
owner_mode: { enum: ['rec', 'rec_except', 'tokens', 'as_is', 'unparsed'] }, owner_named: { type: 'boolean' }, rec_hash: str, answered_role: { enum: ['human', 'agent'] },
|
||
recommended_basis: str, changes: str, origin: str, carried_from: str, chosen_text: str,
|
||
carried_gate_from: str, carried_gate_rev: { type: 'integer', minimum: 1 }, carry_reason: str, carried_answered_by: str, carried_under: str, carry_check: str,
|
||
},
|
||
},
|
||
tasks: {
|
||
type: 'object', description: 'tasks.jsonl v2; v1 rows (phase/run_id/source_finding, scripts/resolvers/tasks-section.ts) still aggregate but do not validate here',
|
||
required: ['schema_version', 'run', 'id', 'title', 'status', 'depends_on', 'findings', 'blocked_by', 'acceptance'],
|
||
properties: {
|
||
schema_version: { const: 2 }, run: str, id: str, title: str,
|
||
status: { enum: ['pending', 'ready', 'blocked_on_dependency', 'blocked_on_decision', 'in_progress', 'done'] },
|
||
depends_on: { ...strList, description: 'task ids, or a `pr` label another task in the file carries' },
|
||
findings: { ...strList, description: 'finding ids' }, blocked_by: { ...strList, description: 'decision ids' },
|
||
acceptance: { ...str, description: 'the command or check that proves the task' },
|
||
pr: str, item: str, tier: { type: 'integer' }, priority: str, component: str, files: strList,
|
||
},
|
||
},
|
||
timing: {
|
||
type: 'object', required: ['schema_version', 'run', 'phases'],
|
||
properties: {
|
||
schema_version: { const: 1 }, run: str, started_at: str, total_wall_s: num, session_kind: str,
|
||
phases: { type: 'array', items: { type: 'object', required: ['phase'], properties: { phase: str, started_at: str, ended_at: str, wall_s: num, outside_s: num, native_s: num, amend_s: num, note: str } } },
|
||
},
|
||
},
|
||
run: {
|
||
type: 'object', description: 'run.json manifest; `gstack-artifact validate run.json` is the completion check',
|
||
required: ['schema_version', 'run', 'status', 'required_phases', 'phases', 'artifacts'],
|
||
properties: {
|
||
schema_version: { const: 1 }, run: str,
|
||
status: { enum: ['complete', 'reviewed', 'gate_pending', 'amend_pending', 'incomplete', 'interrupted', 'refused', 'awaiting_result', 'running'] },
|
||
base: str, host: str, session_kind: str, required_phases: strList,
|
||
phases: { type: 'object', description: 'phase id → { snapshot, native, outside }', properties: {} },
|
||
counts: { type: 'object', description: 'decisions, decisions_pending, findings, tasks: each must equal the rows in its file' },
|
||
artifacts: { type: 'object', description: 'artifact key → { path (relative to the run directory), sha256 }' },
|
||
gate_rev: { type: 'integer' }, gate: { type: 'object' },
|
||
consumed_by: { type: 'array', items: { type: 'object', required: ['consumer', 'at'], properties: { consumer: str, at: str } } },
|
||
deviations: strList,
|
||
dispatches: { type: 'object', description: 'total and by_phase → { native, outside }: reviewer attempts from attempts.jsonl, failed and unknown included' },
|
||
est_input_tokens: { type: 'integer', minimum: 0, description: 'dispatched prompt bytes / 4: a relative estimate, never a bill' },
|
||
},
|
||
},
|
||
pregate: {
|
||
type: 'object', required: ['schema_version', 'tree', 'checks'],
|
||
properties: {
|
||
schema_version: { const: 1 }, run: str, tree: str, stage1_ms: num, stage2_ms: num,
|
||
checks: { type: 'array', items: { type: 'object', required: ['id', 'status'], properties: { id: str, status: { enum: ['pass', 'fail', 'warn', 'incomplete', 'requires-remote', 'not_installed'] }, ms: num, detail: str, stage: { enum: ['preflight', 'tests'] }, inputs: { type: 'object' }, fix: str, code: str, lines: strList, cause: { enum: ['diff', 'environment'] }, environment: { type: 'object', required: ['kind', 'evidence'], properties: { kind: str, evidence: str } }, proven: strList, from_run: { type: 'object', required: ['receipt', 'files'], properties: { receipt: str, files: strList } } } } },
|
||
head: str, base: str, generated_at: str, policy: str, policy_digest: str, policy_user_path: str, registry: str, verdict: str, scratch: str,
|
||
requires_remote: { type: 'array', items: { type: 'object', required: ['lane', 'platform', 'tests', 'receipt'], properties: { lane: str, platform: str, workflow: str, job: str, tests: strList, receipt: nullableStr, cleared_by: str, command: str,
|
||
substitution: { type: 'object', required: ['gated_tree', 'accepted_for', 'policy', 'files'], properties: { gated_tree: str, accepted_for: str, policy: str, files: { type: 'array', items: { type: 'object', required: ['path', 'rule'], properties: { path: str, rule: str } } } } } } } },
|
||
},
|
||
},
|
||
'ship-receipt': {
|
||
type: 'object', required: ['schema_version', 'head', 'base'],
|
||
properties: {
|
||
schema_version: { const: 1 }, pr: { type: 'integer' }, head: str, version: str, base: str, gated_tree: str,
|
||
gate: { type: 'object' }, ci: { type: 'object' }, spend_usd: { type: ['number', 'string'] }, predecessor: str,
|
||
ci_carried: { type: 'array', items: { type: 'object', required: ['lane', 'run', 'attempt', 'tree'], properties: { lane: str, run: { type: 'integer', minimum: 1 }, attempt: { type: 'integer', minimum: 1 }, tree: str } } },
|
||
session_kind: str, artifacts_consumed: { ...str, description: '<n>/<m>' }, run: str,
|
||
thread: str, tree: { enum: ['same-modulo-stamps', 'changed', 'not-compared'] }, gate_reuse: str, policy: str, queue_mode: str, preregistration: str, history: str,
|
||
pregate: { ...str, description: 'pass|warn|requires-remote cleared … @<tree> from pregate.json, current for this head' }, requires_remote: strList,
|
||
approval: { type: 'object', description: 'approval classes: { source, status: checked|unavailable, classes: [{ class, scope, effects, citation: verified|unverified, approved_by, date, via, binding, evidence }], report_only }' },
|
||
review_loop: { type: 'object', required: ['passes', 'blocking_found', 'deferred'], properties: { passes: { type: 'integer', minimum: 0 }, blocking_found: { type: 'integer', minimum: 0 }, deferred: { type: 'integer', minimum: 0 }, noted: { type: 'integer', minimum: 0 }, fixed: { type: 'integer', minimum: 0 }, scope: str, fix_checks: { type: 'integer', minimum: 0 }, rounds: { type: 'integer', minimum: 0 }, dispatches: { type: 'object', description: 'reviewer dispatches by role' }, est_input_tokens: { type: 'integer', minimum: 0 }, unrecorded: { type: 'integer', minimum: 0 }, ledgers_read: { type: 'integer', minimum: 0 } } },
|
||
carry: { type: 'object', required: ['from', 'reason', 'product_patch_id'], properties: { from: str, reason: str, product_patch_id: str, changed_paths: strList, merge_commits: { type: 'integer', minimum: 0 }, range_diff: str, covers: strList, reruns: strList, trees: strList, ancestry: { enum: ['linear', 'rewritten'] } } },
|
||
head_tree: str, product_patch_id: str, gstack_version: str,
|
||
base_red: { type: 'object', required: ['cases', 'files', 'base', 'lane', 'expires_at'], properties: { cases: strList, files: strList, base: str, lane: str, expires_at: str } },
|
||
review_depth: { type: 'object', required: ['depth', 'class', 'runtime_lines', 'rules', 'base', 'gate_scan'], properties: { depth: { enum: ['light', 'standard', 'full'] }, class: { enum: ['docs', 'tests', 'code', 'gate'] }, runtime_lines: { type: 'integer', minimum: 0 }, rules: str, base: str, registry: nullableStr, gate_scan: { type: 'object', required: ['tagged', 'head'], properties: { tagged: { type: ['integer', 'null'], minimum: 0 }, head: str } }, reasons: strList, risk: { type: 'array', items: { enum: ['money', 'auth', 'secret', 'security', 'data'] } } } },
|
||
review_targeted: { type: 'object', required: ['classes', 'rules', 'brief_sha', 'product_patch_id', 'status', 'findings', 'raised_by'], properties: { classes: { type: 'array', items: { enum: ['money', 'gate', 'auth', 'secret'] } }, rules: str, brief_sha: str, product_patch_id: str, status: { enum: ['done', 'failed', 'not-run'] }, findings: { type: 'integer', minimum: 0 }, raised_by: str } },
|
||
review_exclude: { type: 'object', required: ['source', 'patterns', 'applied', 'excluded', 'excluded_bytes', 'kept', 'gate_tagged', 'patterns_sha256', 'list_sha256', 'digest'], properties: { source: str, patterns: strList, applied: strList, excluded: { type: 'integer', minimum: 0 }, excluded_bytes: { type: 'integer', minimum: 0 }, kept: { type: 'integer', minimum: 0 }, gate_tagged: strList, patterns_sha256: str, list_sha256: str, digest: str } },
|
||
review_loop_exception: { type: 'object', required: ['accepted_by', 'date', 'via', 'reason', 'fix_check'], properties: { accepted_by: str, date: str, via: str, reason: str, fix_check: { type: 'object', required: ['head', 'findings'], properties: { head: str, findings: { type: 'integer', minimum: 0 }, files: strList } } } },
|
||
issues: { type: 'object', properties: { fixes: { type: 'array', items: { type: 'integer' } }, refs: { type: 'array', items: { type: 'integer' } }, flakes: { type: 'array', items: { type: 'object', required: ['issue', 'run'], properties: { issue: { type: 'integer' }, run: str } } }, related: { type: 'array', items: { type: 'object', required: ['pr', 'author', 'closes'], properties: { pr: { type: 'integer' }, repo: str, author: str, closes: { type: 'array', items: { type: 'integer' } }, supersedes: { type: 'boolean' }, credit: { type: 'boolean' } } } } } },
|
||
},
|
||
},
|
||
} as const satisfies Record<string, Schema>;
|
||
|
||
export type ArtifactName = keyof typeof ARTIFACT_SCHEMAS;
|
||
export const ARTIFACT_NAMES = Object.keys(ARTIFACT_SCHEMAS) as ArtifactName[];
|
||
export const SCHEMA_VERSIONS: Record<ArtifactName, number> = {
|
||
findings: 1, decisions: 1, tasks: 2, timing: 1, run: 1, pregate: 1, 'ship-receipt': 1,
|
||
};
|
||
|
||
/** JSON Schema document for one artifact, as `gstack-artifact schema <name>` prints it. */
|
||
export function jsonSchema(name: ArtifactName): Record<string, unknown> {
|
||
return {
|
||
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
||
$id: `https://github.com/garrytan/gstack/docs/unattended.md#${name}-v${SCHEMA_VERSIONS[name]}`,
|
||
title: `gstack ${name} v${SCHEMA_VERSIONS[name]}`,
|
||
...ARTIFACT_SCHEMAS[name],
|
||
};
|
||
}
|
||
|
||
export interface ValidationError { code: ResultCodeName; path: string; message: string }
|
||
|
||
function typeOf(v: unknown): string {
|
||
if (v === null) return 'null';
|
||
if (Array.isArray(v)) return 'array';
|
||
if (typeof v === 'number') return Number.isInteger(v) ? 'integer' : 'number';
|
||
return typeof v;
|
||
}
|
||
function typeOk(v: unknown, t: string | string[]): boolean {
|
||
const types = Array.isArray(t) ? t : [t];
|
||
const actual = typeOf(v);
|
||
return types.some(x => x === actual || (x === 'number' && actual === 'integer'));
|
||
}
|
||
|
||
/** Minimal schema check (type, required, enum, const, items, minimum); unknown fields are allowed. */
|
||
export function checkSchema(value: unknown, schema: Schema, at: string, out: ValidationError[], file = ''): void {
|
||
const where = file ? `${file}${at}` : at;
|
||
if (schema.const !== undefined && value !== schema.const) {
|
||
const code: ResultCodeName = at.endsWith('schema_version') ? 'ARTIFACT_UNSUPPORTED_VERSION' : 'ARTIFACT_SCHEMA';
|
||
out.push({ code, path: where, message: `expected ${JSON.stringify(schema.const)}, got ${JSON.stringify(value)}` });
|
||
return;
|
||
}
|
||
if (schema.enum && !schema.enum.includes(value)) {
|
||
out.push({ code: 'ARTIFACT_SCHEMA', path: where, message: `expected one of ${schema.enum.join('|')}, got ${JSON.stringify(value)}` });
|
||
return;
|
||
}
|
||
if (schema.type && !typeOk(value, schema.type)) {
|
||
out.push({ code: 'ARTIFACT_SCHEMA', path: where, message: `expected ${[schema.type].flat().join('|')}, got ${typeOf(value)}` });
|
||
return;
|
||
}
|
||
if (schema.minimum !== undefined && typeof value === 'number' && value < schema.minimum) {
|
||
out.push({ code: 'ARTIFACT_SCHEMA', path: where, message: `expected >= ${schema.minimum}, got ${value}` });
|
||
}
|
||
if (Array.isArray(value) && schema.items) value.forEach((item, i) => checkSchema(item, schema.items!, `${at}[${i}]`, out, file));
|
||
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
||
const obj = value as Record<string, unknown>;
|
||
for (const key of schema.required ?? []) {
|
||
if (!(key in obj)) out.push({ code: 'ARTIFACT_SCHEMA', path: `${where}.${key}`, message: 'required field missing' });
|
||
}
|
||
for (const [key, sub] of Object.entries(schema.properties ?? {})) {
|
||
if (key in obj) checkSchema(obj[key], sub, `${at}.${key}`, out, file);
|
||
}
|
||
}
|
||
}
|
||
|
||
/** Tolerant JSONL read: every line must be one object; a bad line (usually the tail) is an error, not a crash. */
|
||
export function readJsonl(file: string): { rows: Record<string, unknown>[]; errors: ValidationError[] } {
|
||
const rows: Record<string, unknown>[] = [];
|
||
const errors: ValidationError[] = [];
|
||
const text = fs.readFileSync(file, 'utf8');
|
||
const lines = text.split('\n');
|
||
if (lines[lines.length - 1] === '') lines.pop();
|
||
lines.forEach((line, i) => {
|
||
if (line.trim() === '') { errors.push({ code: 'ARTIFACT_MALFORMED_JSONL', path: `${file}:${i + 1}`, message: 'blank line inside a JSONL file' }); return; }
|
||
try {
|
||
const parsed = JSON.parse(line);
|
||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('not an object');
|
||
rows.push(parsed);
|
||
} catch (e: any) {
|
||
errors.push({ code: 'ARTIFACT_MALFORMED_JSONL', path: `${file}:${i + 1}`, message: i === lines.length - 1 ? `malformed tail: ${e.message}` : e.message });
|
||
}
|
||
});
|
||
return { rows, errors };
|
||
}
|
||
|
||
export function sha256File(file: string): string {
|
||
return createHash('sha256').update(fs.readFileSync(file)).digest('hex');
|
||
}
|
||
|
||
/** Resolve an artifact path inside the run dir; symlinks are followed and must stay inside. */
|
||
export function resolveInside(runDir: string, rel: string): { abs: string; error?: ValidationError } {
|
||
const root = fs.realpathSync(runDir);
|
||
if (path.isAbsolute(rel) || rel.split(/[\\/]/).includes('..')) {
|
||
return { abs: path.resolve(root, rel), error: { code: 'ARTIFACT_PATH_ESCAPE', path: rel, message: 'absolute path or `..` segment' } };
|
||
}
|
||
const abs = path.resolve(root, rel);
|
||
if (!fs.existsSync(abs)) return { abs, error: { code: 'ARTIFACT_MISSING', path: rel, message: 'file not found in the run directory' } };
|
||
const real = fs.realpathSync(abs);
|
||
if (real !== root && !real.startsWith(root + path.sep)) {
|
||
return { abs, error: { code: 'ARTIFACT_PATH_ESCAPE', path: rel, message: `resolves to ${real}, outside the run directory` } };
|
||
}
|
||
return { abs: real };
|
||
}
|
||
|
||
function checkIds(rows: Record<string, unknown>[], run: string, file: string, out: ValidationError[]): Set<string> {
|
||
const seen = new Set<string>();
|
||
rows.forEach((row, i) => {
|
||
const id = row.id;
|
||
if (typeof id !== 'string') return;
|
||
if (seen.has(id)) out.push({ code: 'ARTIFACT_DUPLICATE_ID', path: `${file}:${i + 1}`, message: `duplicate id ${id}` });
|
||
seen.add(id);
|
||
if (!id.startsWith(`${run}-`)) out.push({ code: 'ARTIFACT_UNBOUND_ID', path: `${file}:${i + 1}`, message: `id ${id} is not bound to run ${run}` });
|
||
if (row.run !== undefined && row.run !== run) out.push({ code: 'ARTIFACT_UNBOUND_ID', path: `${file}:${i + 1}`, message: `row.run ${JSON.stringify(row.run)} is not ${run}` });
|
||
});
|
||
return seen;
|
||
}
|
||
|
||
function findCycle(tasks: Record<string, unknown>[]): string[] | undefined {
|
||
const byPr = new Map<string, string[]>();
|
||
const ids = new Set<string>();
|
||
for (const t of tasks) {
|
||
if (typeof t.id !== 'string') continue;
|
||
ids.add(t.id);
|
||
if (typeof t.pr === 'string') byPr.set(t.pr, [...(byPr.get(t.pr) ?? []), t.id]);
|
||
}
|
||
const edges = new Map<string, string[]>();
|
||
for (const t of tasks) {
|
||
if (typeof t.id !== 'string') continue;
|
||
const deps = (Array.isArray(t.depends_on) ? t.depends_on : []).flatMap((d: unknown) =>
|
||
typeof d !== 'string' ? [] : ids.has(d) ? [d] : (byPr.get(d) ?? []).filter(x => x !== t.id));
|
||
edges.set(t.id, deps);
|
||
}
|
||
const state = new Map<string, 1 | 2>();
|
||
const stack: string[] = [];
|
||
const visit = (id: string): string[] | undefined => {
|
||
state.set(id, 1); stack.push(id);
|
||
for (const next of edges.get(id) ?? []) {
|
||
if (state.get(next) === 1) return [...stack.slice(stack.indexOf(next)), next];
|
||
if (!state.has(next)) { const found = visit(next); if (found) return found; }
|
||
}
|
||
stack.pop(); state.set(id, 2);
|
||
return undefined;
|
||
};
|
||
for (const id of edges.keys()) if (!state.has(id)) { const found = visit(id); if (found) return found; }
|
||
return undefined;
|
||
}
|
||
|
||
function checkPhases(m: RunManifest, out: ValidationError[]): void {
|
||
if (!TERMINAL_RUN.includes(m.status)) {
|
||
out.push({ code: 'ARTIFACT_RUN_INTERRUPTED', path: 'run.json.status', message: `status ${m.status} is not terminal` });
|
||
}
|
||
// A run stopped by its budget (status incomplete with a stop object) records the phases it never reached as such, not as missing.
|
||
const stopped = m.status === 'incomplete' && !!(m as { stop?: unknown }).stop;
|
||
for (const phase of m.required_phases ?? []) {
|
||
const p = m.phases?.[phase];
|
||
if (!p) { if (!stopped) out.push({ code: 'ARTIFACT_REVIEWER_MISSING', path: `run.json.phases.${phase}`, message: 'required phase absent' }); continue; }
|
||
for (const voice of ['native', 'outside'] as const) {
|
||
const v = p[voice];
|
||
if (!v || typeof v !== 'object') { out.push({ code: 'ARTIFACT_REVIEWER_MISSING', path: `run.json.phases.${phase}.${voice}`, message: 'voice has no outcome' }); continue; }
|
||
checkSchema(v, voiceOutcome, `.phases.${phase}.${voice}`, out, 'run.json');
|
||
if (['pending', 'running'].includes(String(v.status))) {
|
||
if (!stopped) out.push({ code: 'ARTIFACT_RUN_INTERRUPTED', path: `run.json.phases.${phase}.${voice}.status`, message: `voice is still ${v.status}` });
|
||
} else if (!TERMINAL_VOICE.includes(String(v.status))) {
|
||
continue;
|
||
}
|
||
if (v.status === 'completed') {
|
||
const key = typeof v.output === 'string' ? v.output : `${phase}-${voice}.md`;
|
||
if (!m.artifacts?.[key]) out.push({ code: 'ARTIFACT_REVIEWER_MISSING', path: `run.json.phases.${phase}.${voice}`, message: `completed voice has no bound output artifact ${key}` });
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
function checkCounts(m: RunManifest, rows: Partial<Record<ArtifactName, Record<string, unknown>[]>>, out: ValidationError[]): void {
|
||
const counts = m.counts ?? {};
|
||
const expect = (key: string, actual: number | undefined) => {
|
||
if (actual === undefined || typeof counts[key] !== 'number') return;
|
||
if (counts[key] !== actual) out.push({ code: 'ARTIFACT_COUNT_MISMATCH', path: `run.json.counts.${key}`, message: `manifest says ${counts[key]}, file has ${actual}` });
|
||
};
|
||
expect('decisions', rows.decisions?.length);
|
||
expect('decisions_pending', rows.decisions?.filter(r => r.status === 'pending').length);
|
||
expect('findings', rows.findings?.length);
|
||
expect('tasks', rows.tasks?.length);
|
||
}
|
||
|
||
function checkRefs(rows: Partial<Record<ArtifactName, Record<string, unknown>[]>>, ids: Partial<Record<ArtifactName, Set<string>>>, out: ValidationError[]): void {
|
||
const prs = new Set((rows.tasks ?? []).map(t => t.pr).filter((p): p is string => typeof p === 'string'));
|
||
const need = (list: unknown, pool: Set<string> | undefined, where: string, what: string, extra?: Set<string>) => {
|
||
if (!Array.isArray(list) || !pool) return;
|
||
for (const ref of list) {
|
||
if (typeof ref === 'string' && (pool.has(ref) || extra?.has(ref))) continue;
|
||
out.push({ code: 'ARTIFACT_DANGLING_REF', path: where, message: `${what} ${JSON.stringify(ref)} is not defined in this run` });
|
||
}
|
||
};
|
||
(rows.tasks ?? []).forEach((t, i) => {
|
||
need(t.findings, ids.findings, `tasks.jsonl:${i + 1}.findings`, 'finding');
|
||
need(t.blocked_by, ids.decisions, `tasks.jsonl:${i + 1}.blocked_by`, 'decision');
|
||
need(t.depends_on, ids.tasks, `tasks.jsonl:${i + 1}.depends_on`, 'task', prs);
|
||
});
|
||
const cycle = rows.tasks ? findCycle(rows.tasks) : undefined;
|
||
if (cycle) out.push({ code: 'ARTIFACT_DEPENDENCY_CYCLE', path: 'tasks.jsonl', message: cycle.join(' -> ') });
|
||
}
|
||
|
||
export interface ValidationResult { valid: boolean; errors: ValidationError[]; manifest?: RunManifest; runDir: string; attested?: number }
|
||
|
||
/**
|
||
* The deterministic completion check a parent runs first. Fails on: invalid
|
||
* JSON, an unsupported version, a non-terminal run or voice, a required
|
||
* phase or voice without a terminal outcome or bound output, a listed artifact
|
||
* that is missing, stale (hash) or escapes the run directory, malformed JSONL,
|
||
* schema violations, duplicate or unbound ids, dangling references, task
|
||
* dependency cycles, count mismatches, and an unattended run whose
|
||
* review-record.md lacks the GUARD_NOT_INSTALLED line.
|
||
*/
|
||
export function validateRun(runJson: string): ValidationResult {
|
||
const runDir = path.dirname(path.resolve(runJson));
|
||
const errors: ValidationError[] = [];
|
||
let manifest: RunManifest;
|
||
try {
|
||
manifest = JSON.parse(fs.readFileSync(runJson, 'utf8'));
|
||
} catch (e: any) {
|
||
return { valid: false, runDir, errors: [{ code: 'ARTIFACT_INVALID_JSON', path: runJson, message: e.message }] };
|
||
}
|
||
checkSchema(manifest, ARTIFACT_SCHEMAS.run, '', errors, 'run.json');
|
||
if (errors.length) return { valid: false, errors, manifest, runDir };
|
||
checkPhases(manifest, errors);
|
||
|
||
const rows: Partial<Record<ArtifactName, Record<string, unknown>[]>> = {};
|
||
const ids: Partial<Record<ArtifactName, Set<string>>> = {};
|
||
const seenAbs = new Set<string>();
|
||
for (const [key, entry] of Object.entries(manifest.artifacts)) {
|
||
if (!entry || typeof entry.path !== 'string' || typeof entry.sha256 !== 'string') {
|
||
errors.push({ code: 'ARTIFACT_SCHEMA', path: `run.json.artifacts.${key}`, message: 'expected { path, sha256 }' });
|
||
continue;
|
||
}
|
||
const { abs, error } = resolveInside(runDir, entry.path);
|
||
if (error) { errors.push({ ...error, path: `run.json.artifacts.${key} (${entry.path})` }); continue; }
|
||
seenAbs.add(abs);
|
||
const actual = sha256File(abs);
|
||
if (actual !== entry.sha256) errors.push({ code: 'ARTIFACT_STALE', path: entry.path, message: `sha256 ${actual.slice(0, 12)}… differs from the manifest’s ${entry.sha256.slice(0, 12)}…` });
|
||
const name = (['decisions', 'findings', 'tasks'] as const).find(n => key === `${n}.jsonl`);
|
||
if (name) {
|
||
const read = readJsonl(abs);
|
||
errors.push(...read.errors);
|
||
read.rows.forEach((row, i) => checkSchema(row, ARTIFACT_SCHEMAS[name], `:${i + 1}`, errors, entry.path));
|
||
ids[name] = checkIds(read.rows, manifest.run, entry.path, errors);
|
||
rows[name] = read.rows;
|
||
} else if (key === 'timing.json') {
|
||
try { checkSchema(JSON.parse(fs.readFileSync(abs, 'utf8')), ARTIFACT_SCHEMAS.timing, '', errors, entry.path); }
|
||
catch (e: any) { errors.push({ code: 'ARTIFACT_INVALID_JSON', path: entry.path, message: e.message }); }
|
||
}
|
||
}
|
||
checkCounts(manifest, rows, errors);
|
||
checkRefs(rows, ids, errors);
|
||
checkGateSettled(manifest, rows.decisions, errors);
|
||
if (manifest.session_kind === 'unattended') {
|
||
const record = manifest.artifacts['review-record.md'];
|
||
const abs = record ? resolveInside(runDir, record.path).abs : '';
|
||
const text = abs && fs.existsSync(abs) ? fs.readFileSync(abs, 'utf8') : '';
|
||
if (!text.includes('GUARD_NOT_INSTALLED')) errors.push({ code: 'ARTIFACT_GUARD_LINE_MISSING', path: 'review-record.md', message: 'unattended run without the guard line' });
|
||
}
|
||
return { valid: errors.length === 0, errors, manifest, runDir, attested: countAttested(manifest) };
|
||
}
|
||
|
||
/** Voices bound from a hand-run record on the parent's attestation rather than a reviewer receipt. */
|
||
function countAttested(m: RunManifest): number {
|
||
return (m.required_phases ?? []).reduce((n, p) => n + (['native', 'outside'] as const).filter(v => m.phases?.[p]?.[v]?.receipt === 'attested').length, 0);
|
||
}
|
||
|
||
/** A complete run has no pending approval at its last gate revision and no pending plan amendment. */
|
||
function checkGateSettled(m: RunManifest, decisions: Record<string, unknown>[] | undefined, out: ValidationError[]): void {
|
||
if (m.status !== 'complete') return;
|
||
const last = Math.max(0, ...(decisions ?? []).map(d => Number(d.gate_rev) || 0));
|
||
for (const d of decisions ?? []) {
|
||
if (d.gate_rev === last && d.kind === 'approval' && d.status === 'pending') out.push({ code: 'ARTIFACT_GATE_UNSETTLED', path: 'decisions.jsonl', message: `approval ${String(d.label ?? d.id)} at gate_rev ${last} is still pending` });
|
||
}
|
||
for (const a of Array.isArray(m.amendments) ? m.amendments as Array<Record<string, unknown>> : []) {
|
||
if (a.status === 'pending') out.push({ code: 'ARTIFACT_GATE_UNSETTLED', path: 'run.json.amendments', message: `plan amendment ${String(a.decision)} is still pending` });
|
||
}
|
||
for (const d of Array.isArray(m.capped_deltas) ? m.capped_deltas as Array<Record<string, unknown>> : []) {
|
||
if (d.status === 'pending') out.push({ code: 'ARTIFACT_GATE_UNSETTLED', path: 'run.json.capped_deltas', message: `the plan change after the ${String(d.phase)} round cap (revise v${String(d.k)}) was never put to the owner` });
|
||
}
|
||
}
|
||
|
||
/** Validate a standalone artifact file (pregate.json, a ship receipt, timing.json, or a JSONL of rows) against its schema. */
|
||
export function validateFile(file: string, name: ArtifactName, run?: string): ValidationError[] {
|
||
const errors: ValidationError[] = [];
|
||
if (file.endsWith('.jsonl')) {
|
||
const read = readJsonl(file);
|
||
errors.push(...read.errors);
|
||
read.rows.forEach((row, i) => checkSchema(row, ARTIFACT_SCHEMAS[name], `:${i + 1}`, errors, file));
|
||
if (run) checkIds(read.rows, run, file, errors);
|
||
return errors;
|
||
}
|
||
try { checkSchema(JSON.parse(fs.readFileSync(file, 'utf8')), ARTIFACT_SCHEMAS[name], '', errors, file); }
|
||
catch (e: any) { errors.push({ code: 'ARTIFACT_INVALID_JSON', path: file, message: e.message }); }
|
||
return errors;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Derivations shared with the review log (B7) and the final report (B12)
|
||
// ---------------------------------------------------------------------------
|
||
export interface DerivedReviewStatus {
|
||
status: 'clean' | 'issues_open'; unresolved: number; issues_found: number; critical_gaps: number;
|
||
findings_total: number; findings_open: number; findings_resolved: number;
|
||
}
|
||
export function isOpenFinding(row: Record<string, unknown>): boolean {
|
||
if (row.action === 'open') return true;
|
||
if (typeof row.disposition === 'string') return !RESOLVED.includes(row.disposition);
|
||
if (typeof row.status === 'string') return !['resolved', 'fixed', 'closed'].includes(row.status);
|
||
return isOpenByAction(row);
|
||
}
|
||
/** A finding's `action` alone: fixed and auto-fixed resolve it; deferred and noted resolve only one marked `blocking: false` that is not CRITICAL (review policy's floor). */
|
||
export function isOpenByAction(row: Record<string, unknown>): boolean {
|
||
if (row.action === 'fixed' || row.action === 'auto-fixed') return false;
|
||
if (row.action === 'deferred' || row.action === 'noted') return row.blocking !== false || isCriticalFinding(row);
|
||
return true;
|
||
}
|
||
export function isCriticalFinding(row: Record<string, unknown>): boolean {
|
||
return typeof row.severity === 'string' && row.severity.toLowerCase() === 'critical';
|
||
}
|
||
/** `clean` only when no finding is open; counts are over the whole file. */
|
||
export function deriveReviewStatus(rows: Record<string, unknown>[]): DerivedReviewStatus {
|
||
const open = rows.filter(isOpenFinding);
|
||
return {
|
||
status: open.length === 0 ? 'clean' : 'issues_open',
|
||
unresolved: open.length, issues_found: rows.length, critical_gaps: open.filter(isCriticalFinding).length,
|
||
findings_total: rows.length, findings_open: open.length, findings_resolved: rows.length - open.length,
|
||
};
|
||
}
|
||
|
||
const URGENT_HEADING = '## URGENT, outside this plan';
|
||
/** Fixed first block of the final report and plan.md: security or data-loss findings the plan does not own. */
|
||
export function renderUrgentBlock(rows: Record<string, unknown>[]): string {
|
||
const urgent = rows.filter(r => r.urgent === true || r.outside_plan === true);
|
||
if (urgent.length === 0) return `${URGENT_HEADING}\n\nNone.\n`;
|
||
const lines = urgent.map(r => {
|
||
const owner = typeof r.suggested_owner === 'string' ? r.suggested_owner : 'owner: unassigned';
|
||
const sev = typeof r.severity === 'string' ? r.severity : 'unrated';
|
||
return `- ${r.id ?? '(no id)'} [${sev}] ${r.title ?? ''} — ${owner}`;
|
||
});
|
||
return `${URGENT_HEADING}\n\n${lines.join('\n')}\n`;
|
||
}
|
||
export function urgentBlockPrecedes(text: string, summaryMarker = '### Plan Summary'): boolean {
|
||
const u = text.indexOf(URGENT_HEADING);
|
||
const s = text.indexOf(summaryMarker);
|
||
return u >= 0 && (s < 0 || u < s);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// ack: the idempotent consumption report behind P7
|
||
// ---------------------------------------------------------------------------
|
||
export interface AckResult { consumed_by: Array<{ consumer: string; at: string }>; added: boolean; analytics: 'appended' | 'skipped_ephemeral' | 'already_acked' }
|
||
export function ackRun(runJson: string, consumer: string, opts: { analyticsPath: string; ephemeral: boolean; now?: Date }): AckResult {
|
||
if (!/^[A-Za-z0-9_.:@/-]{1,200}$/.test(consumer)) throw new Error(`consumer id must match [A-Za-z0-9_.:@/-]{1,200}, got ${JSON.stringify(consumer)}`);
|
||
// Exactly once across concurrent consumers: the read-modify-write runs under a file lock.
|
||
return withFileLock(runJson, () => ackLocked(runJson, consumer, opts));
|
||
}
|
||
function ackLocked(runJson: string, consumer: string, opts: { analyticsPath: string; ephemeral: boolean; now?: Date }): AckResult {
|
||
const manifest: RunManifest = JSON.parse(fs.readFileSync(runJson, 'utf8'));
|
||
const list = Array.isArray(manifest.consumed_by) ? manifest.consumed_by : [];
|
||
if (list.some(c => c.consumer === consumer)) return { consumed_by: list, added: false, analytics: 'already_acked' };
|
||
const at = (opts.now ?? new Date()).toISOString();
|
||
manifest.consumed_by = [...list, { consumer, at }];
|
||
const tmp = `${runJson}.${process.pid}.tmp`;
|
||
fs.writeFileSync(tmp, JSON.stringify(manifest, null, 2) + '\n');
|
||
fs.renameSync(tmp, runJson);
|
||
if (opts.ephemeral) return { consumed_by: manifest.consumed_by, added: true, analytics: 'skipped_ephemeral' };
|
||
fs.mkdirSync(path.dirname(opts.analyticsPath), { recursive: true });
|
||
fs.appendFileSync(opts.analyticsPath, JSON.stringify({ event: 'ack', run: manifest.run, consumer, ts: at, session_kind: manifest.session_kind ?? null }) + '\n');
|
||
return { consumed_by: manifest.consumed_by, added: true, analytics: 'appended' };
|
||
}
|