Skip to content

Commit ce054cf

Browse files
vincentkocsteipete
authored andcommitted
fix(repair): surface post-flight retries
1 parent 4b83563 commit ce054cf

10 files changed

Lines changed: 133 additions & 37 deletions

‎.github/workflows/repair-cluster-worker.yml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ jobs:
975975
- name: Post-flight finalize fix PRs
976976
id: post_flight
977977
if: ${{ success() && steps.check_job.outputs.job_exists == '1' && env.CLAWSWEEPER_ALLOW_EXECUTE == '1' && env.CLAWSWEEPER_ALLOW_FIX_PR == '1' }}
978+
continue-on-error: true
978979
env:
979980
GH_TOKEN: ${{ steps.target_post_flight_token.outputs.token }}
980981
CLAWSWEEPER_AUTHENTICATED_APP_ID: ${{ steps.mutation_app.outputs.app_id }}
@@ -1044,6 +1045,15 @@ jobs:
10441045
if-no-files-found: warn
10451046

10461047
- name: Record work failure
1047-
if: ${{ always() && (failure() || steps.post_flight.outputs.report_outcome == 'blocked' || steps.post_flight.outputs.report_outcome == 'requeue') && steps.crabfleet_session.outcome == 'success' }}
1048+
if: ${{ always() && (failure() || steps.post_flight.outcome == 'failure' || steps.post_flight.outputs.report_outcome == 'blocked' || steps.post_flight.outputs.report_outcome == 'requeue') && steps.crabfleet_session.outcome == 'success' }}
10481049
continue-on-error: true
10491050
run: pnpm run repair:action-session -- update --state blocked --phase action_failed --summary "Repair Action failed before all completion gates passed" --completion-reason action_failed
1051+
1052+
- name: Fail incomplete post-flight result
1053+
if: ${{ always() && (steps.post_flight.outcome == 'failure' || steps.post_flight.outputs.report_outcome != 'success') }}
1054+
env:
1055+
POST_FLIGHT_OUTCOME: ${{ steps.post_flight.outputs.report_outcome }}
1056+
POST_FLIGHT_DETAIL: ${{ steps.post_flight.outputs.report_detail }}
1057+
run: |
1058+
echo "post-flight did not complete: ${POST_FLIGHT_OUTCOME:-missing}: ${POST_FLIGHT_DETAIL:-no report detail}" >&2
1059+
exit 1

‎src/repair/post-flight-report.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export function summarizePostFlightReport(report: LooseRecord): PostFlightReport
103103
};
104104
}
105105

106+
export function postFlightOutcomeExitCode(outcome: PostFlightReportOutcome): number {
107+
return outcome === "success" ? 0 : 1;
108+
}
109+
106110
function actionDetail(action: JsonValue, fallback: string): string {
107111
const reason = compactOutput(String(action?.reason ?? ""));
108112
const name = compactOutput(String(action?.action ?? ""));

‎src/repair/post-flight.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { serverStrictBaseBindingBlock } from "./strict-base-binding.js";
3636
import { compactText as compactPlainText } from "./text-utils.js";
3737
import { verifyPublishedReceipt } from "./execution-handoff.js";
3838
import {
39+
postFlightOutcomeExitCode,
3940
publicationOnlyPostFlightAction,
4041
shouldFinalizePublicationOnlyPostFlight,
4142
summarizePostFlightReport,
@@ -126,8 +127,7 @@ if (!fixReport) {
126127
status: "skipped",
127128
reason: "no fix-execution-report.json",
128129
});
129-
writeReport(report, resultPath);
130-
process.exit(0);
130+
process.exit(postFlightOutcomeExitCode(writeReport(report, resultPath).outcome));
131131
}
132132

133133
const fixActions = publicationReceipt
@@ -150,7 +150,7 @@ if (report.actions.length === 0) {
150150
});
151151
}
152152

153-
writeReport(report, resultPath);
153+
process.exitCode = postFlightOutcomeExitCode(writeReport(report, resultPath).outcome);
154154

155155
function finalizeFixPr(action: LooseRecord) {
156156
const base = {
@@ -981,6 +981,7 @@ function writeReport(report: LooseRecord, resultPath: string) {
981981
);
982982
}
983983
console.log(JSON.stringify(finalReport, null, 2));
984+
return summary;
984985
}
985986

986987
function normalizeIssueRef(value: JsonValue) {

‎src/repair/publish-result.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ function publishResult(resultPath: string) {
106106
head_sha: headSha || null,
107107
workflow_conclusion: workflowConclusion || null,
108108
workflow_status: workflowStatus || null,
109+
post_flight_outcome: postFlightReport.outcome ?? null,
110+
post_flight_detail: postFlightReport.detail ?? null,
109111
workflow_created_at:
110112
metadata?.createdAt ?? metadata?.created_at ?? previousRecord?.workflow_created_at ?? null,
111113
workflow_updated_at:

‎src/repair/self-heal-failed-runs.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { ghErrorText, ghJson, ghText } from "./github-cli.js";
1717
import { sleepMs } from "./timing.js";
1818
import { REPAIR_CLUSTER_WORKFLOW } from "./constants.js";
19+
import { shouldSelfHealRunRecord } from "./self-heal-policy.js";
1920

2021
const DEFAULT_REPO = currentProjectRepo();
2122
const DEFAULT_WORKFLOW = REPAIR_CLUSTER_WORKFLOW;
@@ -184,7 +185,7 @@ function selectCandidates() {
184185
}
185186

186187
return [...latestByJob.values()]
187-
.filter((record: JsonValue) => record.workflow_conclusion === "failure")
188+
.filter((record: JsonValue) => shouldSelfHealRunRecord(record))
188189
.filter((record: JsonValue) => {
189190
const timestamp = recordTimestampMs(record);
190191
if (timestamp >= cutoffMs) return true;

‎src/repair/self-heal-policy.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { LooseRecord } from "./json-types.js";
2+
3+
export function shouldSelfHealRunRecord(record: LooseRecord): boolean {
4+
const postFlightOutcome = String(record.post_flight_outcome ?? "");
5+
if (postFlightOutcome === "requeue") return true;
6+
if (postFlightOutcome === "blocked") return false;
7+
return record.workflow_conclusion === "failure";
8+
}

‎test/repair/issue-implementation-status.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ test("issue build workflow reports an opened PR without calling pending CI block
8181
workflow,
8282
/steps\.post_flight\.outputs\.report_outcome == 'success'[\s\S]*completion-reason gates_passed/,
8383
);
84+
assert.match(
85+
workflow,
86+
/name: Post-flight finalize fix PRs[\s\S]*continue-on-error: true[\s\S]*name: Fail incomplete post-flight result/,
87+
);
88+
assert.match(
89+
workflow,
90+
/steps\.post_flight\.outcome == 'failure' \|\| steps\.post_flight\.outputs\.report_outcome != 'success'/,
91+
);
8492
assert.match(workflow, /The exact independently validated repair was published at/);
8593
assert.doesNotMatch(
8694
workflow,

‎test/repair/post-flight-report.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from "node:test";
33

44
import {
55
isPublicationOnlyPostFlightJob,
6+
postFlightOutcomeExitCode,
67
publicationOnlyPostFlightAction,
78
shouldFinalizePublicationOnlyPostFlight,
89
summarizePostFlightReport,
@@ -137,3 +138,9 @@ test("post-flight report requests requeue only when every incomplete action is r
137138
"blocked",
138139
);
139140
});
141+
142+
test("post-flight exits nonzero for blocked and requeue reports", () => {
143+
assert.equal(postFlightOutcomeExitCode("success"), 0);
144+
assert.equal(postFlightOutcomeExitCode("blocked"), 1);
145+
assert.equal(postFlightOutcomeExitCode("requeue"), 1);
146+
});

‎test/repair/post-flight.test.ts‎

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert/strict";
2-
import { execFileSync } from "node:child_process";
2+
import { execFileSync, spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import os from "node:os";
55
import path from "node:path";
@@ -332,6 +332,10 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
332332
" process.exit(0);",
333333
"}",
334334
"if (args[0] === 'pr' && args[1] === 'merge') {",
335+
" if (process.env.FAKE_GH_MERGE_RACE === '1') {",
336+
" process.stderr.write('base branch was modified; review and try the merge again\\n');",
337+
" process.exit(1);",
338+
" }",
335339
" fs.writeFileSync(process.env.FAKE_GH_MERGED_FILE, '1');",
336340
" process.exit(0);",
337341
"}",
@@ -382,11 +386,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
382386
const alreadyMergedReport = JSON.parse(fs.readFileSync(alreadyMergedPath, "utf8"));
383387
alreadyMergedReport.actions[0].commit = "d".repeat(40);
384388
fs.writeFileSync(alreadyMergedPath, JSON.stringify(alreadyMergedReport, null, 2));
385-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
386-
cwd: repoRoot,
387-
env,
388-
stdio: "pipe",
389-
});
389+
runPostFlight(jobPath, resultPath, env, 1);
390390
const alreadyMerged = JSON.parse(fs.readFileSync(reportPath, "utf8"));
391391
assert.equal(alreadyMerged.actions[0]?.status, "blocked");
392392
assert.equal(
@@ -405,11 +405,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
405405
staleAfterMerged.actions[0].merge_preflight.validated_head_sha = staleHead;
406406
fs.writeFileSync(fixReportPath, JSON.stringify(staleAfterMerged, null, 2));
407407

408-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
409-
cwd: repoRoot,
410-
env,
411-
stdio: "pipe",
412-
});
408+
runPostFlight(jobPath, resultPath, env, 1);
413409
const stalePostFlight = JSON.parse(fs.readFileSync(reportPath, "utf8"));
414410
assert.equal(stalePostFlight.actions[0]?.status, "blocked");
415411
assert.equal(
@@ -429,11 +425,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
429425
fs.writeFileSync(fixReportPath, JSON.stringify(staleBaseReport, null, 2));
430426
fs.rmSync(reportPath, { force: true });
431427
fs.rmSync(viewCountPath, { force: true });
432-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
433-
cwd: repoRoot,
434-
env,
435-
stdio: "pipe",
436-
});
428+
runPostFlight(jobPath, resultPath, env, 1);
437429
const staleBasePostFlight = JSON.parse(fs.readFileSync(reportPath, "utf8"));
438430
assert.equal(staleBasePostFlight.actions[0]?.status, "blocked");
439431
assert.equal(
@@ -445,11 +437,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
445437
writeMergeReports(runDir, resultPath);
446438
fs.rmSync(reportPath, { force: true });
447439
fs.rmSync(viewCountPath, { force: true });
448-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
449-
cwd: repoRoot,
450-
env,
451-
stdio: "pipe",
452-
});
440+
runPostFlight(jobPath, resultPath, env, 1);
453441
const unboundReport = JSON.parse(fs.readFileSync(reportPath, "utf8"));
454442
assert.equal(unboundReport.actions[0]?.status, "blocked");
455443
assert.equal(
@@ -462,11 +450,12 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
462450
fs.rmSync(reportPath, { force: true });
463451
fs.rmSync(viewCountPath, { force: true });
464452
fs.rmSync(commentsCountPath, { force: true });
465-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
466-
cwd: repoRoot,
467-
env: { ...env, FAKE_GH_STRICT_BASE: "1", FAKE_GH_LATE_SECURITY: "1" },
468-
stdio: "pipe",
469-
});
453+
runPostFlight(
454+
jobPath,
455+
resultPath,
456+
{ ...env, FAKE_GH_STRICT_BASE: "1", FAKE_GH_LATE_SECURITY: "1" },
457+
1,
458+
);
470459
const lateSecurityReport = JSON.parse(fs.readFileSync(reportPath, "utf8"));
471460
assert.equal(lateSecurityReport.actions[0]?.status, "blocked");
472461
assert.equal(
@@ -475,6 +464,23 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
475464
);
476465
assert.equal(fs.existsSync(mergeFlagPath), false);
477466

467+
writeMergeReports(runDir, resultPath);
468+
fs.rmSync(reportPath, { force: true });
469+
fs.rmSync(viewCountPath, { force: true });
470+
fs.rmSync(commentsCountPath, { force: true });
471+
runPostFlight(
472+
jobPath,
473+
resultPath,
474+
{ ...env, FAKE_GH_STRICT_BASE: "1", FAKE_GH_MERGE_RACE: "1" },
475+
1,
476+
);
477+
const mergeRaceReport = JSON.parse(fs.readFileSync(reportPath, "utf8"));
478+
assert.equal(mergeRaceReport.outcome, "requeue");
479+
assert.equal(mergeRaceReport.actions[0]?.status, "blocked");
480+
assert.equal(mergeRaceReport.actions[0]?.retry_recommended, true);
481+
assert.match(mergeRaceReport.actions[0]?.reason, /merge attempt needs branch refresh/);
482+
assert.equal(fs.existsSync(mergeFlagPath), false);
483+
478484
writeMergeReports(runDir, resultPath);
479485
fs.rmSync(reportPath, { force: true });
480486
fs.rmSync(viewCountPath, { force: true });
@@ -595,7 +601,7 @@ test("post-flight keeps no-timestamp pending duplicate checks visible", () => {
595601
}
596602
});
597603

598-
test("post-flight exports a blocked report even when the command exits successfully", () => {
604+
test("post-flight exports a blocked report before exiting unsuccessfully", () => {
599605
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-post-flight-"));
600606
const jobPath = path.join(tmp, "job.md");
601607
const runDir = path.join(tmp, "run");
@@ -620,16 +626,17 @@ test("post-flight exports a blocked report even when the command exits successfu
620626
);
621627

622628
try {
623-
execFileSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
624-
cwd: repoRoot,
625-
env: {
629+
runPostFlight(
630+
jobPath,
631+
resultPath,
632+
{
626633
...process.env,
627634
CLAWSWEEPER_ALLOW_EXECUTE: "1",
628635
CLAWSWEEPER_ALLOWED_OWNER: "openclaw",
629636
GITHUB_OUTPUT: outputPath,
630637
},
631-
stdio: "pipe",
632-
});
638+
1,
639+
);
633640

634641
const report = JSON.parse(fs.readFileSync(reportPath, "utf8"));
635642
assert.equal(report.outcome, "blocked");
@@ -640,6 +647,22 @@ test("post-flight exports a blocked report even when the command exits successfu
640647
}
641648
});
642649

650+
function runPostFlight(
651+
jobPath: string,
652+
resultPath: string,
653+
env: NodeJS.ProcessEnv,
654+
expectedStatus: number,
655+
) {
656+
const child = spawnSync(process.execPath, ["dist/repair/post-flight.js", jobPath, resultPath], {
657+
cwd: repoRoot,
658+
env,
659+
encoding: "utf8",
660+
stdio: "pipe",
661+
});
662+
assert.equal(child.status, expectedStatus, child.stderr || child.stdout);
663+
return child;
664+
}
665+
643666
function writeIssueImplementationJob(jobPath: string) {
644667
fs.writeFileSync(
645668
jobPath,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import assert from "node:assert/strict";
2+
import fs from "node:fs";
3+
import test from "node:test";
4+
5+
import { shouldSelfHealRunRecord } from "../../dist/repair/self-heal-policy.js";
6+
7+
test("self-heal selects retryable post-flight reports even from legacy green runs", () => {
8+
assert.equal(
9+
shouldSelfHealRunRecord({
10+
workflow_conclusion: "success",
11+
post_flight_outcome: "requeue",
12+
}),
13+
true,
14+
);
15+
assert.equal(
16+
shouldSelfHealRunRecord({
17+
workflow_conclusion: "failure",
18+
post_flight_outcome: "blocked",
19+
}),
20+
false,
21+
);
22+
assert.equal(shouldSelfHealRunRecord({ workflow_conclusion: "failure" }), true);
23+
assert.equal(shouldSelfHealRunRecord({ workflow_conclusion: "success" }), false);
24+
});
25+
26+
test("published run records preserve post-flight retry state for self-heal", () => {
27+
const publisher = fs.readFileSync("src/repair/publish-result.ts", "utf8");
28+
const selfHeal = fs.readFileSync("src/repair/self-heal-failed-runs.ts", "utf8");
29+
assert.match(publisher, /post_flight_outcome: postFlightReport\.outcome \?\? null/);
30+
assert.match(publisher, /post_flight_detail: postFlightReport\.detail \?\? null/);
31+
assert.match(selfHeal, /\.filter\(\(record: JsonValue\) => shouldSelfHealRunRecord\(record\)\)/);
32+
});

0 commit comments

Comments
 (0)