Files
orca/src/main/git/runner-windows-host-environment.test.ts
T
Brennan BensonandMerge Sim b5a85890ac perf(git): bound git subprocess execution with an atomic admission scheduler (#16874)
* perf(git): bound git subprocess execution with an atomic admission scheduler

Field traces (#16038, #11363) show Windows freeze storms driven by unbounded
concurrent git children (12+ at once, 50-65s status convoys for 25+ minutes).
Admit every main-process git child against atomic per-budget base+headroom
counters (general / network / per-route), with reserved interactive capacity,
ordering-only aging, close-bound permit release, a 120s fail-safe read timeout
that feeds scheduler backoff, tier plumbing through every option carrier, and
coalesced+jittered visibility pollers. Killswitch: ORCA_GIT_ADMISSION_DISABLED=1.

Storm harness A/B: max concurrent children 65 -> 6, interactive p95 791ms -> 88ms;
output-parity battery byte-identical with admission on vs off.

* test(git): run the admission output-parity battery on every platform

Parity needs real git, not the storm harness's PATH stub, so it must not share
that file's POSIX gate - Windows is the platform where parity evidence matters.

* fix(git): preserve interactive admission invariants

* perf(git): keep admission queue drains linear

* fix(git): close final admission gaps

* perf(git): bound eligible route selection

* fix(merge): remove unrelated stale snapshot changes

* fix(git): preserve refresh lifecycle authority

* test(git): align admission lifetime contracts

* fix(git): harden admission across runtime paths

* fix(git): restore freshness for bulk status reads

* test(git): repoint delete-dialog source pins after admission plumbing

The hydration effect now orders its targets through
orderDeleteWorktreeStatusHydrationTargets and passes includeLineStats
alongside the abort signal, so both literal anchors stopped matching.
The invariants are unchanged and still pinned: dropping the signal, the
main-worktree/folder filter, or getState-instead-of-subscribe each
still reddens this test.

* Fix git admission tier propagation and lock ordering

Decode optional Git status tiers permissively and default runtime RPC status reads to the status lane while preserving renderer caller intent.

Acquire the FETCH_HEAD mutex before atomic admission so same-repository fetch waiters hold no global or route permits.

Preserve automatic pull-request refresh reasons, keep explicit hosted-review refreshes interactive, remove the dead candidate tier, and keep relay scheduling unchanged.

Use tier-aware status lease keys because a shared lease cannot be safely promoted after its admission request is queued or granted.

* test: align expectations with admission plumbing

* refactor(child-process): move the process contract types to process-spec

run-process.ts crossed its line cap after gaining the termination observer;
the public types and defaults move out with re-exports so no caller changes.

* chore: restore pnpm-lock.yaml to main (unintended local drift)

---------

Co-authored-by: Merge Sim <sim@local>
2026-08-30 14:19:05 -07:00

269 lines
8.8 KiB
TypeScript

import { EventEmitter } from 'node:events'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const { execFileMock, spawnMock } = vi.hoisted(() => ({
execFileMock: vi.fn(),
spawnMock: vi.fn()
}))
vi.mock('node:child_process', () => ({
execFile: execFileMock,
execFileSync: vi.fn(),
spawn: spawnMock
}))
import {
awaitWindowsHostGitEnvironmentReady,
configureWindowsHostGitEnvironmentReadiness,
gitExecFileAsync,
gitExecFileAsyncBuffer,
gitSpawnAfterWindowsEnvironmentReady,
gitStreamStdout
} from './runner'
import { _resetGitAdmissionForTests } from './command-runner/git-subprocess-admission'
type MockChildProcess = EventEmitter & {
stdout: EventEmitter
stderr: EventEmitter
pid: number
kill: ReturnType<typeof vi.fn>
}
function createMockChildProcess(pid: number): MockChildProcess {
const child = new EventEmitter() as MockChildProcess
child.stdout = new EventEmitter()
child.stderr = new EventEmitter()
child.pid = pid
child.kill = vi.fn()
return child
}
function deferred(): { promise: Promise<void>; resolve: () => void } {
let resolve!: () => void
const promise = new Promise<void>((next) => {
resolve = next
})
return { promise, resolve }
}
async function withPlatform<T>(platform: NodeJS.Platform, fn: () => Promise<T>): Promise<T> {
const original = process.platform
Object.defineProperty(process, 'platform', { configurable: true, value: platform })
try {
return await fn()
} finally {
Object.defineProperty(process, 'platform', { configurable: true, value: original })
}
}
describe('Windows host Git environment readiness', () => {
const originalPath = process.env.Path
beforeEach(() => {
execFileMock.mockReset()
spawnMock.mockReset()
})
afterEach(() => {
_resetGitAdmissionForTests()
configureWindowsHostGitEnvironmentReadiness(null)
if (originalPath === undefined) {
delete process.env.Path
} else {
process.env.Path = originalPath
}
})
it('waits for each native operation and refreshes a snapshotted PATH', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const waitUntilReady = vi.fn(() => ready.promise)
const child = createMockChildProcess(1234)
execFileMock.mockImplementation((_cmd, _args, _options, callback) => {
callback(null, 'ok', '')
return child
})
configureWindowsHostGitEnvironmentReadiness(waitUntilReady)
process.env.Path = 'hydrating-path'
const firstExec = gitExecFileAsync(['status'], {
cwd: String.raw`C:\repo`,
env: { Path: 'stale-path' }
})
expect(waitUntilReady).toHaveBeenCalledOnce()
expect(execFileMock).not.toHaveBeenCalled()
process.env.Path = 'hydrated-path'
ready.resolve()
await expect(firstExec).resolves.toEqual({ stdout: 'ok', stderr: '' })
expect(execFileMock.mock.calls[0]?.[2]?.env?.Path).toBe('hydrated-path')
await expect(gitExecFileAsync(['status'], { cwd: String.raw`C:\repo` })).resolves.toEqual({
stdout: 'ok',
stderr: ''
})
expect(waitUntilReady).toHaveBeenCalledTimes(2)
})
})
it('waits before native spawned Git and refreshes a snapshotted PATH', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const waitUntilReady = vi.fn(() => ready.promise)
const child = createMockChildProcess(1234)
spawnMock.mockReturnValue(child)
configureWindowsHostGitEnvironmentReadiness(waitUntilReady)
process.env.Path = 'hydrating-path'
const spawned = gitSpawnAfterWindowsEnvironmentReady(['clone', 'url', 'repo'], {
cwd: String.raw`C:\repo`,
env: { Path: 'stale-path' },
stdio: ['ignore', 'ignore', 'pipe']
})
expect(waitUntilReady).toHaveBeenCalledOnce()
expect(spawnMock).not.toHaveBeenCalled()
process.env.Path = 'hydrated-path'
ready.resolve()
await expect(spawned).resolves.toBe(child)
expect(spawnMock.mock.calls[0]?.[2]?.env?.Path).toBe('hydrated-path')
})
})
it('does not gate spawned Git routed through WSL on the Windows host profile', async () => {
await withPlatform('win32', async () => {
const waitUntilReady = vi.fn(() => new Promise<void>(() => {}))
const child = createMockChildProcess(1234)
spawnMock.mockReturnValue(child)
configureWindowsHostGitEnvironmentReadiness(waitUntilReady)
await expect(
gitSpawnAfterWindowsEnvironmentReady(['clone', 'url', 'repo'], {
cwd: String.raw`C:\repo`,
wslDistro: 'Ubuntu',
stdio: ['ignore', 'ignore', 'pipe']
})
).resolves.toBe(child)
expect(waitUntilReady).not.toHaveBeenCalled()
expect(spawnMock).toHaveBeenCalled()
})
})
it('does not spawn Git when canceled during Windows environment readiness', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const controller = new AbortController()
configureWindowsHostGitEnvironmentReadiness(() => ready.promise)
const spawned = gitSpawnAfterWindowsEnvironmentReady(['status'], {
cwd: String.raw`C:\repo`,
signal: controller.signal,
stdio: ['ignore', 'ignore', 'pipe']
})
controller.abort()
await expect(spawned).rejects.toMatchObject({ name: 'AbortError' })
expect(spawnMock).not.toHaveBeenCalled()
ready.resolve()
})
})
it('cancels async Git immediately while Windows environment readiness is pending', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const controller = new AbortController()
configureWindowsHostGitEnvironmentReadiness(() => ready.promise)
const operation = gitExecFileAsync(['status'], {
cwd: String.raw`C:\repo`,
signal: controller.signal
})
controller.abort()
await expect(operation).rejects.toMatchObject({ name: 'AbortError' })
expect(execFileMock).not.toHaveBeenCalled()
ready.resolve()
})
})
it('does not gate WSL paths at a synchronous Git consumer boundary', async () => {
await withPlatform('win32', async () => {
const waitUntilReady = vi.fn(() => new Promise<void>(() => {}))
configureWindowsHostGitEnvironmentReadiness(waitUntilReady)
await expect(
awaitWindowsHostGitEnvironmentReady({
cwd: String.raw`\\wsl.localhost\Ubuntu\home\me\repo`
})
).resolves.toBeUndefined()
expect(waitUntilReady).not.toHaveBeenCalled()
})
})
it('does not gate Git routed through WSL on the Windows host profile', async () => {
await withPlatform('win32', async () => {
const waitUntilReady = vi.fn(() => new Promise<void>(() => {}))
const child = createMockChildProcess(1234)
execFileMock.mockImplementation((_cmd, _args, _options, callback) => {
callback(null, 'ok', '')
return child
})
configureWindowsHostGitEnvironmentReadiness(waitUntilReady)
await expect(
gitExecFileAsync(['status'], {
cwd: String.raw`C:\repo`,
wslDistro: 'Ubuntu'
})
).resolves.toEqual({ stdout: 'ok', stderr: '' })
expect(waitUntilReady).not.toHaveBeenCalled()
expect(execFileMock).toHaveBeenCalled()
})
})
it('gates buffered native Git reads on the current generation', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const child = createMockChildProcess(1234)
execFileMock.mockImplementation((_cmd, _args, _options, callback) => {
callback(null, Buffer.from('blob'), Buffer.alloc(0))
return child
})
configureWindowsHostGitEnvironmentReadiness(() => ready.promise)
const read = gitExecFileAsyncBuffer(['show', 'HEAD:file'], { cwd: String.raw`C:\repo` })
expect(execFileMock).not.toHaveBeenCalled()
ready.resolve()
await expect(read).resolves.toEqual({ stdout: Buffer.from('blob') })
})
})
it('gates native Git streams and refreshes their snapshotted PATH', async () => {
await withPlatform('win32', async () => {
const ready = deferred()
const child = createMockChildProcess(1234)
spawnMock.mockReturnValue(child)
configureWindowsHostGitEnvironmentReadiness(() => ready.promise)
process.env.Path = 'hydrated-stream-path'
const stream = gitStreamStdout(['status'], {
cwd: String.raw`C:\repo`,
env: { Path: 'stale-stream-path' },
onStdout: () => {}
})
expect(spawnMock).not.toHaveBeenCalled()
ready.resolve()
await vi.waitFor(() => expect(spawnMock).toHaveBeenCalledOnce())
expect(spawnMock.mock.calls[0]?.[2]?.env?.Path).toBe('hydrated-stream-path')
child.emit('close', 0)
await expect(stream).resolves.toEqual({ stoppedEarly: false })
})
})
})