Skip to content

Commit b8f8bb5

Browse files
authored
Merge pull request #18 from OnPoint-Dev-Tools/dev
fix: prevent provider discovery from suspending releases
2 parents 07f74c9 + 2298ebf commit b8f8bb5

4 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ Release npm verification/version subprocesses must remain non-interactive with s
201201
disconnected and CI mode enabled so nested commands cannot suspend a release through
202202
shell job control. Keep Git fetch/push on the separate interactive path for credential
203203
helpers, and never infer release success until the version commit, tag, and push are
204-
all observed. See `docs/releasing.md`.
204+
all observed. Provider-discovery shell probes must have piped I/O and a separate
205+
process session so shell profiles cannot take the caller's controlling terminal;
206+
discovery tests must mock those probes. See `docs/releasing.md`.
205207

206208
Automatic provider-usage probes must treat an observed CLI crash differently from an
207209
ordinary unavailable response. Cool down the crashing interactive fallback so window

‎docs/releasing.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ configured credential helpers can still authenticate. A verification command tha
4141
needs input must fail explicitly rather than leaving a stopped release job that could
4242
later resume and mutate version/tag state.
4343

44+
Provider-discovery tests mock shell probes rather than loading the owner's shell
45+
profiles. Runtime login/interactive discovery probes run in a separate process
46+
session with piped I/O: disconnected stdin alone does not remove access to the
47+
controlling terminal through `/dev/tty`. This prevents a shell profile from taking
48+
terminal custody or suspending the release test process group with `SIGTTIN`.
49+
4450
CrewCode's desktop usage indicators probe installed provider CLIs independently of
4551
the release command. If Claude's interactive usage process exits from a crash signal,
4652
CrewCode pauses that fallback for ten minutes. This prevents focus-driven refreshes

‎src/main/headless-agent-resolver.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
import { afterEach, describe, expect, it, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { join } from 'node:path'
3+
import { EventEmitter } from 'node:events'
4+
import { PassThrough } from 'node:stream'
5+
6+
function shellProbe(output = '', code = 0) {
7+
const child = Object.assign(new EventEmitter(), { stdout: new PassThrough() })
8+
queueMicrotask(() => {
9+
child.stdout.end(output)
10+
child.emit('close', code)
11+
})
12+
return child
13+
}
14+
15+
const spawn = vi.fn((_shell: string, _args: string[], _options: { detached?: boolean; timeout?: number }) => shellProbe())
16+
const spawnSync = vi.fn((_shell: string, _args: string[], _options: { detached?: boolean; timeout?: number }) => ({ stdout: '', status: 0 }))
17+
18+
beforeEach(() => {
19+
spawn.mockReset()
20+
spawn.mockImplementation(() => shellProbe())
21+
spawnSync.mockClear()
22+
// Discovery tests must not execute the owner's login/interactive shell profiles.
23+
vi.doMock('child_process', async importOriginal => ({
24+
...await importOriginal<typeof import('child_process')>(),
25+
spawn,
26+
spawnSync,
27+
}))
28+
})
329

430
afterEach(() => {
31+
vi.doUnmock('child_process')
32+
vi.doUnmock('./agents/model-detect')
533
vi.doUnmock('fs')
634
vi.doUnmock('os')
735
vi.resetModules()
@@ -32,9 +60,43 @@ describe('headless agent registry', () => {
3260
available: true,
3361
path: expectedPath,
3462
})
63+
expect(spawnSync).not.toHaveBeenCalled()
64+
if (process.platform !== 'win32') {
65+
expect(spawn).toHaveBeenCalled()
66+
for (const [, , options] of spawn.mock.calls) {
67+
expect(options).toMatchObject({ detached: true, timeout: 3_000 })
68+
}
69+
}
70+
})
71+
72+
it.skipIf(process.platform === 'win32')('retains interactive-shell discovery without caller terminal access', async () => {
73+
const expectedPath = join('/shell', 'codex')
74+
vi.doMock('fs', async importOriginal => {
75+
const actual = await importOriginal<typeof import('fs')>()
76+
return {
77+
...actual,
78+
promises: { ...actual.promises, access: vi.fn(async (candidate: string) => {
79+
if (candidate !== expectedPath) throw new Error('missing')
80+
}) },
81+
}
82+
})
83+
spawn.mockImplementation((_shell, args) => shellProbe(
84+
args[0] === '-ic' && args[1] === 'command -v codex' ? `${expectedPath}\n` : '',
85+
))
86+
87+
const { headlessAgentRegistry } = await import('./headless-agent-resolver')
88+
const registry = await headlessAgentRegistry()
89+
expect(registry.find(agent => agent.id === 'codex')).toMatchObject({ available: true, path: expectedPath })
90+
expect(spawn).toHaveBeenCalledWith(expect.any(String), ['-ic', 'command -v codex'], expect.objectContaining({
91+
detached: true, stdio: ['ignore', 'pipe', 'ignore'], timeout: 3_000,
92+
}))
3593
})
3694

3795
it('lists models from the same provider CLIs as desktop', async () => {
96+
vi.doMock('fs', async importOriginal => ({
97+
...await importOriginal<typeof import('fs')>(),
98+
existsSync: () => false,
99+
}))
38100
vi.doMock('./agents/model-detect', () => ({
39101
listModels: vi.fn(async (provider: string) => (
40102
provider === 'claude'
@@ -47,5 +109,11 @@ describe('headless agent registry', () => {
47109
{ id: 'claude-sonnet-4-6', label: 'Sonnet', provider: 'anthropic' },
48110
])
49111
expect(await listHeadlessAgentModels('codex')).toEqual([])
112+
if (process.platform !== 'win32') {
113+
expect(spawnSync).toHaveBeenCalled()
114+
for (const [, , options] of spawnSync.mock.calls) {
115+
expect(options).toMatchObject({ detached: true, timeout: 3_000 })
116+
}
117+
}
50118
})
51119
})

‎src/main/headless-agent-resolver.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, constants as fsConstants, promises as fsp } from 'fs'
22
import { homedir } from 'os'
33
import { delimiter, join } from 'path'
4-
import { execFile, spawnSync } from 'child_process'
4+
import { spawn, spawnSync } from 'child_process'
55
import { listModels } from './agents/model-detect'
66

77
const PROVIDER_COMMANDS: Record<string, string> = {
@@ -79,8 +79,19 @@ function shellResolve(command: string, flag: '-lc' | '-ic'): Promise<string | nu
7979
if (process.platform === 'win32') return Promise.resolve(null)
8080
const shell = process.env.SHELL || '/bin/sh'
8181
return new Promise(resolve => {
82-
execFile(shell, [flag, `command -v ${command}`], { encoding: 'utf8', timeout: 3_000, windowsHide: true }, (_error, stdout) => {
83-
const path = stdout?.trim().split('\n').pop()?.trim()
82+
// Piped stdin alone leaves /dev/tty accessible to interactive shell profiles.
83+
const child = spawn(shell, [flag, `command -v ${command}`], {
84+
stdio: ['ignore', 'pipe', 'ignore'], detached: true,
85+
timeout: 3_000, killSignal: 'SIGKILL', windowsHide: true,
86+
})
87+
let stdout = ''
88+
child.stdout?.setEncoding('utf8')
89+
child.stdout?.on('data', (chunk: string) => {
90+
stdout = (stdout + chunk).slice(-16_384)
91+
})
92+
child.on('error', () => resolve(null))
93+
child.on('close', code => {
94+
const path = code === 0 ? stdout.trim().split('\n').pop()?.trim() : null
8495
resolve(path || null)
8596
})
8697
})
@@ -106,8 +117,10 @@ export function resolveHeadlessAgentPath(provider: string): string | null {
106117
for (const candidate of candidatePaths(command)) if (candidate && existsSync(candidate)) return candidate
107118
if (process.platform !== 'win32') {
108119
for (const flag of ['-lc', '-ic'] as const) {
109-
const result = spawnSync(process.env.SHELL || '/bin/sh', [flag, `command -v ${command}`], { encoding: 'utf8', timeout: 3_000 })
110-
const resolved = result.stdout?.trim().split('\n').pop()?.trim()
120+
// Shell startup must not take terminal custody from the caller.
121+
const options = { encoding: 'utf8' as const, timeout: 3_000, detached: true, killSignal: 'SIGKILL' as const }
122+
const result = spawnSync(process.env.SHELL || '/bin/sh', [flag, `command -v ${command}`], options)
123+
const resolved = result.status === 0 ? result.stdout?.trim().split('\n').pop()?.trim() : null
111124
if (resolved && existsSync(resolved)) return resolved
112125
}
113126
}

0 commit comments

Comments
 (0)