|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import { AgentClosedError } from "@intx/agent"; |
3 | 3 | import { |
| 4 | + createDeliveryGeneration, |
| 5 | + createSessionOperationQueue, |
4 | 6 | deliverAgentMessage, |
5 | 7 | deliveryResultNotice, |
| 8 | + enqueueCompactionContinuationHop, |
6 | 9 | runGenerationGuardedDeliver, |
| 10 | + settleCompactionContinuationHop, |
7 | 11 | } from "./delivery-queue.js"; |
| 12 | +import { startInterruptRebuild } from "./runner/exit.js"; |
8 | 13 |
|
9 | 14 | describe("deliverAgentMessage", () => { |
10 | 15 | test("reports session-unavailable without calling deliver when rebuild failed", async () => { |
@@ -125,6 +130,101 @@ describe("runGenerationGuardedDeliver", () => { |
125 | 130 | }); |
126 | 131 | }); |
127 | 132 |
|
| 133 | +describe("settleCompactionContinuationHop", () => { |
| 134 | + test("a superseded hop requeues instead of delivering to the outgoing agent", async () => { |
| 135 | + let generation = 1; |
| 136 | + const stillCurrent = () => generation === 1; |
| 137 | + let delivered = 0; |
| 138 | + let requeued = 0; |
| 139 | + generation = 2; |
| 140 | + const result = await settleCompactionContinuationHop({ |
| 141 | + stillCurrent, |
| 142 | + deliver: async () => { |
| 143 | + delivered += 1; |
| 144 | + return { status: "accepted" as const }; |
| 145 | + }, |
| 146 | + onSuperseded: () => { |
| 147 | + requeued += 1; |
| 148 | + }, |
| 149 | + }); |
| 150 | + expect(result).toEqual({ |
| 151 | + status: "not-delivered", |
| 152 | + reason: "superseded", |
| 153 | + detail: "session identity changed before delivery", |
| 154 | + }); |
| 155 | + expect(delivered).toBe(0); |
| 156 | + expect(requeued).toBe(1); |
| 157 | + }); |
| 158 | + |
| 159 | + test("a closed hop re-issues the continue once", async () => { |
| 160 | + let attempts = 0; |
| 161 | + const result = await settleCompactionContinuationHop({ |
| 162 | + stillCurrent: () => true, |
| 163 | + deliver: async () => { |
| 164 | + attempts += 1; |
| 165 | + if (attempts === 1) { |
| 166 | + return { |
| 167 | + status: "not-delivered" as const, |
| 168 | + reason: "agent-closed" as const, |
| 169 | + detail: "agent is closed", |
| 170 | + }; |
| 171 | + } |
| 172 | + return { status: "accepted" as const }; |
| 173 | + }, |
| 174 | + onSuperseded: () => { |
| 175 | + throw new Error("closed hop must not requeue as superseded"); |
| 176 | + }, |
| 177 | + }); |
| 178 | + expect(result).toEqual({ status: "accepted" }); |
| 179 | + expect(attempts).toBe(2); |
| 180 | + }); |
| 181 | + |
| 182 | + test("a current hop delivers once and does not re-issue", async () => { |
| 183 | + let runs = 0; |
| 184 | + const result = await settleCompactionContinuationHop({ |
| 185 | + stillCurrent: () => true, |
| 186 | + deliver: async () => { |
| 187 | + runs += 1; |
| 188 | + return { status: "accepted" as const }; |
| 189 | + }, |
| 190 | + onSuperseded: () => { |
| 191 | + throw new Error("current hop must not requeue"); |
| 192 | + }, |
| 193 | + }); |
| 194 | + expect(result).toEqual({ status: "accepted" }); |
| 195 | + expect(runs).toBe(1); |
| 196 | + }); |
| 197 | + |
| 198 | + test("continuation enqueued then interrupt rebuild queued lands on the replacement agent", async () => { |
| 199 | + const { enqueue, awaitTail } = createSessionOperationQueue(); |
| 200 | + const deliveryGeneration = createDeliveryGeneration(); |
| 201 | + let agent = "outgoing"; |
| 202 | + const deliveredTo: string[] = []; |
| 203 | + |
| 204 | + enqueueCompactionContinuationHop({ |
| 205 | + enqueue, |
| 206 | + captureGeneration: () => deliveryGeneration.capture(), |
| 207 | + deliver: async () => { |
| 208 | + deliveredTo.push(agent); |
| 209 | + return { status: "accepted" as const }; |
| 210 | + }, |
| 211 | + onResult: () => undefined, |
| 212 | + }); |
| 213 | + |
| 214 | + startInterruptRebuild({ |
| 215 | + deliveryGeneration, |
| 216 | + markSendAborted: () => undefined, |
| 217 | + enqueue, |
| 218 | + rebuild: async () => { |
| 219 | + agent = "replacement"; |
| 220 | + }, |
| 221 | + }); |
| 222 | + |
| 223 | + await awaitTail(); |
| 224 | + expect(deliveredTo).toEqual(["replacement"]); |
| 225 | + }); |
| 226 | +}); |
| 227 | + |
128 | 228 | describe("deliveryResultNotice", () => { |
129 | 229 | test("closed restored and deferred copy is actionable", () => { |
130 | 230 | const closed = { |
|
0 commit comments