| 1 | package Acme::Claude::Shell::Hooks; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 7 | ▶ | use 5.020; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 7 | ▶ | use utf8; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | 7 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 7 | our @EXPORT_OK = qw(safety_hooks); | |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 7 | ▶ | use Claude::Agent::CLI qw(stop_spinner status); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | 7 | ▶ | use Claude::Agent::Hook::Matcher; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 13 | 7 | ▶ | use Claude::Agent::Hook::Result; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 14 | 7 | ▶ | use Term::ANSIColor qw(colored); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 15 | 7 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | 4 | ▶ | sub safety_hooks { | ||||||||||||||||||||||
|
Subroutine
safety_hooks: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||
| 18 | 4 | my ($session) = @_; | |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||
| 20 | 4 | my $execute_cmd_pattern = 'execute_command$'; | |||||||||||||||||||||||
| 21 | 4 | my $any_shell_tool = 'mcp__shell-tools__.*'; | |||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | 4 | ▶ | $session->_session_start(time()) unless $session->_session_start; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | return { | ||||||||||||||||||||||||
| 26 | PreToolUse => [ | ||||||||||||||||||||||||
| 27 | Claude::Agent::Hook::Matcher->new( | ||||||||||||||||||||||||
| 28 | matcher => $any_shell_tool, | ||||||||||||||||||||||||
| 29 | 2 | ▶ | hooks => [sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||
| 30 | 2 | my ($input, $tool_use_id, $context) = @_; | |||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | 2 | ▶ | my $tool_name = $input->{tool_name} // 'unknown'; | ||||||||||||||||||||||
|
Condition50%1/2
$input->{"tool_name"} // "unknown"
MC/DC0%0/1
$input->{"tool_name"} // "unknown"
$input->{"tool_name"}
Truth table50%1/2
A
A= $input->{"tool_name"}
| |||||||||||||||||||||||||
| 33 | 2 | ▶ | my $tool_input = $input->{tool_input} // {}; | ||||||||||||||||||||||
|
Condition50%1/2
$input->{"tool_input"} // {}
MC/DC0%0/1
$input->{"tool_input"} // {}
$input->{"tool_input"}
Truth table50%1/2
A
A= $input->{"tool_input"}
| |||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||
| 35 | 2 | my $short_name = $tool_name; | |||||||||||||||||||||||
| 36 | 2 | $short_name =~ s/^mcp__shell-tools__//; | |||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | 2 | ▶ | if ($session->verbose) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 39 | 1 | ▶ | if ($session->colorful) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 40 | 0 | status('info', "Tool: $short_name"); | |||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||||||
| 43 | 1 | print "[Tool] $short_name\n"; | |||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | 2 | ▶ | if ($session->audit_log) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 48 | 1 | push @{$session->_audit_log}, { | |||||||||||||||||||||||
| 49 | time => time(), | ||||||||||||||||||||||||
| 50 | tool => $short_name, | ||||||||||||||||||||||||
| 51 | input => $tool_input, | ||||||||||||||||||||||||
| 52 | tool_use_id => $tool_use_id, | ||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | 2 | return Claude::Agent::Hook::Result->proceed(); | |||||||||||||||||||||||
| 57 | }], | ||||||||||||||||||||||||
| 58 | ), | ||||||||||||||||||||||||
| 59 | ], | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | PostToolUse => [ | ||||||||||||||||||||||||
| 62 | Claude::Agent::Hook::Matcher->new( | ||||||||||||||||||||||||
| 63 | matcher => $execute_cmd_pattern, | ||||||||||||||||||||||||
| 64 | 1 | ▶ | hooks => [sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||
| 65 | 1 | my ($input, $tool_use_id, $context) = @_; | |||||||||||||||||||||||
| 66 | 1 | ▶ | if ($session->_spinner) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 67 | 0 | stop_spinner($session->_spinner); | |||||||||||||||||||||||
| 68 | 0 | $session->_spinner(undef); | |||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||
| 70 | 1 | $session->_tool_count($session->_tool_count + 1); | |||||||||||||||||||||||
| 71 | 1 | return Claude::Agent::Hook::Result->proceed(); | |||||||||||||||||||||||
| 72 | }], | ||||||||||||||||||||||||
| 73 | ), | ||||||||||||||||||||||||
| 74 | ], | ||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | PostToolUseFailure => [ | ||||||||||||||||||||||||
| 77 | Claude::Agent::Hook::Matcher->new( | ||||||||||||||||||||||||
| 78 | matcher => $any_shell_tool, | ||||||||||||||||||||||||
| 79 | 1 | ▶ | hooks => [sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||
| 80 | 1 | my ($input, $tool_use_id, $context) = @_; | |||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||
| 82 | 1 | ▶ | my $tool_name = $input->{tool_name} // 'unknown'; | ||||||||||||||||||||||
|
Condition50%1/2
$input->{"tool_name"} // "unknown"
MC/DC0%0/1
$input->{"tool_name"} // "unknown"
$input->{"tool_name"}
Truth table50%1/2
A
A= $input->{"tool_name"}
| |||||||||||||||||||||||||
| 83 | 1 | ▶ | my $error = $input->{error} // 'Unknown error'; | ||||||||||||||||||||||
|
Condition50%1/2
$input->{"error"} // "Unknown error"
MC/DC0%0/1
$input->{"error"} // "Unknown error"
$input->{"error"}
Truth table50%1/2
A
A= $input->{"error"}
| |||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||
| 85 | 1 | my $short_name = $tool_name; | |||||||||||||||||||||||
| 86 | 1 | $short_name =~ s/^mcp__shell-tools__//; | |||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||
| 88 | 1 | $session->_tool_errors($session->_tool_errors + 1); | |||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 1 | ▶ | if ($session->colorful) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 91 | 0 | status('error', "Tool '$short_name' failed: $error"); | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||
| 94 | 1 | print "[ERROR] Tool '$short_name' failed: $error\n"; | |||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | 1 | return Claude::Agent::Hook::Result->proceed(); | |||||||||||||||||||||||
| 98 | }], | ||||||||||||||||||||||||
| 99 | ), | ||||||||||||||||||||||||
| 100 | ], | ||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||
| 102 | Stop => [ | ||||||||||||||||||||||||
| 103 | Claude::Agent::Hook::Matcher->new( | ||||||||||||||||||||||||
| 104 | matcher => '.*', | ||||||||||||||||||||||||
| 105 | 1 | ▶ | hooks => [sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=26.5
| |||||||||||||||||||||||||
| 106 | 1 | my ($input, $tool_use_id, $context) = @_; | |||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||
| 108 | 1 | ▶ | my $duration = time() - ($session->_session_start // time()); | ||||||||||||||||||||||
|
Condition33%1/3
$session->_session_start // &Acme::Claude::Shell::Hooks::time()
MC/DC0%0/2
$session->_session_start // &Acme::Claude::Shell::Hooks::time()
$session->_session_start
&Acme::Claude::Shell::Hooks::time()
Truth table33%1/3
A // B
A= $session->_session_startB= &Acme::Claude::Shell::Hooks::time()
| |||||||||||||||||||||||||
| 109 | 1 | ▶ | my $tool_count = $session->_tool_count // 0; | ||||||||||||||||||||||
|
Condition50%1/2
$session->_tool_count // 0
MC/DC0%0/1
$session->_tool_count // 0
$session->_tool_count
Truth table50%1/2
A
A= $session->_tool_count
| |||||||||||||||||||||||||
| 110 | 1 | ▶ | my $tool_errors = $session->_tool_errors // 0; | ||||||||||||||||||||||
|
Condition50%1/2
$session->_tool_errors // 0
MC/DC0%0/1
$session->_tool_errors // 0
$session->_tool_errors
Truth table50%1/2
A
A= $session->_tool_errors
| |||||||||||||||||||||||||
| 111 | 1 | ▶ | my $history_count = scalar(@{$session->_history // []}); | ||||||||||||||||||||||
|
Condition50%1/2
$session->_history // []
MC/DC0%0/1
$session->_history // []
$session->_history
Truth table50%1/2
A
A= $session->_history
| |||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | 1 | ▶ | if ($session->colorful) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 114 | 0 | print "\n"; | |||||||||||||||||||||||
| 115 | 0 | print colored(['cyan'], "─" x 40) . "\n"; | |||||||||||||||||||||||
| 116 | 0 | status('info', "Session Statistics"); | |||||||||||||||||||||||
| 117 | 0 | printf " Duration: %.1f seconds\n", $duration; | |||||||||||||||||||||||
| 118 | 0 | printf " Tools used: %d\n", $tool_count; | |||||||||||||||||||||||
| 119 | 0 | ▶ | printf " Tool errors: %d\n", $tool_errors if $tool_errors > 0; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 120 | 0 | printf " Commands in history: %d\n", $history_count; | |||||||||||||||||||||||
| 121 | 0 | print colored(['cyan'], "─" x 40) . "\n"; | |||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||
| 124 | 1 | print "\n--- Session Statistics ---\n"; | |||||||||||||||||||||||
| 125 | 1 | printf "Duration: %.1f seconds\n", $duration; | |||||||||||||||||||||||
| 126 | 1 | printf "Tools used: %d\n", $tool_count; | |||||||||||||||||||||||
| 127 | 1 | ▶ | printf "Tool errors: %d\n", $tool_errors if $tool_errors > 0; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 128 | 1 | printf "Commands in history: %d\n", $history_count; | |||||||||||||||||||||||
| 129 | 1 | print "--------------------------\n"; | |||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||
| 132 | 1 | return Claude::Agent::Hook::Result->proceed(); | |||||||||||||||||||||||
| 133 | }], | ||||||||||||||||||||||||
| 134 | ), | ||||||||||||||||||||||||
| 135 | ], | ||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||
| 137 | Notification => [ | ||||||||||||||||||||||||
| 138 | Claude::Agent::Hook::Matcher->new( | ||||||||||||||||||||||||
| 139 | matcher => '.*', | ||||||||||||||||||||||||
| 140 | 2 | ▶ | hooks => [sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=24.7
| |||||||||||||||||||||||||
| 141 | 2 | my ($input, $tool_use_id, $context) = @_; | |||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||
| 143 | 2 | ▶ | my $type = $input->{notification_type} // $input->{title} // 'unknown'; | ||||||||||||||||||||||
|
Condition40%2/5
($input->{"notification_type"} // $input->{"title"}) // "unknown"
$input->{"notification_type"} // $input->{"title"}
MC/DC0%0/2
($input->{"notification_type"} // $input->{"title"}) // "unknown"
$input->{"notification_type"}
$input->{"title"}
Truth table33%1/3
A // B
A= $input->{"notification_type"}B= $input->{"title"}
| |||||||||||||||||||||||||
| 144 | 2 | ▶ | my $message = $input->{message} // ''; | ||||||||||||||||||||||
|
Condition50%1/2
$input->{"message"} // ""
MC/DC0%0/1
$input->{"message"} // ""
$input->{"message"}
Truth table50%1/2
A
A= $input->{"message"}
| |||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | 2 | ▶ | if ($session->verbose) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 147 | 1 | ▶ | if ($session->colorful) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 148 | 0 | ▶ | status('info', "Notification: $type" . (length $message ? " - $message" : "")); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||
| 151 | 1 | ▶ | print "[Notification] $type", (length $message ? " - $message" : ""), "\n"; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||
| 155 | 2 | return Claude::Agent::Hook::Result->proceed(); | |||||||||||||||||||||||
| 156 | 4 | }], | |||||||||||||||||||||||
| 157 | ), | ||||||||||||||||||||||||
| 158 | ], | ||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||
| 163 | 7 | 1; | |||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||