|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import test from 'node:test'; |
| 5 | +import { makeBundle } from '../src/lang/bundle.js'; |
| 6 | +import { pruneGeneration } from '../src/store/retention.js'; |
| 7 | +import { agent, setup } from './helpers.js'; |
| 8 | + |
| 9 | +const readReceipt = 'SELECT receipt FROM retention_batch_receipt WHERE slot=1'; |
| 10 | +const io = () => |
| 11 | + Object.assign(new Error('fixture lost maintenance commit acknowledgement'), { |
| 12 | + code: 'ERR_SQLITE_ERROR', |
| 13 | + errcode: 10, |
| 14 | + }); |
| 15 | + |
| 16 | +for (const kind of ['runs', 'files'] as const) |
| 17 | + for (const mode of ['committed', 'rollback', 'unreadable', 'different receipt'] as const) |
| 18 | + test(`prune ${kind} acknowledges only its own committed batch: ${mode}`, async (t) => { |
| 19 | + const s = await setup(); |
| 20 | + const db = s.store.db; |
| 21 | + const exec = db.exec.bind(db); |
| 22 | + const prepare = db.prepare.bind(db); |
| 23 | + let deletes = 0, |
| 24 | + lost = false, |
| 25 | + summaries = 0; |
| 26 | + try { |
| 27 | + s.store.publish('owner', makeBundle(agent())); |
| 28 | + const older = Date.now() - 120_000; |
| 29 | + const ids: string[] = []; |
| 30 | + for (const n of [1, 2]) { |
| 31 | + if (kind === 'runs') { |
| 32 | + const reply = await s.request('POST', '/v1/responses', { |
| 33 | + model: 'agent/test', |
| 34 | + input: 'fixture ' + n, |
| 35 | + }); |
| 36 | + assert.equal(reply.statusCode, 200, reply.body); |
| 37 | + const id = String(reply.json().id); |
| 38 | + db.prepare("UPDATE runs SET payload=json_set(payload,'$.updatedAt',?) WHERE id=?").run( |
| 39 | + older, |
| 40 | + id, |
| 41 | + ); |
| 42 | + ids.push(id); |
| 43 | + } else { |
| 44 | + const file = await s.store.addFile('owner', `fixture-${n}.txt`, Buffer.from('fixture')); |
| 45 | + db.prepare("UPDATE files SET payload=json_set(payload,'$.createdAt',?) WHERE id=?").run( |
| 46 | + older, |
| 47 | + file.id, |
| 48 | + ); |
| 49 | + ids.push(file.id); |
| 50 | + } |
| 51 | + } |
| 52 | + db.prepare('INSERT INTO retention_batch_receipt(slot,receipt) VALUES(1,?)').run( |
| 53 | + 'previous fixture receipt', |
| 54 | + ); |
| 55 | + const exists = db.prepare(`SELECT 1 FROM ${kind} WHERE id=?`); |
| 56 | + const events = db.prepare('SELECT count(*) AS n FROM events WHERE run_id=?'); |
| 57 | + const expectedEvents = ids.reduce((count, id) => count + Number(events.get(id)!.n), 0); |
| 58 | + const overview = await s.request('GET', '/v1/overview'); |
| 59 | + const generation = pruneGeneration.value; |
| 60 | + s.store.events.on('summary', () => summaries++); |
| 61 | + db.function('arm_prune_ack', () => ++deletes); |
| 62 | + db.exec(`CREATE TEMP TRIGGER arm_prune_ack AFTER DELETE ON main.${kind} |
| 63 | + BEGIN SELECT arm_prune_ack(); END`); |
| 64 | + t.mock.method(db, 'exec', (sql: string) => { |
| 65 | + if (!deletes || lost || sql !== 'COMMIT') return exec(sql); |
| 66 | + lost = true; |
| 67 | + if (mode !== 'rollback') exec(sql); |
| 68 | + if (mode === 'different receipt') |
| 69 | + prepare('UPDATE retention_batch_receipt SET receipt=? WHERE slot=1').run( |
| 70 | + 'previous fixture receipt', |
| 71 | + ); |
| 72 | + throw io(); |
| 73 | + }); |
| 74 | + t.mock.method(db, 'prepare', (sql: string) => { |
| 75 | + if (lost && mode === 'unreadable' && sql === readReceipt) throw io(); |
| 76 | + return prepare(sql); |
| 77 | + }); |
| 78 | + const prune = () => s.request('POST', '/v1/maintenance/prune', { older_than_ms: 60_000 }); |
| 79 | + const result = await prune(); |
| 80 | + assert.equal(lost, true); |
| 81 | + assert.equal(deletes, ids.length, 'the DELETE was not re-executed'); |
| 82 | + assert.equal(db.isTransaction, false); |
| 83 | + if (mode === 'committed') { |
| 84 | + assert.equal(result.statusCode, 200, result.body); |
| 85 | + const report = result.json(); |
| 86 | + assert.equal(report[kind], ids.length); |
| 87 | + assert.equal(report.events, expectedEvents); |
| 88 | + assert.equal(report.file_bytes, kind === 'files' ? 14 : 0); |
| 89 | + const retry = await prune(); |
| 90 | + assert.equal(retry.statusCode, 200, retry.body); |
| 91 | + assert.equal(retry.json()[kind], 0); |
| 92 | + assert.equal(deletes, ids.length); |
| 93 | + } else if (mode === 'rollback') { |
| 94 | + assert.equal(result.statusCode, 500); |
| 95 | + assert.equal(prepare(readReceipt).get()?.receipt, 'previous fixture receipt'); |
| 96 | + } else { |
| 97 | + assert.equal(result.statusCode, 409, result.body); |
| 98 | + assert.equal(result.json().error.code, 'effect_outcome_unknown'); |
| 99 | + assert.equal(result.headers['x-should-retry'], 'false'); |
| 100 | + } |
| 101 | + const published = kind === 'runs' && mode === 'committed'; |
| 102 | + assert.equal(summaries, published ? 1 : 0); |
| 103 | + assert.equal(pruneGeneration.value, generation + (published ? 1 : 0)); |
| 104 | + const checked = await s.request('GET', '/v1/overview', undefined, undefined, { |
| 105 | + 'if-none-match': String(overview.headers.etag), |
| 106 | + }); |
| 107 | + assert.equal(checked.statusCode, published ? 200 : 304); |
| 108 | + for (const id of ids) { |
| 109 | + assert.equal(Boolean(exists.get(id)), mode === 'rollback'); |
| 110 | + if (kind === 'files') { |
| 111 | + const blob = readFile(path.join(s.store.root, 'files', id), 'utf8'); |
| 112 | + if (mode === 'committed') await assert.rejects(blob, { code: 'ENOENT' }); |
| 113 | + else assert.equal(await blob, 'fixture'); |
| 114 | + } |
| 115 | + } |
| 116 | + } finally { |
| 117 | + await s.cleanup(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | +test('a partial file delete rolls back instead of unlinking a still-listed file', async () => { |
| 122 | + const s = await setup(); |
| 123 | + try { |
| 124 | + const ignored = await s.store.addFile('owner', 'ignored.txt', Buffer.from('keep')), |
| 125 | + other = await s.store.addFile('owner', 'other.txt', Buffer.from('also keep')); |
| 126 | + const db = s.store.db; |
| 127 | + db.prepare("UPDATE files SET payload=json_set(payload,'$.createdAt',?)").run( |
| 128 | + Date.now() - 120_000, |
| 129 | + ); |
| 130 | + db.exec(`CREATE TEMP TRIGGER ignore_fixture_file BEFORE DELETE ON files |
| 131 | + WHEN json_extract(OLD.payload,'$.name')='ignored.txt' BEGIN SELECT RAISE(IGNORE); END`); |
| 132 | + const result = await s.request('POST', '/v1/maintenance/prune', { older_than_ms: 60_000 }); |
| 133 | + assert.equal(result.statusCode, 500); |
| 134 | + assert.equal(result.json().error.code, 'persistence_failed'); |
| 135 | + assert.equal(db.prepare(readReceipt).get(), undefined); |
| 136 | + for (const [file, text] of [ |
| 137 | + [ignored, 'keep'], |
| 138 | + [other, 'also keep'], |
| 139 | + ] as const) { |
| 140 | + assert.ok(db.prepare('SELECT 1 FROM files WHERE id=?').get(file.id)); |
| 141 | + assert.equal(await readFile(path.join(s.store.root, 'files', file.id), 'utf8'), text); |
| 142 | + } |
| 143 | + } finally { |
| 144 | + await s.cleanup(); |
| 145 | + } |
| 146 | +}); |
0 commit comments