Skip to content

Commit 6494ab5

Browse files
authored
fix(health): per-run confirmation with bounded revalidation kills phantom queued runs (#1169)
Fixes the phantom queued run that put operational health into a false "degraded" state after #1167 (production evidence: queued_over_threshold=1 aging 54->58->61min across probes while zero queued runs older than 30min existed in any of the nine involved repos - the App's Workflow runs/jobs subscriptions are not yet enabled, so a run whose terminal transition fell between repair polls stuck in the snapshot forever). Root cause: repository-wide census freshness let unconfirmed queued rows reach health calculation with no per-run confirmation age. Fix, hardened across three review rounds: per-run confirmation timestamps; bounded exact rechecks (batch of 10 per refresh, oldest-stale first, two five-way waves under the 20s cadence - a 200-stale-row snapshot now issues exactly 10 reads instead of 205 and stays fast); race-safe terminal/404 eviction with telemetry; verification failures report unknown, never a phantom alert; omitted unconfirmed rows are unknown and never count toward thresholds; live polling remains authoritative without subscriptions; no rechecks beyond the 24h zombie boundary. Genuine stuck runs still alert. Proof: red reproduced the phantom-degraded state and the 205-read stall; green 22/22 focused, backlog drains in 21 refreshes; full gate 3466/0 host and container; Crabbox local-container receipt at this exact head, machine-verified provenance. Verdict clean (no actionable correctness finding) on 26d446e.
1 parent e17e094 commit 6494ab5

14 files changed

Lines changed: 2599 additions & 33 deletions

‎dashboard/github-webhook-read-model.ts‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ export class GithubWebhookReadModelStore {
444444
});
445445
}
446446

447-
repair(value: unknown, now = Date.now()): { accepted: boolean; watermark: number } {
447+
repair(
448+
value: unknown,
449+
now = Date.now(),
450+
): { accepted: boolean; watermark: number; evicted_workflow_runs: number } {
448451
const body = record(value);
449452
const repository = normalizedRepository(body.repository);
450453
const repairKind = stringValue(body.repair_kind);
@@ -465,6 +468,10 @@ export class GithubWebhookReadModelStore {
465468
? body.complete_workflow_job_runs.map(positiveInteger).filter(nonNullable)
466469
: [];
467470
const workflowJobCensusBoundary = workflowJobCensusStartedAt ?? 0;
471+
const workflowRunVerificationStartedAt = timestampMs(body.workflow_run_verification_started_at);
472+
const evictWorkflowRunIds = Array.isArray(body.evict_workflow_run_ids)
473+
? body.evict_workflow_run_ids.map(positiveInteger).filter(nonNullable)
474+
: [];
468475
if (
469476
!repository ||
470477
!new Set(["items", "comments", "placeholders", "workflows"]).has(repairKind)
@@ -474,14 +481,55 @@ export class GithubWebhookReadModelStore {
474481
if (objects.some((object) => object.repository !== repository)) {
475482
throw new Error("github_webhook_read_model_repair_repository_mismatch");
476483
}
484+
if (
485+
evictWorkflowRunIds.length > 0 &&
486+
(repairKind !== "workflows" || workflowRunVerificationStartedAt === null)
487+
) {
488+
throw new Error("invalid_github_webhook_read_model_workflow_eviction");
489+
}
477490
return this.storage.transactionSync(() => {
478491
const watermark = this.watermarkSync() + 1;
492+
let evictedWorkflowRuns = 0;
479493
this.storage.sql.exec(
480494
`UPDATE github_webhook_read_model_meta_v1 SET watermark = ?, updated_at = ? WHERE singleton_id = 1`,
481495
watermark,
482496
now,
483497
);
484498
const deliveryId = `repair:${repairKind}:${repository}:${watermark}`;
499+
for (const runId of new Set(evictWorkflowRunIds)) {
500+
const row = firstRow(
501+
this.storage.sql.exec(
502+
`SELECT received_at FROM github_webhook_read_model_workflows_v1
503+
WHERE repository = ? AND object_kind = 'workflow_run' AND object_id = ?`,
504+
repository,
505+
runId,
506+
),
507+
);
508+
if (!row || Number(row.received_at) > workflowRunVerificationStartedAt!) continue;
509+
this.storage.sql.exec(
510+
`DELETE FROM github_webhook_read_model_workflows_v1
511+
WHERE repository = ? AND object_kind = 'workflow_run' AND object_id = ?
512+
AND received_at <= ?`,
513+
repository,
514+
runId,
515+
workflowRunVerificationStartedAt,
516+
);
517+
this.storage.sql.exec(
518+
`DELETE FROM github_webhook_read_model_workflows_v1
519+
WHERE repository = ? AND run_id = ? AND object_kind <> 'workflow_run'
520+
AND received_at <= ?`,
521+
repository,
522+
runId,
523+
workflowRunVerificationStartedAt,
524+
);
525+
this.storage.sql.exec(
526+
`DELETE FROM github_webhook_read_model_workflow_coverage_v1
527+
WHERE repository = ? AND coverage_kind = 'run_jobs' AND run_id = ?`,
528+
repository,
529+
runId,
530+
);
531+
evictedWorkflowRuns += 1;
532+
}
485533
if (workflowRunCensusComplete) {
486534
const liveRunIds = new Set(
487535
objects
@@ -597,7 +645,7 @@ export class GithubWebhookReadModelStore {
597645
watermark,
598646
);
599647
this.pruneSync(now);
600-
return { accepted: true, watermark };
648+
return { accepted: true, watermark, evicted_workflow_runs: evictedWorkflowRuns };
601649
});
602650
}
603651

@@ -796,7 +844,7 @@ export class GithubWebhookReadModelStore {
796844
if (!repository) throw new Error("invalid_github_webhook_read_model_workflows_request");
797845
const rows = Array.from(
798846
this.storage.sql.exec(
799-
`SELECT object_kind, run_id, snapshot_json, source_updated_at, received_at
847+
`SELECT object_kind, object_id, run_id, snapshot_json, source_updated_at, received_at
800848
FROM github_webhook_read_model_workflows_v1
801849
WHERE repository = ? ORDER BY source_updated_at DESC, object_id DESC`,
802850
repository,
@@ -845,6 +893,13 @@ export class GithubWebhookReadModelStore {
845893
usable: Boolean(runCensus) && classState.available && !workflowFreshness.stale,
846894
freshness: workflowFreshness,
847895
runs: values.filter((entry) => entry.kind === "workflow_run").map((entry) => entry.value),
896+
run_observations: rows
897+
.filter((row) => row.object_kind === "workflow_run")
898+
.map((row) => ({
899+
run_id: Number(row.object_id),
900+
source_updated_at: new Date(Number(row.source_updated_at)).toISOString(),
901+
confirmed_at: new Date(Number(row.received_at)).toISOString(),
902+
})),
848903
jobs: values.filter((entry) => entry.kind === "workflow_job").map((entry) => entry.value),
849904
checks: values
850905
.filter((entry) => entry.kind === "check_run" || entry.kind === "check_suite")
@@ -1060,13 +1115,24 @@ export class GithubWebhookReadModelStore {
10601115
delivery_id, watermark, received_at)
10611116
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
10621117
ON CONFLICT (repository, object_kind, object_id) DO UPDATE SET
1063-
run_id = excluded.run_id,
1064-
source_updated_at = excluded.source_updated_at,
1065-
snapshot_json = excluded.snapshot_json,
1118+
run_id = CASE
1119+
WHEN excluded.source_updated_at >= github_webhook_read_model_workflows_v1.source_updated_at
1120+
THEN excluded.run_id
1121+
ELSE github_webhook_read_model_workflows_v1.run_id
1122+
END,
1123+
source_updated_at = MAX(
1124+
github_webhook_read_model_workflows_v1.source_updated_at,
1125+
excluded.source_updated_at
1126+
),
1127+
snapshot_json = CASE
1128+
WHEN excluded.source_updated_at >= github_webhook_read_model_workflows_v1.source_updated_at
1129+
THEN excluded.snapshot_json
1130+
ELSE github_webhook_read_model_workflows_v1.snapshot_json
1131+
END,
10661132
delivery_id = excluded.delivery_id,
10671133
watermark = excluded.watermark,
1068-
received_at = excluded.received_at
1069-
WHERE excluded.source_updated_at > github_webhook_read_model_workflows_v1.source_updated_at`,
1134+
received_at = MAX(github_webhook_read_model_workflows_v1.received_at, excluded.received_at)
1135+
WHERE excluded.source_updated_at >= github_webhook_read_model_workflows_v1.source_updated_at`,
10701136
object.repository,
10711137
object.kind,
10721138
object.id,

0 commit comments

Comments
 (0)