diff --git a/fixtures/ce-run/prose.json b/fixtures/ce-run/prose.json index 785c361..8c66af4 100644 --- a/fixtures/ce-run/prose.json +++ b/fixtures/ce-run/prose.json @@ -31,11 +31,13 @@ "highlight": [11] } ], - "comment": "A zero amount now returns as if it was paid. Could this throw instead?" + "comment": "A zero amount now returns as if it was paid. Could this throw instead?", + "rationale": "Line 11 returns without sending when `amount` is `0`, and the caller reports success." }, "2": { "body": [{ "type": "prose", "text": "The amount goes to the shared log." }], "comment": "Should the amount reach the shared log?", + "rationale": "No verifier checked this finding; it rests on the lens's evidence: `src/pay.ts:12` `log(amount);`", "label": "question" } } diff --git a/scripts/ce-review.test.ts b/scripts/ce-review.test.ts index b9216ff..d11e808 100644 --- a/scripts/ce-review.test.ts +++ b/scripts/ce-review.test.ts @@ -7,7 +7,7 @@ import { test } from "node:test"; import { fileURLToPath } from "node:url"; import type { Finding } from "../contract/index.ts"; import { validateDoc } from "../contract/validate.ts"; -import { badgeMarkdown, ceRunId, convert, convertOffline, parseDiff, type RunFiles } from "./ce-review.ts"; +import { badgeMarkdown, ceRunId, convert, convertOffline, parseDiff, rationales, type RunFiles } from "./ce-review.ts"; const DIFF = `diff --git a/src/pay.ts b/src/pay.ts --- a/src/pay.ts @@ -155,6 +155,47 @@ test("the writer's backticks are kept on a note, and a reworded note is ignored" ); }); +test("the writer's backticks are kept on a verification rationale, and a reworded one is ignored", () => { + const review = { + status: "complete", + findings: [ + upstream(1, "P1", 11, { validation_status: "confirmed", validation_reason: "settings.ts:12 checks enabledAt." }), + upstream(2, "P2", 12, { validation_status: "confirmed", validation_reason: "settings.ts:20 reads retryLimit." }), + upstream(3, "P3", 12), + ], + }; + const unverified = "No verifier checked this finding; it rests on the lens's evidence: "; + const { doc, problems } = run( + { review }, + { + 1: written({ rationale: "`settings.ts:12` checks `enabledAt`." }), + 2: written({ rationale: "`settings.ts:20` ignores `retryLimit`." }), + 3: written({ rationale: `${unverified}\`src/pay.ts:11\` \`if (amount > 0) send(amount);\`` }), + }, + ); + assert.deepEqual(problems, []); + assert.deepEqual( + doc?.findings.map((f) => f.verification.rationale), + [ + "`settings.ts:12` checks `enabledAt`.", + "settings.ts:20 reads retryLimit.", + `${unverified}\`src/pay.ts:11\` \`if (amount > 0) send(amount);\``, + ], + ); +}); + +test("rationales lists the verification rationale the converter generates for each finding", () => { + const review = { + status: "complete", + findings: [upstream(1, "P1", 11), upstream(2, "P2", 12, { validation_status: "unresolved" })], + }; + const verdicts = { verdicts: [{ "#": 1, status: "confirmed", reason: "settings.ts:12 checks enabledAt." }] }; + assert.deepEqual(rationales({ review, verdicts }), { + 1: "settings.ts:12 checks enabledAt.", + 2: "The verifier could neither prove nor refute it.", + }); +}); + // ---- offline mode: no gh, no server; the converter validates and writes the output folder ---- const FIXTURE = fileURLToPath(new URL("../fixtures/ce-run/", import.meta.url)); @@ -201,6 +242,13 @@ test("offline mode writes a pinned, valid findings document and every upstream f ["F2", "unverified", ["security", "correctness"]], ], ); + assert.deepEqual( + doc.findings.map((f: Finding) => f.verification.rationale), + [ + "Line 11 returns without sending when `amount` is `0`, and the caller reports success.", + "No verifier checked this finding; it rests on the lens's evidence: `src/pay.ts:12` `log(amount);`", + ], + ); }); test("offline mode validates the document itself and writes nothing when it fails the contract", () => { diff --git a/scripts/ce-review.ts b/scripts/ce-review.ts index 9aa31d7..bb3ab18 100644 --- a/scripts/ce-review.ts +++ b/scripts/ce-review.ts @@ -8,11 +8,12 @@ // pr.json: { owner, repo, number, url, title, runId, baseSha, headSha } // prose.json: { overview: { lede, stats?, sections }, // notes?: [""], -// findings: { "<#>": { body, comment, label?, blocking?, title?, location?, supportingEvidence? } } } +// findings: { "<#>": { body, comment, rationale?, label?, blocking?, title?, location?, supportingEvidence? } } } // -// `--run-dir --list-notes` prints the review notes as JSON, for the writer -// to mark up. A note in prose.json replaces the generated one only when the two -// are equal once backticks are removed, so marking up cannot change what a note says. +// `--run-dir --list-notes` prints the review notes as JSON, and +// `--run-dir --list-rationales` each finding's verification rationale, for the +// writer to mark up. A note or rationale in prose.json replaces the generated one only +// when the two are equal once backticks are removed, so marking up cannot change what it says. // // Offline mode (skill-ce/SKILL.md): `--out-dir ` in place of `--out` validates the // document against the contract itself and writes the output folder (see convertOffline). @@ -147,6 +148,10 @@ function runNote(review: Json): string { const plain = (text: string) => text.replaceAll("`", ""); +/** The writer's wording when it differs from the generated text only in backticks, else the generated text. */ +const markedUp = (generated: string, written: unknown): string => + typeof written === "string" && plain(written) === plain(generated) ? written : generated; + /** The writer's marked-up wording for each note it left otherwise unchanged. */ function markUp(generated: OverviewNote[], written: unknown): OverviewNote[] { const byPlain = new Map(list(written).flatMap((text) => (typeof text === "string" ? [[plain(text), text]] : []))); @@ -204,6 +209,15 @@ function verification(f: Json, verdict: Json | undefined): Finding["verification }; } +/** The verification rationale the converter generates for each finding, by `#`, for the writer to mark up. */ +export function rationales(run: RunFiles, verdicts: Map = verdictsByNumber(run)): Record { + const out: Record = {}; + for (const f of list(run.review.findings)) { + if (isObj(f) && typeof f["#"] === "number") out[f["#"]] = verification(f, verdicts.get(f["#"])).rationale; + } + return out; +} + export function convert(input: { run: RunFiles; pr: Json; prose: Prose; diff: DiffLines; generatedAt: string }): { doc?: FindingsDoc; problems: string[]; @@ -247,6 +261,7 @@ export function convert(input: { run: RunFiles; pr: Json; prose: Prose; diff: Di const label = (str(written.label) ?? fallback.label) as PullpoLabel; const blocking = label === "issue" ? (typeof written.blocking === "boolean" ? written.blocking : severity === "must-fix") : null; + const checked = verification(raw, verdicts.get(n)); return [ { id: `F${n}`, @@ -257,7 +272,7 @@ export function convert(input: { run: RunFiles; pr: Json; prose: Prose; diff: Di location, body: written.body, supportingEvidence: written.supportingEvidence ?? [], - verification: verification(raw, verdicts.get(n)), + verification: { ...checked, rationale: markedUp(checked.rationale, written.rationale) }, source: { producer: "ce-code-review", severity: raw.severity, @@ -374,6 +389,7 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) { out: { type: "string" }, "out-dir": { type: "string" }, "list-notes": { type: "boolean" }, + "list-rationales": { type: "boolean" }, }, }); const generatedAt = new Date().toISOString().replace(/\.\d+Z$/, "Z"); @@ -381,6 +397,10 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) { console.log(JSON.stringify(notes(readRun(values["run-dir"])), null, 2)); process.exit(0); } + if (values["list-rationales"] && values["run-dir"]) { + console.log(JSON.stringify(rationales(readRun(values["run-dir"])), null, 2)); + process.exit(0); + } if (values["out-dir"] && values["run-dir"]) { const outDir = values["out-dir"]; const { problems, findings } = convertOffline({ ...values, runDir: values["run-dir"], outDir, generatedAt }); diff --git a/skill-ce/SKILL.md b/skill-ce/SKILL.md index 8574b05..8042a0e 100644 --- a/skill-ce/SKILL.md +++ b/skill-ce/SKILL.md @@ -53,7 +53,7 @@ The file has this shape: { "overview": { "lede": "...", "stats": { "files": 0, "additions": 0, "deletions": 0 }, "sections": [] }, "notes": ["..."], - "findings": { "<#>": { "body": [], "comment": "...", "label": "issue", "blocking": true } } + "findings": { "<#>": { "body": [], "comment": "...", "rationale": "...", "label": "issue", "blocking": true } } } ``` @@ -65,6 +65,7 @@ The file has this shape: - `body`: open with the behavior and the concern. Include one `code` block with the code the finding turns on, its enclosing function, and `highlight` on the lines that matter. Put each off-diff definition the claim depends on in a `concept` block where the prose introduces it. On a full run the lens's reasoning is in `RUN_DIR/.json`; a lite or focused run has only the finding's own fields in `review.json`. - `comment`: one to three sentences using [WRITING.md](WRITING.md). No badge; the converter adds it. +- `rationale`: the verifier's explanation that revUI shows in the finding's Verification card. `node /scripts/ce-review.ts --run-dir "$RUN_DIR" --list-rationales` prints it for each `#`. Copy it and wrap file references and identifiers in backticks, as in `` `settings.ts:12` checks `enabledAt`. ``. Change nothing else: the converter keeps the verifier's text when the words differ. - `label` and `blocking` are optional. Left out, `P0` and `P1` become a blocking `issue`, `P2` a non-blocking `issue`, `P3` a `nitpick`. Set them when the comment is really a `question` or a `suggestion`. - `location` is optional: `{ file, line, endLine?, side? }`. Set it only when the converter reports that the upstream line is not in the diff.