diff --git a/src/git.ts b/src/git.ts index ebcc3655..f2efb163 100644 --- a/src/git.ts +++ b/src/git.ts @@ -305,6 +305,13 @@ export function runGit(cwd: string, args: string[], timeoutMs = 30_000, options: reject(new GitCommandError(`git ${args[0] ?? ''} timed out after ${timeoutMs}ms`, 'git-error', args.join(' '))) }, timeoutMs) child.stdout.on('data', (chunk: Buffer) => { + // The caller's row cap already decided the answer: ignore everything + // that arrives afterwards. Without this guard the retained output kept + // growing by whatever the killed child had already buffered into the + // pipe, so "capped" was only a claim about the kill and not a bound on + // what the caller receives (CI: a 125 KB answer plus a fast child made + // the capped call return all 2,500 rows). + if (fieldCapped) return stdout += chunk.toString('utf8') outBytes += chunk.length if (outBytes > maxBytes) { diff --git a/tests/git-spawn.spec.ts b/tests/git-spawn.spec.ts index 5f5c3808..3de0d9c4 100644 --- a/tests/git-spawn.spec.ts +++ b/tests/git-spawn.spec.ts @@ -6,7 +6,7 @@ const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() })) vi.mock('node:child_process', () => ({ spawn: spawnMock })) -import { isGitRepo, worktrees } from '../src/git.ts' +import { isGitRepo, runGit, worktrees } from '../src/git.ts' afterEach(() => { spawnMock.mockReset() @@ -100,3 +100,65 @@ describe('git subprocess spawning', () => { expect(nulAttempts).toHaveLength(1) }) }) + +/** + * The two output caps, pinned against a MOCKED child — the only way to make + * "production stopped" deterministic. Against a real `git` the answer is a + * race: the killed child may already have written its whole output into the + * pipe, so the retained text legitimately still contained every row (CI red: + * a capped 2,500-row status returned all 2,500 fields). Here we control both + * the crossing chunk AND the tail that arrives after the kill. + */ +describe('git output caps', () => { + /** A child whose stdout is driven by the test, with a spy on `kill`. */ + function controlledChild(script: (stdout: PassThrough, child: EventEmitter) => void): { + spawn: () => EventEmitter + kill: ReturnType + } { + const kill = vi.fn() + return { + kill, + spawn: () => { + const child = new EventEmitter() + const stdout = new PassThrough() + const stderr = new PassThrough() + Object.assign(child, { stdout, stderr, kill }) + queueMicrotask(() => { script(stdout, child) }) + return child + }, + } + } + + it('stops retaining output once the NUL field cap is reached', async () => { + const field = (letter: string): string => `${letter.repeat(10)}\0` + const child = controlledChild((stdout, proc) => { + stdout.write(field('a').repeat(3)) // crosses the cap of 2 → kill + // The killed child's already-buffered tail must NOT reach the caller. + setImmediate(() => { + stdout.write(field('b').repeat(50)) + stdout.end() + proc.emit('close', null) // killed by a signal, not a clean exit + }) + }) + spawnMock.mockImplementation(child.spawn) + + const out = await runGit('/repo', ['status'], 30_000, { maxNulFields: 2 }) + + expect(child.kill).toHaveBeenCalledTimes(1) + expect(out).toBe(field('a').repeat(3)) + expect(out).not.toContain('bbbbbbbbbb') + }) + + it('kills the child and rejects when stdout exceeds the byte cap', async () => { + const child = controlledChild((stdout, proc) => { + stdout.write(Buffer.alloc(64, 0x78)) + stdout.end() + proc.emit('close', 0) + }) + spawnMock.mockImplementation(child.spawn) + + await expect(runGit('/repo', ['log'], 30_000, { maxBytes: 32 })) + .rejects.toThrow(/output exceeded 32 bytes/) + expect(child.kill).toHaveBeenCalledTimes(1) + }) +}) diff --git a/tests/git-worktree.spec.ts b/tests/git-worktree.spec.ts index 8664d7bc..94caf614 100644 --- a/tests/git-worktree.spec.ts +++ b/tests/git-worktree.spec.ts @@ -255,10 +255,15 @@ describe('linked Git worktrees', () => { git(root, ['init', '-q']) // Long names, so the status answer spans several 64 KiB pipe reads: the // cap is checked per chunk, which is exactly how a runaway untracked - // walk is cut off mid-stream. + // walk is cut off mid-stream. They are long enough that ONE 64 KiB chunk + // carries at most ~500 fields, so "the capped answer is shorter than the + // whole one" is a real bound (cap 1,000 plus at most that one crossing + // chunk) rather than a race against the kill: `runGit` ignores every + // chunk that arrives after the cap trips. The kill itself is pinned + // deterministically against a mocked child in tests/git-spawn.spec.ts. const files = 2_500 for (let index = 0; index < files; index += 1) { - writeFileSync(join(root, `f${index}-${'x'.repeat(40)}.ts`), 'x') + writeFileSync(join(root, `f${index}-${'x'.repeat(120)}.ts`), 'x') } const args = ['status', '--porcelain=v1', '-z', '--untracked-files=all'] const all = await runGit(root, args)