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 <noreply@anthropic.com>
This commit is contained in:
NeoandClaude Opus 5.5 committed 2026-11-18 20:32:52 -05:00
1 parent 3f14a103a8
commit 06aeab9aa1
3 files changed
+49 -4

No files matched your search

+9 -4
View File
@@ -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;
+18
View File
@@ -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');
+22
View File
@@ -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 {