Skip to content

Commit ca335bc

Browse files
committed
fix: preserve recoverable command dispatch
1 parent eab3622 commit ca335bc

8 files changed

Lines changed: 259 additions & 82 deletions

File tree

‎.github/workflows/assist.yml‎

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ jobs:
6868
outputs:
6969
proceed: ${{ steps.receipt.outputs.proceed }}
7070
steps:
71+
- uses: actions/checkout@v5
72+
with:
73+
sparse-checkout: scripts/dispatch-receipt-owner.sh
74+
sparse-checkout-cone-mode: false
75+
7176
- id: receipt
7277
env:
7378
DISPATCH_KEY: ${{ github.event.client_payload.dispatch_key || '' }}
@@ -81,38 +86,11 @@ jobs:
8186
exit 0
8287
fi
8388
expected_title="Assist ${TARGET_REPO}#${ITEM_NUMBER} [${DISPATCH_KEY}]"
84-
duplicate_run_id="$(
85-
gh api --method GET "repos/${GITHUB_REPOSITORY}/actions/workflows/assist.yml/runs?per_page=100" \
86-
| jq -r --arg title "$expected_title" --arg current "$GITHUB_RUN_ID" '
87-
.workflow_runs[]
88-
| select(.display_title == $title and .id < ($current | tonumber))
89-
| select(.status == "queued" or .status == "in_progress" or .status == "waiting" or .status == "pending" or .status == "requested")
90-
| .id
91-
' \
92-
| head -n 1
93-
)"
94-
if [ -z "$duplicate_run_id" ]; then
95-
while IFS= read -r run_id; do
96-
if [ -z "$run_id" ]; then
97-
continue
98-
fi
99-
if gh run view "$run_id" --repo "${GITHUB_REPOSITORY}" --json jobs \
100-
| jq -e '.jobs[]? | select(.name == "assist" and .conclusion == "success")' >/dev/null; then
101-
duplicate_run_id="$run_id"
102-
break
103-
fi
104-
done < <(
105-
gh api --method GET "repos/${GITHUB_REPOSITORY}/actions/workflows/assist.yml/runs?per_page=100" \
106-
| jq -r --arg title "$expected_title" --arg current "$GITHUB_RUN_ID" '
107-
.workflow_runs[]
108-
| select(.display_title == $title and .id < ($current | tonumber) and .conclusion == "success")
109-
| .id
110-
'
111-
)
112-
fi
113-
if [ -n "$duplicate_run_id" ]; then
89+
owner="$(bash scripts/dispatch-receipt-owner.sh \
90+
assist.yml "$expected_title" "$GITHUB_RUN_ID" assist)"
91+
if [ "$owner" = "owner" ]; then
11492
echo "proceed=false" >> "$GITHUB_OUTPUT"
115-
echo "An older exact command dispatch receipt already exists; skipping duplicate assist."
93+
echo "An older active or successfully executed command dispatch already exists; skipping duplicate assist."
11694
exit 0
11795
fi
11896
echo "proceed=true" >> "$GITHUB_OUTPUT"

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

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ jobs:
7777
runs-on: ubuntu-latest
7878
permissions:
7979
actions: read
80+
contents: read
8081
outputs:
8182
proceed: ${{ steps.receipt.outputs.proceed }}
8283
steps:
84+
- uses: actions/checkout@v5
85+
with:
86+
sparse-checkout: scripts/dispatch-receipt-owner.sh
87+
sparse-checkout-cone-mode: false
88+
8389
- id: receipt
8490
env:
8591
DISPATCH_KEY: ${{ inputs.dispatch_key }}
@@ -97,38 +103,11 @@ jobs:
97103
*) title="repair cluster $JOB_PATH" ;;
98104
esac
99105
expected_title="${title} [${DISPATCH_KEY}]"
100-
duplicate_run_id="$(
101-
gh api --method GET "repos/${GITHUB_REPOSITORY}/actions/workflows/repair-cluster-worker.yml/runs?per_page=100" \
102-
| jq -r --arg title "$expected_title" --arg current "$GITHUB_RUN_ID" '
103-
.workflow_runs[]
104-
| select(.display_title == $title and .id < ($current | tonumber))
105-
| select(.status == "queued" or .status == "in_progress" or .status == "waiting" or .status == "pending" or .status == "requested")
106-
| .id
107-
' \
108-
| head -n 1
109-
)"
110-
if [ -z "$duplicate_run_id" ]; then
111-
while IFS= read -r run_id; do
112-
if [ -z "$run_id" ]; then
113-
continue
114-
fi
115-
if gh run view "$run_id" --repo "${GITHUB_REPOSITORY}" --json jobs \
116-
| jq -e '.jobs[]? | select(.name == "Plan and review cluster" and .conclusion == "success")' >/dev/null; then
117-
duplicate_run_id="$run_id"
118-
break
119-
fi
120-
done < <(
121-
gh api --method GET "repos/${GITHUB_REPOSITORY}/actions/workflows/repair-cluster-worker.yml/runs?per_page=100" \
122-
| jq -r --arg title "$expected_title" --arg current "$GITHUB_RUN_ID" '
123-
.workflow_runs[]
124-
| select(.display_title == $title and .id < ($current | tonumber) and .conclusion == "success")
125-
| .id
126-
'
127-
)
128-
fi
129-
if [ -n "$duplicate_run_id" ]; then
106+
owner="$(bash scripts/dispatch-receipt-owner.sh \
107+
repair-cluster-worker.yml "$expected_title" "$GITHUB_RUN_ID" "Plan and review cluster")"
108+
if [ "$owner" = "owner" ]; then
130109
echo "proceed=false" >> "$GITHUB_OUTPUT"
131-
echo "An older exact command dispatch receipt already exists; skipping duplicate repair."
110+
echo "An older active or successfully executed command dispatch already exists; skipping duplicate repair."
132111
exit 0
133112
fi
134113
echo "proceed=true" >> "$GITHUB_OUTPUT"

‎.github/workflows/sweep.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ run-name: >-
2020
format('Review event item {0}#{1} [{2}]', github.event.client_payload.target_repo || 'openclaw/openclaw', github.event.client_payload.item_number || '?', github.event.client_payload.dispatch_key) ||
2121
github.event_name == 'repository_dispatch' &&
2222
format('Review event item {0}#{1}', github.event.client_payload.target_repo || 'openclaw/openclaw', github.event.client_payload.item_number || '?') ||
23+
(github.event_name == 'workflow_dispatch' && startsWith(github.event.inputs.item_numbers, 'router-')) &&
24+
format('Review event item {0}#{1} [{2}]', github.event.inputs.target_repo || 'openclaw/openclaw', github.event.inputs.item_number || '?', github.event.inputs.item_numbers) ||
2325
((github.event_name == 'workflow_dispatch' &&
2426
github.event.inputs.apply_existing == 'true' &&
2527
github.event.inputs.apply_sync_comments_only == 'true') ||
@@ -1162,7 +1164,9 @@ jobs:
11621164
BATCH_SIZE: ${{ steps.mode.outputs.batch_size }}
11631165
HOT_INTAKE: ${{ steps.mode.outputs.hot_intake }}
11641166
ITEM_NUMBER: ${{ github.event.inputs.item_number || '' }}
1165-
ITEM_NUMBERS: ${{ github.event.inputs.item_numbers || github.event.client_payload.item_number || '' }}
1167+
# A router fallback carries its receipt key in item_numbers because GitHub caps this
1168+
# workflow at 25 inputs; item_number remains the exact review target in that case.
1169+
ITEM_NUMBERS: ${{ (github.event.inputs.item_number != '' && startsWith(github.event.inputs.item_numbers, 'router-') && github.event.inputs.item_number) || github.event.inputs.item_numbers || github.event.client_payload.item_number || '' }}
11661170
MAX_PAGES: ${{ steps.mode.outputs.max_pages }}
11671171
MIN_ACTIVE_SHARDS: ${{ steps.mode.outputs.min_active_shards }}
11681172
MIN_BACKFILL_REVIEW_AGE_MINUTES: ${{ steps.mode.outputs.min_backfill_review_age_minutes }}

‎scripts/dispatch-receipt-owner.sh‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
workflow="${1:?workflow file is required}"
5+
expected_title="${2:?expected run title is required}"
6+
current_run_id="${3:?current run id is required}"
7+
required_job_name="${4:?required worker job name is required}"
8+
9+
runs_json="$(gh api --method GET \
10+
"repos/${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is required}/actions/workflows/${workflow}/runs?per_page=100")"
11+
12+
active_owner_id="$(
13+
jq -r --arg title "$expected_title" --arg current "$current_run_id" '
14+
first(
15+
.workflow_runs[]
16+
| select(.display_title == $title and .id < ($current | tonumber))
17+
| select(.status == "queued" or .status == "in_progress" or .status == "waiting" or .status == "pending" or .status == "requested")
18+
| .id
19+
) // empty
20+
' <<<"$runs_json"
21+
)"
22+
if [ -n "$active_owner_id" ]; then
23+
printf 'owner\n'
24+
exit 0
25+
fi
26+
27+
successful_run_ids="$(
28+
jq -r --arg title "$expected_title" --arg current "$current_run_id" '
29+
.workflow_runs[]
30+
| select(.display_title == $title and .id < ($current | tonumber) and .conclusion == "success")
31+
| .id
32+
' <<<"$runs_json"
33+
)"
34+
while IFS= read -r run_id; do
35+
if [ -z "$run_id" ]; then
36+
continue
37+
fi
38+
jobs_json="$(gh api --method GET \
39+
"repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/jobs?per_page=100")"
40+
if jq -e --arg required "$required_job_name" \
41+
'any(.jobs[]; .name == $required and .conclusion == "success")' \
42+
<<<"$jobs_json" >/dev/null; then
43+
printf 'owner\n'
44+
exit 0
45+
fi
46+
done <<<"$successful_run_ids"
47+
48+
printf 'none\n'

‎src/repair/comment-router.ts‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const collaboratorPermissionCache = new Map();
182182
const activeRepairRunsByPrefix = new Map<string, LooseRecord[]>();
183183
const liveTargetCache = new Map<number, LooseRecord>();
184184
const issueCommentsCache = new Map<number, JsonValue[]>();
185+
const MAX_MEDIA_PREPROCESSING_TIMEOUT_MS = 480_000;
185186
const PROOF_OVERRIDE_DESCRIPTION_MARKER = "<!-- clawsweeper-proof-override-note -->";
186187
const cachedIssueComments = createCachedIssueCommentsLookup(
187188
(number) => ghPaged<JsonValue>(`repos/${targetRepo}/issues/${number}/comments?per_page=100`),
@@ -2378,6 +2379,11 @@ function dispatchClawSweeperReview(command: LooseRecord): LooseRecord {
23782379
command.target?.kind === "pull_request"
23792380
? adaptiveReviewBudgetForPullRequest(command.target)
23802381
: null;
2382+
// Comment-only media is hydrated after dispatch, so fallback must reserve the full bounded
2383+
// preprocessing budget even when the initial PR title/body did not expose media.
2384+
const fallbackCodexTimeoutMs = reviewBudget
2385+
? reviewBudget.codexTimeoutMs + MAX_MEDIA_PREPROCESSING_TIMEOUT_MS
2386+
: null;
23812387
const commandStatus = ["re_review", "autofix", "automerge"].includes(String(command.intent ?? ""))
23822388
? {
23832389
command_status_marker: commandStatusMarker(command),
@@ -2412,11 +2418,45 @@ function dispatchClawSweeperReview(command: LooseRecord): LooseRecord {
24122418
},
24132419
);
24142420
if (result.status !== 0) {
2415-
throw new Error(
2416-
`failed to dispatch ClawSweeper review for #${command.issue_number}: ${
2417-
result.stderr || result.stdout
2418-
}`,
2421+
const fallback = ghSpawn(
2422+
[
2423+
"workflow",
2424+
"run",
2425+
reviewWorkflow,
2426+
"--repo",
2427+
reviewRepo,
2428+
"-f",
2429+
`target_repo=${command.repo}`,
2430+
"-f",
2431+
...(command.target_branch ? [`target_branch=${String(command.target_branch)}`, "-f"] : []),
2432+
`item_number=${command.issue_number}`,
2433+
"-f",
2434+
`item_numbers=${dispatchKey}`,
2435+
"-f",
2436+
`additional_prompt=${freeformReviewPrompt(command)}`,
2437+
"-f",
2438+
...(fallbackCodexTimeoutMs ? [`codex_timeout_ms=${fallbackCodexTimeoutMs}`, "-f"] : []),
2439+
"batch_size=1",
2440+
"-f",
2441+
"shard_count=1",
2442+
],
2443+
{ env: dispatchTokenEnv() },
24192444
);
2445+
if (fallback.status !== 0) {
2446+
throw new Error(
2447+
`failed to dispatch ClawSweeper review for #${command.issue_number}: repository_dispatch=${
2448+
result.stderr || result.stdout
2449+
}; workflow_dispatch=${fallback.stderr || fallback.stdout}`,
2450+
);
2451+
}
2452+
return {
2453+
workflow: reviewWorkflow,
2454+
event: "workflow_dispatch",
2455+
repo: reviewRepo,
2456+
item_number: command.issue_number,
2457+
dispatch_key: dispatchKey,
2458+
fallback_reason: stripAnsi(result.stderr || result.stdout).trim(),
2459+
};
24202460
}
24212461
return {
24222462
workflow: reviewWorkflow,
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import assert from "node:assert/strict";
2+
import { execFileSync, spawnSync } from "node:child_process";
3+
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { delimiter, join } from "node:path";
6+
import test from "node:test";
7+
8+
const SCRIPT = join(process.cwd(), "scripts", "dispatch-receipt-owner.sh");
9+
const EXPECTED_TITLE = "Assist openclaw/openclaw#42 [router-proof]";
10+
11+
function fixtureEnv(options: {
12+
jobs101?: unknown[];
13+
jobs102?: unknown[];
14+
runs: unknown[];
15+
failJobs?: boolean;
16+
}) {
17+
const binDir = mkdtempSync(join(tmpdir(), "dispatch-receipt-owner-"));
18+
const gh = join(binDir, "gh");
19+
writeFileSync(
20+
gh,
21+
`#!/bin/sh
22+
case "$*" in
23+
*actions/workflows/*) printf '%s\\n' "$FAKE_RUNS_JSON" ;;
24+
*actions/runs/101/jobs*)
25+
[ "\${FAKE_FAIL_JOBS:-0}" = 1 ] && exit 7
26+
printf '%s\\n' "$FAKE_JOBS_101_JSON"
27+
;;
28+
*actions/runs/102/jobs*)
29+
[ "\${FAKE_FAIL_JOBS:-0}" = 1 ] && exit 7
30+
printf '%s\\n' "$FAKE_JOBS_102_JSON"
31+
;;
32+
*) echo "unexpected gh args: $*" >&2; exit 9 ;;
33+
esac
34+
`,
35+
);
36+
chmodSync(gh, 0o755);
37+
return {
38+
binDir,
39+
env: {
40+
...process.env,
41+
PATH: `${binDir}${delimiter}${process.env.PATH ?? ""}`,
42+
GITHUB_REPOSITORY: "openclaw/clawsweeper",
43+
FAKE_RUNS_JSON: JSON.stringify({ workflow_runs: options.runs }),
44+
FAKE_JOBS_101_JSON: JSON.stringify({ jobs: options.jobs101 ?? [] }),
45+
FAKE_JOBS_102_JSON: JSON.stringify({ jobs: options.jobs102 ?? [] }),
46+
FAKE_FAIL_JOBS: options.failJobs ? "1" : "0",
47+
},
48+
};
49+
}
50+
51+
function runGate(options: Parameters<typeof fixtureEnv>[0], requiredJobName = "assist") {
52+
const fixture = fixtureEnv(options);
53+
try {
54+
return execFileSync("bash", [SCRIPT, "assist.yml", EXPECTED_TITLE, "200", requiredJobName], {
55+
encoding: "utf8",
56+
env: fixture.env,
57+
}).trim();
58+
} finally {
59+
rmSync(fixture.binDir, { recursive: true, force: true });
60+
}
61+
}
62+
63+
test("dispatch receipt gate keeps an older active run as owner", () => {
64+
assert.equal(
65+
runGate({
66+
runs: [{ id: 101, display_title: EXPECTED_TITLE, status: "in_progress" }],
67+
}),
68+
"owner",
69+
);
70+
});
71+
72+
test("dispatch receipt gate allows retry after failed owner and receipt-only success", () => {
73+
for (const requiredJobName of ["assist", "Plan and review cluster"]) {
74+
assert.equal(
75+
runGate(
76+
{
77+
runs: [
78+
{ id: 101, display_title: EXPECTED_TITLE, status: "completed", conclusion: "failure" },
79+
{ id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" },
80+
],
81+
jobs102: [
82+
{ name: "Deduplicate command dispatch receipt", conclusion: "success" },
83+
{ name: requiredJobName, conclusion: "skipped" },
84+
],
85+
},
86+
requiredJobName,
87+
),
88+
"none",
89+
);
90+
}
91+
});
92+
93+
test("dispatch receipt gate keeps a successfully executed worker as owner", () => {
94+
assert.equal(
95+
runGate({
96+
runs: [
97+
{ id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" },
98+
],
99+
jobs102: [{ name: "assist", conclusion: "success" }],
100+
}),
101+
"owner",
102+
);
103+
});
104+
105+
test("dispatch receipt gate fails closed when worker job verification fails", () => {
106+
const fixture = fixtureEnv({
107+
runs: [{ id: 102, display_title: EXPECTED_TITLE, status: "completed", conclusion: "success" }],
108+
failJobs: true,
109+
});
110+
try {
111+
const result = spawnSync("bash", [SCRIPT, "assist.yml", EXPECTED_TITLE, "200", "assist"], {
112+
encoding: "utf8",
113+
env: fixture.env,
114+
});
115+
assert.equal(result.status, 7);
116+
assert.equal(result.stdout, "");
117+
} finally {
118+
rmSync(fixture.binDir, { recursive: true, force: true });
119+
}
120+
});

0 commit comments

Comments
 (0)