-
-
Notifications
You must be signed in to change notification settings - Fork 80.7k
Expand file tree
/
Copy pathcommand-queue.test.ts
More file actions
877 lines (753 loc) · 27 KB
/
Copy pathcommand-queue.test.ts
File metadata and controls
877 lines (753 loc) · 27 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// Command queue tests cover bounded command execution and queue ordering.
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { CommandLane } from "./lanes.js";
const diagnosticMocks = vi.hoisted(() => ({
logLaneEnqueue: vi.fn(),
logLaneDequeue: vi.fn(),
diag: {
debug: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
vi.mock("../logging/diagnostic-runtime.js", () => ({
logLaneEnqueue: diagnosticMocks.logLaneEnqueue,
logLaneDequeue: diagnosticMocks.logLaneDequeue,
diagnosticLogger: diagnosticMocks.diag,
}));
type CommandQueueModule = typeof import("./command-queue.js");
let clearCommandLane: CommandQueueModule["clearCommandLane"];
let CommandLaneClearedError: CommandQueueModule["CommandLaneClearedError"];
let CommandLaneTaskTimeoutError: CommandQueueModule["CommandLaneTaskTimeoutError"];
let enqueueCommand: CommandQueueModule["enqueueCommand"];
let enqueueCommandInLane: CommandQueueModule["enqueueCommandInLane"];
let GatewayDrainingError: CommandQueueModule["GatewayDrainingError"];
let getActiveTaskCount: CommandQueueModule["getActiveTaskCount"];
let getCommandLaneSnapshot: CommandQueueModule["getCommandLaneSnapshot"];
let getCommandLaneSnapshots: CommandQueueModule["getCommandLaneSnapshots"];
let getQueueSize: CommandQueueModule["getQueueSize"];
let markGatewayDraining: CommandQueueModule["markGatewayDraining"];
let resetAllLanes: CommandQueueModule["resetAllLanes"];
let resetCommandLane: CommandQueueModule["resetCommandLane"];
let resetCommandQueueStateForTest: CommandQueueModule["resetCommandQueueStateForTest"];
let setCommandLaneConcurrency: CommandQueueModule["setCommandLaneConcurrency"];
let waitForActiveTasks: CommandQueueModule["waitForActiveTasks"];
function createDeferred(): { promise: Promise<void>; resolve: () => void } {
let resolve: (() => void) | undefined;
const promise = new Promise<void>((r) => {
resolve = r;
});
if (!resolve) {
throw new Error("Expected deferred resolver to be initialized");
}
return { promise, resolve };
}
function mockCallArg(
mock: { mock: { calls: readonly unknown[][] } },
label: string,
argIndex: number,
): unknown {
const [call] = mock.mock.calls;
if (!call) {
throw new Error(`expected ${label} call`);
}
return call[argIndex];
}
function enqueueBlockedMainTask<T = void>(
onRelease?: () => Promise<T> | T,
): {
task: Promise<T>;
release: () => void;
} {
const deferred = createDeferred();
const task = enqueueCommand(async () => {
await deferred.promise;
return (await onRelease?.()) as T;
});
return { task, release: deferred.resolve };
}
function expectLaneSnapshotFields(
lane: string,
fields: Partial<ReturnType<CommandQueueModule["getCommandLaneSnapshot"]>>,
): void {
const snapshot = getCommandLaneSnapshot(lane);
for (const [key, value] of Object.entries(fields)) {
expect(snapshot[key as keyof typeof snapshot]).toBe(value);
}
}
function diagnosticDebugMessages(): string[] {
return diagnosticMocks.diag.debug.mock.calls
.map(([message]) => message)
.filter((message): message is string => typeof message === "string");
}
describe("command queue", () => {
beforeAll(async () => {
({
clearCommandLane,
CommandLaneClearedError,
CommandLaneTaskTimeoutError,
enqueueCommand,
enqueueCommandInLane,
GatewayDrainingError,
getActiveTaskCount,
getCommandLaneSnapshot,
getCommandLaneSnapshots,
getQueueSize,
markGatewayDraining,
resetAllLanes,
resetCommandLane,
resetCommandQueueStateForTest,
setCommandLaneConcurrency,
waitForActiveTasks,
} = await import("./command-queue.js"));
});
beforeEach(() => {
vi.useRealTimers();
resetCommandQueueStateForTest();
// Queue state is global across module instances, so reset main lane
// concurrency explicitly to avoid cross-file leakage.
setCommandLaneConcurrency(CommandLane.Main, 1);
diagnosticMocks.logLaneEnqueue.mockClear();
diagnosticMocks.logLaneDequeue.mockClear();
diagnosticMocks.diag.debug.mockClear();
diagnosticMocks.diag.warn.mockClear();
diagnosticMocks.diag.error.mockClear();
});
afterEach(() => {
vi.useRealTimers();
});
it("resetAllLanes is safe when no lanes have been created", () => {
expect(getActiveTaskCount()).toBe(0);
resetAllLanes();
expect(getActiveTaskCount()).toBe(0);
});
it("runs tasks one at a time in order", async () => {
let active = 0;
let maxActive = 0;
const calls: number[] = [];
const makeTask = (id: number) => async () => {
active += 1;
maxActive = Math.max(maxActive, active);
calls.push(id);
await Promise.resolve();
active -= 1;
return id;
};
const results = await Promise.all([
enqueueCommand(makeTask(1)),
enqueueCommand(makeTask(2)),
enqueueCommand(makeTask(3)),
]);
expect(results).toEqual([1, 2, 3]);
expect(calls).toEqual([1, 2, 3]);
expect(maxActive).toBe(1);
expect(getQueueSize()).toBe(0);
});
it("runs foreground work before already queued background work", async () => {
const { task: blocker, release } = enqueueBlockedMainTask(async () => "blocker");
const calls: string[] = [];
const background = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("background");
return "background";
},
{ priority: "background" },
);
const normal = enqueueCommandInLane(CommandLane.Main, async () => {
calls.push("normal");
return "normal";
});
const foreground = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("foreground");
return "foreground";
},
{ priority: "foreground" },
);
release();
await expect(blocker).resolves.toBe("blocker");
await expect(foreground).resolves.toBe("foreground");
await expect(normal).resolves.toBe("normal");
await expect(background).resolves.toBe("background");
expect(calls).toEqual(["foreground", "normal", "background"]);
});
it("preserves FIFO order within each priority", async () => {
const { task: blocker, release } = enqueueBlockedMainTask(async () => "blocker");
const calls: string[] = [];
const first = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("first");
},
{ priority: "foreground" },
);
const second = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("second");
},
{ priority: "foreground" },
);
release();
await blocker;
await Promise.all([first, second]);
expect(calls).toEqual(["first", "second"]);
});
it("reports queueAhead after priority insertion", async () => {
vi.useFakeTimers();
try {
const { task: blocker, release } = enqueueBlockedMainTask(async () => "blocker");
const calls: string[] = [];
let queuedAhead: number | null = null;
const background = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("background");
return "background";
},
{ priority: "background" },
);
const foreground = enqueueCommandInLane(
CommandLane.Main,
async () => {
calls.push("foreground");
return "foreground";
},
{
priority: "foreground",
warnAfterMs: 5,
onWait: (_ms, ahead) => {
queuedAhead = ahead;
},
},
);
await vi.advanceTimersByTimeAsync(6);
release();
await expect(blocker).resolves.toBe("blocker");
await expect(foreground).resolves.toBe("foreground");
await expect(background).resolves.toBe("background");
expect(calls).toEqual(["foreground", "background"]);
expect(queuedAhead).toBe(0);
const waitWarning = diagnosticMocks.diag.warn.mock.calls.find(
([message]) =>
typeof message === "string" && message.includes("lane wait exceeded: lane=main"),
);
expect(waitWarning?.[0]).toContain("queueAhead=0 activeAhead=1");
expect(waitWarning?.[0]).toContain("queueBehind=1");
} finally {
vi.useRealTimers();
}
});
it("logs enqueue depth after push", async () => {
const task = enqueueCommand(async () => {});
expect(diagnosticMocks.logLaneEnqueue).toHaveBeenCalledTimes(1);
expect(mockCallArg(diagnosticMocks.logLaneEnqueue, "logLaneEnqueue", 1)).toBe(1);
await task;
});
it("invokes onWait callback when a task waits past the threshold", async () => {
let waited: number | null = null;
let queuedAhead: number | null = null;
vi.useFakeTimers();
try {
const blocker = createDeferred();
const first = enqueueCommand(async () => {
await blocker.promise;
});
const second = enqueueCommand(async () => {}, {
warnAfterMs: 5,
onWait: (ms, ahead) => {
waited = ms;
queuedAhead = ahead;
},
});
await vi.advanceTimersByTimeAsync(6);
blocker.resolve();
await Promise.all([first, second]);
expect(typeof waited).toBe("number");
expect(waited).toBeGreaterThanOrEqual(5);
expect(queuedAhead).toBe(0);
const waitWarning = diagnosticMocks.diag.warn.mock.calls.find(
([message]) =>
typeof message === "string" && message.includes("lane wait exceeded: lane=main"),
);
expect(waitWarning?.[0]).toContain("queueAhead=0 activeAhead=1");
} finally {
vi.useRealTimers();
}
});
it("demotes live model switch lane failures to debug noise", async () => {
const error = new Error("Live session model switch requested: anthropic/claude-opus-4-6");
error.name = "LiveSessionModelSwitchError";
await expect(
enqueueCommandInLane("nested", async () => {
throw error;
}),
).rejects.toBe(error);
expect(diagnosticMocks.diag.error).not.toHaveBeenCalled();
expect(
diagnosticDebugMessages().some((message) =>
message.includes("lane task interrupted: lane=nested"),
),
).toBe(true);
});
it("getActiveTaskCount returns count of currently executing tasks", async () => {
const { task, release } = enqueueBlockedMainTask();
expect(getActiveTaskCount()).toBe(1);
release();
await task;
expect(getActiveTaskCount()).toBe(0);
});
it("waitForActiveTasks resolves immediately when no tasks are active", async () => {
const { drained } = await waitForActiveTasks(1000);
expect(drained).toBe(true);
});
it("waitForActiveTasks waits for active tasks to finish", async () => {
const { task, release } = enqueueBlockedMainTask();
vi.useFakeTimers();
try {
const drainPromise = waitForActiveTasks(5000);
await vi.advanceTimersByTimeAsync(50);
release();
await vi.advanceTimersByTimeAsync(50);
const { drained } = await drainPromise;
expect(drained).toBe(true);
await task;
} finally {
vi.useRealTimers();
}
});
it("waitForActiveTasks returns drained=false when timeout is zero and tasks are active", async () => {
const { task, release } = enqueueBlockedMainTask();
const { drained } = await waitForActiveTasks(0);
expect(drained).toBe(false);
release();
await task;
});
it("waitForActiveTasks returns drained=false on timeout", async () => {
const { task, release } = enqueueBlockedMainTask();
vi.useFakeTimers();
try {
const waitPromise = waitForActiveTasks(50);
await vi.advanceTimersByTimeAsync(100);
const { drained } = await waitPromise;
expect(drained).toBe(false);
release();
await task;
} finally {
vi.useRealTimers();
}
});
it("resetAllLanes drains queued work immediately after reset", async () => {
const lane = `reset-test-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
const blocker = createDeferred();
// Start a task that blocks the lane
const task1 = enqueueCommandInLane(lane, async () => {
await blocker.promise;
});
expect(getActiveTaskCount()).toBeGreaterThanOrEqual(1);
// Enqueue another task — it should be stuck behind the blocker
let task2Ran = false;
const task2 = enqueueCommandInLane(lane, async () => {
task2Ran = true;
});
expect(getQueueSize(lane)).toBeGreaterThanOrEqual(2);
expect(task2Ran).toBe(false);
// Simulate SIGUSR1: reset all lanes. Queued work (task2) should be
// drained immediately — no fresh enqueue needed.
resetAllLanes();
// Complete the stale in-flight task; generation mismatch makes its
// completion path a no-op for queue bookkeeping.
blocker.resolve();
await task1;
// task2 should have been pumped by resetAllLanes's drain pass.
await task2;
expect(task2Ran).toBe(true);
});
it("resetCommandLane releases one stuck lane and drains its queued work", async () => {
const lane = `reset-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
const otherLane = `reset-lane-other-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
setCommandLaneConcurrency(otherLane, 1);
const blocker = createDeferred();
const otherBlocker = createDeferred();
const first = enqueueCommandInLane(lane, async () => {
await blocker.promise;
return "first";
});
const other = enqueueCommandInLane(otherLane, async () => {
await otherBlocker.promise;
return "other";
});
let secondRan = false;
const second = enqueueCommandInLane(lane, async () => {
secondRan = true;
return "second";
});
expect(secondRan).toBe(false);
expect(getActiveTaskCount()).toBe(2);
expect(resetCommandLane(lane)).toBe(1);
await expect(second).resolves.toBe("second");
expect(secondRan).toBe(true);
expect(getQueueSize(lane)).toBe(0);
expect(getQueueSize(otherLane)).toBe(1);
blocker.resolve();
otherBlocker.resolve();
await expect(first).resolves.toBe("first");
await expect(other).resolves.toBe("other");
});
it("task timeout releases a stuck lane and drains queued work", async () => {
const lane = `timeout-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
vi.useFakeTimers();
try {
const first = enqueueCommandInLane(lane, async () => new Promise<never>(() => {}), {
taskTimeoutMs: 25,
});
const firstRejected = expect(first).rejects.toBeInstanceOf(CommandLaneTaskTimeoutError);
let secondRan = false;
const second = enqueueCommandInLane(lane, async () => {
secondRan = true;
return "second";
});
expect(secondRan).toBe(false);
expectLaneSnapshotFields(lane, {
activeCount: 1,
queuedCount: 1,
});
await vi.advanceTimersByTimeAsync(25);
await firstRejected;
await expect(second).resolves.toBe("second");
expect(secondRan).toBe(true);
expectLaneSnapshotFields(lane, {
activeCount: 0,
queuedCount: 0,
});
} finally {
vi.useRealTimers();
}
});
it("task timeout renews from progress timestamps", async () => {
const lane = `timeout-progress-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
vi.useFakeTimers();
try {
let progressAtMs = Date.now();
const blocker = createDeferred();
const first = enqueueCommandInLane(
lane,
async () => {
await blocker.promise;
return "first";
},
{
taskTimeoutMs: 25,
taskTimeoutProgressAtMs: () => progressAtMs,
},
);
let secondRan = false;
const second = enqueueCommandInLane(lane, async () => {
secondRan = true;
return "second";
});
await vi.advanceTimersByTimeAsync(20);
progressAtMs = Date.now();
await vi.advanceTimersByTimeAsync(20);
expect(secondRan).toBe(false);
blocker.resolve();
await expect(first).resolves.toBe("first");
await expect(second).resolves.toBe("second");
expect(secondRan).toBe(true);
} finally {
vi.useRealTimers();
}
});
it("task timeout falls back when progress timestamp callback throws", async () => {
const lane = `timeout-progress-throw-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
vi.useFakeTimers();
try {
const first = enqueueCommandInLane(lane, async () => new Promise<never>(() => {}), {
taskTimeoutMs: 25,
taskTimeoutProgressAtMs: () => {
throw new Error("progress failed");
},
});
const firstRejected = expect(first).rejects.toBeInstanceOf(CommandLaneTaskTimeoutError);
await vi.advanceTimersByTimeAsync(25);
await firstRejected;
expect(
diagnosticMocks.diag.warn.mock.calls.some(([message]) =>
String(message).includes("lane task timeout progress callback failed"),
),
).toBe(true);
} finally {
vi.useRealTimers();
}
});
it("keeps work queued while a lane has zero concurrency and drains after resume", async () => {
const lane = `suspended-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 0);
let ran = false;
const task = enqueueCommandInLane(lane, async () => {
ran = true;
return "resumed";
});
await Promise.resolve();
expect(ran).toBe(false);
expectLaneSnapshotFields(lane, {
activeCount: 0,
queuedCount: 1,
maxConcurrent: 0,
});
setCommandLaneConcurrency(lane, 1);
await expect(task).resolves.toBe("resumed");
expect(ran).toBe(true);
expectLaneSnapshotFields(lane, {
activeCount: 0,
queuedCount: 0,
maxConcurrent: 1,
});
});
it("getCommandLaneSnapshot reports active and queued work for one lane", async () => {
const lane = `snapshot-lane-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
const blocker = createDeferred();
const first = enqueueCommandInLane(lane, async () => {
await blocker.promise;
return "first";
});
const second = enqueueCommandInLane(lane, async () => "second");
expectLaneSnapshotFields(lane, {
lane,
activeCount: 1,
queuedCount: 1,
maxConcurrent: 1,
draining: false,
generation: 0,
});
blocker.resolve();
await expect(first).resolves.toBe("first");
await expect(second).resolves.toBe("second");
});
it("getCommandLaneSnapshots reports all live lanes in stable order", async () => {
const alphaLane = `snapshot-all-alpha-${Date.now()}-${Math.random().toString(16).slice(2)}`;
const betaLane = `snapshot-all-beta-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(alphaLane, 1);
setCommandLaneConcurrency(betaLane, 1);
const alphaBlocker = createDeferred();
const betaBlocker = createDeferred();
const alpha = enqueueCommandInLane(alphaLane, async () => {
await alphaBlocker.promise;
return "alpha";
});
const beta = enqueueCommandInLane(betaLane, async () => {
await betaBlocker.promise;
return "beta";
});
const snapshots = getCommandLaneSnapshots().filter(
(snapshot) => snapshot.lane === alphaLane || snapshot.lane === betaLane,
);
expect(snapshots.map((snapshot) => snapshot.lane)).toEqual([alphaLane, betaLane]);
expect(snapshots[0]?.lane).toBe(alphaLane);
expect(snapshots[0]?.activeCount).toBe(1);
expect(snapshots[0]?.queuedCount).toBe(0);
expect(snapshots[1]?.lane).toBe(betaLane);
expect(snapshots[1]?.activeCount).toBe(1);
expect(snapshots[1]?.queuedCount).toBe(0);
alphaBlocker.resolve();
betaBlocker.resolve();
await expect(alpha).resolves.toBe("alpha");
await expect(beta).resolves.toBe("beta");
});
it("waitForActiveTasks ignores tasks that start after the call", async () => {
const lane = `drain-snapshot-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 2);
const blocker1 = createDeferred();
const blocker2 = createDeferred();
const firstStarted = createDeferred();
const first = enqueueCommandInLane(lane, async () => {
firstStarted.resolve();
await blocker1.promise;
});
await firstStarted.promise;
const drainPromise = waitForActiveTasks(2000);
// Starts after waitForActiveTasks snapshot and should not block drain completion.
const second = enqueueCommandInLane(lane, async () => {
await blocker2.promise;
});
expect(getActiveTaskCount()).toBeGreaterThanOrEqual(2);
blocker1.resolve();
const { drained } = await drainPromise;
expect(drained).toBe(true);
blocker2.resolve();
await Promise.all([first, second]);
});
it("clearCommandLane rejects pending promises", async () => {
// First task blocks the lane.
const { task: first, release } = enqueueBlockedMainTask(async () => "first");
// Second task is queued behind the first.
const second = enqueueCommand(async () => "second");
const removed = clearCommandLane();
expect(removed).toBe(1); // only the queued (not active) entry
// The queued promise should reject.
await expect(second).rejects.toBeInstanceOf(CommandLaneClearedError);
// Let the active task finish normally.
release();
await expect(first).resolves.toBe("first");
});
it("keeps draining functional after synchronous onWait failure", async () => {
const lane = `drain-sync-throw-${Date.now()}-${Math.random().toString(16).slice(2)}`;
setCommandLaneConcurrency(lane, 1);
const deferred = createDeferred();
const first = enqueueCommandInLane(lane, async () => {
await deferred.promise;
return "first";
});
const second = enqueueCommandInLane(lane, async () => "second", {
warnAfterMs: 0,
onWait: () => {
throw new Error("onWait exploded");
},
});
await Promise.resolve();
expect(getQueueSize(lane)).toBeGreaterThanOrEqual(2);
deferred.resolve();
await expect(first).resolves.toBe("first");
await expect(second).resolves.toBe("second");
});
it("rejects new enqueues with GatewayDrainingError after markGatewayDraining", async () => {
markGatewayDraining();
await expect(enqueueCommand(async () => "blocked")).rejects.toBeInstanceOf(
GatewayDrainingError,
);
});
it("does not affect already-active tasks after markGatewayDraining", async () => {
const { task, release } = enqueueBlockedMainTask(async () => "ok");
markGatewayDraining();
release();
await expect(task).resolves.toBe("ok");
});
it("resetAllLanes clears gateway draining flag and re-allows enqueue", async () => {
markGatewayDraining();
resetAllLanes();
await expect(enqueueCommand(async () => "ok")).resolves.toBe("ok");
});
it("migrates legacy queue state missing activeTaskWaiters without crashing", async () => {
// Simulate a SIGUSR1 in-process restart where the globalThis singleton was
// created by an older code version (e.g. v2026.4.2) that did not include
// the `activeTaskWaiters` field. The schema migration in getQueueState()
// must patch the missing field so resetAllLanes() and
// notifyActiveTaskWaiters() do not throw.
const key = Symbol.for("openclaw.commandQueueState");
const globalStore = globalThis as Record<PropertyKey, unknown>;
const original = globalStore[key];
try {
// Plant a legacy-shaped state object (no activeTaskWaiters).
globalStore[key] = {
gatewayDraining: false,
lanes: new Map(),
nextTaskId: 1,
};
// resetAllLanes calls notifyActiveTaskWaiters → Array.from(state.activeTaskWaiters).
// Without the migration this would throw:
// TypeError: undefined is not iterable
resetAllLanes();
// waitForActiveTasks also accesses activeTaskWaiters.
await expect(waitForActiveTasks(0)).resolves.toEqual({ drained: true });
} finally {
// Restore original state so subsequent tests are not affected.
if (original !== undefined) {
globalStore[key] = original;
} else {
delete globalStore[key];
}
resetCommandQueueStateForTest();
}
});
it("migrates legacy queued entries missing priority and wait diagnostics", async () => {
const key = Symbol.for("openclaw.commandQueueState");
const globalStore = globalThis as Record<PropertyKey, unknown>;
const original = globalStore[key];
let queuedAhead: number | null = null;
const legacyTask = new Promise<string>((resolve, reject) => {
globalStore[key] = {
gatewayDraining: false,
lanes: new Map([
[
CommandLane.Main,
{
lane: CommandLane.Main,
queue: [
{
task: async () => "done",
resolve,
reject,
enqueuedAt: Date.now() - 10,
warnAfterMs: 0,
onWait: (_ms: number, ahead: number) => {
queuedAhead = ahead;
},
},
],
activeTaskIds: new Set(),
maxConcurrent: 1,
draining: false,
generation: 0,
},
],
]),
activeTaskWaiters: new Set(),
nextTaskId: 1,
nextQueueSequence: 1,
};
});
try {
resetAllLanes();
await expect(legacyTask).resolves.toBe("done");
expect(queuedAhead).toBe(0);
const waitWarning = diagnosticMocks.diag.warn.mock.calls.find(
([message]) =>
typeof message === "string" && message.includes("lane wait exceeded: lane=main"),
);
expect(waitWarning?.[0]).toContain("queueAhead=0 activeAhead=0");
} finally {
if (original !== undefined) {
globalStore[key] = original;
} else {
delete globalStore[key];
}
resetCommandQueueStateForTest();
}
});
it("shares lane state across distinct module instances", async () => {
const commandQueueA = await importFreshModule<typeof import("./command-queue.js")>(
import.meta.url,
"./command-queue.js?scope=shared-a",
);
const commandQueueB = await importFreshModule<typeof import("./command-queue.js")>(
import.meta.url,
"./command-queue.js?scope=shared-b",
);
const lane = `shared-state-${Date.now()}-${Math.random().toString(16).slice(2)}`;
const blocker = createDeferred();
commandQueueA.resetAllLanes();
try {
const task = commandQueueA.enqueueCommandInLane(lane, async () => {
await blocker.promise;
return "done";
});
expect(commandQueueB.getQueueSize(lane)).toBe(1);
expect(commandQueueB.getActiveTaskCount()).toBe(1);
blocker.resolve();
await expect(task).resolves.toBe("done");
expect(commandQueueB.getQueueSize(lane)).toBe(0);
} finally {
blocker.resolve();
commandQueueA.resetAllLanes();
}
});
});