Skip to content

Commit c43ca6f

Browse files
committed
fix(recovery): rotate placeholder discovery cursors
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 81c23be commit c43ca6f

14 files changed

Lines changed: 1200 additions & 172 deletions

‎.github/workflows/exact-review-reconcile.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ jobs:
233233
GH_TOKEN: ${{ github.token }}
234234
TARGET_WRITE_TOKEN: ${{ steps.target-write-token.outputs.token }}
235235
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
236+
REVIEW_PLACEHOLDER_CURSOR_STORE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
236237
REVIEW_PLACEHOLDER_LOOKBACK_HOURS: ${{ vars.REVIEW_PLACEHOLDER_LOOKBACK_HOURS || '48' }}
237238
REVIEW_PLACEHOLDER_MAX_CHECKS: ${{ vars.REVIEW_PLACEHOLDER_MAX_CHECKS || '20' }}
238239
REVIEW_PLACEHOLDER_MAX_RECOVERIES: ${{ vars.REVIEW_PLACEHOLDER_MAX_RECOVERIES || '5' }}

‎dashboard/exact-review-queue.ts‎

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,16 @@ const EXACT_REVIEW_QUEUE_ROLLBACK_CLOCK_SKEW_MS = 5 * 60 * 1000;
489489
const EXACT_REVIEW_QUEUE_LEGACY_GENERATION_PREFIX = "__clawsweeper_sql_generation:";
490490
const EXACT_REVIEW_QUEUE_STATE_KEY = "exact-review-queue";
491491
const EXACT_REVIEW_SOURCE_AUTHORITY_SEQUENCE_KEY = "exact-review-source-authority-sequence:v1";
492-
const TARGET_FANOUT_CURSOR_KEY_PREFIX = "target-fanout-cursor:v1:";
493-
type TargetFanoutMode = "hot-intake" | "normal-review" | "audit";
494-
type TargetFanoutCursor = {
495-
mode: TargetFanoutMode;
492+
// Preserve the original key prefix so existing fanout cursors survive the
493+
// broader operational-cursor contract introduced for bounded recovery lanes.
494+
const OPERATIONAL_CURSOR_KEY_PREFIX = "target-fanout-cursor:v1:";
495+
type OperationalCursorMode =
496+
| "hot-intake"
497+
| "normal-review"
498+
| "audit"
499+
| `review-placeholder-${string}-${"open" | "closed"}`;
500+
type OperationalCursor = {
501+
mode: OperationalCursorMode;
496502
nextCursor: number;
497503
revision: number;
498504
updatedAt: number;
@@ -632,12 +638,15 @@ export class ExactReviewQueue {
632638
this.reviewCoverageCache = null;
633639
return json({ ok: true, accepted: true }, 202);
634640
}
635-
const targetFanoutMode = targetFanoutModeFromPath(url.pathname);
636-
if (targetFanoutMode && request.method === "GET") {
637-
return this.readTargetFanoutCursor(targetFanoutMode);
641+
const operationalCursorMode = operationalCursorModeFromPath(url.pathname);
642+
if (operationalCursorMode && request.method === "GET") {
643+
return this.readOperationalCursor(operationalCursorMode);
638644
}
639-
if (targetFanoutMode && request.method === "PUT") {
640-
return this.writeTargetFanoutCursor(targetFanoutMode, await request.json().catch(() => null));
645+
if (operationalCursorMode && request.method === "PUT") {
646+
return this.writeOperationalCursor(
647+
operationalCursorMode,
648+
await request.json().catch(() => null),
649+
);
641650
}
642651
if (request.method === "GET" && url.pathname === "/recent-durable-publication-events") {
643652
const events = this.recentDurablePublicationEvents(
@@ -3100,13 +3109,13 @@ export class ExactReviewQueue {
31003109
return value;
31013110
}
31023111

3103-
private readTargetFanoutCursor(mode: TargetFanoutMode) {
3104-
const stored = readTargetFanoutCursor(this.storage.kv.get(targetFanoutCursorKey(mode)), mode);
3112+
private readOperationalCursor(mode: OperationalCursorMode) {
3113+
const stored = readOperationalCursor(this.storage.kv.get(operationalCursorKey(mode)), mode);
31053114
if (stored === "invalid") return json({ error: "fanout_cursor_store_invalid" }, 500);
3106-
return json(targetFanoutCursorJson(stored ?? emptyTargetFanoutCursor(mode)));
3115+
return json(operationalCursorJson(stored ?? emptyOperationalCursor(mode)));
31073116
}
31083117

3109-
private writeTargetFanoutCursor(mode: TargetFanoutMode, value: unknown) {
3118+
private writeOperationalCursor(mode: OperationalCursorMode, value: unknown) {
31103119
const body = objectValue(value);
31113120
const nextCursor = Number(body.next_cursor);
31123121
const expectedRevision = Number(body.expected_revision);
@@ -3120,38 +3129,35 @@ export class ExactReviewQueue {
31203129
}
31213130
try {
31223131
const result = this.storage.transactionSync(() => {
3123-
const stored = readTargetFanoutCursor(
3124-
this.storage.kv.get(targetFanoutCursorKey(mode)),
3125-
mode,
3126-
);
3132+
const stored = readOperationalCursor(this.storage.kv.get(operationalCursorKey(mode)), mode);
31273133
if (stored === "invalid") return { kind: "invalid" as const };
3128-
const current = stored ?? emptyTargetFanoutCursor(mode);
3134+
const current = stored ?? emptyOperationalCursor(mode);
31293135
if (current.revision !== expectedRevision) {
31303136
return { kind: "conflict" as const, current };
31313137
}
31323138
if (current.revision >= Number.MAX_SAFE_INTEGER) {
31333139
return { kind: "invalid" as const };
31343140
}
3135-
const next: TargetFanoutCursor = {
3141+
const next: OperationalCursor = {
31363142
mode,
31373143
nextCursor,
31383144
revision: current.revision + 1,
31393145
updatedAt: Date.now(),
31403146
};
3141-
this.storage.kv.put(targetFanoutCursorKey(mode), next);
3147+
this.storage.kv.put(operationalCursorKey(mode), next);
31423148
return { kind: "written" as const, cursor: next };
31433149
});
31443150
if (result.kind === "invalid") return json({ error: "fanout_cursor_store_invalid" }, 500);
31453151
if (result.kind === "conflict") {
31463152
return json(
31473153
{
31483154
error: "fanout_cursor_revision_conflict",
3149-
current: targetFanoutCursorJson(result.current),
3155+
current: operationalCursorJson(result.current),
31503156
},
31513157
409,
31523158
);
31533159
}
3154-
return json(targetFanoutCursorJson(result.cursor), 202);
3160+
return json(operationalCursorJson(result.cursor), 202);
31553161
} catch (error) {
31563162
console.error(`fanout cursor write failed: ${sanitizedServerError(error)}`);
31573163
return json({ error: "fanout_cursor_store_unavailable" }, 503);
@@ -13699,23 +13705,26 @@ function objectValue(value) {
1369913705
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
1370013706
}
1370113707

13702-
function targetFanoutModeFromPath(path: string): TargetFanoutMode | null {
13703-
const match = /^\/cursors\/(hot-intake|normal-review|audit)$/.exec(path);
13704-
return (match?.[1] as TargetFanoutMode | undefined) ?? null;
13708+
function operationalCursorModeFromPath(path: string): OperationalCursorMode | null {
13709+
const match =
13710+
/^\/cursors\/(hot-intake|normal-review|audit|review-placeholder-[a-f0-9]{16}-(?:open|closed))$/.exec(
13711+
path,
13712+
);
13713+
return (match?.[1] as OperationalCursorMode | undefined) ?? null;
1370513714
}
1370613715

13707-
function targetFanoutCursorKey(mode: TargetFanoutMode): string {
13708-
return `${TARGET_FANOUT_CURSOR_KEY_PREFIX}${mode}`;
13716+
function operationalCursorKey(mode: OperationalCursorMode): string {
13717+
return `${OPERATIONAL_CURSOR_KEY_PREFIX}${mode}`;
1370913718
}
1371013719

13711-
function emptyTargetFanoutCursor(mode: TargetFanoutMode): TargetFanoutCursor {
13720+
function emptyOperationalCursor(mode: OperationalCursorMode): OperationalCursor {
1371213721
return { mode, nextCursor: 0, revision: 0, updatedAt: 0 };
1371313722
}
1371413723

13715-
function readTargetFanoutCursor(
13724+
function readOperationalCursor(
1371613725
value: unknown,
13717-
mode: TargetFanoutMode,
13718-
): TargetFanoutCursor | "invalid" | null {
13726+
mode: OperationalCursorMode,
13727+
): OperationalCursor | "invalid" | null {
1371913728
if (value === undefined) return null;
1372013729
const cursor = objectValue(value);
1372113730
const nextCursor = Number(cursor.nextCursor);
@@ -13735,7 +13744,7 @@ function readTargetFanoutCursor(
1373513744
return { mode, nextCursor, revision, updatedAt };
1373613745
}
1373713746

13738-
function targetFanoutCursorJson(cursor: TargetFanoutCursor) {
13747+
function operationalCursorJson(cursor: OperationalCursor) {
1373913748
return {
1374013749
ok: true,
1374113750
mode: cursor.mode,

‎dashboard/worker.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,11 @@ export default {
551551
return authenticatedExactReviewQueueRequest(request, env, "/source-authority");
552552
if (url.pathname === "/internal/review-coverage/inventory" && request.method === "POST")
553553
return authenticatedExactReviewQueueRequest(request, env, "/review-coverage/inventory");
554-
const fanoutCursorPath = /^\/internal\/state\/cursors\/(hot-intake|normal-review|audit)$/.exec(
555-
url.pathname,
556-
);
557-
if (fanoutCursorPath && (request.method === "GET" || request.method === "PUT"))
554+
const operationalCursorPath =
555+
/^\/internal\/state\/cursors\/(hot-intake|normal-review|audit|review-placeholder-[a-f0-9]{16}-(?:open|closed))$/.exec(
556+
url.pathname,
557+
);
558+
if (operationalCursorPath && (request.method === "GET" || request.method === "PUT"))
558559
return authenticatedExactReviewQueueCursorRequest(
559560
request,
560561
env,

‎docs/limits.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ and hot intake `14`. Existing repair lanes keep their
318318
the two most recently updated pages for sponsorship commands, then alternates
319319
newest/oldest created-order scans. Reaction-only revival can lag in very large
320320
archives; raise this override to scan deeper per run.
321-
- `REVIEW_PLACEHOLDER_MAX_CHECKS` overrides the number of open search candidates
322-
examined by each 15-minute orphaned-placeholder recovery pass; the default is
323-
20 and the maximum is 1000.
321+
- `REVIEW_PLACEHOLDER_MAX_CHECKS` overrides the number of search candidates
322+
examined per open and closed state class by each 15-minute
323+
orphaned-placeholder recovery pass; the default is 20 and the maximum is
324+
1000. Independent durable cursors rotate both state classes so non-actionable
325+
matches cannot permanently pin later placeholders behind the bounded pass.
324326
- `REVIEW_PLACEHOLDER_MIN_AGE_HOURS` overrides how old the latest ClawSweeper bot
325327
review-start placeholder must be before recovery; the default is 2 hours and
326328
the maximum is 720 hours.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Review-placeholder cursor rotation proof contract
2+
3+
## Claim
4+
5+
Bounded placeholder recovery retains independent open and closed discovery
6+
offsets in the authenticated ExactReviewQueue cursor store. Successive runs
7+
inspect distinct GitHub Search windows, a complete rotation returns to the
8+
first window, and a revision-CAS conflict leaves the old offset available for a
9+
safe retry instead of skipping candidates.
10+
11+
## Exercised surface
12+
13+
`run-proof.sh` starts the real dashboard Worker with local Wrangler and a
14+
disposable persisted SQLite Durable Object. The proof then drives the compiled
15+
recovery implementation through its normal network boundary:
16+
17+
```text
18+
runReviewPlaceholderRecovery()
19+
-> signed GET /internal/state/cursors/<open-or-closed-mode>
20+
-> bounded GitHub Search and live comment validation
21+
-> signed POST /internal/exact-review/enqueue
22+
-> signed PUT /internal/state/cursors/<open-or-closed-mode>
23+
-> Worker authentication and ExactReviewQueue revision-CAS storage
24+
```
25+
26+
Only GitHub is substituted with deterministic responses. Cursor reads/writes
27+
and enqueue requests cross a real local HTTP server, the Worker router, HMAC
28+
authentication, and the Wrangler Durable Object implementation. The cursor
29+
client requires an HTTPS production URL, so the injected transport rewrites
30+
only the disposable Wrangler origin's scheme from `https` to `http`; the URL
31+
path, method, body, and signature are unchanged.
32+
33+
## Controlled scenarios
34+
35+
The rotation scenario supplies 180 open and 180 closed search matches with a
36+
60-candidate budget per state. Three runs must inspect offsets `0..59`,
37+
`60..119`, and `120..179`; the next run must revisit `0..59`. A closed orphan at
38+
rank 144 is cleaned only in the third window. An open placeholder in the first
39+
window is initially younger than the two-hour activity guard and is enqueued
40+
only after the cursor wraps and the synthetic clock advances.
41+
42+
The CAS scenario uses a new repository cursor. Immediately before recovery
43+
persists offset 1, the harness sends a correctly signed competing write that
44+
keeps offset 0 but advances the revision. Recovery's stale write receives 409.
45+
The next invocation reads offset 0 again, revalidates the same candidate, and
46+
the real queue accepts the repeated idempotent enqueue before offset 1 is
47+
persisted.
48+
49+
## Command and artifacts
50+
51+
```bash
52+
docs/proof/review-placeholder-cursor-rotation/run-proof.sh
53+
```
54+
55+
The script requires Node 24+, pnpm, and a local Wrangler runtime. It writes
56+
`proof-summary.json`, `runtime-transcript.md`, build output, focused test output,
57+
and the Wrangler log beneath
58+
`.artifacts/review-placeholder-cursor-rotation/` by default.
59+
60+
## Expected observation
61+
62+
```text
63+
rotation windows: open=1001..1060 | 1061..1120 | 1121..1180 | 1001..1060
64+
cursor offsets: 60 -> 120 -> 0 -> 60
65+
closed cleanup: rank 144
66+
age guard: first-window open placeholder enqueued only after wrap
67+
CAS retry: checked 8001 twice; offset stayed 0 after 409, then advanced to 1
68+
RESULT: PASS
69+
```
70+
71+
## Limits
72+
73+
This is controlled local Worker/Durable Object evidence, not a production
74+
deployment trace. GitHub Search, comment reads, and destructive deletion are
75+
modeled with synthetic data, and no live repository, workflow, Cloudflare
76+
account, secret, queue, R2 object, or production Durable Object is contacted.
77+
The proof establishes the real local authenticated routing and persistence
78+
contract but cannot establish production network availability or GitHub Search
79+
ordering under concurrent live mutations.

0 commit comments

Comments
 (0)