Skip to content

Commit cdcb851

Browse files
authored
fix(apply): tolerate deleted review targets (#409)
Treat confirmed item-level 404s as terminal without masking repository access failures or changing comment-only archival semantics. Add focused live-state regression coverage.
1 parent 5565262 commit cdcb851

2 files changed

Lines changed: 185 additions & 1 deletion

File tree

‎src/clawsweeper.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17554,7 +17554,42 @@ async function applyDecisionsCommand(args: Args): Promise<void> {
1755417554
if (decision === "close" && !isCloseProposal && !shouldProbeClosedState) {
1755517555
continue;
1755617556
}
17557-
const { item, state } = fetchItem(number);
17557+
let liveItem: ReturnType<typeof fetchItem>;
17558+
try {
17559+
liveItem = fetchItem(number);
17560+
} catch (error) {
17561+
if (!isGitHubNotFoundError(error)) throw error;
17562+
// A repository lookup can return the same 404 when the repo is missing or
17563+
// inaccessible. Confirm repo access before treating this as an item miss.
17564+
ghJson<unknown>(["api", `repos/${targetRepo()}`]);
17565+
if (syncCommentsOnly) {
17566+
markApplyChecked();
17567+
results.push({
17568+
number,
17569+
action: "skipped_already_closed",
17570+
reason: "item not found on GitHub",
17571+
});
17572+
processedCount += 1;
17573+
maybeLogProgress(`skipped comment sync #${number}: item not found on GitHub`);
17574+
if (processedCount >= processedLimit) break;
17575+
continue;
17576+
}
17577+
// Items can be deleted after review but before apply. Treat that terminal
17578+
// state like an already-closed item instead of failing the whole apply run.
17579+
markdown = replaceFrontMatterValue(markdown, "action_taken", "skipped_already_closed");
17580+
markdown = replaceFrontMatterValue(markdown, "apply_checked_at", new Date().toISOString());
17581+
archiveClosed(markdown);
17582+
results.push({
17583+
number,
17584+
action: "skipped_already_closed",
17585+
reason: "item not found on GitHub",
17586+
});
17587+
processedCount += 1;
17588+
maybeLogProgress(`archived #${number}: item not found on GitHub`);
17589+
if (processedCount >= processedLimit) break;
17590+
continue;
17591+
}
17592+
const { item, state } = liveItem;
1755817593
const previousLabels = [...item.labels];
1755917594
let currentContext: ItemContext | undefined;
1756017595
let currentClosingPullRequests: unknown[] | undefined;

‎test/apply-live-state.test.ts‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import assert from "node:assert/strict";
2+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
import test from "node:test";
5+
6+
import {
7+
implementedCloseReport,
8+
readText,
9+
runApplyDecisionsForTest,
10+
tmpPrefix,
11+
withMockGh,
12+
} from "./helpers.ts";
13+
14+
test("apply-decisions archives records deleted after review instead of failing the run", () => {
15+
const root = mkdtempSync(tmpPrefix);
16+
try {
17+
const itemsDir = join(root, "items");
18+
const closedDir = join(root, "closed");
19+
const plansDir = join(root, "plans");
20+
const reportPath = join(root, "apply-report.json");
21+
mkdirSync(itemsDir, { recursive: true });
22+
mkdirSync(plansDir, { recursive: true });
23+
writeFileSync(
24+
join(itemsDir, "321.md"),
25+
implementedCloseReport({ action_taken: "proposed_close" }),
26+
"utf8",
27+
);
28+
29+
const ghMock = `
30+
const rawArgs = process.argv.slice(2);
31+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
32+
const path = args[1] || "";
33+
if (args[0] === "api" && /\\/issues\\/321$/.test(path)) {
34+
console.error("gh: Not Found (HTTP 404)");
35+
process.exit(1);
36+
}
37+
if (args[0] === "api" && path === "repos/openclaw/clawsweeper") {
38+
console.log(JSON.stringify({ full_name: "openclaw/clawsweeper" }));
39+
process.exit(0);
40+
}
41+
console.error("unexpected gh args", JSON.stringify(args));
42+
process.exit(1);
43+
`;
44+
withMockGh(root, ghMock, () => {
45+
runApplyDecisionsForTest({ itemsDir, closedDir, plansDir, reportPath });
46+
});
47+
48+
assert.equal(existsSync(join(itemsDir, "321.md")), false);
49+
assert.ok(existsSync(join(closedDir, "321.md")));
50+
assert.match(readText(join(closedDir, "321.md")), /^action_taken: skipped_already_closed$/m);
51+
assert.deepEqual(JSON.parse(readText(reportPath)), [
52+
{
53+
number: 321,
54+
action: "skipped_already_closed",
55+
reason: "item not found on GitHub",
56+
},
57+
]);
58+
} finally {
59+
rmSync(root, { recursive: true, force: true });
60+
}
61+
});
62+
63+
test("apply-decisions keeps missing records queued during comment-only sync", () => {
64+
const root = mkdtempSync(tmpPrefix);
65+
try {
66+
const itemsDir = join(root, "items");
67+
const closedDir = join(root, "closed");
68+
const plansDir = join(root, "plans");
69+
const reportPath = join(root, "apply-report.json");
70+
mkdirSync(itemsDir, { recursive: true });
71+
mkdirSync(plansDir, { recursive: true });
72+
writeFileSync(join(itemsDir, "321.md"), implementedCloseReport(), "utf8");
73+
74+
const ghMock = `
75+
const rawArgs = process.argv.slice(2);
76+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
77+
const path = args[1] || "";
78+
if (args[0] === "api" && /\\/issues\\/321$/.test(path)) {
79+
console.error("gh: Not Found (HTTP 404)");
80+
process.exit(1);
81+
}
82+
if (args[0] === "api" && path === "repos/openclaw/clawsweeper") {
83+
console.log(JSON.stringify({ full_name: "openclaw/clawsweeper" }));
84+
process.exit(0);
85+
}
86+
console.error("unexpected gh args", JSON.stringify(args));
87+
process.exit(1);
88+
`;
89+
withMockGh(root, ghMock, () => {
90+
runApplyDecisionsForTest({
91+
itemsDir,
92+
closedDir,
93+
plansDir,
94+
reportPath,
95+
extraArgs: ["--sync-comments-only"],
96+
});
97+
});
98+
99+
assert.ok(existsSync(join(itemsDir, "321.md")));
100+
assert.equal(existsSync(join(closedDir, "321.md")), false);
101+
assert.match(readText(join(itemsDir, "321.md")), /^action_taken: proposed_close$/m);
102+
assert.deepEqual(JSON.parse(readText(reportPath)), [
103+
{
104+
number: 321,
105+
action: "skipped_already_closed",
106+
reason: "item not found on GitHub",
107+
},
108+
]);
109+
} finally {
110+
rmSync(root, { recursive: true, force: true });
111+
}
112+
});
113+
114+
test("apply-decisions fails safely when a missing repository also returns 404", () => {
115+
const root = mkdtempSync(tmpPrefix);
116+
try {
117+
const itemsDir = join(root, "items");
118+
const closedDir = join(root, "closed");
119+
const plansDir = join(root, "plans");
120+
const reportPath = join(root, "apply-report.json");
121+
mkdirSync(itemsDir, { recursive: true });
122+
mkdirSync(plansDir, { recursive: true });
123+
writeFileSync(join(itemsDir, "321.md"), implementedCloseReport(), "utf8");
124+
125+
const ghMock = `
126+
const rawArgs = process.argv.slice(2);
127+
const args = rawArgs[0] === "--repo" ? rawArgs.slice(2) : rawArgs;
128+
const path = args[1] || "";
129+
if (args[0] === "api" && (/\\/issues\\/321$/.test(path) || path === "repos/openclaw/clawsweeper")) {
130+
console.error("gh: Not Found (HTTP 404)");
131+
process.exit(1);
132+
}
133+
console.error("unexpected gh args", JSON.stringify(args));
134+
process.exit(1);
135+
`;
136+
assert.throws(
137+
() =>
138+
withMockGh(root, ghMock, () => {
139+
runApplyDecisionsForTest({ itemsDir, closedDir, plansDir, reportPath });
140+
}),
141+
/Not Found/,
142+
);
143+
144+
assert.ok(existsSync(join(itemsDir, "321.md")));
145+
assert.equal(existsSync(join(closedDir, "321.md")), false);
146+
} finally {
147+
rmSync(root, { recursive: true, force: true });
148+
}
149+
});

0 commit comments

Comments
 (0)