Skip to content

Commit 429a73b

Browse files
authored
fix: stabilize workflow status publishing (#278)
* fix: stabilize clawsweeper workflow status publishing * Preserve Codex failure diagnostics
1 parent fe91381 commit 429a73b

6 files changed

Lines changed: 303 additions & 49 deletions

File tree

‎.github/workflows/github-activity.yml‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ on:
4848
- completed
4949

5050
permissions:
51-
contents: read
51+
contents: write
5252
actions: read
5353
issues: read
5454
pull-requests: read
@@ -91,6 +91,52 @@ jobs:
9191
echo "skip=false" >> "$GITHUB_OUTPUT"
9292
fi
9393
94+
- name: Dispatch spam comment intake candidates
95+
if: steps.core-budget.outputs.skip != 'true'
96+
env:
97+
GH_TOKEN: ${{ github.token }}
98+
shell: bash
99+
run: |
100+
set -euo pipefail
101+
payload_path="$RUNNER_TEMP/spam-comment-intake-dispatch.json"
102+
if ! node <<'NODE' > "$payload_path"; then
103+
echo "No spam comment intake candidate for this activity."
104+
exit 0
105+
fi
106+
const fs = require("fs");
107+
const eventName = process.env.GITHUB_EVENT_NAME || "";
108+
const root = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8"));
109+
const clientPayload = root.client_payload || {};
110+
const activity = clientPayload.activity || clientPayload || {};
111+
const type = activity.type || clientPayload.event_name || (eventName === "repository_dispatch" ? "" : eventName);
112+
const action = activity.action || clientPayload.action || root.action || "";
113+
if (!["issue_comment", "pull_request_review_comment"].includes(type)) process.exit(1);
114+
if (!["created", "edited"].includes(action)) process.exit(1);
115+
const repo =
116+
activity.target_repo ||
117+
activity.repo ||
118+
(activity.repository && activity.repository.full_name) ||
119+
(typeof activity.repository === "string" && activity.repository) ||
120+
clientPayload.target_repo ||
121+
clientPayload.repo ||
122+
(clientPayload.repository && clientPayload.repository.full_name) ||
123+
(typeof clientPayload.repository === "string" && clientPayload.repository) ||
124+
(root.repository && root.repository.full_name) ||
125+
process.env.GITHUB_REPOSITORY;
126+
process.stdout.write(JSON.stringify({
127+
event_type: "clawsweeper_spam_comment_intake",
128+
client_payload: {
129+
target_repo: repo,
130+
event_name: type,
131+
action,
132+
activity: { ...activity, type, action, repo },
133+
},
134+
}));
135+
NODE
136+
gh api "repos/${GITHUB_REPOSITORY}/dispatches" \
137+
--method POST \
138+
--input "$payload_path"
139+
94140
- uses: actions/checkout@v6
95141
if: steps.core-budget.outputs.skip != 'true'
96142
with:

‎.github/workflows/spam-comment-intake.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: spam comment intake
22

33
on:
44
repository_dispatch:
5-
types: [github_activity]
5+
types: [clawsweeper_spam_comment_intake]
66

77
permissions:
88
contents: read

‎.github/workflows/sweep.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,12 +1501,6 @@ jobs:
15011501
done
15021502
echo "::warning::Unable to dispatch background comment sync after three attempts; apply/comment-sync backstops can pick it up later."
15031503
1504-
- uses: ./.github/actions/setup-codex
1505-
if: ${{ success() && steps.target-write-token.outputs.token != '' && (((github.event_name == 'repository_dispatch' && github.event.action != 'clawsweeper_target_sweep') || github.event.inputs.item_number != '' || github.event.inputs.item_numbers != '') || github.event.inputs.apply_after_review == 'true') }}
1506-
env:
1507-
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
1508-
CLAWSWEEPER_INTERNAL_MODEL: ${{ secrets.CLAWSWEEPER_MODEL }}
1509-
15101504
- name: Sync selected review comments
15111505
if: ${{ success() && steps.target-write-token.outputs.token != '' && ((github.event_name == 'repository_dispatch' && github.event.action != 'clawsweeper_target_sweep') || github.event.inputs.item_number != '' || github.event.inputs.item_numbers != '') }}
15121506
timeout-minutes: 15
@@ -1555,6 +1549,12 @@ jobs:
15551549
--path "results/sweep-status/${target_slug}.json" \
15561550
--rebase-strategy theirs
15571551
1552+
- uses: ./.github/actions/setup-codex
1553+
if: ${{ success() && steps.target-write-token.outputs.token != '' && github.event.inputs.apply_after_review == 'true' }}
1554+
env:
1555+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
1556+
CLAWSWEEPER_INTERNAL_MODEL: ${{ secrets.CLAWSWEEPER_MODEL }}
1557+
15581558
- name: Apply selected safe close proposals
15591559
if: ${{ success() && steps.target-write-token.outputs.token != '' && github.event.inputs.apply_after_review == 'true' }}
15601560
timeout-minutes: 30

‎src/clawsweeper.ts‎

Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6344,12 +6344,29 @@ function codexFailureReason(detail: string): string {
63446344
if (detail.includes("invalid JSON")) return "invalid structured output";
63456345
if (detail.includes("ENOBUFS") || detail.includes("maxBuffer")) return "output buffer overflow";
63466346
if (detail.includes("timed out") || detail.includes("ETIMEDOUT")) return "timeout";
6347+
if (
6348+
/rate limit reached|tokens per min|\bTPM\b|requests per min|\b429\b|temporarily unavailable|overloaded|please try again in \d+(?:ms|s)/i.test(
6349+
detail,
6350+
)
6351+
) {
6352+
return "retryable codex transport failure";
6353+
}
6354+
if (
6355+
/ECONNRESET|EAI_AGAIN|ENOTFOUND|socket hang up|fetch failed|transport failure/i.test(detail)
6356+
) {
6357+
return "retryable codex transport failure";
6358+
}
63476359
return "codex execution failed";
63486360
}
63496361

6350-
function codexFailureDecision(status: number | null, stderr: string, stdout = ""): Decision {
6351-
const detail = stderr || "No stderr.";
6352-
const reason = codexFailureReason(detail);
6362+
function codexFailureDecision(
6363+
status: number | null,
6364+
detail: string,
6365+
stdout = "",
6366+
stderr = "",
6367+
): Decision {
6368+
const failureDetail = detail || "No failure detail.";
6369+
const reason = codexFailureReason(`${failureDetail}\n${stderr}\n${stdout}`);
63536370
return {
63546371
decision: "keep_open",
63556372
closeReason: "none",
@@ -6358,8 +6375,15 @@ function codexFailureDecision(status: number | null, stderr: string, stdout = ""
63586375
changeSummary: "Review failed before ClawSweeper could summarize the requested change.",
63596376
evidence: [
63606377
evidenceEntry({ label: "failure reason", detail: reason }),
6361-
evidenceEntry({ label: "codex failure detail", detail: trimMiddle(detail, 4000) }),
6362-
evidenceEntry({ label: "codex stdout", detail: trimMiddle(stdout || "No stdout.", 2000) }),
6378+
evidenceEntry({ label: "codex failure detail", detail: trimMiddle(failureDetail, 4000) }),
6379+
evidenceEntry({
6380+
label: "codex stderr",
6381+
detail: trimMiddle(stderr || "No stderr captured.", 3000),
6382+
}),
6383+
evidenceEntry({
6384+
label: "codex stdout",
6385+
detail: trimMiddle(stdout || "No stdout captured.", 2000),
6386+
}),
63636387
],
63646388
likelyOwners: [
63656389
{
@@ -6452,6 +6476,46 @@ function codexFailureDecision(status: number | null, stderr: string, stdout = ""
64526476
};
64536477
}
64546478

6479+
export function codexFailureDecisionForTest(
6480+
status: number | null,
6481+
detail: string,
6482+
stdout = "",
6483+
stderr = "",
6484+
): Decision {
6485+
return codexFailureDecision(status, detail, stdout, stderr);
6486+
}
6487+
6488+
function redactedOutputTail(value: string | Buffer | null | undefined, maxLength = 6000): string {
6489+
return safeOutputTail(value, maxLength)
6490+
.replace(/\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\b/g, "[REDACTED_OPENAI_KEY]")
6491+
.replace(/\bgithub_pat_[A-Za-z0-9_]{20,}\b/g, "[REDACTED_GITHUB_TOKEN]")
6492+
.replace(/\bgh[pousr]_[A-Za-z0-9_]{20,}\b/g, "[REDACTED_GITHUB_TOKEN]")
6493+
.replace(/\b(OPENAI_API_KEY|CODEX_API_KEY|GH_TOKEN|GITHUB_TOKEN)=([^\s"']+)/g, "$1=[REDACTED]")
6494+
.replace(
6495+
/"((?:OPENAI_API_KEY|CODEX_API_KEY|GH_TOKEN|GITHUB_TOKEN))"\s*:\s*"[^"]*"/g,
6496+
'"$1":"[REDACTED]"',
6497+
);
6498+
}
6499+
6500+
class CodexReviewError extends Error {
6501+
readonly status: number | null;
6502+
readonly stdout: string;
6503+
readonly stderr: string;
6504+
6505+
constructor(options: {
6506+
message: string;
6507+
status: number | null;
6508+
stdout?: string;
6509+
stderr?: string;
6510+
}) {
6511+
super(options.message);
6512+
this.name = "CodexReviewError";
6513+
this.status = options.status;
6514+
this.stdout = options.stdout ?? "";
6515+
this.stderr = options.stderr ?? "";
6516+
}
6517+
}
6518+
64556519
function openclawDirtyStatus(openclawDir: string): string {
64566520
return run("git", ["status", "--porcelain=v1", "--untracked-files=all"], {
64576521
cwd: openclawDir,
@@ -6587,11 +6651,12 @@ function runCodex(options: {
65876651
);
65886652
}
65896653
if (result.error) {
6590-
throw new Error(
6591-
`Codex review failed for #${options.item.number}: ${result.error.message}\n${
6592-
safeOutputTail(result.stderr) || safeOutputTail(result.stdout) || "No output."
6593-
}`,
6594-
);
6654+
throw new CodexReviewError({
6655+
message: `Codex review failed for #${options.item.number}: ${result.error.message}`,
6656+
status: result.status,
6657+
stdout: redactedOutputTail(result.stdout),
6658+
stderr: redactedOutputTail(result.stderr),
6659+
});
65956660
}
65966661
if (result.status !== 0) {
65976662
if (existsSync(outputPath)) {
@@ -6607,32 +6672,32 @@ function runCodex(options: {
66076672
);
66086673
return decision;
66096674
} catch (error) {
6610-
throw new Error(
6611-
`Codex review failed for #${options.item.number} with exit ${
6675+
throw new CodexReviewError({
6676+
message: `Codex review failed for #${options.item.number} with exit ${
66126677
result.status ?? "unknown"
66136678
} and wrote invalid JSON or schema-invalid output to ${outputPath}: ${
66146679
error instanceof Error ? error.message : String(error)
6615-
}.\n${safeOutputTail(result.stderr) || safeOutputTail(result.stdout) || "No output."}`,
6616-
);
6680+
}.`,
6681+
status: result.status,
6682+
stdout: redactedOutputTail(result.stdout),
6683+
stderr: redactedOutputTail(result.stderr),
6684+
});
66176685
}
66186686
}
6619-
throw new Error(
6620-
`Codex review failed for #${options.item.number} with exit ${
6621-
result.status ?? "unknown"
6622-
}.\n${safeOutputTail(result.stderr) || safeOutputTail(result.stdout) || "No output."}`,
6623-
);
6687+
throw new CodexReviewError({
6688+
message: `Codex review failed for #${options.item.number} with exit ${result.status ?? "unknown"}.`,
6689+
status: result.status,
6690+
stdout: redactedOutputTail(result.stdout),
6691+
stderr: redactedOutputTail(result.stderr),
6692+
});
66246693
}
66256694
if (!existsSync(outputPath)) {
6626-
const decision = codexFailureDecision(
6627-
result.status,
6628-
`Codex exited successfully but did not write ${outputPath}.`,
6629-
result.stdout,
6630-
);
6631-
throw new Error(
6632-
`Codex review did not produce output for #${options.item.number}: ${decision.evidence
6633-
.map((entry) => entry.detail)
6634-
.join("\n")}`,
6635-
);
6695+
throw new CodexReviewError({
6696+
message: `Codex exited successfully but did not write ${outputPath}.`,
6697+
status: result.status,
6698+
stdout: redactedOutputTail(result.stdout),
6699+
stderr: redactedOutputTail(result.stderr),
6700+
});
66366701
}
66376702
try {
66386703
return parseDecision(JSON.parse(readFileSync(outputPath, "utf8").trim()), options.item);
@@ -6642,13 +6707,17 @@ function runCodex(options: {
66426707
`Codex wrote invalid JSON or schema-invalid output to ${outputPath}: ${
66436708
error instanceof Error ? error.message : String(error)
66446709
}`,
6645-
result.stdout,
6710+
redactedOutputTail(result.stdout),
6711+
redactedOutputTail(result.stderr),
66466712
);
6647-
throw new Error(
6648-
`Codex review wrote invalid JSON for #${options.item.number}: ${decision.evidence
6713+
throw new CodexReviewError({
6714+
message: `Codex review wrote invalid JSON for #${options.item.number}: ${decision.evidence
66496715
.map((entry) => entry.detail)
66506716
.join("\n")}`,
6651-
);
6717+
status: result.status,
6718+
stdout: redactedOutputTail(result.stdout),
6719+
stderr: redactedOutputTail(result.stderr),
6720+
});
66526721
}
66536722
}
66546723

@@ -14549,11 +14618,15 @@ function reviewCommand(args: Args): void {
1454914618
});
1455014619
} catch (error) {
1455114620
codexFailures += 1;
14552-
decision = codexFailureDecision(
14553-
null,
14554-
error instanceof Error ? error.message : String(error),
14555-
"Per-item Codex failure; continuing with the rest of the shard.",
14556-
);
14621+
if (error instanceof CodexReviewError) {
14622+
decision = codexFailureDecision(error.status, error.message, error.stdout, error.stderr);
14623+
} else {
14624+
decision = codexFailureDecision(
14625+
null,
14626+
error instanceof Error ? error.message : String(error),
14627+
"Per-item Codex failure; continuing with the rest of the shard.",
14628+
);
14629+
}
1455714630
} finally {
1455814631
codexElapsedMs = Date.now() - codexStartedAt;
1455914632
}

‎src/repair/comment-router.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,8 +3100,9 @@ function hasExistingModeStatusResponse(number: JsonValue, intent: JsonValue) {
31003100
}
31013101

31023102
function postComment(command: LooseRecord, body: string) {
3103-
const existing =
3104-
findExistingCommandStatusComment(command) ?? findPrecreatedCommandStatusComment(command);
3103+
const existingStatus = findExistingCommandStatusComment(command);
3104+
const precreated = findPrecreatedCommandStatusComment(command);
3105+
const existing = existingStatus ?? precreated;
31053106
const ackMarker = existing ? commandAckMarkerFromBody(existing.body) : null;
31063107
const responseBody = ackMarker && !body.includes(ackMarker) ? `${ackMarker}\n${body}` : body;
31073108
const nextBody = usesSharedAutomergeStatus(command)
@@ -3123,7 +3124,24 @@ function postComment(command: LooseRecord, body: string) {
31233124
"--input",
31243125
payloadPath,
31253126
]);
3126-
return { mode: "updated", comment_id: String(existing.id) };
3127+
const precreatedId = Number(precreated?.id ?? 0) || 0;
3128+
const existingId = Number(existing.id ?? 0) || 0;
3129+
if (existingStatus && precreatedId > 0 && precreatedId !== existingId) {
3130+
ghBestEffort([
3131+
"api",
3132+
`repos/${command.repo}/issues/comments/${precreatedId}`,
3133+
"--method",
3134+
"DELETE",
3135+
]);
3136+
issueCommentsCache.delete(Number(command.issue_number));
3137+
}
3138+
return {
3139+
mode: "updated",
3140+
comment_id: String(existing.id),
3141+
...(precreatedId > 0 && precreatedId !== existingId
3142+
? { pruned_ack_comment_id: String(precreatedId) }
3143+
: {}),
3144+
};
31273145
}
31283146
ghText([
31293147
"api",

0 commit comments

Comments
 (0)