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;