From 0bc6d1d052716de55b77a9091566d8c09426eb8d Mon Sep 17 00:00:00 2001 From: Neo Date: Wed, 18 Nov 2026 20:27:38 -0500 Subject: [PATCH 1/2] fix(eval-harness): make variant directory digests unique and locale-independent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit digestDir hashed each file as `path \0 bytes \0` with no length, so file contents holding NUL could absorb the following files: a one-file tree and a two-file tree produced the same digest. Frame each file as `path \0 byteLength \0 bytes`. Entries were also sorted with localeCompare, which follows the host locale (sv_SE sorts `ä` after `z`), so the same tree got different digests on different machines. Sort by code point. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/eval-harness/gate.js | 20 ++++++++++++++++--- tests/lib/eval-harness/gate.test.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/scripts/lib/eval-harness/gate.js b/scripts/lib/eval-harness/gate.js index 6ec49938782..0226de5e5f2 100644 --- a/scripts/lib/eval-harness/gate.js +++ b/scripts/lib/eval-harness/gate.js @@ -52,8 +52,15 @@ class GateError extends Error { } } +// Code-point order: localeCompare follows the host locale (sv_SE sorts `ä` after `z`), +// which would give the same tree a different digest on another machine. +function compareNames(a, b) { + if (a.name === b.name) return 0; + return a.name < b.name ? -1 : 1; +} + function listFiles(dir, base = dir, acc = []) { - for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) { + for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort(compareNames)) { if (entry.name === 'node_modules' || entry.name === '.git') { continue; } @@ -94,14 +101,21 @@ function readRegularFile(filePath, encoding) { } } -/** Content digest of a directory tree: sorted relative paths and bytes. */ +/** + * Content digest of a directory tree: sorted relative paths and bytes. + * Each file is framed as `path \0 byteLength \0 bytes`. Paths cannot contain + * NUL, and the length bounds the bytes, so contents holding NUL cannot shift + * a file boundary and two different trees cannot share a byte stream. + */ function digestDir(dir) { const hash = crypto.createHash('sha256'); for (const relative of listFiles(dir)) { + const bytes = readRegularFile(path.join(dir, relative)); hash.update(relative); hash.update('\0'); - hash.update(readRegularFile(path.join(dir, relative))); + hash.update(String(bytes.length)); hash.update('\0'); + hash.update(bytes); } return hash.digest('hex'); } diff --git a/tests/lib/eval-harness/gate.test.js b/tests/lib/eval-harness/gate.test.js index 7fa9344e94c..ae170d251c6 100644 --- a/tests/lib/eval-harness/gate.test.js +++ b/tests/lib/eval-harness/gate.test.js @@ -2,6 +2,7 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { spawnSync } = require('child_process'); const gate = require('../../../scripts/lib/eval-harness/gate'); const { test, tempDir, cleanup, finish } = require('./helpers'); const example = path.resolve(__dirname,'../../../examples/eval-harness'); @@ -14,6 +15,35 @@ test('directory digests are stable and distinguish baseline from candidate',()=> assert.notEqual(gate.digestDir(candidate),gate.digestDir(baseline)); assert.match(gate.digestDir(candidate),/^[0-9a-f]{64}$/); }); +test('directory digests frame file contents, so NUL bytes cannot merge or split files',()=>{ + const root=tempDir('gate-digest-framing'); + try { + const merged=path.join(root,'merged');const split=path.join(root,'split'); + fs.mkdirSync(merged);fs.mkdirSync(split); + fs.writeFileSync(path.join(merged,'a'),'x\0b\0y'); + fs.writeFileSync(path.join(split,'a'),'x');fs.writeFileSync(path.join(split,'b'),'y'); + assert.notEqual(gate.digestDir(merged),gate.digestDir(split)); + const empty=path.join(root,'empty');const nul=path.join(root,'nul'); + fs.mkdirSync(empty);fs.mkdirSync(nul); + fs.writeFileSync(path.join(empty,'a'),'');fs.writeFileSync(path.join(nul,'a'),'\0'); + assert.notEqual(gate.digestDir(empty),gate.digestDir(nul)); + } finally {cleanup(root);} +}); +test('directory digests order entries by code point, independent of the host locale',()=>{ + const root=tempDir('gate-digest-locale'); + try { + for (const name of ['a.js','B.js','z.js','ä.js','_x.js','-x.js']) fs.writeFileSync(path.join(root,name),name); + fs.mkdirSync(path.join(root,'a'));fs.writeFileSync(path.join(root,'a','b.js'),'nested'); + const digestUnder=locale=>{ + const child=spawnSync(process.execPath,['-e',`process.stdout.write(require(${JSON.stringify(require.resolve('../../../scripts/lib/eval-harness/gate'))}).digestDir(${JSON.stringify(root)}))`], + {encoding:'utf8',env:{...process.env,LC_ALL:locale,LANG:locale}}); + assert.equal(child.status,0,child.stderr); + return child.stdout; + }; + const reference=gate.digestDir(root); + for (const locale of ['en_US.UTF-8','sv_SE.UTF-8','tr_TR.UTF-8','C']) assert.equal(digestUnder(locale),reference,locale); + } finally {cleanup(root);} +}); test('variant and taskset inspection remains available without execution',()=>{ const v=gate.loadVariant(candidate);const t=gate.loadTaskset(taskset); assert.equal(v.entry,'run.js');assert.equal(t.tasks.length,12);assert.match(t.digest,/^[0-9a-f]{64}$/); From 18f1db503a972527efd68f2bec2849811b54f662 Mon Sep 17 00:00:00 2001 From: Neo Date: Wed, 18 Nov 2026 20:33:37 -0500 Subject: [PATCH 2/2] docs(eval-harness): describe digest sort as UTF-16 code-unit order Co-Authored-By: Claude Opus 5.5 --- scripts/lib/eval-harness/gate.js | 5 +++-- tests/lib/eval-harness/gate.test.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/lib/eval-harness/gate.js b/scripts/lib/eval-harness/gate.js index 0226de5e5f2..5fbbd80960a 100644 --- a/scripts/lib/eval-harness/gate.js +++ b/scripts/lib/eval-harness/gate.js @@ -52,8 +52,9 @@ class GateError extends Error { } } -// Code-point order: localeCompare follows the host locale (sv_SE sorts `ä` after `z`), -// which would give the same tree a different digest on another machine. +// UTF-16 code-unit order (plain `<`). localeCompare follows the host locale +// (sv_SE sorts `ä` after `z`), which would give the same tree a different +// digest on another machine. function compareNames(a, b) { if (a.name === b.name) return 0; return a.name < b.name ? -1 : 1; diff --git a/tests/lib/eval-harness/gate.test.js b/tests/lib/eval-harness/gate.test.js index ae170d251c6..f7fd8197f0d 100644 --- a/tests/lib/eval-harness/gate.test.js +++ b/tests/lib/eval-harness/gate.test.js @@ -29,7 +29,7 @@ test('directory digests frame file contents, so NUL bytes cannot merge or split assert.notEqual(gate.digestDir(empty),gate.digestDir(nul)); } finally {cleanup(root);} }); -test('directory digests order entries by code point, independent of the host locale',()=>{ +test('directory digests order entries by UTF-16 code unit, independent of the host locale',()=>{ const root=tempDir('gate-digest-locale'); try { for (const name of ['a.js','B.js','z.js','ä.js','_x.js','-x.js']) fs.writeFileSync(path.join(root,name),name);