Skip to content

Add a pre-execution hook for the Bash tool so commands can be rewritten (RTK-style output compression) #1760

Backlog

Description

@ckir

What problem are you trying to solve?

Every shell command the agent runs lands in the model's context in full, and most of that output is noise: full git log bodies, passing test scaffolding, ls -R trees, build logs. Tools like RTK exist to compress that output before the model sees it (README claims 60-90% smaller output on common dev commands), and its Claude Code integration works by rewriting the command itself to rtk <cmd> via a PreToolUse hook.

In Freebuff this does not work automatically. I installed RTK, ran rtk init -g, and confirmed the hook is registered and functional in ~/.claude/settings.json, but the agent's Bash tool never consults it:

  • rtk init --show reports the hook as OK.
  • Piping the hook payload manually returns a valid rewrite:
    {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk git status"}}}
  • But commands issued through the agent's Bash tool still execute verbatim (git status, not rtk git status), and rtk gain --history shows no entries for them, while other agents on the same machine do show entries.

So a user can do all the setup the tool documents and still get none of the benefit. The only workarounds today are prefixing every command by hand (error-prone, and only as good as the model's discipline) or a BASH_ENV shim that shadows git/grep/ls with shell functions (fragile: misses absolute paths, subshells, and builtins).

What would you like to happen?

Add an opt-in pre-execution hook stage to the Bash tool, modeled on Claude Code's PreToolUse contract:

  1. Before spawning bash -c <command>, if a hook is configured, send a JSON payload on stdin:
    {"tool_name":"Bash","tool_input":{"command":"git status"},"cwd":"/path/to/project"}
  2. If the process exits 0 and stdout contains hookSpecificOutput.updatedInput.command, execute that command instead of the original.
  3. Otherwise execute the original command unchanged. A non-zero exit, malformed JSON, or a timeout must degrade to "run the original" — never block or fail the tool.
  4. Record the rewritten command somewhere inspectable (the tool call display or an audit log) so the transcript shows what actually ran.

Configuration could mirror existing conventions: a hooks key in the user/project config file, with the command path, a small timeout (e.g. 2s), and an enable/disable switch. Load only from trusted, explicit config — not from a hook discovered automatically in the repo.

Security considerations (this is the part worth designing carefully)

A hook is arbitrary code execution on every tool call, so I'd suggest:

  • Off by default; enabling requires an explicit config edit and, for project-level hooks, an approval prompt.
  • A hard timeout and no stdin beyond the payload.
  • Hook stderr captured for diagnostics but not injected into the model's context.
  • A documented way to bypass per call (RTK_DISABLED=1 <cmd>, or a tool argument) and to inspect what was rewritten.

RTK is a good test case because its hook is rewrite-only: it never executes the command itself, just emits JSON. That keeps the blast radius small and makes the feature useful beyond RTK (any command-normalizing tool).

Why this is not just "tell the agent to prefix"

CLAUDE.md-style instructions already work and are the documented fallback for instruction-based agents (Codex, Cursor). But they are advisory only: the model can forget, and it cannot know which commands have filters. An execution-layer hook applies uniformly and survives long sessions.

Area

CLI (terminal client)

Contribution

I would be willing to open a PR for this.

Activity

  1. added
    area:cli
    bot:triaged
    type:feature
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibot:triagedtype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions