T-23: Benchmark self-tests hash every untracked file in the worktree and fail on unreadable ones

This commit is contained in:
Studio 2026-11-27 12:43:30 -05:00
parent c2e4f3b2bd
commit b9536a7740
2 changed files with 223 additions and 11 deletions

View file

@ -247,25 +247,57 @@ async function child(workload: Workload) {
};
}
function command(args: string[]): string {
const result = Bun.spawnSync(args, { cwd: root, stdout: 'pipe', stderr: 'pipe' });
function command(args: string[], cwd: string = root): string {
const result = Bun.spawnSync(args, { cwd, stdout: 'pipe', stderr: 'pipe' });
if (result.exitCode !== 0) {
throw new Error(`${args[0]} failed: ${result.stderr.toString()}`);
}
return result.stdout.toString().trimEnd();
}
export function identity() {
const paths = command(['git', 'ls-files', '--cached', '--others', '--exclude-standard', '-z'])
function errorCode(error: unknown): string | undefined {
if (typeof error === 'object' && error !== null && 'code' in error) {
const code = (error as { code?: unknown }).code;
return typeof code === 'string' ? code : undefined;
}
return undefined;
}
export function identity(
options: {
sourceRoot?: string;
readSourceFile?: (path: string) => Uint8Array;
} = {}
) {
const sourceRoot = options.sourceRoot ?? root;
const readSourceFile = options.readSourceFile ?? readFileSync;
const tracked = new Set(
command(['git', 'ls-files', '--cached', '-z'], sourceRoot).split('\0').filter(Boolean)
);
const paths = command(
['git', 'ls-files', '--cached', '--others', '--exclude-standard', '-z'],
sourceRoot
)
.split('\0')
.filter(Boolean)
.sort();
const source = createHash('sha256');
for (const path of paths) {
const absolute = join(root, path);
source.update(
JSON.stringify([path, existsSync(absolute) ? sha256(readFileSync(absolute)) : 'deleted'])
);
const absolute = join(sourceRoot, path);
let entry: string;
try {
entry = sha256(readSourceFile(absolute));
} catch (error) {
const code = errorCode(error);
if (code === 'ENOENT') {
entry = 'deleted';
} else if ((code === 'EACCES' || code === 'EPERM') && !tracked.has(path)) {
entry = 'unreadable';
} else {
throw error;
}
}
source.update(JSON.stringify([path, entry]));
}
const addon = join(root, 'apps/stock/engine/dist/engine.node');
const nativeSha256 = sha256(readFileSync(addon));
@ -285,12 +317,12 @@ export function identity() {
};
return {
source: {
commit: command(['git', 'rev-parse', 'HEAD']),
dirty: command(['git', 'status', '--porcelain']).length > 0,
commit: command(['git', 'rev-parse', 'HEAD'], sourceRoot),
dirty: command(['git', 'status', '--porcelain'], sourceRoot).length > 0,
contentSha256: source.digest('hex'),
files: paths.length,
scope:
'Tracked and nonignored untracked file paths/content, including dirty changes; generated ignored artifacts excluded.',
'Tracked and nonignored untracked file paths/content, including dirty changes; unreadable untracked files are hashed by path with an unreadable marker; generated ignored artifacts excluded.',
},
native: {
path: addon,