|
| 1 | +/** |
| 2 | + * Regression tests for #3366: ACP local storage persist() failure cascade. |
| 3 | + * |
| 4 | + * Bug: FileAcpLocalStorageProfile.persist() chains writes onto writeChain |
| 5 | + * without error recovery. A single failed write poisons all subsequent writes |
| 6 | + * because every .then() chains onto the rejected promise. |
| 7 | + * |
| 8 | + * Fix: persist() now catches errors, resets the chain, and logs the failure. |
| 9 | + * Subsequent writes get a fresh attempt. |
| 10 | + */ |
| 11 | +import { readFile } from 'node:fs/promises'; |
| 12 | +import fs from 'node:fs'; |
| 13 | +import path from 'node:path'; |
| 14 | +import os from 'node:os'; |
| 15 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 16 | + |
| 17 | +import { createFileAcpLocalStorageProfile } from '../services/acp/local-storage.js'; |
| 18 | + |
| 19 | +function makeSession(id: string) { |
| 20 | + const now = Date.now(); |
| 21 | + return { |
| 22 | + id, |
| 23 | + conversationId: `conv-${id}`, |
| 24 | + transcriptId: `transcript-${id}`, |
| 25 | + tenantId: 'test-tenant', |
| 26 | + ownerKeyId: 'test-key', |
| 27 | + runnerType: 'acp' as const, |
| 28 | + status: 'initializing' as const, |
| 29 | + createdAt: now, |
| 30 | + updatedAt: now, |
| 31 | + metadata: {}, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('Issue #3366: ACP local storage persist() failure cascade', () => { |
| 36 | + let tmpDir: string; |
| 37 | + let filePath: string; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'aegis-3366-')); |
| 41 | + filePath = path.join(tmpDir, 'acp-local-storage.json'); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + // Restore permissions so cleanup can succeed |
| 46 | + await fs.promises.chmod(tmpDir, 0o755).catch(() => {}); |
| 47 | + await fs.promises.rm(tmpDir, { recursive: true, force: true }).catch(() => {}); |
| 48 | + }); |
| 49 | + |
| 50 | + it('recovers from a failed persist — subsequent writes succeed', async () => { |
| 51 | + const profile = createFileAcpLocalStorageProfile({ filePath }); |
| 52 | + await profile.start(); |
| 53 | + expect(profile.getPersistError()).toBeNull(); |
| 54 | + |
| 55 | + // Make the directory read-only to force a write failure |
| 56 | + await fs.promises.chmod(tmpDir, 0o444); |
| 57 | + |
| 58 | + // Trigger persist via a mutation — should fail silently |
| 59 | + await profile.sessionStore.create(makeSession('fail-1')); |
| 60 | + expect(profile.getPersistError()).not.toBeNull(); |
| 61 | + |
| 62 | + // Restore write permissions |
| 63 | + await fs.promises.chmod(tmpDir, 0o755); |
| 64 | + |
| 65 | + // Trigger another mutation — this should SUCCEED (not cascade!) |
| 66 | + await profile.sessionStore.create(makeSession('recover-1')); |
| 67 | + expect(profile.getPersistError()).toBeNull(); |
| 68 | + |
| 69 | + // Verify the file actually contains the data |
| 70 | + const content = await readFile(filePath, 'utf8'); |
| 71 | + const parsed = JSON.parse(content); |
| 72 | + expect(parsed.sessions).toHaveLength(2); |
| 73 | + |
| 74 | + await profile.stop(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does NOT cascade rejection across multiple sequential failures', async () => { |
| 78 | + const profile = createFileAcpLocalStorageProfile({ filePath }); |
| 79 | + await profile.start(); |
| 80 | + |
| 81 | + // Make dir read-only |
| 82 | + await fs.promises.chmod(tmpDir, 0o444); |
| 83 | + |
| 84 | + // First failed persist |
| 85 | + await profile.sessionStore.create(makeSession('fail-1')); |
| 86 | + expect(profile.getPersistError()).not.toBeNull(); |
| 87 | + |
| 88 | + // Second failed persist — should NOT be chained to rejected promise |
| 89 | + await profile.sessionStore.create(makeSession('fail-2')); |
| 90 | + expect(profile.getPersistError()).not.toBeNull(); |
| 91 | + |
| 92 | + // Third failed persist — still no cascade |
| 93 | + await profile.sessionStore.create(makeSession('fail-3')); |
| 94 | + expect(profile.getPersistError()).not.toBeNull(); |
| 95 | + |
| 96 | + // Now restore and verify recovery |
| 97 | + await fs.promises.chmod(tmpDir, 0o755); |
| 98 | + await profile.sessionStore.create(makeSession('recover')); |
| 99 | + expect(profile.getPersistError()).toBeNull(); |
| 100 | + |
| 101 | + const content = await readFile(filePath, 'utf8'); |
| 102 | + const parsed = JSON.parse(content); |
| 103 | + expect(parsed.sessions).toHaveLength(4); |
| 104 | + |
| 105 | + await profile.stop(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('stop() does not throw even when writeChain has a rejection', async () => { |
| 109 | + const profile = createFileAcpLocalStorageProfile({ filePath }); |
| 110 | + await profile.start(); |
| 111 | + |
| 112 | + // Make dir read-only so persist fails |
| 113 | + await fs.promises.chmod(tmpDir, 0o444); |
| 114 | + await profile.sessionStore.create(makeSession('doomed')); |
| 115 | + |
| 116 | + // stop() should NOT throw — it catches the chain rejection |
| 117 | + await fs.promises.chmod(tmpDir, 0o755); |
| 118 | + await expect(profile.stop()).resolves.toBeUndefined(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments