Skip to content

Commit 86ef55f

Browse files
committed
fix(review): reduce first-feedback latency
1 parent 095f5c5 commit 86ef55f

5 files changed

Lines changed: 362 additions & 27 deletions

File tree

‎.github/workflows/sweep.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,12 +855,6 @@ jobs:
855855
permission-pull-requests: write
856856
permission-statuses: read
857857

858-
- uses: ./.github/actions/setup-pnpm
859-
id: setup-pnpm
860-
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && steps.live-item.outcome == 'success' && steps.live-item.outputs.admission_retry != 'true' }}
861-
with:
862-
build-script: ${{ steps.live-item.outputs.proceed == 'true' && 'build:all' || 'build:repair' }}
863-
864858
- name: React to target item review start
865859
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && steps.live-item.outputs.proceed == 'true' }}
866860
continue-on-error: true
@@ -890,6 +884,12 @@ jobs:
890884
}
891885
react "eyes"
892886
887+
- uses: ./.github/actions/setup-pnpm
888+
id: setup-pnpm
889+
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && steps.live-item.outcome == 'success' && steps.live-item.outputs.admission_retry != 'true' }}
890+
with:
891+
build-script: ${{ steps.live-item.outputs.proceed == 'true' && 'build:all' || 'build:repair' }}
892+
893893
- uses: actions/cache@v6
894894
if: ${{ steps.claim-exact-review-queue.outputs.claimed == 'true' && steps.live-item.outputs.proceed == 'true' }}
895895
with:

‎dashboard/exact-review-queue.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ export class ExactReviewQueue {
13121312
revision: this.nextExactReviewItemRevisionSync(key),
13131313
createdAt: now,
13141314
updatedAt: now,
1315-
...exactReviewQueueDebouncedAttempt(state, decision, now, now, this.env),
1315+
...exactReviewQueueDebouncedAttempt(state, decision, now, now, this.env, true),
13161316
attempts: 0,
13171317
...(exactReviewSourceAuthorityWatermark(decision)
13181318
? { sourceAuthorityWatermark: exactReviewSourceAuthorityWatermark(decision)! }
@@ -11124,9 +11124,12 @@ function exactReviewQueueDebouncedAttempt(
1112411124
now: number,
1112511125
firstEnqueuedAt: number,
1112611126
env,
11127+
isFirstEvent = false,
1112711128
) {
1112811129
const baseAttemptAt = exactReviewQueueEnqueueAttemptAt(state, now);
11129-
if (isImmediateExactReviewDecision(decision)) return exactReviewQueueEnqueueAttempt(state, now);
11130+
if (isImmediateExactReviewDecision(decision, isFirstEvent)) {
11131+
return exactReviewQueueEnqueueAttempt(state, now);
11132+
}
1113011133
const debounceAt = Math.min(
1113111134
now + exactReviewDispatchDebounceMs(env),
1113211135
firstEnqueuedAt + exactReviewDispatchDebounceMaxMs(env),
@@ -11143,9 +11146,14 @@ function exactReviewQueueDebouncedAttempt(
1114311146
};
1114411147
}
1114511148

11146-
function isImmediateExactReviewDecision(decision: ExactReviewDecision) {
11149+
function isImmediateExactReviewDecision(decision: ExactReviewDecision, isFirstEvent = false) {
1114711150
return Boolean(
11148-
decision.commandStatusMarker || decision.publication || exactReviewScheduledLane(decision),
11151+
decision.commandStatusMarker ||
11152+
decision.publication ||
11153+
exactReviewScheduledLane(decision) ||
11154+
(isFirstEvent &&
11155+
decision.itemKind === "pull_request" &&
11156+
["opened", "ready_for_review"].includes(decision.sourceAction)),
1114911157
);
1115011158
}
1115111159

‎dashboard/worker.ts‎

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,7 @@ async function githubWebhook(request, env, ctx) {
12641264
ingress,
12651265
});
12661266
if (!queued) return json({ error: "exact_review_queue_not_configured" }, 503);
1267+
await acknowledgePullRequestEnqueue({ env, ctx, decision: exactReviewDecision });
12671268
if (sourceAuthoritySeq !== null) {
12681269
await completeExactReviewSourceAuthority(
12691270
env,
@@ -2444,18 +2445,63 @@ async function enqueueExactReview({
24442445
return body;
24452446
}
24462447

2447-
async function createFastAckComment({ token, repo, itemNumber, sourceCommentId }) {
2448-
const existingId = await pruneFastAckComments({ token, repo, itemNumber, sourceCommentId });
2448+
async function acknowledgePullRequestEnqueue({ env, ctx, decision }) {
2449+
if (
2450+
decision.itemKind !== "pull_request" ||
2451+
!["opened", "ready_for_review"].includes(decision.sourceAction)
2452+
) {
2453+
return null;
2454+
}
2455+
const credentials = githubAppCredentials(env);
2456+
if (!credentials || !Number.isInteger(decision.installationId) || decision.installationId <= 0) {
2457+
return null;
2458+
}
2459+
const appJwt = await signGithubAppJwt(credentials.issuer, credentials.privateKey);
2460+
const token = await createGithubAppTokenFor({
2461+
appJwt,
2462+
installationId: decision.installationId,
2463+
label: decision.targetRepo,
2464+
repositories: [repoName(decision.targetRepo)],
2465+
permissions: { issues: "write", pull_requests: "read" },
2466+
});
2467+
const ackMarker = pullRequestFastAckMarker(decision.itemNumber, decision.sourceAction);
2468+
const statusCommentId = await createFastAckCommentOnce({
2469+
token,
2470+
repo: decision.targetRepo,
2471+
itemNumber: decision.itemNumber,
2472+
ackMarker,
2473+
ackBody: renderPullRequestFastAckComment(ackMarker),
2474+
});
2475+
settleFastAckComments({
2476+
token,
2477+
repo: decision.targetRepo,
2478+
itemNumber: decision.itemNumber,
2479+
ackMarker,
2480+
delaysMs: fastAckSettleDelaysMs(env.CLAWSWEEPER_FAST_ACK_SETTLE_DELAYS_MS),
2481+
waitUntil: ctx?.waitUntil?.bind(ctx),
2482+
});
2483+
return statusCommentId;
2484+
}
2485+
2486+
async function createFastAckComment({
2487+
token,
2488+
repo,
2489+
itemNumber,
2490+
sourceCommentId = undefined,
2491+
ackMarker = fastAckMarker(sourceCommentId),
2492+
ackBody = renderFastAckComment(sourceCommentId),
2493+
}) {
2494+
const existingId = await pruneFastAckComments({ token, repo, itemNumber, ackMarker });
24492495
if (existingId) return existingId;
24502496
const payload = await githubTokenJson({
24512497
token,
24522498
path: `/repos/${repo}/issues/${itemNumber}/comments`,
24532499
method: "POST",
2454-
body: { body: renderFastAckComment(sourceCommentId) },
2500+
body: { body: ackBody },
24552501
errorLabel: "ClawSweeper ack comment",
24562502
});
24572503
return (
2458-
(await pruneFastAckComments({ token, repo, itemNumber, sourceCommentId })) ||
2504+
(await pruneFastAckComments({ token, repo, itemNumber, ackMarker })) ||
24592505
Number(payload.id) ||
24602506
null
24612507
);
@@ -2465,14 +2511,15 @@ function settleFastAckComments({
24652511
token,
24662512
repo,
24672513
itemNumber,
2468-
sourceCommentId,
2514+
sourceCommentId = undefined,
2515+
ackMarker = fastAckMarker(sourceCommentId),
24692516
delaysMs = DEFAULT_FAST_ACK_SETTLE_DELAYS_MS,
24702517
waitUntil,
24712518
}) {
24722519
const cleanup = async () => {
24732520
for (const delayMs of delaysMs) {
24742521
await sleep(delayMs);
2475-
await pruneFastAckComments({ token, repo, itemNumber, sourceCommentId });
2522+
await pruneFastAckComments({ token, repo, itemNumber, ackMarker });
24762523
}
24772524
};
24782525
const promise = cleanup().catch((error) => {
@@ -2489,19 +2536,26 @@ function fastAckSettleDelaysMs(value) {
24892536
return delays.length > 0 ? delays : DEFAULT_FAST_ACK_SETTLE_DELAYS_MS;
24902537
}
24912538

2492-
async function createFastAckCommentOnce({ token, repo, itemNumber, sourceCommentId }) {
2493-
const key = fastAckKey({ repo, itemNumber, sourceCommentId });
2539+
async function createFastAckCommentOnce({
2540+
token,
2541+
repo,
2542+
itemNumber,
2543+
sourceCommentId = undefined,
2544+
ackMarker = fastAckMarker(sourceCommentId),
2545+
ackBody = renderFastAckComment(sourceCommentId),
2546+
}) {
2547+
const key = fastAckKey({ repo, itemNumber, ackMarker });
24942548
const pending = inFlightFastAcks.get(key);
24952549
if (pending) return pending;
2496-
const next = createFastAckComment({ token, repo, itemNumber, sourceCommentId }).finally(() => {
2550+
const next = createFastAckComment({ token, repo, itemNumber, ackMarker, ackBody }).finally(() => {
24972551
inFlightFastAcks.delete(key);
24982552
});
24992553
inFlightFastAcks.set(key, next);
25002554
return next;
25012555
}
25022556

2503-
function fastAckKey({ repo, itemNumber, sourceCommentId }) {
2504-
return `${String(repo).toLowerCase()}:${itemNumber}:${sourceCommentId}`;
2557+
function fastAckKey({ repo, itemNumber, ackMarker }) {
2558+
return `${String(repo).toLowerCase()}:${itemNumber}:${ackMarker}`;
25052559
}
25062560

25072561
function sleep(delayMs) {
@@ -2511,8 +2565,14 @@ function sleep(delayMs) {
25112565
});
25122566
}
25132567

2514-
async function pruneFastAckComments({ token, repo, itemNumber, sourceCommentId }) {
2515-
const comments = await listFastAckComments({ token, repo, itemNumber, sourceCommentId });
2568+
async function pruneFastAckComments({
2569+
token,
2570+
repo,
2571+
itemNumber,
2572+
sourceCommentId = undefined,
2573+
ackMarker = fastAckMarker(sourceCommentId),
2574+
}) {
2575+
const comments = await listFastAckComments({ token, repo, itemNumber, ackMarker });
25162576
if (!comments.length) return null;
25172577
const hasStatusComment = comments.some(isStatusBearingFastAckComment);
25182578
comments.sort(compareFastAckKeepPriority);
@@ -2569,9 +2629,8 @@ function compareCommentsByCreatedAt(left, right) {
25692629
);
25702630
}
25712631

2572-
async function listFastAckComments({ token, repo, itemNumber, sourceCommentId }) {
2632+
async function listFastAckComments({ token, repo, itemNumber, ackMarker }) {
25732633
const comments = [];
2574-
const marker = fastAckMarker(sourceCommentId);
25752634
const since = encodeURIComponent(new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString());
25762635
for (let page = 1; page <= 5; page += 1) {
25772636
const payload = await githubTokenJson({
@@ -2584,7 +2643,7 @@ async function listFastAckComments({ token, repo, itemNumber, sourceCommentId })
25842643
if (!Array.isArray(payload)) return comments;
25852644
for (const comment of payload) {
25862645
if (
2587-
String(objectValue(comment).body || "").includes(marker) &&
2646+
String(objectValue(comment).body || "").includes(ackMarker) &&
25882647
isClawsweeperGithubWebhookSender(objectValue(objectValue(comment).user))
25892648
) {
25902649
comments.push(comment);
@@ -2609,6 +2668,20 @@ function fastAckMarker(sourceCommentId) {
26092668
return `<!-- clawsweeper-command-ack:${sourceCommentId} -->`;
26102669
}
26112670

2671+
function pullRequestFastAckMarker(itemNumber, sourceAction) {
2672+
return `<!-- clawsweeper-pr-ack:${sourceAction} item=${itemNumber} -->`;
2673+
}
2674+
2675+
function renderPullRequestFastAckComment(ackMarker) {
2676+
return [
2677+
ackMarker,
2678+
"🦞👀",
2679+
"ClawSweeper picked this up.",
2680+
"",
2681+
"Pull request review queued. I will update this pull request when review starts.",
2682+
].join("\n");
2683+
}
2684+
26122685
async function addIssueCommentReaction({ token, repo, commentId, content }) {
26132686
await githubTokenJson({
26142687
token,

0 commit comments

Comments
 (0)