-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace-condition.test.ts
More file actions
582 lines (500 loc) · 21.9 KB
/
race-condition.test.ts
File metadata and controls
582 lines (500 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import { describe, test, expect, mock, beforeEach } from "bun:test"
import { createFallbackState, prepareFallback } from "./fallback-state"
import { DEFAULT_CONFIG } from "./constants"
import type { HookDeps, FallbackPluginConfig, FallbackState } from "./types"
/**
* Helper to create mock HookDeps with controllable behavior.
* promptAsyncFn can be customized to simulate success, failure, or delays.
*/
function createMockDeps(overrides?: Partial<{
messagesData: Array<{
info?: Record<string, unknown>
role?: string
parts?: Array<{ type?: string; text?: string } & Record<string, unknown>>
}>
promptAsyncFn: (...args: unknown[]) => Promise<void>
abortFn: (...args: unknown[]) => Promise<void>
config: Partial<FallbackPluginConfig>
agentConfigs: Record<string, unknown>
globalFallbackModels: string[]
}>): HookDeps {
const messagesData = overrides?.messagesData ?? [
{ info: { role: "user" }, parts: [{ type: "text", text: "hello" }] },
{ info: { role: "assistant" }, parts: [{ type: "text", text: "response" }] },
]
const promptAsyncFn = overrides?.promptAsyncFn ?? (async () => {})
const abortFn = overrides?.abortFn ?? (async () => {})
return {
ctx: {
directory: "/test",
client: {
session: {
abort: mock(abortFn as any),
messages: mock(async () => ({ data: messagesData })),
promptAsync: mock(promptAsyncFn as any),
get: mock(async () => ({ data: {} })),
},
tui: {
showToast: mock(async () => {}),
},
},
},
config: { ...DEFAULT_CONFIG, ...overrides?.config } as Required<FallbackPluginConfig>,
agentConfigs: overrides?.agentConfigs ?? undefined,
globalFallbackModels: overrides?.globalFallbackModels ?? [],
sessionStates: new Map(),
sessionLastAccess: new Map(),
sessionRetryInFlight: new Set(),
sessionAwaitingFallbackResult: new Set(),
sessionFallbackTimeouts: new Map(),
sessionFirstTokenReceived: new Map(),
sessionSelfAbortTimestamp: new Map(),
sessionParentID: new Map(),
sessionIdleResolvers: new Map(),
sessionLastMessageTime: new Map(),
sessionCompactionInFlight: new Set(),
}
}
describe("race condition protection", () => {
describe("#given concurrent prepareFallback calls on shared state", () => {
describe("#when two handlers call prepareFallback simultaneously without locking", () => {
test("#then the second call advances state past the end of the fallback chain", () => {
// This test demonstrates the race condition that existed before the fix.
// Both handlers share the same state object and both mutate it.
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b"]
// First handler calls prepareFallback - advances to model-a
const result1 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result1.success).toBe(true)
expect(result1.newModel).toBe("fallback/model-a")
expect(state.fallbackIndex).toBe(0)
expect(state.attemptCount).toBe(1)
// Second handler calls prepareFallback on the SAME state - advances to model-b
const result2 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result2.success).toBe(true)
expect(result2.newModel).toBe("fallback/model-b")
expect(state.fallbackIndex).toBe(1)
expect(state.attemptCount).toBe(2)
// A THIRD call would exhaust the chain
const result3 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result3.success).toBe(false)
expect(result3.error).toContain("No available fallback models")
})
})
describe("#when the retry lock prevents the second handler from calling prepareFallback", () => {
test("#then only one handler advances the state", () => {
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b", "fallback/model-c"]
const retryInFlight = new Set<string>()
// Simulate handler 1 (message.updated): acquires lock, calls prepareFallback
expect(retryInFlight.has("session-1")).toBe(false)
retryInFlight.add("session-1")
const result1 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result1.success).toBe(true)
expect(result1.newModel).toBe("fallback/model-a")
// Simulate handler 2 (session.error): checks lock, finds it held, skips
expect(retryInFlight.has("session-1")).toBe(true)
// Handler 2 would return early here without calling prepareFallback
// State should only have advanced once
expect(state.attemptCount).toBe(1)
expect(state.currentModel).toBe("fallback/model-a")
expect(state.fallbackIndex).toBe(0)
// Release lock
retryInFlight.delete("session-1")
expect(retryInFlight.has("session-1")).toBe(false)
})
})
})
describe("#given retry lock lifecycle", () => {
describe("#when prepareFallback succeeds and autoRetry completes", () => {
test("#then lock is acquired before prepareFallback and released after autoRetry", () => {
const retryInFlight = new Set<string>()
// Lock acquired
retryInFlight.add("session-1")
expect(retryInFlight.has("session-1")).toBe(true)
// prepareFallback runs...
const state = createFallbackState("primary/model")
const result = prepareFallback("session-1", state, ["fallback/model-a"], DEFAULT_CONFIG)
expect(result.success).toBe(true)
// Lock still held during autoRetry
expect(retryInFlight.has("session-1")).toBe(true)
// autoRetry completes, lock released in finally block
retryInFlight.delete("session-1")
expect(retryInFlight.has("session-1")).toBe(false)
})
})
describe("#when prepareFallback fails (exhausted models)", () => {
test("#then lock is released immediately", () => {
const retryInFlight = new Set<string>()
const state = createFallbackState("primary/model")
state.fallbackIndex = 0
state.failedModels.set("fallback/model-a", Date.now())
// Lock acquired
retryInFlight.add("session-1")
// prepareFallback fails
const result = prepareFallback("session-1", state, ["fallback/model-a"], DEFAULT_CONFIG)
expect(result.success).toBe(false)
// Lock should be released on failure path
retryInFlight.delete("session-1")
expect(retryInFlight.has("session-1")).toBe(false)
})
})
describe("#when autoRetry throws an exception", () => {
test("#then lock is released via finally block", async () => {
const retryInFlight = new Set<string>()
retryInFlight.add("session-1")
expect(retryInFlight.has("session-1")).toBe(true)
// Simulate the try/finally pattern used in the handlers
let errorCaught = false
try {
// Simulate autoRetry throwing
await (async () => { throw new Error("Network error during replay") })()
} catch {
errorCaught = true
} finally {
retryInFlight.delete("session-1")
}
expect(errorCaught).toBe(true)
expect(retryInFlight.has("session-1")).toBe(false)
})
})
describe("#when lock is released after first handler", () => {
test("#then a subsequent error from the fallback model can acquire the lock and proceed", () => {
const retryInFlight = new Set<string>()
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b"]
// First error: primary model fails -> fallback to model-a
retryInFlight.add("session-1")
const result1 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result1.success).toBe(true)
expect(result1.newModel).toBe("fallback/model-a")
retryInFlight.delete("session-1") // released after autoRetry
// Second error: model-a also fails -> fallback to model-b
expect(retryInFlight.has("session-1")).toBe(false)
retryInFlight.add("session-1")
const result2 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result2.success).toBe(true)
expect(result2.newModel).toBe("fallback/model-b")
retryInFlight.delete("session-1")
expect(state.attemptCount).toBe(2)
expect(state.currentModel).toBe("fallback/model-b")
})
})
})
describe("#given fallback chain progression when fallback model fails", () => {
describe("#when the first fallback model hits a quota error", () => {
test("#then state advances to the next fallback model in the chain", () => {
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b", "fallback/model-c"]
// Primary fails -> advance to model-a
const result1 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result1.success).toBe(true)
expect(result1.newModel).toBe("fallback/model-a")
expect(state.currentModel).toBe("fallback/model-a")
// model-a hits quota -> advance to model-b
const result2 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result2.success).toBe(true)
expect(result2.newModel).toBe("fallback/model-b")
expect(state.currentModel).toBe("fallback/model-b")
// model-b also fails -> advance to model-c
const result3 = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result3.success).toBe(true)
expect(result3.newModel).toBe("fallback/model-c")
expect(state.currentModel).toBe("fallback/model-c")
// All failed models are recorded with timestamps
expect(state.failedModels.has("primary/model")).toBe(true)
expect(state.failedModels.has("fallback/model-a")).toBe(true)
expect(state.failedModels.has("fallback/model-b")).toBe(true)
})
})
describe("#when all fallback models fail sequentially", () => {
test("#then eventually returns exhausted after the last model", () => {
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b"]
prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
// Now all are exhausted
const result = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result.success).toBe(false)
expect(result.error).toContain("No available fallback models")
})
})
describe("#when max_fallback_attempts is reached before chain is exhausted", () => {
test("#then returns maxAttemptsReached even if more models are available", () => {
const config = { ...DEFAULT_CONFIG, max_fallback_attempts: 2 }
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b", "fallback/model-c"]
prepareFallback("session-1", state, fallbackModels, config)
prepareFallback("session-1", state, fallbackModels, config)
// Attempt 3 blocked by max_fallback_attempts=2
const result = prepareFallback("session-1", state, fallbackModels, config)
expect(result.success).toBe(false)
expect(result.maxAttemptsReached).toBe(true)
expect(state.attemptCount).toBe(2)
})
})
})
describe("#given pendingFallbackModel guard in message.updated handler", () => {
describe("#when error comes from a model that is NOT the pending fallback", () => {
test("#then the error is for a stale model and should be skipped", () => {
const state = createFallbackState("primary/model")
state.pendingFallbackModel = "fallback/model-a"
state.currentModel = "fallback/model-a"
// Error comes from "primary/model" (stale) while waiting for "fallback/model-a"
const errorModel = "primary/model"
const shouldSkip = state.pendingFallbackModel && errorModel !== state.pendingFallbackModel
expect(shouldSkip).toBe(true)
})
})
describe("#when error comes from the pending fallback model itself", () => {
test("#then the error is for the active fallback and should NOT be skipped", () => {
const state = createFallbackState("primary/model")
state.pendingFallbackModel = "fallback/model-a"
state.currentModel = "fallback/model-a"
// Error comes from "fallback/model-a" (current fallback hit quota)
const errorModel = "fallback/model-a"
const shouldSkip = state.pendingFallbackModel && errorModel !== state.pendingFallbackModel
expect(shouldSkip).toBe(false)
})
})
describe("#when there is no pending fallback model", () => {
test("#then the guard does not skip", () => {
const state = createFallbackState("primary/model")
state.pendingFallbackModel = undefined
const errorModel = "primary/model"
const shouldSkip = state.pendingFallbackModel && errorModel !== state.pendingFallbackModel
expect(shouldSkip).toBeFalsy()
})
})
})
describe("#given concurrent event simulation end-to-end", () => {
describe("#when message.updated and session.error fire for the same failure", () => {
test("#then only one handler processes the fallback, state advances exactly once", () => {
const state = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b"]
const retryInFlight = new Set<string>()
// Simulate the race: both handlers check lock and try to proceed
// Handler 1 (message.updated) wins the race
const handler1Acquired = !retryInFlight.has("session-1")
if (handler1Acquired) {
retryInFlight.add("session-1")
}
// Handler 2 (session.error) loses the race
const handler2Acquired = !retryInFlight.has("session-1")
// handler2Acquired is false because handler1 already set the lock
expect(handler1Acquired).toBe(true)
expect(handler2Acquired).toBe(false)
// Only handler 1 calls prepareFallback
if (handler1Acquired) {
const result = prepareFallback("session-1", state, fallbackModels, DEFAULT_CONFIG)
expect(result.success).toBe(true)
expect(result.newModel).toBe("fallback/model-a")
}
// State should reflect exactly ONE advancement
expect(state.attemptCount).toBe(1)
expect(state.currentModel).toBe("fallback/model-a")
expect(state.fallbackIndex).toBe(0)
// Handler 1 completes, releases lock
retryInFlight.delete("session-1")
})
})
describe("#when three events fire for the same failure", () => {
test("#then only the first one processes, the other two are skipped", () => {
const retryInFlight = new Set<string>()
let prepareFallbackCallCount = 0
const tryAcquireAndProcess = () => {
if (retryInFlight.has("session-1")) {
return false // skipped
}
retryInFlight.add("session-1")
prepareFallbackCallCount++
return true // processed
}
const results = [
tryAcquireAndProcess(), // handler 1 - wins
tryAcquireAndProcess(), // handler 2 - skipped
tryAcquireAndProcess(), // handler 3 - skipped
]
expect(results).toEqual([true, false, false])
expect(prepareFallbackCallCount).toBe(1)
// Cleanup
retryInFlight.delete("session-1")
})
})
})
describe("#given state isolation between sessions", () => {
describe("#when two different sessions have errors at the same time", () => {
test("#then each session's lock and state are independent", () => {
const retryInFlight = new Set<string>()
const state1 = createFallbackState("primary/model")
const state2 = createFallbackState("primary/model")
const fallbackModels = ["fallback/model-a", "fallback/model-b"]
// Session 1 acquires lock
retryInFlight.add("session-1")
const result1 = prepareFallback("session-1", state1, fallbackModels, DEFAULT_CONFIG)
// Session 2 should NOT be blocked by session 1's lock
expect(retryInFlight.has("session-2")).toBe(false)
retryInFlight.add("session-2")
const result2 = prepareFallback("session-2", state2, fallbackModels, DEFAULT_CONFIG)
// Both should succeed independently
expect(result1.success).toBe(true)
expect(result1.newModel).toBe("fallback/model-a")
expect(result2.success).toBe(true)
expect(result2.newModel).toBe("fallback/model-a")
// States are independent
expect(state1.attemptCount).toBe(1)
expect(state2.attemptCount).toBe(1)
retryInFlight.delete("session-1")
retryInFlight.delete("session-2")
})
})
})
describe("#given self-abort protection (sessionSelfAbortTimestamp)", () => {
describe("#when abortSessionRequest is called", () => {
test("#then sessionSelfAbortTimestamp is set for the session", async () => {
const deps = createMockDeps()
const { createAutoRetryHelpers } = await import("./auto-retry")
const helpers = createAutoRetryHelpers(deps)
expect(deps.sessionSelfAbortTimestamp.has("test-session")).toBe(false)
await helpers.abortSessionRequest("test-session", "test")
expect(deps.sessionSelfAbortTimestamp.has("test-session")).toBe(true)
expect(typeof deps.sessionSelfAbortTimestamp.get("test-session")).toBe("number")
})
})
describe("#when MessageAbortedError arrives within self-abort window", () => {
test("#then it is ignored if sessionAwaitingFallbackResult is set", async () => {
const deps = createMockDeps({
agentConfigs: {
test: {
model: "primary/model",
fallback_models: ["fallback/model-a", "fallback/model-b"],
},
},
})
// Set up state as if we just sent a fallback request to model-a (index 0)
const state = createFallbackState("primary/model")
state.currentModel = "fallback/model-a"
state.originalModel = "primary/model"
state.attemptCount = 1
state.fallbackIndex = 0 // model-a is at index 0
deps.sessionStates.set("test-session", state)
deps.sessionAwaitingFallbackResult.add("test-session")
// Mark self-abort as very recent (now)
deps.sessionSelfAbortTimestamp.set("test-session", Date.now())
const { createMessageUpdateHandler } = await import("./message-update-handler")
const { createAutoRetryHelpers } = await import("./auto-retry")
const helpers = createAutoRetryHelpers(deps)
const handler = createMessageUpdateHandler(deps, helpers)
// Simulate MessageAbortedError on the current fallback model
await handler({
info: {
role: "assistant",
sessionID: "test-session",
model: "fallback/model-a",
error: { name: "MessageAbortedError", message: "aborted" },
},
})
// The state should NOT have advanced — the error was ignored
expect(state.attemptCount).toBe(1)
expect(state.fallbackIndex).toBe(0)
expect(state.currentModel).toBe("fallback/model-a")
// Should still be awaiting the fallback result
expect(deps.sessionAwaitingFallbackResult.has("test-session")).toBe(true)
})
})
describe("#when MessageAbortedError arrives outside self-abort window", () => {
test("#then it is treated as a real error and advances the chain", async () => {
const deps = createMockDeps({
messagesData: [
{
info: { role: "user" },
parts: [{ type: "text", text: "test" }],
},
],
agentConfigs: {
test: {
model: "primary/model",
fallback_models: ["fallback/model-a", "fallback/model-b"],
},
},
})
// Set up state as if we just sent a fallback request to model-a (index 0)
const state = createFallbackState("primary/model")
state.currentModel = "fallback/model-a"
state.originalModel = "primary/model"
state.attemptCount = 1
state.fallbackIndex = 0 // model-a is at index 0, so next search starts at 1
deps.sessionStates.set("test-session", state)
deps.sessionAwaitingFallbackResult.add("test-session")
// Mark self-abort as OLD (3 seconds ago, beyond 2s window)
deps.sessionSelfAbortTimestamp.set("test-session", Date.now() - 3000)
const { createMessageUpdateHandler } = await import("./message-update-handler")
const { createAutoRetryHelpers } = await import("./auto-retry")
const helpers = createAutoRetryHelpers(deps)
const handler = createMessageUpdateHandler(deps, helpers)
// Simulate MessageAbortedError on the current fallback model
await handler({
info: {
role: "assistant",
sessionID: "test-session",
model: "fallback/model-a",
error: { name: "MessageAbortedError", message: "aborted" },
},
})
// The chain should have advanced (non-retryable but in fallback chain)
expect(state.attemptCount).toBe(2)
expect(state.currentModel).toBe("fallback/model-b")
})
})
describe("#when MessageAbortedError arrives with no self-abort timestamp", () => {
test("#then it is treated as user cancellation in fallback chain", async () => {
const deps = createMockDeps({
messagesData: [
{
info: { role: "user" },
parts: [{ type: "text", text: "test" }],
},
],
agentConfigs: {
test: {
model: "primary/model",
fallback_models: ["fallback/model-a", "fallback/model-b"],
},
},
})
const state = createFallbackState("primary/model")
state.currentModel = "fallback/model-a"
state.originalModel = "primary/model"
state.attemptCount = 1
state.fallbackIndex = 0 // model-a is at index 0
deps.sessionStates.set("test-session", state)
deps.sessionAwaitingFallbackResult.add("test-session")
// No self-abort timestamp at all
const { createMessageUpdateHandler } = await import("./message-update-handler")
const { createAutoRetryHelpers } = await import("./auto-retry")
const helpers = createAutoRetryHelpers(deps)
const handler = createMessageUpdateHandler(deps, helpers)
await handler({
info: {
role: "assistant",
sessionID: "test-session",
model: "fallback/model-a",
error: { name: "MessageAbortedError", message: "aborted" },
},
})
// Without self-abort, this is treated as a real error — chain advances
expect(state.attemptCount).toBe(2)
})
})
describe("#when self-abort timestamp is cleaned up", () => {
test("#then sessionSelfAbortTimestamp is removed on session deletion", () => {
const deps = createMockDeps()
deps.sessionSelfAbortTimestamp.set("test-session", Date.now())
expect(deps.sessionSelfAbortTimestamp.has("test-session")).toBe(true)
// Simulate what handleSessionDeleted does
deps.sessionSelfAbortTimestamp.delete("test-session")
expect(deps.sessionSelfAbortTimestamp.has("test-session")).toBe(false)
})
})
})
})