{{ message }}
Repository navigation
Expand file tree
/
Copy pathagent-session-recovery-capsule.test.ts
More file actions
633 lines (541 loc) · 23.8 KB
/
Copy pathagent-session-recovery-capsule.test.ts
File metadata and controls
633 lines (541 loc) · 23.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
import { mkdtemp, readFile, readdir, rm, utimes, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { z } from 'zod'
import * as durable from '../durable-file-write'
import {
marker,
NOW,
SESSION
} from '../native-chat/agent-session-wire/structured-agent-session-restart-resume-test-harness'
import {
AgentSessionRecoveryCapsule,
AGENT_SESSION_RECOVERY_CAPSULE_FILE
} from './agent-session-recovery-capsule'
import type { AgentSessionResumeFailureInput } from './agent-session-recovery-capsule-entries'
let directory: string
let filePath: string
let capsule: AgentSessionRecoveryCapsule
beforeEach(async () => {
directory = await mkdtemp(join(tmpdir(), 'orca-recovery-capsule-'))
filePath = join(directory, AGENT_SESSION_RECOVERY_CAPSULE_FILE)
capsule = new AgentSessionRecoveryCapsule(directory)
})
afterEach(async () => {
vi.restoreAllMocks()
await rm(directory, { recursive: true, force: true })
})
function failure(
overrides: Partial<AgentSessionResumeFailureInput> = {}
): AgentSessionResumeFailureInput {
return {
sessionId: SESSION,
failedAt: NOW,
outcome: 'refused',
reason: 'agent_session_restart_work_superseded',
latestPrompt: '',
latestUserItemId: 'user-after-failure',
...overrides
}
}
async function fileFailure(overrides: Partial<AgentSessionResumeFailureInput> = {}) {
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.failResume('operation-a', [failure(overrides)], NOW)
}
function failPublish() {
return vi.spyOn(durable, 'renameDurable').mockRejectedValueOnce(new Error('publish unavailable'))
}
describe('durable restart offers', () => {
it('lists without spending or rewriting the durable records', async () => {
const markers = [marker(), marker({ sessionId: 'second' })]
await capsule.record(markers, NOW)
const before = await readFile(filePath)
expect(await capsule.list(NOW)).toEqual(markers)
expect(await capsule.list(NOW)).toEqual(markers)
expect(await readFile(filePath)).toEqual(before)
})
it('merges fresh teardown markers with existing offers by session', async () => {
const replacement = marker({ teardownId: 'teardown-new', recordedAt: NOW + 1 })
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
await capsule.record([replacement], NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([replacement, marker({ sessionId: 'second' })])
})
it('does not let a late older teardown replace a newer offer', async () => {
const older = marker({ recordedAt: NOW })
const newer = marker({ recordedAt: NOW + 1, teardownId: 'teardown-new' })
await capsule.record([newer], NOW + 1)
await capsule.record([older], NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([newer])
})
it('reserves only one copy of a selected session across competing owners', async () => {
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
const other = new AgentSessionRecoveryCapsule(directory)
const results = await Promise.allSettled([
capsule.beginResume([SESSION], 'operation-a', NOW),
other.beginResume([SESSION], 'operation-b', NOW)
])
const selected = results.flatMap((result) =>
result.status === 'fulfilled' ? result.value : []
)
// File locking serializes admission; the second action sees the first reservation.
expect(selected).toHaveLength(1)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'second' })])
})
it('deletes only completed sessions and leaves unrelated offers', async () => {
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
const reserved = await capsule.beginResume([SESSION], 'operation-a', NOW)
expect(reserved).toEqual([marker()])
await capsule.completeResume('operation-a', [SESSION], NOW)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'second' })])
})
it('files an acted-on session whose agent did not carry on as a durable failure', async () => {
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.failResume(
'operation-a',
[
failure({
failedAt: NOW + 5,
reason: 'agent_session_restart_work_superseded',
latestPrompt: 'fix the auth bug'
})
],
NOW + 5
)
expect(await capsule.list(NOW + 5)).toEqual([marker({ sessionId: 'second' })])
expect(await capsule.listFailed(NOW + 5)).toEqual([
{
marker: marker(),
failedAt: NOW + 5,
outcome: 'refused',
reason: 'agent_session_restart_work_superseded',
latestPrompt: 'fix the auth bug',
latestUserItemId: 'user-after-failure'
}
])
await capsule.rollbackResume('operation-a', NOW + 5)
expect(await capsule.list(NOW + 5)).toEqual([marker({ sessionId: 'second' })])
expect(await capsule.listFailed(NOW + 5)).toHaveLength(1)
})
it('only files failures for the operation that owns the reservation', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.failResume('operation-b', [failure()], NOW)
expect(await capsule.listFailed(NOW)).toEqual([])
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('retries a failure only when the action names it, and a success removes it', async () => {
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
await fileFailure()
expect(await capsule.beginResume(undefined, 'operation-b', NOW)).toEqual([
marker({ sessionId: 'second' })
])
await capsule.rollbackResume('operation-b', NOW)
// Explicit selection retries failures without removing their guidance until settlement.
expect(await capsule.beginResume([SESSION], 'operation-c', NOW)).toEqual([marker()])
expect(await capsule.listFailed(NOW)).toHaveLength(1)
await capsule.completeResume('operation-c', [SESSION], NOW)
expect(await capsule.listFailed(NOW)).toEqual([])
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'second' })])
})
it('keeps a retried failure a failure when the retry rolls back or its lease lapses', async () => {
await capsule.record([marker()], NOW)
await fileFailure()
await capsule.beginResume([SESSION], 'operation-b', NOW)
await capsule.rollbackResume('operation-b', NOW)
expect(await capsule.list(NOW)).toEqual([])
expect(await capsule.listFailed(NOW)).toHaveLength(1)
expect(await capsule.beginResume(undefined, 'operation-c', NOW)).toEqual([])
await capsule.beginResume([SESSION], 'operation-d', NOW)
const later = NOW + 10 * 60 * 1000 + 1
expect(await capsule.list(later)).toEqual([])
expect(await capsule.beginResume(undefined, 'operation-e', later)).toEqual([])
expect(await capsule.listFailed(later)).toHaveLength(1)
})
it('refiles a failed retry with its new reason', async () => {
await capsule.record([marker()], NOW)
await fileFailure()
await capsule.beginResume([SESSION], 'operation-b', NOW + 1)
await capsule.failResume(
'operation-b',
[failure({ failedAt: NOW + 1, reason: 'agent_session_conflict' })],
NOW + 1
)
expect(await capsule.listFailed(NOW + 1)).toMatchObject([
{ failedAt: NOW + 1, reason: 'agent_session_conflict' }
])
})
it('lets a newer teardown of the same chat supersede its recorded failure', async () => {
await capsule.record([marker()], NOW)
await fileFailure({ outcome: 'unconfirmed', reason: 'pending' })
await capsule.record([marker()], NOW)
expect(await capsule.list(NOW)).toEqual([])
expect(await capsule.listFailed(NOW)).toHaveLength(1)
const newer = marker({ recordedAt: NOW + 1, teardownId: 'teardown-new' })
await capsule.record([newer], NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([newer])
expect(await capsule.listFailed(NOW + 1)).toEqual([])
})
it('forgets a superseded failure only while it is the one that was read', async () => {
await capsule.record([marker()], NOW)
await fileFailure()
await capsule.forgetSuperseded(
[{ sessionId: SESSION, recordedAt: NOW, failedAt: NOW - 1 }],
NOW
)
expect(await capsule.listFailed(NOW)).toHaveLength(1)
await capsule.forgetSuperseded([{ sessionId: SESSION, recordedAt: NOW, failedAt: NOW }], NOW)
expect(await capsule.listFailed(NOW)).toEqual([])
})
// New user messages invalidate only the observed witness; preserve later teardown records.
it('forgets a superseded pending offer only for the witness that was read', async () => {
await capsule.record([marker()], NOW)
await capsule.forgetSuperseded([{ sessionId: SESSION, recordedAt: NOW - 1 }], NOW)
expect(await capsule.list(NOW)).toHaveLength(1)
await capsule.forgetSuperseded([{ sessionId: SESSION, recordedAt: NOW }], NOW)
expect(await capsule.list(NOW)).toEqual([])
})
it('does not forget an in-progress reservation as superseded', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.forgetSuperseded([{ sessionId: SESSION, recordedAt: NOW }], NOW)
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('forgets named records of any state and reports how many went', async () => {
await capsule.record(
[marker(), marker({ sessionId: 'second' }), marker({ sessionId: 'third' })],
NOW
)
await fileFailure()
expect(await capsule.dismiss([SESSION, 'second', 'missing'], NOW)).toBe(2)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'third' })])
expect(await capsule.listFailed(NOW)).toEqual([])
const publish = vi.spyOn(durable, 'renameDurable')
expect(await capsule.dismiss(['missing'], NOW)).toBe(0)
expect(publish).not.toHaveBeenCalled()
await capsule.record([marker()], NOW)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'third' }), marker()])
})
it('forgets every record of any state except the ones kept, without a fence', async () => {
await capsule.record(
[marker(), marker({ sessionId: 'second' }), marker({ sessionId: 'kept' })],
NOW
)
await fileFailure()
expect(await capsule.listFailed(NOW)).toHaveLength(1)
await capsule.beginResume(['second'], 'operation-b', NOW)
const keep = (stored: { sessionId: string }) => stored.sessionId === 'kept'
expect(await capsule.dismiss('all', NOW, keep)).toBe(2)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'kept' })])
expect(await capsule.listFailed(NOW)).toEqual([])
await capsule.record([marker()], NOW)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'kept' }), marker()])
})
it('keeps a months-old failure on record', async () => {
await capsule.record([marker()], NOW)
await fileFailure()
expect(await capsule.listFailed(NOW + 90 * 24 * 60 * 60 * 1000)).toHaveLength(1)
})
// Older parsers reject new entry states but ignore unknown top-level keys; store failures separately.
it('writes failures in a file an older build still reads its offers from', async () => {
const olderEntry = z.object({
state: z.enum(['pending', 'in-progress']),
operationId: z.string().min(1).optional(),
startedAt: z.number().int().nonnegative().optional(),
marker: z.unknown(),
replacement: z.unknown().optional()
})
const olderCapsule = z.object({
version: z.literal(2),
entries: z.array(z.unknown()),
dismissedAt: z.number().int().nonnegative().optional()
})
const olderStates = async () =>
olderCapsule
.parse(JSON.parse(await readFile(filePath, 'utf8')))
.entries.map((entry) => olderEntry.parse(entry).state)
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
await fileFailure()
expect(await olderStates()).toEqual(['pending'])
await capsule.beginResume([SESSION], 'operation-retry', NOW)
expect((await olderStates()).sort()).toEqual(['in-progress', 'pending'])
expect(JSON.parse(await readFile(filePath, 'utf8'))).toMatchObject({
failed: [{ marker: { sessionId: SESSION } }]
})
})
// Downgrade must skip unknown failures without losing offers or blocking newer teardowns.
it('skips a failure record it cannot read instead of rejecting the whole file', async () => {
const readable = {
marker: marker(),
failedAt: NOW,
outcome: 'refused',
reason: 'agent_session_restart_work_superseded',
latestPrompt: '',
latestUserItemId: null
}
await writeFile(
filePath,
JSON.stringify({
version: 2,
entries: [{ state: 'pending', marker: marker({ sessionId: 'second' }) }],
failed: [
{ ...readable, marker: marker({ sessionId: 'future' }), outcome: 'later-outcome' },
{ ...readable, marker: { ...marker({ sessionId: 'future-marker' }), trigger: 'later' } },
readable
]
})
)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'second' })])
expect(await capsule.listFailed(NOW)).toEqual([readable])
await capsule.record([marker({ sessionId: 'third' })], NOW)
expect(await capsule.list(NOW)).toEqual([
marker({ sessionId: 'second' }),
marker({ sessionId: 'third' })
])
expect(await capsule.listFailed(NOW)).toEqual([readable])
expect(JSON.parse(await readFile(filePath, 'utf8')).failed).toEqual([readable])
})
it('keeps refusal details beside the code, read back against that code', async () => {
const readable = {
marker: marker(),
failedAt: NOW,
outcome: 'refused',
reason: 'agent_session_conflict',
latestPrompt: '',
latestUserItemId: null
}
await writeFile(
filePath,
JSON.stringify({
version: 2,
entries: [],
failed: [
{ ...readable, details: { reason: 'claimConflicted', note: 'dropped' } },
// Retired cause fields supply no current details.
{ ...readable, marker: marker({ sessionId: 'older' }), cause: 'claimConflicted' },
{
...readable,
marker: marker({ sessionId: 'foreign' }),
details: { reason: 'promptGone' }
}
]
})
)
expect(await capsule.listFailed(NOW)).toEqual([
{ ...readable, details: { reason: 'claimConflicted' } },
{ ...readable, marker: marker({ sessionId: 'older' }) },
{ ...readable, marker: marker({ sessionId: 'foreign' }) }
])
})
it('reads a malformed failure list as no failures', async () => {
await writeFile(
filePath,
JSON.stringify({
version: 2,
entries: [{ state: 'pending', marker: marker() }],
failed: { unexpected: true }
})
)
expect(await capsule.list(NOW)).toEqual([marker()])
expect(await capsule.listFailed(NOW)).toEqual([])
await capsule.record([marker({ sessionId: 'second' })], NOW)
expect(await capsule.list(NOW)).toEqual([marker(), marker({ sessionId: 'second' })])
})
it('rolls a failed acquisition back to a pending offer', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([])
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('does not let another operation complete or roll back an active reservation', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.completeResume('operation-b', [SESSION], NOW)
await capsule.rollbackResume('operation-b', NOW)
expect(await capsule.list(NOW)).toEqual([])
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('preserves an active reservation when teardown records another session', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.record([marker({ sessionId: 'second' })], NOW)
expect(await capsule.list(NOW)).toEqual([marker({ sessionId: 'second' })])
const raw = JSON.parse(await readFile(filePath, 'utf8'))
expect(raw.entries).toEqual(
expect.arrayContaining([
expect.objectContaining({
state: 'in-progress',
operationId: 'operation-a',
marker: expect.objectContaining({ sessionId: SESSION })
}),
expect.objectContaining({
state: 'pending',
marker: expect.objectContaining({ sessionId: 'second' })
})
])
)
})
it('keeps a newer same-session teardown behind an active reservation', async () => {
const newer = marker({
teardownId: 'teardown-new',
recordedAt: NOW + 1,
work: { kind: 'turn', id: 'turn-new' }
})
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
await capsule.record([newer], NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([])
await capsule.rollbackResume('operation-a', NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([newer])
await capsule.beginResume([SESSION], 'operation-b', NOW)
await capsule.completeResume('operation-b', [SESSION], NOW)
expect(await capsule.list(NOW)).toEqual([])
})
it('reclaims an expired in-progress reservation on the next mutation', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
const later = NOW + 10 * 60 * 1000 + 1
// Read without writing; expired action leases appear pending.
expect(await capsule.list(later)).toEqual([marker()])
const reserved = await capsule.beginResume([SESSION], 'operation-b', later)
expect(reserved).toEqual([marker()])
await capsule.completeResume('operation-b', [SESSION], later)
expect(await capsule.list(later)).toEqual([])
})
it('dismisses pending and active recovery records', async () => {
await capsule.record([marker(), marker({ sessionId: 'second' })], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
expect(await capsule.clearAll(NOW)).toBe(1)
expect(await capsule.list(NOW)).toEqual([])
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([])
})
it('fences a late teardown write until a genuinely newer interruption', async () => {
await capsule.record([marker()], NOW)
await capsule.clearAll(NOW + 1)
// Model a teardown callback outliving another host's timed phase.
await capsule.record([marker()], NOW + 1)
expect(await capsule.list(NOW + 1)).toEqual([])
const newer = marker({ recordedAt: NOW + 2, teardownId: 'teardown-new' })
await capsule.record([newer], NOW + 2)
expect(await capsule.list(NOW + 2)).toEqual([newer])
// New interruptions must preserve the fence against older delayed callbacks.
await capsule.record([marker()], NOW + 2)
expect(await capsule.list(NOW + 2)).toEqual([newer])
})
it('keeps a legacy v1 file readable until a mutating operation migrates it', async () => {
const legacy = JSON.stringify({ version: 1, markers: [marker()] })
await writeFile(filePath, legacy)
expect(await capsule.list(NOW)).toEqual([marker()])
expect(await readFile(filePath, 'utf8')).toBe(legacy)
await capsule.record([marker({ sessionId: 'second' })], NOW)
expect(JSON.parse(await readFile(filePath, 'utf8'))).toMatchObject({ version: 2 })
expect(await capsule.list(NOW)).toEqual([marker(), marker({ sessionId: 'second' })])
})
it('preserves unreadable bytes on list and ordinary writes', async () => {
const corrupt = '{"version":2,"entries":[{"state":"pending"}]}'
await writeFile(filePath, corrupt)
await expect(capsule.list(NOW)).rejects.toThrow()
await expect(capsule.record([marker()], NOW)).rejects.toThrow()
expect(await readFile(filePath, 'utf8')).toBe(corrupt)
})
it('fails closed on duplicate session records instead of allowing a second resume', async () => {
const duplicate = JSON.stringify({
version: 2,
entries: [
{ state: 'in-progress', operationId: 'operation-a', startedAt: NOW, marker: marker() },
{ state: 'pending', marker: marker({ teardownId: 'duplicate' }) }
]
})
await writeFile(filePath, duplicate)
await expect(capsule.list(NOW)).rejects.toThrow('duplicate_session')
expect(await readFile(filePath, 'utf8')).toBe(duplicate)
})
it('lets explicit dismissal replace an unreadable file with an empty fence', async () => {
await writeFile(filePath, '{')
expect(await capsule.clearAll(NOW)).toBe(0)
await expect(capsule.list(NOW)).resolves.toEqual([])
expect(JSON.parse(await readFile(filePath, 'utf8'))).toMatchObject({
version: 2,
entries: [],
dismissedAt: NOW
})
})
it('preserves prior records when a durable publication fails', async () => {
await capsule.record([marker()], NOW)
const publish = failPublish()
await expect(capsule.record([marker({ sessionId: 'second' })], NOW)).rejects.toThrow(
'publish unavailable'
)
publish.mockRestore()
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('preserves a pending offer when reservation publication fails', async () => {
await capsule.record([marker()], NOW)
const publish = failPublish()
await expect(capsule.beginResume([SESSION], 'operation-a', NOW)).rejects.toThrow(
'publish unavailable'
)
publish.mockRestore()
expect(await capsule.list(NOW)).toEqual([marker()])
})
it('preserves an active reservation when completion publication fails', async () => {
await capsule.record([marker()], NOW)
await capsule.beginResume([SESSION], 'operation-a', NOW)
const publish = failPublish()
await expect(capsule.completeResume('operation-a', [SESSION], NOW)).rejects.toThrow(
'publish unavailable'
)
publish.mockRestore()
expect(await capsule.list(NOW)).toEqual([])
await capsule.rollbackResume('operation-a', NOW)
expect(await capsule.list(NOW)).toEqual([marker()])
})
// Offers expire only by user action, never TTL or clock drift.
it.each([NOW - 90 * 24 * 60 * 60 * 1000, NOW + 1])(
'still lists a months-old or clock-skewed marker (%s)',
async (recordedAt) => {
await capsule.record([marker({ recordedAt })], recordedAt)
expect(await capsule.list(NOW)).toHaveLength(1)
}
)
it('reclaims stale durable-write debris without touching unrelated files', async () => {
const abandoned = `${filePath}.0.1.abandoned.tmp`
const recent = `${filePath}.0.2.recent.tmp`
const unrelated = join(directory, 'conversation.tmp')
await Promise.all([abandoned, recent, unrelated].map((path) => writeFile(path, 'debris')))
const old = new Date(Date.now() - 25 * 60 * 60 * 1000)
await utimes(abandoned, old, old)
await capsule.record([marker()], NOW)
expect(await readdir(directory)).toEqual(
expect.arrayContaining([
AGENT_SESSION_RECOVERY_CAPSULE_FILE,
`${AGENT_SESSION_RECOVERY_CAPSULE_FILE}.0.2.recent.tmp`,
'conversation.tmp'
])
)
expect(await readdir(directory)).not.toContain(
`${AGENT_SESSION_RECOVERY_CAPSULE_FILE}.0.1.abandoned.tmp`
)
})
})
it('does not retain lock timers after reads, writes, or failed publications', async () => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'] })
try {
expect(vi.getTimerCount()).toBe(0)
await capsule.record([marker()], NOW)
expect(vi.getTimerCount()).toBe(0)
await capsule.list(NOW)
expect(vi.getTimerCount()).toBe(0)
failPublish()
await expect(capsule.record([marker()], NOW)).rejects.toThrow()
expect(vi.getTimerCount()).toBe(0)
} finally {
vi.useRealTimers()
}
})
You can’t perform that action at this time.
