rungate 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hooks/ActionVerificationGate.hook.ts +96 -0
- package/hooks/AgentBriefGuard.hook.ts +38 -0
- package/hooks/AgentVerdictCapture.hook.ts +87 -0
- package/hooks/AutoVerifyGate.hook.ts +79 -0
- package/hooks/BashToolGuard.hook.ts +65 -0
- package/hooks/BashWriteGuard.hook.ts +60 -0
- package/hooks/CommitEnforcement.hook.ts +130 -0
- package/hooks/DocHygiene.hook.ts +148 -0
- package/hooks/GateEnforcement.hook.ts +58 -0
- package/hooks/GateResultProtection.hook.ts +69 -0
- package/hooks/HarnessAFKGate.hook.ts +85 -0
- package/hooks/HookCurrency.hook.ts +99 -0
- package/hooks/IssueCloseGuard.hook.ts +149 -0
- package/hooks/MergeGuard.hook.ts +58 -0
- package/hooks/PostCompact.hook.ts +60 -0
- package/hooks/ProjectStateSync.hook.ts +103 -0
- package/hooks/SkillGuard.hook.ts +69 -0
- package/hooks/SpecConformityTrigger.hook.ts +43 -0
- package/hooks/SpecSCGuard.hook.ts +123 -0
- package/hooks/StaleTTLCleanup.hook.ts +141 -0
- package/hooks/TaskCompleted.hook.ts +51 -0
- package/hooks/TeamInit.hook.ts +85 -0
- package/hooks/TestSuiteGuard.hook.ts +70 -0
- package/hooks/TestSuiteRelease.hook.ts +85 -0
- package/hooks/TopLevelCdGuard.hook.ts +108 -0
- package/hooks/VerifyPhaseLock.hook.ts +84 -0
- package/hooks/WorkflowStateGuard.hook.ts +40 -0
- package/hooks/lib/agent-config.json +9 -0
- package/hooks/lib/agentDetection.ts +201 -0
- package/hooks/lib/comment-template.ts +86 -0
- package/hooks/lib/findWorkflow.ts +101 -0
- package/hooks/lib/parseStdin.ts +37 -0
- package/hooks/lib/paths.ts +111 -0
- package/hooks/lib/utils.ts +338 -0
- package/lib/affected-tests.ts +427 -0
- package/lib/agent-audit.ts +2 -3
- package/lib/agent-worktree.ts +287 -0
- package/lib/agents-md-sections.ts +81 -16
- package/lib/bash-write-guard.ts +373 -0
- package/lib/behavioral-cache.ts +150 -2
- package/lib/branch-cleanup.ts +279 -7
- package/lib/branch-protection.ts +790 -0
- package/lib/brief-ac-alignment.ts +104 -0
- package/lib/brief-references.ts +205 -0
- package/lib/bun-version.ts +93 -14
- package/lib/check-remedy.ts +140 -0
- package/lib/ci-checkout.ts +17 -0
- package/lib/ci-history-depth.ts +159 -0
- package/lib/ci-setup-step.ts +96 -0
- package/lib/claude-code-action.ts +85 -0
- package/lib/claude-tool-vocabulary.ts +188 -0
- package/lib/cli-commands.ts +15 -0
- package/lib/commit-sha.ts +42 -0
- package/lib/commit-subject.ts +90 -0
- package/lib/compliance-report.ts +377 -45
- package/lib/compliance.ts +191 -17
- package/lib/config-loader.ts +39 -1
- package/lib/config-trackable.ts +400 -0
- package/lib/conformity.ts +1087 -100
- package/lib/containment.ts +238 -0
- package/lib/content-hash.ts +24 -0
- package/lib/create-sc.ts +249 -16
- package/lib/dev-liveness.ts +180 -0
- package/lib/discovery-branch-points.ts +432 -0
- package/lib/discovery-stability.ts +481 -0
- package/lib/distribution-currency.ts +236 -0
- package/lib/doc-routing.ts +157 -0
- package/lib/evidence-prevalidator.ts +224 -2
- package/lib/evidence-satisfiability.ts +534 -12
- package/lib/failure-intake.ts +323 -0
- package/lib/fallow-availability.ts +157 -0
- package/lib/fs-write-capabilities.ts +232 -0
- package/lib/gate-enforcement.ts +222 -28
- package/lib/gate-names.ts +72 -0
- package/lib/gate-result-protection.ts +427 -0
- package/lib/gate-run-split.ts +147 -0
- package/lib/generators/agents-md.ts +140 -32
- package/lib/git-isolation.ts +140 -0
- package/lib/github.ts +15 -0
- package/lib/google-auth-action.ts +93 -0
- package/lib/harness-project.ts +48 -0
- package/lib/harness-provenance.ts +255 -0
- package/lib/hook-evidence.ts +235 -0
- package/lib/hook-scopes.ts +386 -0
- package/lib/hook-target-tree.ts +398 -0
- package/lib/hook-trees.ts +447 -0
- package/lib/intake-sources.ts +380 -0
- package/lib/js-top-level-code.ts +78 -10
- package/lib/junit-checks.ts +52 -0
- package/lib/late-env-spawn.ts +331 -0
- package/lib/launch-base.ts +376 -0
- package/lib/line-citation.ts +372 -0
- package/lib/orphan-reaper.ts +468 -0
- package/lib/parity-binding.ts +205 -0
- package/lib/paths.ts +96 -3
- package/lib/per-test-timeouts.ts +219 -0
- package/lib/phase2-decomposition.ts +1 -1
- package/lib/plan-coverage.ts +151 -0
- package/lib/prior-branch.ts +201 -8
- package/lib/process-table.ts +146 -0
- package/lib/project-state-derivation.ts +219 -0
- package/lib/project-state-root.ts +258 -0
- package/lib/published-artifact.ts +669 -0
- package/lib/readdir-sorted.ts +45 -0
- package/lib/review-execution-reading.ts +206 -0
- package/lib/review-tool-agreement.ts +433 -0
- package/lib/rule-promotion.ts +234 -0
- package/lib/rule-retirement.ts +525 -0
- package/lib/run-branch.ts +612 -0
- package/lib/rungate-schema.ts +19 -0
- package/lib/runner-home.ts +274 -0
- package/lib/sc-id-cross-tree.ts +103 -15
- package/lib/sc-ids.ts +121 -13
- package/lib/sc-reservations.ts +289 -0
- package/lib/scaffold/ai-review-findings.ts +409 -0
- package/lib/scaffold/ai-review-step.ts +5406 -64
- package/lib/scaffold/defaults.ts +82 -1
- package/lib/scaffold/managed-workflow.ts +587 -192
- package/lib/scaffold/owned-files.ts +152 -45
- package/lib/scaffold/pre-push-hook.ts +49 -5
- package/lib/scaffold/steps.ts +1836 -275
- package/lib/scaffold/workflow-census.ts +217 -0
- package/lib/scaffold/workflow-drift.ts +217 -0
- package/lib/scaffold/write-steps.ts +4 -0
- package/lib/scan-budget.ts +247 -0
- package/lib/scanner.ts +43 -51
- package/lib/scratch-roots.ts +50 -5
- package/lib/screenshot-discovery.ts +121 -0
- package/lib/screenshot-upload.ts +41 -1
- package/lib/secret-patterns.ts +511 -8
- package/lib/security-verdict.ts +1 -1
- package/lib/shell-path-interpolation.ts +594 -0
- package/lib/ship-mode.ts +219 -0
- package/lib/source-slice.ts +294 -0
- package/lib/stale-cleanup.ts +37 -3
- package/lib/suite-flake.ts +278 -0
- package/lib/suite-measurement.ts +23 -4
- package/lib/suite-run-reading.ts +241 -1
- package/lib/suite-timeout-margin.ts +193 -0
- package/lib/test-case-bodies.ts +883 -0
- package/lib/test-case-deletion.ts +653 -0
- package/lib/test-census.ts +97 -29
- package/lib/test-command-positions.ts +274 -0
- package/lib/test-suite-lock.ts +159 -16
- package/lib/text-dependents.ts +875 -0
- package/lib/tool-probe.ts +31 -9
- package/lib/top-level-cd.ts +393 -0
- package/lib/tree-entries.ts +89 -0
- package/lib/ts-ast.ts +200 -0
- package/lib/walk-test-files.ts +97 -0
- package/lib/workflow-actions.ts +263 -0
- package/lib/workflow-security.ts +29 -0
- package/lib/workflow-taint.ts +732 -0
- package/lib/worktree-cleanup.ts +65 -38
- package/lib/worktree-collect.ts +177 -5
- package/lib/worktree-repo.ts +68 -3
- package/package.json +4 -1
- package/scripts/ai-review-findings.ts +45 -0
- package/scripts/check-installed-hook-currency.ts +825 -0
- package/scripts/create-sc.ts +21 -1
- package/scripts/extract-constraints.ts +107 -7
- package/scripts/generate-code-map.ts +23 -30
- package/scripts/scaffold-project.ts +8 -1
- package/scripts/sync-sc-status.ts +77 -16
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SC id reservations, as a file in the repository rather than in a prompt (#725).
|
|
3
|
+
*
|
|
4
|
+
* ## Why a file, when the allocator already reads every worktree and every ref
|
|
5
|
+
*
|
|
6
|
+
* `readScNamespace` (#503) reads sibling worktrees' `specs/` and every local
|
|
7
|
+
* and remote-tracking ref. That is a reading of ids that have already been
|
|
8
|
+
* **written down somewhere this machine can see**. It cannot see an id a
|
|
9
|
+
* session has decided to use and not yet typed, and it cannot see anything on
|
|
10
|
+
* another clone at all.
|
|
11
|
+
*
|
|
12
|
+
* On 2026-10-19 that produced four collisions in one day. The first was eight
|
|
13
|
+
* PRs merging in sequence, two of which both claimed SC-2161 and SC-2162. The
|
|
14
|
+
* other three are the ones that matter for this module, because **in none of
|
|
15
|
+
* them was the allocator ever run**:
|
|
16
|
+
*
|
|
17
|
+
* | Instance | How the id was consumed |
|
|
18
|
+
* |---|---|
|
|
19
|
+
* | two | a renumber during the repair of instance one, picked by reading the highest id in the tree by hand |
|
|
20
|
+
* | three | two branches each allocating correctly against a view that excluded the other, found only by sweeping branch diffs |
|
|
21
|
+
* | four | ids hand-written into a new test file, caught by `sc-fixture-namespace` |
|
|
22
|
+
*
|
|
23
|
+
* So no change to what the allocator *returns* could have prevented three of
|
|
24
|
+
* the four: nothing consulted it. The class is "an id consumed without going
|
|
25
|
+
* through the allocator", and the only thing that covers that class is a
|
|
26
|
+
* declaration someone makes **before** consuming the ids, in a place every
|
|
27
|
+
* other session reads. That is this file.
|
|
28
|
+
*
|
|
29
|
+
* The convention already existed — five sessions were handed disjoint blocks
|
|
30
|
+
* in their briefs — and it worked. It lived entirely in prompts, so nothing in
|
|
31
|
+
* the tree recorded which blocks were reserved or by whom, and it survived
|
|
32
|
+
* exactly as long as everyone remembered. Instance two was made by the person
|
|
33
|
+
* handing the blocks out, outside any block, because a repair did not feel
|
|
34
|
+
* like an allocation. A renumber is an allocation.
|
|
35
|
+
*
|
|
36
|
+
* ## `{}` and `{"reservations": []}` are not the same state
|
|
37
|
+
*
|
|
38
|
+
* This is `.claude/rules/config-access.md`'s *Silent Default Substitution* in
|
|
39
|
+
* the place it does the most damage: **zero known reservations is not "no
|
|
40
|
+
* conflict", it is very often an unread store.** A `JSON.parse` that succeeds
|
|
41
|
+
* and yields an object with no `reservations` key gives an empty block list,
|
|
42
|
+
* which is byte-identical to a store that genuinely reserves nothing — and the
|
|
43
|
+
* allocator then allocates as if nothing were reserved, which is the pre-#725
|
|
44
|
+
* behaviour wearing a fix.
|
|
45
|
+
*
|
|
46
|
+
* So `RESERVATIONS_CONTROL_KEY` is the control string
|
|
47
|
+
* `.claude/rules/checks-must-be-able-to-fail.md` asks for: a string that MUST
|
|
48
|
+
* be present if the store holds anything at all. Absent, the reading is
|
|
49
|
+
* `unreadable`, not empty, and `allocateScNumber` refuses rather than
|
|
50
|
+
* returning a number.
|
|
51
|
+
*
|
|
52
|
+
* Three states, three different facts:
|
|
53
|
+
*
|
|
54
|
+
* | State | Means | Allocator |
|
|
55
|
+
* |---|---|---|
|
|
56
|
+
* | `absent` | no store in this tree — the normal state for a scaffolded consumer | allocates, with a `skipped` leg and a named `unmeasured` entry |
|
|
57
|
+
* | `unreadable` | a store is there and this process could not read it | **refuses** |
|
|
58
|
+
* | `read` | the store parsed and declared its control key | allocates, treating every block as occupied |
|
|
59
|
+
*
|
|
60
|
+
* `absent` does not refuse on purpose. Every consumer project this harness
|
|
61
|
+
* scaffolds has no store, and a refusal they cannot act on is the fastest way
|
|
62
|
+
* to build a gate everyone learns to click past.
|
|
63
|
+
*/
|
|
64
|
+
import { execFileSync } from "child_process";
|
|
65
|
+
import { existsSync, readFileSync } from "fs";
|
|
66
|
+
import { join } from "path";
|
|
67
|
+
|
|
68
|
+
import { FIXTURE_SC_FLOOR } from "./sc-ids";
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Where the store lives, relative to the project root.
|
|
72
|
+
*
|
|
73
|
+
* `specs/` because SC ids are a property of the spec namespace, and because
|
|
74
|
+
* the cross-tree reader already points every session at that directory. It is
|
|
75
|
+
* JSON rather than Markdown so that a reserved block is a pair of integers
|
|
76
|
+
* and never the string `SC-<N>` — a store spelling ids literally would be
|
|
77
|
+
* picked up by `scIdsSpelledInSource` and by `spec-sc-ids-unique`, and a
|
|
78
|
+
* reservation would then read as a declaration.
|
|
79
|
+
*/
|
|
80
|
+
export const SC_RESERVATIONS_PATH = "specs/sc-reservations.json";
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The key that must be present if the store holds anything.
|
|
84
|
+
*
|
|
85
|
+
* One binding, because a second place that decides what a readable store looks
|
|
86
|
+
* like would survive any mutation of the first
|
|
87
|
+
* (`.claude/rules/checks-must-be-able-to-fail.md`, fourth shape).
|
|
88
|
+
*/
|
|
89
|
+
export const RESERVATIONS_CONTROL_KEY = "reservations";
|
|
90
|
+
|
|
91
|
+
/** The message prefix every refusal out of this module and its allocator uses. */
|
|
92
|
+
export const RESERVATION_REFUSAL = "SC reservation store is present and unreadable";
|
|
93
|
+
|
|
94
|
+
/** One declared block. `from` and `to` are inclusive. */
|
|
95
|
+
export interface ScReservation {
|
|
96
|
+
/** Branch name or issue ref. The thing another session can look you up by. */
|
|
97
|
+
owner: string;
|
|
98
|
+
from: number;
|
|
99
|
+
to: number;
|
|
100
|
+
note?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type ScReservationStatus = "read" | "absent" | "unreadable";
|
|
104
|
+
|
|
105
|
+
export interface ScReservationReading {
|
|
106
|
+
status: ScReservationStatus;
|
|
107
|
+
/** Absolute path the reading was taken from, whether or not it existed. */
|
|
108
|
+
path: string;
|
|
109
|
+
/** Empty under every status except a `read` of a store that declares blocks. */
|
|
110
|
+
blocks: ScReservation[];
|
|
111
|
+
/** Why it could not be read, or why the store is absent. Absent on a clean read. */
|
|
112
|
+
reason?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A positive integer and nothing else — `1.5`, `NaN` and `"7"` are all refused. */
|
|
116
|
+
function isCount(v: unknown): v is number {
|
|
117
|
+
return typeof v === "number" && Number.isSafeInteger(v) && v >= 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Validate one entry, returning the reason it is unusable or `null`.
|
|
122
|
+
*
|
|
123
|
+
* A block at or above the fixture floor is refused rather than clamped: the
|
|
124
|
+
* 9000s exist so a fixture can spell an id that no criterion will ever take,
|
|
125
|
+
* and a reservation up there is a declaration the allocator structurally
|
|
126
|
+
* cannot honour (`nextSCNumber` drops fixture ids on purpose). Silently
|
|
127
|
+
* ignoring it would make the store say one thing and the allocator do another.
|
|
128
|
+
*/
|
|
129
|
+
function blockFault(raw: unknown, index: number): string | null {
|
|
130
|
+
const at = `${RESERVATIONS_CONTROL_KEY}[${index}]`;
|
|
131
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) return `${at} is not an object`;
|
|
132
|
+
const b = raw as Record<string, unknown>;
|
|
133
|
+
if (typeof b.owner !== "string" || b.owner.trim() === "") return `${at} has no owner`;
|
|
134
|
+
if (!isCount(b.from)) return `${at} (${String(b.owner)}) has no usable "from"`;
|
|
135
|
+
if (!isCount(b.to)) return `${at} (${String(b.owner)}) has no usable "to"`;
|
|
136
|
+
if ((b.to as number) < (b.from as number)) return `${at} (${String(b.owner)}) runs backwards`;
|
|
137
|
+
if ((b.to as number) >= FIXTURE_SC_FLOOR) {
|
|
138
|
+
return `${at} (${String(b.owner)}) reaches into the fixture range, which the allocator cannot honour`;
|
|
139
|
+
}
|
|
140
|
+
if (b.note !== undefined && typeof b.note !== "string") return `${at} (${String(b.owner)}) has a non-string note`;
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Two blocks that overlap are a collision the store itself declares.
|
|
146
|
+
*
|
|
147
|
+
* This is the one failure mode a reservation file adds that a prompt did not
|
|
148
|
+
* have, so it is refused rather than merged: a store that hands the same id to
|
|
149
|
+
* two owners is strictly worse than no store, because the sessions reading it
|
|
150
|
+
* both believe they are separated.
|
|
151
|
+
*/
|
|
152
|
+
function overlapFault(blocks: ScReservation[]): string | null {
|
|
153
|
+
const sorted = [...blocks].sort((a, b) => a.from - b.from);
|
|
154
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
155
|
+
const prev = sorted[i - 1];
|
|
156
|
+
const cur = sorted[i];
|
|
157
|
+
if (cur.from <= prev.to) {
|
|
158
|
+
return `${prev.owner} (${prev.from}-${prev.to}) and ${cur.owner} (${cur.from}-${cur.to}) overlap`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Read the store, naming which of the three states it is in.
|
|
166
|
+
*
|
|
167
|
+
* Never throws. The refusal belongs to the allocator, which is the thing with
|
|
168
|
+
* a number to withhold; a reader that threw would make `absent` and
|
|
169
|
+
* `unreadable` reach the caller the same way.
|
|
170
|
+
*/
|
|
171
|
+
export function readScReservations(root: string): ScReservationReading {
|
|
172
|
+
const path = join(root, SC_RESERVATIONS_PATH);
|
|
173
|
+
if (!existsSync(path)) {
|
|
174
|
+
return { status: "absent", path, blocks: [], reason: `no reservation store at ${SC_RESERVATIONS_PATH}` };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let raw: string;
|
|
178
|
+
try {
|
|
179
|
+
raw = readFileSync(path, "utf-8");
|
|
180
|
+
} catch (err) {
|
|
181
|
+
return { status: "unreadable", path, blocks: [], reason: (err as Error).message };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let parsed: unknown;
|
|
185
|
+
try {
|
|
186
|
+
parsed = JSON.parse(raw);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
return { status: "unreadable", path, blocks: [], reason: `invalid JSON: ${(err as Error).message}` };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
192
|
+
return { status: "unreadable", path, blocks: [], reason: "the store is not a JSON object" };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// The control string. An object with no `reservations` key yields an empty
|
|
196
|
+
// block list through every other path in this function, and that empty list
|
|
197
|
+
// is indistinguishable from a store that reserves nothing — so it is checked
|
|
198
|
+
// before any empty result is believed.
|
|
199
|
+
const declared = (parsed as Record<string, unknown>)[RESERVATIONS_CONTROL_KEY];
|
|
200
|
+
if (declared === undefined) {
|
|
201
|
+
return {
|
|
202
|
+
status: "unreadable",
|
|
203
|
+
path,
|
|
204
|
+
blocks: [],
|
|
205
|
+
reason: `the store declares no "${RESERVATIONS_CONTROL_KEY}" key, so an empty reading would be a reading of nothing`,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (!Array.isArray(declared)) {
|
|
209
|
+
return { status: "unreadable", path, blocks: [], reason: `"${RESERVATIONS_CONTROL_KEY}" is not an array` };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const faults = declared.map((b, i) => blockFault(b, i)).filter((f): f is string => f !== null);
|
|
213
|
+
if (faults.length > 0) {
|
|
214
|
+
return { status: "unreadable", path, blocks: [], reason: faults.join("; ") };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const blocks = declared.map(b => {
|
|
218
|
+
const r = b as Record<string, unknown>;
|
|
219
|
+
const block: ScReservation = { owner: r.owner as string, from: r.from as number, to: r.to as number };
|
|
220
|
+
if (typeof r.note === "string") block.note = r.note;
|
|
221
|
+
return block;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const overlap = overlapFault(blocks);
|
|
225
|
+
if (overlap !== null) {
|
|
226
|
+
return { status: "unreadable", path, blocks: [], reason: `the store declares an overlap: ${overlap}` };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return { status: "read", path, blocks };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** How many ids the store declares occupied. `0` under every non-`read` status. */
|
|
233
|
+
export function reservedIdCount(blocks: ScReservation[]): number {
|
|
234
|
+
return blocks.reduce((n, b) => n + (b.to - b.from + 1), 0);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The highest id any block reaches, or `0` when nothing is reserved. */
|
|
238
|
+
export function reservedCeiling(blocks: ScReservation[]): number {
|
|
239
|
+
return blocks.reduce((n, b) => Math.max(n, b.to), 0);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The block reserved to `owner`, or `null`.
|
|
244
|
+
*
|
|
245
|
+
* Exact match on the owner string. A prefix or substring match is the tempting
|
|
246
|
+
* widening and it is the wrong one: `fix/725` matching `fix/725-followup`
|
|
247
|
+
* hands one block to two branches, which is the defect this file exists over.
|
|
248
|
+
*/
|
|
249
|
+
export function reservationFor(blocks: ScReservation[], owner: string | null): ScReservation | null {
|
|
250
|
+
if (owner === null || owner.trim() === "") return null;
|
|
251
|
+
return blocks.find(b => b.owner === owner) ?? null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The lowest id in `block` that nothing in `taken` already holds, or `null`
|
|
256
|
+
* when the block is spent.
|
|
257
|
+
*
|
|
258
|
+
* `null` is a refusal the caller has to say something about. Falling back to
|
|
259
|
+
* "allocate above the block" would quietly undo the separation the block was
|
|
260
|
+
* handed out to provide, on the one day it matters.
|
|
261
|
+
*/
|
|
262
|
+
export function lowestFreeInBlock(block: ScReservation, taken: ReadonlySet<number>): number | null {
|
|
263
|
+
for (let n = block.from; n <= block.to; n++) if (!taken.has(n)) return n;
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The branch name a CLI should look itself up by, or `null`.
|
|
269
|
+
*
|
|
270
|
+
* `null` on a detached HEAD or on anything that is not a git checkout, and
|
|
271
|
+
* never a guess: an owner this function invented would match no block, which
|
|
272
|
+
* routes the caller to the no-block path — above every reservation — rather
|
|
273
|
+
* than into somebody else's.
|
|
274
|
+
*/
|
|
275
|
+
export function currentScOwner(root: string): string | null {
|
|
276
|
+
try {
|
|
277
|
+
const branch = execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
|
|
278
|
+
cwd: root,
|
|
279
|
+
encoding: "utf-8",
|
|
280
|
+
timeout: 30_000,
|
|
281
|
+
// Captured, not inherited — a `fatal: not a git repository` in the
|
|
282
|
+
// middle of a suite's output breaks every downstream reader of it (#60).
|
|
283
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
284
|
+
}).trim();
|
|
285
|
+
return branch === "" || branch === "HEAD" ? null : branch;
|
|
286
|
+
} catch {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scaffold/ai-review-findings.ts — the AI review's findings contract, and the
|
|
3
|
+
* blocking verdict derived from it (#631).
|
|
4
|
+
*
|
|
5
|
+
* ## Why the agent's verdict word is not the decider
|
|
6
|
+
*
|
|
7
|
+
* The reviewer is an agent, and an agent grading its own gate is the shape
|
|
8
|
+
* this repo distrusts most. So the contract is: the agent writes a payload of
|
|
9
|
+
* findings, each carrying a severity, and the blocking decision is computed
|
|
10
|
+
* HERE from those severities. The verdict word is read for exactly one
|
|
11
|
+
* purpose — to notice that the agent contradicted its own payload — and never
|
|
12
|
+
* to decide anything. `prevalidationViolations` in `gates/workflow.test.ts` is
|
|
13
|
+
* the shape being copied: a PASS carrying broken ids is a contradiction, not a
|
|
14
|
+
* pass. Without that branch a writer could record the blocking list faithfully
|
|
15
|
+
* and still be waved through, which is the fail-open that survives every test
|
|
16
|
+
* written only against the happy path.
|
|
17
|
+
*
|
|
18
|
+
* ## Why the key is a constant and not a string
|
|
19
|
+
*
|
|
20
|
+
* #295, in that same function: the reader asked for `reading.broken`, a key no
|
|
21
|
+
* writer ever produced and the schema never declared. The list was empty on
|
|
22
|
+
* every run, so every FAIL fell through to "no broken ids were recorded" — an
|
|
23
|
+
* absent key reads as an empty list reads as clean. One exported constant is
|
|
24
|
+
* the whole fix: the step that reads the payload and the prompt that tells the
|
|
25
|
+
* agent what to write name the same binding, so they cannot drift, and
|
|
26
|
+
* `test/ai-review-findings.test.ts` fails the suite if a second spelling of it
|
|
27
|
+
* appears anywhere in this module.
|
|
28
|
+
*
|
|
29
|
+
* ## Why so many ways to be UNMEASURED
|
|
30
|
+
*
|
|
31
|
+
* `0`, `{}`, `null` and `SKIP` are indistinguishable from success; UNMEASURED
|
|
32
|
+
* is not (`.claude/rules/config-access.md`). A payload this module cannot read
|
|
33
|
+
* is a review whose result nobody knows, and the states it can be in — no
|
|
34
|
+
* file, unreadable file, not JSON, no declared key, a finding with no severity
|
|
35
|
+
* — are different facts with different fixes, so each is named. None of them
|
|
36
|
+
* reads as clean, and the derived decision is WITHHELD rather than defaulted:
|
|
37
|
+
* `blocked` is `null` under an UNMEASURED reading.
|
|
38
|
+
*
|
|
39
|
+
* ## The exit code
|
|
40
|
+
*
|
|
41
|
+
* Every refusal routes through `AI_REVIEW_REQUIRED_EXIT`, imported from
|
|
42
|
+
* `./ai-review-step` and never re-declared here. One site to mutate, so
|
|
43
|
+
* `test/ai-review-exit-binding.test.ts` can zero it and prove each refusal is
|
|
44
|
+
* what the case observed. A later branch inlining its own literal would
|
|
45
|
+
* survive that mutation and the case would go green having witnessed nothing.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
import { readFileSync } from "fs";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The contract itself is declared in `./ai-review-step`, beside the prompt
|
|
52
|
+
* that asks a writer to honour it, and imported here rather than restated.
|
|
53
|
+
*
|
|
54
|
+
* That direction is forced and it is the right one anyway: `ai-review-step.ts`
|
|
55
|
+
* has no relative imports, because `test/ai-review-scaffold.test.ts` builds a
|
|
56
|
+
* mutant copy of it that runs from a temp directory. So the module holding the
|
|
57
|
+
* contract cannot depend on the module reading it, and every name below has
|
|
58
|
+
* exactly one declaration — which is the whole of #295's lesson. A reader and
|
|
59
|
+
* a writer that merely agree today are two contracts, and nothing fails to
|
|
60
|
+
* compile when they stop agreeing.
|
|
61
|
+
*
|
|
62
|
+
* Re-exported because the reader's own callers and tests name them, and a
|
|
63
|
+
* second `export const` here would be the duplicate this paragraph is about.
|
|
64
|
+
*/
|
|
65
|
+
import {
|
|
66
|
+
AI_REVIEW_BLOCK_VERDICT,
|
|
67
|
+
AI_REVIEW_FINDING_FIELDS,
|
|
68
|
+
AI_REVIEW_FINDINGS_KEY,
|
|
69
|
+
AI_REVIEW_OUTCOME_LINE,
|
|
70
|
+
AI_REVIEW_PASS_VERDICT,
|
|
71
|
+
AI_REVIEW_REQUIRED_EXIT,
|
|
72
|
+
AI_REVIEW_SEVERITIES,
|
|
73
|
+
AI_REVIEW_SEVERITY_KEY,
|
|
74
|
+
AI_REVIEW_VERDICT_KEY,
|
|
75
|
+
} from "./ai-review-step";
|
|
76
|
+
|
|
77
|
+
export {
|
|
78
|
+
AI_REVIEW_BLOCK_VERDICT,
|
|
79
|
+
AI_REVIEW_FINDING_FIELDS,
|
|
80
|
+
AI_REVIEW_FINDINGS_KEY,
|
|
81
|
+
AI_REVIEW_OUTCOME_LINE,
|
|
82
|
+
AI_REVIEW_PASS_VERDICT,
|
|
83
|
+
AI_REVIEW_SEVERITIES,
|
|
84
|
+
AI_REVIEW_SEVERITY_KEY,
|
|
85
|
+
AI_REVIEW_VERDICT_KEY,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Percent-encode a value before it is interpolated into a workflow command.
|
|
90
|
+
*
|
|
91
|
+
* ONE escaper, because there is one rule and a second copy is a second rule
|
|
92
|
+
* nobody updates. GitHub parses `::command::` lines one per line, so a newline
|
|
93
|
+
* inside an interpolated value starts a NEW command — and the payload this
|
|
94
|
+
* reads is written by an agent that has just read untrusted branch content.
|
|
95
|
+
*
|
|
96
|
+
* Measured on wave 2 at `22c9da89`, not reasoned about: a finding titled
|
|
97
|
+
* `benign\n::stop-commands::deadbeef\n::notice::all clear` made the reader emit
|
|
98
|
+
* `::stop-commands::deadbeef` as its own line. That token disables all
|
|
99
|
+
* subsequent workflow-command processing, so a BLOCKED review becomes
|
|
100
|
+
* indistinguishable from a clean one — the exact failure this module exists to
|
|
101
|
+
* prevent, reachable by writing a string.
|
|
102
|
+
*/
|
|
103
|
+
export function escapeAnnotation(value: unknown): string {
|
|
104
|
+
return String(value)
|
|
105
|
+
.replace(/%/g, "%25")
|
|
106
|
+
.replace(/\r/g, "%0D")
|
|
107
|
+
.replace(/\n/g, "%0A");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Annotation detail is capped: a megabyte of agent prose is not a message. */
|
|
111
|
+
export const AI_REVIEW_DETAIL_MAX = 300;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* One finding, rendered for an annotation — capped, naming file and summary.
|
|
115
|
+
*
|
|
116
|
+
* Deliberately does NOT escape. Escaping happens once, at the single emission
|
|
117
|
+
* site in `main()`, because two escapers applied in sequence turn `%` into
|
|
118
|
+
* `%2525` and the bug that produces is invisible until someone reads a log.
|
|
119
|
+
* One rule, one binding, applied last.
|
|
120
|
+
*/
|
|
121
|
+
export function renderFinding(index: number, finding: Record<string, unknown>): string {
|
|
122
|
+
const f = AI_REVIEW_FINDING_FIELDS;
|
|
123
|
+
const where = finding[f.file] !== undefined
|
|
124
|
+
? `${String(finding[f.file])}:${String(finding[f.line] ?? "?")}`
|
|
125
|
+
: "(no file recorded)";
|
|
126
|
+
const what = finding[f.summary] !== undefined
|
|
127
|
+
? String(finding[f.summary])
|
|
128
|
+
: "(no summary recorded)";
|
|
129
|
+
const line = `finding ${index} is ${String(finding[f.severity])} at ${where}: ${what}`;
|
|
130
|
+
return line.length > AI_REVIEW_DETAIL_MAX ? `${line.slice(0, AI_REVIEW_DETAIL_MAX)}…` : line;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The annotation title the script prints its refusal under. */
|
|
134
|
+
export const AI_REVIEW_FINDINGS_TITLE = "AI review findings";
|
|
135
|
+
|
|
136
|
+
/** The four states a reading can be in. Exactly one is reached per run. */
|
|
137
|
+
export type AiReviewOutcome = "CLEAN" | "BLOCKED" | "CONTRADICTION" | "UNMEASURED";
|
|
138
|
+
|
|
139
|
+
/** Why a reading is UNMEASURED. Each is a different fact with a different fix. */
|
|
140
|
+
export type AiReviewUnmeasuredReason =
|
|
141
|
+
| "FINDINGS_FILE_ABSENT"
|
|
142
|
+
| "FINDINGS_FILE_UNREADABLE"
|
|
143
|
+
| "FINDINGS_UNPARSEABLE"
|
|
144
|
+
| "FINDINGS_NOT_AN_OBJECT"
|
|
145
|
+
| "FINDINGS_KEY_ABSENT"
|
|
146
|
+
| "FINDINGS_KEY_NOT_A_LIST"
|
|
147
|
+
| "FINDING_NOT_AN_OBJECT"
|
|
148
|
+
| "SEVERITY_ABSENT"
|
|
149
|
+
| "SEVERITY_UNRECOGNISED"
|
|
150
|
+
| "VERDICT_ABSENT"
|
|
151
|
+
| "VERDICT_UNRECOGNISED";
|
|
152
|
+
|
|
153
|
+
export interface AiReviewFindingsReading {
|
|
154
|
+
/** The single discriminator. Everything else is a projection of the same pass. */
|
|
155
|
+
outcome: AiReviewOutcome;
|
|
156
|
+
/**
|
|
157
|
+
* Computed from the severities alone, and `null` under UNMEASURED — a
|
|
158
|
+
* withheld decision, not a defaulted one.
|
|
159
|
+
*/
|
|
160
|
+
blocked: boolean | null;
|
|
161
|
+
/** The payload as read, or `null` when there was nothing readable to read. */
|
|
162
|
+
reported: unknown[] | null;
|
|
163
|
+
/** The subset of `reported` whose severity blocks, or `null` under UNMEASURED. */
|
|
164
|
+
blockingReported: unknown[] | null;
|
|
165
|
+
/** The agent's own word, or `null` when it did not write a usable one. */
|
|
166
|
+
verdict: string | null;
|
|
167
|
+
/** Empty if and only if the outcome is not UNMEASURED. */
|
|
168
|
+
unmeasuredReasons: AiReviewUnmeasuredReason[];
|
|
169
|
+
/** The producing code's own sentences, one per reason or blocking finding. */
|
|
170
|
+
detail: string[];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* `true`, `false`, or `null` for a severity the table does not declare.
|
|
175
|
+
*
|
|
176
|
+
* Three states, not two: an unrecognised severity is unclassified, and
|
|
177
|
+
* unclassified is not "does not block".
|
|
178
|
+
*/
|
|
179
|
+
export function severityBlocks(severity: unknown): boolean | null {
|
|
180
|
+
if (typeof severity !== "string") return null;
|
|
181
|
+
const blocks = AI_REVIEW_SEVERITIES[severity];
|
|
182
|
+
return blocks === undefined ? null : blocks;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function unmeasured(
|
|
186
|
+
reasons: AiReviewUnmeasuredReason[],
|
|
187
|
+
detail: string[],
|
|
188
|
+
reported: unknown[] | null = null,
|
|
189
|
+
): AiReviewFindingsReading {
|
|
190
|
+
return {
|
|
191
|
+
outcome: "UNMEASURED",
|
|
192
|
+
blocked: null,
|
|
193
|
+
reported,
|
|
194
|
+
blockingReported: null,
|
|
195
|
+
verdict: null,
|
|
196
|
+
unmeasuredReasons: reasons,
|
|
197
|
+
detail,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Read a findings payload and decide what it means.
|
|
203
|
+
*
|
|
204
|
+
* `raw` is `null` for "there was no payload at all", which is a reading and
|
|
205
|
+
* not an error: a job whose reviewer never wrote anything has not passed a
|
|
206
|
+
* review, it has not had one.
|
|
207
|
+
*/
|
|
208
|
+
export function readAiReviewFindings(raw: string | null): AiReviewFindingsReading {
|
|
209
|
+
if (raw === null) {
|
|
210
|
+
return unmeasured(["FINDINGS_FILE_ABSENT"], ["no findings payload was produced"]);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let doc: unknown;
|
|
214
|
+
try {
|
|
215
|
+
doc = JSON.parse(raw);
|
|
216
|
+
} catch (e) {
|
|
217
|
+
return unmeasured(
|
|
218
|
+
["FINDINGS_UNPARSEABLE"],
|
|
219
|
+
[`the payload is not JSON: ${(e as Error).message}`],
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
|
|
224
|
+
return unmeasured(
|
|
225
|
+
["FINDINGS_NOT_AN_OBJECT"],
|
|
226
|
+
[`the payload parsed to ${Array.isArray(doc) ? "an array" : String(doc)}, not an object`],
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const obj = doc as Record<string, unknown>;
|
|
231
|
+
const present = Object.keys(obj);
|
|
232
|
+
|
|
233
|
+
if (!(AI_REVIEW_FINDINGS_KEY in obj)) {
|
|
234
|
+
return unmeasured(
|
|
235
|
+
["FINDINGS_KEY_ABSENT"],
|
|
236
|
+
[
|
|
237
|
+
`the payload carries no '${AI_REVIEW_FINDINGS_KEY}' key ` +
|
|
238
|
+
`(it carries ${present.length > 0 ? present.join(", ") : "no keys at all"}). ` +
|
|
239
|
+
`An absent key is not an empty list and an empty list is not a clean review.`,
|
|
240
|
+
],
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const list = obj[AI_REVIEW_FINDINGS_KEY];
|
|
245
|
+
if (!Array.isArray(list)) {
|
|
246
|
+
return unmeasured(
|
|
247
|
+
["FINDINGS_KEY_NOT_A_LIST"],
|
|
248
|
+
[`'${AI_REVIEW_FINDINGS_KEY}' is ${typeof list}, not a list`],
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const reasons: AiReviewUnmeasuredReason[] = [];
|
|
253
|
+
const detail: string[] = [];
|
|
254
|
+
const blocking: unknown[] = [];
|
|
255
|
+
|
|
256
|
+
list.forEach((item, i) => {
|
|
257
|
+
if (item === null || typeof item !== "object" || Array.isArray(item)) {
|
|
258
|
+
reasons.push("FINDING_NOT_AN_OBJECT");
|
|
259
|
+
detail.push(`finding ${i} is not an object`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const finding = item as Record<string, unknown>;
|
|
263
|
+
if (!(AI_REVIEW_SEVERITY_KEY in finding)) {
|
|
264
|
+
reasons.push("SEVERITY_ABSENT");
|
|
265
|
+
detail.push(`finding ${i} carries no '${AI_REVIEW_SEVERITY_KEY}'`);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const severity = finding[AI_REVIEW_SEVERITY_KEY];
|
|
269
|
+
const blocks = severityBlocks(severity);
|
|
270
|
+
if (blocks === null) {
|
|
271
|
+
reasons.push("SEVERITY_UNRECOGNISED");
|
|
272
|
+
detail.push(
|
|
273
|
+
`finding ${i} has severity ${JSON.stringify(severity)}, which the contract ` +
|
|
274
|
+
`does not declare (${Object.keys(AI_REVIEW_SEVERITIES).join(", ")})`,
|
|
275
|
+
);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (blocks) {
|
|
279
|
+
blocking.push(item);
|
|
280
|
+
detail.push(renderFinding(i, finding));
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
let verdict: string | null = null;
|
|
285
|
+
if (!(AI_REVIEW_VERDICT_KEY in obj)) {
|
|
286
|
+
reasons.push("VERDICT_ABSENT");
|
|
287
|
+
detail.push(`the payload carries no '${AI_REVIEW_VERDICT_KEY}' key`);
|
|
288
|
+
} else {
|
|
289
|
+
const word = obj[AI_REVIEW_VERDICT_KEY];
|
|
290
|
+
if (word !== AI_REVIEW_PASS_VERDICT && word !== AI_REVIEW_BLOCK_VERDICT) {
|
|
291
|
+
reasons.push("VERDICT_UNRECOGNISED");
|
|
292
|
+
detail.push(
|
|
293
|
+
`'${AI_REVIEW_VERDICT_KEY}' is ${JSON.stringify(word)}, which is neither ` +
|
|
294
|
+
`${AI_REVIEW_PASS_VERDICT} nor ${AI_REVIEW_BLOCK_VERDICT}`,
|
|
295
|
+
);
|
|
296
|
+
} else {
|
|
297
|
+
verdict = word;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (reasons.length > 0) return unmeasured(reasons, detail, list);
|
|
302
|
+
|
|
303
|
+
// The decision, and the only place it is made. `verdict` is compared to it
|
|
304
|
+
// afterwards; it never contributes to it.
|
|
305
|
+
const blocked = blocking.length > 0;
|
|
306
|
+
const agreed = verdict === (blocked ? AI_REVIEW_BLOCK_VERDICT : AI_REVIEW_PASS_VERDICT);
|
|
307
|
+
const outcome: AiReviewOutcome = !agreed ? "CONTRADICTION" : blocked ? "BLOCKED" : "CLEAN";
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
outcome,
|
|
311
|
+
blocked,
|
|
312
|
+
reported: list,
|
|
313
|
+
blockingReported: blocking,
|
|
314
|
+
verdict,
|
|
315
|
+
unmeasuredReasons: [],
|
|
316
|
+
detail,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Read the payload from disk. An absent file and an unreadable one differ. */
|
|
321
|
+
export function readAiReviewFindingsFile(path: string): AiReviewFindingsReading {
|
|
322
|
+
let raw: string;
|
|
323
|
+
try {
|
|
324
|
+
raw = readFileSync(path, "utf-8");
|
|
325
|
+
} catch (e) {
|
|
326
|
+
const code = (e as NodeJS.ErrnoException).code;
|
|
327
|
+
if (code === "ENOENT") {
|
|
328
|
+
return unmeasured(["FINDINGS_FILE_ABSENT"], [`no findings payload at ${path}`]);
|
|
329
|
+
}
|
|
330
|
+
return unmeasured(
|
|
331
|
+
["FINDINGS_FILE_UNREADABLE"],
|
|
332
|
+
[`the findings payload at ${path} could not be read: ${(e as Error).message}`],
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return readAiReviewFindings(raw);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** The sentence a refusal prints, or `null` when there is nothing to refuse. */
|
|
339
|
+
export function aiReviewRefusal(reading: AiReviewFindingsReading): string | null {
|
|
340
|
+
// The producing code's own sentences, carried rather than re-derived. An
|
|
341
|
+
// empty list is a reading too, so it must not print as a stray full stop.
|
|
342
|
+
const detail = reading.detail.length > 0 ? `${reading.detail.join("; ")}. ` : "";
|
|
343
|
+
switch (reading.outcome) {
|
|
344
|
+
case "CLEAN":
|
|
345
|
+
return null;
|
|
346
|
+
case "UNMEASURED":
|
|
347
|
+
return (
|
|
348
|
+
`The AI review is UNMEASURED: ${reading.unmeasuredReasons.join(", ")}. ` +
|
|
349
|
+
`${detail}Failing rather than reporting a green check for a review ` +
|
|
350
|
+
`whose result nobody can read.`
|
|
351
|
+
);
|
|
352
|
+
case "BLOCKED":
|
|
353
|
+
return (
|
|
354
|
+
`The AI review reported ${reading.blockingReported?.length ?? 0} blocking ` +
|
|
355
|
+
`finding(s). ${detail}`.trimEnd()
|
|
356
|
+
);
|
|
357
|
+
case "CONTRADICTION":
|
|
358
|
+
return (
|
|
359
|
+
`The AI review's verdict ${JSON.stringify(reading.verdict)} contradicts its ` +
|
|
360
|
+
`own payload: ${reading.blockingReported?.length ?? 0} of ` +
|
|
361
|
+
`${reading.reported?.length ?? 0} finding(s) block. ${detail}` +
|
|
362
|
+
`A verdict that disagrees with the findings beneath it is a contradiction, ` +
|
|
363
|
+
`not a result.`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** 0 for a clean reading; the one imported refusal code for everything else. */
|
|
369
|
+
export function aiReviewExitCode(reading: AiReviewFindingsReading): number {
|
|
370
|
+
return reading.outcome === "CLEAN" ? 0 : AI_REVIEW_REQUIRED_EXIT;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* The CLI the generated workflow step runs: one argument, the payload's path.
|
|
375
|
+
*
|
|
376
|
+
* Returns the exit code rather than calling `process.exit`, so a test can run
|
|
377
|
+
* it in-process as well as spawn it.
|
|
378
|
+
*/
|
|
379
|
+
export function main(argv: string[]): number {
|
|
380
|
+
const path = argv[0];
|
|
381
|
+
const reading =
|
|
382
|
+
path === undefined || path === ""
|
|
383
|
+
? unmeasured(
|
|
384
|
+
["FINDINGS_FILE_ABSENT"],
|
|
385
|
+
["no findings payload path was given to the findings reader"],
|
|
386
|
+
)
|
|
387
|
+
: readAiReviewFindingsFile(path);
|
|
388
|
+
|
|
389
|
+
// FIRST, and before any branch below can return. The generated workflow step
|
|
390
|
+
// greps its own output for this prefix and will not read the exit code as a
|
|
391
|
+
// verdict without it (#1013) — an adjudicator that could not run and one that
|
|
392
|
+
// refused both exit non-zero, and only this line tells them apart.
|
|
393
|
+
console.log(`${AI_REVIEW_OUTCOME_LINE} ${reading.outcome}`);
|
|
394
|
+
|
|
395
|
+
// The ONE place a value reaches a workflow command. Everything upstream of
|
|
396
|
+
// here — `renderFinding`, every `detail` sentence, `aiReviewRefusal` — builds
|
|
397
|
+
// plain text and leaves escaping alone, so there is one binding to mutate and
|
|
398
|
+
// no path where two escapers run in sequence and turn `%` into `%2525`.
|
|
399
|
+
const refusal = aiReviewRefusal(reading);
|
|
400
|
+
if (refusal !== null) {
|
|
401
|
+
console.error(`::error title=${AI_REVIEW_FINDINGS_TITLE}::${escapeAnnotation(refusal)}`);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return aiReviewExitCode(reading);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (import.meta.main) {
|
|
408
|
+
process.exit(main(process.argv.slice(2)));
|
|
409
|
+
}
|