Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(hooks): pre-bash TypeError + global-install MODULE_NOT_FOUND (#1944, #1943) (#1957)

rUv3657a693662d
diff --git a/.claude/helpers/hook-handler.cjs b/.claude/helpers/hook-handler.cjs
index 823167b951..f7907e3573 100644
--- a/.claude/helpers/hook-handler.cjs
+++ b/.claude/helpers/hook-handler.cjs
@@ -100,7 +100,10 @@ async function main() {
var toolInput = hookInput.toolInput || hookInput.tool_input || {};
var toolName = hookInput.toolName || hookInput.tool_name || '';
- var prompt = hookInput.prompt || hookInput.command || toolInput
+ // `toolInput` is an object (e.g. {command:"ls"}) β€” falling back to it
+ // directly bound `prompt` to the object and tripped `.toLowerCase()` /
+ // `.substring()` on every Bash hook (#1944). Use the `.command` field.
+ var prompt = hookInput.prompt || hookInput.command || toolInput.command
|| process.env.PROMPT || process.env.TOOL_INPUT_command || '';
const handlers = {
diff --git a/.github/workflows/v3-ci.yml b/.github/workflows/v3-ci.yml
index 5e1dfd338e..f02868219d 100644
--- a/.github/workflows/v3-ci.yml
+++ b/.github/workflows/v3-ci.yml
@@ -545,6 +545,10 @@ jobs:
shell: bash
run: node scripts/audit-hook-commands.mjs
+ - name: Run hook-handler prompt-resolution audit (#1944)
+ shell: bash
+ run: node scripts/audit-hook-handler-prompt.mjs
+
- name: Lint the ruflo-hook shims
shell: bash
run: |
diff --git a/scripts/audit-hook-handler-prompt.mjs b/scripts/audit-hook-handler-prompt.mjs
new file mode 100755
index 0000000000..a700934b7a
--- /dev/null
+++ b/scripts/audit-hook-handler-prompt.mjs
@@ -0,0 +1,87 @@
+#!/usr/bin/env node
+/**
+ * Hook-handler prompt-resolution audit β€” regression guard for #1944.
+ *
+ * Claude Code sends `pre-bash`/`pre-edit`/etc. hooks a JSON payload like
+ * `{"tool_input":{"command":"ls"},"tool_name":"Bash"}` on stdin. The hook
+ * handler in `helpers/hook-handler.cjs` (and its template in
+ * `helpers-generator.ts`) builds a single `prompt` string from a fallback
+ * chain. If it falls back to the **object** form β€” `hookInput.toolInput`
+ * or the locally-normalised `toolInput` β€” instead of `.command`, the prompt
+ * gets bound to an object and the very next call (`.toLowerCase()`,
+ * `.substring()`) throws on every Bash tool call.
+ *
+ * The fix is to fall back to `toolInput.command` (the actual string). This
+ * guard fails CI if the regression returns: any line that contains
+ * `|| <something>toolInput` (with no `.` after `toolInput`) inside a hook
+ * handler / generator source.
+ *
+ * Usage:
+ * node scripts/audit-hook-handler-prompt.mjs # exit 1 on any hit
+ * node scripts/audit-hook-handler-prompt.mjs --json # machine-readable report
+ */
+
+import { readFileSync, readdirSync, statSync } from 'node:fs';
+import { join, relative } from 'node:path';
+
+const REPO_ROOT = process.cwd();
+const JSON_OUT = process.argv.includes('--json');
+
+// Files we audit. The deployed/tracked `.cjs` files + the TS template that
+// generates them at `ruflo init` time.
+const TARGETS = [
+ 'v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs',
+ '.claude/helpers/hook-handler.cjs',
+ 'v3/@claude-flow/cli/src/init/helpers-generator.ts',
+];
+
+// `||` followed by an identifier ending in `toolInput`, NOT followed by `.`
+// (i.e. the object form, not the safe `toolInput.command` form). Catches:
+// - `|| toolInput` (post-normalisation local form)
+// - `|| hookInput.toolInput` (raw stdin form)
+// - `|| (anything).toolInput` (defensively)
+// Also catches `|| toolInput\s|$` to cover the multi-line wrap from the
+// reporter's repro (`|| toolInput\n || process.env.PROMPT`).
+const BAD = /\|\|\s*([A-Za-z_$][\w$]*\.)?toolInput\b(?!\.[A-Za-z_$])/g;
+
+const offenders = [];
+for (const rel of TARGETS) {
+ const p = join(REPO_ROOT, rel);
+ let src;
+ try {
+ statSync(p);
+ src = readFileSync(p, 'utf8');
+ } catch {
+ // Missing files are fine β€” they may not exist in every checkout.
+ continue;
+ }
+ let m;
+ BAD.lastIndex = 0;
+ while ((m = BAD.exec(src)) !== null) {
+ const line = src.slice(0, m.index).split('\n').length;
+ const lineText = src.split('\n')[line - 1]?.trim() ?? '';
+ offenders.push({ file: rel, line, match: m[0], context: lineText });
+ }
+}
+
+if (JSON_OUT) {
+ process.stdout.write(JSON.stringify({ offenders }, null, 2) + '\n');
+ process.exit(offenders.length === 0 ? 0 : 1);
+}
+
+console.log(`hook-handler prompt-resolution audit β€” guard for #1944`);
+console.log(` scanned ${TARGETS.length} target(s)`);
+
+if (offenders.length === 0) {
+ console.log(` βœ“ no `+`'|| <…>toolInput' (object) fallbacks found`);
+ process.exit(0);
+}
+
+console.error(`\n βœ— ${offenders.length} offending fallback(s) β€” #1944 regression:`);
+for (const o of offenders) {
+ console.error(` ${o.file}:${o.line}`);
+ console.error(` match: ${o.match}`);
+ console.error(` context: ${o.context}`);
+}
+console.error('\n Fix: replace `|| <…>toolInput` with `|| <…>toolInput.command` (or pull `.command` off whichever stdin shape Claude Code sent β€” `tool_input.command` / `toolInput.command`).');
+process.exit(1);
diff --git a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
index 740c44c16f..c8a7668d17 100644
--- a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
+++ b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
@@ -112,8 +112,12 @@ async function main() {
const toolInput = hookInput.toolInput || hookInput.tool_input || {};
const toolName = hookInput.toolName || hookInput.tool_name || '';
- // Merge stdin data into prompt resolution: prefer stdin fields, then env, then argv
- const prompt = hookInput.prompt || hookInput.command || toolInput
+ // Merge stdin data into prompt resolution: prefer stdin fields, then env, then argv.
+ // `toolInput` is an object (e.g. {command:"ls"}) β€” it's truthy but not a string,
+ // so falling back to it directly bound `prompt` to the object and tripped
+ // `.toLowerCase()` / `.substring()` on every Bash hook (#1944). Use the
+ // `.command` field instead, which is the actual string the hook needs.
+ const prompt = hookInput.prompt || hookInput.command || toolInput.command
|| process.env.PROMPT || process.env.TOOL_INPUT_command || args.join(' ') || '';
const handlers = {
diff --git a/v3/@claude-flow/cli/src/init/helpers-generator.ts b/v3/@claude-flow/cli/src/init/helpers-generator.ts
index e29882efaa..6cdd84949b 100644
--- a/v3/@claude-flow/cli/src/init/helpers-generator.ts
+++ b/v3/@claude-flow/cli/src/init/helpers-generator.ts
@@ -432,8 +432,12 @@ export function generateHookHandler(): string {
' if (stdinData.trim()) {',
' try { hookInput = JSON.parse(stdinData); } catch (e) { /* ignore */ }',
' }',
- ' // Prefer stdin fields, then env, then argv',
- " var prompt = hookInput.prompt || hookInput.command || hookInput.toolInput || process.env.PROMPT || process.env.TOOL_INPUT_command || args.join(' ') || '';",
+ ' // Prefer stdin fields, then env, then argv. `hookInput.toolInput` is an',
+ ' // object (e.g. {command:"ls"}); falling back to it directly bound prompt',
+ ' // to the object and tripped .toLowerCase() / .substring() on every Bash',
+ ' // hook (#1944). Pull `.command` off whichever stdin shape Claude Code sent.',
+ ' var toolInputObj = hookInput.toolInput || hookInput.tool_input || {};',
+ " var prompt = hookInput.prompt || hookInput.command || toolInputObj.command || process.env.PROMPT || process.env.TOOL_INPUT_command || args.join(' ') || '';",
'',
'const handlers = {',
" 'route': () => {",
diff --git a/v3/@claude-flow/cli/src/init/settings-generator.ts b/v3/@claude-flow/cli/src/init/settings-generator.ts
index 3babb7d093..2c00009833 100644
--- a/v3/@claude-flow/cli/src/init/settings-generator.ts
+++ b/v3/@claude-flow/cli/src/init/settings-generator.ts
@@ -168,21 +168,35 @@ export function generateSettings(options: InitOptions): object {
const IS_WINDOWS = process.platform === 'win32';
/**
- * Build a hook command with reliable $CLAUDE_PROJECT_DIR expansion.
- * Wraps in `sh -c` to guarantee shell expansion on all platforms (macOS zsh,
- * Linux bash). Falls back to "." if CLAUDE_PROJECT_DIR is unset, since
- * Claude Code runs hooks from the project root.
- * On Windows, uses `cmd /c` with %CLAUDE_PROJECT_DIR%.
+ * Build a hook command that resolves to the right helpers dir on every
+ * install layout. `ruflo init` can land helpers either project-locally
+ * (`<project>/.claude/helpers/…`, when run from a project root) or globally
+ * (`$HOME/.claude/helpers/…`, when settings.json gets merged into the
+ * user-level Claude Code config). The earlier `${CLAUDE_PROJECT_DIR:-.}`
+ * form assumed project-local β€” so any global-install user hit
+ * `MODULE_NOT_FOUND` on every Bash/Edit/Session hook (#1943).
+ *
+ * The fix is a tiny POSIX `sh` probe: try `$CLAUDE_PROJECT_DIR/.claude/...`
+ * first, fall back to `$HOME/.claude/...` if it's missing. Both modes work,
+ * the global install never crashes, and project-local overrides still take
+ * precedence when present. On Windows, the same probe via `cmd /c` (the %~%
+ * fallback uses `IF EXIST`).
*/
function hookCmd(script: string, subcommand: string): string {
if (IS_WINDOWS) {
- return `cmd /c node %CLAUDE_PROJECT_DIR%/${script} ${subcommand}`.trim();
+ // cmd.exe equivalent of the sh probe below. `IF EXIST` checks the
+ // project-local path; falls back to %USERPROFILE% if missing.
+ return `cmd /c "IF EXIST \"%CLAUDE_PROJECT_DIR%\\${script.replace(/\//g, '\\')}\" (node \"%CLAUDE_PROJECT_DIR%\\${script.replace(/\//g, '\\')}\" ${subcommand}) ELSE (node \"%USERPROFILE%\\${script.replace(/\//g, '\\')}\" ${subcommand})"`;
}
- // Use sh -c to ensure $CLAUDE_PROJECT_DIR is expanded by a real shell,
- // even if Claude Code doesn't invoke hooks through a shell on macOS.
+ // POSIX sh: prefer project-local helpers, fall back to $HOME/.claude/.
+ // The fallback handles `ruflo init`'s global-install path where helpers
+ // live at `$HOME/.claude/helpers/` but Claude Code still sets
+ // `CLAUDE_PROJECT_DIR` to the *project* root (which has no helpers).
// eslint-disable-next-line no-template-curly-in-string
- const dir = '${CLAUDE_PROJECT_DIR:-.}';
- return `sh -c 'exec node "${dir}/${script}" ${subcommand}'`;
+ const projVar = '${CLAUDE_PROJECT_DIR:-.}';
+ // eslint-disable-next-line no-template-curly-in-string
+ const homeVar = '${HOME}';
+ return `sh -c 'D="${projVar}"; [ -f "$D/${script}" ] || D="${homeVar}"; exec node "$D/${script}" ${subcommand}'`;
}
/** Shorthand for CJS hook-handler commands */
@@ -205,11 +219,18 @@ function generateStatusLineConfig(_options: InitOptions): object {
// The script runs after each assistant message (debounced 300ms).
// NOTE: statusline must NOT use `cmd /c` β€” Claude Code manages its stdin
// directly for statusline commands, and `cmd /c` blocks stdin forwarding.
+ //
+ // Same project-local / $HOME fallback as `hookCmd()` (see #1943): the
+ // earlier `${CLAUDE_PROJECT_DIR:-.}` form broke statusline for any
+ // global-install user. Probe project-local first, fall back to $HOME.
+ const script = '.claude/helpers/statusline.cjs';
+ // eslint-disable-next-line no-template-curly-in-string
+ const projVar = '${CLAUDE_PROJECT_DIR:-.}';
// eslint-disable-next-line no-template-curly-in-string
- const dir = '${CLAUDE_PROJECT_DIR:-.}';
+ const homeVar = '${HOME}';
return {
type: 'command',
- command: `sh -c 'exec node "${dir}/.claude/helpers/statusline.cjs"'`,
+ command: `sh -c 'D="${projVar}"; [ -f "$D/${script}" ] || D="${homeVar}"; exec node "$D/${script}"'`,
};
}