Skip to content

Commit 3630fff

Browse files
committed
fix(ci): preserve primary result on ledger flush failure
1 parent 674c0f8 commit 3630fff

2 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎src/clawsweeper.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30307,16 +30307,31 @@ function publishActionEventPathsCommand(args: Args): void {
3030730307
console.log(JSON.stringify({ result, path_count: paths.length }));
3030830308
}
3030930309

30310-
export async function main(argv = process.argv.slice(2)): Promise<void> {
30310+
function isExplicitActionLedgerCommand(command: string): boolean {
30311+
return (
30312+
command === "finalize-action-events" ||
30313+
command === "publish-action-events" ||
30314+
command === "publish-action-event-paths"
30315+
);
30316+
}
30317+
30318+
export async function main(
30319+
argv = process.argv.slice(2),
30320+
dependencies: {
30321+
flushWorkflowActionEvents?: typeof flushWorkflowActionEvents;
30322+
} = {},
30323+
): Promise<void> {
3031130324
const args = parseArgs(argv);
3031230325
const command = args._[0] ?? "review";
30326+
const flushActionEvents = dependencies.flushWorkflowActionEvents ?? flushWorkflowActionEvents;
3031330327
if (!process.env.CLAWSWEEPER_ACTION_LEDGER_INVOCATION) {
3031430328
process.env.CLAWSWEEPER_ACTION_LEDGER_INVOCATION = sha256(stableJson({ command, args })).slice(
3031530329
0,
3031630330
16,
3031730331
);
3031830332
}
30319-
let commandError: unknown = null;
30333+
let commandFailed = false;
30334+
let commandError: unknown;
3032030335
try {
3032130336
if (command === "plan") planCommand(args);
3032230337
else if (command === "review") reviewCommand(args);
@@ -30366,11 +30381,11 @@ export async function main(argv = process.argv.slice(2)): Promise<void> {
3036630381
}
3036730382
} else throw new UserFacingCommandError(`Unknown command: ${command}`);
3036830383
} catch (error) {
30384+
commandFailed = true;
3036930385
commandError = error;
3037030386
}
30371-
if (command === "finalize-action-events" && commandError) throw commandError;
3037230387
try {
30373-
const shardPaths = await flushWorkflowActionEvents(ROOT);
30388+
const shardPaths = await flushActionEvents(ROOT);
3037430389
if (shardPaths.length > 0) {
3037530390
console.error(
3037630391
`[action-ledger] finalized ${shardPaths.length} immutable workflow shard${
@@ -30379,17 +30394,24 @@ export async function main(argv = process.argv.slice(2)): Promise<void> {
3037930394
);
3038030395
}
3038130396
} catch (error) {
30382-
if (commandError) {
30397+
if (commandFailed) {
3038330398
console.error(
30384-
`[action-ledger] finalization failed after command failure: ${
30399+
`[action-ledger] best-effort finalization failed after command failure: ${
3038530400
error instanceof Error ? error.message : String(error)
3038630401
}`,
3038730402
);
30388-
} else {
30403+
} else if (isExplicitActionLedgerCommand(command)) {
30404+
commandFailed = true;
3038930405
commandError = error;
30406+
} else {
30407+
console.error(
30408+
`[action-ledger] best-effort finalization failed after successful ${command}: ${
30409+
error instanceof Error ? error.message : String(error)
30410+
}`,
30411+
);
3039030412
}
3039130413
}
30392-
if (commandError) throw commandError;
30414+
if (commandFailed) throw commandError;
3039330415
}
3039430416

3039530417
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {

‎test/clawsweeper-action-ledger.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
codexReviewFailureRetryableForTest,
1515
heldReviewStartStatusCommentResultForTest,
1616
isGitHubLabelAlreadyExistsErrorForTest,
17+
main,
1718
observedGitHubMutationAttemptsForTest,
1819
reviewCommentPublicationEventDisposition,
1920
reviewRetryActionDisposition,
@@ -24,6 +25,68 @@ import {
2425
import { actionIdempotencyKey } from "../dist/action-ledger.js";
2526
import { readText } from "./helpers.ts";
2627

28+
test("primary command success survives best-effort action ledger flush failure", async (t) => {
29+
const errors: string[] = [];
30+
let flushCalls = 0;
31+
t.mock.method(console, "error", (...args: unknown[]) => {
32+
errors.push(args.map(String).join(" "));
33+
});
34+
35+
await main(["check"], {
36+
flushWorkflowActionEvents: async () => {
37+
flushCalls += 1;
38+
throw new Error("simulated flush failure");
39+
},
40+
});
41+
42+
assert.equal(flushCalls, 1);
43+
assert.match(
44+
errors.join("\n"),
45+
/\[action-ledger\] best-effort finalization failed after successful check: simulated flush failure/,
46+
);
47+
});
48+
49+
test("primary command failure is not masked by action ledger flush failure", async (t) => {
50+
const errors: string[] = [];
51+
let flushCalls = 0;
52+
t.mock.method(console, "error", (...args: unknown[]) => {
53+
errors.push(args.map(String).join(" "));
54+
});
55+
56+
await assert.rejects(
57+
main(["unknown-primary-command"], {
58+
flushWorkflowActionEvents: async () => {
59+
flushCalls += 1;
60+
throw new Error("simulated flush failure");
61+
},
62+
}),
63+
(error: unknown) => {
64+
assert.equal(
65+
error instanceof Error ? error.message : String(error),
66+
"Unknown command: unknown-primary-command",
67+
);
68+
return true;
69+
},
70+
);
71+
72+
assert.equal(flushCalls, 1);
73+
assert.match(
74+
errors.join("\n"),
75+
/\[action-ledger\] best-effort finalization failed after command failure: simulated flush failure/,
76+
);
77+
});
78+
79+
test("explicit action ledger finalization keeps flush failure strict", async () => {
80+
await assert.rejects(
81+
main(["finalize-action-events"], {
82+
flushWorkflowActionEvents: async () => {
83+
throw new Error("simulated flush failure");
84+
},
85+
}),
86+
/simulated flush failure/,
87+
);
88+
});
89+
2790
test("review and apply outcome classifiers cover terminal and resumable states", () => {
2891
assert.deepEqual(actionLedgerFailureDisposition(new Error("worker timed out after 30s")), {
2992
status: "failed",

0 commit comments

Comments
 (0)