-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.test.ts
More file actions
645 lines (579 loc) · 21.8 KB
/
Copy pathdispatch.test.ts
File metadata and controls
645 lines (579 loc) · 21.8 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
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type {
ActiveRunEntry,
RunRegistryAdapter,
RunReservation,
ThreadStore,
} from "../adapters/run-registry/types.js";
import type { Db } from "../db/client.js";
import {
triggerRateLimits,
triggerRejectionCounters,
workflowRuns,
} from "../db/schema.js";
import { createTestDb } from "../db/test-db.js";
import type { Adapters } from "./adapters.js";
const testEnv = vi.hoisted(() => ({
JIRA_PROJECT_KEY: "PROJ",
COLUMN_AI: "AI",
TRIGGER_RATE_LIMIT_MAX: undefined as number | undefined,
TRIGGER_RATE_LIMIT_WINDOW: undefined as "minute" | "hour" | "day" | "month" | undefined,
}));
vi.mock("../../env.js", () => ({ env: testEnv }));
const mockStart = vi.fn();
vi.mock("workflow/api", () => ({ start: (...args: any[]) => mockStart(...args) }));
vi.mock("../workflows/agent.js", () => ({ agentWorkflow: "agentWorkflow_sentinel" }));
// A real in-memory Postgres: the no-definition skip now writes a durable run
// row, and its anti-spam guard is a query the fake object could not answer.
const dbRef = vi.hoisted(() => ({ current: null as unknown as Db }));
vi.mock("../db/client.js", () => ({ getDb: () => dbRef.current }));
const mockGetEnabled = vi.fn();
const mockHasBlockingApproval = vi.fn();
vi.mock("../workflow-definition/store.js", () => ({
getEnabledWorkflowDefinitionForTrigger: (...args: any[]) => mockGetEnabled(...args),
}));
vi.mock("../approvals/store.js", () => ({
hasDispatchBlockingApprovalForTicket: (...args: any[]) =>
mockHasBlockingApproval(...args),
}));
const { dispatchTicket, STALE_CLAIM_MS, capacityConsumerCount } = await import(
"./dispatch.js"
);
const { NO_DEFINITION_BLOCKED_REASON } = await import("./run-start-lifecycle.js");
function entry(overrides: Partial<ActiveRunEntry> = {}): ActiveRunEntry {
return {
subjectKey: "ticket:jira:OTHER-1",
ticketKey: "OTHER-1",
ownerToken: "owner:other",
runId: "run-other",
state: "bound",
kind: "ticket",
createdAt: Date.now(),
updatedAt: Date.now(),
...overrides,
};
}
function registry(options: {
reserveResult?: boolean;
initial?: ActiveRunEntry[];
listError?: Error;
failed?: boolean;
failedError?: Error;
capacityEntries?: ActiveRunEntry[];
} = {}): RunRegistryAdapter & ThreadStore {
const rows = [...(options.initial ?? [])];
return {
reserve: vi.fn(async (reservation: RunReservation) => {
if (options.reserveResult === false || rows.some((row) => row.subjectKey === reservation.subjectKey)) {
return false;
}
const now = Date.now();
rows.push({ ...reservation, runId: null, state: "reserved", createdAt: now, updatedAt: now });
return true;
}),
commitStartedRun: vi.fn(async () => true),
markRunEntryStarted: vi.fn(async () => true),
bindRun: vi.fn(),
beginParking: vi.fn(),
finishParking: vi.fn(),
handoff: vi.fn(),
get: vi.fn(async (subjectKey) => rows.find((row) => row.subjectKey === subjectKey) ?? null),
beginCancellation: vi.fn(),
releaseCancellation: vi.fn(),
releaseReservation: vi.fn(async (subjectKey, ownerToken) => {
const index = rows.findIndex(
(row) => row.subjectKey === subjectKey && row.ownerToken === ownerToken && row.state === "reserved",
);
if (index < 0) return false;
rows.splice(index, 1);
return true;
}),
release: vi.fn(),
listAll: vi.fn(async () => {
if (options.listError) throw options.listError;
return [...rows];
}),
...(options.capacityEntries
? { listCapacityConsumers: vi.fn(async () => [...options.capacityEntries!]) }
: {}),
registerSandbox: vi.fn(),
listSandboxes: vi.fn(),
markFailed: vi.fn(),
isTicketFailed: vi.fn(async () => {
if (options.failedError) throw options.failedError;
return options.failed ?? false;
}),
listAllFailed: vi.fn(),
clearFailedMark: vi.fn(),
getParent: vi.fn(),
setParent: vi.fn(),
clearParent: vi.fn(),
};
}
function ticket(overrides: Record<string, unknown> = {}) {
return {
id: "ticket-id",
identifier: "PROJ-42",
projectKey: "PROJ",
title: "Implement it",
description: "",
acceptanceCriteria: "",
comments: [],
labels: [],
trackerStatus: "AI",
attachments: [],
...overrides,
};
}
function adapters(runRegistry = registry(), ticketValue = ticket()): Adapters {
return {
runRegistry,
issueTracker: {
fetchTicket: vi.fn().mockResolvedValue(ticketValue),
moveTicket: vi.fn(),
postComment: vi.fn(),
searchTickets: vi.fn(),
},
messaging: {} as never,
vcs: {} as never,
};
}
describe("dispatchTicket owner reservation", () => {
beforeAll(async () => {
dbRef.current = await createTestDb();
});
beforeEach(async () => {
await dbRef.current.delete(workflowRuns);
mockStart.mockReset();
mockGetEnabled.mockReset();
mockHasBlockingApproval.mockReset().mockResolvedValue(false);
mockStart.mockResolvedValue({ runId: "run-started" });
mockGetEnabled.mockResolvedValue({
definition: { id: 7 },
current: { definitionId: 7, version: 4 },
});
});
it("does not replace a pending or approved-undispatched pinned plan", async () => {
mockHasBlockingApproval.mockResolvedValue(true);
const runRegistry = registry();
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 3)).toEqual({
started: false,
reason: "approval_pending",
});
expect(mockHasBlockingApproval).toHaveBeenCalledWith(expect.anything(), "PROJ-42");
expect(runRegistry.releaseReservation).toHaveBeenCalledOnce();
expect(mockGetEnabled).not.toHaveBeenCalled();
expect(mockStart).not.toHaveBeenCalled();
});
it("reserves the normalized ticket subject and pins the deployed definition for the candidate", async () => {
const runRegistry = registry();
const result = await dispatchTicket("proj-42", adapters(runRegistry), 3);
expect(result).toEqual({ started: true, runId: "run-started" });
expect(runRegistry.reserve).toHaveBeenCalledWith({
subjectKey: "ticket:jira:PROJ-42",
ticketKey: "proj-42",
ownerToken: expect.stringMatching(/^owner:/),
kind: "ticket",
});
expect(mockStart).toHaveBeenCalledWith("agentWorkflow_sentinel", [
expect.objectContaining({
kind: "ticket",
subjectKey: "ticket:jira:PROJ-42",
ticketKey: "proj-42",
ownerToken: expect.stringMatching(/^owner:/),
definitionId: 7,
definitionVersion: 4,
}),
]);
});
it("pins the built-in fallback selection while retaining owner identity", async () => {
mockGetEnabled.mockResolvedValue({
definition: { id: 1 },
current: null,
});
const runRegistry = registry();
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 3)).toEqual({
started: true,
runId: "run-started",
});
expect(mockStart).toHaveBeenCalledWith("agentWorkflow_sentinel", [
expect.objectContaining({
kind: "ticket",
subjectKey: "ticket:jira:PROJ-42",
ownerToken: expect.stringMatching(/^owner:/),
definitionId: 1,
definitionVersion: "builtin_fallback",
}),
]);
});
it("owner-releases the reservation when no deployed definition is available", async () => {
mockGetEnabled.mockResolvedValue(null);
const runRegistry = registry();
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 3)).toEqual({
started: false,
reason: "no_definition",
});
expect(runRegistry.releaseReservation).toHaveBeenCalledOnce();
expect(mockStart).not.toHaveBeenCalled();
});
it("records the skipped ticket as a blocked run so the skip is visible", async () => {
mockGetEnabled.mockResolvedValue(null);
const runRegistry = registry();
await dispatchTicket("PROJ-42", adapters(runRegistry), 3);
const rows = await dbRef.current.select().from(workflowRuns);
expect(rows).toHaveLength(1);
expect(rows[0]).toMatchObject({
status: "blocked",
statusReason: NO_DEFINITION_BLOCKED_REASON,
subjectKey: "ticket:jira:PROJ-42",
ticketKey: "PROJ-42",
ticketTitle: "Implement it",
});
expect(runRegistry.markFailed).not.toHaveBeenCalled();
});
it("records the blocked run once while the ticket keeps being polled", async () => {
mockGetEnabled.mockResolvedValue(null);
await dispatchTicket("PROJ-42", adapters(), 3);
expect(await dispatchTicket("PROJ-42", adapters(), 3)).toEqual({
started: false,
reason: "no_definition",
});
expect(await dbRef.current.select().from(workflowRuns)).toHaveLength(1);
});
it("dispatches the blocked ticket normally once a definition owns the trigger", async () => {
mockGetEnabled.mockResolvedValueOnce(null);
await dispatchTicket("PROJ-42", adapters(), 3);
expect(await dispatchTicket("PROJ-42", adapters(), 3)).toEqual({
started: true,
runId: "run-started",
});
});
it("releases the reservation when the live ticket left the AI column", async () => {
const runRegistry = registry();
const result = await dispatchTicket(
"PROJ-42",
adapters(runRegistry, ticket({ trackerStatus: "Backlog" })),
3,
);
expect(result).toEqual({ started: false, reason: "not_in_ai_column" });
expect(runRegistry.releaseReservation).toHaveBeenCalledOnce();
expect(mockStart).not.toHaveBeenCalled();
});
it("rejects a ticket outside the configured project", async () => {
const result = await dispatchTicket(
"OTHER-42",
adapters(registry(), ticket({ identifier: "OTHER-42", projectKey: "OTHER" })),
3,
);
expect(result).toEqual({ started: false, reason: "wrong_project_key" });
});
it("does not auto-enrol a subject already claimed by a manual dispatch", async () => {
const manualClaim = entry({
subjectKey: "ticket:jira:PROJ-42",
ticketKey: "PROJ-42",
ownerToken: "owner:manual",
runId: "run-manual",
kind: "manual_ticket",
});
const connected = adapters(registry({ initial: [manualClaim] }));
const result = await dispatchTicket("PROJ-42", connected, 3);
expect(result).toEqual({ started: false, reason: "already_claimed" });
expect(connected.issueTracker.fetchTicket).not.toHaveBeenCalled();
expect(mockStart).not.toHaveBeenCalled();
});
it("returns at_capacity without reserving when bound capacity is full", async () => {
const runRegistry = registry({ initial: [entry()] });
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: false,
reason: "at_capacity",
});
expect(runRegistry.reserve).not.toHaveBeenCalled();
});
it("admits work when the exact parked owner is absent from the capacity view", async () => {
const parked = entry({
subjectKey: "ticket:jira:PROJ-PARKED",
ticketKey: "PROJ-PARKED",
ownerToken: "owner-parked",
runId: "run-parked",
});
const runRegistry = registry({ initial: [parked], capacityEntries: [] });
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: true,
runId: "run-started",
});
expect(runRegistry.listCapacityConsumers).toHaveBeenCalled();
});
it("ignores stale unbound reservations in capacity", async () => {
const stale = entry({
state: "reserved",
runId: null,
createdAt: Date.now() - STALE_CLAIM_MS - 1,
updatedAt: Date.now() - STALE_CLAIM_MS - 1,
});
const result = await dispatchTicket("PROJ-42", adapters(registry({ initial: [stale] })), 1);
expect(result.started).toBe(true);
});
it("trusts an adapter capacity view instead of reapplying the process clock", async () => {
const databaseLiveReservation = entry({
state: "reserved",
runId: null,
createdAt: Date.now() - STALE_CLAIM_MS - 1,
updatedAt: Date.now() - STALE_CLAIM_MS - 1,
});
const runRegistry = registry({ capacityEntries: [databaseLiveReservation] });
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: false,
reason: "at_capacity",
});
expect(runRegistry.reserve).not.toHaveBeenCalled();
});
it("counts a freshly handed-off reservation by its refreshed timestamp", async () => {
const handedOff = entry({
state: "reserved",
runId: null,
ownerToken: "owner:clarification-successor",
createdAt: Date.now() - STALE_CLAIM_MS - 1,
updatedAt: Date.now(),
});
const runRegistry = registry({ initial: [handedOff] });
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: false,
reason: "at_capacity",
});
expect(runRegistry.reserve).not.toHaveBeenCalled();
});
it("counts a cancelling claim even when cleanup has been pending past the stale threshold", async () => {
const cancelling = entry({
state: "cancelling",
createdAt: Date.now() - STALE_CLAIM_MS - 1,
updatedAt: Date.now() - STALE_CLAIM_MS - 1,
});
const runRegistry = registry({ initial: [cancelling] });
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: false,
reason: "at_capacity",
});
expect(runRegistry.reserve).not.toHaveBeenCalled();
});
it("does not let a new reservation outrank a claim that starts cancelling during arbitration", async () => {
const runRegistry = registry();
const cancelling = entry({
state: "cancelling",
createdAt: Date.now() - STALE_CLAIM_MS - 1,
updatedAt: Date.now(),
});
vi.mocked(runRegistry.listAll)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
entry({
subjectKey: "ticket:jira:PROJ-42",
ticketKey: "PROJ-42",
ownerToken: "owner:candidate",
runId: null,
state: "reserved",
}),
cancelling,
]);
expect(await dispatchTicket("PROJ-42", adapters(runRegistry), 1)).toEqual({
started: false,
reason: "at_capacity",
});
expect(runRegistry.releaseReservation).toHaveBeenCalledOnce();
expect(mockStart).not.toHaveBeenCalled();
});
it("fails closed when registry capacity cannot be read", async () => {
const result = await dispatchTicket(
"PROJ-42",
adapters(registry({ listError: new Error("registry unavailable") })),
3,
);
expect(result).toEqual({ started: false, reason: "at_capacity" });
expect(mockStart).not.toHaveBeenCalled();
});
it("skips tickets with a durable failed marker", async () => {
const result = await dispatchTicket("PROJ-42", adapters(registry({ failed: true })), 3);
expect(result).toEqual({ started: false, reason: "previously_failed" });
});
it("returns error and owner-releases when the post-reservation ticket read fails", async () => {
const runRegistry = registry();
const value = adapters(runRegistry);
vi.mocked(value.issueTracker.fetchTicket).mockRejectedValue(new Error("jira down"));
expect(await dispatchTicket("PROJ-42", value, 3)).toEqual({
started: false,
reason: "error",
});
expect(runRegistry.releaseReservation).toHaveBeenCalledOnce();
});
it("allows only one of two concurrent dispatches to reserve the subject", async () => {
const runRegistry = registry();
const [first, second] = await Promise.all([
dispatchTicket("PROJ-42", adapters(runRegistry), 3),
dispatchTicket("PROJ-42", adapters(runRegistry), 3),
]);
expect([first.started, second.started].sort()).toEqual([false, true]);
expect(mockStart).toHaveBeenCalledOnce();
});
});
describe("dispatchTicket trigger rate limit", () => {
beforeAll(async () => {
dbRef.current = await createTestDb();
});
beforeEach(async () => {
await dbRef.current.delete(triggerRateLimits);
await dbRef.current.delete(triggerRejectionCounters);
mockStart.mockReset().mockResolvedValue({ runId: "run-started" });
mockGetEnabled.mockReset();
mockHasBlockingApproval.mockReset().mockResolvedValue(false);
testEnv.TRIGGER_RATE_LIMIT_MAX = undefined;
testEnv.TRIGGER_RATE_LIMIT_WINDOW = undefined;
});
function enabledWithTriggerParams(params: Record<string, unknown>) {
return {
definition: { id: 7 },
current: {
definitionId: 7,
version: 4,
definition: {
schemaVersion: 2,
nodes: [
{ id: "ticket-trigger", type: "trigger_ticket_ai", configuration: params },
],
edges: [],
},
},
};
}
it("drops the start once the node limit is spent and tallies the refusal", async () => {
mockGetEnabled.mockResolvedValue(
enabledWithTriggerParams({ rateLimitMax: 1, rateLimitWindow: "day" }),
);
await expect(dispatchTicket("PROJ-42", adapters(), 3)).resolves.toEqual({
started: true,
runId: "run-started",
});
await expect(
dispatchTicket(
"PROJ-43",
adapters(registry(), ticket({ identifier: "PROJ-43" })),
3,
),
).resolves.toEqual({ started: false, reason: "rate_limited" });
expect(mockStart).toHaveBeenCalledOnce();
expect(await dbRef.current.select().from(triggerRateLimits)).toEqual([
expect.objectContaining({
definitionId: "7",
nodeId: "ticket-trigger",
count: 2,
}),
]);
expect(await dbRef.current.select().from(triggerRejectionCounters)).toEqual([
expect.objectContaining({
definitionId: "7",
nodeId: "ticket-trigger",
reason: "rate_limited",
count: 1,
}),
]);
});
it("never spends the limit on a candidate refused by an earlier guard", async () => {
mockGetEnabled.mockResolvedValue(
enabledWithTriggerParams({ rateLimitMax: 1, rateLimitWindow: "day" }),
);
// Duplicate guard: the subject is already claimed, so the candidate must
// not consume the limit nor tally a rejection.
const runRegistry = registry();
await dispatchTicket("PROJ-42", adapters(runRegistry), 3);
await expect(
dispatchTicket("PROJ-42", adapters(runRegistry), 3),
).resolves.toEqual({ started: false, reason: "already_claimed" });
// Same for a guard inside the claim: this ticket left the AI column.
await expect(
dispatchTicket(
"PROJ-43",
adapters(registry(), ticket({ identifier: "PROJ-43", trackerStatus: "Backlog" })),
3,
),
).resolves.toEqual({ started: false, reason: "not_in_ai_column" });
expect(await dbRef.current.select().from(triggerRateLimits)).toEqual([
expect.objectContaining({ definitionId: "7", nodeId: "ticket-trigger", count: 1 }),
]);
expect(await dbRef.current.select().from(triggerRejectionCounters)).toEqual([]);
});
it("writes nothing when no limit is configured", async () => {
mockGetEnabled.mockResolvedValue({
definition: { id: 7 },
current: { definitionId: 7, version: 4 },
});
await expect(dispatchTicket("PROJ-42", adapters(), 3)).resolves.toEqual({
started: true,
runId: "run-started",
});
expect(await dbRef.current.select().from(triggerRateLimits)).toEqual([]);
expect(await dbRef.current.select().from(triggerRejectionCounters)).toEqual([]);
});
it("applies the env default when the node has no params of its own", async () => {
testEnv.TRIGGER_RATE_LIMIT_MAX = 1;
testEnv.TRIGGER_RATE_LIMIT_WINDOW = "day";
mockGetEnabled.mockResolvedValue(enabledWithTriggerParams({}));
await dispatchTicket("PROJ-42", adapters(), 3);
await expect(
dispatchTicket(
"PROJ-43",
adapters(registry(), ticket({ identifier: "PROJ-43" })),
3,
),
).resolves.toEqual({ started: false, reason: "rate_limited" });
// A limit that is purely the env default is keyed under the definition's
// first trigger node.
expect(await dbRef.current.select().from(triggerRejectionCounters)).toEqual([
expect.objectContaining({
definitionId: "7",
nodeId: "ticket-trigger",
reason: "rate_limited",
count: 1,
}),
]);
});
it("prefers the node's own params over the env default", async () => {
testEnv.TRIGGER_RATE_LIMIT_MAX = 5;
testEnv.TRIGGER_RATE_LIMIT_WINDOW = "day";
mockGetEnabled.mockResolvedValue(
enabledWithTriggerParams({ rateLimitMax: 1, rateLimitWindow: "day" }),
);
await dispatchTicket("PROJ-42", adapters(), 3);
await expect(
dispatchTicket(
"PROJ-43",
adapters(registry(), ticket({ identifier: "PROJ-43" })),
3,
),
).resolves.toEqual({ started: false, reason: "rate_limited" });
});
});
describe("capacityConsumerCount", () => {
// The dashboard occupied-slot count must equal what the refusal path counts:
// listCapacityConsumers (parked claims and fresh reservations included).
it("returns listCapacityConsumers().length when the registry exposes it", async () => {
const consumers = [
entry({ subjectKey: "ticket:jira:A-1", state: "bound" }),
entry({ subjectKey: "ticket:jira:A-2", state: "parked" }),
entry({ subjectKey: "ticket:jira:A-3", state: "reserved" }),
];
const runRegistry = registry({ capacityEntries: consumers });
const count = await capacityConsumerCount(runRegistry);
expect(count).toBe(consumers.length);
expect(count).toBe((await runRegistry.listCapacityConsumers!()).length);
});
it("falls back to the live (non-stale) entries of listAll when unavailable", async () => {
const fresh = entry({ subjectKey: "ticket:jira:B-1", state: "bound" });
const staleReservation = entry({
subjectKey: "ticket:jira:B-2",
state: "reserved",
updatedAt: Date.now() - STALE_CLAIM_MS - 1_000,
});
const runRegistry = registry({ initial: [fresh, staleReservation] });
expect(runRegistry.listCapacityConsumers).toBeUndefined();
// The stale reservation is dropped, exactly as the refusal path drops it.
expect(await capacityConsumerCount(runRegistry)).toBe(1);
});
});