rungate 0.5.0 → 0.7.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/lib/affected-tests.ts +398 -0
- package/lib/agent-audit.ts +2 -3
- package/lib/agent-worktree.ts +287 -0
- package/lib/agents-md-sections.ts +209 -0
- package/lib/behavioral-cache.ts +150 -2
- package/lib/branch-diff.ts +139 -0
- 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 +347 -0
- package/lib/check-remedy.ts +140 -0
- package/lib/ci-checkout.ts +18 -1
- 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 +196 -20
- package/lib/config-loader.ts +51 -1
- package/lib/config-trackable.ts +391 -0
- package/lib/conformity.ts +1272 -189
- package/lib/containment.ts +238 -0
- package/lib/content-hash.ts +24 -0
- package/lib/context-docs.ts +143 -28
- package/lib/create-brief.ts +2 -1
- package/lib/create-sc.ts +323 -4
- 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 +186 -0
- package/lib/doc-routing.ts +157 -0
- package/lib/doc-scan-targets.ts +9 -0
- package/lib/evidence-command-transport.ts +160 -0
- package/lib/evidence-prevalidator.ts +246 -2
- package/lib/evidence-satisfiability.ts +534 -12
- package/lib/failure-intake.ts +317 -0
- package/lib/fallow-availability.ts +286 -0
- package/lib/fs-write-capabilities.ts +232 -0
- package/lib/gate-refusal-currency.ts +112 -0
- package/lib/gate-run-split.ts +147 -0
- package/lib/gate-timeouts.ts +82 -0
- package/lib/generators/agent-briefs.ts +4 -1
- package/lib/generators/agents-md.ts +204 -48
- package/lib/generators/code-map.ts +2 -1
- package/lib/generators/types.ts +8 -0
- package/lib/git-dates.ts +55 -0
- 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/js-top-level-code.ts +78 -10
- package/lib/junit-checks.ts +536 -0
- package/lib/launch-base.ts +253 -0
- package/lib/line-citation.ts +372 -0
- package/lib/local-calendar-day.ts +78 -0
- package/lib/orphan-reaper.ts +468 -0
- package/lib/parity-binding.ts +205 -0
- package/lib/per-test-timeouts.ts +219 -0
- 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/project-state-shape.ts +59 -0
- package/lib/promote-outputs.ts +6 -2
- package/lib/published-artifact.ts +525 -0
- package/lib/readdir-sorted.ts +45 -0
- package/lib/report-provenance.ts +156 -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-claim-forms.ts +75 -0
- package/lib/sc-id-cross-tree.ts +323 -0
- package/lib/sc-ids.ts +121 -13
- package/lib/sc-reservations.ts +289 -0
- package/lib/scaffold/ai-review-findings.ts +403 -0
- package/lib/scaffold/ai-review-step.ts +4777 -0
- package/lib/scaffold/managed-workflow.ts +587 -192
- package/lib/scaffold/owned-files.ts +186 -39
- package/lib/scaffold/pre-push-hook.ts +49 -5
- package/lib/scaffold/rehearsal.ts +205 -71
- package/lib/scaffold/steps.ts +1517 -293
- package/lib/scaffold/workflow-census.ts +217 -0
- package/lib/scaffold/workflow-drift.ts +217 -0
- package/lib/scaffold/write-steps.ts +10 -4
- package/lib/scan-budget.ts +247 -0
- package/lib/scanner.ts +80 -62
- 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/ship-mode.ts +219 -0
- package/lib/source-slice.ts +294 -0
- package/lib/spec-listing.ts +148 -0
- package/lib/spec-requirements.ts +209 -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 +411 -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-command.ts +133 -0
- package/lib/test-suite-lock.ts +325 -26
- package/lib/tool-probe.ts +142 -0
- package/lib/top-level-cd.ts +393 -0
- package/lib/tracked-vs-ignored.ts +234 -0
- package/lib/tree-entries.ts +89 -0
- package/lib/ts-ast.ts +200 -0
- package/lib/validators/spec-validators.ts +3 -2
- package/lib/walk-test-files.ts +97 -0
- package/lib/workflow-actions.ts +263 -0
- package/lib/worktree-collect.ts +177 -5
- package/lib/worktree-repo.ts +36 -1
- package/package.json +2 -1
- package/scripts/ai-review-findings.ts +45 -0
- package/scripts/audit-specs.ts +247 -12
- package/scripts/audit-transcript.ts +2 -1
- package/scripts/convert-spec.ts +2 -1
- package/scripts/create-sc.ts +37 -7
- package/scripts/create-spec.ts +2 -1
- package/scripts/create-success.ts +2 -1
- package/scripts/extract-constraints.ts +197 -26
- package/scripts/generate-code-map.ts +62 -28
- package/scripts/scaffold-project.ts +12 -2
- package/scripts/split-spec.ts +2 -1
- package/scripts/sync-sc-status.ts +127 -14
- package/scripts/sync-spec-tests.ts +2 -1
|
@@ -0,0 +1,403 @@
|
|
|
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_PASS_VERDICT,
|
|
70
|
+
AI_REVIEW_REQUIRED_EXIT,
|
|
71
|
+
AI_REVIEW_SEVERITIES,
|
|
72
|
+
AI_REVIEW_SEVERITY_KEY,
|
|
73
|
+
AI_REVIEW_VERDICT_KEY,
|
|
74
|
+
} from "./ai-review-step";
|
|
75
|
+
|
|
76
|
+
export {
|
|
77
|
+
AI_REVIEW_BLOCK_VERDICT,
|
|
78
|
+
AI_REVIEW_FINDING_FIELDS,
|
|
79
|
+
AI_REVIEW_FINDINGS_KEY,
|
|
80
|
+
AI_REVIEW_PASS_VERDICT,
|
|
81
|
+
AI_REVIEW_SEVERITIES,
|
|
82
|
+
AI_REVIEW_SEVERITY_KEY,
|
|
83
|
+
AI_REVIEW_VERDICT_KEY,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Percent-encode a value before it is interpolated into a workflow command.
|
|
88
|
+
*
|
|
89
|
+
* ONE escaper, because there is one rule and a second copy is a second rule
|
|
90
|
+
* nobody updates. GitHub parses `::command::` lines one per line, so a newline
|
|
91
|
+
* inside an interpolated value starts a NEW command — and the payload this
|
|
92
|
+
* reads is written by an agent that has just read untrusted branch content.
|
|
93
|
+
*
|
|
94
|
+
* Measured on wave 2 at `22c9da89`, not reasoned about: a finding titled
|
|
95
|
+
* `benign\n::stop-commands::deadbeef\n::notice::all clear` made the reader emit
|
|
96
|
+
* `::stop-commands::deadbeef` as its own line. That token disables all
|
|
97
|
+
* subsequent workflow-command processing, so a BLOCKED review becomes
|
|
98
|
+
* indistinguishable from a clean one — the exact failure this module exists to
|
|
99
|
+
* prevent, reachable by writing a string.
|
|
100
|
+
*/
|
|
101
|
+
export function escapeAnnotation(value: unknown): string {
|
|
102
|
+
return String(value)
|
|
103
|
+
.replace(/%/g, "%25")
|
|
104
|
+
.replace(/\r/g, "%0D")
|
|
105
|
+
.replace(/\n/g, "%0A");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Annotation detail is capped: a megabyte of agent prose is not a message. */
|
|
109
|
+
export const AI_REVIEW_DETAIL_MAX = 300;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* One finding, rendered for an annotation — capped, naming file and summary.
|
|
113
|
+
*
|
|
114
|
+
* Deliberately does NOT escape. Escaping happens once, at the single emission
|
|
115
|
+
* site in `main()`, because two escapers applied in sequence turn `%` into
|
|
116
|
+
* `%2525` and the bug that produces is invisible until someone reads a log.
|
|
117
|
+
* One rule, one binding, applied last.
|
|
118
|
+
*/
|
|
119
|
+
export function renderFinding(index: number, finding: Record<string, unknown>): string {
|
|
120
|
+
const f = AI_REVIEW_FINDING_FIELDS;
|
|
121
|
+
const where = finding[f.file] !== undefined
|
|
122
|
+
? `${String(finding[f.file])}:${String(finding[f.line] ?? "?")}`
|
|
123
|
+
: "(no file recorded)";
|
|
124
|
+
const what = finding[f.summary] !== undefined
|
|
125
|
+
? String(finding[f.summary])
|
|
126
|
+
: "(no summary recorded)";
|
|
127
|
+
const line = `finding ${index} is ${String(finding[f.severity])} at ${where}: ${what}`;
|
|
128
|
+
return line.length > AI_REVIEW_DETAIL_MAX ? `${line.slice(0, AI_REVIEW_DETAIL_MAX)}…` : line;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The annotation title the script prints its refusal under. */
|
|
132
|
+
export const AI_REVIEW_FINDINGS_TITLE = "AI review findings";
|
|
133
|
+
|
|
134
|
+
/** The four states a reading can be in. Exactly one is reached per run. */
|
|
135
|
+
export type AiReviewOutcome = "CLEAN" | "BLOCKED" | "CONTRADICTION" | "UNMEASURED";
|
|
136
|
+
|
|
137
|
+
/** Why a reading is UNMEASURED. Each is a different fact with a different fix. */
|
|
138
|
+
export type AiReviewUnmeasuredReason =
|
|
139
|
+
| "FINDINGS_FILE_ABSENT"
|
|
140
|
+
| "FINDINGS_FILE_UNREADABLE"
|
|
141
|
+
| "FINDINGS_UNPARSEABLE"
|
|
142
|
+
| "FINDINGS_NOT_AN_OBJECT"
|
|
143
|
+
| "FINDINGS_KEY_ABSENT"
|
|
144
|
+
| "FINDINGS_KEY_NOT_A_LIST"
|
|
145
|
+
| "FINDING_NOT_AN_OBJECT"
|
|
146
|
+
| "SEVERITY_ABSENT"
|
|
147
|
+
| "SEVERITY_UNRECOGNISED"
|
|
148
|
+
| "VERDICT_ABSENT"
|
|
149
|
+
| "VERDICT_UNRECOGNISED";
|
|
150
|
+
|
|
151
|
+
export interface AiReviewFindingsReading {
|
|
152
|
+
/** The single discriminator. Everything else is a projection of the same pass. */
|
|
153
|
+
outcome: AiReviewOutcome;
|
|
154
|
+
/**
|
|
155
|
+
* Computed from the severities alone, and `null` under UNMEASURED — a
|
|
156
|
+
* withheld decision, not a defaulted one.
|
|
157
|
+
*/
|
|
158
|
+
blocked: boolean | null;
|
|
159
|
+
/** The payload as read, or `null` when there was nothing readable to read. */
|
|
160
|
+
reported: unknown[] | null;
|
|
161
|
+
/** The subset of `reported` whose severity blocks, or `null` under UNMEASURED. */
|
|
162
|
+
blockingReported: unknown[] | null;
|
|
163
|
+
/** The agent's own word, or `null` when it did not write a usable one. */
|
|
164
|
+
verdict: string | null;
|
|
165
|
+
/** Empty if and only if the outcome is not UNMEASURED. */
|
|
166
|
+
unmeasuredReasons: AiReviewUnmeasuredReason[];
|
|
167
|
+
/** The producing code's own sentences, one per reason or blocking finding. */
|
|
168
|
+
detail: string[];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* `true`, `false`, or `null` for a severity the table does not declare.
|
|
173
|
+
*
|
|
174
|
+
* Three states, not two: an unrecognised severity is unclassified, and
|
|
175
|
+
* unclassified is not "does not block".
|
|
176
|
+
*/
|
|
177
|
+
export function severityBlocks(severity: unknown): boolean | null {
|
|
178
|
+
if (typeof severity !== "string") return null;
|
|
179
|
+
const blocks = AI_REVIEW_SEVERITIES[severity];
|
|
180
|
+
return blocks === undefined ? null : blocks;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function unmeasured(
|
|
184
|
+
reasons: AiReviewUnmeasuredReason[],
|
|
185
|
+
detail: string[],
|
|
186
|
+
reported: unknown[] | null = null,
|
|
187
|
+
): AiReviewFindingsReading {
|
|
188
|
+
return {
|
|
189
|
+
outcome: "UNMEASURED",
|
|
190
|
+
blocked: null,
|
|
191
|
+
reported,
|
|
192
|
+
blockingReported: null,
|
|
193
|
+
verdict: null,
|
|
194
|
+
unmeasuredReasons: reasons,
|
|
195
|
+
detail,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Read a findings payload and decide what it means.
|
|
201
|
+
*
|
|
202
|
+
* `raw` is `null` for "there was no payload at all", which is a reading and
|
|
203
|
+
* not an error: a job whose reviewer never wrote anything has not passed a
|
|
204
|
+
* review, it has not had one.
|
|
205
|
+
*/
|
|
206
|
+
export function readAiReviewFindings(raw: string | null): AiReviewFindingsReading {
|
|
207
|
+
if (raw === null) {
|
|
208
|
+
return unmeasured(["FINDINGS_FILE_ABSENT"], ["no findings payload was produced"]);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let doc: unknown;
|
|
212
|
+
try {
|
|
213
|
+
doc = JSON.parse(raw);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
return unmeasured(
|
|
216
|
+
["FINDINGS_UNPARSEABLE"],
|
|
217
|
+
[`the payload is not JSON: ${(e as Error).message}`],
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
|
|
222
|
+
return unmeasured(
|
|
223
|
+
["FINDINGS_NOT_AN_OBJECT"],
|
|
224
|
+
[`the payload parsed to ${Array.isArray(doc) ? "an array" : String(doc)}, not an object`],
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const obj = doc as Record<string, unknown>;
|
|
229
|
+
const present = Object.keys(obj);
|
|
230
|
+
|
|
231
|
+
if (!(AI_REVIEW_FINDINGS_KEY in obj)) {
|
|
232
|
+
return unmeasured(
|
|
233
|
+
["FINDINGS_KEY_ABSENT"],
|
|
234
|
+
[
|
|
235
|
+
`the payload carries no '${AI_REVIEW_FINDINGS_KEY}' key ` +
|
|
236
|
+
`(it carries ${present.length > 0 ? present.join(", ") : "no keys at all"}). ` +
|
|
237
|
+
`An absent key is not an empty list and an empty list is not a clean review.`,
|
|
238
|
+
],
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const list = obj[AI_REVIEW_FINDINGS_KEY];
|
|
243
|
+
if (!Array.isArray(list)) {
|
|
244
|
+
return unmeasured(
|
|
245
|
+
["FINDINGS_KEY_NOT_A_LIST"],
|
|
246
|
+
[`'${AI_REVIEW_FINDINGS_KEY}' is ${typeof list}, not a list`],
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const reasons: AiReviewUnmeasuredReason[] = [];
|
|
251
|
+
const detail: string[] = [];
|
|
252
|
+
const blocking: unknown[] = [];
|
|
253
|
+
|
|
254
|
+
list.forEach((item, i) => {
|
|
255
|
+
if (item === null || typeof item !== "object" || Array.isArray(item)) {
|
|
256
|
+
reasons.push("FINDING_NOT_AN_OBJECT");
|
|
257
|
+
detail.push(`finding ${i} is not an object`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const finding = item as Record<string, unknown>;
|
|
261
|
+
if (!(AI_REVIEW_SEVERITY_KEY in finding)) {
|
|
262
|
+
reasons.push("SEVERITY_ABSENT");
|
|
263
|
+
detail.push(`finding ${i} carries no '${AI_REVIEW_SEVERITY_KEY}'`);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const severity = finding[AI_REVIEW_SEVERITY_KEY];
|
|
267
|
+
const blocks = severityBlocks(severity);
|
|
268
|
+
if (blocks === null) {
|
|
269
|
+
reasons.push("SEVERITY_UNRECOGNISED");
|
|
270
|
+
detail.push(
|
|
271
|
+
`finding ${i} has severity ${JSON.stringify(severity)}, which the contract ` +
|
|
272
|
+
`does not declare (${Object.keys(AI_REVIEW_SEVERITIES).join(", ")})`,
|
|
273
|
+
);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (blocks) {
|
|
277
|
+
blocking.push(item);
|
|
278
|
+
detail.push(renderFinding(i, finding));
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
let verdict: string | null = null;
|
|
283
|
+
if (!(AI_REVIEW_VERDICT_KEY in obj)) {
|
|
284
|
+
reasons.push("VERDICT_ABSENT");
|
|
285
|
+
detail.push(`the payload carries no '${AI_REVIEW_VERDICT_KEY}' key`);
|
|
286
|
+
} else {
|
|
287
|
+
const word = obj[AI_REVIEW_VERDICT_KEY];
|
|
288
|
+
if (word !== AI_REVIEW_PASS_VERDICT && word !== AI_REVIEW_BLOCK_VERDICT) {
|
|
289
|
+
reasons.push("VERDICT_UNRECOGNISED");
|
|
290
|
+
detail.push(
|
|
291
|
+
`'${AI_REVIEW_VERDICT_KEY}' is ${JSON.stringify(word)}, which is neither ` +
|
|
292
|
+
`${AI_REVIEW_PASS_VERDICT} nor ${AI_REVIEW_BLOCK_VERDICT}`,
|
|
293
|
+
);
|
|
294
|
+
} else {
|
|
295
|
+
verdict = word;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (reasons.length > 0) return unmeasured(reasons, detail, list);
|
|
300
|
+
|
|
301
|
+
// The decision, and the only place it is made. `verdict` is compared to it
|
|
302
|
+
// afterwards; it never contributes to it.
|
|
303
|
+
const blocked = blocking.length > 0;
|
|
304
|
+
const agreed = verdict === (blocked ? AI_REVIEW_BLOCK_VERDICT : AI_REVIEW_PASS_VERDICT);
|
|
305
|
+
const outcome: AiReviewOutcome = !agreed ? "CONTRADICTION" : blocked ? "BLOCKED" : "CLEAN";
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
outcome,
|
|
309
|
+
blocked,
|
|
310
|
+
reported: list,
|
|
311
|
+
blockingReported: blocking,
|
|
312
|
+
verdict,
|
|
313
|
+
unmeasuredReasons: [],
|
|
314
|
+
detail,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Read the payload from disk. An absent file and an unreadable one differ. */
|
|
319
|
+
export function readAiReviewFindingsFile(path: string): AiReviewFindingsReading {
|
|
320
|
+
let raw: string;
|
|
321
|
+
try {
|
|
322
|
+
raw = readFileSync(path, "utf-8");
|
|
323
|
+
} catch (e) {
|
|
324
|
+
const code = (e as NodeJS.ErrnoException).code;
|
|
325
|
+
if (code === "ENOENT") {
|
|
326
|
+
return unmeasured(["FINDINGS_FILE_ABSENT"], [`no findings payload at ${path}`]);
|
|
327
|
+
}
|
|
328
|
+
return unmeasured(
|
|
329
|
+
["FINDINGS_FILE_UNREADABLE"],
|
|
330
|
+
[`the findings payload at ${path} could not be read: ${(e as Error).message}`],
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
return readAiReviewFindings(raw);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The sentence a refusal prints, or `null` when there is nothing to refuse. */
|
|
337
|
+
export function aiReviewRefusal(reading: AiReviewFindingsReading): string | null {
|
|
338
|
+
// The producing code's own sentences, carried rather than re-derived. An
|
|
339
|
+
// empty list is a reading too, so it must not print as a stray full stop.
|
|
340
|
+
const detail = reading.detail.length > 0 ? `${reading.detail.join("; ")}. ` : "";
|
|
341
|
+
switch (reading.outcome) {
|
|
342
|
+
case "CLEAN":
|
|
343
|
+
return null;
|
|
344
|
+
case "UNMEASURED":
|
|
345
|
+
return (
|
|
346
|
+
`The AI review is UNMEASURED: ${reading.unmeasuredReasons.join(", ")}. ` +
|
|
347
|
+
`${detail}Failing rather than reporting a green check for a review ` +
|
|
348
|
+
`whose result nobody can read.`
|
|
349
|
+
);
|
|
350
|
+
case "BLOCKED":
|
|
351
|
+
return (
|
|
352
|
+
`The AI review reported ${reading.blockingReported?.length ?? 0} blocking ` +
|
|
353
|
+
`finding(s). ${detail}`.trimEnd()
|
|
354
|
+
);
|
|
355
|
+
case "CONTRADICTION":
|
|
356
|
+
return (
|
|
357
|
+
`The AI review's verdict ${JSON.stringify(reading.verdict)} contradicts its ` +
|
|
358
|
+
`own payload: ${reading.blockingReported?.length ?? 0} of ` +
|
|
359
|
+
`${reading.reported?.length ?? 0} finding(s) block. ${detail}` +
|
|
360
|
+
`A verdict that disagrees with the findings beneath it is a contradiction, ` +
|
|
361
|
+
`not a result.`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** 0 for a clean reading; the one imported refusal code for everything else. */
|
|
367
|
+
export function aiReviewExitCode(reading: AiReviewFindingsReading): number {
|
|
368
|
+
return reading.outcome === "CLEAN" ? 0 : AI_REVIEW_REQUIRED_EXIT;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* The CLI the generated workflow step runs: one argument, the payload's path.
|
|
373
|
+
*
|
|
374
|
+
* Returns the exit code rather than calling `process.exit`, so a test can run
|
|
375
|
+
* it in-process as well as spawn it.
|
|
376
|
+
*/
|
|
377
|
+
export function main(argv: string[]): number {
|
|
378
|
+
const path = argv[0];
|
|
379
|
+
const reading =
|
|
380
|
+
path === undefined || path === ""
|
|
381
|
+
? unmeasured(
|
|
382
|
+
["FINDINGS_FILE_ABSENT"],
|
|
383
|
+
["no findings payload path was given to the findings reader"],
|
|
384
|
+
)
|
|
385
|
+
: readAiReviewFindingsFile(path);
|
|
386
|
+
|
|
387
|
+
console.log(`ai-review findings outcome: ${reading.outcome}`);
|
|
388
|
+
|
|
389
|
+
// The ONE place a value reaches a workflow command. Everything upstream of
|
|
390
|
+
// here — `renderFinding`, every `detail` sentence, `aiReviewRefusal` — builds
|
|
391
|
+
// plain text and leaves escaping alone, so there is one binding to mutate and
|
|
392
|
+
// no path where two escapers run in sequence and turn `%` into `%2525`.
|
|
393
|
+
const refusal = aiReviewRefusal(reading);
|
|
394
|
+
if (refusal !== null) {
|
|
395
|
+
console.error(`::error title=${AI_REVIEW_FINDINGS_TITLE}::${escapeAnnotation(refusal)}`);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return aiReviewExitCode(reading);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (import.meta.main) {
|
|
402
|
+
process.exit(main(process.argv.slice(2)));
|
|
403
|
+
}
|