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): record failed tool outcomes in core plugin

Rudy Celekli49040a24a969
diff --git a/plugins/ruflo-core/hooks/hooks.json b/plugins/ruflo-core/hooks/hooks.json
index 3193eb687a..1df1f1003f 100644
--- a/plugins/ruflo-core/hooks/hooks.json
+++ b/plugins/ruflo-core/hooks/hooks.json
@@ -41,6 +41,26 @@
]
}
],
+ "PostToolUseFailure": [
+ {
+ "matcher": "Bash",
+ "hooks": [
+ {
+ "type": "command",
+ "command": "node -e \"process.argv=[process.argv[0],'x','post-command'];require(require('path').join(process.env.CLAUDE_PLUGIN_ROOT,'scripts','ruflo-hook.cjs'))\""
+ }
+ ]
+ },
+ {
+ "matcher": "Write|Edit|MultiEdit",
+ "hooks": [
+ {
+ "type": "command",
+ "command": "node -e \"process.argv=[process.argv[0],'x','post-edit'];require(require('path').join(process.env.CLAUDE_PLUGIN_ROOT,'scripts','ruflo-hook.cjs'))\""
+ }
+ ]
+ }
+ ],
"PreCompact": [
{
"matcher": "manual",
diff --git a/plugins/ruflo-core/scripts/fixtures/hook-cli-recorder.cjs b/plugins/ruflo-core/scripts/fixtures/hook-cli-recorder.cjs
index 8749250b75..609623b9ec 100644
--- a/plugins/ruflo-core/scripts/fixtures/hook-cli-recorder.cjs
+++ b/plugins/ruflo-core/scripts/fixtures/hook-cli-recorder.cjs
@@ -7,3 +7,6 @@
const [, , group, subcommand] = process.argv;
if (group !== 'hooks' || !subcommand) process.exit(2);
process.stdout.write(`hook-telemetry:${subcommand}`);
+if (process.env.RUFLO_HOOK_RECORD_ARGS === '1') {
+ process.stdout.write(` ${JSON.stringify(process.argv.slice(2))}`);
+}
diff --git a/plugins/ruflo-core/scripts/ruflo-hook.cjs b/plugins/ruflo-core/scripts/ruflo-hook.cjs
index d15545bf90..8f28ecaeab 100644
--- a/plugins/ruflo-core/scripts/ruflo-hook.cjs
+++ b/plugins/ruflo-core/scripts/ruflo-hook.cjs
@@ -18,7 +18,7 @@
* empty stdout: a bare Cursor permission object is not valid Codex
* hook JSON and is rejected with "hook returned invalid pre-tool-use
* JSON output" (#2816, #2856).
- * - post-command / post-edit (PostToolUse) β€” parse the hook event JSON
+ * - post-command / post-edit (PostToolUse/Failure) β€” parse the hook event JSON
* from stdin (no jq), extract the same fields the bash version pulled
* with jq, and forward them as CLI flags.
* - precompact-manual / precompact-auto (PreCompact) β€” static guidance
@@ -446,19 +446,29 @@ function main() {
done();
}
- // PostToolUse: derive CLI flags from the hook event JSON (replaces jq).
+ // Both completion events use the same commands. Failed tools have no
+ // tool_response.exit_code; Claude Code supplies hook_event_name and an error
+ // beginning with "Exit code N" when a shell exited normally. Never let a
+ // missing code turn PostToolUseFailure into a recorded success (#4127).
if (subcommand === 'post-command') {
if (commandRecordingOff()) done();
const cmd = event?.tool_input?.command;
if (!cmd) done(); // bash version: `[ -z "$CMD" ] && exit 0`
- const exitCode = event?.tool_response?.exit_code ?? 0;
- invokeCli('post-command', ['-c', String(cmd), '-s', String(exitCode === 0), '-e', String(exitCode)], stdinData);
+ const failed = event?.hook_event_name === 'PostToolUseFailure';
+ const errorExit = typeof event?.error === 'string'
+ ? /^Exit code ([1-9]\d*)\b/.exec(event.error)?.[1]
+ : undefined;
+ const parsedExit = Number(errorExit);
+ const exitCode = failed
+ ? (Number.isSafeInteger(parsedExit) && parsedExit > 0 ? parsedExit : 1)
+ : (event?.tool_response?.exit_code ?? 0);
+ invokeCli('post-command', ['-c', String(cmd), '-s', String(!failed && exitCode === 0), '-e', String(exitCode)], stdinData);
done();
}
if (subcommand === 'post-edit') {
const file = event?.tool_input?.file_path ?? event?.tool_input?.path;
if (!file) done(); // bash version: `[ -z "$FILE" ] && exit 0`
- invokeCli('post-edit', ['-f', String(file), '-s', 'true'], stdinData);
+ invokeCli('post-edit', ['-f', String(file), '-s', String(event?.hook_event_name !== 'PostToolUseFailure')], stdinData);
done();
}
diff --git a/plugins/ruflo-core/scripts/test-hooks.mjs b/plugins/ruflo-core/scripts/test-hooks.mjs
index a134620431..9466e3424a 100644
--- a/plugins/ruflo-core/scripts/test-hooks.mjs
+++ b/plugins/ruflo-core/scripts/test-hooks.mjs
@@ -3,7 +3,7 @@
* Regression guard for ruvnet/ruflo#1859, #1862, #2721, #2816, #2856.
*
* Drives every hook command from `hooks/hooks.json` β€” PreToolUse,
- * PostToolUse, PreCompact, Stop β€” with synthetic Claude-Code-style stdin,
+ * PostToolUse, PostToolUseFailure, PreCompact, Stop β€” with synthetic Claude-Code-style stdin,
* against a locally built CLI, executed EXACTLY as Claude Code/Codex would
* run it: `spawnSync(command, { shell: true, ... })`, no bash wrapper of
* our own. That's the point of this rewrite (#2721) β€” the old version
@@ -77,6 +77,8 @@ const cmdModifyBash = findHook('PreToolUse', 'Bash');
const cmdModifyFile = findHook('PreToolUse', 'Write|Edit|MultiEdit');
const cmdPostCommand = findHook('PostToolUse', 'Bash');
const cmdPostEdit = findHook('PostToolUse', 'Write|Edit|MultiEdit');
+const cmdFailedCommand = findHook('PostToolUseFailure', 'Bash');
+const cmdFailedEdit = findHook('PostToolUseFailure', 'Write|Edit|MultiEdit');
const cmdPrecompactManual = findHook('PreCompact', 'manual');
const cmdPrecompactAuto = findHook('PreCompact', 'auto');
const cmdStop = findHook('Stop', undefined);
@@ -254,6 +256,53 @@ run('Bash hook silently no-ops on empty stdin',
'',
[]);
+// #4127: Claude Code sends failed tools to PostToolUseFailure, not PostToolUse.
+// Verify actual plugin registration and the argv passed to the CLI; a hook
+// returning 0 alone would miss the original false-success telemetry bug.
+const recorderOverride = `"${process.execPath}" "${HOOK_RECORDER}"`;
+const recordedArgs = {
+ cliOverride: recorderOverride,
+ // Deduplication is covered separately below; fixed event IDs would otherwise
+ // make a repeated local run skip these argv assertions.
+ env: { RUFLO_HOOK_RECORD_ARGS: '1', RUFLO_DISABLE_HOOK_DEDUP: '1' },
+};
+
+run('Failed Bash hook records non-zero exit and false success',
+ cmdFailedCommand,
+ JSON.stringify({
+ hook_event_name: 'PostToolUseFailure', tool_use_id: 'toolu_failed_bash_4127',
+ tool_input: { command: 'echo failed; exit 3' }, error: 'Exit code 3\nfailed',
+ }),
+ [{ contains: JSON.stringify(['hooks', 'post-command', '-c', 'echo failed; exit 3', '-s', 'false', '-e', '3']) }],
+ recordedArgs);
+
+run('Failed Bash hook without numeric exit is still a failure',
+ cmdFailedCommand,
+ JSON.stringify({
+ hook_event_name: 'PostToolUseFailure', tool_use_id: 'toolu_failed_start_4127',
+ tool_input: { command: 'missing-shell-command' }, error: 'Could not start shell',
+ }),
+ [{ contains: JSON.stringify(['hooks', 'post-command', '-c', 'missing-shell-command', '-s', 'false', '-e', '1']) }],
+ recordedArgs);
+
+run('Failed Edit hook records false success',
+ cmdFailedEdit,
+ JSON.stringify({
+ hook_event_name: 'PostToolUseFailure', tool_use_id: 'toolu_failed_edit_4127',
+ tool_input: { file_path: '/tmp/failed.ts' }, error: 'Permission denied',
+ }),
+ [{ contains: JSON.stringify(['hooks', 'post-edit', '-f', '/tmp/failed.ts', '-s', 'false']) }],
+ recordedArgs);
+
+run('Successful Edit hook still records true success',
+ cmdPostEdit,
+ JSON.stringify({
+ hook_event_name: 'PostToolUse', tool_use_id: 'toolu_success_edit_4127',
+ tool_input: { file_path: '/tmp/success.ts' }, tool_response: {},
+ }),
+ [{ contains: JSON.stringify(['hooks', 'post-edit', '-f', '/tmp/success.ts', '-s', 'true']) }],
+ recordedArgs);
+
// --- PreCompact: pure guidance text, no CLI call ---
run('PreCompact (manual) prints guidance and includes custom instructions',
cmdPrecompactManual,