diff --git a/scripts/lib/eval-harness/replay.js b/scripts/lib/eval-harness/replay.js index 6a8d1faf8..5cb9d051f 100644 --- a/scripts/lib/eval-harness/replay.js +++ b/scripts/lib/eval-harness/replay.js @@ -18,6 +18,7 @@ const fs = require('fs'); const path = require('path'); +const { readFileWithSharingRetry, writeFileAtomic } = require('../atomic-write'); const { canonicalJson, hashValue } = require('./canonical'); const envelope = require('./envelope'); @@ -57,7 +58,7 @@ class FixtureStore { response_hash: hashValue(response), response, }; - fs.writeFileSync(this.pathFor(key), canonicalJson(record) + '\n', 'utf8'); + writeFileAtomic(this.pathFor(key), canonicalJson(record) + '\n'); return record; } @@ -69,7 +70,7 @@ class FixtureStore { } let record; try { - record = JSON.parse(fs.readFileSync(filePath, 'utf8')); + record = JSON.parse(readFileWithSharingRetry(filePath, 'utf8')); } catch (_error) { throw new ReplayError('tool.fixture_corrupt', `fixture ${key.slice(0, 16)} is not valid JSON`, { tool, key }); } diff --git a/tests/lib/eval-harness/replay.test.js b/tests/lib/eval-harness/replay.test.js index 9095ae79a..85e1861f3 100644 --- a/tests/lib/eval-harness/replay.test.js +++ b/tests/lib/eval-harness/replay.test.js @@ -53,6 +53,31 @@ test('replay mode never calls the implementation and fails closed on a missing f } }); +test('an interrupted recording preserves the prior replayable fixture', () => { + const dir = tempDir('interrupted-record'); + const write = fs.writeFileSync; + try { + const store = new replay.FixtureStore(dir); + const existing = store.put('read_inventory', { sku: 'x' }, { count: 1 }); + const before = fs.readFileSync(store.pathFor(existing.key)); + fs.writeFileSync = (destination, data, options) => { + // Simulate a write that reaches disk only partially before exhausting space. + write(destination, data.slice(0, 10), options); + throw Object.assign(new Error('disk full'), { code: 'ENOSPC' }); + }; + try { + assert.throws(() => store.put('read_inventory', { sku: 'x' }, { count: 2 }), { code: 'ENOSPC' }); + } finally { + fs.writeFileSync = write; + } + assert.deepStrictEqual(fs.readFileSync(store.pathFor(existing.key)), before); + assert.deepStrictEqual(store.get('read_inventory', { sku: 'x' }).response, { count: 1 }); + assert.deepStrictEqual(fs.readdirSync(dir), [existing.key + '.json']); + } finally { + cleanup(dir); + } +}); + test('a hash-mismatched or corrupt fixture fails closed', () => { const dir = tempDir('mismatch'); try {