Skip to content

Commit 537fa69

Browse files
committed
fix(pr): bind review artifacts to the PR being landed
scripts/pr review artifacts carried no PR identity, so a review authored for another PR validated clean and could gate someone else's landing. Stamp both review.json and review.md with the reviewed PR number and head, anchor pr-meta.json to the guarded pr-meta.env, and re-stamp foreign artifacts on init instead of silently adopting them. review-claim now works in the PR worktree so no subcommand writes review state to the shared canonical checkout.
1 parent b4e246b commit 537fa69

3 files changed

Lines changed: 365 additions & 45 deletions

File tree

‎scripts/pr-lib/review-artifacts.mjs‎

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,44 @@ function reviewArtifactEnumHint(enumName, initialValue) {
2929
return `${initialValue} (allowed: ${allowed.join("|")})`;
3030
}
3131

32-
function createReviewArtifactTemplate() {
32+
// Both artifacts carry the same stamp: review.md holds the prose verdict a human
33+
// reads, so binding only review.json would still let a foreign markdown verdict
34+
// ride along with a freshly stamped JSON.
35+
function reviewIdentityLine({ number, headSha }) {
36+
return `Review artifact for PR #${number} at ${headSha}`;
37+
}
38+
39+
function createReviewMarkdownTemplate(identity) {
40+
return `${reviewIdentityLine(identity)}
41+
42+
A) TL;DR recommendation
43+
44+
B) What changed and what is good?
45+
46+
C) Security findings
47+
48+
D) What is the PR intent? Is this the most optimal implementation?
49+
50+
E) Concerns or questions (actionable)
51+
52+
F) Tests
53+
54+
G) Docs status
55+
56+
H) Changelog
57+
58+
I) Follow ups (optional)
59+
60+
J) Suggested PR comment (optional)
61+
`;
62+
}
63+
64+
function createReviewArtifactTemplate({ number, headSha }) {
3365
return {
66+
// Identity stamp, not reviewer input: validation refuses artifacts whose pr
67+
// disagrees with .local/pr-meta.json, so a review written for another PR (or
68+
// an already-superseded head) can never be landed as this one's verdict.
69+
pr: { number, headSha },
3470
recommendation: reviewArtifactEnumHint("recommendation", "NEEDS WORK"),
3571
findings: [],
3672
nitSweep: {
@@ -100,6 +136,50 @@ function validateReviewArtifacts({ review, reviewMarkdown, prMeta }) {
100136
"Invalid .local/review.json: top-level value must be an object",
101137
);
102138
const value = reviewIsObject ? review : {};
139+
140+
const prMetaIsValid =
141+
isObject(prMeta) &&
142+
Array.isArray(prMeta.files) &&
143+
prMeta.files.every((file) => isObject(file) && typeof file.path === "string");
144+
if (!prMetaIsValid) {
145+
add("Invalid .local/pr-meta.json: files must be an array of objects with string path");
146+
}
147+
const prMetaIdentifiesHead =
148+
isObject(prMeta) && Number.isInteger(prMeta.number) && typeof prMeta.headRefOid === "string";
149+
if (!prMetaIdentifiesHead) {
150+
add(
151+
"Invalid .local/pr-meta.json: number and headRefOid must identify the reviewed PR head; re-run scripts/pr review-init",
152+
);
153+
}
154+
const stamp = isObject(value.pr) ? value.pr : undefined;
155+
const stampIsValid =
156+
stamp !== undefined && Number.isInteger(stamp.number) && typeof stamp.headSha === "string";
157+
if (!stampIsValid) {
158+
add(
159+
"Invalid PR identity in .local/review.json: pr.number must be an integer and pr.headSha must be a string; re-run scripts/pr review-artifacts-init",
160+
);
161+
}
162+
if (
163+
stampIsValid &&
164+
prMetaIdentifiesHead &&
165+
(stamp.number !== prMeta.number || stamp.headSha !== prMeta.headRefOid)
166+
) {
167+
add(
168+
`Review artifact identity mismatch in .local/review.json: authored for PR #${stamp.number} at ${stamp.headSha}, but .local/pr-meta.json describes PR #${prMeta.number} at ${prMeta.headRefOid}; re-run scripts/pr review-artifacts-init`,
169+
);
170+
}
171+
if (prMetaIdentifiesHead) {
172+
const expectedLine = reviewIdentityLine({
173+
number: prMeta.number,
174+
headSha: prMeta.headRefOid,
175+
});
176+
if (reviewMarkdown.split("\n")[0] !== expectedLine) {
177+
add(
178+
`Review artifact identity mismatch in .local/review.md: first line must be ${jsonValue(expectedLine)}; re-run scripts/pr review-artifacts-init`,
179+
);
180+
}
181+
}
182+
103183
const recommendationIsString = requireType(
104184
typeof value.recommendation === "string",
105185
"Invalid recommendation in .local/review.json: recommendation must be a string",
@@ -259,13 +339,6 @@ function validateReviewArtifacts({ review, reviewMarkdown, prMeta }) {
259339
);
260340
}
261341

262-
const prMetaIsValid =
263-
isObject(prMeta) &&
264-
Array.isArray(prMeta.files) &&
265-
prMeta.files.every((file) => isObject(file) && typeof file.path === "string");
266-
if (!prMetaIsValid) {
267-
add("Invalid .local/pr-meta.json: files must be an array of objects with string path");
268-
}
269342
const runtimeFileCount = prMetaIsValid
270343
? prMeta.files.filter(
271344
({ path }) =>
@@ -462,8 +535,21 @@ function readJson(filePath) {
462535

463536
function main(argv = process.argv.slice(2)) {
464537
const [command, ...args] = argv;
465-
if (command === "template" && args.length === 0) {
466-
process.stdout.write(`${JSON.stringify(createReviewArtifactTemplate(), null, 2)}\n`);
538+
if ((command === "template" || command === "markdown") && args.length === 2) {
539+
const [prNumber, headSha] = args;
540+
if (!/^[1-9][0-9]*$/u.test(prNumber) || !/^[0-9a-f]{40}$/u.test(headSha)) {
541+
console.error(
542+
`Usage: review-artifacts.mjs ${command} <pr-number> <head-sha>; pr-number must be a positive integer and head-sha a 40-character lowercase hex commit id.`,
543+
);
544+
process.exitCode = 2;
545+
return;
546+
}
547+
const identity = { number: Number(prNumber), headSha };
548+
process.stdout.write(
549+
command === "markdown"
550+
? createReviewMarkdownTemplate(identity)
551+
: `${JSON.stringify(createReviewArtifactTemplate(identity), null, 2)}\n`,
552+
);
467553
return;
468554
}
469555
if (command === "validate" && args.length === 3) {
@@ -483,7 +569,7 @@ function main(argv = process.argv.slice(2)) {
483569
return;
484570
}
485571
console.error(
486-
"Usage: review-artifacts.mjs template | validate <review.json> <review.md> <pr-meta.json>",
572+
"Usage: review-artifacts.mjs template|markdown <pr-number> <head-sha> | validate <review.json> <review.md> <pr-meta.json>",
487573
);
488574
process.exitCode = 2;
489575
}

‎scripts/pr-lib/review.sh‎

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ review_artifacts_helper_path() {
1818
review_claim() {
1919
local pr="$1"
2020
mark_pr_operation_side_effects_started
21-
local root
22-
root=$(repo_root)
23-
cd "$root"
24-
mkdir -p .local
21+
# Claim logs are per-PR review state: keeping them in the PR worktree leaves the
22+
# shared canonical checkout with no scripts/pr-owned .local, so a stray artifact
23+
# there can never be mistaken for this flow's output. Claiming still works on a
24+
# cold PR because enter_worktree provisions both the worktree and .local.
25+
enter_worktree "$pr" false
2526

2627
local reviewer=""
2728
local max_attempts=3
@@ -107,6 +108,11 @@ review_guard() {
107108
# shellcheck disable=SC1091
108109
source .local/pr-meta.env
109110

111+
if [ "${PR_NUMBER:-}" != "$pr" ]; then
112+
echo "Review guard failed: .local/pr-meta.env describes PR #${PR_NUMBER:-unknown}, not #$pr. Re-run: scripts/pr review-init $pr"
113+
exit 1
114+
fi
115+
110116
local branch
111117
branch=$(git branch --show-current)
112118
local head_sha
@@ -147,42 +153,74 @@ review_artifacts_init() {
147153
local pr="$1"
148154
enter_worktree "$pr" false
149155
require_artifact .local/pr-meta.env
156+
require_artifact .local/pr-meta.json
150157

151158
mark_pr_operation_side_effects_started
152159

153-
if [ ! -f .local/review.md ]; then
154-
cat > .local/review.md <<'EOF_MD'
155-
A) TL;DR recommendation
156-
157-
B) What changed and what is good?
158-
159-
C) Security findings
160-
161-
D) What is the PR intent? Is this the most optimal implementation?
162-
163-
E) Concerns or questions (actionable)
164-
165-
F) Tests
166-
167-
G) Docs status
168-
169-
H) Changelog
160+
local meta_number head_sha
161+
meta_number=$(jq -r '.number' .local/pr-meta.json)
162+
head_sha=$(jq -r '.headRefOid' .local/pr-meta.json)
163+
if [ "$meta_number" != "$pr" ] || ! printf '%s' "$head_sha" | rg -q '^[0-9a-f]{40}$'; then
164+
echo "Review artifacts init failed: .local/pr-meta.json describes PR #$meta_number at '$head_sha', not PR #$pr. Re-run: scripts/pr review-init $pr"
165+
exit 1
166+
fi
170167

171-
I) Follow ups (optional)
168+
# Take the first line in the shell, not through `head`: pipefail turns the
169+
# helper's EPIPE into a spurious failure once the template outgrows the pipe.
170+
local identity_line
171+
identity_line=$(node "$(review_artifacts_helper_path)" markdown "$meta_number" "$head_sha")
172+
identity_line=${identity_line%%$'\n'*}
172173

173-
J) Suggested PR comment (optional)
174-
EOF_MD
174+
if [ -f .local/review.json ] && [ -f .local/review.md ] &&
175+
jq -e --argjson number "$meta_number" --arg head "$head_sha" \
176+
'.pr.number == $number and .pr.headSha == $head' .local/review.json >/dev/null 2>&1 &&
177+
[ "$(head -n1 .local/review.md)" = "$identity_line" ]
178+
then
179+
echo "review artifacts already stamped for PR #$meta_number at $head_sha"
180+
echo "files=.local/review.md .local/review.json"
181+
return 0
175182
fi
176183

177-
if [ ! -f .local/review.json ]; then
178-
node "$(review_artifacts_helper_path)" template > .local/review.json
179-
fi
184+
# Artifacts on disk were authored for another PR or a superseded head. Keep them
185+
# instead of deleting: a mid-review head change is legitimate and the prose is
186+
# worth salvaging, but only a freshly stamped template may gate this landing.
187+
# mktemp -d allocates the archive slot atomically so a retry, a concurrent init,
188+
# or a repeated clock second cannot overwrite an earlier preserved review.
189+
local superseded_dir="" ext
190+
for ext in json md; do
191+
[ -f ".local/review.$ext" ] || continue
192+
if [ -z "$superseded_dir" ]; then
193+
mkdir -p .local/superseded
194+
superseded_dir=$(mktemp -d ".local/superseded/$(date -u +%Y%m%dT%H%M%SZ)-XXXXXX")
195+
fi
196+
mv ".local/review.$ext" "$superseded_dir/review.$ext"
197+
echo "moved aside .local/review.$ext -> $superseded_dir/review.$ext (not authored for PR #$meta_number at $head_sha)"
198+
done
199+
200+
node "$(review_artifacts_helper_path)" markdown "$meta_number" "$head_sha" > .local/review.md
201+
node "$(review_artifacts_helper_path)" template "$meta_number" "$head_sha" > .local/review.json
180202

181203
echo "review artifact templates are ready"
182204
echo "files=.local/review.md .local/review.json"
183205
}
184206

185207
validate_review_artifact_data() {
208+
# pr-meta.json is the identity authority the review artifacts are stamped against,
209+
# so it must itself be anchored: review_guard binds pr-meta.env to the guarded PR
210+
# and the checked-out head, and this ties pr-meta.json to pr-meta.env. Without it a
211+
# wholly foreign but self-consistent .local set still gates the landing.
212+
local meta_number meta_head
213+
meta_number=$(jq -r '.number' .local/pr-meta.json)
214+
meta_head=$(jq -r '.headRefOid' .local/pr-meta.json)
215+
if ! (
216+
# shellcheck disable=SC1091
217+
source .local/pr-meta.env
218+
[ "$meta_number" = "${PR_NUMBER:-}" ] && [ "$meta_head" = "${PR_HEAD_SHA:-}" ]
219+
); then
220+
echo "Review artifact identity mismatch: .local/pr-meta.json describes PR #$meta_number at $meta_head, which does not match .local/pr-meta.env. Re-run: scripts/pr review-init"
221+
return 1
222+
fi
223+
186224
if ! node "$(review_artifacts_helper_path)" validate \
187225
.local/review.json \
188226
.local/review.md \

0 commit comments

Comments
 (0)