Skip to content

Commit a769f25

Browse files
steipetePeter Steinberger
andauthored
fix: finalize legacy command acknowledgements without webhook races (#1018)
* fix: finalize legacy command acknowledgements without webhook races * fix: recover fenced legacy command acknowledgements safely * fix: fence command receipts and deduplicate Bay completions * fix: fence terminal acknowledgements to original command delivery * fix: reuse fresh router comment snapshots after dispatch recovery --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent e55de43 commit a769f25

15 files changed

Lines changed: 2793 additions & 165 deletions

‎.github/workflows/repair-comment-router.yml‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ jobs:
222222
item_numbers="${{ github.event.client_payload.item_number || '' }}"
223223
comment_ids="${{ github.event.client_payload.comment_id || '' }}"
224224
status_comment_id="${{ github.event.client_payload.status_comment_id || '' }}"
225-
source_event="${{ github.event.client_payload.source_event || '' }}"
225+
source_delivery_id="${{ github.event.client_payload.source_delivery_id || '' }}"
226+
source_event="${{ github.event.client_payload.source_event || 'issue_comment' }}"
226227
source_action="${{ github.event.client_payload.source_action || '' }}"
227228
dispatch_actor="${{ github.actor }}"
228229
comment_event_auth="${{ github.event.client_payload.comment_event_auth || '' }}"
@@ -232,6 +233,7 @@ jobs:
232233
attempt_id="${{ github.event.client_payload.attempt_id || '' }}"
233234
else
234235
status_comment_id=""
236+
source_delivery_id=""
235237
source_event=""
236238
source_action=""
237239
dispatch_actor=""
@@ -271,6 +273,9 @@ jobs:
271273
if [ -n "$status_comment_id" ]; then
272274
args+=(--status-comment-id "$status_comment_id")
273275
fi
276+
if [ -n "$source_delivery_id" ]; then
277+
args+=(--source-delivery-id "$source_delivery_id")
278+
fi
274279
if [ -n "$source_event" ]; then
275280
args+=(--source-event "$source_event")
276281
fi
@@ -373,7 +378,8 @@ jobs:
373378
item_numbers="${{ github.event.client_payload.item_number || '' }}"
374379
comment_ids="${{ github.event.client_payload.comment_id || '' }}"
375380
status_comment_id="${{ github.event.client_payload.status_comment_id || '' }}"
376-
source_event="${{ github.event.client_payload.source_event || '' }}"
381+
source_delivery_id="${{ github.event.client_payload.source_delivery_id || '' }}"
382+
source_event="${{ github.event.client_payload.source_event || 'issue_comment' }}"
377383
source_action="${{ github.event.client_payload.source_action || '' }}"
378384
dispatch_actor="${{ github.actor }}"
379385
comment_event_auth="${{ github.event.client_payload.comment_event_auth || '' }}"
@@ -383,6 +389,7 @@ jobs:
383389
attempt_id="${{ github.event.client_payload.attempt_id || '' }}"
384390
else
385391
status_comment_id=""
392+
source_delivery_id=""
386393
source_event=""
387394
source_action=""
388395
dispatch_actor=""
@@ -422,6 +429,9 @@ jobs:
422429
if [ -n "$status_comment_id" ]; then
423430
args+=(--status-comment-id "$status_comment_id")
424431
fi
432+
if [ -n "$source_delivery_id" ]; then
433+
args+=(--source-delivery-id "$source_delivery_id")
434+
fi
425435
if [ -n "$source_event" ]; then
426436
args+=(--source-event "$source_event")
427437
fi

‎.github/workflows/sweep.yml‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ jobs:
335335
const queueClaim = payload.queue_claim && typeof payload.queue_claim === "object"
336336
? payload.queue_claim
337337
: {};
338+
const reviewOptions = payload.review_options && typeof payload.review_options === "object"
339+
? payload.review_options
340+
: {};
338341
const dispatchKey = String(payload.dispatch_key || "").trim();
339342
const ingressFingerprint = String(payload.ingress_fingerprint || "").trim().toLowerCase();
340343
const ingress =
@@ -366,6 +369,9 @@ jobs:
366369
sourceEvent,
367370
sourceAction: payload.source_action || "legacy_dispatch",
368371
supersedesInProgress: payload.supersedes_in_progress === true,
372+
...(typeof payload.source_delivery_id === "string" && payload.source_delivery_id
373+
? { sourceDeliveryId: payload.source_delivery_id }
374+
: {}),
369375
...(/^[0-9a-f]{40}$/.test(sourceHeadSha)
370376
? { sourceHeadSha }
371377
: {}),
@@ -379,15 +385,27 @@ jobs:
379385
...(sourceUpdatedAt && Number.isFinite(Date.parse(sourceUpdatedAt))
380386
? { sourceUpdatedAt }
381387
: {}),
382-
...(Number.isFinite(Number(queueClaim.codex_timeout_ms ?? payload.codex_timeout_ms))
383-
? { codexTimeoutMs: Number(queueClaim.codex_timeout_ms ?? payload.codex_timeout_ms) }
388+
...(Number.isFinite(
389+
Number(queueClaim.codex_timeout_ms ?? reviewOptions.codex_timeout_ms ?? payload.codex_timeout_ms),
390+
)
391+
? {
392+
codexTimeoutMs: Number(
393+
queueClaim.codex_timeout_ms ?? reviewOptions.codex_timeout_ms ?? payload.codex_timeout_ms,
394+
),
395+
}
384396
: {}),
385397
...(Number.isFinite(
386-
Number(queueClaim.media_proof_timeout_ms ?? payload.media_proof_timeout_ms),
398+
Number(
399+
queueClaim.media_proof_timeout_ms ??
400+
reviewOptions.media_proof_timeout_ms ??
401+
payload.media_proof_timeout_ms,
402+
),
387403
)
388404
? {
389405
mediaProofTimeoutMs: Number(
390-
queueClaim.media_proof_timeout_ms ?? payload.media_proof_timeout_ms,
406+
queueClaim.media_proof_timeout_ms ??
407+
reviewOptions.media_proof_timeout_ms ??
408+
payload.media_proof_timeout_ms,
391409
),
392410
}
393411
: {}),
@@ -3248,30 +3266,42 @@ jobs:
32483266
env:
32493267
TARGET_REPO: ${{ steps.finalization-context.outputs.target_repo }}
32503268
ITEM_NUMBER: ${{ steps.finalization-context.outputs.item_number }}
3269+
FENCE_KEY: ${{ steps.finalization-context.outputs.lifecycle_fence_key }}
3270+
REVISION: ${{ steps.finalization-context.outputs.lifecycle_revision }}
32513271
STATUS_MARKER: ${{ steps.finalization-context.outputs.status_marker }}
32523272
STATUS_COMMENT_ID: ${{ steps.finalization-context.outputs.status_comment_id }}
32533273
COMMAND_COMMENT_ID: ${{ steps.update-final-command-status.outputs.command_comment_id }}
32543274
COMPLETION_COMMENT_ID: ${{ steps.update-final-command-status.outputs.completion_comment_id }}
3275+
COMPLETION_COMPLETED_AT: ${{ steps.update-final-command-status.outputs.completion_completed_at }}
32553276
QUEUE_URL: ${{ vars.CLAWSWEEPER_EXACT_REVIEW_QUEUE_URL || 'https://clawsweeper.openclaw.ai' }}
32563277
CLAWSWEEPER_WEBHOOK_SECRET: ${{ secrets.CLAWSWEEPER_WEBHOOK_SECRET }}
32573278
run: |
32583279
set -euo pipefail
32593280
payload="$(node -e '
3281+
const fenceKey = process.env.FENCE_KEY || "";
3282+
const revision = Number(process.env.REVISION);
32603283
const statusMarker = process.env.STATUS_MARKER || null;
32613284
const statusCommentId = process.env.STATUS_COMMENT_ID === "" ? null : Number(process.env.STATUS_COMMENT_ID);
32623285
const commandCommentId = Number(process.env.COMMAND_COMMENT_ID);
32633286
const completionCommentId = Number(process.env.COMPLETION_COMMENT_ID);
3287+
const completedAt = process.env.COMPLETION_COMPLETED_AT || "";
32643288
if (
3289+
!fenceKey || !Number.isSafeInteger(revision) || revision < 1 ||
32653290
(!statusMarker && statusCommentId === null) ||
32663291
(statusCommentId !== null && (!Number.isInteger(statusCommentId) || statusCommentId < 1)) ||
32673292
!Number.isInteger(commandCommentId) || commandCommentId < 1 ||
3268-
!Number.isInteger(completionCommentId) || completionCommentId < 1
3293+
!Number.isInteger(completionCommentId) || completionCommentId < 1 ||
3294+
!completedAt || !Number.isFinite(Date.parse(completedAt))
32693295
) process.exit(1);
32703296
process.stdout.write(JSON.stringify({
32713297
canonical_target_key: `${process.env.TARGET_REPO}#${process.env.ITEM_NUMBER}`,
3298+
fence_key: fenceKey,
3299+
revision,
32723300
...(statusMarker ? { status_marker: statusMarker } : {}),
3301+
...(statusCommentId === null ? {} : { status_comment_id: statusCommentId }),
32733302
command_comment_id: commandCommentId,
32743303
completion_comment_id: completionCommentId,
3304+
completed_at: completedAt,
32753305
observed_at: Date.now(),
32763306
}));
32773307
')"

‎dashboard/exact-review-lifecycle.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export type ExactReviewLifecycleProjection = {
205205
revision: number;
206206
admission: {
207207
deliveryId: string;
208+
sourceDeliveryId?: string;
208209
sourceAction: string;
209210
commandOriginated: boolean;
210211
statusMarker: string | null;
@@ -297,6 +298,7 @@ export class ExactReviewLifecycleProjectionStore {
297298
recordAdmission(
298299
input: ProjectionIdentity & {
299300
deliveryId: string;
301+
sourceDeliveryId?: string;
300302
sourceAction: string;
301303
commandOriginated: boolean;
302304
statusMarker: string | null;
@@ -308,6 +310,9 @@ export class ExactReviewLifecycleProjectionStore {
308310
if (!validText(input.deliveryId, 1, 300) || !validText(input.sourceAction, 1, 200)) {
309311
throw new Error("invalid lifecycle admission fact");
310312
}
313+
if (input.sourceDeliveryId !== undefined && !validText(input.sourceDeliveryId, 1, 200)) {
314+
throw new Error("invalid lifecycle source delivery identity");
315+
}
311316
if (input.statusMarker !== null && !validText(input.statusMarker, 1, 300)) {
312317
throw new Error("invalid lifecycle status marker");
313318
}
@@ -322,6 +327,7 @@ export class ExactReviewLifecycleProjectionStore {
322327
const admission = existing.admission;
323328
if (
324329
admission.deliveryId !== input.deliveryId ||
330+
admission.sourceDeliveryId !== input.sourceDeliveryId ||
325331
admission.sourceAction !== input.sourceAction ||
326332
admission.commandOriginated !== input.commandOriginated ||
327333
admission.statusMarker !== input.statusMarker ||
@@ -338,6 +344,7 @@ export class ExactReviewLifecycleProjectionStore {
338344
revision: input.revision,
339345
admission: {
340346
deliveryId: input.deliveryId,
347+
...(input.sourceDeliveryId ? { sourceDeliveryId: input.sourceDeliveryId } : {}),
341348
sourceAction: input.sourceAction,
342349
commandOriginated: input.commandOriginated,
343350
statusMarker: input.statusMarker,
@@ -681,16 +688,24 @@ export class ExactReviewLifecycleProjectionStore {
681688

682689
observeCommandAcknowledgement(input: {
683690
canonicalTargetKey: string;
691+
fenceKey?: string;
692+
revision?: number;
684693
statusMarker: string | null;
685694
commandCommentId: number;
686695
completionCommentId: number;
696+
statusCommentId?: number;
697+
requireExactStatusComment?: boolean;
687698
observedAt: number;
688699
}) {
689700
if (
690701
!validCanonicalTargetKey(input.canonicalTargetKey) ||
702+
(input.fenceKey === undefined) !== (input.revision === undefined) ||
703+
(input.fenceKey !== undefined && !validFenceKey(input.fenceKey)) ||
704+
(input.revision !== undefined && !positiveInteger(input.revision)) ||
691705
(input.statusMarker !== null && !validText(input.statusMarker, 1, 300)) ||
692706
!positiveInteger(input.commandCommentId) ||
693-
!positiveInteger(input.completionCommentId)
707+
!positiveInteger(input.completionCommentId) ||
708+
(input.statusCommentId !== undefined && !positiveInteger(input.statusCommentId))
694709
) {
695710
throw new Error("invalid lifecycle acknowledgement receipt");
696711
}
@@ -702,14 +717,51 @@ export class ExactReviewLifecycleProjectionStore {
702717
input.canonicalTargetKey,
703718
),
704719
);
720+
const matchedProjections: ExactReviewLifecycleProjection[] = [];
705721
for (const row of rows) {
706722
const projection = projectionFromRow(String(row.projection_json || ""));
723+
if (
724+
input.fenceKey !== undefined &&
725+
(projection.fenceKey !== input.fenceKey || projection.revision !== input.revision)
726+
) {
727+
continue;
728+
}
707729
const attempted = projection.acknowledgement.attempts.some(
708730
(attempt) =>
709-
attempt.statusCommentId === input.completionCommentId ||
710-
(input.statusMarker !== null && attempt.statusMarker === input.statusMarker),
731+
(input.statusCommentId === undefined ||
732+
attempt.statusCommentId === input.statusCommentId) &&
733+
((attempt.statusCommentId === input.completionCommentId &&
734+
(attempt.statusMarker === input.statusMarker ||
735+
attempt.statusMarker === null ||
736+
(!input.requireExactStatusComment && input.statusMarker === null))) ||
737+
(input.requireExactStatusComment &&
738+
attempt.statusCommentId === null &&
739+
input.statusMarker !== null &&
740+
attempt.statusMarker === input.statusMarker) ||
741+
(!input.requireExactStatusComment &&
742+
input.statusMarker !== null &&
743+
attempt.statusMarker === input.statusMarker)),
711744
);
712-
if (!attempted || !projection.acknowledgement.required) continue;
745+
if (attempted && projection.acknowledgement.required) matchedProjections.push(projection);
746+
}
747+
const exactStatusCommentMatches =
748+
input.fenceKey === undefined && matchedProjections.length > 1
749+
? matchedProjections.filter((projection) =>
750+
projection.acknowledgement.attempts.some(
751+
(attempt) => attempt.statusCommentId === input.completionCommentId,
752+
),
753+
)
754+
: [];
755+
if (
756+
input.fenceKey === undefined &&
757+
matchedProjections.length > 1 &&
758+
exactStatusCommentMatches.length !== 1
759+
) {
760+
return { accepted: false, projection: null, state: null, acknowledgement: null };
761+
}
762+
for (const projection of exactStatusCommentMatches.length
763+
? exactStatusCommentMatches
764+
: matchedProjections) {
713765
const observed = {
714766
statusMarker: input.statusMarker,
715767
commandCommentId: input.commandCommentId,
@@ -1655,6 +1707,8 @@ function validDurableLifecycleBayProjection(value: ExactReviewLifecycleProjectio
16551707
!positiveInteger(value.revision) ||
16561708
!finiteTimestamp(value.updatedAt) ||
16571709
!validText(value.admission.deliveryId, 1, 300) ||
1710+
(value.admission.sourceDeliveryId !== undefined &&
1711+
!validText(value.admission.sourceDeliveryId, 1, 200)) ||
16581712
!validText(value.admission.sourceAction, 1, 200) ||
16591713
(value.admission.statusMarker !== null && !validText(value.admission.statusMarker, 1, 300)) ||
16601714
(value.admission.statusCommentId !== null &&

0 commit comments

Comments
 (0)