| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # PODNAME: mcp-run-compress | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Claude Code hooks that run Bash tool output through MCP::Run::Compress | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 44 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 44 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 44 | ▶ | use Getopt::Long qw( GetOptions ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 44 | ▶ | use Encode qw( encode_utf8 decode_utf8 ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # Only core modules are loaded at compile time. JSON::MaybeXS, Path::Tiny | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # and the MCP::Run::* classes are required at RUNTIME by the path that | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # needs them, so a broken or half-upgraded install cannot kill the script | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # before a single line of runtime code has run. See run_hook(). karr #19. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # The pass-through hook responses as literals: emitting one must not depend | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # on JSON::MaybeXS being loadable, because that is exactly what may be | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # missing when we need it. hookEventName has to match the event that is | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # actually firing — with the wrong name Claude Code drops the whole | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # response silently. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 44 | ▶ | use constant HOOK_PASSTHROUGH_PRE => '{"hookSpecificOutput":{"hookEventName":"PreToolUse"}}'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 44 | ▶ | use constant HOOK_PASSTHROUGH_POST => '{"hookSpecificOutput":{"hookEventName":"PostToolUse"}}'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # What PreToolUse leaves behind in the command when the user typed the | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # `no-compress ` prefix, and what PostToolUse looks for. PostToolUse sees | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # the command as PreToolUse rewrote it, never the user's original, so the | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # wish has to survive inside the command itself. A trailing comment line is | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # the only carrier that is invisible to bash and leaves the command's first | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # word — and with it Claude Code's permission prefix matching — intact. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 44 | ▶ | use constant NO_COMPRESS_MARKER => '# mcp-run-compress: no-compress'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Claude Code caps a Bash tool_response.stdout at 30000 bytes and persists | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # anything longer to a file. That cap applies to the REPLACEMENT too: | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # an updatedToolOutput above it is written to a fresh tool-results file and | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # the model is shown a 2 KB preview of it. A compressor that returns 40 KB | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # therefore delivers 2 KB. Everything we emit has to stay below the cap, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # with headroom for the harness's own framing. karr #9, karr #16. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 44 | ▶ | use constant OUTPUT_BYTE_CAP => 29000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Ceiling on the persisted file we are willing to pull into memory and push | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # through 30+ regex stages inside a hook. Above it we leave the harness's | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # own <persisted-output> framing alone: a 2 KB preview plus a path beats | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # spending seconds and hundreds of megabytes to produce the same 29 KB. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 44 | ▶ | use constant MAX_PERSISTED_READ => 4_000_000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Checked before GetOptions can fail on it: a hook entry carrying an option | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # this version no longer knows must not make the script exit non-zero. For | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # PreToolUse a non-zero exit does not mean "error", it means "block the | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # tool call" — on every single Bash command in every session. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 44 | my $hook_requested = grep { $_ eq '--hook' } @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 44 | my ( $hook, $install, $help ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 'hook' => \$hook, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 'install-claude' => \$install, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 'help|h' => \$help, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 44 | ▶ | ) or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if ($hook_requested) { $hook = 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | else { print_help(); exit 2 } | |||||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 44 | ▶ | if ($hook) { exit run_hook() } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ($install) { exit install_claude() } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | print_help(); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | # 'docker' means: install_claude writes a hook that spawns a docker | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # container instead of calling the script directly. Baked into the Docker | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # image via ENV; unset on a native Perl install. Override manually by | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # exporting the env var. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | sub install_mode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
install_mode: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | ▶ | return $ENV{MCP_RUN_COMPRESS_INSTALL_MODE} // 'native'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"} // "native"
MC/DC0%0/1
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"} // "native"
$ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"}
Truth table50%1/2
A
A= $ENV{"MCP_RUN_COMPRESS_INSTALL_MODE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | sub image_ref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
image_ref: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | return $ENV{MCP_RUN_COMPRESS_IMAGE} // 'raudssus/mcp-run-compress:latest'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"MCP_RUN_COMPRESS_IMAGE"} // "raudssus/mcp-run-compress:latest"
MC/DC100%1/1
$ENV{"MCP_RUN_COMPRESS_IMAGE"} // "raudssus/mcp-run-compress:latest"
$ENV{"MCP_RUN_COMPRESS_IMAGE"}
Truth table100%2/2
A
A= $ENV{"MCP_RUN_COMPRESS_IMAGE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # The one command string written into settings.json for BOTH events. The | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # script decides what to do from the payload's hook_event_name, so there is | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # nothing to keep in sync between the two entries. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # --network none: the hook reads stdin and writes stdout, nothing else. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # karr #14. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 10 | ▶ | sub hook_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hook_command: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 10 | ▶ | return install_mode() eq 'docker' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | ? 'docker run --rm -i --network none ' . image_ref() . ' --hook' | ||||||||||||||||||||||||||||||||||||||||||
| 88 | : 'mcp-run-compress --hook'; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 9 | ▶ | sub output_byte_cap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_byte_cap: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | my $cap = $ENV{MCP_RUN_COMPRESS_MAX_BYTES}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 9 | ▶ | return OUTPUT_BYTE_CAP | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $cap and $cap =~ /\A[0-9]+\z/ and $cap > 0
defined $cap and $cap =~ /\A[0-9]+\z/
MC/DC33%1/3
defined $cap and $cap =~ /\A[0-9]+\z/ and $cap > 0
defined $cap
$cap =~ /\A[0-9]+\z/
$cap > 0
Truth table50%2/4
A and B and C
A= defined $capB= $cap =~ /\A[0-9]+\z/C= $cap > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | unless defined $cap && $cap =~ /\A[0-9]+\z/ && $cap > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | return $cap; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 34 | ▶ | sub _json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 34 | return JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 34 | ▶ | sub _bytelen { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bytelen: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 34 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 34 | ▶ | return 0 unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 34 | return length encode_utf8($text); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # FAIL OPEN. A PreToolUse hook that exits non-zero tells Claude Code to | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # BLOCK the tool call, so anything this script cannot do must cost the user | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # compression, never their shell: on any failure we emit the pass-through | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # response and exit 0. Uncompressed output is a small harm, a dead Bash tool | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # in every session is a large one. That is why the modules are required at | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # runtime (below) rather than used at compile time — a `use` failure happens | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # before any eval here could catch it. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # The same applies to PostToolUse, for a smaller but real reason: the | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # command has already run, so a dying hook cannot cost the user their | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # output — but it can print a stack trace after every Bash call, which is | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # precisely the noise this product exists to remove. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # Deliberately the OPPOSITE of lib/MCP/Run.pm, which loads MCP::Run::Compress | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # at COMPILE time so a broken compressor kills the server at startup instead | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # of at the user's first tools/call (Sharp Edge in CLAUDE.md, Changes 0.106). | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # Server: fail fast, it is a startup concern. Hook: fail open, it sits in the | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # critical path of every Bash call. Do NOT align these two — same as the two | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # compression defaults that intentionally differ. karr #19. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 34 | ▶ | sub run_hook { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_hook: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | # Drain stdin before anything can fail: whatever goes wrong below, the | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # caller writing the hook payload must not hit a closed pipe. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 34 | my $raw = do { local $/; <STDIN> }; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 34 | my $response = eval { hook_response($raw) }; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 34 | ▶ | if ( !defined $response ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | print STDERR 'mcp-run-compress: compression unavailable, output passed ' | |||||||||||||||||||||||||||||||||||||||||
| 136 | . 'through unchanged: ' . _brief_error($@) . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $response = _passthrough_for($raw); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 34 | print $response; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 34 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # Picks the pass-through literal by looking at the raw payload text rather | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # than parsed JSON: this runs on the path where JSON::MaybeXS is what failed | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # to load. A pass-through with the wrong hookEventName would be dropped, and | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # a dropped pass-through is still a pass-through — but getting it right | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # keeps Claude Code from logging a mismatch on every command. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub _passthrough_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_passthrough_for: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | my ($raw) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return HOOK_PASSTHROUGH_POST | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $raw and $raw =~ /"hook_event_name"\s*:\s*"PostToolUse"/
MC/DC0%0/2
defined $raw and $raw =~ /"hook_event_name"\s*:\s*"PostToolUse"/
defined $raw
$raw =~ /"hook_event_name"\s*:\s*"PostToolUse"/
Truth table0%0/3
A and B
A= defined $rawB= $raw =~ /"hook_event_name"\s*:\s*"PostToolUse"/
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | if defined $raw && $raw =~ /"hook_event_name"\s*:\s*"PostToolUse"/; | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | return HOOK_PASSTHROUGH_PRE; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | # First line of an error, without the "at FILE line N." tail. The hook runs | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # on every single Bash command; a full Perl stack trace on stderr would be | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # exactly the noise this product exists to remove. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _brief_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_brief_error: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my ($err) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $err = 'unknown error' unless defined $err && length $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $err and length $err
MC/DC0%0/2
defined $err and length $err
defined $err
length $err
Truth table0%0/3
A and B
A= defined $errB= length $err
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ($err) = split /\n/, $err; | |||||||||||||||||||||||||||||||||||||||||
| 163 | # "Can't locate Foo.pm in @INC" drags the entire @INC listing along; the | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # module name is the whole diagnosis, the rest is a screenful. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $err =~ s/\s*\(\@INC entries checked:[^)]*\)//; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $err =~ s/\s*\(you may need to install the \S+ module\)//; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $err =~ s/\s+at\s+\S+\s+line\s+\d+[^\n]*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $err =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $err = substr( $err, 0, 120 ) . '...' if length $err > 123; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return length $err ? $err : 'unknown error'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Builds the hook response string. Dies if anything is missing — run_hook() | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # turns that into the pass-through response. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 34 | ▶ | sub hook_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hook_response: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 34 | my ($raw) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 34 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 34 | my $json = _json(); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 34 | ▶ | my $in = eval { $json->decode($raw) } || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
$json->decode($raw)
}
} || {}
MC/DC100%1/1
eval {
do {
$json->decode($raw)
}
} || {}
eval {
do {
$json->decode($raw)
}
}
Truth table100%2/2
A
A= eval {
do {
$json->decode($raw)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 34 | my $event = $in->{hook_event_name}; | |||||||||||||||||||||||||||||||||||||||||
| 182 | # No event name (hand-written payload, older harness): a tool_response is | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # only ever present after the tool ran. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 34 | ▶ | $event = ref $in->{tool_response} eq 'HASH' ? 'PostToolUse' : 'PreToolUse' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $event and length $event
MC/DC50%1/2
defined $event and length $event
defined $event
length $event
Truth table66%2/3
A and B
A= defined $eventB= length $event
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | unless defined $event && length $event; | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 34 | ▶ | return post_tool_use_response( $json, $in ) if $event eq 'PostToolUse'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 18 | ▶ | return pre_tool_use_response( $json, $in ) if $event eq 'PreToolUse'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # Anything else — PostToolUseFailure above all — has nothing for us to do. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # Answer with its own event name so the response is not silently dropped. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | return $json->encode( { hookSpecificOutput => { hookEventName => $event } } ); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # PreToolUse has exactly two jobs left, and both of them MUST happen before | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # the command runs: the Co-Authored-By rewrite, and turning the user's | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # `no-compress ` prefix into a marker PostToolUse can still see. Compression | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # itself moved to PostToolUse — the command is no longer touched for it. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 17 | ▶ | sub pre_tool_use_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pre_tool_use_response: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 17 | my ( $json, $in ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 17 | my $out = { hookSpecificOutput => { hookEventName => 'PreToolUse' } }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 17 | my $cmd = $in->{tool_input}{command}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 17 | ▶ | return $json->encode($out) if !defined $cmd || !length $cmd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cmd and length $cmd
MC/DC50%1/2
defined $cmd and length $cmd
defined $cmd
length $cmd
Truth table66%2/3
A and B
A= defined $cmdB= length $cmd
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | # The two jobs are independent and must not ride on one flag. Everything | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # about `no-compress` and background jobs governs COMPRESSION. The | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # Co-Authored-By rewrite is a commit policy: it applies to every command | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # and answers to exactly one switch, MCP_RUN_COMPRESS_NO_CO_AUTHORED, | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # which transform_command honours itself. karr #26. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 16 | my $updated = $cmd; | |||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 16 | my $no_compress = 0; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 16 | ▶ | if ( $updated =~ /\A\s*no-compress\s+(\S.*)\z/s ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 4 | $updated = $1; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | $no_compress = 1; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | # No self-bypass here, deliberately, and unlike the PostToolUse side. | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # Until 0.106 this hook rewrote the command into a compressing wrapper | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # and had to avoid wrapping its own wrapper; it does not rewrite anything | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # for compression any more, so the only thing a name match still switched | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # off was the trailer. `git commit -m "Fix mcp-run-compress hook"` losing | ||||||||||||||||||||||||||||||||||||||||||
| 224 | # its Co-Authored-By line, silently, is not a bypass anyone asked for — | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # and in this repository that message is the common case. karr #26. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 16 | require MCP::Run::Compress; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 16 | $updated = MCP::Run::Compress->new->transform_command($updated); | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | # Marker last, so it stays the final line of whatever the transform | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # produced. Skipped for background commands: their output never reaches | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # PostToolUse as tool output, so the marker would have nothing to tell. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | $updated = mark_no_compress($updated) | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 16 | ▶ | if $no_compress && !$in->{tool_input}{run_in_background}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$no_compress and not $in->{"tool_input"}{"run_in_background"}
MC/DC100%2/2
$no_compress and not $in->{"tool_input"}{"run_in_background"}
$no_compress
not $in->{"tool_input"}{"run_in_background"}
Truth table100%3/3
A and not(B)
A= $no_compressB= not $in->{"tool_input"}{"run_in_background"}
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | # Only claim an updatedInput when something actually changed. An | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # updatedInput identical to the input on every Bash call is noise that | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # Claude Code has to re-run its permission matching over for nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 16 | ▶ | $out->{hookSpecificOutput}{updatedInput} = { command => $updated } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | if $updated ne $cmd; | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 16 | return $json->encode($out); | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # Appends the marker as its own trailing comment line. Measured against 24 | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # command shapes (multiline, heredocs incl. <<- and quoted, double heredocs, | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # functions, if/case/while blocks, subshells, line continuations, background | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # jobs): output and exit code stay byte-identical in all of them but two. | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # * a command ending in a dangling backslash — guarded below, the marker | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # would be spliced onto the last command line by the line continuation | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # * an UNTERMINATED heredoc, where the marker lands in the heredoc body. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # bash already warns about that command; not guarded, see POD. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # Dropping the marker costs the bypass for that one call (the output gets | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # compressed), never the command. | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | sub mark_no_compress { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mark_no_compress: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | my ($cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | ▶ | return $cmd if $cmd =~ /(?<!\\)(?:\\\\)*\\\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | return $cmd . "\n" . NO_COMPRESS_MARKER; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # PostToolUse is where compression happens now. The command has already run; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # we replace its recorded output with the compressed version through | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # hookSpecificOutput.updatedToolOutput. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # LIMIT, by design and not by omission: this never sees a command that | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # exited non-zero. Claude Code fires PostToolUseFailure for those, that | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # payload carries no tool_response at all, and updatedToolOutput is ignored | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # there (four shapes tested, karr #9). Failing builds — the loudest output | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # there is — arrive uncompressed. Making that visible in --help and POD is | ||||||||||||||||||||||||||||||||||||||||||
| 271 | # part of the deal; hiding it is not. | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 16 | ▶ | sub post_tool_use_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_tool_use_response: called CC=23 SCAR=31.6
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 16 | my ( $json, $in ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 16 | my $out = { hookSpecificOutput => { hookEventName => 'PostToolUse' } }; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 16 | my $resp = $in->{tool_response}; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 16 | ▶ | return $json->encode($out) unless ref $resp eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 16 | ▶ | my $cmd = $in->{tool_input}{command} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$in->{"tool_input"}{"command"} // ""
MC/DC0%0/1
$in->{"tool_input"}{"command"} // ""
$in->{"tool_input"}{"command"}
Truth table50%1/2
A
A= $in->{"tool_input"}{"command"}
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | # Background commands: the hook fires at START, not at completion. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # stdout/stderr are empty and the response is the "running in background" | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # notice — compressing it would only glue a header onto that notice. The | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # real output arrives later over the task file and is not a Bash tool | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # response any more. noOutputExpected is present-but-false on ordinary | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # responses, so this has to test truth, not existence. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | return $json->encode($out) | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 16 | ▶ | if $resp->{backgroundTaskId} || $resp->{noOutputExpected}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$resp->{"backgroundTaskId"} or $resp->{"noOutputExpected"}
MC/DC100%2/2
$resp->{"backgroundTaskId"} or $resp->{"noOutputExpected"}
$resp->{"backgroundTaskId"}
$resp->{"noOutputExpected"}
Truth table100%3/3
A or B
A= $resp->{"backgroundTaskId"}B= $resp->{"noOutputExpected"}
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | # The user asked for raw output. Anchored at the end: only the marker | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # PreToolUse appended counts, never the same text quoted inside a heredoc | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # body somewhere in the middle of the command. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 13 | ▶ | return $json->encode($out) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 13 | if $cmd =~ /\n\Q${\ NO_COMPRESS_MARKER }\E[ \t]*\z/; | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | # Self-bypass: a stale PreToolUse hook from an older install that still | ||||||||||||||||||||||||||||||||||||||||||
| 297 | # rewrites commands into a compressing wrapper. Whatever it produced is | ||||||||||||||||||||||||||||||||||||||||||
| 298 | # compressed already and doing it twice is worse than not at all. | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # Narrower than the PreToolUse bypass, and on purpose. Matching every | ||||||||||||||||||||||||||||||||||||||||||
| 301 | # command that merely NAMES this tool would switch compression off for | ||||||||||||||||||||||||||||||||||||||||||
| 302 | # `grep -r mcp-run-compress`, for editing this script, and for the marker | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # line above — silently, since an uncompressed output looks like any | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # other. Only the two flags of the rewrite era identify an invocation | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # that already compressed something. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 12 | ▶ | return $json->encode($out) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cmd =~ /\bmcp-run-compress\b/ and $cmd =~ /--(?:b64|filter-files)\b/
MC/DC100%2/2
$cmd =~ /\bmcp-run-compress\b/ and $cmd =~ /--(?:b64|filter-files)\b/
$cmd =~ /\bmcp-run-compress\b/
$cmd =~ /--(?:b64|filter-files)\b/
Truth table100%3/3
A and B
A= $cmd =~ /\bmcp-run-compress\b/B= $cmd =~ /--(?:b64|filter-files)\b/
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | if $cmd =~ /\bmcp-run-compress\b/ && $cmd =~ /--(?:b64|filter-files)\b/; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 11 | ▶ | my $stdout = defined $resp->{stdout} ? $resp->{stdout} : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # Above 30000 bytes the harness truncates stdout and puts the full text | ||||||||||||||||||||||||||||||||||||||||||
| 312 | # in a file, which is readable and complete while the hook runs. Reading | ||||||||||||||||||||||||||||||||||||||||||
| 313 | # it is what lets compression work on the whole output instead of on a | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # prefix — and deleting the two persist fields from the replacement is | ||||||||||||||||||||||||||||||||||||||||||
| 315 | # what removes the <persisted-output> framing with its 2 KB preview. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 11 | my ( $raw_path, $raw_unread ); | |||||||||||||||||||||||||||||||||||||||||
| 317 | 11 | ▶ | if ( defined $resp->{persistedOutputPath} && length $resp->{persistedOutputPath} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $resp->{"persistedOutputPath"} and length $resp->{"persistedOutputPath"}
MC/DC50%1/2
defined $resp->{"persistedOutputPath"} and length $resp->{"persistedOutputPath"}
defined $resp->{"persistedOutputPath"}
length $resp->{"persistedOutputPath"}
Truth table66%2/3
A and B
A= defined $resp->{"persistedOutputPath"}B= length $resp->{"persistedOutputPath"}
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 5 | $raw_path = $resp->{persistedOutputPath}; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 5 | my $size = $resp->{persistedOutputSize}; | |||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 5 | ▶ | if ( defined $size && $size =~ /\A[0-9]+\z/ && $size > MAX_PERSISTED_READ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $size and $size =~ /\A[0-9]+\z/ and $size > 4000000
defined $size and $size =~ /\A[0-9]+\z/
MC/DC33%1/3
defined $size and $size =~ /\A[0-9]+\z/ and $size > 4000000
defined $size
$size =~ /\A[0-9]+\z/
$size > 4000000
Truth table50%2/4
A and B and C
A= defined $sizeB= $size =~ /\A[0-9]+\z/C= $size > 4000000
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | # Too large to be worth a hook's memory and CPU. The harness's own | ||||||||||||||||||||||||||||||||||||||||||
| 323 | # framing already points at the file; leave it in place. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | return $json->encode($out); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 4 | my $full = eval { _slurp_utf8($raw_path) }; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 4 | ▶ | if ( defined $full ) { $stdout = $full } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # Unreadable. In docker mode that is the normal case, not an error: | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # persistedOutputPath is a HOST path and the container cannot see it. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | # Mounting it is not on the table — the file lives in | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # ~/.claude/projects/<project>/<session>/tool-results/, so the only | ||||||||||||||||||||||||||||||||||||||||||
| 334 | # ancestor known at install time is ~/.claude/projects, which holds | ||||||||||||||||||||||||||||||||||||||||||
| 335 | # every session transcript of every project. We compress the 30000 | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # bytes we were given, still drop the framing, and say so in the text: | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # 29 KB compressed beats a 2 KB preview. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 1 | else { $raw_unread = 1 } | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 10 | ▶ | return $json->encode($out) unless length $stdout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 10 | require MCP::Run::Compress; | |||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | # stderr is deliberately not fed to the compressor. Claude Code merges | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # both streams of the command into stdout; tool_response.stderr carries | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # harness notices ("Shell cwd was reset"), not command output, and a | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # filter with filter_stderr would fold those into the command's output. | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 10 | my ($compressed) = MCP::Run::Compress->new->compress( $cmd, $stdout, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 350 | 10 | ▶ | $compressed = '' unless defined $compressed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | # Nothing gained and nothing to re-frame: leave the response alone. Any | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # updatedToolOutput we return is a claim over the whole output, and a | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # no-op claim is one more thing that can go wrong. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 10 | ▶ | return $json->encode($out) if !defined $raw_path && $compressed eq $stdout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $raw_path and $compressed eq $stdout
MC/DC100%2/2
not defined $raw_path and $compressed eq $stdout
not defined $raw_path
$compressed eq $stdout
Truth table100%3/3
not(A) and B
A= not defined $raw_pathB= $compressed eq $stdout
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 9 | my $header = _provenance_header( $raw_path, _bytelen($stdout), $raw_unread ); | |||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | # Without a raw file on disk there is nothing to point the model at, so | ||||||||||||||||||||||||||||||||||||||||||
| 360 | # cutting would destroy output the harness was already willing to show | ||||||||||||||||||||||||||||||||||||||||||
| 361 | # inline. The cap can then only raise, never lower. | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 9 | my $limit = output_byte_cap(); | |||||||||||||||||||||||||||||||||||||||||
| 363 | 9 | ▶ | $limit = _bytelen($stdout) if !defined $raw_path && _bytelen($stdout) > $limit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $raw_path and _bytelen($stdout) > $limit
MC/DC100%2/2
not defined $raw_path and _bytelen($stdout) > $limit
not defined $raw_path
_bytelen($stdout) > $limit
Truth table100%3/3
not(A) and B
A= not defined $raw_pathB= _bytelen($stdout) > $limit
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 9 | ▶ | $limit -= _bytelen($header) + 1 if length $header; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 9 | $compressed = _cap_output( $compressed, $limit, $raw_path ); | |||||||||||||||||||||||||||||||||||||||||
| 367 | 9 | ▶ | $compressed = $header . "\n" . $compressed if length $header; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 9 | my %updated = %$resp; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 9 | $updated{stdout} = $compressed; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 9 | delete $updated{persistedOutputPath}; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 9 | delete $updated{persistedOutputSize}; | |||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 9 | $out->{hookSpecificOutput}{updatedToolOutput} = \%updated; | |||||||||||||||||||||||||||||||||||||||||
| 375 | 9 | return $json->encode($out); | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | # Deleting persistedOutputPath is what removes the 2 KB preview framing, | ||||||||||||||||||||||||||||||||||||||||||
| 379 | # and it takes the only pointer to the untouched original with it. So the | ||||||||||||||||||||||||||||||||||||||||||
| 380 | # pointer goes into the text. | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 9 | ▶ | sub _provenance_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_provenance_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 9 | my ( $raw_path, $bytes, $unread ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 9 | ▶ | return '' unless defined $raw_path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | ▶ | return "[mcp-run-compress: only the first $bytes bytes were compressed, " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | . "the raw output is not readable from here; full raw output: $raw_path]" | ||||||||||||||||||||||||||||||||||||||||||
| 386 | if $unread; | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 3 | return "[mcp-run-compress: compressed from $bytes bytes; " | |||||||||||||||||||||||||||||||||||||||||
| 388 | . "full raw output: $raw_path]"; | ||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # Head-and-tail cut to a byte budget, snapped to line boundaries, with the | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # cut named in the middle. Half and half: the start of an output carries | ||||||||||||||||||||||||||||||||||||||||||
| 393 | # what ran, the end carries how it ended, and a generic cap cannot know | ||||||||||||||||||||||||||||||||||||||||||
| 394 | # which of the two the caller is after. Silent shortening is the one thing | ||||||||||||||||||||||||||||||||||||||||||
| 395 | # this product must never do, so the marker is not optional. | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 9 | ▶ | sub _cap_output { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cap_output: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 9 | my ( $text, $limit, $raw_path ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 9 | my $total = _bytelen($text); | |||||||||||||||||||||||||||||||||||||||||
| 400 | 9 | ▶ | return $text if $total <= $limit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | # Sized with the largest number it could hold so the marker can never | ||||||||||||||||||||||||||||||||||||||||||
| 403 | # push the result back over the limit. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 2 | my $reserve = _bytelen( _cap_marker( $total, $total, $raw_path ) ) + 2; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 2 | my $budget = $limit - $reserve; | |||||||||||||||||||||||||||||||||||||||||
| 406 | 2 | ▶ | return _take_bytes( $text, $limit, 0 ) if $budget < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 2 | my $head = _take_bytes( $text, int( $budget / 2 ), 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | my $tail = _take_bytes( $text, $budget - int( $budget / 2 ), 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | 2 | my $omitted = $total - _bytelen($head) - _bytelen($tail); | |||||||||||||||||||||||||||||||||||||||||
| 412 | 2 | return join "\n", $head, _cap_marker( $omitted, $total, $raw_path ), $tail; | |||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 4 | ▶ | sub _cap_marker { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cap_marker: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 4 | my ( $omitted, $total, $raw_path ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 417 | 4 | ▶ | return "[mcp-run-compress: $omitted of $total bytes omitted here; " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | . "full output: $raw_path]" | ||||||||||||||||||||||||||||||||||||||||||
| 419 | if defined $raw_path; | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | return "[mcp-run-compress: $omitted of $total bytes omitted here to stay " | |||||||||||||||||||||||||||||||||||||||||
| 421 | . 'under the tool output cap]'; | ||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | # Byte-exact prefix or suffix, pulled back to a line boundary so no line is | ||||||||||||||||||||||||||||||||||||||||||
| 425 | # served half. A single line longer than the budget is cut hard — a lenient | ||||||||||||||||||||||||||||||||||||||||||
| 426 | # decode turns a split character into one replacement char rather than | ||||||||||||||||||||||||||||||||||||||||||
| 427 | # poisoning the JSON response. | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 4 | ▶ | sub _take_bytes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_take_bytes: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 4 | my ( $text, $n, $from_end ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 4 | ▶ | return '' if $n <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 4 | my $bytes = encode_utf8($text); | |||||||||||||||||||||||||||||||||||||||||
| 432 | 4 | ▶ | return $text if length $bytes <= $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 4 | ▶ | my $chunk = $from_end ? substr( $bytes, -$n ) : substr( $bytes, 0, $n ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 4 | ▶ | if ( index( $chunk, "\n" ) >= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 4 | ▶ | if ($from_end) { $chunk =~ s/\A[^\n]*\n// } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 2 | else { $chunk =~ s/\n[^\n]*\z// } | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 4 | return decode_utf8($chunk); | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 4 | ▶ | sub _slurp_utf8 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_slurp_utf8: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | 4 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 444 | 4 | ▶ | open my $fh, '<:raw', $path or die "cannot read $path: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 3 | my $bytes = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 3 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 3 | ▶ | return decode_utf8( defined $bytes ? $bytes : '' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 10 | ▶ | sub install_claude { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
install_claude: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | # Deliberately NOT fail-open: this is an interactive installer, and an | ||||||||||||||||||||||||||||||||||||||||||
| 452 | # install that silently did nothing would be worse than a loud failure. | ||||||||||||||||||||||||||||||||||||||||||
| 453 | 10 | require Path::Tiny; Path::Tiny->import('path'); | |||||||||||||||||||||||||||||||||||||||||
| 454 | 10 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 44 | ▶ | my $file = path( $ENV{HOME} // glob('~'), '.claude', 'settings.json' ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Condition33%1/3
$ENV{"HOME"} // glob("~")
MC/DC0%0/2
$ENV{"HOME"} // glob("~")
$ENV{"HOME"}
glob("~")
Truth table33%1/3
A // B
A= $ENV{"HOME"}B= glob("~")
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 10 | my $json = JSON::MaybeXS->new( | |||||||||||||||||||||||||||||||||||||||||
| 458 | utf8 => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 459 | pretty => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 460 | canonical => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 461 | convert_blessed => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 462 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 10 | my $cfg = {}; | |||||||||||||||||||||||||||||||||||||||||
| 465 | 10 | ▶ | if ( $file->exists ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | 6 | my $raw = $file->slurp_utf8; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 6 | ▶ | $cfg = $json->decode($raw) if length $raw; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 10 | ▶ | $cfg->{hooks} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cfg->{"hooks"} //= {}
MC/DC100%1/1
$cfg->{"hooks"} //= {}
$cfg->{"hooks"}
Truth table100%2/2
A
A= $cfg->{"hooks"}
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 10 | my $hook_cmd = hook_command(); | |||||||||||||||||||||||||||||||||||||||||
| 472 | 10 | my $dirty = 0; | |||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | 10 | for my $stale ( _stale_hooks( $cfg, $hook_cmd ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 475 | 2 | print "Migrated stale hook command: $stale->{command}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 2 | $stale->{command} = $hook_cmd; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 2 | $dirty++; | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | # Both events, one command. PreToolUse for the Co-Authored-By rewrite and | ||||||||||||||||||||||||||||||||||||||||||
| 481 | # the no-compress marker, PostToolUse for the compression itself. An | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # install from before the PostToolUse move has only the first one; adding | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # the second IS the migration for it, and it is the common case, not an | ||||||||||||||||||||||||||||||||||||||||||
| 484 | # edge one. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 10 | for my $event (qw( PreToolUse PostToolUse )) { | |||||||||||||||||||||||||||||||||||||||||
| 486 | 20 | ▶ | $cfg->{hooks}{$event} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cfg->{"hooks"}{$event} //= []
MC/DC100%1/1
$cfg->{"hooks"}{$event} //= []
$cfg->{"hooks"}{$event}
Truth table100%2/2
A
A= $cfg->{"hooks"}{$event}
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 20 | ▶ | if ( _hook_installed( $cfg, $event ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 7 | print "$event hook already installed in $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 489 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 13 | push @{ $cfg->{hooks}{$event} }, { | |||||||||||||||||||||||||||||||||||||||||
| 492 | matcher => 'Bash', | ||||||||||||||||||||||||||||||||||||||||||
| 493 | hooks => [ { type => 'command', command => $hook_cmd } ], | ||||||||||||||||||||||||||||||||||||||||||
| 494 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 13 | print "Installed $event hook in $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 496 | 13 | $dirty++; | |||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | # Only rewrite the file when something changed: this is the user's | ||||||||||||||||||||||||||||||||||||||||||
| 500 | # settings.json, and reformatting it on every run is not our business. | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 10 | ▶ | if ($dirty) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 8 | ▶ | $file->parent->mkpath unless $file->parent->exists; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 8 | $file->spew_utf8( $json->encode($cfg) ); | |||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 10 | install_skill( $file->parent ); | |||||||||||||||||||||||||||||||||||||||||
| 507 | 10 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | # Every mcp-run-compress hook entry in the config, across all events. | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 30 | ▶ | sub _our_hooks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_our_hooks: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 30 | my ($cfg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 30 | my @found; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 30 | for my $event ( sort keys %{ $cfg->{hooks} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 515 | 41 | ▶ | next unless ref $cfg->{hooks}{$event} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | 41 | for my $group ( @{ $cfg->{hooks}{$event} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 517 | 29 | ▶ | next unless ref $group eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 29 | ▶ | for my $h ( @{ $group->{hooks} // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$group->{"hooks"} // []
MC/DC0%0/1
$group->{"hooks"} // []
$group->{"hooks"}
Truth table50%1/2
A
A= $group->{"hooks"}
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 29 | ▶ | next unless ref $h eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 520 | 29 | ▶ | next unless ( $h->{command} // '' ) =~ /\bmcp-run-compress\b/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$h->{"command"} // ""
MC/DC0%0/1
$h->{"command"} // ""
$h->{"command"}
Truth table50%1/2
A
A= $h->{"command"}
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 26 | push @found, { event => $event, group => $group, hook => $h }; | |||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 30 | return @found; | |||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | 20 | ▶ | sub _hook_installed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hook_installed: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 20 | my ( $cfg, $event ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 20 | for my $found ( _our_hooks($cfg) ) { | |||||||||||||||||||||||||||||||||||||||||
| 531 | 17 | ▶ | next unless $found->{event} eq $event; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 7 | ▶ | next unless ( $found->{group}{matcher} // '' ) eq 'Bash'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$found->{"group"}{"matcher"} // ""
MC/DC0%0/1
$found->{"group"}{"matcher"} // ""
$found->{"group"}{"matcher"}
Truth table50%1/2
A
A= $found->{"group"}{"matcher"}
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 7 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 13 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | # Entries that would misbehave if left alone, returned as the hook hashrefs | ||||||||||||||||||||||||||||||||||||||||||
| 539 | # so the caller can rewrite them in place: | ||||||||||||||||||||||||||||||||||||||||||
| 540 | # | ||||||||||||||||||||||||||||||||||||||||||
| 541 | # * a flag this version no longer has (--b64, --filter-files, from the | ||||||||||||||||||||||||||||||||||||||||||
| 542 | # rewrite era). GetOptions would fail on it, and a PreToolUse hook that | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # exits non-zero blocks every Bash call. | ||||||||||||||||||||||||||||||||||||||||||
| 544 | # * a `docker run` of an image ref that is not the one we would write now. | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # A pinned older image still does the old PreToolUse rewrite, which | ||||||||||||||||||||||||||||||||||||||||||
| 546 | # compresses inside the command — with the new PostToolUse hook next to | ||||||||||||||||||||||||||||||||||||||||||
| 547 | # it that is two compressions on one output. The docker command is | ||||||||||||||||||||||||||||||||||||||||||
| 548 | # generated by us end to end, so replacing it wholesale is safe. | ||||||||||||||||||||||||||||||||||||||||||
| 549 | # | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # A native command that merely differs in spelling (an absolute path, say) | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # is left alone: it still works, and rewriting it to a bare `mcp-run-compress` | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # could take the hook off a PATH that never had it. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 10 | ▶ | sub _stale_hooks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stale_hooks: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 10 | my ( $cfg, $hook_cmd ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 555 | 10 | my @stale; | |||||||||||||||||||||||||||||||||||||||||
| 556 | 10 | for my $found ( _our_hooks($cfg) ) { | |||||||||||||||||||||||||||||||||||||||||
| 557 | 7 | ▶ | my $cmd = $found->{hook}{command} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$found->{"hook"}{"command"} // ""
MC/DC0%0/1
$found->{"hook"}{"command"} // ""
$found->{"hook"}{"command"}
Truth table50%1/2
A
A= $found->{"hook"}{"command"}
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | 7 | ▶ | next if $cmd eq $hook_cmd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | push @stale, $found->{hook} | ||||||||||||||||||||||||||||||||||||||||||
| 560 | 2 | ▶ | if $cmd =~ /--b64\b/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$cmd =~ /--b64\b/ or $cmd =~ /--filter-files\b/ or $cmd =~ /\bdocker\s+run\b/
$cmd =~ /--b64\b/ or $cmd =~ /--filter-files\b/
MC/DC0%0/3
$cmd =~ /--b64\b/ or $cmd =~ /--filter-files\b/ or $cmd =~ /\bdocker\s+run\b/
$cmd =~ /--b64\b/
$cmd =~ /--filter-files\b/
$cmd =~ /\bdocker\s+run\b/
Truth table50%2/4
A or B or C
A= $cmd =~ /--b64\b/B= $cmd =~ /--filter-files\b/C= $cmd =~ /\bdocker\s+run\b/
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | || $cmd =~ /--filter-files\b/ | ||||||||||||||||||||||||||||||||||||||||||
| 562 | || $cmd =~ /\bdocker\s+run\b/; | ||||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | 10 | return @stale; | |||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | # Installed independently of the hooks: the skill is what tells the model | ||||||||||||||||||||||||||||||||||||||||||
| 568 | # that it is looking at compressed output and how to opt out. An | ||||||||||||||||||||||||||||||||||||||||||
| 569 | # already-installed hook must not mask a deleted or outdated skill. karr #12. | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 10 | ▶ | sub install_skill { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
install_skill: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 571 | 10 | my ($claude_dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | 10 | my $skill = $claude_dir->child( | |||||||||||||||||||||||||||||||||||||||||
| 574 | 'skills', 'bash-output-is-compressed-prefix-no-compress-to-bypass', 'SKILL.md', | ||||||||||||||||||||||||||||||||||||||||||
| 575 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 576 | 10 | my $skill_body = <<'SKILL'; | |||||||||||||||||||||||||||||||||||||||||
| 577 | --- | ||||||||||||||||||||||||||||||||||||||||||
| 578 | name: bash-output-is-compressed-prefix-no-compress-to-bypass | ||||||||||||||||||||||||||||||||||||||||||
| 579 | description: READ THIS BEFORE RUNNING ANY Bash TOOL COMMAND. Bash tool output is replaced by the mcp-run-compress hook, which strips columns and noise and shortens long output — what you see is a compressed view, NOT the real shell output. If you need the raw unfiltered output (full `ls -l` columns, verbose logs, exact byte counts, etc.), prefix the command with `no-compress ` for that single invocation. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | --- | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | Every Bash command you run is compressed by a PostToolUse hook before you | ||||||||||||||||||||||||||||||||||||||||||
| 583 | see its output. Over 30 command-specific filters strip noise, reduce | ||||||||||||||||||||||||||||||||||||||||||
| 584 | columns, drop progress lines, and cap long output with head+tail. | ||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | To get the real, unmodified output for one command, prefix it with | ||||||||||||||||||||||||||||||||||||||||||
| 587 | `no-compress `: | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | no-compress ls -l | ||||||||||||||||||||||||||||||||||||||||||
| 590 | no-compress cat /var/log/syslog | ||||||||||||||||||||||||||||||||||||||||||
| 591 | no-compress dmesg | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | Use it whenever compressed output would hide something you actually need | ||||||||||||||||||||||||||||||||||||||||||
| 594 | to see. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | What is NOT compressed, so do not read anything into the difference: | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | * commands that exit non-zero — their output cannot be replaced, you | ||||||||||||||||||||||||||||||||||||||||||
| 599 | get it raw | ||||||||||||||||||||||||||||||||||||||||||
| 600 | * background commands — their output does not arrive as tool output | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | Any line that starts with `[mcp-run-compress:` is the hook talking to you, | ||||||||||||||||||||||||||||||||||||||||||
| 603 | not the command. Where output was shortened, that line says how much was | ||||||||||||||||||||||||||||||||||||||||||
| 604 | dropped and names the file holding the complete original, which you can | ||||||||||||||||||||||||||||||||||||||||||
| 605 | read directly. | ||||||||||||||||||||||||||||||||||||||||||
| 606 | SKILL | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | 10 | ▶ | if ( $skill->exists && $skill->slurp_utf8 eq $skill_body ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$skill->exists and $skill->slurp_utf8 eq $skill_body
MC/DC50%1/2
$skill->exists and $skill->slurp_utf8 eq $skill_body
$skill->exists
$skill->slurp_utf8 eq $skill_body
Truth table66%2/3
A and B
A= $skill->existsB= $skill->slurp_utf8 eq $skill_body
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 1 | print "Skill already installed at $skill\n"; | |||||||||||||||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||||||||||||||||
| 611 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 9 | ▶ | $skill->parent->mkpath unless $skill->parent->exists; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 613 | 9 | $skill->spew_utf8($skill_body); | |||||||||||||||||||||||||||||||||||||||||
| 614 | 9 | print "Installed skill at $skill\n"; | |||||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | ▶ | sub print_help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
print_help: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | print <<'HELP'; | |||||||||||||||||||||||||||||||||||||||||
| 621 | mcp-run-compress — Claude Code hooks that run Bash tool output through the | ||||||||||||||||||||||||||||||||||||||||||
| 622 | MCP::Run::Compress filters so what the model sees is LLM-friendly (noise | ||||||||||||||||||||||||||||||||||||||||||
| 623 | stripped, long output shortened). | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | HOW IT WORKS | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | Two hooks, one command (`mcp-run-compress --hook`), one job each: | ||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | PostToolUse Compresses the output. The command is not touched; the | ||||||||||||||||||||||||||||||||||||||||||
| 630 | recorded output is replaced via updatedToolOutput. | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | PreToolUse Everything that has to happen BEFORE the command runs: | ||||||||||||||||||||||||||||||||||||||||||
| 633 | the Co-Authored-By rewrite of `git commit`, and turning a | ||||||||||||||||||||||||||||||||||||||||||
| 634 | `no-compress ` prefix into a marker the PostToolUse hook | ||||||||||||||||||||||||||||||||||||||||||
| 635 | can still see. | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | INSTALL MODES | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | native User has Perl and installed MCP::Run (via cpanm / Dist::Zilla). | ||||||||||||||||||||||||||||||||||||||||||
| 640 | Both hooks are `mcp-run-compress --hook`. | ||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||
| 642 | docker User has only Docker (no Perl). Both hooks are | ||||||||||||||||||||||||||||||||||||||||||
| 643 | `docker run --rm -i --network none <image> --hook`, reading | ||||||||||||||||||||||||||||||||||||||||||
| 644 | the hook JSON on stdin and writing the response on stdout. | ||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||
| 646 | The mode is selected by the MCP_RUN_COMPRESS_INSTALL_MODE env var. The | ||||||||||||||||||||||||||||||||||||||||||
| 647 | Docker image sets this to `docker` in its ENV; native installs leave it | ||||||||||||||||||||||||||||||||||||||||||
| 648 | unset (default `native`). Override manually if you need to. | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | mcp-run-compress --hook | ||||||||||||||||||||||||||||||||||||||||||
| 653 | Hook entry point for both events. Reads the hook invocation JSON on | ||||||||||||||||||||||||||||||||||||||||||
| 654 | stdin, dispatches on its hook_event_name, and writes the response | ||||||||||||||||||||||||||||||||||||||||||
| 655 | JSON on stdout. | ||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | mcp-run-compress --install-claude | ||||||||||||||||||||||||||||||||||||||||||
| 658 | Patch ~/.claude/settings.json to register the hook for both PreToolUse | ||||||||||||||||||||||||||||||||||||||||||
| 659 | and PostToolUse on the Bash tool, and drop the companion skill. | ||||||||||||||||||||||||||||||||||||||||||
| 660 | Idempotent, and it migrates an installation from before the | ||||||||||||||||||||||||||||||||||||||||||
| 661 | PostToolUse move. See MIGRATION below. | ||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | mcp-run-compress [--help] | ||||||||||||||||||||||||||||||||||||||||||
| 664 | Print this help. | ||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | ENV VARS | ||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||
| 668 | MCP_RUN_COMPRESS_INSTALL_MODE `native` (default) or `docker`. | ||||||||||||||||||||||||||||||||||||||||||
| 669 | MCP_RUN_COMPRESS_IMAGE Docker image ref for the hook command. | ||||||||||||||||||||||||||||||||||||||||||
| 670 | Defaults to `raudssus/mcp-run-compress:latest` | ||||||||||||||||||||||||||||||||||||||||||
| 671 | but the shipped Docker image pins this | ||||||||||||||||||||||||||||||||||||||||||
| 672 | to the exact version that built it. | ||||||||||||||||||||||||||||||||||||||||||
| 673 | MCP_RUN_COMPRESS_MAX_BYTES Byte ceiling for the replaced output | ||||||||||||||||||||||||||||||||||||||||||
| 674 | (default 29000). See LIMITS. | ||||||||||||||||||||||||||||||||||||||||||
| 675 | MCP_RUN_COMPRESS_NO_CO_AUTHORED Disable the Co-Authored-By rewrite. | ||||||||||||||||||||||||||||||||||||||||||
| 676 | CO_AUTHORED_BY Replacement value for the trailer. | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | MANUAL INSTALL | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | Add this block to ~/.claude/settings.json (merge with existing hooks): | ||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||
| 682 | { | ||||||||||||||||||||||||||||||||||||||||||
| 683 | "hooks": { | ||||||||||||||||||||||||||||||||||||||||||
| 684 | "PreToolUse": [ | ||||||||||||||||||||||||||||||||||||||||||
| 685 | { | ||||||||||||||||||||||||||||||||||||||||||
| 686 | "matcher": "Bash", | ||||||||||||||||||||||||||||||||||||||||||
| 687 | "hooks": [ | ||||||||||||||||||||||||||||||||||||||||||
| 688 | { "type": "command", "command": "mcp-run-compress --hook" } | ||||||||||||||||||||||||||||||||||||||||||
| 689 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 690 | } | ||||||||||||||||||||||||||||||||||||||||||
| 691 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 692 | "PostToolUse": [ | ||||||||||||||||||||||||||||||||||||||||||
| 693 | { | ||||||||||||||||||||||||||||||||||||||||||
| 694 | "matcher": "Bash", | ||||||||||||||||||||||||||||||||||||||||||
| 695 | "hooks": [ | ||||||||||||||||||||||||||||||||||||||||||
| 696 | { "type": "command", "command": "mcp-run-compress --hook" } | ||||||||||||||||||||||||||||||||||||||||||
| 697 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||
| 699 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||
| 703 | MIGRATION FROM THE PreToolUse REWRITE | ||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||
| 705 | Up to 0.106 compression happened by rewriting the Bash command to | ||||||||||||||||||||||||||||||||||||||||||
| 706 | `mcp-run-compress --b64 <…>` from a PreToolUse hook. Those two flags | ||||||||||||||||||||||||||||||||||||||||||
| 707 | (--b64, --filter-files) are gone. | ||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||
| 709 | `--install-claude` handles the upgrade: it adds the missing PostToolUse | ||||||||||||||||||||||||||||||||||||||||||
| 710 | hook, and it rewrites any hook entry that still names a removed flag or | ||||||||||||||||||||||||||||||||||||||||||
| 711 | a stale Docker image ref. Run it once after upgrading. | ||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||
| 713 | If you installed by hand, add the PostToolUse block above. Leaving an old | ||||||||||||||||||||||||||||||||||||||||||
| 714 | docker-mode PreToolUse hook in place next to the new PostToolUse one would | ||||||||||||||||||||||||||||||||||||||||||
| 715 | compress twice; the PostToolUse hook detects the old wrapper in the | ||||||||||||||||||||||||||||||||||||||||||
| 716 | command and steps aside, but the stale entry is still worth removing. | ||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||
| 718 | LIMITS | ||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||
| 720 | * Commands that exit non-zero are NOT compressed. Claude Code fires | ||||||||||||||||||||||||||||||||||||||||||
| 721 | PostToolUseFailure for them, that event carries no tool output and | ||||||||||||||||||||||||||||||||||||||||||
| 722 | ignores a replacement, so the raw output of a failing command comes | ||||||||||||||||||||||||||||||||||||||||||
| 723 | through unchanged. Failing builds are the loudest output there is — | ||||||||||||||||||||||||||||||||||||||||||
| 724 | this is a real limit of the hook, not a bug in the filters. | ||||||||||||||||||||||||||||||||||||||||||
| 725 | * Background commands are not compressed. The hook fires when the | ||||||||||||||||||||||||||||||||||||||||||
| 726 | command STARTS; the output arrives later out of band. | ||||||||||||||||||||||||||||||||||||||||||
| 727 | * PostToolUse hooks do not chain. If another PostToolUse hook on Bash | ||||||||||||||||||||||||||||||||||||||||||
| 728 | also returns a replacement, the last registered one wins and the other | ||||||||||||||||||||||||||||||||||||||||||
| 729 | replacement is dropped without a word — that can silently switch | ||||||||||||||||||||||||||||||||||||||||||
| 730 | compression off. `--install-claude` appends, so a later install of | ||||||||||||||||||||||||||||||||||||||||||
| 731 | another hook takes precedence over this one. | ||||||||||||||||||||||||||||||||||||||||||
| 732 | * Replaced output has a byte ceiling (MCP_RUN_COMPRESS_MAX_BYTES, | ||||||||||||||||||||||||||||||||||||||||||
| 733 | default 29000). Claude Code persists a tool output above ~30000 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 734 | to a file and shows the model a 2 KB preview of it — including output | ||||||||||||||||||||||||||||||||||||||||||
| 735 | this hook produced, so a compressed result over the cap would arrive | ||||||||||||||||||||||||||||||||||||||||||
| 736 | smaller than an uncompressed one. Output that would exceed the cap is | ||||||||||||||||||||||||||||||||||||||||||
| 737 | cut head-and-tail with a `[mcp-run-compress: … omitted …]` line naming | ||||||||||||||||||||||||||||||||||||||||||
| 738 | the file that holds the complete text. | ||||||||||||||||||||||||||||||||||||||||||
| 739 | * In docker mode, output above 30000 bytes is compressed only as far as | ||||||||||||||||||||||||||||||||||||||||||
| 740 | the 30000 bytes the hook is handed. The full text sits in a file on the | ||||||||||||||||||||||||||||||||||||||||||
| 741 | HOST that the container cannot read, and mounting it would mean | ||||||||||||||||||||||||||||||||||||||||||
| 742 | mounting ~/.claude/projects, which holds every session transcript. | ||||||||||||||||||||||||||||||||||||||||||
| 743 | The path is named in the output instead. | ||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | NOTES | ||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | * The hooks make no permission decisions; the usual allow/deny prompts | ||||||||||||||||||||||||||||||||||||||||||
| 748 | keep firing untouched. | ||||||||||||||||||||||||||||||||||||||||||
| 749 | * Both hooks are fail-open. A PreToolUse hook that exits non-zero blocks | ||||||||||||||||||||||||||||||||||||||||||
| 750 | the tool call, so if this script cannot load its dependencies (broken | ||||||||||||||||||||||||||||||||||||||||||
| 751 | or half-upgraded install) it emits a valid pass-through response, exits | ||||||||||||||||||||||||||||||||||||||||||
| 752 | 0, and notes it in one line on stderr. You lose compression, never your | ||||||||||||||||||||||||||||||||||||||||||
| 753 | shell. | ||||||||||||||||||||||||||||||||||||||||||
| 754 | * Prefix a command with `no-compress ` to bypass compression for that | ||||||||||||||||||||||||||||||||||||||||||
| 755 | single invocation. `--install-claude` also drops a skill at | ||||||||||||||||||||||||||||||||||||||||||
| 756 | ~/.claude/skills/bash-output-is-compressed-prefix-no-compress-to-bypass/ | ||||||||||||||||||||||||||||||||||||||||||
| 757 | that tells Claude about it. | ||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||
| 759 | SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 760 | MCP::Run::Compress, MCP::Run::Bash, mcp-run-bash | ||||||||||||||||||||||||||||||||||||||||||
| 761 | HELP | ||||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | __END__ | ||||||||||||||||||||||||||||||||||||||||||