Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-11679]): bound provider CLI test probes (#11718)
[._.]/e4d4060bfd5e
diff --git a/server/lib/aiToolkit/internal/providerCatalogService.js b/server/lib/aiToolkit/internal/providerCatalogService.jsindex 2886db7265..87347ff82e 100644--- a/server/lib/aiToolkit/internal/providerCatalogService.js+++ b/server/lib/aiToolkit/internal/providerCatalogService.js@@ -2,7 +2,7 @@ import { readFile, readdir } from 'fs/promises';import { existsSync } from 'fs';import { homedir } from 'os';import { join } from 'path';-import { execFile } from 'child_process';+import { execFile, spawn } from 'child_process';import { promisify } from 'util';import { assertSecretEndpoint, evaluateSecretEndpoint } from '../endpointGuard.js';import { composeBootstrapSpawn } from './credentialBootstrap.js';@@ -22,6 +22,168 @@ function resolveProbeSpawn(provider, defaultBin, args) {const execFileAsync = (file, args, options) =>promisify(execFile)(file, args, { windowsHide: true, ...options });+const CLI_COMMAND_LOOKUP_TIMEOUT_MS = 5_000;+const CLI_VERSION_PROBE_TIMEOUT_MS = 10_000;+const PROBE_CLEANUP_CONFIRM_TIMEOUT_MS = 1_000;+const WINDOWS_TREE_KILL_TIMEOUT_MS = 2_000;++function destroyProbeStreams(child) {+ for (const stream of [child?.stdin, child?.stdout, child?.stderr]) {+ try { stream?.destroy(); } catch { /* the stream may already be closed */ }+ }+}++function waitForProbeGroupExit(pid) {+ return new Promise((resolve) => {+ const deadline = Date.now() + PROBE_CLEANUP_CONFIRM_TIMEOUT_MS;+ const check = () => {+ try {+ process.kill(-pid, 0);+ } catch (err) {+ if (err?.code === 'ESRCH') return resolve(true);+ return resolve(false);+ }+ if (Date.now() >= deadline) return resolve(false);+ const timer = setTimeout(check, 25);+ timer.unref?.();+ };+ check();+ });+}++function killWindowsProbeTree(child, directExited = false) {+ if (!child?.pid) return Promise.resolve(false);+ if (directExited || child.exitCode != null || child.signalCode != null) {+ destroyProbeStreams(child);+ return Promise.resolve(false);+ }+ return new Promise((resolve) => {+ let settled = false;+ let killer;+ let timer;+ const finish = (confirmed) => {+ if (settled) return;+ settled = true;+ clearTimeout(timer);+ destroyProbeStreams(child);+ resolve(confirmed);+ };+ try {+ killer = spawn('taskkill', ['/T', '/F', '/PID', String(child.pid)], {+ shell: false,+ stdio: 'ignore',+ windowsHide: true,+ });+ } catch {+ try { child.kill('SIGKILL'); } catch { /* already gone */ }+ return finish(false);+ }+ killer.once('error', () => finish(false));+ killer.once('close', code => finish(code === 0));+ timer = setTimeout(() => {+ try { killer.kill('SIGKILL'); } catch { /* the tree-kill helper may already be gone */ }+ finish(false);+ }, WINDOWS_TREE_KILL_TIMEOUT_MS);+ timer.unref?.();+ });+}++async function killOwnedProbe(child, killSignal, directExited = false) {+ if (!child?.pid) {+ try { child?.kill(killSignal); } catch { /* the child may not have spawned */ }+ destroyProbeStreams(child);+ return false;+ }+ if (process.platform === 'win32') {+ // Once the direct child exits, its PID may be reused. Do not invoke+ // taskkill against that PID; descendant cleanup is then unconfirmed.+ if (directExited) {+ destroyProbeStreams(child);+ return false;+ }+ return killWindowsProbeTree(child, directExited);+ }++ try {+ process.kill(-child.pid, killSignal);+ } catch (err) {+ if (err?.code === 'ESRCH') {+ destroyProbeStreams(child);+ return true;+ }+ try { child.kill(killSignal); } catch { /* the direct child may already be gone */ }+ destroyProbeStreams(child);+ return false;+ }+ destroyProbeStreams(child);+ return waitForProbeGroupExit(child.pid);+}++function execFileBounded(file, args, { timeout, killSignal = 'SIGTERM' }) {+ return new Promise((resolve) => {+ let child = null;+ let spawned = false;+ let directExited = false;+ let settled = false;+ let timer;+ let cleanupPromise = null;+ const finish = (result) => {+ if (settled) return;+ settled = true;+ clearTimeout(timer);+ resolve({ ...result, spawned });+ };+ const finishTimedOut = (result) => {+ cleanupPromise ||= killOwnedProbe(child, killSignal, directExited);+ cleanupPromise.then(+ cleanupConfirmed => finish({ ...result, timedOut: true, cleanupConfirmed }),+ () => finish({ ...result, timedOut: true, cleanupConfirmed: false }),+ );+ };++ // This watchdog exclusively owns timeout cleanup. Keeping execFile's+ // native timeout disabled avoids racing the tree cleanup below it.+ timer = setTimeout(() => finishTimedOut({ error: null, stdout: '', stderr: '' }), timeout);+ timer.unref?.();++ try {+ child = execFile(file, args, {+ windowsHide: true,+ timeout: 0,+ ...(process.platform === 'win32' ? {} : { detached: true }),+ }, (error, stdout, stderr) => {+ // execFile callbacks are asynchronous in Node. Deferring the handling+ // also keeps synchronous test doubles from racing child assignment.+ queueMicrotask(() => {+ const timedOut = error?.code === 'ETIMEDOUT'+ || (error?.killed === true && error?.signal === killSignal);+ const result = { error, stdout: stdout || '', stderr: stderr || '' };+ if (timedOut) finishTimedOut(result);+ else {+ // The executable can exit after daemonizing a helper that still+ // holds the probe's stdio pipes. Clean the owned process group/tree+ // even when execFile reports a normal exit. On Windows, the direct+ // process has already exited by this callback; taskkill against its+ // PID could target a reused PID, so only confirm the direct child.+ cleanupPromise ||= process.platform === 'win32'+ ? Promise.resolve(true)+ : killOwnedProbe(child, killSignal, true);+ cleanupPromise.then(+ cleanupConfirmed => finish({ ...result, timedOut: false, cleanupConfirmed }),+ () => finish({ ...result, timedOut: false, cleanupConfirmed: false }),+ );+ }+ });+ });+ spawned = Boolean(child?.pid);+ child?.once?.('exit', () => { directExited = true; });+ child?.once?.('spawn', () => { spawned = true; });+ } catch (error) {+ finish({ error, stdout: '', stderr: '', timedOut: false });+ }+ });+}+const TOOL_USE_RE = new RegExp(['qwen','llama-?3\\.[1-9]', 'llama-?4',@@ -54,7 +216,10 @@ export function createProviderCatalogService({cachedModelIds = null,sampleFile = null,defaultSamplePath,+ cliProbeTimeouts = {},}) {+ const lookupTimeoutMs = cliProbeTimeouts.lookup ?? CLI_COMMAND_LOOKUP_TIMEOUT_MS;+ const versionTimeoutMs = cliProbeTimeouts.version ?? CLI_VERSION_PROBE_TIMEOUT_MS;return {async testProvider(id) {const data = await loadProviders();@@ -65,8 +230,23 @@ export function createProviderCatalogService({const isWin32 = process.platform === 'win32';const lookup = isWin32 ? 'where' : 'which';const probeCommand = composeBootstrapSpawn(provider, provider.command, []).command;- const { stdout } = await execFileAsync(lookup, [probeCommand], { windowsHide: true })- .catch(() => ({ stdout: '', stderr: 'not found' }));+ const lookupResult = await execFileBounded(lookup, [probeCommand], {+ timeout: lookupTimeoutMs,+ killSignal: 'SIGKILL',+ });+ if (lookupResult.timedOut) {+ return {+ success: false,+ error: `Timed out looking up command '${probeCommand}' in PATH after ${lookupTimeoutMs} ms`,+ };+ }+ if (lookupResult.spawned && !lookupResult.cleanupConfirmed) {+ return {+ success: false,+ error: `Could not confirm cleanup after looking up '${probeCommand}' in PATH`,+ };+ }+ const { stdout } = lookupResult;const commandPath = stdout.split(/\r?\n/).map(s => s.trim()).find(Boolean) || '';if (!commandPath) return { success: false, error: `Command '${probeCommand}' not found in PATH` };@@ -78,15 +258,63 @@ export function createProviderCatalogService({const tryVersion = async (flag) => {try {const { command: execCommand, args: execArgs } = prepareWindowsSafeSpawn(invokePath, [flag]);- const out = await execFileAsync(execCommand, execArgs);+ const out = await execFileBounded(execCommand, execArgs, {+ timeout: versionTimeoutMs,+ killSignal: 'SIGKILL',+ });+ if (out.timedOut) {+ everSpawned = true;+ return { version: null, timedOut: true, cleanupConfirmed: out.cleanupConfirmed };+ }+ if (out.spawned && !out.cleanupConfirmed) {+ return { version: null, timedOut: false, cleanupConfirmed: false };+ }+ if (out.error) {+ if (typeof out.error.code === 'number' || out.spawned) everSpawned = true;+ return {+ version: null,+ timedOut: false,+ cleanupConfirmed: out.spawned ? out.cleanupConfirmed : undefined,+ };+ }everSpawned = true;- return out?.stdout?.trim() || null;- } catch (err) {- if (typeof err?.code === 'number') everSpawned = true;- return null;+ return { version: out.stdout.trim() || null, timedOut: false, cleanupConfirmed: out.cleanupConfirmed };+ } catch {+ return { version: null, timedOut: false };}};- const versionOut = (await tryVersion('--version')) || (await tryVersion('-v'));+ const primaryVersion = await tryVersion('--version');+ if (primaryVersion.cleanupConfirmed === false && !primaryVersion.timedOut) {+ return {+ success: false,+ error: `Could not confirm cleanup after probing '${probeCommand}'`,+ };+ }+ if (primaryVersion.timedOut && !primaryVersion.cleanupConfirmed) {+ return {+ success: false,+ error: `Timed out probing '${probeCommand}', and process cleanup could not be confirmed`,+ };+ }+ // A timeout is evidence that the binary spawned but did not answer.+ // Do not spend another ten seconds launching a second version flag.+ let fallbackVersion = null;+ if (!primaryVersion.version && !primaryVersion.timedOut) {+ fallbackVersion = await tryVersion('-v');+ }+ if (fallbackVersion?.timedOut && !fallbackVersion.cleanupConfirmed) {+ return {+ success: false,+ error: `Timed out probing '${probeCommand}', and process cleanup could not be confirmed`,+ };+ }+ if (fallbackVersion?.cleanupConfirmed === false) {+ return {+ success: false,+ error: `Could not confirm cleanup after probing '${probeCommand}'`,+ };+ }+ const versionOut = primaryVersion.version || fallbackVersion?.version;if (!everSpawned) {return {success: false,diff --git a/server/lib/aiToolkit/providers.cliPathResolve.test.js b/server/lib/aiToolkit/providers.cliPathResolve.test.jsindex d58d251116..b46a009b15 100644--- a/server/lib/aiToolkit/providers.cliPathResolve.test.js+++ b/server/lib/aiToolkit/providers.cliPathResolve.test.js@@ -2,19 +2,16 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import { mkdtemp, rm, writeFile } from 'fs/promises';import { join } from 'path';import { tmpdir } from 'os';+import { EventEmitter } from 'events';// Mock child_process so testProvider's command-resolution branch runs against a// controllable `which`/`where` + version probe instead of the host's real PATH.-// execFile is replaced with a plain vi.fn (the real one carries a custom-// util.promisify symbol; the mock doesn't, so promisify resolves to the single-// value we hand the callback — we pass a `{ stdout, stderr }` object to keep the-// `const { stdout } = await execFileAsync(...)` destructure working).vi.mock('child_process', async (importOriginal) => {const actual = await importOriginal();- return { ...actual, execFile: vi.fn() };+ return { ...actual, execFile: vi.fn(), spawn: vi.fn() };});-import { execFile } from 'child_process';+import { execFile, spawn } from 'child_process';import { createProviderService } from './providers.js';import { pinPlatform } from '../testHelper.js';@@ -28,11 +25,18 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>let restorePlatform = () => {};let originalPath;let fakePathDir;+ let nextFakePid = 10_000;beforeEach(async () => {restorePlatform = pinPlatform(process.platform);TEST_DATA_DIR = await mkdtemp(join(tmpdir(), 'portos-cli-resolve-'));providerService = createProviderService({ dataDir: TEST_DATA_DIR, providersFile: 'providers.json' });+ vi.mocked(spawn).mockImplementation(() => {+ const child = new EventEmitter();+ child.kill = vi.fn();+ queueMicrotask(() => child.emit('close', 0));+ return child;+ });// testProvider's win32 path now ALSO does its own filesystem-based// re-resolution (resolveWindowsExecutable) independent of the mocked// `where`/`which` lookup below — point PATH at a controlled, normally-empty@@ -45,6 +49,8 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>});afterEach(async () => {+ vi.useRealTimers();+ vi.restoreAllMocks();restorePlatform();process.env.PATH = originalPath;if (fakePathDir) await rm(fakePathDir, { recursive: true, force: true });@@ -52,12 +58,35 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>if (TEST_DATA_DIR) await rm(TEST_DATA_DIR, { recursive: true, force: true });});- // execFile's callback is always its last argument — the version probe now- // passes an extra `{ shell }` options object (4 args) while the which/where- // lookup stays at 3, so find the callback positionally instead of assuming- // a fixed arity.+ // execFile's callback is always its last argument; options vary between the+ // lookup and version calls, so find the callback positionally.const lastCallback = (args) => args[args.length - 1];+ const mockOwnedGroupCleanup = (pid) => {+ vi.spyOn(process, 'kill').mockImplementation((target, signal) => {+ if (target === -pid && signal === 0) {+ const error = new Error('process group is gone');+ error.code = 'ESRCH';+ throw error;+ }+ if (target === -pid && signal === 'SIGKILL') return true;+ throw new Error(`Unexpected process.kill(${target}, ${signal})`);+ });+ };++ const makeProbeChild = () => {+ const pid = nextFakePid++;+ if (process.platform !== 'win32') mockOwnedGroupCleanup(pid);+ return {+ pid,+ kill: vi.fn(),+ stdin: { destroy: vi.fn() },+ stdout: { destroy: vi.fn() },+ stderr: { destroy: vi.fn() },+ once: vi.fn(),+ };+ };+// Drive the mocked execFile: `lookup` is the stdout the which/where call emits,// `version` is the stdout the resolved-binary --version probe emits.const stubExec = ({ lookup, version = 'claude 1.0.0\n' }) => {@@ -65,10 +94,12 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>const cb = lastCallback(rest);if (cmd === 'which' || cmd === 'where') {if (lookup === null) return cb(new Error('not found'));- return cb(null, { stdout: lookup, stderr: '' });+ return cb(null, lookup, '');}// version probe against the resolved path- return cb(null, { stdout: version, stderr: '' });+ const child = makeProbeChild();+ cb(null, version, '');+ return child;});};@@ -89,6 +120,12 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>const lookupCmds = vi.mocked(execFile).mock.calls.map((c) => c[0]);expect(lookupCmds).toContain('where');expect(lookupCmds).not.toContain('which');+ const lookupCall = vi.mocked(execFile).mock.calls.find((c) => c[0] === 'where');+ expect(lookupCall?.[2]).toMatchObject({ timeout: 0, windowsHide: true });+ expect(lookupCall?.[2]).not.toHaveProperty('killSignal');+ // Successful completion means the direct process has exited. Do not run+ // taskkill against its possibly reused PID on the normal path.+ expect(spawn).not.toHaveBeenCalled();});it('takes the first line when `where` returns multiple matches', async () => {@@ -126,7 +163,11 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>// (file, args, options, callback) — the options slot carries windowsHide so// this probe can't pop a console window out of a PM2 fork on Windows.expect(versionCall?.length).toBe(4);- expect(versionCall?.[2]).toMatchObject({ windowsHide: true });+ expect(versionCall?.[2]).toMatchObject({+ windowsHide: true,+ timeout: 0,+ });+ expect(versionCall?.[2]).not.toHaveProperty('killSignal');expect(typeof versionCall?.[3]).toBe('function');});@@ -180,15 +221,210 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>expect(result.error).toBe("Command 'claude' not found in PATH");});+ it('reports a timed-out command lookup separately from a missing command', async () => {+ pinPlatform('linux');+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ if (cmd !== 'which') throw new Error(`Unexpected command: ${cmd}`);+ // Node's execFile timeout callback has code=null and exposes `killed`+ // plus the signal; it does not set code=ETIMEDOUT.+ const err = Object.assign(new Error('Command failed: which slow-which'), {+ code: null,+ killed: true,+ signal: 'SIGKILL',+ });+ return cb(err);+ });+ const p = await makeCliProvider('slow-which');++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(false);+ expect(result.error).toMatch(/Timed out looking up command 'slow-which'/);+ expect(result.error).not.toMatch(/not found/);+ expect(vi.mocked(execFile).mock.calls[0][2]).toMatchObject({ timeout: 0 });+ expect(vi.mocked(execFile).mock.calls).toHaveLength(1);+ });++ it('does not start the version probe when lookup cleanup is unconfirmed', async () => {+ pinPlatform('linux');+ vi.spyOn(process, 'kill').mockImplementation(() => {+ const error = new Error('permission denied');+ error.code = 'EPERM';+ throw error;+ });+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ if (cmd !== 'which') throw new Error(`Unexpected command after unsafe lookup: ${cmd}`);+ cb(null, '/usr/local/bin/slow-which\n', '');+ return {+ pid: 2468,+ kill: vi.fn(),+ stdin: { destroy: vi.fn() },+ stdout: { destroy: vi.fn() },+ stderr: { destroy: vi.fn() },+ once: vi.fn(),+ };+ });+ const p = await makeCliProvider('slow-which');++ const result = await providerService.testProvider(p.id);++ expect(result).toMatchObject({ success: false });+ expect(result.error).toMatch(/cleanup after looking up/);+ expect(vi.mocked(execFile).mock.calls).toHaveLength(1);+ });++ it('treats Node execFile timeout results as a spawned CLI with no version', async () => {+ pinPlatform('linux');+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ if (cmd === 'which') return cb(null, '/usr/local/bin/slow-cli\n', '');+ mockOwnedGroupCleanup(4321);+ const err = Object.assign(new Error('Command failed: slow-cli --version'), {+ code: null,+ killed: true,+ signal: 'SIGKILL',+ });+ cb(err);+ return {+ pid: 4321,+ kill: vi.fn(),+ stdin: { destroy: vi.fn() },+ stdout: { destroy: vi.fn() },+ stderr: { destroy: vi.fn() },+ once: vi.fn(),+ };+ });+ const p = await makeCliProvider('slow-cli');++ const result = await providerService.testProvider(p.id);++ expect(result).toMatchObject({ success: true, path: '/usr/local/bin/slow-cli', version: 'available' });+ const versionCalls = vi.mocked(execFile).mock.calls.filter(([cmd]) => cmd !== 'which' && cmd !== 'where');+ expect(versionCalls).toHaveLength(1);+ expect(versionCalls[0][1]).toEqual(['--version']);+ });++ it('does not report a timed-out CLI as available when cleanup cannot be confirmed', async () => {+ pinPlatform('linux');+ vi.spyOn(process, 'kill').mockImplementation(() => {+ const error = new Error('permission denied');+ error.code = 'EPERM';+ throw error;+ });+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ if (cmd === 'which') return cb(null, '/usr/local/bin/slow-cli\n', '');+ cb(Object.assign(new Error('Command failed: slow-cli --version'), {+ code: null,+ killed: true,+ signal: 'SIGKILL',+ }));+ return {+ pid: 9876,+ kill: vi.fn(),+ stdin: { destroy: vi.fn() },+ stdout: { destroy: vi.fn() },+ stderr: { destroy: vi.fn() },+ once: vi.fn(),+ };+ });+ const p = await makeCliProvider('slow-cli');++ const result = await providerService.testProvider(p.id);++ expect(result).toMatchObject({ success: false });+ expect(result.error).toMatch(/process cleanup could not be confirmed/);+ expect(result.version).toBeUndefined();+ });++ it('kills and settles a CLI that never closes at the version-probe deadline', async () => {+ pinPlatform('linux');+ vi.useFakeTimers();+ mockOwnedGroupCleanup(1234);+ let versionProbeStarted;+ let hangingChild;+ const started = new Promise((resolve) => { versionProbeStarted = resolve; });+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ const options = rest[rest.length - 2];+ if (cmd === 'which') return cb(null, '/usr/local/bin/never-exits\n', '');+ hangingChild = {+ pid: 1234,+ kill: vi.fn(),+ stdin: { destroy: vi.fn() },+ stdout: { destroy: vi.fn() },+ stderr: { destroy: vi.fn() },+ once: vi.fn(),+ };+ versionProbeStarted(options);+ // This simulated ChildProcess deliberately never calls its callback.+ return hangingChild;+ });+ const p = await makeCliProvider('never-exits');++ const testPromise = providerService.testProvider(p.id);+ const versionOptions = await started;+ expect(versionOptions).toMatchObject({ timeout: 0 });+ await vi.advanceTimersByTimeAsync(10_000);+ const result = await testPromise;++ expect(result).toMatchObject({ success: true, path: '/usr/local/bin/never-exits', version: 'available' });+ expect(process.kill).toHaveBeenCalledWith(-1234, 'SIGKILL');+ expect(hangingChild.stdin.destroy).toHaveBeenCalledOnce();+ expect(hangingChild.stdout.destroy).toHaveBeenCalledOnce();+ expect(hangingChild.stderr.destroy).toHaveBeenCalledOnce();+ const versionCalls = vi.mocked(execFile).mock.calls.filter(([cmd]) => cmd !== 'which' && cmd !== 'where');+ expect(versionCalls).toHaveLength(1);+ });++ it('does not taskkill a Windows probe PID after its parent exited before timeout', async () => {+ pinPlatform('win32');+ vi.useFakeTimers();+ vi.mocked(spawn).mockClear();+ let versionProbeStarted;+ let hangingChild;+ const started = new Promise((resolve) => { versionProbeStarted = resolve; });+ vi.mocked(execFile).mockImplementation((cmd, ...rest) => {+ const cb = lastCallback(rest);+ if (cmd === 'where') return cb(null, 'C:\\tools\\slow-cli.exe\r\n', '');+ hangingChild = new EventEmitter();+ hangingChild.pid = 5678;+ hangingChild.kill = vi.fn();+ hangingChild.stdin = { destroy: vi.fn() };+ hangingChild.stdout = { destroy: vi.fn() };+ hangingChild.stderr = { destroy: vi.fn() };+ versionProbeStarted(rest[rest.length - 2]);+ // The direct process exits but its descendant retains the execFile pipes,+ // so the callback never fires and the watchdog owns the timeout.+ return hangingChild;+ });+ const p = await makeCliProvider('slow-cli');++ const testPromise = providerService.testProvider(p.id);+ await started;+ hangingChild.emit('exit', 0, null);+ await vi.advanceTimersByTimeAsync(10_000);+ const result = await testPromise;++ expect(result).toMatchObject({ success: false });+ expect(result.error).toMatch(/process cleanup could not be confirmed/);+ expect(spawn).not.toHaveBeenCalled();+ expect(hangingChild.kill).not.toHaveBeenCalled();+ });+it('falls back to "available" when the binary spawns but supports no version flag', async () => {pinPlatform('linux');vi.mocked(execFile).mockImplementation((cmd, ...rest) => {const cb = lastCallback(rest);- if (cmd === 'which' || cmd === 'where') return cb(null, { stdout: '/usr/local/bin/claude\n', stderr: '' });+ if (cmd === 'which' || cmd === 'where') return cb(null, '/usr/local/bin/claude\n', '');// The binary ran (it's spawnable) but exited non-zero on the unknown flag —// a numeric exit code is how Node surfaces a non-zero exit from execFile.const e = new Error('Command failed: claude --version'); e.code = 1;- return cb(e);+ const child = makeProbeChild();+ cb(e);+ return child;});const p = await makeCliProvider();@@ -202,7 +438,7 @@ describe('testProvider — cli command resolution (cross-platform PATH)', () =>pinPlatform('win32');vi.mocked(execFile).mockImplementation((cmd, ...rest) => {const cb = lastCallback(rest);- if (cmd === 'which' || cmd === 'where') return cb(null, { stdout: 'C:\\Users\\Joe\\AppData\\npm\\claude.cmd\r\n', stderr: '' });+ if (cmd === 'which' || cmd === 'where') return cb(null, 'C:\\Users\\Joe\\AppData\\npm\\claude.cmd\r\n', '');// Simulate a genuinely unspawnable shim even after resolveWindowsExecutable// picks it (e.g. corrupted/missing target) — a spawn error carries a// string code (ENOENT), distinct from a non-zero exit's numeric code.diff --git a/server/lib/aiToolkit/providers.cliProbeLifecycle.test.js b/server/lib/aiToolkit/providers.cliProbeLifecycle.test.jsnew file mode 100644index 0000000000..f03a07e7f9--- /dev/null+++ b/server/lib/aiToolkit/providers.cliProbeLifecycle.test.js@@ -0,0 +1,41 @@+import { afterEach, describe, expect, it } from 'vitest';+import { chmod, mkdtemp, rm, writeFile } from 'fs/promises';+import { join } from 'path';+import { tmpdir } from 'os';+import { createProviderService } from './providers.js';++describe('testProvider — real CLI process lifecycle', () => {+ let tempDir;+ let originalPath;++ afterEach(async () => {+ if (originalPath !== undefined) process.env.PATH = originalPath;+ if (tempDir) await rm(tempDir, { recursive: true, force: true });+ });++ it('bounds a version probe and cleans up a descendant holding inherited pipes', async () => {+ if (process.platform === 'win32') return;++ tempDir = await mkdtemp(join(tmpdir(), 'portos-cli-probe-'));+ originalPath = process.env.PATH;+ const commandPath = join(tempDir, 'hang-cli');+ await writeFile(commandPath, `#!/usr/bin/env node+const { spawn } = require('node:child_process');+spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'inherit' });+setInterval(() => {}, 1000);+`);+ await chmod(commandPath, 0o755);+ process.env.PATH = `${tempDir}${process.platform === 'win32' ? ';' : ':'}${originalPath}`;++ const service = createProviderService({+ dataDir: tempDir,+ providersFile: 'providers.json',+ cliProbeTimeouts: { lookup: 1_000, version: 150 },+ });+ const provider = await service.createProvider({ name: 'Hanging CLI', type: 'cli', command: 'hang-cli' });++ const result = await service.testProvider(provider.id);++ expect(result).toMatchObject({ success: true, path: commandPath, version: 'available' });+ }, 5_000);+});diff --git a/server/lib/aiToolkit/providers.js b/server/lib/aiToolkit/providers.jsindex 28bcea9f81..e9c7f0106a 100644--- a/server/lib/aiToolkit/providers.js+++ b/server/lib/aiToolkit/providers.js@@ -73,6 +73,7 @@ export function createProviderService(config = {}) {cachedModelIds,sampleFile,defaultSamplePath: DEFAULT_SAMPLE_PATH,+ cliProbeTimeouts: config.cliProbeTimeouts,});return { ...crud, ...catalog };}