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-1801]): resolve CLI provider on Windows with where, not which
Adam Eivyff0ecfea2694
diff --git a/server/lib/aiToolkit/providers.cliPathResolve.test.js b/server/lib/aiToolkit/providers.cliPathResolve.test.jsnew file mode 100644index 0000000000..a8825322d5--- /dev/null+++ b/server/lib/aiToolkit/providers.cliPathResolve.test.js@@ -0,0 +1,133 @@+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';+import { mkdir, rm } from 'fs/promises';+import { existsSync } from 'fs';+import { join } from 'path';++// 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() };+});++import { execFile } from 'child_process';+import { createProviderService } from './providers.js';++const TEST_DATA_DIR = join(process.cwd(), 'test-data-cli-resolve');++describe('testProvider — cli command resolution (cross-platform PATH)', () => {+ let providerService;+ let originalPlatform;++ beforeEach(async () => {+ originalPlatform = process.platform;+ if (!existsSync(TEST_DATA_DIR)) await mkdir(TEST_DATA_DIR, { recursive: true });+ providerService = createProviderService({ dataDir: TEST_DATA_DIR, providersFile: 'providers.json' });+ });++ afterEach(async () => {+ Object.defineProperty(process, 'platform', { value: originalPlatform, configurable: true });+ vi.mocked(execFile).mockReset();+ if (existsSync(TEST_DATA_DIR)) await rm(TEST_DATA_DIR, { recursive: true });+ });++ const setPlatform = (value) => Object.defineProperty(process, 'platform', { value, configurable: true });++ // 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' }) => {+ vi.mocked(execFile).mockImplementation((cmd, argv, cb) => {+ if (cmd === 'which' || cmd === 'where') {+ if (lookup === null) return cb(new Error('not found'));+ return cb(null, { stdout: lookup, stderr: '' });+ }+ // version probe against the resolved path+ return cb(null, { stdout: version, stderr: '' });+ });+ };++ const makeCliProvider = (command = 'claude') =>+ providerService.createProvider({ name: 'Claude CLI', type: 'cli', command });++ it('uses `where` (not `which`) on win32 and reports success when the command resolves', async () => {+ setPlatform('win32');+ stubExec({ lookup: 'C:\\Users\\Joe\\.local\\bin\\claude.exe\r\n' });+ const p = await makeCliProvider();++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(true);+ expect(result.path).toBe('C:\\Users\\Joe\\.local\\bin\\claude.exe');+ expect(result.version).toBe('claude 1.0.0');+ // The lookup must have gone through `where`, never `which`, on Windows.+ const lookupCmds = vi.mocked(execFile).mock.calls.map((c) => c[0]);+ expect(lookupCmds).toContain('where');+ expect(lookupCmds).not.toContain('which');+ });++ it('takes the first line when `where` returns multiple matches', async () => {+ setPlatform('win32');+ stubExec({ lookup: 'C:\\a\\claude.exe\r\nC:\\b\\claude.exe\r\n' });+ const p = await makeCliProvider();++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(true);+ expect(result.path).toBe('C:\\a\\claude.exe');+ });++ it('invokes the resolved path for the version probe (so win32 runs the exact .exe)', async () => {+ setPlatform('win32');+ stubExec({ lookup: 'C:\\Users\\Joe\\.local\\bin\\claude.exe\r\n' });+ const p = await makeCliProvider();++ await providerService.testProvider(p.id);++ const versionCall = vi.mocked(execFile).mock.calls.find((c) => c[0] !== 'where' && c[0] !== 'which');+ expect(versionCall?.[0]).toBe('C:\\Users\\Joe\\.local\\bin\\claude.exe');+ expect(versionCall?.[1]).toEqual(['--version']);+ });++ it('uses `which` on non-win32 platforms', async () => {+ setPlatform('linux');+ stubExec({ lookup: '/usr/local/bin/claude\n' });+ const p = await makeCliProvider();++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(true);+ expect(result.path).toBe('/usr/local/bin/claude');+ const lookupCmds = vi.mocked(execFile).mock.calls.map((c) => c[0]);+ expect(lookupCmds).toContain('which');+ expect(lookupCmds).not.toContain('where');+ });++ it('reports not-found when the lookup resolves nothing', async () => {+ setPlatform('win32');+ stubExec({ lookup: null });+ const p = await makeCliProvider('claude');++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(false);+ expect(result.error).toBe("Command 'claude' not found in PATH");+ });++ it('falls back to "available" when no version flag yields output', async () => {+ setPlatform('linux');+ vi.mocked(execFile).mockImplementation((cmd, argv, cb) => {+ if (cmd === 'which' || cmd === 'where') return cb(null, { stdout: '/usr/local/bin/claude\n', stderr: '' });+ return cb(new Error('unknown flag')); // every --version / -v probe fails+ });+ const p = await makeCliProvider();++ const result = await providerService.testProvider(p.id);++ expect(result.success).toBe(true);+ expect(result.version).toBe('available');+ });+});diff --git a/server/lib/aiToolkit/providers.js b/server/lib/aiToolkit/providers.jsindex 21f3a4a4cd..de4b3840a2 100644--- a/server/lib/aiToolkit/providers.js+++ b/server/lib/aiToolkit/providers.js@@ -439,24 +439,34 @@ export function createProviderService(config = {}) {}if (provider.type === 'cli' || provider.type === 'tui') {+ // Resolve the command on PATH. Windows has no `which` — it ships `where`+ // instead — so a `which` lookup there always fails and falsely reports the+ // command "not found in PATH" even when it resolves fine from a shell.// Use execFile (no shell) so user-configured `provider.command` cannot// inject extra shell commands via metacharacters.- const { stdout } = await execFileAsync('which', [provider.command])+ const lookup = process.platform === 'win32' ? 'where' : 'which';+ const { stdout } = await execFileAsync(lookup, [provider.command]).catch(() => ({ stdout: '', stderr: 'not found' }));- if (!stdout.trim()) {+ // `where` lists every match (one per line); `which` prints one. Take the+ // first non-empty line as the resolved absolute path.+ const commandPath = stdout.split(/\r?\n/).map(s => s.trim()).find(Boolean) || '';++ if (!commandPath) {return { success: false, error: `Command '${provider.command}' not found in PATH` };}const tryVersion = async (flag) => {- const out = await execFileAsync(provider.command, [flag]).catch(() => null);+ // Invoke the resolved path so Windows runs the exact `.exe` we found —+ // execFile won't re-apply PATHEXT to a bare command name.+ const out = await execFileAsync(commandPath, [flag]).catch(() => null);return out?.stdout?.trim() || null;};const versionOut = (await tryVersion('--version')) || (await tryVersion('-v')) || 'available';return {success: true,- path: stdout.trim(),+ path: commandPath,version: versionOut};}