/sync-gbrain's read check works when the first page is over 64 KB (#4396) (#4497)

/sync-gbrain's read check no longer reports `unknown` when the first
page in your code source is over 64 KB.

Why: the check read the whole page to confirm its source and slug, and
big pages (lockfiles) hit the 64 KiB output limit on every run.

Nothing to do: it now reads a 4 KB window of the page and still treats
any cut-off output as `unknown`.

Co-authored-by: nfitzsimmons <2198211+nfitzsimmons@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=testing
This commit is contained in:
authored and GitHub committed 2026-11-18 17:08:51 +00:00
1 parent 0ab5657112
commit d789dd965b
7 files changed
+37 -11

No files matched your search

+28 -6
View File
@@ -7,7 +7,9 @@ import { spawnSync } from 'node:child_process';
const roots: string[] = [];
afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
function fixture(opts: { state?: unknown; pin?: string; registration?: unknown | ((repo: string) => unknown); slug?: string; list?: string; get?: unknown; fail?: string; gitExecutable?: string } = {}) {
const WINDOW = '--body window --max-bytes 4096';
function fixture(opts: { state?: unknown; pin?: string; registration?: unknown | ((repo: string) => unknown); slug?: string; list?: string; get?: unknown; window?: unknown; pageBytes?: number; legacy?: boolean; fail?: string; gitExecutable?: string } = {}) {
const root = mkdtempSync(join(tmpdir(), 'gbrain-read-'));
roots.push(root);
const repo = join(root, 'repo');
@@ -31,10 +33,11 @@ function fixture(opts: { state?: unknown; pin?: string; registration?: unknown |
import { appendFileSync } from 'node:fs';
appendFileSync(${JSON.stringify(log)}, process.argv.slice(2).join(' ') + '\\n');
const args = process.argv.slice(2).join(' ');
if (args === ${JSON.stringify(opts.fail)}) process.exit(2);
if (${JSON.stringify(opts.fail ?? null)} !== null && args.startsWith(${JSON.stringify(opts.fail ?? '')})) process.exit(2);
if (args === 'sources list --json') console.log(${JSON.stringify(JSON.stringify(registration ?? { sources: [{ id: source, local_path: repo }] }))});
else if (args === 'list --source client-repo --limit 1') console.log(${JSON.stringify(opts.list ?? `${slug}\tcode\t2026-09-24\tReadme`)});
else if (args === ${JSON.stringify(`get ${slug} --source client-repo --json`)}) console.log(${JSON.stringify(JSON.stringify(opts.get ?? { source_id: source, slug }))});
else if (args === ${JSON.stringify(`get ${slug} --source client-repo --json ${WINDOW}`)} && ${!opts.legacy}) console.log(${JSON.stringify(JSON.stringify(opts.window ?? opts.get ?? { source_id: source, slug, body: 'x'.repeat(Math.min(opts.pageBytes ?? 8, 4096)) }))});
else if (args === ${JSON.stringify(`get ${slug} --source client-repo --json`)}) console.log(${JSON.stringify(JSON.stringify(opts.get ?? { source_id: source, slug, compiled_truth: 'x'.repeat(opts.pageBytes ?? 8) }))});
else process.exit(3);
`);
chmodSync(join(bin, 'gbrain'), 0o755);
@@ -53,7 +56,7 @@ test('source-scoped read validates a pretty sync state, registration, and matchi
const result = f.run();
expect(result.result.status).toBe(0);
expect(result.value.status).toBe('ready');
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1', 'get code/repo/readme --source client-repo --json']);
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1', `get code/repo/readme --source client-repo --json ${WINDOW}`]);
});
test('symlink-equivalent registered worktree passes but another source path fails closed', () => {
@@ -70,7 +73,7 @@ test('symlink-equivalent registered worktree passes but another source path fail
writeFileSync(join(f.repo, '.gbrain-source'), 'other-source\n');
const result = f.run();
expect(result.value.status).toBe('unknown');
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1', 'get code/repo/readme --source client-repo --json']);
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1', `get code/repo/readme --source client-repo --json ${WINDOW}`]);
});
test('rejects the former header-only and multi-row TSV response before get', () => {
@@ -95,7 +98,7 @@ test('accepts a bounded literal-Unicode code slug', () => {
const slug = 'code/路径/mañana🌳.ts';
const result = fixture({ slug }).run();
expect(result.value.status).toBe('ready');
expect(result.calls.at(-1)).toBe(`get ${slug} --source client-repo --json`);
expect(result.calls.at(-1)).toBe(`get ${slug} --source client-repo --json ${WINDOW}`);
});
test.each(['--help', 'code/a/../b', 'code/./b', 'code/\u0007bad', 'a'.repeat(513)])('rejects unsafe slug %j before get', slug => {
@@ -104,6 +107,25 @@ test.each(['--help', 'code/a/../b', 'code/./b', 'code/\u0007bad', 'a'.repeat(513
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1']);
});
test('a first page over 64 KB reads ready through a bounded window', () => {
const result = fixture({ pageBytes: 300_000 }).run();
expect(result.value.status).toBe('ready');
expect(result.calls).toEqual(['sources list --json', 'list --source client-repo --limit 1', `get code/repo/readme --source client-repo --json ${WINDOW}`]);
});
test('a window response over the read limit is unknown, never a truncated ready', () => {
const result = fixture({ pageBytes: 300_000, window: { source_id: 'client-repo', slug: 'code/repo/readme', body: 'x'.repeat(300_000) } }).run();
expect(result.value.status).toBe('unknown');
expect(result.calls).toHaveLength(4);
});
test('a gbrain without body windows falls back to the whole-page read under the same limit', () => {
const small = fixture({ legacy: true }).run();
expect(small.value.status).toBe('ready');
expect(small.calls.slice(2)).toEqual([`get code/repo/readme --source client-repo --json ${WINDOW}`, 'get code/repo/readme --source client-repo --json']);
expect(fixture({ legacy: true, pageBytes: 300_000 }).run().value.status).toBe('unknown');
});
test('rejects a JSON error envelope with matching source and slug', () => {
const result = fixture({ get: { source_id: 'client-repo', slug: 'code/repo/readme', content: '# Readme', error: { code: 'not_found' } } }).run();
expect(result.value.status).toBe('unknown');
@@ -37,7 +37,7 @@ const args = process.argv.slice(2).join(' ');
appendFileSync(${JSON.stringify(log)}, args + '\\n');
if (args === 'sources list --json') console.log(${JSON.stringify(JSON.stringify({ sources: [{ id: 'client-fixture', local_path: workDir }] }))});
else if (args === 'list --source client-fixture --limit 1') console.log('code/fixture/readme\\tcode\\t2026-09-24\\tReadme');
else if (args === 'get code/fixture/readme --source client-fixture --json') ${kind === 'ready'
else if (args.startsWith('get code/fixture/readme --source client-fixture --json')) ${kind === 'ready'
? `console.log(${JSON.stringify(JSON.stringify({ source_id: 'client-fixture', slug: 'code/fixture/readme', content: '# Readme' }))});`
: "{ console.error('temporary read failure'); process.exit(2); }"}
else { console.error('unsupported operation'); process.exit(3); }
+2 -1
View File
@@ -24,7 +24,8 @@ async function runReadiness(kind: 'ready' | 'unknown', name: string) {
expect(fixture.calls()).toEqual([
'sources list --json',
'list --source client-fixture --limit 1',
'get code/fixture/readme --source client-fixture --json',
'get code/fixture/readme --source client-fixture --json --body window --max-bytes 4096',
...(kind === 'unknown' ? ['get code/fixture/readme --source client-fixture --json'] : []),
]);
const content = fixture.content();
expect(fixture.sourceIntact()).toBe(true);
+2 -1
View File
@@ -14,7 +14,8 @@ for (const kind of ['ready', 'unknown'] as const) {
expect(JSON.parse(check.stdout).status).toBe(kind);
expect(fixture.calls()).toEqual([
'sources list --json', 'list --source client-fixture --limit 1',
'get code/fixture/readme --source client-fixture --json',
'get code/fixture/readme --source client-fixture --json --body window --max-bytes 4096',
...(kind === 'unknown' ? ['get code/fixture/readme --source client-fixture --json'] : []),
]);
expect(fixture.sourceIntact()).toBe(true);
} finally { fixture.cleanup(); }