|
7 | 7 | ExactReviewQueue, |
8 | 8 | mergeBayJourneyState, |
9 | 9 | ExactReviewPublicationBatchStore, |
| 10 | + ExactReviewDirectPublicationStore, |
| 11 | + validateDirectPublicationPlan, |
10 | 12 | commandAcknowledgementState, |
11 | 13 | ExactReviewLifecycleProjectionStore, |
12 | 14 | lifecycleState, |
@@ -969,6 +971,149 @@ test("direct lifecycle requeue becomes a fresh fenced source-drift revision", as |
969 | 971 | ); |
970 | 972 | }); |
971 | 973 |
|
| 974 | +for (const scenario of [ |
| 975 | + { receipt: "accepted", lifecycle: "requeue", requeued: true }, |
| 976 | + { receipt: "deduped", lifecycle: "requeue", requeued: true }, |
| 977 | + { receipt: "superseded", lifecycle: "requeue", requeued: false }, |
| 978 | + { receipt: "accepted", lifecycle: undefined, requeued: false }, |
| 979 | +] as const) { |
| 980 | + test(`lost completion reconciles ${scenario.receipt} receipt with ${scenario.lifecycle ?? "no"} plan`, async () => { |
| 981 | + const storage = new MemoryDurableStorage(); |
| 982 | + const leased = leasedExactReviewQueueItem(705, "7050"); |
| 983 | + leased.revision = 4; |
| 984 | + leased.leaseRevision = 4; |
| 985 | + leased.claimGeneration = 2; |
| 986 | + await storage.put("exact-review-queue", { deliveries: {}, items: { [leased.key]: leased } }); |
| 987 | + const queue = new ExactReviewQueue({ storage }, {}); |
| 988 | + const env = { |
| 989 | + CLAWSWEEPER_WEBHOOK_SECRET: "test-secret", |
| 990 | + EXACT_REVIEW_QUEUE: new MemoryDurableNamespace(queue), |
| 991 | + }; |
| 992 | + const post = (route: string, value: unknown) => { |
| 993 | + const body = JSON.stringify(value); |
| 994 | + return worker.fetch( |
| 995 | + new Request(`https://clawsweeper.openclaw.ai/internal/exact-review/${route}`, { |
| 996 | + method: "POST", |
| 997 | + headers: { |
| 998 | + "x-clawsweeper-exact-review-signature": `sha256=${createHmac("sha256", "test-secret").update(body).digest("hex")}`, |
| 999 | + }, |
| 1000 | + body, |
| 1001 | + }), |
| 1002 | + env, |
| 1003 | + ); |
| 1004 | + }; |
| 1005 | + const publication = { |
| 1006 | + canonicalTargetKey: leased.key, |
| 1007 | + fenceKey: leased.key, |
| 1008 | + revision: 4, |
| 1009 | + sourceSha: "e".repeat(40), |
| 1010 | + identity: { |
| 1011 | + canonicalTargetKey: leased.key, |
| 1012 | + fenceKey: leased.key, |
| 1013 | + revision: 4, |
| 1014 | + claimGeneration: 2, |
| 1015 | + }, |
| 1016 | + operations: [ |
| 1017 | + { |
| 1018 | + path: "records/openclaw-openclaw/items/705.md", |
| 1019 | + deleted: false, |
| 1020 | + mode: "100644", |
| 1021 | + bytes: 1, |
| 1022 | + contentBase64: "eA==", |
| 1023 | + }, |
| 1024 | + ], |
| 1025 | + totalBytes: 1, |
| 1026 | + ...(scenario.lifecycle ? { lifecycle: { kind: scenario.lifecycle } } : {}), |
| 1027 | + }; |
| 1028 | + if (scenario.receipt === "superseded") { |
| 1029 | + const newerPublisher = new ExactReviewDirectPublicationStore(storage); |
| 1030 | + newerPublisher.ensureSchemaSync(); |
| 1031 | + const newer = await validateDirectPublicationPlan({ |
| 1032 | + ...publication, |
| 1033 | + revision: 5, |
| 1034 | + identity: { ...publication.identity, revision: 5 }, |
| 1035 | + lifecycle: { kind: "policy_noop" }, |
| 1036 | + }); |
| 1037 | + assert.equal(newerPublisher.accept(newer, Date.now()).outcome, "accepted"); |
| 1038 | + } |
| 1039 | + if (scenario.receipt === "deduped") { |
| 1040 | + const first = await post("publication-results", publication); |
| 1041 | + assert.equal(first.status, 202); |
| 1042 | + assert.equal((await first.json()).accepted, true); |
| 1043 | + } |
| 1044 | + const receipt = await post("publication-results", publication); |
| 1045 | + assert.equal(receipt.status, 202); |
| 1046 | + assert.equal((await receipt.json())[scenario.receipt], true); |
| 1047 | + const readState = async () => |
| 1048 | + (await storage.get("exact-review-queue")) as { |
| 1049 | + items: Record< |
| 1050 | + string, |
| 1051 | + { |
| 1052 | + state: string; |
| 1053 | + revision: number; |
| 1054 | + leaseId?: string; |
| 1055 | + admissionDeliveryId?: string; |
| 1056 | + decision: { |
| 1057 | + sourceAction: string; |
| 1058 | + publication?: { |
| 1059 | + directLifecycle?: { plan: { kind: string }; receiptOutcome: string }; |
| 1060 | + }; |
| 1061 | + }; |
| 1062 | + } |
| 1063 | + >; |
| 1064 | + }; |
| 1065 | + if (scenario.lifecycle) { |
| 1066 | + assert.deepEqual( |
| 1067 | + (await readState()).items[leased.key]?.decision.publication?.directLifecycle, |
| 1068 | + { |
| 1069 | + plan: { kind: scenario.lifecycle }, |
| 1070 | + receiptOutcome: scenario.receipt === "deduped" ? "accepted" : scenario.receipt, |
| 1071 | + }, |
| 1072 | + ); |
| 1073 | + } |
| 1074 | + const terminalRun = { |
| 1075 | + terminal_runs: [ |
| 1076 | + { |
| 1077 | + run_id: leased.claimedRunId, |
| 1078 | + run_attempt: leased.claimedRunAttempt, |
| 1079 | + claimed_run_attempt: leased.claimedRunAttempt, |
| 1080 | + claim_generation: 2, |
| 1081 | + outcome: "success", |
| 1082 | + }, |
| 1083 | + ], |
| 1084 | + }; |
| 1085 | + const reconciled = await post("reconcile", terminalRun); |
| 1086 | + assert.equal(reconciled.status, 200); |
| 1087 | + assert.deepEqual(await reconciled.json(), { |
| 1088 | + ok: true, |
| 1089 | + reconciled: 1, |
| 1090 | + requeued: Number(scenario.requeued), |
| 1091 | + completed: Number(!scenario.requeued), |
| 1092 | + }); |
| 1093 | + const item = (await readState()).items[leased.key]; |
| 1094 | + if (scenario.requeued) { |
| 1095 | + assert.equal(item?.state, "pending"); |
| 1096 | + assert.equal(item?.revision, 5); |
| 1097 | + assert.equal(item?.leaseId, undefined); |
| 1098 | + assert.equal(item?.decision.sourceAction, "source_drift_requeue"); |
| 1099 | + assert.equal(item?.decision.publication, undefined); |
| 1100 | + assert.equal(item?.admissionDeliveryId, `direct-lifecycle-requeue:${leased.key}:4`); |
| 1101 | + } else { |
| 1102 | + assert.equal(item, undefined); |
| 1103 | + } |
| 1104 | + if (scenario.lifecycle) { |
| 1105 | + assert.equal( |
| 1106 | + new ExactReviewLifecycleProjectionStore(storage).read(leased.key, leased.key, 4) |
| 1107 | + ?.terminalDisposition?.kind, |
| 1108 | + scenario.requeued ? "requeue" : "superseded", |
| 1109 | + ); |
| 1110 | + } |
| 1111 | + const repeated = await post("reconcile", terminalRun); |
| 1112 | + assert.deepEqual(await repeated.json(), { ok: true, reconciled: 0, requeued: 0, completed: 0 }); |
| 1113 | + assert.deepEqual((await readState()).items[leased.key], item); |
| 1114 | + }); |
| 1115 | +} |
| 1116 | + |
972 | 1117 | test("direct lifecycle requeue preserves a newer command follow-up revision", async () => { |
973 | 1118 | const storage = new MemoryDurableStorage(); |
974 | 1119 | const leased = leasedExactReviewQueueItem(704, "7040"); |
|
0 commit comments