Skip to content

Commit 8558de5

Browse files
steipetePeter Steinberger
andauthored
refactor: simplify planning and accelerate safe publication retries (#1021)
* refactor: simplify planning and accelerate safe publication retries * fix: preserve permanent publication retry confirmation windows --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 0036015 commit 8558de5

6 files changed

Lines changed: 300 additions & 123 deletions
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
type PublicationCompletion = {
2+
kind: string;
3+
reasonCode: string;
4+
};
5+
6+
const TRANSIENT_RETRY_MAX_AGE_MS = 24 * 60 * 60 * 1000;
7+
const UNKNOWN_RETRY_MAX_AGE_MS = 60 * 60 * 1000;
8+
// Retry more often without shortening the existing outage-recovery windows.
9+
// Twelve old transient attempts spanned about 211 minutes; five old unknown
10+
// attempts spanned about 51 minutes before they entered the dead-letter lane.
11+
const TRANSIENT_RETRY_LIMIT = 48;
12+
const PERMANENT_RETRY_LIMIT = 3;
13+
const UNKNOWN_RETRY_LIMIT = 14;
14+
const NORMAL_RETRY_MAX_MS = 5 * 60_000;
15+
const RATE_LIMIT_RETRY_MAX_MS = 60 * 60_000;
16+
17+
export function exactReviewPublicationRetryExhausted(
18+
completion: PublicationCompletion,
19+
attempt: number,
20+
firstFailureAt: number,
21+
now: number,
22+
): boolean {
23+
if (completion.kind === "retryable_failure") {
24+
if (completion.reasonCode === "artifact_unavailable") return false;
25+
if (completion.reasonCode === "unknown_failure") {
26+
return attempt >= UNKNOWN_RETRY_LIMIT || now >= firstFailureAt + UNKNOWN_RETRY_MAX_AGE_MS;
27+
}
28+
return attempt >= TRANSIENT_RETRY_LIMIT || now >= firstFailureAt + TRANSIENT_RETRY_MAX_AGE_MS;
29+
}
30+
if (completion.kind !== "permanent_failure") return false;
31+
if (completion.reasonCode === "unknown_failure") {
32+
return attempt >= UNKNOWN_RETRY_LIMIT || now >= firstFailureAt + UNKNOWN_RETRY_MAX_AGE_MS;
33+
}
34+
return attempt >= PERMANENT_RETRY_LIMIT;
35+
}
36+
37+
export function exactReviewPublicationRetryDelayMs(
38+
itemKey: string,
39+
completion: PublicationCompletion,
40+
attempt: number,
41+
): number {
42+
const maximum =
43+
completion.reasonCode === "github_rate_limit" ? RATE_LIMIT_RETRY_MAX_MS : NORMAL_RETRY_MAX_MS;
44+
const baselineDelay =
45+
completion.kind === "permanent_failure" && completion.reasonCode !== "unknown_failure"
46+
? attempt <= 1
47+
? 60_000
48+
: NORMAL_RETRY_MAX_MS
49+
: 60_000 * 2 ** Math.min(Math.max(attempt - 1, 0), 6);
50+
const delay = Math.min(maximum, baselineDelay);
51+
const hash = [...`${itemKey}:${attempt}`].reduce(
52+
(current, character) => (current * 33 + character.charCodeAt(0)) >>> 0,
53+
5381,
54+
);
55+
return delay + Math.floor(delay * ((hash % 21) / 100));
56+
}

‎dashboard/exact-review-queue.ts‎

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import {
1616
type PublicationBatchFence,
1717
type PublicationBatchObservationStage,
1818
} from "./exact-review-publication-batches.ts";
19+
import {
20+
exactReviewPublicationRetryDelayMs,
21+
exactReviewPublicationRetryExhausted,
22+
} from "./exact-review-publication-retry.ts";
1923
import {
2024
CanonicalRecordTupleConflictError,
2125
DIRECT_PUBLICATION_LIFECYCLE_KINDS,
@@ -452,11 +456,6 @@ type ExactReviewScheduledLane = "hot_intake" | "normal_backfill";
452456
type ExactReviewScheduledBucket = ExactReviewScheduledLane | "global";
453457
const EXACT_REVIEW_COMPLETION_RETRY_MAX_MS = 2 * 60 * 60 * 1000;
454458
const EXACT_REVIEW_ARTIFACT_RETRY_MAX_MS = 80 * 24 * 60 * 60 * 1000;
455-
const EXACT_REVIEW_PUBLICATION_TRANSIENT_RETRY_MAX_AGE_MS = 24 * 60 * 60 * 1000;
456-
const EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_MAX_AGE_MS = 60 * 60 * 1000;
457-
const EXACT_REVIEW_PUBLICATION_TRANSIENT_RETRY_LIMIT = 12;
458-
const EXACT_REVIEW_PUBLICATION_PERMANENT_RETRY_LIMIT = 3;
459-
const EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_LIMIT = 5;
460459
const EXACT_REVIEW_PUBLICATION_ARTIFACT_RETRY_LIMIT = 3;
461460
const EXACT_REVIEW_RETRY_LIMIT = 8;
462461
const EXACT_REVIEW_PARKED_RECOVERY_LIMIT = 3;
@@ -10678,59 +10677,6 @@ function finishExactReviewPublicationQueueItem({
1067810677
return { requeued: true, retried: true, refreshed: false, parked: false };
1067910678
}
1068010679

10681-
function exactReviewPublicationRetryExhausted(
10682-
completion: ExactReviewPublicationCompletion,
10683-
attempt: number,
10684-
firstFailureAt: number,
10685-
now: number,
10686-
) {
10687-
if (completion.kind === "retryable_failure") {
10688-
if (completion.reasonCode === "artifact_unavailable") return false;
10689-
if (completion.reasonCode === "unknown_failure") {
10690-
return (
10691-
attempt >= EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_LIMIT ||
10692-
now >= firstFailureAt + EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_MAX_AGE_MS
10693-
);
10694-
}
10695-
return (
10696-
attempt >= EXACT_REVIEW_PUBLICATION_TRANSIENT_RETRY_LIMIT ||
10697-
now >= firstFailureAt + EXACT_REVIEW_PUBLICATION_TRANSIENT_RETRY_MAX_AGE_MS
10698-
);
10699-
}
10700-
if (completion.kind === "permanent_failure") {
10701-
const limit =
10702-
completion.reasonCode === "unknown_failure"
10703-
? EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_LIMIT
10704-
: EXACT_REVIEW_PUBLICATION_PERMANENT_RETRY_LIMIT;
10705-
return (
10706-
attempt >= limit ||
10707-
(completion.reasonCode === "unknown_failure" &&
10708-
now >= firstFailureAt + EXACT_REVIEW_PUBLICATION_UNKNOWN_RETRY_MAX_AGE_MS)
10709-
);
10710-
}
10711-
return false;
10712-
}
10713-
10714-
function exactReviewPublicationRetryDelayMs(
10715-
itemKey: string,
10716-
completion: ExactReviewPublicationCompletion,
10717-
attempt: number,
10718-
) {
10719-
let delay: number;
10720-
if (completion.kind === "permanent_failure" || completion.reasonCode === "unknown_failure") {
10721-
const steps = [60_000, 5 * 60_000, 15 * 60_000, 30 * 60_000];
10722-
delay = steps[Math.min(attempt - 1, steps.length - 1)];
10723-
} else {
10724-
const maximum = completion.reasonCode === "github_rate_limit" ? 60 * 60_000 : 30 * 60_000;
10725-
delay = Math.min(maximum, 60_000 * 2 ** Math.min(attempt - 1, 6));
10726-
}
10727-
const hash = [...`${itemKey}:${attempt}`].reduce(
10728-
(current, character) => (current * 33 + character.charCodeAt(0)) >>> 0,
10729-
5381,
10730-
);
10731-
return delay + Math.floor(delay * ((hash % 21) / 100));
10732-
}
10733-
1073410680
function exactReviewDeadLetterId(item: ExactReviewQueueItem, ownedRevision?: number) {
1073510681
return `${item.key}@revision:${ownedRevision || item.leaseRevision || item.revision}`;
1073610682
}

‎src/clawsweeper-plan-command.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { resolve } from "node:path";
2+
import { boolArg, itemNumbersArg, numberArg, stringArg, type Args } from "./clawsweeper-args.js";
3+
import {
4+
DEFAULT_BACKFILL_REVIEW_AGE_MINUTES,
5+
DEFAULT_CODEX_MODEL,
6+
DEFAULT_REASONING_EFFORT,
7+
DEFAULT_SERVICE_TIER,
8+
} from "./clawsweeper-policy.js";
9+
import type { createReviewPlanning } from "./clawsweeper-review-planning.js";
10+
import type { RepositoryProfile } from "./repository-profiles.js";
11+
import { coverageTrackedItemIdsFromManifest } from "./review-coverage-manifest.js";
12+
13+
type PlanCandidates = ReturnType<typeof createReviewPlanning>["planCandidates"];
14+
15+
type PlanCommandDependencies = {
16+
defaultBatchSize: number;
17+
defaultItemsDir: () => string;
18+
defaultShardCount: number;
19+
planCandidates: PlanCandidates;
20+
repoFromArgs: (args: Args) => RepositoryProfile;
21+
reviewPolicyHash: (options: {
22+
model?: string;
23+
reasoningEffort?: string;
24+
sandboxMode?: string;
25+
serviceTier?: string;
26+
}) => string;
27+
targetProfile: () => RepositoryProfile;
28+
};
29+
30+
export function createPlanCommand(dependencies: PlanCommandDependencies): (args: Args) => void {
31+
return (args) => {
32+
dependencies.repoFromArgs(args);
33+
const itemsDir = resolve(stringArg(args.items_dir, dependencies.defaultItemsDir()));
34+
const batchSize = numberArg(args.batch_size, dependencies.defaultBatchSize);
35+
const maxPages = numberArg(args.max_pages, 250);
36+
const shardCount = numberArg(args.shard_count, dependencies.defaultShardCount);
37+
const minimumActiveShards = numberArg(args.min_active_shards, 0);
38+
const minimumBackfillReviewAgeMs =
39+
numberArg(args.min_backfill_review_age_minutes, DEFAULT_BACKFILL_REVIEW_AGE_MINUTES) *
40+
60 *
41+
1000;
42+
const itemNumbers = itemNumbersArg(args.item_numbers, args.item_number);
43+
const hasItemNumbersInput = typeof args.item_numbers === "string" && args.item_numbers.trim();
44+
const hotIntake = boolArg(args.hot_intake);
45+
const model = stringArg(args.codex_model, DEFAULT_CODEX_MODEL);
46+
const reasoningEffort = stringArg(args.codex_reasoning_effort, DEFAULT_REASONING_EFFORT);
47+
const sandboxMode = stringArg(args.codex_sandbox, "read-only");
48+
const serviceTier = stringArg(args.codex_service_tier, DEFAULT_SERVICE_TIER);
49+
const reviewPolicy = dependencies.reviewPolicyHash({
50+
model,
51+
reasoningEffort,
52+
sandboxMode,
53+
serviceTier,
54+
});
55+
const coverageManifest = stringArg(args.coverage_tracked_items_manifest, "").trim();
56+
const coverageTrackedItemIds = coverageManifest
57+
? coverageTrackedItemIdsFromManifest(
58+
resolve(coverageManifest),
59+
dependencies.targetProfile().slug,
60+
)
61+
: undefined;
62+
const planOptions: Parameters<PlanCandidates>[0] = {
63+
batchSize,
64+
maxPages,
65+
shardCount,
66+
itemsDir,
67+
reviewPolicy,
68+
minimumActiveShards,
69+
minimumBackfillReviewAgeMs,
70+
...(coverageTrackedItemIds ? { coverageTrackedItemIds } : {}),
71+
};
72+
if (hasItemNumbersInput || itemNumbers.length > 0) planOptions.itemNumbers = itemNumbers;
73+
if (hotIntake) planOptions.hotIntake = true;
74+
const plan = dependencies.planCandidates(planOptions);
75+
console.log(
76+
JSON.stringify(
77+
{
78+
...plan,
79+
reviewPolicy,
80+
matrix: plan.shards.map((shard) => ({
81+
shard: shard.shard,
82+
item_numbers: shard.itemNumbers.join(",") || "none",
83+
})),
84+
},
85+
null,
86+
2,
87+
),
88+
);
89+
};
90+
}

‎src/clawsweeper-runtime.ts‎

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync } from "node:fs";
44
import { dirname, join, relative, resolve } from "node:path";
55
import { fileURLToPath } from "node:url";
66
import { flushWorkflowActionEvents } from "./action-ledger-runtime.js";
7-
import {
8-
boolArg,
9-
itemNumbersArg,
10-
numberArg,
11-
parseArgs,
12-
stringArg,
13-
type Args,
14-
} from "./clawsweeper-args.js";
7+
import { parseArgs, stringArg, type Args } from "./clawsweeper-args.js";
158
import { dispatchCommand, type CommandHandler } from "./clawsweeper-command-dispatch.js";
169
import { createDecisionParser } from "./clawsweeper-decision-parser.js";
1710
import { runText } from "./command.js";
@@ -23,7 +16,6 @@ import {
2316
type RepositoryProfile,
2417
} from "./repository-profiles.js";
2518
import { reviewPullChecksDigestParts } from "./review-checks-digest.js";
26-
import { coverageTrackedItemIdsFromManifest } from "./review-coverage-manifest.js";
2719
import {
2820
reviewStructuralQuery,
2921
reviewStructuralRecordFromGraphql,
@@ -75,11 +67,9 @@ import {
7567
import { createLabelPolicy } from "./clawsweeper-label-policy.js";
7668
import { createRepositoryLinks } from "./clawsweeper-links.js";
7769
import { createLocalRangeReviewer } from "./clawsweeper-local-review.js";
70+
import { createPlanCommand } from "./clawsweeper-plan-command.js";
7871
import {
79-
DEFAULT_BACKFILL_REVIEW_AGE_MINUTES,
80-
DEFAULT_CODEX_MODEL,
8172
DEFAULT_REASONING_EFFORT,
82-
DEFAULT_SERVICE_TIER,
8373
EVENT_GUARDED_OPEN_ACTIONS,
8474
FRESH_DAYS,
8575
REVIEW_POLICY_VERSION,
@@ -1095,57 +1085,15 @@ function securityConcernLocation(concern: SecurityConcern): string {
10951085
return `${concern.file}${concern.line ? `:${concern.line}` : ""}`;
10961086
}
10971087

1098-
function planCommand(args: Args): void {
1099-
repoFromArgs(args);
1100-
const itemsDir = resolve(stringArg(args.items_dir, defaultItemsDir()));
1101-
const batchSize = numberArg(args.batch_size, DEFAULT_PLAN_BATCH_SIZE);
1102-
const maxPages = numberArg(args.max_pages, 250);
1103-
const shardCount = numberArg(args.shard_count, DEFAULT_PLAN_SHARD_COUNT);
1104-
const minimumActiveShards = numberArg(args.min_active_shards, 0);
1105-
const minimumBackfillReviewAgeMs =
1106-
numberArg(args.min_backfill_review_age_minutes, DEFAULT_BACKFILL_REVIEW_AGE_MINUTES) *
1107-
60 *
1108-
1000;
1109-
const itemNumbers = itemNumbersArg(args.item_numbers, args.item_number);
1110-
const hasItemNumbersInput = typeof args.item_numbers === "string" && args.item_numbers.trim();
1111-
const hotIntake = boolArg(args.hot_intake);
1112-
const model = stringArg(args.codex_model, DEFAULT_CODEX_MODEL);
1113-
const reasoningEffort = stringArg(args.codex_reasoning_effort, DEFAULT_REASONING_EFFORT);
1114-
const sandboxMode = stringArg(args.codex_sandbox, "read-only");
1115-
const serviceTier = stringArg(args.codex_service_tier, DEFAULT_SERVICE_TIER);
1116-
const reviewPolicy = reviewPolicyHash({ model, reasoningEffort, sandboxMode, serviceTier });
1117-
const coverageManifest = stringArg(args.coverage_tracked_items_manifest, "").trim();
1118-
const coverageTrackedItemIds = coverageManifest
1119-
? coverageTrackedItemIdsFromManifest(resolve(coverageManifest), targetProfile().slug)
1120-
: undefined;
1121-
const planOptions: Parameters<typeof planCandidates>[0] = {
1122-
batchSize,
1123-
maxPages,
1124-
shardCount,
1125-
itemsDir,
1126-
reviewPolicy,
1127-
minimumActiveShards,
1128-
minimumBackfillReviewAgeMs,
1129-
...(coverageTrackedItemIds ? { coverageTrackedItemIds } : {}),
1130-
};
1131-
if (hasItemNumbersInput || itemNumbers.length > 0) planOptions.itemNumbers = itemNumbers;
1132-
if (hotIntake) planOptions.hotIntake = true;
1133-
const plan = planCandidates(planOptions);
1134-
console.log(
1135-
JSON.stringify(
1136-
{
1137-
...plan,
1138-
reviewPolicy,
1139-
matrix: plan.shards.map((shard) => ({
1140-
shard: shard.shard,
1141-
item_numbers: shard.itemNumbers.join(",") || "none",
1142-
})),
1143-
},
1144-
null,
1145-
2,
1146-
),
1147-
);
1148-
}
1088+
const planCommand = createPlanCommand({
1089+
defaultBatchSize: DEFAULT_PLAN_BATCH_SIZE,
1090+
defaultItemsDir,
1091+
defaultShardCount: DEFAULT_PLAN_SHARD_COUNT,
1092+
planCandidates,
1093+
repoFromArgs,
1094+
reviewPolicyHash,
1095+
targetProfile,
1096+
});
11491097

11501098
// Offline local-range review: synthesize the Item + ItemContext from the local
11511099
// git range (merge-base(base, HEAD)..HEAD) so the FULL review (real-behavior

‎test/dashboard-worker.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7602,7 +7602,7 @@ test("Worker retains a committed terminal command acknowledgement for fenced ret
76027602
const claimedState = storage.sql.readNormalizedQueue() as {
76037603
items: Record<string, { publicationFailureAttempts?: number; firstFailureAt?: number }>;
76047604
};
7605-
claimedState.items[leased.key]!.publicationFailureAttempts = 11;
7605+
claimedState.items[leased.key]!.publicationFailureAttempts = 47;
76067606
claimedState.items[leased.key]!.firstFailureAt = Date.now();
76077607
storage.sql.replaceNormalizedQueue(claimedState);
76087608
const committed = await queue.fetch(
@@ -17740,12 +17740,18 @@ test("exact-review publication gives state contention the transient retry budget
1774017740
const state = (await storage.get("exact-review-queue")) as {
1774117741
items: Record<
1774217742
string,
17743-
{ state: string; lastFailureReason?: string; publicationFailureAttempts?: number }
17743+
{
17744+
state: string;
17745+
lastFailureReason?: string;
17746+
publicationFailureAttempts?: number;
17747+
nextAttemptAt?: number;
17748+
}
1774417749
>;
1774517750
};
1774617751
assert.equal(state.items[item.key]?.state, "pending");
1774717752
assert.equal(state.items[item.key]?.lastFailureReason, "state_contention");
1774817753
assert.equal(state.items[item.key]?.publicationFailureAttempts, 5);
17754+
assert.ok((state.items[item.key]?.nextAttemptAt ?? Infinity) <= Date.now() + 6 * 60_000);
1774917755

1775017756
const stats = await (
1775117757
await queue.fetch(new Request("https://clawsweeper-exact-review-queue/stats"))

0 commit comments

Comments
 (0)