🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(doctor): recognize matching npm launchers on Windows (#4473)
Lenin Mookiahe30d81b84581
diff --git a/v3/@claude-flow/cli/__tests__/mods-claude-installs-cmd-shim-4397.test.ts b/v3/@claude-flow/cli/__tests__/mods-claude-installs-cmd-shim-4397.test.tsindex 0176d3f147..fe63671f76 100644--- a/v3/@claude-flow/cli/__tests__/mods-claude-installs-cmd-shim-4397.test.ts+++ b/v3/@claude-flow/cli/__tests__/mods-claude-installs-cmd-shim-4397.test.ts@@ -1,7 +1,10 @@// #4397 part 3: an npm-global claude.cmd shim must be probed through cmd.exe,// not execFile'd directly (Node rejects that with EINVAL on Windows).-import { describe, it, expect } from 'vitest';-import { versionProbe } from '../src/mods/claude-installs.js';+import { afterEach, describe, it, expect, vi } from 'vitest';+import { openSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';+import { delimiter, join } from 'node:path';+import { tmpdir } from 'node:os';+import { findClaudeInstalls, judgeInstalls, versionProbe } from '../src/mods/claude-installs.js';describe('versionProbe (#4397)', () => {const shim = 'C:\\Users\\me\\AppData\\Roaming\\npm\\claude.cmd';@@ -28,3 +31,200 @@ describe('versionProbe (#4397)', () => {expect(versionProbe('/opt/claude.cmd', 'linux')?.command).toBe('/opt/claude.cmd');});});++vi.mock('node:fs', async (importOriginal) => {+ const fs = await importOriginal<typeof import('node:fs')>();+ return { ...fs, openSync: vi.fn(fs.openSync) };+});++// npm/cmd-shim generates both launchers for one entrypoint. These synthetic+// fixtures model its legacy basedir and modern basedir_win forms; never run them.++const { openSync: realOpenSync } = await vi.importActual<typeof import('node:fs')>('node:fs');++const target = 'node_modules/@anthropic-ai/claude-code/cli.js';+const cmdShim = (entry = target, modern = false) => [+ '@ECHO off', 'GOTO start', ':find_dp0', 'SET dp0=%~dp0', 'EXIT /b', ':start',+ 'SETLOCAL', 'CALL :find_dp0', '', 'IF EXIST "%dp0%\\node.exe" (',+ ' SET "_prog=%dp0%\\node.exe"', ') ELSE (', ' SET "_prog=node"',+ ...(!modern ? [' SET PATHEXT=%PATHEXT:;.JS;=;%'] : []), ')', '',+ 'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & '+ + (modern ? 'set PATHEXT=%PATHEXT:;.JS;=;% & ' : '')+ + `"%_prog%" "%dp0%\\${entry.replaceAll('/', '\\')}" %*`, '',+].join('\r\n');+const shShim = (entry = target, modern = false) => [+ '#!/bin/sh', 'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',+ ...(modern ? ['basedir_win="$basedir"'] : []), '',+ modern ? 'case `uname -a` in' : 'case `uname` in',+ ...(modern ? [+ ' *CYGWIN*|*MINGW*|*MSYS*)', ' if command -v cygpath > /dev/null 2>&1; then',+ ' basedir_win=`cygpath -w "$basedir"`', ' fi', ' ;;', ' *WSL2*)',+ ' if command -v wslpath > /dev/null 2>&1; then',+ ' basedir_win="$(wslpath -w "$basedir" 2> /dev/null)"',+ ' if [ $? -ne 0 ] || [ -z "$basedir_win" ]; then',+ ' echo "Error: wslpath failed to convert path. WSL environment may be misconfigured." >&2',+ ' exit 1', ' fi', ' fi', ' ;;',+ ] : [+ ' *CYGWIN*|*MINGW*|*MSYS*)', ' if command -v cygpath > /dev/null 2>&1; then',+ ' basedir=`cygpath -w "$basedir"`', ' fi', ' ;;',+ ]), 'esac', '',+ ...(modern ? [+ 'PROG_EXE="$basedir/node.exe"', 'if ! [ -x "$PROG_EXE" ]; then',+ ' PROG_EXE="$basedir/node"', ' if ! [ -x "$PROG_EXE" ]; then',+ ' PROG_EXE=node', ' if ! [ -x "$PROG_EXE" ]; then', ' PROG_EXE=node.exe',+ ' fi', ' fi', 'fi', '', `exec "$PROG_EXE" "$basedir_win/${entry}" "$@"`,+ ] : [+ 'if [ -x "$basedir/node" ]; then', ` exec "$basedir/node" "$basedir/${entry}" "$@"`,+ 'else ', ` exec node "$basedir/${entry}" "$@"`, 'fi',+ ]), '',+].join('\n');++const directCmdShim = (entry: string) => [+ '@ECHO off', 'GOTO start', ':find_dp0', 'SET dp0=%~dp0', 'EXIT /b', ':start',+ 'SETLOCAL', 'CALL :find_dp0', `"%dp0%\\${entry.replaceAll('/', '\\')}" %*`, '',+].join('\r\n');+const directShShim = (entry: string, modern = false) => {+ const node = shShim(entry, modern);+ return node.slice(0, node.indexOf(modern ? 'PROG_EXE=' : 'if [ -x')) + `exec "$basedir/${entry}" "$@"\n`;+};++describe('npm companion launchers (#4397)', () => {+ let scratch: string | undefined;+ afterEach(() => { if (scratch) rmSync(scratch, { recursive: true, force: true }); scratch = undefined; });+ const setup = () => {+ scratch = mkdtempSync(join(tmpdir(), 'claude-shim-'));+ const a = join(scratch, 'native'); const b = join(scratch, 'npm');+ mkdirSync(a); mkdirSync(b);+ return { a, b, home: join(scratch, 'home') };+ };+ const file = (dir: string, name: string, body = '') => {+ const path = join(dir, name); mkdirSync(join(path, '..'), { recursive: true });+ writeFileSync(path, body); return path;+ };+ const pair = (dir: string, modern = false, entry = target) => {+ file(dir, entry); file(dir, 'claude.cmd', cmdShim(entry, modern)); file(dir, 'claude', shShim(entry, modern));+ };+ // Restrict the platform override to this synchronous call, restoring even on error.+ const find = (dirs: string[], home: string, versions: Record<string, string>, platform = 'win32') => {+ const descriptor = Object.getOwnPropertyDescriptor(process, 'platform')!;+ Object.defineProperty(process, 'platform', { ...descriptor, value: platform });+ try { return findClaudeInstalls({ PATH: dirs.join(delimiter) }, home, path => versions[path] ?? null); }+ finally { Object.defineProperty(process, 'platform', descriptor); }+ };+ for (const modern of [false, true]) {+ it(`recognizes the ${modern ? 'modern' : 'legacy'} pair without a stale warning`, () => {+ const { b, home } = setup(); pair(b, modern);+ const installs = find([b], home, { [join(b, 'claude.cmd')]: '2.1.287' });+ expect(installs).toEqual([+ { path: join(b, 'claude.cmd'), version: '2.1.287' },+ { path: join(b, 'claude'), version: '2.1.287' },+ ]);+ expect(judgeInstalls(installs, false)).toEqual({ status: 'pass', message: `claude resolves to ${join(b, 'claude.cmd')} (2.1.287): mods on by default` });+ });+ }+ it('keeps both paths in removal advice for an old npm pair behind a current native install', () => {+ const { a, b, home } = setup(); file(a, 'claude.exe'); pair(b);+ const installs = find([a, b], home, { [join(a, 'claude.exe')]: '2.1.287', [join(b, 'claude.cmd')]: '2.1.107' });+ expect(installs.map(i => i.version)).toEqual(['2.1.287', '2.1.107', '2.1.107']);+ expect(judgeInstalls(installs, false)).toEqual({+ status: 'warn',+ message: `claude resolves to ${join(a, 'claude.exe')} (2.1.287): mods on by default; also installed: ${join(b, 'claude.cmd')} (2.1.107), ${join(b, 'claude')} (2.1.107). The first on PATH runs; a stale one can shadow a current one.`,+ fix: `put the claude you want first on PATH, or remove: ${join(b, 'claude.cmd')} ${join(b, 'claude')}`,+ });+ });+ for (const modern of [false, true]) {+ it(`recognizes the ${modern ? 'modern' : 'legacy'} native-executable pair`, () => {+ const { b, home } = setup(); const entry = 'node_modules/@anthropic-ai/claude-code/bin/claude.exe';+ file(b, entry, 'synthetic native binary'); file(b, 'claude.cmd', directCmdShim(entry)); file(b, 'claude', directShShim(entry, modern));+ const installs = find([b], home, { [join(b, 'claude.cmd')]: '2.1.310' });+ expect(installs.map(i => i.version)).toEqual(['2.1.310', '2.1.310']);+ expect(judgeInstalls(installs, false)).toEqual({ status: 'pass', message: `claude resolves to ${join(b, 'claude.cmd')} (2.1.310): mods on by default` });+ });+ }+ it('keeps both stale native npm launcher paths in removal advice', () => {+ const { a, b, home } = setup(); const entry = 'bin/claude.exe';+ file(a, 'claude.exe'); file(b, entry); file(b, 'claude.cmd', directCmdShim(entry)); file(b, 'claude', directShShim(entry));+ const installs = find([a, b], home, { [join(a, 'claude.exe')]: '2.1.310', [join(b, 'claude.cmd')]: '2.1.107' });+ expect(installs.map(i => i.version)).toEqual(['2.1.310', '2.1.107', '2.1.107']);+ expect(judgeInstalls(installs, false).fix).toBe(`put the claude you want first on PATH, or remove: ${join(b, 'claude.cmd')} ${join(b, 'claude')}`);+ });+ for (const directCmd of [false, true]) {+ it(`does not equate Node and direct execution (${directCmd ? 'native cmd' : 'node cmd'})`, () => {+ const { b, home } = setup(); file(b, target);+ file(b, 'claude.cmd', directCmd ? directCmdShim(target) : cmdShim(target));+ file(b, 'claude', directCmd ? shShim(target) : directShShim(target));+ expect(find([b], home, { [join(b, 'claude.cmd')]: '2.1.310' })[1].version).toBeNull();+ });+ }+ it('does not equate different native executable targets', () => {+ const { b, home } = setup(); file(b, 'a.exe'); file(b, 'b.exe');+ file(b, 'claude.cmd', directCmdShim('a.exe')); file(b, 'claude', directShShim('b.exe'));+ expect(find([b], home, { [join(b, 'claude.cmd')]: '2.1.310' })[1].version).toBeNull();+ });+ it('preserves a standalone unknown Git Bash launcher', () => {+ const { b, home } = setup(); file(b, 'claude', shShim()); file(b, target);+ const installs = find([b], home, {});+ expect(installs).toEqual([{ path: join(b, 'claude'), version: null }]);+ expect(judgeInstalls(installs, false).message).toBe(`claude resolves to ${join(b, 'claude')} (version unknown)`);+ });+ it('does not infer an extensionless version from a native executable', () => {+ const { b, home } = setup(); file(b, 'claude.exe'); file(b, 'claude', shShim()); file(b, target);+ expect(find([b], home, { [join(b, 'claude.exe')]: '2.1.287' })[1].version).toBeNull();+ });+ it('still warns about a stale .cmd in another directory', () => {+ const { a, b, home } = setup(); file(a, 'claude.exe'); file(b, 'claude.cmd', cmdShim());+ const result = judgeInstalls(find([a, b], home, { [join(a, 'claude.exe')]: '2.1.287', [join(b, 'claude.cmd')]: '2.1.107' }), false);+ expect(result.status).toBe('warn');+ expect(result.fix).toBe(`put the claude you want first on PATH, or remove: ${join(b, 'claude.cmd')}`);+ });+ for (const kind of ['different', 'missing', 'oversized', 'unreadable', 'unrecognized', 'comment-only', 'dynamic', 'disagreeing', 'cmd-comment-only', 'cmd-unrecognized', 'cmd-dynamic']) {+ it.skipIf(kind === 'unreadable' && process.platform === 'win32')(`does not propagate for ${kind} targets or launcher text`, () => {+ const { b, home } = setup(); pair(b); let sh = shShim(); let cmd = cmdShim();+ if (kind === 'different') { file(b, 'other.js'); sh = shShim('other.js'); }+ if (kind === 'missing') { sh = shShim('missing.js'); cmd = cmdShim('missing.js'); }+ if (kind === 'oversized') sh += '#'.repeat(65537);+ if (kind === 'unrecognized') sh = 'arbitrary launcher';+ if (kind === 'comment-only') sh = '#!/bin/sh\n# exec node "$basedir/' + target + '" "$@"\n';+ if (kind === 'dynamic') sh = shShim('$(other)/cli.js');+ if (kind === 'disagreeing') sh = sh.replace(`exec node "$basedir/${target}"`, 'exec node "$basedir/other.js"');+ if (kind === 'cmd-comment-only') cmd = '@ECHO off\r\nREM "%_prog%" "%dp0%\\' + target.replaceAll('/', '\\') + '" %*\r\n';+ if (kind === 'cmd-unrecognized') cmd += 'echo extra command\r\n';+ if (kind === 'cmd-dynamic') cmd = cmdShim('%OTHER%/cli.js');+ file(b, 'claude', sh); file(b, 'claude.cmd', cmd);+ if (kind === 'unreadable') vi.mocked(openSync).mockImplementationOnce(() => { throw Object.assign(new Error('synthetic read error'), { code: 'EACCES' }); });+ try {+ const installs = find([b], home, { [join(b, 'claude.cmd')]: '2.1.287' });+ expect(installs[1].version).toBeNull(); expect(judgeInstalls(installs, false).status).toBe('warn');+ } finally { if (kind === 'unreadable') vi.mocked(openSync).mockReset().mockImplementation(realOpenSync); }+ });+ }+ it.skipIf(process.platform === 'win32')('uses canonical entrypoint identity, including a symlinked entrypoint', () => {+ const { b, home } = setup(); pair(b); symlinkSync(join(b, target), join(b, 'linked.js'));+ file(b, 'claude', shShim('linked.js'));+ expect(find([b], home, { [join(b, 'claude.cmd')]: '2.1.287' })[1].version).toBe('2.1.287');+ });+ it.skipIf(process.platform === 'win32')('does not infer from a .cmd deduplicated through an earlier directory', () => {+ const { a, b, home } = setup(); pair(a); pair(b);+ // Both aliases resolve to one .cmd file, but their relative entrypoints differ.+ // Replace only the synthetic fixture link, never an installed launcher.+ rmSync(join(b, 'claude.cmd')); symlinkSync(join(a, 'claude.cmd'), join(b, 'claude.cmd'));+ const installs = find([a, b], home, { [join(a, 'claude.cmd')]: '2.1.287' });+ expect(installs.find(i => i.path === join(b, 'claude'))?.version).toBeNull();+ });+ it('does not borrow a version across directories even when entrypoints match', () => {+ const { a, b, home } = setup(); pair(a); file(b, 'claude', shShim('../native/' + target));+ expect(find([a, b], home, { [join(a, 'claude.cmd')]: '2.1.287' }).at(-1)?.version).toBeNull();+ });+ it('does not borrow an unknown .cmd version', () => {+ const { b, home } = setup(); pair(b);+ expect(find([b], home, {}).map(i => i.version)).toEqual([null, null]);+ });+ it('keeps a successful independent probe result', () => {+ const { b, home } = setup(); pair(b);+ expect(find([b], home, { [join(b, 'claude.cmd')]: '2.1.287', [join(b, 'claude')]: '2.1.107' })[1].version).toBe('2.1.107');+ });+ it('preserves Linux enumeration and probing', () => {+ const { b, home } = setup(); pair(b);+ expect(find([b], home, { [join(b, 'claude.cmd')]: '2.1.287' }, 'linux')).toEqual([{ path: join(b, 'claude'), version: null }]);+ });+});diff --git a/v3/@claude-flow/cli/src/mods/claude-installs.ts b/v3/@claude-flow/cli/src/mods/claude-installs.tsindex 28a1b37209..37a91eef0b 100644--- a/v3/@claude-flow/cli/src/mods/claude-installs.ts+++ b/v3/@claude-flow/cli/src/mods/claude-installs.ts@@ -8,8 +8,8 @@*/import { execFileSync } from 'node:child_process';-import { existsSync, realpathSync, statSync } from 'node:fs';-import { delimiter, join } from 'node:path';+import { closeSync, existsSync, fstatSync, openSync, readSync, realpathSync, statSync } from 'node:fs';+import { delimiter, dirname, join, resolve } from 'node:path';export const MODS_DEFAULT_ON = '2.1.287';export const MODS_FIRST = '2.1.277';@@ -63,6 +63,93 @@ export const defaultVersionOf: VersionOf = (path) => {}};+/** Known npm/cmd-shim Node launcher forms (legacy basedir and basedir_win).+ * Match the whole template, not comments or arbitrary shell code. Unknown npm+ * versions/formats simply keep the independent probe result. Never execute it.+ */+const npmCmdShim = (entry: string, modern: boolean) => [+ '@ECHO off', 'GOTO start', ':find_dp0', 'SET dp0=%~dp0', 'EXIT /b', ':start',+ 'SETLOCAL', 'CALL :find_dp0', '', 'IF EXIST "%dp0%\\node.exe" (',+ ' SET "_prog=%dp0%\\node.exe"', ') ELSE (', ' SET "_prog=node"',+ ...(!modern ? [' SET PATHEXT=%PATHEXT:;.JS;=;%'] : []), ')', '',+ 'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & '+ + (modern ? 'set PATHEXT=%PATHEXT:;.JS;=;% & ' : '')+ + `"%_prog%" "%dp0%\\${entry.replaceAll('/', '\\')}" %*`, '',+].join('\r\n');+const npmPosixShim = (entry: string, modern: boolean) => [+ '#!/bin/sh', 'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',+ ...(modern ? ['basedir_win="$basedir"'] : []), '',+ modern ? 'case `uname -a` in' : 'case `uname` in',+ ...(modern ? [+ ' *CYGWIN*|*MINGW*|*MSYS*)', ' if command -v cygpath > /dev/null 2>&1; then',+ ' basedir_win=`cygpath -w "$basedir"`', ' fi', ' ;;', ' *WSL2*)',+ ' if command -v wslpath > /dev/null 2>&1; then',+ ' basedir_win="$(wslpath -w "$basedir" 2> /dev/null)"',+ ' if [ $? -ne 0 ] || [ -z "$basedir_win" ]; then',+ ' echo "Error: wslpath failed to convert path. WSL environment may be misconfigured." >&2',+ ' exit 1', ' fi', ' fi', ' ;;',+ ] : [+ ' *CYGWIN*|*MINGW*|*MSYS*)', ' if command -v cygpath > /dev/null 2>&1; then',+ ' basedir=`cygpath -w "$basedir"`', ' fi', ' ;;',+ ]), 'esac', '',+ ...(modern ? [+ 'PROG_EXE="$basedir/node.exe"', 'if ! [ -x "$PROG_EXE" ]; then',+ ' PROG_EXE="$basedir/node"', ' if ! [ -x "$PROG_EXE" ]; then',+ ' PROG_EXE=node', ' if ! [ -x "$PROG_EXE" ]; then', ' PROG_EXE=node.exe',+ ' fi', ' fi', 'fi', '', `exec "$PROG_EXE" "$basedir_win/${entry}" "$@"`,+ ] : [+ 'if [ -x "$basedir/node" ]; then', ` exec "$basedir/node" "$basedir/${entry}" "$@"`,+ 'else ', ` exec node "$basedir/${entry}" "$@"`, 'fi',+ ]), '',+].join('\n');++// A no-shebang/native bin uses npm's direct-exec forms, not a Node wrapper.+const npmDirectCmdShim = (entry: string) => [+ '@ECHO off', 'GOTO start', ':find_dp0', 'SET dp0=%~dp0', 'EXIT /b', ':start',+ 'SETLOCAL', 'CALL :find_dp0', `"%dp0%\\${entry.replaceAll('/', '\\')}" %*`, '',+].join('\r\n');+const npmDirectPosixShim = (entry: string, modern: boolean) => {+ const nodeShim = npmPosixShim(entry, modern);+ const preamble = nodeShim.slice(0, nodeShim.indexOf(modern ? 'PROG_EXE=' : 'if [ -x'));+ return preamble + `exec "$basedir/${entry}" "$@"\n`;+};++function npmShimTarget(path: string, cmd: boolean): { path: string; mode: 'node' | 'direct' } | null {+ let fd: number | undefined;+ try {+ fd = openSync(path, 'r');+ const stat = fstatSync(fd);+ if (!stat.isFile() || stat.size > 64 * 1024) return null;+ // Also cap the actual read if a file grows after stat.+ const bytes = Buffer.alloc(64 * 1024 + 1);+ let size = 0;+ while (size < bytes.length) {+ const n = readSync(fd, bytes, size, bytes.length - size, null);+ if (!n) break;+ size += n;+ }+ if (size > 64 * 1024) return null;+ const text = bytes.subarray(0, size).toString('utf8').replace(/\r\n/g, '\n').trimEnd();+ const targets = cmd+ ? [...text.matchAll(/"%dp0%\\([^"\r\n]+)" +%\*$/gm)].map(m => m[1].replaceAll('\\', '/'))+ : [...text.matchAll(/^\s*exec (?:[^\r\n]* )?"\$basedir(?:_win)?\/([^"\r\n]+)" +"\$@"$/gm)].map(m => m[1]);+ const entry = targets[0];+ if (!entry || targets.some(t => t !== entry) || !/^[a-zA-Z0-9@._ /-]+$/.test(entry) || entry.startsWith('/')) return null;+ const template = cmd ? npmCmdShim : npmPosixShim;+ const normalize = (value: string) => value.replace(/\r\n/g, '\n').trimEnd();+ const node = [false, true].some(modern => normalize(template(entry, modern)) === text);+ const direct = cmd ? normalize(npmDirectCmdShim(entry)) === text+ : [false, true].some(modern => normalize(npmDirectPosixShim(entry, modern)) === text);+ if (!node && !direct) return null;+ const target = realpathSync(resolve(dirname(path), entry));+ return statSync(target).isFile() ? { path: target, mode: node ? 'node' : 'direct' } : null;+ } catch {+ return null;+ } finally {+ if (fd !== undefined) closeSync(fd);+ }+}+/** Every `claude` on PATH, in PATH order (the first is what runs), plus ~/.local/bin. */export function findClaudeInstalls(env: NodeJS.ProcessEnv, home: string, versionOf: VersionOf = defaultVersionOf): ClaudeInstall[] {const names = process.platform === 'win32' ? ['claude.exe', 'claude.cmd', 'claude'] : ['claude'];@@ -70,6 +157,7 @@ export function findClaudeInstalls(env: NodeJS.ProcessEnv, home: string, versionconst seen = new Set<string>();const installs: ClaudeInstall[] = [];for (const dir of dirs) {+ let cmd: ClaudeInstall | undefined;for (const name of names) {const path = join(dir, name);let real: string;@@ -81,7 +169,17 @@ export function findClaudeInstalls(env: NodeJS.ProcessEnv, home: string, version}if (seen.has(real)) continue;seen.add(real);- installs.push({ path, version: versionOf(path) });+ let version = versionOf(path);+ if (process.platform === 'win32' && name === 'claude' && version === null && cmd?.version) {+ const target = npmShimTarget(path, false);+ const cmdTarget = npmShimTarget(cmd.path, true);+ if (target && cmdTarget && target.path === cmdTarget.path && target.mode === cmdTarget.mode) version = cmd.version;+ }+ const install = { path, version };+ installs.push(install);+ // Only a .cmd actually probed in this directory can supply a version.+ // A realpath-deduplicated alias in another directory proves nothing here.+ if (name === 'claude.cmd') cmd = install;}}return installs;