From 8009a152f8f71fecff08da66dfa72218d430cb4a Mon Sep 17 00:00:00 2001 From: ruv Date: Mon, 22 Jun 2026 14:00:19 -0400 Subject: [PATCH] =?UTF-8?q?fix(init,doctor):=20#2448=20=E2=80=94=20migrate?= =?UTF-8?q?=20stale=20`npx=20@latest`=20statusLine=20+=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CRITICAL — affected user reported load average 49 / jetsam / kernel watchdog panic on macOS 26.1 / 48 GB. Pre-#2337 init wrote `npx @claude-flow/cli@latest hooks ` into ~/.claude/settings.json for both the statusLine and the per-action hooks. Each invocation cold- spawns a Node process (~130 MB) plus an npm registry round-trip to re-resolve @latest. The statusLine refires every few hundred ms; hooks fire per tool use. They stack faster than they exit. The current source already emits the local-helper form (`sh -c '… exec node "$D/.claude/helpers/" '`), but executor.ts:362 preserved the user's existing `statusLine.command` verbatim on re-init — so anyone who installed pre-#2337 and upgraded to 3.13.2 still has the runaway commands. Two changes: 1. v3/@claude-flow/cli/src/init/executor.ts - Detect the broken `npx @claude-flow/cli@latest hooks …` pattern in `existing.statusLine.command` AND walk every hooks.[] hooks[].command — regenerate to the local-helper form when matched. Idempotent on already-correct settings. - Captures the subcommand from the broken string (pre-bash, post-edit, route, etc.) so the regenerated command preserves the user's intent. 2. v3/@claude-flow/cli/src/commands/doctor.ts - New `checkStaleSettingsNpx` health check. Scans both project-local AND $HOME/.claude/settings.json for the broken pattern in statusLine + hooks. Reports `fail` with file path + offending sections when found, with fix instruction (`npx ruflo init` to migrate). Reports `pass` otherwise. - Registered as `stale-settings` in the component map so users can run `ruflo doctor --component stale-settings` directly. Smoke-verified against a synthetic fixture containing all three offender shapes (statusLine, PreToolUse hook, UserPromptSubmit hook with --prefer-offline prefix). Doctor correctly flagged all three. Release: 3.13.2 → 3.13.3 (PATCH per semver — pure bug fix). Published with all three dist-tags (latest/alpha/v3alpha) for @claude-flow/cli, claude-flow, and ruflo. Closes #2448. Co-Authored-By: RuFlo --- package.json | 2 +- ruflo/package.json | 2 +- v3/@claude-flow/cli/package.json | 2 +- v3/@claude-flow/cli/src/commands/doctor.ts | 81 ++++++++++++++++++++++ v3/@claude-flow/cli/src/init/executor.ts | 53 +++++++++++++- 5 files changed, 136 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8e0dc794f5..ebaeb15dca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "claude-flow", - "version": "3.13.2", + "version": "3.13.3", "description": "Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration", "main": "dist/index.js", "type": "module", diff --git a/ruflo/package.json b/ruflo/package.json index 6330650889..beefe77ba4 100644 --- a/ruflo/package.json +++ b/ruflo/package.json @@ -1,6 +1,6 @@ { "name": "ruflo", - "version": "3.13.2", + "version": "3.13.3", "description": "Ruflo - Enterprise AI agent orchestration platform. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration", "main": "bin/ruflo.js", "type": "module", diff --git a/v3/@claude-flow/cli/package.json b/v3/@claude-flow/cli/package.json index b050f4aa92..20616e7b9a 100644 --- a/v3/@claude-flow/cli/package.json +++ b/v3/@claude-flow/cli/package.json @@ -1,6 +1,6 @@ { "name": "@claude-flow/cli", - "version": "3.13.2", + "version": "3.13.3", "type": "module", "description": "Ruflo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code", "main": "dist/src/index.js", diff --git a/v3/@claude-flow/cli/src/commands/doctor.ts b/v3/@claude-flow/cli/src/commands/doctor.ts index 0647e9008e..c939a855ed 100644 --- a/v3/@claude-flow/cli/src/commands/doctor.ts +++ b/v3/@claude-flow/cli/src/commands/doctor.ts @@ -133,6 +133,85 @@ async function checkConfigFile(): Promise { } // Check daemon status +/** + * #2448 — Detect the runaway `npx @claude-flow/cli@latest` statusLine / hook + * commands left over in `.claude/settings.json` from pre-#2337 installs. + * + * These fire on every Claude Code event (statusLine refires every few hundred + * ms, hooks fire per tool-use), each spawning a cold Node process + npm + * registry round-trip. On the reporter's 48 GB macOS box this produced + * load average 49, jetsam, and a kernel watchdog panic two minutes after + * boot. Severity is CRITICAL when present; users who installed before #2337 + * and never re-ran `ruflo init` still have it. + * + * Detection only — does not modify settings. Fix path is `ruflo init` (the + * executor's migration logic, also patched in #2448, will now regenerate + * the broken commands). + */ +async function checkStaleSettingsNpx(): Promise { + // Same regex pattern the executor migration uses — kept in sync. + const BROKEN_RE = /npx\s+(?:--?\S+\s+)*@?claude-flow\/cli@latest\s+hooks\s+(?:statusline|\S+)/; + + // Look in both project-local and home-dir settings. + const candidates = [ + join(process.cwd(), '.claude', 'settings.json'), + join(process.env.HOME ?? '', '.claude', 'settings.json'), + ].filter((p, i, a) => p && a.indexOf(p) === i); + + const offenders: Array<{ path: string; where: string }> = []; + for (const settingsPath of candidates) { + if (!existsSync(settingsPath)) continue; + let settings: Record; + try { + settings = JSON.parse(readFileSync(settingsPath, 'utf8')); + } catch { + continue; // checkConfigFile reports JSON errors separately + } + + // statusLine.command + const sl = settings.statusLine as { command?: string } | undefined; + if (sl?.command && BROKEN_RE.test(sl.command)) { + offenders.push({ path: settingsPath, where: 'statusLine' }); + } + + // hooks.[].hooks[].command + const hooks = settings.hooks as Record }>> | undefined; + if (hooks) { + for (const [eventName, groups] of Object.entries(hooks)) { + if (!Array.isArray(groups)) continue; + for (const group of groups) { + if (!Array.isArray(group.hooks)) continue; + for (const h of group.hooks) { + if (typeof h?.command === 'string' && BROKEN_RE.test(h.command)) { + offenders.push({ path: settingsPath, where: `hooks.${eventName}` }); + } + } + } + } + } + } + + if (offenders.length === 0) { + return { name: 'Stale npx@latest in settings (#2448)', status: 'pass', message: 'no runaway commands detected' }; + } + + // Group by file for readable output + const byFile = offenders.reduce((acc, o) => { + (acc[o.path] ??= []).push(o.where); + return acc; + }, {} as Record); + const summary = Object.entries(byFile) + .map(([p, wheres]) => `${p} [${[...new Set(wheres)].join(', ')}]`) + .join('; '); + + return { + name: 'Stale npx@latest in settings (#2448)', + status: 'fail', + message: `CRITICAL — runaway \`npx @claude-flow/cli@latest\` commands detected: ${summary}`, + fix: 'Re-run `npx ruflo init` to migrate (the v3.13.3+ init migrator regenerates these to local-helper form). On macOS this prevents the process-storm / kernel-panic class reported in #2448.', + }; +} + async function checkDaemonStatus(): Promise { try { const pidFile = '.claude-flow/daemon.pid'; @@ -1002,6 +1081,7 @@ export const doctorCommand: Command = { checkGit, checkGitRepo, checkConfigFile, + checkStaleSettingsNpx, // #2448 — runaway `npx @latest` in statusLine/hooks checkDaemonStatus, checkMemoryDatabase, checkApiKeys, @@ -1023,6 +1103,7 @@ export const doctorCommand: Command = { 'npm': checkNpmVersion, 'claude': checkClaudeCode, 'config': checkConfigFile, + 'stale-settings': checkStaleSettingsNpx, // #2448 'daemon': checkDaemonStatus, 'memory': checkMemoryDatabase, 'api': checkApiKeys, diff --git a/v3/@claude-flow/cli/src/init/executor.ts b/v3/@claude-flow/cli/src/init/executor.ts index eba4738dda..927f266f19 100644 --- a/v3/@claude-flow/cli/src/init/executor.ts +++ b/v3/@claude-flow/cli/src/init/executor.ts @@ -360,15 +360,66 @@ function mergeSettingsForUpgrade(existing: Record): Record | undefined; if (existingStatusLine) { + const existingCmd = typeof existingStatusLine.command === 'string' ? existingStatusLine.command : ''; + const isBroken = BROKEN_STATUSLINE_RE.test(existingCmd); merged.statusLine = { type: 'command', - command: existingStatusLine.command || `node -e "var c=require('child_process'),p=require('path'),r;try{r=c.execSync('git rev-parse --show-toplevel',{encoding:'utf8'}).trim()}catch(e){r=process.cwd()}var s=p.join(r,'.claude/helpers/statusline.cjs');process.argv.splice(1,0,s);require(s)"`, + command: isBroken || !existingCmd ? NEW_STATUSLINE_CMD : existingCmd, // Remove invalid fields: refreshMs, enabled (not supported by Claude Code) }; } + // #2448 — Detect and REGENERATE per-action hook commands that still use + // the runaway `npx @claude-flow/cli@latest hooks ` form. These fire + // on every PreToolUse/PostToolUse/UserPromptSubmit, each spawning ~130 MB + // of cold Node + npm registry resolution; the storm is what kernel-paniced + // the reporter's machine in #2448. + // + // We walk each hook event's `hooks[]` and swap any command matching the + // broken pattern for the local-helper form. Idempotent: re-running this + // migration on already-correct settings is a no-op. + const BROKEN_HOOK_RE = /npx\s+(?:--?\S+\s+)*@?claude-flow\/cli@latest\s+hooks\s+(\S+)/; + const localHookCmd = (sub: string): string => { + // POSIX form mirrors settings-generator.ts::hookCmd() exactly. + // Windows users hit a separate code path (cmd /c …) — Claude Code on + // Windows is rarer and the migration there is left to a follow-up. + if (process.platform === 'win32') { + return `cmd /c "IF EXIST \"%CLAUDE_PROJECT_DIR%\\.claude\\helpers\\hook-handler.cjs\" (node \"%CLAUDE_PROJECT_DIR%\\.claude\\helpers\\hook-handler.cjs\" ${sub}) ELSE (node \"%USERPROFILE%\\.claude\\helpers\\hook-handler.cjs\" ${sub})"`; + } + return `sh -c 'D="\${CLAUDE_PROJECT_DIR:-.}"; [ -f "$D/.claude/helpers/hook-handler.cjs" ] || D="\${HOME}"; exec node "$D/.claude/helpers/hook-handler.cjs" ${sub}'`; + }; + const mergedHooks = merged.hooks as Record | undefined; + if (mergedHooks) { + for (const eventName of Object.keys(mergedHooks)) { + const groups = mergedHooks[eventName] as Array<{ hooks?: Array<{ type?: string; command?: string; timeout?: number }> }> | undefined; + if (!Array.isArray(groups)) continue; + for (const group of groups) { + if (!Array.isArray(group.hooks)) continue; + for (const h of group.hooks) { + if (typeof h?.command !== 'string') continue; + const m = BROKEN_HOOK_RE.exec(h.command); + if (!m) continue; + // Subcommand captured (e.g. "pre-bash", "post-edit", "route") — keep it. + h.command = localHookCmd(m[1]); + } + } + } + } + // 4. Merge claudeFlow settings (preserve existing, add agentTeams + memory) const existingClaudeFlow = (existing.claudeFlow as Record) || {}; const existingMemory = (existingClaudeFlow.memory as Record) || {};