|
8 | 8 | loadPendingSessionDeliveries, |
9 | 9 | markSessionDeliveryPlatformOutcomeUnknown, |
10 | 10 | recoverPendingSessionDeliveries, |
| 11 | + SessionDeliverySendUncertainError, |
11 | 12 | } from "./session-delivery-queue.js"; |
12 | 13 |
|
13 | 14 | describe("session-delivery queue recovery", () => { |
@@ -219,12 +220,14 @@ describe("session-delivery queue recovery", () => { |
219 | 220 | // Model the production delivery seam on a partial_failed send: some |
220 | 221 | // payloads already reached the platform (sentBeforeError), so |
221 | 222 | // deliverQueuedSessionDelivery persists the unknown_after_send marker and |
222 | | - // then throws. Recovery must refuse a blind replay rather than clearing the |
223 | | - // marker and re-running the turn / re-sending the already-sent reply. |
| 223 | + // throws a SessionDeliverySendUncertainError. Recovery must refuse a blind |
| 224 | + // replay rather than clearing the marker and re-sending the already-sent reply. |
224 | 225 | const deliver = vi.fn(async () => { |
225 | 226 | deliverCount += 1; |
226 | 227 | await markSessionDeliveryPlatformOutcomeUnknown(id, tempDir); |
227 | | - throw new Error("partial_failed: reply chunk sent before error"); |
| 228 | + throw new SessionDeliverySendUncertainError( |
| 229 | + new Error("partial_failed: reply chunk sent before error"), |
| 230 | + ); |
228 | 231 | }); |
229 | 232 | const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn() }; |
230 | 233 |
|
@@ -256,6 +259,61 @@ describe("session-delivery queue recovery", () => { |
256 | 259 | }); |
257 | 260 | }); |
258 | 261 |
|
| 262 | + it("does not re-deliver a partially sent entry even if persisting the unknown marker fails", async () => { |
| 263 | + await withTempDir({ prefix: "openclaw-session-delivery-" }, async (tempDir) => { |
| 264 | + const id = await enqueueSessionDelivery( |
| 265 | + { |
| 266 | + kind: "agentTurn", |
| 267 | + sessionKey: "agent:main:main", |
| 268 | + message: "continue", |
| 269 | + messageId: "restart-sentinel:agent:main:main:agentTurn:654", |
| 270 | + route: { channel: "telegram", to: "chat-1", chatType: "direct" }, |
| 271 | + }, |
| 272 | + tempDir, |
| 273 | + ); |
| 274 | + |
| 275 | + let deliverCount = 0; |
| 276 | + // Model a partial_failed send whose unknown_after_send marker write ALSO |
| 277 | + // fails: the delivery seam still throws the sent-before-error signal, but |
| 278 | + // the entry is left at send_attempt_started (the marker was not advanced). |
| 279 | + // Recovery must refuse the blind replay from the thrown signal alone, not |
| 280 | + // from the (missing) durable marker. |
| 281 | + const deliver = vi.fn(async () => { |
| 282 | + deliverCount += 1; |
| 283 | + // No markSessionDeliveryPlatformOutcomeUnknown call here: the marker write failed. |
| 284 | + throw new SessionDeliverySendUncertainError( |
| 285 | + new Error("partial_failed with marker write failure"), |
| 286 | + ); |
| 287 | + }); |
| 288 | + const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn() }; |
| 289 | + |
| 290 | + const drainOnce = () => |
| 291 | + drainPendingSessionDeliveries({ |
| 292 | + drainKey: "test-sent-before-error-marker-fail", |
| 293 | + logLabel: "test sent-before-error marker fail", |
| 294 | + deliver, |
| 295 | + stateDir: tempDir, |
| 296 | + log, |
| 297 | + selectEntry: (entry) => ({ match: entry.id === id, bypassBackoff: true }), |
| 298 | + }); |
| 299 | + |
| 300 | + // PASS 1: delivery partially sends but the marker write failed, so the |
| 301 | + // entry is left at send_attempt_started. |
| 302 | + await drainOnce(); |
| 303 | + // It must still be moved to failed/, not requeued. |
| 304 | + expect(await loadPendingSessionDeliveries(tempDir)).toStrictEqual([]); |
| 305 | + |
| 306 | + // PASS 2: restart recovery re-entry. |
| 307 | + await drainOnce(); |
| 308 | + |
| 309 | + // Before the fix: the catch saw only send_attempt_started (the marker write |
| 310 | + // failed) and requeued, so PASS 2 blind-replays -> deliverCount === 2. |
| 311 | + // After the fix: the thrown sent-before-error signal makes it |
| 312 | + // non-replayable -> deliverCount === 1. |
| 313 | + expect(deliverCount).toBe(1); |
| 314 | + }); |
| 315 | + }); |
| 316 | + |
259 | 317 | it("does not re-deliver when the outcome-marker write fails after a successful delivery", async () => { |
260 | 318 | await withTempDir({ prefix: "openclaw-session-delivery-" }, async (tempDir) => { |
261 | 319 | const id = await enqueueSessionDelivery( |
|
0 commit comments