blib/lib/Acme/Claude/Shell/Hooks.pm

filter: Statementstmt 47.8% 45 / 94 Branchbran 15.0% 3 / 20 Conditioncond 20.0% 6 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 1 totaltotal 36.9% 65 / 176 CC 25 SCAR 31.8
CC
25
Cov
38%
__ANON__
72.0
__ANON__
30.0
__ANON__
20.0
CRAP 24.0
« blib/lib/Acme/Claude/Shell.pm ↑ Summary bin/acme_claude_shell »
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 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 our @EXPORT_OK = qw(safety_hooks);
9  
10 7 ▶use Claude::Agent::CLI qw(stop_spinner status);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Claude::Agent::Hook::Matcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Claude::Agent::Hook::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Acme::Claude::Shell::Hooks - Safety hooks for Acme::Claude::Shell
19  
20 =head1 SYNOPSIS
21  
22 use Acme::Claude::Shell::Hooks qw(safety_hooks);
23  
24 my $hooks = safety_hooks($session);
25  
26 =head1 DESCRIPTION
27  
28 Provides hooks for Acme::Claude::Shell. These hooks integrate with the
29 Claude::Agent SDK hook system to provide logging, statistics, and error
30 handling.
31  
32 B<Note:> Command approval is handled directly in the tool handler (Tools.pm)
33 to ensure it happens synchronously before execution.
34  
35 =head2 Hooks
36  
37 =over 4
38  
39 =item * B<PreToolUse> - Audit logging of tool calls
40  
41 Triggered before any shell-tools MCP tool executes. Logs tool usage in
42 verbose mode and tracks calls in an audit log if C<< $session->{audit_log} >>
43 is enabled.
44  
45 =item * B<PostToolUse> - Stop spinner after command execution
46  
47 Triggered after C<execute_command> completes successfully. Stops the
48 execution spinner and increments the tool usage counter.
49  
50 =item * B<PostToolUseFailure> - Handle tool failures gracefully
51  
52 Triggered when any shell-tools MCP tool fails. Displays a user-friendly
53 error message and tracks error count for session statistics.
54  
55 =item * B<Stop> - Show session statistics when agent stops
56  
57 Triggered when the agent stops (end of session). Displays:
58  
59 =over 4
60  
61 =item * Session duration
62  
63 =item * Number of tools used
64  
65 =item * Number of tool errors (if any)
66  
67 =item * Commands in history
68  
69 =back
70  
71 =item * B<Notification> - Log important events
72  
73 Triggered for SDK notifications. Logs notification types in verbose mode.
74  
75 =back
76  
77 =head2 Session Options
78  
79 The following session attributes affect hook behavior:
80  
81 =over 4
82  
83 =item * C<verbose> - Enable verbose logging of tool calls and notifications
84  
85 =item * C<audit_log> - Enable detailed audit logging to C<< $session->{_audit_log} >>
86  
87 =item * C<colorful> - Use colored output (default: auto-detect TTY)
88  
89 =back
90  
91 =cut
92  
93 3 ▶sub safety_hooks {
Subroutine safety_hooks: called CC=4 SCAR=24.4
Pod undocumented
94 3 my ($session) = @_;
95  
96 # Tool name pattern - matches mcp__shell-tools__execute_command
97 3 my $execute_cmd_pattern = 'execute_command$';
98 # Match any tool from our shell-tools server
99 3 my $any_shell_tool = 'shell-tools__';
100  
101 # Track session start time
102 3 ▶ $session->{_session_start} //= time();
Condition33%1/3
$session->{"_session_start"} //= &Acme::Claude::Shell::Hooks::time()
l!l&&r!l&&!r
030
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_start"}B= &Acme::Claude::Shell::Hooks::time()
ABresult
1X1
011
000
103 3 ▶ $session->{_tool_count} //= 0;
Condition50%1/2
$session->{"_tool_count"} //= 0
l!l
03
MC/DC0%0/1
$session->{"_tool_count"} //= 0
$session->{"_tool_count"}
Truth table50%1/2
A
A= $session->{"_tool_count"}
Aresult
11
00
104 3 ▶ $session->{_tool_errors} //= 0;
Condition50%1/2
$session->{"_tool_errors"} //= 0
l!l
03
MC/DC0%0/1
$session->{"_tool_errors"} //= 0
$session->{"_tool_errors"}
Truth table50%1/2
A
A= $session->{"_tool_errors"}
Aresult
11
00
105  
106 return {
107 # PreToolUse: Audit logging - track what tools Claude wants to use
108 PreToolUse => [
109 Claude::Agent::Hook::Matcher->new(
110 matcher => $any_shell_tool,
111 2 ▶ hooks => [sub {
Subroutine __ANON__: called CC=7 SCAR=22.3
112 2 my ($input, $tool_use_id, $context) = @_;
113  
114 2 ▶ my $tool_name = $input->{tool_name} // 'unknown';
Condition50%1/2
$input->{"tool_name"} // "unknown"
l!l
20
MC/DC0%0/1
$input->{"tool_name"} // "unknown"
$input->{"tool_name"}
Truth table50%1/2
A
A= $input->{"tool_name"}
Aresult
11
00
115 2 ▶ my $tool_input = $input->{tool_input} // {};
Condition50%1/2
$input->{"tool_input"} // {}
l!l
20
MC/DC0%0/1
$input->{"tool_input"} // {}
$input->{"tool_input"}
Truth table50%1/2
A
A= $input->{"tool_input"}
Aresult
11
00
116  
117 # Extract short tool name (after mcp__shell-tools__)
118 2 my $short_name = $tool_name;
119 2 $short_name =~ s/^mcp__shell-tools__//;
120  
121 # Log tool usage in verbose mode
122 2 ▶ if ($session->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"verbose"}) 0 2 2
123 0 ▶ if ($session->colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->colorful) { } 0 0 0
124 0 status('info', "Tool: $short_name");
125 }
126 else {
127 0 print "[Tool] $short_name\n";
128 }
129 }
130  
131 # Track in audit log if enabled
132 2 ▶ if ($session->{audit_log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"audit_log"}) 1 1 2
133 1 ▶ push @{$session->{_audit_log} //= []}, {
Condition50%1/2
$session->{"_audit_log"} //= []
l!l
01
MC/DC0%0/1
$session->{"_audit_log"} //= []
$session->{"_audit_log"}
Truth table50%1/2
A
A= $session->{"_audit_log"}
Aresult
11
00
134 time => time(),
135 tool => $short_name,
136 input => $tool_input,
137 tool_use_id => $tool_use_id,
138 };
139 }
140  
141 2 return Claude::Agent::Hook::Result->proceed();
142 }],
143 ),
144 ],
145  
146 # PostToolUse: Stop spinner after command execution and track stats
147 PostToolUse => [
148 Claude::Agent::Hook::Matcher->new(
149 matcher => $execute_cmd_pattern,
150 0 ▶ hooks => [sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 0 my ($input, $tool_use_id, $context) = @_;
152 # Stop the execution spinner
153 0 ▶ if ($session->_spinner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->_spinner) 0 0 0
154 0 stop_spinner($session->_spinner);
155 0 $session->_spinner(undef);
156 }
157 # Track tool usage
158 0 $session->{_tool_count}++;
159 0 return Claude::Agent::Hook::Result->proceed();
160 }],
161 ),
162 ],
163  
164 # PostToolUseFailure: Handle tool failures gracefully
165 PostToolUseFailure => [
166 Claude::Agent::Hook::Matcher->new(
167 matcher => $any_shell_tool,
168 0 ▶ hooks => [sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
169 0 my ($input, $tool_use_id, $context) = @_;
170  
171 0 ▶ my $tool_name = $input->{tool_name} // 'unknown';
Condition0%0/2
$input->{"tool_name"} // "unknown"
l!l
00
MC/DC0%0/1
$input->{"tool_name"} // "unknown"
$input->{"tool_name"}
Truth table0%0/2
A
A= $input->{"tool_name"}
Aresult
11
00
172 0 ▶ my $error = $input->{error} // 'Unknown error';
Condition0%0/2
$input->{"error"} // "Unknown error"
l!l
00
MC/DC0%0/1
$input->{"error"} // "Unknown error"
$input->{"error"}
Truth table0%0/2
A
A= $input->{"error"}
Aresult
11
00
173  
174 # Extract short tool name
175 0 my $short_name = $tool_name;
176 0 $short_name =~ s/^mcp__shell-tools__//;
177  
178 # Track error count
179 0 $session->{_tool_errors}++;
180  
181 # Show error to user
182 0 ▶ if ($session->colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->colorful) { } 0 0 0
183 0 status('error', "Tool '$short_name' failed: $error");
184 }
185 else {
186 0 print "[ERROR] Tool '$short_name' failed: $error\n";
187 }
188  
189 0 return Claude::Agent::Hook::Result->proceed();
190 }],
191 ),
192 ],
193  
194 # Stop: Show session statistics when the agent stops
195 Stop => [
196 Claude::Agent::Hook::Matcher->new(
197 matcher => '.*', # Match any stop reason
198 0 ▶ hooks => [sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
199 0 my ($input, $tool_use_id, $context) = @_;
200  
201 0 ▶ my $duration = time() - ($session->{_session_start} // time());
Condition0%0/3
$session->{"_session_start"} // &Acme::Claude::Shell::Hooks::time()
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->{"_session_start"} // &Acme::Claude::Shell::Hooks::time()
$session->{"_session_start"} &Acme::Claude::Shell::Hooks::time()
Truth table0%0/3
A // B
A= $session->{"_session_start"}B= &Acme::Claude::Shell::Hooks::time()
ABresult
1X1
011
000
202 0 ▶ my $tool_count = $session->{_tool_count} // 0;
Condition0%0/2
$session->{"_tool_count"} // 0
l!l
00
MC/DC0%0/1
$session->{"_tool_count"} // 0
$session->{"_tool_count"}
Truth table0%0/2
A
A= $session->{"_tool_count"}
Aresult
11
00
203 0 ▶ my $tool_errors = $session->{_tool_errors} // 0;
Condition0%0/2
$session->{"_tool_errors"} // 0
l!l
00
MC/DC0%0/1
$session->{"_tool_errors"} // 0
$session->{"_tool_errors"}
Truth table0%0/2
A
A= $session->{"_tool_errors"}
Aresult
11
00
204 0 ▶ my $history_count = scalar(@{$session->_history // []});
Condition0%0/2
$session->_history // []
l!l
00
MC/DC0%0/1
$session->_history // []
$session->_history
Truth table0%0/2
A
A= $session->_history
Aresult
11
00
205  
206 0 ▶ if ($session->colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->colorful) { } 0 0 0
207 0 print "\n";
208 0 print colored(['cyan'], "─" x 40) . "\n";
209 0 status('info', "Session Statistics");
210 0 printf " Duration: %.1f seconds\n", $duration;
211 0 printf " Tools used: %d\n", $tool_count;
212 0 ▶ printf " Tool errors: %d\n", $tool_errors if $tool_errors > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tool_errors > 0 0 0 0
213 0 printf " Commands in history: %d\n", $history_count;
214 0 print colored(['cyan'], "─" x 40) . "\n";
215 }
216 else {
217 0 print "\n--- Session Statistics ---\n";
218 0 printf "Duration: %.1f seconds\n", $duration;
219 0 printf "Tools used: %d\n", $tool_count;
220 0 ▶ printf "Tool errors: %d\n", $tool_errors if $tool_errors > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tool_errors > 0 0 0 0
221 0 printf "Commands in history: %d\n", $history_count;
222 0 print "--------------------------\n";
223 }
224  
225 0 return Claude::Agent::Hook::Result->proceed();
226 }],
227 ),
228 ],
229  
230 # Notification: Log important events
231 Notification => [
232 Claude::Agent::Hook::Matcher->new(
233 matcher => '.*', # Match all notifications
234 0 ▶ hooks => [sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
235 0 my ($input, $tool_use_id, $context) = @_;
236  
237 0 ▶ my $type = $input->{notification_type} // 'unknown';
Condition0%0/2
$input->{"notification_type"} // "unknown"
l!l
00
MC/DC0%0/1
$input->{"notification_type"} // "unknown"
$input->{"notification_type"}
Truth table0%0/2
A
A= $input->{"notification_type"}
Aresult
11
00
238 0 ▶ my $data = $input->{data} // {};
Condition0%0/2
$input->{"data"} // {}
l!l
00
MC/DC0%0/1
$input->{"data"} // {}
$input->{"data"}
Truth table0%0/2
A
A= $input->{"data"}
Aresult
11
00
239  
240 # Log certain notification types if verbose mode is on
241 0 ▶ if ($session->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"verbose"}) 0 0 0
242 0 ▶ if ($session->colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->colorful) { } 0 0 0
243 0 status('info', "Notification: $type");
244 }
245 else {
246 0 print "[Notification] $type\n";
247 }
248 }
249  
250 0 return Claude::Agent::Hook::Result->proceed();
251 3 }],
252 ),
253 ],
254 };
255 }
256  
257 =head1 AUTHOR
258  
259 LNATION, C<< <email at lnation.org> >>
260  
261 =head1 LICENSE AND COPYRIGHT
262  
263 This software is Copyright (c) 2026 by LNATION.
264  
265 This is free software, licensed under:
266  
267 The Artistic License 2.0 (GPL Compatible)
268  
269 =cut
270  
271 7 1;
« blib/lib/Acme/Claude/Shell.pm ↑ Summary bin/acme_claude_shell »