Files
orca/src/main/git/worktree-include-file.test.ts
T
Brennan Benson b31e9bb03d fix(worktree): bound the .worktreeinclude copy so a huge include can't freeze workspace creation (#10540)
* fix(worktree): bound the .worktreeinclude copy so a huge include can't freeze creation

`.worktreeinclude` copying was bounded in entry count (1000) but unbounded in
bytes and files, and awaited inline during worktree creation. A repo listing
`node_modules` froze creation for minutes behind the create dialog on Linux and
Windows, where the fallback is a full `fs.cp` (macOS gets a cheap APFS clone).

Measure each copy-mode source against a cumulative budget (2 GB / 50k files)
before the first byte is written, and refuse the entries that bust it. Refused
entries ride the existing `CreateWorktreeResult.warning` channel so a workspace
never silently comes up missing its included files.

Pre-measurement rather than mid-copy abort: `fs.cp` ignores its `signal`
option, so a started copy cannot be cancelled and would strand a partial tree.
Refusing up front means there is no partial state to clean up.

* fix(worktree): don't charge bytes for copy-on-write clones, and bound the sizing walk

Two defects in the copy budget, both found by review:

- The byte limit was applied on macOS, where the copy is an APFS clonefile.
  Measured: a 2.7 GB tree clones in 22 ms and consumes no disk. Refusing it on
  a 2 GB byte ceiling denied work that was already free — a regression on the
  one platform this bound was never meant to touch. Bytes are now charged only
  when a byte-for-byte copy will actually run; the volume probe that decides
  this is the same cached df+diskutil pair the clone runs, and writes nothing,
  so the "refuse before the first byte" invariant holds. The entry limit still
  applies everywhere: inodes are real work even on the clone path.

- A refused entry consumed no budget, so a `.worktreeinclude` listing many
  over-budget directories paid a fresh full-limit walk for each one — up to
  1000 x 50,000 lstat calls, re-creating the stall this bounds. The walk is now
  charged against its own ceiling whatever the verdict.

Also documents that `admit()` must be awaited sequentially (CodeRabbit).

* fix(worktree): give the sizing walk headroom so one huge entry can't starve the rest

The walk ceiling added in the previous commit was seeded with maxEntries, the
same number the entry limit uses. Sizing an entry that busts the file-count
limit walks maxEntries + 1, driving the ceiling negative, so every later
`.worktreeinclude` entry was refused without being measured at all.

That regressed the common case: a repo listing `node_modules` plus `.env` used
to get `.env`; it silently got nothing. Reproduced, and now covered by a test
that fails when the headroom is removed.

The walk now gets 5x the entry budget, so total sizing work stays bounded
(<=250k lstat per materialization, vs the 1000 x 50k this ceiling exists to
prevent) while ordinary lists never reach it. Entries refused because earlier
ones exhausted the walk report a distinct 'sizing' reason, so the warning stops
quoting size limits at a 4-byte file that was never measured.

* fix(worktree): bill a failed clone's bytes, and blame the right ceiling

Two follow-on defects from the copy-on-write fix:

- A predicted APFS clone that then failed mid-copy (EPERM, ENOSPC) fell through
  to a real `fs.cp` whose bytes were never charged, because the entry had been
  admitted on the premise that cloning is free. That reopened the unbounded
  copy on macOS. The measured size is already known, so the fallback now bills
  it and refuses if it no longer fits, reporting the entry as skipped instead
  of silently copying gigabytes. A clone that was never viable
  (ApfsCloneUnavailableError) was already charged as a real copy, so that path
  keeps falling back as before.

- The walk ceiling is also applied inside the measurement via
  min(remainingEntries, remainingWalk), and when the walk term bound, the
  refusal was still reported as 'entries' — telling the user a 3-file directory
  busted a 4-file limit. It now attributes to whichever ceiling actually bound.

Also fixes the singular warning text, which said "entry X was not copied ...
copying them would exceed ... Copy them in manually".

* fix(worktree): flag a partial clone leftover, cap the warning, cover two branches

- A clone that fails partway only removes an *empty* reservation, so leftovers
  can survive at the target. Reporting that entry as simply "not copied" sent
  the user to copy it in manually, straight into a half-populated directory.
  Those skips now carry mayBePartial and the warning says to check the path
  first. Cleaning up the leftovers stays the deferred follow-up it already was.

- The warning enumerated every skipped path. `.worktreeinclude` allows 1000
  entries and all of them can be skipped, so it now names five and counts the
  rest — an unbounded string is a poor look in a PR about bounds.

- Two load-bearing branches had no test, both proven by surviving mutants: the
  `bytesAreCopied` short-circuit (reachable when a wedged df/diskutil makes the
  volume probe answer "no clone", so bytes are charged up front and must not be
  billed twice), and chargeBytes actually consuming budget for later entries.

* fix(worktree): only flag directory clones as partial, and cap that list too

- mayBePartial was set for every refused clone fallback, but only a *directory*
  clone can leave anything behind: the file path clones into a temp name and
  publishes with link(2), so a failure leaves nothing at the target. Sending
  the user to inspect a path that does not exist is its own small lie.

- The partial-copy sentence sliced to five names without the "and N more" that
  the other sentence appends, so entries past the fifth were surfaced nowhere.
  Both sentences now share one nameList helper.
2026-07-25 05:11:03 -07:00

154 lines
5.6 KiB
TypeScript

import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { parseWorktreeIncludeFile, resolveWorktreeIncludePaths } from './worktree-include-file'
import { gitExecFileAsync } from './runner'
vi.mock('./runner', () => ({
gitExecFileAsync: vi.fn()
}))
const gitExecFileAsyncMock = vi.mocked(gitExecFileAsync)
/** check-ignore echoes back every stdin path present in `ignored` (all requested
* when unset); exit code 1 with empty stdout means "none ignored". */
function mockCheckIgnore(ignored?: string[]): void {
gitExecFileAsyncMock.mockImplementation(async (args, execOptions) => {
if (!args.includes('check-ignore')) {
throw new Error(`Unexpected git args: ${args.join(' ')}`)
}
const requested = (execOptions.stdin ?? '').split('\0').filter(Boolean)
const ignoredSet = new Set(ignored ?? requested)
const matched = requested.filter((path) => ignoredSet.has(path))
if (matched.length === 0) {
throw Object.assign(new Error('no matches'), { code: 1 })
}
return { stdout: matched.map((path) => `${path}\0`).join(''), stderr: '' }
})
}
describe('parseWorktreeIncludeFile', () => {
it('skips blank lines and comments, dedupes, strips ./ and trailing slash', () => {
const entries = parseWorktreeIncludeFile(
'# secrets\n\n.env\n \n# more\n./config/secrets.json\n.vscode/\n.env\n'
)
expect(entries).toEqual(['.env', 'config/secrets.json', '.vscode'])
})
it('normalizes backslashes to forward slashes', () => {
expect(parseWorktreeIncludeFile('apps\\web\\.env\n')).toEqual(['apps/web/.env'])
})
})
describe('resolveWorktreeIncludePaths', () => {
let repo: string
let warn: ReturnType<typeof vi.spyOn>
beforeEach(() => {
repo = mkdtempSync(join(tmpdir(), 'orca-worktreeinclude-'))
gitExecFileAsyncMock.mockReset()
warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
warn.mockRestore()
rmSync(repo, { recursive: true, force: true })
})
function writeInclude(content: string): void {
writeFileSync(join(repo, '.worktreeinclude'), content)
}
it('returns [] without spawning git when the file is absent', async () => {
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual([])
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
})
it('resolves existing gitignored literal files and directories', async () => {
writeInclude('.env\nconfig/secrets.json\n.vscode/\nmissing.txt\n')
writeFileSync(join(repo, '.env'), 'A=1')
mkdirSync(join(repo, 'config'))
writeFileSync(join(repo, 'config', 'secrets.json'), '{}')
mkdirSync(join(repo, '.vscode'))
mockCheckIgnore(['.env', 'config/secrets.json', '.vscode'])
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual([
'.env',
'.vscode',
'config/secrets.json'
])
})
it('drops listed paths that exist but are not gitignored', async () => {
writeInclude('.env\ntracked.json\n')
writeFileSync(join(repo, '.env'), 'A=1')
writeFileSync(join(repo, 'tracked.json'), '{}')
mockCheckIgnore(['.env'])
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual(['.env'])
})
it('skips a listed path that is absent from the primary checkout', async () => {
writeInclude('.env\nnode_modules\n')
writeFileSync(join(repo, '.env'), 'A=1')
mockCheckIgnore(['.env'])
// node_modules absent (not installed yet) → not stat-able → not requested from git.
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual(['.env'])
})
it('resolves a gitignored symlink entry without following it', async () => {
writeInclude('.env\n')
writeFileSync(join(repo, '.env.real'), 'A=1')
symlinkSync(join(repo, '.env.real'), join(repo, '.env'))
mockCheckIgnore(['.env'])
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual(['.env'])
})
it('skips glob and negation entries with a warning', async () => {
writeInclude('.env.*\n!.env.production\n.env\n')
writeFileSync(join(repo, '.env'), 'A=1')
mockCheckIgnore(['.env'])
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual(['.env'])
expect(warn).toHaveBeenCalledWith(expect.stringContaining('unsupported'))
})
it('rejects traversal, absolute, and .git entries', async () => {
writeInclude('../outside\n/etc/passwd\n.git/config\n.env\n')
writeFileSync(join(repo, '.env'), 'A=1')
mockCheckIgnore(['.env'])
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual(['.env'])
expect(warn).toHaveBeenCalledWith(expect.stringContaining('unsafe'))
})
it('stops after 1000 entries so one repo file cannot request unbounded work', async () => {
const names = Array.from({ length: 1001 }, (_, index) => `ignored-${index}.env`)
for (const name of names) {
writeFileSync(join(repo, name), 'A=1')
}
writeInclude(`${names.join('\n')}\n`)
mockCheckIgnore()
const resolved = await resolveWorktreeIncludePaths(repo)
expect(resolved).toHaveLength(1000)
expect(warn).toHaveBeenCalledWith(expect.stringContaining('more than 1000 entries'))
})
it('resolves to [] when git fails instead of throwing', async () => {
writeInclude('.env\n')
writeFileSync(join(repo, '.env'), 'A=1')
gitExecFileAsyncMock.mockRejectedValue(new Error('git exploded'))
await expect(resolveWorktreeIncludePaths(repo)).resolves.toEqual([])
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Failed to resolve'),
expect.any(Error)
)
})
})