Skip to content

Commit c272176

Browse files
committed
fix(bay): correlate direct re-review completions
1 parent 9fb6d6b commit c272176

7 files changed

Lines changed: 349 additions & 10 deletions

‎dashboard/exact-review-command-intake.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { exactReviewBaseDecisionFrom } from "./exact-review-decision.ts";
88
const COMMAND_INTAKE_TABLE = "exact_review_command_intakes";
99
const COMMAND_WATERMARK_TABLE = "exact_review_command_watermarks";
1010
const COMMAND_RECEIPT_TABLE = "exact_review_command_receipts";
11+
const COMMAND_BAY_JOURNEY_TABLE = "exact_review_command_bay_journeys";
1112
const ITEM_REVISION_TABLE = "exact_review_item_revisions";
1213
const COMMAND_INTAKE_LIMIT = 16;
1314
const COMMAND_RETRY_BASE_MS = 15_000;
@@ -36,6 +37,7 @@ export type ExactReviewCommandAdmission =
3637
accepted: true;
3738
deduped: boolean;
3839
commandVersionId: string;
40+
bayJourneyDeliveryId?: string;
3941
}
4042
| {
4143
accepted: false;
@@ -85,6 +87,12 @@ export class ExactReviewCommandIntakeStore {
8587
`CREATE INDEX IF NOT EXISTS exact_review_command_receipts_terminal_observed
8688
ON ${COMMAND_RECEIPT_TABLE} (outcome, observed_at, command_version_id)`,
8789
);
90+
this.storage.sql.exec(
91+
`CREATE TABLE IF NOT EXISTS ${COMMAND_BAY_JOURNEY_TABLE} (
92+
command_version_id TEXT PRIMARY KEY,
93+
bay_journey_delivery_id TEXT NOT NULL
94+
) STRICT`,
95+
);
8896
this.storage.sql.exec(
8997
`CREATE TABLE IF NOT EXISTS ${ITEM_REVISION_TABLE} (
9098
item_key TEXT PRIMARY KEY,
@@ -101,7 +109,11 @@ export class ExactReviewCommandIntakeStore {
101109
return this.storage.transactionSync(() => {
102110
const receipt = firstRow(
103111
this.storage.sql.exec(
104-
`SELECT outcome, detail FROM ${COMMAND_RECEIPT_TABLE} WHERE command_version_id = ?`,
112+
`SELECT receipt.outcome, receipt.detail, journey.bay_journey_delivery_id
113+
FROM ${COMMAND_RECEIPT_TABLE} receipt
114+
LEFT JOIN ${COMMAND_BAY_JOURNEY_TABLE} journey
115+
ON journey.command_version_id = receipt.command_version_id
116+
WHERE receipt.command_version_id = ?`,
105117
intake.commandVersionId,
106118
),
107119
);
@@ -118,6 +130,9 @@ export class ExactReviewCommandIntakeStore {
118130
accepted: true,
119131
deduped: true,
120132
commandVersionId: intake.commandVersionId,
133+
...(typeof receipt.bay_journey_delivery_id === "string"
134+
? { bayJourneyDeliveryId: receipt.bay_journey_delivery_id }
135+
: {}),
121136
};
122137
}
123138

@@ -196,6 +211,15 @@ export class ExactReviewCommandIntakeStore {
196211
sourceCommentKey,
197212
now,
198213
);
214+
if (intake.decision.bayJourneyDeliveryId) {
215+
this.storage.sql.exec(
216+
`INSERT INTO ${COMMAND_BAY_JOURNEY_TABLE}
217+
(command_version_id, bay_journey_delivery_id)
218+
VALUES (?, ?)`,
219+
intake.commandVersionId,
220+
intake.decision.bayJourneyDeliveryId,
221+
);
222+
}
199223
this.storage.sql.exec(
200224
`INSERT INTO ${COMMAND_INTAKE_TABLE}
201225
(command_version_id, source_comment_key, source_updated_at, record_json, next_attempt_at)
@@ -206,7 +230,14 @@ export class ExactReviewCommandIntakeStore {
206230
JSON.stringify(record),
207231
now,
208232
);
209-
return { accepted: true, deduped: false, commandVersionId: intake.commandVersionId };
233+
return {
234+
accepted: true,
235+
deduped: false,
236+
commandVersionId: intake.commandVersionId,
237+
...(intake.decision.bayJourneyDeliveryId
238+
? { bayJourneyDeliveryId: intake.decision.bayJourneyDeliveryId }
239+
: {}),
240+
};
210241
});
211242
}
212243

@@ -395,6 +426,10 @@ export class ExactReviewCommandIntakeStore {
395426
WHERE outcome != 'pending' AND observed_at <= ?`,
396427
now - EXACT_REVIEW_COMMAND_RECEIPT_RETENTION_MS,
397428
);
429+
this.storage.sql.exec(
430+
`DELETE FROM ${COMMAND_BAY_JOURNEY_TABLE}
431+
WHERE command_version_id NOT IN (SELECT command_version_id FROM ${COMMAND_RECEIPT_TABLE})`,
432+
);
398433
}
399434

400435
allocateItemRevision(

‎dashboard/exact-review-decision.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type ExactReviewBaseDecision = {
4141
sourceAuthoritySeq?: number;
4242
sourceUpdatedAt?: string;
4343
sourceDeliveryId?: string;
44+
bayJourneyDeliveryId?: string;
4445
codexTimeoutMs?: number;
4546
mediaProofTimeoutMs?: number;
4647
commandStatusMarker?: string;
@@ -325,6 +326,8 @@ export function exactReviewBaseDecisionFrom(value: unknown): ExactReviewBaseDeci
325326
: undefined;
326327
const hasSourceDeliveryId = Object.hasOwn(decision, "sourceDeliveryId");
327328
const sourceDeliveryId = hasSourceDeliveryId ? decision.sourceDeliveryId : undefined;
329+
const hasBayJourneyDeliveryId = Object.hasOwn(decision, "bayJourneyDeliveryId");
330+
const bayJourneyDeliveryId = hasBayJourneyDeliveryId ? decision.bayJourneyDeliveryId : undefined;
328331
const hasCommandStatusMarker = Object.hasOwn(decision, "commandStatusMarker");
329332
const commandStatusMarker = hasCommandStatusMarker ? decision.commandStatusMarker : undefined;
330333
const hasStatusCommentId = Object.hasOwn(decision, "statusCommentId");
@@ -372,6 +375,13 @@ export function exactReviewBaseDecisionFrom(value: unknown): ExactReviewBaseDeci
372375
) {
373376
return null;
374377
}
378+
if (
379+
hasBayJourneyDeliveryId &&
380+
(typeof bayJourneyDeliveryId !== "string" ||
381+
!/^[A-Za-z0-9_.:-]{1,200}$/.test(bayJourneyDeliveryId))
382+
) {
383+
return null;
384+
}
375385
if (
376386
hasCommandStatusMarker &&
377387
(typeof commandStatusMarker !== "string" ||
@@ -438,6 +448,7 @@ export function exactReviewBaseDecisionFrom(value: unknown): ExactReviewBaseDeci
438448
...(sourceAuthoritySeq === undefined ? {} : { sourceAuthoritySeq }),
439449
...(sourceUpdatedAt === undefined ? {} : { sourceUpdatedAt }),
440450
...(typeof sourceDeliveryId === "string" ? { sourceDeliveryId } : {}),
451+
...(typeof bayJourneyDeliveryId === "string" ? { bayJourneyDeliveryId } : {}),
441452
...(Number.isFinite(Number(decision.codexTimeoutMs))
442453
? { codexTimeoutMs: Number(decision.codexTimeoutMs) }
443454
: {}),

‎dashboard/exact-review-lifecycle.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export type ExactReviewLifecycleProjection = {
173173
admission: {
174174
deliveryId: string;
175175
sourceDeliveryId?: string;
176+
bayJourneyDeliveryId?: string;
176177
sourceAction: string;
177178
commandOriginated: boolean;
178179
statusMarker: string | null;
@@ -231,6 +232,7 @@ type ProjectionIdentity = {
231232
type LifecycleAdmissionInput = ProjectionIdentity & {
232233
deliveryId: string;
233234
sourceDeliveryId?: string;
235+
bayJourneyDeliveryId?: string;
234236
sourceAction: string;
235237
commandOriginated: boolean;
236238
statusMarker: string | null;
@@ -283,6 +285,12 @@ export class ExactReviewLifecycleProjectionStore {
283285
if (input.sourceDeliveryId !== undefined && !validText(input.sourceDeliveryId, 1, 200)) {
284286
throw new Error("invalid lifecycle source delivery identity");
285287
}
288+
if (
289+
input.bayJourneyDeliveryId !== undefined &&
290+
!validText(input.bayJourneyDeliveryId, 1, 200)
291+
) {
292+
throw new Error("invalid lifecycle Bay journey delivery identity");
293+
}
286294
if (input.statusMarker !== null && !validText(input.statusMarker, 1, 300)) {
287295
throw new Error("invalid lifecycle status marker");
288296
}
@@ -297,6 +305,7 @@ export class ExactReviewLifecycleProjectionStore {
297305
if (
298306
admission.deliveryId !== input.deliveryId ||
299307
admission.sourceDeliveryId !== input.sourceDeliveryId ||
308+
admission.bayJourneyDeliveryId !== input.bayJourneyDeliveryId ||
300309
admission.sourceAction !== input.sourceAction ||
301310
admission.commandOriginated !== input.commandOriginated ||
302311
admission.statusMarker !== input.statusMarker ||
@@ -314,6 +323,7 @@ export class ExactReviewLifecycleProjectionStore {
314323
admission: {
315324
deliveryId: input.deliveryId,
316325
...(input.sourceDeliveryId ? { sourceDeliveryId: input.sourceDeliveryId } : {}),
326+
...(input.bayJourneyDeliveryId ? { bayJourneyDeliveryId: input.bayJourneyDeliveryId } : {}),
317327
sourceAction: input.sourceAction,
318328
commandOriginated: input.commandOriginated,
319329
statusMarker: input.statusMarker,
@@ -1557,6 +1567,8 @@ function validDurableLifecycleBayProjection(value: ExactReviewLifecycleProjectio
15571567
!validText(value.admission.deliveryId, 1, 300) ||
15581568
(value.admission.sourceDeliveryId !== undefined &&
15591569
!validText(value.admission.sourceDeliveryId, 1, 200)) ||
1570+
(value.admission.bayJourneyDeliveryId !== undefined &&
1571+
!validText(value.admission.bayJourneyDeliveryId, 1, 200)) ||
15601572
!validText(value.admission.sourceAction, 1, 200) ||
15611573
(value.admission.statusMarker !== null && !validText(value.admission.statusMarker, 1, 300)) ||
15621574
(value.admission.statusCommentId !== null &&

‎dashboard/exact-review-queue.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ export class ExactReviewQueue {
840840
accepted: admitted.accepted,
841841
...(admitted.accepted ? { deduped: admitted.deduped } : { reason: admitted.reason }),
842842
command_version_id: admitted.commandVersionId,
843+
...(admitted.accepted && admitted.bayJourneyDeliveryId
844+
? { bay_journey_delivery_id: admitted.bayJourneyDeliveryId }
845+
: {}),
843846
},
844847
202,
845848
);
@@ -6749,6 +6752,9 @@ export class ExactReviewQueue {
67496752
...(commandDecision.sourceDeliveryId
67506753
? { sourceDeliveryId: commandDecision.sourceDeliveryId }
67516754
: {}),
6755+
...(commandDecision.bayJourneyDeliveryId
6756+
? { bayJourneyDeliveryId: commandDecision.bayJourneyDeliveryId }
6757+
: {}),
67526758
observedAt: now,
67536759
});
67546760
}
@@ -7653,6 +7659,10 @@ export class ExactReviewQueue {
76537659
result.projection?.admission.sourceDeliveryId
76547660
? { source_delivery_id: result.projection.admission.sourceDeliveryId }
76557661
: {}),
7662+
...((fenceKey !== undefined || includeDeliveryIdentity === true) &&
7663+
result.projection?.admission.bayJourneyDeliveryId
7664+
? { bay_journey_delivery_id: result.projection.admission.bayJourneyDeliveryId }
7665+
: {}),
76567666
});
76577667
} catch (error) {
76587668
console.warn(`lifecycle acknowledgement receipt rejected: ${sanitizedServerError(error)}`);

‎dashboard/worker.ts‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,8 +1694,12 @@ async function githubWebhook(request, env, ctx) {
16941694
ctx,
16951695
[],
16961696
[
1697-
acknowledgement.sourceDeliveryId
1698-
? { ...completion, source_delivery_id: acknowledgement.sourceDeliveryId }
1697+
acknowledgement.bayJourneyDeliveryId || acknowledgement.sourceDeliveryId
1698+
? {
1699+
...completion,
1700+
source_delivery_id:
1701+
acknowledgement.bayJourneyDeliveryId ?? acknowledgement.sourceDeliveryId,
1702+
}
16991703
: completion,
17001704
],
17011705
);
@@ -1820,7 +1824,6 @@ async function githubWebhook(request, env, ctx) {
18201824
payload,
18211825
deliveryId: deliveryHeaders.deliveryId,
18221826
});
1823-
if (trigger) await recordBayJourneyTelemetry(env, ctx, [trigger], []);
18241827

18251828
const commentDecision = decision;
18261829
if (
@@ -1839,6 +1842,7 @@ async function githubWebhook(request, env, ctx) {
18391842
sourceCommentUpdatedAt: commentDecision.commentUpdatedAt,
18401843
commandBodyDigest: await sha256Text(commentDecision.commentBody),
18411844
commandOrigin: "hosted_webhook",
1845+
...(trigger?.source_delivery_id ? { bayJourneyDeliveryId: trigger.source_delivery_id } : {}),
18421846
additionalPrompt: directReReviewAdditionalPrompt({
18431847
body: commentDecision.commentBody,
18441848
maintainerAuthorized: commentDecision.maintainerAuthorized,
@@ -1848,15 +1852,33 @@ async function githubWebhook(request, env, ctx) {
18481852
});
18491853
const queue = exactReviewQueueStub(env);
18501854
if (!queue) return json({ error: "exact_review_queue_not_configured" }, 503);
1851-
return queue.fetch(
1855+
const intakeResponse = await queue.fetch(
18521856
new Request("https://clawsweeper-exact-review-queue/command-intake", {
18531857
method: "POST",
18541858
headers: { "content-type": "application/json" },
18551859
body: JSON.stringify(intake),
18561860
}),
18571861
);
1862+
const intakeResult = objectValue(
1863+
await intakeResponse
1864+
.clone()
1865+
.json()
1866+
.catch(() => null),
1867+
);
1868+
const bayJourneyDeliveryId = nullableString(intakeResult.bay_journey_delivery_id);
1869+
if (trigger && intakeResult.accepted === true && bayJourneyDeliveryId) {
1870+
await recordBayJourneyTelemetry(
1871+
env,
1872+
ctx,
1873+
[{ ...trigger, source_delivery_id: bayJourneyDeliveryId }],
1874+
[],
1875+
);
1876+
}
1877+
return intakeResponse;
18581878
}
18591879

1880+
if (trigger) await recordBayJourneyTelemetry(env, ctx, [trigger], []);
1881+
18601882
const credentials = githubAppCredentials(env);
18611883
if (!credentials) return json({ error: "github_app_not_configured" }, 503);
18621884
const appJwt = await signGithubAppJwt(credentials.issuer, credentials.privateKey);
@@ -2663,7 +2685,7 @@ function exactReviewQueueStub(env): DurableObjectStub | null {
26632685

26642686
async function recordLifecycleCommandAcknowledgement(env, completion) {
26652687
const queue = exactReviewQueueStub(env);
2666-
if (!queue) return { accepted: true, sourceDeliveryId: null };
2688+
if (!queue) return { accepted: true, sourceDeliveryId: null, bayJourneyDeliveryId: null };
26672689
const observedAt = Date.parse(String(completion.completed_at || ""));
26682690
const response = await queue.fetch(
26692691
new Request("https://clawsweeper-exact-review-queue/lifecycle/command-ack/observed", {
@@ -2685,6 +2707,7 @@ async function recordLifecycleCommandAcknowledgement(env, completion) {
26852707
return {
26862708
accepted: result.accepted !== false,
26872709
sourceDeliveryId: nullableString(result.source_delivery_id),
2710+
bayJourneyDeliveryId: nullableString(result.bay_journey_delivery_id),
26882711
};
26892712
}
26902713

@@ -3065,8 +3088,13 @@ async function authenticatedLifecycleCommandAcknowledgement(request, env, ctx) {
30653088
repository: target[1],
30663089
number: Number(target[2]),
30673090
source_comment_id: Number(receipt.command_comment_id),
3068-
...(nullableString(result.source_delivery_id)
3069-
? { source_delivery_id: nullableString(result.source_delivery_id) }
3091+
...(nullableString(result.bay_journey_delivery_id) ||
3092+
nullableString(result.source_delivery_id)
3093+
? {
3094+
source_delivery_id:
3095+
nullableString(result.bay_journey_delivery_id) ??
3096+
nullableString(result.source_delivery_id),
3097+
}
30703098
: {}),
30713099
completed_at: completedAt,
30723100
completion_kind: "final_command_status",

‎src/repair/direct-re-review-admission.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type DirectReReviewDecision = {
99
sourceAction: "re_review";
1010
supersedesInProgress: false;
1111
sourceDeliveryId: string;
12+
bayJourneyDeliveryId?: string;
1213
sourceCommentId: number;
1314
sourceCommentUpdatedAt: string;
1415
commandBodyDigest: string;
@@ -68,6 +69,7 @@ export function directReReviewIntake(options: {
6869
additionalPrompt: string;
6970
statusCommentId?: number;
7071
candidateHeadSha?: string;
72+
bayJourneyDeliveryId?: string;
7173
}): DirectReReviewIntake {
7274
const commandVersionId = reReviewCommandVersionIdentity({
7375
commentId: options.sourceCommentId,
@@ -86,6 +88,7 @@ export function directReReviewIntake(options: {
8688
sourceAction: "re_review",
8789
supersedesInProgress: false,
8890
sourceDeliveryId: commandVersionId,
91+
...(options.bayJourneyDeliveryId ? { bayJourneyDeliveryId: options.bayJourneyDeliveryId } : {}),
8992
sourceCommentId: options.sourceCommentId,
9093
sourceCommentUpdatedAt: options.sourceCommentUpdatedAt,
9194
commandBodyDigest: options.commandBodyDigest,
@@ -129,6 +132,9 @@ export function validateDirectReReviewIntake(value: unknown): DirectReReviewInta
129132
bodySha256: String(intake.commandBodyDigest),
130133
}) ||
131134
decision.sourceDeliveryId !== intake.commandVersionId ||
135+
(decision.bayJourneyDeliveryId !== undefined &&
136+
(typeof decision.bayJourneyDeliveryId !== "string" ||
137+
!/^[A-Za-z0-9_.:-]{1,200}$/.test(decision.bayJourneyDeliveryId))) ||
132138
decision.sourceCommentId !== intake.sourceCommentId ||
133139
decision.sourceCommentUpdatedAt !== intake.sourceCommentUpdatedAt ||
134140
decision.commandBodyDigest !== intake.commandBodyDigest ||

0 commit comments

Comments
 (0)