Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
// mine-shard-weights.test.ts — pure-function tests for the log parser
// and weight extraction. The full pipeline (gh run view → write JSON)
// is integration-tested by actually running it once during T4.
import { describe, expect, it } from "bun:test";
import { chmodSync, mkdirSync, mkdtempSync, writeFileSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { spawnSync } from 'node:child_process';
import {
computeWeights,
medianWeights,
mineWeights,
parseLog,
serializeWeights,
sourceE2ECorpus,
} from "../../scripts/mine-shard-weights.ts";
/** A checkout tracking `files`, the set the miner bounds its output by (GBRAIN_MINE_ROOT). */
function trackedCheckout(dir: string, files: string[]): NodeJS.ProcessEnv {
const root = join(dir, 'checkout');
mkdirSync(root, { recursive: true });
for (const file of files) {
mkdirSync(dirname(join(root, file)), { recursive: true });
writeFileSync(join(root, file), '');
}
for (const args of [['init', '-q'], ['add', '-A']]) expect(spawnSync('git', args, { cwd: root, encoding: 'utf8' }).status).toBe(0);
return { ...process.env, GBRAIN_MINE_ROOT: root };
}
const SAMPLE = `test (1)\tUNKNOWN STEP\t2026-05-25T11:26:40.000000Z ##[group]test/alpha.test.ts:
test (1)\tUNKNOWN STEP\t2026-05-25T11:26:41.000000Z (pass) some test [1.00ms]
test (1)\tUNKNOWN STEP\t2026-05-25T11:26:43.500000Z ##[group]test/beta.test.ts:
test (1)\tUNKNOWN STEP\t2026-05-25T11:26:45.000000Z (pass) another test
test (1)\tUNKNOWN STEP\t2026-05-25T11:26:48.000000Z ##[group]test/gamma.test.ts:
test (1)\tUNKNOWN STEP\t2026-05-25T11:26:50.000000Z ##[group]test/delta.test.ts:
test (2)\tUNKNOWN STEP\t2026-05-25T11:26:42.000000Z ##[group]test/epsilon.test.ts:
test (2)\tUNKNOWN STEP\t2026-05-25T11:26:55.000000Z ##[group]test/zeta.test.ts:
`;
describe("parseLog", () => {
it("extracts file-start events with timestamps and jobs", () => {
const events = parseLog(SAMPLE);
expect(events.length).toBe(6);
expect(events[0]).toMatchObject({
job: "test (1)",
file: "test/alpha.test.ts",
});
expect(events[5]).toMatchObject({
job: "test (2)",
file: "test/zeta.test.ts",
});
});
it("preserves stream order", () => {
const events = parseLog(SAMPLE);
const files = events.map((e) => e.file);
expect(files).toEqual([
"test/alpha.test.ts",
"test/beta.test.ts",
"test/gamma.test.ts",
"test/delta.test.ts",
"test/epsilon.test.ts",
"test/zeta.test.ts",
]);
});
it("ignores non-group lines", () => {
// The sample has (pass) lines mixed in. They should not appear in the
// event list; parseLog only emits ##[group]test/X.test.ts: matches.
const events = parseLog(SAMPLE);
expect(events.every((e) => e.file.startsWith("test/"))).toBe(true);
expect(events.every((e) => e.file.endsWith(".test.ts"))).toBe(true);
});
it("handles empty input", () => {
expect(parseLog("")).toEqual([]);
});
it("ignores malformed timestamps", () => {
const bad = "test (1)\tUNKNOWN\tBAD-TS ##[group]test/x.test.ts:\n";
expect(parseLog(bad)).toEqual([]);
});
it("rejects non-test-file groups (e.g. setup-bun action groups)", () => {
const noise =
"test (1)\tUNKNOWN\t2026-05-25T11:26:40.000Z ##[group]Run actions/checkout\n";
expect(parseLog(noise)).toEqual([]);
});
});
describe('full E2E timing extraction', () => {
const expectedFiles = ['test/e2e/alpha.test.ts', 'test/phantom-redirect-engine-parity.test.ts'];
const line = (job: string, text: string) => `${job}\tstep\t2026-09-24T06:00:00.000Z ${text}\n`;
const file = (job: string, name: string) => line(job, `=== ${name} ===`) + line(job, ' 0 fail') + line(job, 'Ran 1 test across 1 file. [2.50s]');
const complete = (job: string, names: string[]) => names.map(name => file(job, name)).join('') + line(job, `Files: ${names.length} total, ${names.length} passed, 0 failed`);
const raw = complete('coverage-full-e2e', ['alpha.test.ts', 'phantom-redirect-engine-parity.test.ts']);
const opts = { e2eProfile: 'full' as const, expectedFiles, expectedJobs: ['coverage-full-e2e'] };
it('resolves the outside-directory parity file without including selected-job timings', () => {
const noise = complete('Selected E2E (diff-relevant) 1', ['unrelated.test.ts']);
expect(Object.fromEntries(mineWeights(raw + noise, 'e2e', opts))).toEqual(Object.fromEntries(expectedFiles.map(file => [file, 2500])));
expect(Object.fromEntries(mineWeights(raw + noise, 'e2e'))).toEqual({ 'test/e2e/unrelated.test.ts': 2500 });
});
it('uses the final parent summary rather than a nested child summary', () => {
for (const [job, options] of [['coverage-full-e2e', opts], ['Selected E2E (diff-relevant) 1', {}]] as const) {
const nested = line(job, '=== alpha.test.ts ===') + line(job, ' 0 fail') + line(job, 'Ran 1 test across 1 file. [1.00ms]')
+ line(job, ' 0 fail') + line(job, 'Ran 1 test across 1 file. [80.00ms]')
+ file(job, 'phantom-redirect-engine-parity.test.ts') + line(job, 'Files: 2 total, 2 passed, 0 failed');
expect(mineWeights(nested, 'e2e', options).get(expectedFiles[0])).toBe(80);
expect(() => mineWeights(nested.replace(' 0 fail\n' + line(job, 'Ran 1 test across 1 file. [80.00ms]'), ' 1 fail\n' + line(job, 'Ran 1 test across 1 file. [80.00ms]')), 'e2e', options)).toThrow();
}
});
it('requires complete, disjoint matrix jobs and rejects missing, ambiguous and extra files', () => {
const jobs = ['coverage-full-e2e (1)', 'coverage-full-e2e (2)'];
const matrix = complete(jobs[0], ['alpha.test.ts']) + complete(jobs[1], ['phantom-redirect-engine-parity.test.ts']);
expect(mineWeights(matrix, 'e2e', { ...opts, expectedJobs: jobs }).size).toBe(2);
for (const bad of [
complete(jobs[0], ['alpha.test.ts']),
complete(jobs[0], ['alpha.test.ts']) + complete(jobs[1], ['alpha.test.ts']),
matrix.replace(' 0 fail', ' 1 fail'),
matrix.replace('1 passed, 0 failed', '0 passed, 1 failed'),
matrix.replace('1 passed, 0 failed', '0 passed, 0 failed'),
matrix.replace('phantom-redirect-engine-parity.test.ts', 'unknown.test.ts'),
matrix.replace('Ran 1 test across 1 file. [2.50s]', ''),
]) expect(() => mineWeights(bad, 'e2e', { ...opts, expectedJobs: jobs })).toThrow();
expect(() => mineWeights(raw, 'e2e', { ...opts, expectedFiles: [...expectedFiles, 'test/alpha.test.ts'] })).toThrow('ambiguous');
expect(() => mineWeights(raw, 'e2e', { ...opts, expectedFiles: [...expectedFiles, 'test/e2e/missing.test.ts'] })).toThrow('missing source files');
expect(() => mineWeights(raw, 'e2e', { e2eProfile: 'full' })).toThrow('source corpus');
});
});
describe("computeWeights", () => {
it("computes delta between consecutive file headers within a job", () => {
const events = parseLog(SAMPLE);
const weights = computeWeights(events);
// job test (1): alpha → 3500ms (40s → 43.5s), beta → 4500ms (43.5s → 48s),
// gamma → 2000ms (48s → 50s), delta dropped (no successor).
expect(weights.get("test/alpha.test.ts")).toBe(3500);
expect(weights.get("test/beta.test.ts")).toBe(4500);
expect(weights.get("test/gamma.test.ts")).toBe(2000);
expect(weights.get("test/delta.test.ts")).toBeUndefined();
// job test (2): epsilon → 13000ms (42s → 55s), zeta dropped.
expect(weights.get("test/epsilon.test.ts")).toBe(13000);
expect(weights.get("test/zeta.test.ts")).toBeUndefined();
});
it("does not cross job boundaries", () => {
// If we naively diffed across jobs we'd get bogus values when
// alpha (test 1) was followed by epsilon (test 2) by clock skew.
const events = parseLog(SAMPLE);
const weights = computeWeights(events);
// alpha → beta within same job: 3500ms. Not 2000ms (which would be
// alpha's stamp to epsilon's stamp across jobs).
expect(weights.get("test/alpha.test.ts")).toBe(3500);
});
it("takes the max when a file appears with multiple deltas", () => {
const events = [
{ job: "test (1)", timestampMs: 1000, file: "test/x.test.ts" },
{ job: "test (1)", timestampMs: 2000, file: "test/y.test.ts" },
{ job: "test (2)", timestampMs: 3000, file: "test/x.test.ts" },
{ job: "test (2)", timestampMs: 8000, file: "test/y.test.ts" },
];
const w = computeWeights(events);
// x: 1000→2000 (1000ms in job 1), 3000→8000 (5000ms in job 2). Max = 5000.
expect(w.get("test/x.test.ts")).toBe(5000);
});
it("skips out-of-order events defensively (negative delta)", () => {
const events = [
{ job: "test (1)", timestampMs: 5000, file: "test/x.test.ts" },
{ job: "test (1)", timestampMs: 3000, file: "test/y.test.ts" },
];
const w = computeWeights(events);
// x → negative delta → skipped. y has no successor → not recorded.
expect(w.size).toBe(0);
});
it("empty input → empty map", () => {
expect(computeWeights([]).size).toBe(0);
});
});
describe("serializeWeights", () => {
it("sorts keys alphabetically for stable diffs", () => {
const w = new Map([
["test/z.test.ts", 100],
["test/a.test.ts", 200],
["test/m.test.ts", 50],
]);
const json = serializeWeights(w);
const lines = json.split("\n").filter((l) => l.includes('"test/'));
expect(lines[0]).toContain("a.test.ts");
expect(lines[1]).toContain("m.test.ts");
expect(lines[2]).toContain("z.test.ts");
});
it("ends with a trailing newline (POSIX-friendly diff)", () => {
const w = new Map([["test/x.test.ts", 1]]);
expect(serializeWeights(w).endsWith("\n")).toBe(true);
});
it("empty map → empty object JSON", () => {
expect(serializeWeights(new Map())).toBe("{}\n");
});
it("round-trips: parse our own output", () => {
const w = new Map([
["test/alpha.test.ts", 100],
["test/beta.test.ts", 250],
]);
const json = serializeWeights(w);
const parsed = JSON.parse(json);
expect(parsed["test/alpha.test.ts"]).toBe(100);
expect(parsed["test/beta.test.ts"]).toBe(250);
});
});
describe('complete CI timing extraction', () => {
const line = (job: string, seconds: number, message: string) => `${job}\tstep\t2026-05-25T11:26:${String(seconds).padStart(2, '0')}.000Z ${message}\n`;
const header = (file: string) => `##[group]${file}:`;
const unitLog = line('test (1)', 1, header('test/one.test.ts')) + line('test (1)', 3, header('evals/two.test.ts')) + line('test (1)', 4, ' 0 fail') + line('test (1)', 6, 'Ran 2 tests across 2 files. [5.00s]');
it('includes evals and the final file, while excluding buffered/dedicated jobs', () => {
const noise = line('serial-tests', 1, header('test/wrong.test.ts')) + line('slow-eval', 1, header('test/also-wrong.test.ts'));
expect(Object.fromEntries(mineWeights(unitLog + noise, 'unit'))).toEqual({ 'test/one.test.ts': 2000, 'evals/two.test.ts': 3000 });
});
it('handles a one-file job', () => {
const raw = line('test (2)', 1, header('test/one.test.ts')) + line('test (2)', 2, ' 0 fail') + line('test (2)', 4, 'Ran 1 test across 1 file. [3.00s]');
expect(mineWeights(raw, 'unit').get('test/one.test.ts')).toBe(3000);
});
it('rejects truncated, failed, mismatched and out-of-order unit input', () => {
for (const raw of [SAMPLE, unitLog.replace('0 fail', '1 fail'), unitLog.replace('across 2', 'across 3'), unitLog.replace('11:26:03', '11:26:00')]) expect(() => mineWeights(raw, 'unit')).toThrow();
});
it('refuses setup-only jobs and missing jobs promised by GitHub metadata', () => {
expect(() => mineWeights(unitLog + line('test (2)', 1, 'snapshot setup started'), 'unit')).toThrow('incomplete Bun summary');
expect(() => mineWeights(unitLog, 'unit', { expectedJobs: ['test (1)', 'test (2)'] })).toThrow('missing: test (2)');
expect(() => mineWeights(unitLog, 'unit', { expectedJobs: ['test (2)'] })).toThrow('unexpected: test (1)');
expect(mineWeights(unitLog, 'unit', { expectedJobs: ['test (1)'] }).size).toBe(2);
});
it('mines raw Bun group commands and requires the outer capture completion', () => {
// Retained capture logs contain the original workflow command. Only the
// rendered GitHub log rewrites ::group:: into ##[group].
const captured = unitLog.replaceAll('\tstep\t', '\tcapture\t').replaceAll('##[group]', '::group::');
const marker = (seconds: number, text: string) => line('test (1)', seconds, text).replace('\tstep\t', '\tcapture\t');
const start = marker(0, '##[gbrain-capture-start]');
const complete = marker(7, '##[gbrain-capture-complete] exit=0');
expect(Object.fromEntries(mineWeights(start + captured + complete, 'unit'))).toEqual({ 'test/one.test.ts': 2000, 'evals/two.test.ts': 3000 });
for (const raw of [captured, start + captured, captured + complete, start + captured + complete.replace('exit=0', 'exit=7'), start + captured + complete + start, start + start + captured + complete]) {
expect(() => mineWeights(raw, 'unit')).toThrow('capture completion');
}
// Historical GitHub step logs remain supported with authoritative --run metadata.
expect(mineWeights(unitLog, 'unit').size).toBe(2);
});
it('does not mix interleaved jobs', () => {
const raw = line('test (1)', 1, header('test/a.test.ts')) + line('test (2)', 2, header('test/b.test.ts')) + line('test (1)', 3, ' 0 fail') + line('test (2)', 4, ' 0 fail') + line('test (2)', 5, 'Ran 1 test across 1 file. [3s]') + line('test (1)', 6, 'Ran 1 test across 1 file. [5s]');
expect(Object.fromEntries(mineWeights(raw, 'unit'))).toEqual({ 'test/a.test.ts': 5000, 'test/b.test.ts': 3000 });
});
it('uses serial runner durations in seconds, not buffered timestamp deltas', () => {
const raw = line('serial-tests (1)', 50, '[serial-tests] PASS 19s test/a.serial.test.ts (2pass)') + line('serial-tests (1)', 50, '[serial-tests] PASS 2s test/b.serial.test.ts (1pass)') + line('serial-tests (1)', 51, '[serial-tests] all 2 file(s) passed in 19s (pool=4)');
expect(Object.fromEntries(mineWeights(raw, 'serial'))).toEqual({ 'test/a.serial.test.ts': 19, 'test/b.serial.test.ts': 2 });
expect(() => mineWeights(raw.replace('all 2', 'all 3'), 'serial')).toThrow();
expect(() => mineWeights(raw + line('serial-tests (2)', 1, 'snapshot setup started'), 'serial')).toThrow('incomplete serial execution');
for (const bad of ['1..2', '9'.repeat(310)]) expect(() => mineWeights(raw.replace('PASS 19s', `PASS ${bad}s`), 'serial')).toThrow('invalid duration');
});
it('reads E2E summaries as milliseconds and requires the complete lane summary', () => {
const job = 'Selected E2E (diff-relevant) 1';
const raw = line(job, 1, '=== alpha.test.ts ===') + line(job, 2, ' 0 fail') + line(job, 3, 'Ran 4 tests across 1 file. [1.51s]') + line(job, 4, 'Files: 1 total, 1 passed, 0 failed');
expect(mineWeights(raw, 'e2e').get('test/e2e/alpha.test.ts')).toBe(1510);
expect(() => mineWeights(raw.replace('0 failed', '1 failed'), 'e2e')).toThrow();
expect(() => mineWeights(raw.replace(' 0 fail', ' 1 fail'), 'e2e')).toThrow();
expect(() => mineWeights(raw + line('Selected E2E (diff-relevant) 2', 1, 'snapshot setup started'), 'e2e')).toThrow('incomplete e2e execution');
expect(mineWeights(raw + line('Selected E2E (diff-relevant) 2', 1, 'selected E2E: explicit empty selection; no tests launched'), 'e2e').size).toBe(1);
for (const bad of ['1..2', '9'.repeat(310)]) expect(() => mineWeights(raw.replace('[1.51s]', `[${bad}s]`), 'e2e')).toThrow('invalid duration');
});
it('never serializes non-finite or negative weights into a poisoned map', () => {
for (const value of [NaN, Infinity, -1]) expect(() => serializeWeights(new Map([['test/bad.test.ts', value]]))).toThrow('invalid weight');
});
});
it('a failed CLI refresh leaves the existing output untouched', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-failure-'));
try {
const out = join(dir, 'weights');
const input = join(dir, 'truncated.log');
const env = trackedCheckout(dir, ['keep', ...['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta'].map(name => `test/${name}.test.ts`)]);
writeFileSync(out, '{"keep":123}\n');
writeFileSync(input, SAMPLE);
const failed = spawnSync(process.execPath, [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--from-file', input, '--out', out], { env, encoding: 'utf8' });
expect(failed.status).not.toBe(0);
expect(readFileSync(out, 'utf8')).toBe('{"keep":123}\n');
writeFileSync(input, SAMPLE + 'test (1)\tstep\t2026-05-25T11:26:56.000Z 0 fail\ntest (1)\tstep\t2026-05-25T11:26:57.000Z Ran 4 tests across 4 files. [17s]\ntest (2)\tstep\t2026-05-25T11:26:56.000Z 0 fail\ntest (2)\tstep\t2026-05-25T11:26:58.000Z Ran 2 tests across 2 files. [16s]\n');
const passed = spawnSync(process.execPath, [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--from-file', input, '--out', out], { env, encoding: 'utf8' });
expect(passed.status, passed.stderr).toBe(0);
const weights = JSON.parse(readFileSync(out, 'utf8'));
expect(weights['test/delta.test.ts']).toBe(7000);
expect(weights.keep).toBe(123);
expect(JSON.parse(readFileSync(out + '.metadata.json', 'utf8'))).toMatchObject({ measuredFiles: 6, totalFiles: 7, mergeExisting: true });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('partial serial artifacts preserve unobserved files; malformed durations cannot replace either output', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-serial-'));
try {
const out = join(dir, 'weights.json');
const meta = join(dir, 'weights.metadata.json');
const input = join(dir, 'serial.log');
const line = (text: string) => `serial-tests (1)\tstep\t2026-05-25T11:26:50.000Z ${text}\n`;
const raw = line('[serial-tests] PASS 7s test/a.serial.test.ts (1pass)') + line('[serial-tests] all 1 file(s) passed in 7s (pool=4)');
const env = trackedCheckout(dir, ['test/a.serial.test.ts', 'test/unobserved.serial.test.ts']);
writeFileSync(out, '{"test/unobserved.serial.test.ts":42}\n');
writeFileSync(input, raw);
const args = [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--lane', 'serial', '--from-file', input, '--out', out];
expect(spawnSync(process.execPath, args, { env, encoding: 'utf8' }).status).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/a.serial.test.ts': 7, 'test/unobserved.serial.test.ts': 42 });
expect(JSON.parse(readFileSync(meta, 'utf8'))).toMatchObject({ measuredFiles: 1, totalFiles: 2, mergeExisting: true });
const saved = [readFileSync(out, 'utf8'), readFileSync(meta, 'utf8')];
writeFileSync(input, raw.replace('PASS 7s', 'PASS 1..2s'));
expect(spawnSync(process.execPath, args, { env, encoding: 'utf8' }).status).not.toBe(0);
expect([readFileSync(out, 'utf8'), readFileSync(meta, 'utf8')]).toEqual(saved);
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('GitHub refresh verifies every eligible job before replacing the complete lane', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-github-'));
try {
const bin = join(dir, 'bin');
const artifacts = join(dir, 'artifacts');
mkdirSync(bin);
mkdirSync(artifacts);
const fakeGh = join(bin, 'gh');
writeFileSync(fakeGh, '#!/bin/sh\ncase "$*" in\n "run view 123 --json conclusion,headSha,jobs") cat "$FIXTURE_GH_INFO" ;;\n "run download 123 --name timings-unit-"*) [ -f "$FIXTURE_GH_ARTIFACTS/$5.log" ] && cp "$FIXTURE_GH_ARTIFACTS/$5.log" "$7/unit.log" ;;\n *) exit 2 ;;\nesac\n');
chmodSync(fakeGh, 0o755);
const info = join(dir, 'run.json');
const out = join(dir, 'weights.json');
const meta = join(dir, 'weights.metadata.json');
const line = (job: string, text: string) => `${job}\tcapture\t2026-05-25T11:26:50.000Z ${text}\n`;
const complete = (job: string, file: string) => line(job, '##[gbrain-capture-start]') + line(job, `::group::${file}:`) + line(job, '0 fail')
+ line(job, 'Ran 1 test across 1 file. [1ms]') + line(job, '##[gbrain-capture-complete] exit=0');
const artifact = (shard: number, body: string | null) => {
const path = join(artifacts, `timings-unit-${shard}.log`);
if (body === null) rmSync(path, { force: true }); else writeFileSync(path, body);
};
artifact(1, complete('test (1)', 'test/one.test.ts'));
writeFileSync(info, JSON.stringify({ conclusion: 'success', headSha: 'fixture-commit', jobs: [{ name: 'test (1)', conclusion: 'success' }, { name: 'test (2)', conclusion: 'success' }, { name: 'verify', conclusion: 'success' }] }));
writeFileSync(out, '{"test/old.test.ts":42}\n');
writeFileSync(meta, '{"existing":true}\n');
const args = [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--run', '123', '--out', out];
const env = { ...trackedCheckout(dir, ['test/one.test.ts', 'test/two.test.ts']), PATH: `${bin}:${process.env.PATH}`, FIXTURE_GH_INFO: info, FIXTURE_GH_ARTIFACTS: artifacts };
for (const partial of [null, line('test (2)', '##[gbrain-capture-start]') + line('test (2)', 'snapshot setup started') + line('test (2)', '##[gbrain-capture-complete] exit=0')]) {
artifact(2, partial);
expect(spawnSync(process.execPath, args, { env, encoding: 'utf8' }).status).not.toBe(0);
expect(readFileSync(out, 'utf8')).toBe('{"test/old.test.ts":42}\n');
expect(readFileSync(meta, 'utf8')).toBe('{"existing":true}\n');
}
artifact(2, complete('test (2)', 'test/two.test.ts'));
const result = spawnSync(process.execPath, args, { env, encoding: 'utf8' });
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/one.test.ts': 0, 'test/two.test.ts': 0 });
expect(JSON.parse(readFileSync(meta, 'utf8'))).toMatchObject({ commit: 'fixture-commit', measuredFiles: 2, totalFiles: 2, mergeExisting: false });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('GitHub unit refresh requires each shard capture artifact to record a successful completion', () => {
const job = 'test (1)';
const github = (text: string) => `${job}\tUNKNOWN STEP\t2026-10-10T06:00:01.000Z ${text}\n`;
const captured = (text: string) => `${job}\tcapture\t2026-10-10T06:00:01.000Z ${text}\n`;
const body = (line: (text: string) => string) => line('::group::test/one.test.ts:') + line(' 0 fail') + line('Ran 1 test across 1 file. [1ms]');
const opts = { expectedJobs: [job], requireCapture: true };
for (const raw of [body(github), body(captured), captured('##[gbrain-capture-start]') + body(captured),
captured('##[gbrain-capture-start]') + body(captured) + captured('##[gbrain-capture-complete] exit=1')]) {
expect(() => mineWeights(raw, 'unit', opts)).toThrow('capture completion');
}
expect([...mineWeights(captured('##[gbrain-capture-start]') + body(captured) + captured('##[gbrain-capture-complete] exit=0'), 'unit', opts).keys()]).toEqual(['test/one.test.ts']);
expect(mineWeights(body(github), 'unit', { expectedJobs: [job] }).size).toBe(1);
});
it('GitHub serial refresh reads each job log by id instead of the whole-run log', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-serial-'));
try {
const bin = join(dir, 'bin');
mkdirSync(bin);
writeFileSync(join(bin, 'gh'), '#!/bin/sh\ncase "$*" in\n "run view 123 --json conclusion,headSha,jobs") cat "$FIXTURE_GH_INFO" ;;\n "run view 123 --job 77 --log") cat "$FIXTURE_GH_LOG" ;;\n *) exit 2 ;;\nesac\n', { mode: 0o755 });
const info = join(dir, 'run.json');
const log = join(dir, 'serial.log');
const out = join(dir, 'weights.json');
const job = 'serial-tests (1)';
const line = (text: string) => `${job}\tUNKNOWN STEP\t2026-10-10T06:00:01.000Z ${text}\n`;
writeFileSync(log, line('[serial-tests] PASS 4s test/a.serial.test.ts (1pass)') + line('[serial-tests] all 1 file(s) passed in 4s (pool=4)'));
const args = [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--lane', 'serial', '--run', '123', '--out', out];
const env = { ...trackedCheckout(dir, ['test/a.serial.test.ts']), PATH: `${bin}:${process.env.PATH}`, FIXTURE_GH_INFO: info, FIXTURE_GH_LOG: log };
writeFileSync(info, JSON.stringify({ conclusion: 'success', headSha: 'fixture-commit', jobs: [{ name: job, conclusion: 'success' }] }));
expect(spawnSync(process.execPath, args, { env, encoding: 'utf8' }).stderr).toContain('serial: missing job identity');
writeFileSync(info, JSON.stringify({ conclusion: 'success', headSha: 'fixture-commit', jobs: [{ name: job, conclusion: 'success', databaseId: 77 }] }));
const result = spawnSync(process.execPath, args, { env, encoding: 'utf8' });
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/a.serial.test.ts': 4 });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('full-profile CLI pins source, attempt and job provenance and preserves both outputs on refused evidence', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-full-'));
try {
const repo = join(import.meta.dir, '../..');
const sha = spawnSync('git', ['rev-parse', 'HEAD'], { cwd: repo, encoding: 'utf8' }).stdout.trim();
const corpus = sourceE2ECorpus(sha);
expect(corpus).toContain('test/phantom-redirect-engine-parity.test.ts');
expect(() => sourceE2ECorpus('wrong-commit')).toThrow('source SHA');
const bin = join(dir, 'bin');
mkdirSync(bin);
writeFileSync(join(bin, 'gh'), `#!/bin/sh
case "$*" in
"run view 123 --json attempt,conclusion,headSha,jobs") cat "$FIXTURE_GH_INFO" ;;
"run view 123 --attempt 2 --job 456 --log") cat "$FIXTURE_GH_LOG" ;;
*) exit 2 ;;
esac
`, { mode: 0o755 });
const infoPath = join(dir, 'run.json');
const logPath = join(dir, 'full.log');
const out = join(dir, 'weights.json');
const meta = join(dir, 'weights.metadata.json');
const info = { attempt: 2, conclusion: 'success', headSha: sha, jobs: [{ name: 'coverage-full-e2e', databaseId: 456, conclusion: 'success' }] };
const line = (text: string) => `coverage-full-e2e\tstep\t2026-09-24T06:00:00.000Z ${text}\n`;
const files = corpus.map(path => line(`=== ${path.split('/').at(-1)} ===`) + line(' 0 fail') + line('Ran 1 test across 1 file. [1.00ms]'));
const complete = files.join('') + line(`Files: ${files.length} total, ${files.length} passed, 0 failed`);
const env = { ...process.env, PATH: `${bin}:${process.env.PATH}`, FIXTURE_GH_INFO: infoPath, FIXTURE_GH_LOG: logPath };
const base = [join(repo, 'scripts/mine-shard-weights.ts'), '--lane', 'e2e', '--e2e-profile', 'full', '--out', out];
const run = (args = ['--run', '123']) => spawnSync(process.execPath, [...base, ...args], { env, encoding: 'utf8' });
writeFileSync(out, '{"keep":123}\n');
writeFileSync(meta, '{"prior":true}\n');
for (const [metadata, log] of [
[{ ...info, conclusion: 'failure' }, complete],
[{ ...info, headSha: 'other-commit' }, complete],
[{ ...info, attempt: undefined }, complete],
[{ ...info, jobs: [{ ...info.jobs[0], conclusion: 'cancelled' }] }, complete],
[{ ...info, jobs: [{ ...info.jobs[0], databaseId: undefined }] }, complete],
[info, files.slice(1).join('') + line(`Files: ${files.length - 1} total, ${files.length - 1} passed, 0 failed`)],
[info, complete.replace(' 0 fail', ' 1 fail')],
] as const) {
writeFileSync(infoPath, JSON.stringify(metadata));
writeFileSync(logPath, log);
expect(run().status).not.toBe(0);
expect(readFileSync(out, 'utf8')).toBe('{"keep":123}\n');
expect(readFileSync(meta, 'utf8')).toBe('{"prior":true}\n');
}
expect(run(['--from-file', logPath]).status).not.toBe(0);
writeFileSync(infoPath, JSON.stringify(info));
writeFileSync(logPath, complete);
const result = run();
expect(result.status, result.stderr).toBe(0);
expect(Object.keys(JSON.parse(readFileSync(out, 'utf8'))).sort()).toEqual([...corpus].sort());
expect(JSON.parse(readFileSync(meta, 'utf8'))).toMatchObject({
lane: 'e2e', e2eProfile: 'full', run: '123', commit: sha, attempt: 2, source: 'github',
jobs: [{ name: 'coverage-full-e2e', databaseId: 456 }], measuredFiles: corpus.length, totalFiles: corpus.length, mergeExisting: false,
logSha256: expect.stringMatching(/^[0-9a-f]{64}$/), corpusSha256: expect.stringMatching(/^[0-9a-f]{64}$/),
});
} finally { rmSync(dir, { recursive: true, force: true }); }
}, 30000);
it('GitHub refresh mines every shard of a packed unit job (test.yml runs two per runner)', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-packed-'));
try {
const bin = join(dir, 'bin');
const artifacts = join(dir, 'artifacts');
mkdirSync(bin);
mkdirSync(artifacts);
writeFileSync(join(bin, 'gh'), '#!/bin/sh\ncase "$*" in\n "run view 123 --json conclusion,headSha,jobs") cat "$FIXTURE_GH_INFO" ;;\n "run download 123 --name timings-unit-"*) [ -f "$FIXTURE_GH_ARTIFACTS/$5.log" ] && cp "$FIXTURE_GH_ARTIFACTS/$5.log" "$7/unit.log" ;;\n *) exit 2 ;;\nesac\n');
chmodSync(join(bin, 'gh'), 0o755);
const info = join(dir, 'run.json');
const out = join(dir, 'weights.json');
const line = (job: string, text: string) => `${job}\tcapture\t2026-05-25T11:26:50.000Z ${text}\n`;
const complete = (job: string, file: string) => line(job, '##[gbrain-capture-start]') + line(job, `::group::${file}:`) + line(job, '0 fail')
+ line(job, 'Ran 1 test across 1 file. [1ms]') + line(job, '##[gbrain-capture-complete] exit=0');
writeFileSync(join(artifacts, 'timings-unit-1.log'), complete('test (1)', 'test/one.test.ts'));
writeFileSync(join(artifacts, 'timings-unit-3.log'), complete('test (3)', 'test/three.test.ts'));
writeFileSync(join(artifacts, 'timings-unit-4.log'), complete('test (4)', 'test/four.test.ts'));
writeFileSync(info, JSON.stringify({ conclusion: 'success', headSha: 'fixture-commit', jobs: [{ name: 'test (1, 2)', conclusion: 'success' }, { name: 'test (3, 4)', conclusion: 'success' }] }));
writeFileSync(out, '{"test/old.test.ts":42}\n');
const args = [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--run', '123', '--out', out];
const env = { ...trackedCheckout(dir, ['test/one.test.ts', 'test/two.test.ts', 'test/three.test.ts', 'test/four.test.ts']), PATH: `${bin}:${process.env.PATH}`, FIXTURE_GH_INFO: info, FIXTURE_GH_ARTIFACTS: artifacts };
expect(spawnSync(process.execPath, args, { env, encoding: 'utf8' }).status).not.toBe(0);
expect(readFileSync(out, 'utf8')).toBe('{"test/old.test.ts":42}\n');
writeFileSync(join(artifacts, 'timings-unit-2.log'), complete('test (2)', 'test/two.test.ts'));
const result = spawnSync(process.execPath, args, { env, encoding: 'utf8' });
expect(result.status, result.stderr).toBe(0);
expect(Object.keys(JSON.parse(readFileSync(out, 'utf8')))).toEqual(['test/four.test.ts', 'test/one.test.ts', 'test/three.test.ts', 'test/two.test.ts']);
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('a refresh from several GitHub runs takes each file\'s median, so one fast or slow run or runner cannot set the weights', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-runs-'));
try {
const bin = join(dir, 'bin');
const fixtures = join(dir, 'fixtures');
mkdirSync(bin);
mkdirSync(fixtures);
writeFileSync(join(bin, 'gh'), '#!/bin/sh\ncase "$*" in\n "run view "*" --json conclusion,headSha,jobs") cat "$FIXTURE_GH/$3.json" ;;\n "run download "*" --name timings-unit-"*) [ -f "$FIXTURE_GH/$3-$5.log" ] && cp "$FIXTURE_GH/$3-$5.log" "$7/unit.log" ;;\n *) exit 2 ;;\nesac\n', { mode: 0o755 });
const at = (ms: number) => new Date(Date.UTC(2026, 9, 30, 12) + ms).toISOString();
const line = (job: string, ms: number, text: string) => `${job}\tcapture\t${at(ms)} ${text}\n`;
const shard = (job: string, files: [string, number][]) => {
let t = 0;
let body = line(job, t, '##[gbrain-capture-start]');
for (const [file, ms] of files) { body += line(job, t, `::group::${file}:`); t += ms; }
return body + line(job, t, ' 0 fail') + line(job, t, `Ran ${files.length} tests across ${files.length} files. [1ms]`) + line(job, t, '##[gbrain-capture-complete] exit=0');
};
const run = (id: string, commit: string, files: [string, number][], conclusion = 'success') => {
writeFileSync(join(fixtures, `${id}.json`), JSON.stringify({ conclusion, headSha: commit, jobs: [{ name: 'test (1, 2)', conclusion }] }));
writeFileSync(join(fixtures, `${id}-timings-unit-1.log`), shard('test (1)', files.slice(0, 1)));
writeFileSync(join(fixtures, `${id}-timings-unit-2.log`), shard('test (2)', files.slice(1)));
};
run('101', 'commit-a', [['test/one.test.ts', 1000], ['test/two.test.ts', 9000], ['test/new.test.ts', 500]]);
run('102', 'commit-b', [['test/one.test.ts', 2000], ['test/two.test.ts', 6000]]);
// test/gone.test.ts was deleted after run 103 (git no longer tracks it), so it must not stay in the weights.
run('103', 'commit-c', [['test/one.test.ts', 4000], ['test/two.test.ts', 3000], ['test/gone.test.ts', 700]]);
run('104', 'commit-d', [['test/one.test.ts', 1], ['test/two.test.ts', 1]], 'failure');
const out = join(dir, 'weights.json');
const meta = join(dir, 'weights.metadata.json');
const env = { ...trackedCheckout(dir, ['test/one.test.ts', 'test/two.test.ts', 'test/new.test.ts']), PATH: `${bin}:${process.env.PATH}`, FIXTURE_GH: fixtures };
const mine = (...args: string[]) => spawnSync(process.execPath, [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), ...args, '--out', out], { env, encoding: 'utf8' });
writeFileSync(out, '{"test/old.test.ts":42}\n');
writeFileSync(meta, '{"prior":true}\n');
expect(mine('--run', '101', '--run', '104', '--run', '103').status).not.toBe(0);
expect(mine('--lane', 'e2e', '--e2e-profile', 'full', '--run', '101', '--run', '102').status).not.toBe(0);
expect(readFileSync(out, 'utf8')).toBe('{"test/old.test.ts":42}\n');
expect(readFileSync(meta, 'utf8')).toBe('{"prior":true}\n');
const result = mine('--run', '101', '--run', '102', '--run', '103');
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/new.test.ts': 500, 'test/one.test.ts': 2000, 'test/two.test.ts': 6000 });
expect(JSON.parse(readFileSync(meta, 'utf8'))).toMatchObject({
lane: 'unit', run: '101', commit: 'commit-a', runs: ['101', '102', '103'], commits: ['commit-a', 'commit-b', 'commit-c'],
measuredFiles: 3, totalFiles: 3, mergeExisting: false,
});
// Oldest first gives the same map: the tracked files decide what stays, not which run came first.
const oldestFirst = mine('--run', '103', '--run', '102', '--run', '101');
expect(oldestFirst.status, oldestFirst.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/new.test.ts': 500, 'test/one.test.ts': 2000, 'test/two.test.ts': 6000 });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('selected E2E from several runs keeps the median of a file only an older run selected', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-e2e-runs-'));
try {
const bin = join(dir, 'bin');
const fixtures = join(dir, 'fixtures');
mkdirSync(bin);
mkdirSync(fixtures);
writeFileSync(join(bin, 'gh'), '#!/bin/sh\ncase "$*" in\n "run view "*" --json conclusion,headSha,jobs") cat "$FIXTURE_GH/$3.json" ;;\n "run view "*" --log") cat "$FIXTURE_GH/$3.log" ;;\n *) exit 2 ;;\nesac\n', { mode: 0o755 });
const job = 'Selected E2E (diff-relevant) 1';
const line = (text: string) => job + '\tstep\t2026-10-30T12:00:00.000Z ' + text + '\n';
const run = (id: string, files: [string, number][]) => {
writeFileSync(join(fixtures, id + '.json'), JSON.stringify({ conclusion: 'success', headSha: 'commit-' + id, jobs: [{ name: job, conclusion: 'success' }] }));
writeFileSync(join(fixtures, id + '.log'), files.map(([name, ms]) => line('=== ' + name + ' ===') + line(' 0 fail') + line('Ran 1 test across 1 file. [' + ms + '.00ms]')).join('')
+ line('Files: ' + files.length + ' total, ' + files.length + ' passed, 0 failed'));
};
run('201', [['alpha.test.ts', 1000]]);
run('202', [['alpha.test.ts', 3000], ['beta.test.ts', 800]]);
const out = join(dir, 'e2e-weights.json');
writeFileSync(out, '{"test/e2e/beta.test.ts":42}\n');
const result = spawnSync(process.execPath, [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--lane', 'e2e', '--run', '201', '--run', '202', '--out', out],
{ env: { ...trackedCheckout(dir, ['test/e2e/alpha.test.ts', 'test/e2e/beta.test.ts']), PATH: bin + ':' + process.env.PATH, FIXTURE_GH: fixtures }, encoding: 'utf8' });
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/e2e/alpha.test.ts': 2000, 'test/e2e/beta.test.ts': 800 });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
it('a merging source drops the existing entry of a test file git no longer tracks (#8103)', () => {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-mine-merge-prune-'));
try {
const out = join(dir, 'weights.json');
const input = join(dir, 'serial.log');
const line = (text: string) => `serial-tests (1)\tstep\t2026-05-25T11:26:50.000Z ${text}\n`;
writeFileSync(input, line('[serial-tests] PASS 7s test/a.serial.test.ts (1pass)') + line('[serial-tests] all 1 file(s) passed in 7s (pool=4)'));
writeFileSync(out, '{"test/a.serial.test.ts":3,"test/deleted.serial.test.ts":9,"test/unobserved.serial.test.ts":42}\n');
const env = trackedCheckout(dir, ['test/a.serial.test.ts', 'test/unobserved.serial.test.ts']);
const result = spawnSync(process.execPath, [join(import.meta.dir, '../../scripts/mine-shard-weights.ts'), '--lane', 'serial', '--from-file', input, '--out', out], { env, encoding: 'utf8' });
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(out, 'utf8'))).toEqual({ 'test/a.serial.test.ts': 7, 'test/unobserved.serial.test.ts': 42 });
expect(JSON.parse(readFileSync(join(dir, 'weights.metadata.json'), 'utf8'))).toMatchObject({ measuredFiles: 1, totalFiles: 2, mergeExisting: true });
} finally { rmSync(dir, { recursive: true, force: true }); }
});
describe('medianWeights', () => {
it('takes each file\'s median over the runs that measured it, averaging the middle two of an even count', () => {
const runs = [new Map([['a', 1], ['b', 10]]), new Map([['a', 9], ['b', 30], ['c', 7]]), new Map([['a', 3]]), new Map([['a', 5], ['b', 20]])];
expect(Object.fromEntries(medianWeights(runs))).toEqual({ a: 4, b: 20, c: 7 });
expect(medianWeights([]).size).toBe(0);
});
});
describe('committed shard weights', () => {
it.each([
['scripts/test-weights.json', 'scripts/test-weights.metadata.json', 'unit', 'milliseconds'],
['scripts/serial-weights.json', 'scripts/serial-weights.metadata.json', 'serial', 'seconds'],
['scripts/e2e-weights.json', 'scripts/e2e-weights.metadata.json', 'e2e', 'milliseconds'],
])('%s matches the metadata the miner wrote beside it', (weightsPath, metadataPath, lane, unit) => {
const root = join(import.meta.dir, '..', '..');
const weights = JSON.parse(readFileSync(join(root, weightsPath), 'utf8')) as Record<string, number>;
const metadata = JSON.parse(readFileSync(join(root, metadataPath), 'utf8'));
expect(metadata).toMatchObject({ lane, unit, totalFiles: Object.keys(weights).length });
expect(metadata.runs?.length ?? 1).toBe(metadata.commits?.length ?? 1);
});
});