Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fixtures/ce-run/prose.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
"highlight": [11]
}
],
"comment": "A zero amount now returns as if it was paid. Could this throw instead?"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that providing the rationale is out of scope. What you should have done instead is just replace this comment with its appropriate backticks so that agents know to match the format that you requested.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declined. No agent reads fixtures/ce-run/prose.json: the writer's format example is fixtures/demo-story.findings.json (skill-ce/SKILL.md:48), and comment becomes the suggested comment (scripts/ce-review.ts:283), not the Verification card's verification.rationale (scripts/ce-review.ts:275). The upstream verifier runs unchanged (skill-ce/SKILL.md:8), so the writer's rationale field, checked against the verifier's wording (scripts/ce-review.ts:152), is the smallest change that reaches the generated rationale, and the fixture's two rationale lines are the fictional example the ticket asks for (scripts/ce-review.test.ts:245-251).

"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"
}
}
Expand Down
50 changes: 49 additions & 1 deletion scripts/ce-review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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", () => {
Expand Down
30 changes: 25 additions & 5 deletions scripts/ce-review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
// pr.json: { owner, repo, number, url, title, runId, baseSha, headSha }
// prose.json: { overview: { lede, stats?, sections },
// notes?: ["<a review note with backticks added>"],
// findings: { "<#>": { body, comment, label?, blocking?, title?, location?, supportingEvidence? } } }
// findings: { "<#>": { body, comment, rationale?, label?, blocking?, title?, location?, supportingEvidence? } } }
//
// `--run-dir <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 <dir> --list-notes` prints the review notes as JSON, and
// `--run-dir <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 <dir>` in place of `--out` validates the
// document against the contract itself and writes the output folder (see convertOffline).
Expand Down Expand Up @@ -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]] : [])));
Expand Down Expand Up @@ -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<number, Json> = verdictsByNumber(run)): Record<number, string> {
const out: Record<number, string> = {};
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[];
Expand Down Expand Up @@ -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}`,
Expand All @@ -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,
Expand Down Expand Up @@ -374,13 +389,18 @@ 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");
if (values["list-notes"] && values["run-dir"]) {
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 });
Expand Down
3 changes: 2 additions & 1 deletion skill-ce/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
}
```

Expand All @@ -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/<reviewer>.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 <revui>/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.

Expand Down
Loading