Skip to content

Commit be14940

Browse files
committed
fix(review): acknowledge pull requests at receipt
1 parent 1ab4350 commit be14940

2 files changed

Lines changed: 89 additions & 23 deletions

File tree

‎dashboard/worker.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@ async function githubWebhook(request, env, ctx) {
11981198
if ("type" in decision && decision.type === "item") {
11991199
const deliveryId = request.headers.get("x-github-delivery") || "";
12001200
let itemDecision = decision as ExactReviewDecision & { installationId?: number };
1201+
if (itemDecision.itemKind === "pull_request") {
1202+
await acknowledgePullRequestReceipt({ env, ctx, decision: itemDecision }).catch(
1203+
() => undefined,
1204+
);
1205+
}
12011206
itemDecision = await withPullRequestEditContentRevision({
12021207
event,
12031208
payload,
@@ -1264,7 +1269,6 @@ async function githubWebhook(request, env, ctx) {
12641269
ingress,
12651270
});
12661271
if (!queued) return json({ error: "exact_review_queue_not_configured" }, 503);
1267-
await acknowledgePullRequestEnqueue({ env, ctx, decision: exactReviewDecision });
12681272
if (sourceAuthoritySeq !== null) {
12691273
await completeExactReviewSourceAuthority(
12701274
env,
@@ -2445,7 +2449,7 @@ async function enqueueExactReview({
24452449
return body;
24462450
}
24472451

2448-
async function acknowledgePullRequestEnqueue({ env, ctx, decision }) {
2452+
async function acknowledgePullRequestReceipt({ env, ctx, decision }) {
24492453
if (
24502454
decision.itemKind !== "pull_request" ||
24512455
!["opened", "ready_for_review"].includes(decision.sourceAction)
@@ -2678,7 +2682,7 @@ function renderPullRequestFastAckComment(ackMarker) {
26782682
"🦞👀",
26792683
"ClawSweeper picked this up.",
26802684
"",
2681-
"Pull request review queued. I will update this pull request when review starts.",
2685+
"Pull request received. I will update this pull request when review starts.",
26822686
].join("\n");
26832687
}
26842688

‎test/dashboard-worker.test.ts‎

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25901,7 +25901,7 @@ test("hosted webhook ignores removal of non-close-guard labels", async () => {
2590125901
});
2590225902
});
2590325903

25904-
test("hosted pull request openings post one fast ack across both ingress routes", async () => {
25904+
test("hosted pull request receipt fast acks precede verification and stay idempotent", async () => {
2590525905
const originalFetch = globalThis.fetch;
2590625906
const { privateKey } = generateKeyPairSync("rsa", {
2590725907
modulusLength: 2048,
@@ -25910,14 +25910,15 @@ test("hosted pull request openings post one fast ack across both ingress routes"
2591025910
});
2591125911
const storage = new MemoryDurableStorage();
2591225912
const queue = new ExactReviewQueue({ storage }, {});
25913-
const comments: Array<{
25913+
type AckComment = {
2591425914
id: number;
2591525915
body: string;
2591625916
created_at: string;
2591725917
user: { login: string };
25918-
}> = [];
25918+
};
25919+
const comments = new Map<number, AckComment[]>();
2591925920
const waitUntilPromises: Promise<unknown>[] = [];
25920-
let fastAckPosts = 0;
25921+
const fastAckPostAttempts = new Map<number, number>();
2592125922
const repository = {
2592225923
full_name: "openclaw/gogcli",
2592325924
default_branch: "trunk",
@@ -25952,24 +25953,40 @@ test("hosted pull request openings post one fast ack across both ingress routes"
2595225953
if (url.pathname === "/app/installations/123/access_tokens") {
2595325954
return jsonResponse({ token: "target-token" });
2595425955
}
25955-
if (/^\/repos\/openclaw\/gogcli\/pulls\/(597|598)$/.test(url.pathname)) {
25956+
const pullMatch = /^\/repos\/openclaw\/gogcli\/pulls\/(\d+)$/.exec(url.pathname);
25957+
if (pullMatch) {
25958+
assert.equal(new Headers(init?.headers).get("authorization"), "Bearer verification-token");
25959+
const itemNumber = Number(pullMatch[1]);
25960+
if (itemNumber === 599) throw new Error("transient GitHub verification failure");
2595625961
return jsonResponse({
25957-
head: { sha: url.pathname.endsWith("597") ? "a".repeat(40) : "b".repeat(40) },
25962+
head: {
25963+
sha:
25964+
itemNumber === 597
25965+
? "a".repeat(40)
25966+
: itemNumber === 598
25967+
? "b".repeat(40)
25968+
: itemNumber === 600
25969+
? "e".repeat(40)
25970+
: "f".repeat(40),
25971+
},
2595825972
});
2595925973
}
25960-
if (url.pathname === "/repos/openclaw/gogcli/issues/597/comments" && init?.method === "GET") {
25961-
return jsonResponse([...comments]);
25974+
const commentMatch = /^\/repos\/openclaw\/gogcli\/issues\/(\d+)\/comments$/.exec(url.pathname);
25975+
if (commentMatch && init?.method === "GET") {
25976+
return jsonResponse([...(comments.get(Number(commentMatch[1])) || [])]);
2596225977
}
25963-
if (url.pathname === "/repos/openclaw/gogcli/issues/597/comments" && init?.method === "POST") {
25964-
fastAckPosts += 1;
25978+
if (commentMatch && init?.method === "POST") {
25979+
const itemNumber = Number(commentMatch[1]);
25980+
fastAckPostAttempts.set(itemNumber, (fastAckPostAttempts.get(itemNumber) || 0) + 1);
25981+
if (itemNumber === 601) throw new Error("GitHub comment write failed");
2596525982
const body = JSON.parse(String(init.body || "{}"));
2596625983
const comment = {
25967-
id: 9001,
25984+
id: 9000 + itemNumber,
2596825985
body: String(body.body || ""),
2596925986
created_at: "2026-08-08T12:00:01Z",
2597025987
user: { login: "openclaw-clawsweeper[bot]" },
2597125988
};
25972-
comments.push(comment);
25989+
comments.set(itemNumber, [...(comments.get(itemNumber) || []), comment]);
2597325990
return jsonResponse(comment);
2597425991
}
2597525992
throw new Error(`unexpected fetch ${url}`);
@@ -25980,6 +25997,7 @@ test("hosted pull request openings post one fast ack across both ingress routes"
2598025997
CLAWSWEEPER_APP_PRIVATE_KEY: privateKey,
2598125998
CLAWSWEEPER_WEBHOOK_SECRET: "test-secret",
2598225999
CLAWSWEEPER_FAST_ACK_SETTLE_DELAYS_MS: "0",
26000+
GITHUB_TOKEN: "verification-token",
2598326001
EXACT_REVIEW_QUEUE: new MemoryDurableNamespace(queue),
2598426002
};
2598526003
const context = {
@@ -26018,21 +26036,24 @@ test("hosted pull request openings post one fast ack across both ingress routes"
2601826036

2601926037
assert.equal((await send("app-opened-1", "opened")).status, 202);
2602026038
assert.equal((await send("app-opened-2", "opened")).status, 202);
26021-
assert.equal(fastAckPosts, 1);
26022-
assert.equal(comments.length, 1);
26039+
assert.equal(fastAckPostAttempts.get(597), 1);
26040+
assert.equal(comments.get(597)?.length, 1);
2602326041
assert.equal(
26024-
comments[0]?.body,
26042+
comments.get(597)?.[0]?.body,
2602526043
[
2602626044
"<!-- clawsweeper-pr-ack:opened item=597 -->",
2602726045
"🦞👀",
2602826046
"ClawSweeper picked this up.",
2602926047
"",
26030-
"Pull request review queued. I will update this pull request when review starts.",
26048+
"Pull request received. I will update this pull request when review starts.",
2603126049
].join("\n"),
2603226050
);
26033-
assert.doesNotMatch(comments[0]?.body || "", /clawsweeper-command-ack:/);
26034-
assert.doesNotMatch(comments[0]?.body || "", /clawsweeper-review-status:started/);
26035-
assert.doesNotMatch(comments[0]?.body || "", /^ClawSweeper status: review started\./);
26051+
assert.doesNotMatch(comments.get(597)?.[0]?.body || "", /clawsweeper-command-ack:/);
26052+
assert.doesNotMatch(comments.get(597)?.[0]?.body || "", /clawsweeper-review-status:started/);
26053+
assert.doesNotMatch(
26054+
comments.get(597)?.[0]?.body || "",
26055+
/^ClawSweeper status: review started\./,
26056+
);
2603626057

2603726058
assert.equal(
2603826059
(
@@ -26045,7 +26066,48 @@ test("hosted pull request openings post one fast ack across both ingress routes"
2604526066
).status,
2604626067
202,
2604726068
);
26048-
assert.equal(fastAckPosts, 1);
26069+
assert.equal(fastAckPostAttempts.has(598), false);
26070+
assert.equal(comments.has(598), false);
26071+
26072+
const deferred = await send("app-opened-deferred", "opened", {
26073+
number: 599,
26074+
head: { sha: "c".repeat(40) },
26075+
updated_at: "2026-08-08T12:02:00Z",
26076+
body: "Verification will be retried.",
26077+
});
26078+
assert.deepEqual(await deferred.json(), {
26079+
ok: true,
26080+
accepted: true,
26081+
deferred: true,
26082+
reason: "pull request head verification deferred",
26083+
});
26084+
assert.equal(fastAckPostAttempts.get(599), 1);
26085+
assert.equal(comments.get(599)?.length, 1);
26086+
26087+
const stale = await send("app-ready-stale", "ready_for_review", {
26088+
number: 600,
26089+
head: { sha: "d".repeat(40) },
26090+
updated_at: "2026-08-08T12:03:00Z",
26091+
body: "The webhook head is already stale.",
26092+
});
26093+
assert.deepEqual(await stale.json(), {
26094+
ok: true,
26095+
accepted: false,
26096+
reason: "stale pull request head",
26097+
});
26098+
assert.equal(fastAckPostAttempts.get(600), 1);
26099+
assert.equal(comments.get(600)?.length, 1);
26100+
assert.match(comments.get(600)?.[0]?.body || "", /clawsweeper-pr-ack:ready_for_review/);
26101+
26102+
const ackFailure = await send("app-opened-ack-failure", "opened", {
26103+
number: 601,
26104+
head: { sha: "f".repeat(40) },
26105+
updated_at: "2026-08-08T12:04:00Z",
26106+
body: "The ack write fails, but enqueue continues.",
26107+
});
26108+
assert.equal(ackFailure.status, 202);
26109+
assert.equal((await ackFailure.json()).queued, true);
26110+
assert.equal(fastAckPostAttempts.get(601), 1);
2604926111
await Promise.all(waitUntilPromises);
2605026112
} finally {
2605126113
globalThis.fetch = originalFetch;

0 commit comments

Comments
 (0)