Skip to content

Commit ff2afbe

Browse files
committed
fix(bay): measure review journeys to final receipt
1 parent ed20a56 commit ff2afbe

14 files changed

Lines changed: 760 additions & 165 deletions

‎dashboard/bay-page.ts‎

Lines changed: 11 additions & 9 deletions
Large diffs are not rendered by default.

‎dashboard/exact-review-lifecycle-telemetry.ts‎

Lines changed: 163 additions & 56 deletions
Large diffs are not rendered by default.

‎dashboard/exact-review-lifecycle.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ export class ExactReviewLifecycleProjectionStore {
812812
throw new Error("conflicting lifecycle acknowledgement receipt");
813813
}
814814
projection.acknowledgement.observed ??= observed;
815+
// The final receipt is the only timing boundary for command journeys.
816+
// Queue its Bay materialization in this same source transaction so a
817+
// Durable Object restart cannot persist the receipt without its outbox
818+
// marker.
819+
projection.bayTelemetryPending = true;
815820
projection.updatedAt = input.observedAt;
816821
this.writeSync(projection);
817822
return {
@@ -1280,9 +1285,17 @@ export class ExactReviewLifecycleProjectionStore {
12801285
return !more;
12811286
}
12821287

1288+
markBayTelemetryPending(input: ProjectionIdentity) {
1289+
this.validateIdentity(input);
1290+
return this.mutate(input, (projection) => {
1291+
projection.bayTelemetryPending = true;
1292+
return projection;
1293+
});
1294+
}
1295+
12831296
markBayTelemetryMaterialized(input: ProjectionIdentity & { bayTelemetryEventId?: string }) {
12841297
this.validateIdentity(input);
1285-
if (input.bayTelemetryEventId !== undefined && !validText(input.bayTelemetryEventId, 1, 533)) {
1298+
if (input.bayTelemetryEventId !== undefined && !validText(input.bayTelemetryEventId, 1, 536)) {
12861299
throw new Error("invalid lifecycle Bay telemetry event identity");
12871300
}
12881301
return this.mutate(input, (projection) => {
@@ -1730,9 +1743,9 @@ function validDurableLifecycleBayProjection(value: ExactReviewLifecycleProjectio
17301743
!positiveInteger(value.revision) ||
17311744
!finiteTimestamp(value.updatedAt) ||
17321745
typeof value.bayTelemetryPending !== "boolean" ||
1733-
// `bay:` + a valid 512-character fence key + `:` + a safe-integer
1746+
// `bay:v2:` + a valid 512-character fence key + `:` + a safe-integer
17341747
// revision (up to 16 decimal digits).
1735-
(value.bayTelemetryEventId !== undefined && !validText(value.bayTelemetryEventId, 1, 533)) ||
1748+
(value.bayTelemetryEventId !== undefined && !validText(value.bayTelemetryEventId, 1, 536)) ||
17361749
!validText(value.admission.deliveryId, 1, 300) ||
17371750
(value.admission.sourceDeliveryId !== undefined &&
17381751
!validText(value.admission.sourceDeliveryId, 1, 200)) ||

‎dashboard/exact-review-queue.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7695,6 +7695,8 @@ export class ExactReviewQueue {
76957695
const includeDeliveryIdentity = body.include_delivery_identity;
76967696
const requireExactStatusComment = body.require_exact_status_comment;
76977697
const observedAt = Number(body.observed_at);
7698+
const receivedAt = Date.now();
7699+
const recordedAt = Math.min(observedAt, receivedAt);
76987700
if (
76997701
!canonicalTargetKey ||
77007702
(fenceKey === undefined) !== (revision === undefined) ||
@@ -7725,8 +7727,17 @@ export class ExactReviewQueue {
77257727
completionCommentId,
77267728
...(statusCommentId === undefined ? {} : { statusCommentId: Number(statusCommentId) }),
77277729
...(requireExactStatusComment ? { requireExactStatusComment: true } : {}),
7728-
observedAt,
7730+
observedAt: recordedAt,
77297731
});
7732+
// The final command-status receipt is the end-to-end Bay boundary. The
7733+
// routing terminal was recorded earlier and must not publish a duration
7734+
// until this correlated receipt has arrived.
7735+
if (result.accepted && result.projection) {
7736+
// observeCommandAcknowledgement persists the observed receipt and its
7737+
// outbox marker atomically. Materialization may then fail and replay
7738+
// safely, but it cannot lose this timing boundary between writes.
7739+
this.syncBayLifecycle(result.projection);
7740+
}
77307741
if (result.accepted && result.projection) {
77317742
const state = this.readStateSync();
77327743
const drivers = Object.values(state.items).filter((item) => {
@@ -7741,7 +7752,7 @@ export class ExactReviewQueue {
77417752
if (drivers.length) {
77427753
for (const driver of drivers) delete state.items[driver.key];
77437754
await this.writeState(state);
7744-
await this.scheduleNext(state, observedAt);
7755+
await this.scheduleNext(state, recordedAt);
77457756
}
77467757
}
77477758
return json({

‎dashboard/worker.ts‎

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,7 @@ const PUBLIC_STATUS_TEXT_VALUES = new Set([
14581458
"completed_exact_review_lifecycles",
14591459
"completed_review_journeys",
14601460
"durable_exact_review_lifecycles",
1461+
"verified_final_review_receipts",
14611462
"degraded",
14621463
"exact-review",
14631464
"6h",
@@ -1531,6 +1532,7 @@ const PUBLIC_STATUS_TEXT_VALUES = new Set([
15311532

15321533
const PUBLIC_STATUS_TEXT_FIELDS = new Set([
15331534
"conclusion",
1535+
"completion_source",
15341536
"mode",
15351537
"outcome",
15361538
"reason",
@@ -1548,6 +1550,7 @@ const PUBLIC_STATUS_TEXT_FIELDS = new Set([
15481550
const PUBLIC_STATUS_TIME_FIELDS = new Set([
15491551
"at",
15501552
"completed_at",
1553+
"ended_at",
15511554
"generated_at",
15521555
"observed_at",
15531556
"oldest_at",
@@ -1686,6 +1689,8 @@ const PUBLIC_STATUS_COUNT_FIELDS = new Set([
16861689
"longest_duration_ms",
16871690
"maximum_age_ms",
16881691
"median_ms",
1692+
"journey_duration_ms",
1693+
"bucket_minutes",
16891694
"oldest_age_seconds",
16901695
"oldest_dispatching_age_seconds",
16911696
"oldest_leased_age_seconds",
@@ -1765,6 +1770,8 @@ const PUBLIC_STATUS_CONTAINER_FIELDS = new Set([
17651770
"timeline",
17661771
"ci",
17671772
"timings",
1773+
"history",
1774+
"points",
17681775
"overall",
17691776
"terminal_buffer",
17701777
"recently_washed",
@@ -2195,6 +2202,7 @@ function publicBayTerminalOutcome(value, allowedRepositories: ReadonlySet<string
21952202
if (!PUBLIC_BAY_OUTCOMES.has(outcome)) return null;
21962203
const explicitRepository = typeof source.repository === "string" ? source.repository.trim() : "";
21972204
const explicitNumber = source.item_number ?? source.number;
2205+
const journeyDuration = source.journey_duration_ms;
21982206
const itemKey = typeof source.item_key === "string" ? source.item_key.trim() : "";
21992207
const keyMatch = itemKey.match(/^([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)#(\d+)$/);
22002208
const repository = explicitRepository || keyMatch?.[1] || "";
@@ -2212,6 +2220,13 @@ function publicBayTerminalOutcome(value, allowedRepositories: ReadonlySet<string
22122220
) {
22132221
const canonicalRepository = repository.toLowerCase();
22142222
if (allowedRepositories.has(canonicalRepository)) {
2223+
if (
2224+
!Number.isSafeInteger(journeyDuration) ||
2225+
journeyDuration < 0 ||
2226+
journeyDuration > 24 * 60 * 60 * 1000
2227+
) {
2228+
return { outcome };
2229+
}
22152230
const action = publicBayActionFromWorker(
22162231
{ ...source, status: "completed", run_url: source.run_url ?? source.job_url },
22172232
allowedRepositories,
@@ -2220,6 +2235,7 @@ function publicBayTerminalOutcome(value, allowedRepositories: ReadonlySet<string
22202235
repository: canonicalRepository,
22212236
item_number: itemNumber,
22222237
outcome,
2238+
journey_duration_ms: journeyDuration,
22232239
...(action ? { action } : {}),
22242240
};
22252241
}
@@ -5208,7 +5224,7 @@ async function exactReviewBayLifecycleMetricsSnapshot(env) {
52085224
if (!response.ok) return null;
52095225
const source = objectValue(body.bay_lifecycle_metrics);
52105226
const collection = objectValue(source.collection);
5211-
if (source.version !== 1 || collection.state !== "complete") return null;
5227+
if (source.version !== 2 || collection.state !== "complete") return null;
52125228
const coverage = objectValue(source.coverage);
52135229
const timings = objectValue(source.timings);
52145230
const terminal = objectValue(source.terminal);
@@ -5218,15 +5234,12 @@ async function exactReviewBayLifecycleMetricsSnapshot(env) {
52185234
const windowMinutes = publicQueueCount(timings.window_minutes, 24 * 60);
52195235
const sampleLimit = publicQueueCount(timings.sample_limit, 100_000);
52205236
const overall = objectValue(timings.overall);
5237+
const history = bayLifecycleTimingHistory(timings.history);
52215238
const samples = publicQueueCount(overall.samples, 100_000);
52225239
const average =
5223-
overall.average_ms === null
5224-
? null
5225-
: publicQueueCount(overall.average_ms, 31 * 24 * 60 * 60 * 1000);
5240+
overall.average_ms === null ? null : publicQueueCount(overall.average_ms, 24 * 60 * 60 * 1000);
52265241
const median =
5227-
overall.median_ms === null
5228-
? null
5229-
: publicQueueCount(overall.median_ms, 31 * 24 * 60 * 60 * 1000);
5242+
overall.median_ms === null ? null : publicQueueCount(overall.median_ms, 24 * 60 * 60 * 1000);
52305243
const invalidTimingAggregate =
52315244
samples === null ||
52325245
(samples === 0 ? average !== null || median !== null : average === null || median === null);
@@ -5240,10 +5253,11 @@ async function exactReviewBayLifecycleMetricsSnapshot(env) {
52405253
if (
52415254
!startedAt ||
52425255
typeof timingComplete !== "boolean" ||
5243-
sampleKind !== "completed_exact_review_lifecycles" ||
5256+
sampleKind !== "completed_final_review_journeys" ||
52445257
windowMinutes !== 60 ||
52455258
sampleLimit === null ||
52465259
invalidTimingAggregate ||
5260+
!history ||
52475261
terminalCount === null ||
52485262
tideThreshold !== 20 ||
52495263
tideGeneration === null ||
@@ -5261,11 +5275,13 @@ async function exactReviewBayLifecycleMetricsSnapshot(env) {
52615275
timings: {
52625276
window_minutes: windowMinutes,
52635277
// `sample_kind` is a v1 public enum. Preserve its established spelling for
5264-
// strict clients, and expose the durable replacement provenance additively.
5278+
// strict clients, and expose end-to-end final-review provenance additively.
52655279
sample_kind: "completed_review_journeys",
52665280
source: "durable_exact_review_lifecycles",
5281+
completion_source: "verified_final_review_receipts",
52675282
sample_limit: sampleLimit,
52685283
overall: { average_ms: average, median_ms: median, samples },
5284+
history,
52695285
},
52705286
terminal_count: terminalCount,
52715287
tide_threshold: tideThreshold,
@@ -5284,18 +5300,55 @@ function bayLifecycleTerminalRows(value) {
52845300
const itemKey = String(row.item_key || "").toLowerCase();
52855301
const outcome = String(row.outcome || "");
52865302
const completedAt = publicQueueTimestamp(row.completed_at);
5303+
const journeyDuration = publicQueueCount(row.journey_duration_ms, 24 * 60 * 60 * 1000);
52875304
if (
52885305
!/^[a-z0-9_.-]+\/[a-z0-9_.-]+#[1-9]\d*$/.test(itemKey) ||
52895306
!["success", "failure", "cancelled"].includes(outcome) ||
5290-
!completedAt
5307+
!completedAt ||
5308+
journeyDuration === null
52915309
) {
52925310
return null;
52935311
}
5294-
rows.push({ item_key: itemKey, outcome, completed_at: completedAt });
5312+
rows.push({
5313+
item_key: itemKey,
5314+
outcome,
5315+
completed_at: completedAt,
5316+
journey_duration_ms: journeyDuration,
5317+
});
52955318
}
52965319
return rows;
52975320
}
52985321

5322+
function bayLifecycleTimingHistory(value) {
5323+
const source = objectValue(value);
5324+
const points = source && Array.isArray(source.points) ? source.points : null;
5325+
if (!source || source.bucket_minutes !== 5 || !points || points.length > 12) return null;
5326+
const result = [];
5327+
let previousEndedAt = 0;
5328+
for (const point of points) {
5329+
const row = objectValue(point);
5330+
const endedAt = row && publicQueueTimestamp(row.ended_at);
5331+
const average = row && publicQueueCount(row.average_ms, 24 * 60 * 60 * 1000);
5332+
const median = row && publicQueueCount(row.median_ms, 24 * 60 * 60 * 1000);
5333+
const samples = row && publicQueueCount(row.samples, 100_000);
5334+
const endedAtMs = endedAt ? Date.parse(endedAt) : Number.NaN;
5335+
if (
5336+
!endedAt ||
5337+
!Number.isFinite(endedAtMs) ||
5338+
endedAtMs <= previousEndedAt ||
5339+
average === null ||
5340+
median === null ||
5341+
samples === null ||
5342+
samples < 1
5343+
) {
5344+
return null;
5345+
}
5346+
previousEndedAt = endedAtMs;
5347+
result.push({ ended_at: endedAt, average_ms: average, median_ms: median, samples });
5348+
}
5349+
return { bucket_minutes: 5, points: result };
5350+
}
5351+
52995352
async function authenticatedExactReviewEnqueue(request, env) {
53005353
const secret = stringEnv(env.CLAWSWEEPER_WEBHOOK_SECRET);
53015354
if (!secret) return json({ error: "webhook_not_configured" }, 503);
@@ -6149,6 +6202,7 @@ async function statusSnapshot(env) {
61496202
if (
61506203
cachedTimings.sample_kind === "completed_review_journeys" &&
61516204
cachedTimings.source === "durable_exact_review_lifecycles" &&
6205+
cachedTimings.completion_source === "verified_final_review_receipts" &&
61526206
scopedCached === cached
61536207
) {
61546208
return cachedProjection;
@@ -6375,8 +6429,10 @@ async function statusSnapshot(env) {
63756429
window_minutes: BAY_TIMING_WINDOW_MS / 60_000,
63766430
sample_kind: "completed_review_journeys",
63776431
source: "durable_exact_review_lifecycles",
6432+
completion_source: "verified_final_review_receipts",
63786433
sample_limit: 0,
63796434
overall: { average_ms: null, median_ms: null, samples: 0 },
6435+
history: { bucket_minutes: 5, points: [] },
63806436
},
63816437
};
63826438
const { recent_attempts: _recentAttempts, ...publicWorkerHealth } = workerHealth;

0 commit comments

Comments
 (0)