From f4261bd83c08c263cf2b3c7463d57412dd6fc3ab Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Wed, 18 Nov 2026 00:55:35 -0500 Subject: [PATCH] fix(eval-harness): preserve fixtures when recording fails --- scripts/lib/eval-harness/replay.js | 3 ++- tests/lib/eval-harness/replay.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/scripts/lib/eval-harness/replay.js b/scripts/lib/eval-harness/replay.js index 6a8d1faf8..b669969a1 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 { 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; } 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 {