Skip to content

Commit bf762f3

Browse files
authored
feat(worker): webhook-materialized read model replaces residual polling (#1167)
Quota campaign PR 5/5: webhook-materialized read model - the pollers stop re-asking GitHub for facts the App already delivered. Typed ingress (extending the #1132 discriminated union) now covers full issue lifecycle, all issue-comment create/edit/delete including non-commands, PR reviews and review comments, workflow runs/jobs, and check runs/suites. The queue DO materializes bounded normalized snapshots: per-item metadata, ordered comment collections with tombstones, review-activity digests, and workflow state - GUID dedupe, monotonic watermarks (out-of-order deliveries cannot regress state), gap detection, and per-class TTLs (items 15m, comments/reviews 30m, workflows 5m, placeholder census 6h). Reads serve "snapshot as of watermark W" with staleness attached. Migrated pollers (measured residual was item_metadata ~2.2k/h + comments ~750/h post PRs 1-4), counted on fixtures: planning 1->0, dashboard health 8->0, placeholder discovery 3->0, router hydration 1->0 GitHub requests on warm snapshots; each keeps a bounded repair poll for gap healing. Hard safety line proven: mutation guards and two-sided lease checks bypass the read model entirely (zero read-model guard hits observed; live hits confirmed). Degradation contract: event classes the GitHub App does not deliver keep their poll paths and emit structured telemetry. REQUIRED APP SUBSCRIPTIONS for full effect: Issues, Pull requests, Issue comments, PR reviews, PR review comments, Workflow runs, Workflow jobs, Check runs, Check suites. Proof: signed loopback deliveries (out-of-order + duplicate GUIDs), poller equivalence with counted formulas, safety fixtures; 59/59 focused, full gate 3455 passed; Crabbox local-container receipt at the final head, machine-verified provenance. Verdict clean (no actionable findings, proof sufficient). Merge note: the red "automerge quota-fixture" check fails identically on origin/main (inherited, tracked separately).
1 parent 386730f commit bf762f3

29 files changed

Lines changed: 3756 additions & 78 deletions

‎config/documentation-site.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"limits.md",
2626
"public-api.md",
2727
"github-egress-telemetry.md",
28+
"github-webhook-read-model.md",
2829
"operator-configuration.md",
2930
"live-dashboard.md",
3031
"openclaw-bay-demo.md",

‎dashboard/exact-review-queue.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import {
7474
import { recentDurablePublicationEvents } from "./recent-durable-publication-events.ts";
7575
import { sanitizedServerError } from "./error-safety.ts";
7676
import { GithubEtagResponseStore } from "./github-etag-cache.ts";
77+
import { GithubWebhookReadModelStore } from "./github-webhook-read-model.ts";
7778
import { githubEtagCacheKeyFromValue } from "../src/github-etag-cache-contract.ts";
7879
import {
7980
ExactReviewArtifactReceiptStore,
@@ -668,6 +669,7 @@ export class ExactReviewQueue {
668669
private commandIntakeStore;
669670
private artifactReceiptStore;
670671
private githubEtagResponseStore;
672+
private githubWebhookReadModelStore;
671673
private readonly random: () => number;
672674
private readonly baselines = new WeakMap<ExactReviewQueueState, ExactReviewQueueBaseline>();
673675
private reviewCoverageCache: { at: number; summary: ReviewCoverageSummary } | null = null;
@@ -698,6 +700,7 @@ export class ExactReviewQueue {
698700
env.STATE_SNAPSHOTS,
699701
);
700702
this.githubEtagResponseStore = new GithubEtagResponseStore(this.storage);
703+
this.githubWebhookReadModelStore = new GithubWebhookReadModelStore(this.storage);
701704
// Direct in-process users retain the established eager setup behavior.
702705
// A real Durable Object has blockConcurrencyWhile; defer that setup until a
703706
// non-Bay request so constructing it for the public pure reader cannot
@@ -3046,6 +3049,146 @@ export class ExactReviewQueue {
30463049
return json(result);
30473050
}
30483051

3052+
if (request.method === "POST" && url.pathname === "/github-read-model/ingest") {
3053+
try {
3054+
return json(
3055+
this.githubWebhookReadModelStore.ingest(await request.json().catch(() => null)),
3056+
202,
3057+
);
3058+
} catch (error) {
3059+
return json({ error: sanitizedServerError(error) }, 400);
3060+
}
3061+
}
3062+
3063+
if (request.method === "POST" && url.pathname === "/github-read-model/lease-item") {
3064+
const body = objectValue(await request.json().catch(() => null));
3065+
const itemKey = String(body.item_key || "").trim();
3066+
const leaseId = String(body.lease_id || "").trim();
3067+
const leaseRevision = Number(body.lease_revision);
3068+
const claimGeneration = Number(body.claim_generation);
3069+
const runId = String(body.run_id || "").trim();
3070+
const runAttempt = exactReviewRunAttempt(body.run_attempt);
3071+
const sourceHeadSha = String(body.source_head_sha || "")
3072+
.trim()
3073+
.toLowerCase();
3074+
const repository = String(body.repository || "")
3075+
.trim()
3076+
.toLowerCase();
3077+
const number = Number(body.number);
3078+
if (
3079+
!itemKey ||
3080+
!leaseId ||
3081+
!/^\d+$/.test(runId) ||
3082+
!Number.isInteger(leaseRevision) ||
3083+
leaseRevision < 1 ||
3084+
!Number.isInteger(claimGeneration) ||
3085+
claimGeneration < 1 ||
3086+
runAttempt === null ||
3087+
!/^[a-z0-9_.-]+\/[a-z0-9_.-]+$/.test(repository) ||
3088+
!Number.isSafeInteger(number) ||
3089+
number < 1 ||
3090+
(sourceHeadSha && !/^[0-9a-f]{40}$/.test(sourceHeadSha))
3091+
) {
3092+
return json({ error: "invalid_lease_read_tuple" }, 400);
3093+
}
3094+
const item = this.readStateSync().items[itemKey];
3095+
const now = Date.now();
3096+
if (
3097+
!item ||
3098+
item.state !== "leased" ||
3099+
item.leaseId !== leaseId ||
3100+
item.leaseRevision !== leaseRevision ||
3101+
exactReviewClaimGeneration(item.claimGeneration) !== claimGeneration ||
3102+
item.claimedRunId !== runId ||
3103+
item.claimedRunAttempt !== runAttempt ||
3104+
item.decision.targetRepo.toLowerCase() !== repository ||
3105+
item.decision.itemNumber !== number ||
3106+
(item.leaseDecision?.sourceHeadSha &&
3107+
item.leaseDecision.sourceHeadSha.toLowerCase() !== sourceHeadSha) ||
3108+
!isLiveExactReviewLease(
3109+
item,
3110+
now,
3111+
exactReviewPublicationDispatchLeaseMs(this.env),
3112+
exactReviewHeartbeatGraceMs(this.env),
3113+
)
3114+
) {
3115+
return json({ error: "lease_not_active" }, 409);
3116+
}
3117+
return json({
3118+
...(await this.githubWebhookReadModelStore.readItem({ repository, number }, now)),
3119+
lease_authorized: true,
3120+
});
3121+
}
3122+
3123+
if (request.method === "POST" && url.pathname === "/github-read-model/repair") {
3124+
try {
3125+
return json(
3126+
this.githubWebhookReadModelStore.repair(await request.json().catch(() => null)),
3127+
202,
3128+
);
3129+
} catch (error) {
3130+
return json({ error: sanitizedServerError(error) }, 400);
3131+
}
3132+
}
3133+
3134+
if (request.method === "POST" && url.pathname === "/github-read-model/item") {
3135+
try {
3136+
return json(
3137+
await this.githubWebhookReadModelStore.readItem(await request.json().catch(() => null)),
3138+
);
3139+
} catch (error) {
3140+
return json({ error: sanitizedServerError(error) }, 400);
3141+
}
3142+
}
3143+
3144+
if (request.method === "POST" && url.pathname === "/github-read-model/comments") {
3145+
try {
3146+
return json(
3147+
await this.githubWebhookReadModelStore.readComments(
3148+
await request.json().catch(() => null),
3149+
),
3150+
);
3151+
} catch (error) {
3152+
return json({ error: sanitizedServerError(error) }, 400);
3153+
}
3154+
}
3155+
3156+
if (request.method === "POST" && url.pathname === "/github-read-model/activity") {
3157+
try {
3158+
return json(
3159+
await this.githubWebhookReadModelStore.readActivity(
3160+
await request.json().catch(() => null),
3161+
),
3162+
);
3163+
} catch (error) {
3164+
return json({ error: sanitizedServerError(error) }, 400);
3165+
}
3166+
}
3167+
3168+
if (request.method === "POST" && url.pathname === "/github-read-model/workflows") {
3169+
try {
3170+
return json(
3171+
await this.githubWebhookReadModelStore.readWorkflows(
3172+
await request.json().catch(() => null),
3173+
),
3174+
);
3175+
} catch (error) {
3176+
return json({ error: sanitizedServerError(error) }, 400);
3177+
}
3178+
}
3179+
3180+
if (request.method === "POST" && url.pathname === "/github-read-model/placeholders") {
3181+
try {
3182+
return json(
3183+
await this.githubWebhookReadModelStore.readPlaceholders(
3184+
await request.json().catch(() => null),
3185+
),
3186+
);
3187+
} catch (error) {
3188+
return json({ error: sanitizedServerError(error) }, 400);
3189+
}
3190+
}
3191+
30493192
if (request.method === "POST" && url.pathname === "/artifact-cache/receipt/store") {
30503193
const body = await request.json().catch(() => null);
30513194
const now = Date.now();
@@ -7602,6 +7745,7 @@ export class ExactReviewQueue {
76027745
this.githubEgressTelemetryStore.ensureSchemaSync();
76037746
this.artifactReceiptStore.ensureSchemaSync();
76047747
this.githubEtagResponseStore.ensureSchemaSync();
7748+
this.githubWebhookReadModelStore.ensureSchemaSync();
76057749
let meta = this.readStorageMetaSync();
76067750
let migratedLegacy = false;
76077751
const legacy = this.storage.kv.get(EXACT_REVIEW_QUEUE_STATE_KEY) as

0 commit comments

Comments
 (0)