Filter bash output through rtk before it reaches the model

Register a PreToolUse hook on Bash that rewrites each command through rtk,
so `git status` runs as `rtk git status` and the output is compressed on the
way back. The rewrite is transparent; commands are still written normally.

Measured on this repo, a full `git diff HEAD~3` drops from 521 KB to 28 KB,
a recursive grep over app/src/main from 117 KB to 19 KB, and `git log -20`
from 20 KB to 5.3 KB. Small outputs grow marginally because rtk adds a
header, which at those sizes is not worth avoiding.

The container starts empty, so a SessionStart hook installs the binary first.
It pins RTK_VERSION because the installer's default is to resolve the latest
tag through api.github.com, which the proxy answers 403 for every repository
but this one. Outside the sandbox the hook installs nothing and only prints
the command to run, and every failure path exits clean: with rtk absent the
rewrite no-ops and the only cost is verbose output.

Verified that rtk leaves what must not change alone -- `rtk read` returns
GameSetupScreen.kt byte-for-byte, commit messages keep their quoting, and
already-rewritten commands are not wrapped twice.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Urn6eEBA6XzrnfoibL7faC
This commit is contained in:
Claude
2026-08-08 04:30:36 +00:00
parent f53a628c7a
commit af6f27cf60
4 changed files with 122 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Make sure rtk is on PATH, so the PreToolUse hook in .claude/settings.json has
# something to call. Without this the sandbox starts every session with an empty
# container and the rewrite hook quietly no-ops.
#
# Deliberately never fails: a missing rtk costs tokens, not correctness, and a
# session that refuses to start because a compression tool did not download
# would be a much worse trade.
set -uo pipefail
RTK_VERSION="v0.45.0"
INSTALLER="https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh"
export PATH="$HOME/.local/bin:$PATH"
if command -v rtk >/dev/null 2>&1; then
exit 0
fi
# Only install unattended in the ephemeral web sandbox. On a personal machine,
# say what to run instead — dropping a binary into someone's ~/.local/bin
# without asking is not ours to do.
if [ "${CLAUDE_CODE_REMOTE:-}" != "true" ]; then
echo "rtk is not installed, so bash output will not be compressed."
echo "To install it: curl -fsSL $INSTALLER | sh"
exit 0
fi
# The version has to be pinned. The installer's default is to look up the latest
# tag through api.github.com, which the sandbox proxy answers with a 403 for
# every repository except this one, so the lookup fails and takes the install
# with it. Bump this by hand.
if ! curl -fsSL "$INSTALLER" 2>/dev/null | RTK_VERSION="$RTK_VERSION" sh >/dev/null 2>&1; then
echo "rtk $RTK_VERSION did not install; continuing without output compression."
exit 0
fi
echo "rtk $(rtk --version 2>/dev/null | awk '{print $2}') ready; bash output is compressed."