From 06aeab9aa1d63f06bfcec9a86b88ce910f022de3 Mon Sep 17 00:00:00 2001 From: Neo Date: Wed, 18 Nov 2026 20:32:52 -0500 Subject: [PATCH] fix(eval-harness): canonicalize toJSON values as JSON.stringify does canonicalize walked Object.keys and never called toJSON, so every Date hashed as {}. Replay fixtures are keyed by hashValue({ tool, args }), so a call with a different Date argument replayed the fixture recorded for another date instead of failing with tool.fixture_missing, and the stored response held {} where the live call returned a date. Call toJSON with the holder key before canonicalizing, matching the JSON coercion policy the module already follows. Plain JSON data hashes are unchanged; the pinned byte and hash vectors still pass. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/eval-harness/canonical.js | 13 +++++++++---- tests/lib/eval-harness/canonical.test.js | 18 ++++++++++++++++++ tests/lib/eval-harness/replay.test.js | 22 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/scripts/lib/eval-harness/canonical.js b/scripts/lib/eval-harness/canonical.js index bfba94b6d..1de2cc262 100644 --- a/scripts/lib/eval-harness/canonical.js +++ b/scripts/lib/eval-harness/canonical.js @@ -11,22 +11,27 @@ const crypto = require('crypto'); -function canonicalize(value) { +function canonicalize(value, holderKey = '') { + // Follow JSON.stringify: a Date (or any toJSON value) is hashed as the JSON + // it serializes to, not as its own enumerable keys, which are none. + if (value !== null && typeof value === 'object' && typeof value.toJSON === 'function') { + value = value.toJSON(holderKey); + } if (value === null || typeof value !== 'object') { return value; } if (Array.isArray(value)) { - return value.map(canonicalize); + return value.map((item, index) => canonicalize(item, String(index))); } const out = {}; for (const key of Object.keys(value).sort()) { - const item = value[key]; + const item = canonicalize(value[key], key); if (item === undefined) { continue; } // Generic JSON keys are data, including __proto__; never invoke a setter. Object.defineProperty(out, key, { - value: canonicalize(item), enumerable: true, writable: true, configurable: true, + value: item, enumerable: true, writable: true, configurable: true, }); } return out; diff --git a/tests/lib/eval-harness/canonical.test.js b/tests/lib/eval-harness/canonical.test.js index c932fd157..bdbfe1fd7 100644 --- a/tests/lib/eval-harness/canonical.test.js +++ b/tests/lib/eval-harness/canonical.test.js @@ -109,4 +109,22 @@ test('this fix retains existing non-JSON omission and coercion policy', () => { assert.strictEqual(canonicalJson(input), '{}'); }); +test('toJSON values canonicalize as JSON.stringify serializes them', () => { + const early = new Date('2024-01-01T00:00:00.000Z'); + const late = new Date('2025-06-01T00:00:00.000Z'); + assert.strictEqual(canonicalJson(early), '"2024-01-01T00:00:00.000Z"'); + assert.strictEqual(canonicalJson({ since: early, list: [late] }), '{"list":["2025-06-01T00:00:00.000Z"],"since":"2024-01-01T00:00:00.000Z"}'); + assert.notStrictEqual(hashValue({ since: early }), hashValue({ since: late })); + assert.notStrictEqual(hashValue({ since: early }), hashValue({ since: {} })); + const seen = []; + const custom = { toJSON(key) { seen.push(key); return { z: 1, a: [2] }; } }; + assert.strictEqual(canonicalJson({ outer: custom, list: [custom] }), '{"list":[{"a":[2],"z":1}],"outer":{"a":[2],"z":1}}'); + assert.deepStrictEqual(seen.sort(), ['0', 'outer']); + assert.strictEqual(canonicalJson({ gone: { toJSON: () => undefined }, kept: 1 }), '{"kept":1}'); + assert.strictEqual(canonicalJson([{ toJSON: () => undefined }]), '[null]'); + for (const value of [{ when: early, nested: { list: [late, custom] } }, [early, 1, 'x']]) { + assert.strictEqual(canonicalJson(value), canonicalJson(JSON.parse(JSON.stringify(value)))); + } +}); + finish('canonical'); diff --git a/tests/lib/eval-harness/replay.test.js b/tests/lib/eval-harness/replay.test.js index 9095ae79a..c07a447e0 100644 --- a/tests/lib/eval-harness/replay.test.js +++ b/tests/lib/eval-harness/replay.test.js @@ -53,6 +53,28 @@ test('replay mode never calls the implementation and fails closed on a missing f } }); +test('Date arguments key distinct fixtures and replay the recorded JSON response', () => { + const dir = tempDir('replay-date'); + try { + const store = new replay.FixtureStore(dir); + let liveCalls = 0; + const dated = { prices: { effect_class: 'SE0', determinism: 'deterministic', impl: (args) => { + liveCalls += 1; + return { since: args.since, close: args.since.getUTCFullYear() === 2024 ? 100 : 999 }; + } } }; + const live = replay.createReplayer(dated, { mode: 'record', store }).call('prices', { since: new Date('2024-01-01T00:00:00.000Z') }); + const replayer = replay.createReplayer(dated, { mode: 'replay', store }); + assert.throws(() => replayer.call('prices', { since: new Date('2025-06-01T00:00:00.000Z') }), (error) => error.code === 'tool.fixture_missing'); + assert.throws(() => replayer.call('prices', { since: {} }), (error) => error.code === 'tool.fixture_missing'); + const replayed = replayer.call('prices', { since: new Date('2024-01-01T00:00:00.000Z') }); + assert.deepStrictEqual(replayed, JSON.parse(JSON.stringify(live))); + assert.strictEqual(replayed.since, '2024-01-01T00:00:00.000Z'); + assert.strictEqual(liveCalls, 1); + } finally { + cleanup(dir); + } +}); + test('a hash-mismatched or corrupt fixture fails closed', () => { const dir = tempDir('mismatch'); try {