Skip to content

Commit de41c7d

Browse files
authored
fix(scripts): release the PR operation lock when review-init metadata fails (#125547)
The lock auto-releases only while the pre-side-effect validation marker is active; review_init marked side effects before the read-only metadata fetch, so a transient GitHub failure retained the lock and forced a lock-recover loop. The read-only fetch now runs before the marker.
1 parent 4ed0196 commit de41c7d

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

‎scripts/pr-lib/review.sh‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,16 @@ review_tests() {
313313

314314
review_init() {
315315
local pr="$1"
316+
local root json pr_url
317+
root=$(repo_root) || return 1
318+
# Metadata reads are read-only, so fetching before the side-effect marker keeps a
319+
# transient GitHub failure inside the lock's auto-release window. Command substitution
320+
# is already a subshell, so this cd gives gh its repo context without moving the
321+
# caller - enter_worktree still reports the real invocation cwd.
322+
json=$(cd "$root" && pr_meta_json "$pr") || return 1
323+
316324
mark_pr_operation_side_effects_started
317325
enter_worktree "$pr" true || return 1
318-
319-
local json pr_url
320-
json=$(pr_meta_json "$pr")
321326
write_pr_meta_files "$json"
322327
pr_url=$(printf '%s\n' "$json" | jq -r .url)
323328

‎test/scripts/pr-operation-lock.test.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ function installPrCliFixture(repoDir: string) {
240240
return { binDir, cli };
241241
}
242242

243+
function installRequiredPrCommandStubs(binDir: string) {
244+
for (const command of ["gh", "jq", "pnpm", "rg"]) {
245+
const stub = join(binDir, command);
246+
writeFileSync(stub, "#!/bin/sh\nexit 0\n");
247+
chmodSync(stub, 0o755);
248+
}
249+
}
250+
243251
interface SupervisedFixtureOptions {
244252
accelerateTimeouts?: boolean;
245253
cwd?: string;
@@ -884,11 +892,7 @@ describePosix("scripts/pr per-PR operation lock", () => {
884892
const { binDir, cli } = installPrCliFixture(repoDir);
885893
const reviewScript = join(repoDir, "scripts/pr-lib/review.sh");
886894
writeFileSync(reviewScript, `${readFileSync(reviewScript, "utf8")}\nreview_init() { :; }\n`);
887-
for (const command of ["gh", "jq", "pnpm", "rg"]) {
888-
const stub = join(binDir, command);
889-
writeFileSync(stub, "#!/bin/sh\nexit 0\n");
890-
chmodSync(stub, 0o755);
891-
}
895+
installRequiredPrCommandStubs(binDir);
892896
const env: NodeJS.ProcessEnv = {
893897
...process.env,
894898
OPENCLAW_PR_DEDICATED_PROCESS_GROUP: "1",
@@ -912,11 +916,7 @@ describePosix("scripts/pr per-PR operation lock", () => {
912916
reviewScript,
913917
`${readFileSync(reviewScript, "utf8")}\nreview_init() { printf 'review ran\\n'; }\n`,
914918
);
915-
for (const command of ["gh", "jq", "pnpm", "rg"]) {
916-
const stub = join(binDir, command);
917-
writeFileSync(stub, "#!/bin/sh\nexit 0\n");
918-
chmodSync(stub, 0o755);
919-
}
919+
installRequiredPrCommandStubs(binDir);
920920
const mktempStub = join(binDir, "mktemp");
921921
writeFileSync(mktempStub, "#!/bin/sh\necho 'mktemp: No space left on device' >&2\nexit 1\n");
922922
chmodSync(mktempStub, 0o755);
@@ -936,6 +936,29 @@ describePosix("scripts/pr per-PR operation lock", () => {
936936
expect(result.stdout).not.toContain("review ran");
937937
expect(refExists(repoDir)).toBe(false);
938938
});
939+
it("releases a validation-phase lock when review-init metadata fails", () => {
940+
const repoDir = createRepo();
941+
const { binDir, cli } = installPrCliFixture(repoDir);
942+
const reviewScript = join(repoDir, "scripts/pr-lib/review.sh");
943+
writeFileSync(
944+
reviewScript,
945+
`${readFileSync(reviewScript, "utf8")}\nenter_worktree() { printf 'entered-worktree\\n'; }\npr_meta_json() { echo 'fixture metadata failure' >&2; return 1; }\n`,
946+
);
947+
installRequiredPrCommandStubs(binDir);
948+
949+
const result = spawnSync(cli, ["review-init", "42"], {
950+
cwd: repoDir,
951+
encoding: "utf8",
952+
env: { ...process.env, PATH: `${binDir}:${process.env.PATH ?? ""}` },
953+
});
954+
955+
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(1);
956+
expect(refExists(repoDir), result.stderr).toBe(false);
957+
expect(result.stderr).toContain("fixture metadata failure");
958+
expect(result.stderr).not.toContain("Retaining the operation lock");
959+
expect(result.stderr).not.toContain("scripts/pr lock-recover");
960+
expect(result.stdout).not.toContain("entered-worktree");
961+
});
939962
it.each([["--dryrun"], ["--dry-run", "extra"]])(
940963
"rejects invalid gc arguments before cleanup: %s",
941964
(...args: string[]) => {

0 commit comments

Comments
 (0)