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

filter: Statementstmt 49.6% 184 / 371 Branchbran 35.0% 68 / 194 Conditioncond 25.2% 23 / 91 MC/DCmcdc 12.7% 7 / 55 Subroutinesub 64.8% 24 / 37 Podpod 100% 1 / 1 totaltotal 40.9% 307 / 749 CC 136 SCAR 50.7
CC
136
Cov
42%
_confirm_command
812.0
_format_perms
110.0
_execute_command
90.0
CRAP 159.6
↑ Summary blib/lib/Acme/Claude/Shell/Query.pm »
1 package Acme::Claude::Shell::Tools;
2  
3 9 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 our @EXPORT_OK = qw(shell_tools);
9  
10 9 ▶use Claude::Agent qw(tool);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Claude::Agent::CLI qw(menu status ask_yn prompt start_spinner stop_spinner);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶sub shell_tools {
Subroutine shell_tools: called CC=1 SCAR=0.0
Pod documented
19 1 my ($session) = @_;
20  
21 return [
22 tool(
23 'execute_command',
24 'Execute a shell command and return its output. Use this for ALL shell operations including listing files, reading files, etc. The user will be prompted to approve each command.',
25 {
26 type => 'object',
27 properties => {
28 command => {
29 type => 'string',
30 description => 'The shell command to execute (e.g., "ls -la", "cat file.txt", "find . -name *.pl")',
31 },
32 working_dir => {
33 type => 'string',
34 description => 'Directory to run command in (optional, defaults to current directory)',
35 },
36 },
37 required => ['command'],
38 },
39 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 my ($params, $loop) = @_;
41 0 return _execute_command($session, $params, $loop);
42 },
43 ),
44  
45 tool(
46 'get_working_directory',
47 'Get the current working directory. This is safe and does not require user confirmation.',
48 {
49 type => 'object',
50 properties => {},
51 },
52 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 my ($params, $loop) = @_;
54 1 my $future = $loop->new_future;
55 1 $future->done(_mcp_result(getcwd()));
56 1 return $future;
57 },
58 ),
59  
60 tool(
61 'read_file',
62 'Read the contents of a file. Safe operation - does not require user confirmation. Use this instead of execute_command for reading files.',
63 {
64 type => 'object',
65 properties => {
66 path => {
67 type => 'string',
68 description => 'Path to the file to read',
69 },
70 lines => {
71 type => 'integer',
72 description => 'Number of lines to read from the beginning (optional)',
73 },
74 tail => {
75 type => 'integer',
76 description => 'Number of lines to read from the end (optional)',
77 },
78 },
79 required => ['path'],
80 },
81 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 3 my ($params, $loop) = @_;
83 3 return _read_file_safe($session, $params, $loop);
84 },
85 ),
86  
87 tool(
88 'list_directory',
89 'List the contents of a directory. Safe operation - does not require user confirmation. Use this instead of execute_command for listing files.',
90 {
91 type => 'object',
92 properties => {
93 path => {
94 type => 'string',
95 description => 'Path to the directory to list (defaults to current directory)',
96 },
97 pattern => {
98 type => 'string',
99 description => 'Glob pattern to filter files (e.g., "*.pl", "*.txt")',
100 },
101 long_format => {
102 type => 'boolean',
103 description => 'Show detailed file information (size, date, permissions)',
104 },
105 show_hidden => {
106 type => 'boolean',
107 description => 'Include hidden files (starting with .)',
108 },
109 },
110 },
111 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 2 my ($params, $loop) = @_;
113 2 return _list_directory_safe($session, $params, $loop);
114 },
115 ),
116  
117 tool(
118 'search_files',
119 'Search for files by name pattern or content. Safe operation - does not require user confirmation.',
120 {
121 type => 'object',
122 properties => {
123 pattern => {
124 type => 'string',
125 description => 'File name pattern to search for (e.g., "*.pm", "config*")',
126 },
127 content => {
128 type => 'string',
129 description => 'Text pattern to search for within files (grep)',
130 },
131 path => {
132 type => 'string',
133 description => 'Directory to search in (defaults to current directory)',
134 },
135 max_depth => {
136 type => 'integer',
137 description => 'Maximum directory depth to search',
138 },
139 },
140 },
141 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 2 my ($params, $loop) = @_;
143 2 return _search_files_safe($session, $params, $loop);
144 },
145 ),
146  
147 tool(
148 'get_system_info',
149 'Get system information including OS, disk space, and memory. Safe operation - does not require user confirmation.',
150 {
151 type => 'object',
152 properties => {
153 info_type => {
154 type => 'string',
155 description => 'Type of info: "all", "os", "disk", "memory", "processes" (defaults to "all")',
156 enum => ['all', 'os', 'disk', 'memory', 'processes'],
157 },
158 },
159 },
160 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 2 my ($params, $loop) = @_;
162 2 return _get_system_info($session, $params, $loop);
163 },
164 1 ),
165 ];
166 }
167  
168 0 ▶sub _execute_command {
Subroutine _execute_command: not called CC=9 SCAR=45.0
169 0 my ($session, $params, $loop) = @_;
170  
171 0 my $command = $params->{command};
172 0 ▶ my $dir = $params->{working_dir} // $session->working_dir;
Condition0%0/3
$params->{"working_dir"} // $session->working_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{"working_dir"} // $session->working_dir
$params->{"working_dir"} $session->working_dir
Truth table0%0/3
A // B
A= $params->{"working_dir"}B= $session->working_dir
ABresult
1X1
011
000
173 0 my $colorful = $session->colorful;
174  
175 0 ▶ if ($session->can('_spinner') && $session->_spinner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->can("_spinner") and $session->_spinner) 0 0 0
Condition0%0/3
$session->can("_spinner") and $session->_spinner
!ll&&!rl&&r
000
MC/DC0%0/2
$session->can("_spinner") and $session->_spinner
$session->can("_spinner") $session->_spinner
Truth table0%0/3
A and B
A= $session->can("_spinner")B= $session->_spinner
ABresult
0X0
100
111
176 0 stop_spinner($session->_spinner);
177 0 $session->_spinner(undef);
178 }
179  
180 0 my ($approved, $new_command) = _confirm_command($session, $command);
181  
182 0 ▶ unless ($approved) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($approved) 0 0 0
183 0 my $future = $loop->new_future;
184 0 $future->done(_mcp_result("User cancelled command", 1));
185 0 return $future;
186 }
187  
188 0 ▶ $command = $new_command if defined $new_command;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_command 0 0 0
189  
190 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) 0 0 0
191 0 $session->_spinner(start_spinner("Executing...", $loop));
192 }
193  
194 0 push @{$session->_history}, {
195 time => _timestamp(),
196 command => $command,
197 status => 'running',
198 };
199  
200 0 my $future = $loop->new_future;
201 0 my $stdout = '';
202 0 my $stderr = '';
203  
204 my $process = IO::Async::Process->new(
205 command => [ '/bin/sh', '-c', $command ],
206 ($dir && -d $dir ? (setup => [ chdir => $dir ]) : ()),
207 stdout => {
208 into => \$stdout,
209 },
210 stderr => {
211 into => \$stderr,
212 },
213 0 ▶ on_finish => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
214 0 my ($self, $exitcode) = @_;
215 0 my $exit_status = $exitcode >> 8;
216  
217 0 ▶ if ($exit_status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_status != 0) { } 0 0 0
218 0 $session->_history->[-1]{status} = "exit $exit_status";
219 0 ▶ my $output = $stderr || $stdout || "Command failed with exit code $exit_status";
Condition0%0/5
$stderr || $stdout || "Command failed with exit code $exit_status"
l!l
00
$stderr or $stdout
l!l&&r!l&&!r
000
MC/DC0%0/2
$stderr || $stdout || "Command failed with exit code $exit_status"
$stderr $stdout
Truth table0%0/3
A or B
A= $stderrB= $stdout
ABresult
1X1
011
000
220 0 $future->done(_mcp_result($output));
221 } else {
222 0 $session->_history->[-1]{status} = 'success';
223 0 ▶ $future->done(_mcp_result($stdout // ''));
Condition0%0/2
$stdout // ""
l!l
00
MC/DC0%0/1
$stdout // ""
$stdout
Truth table0%0/2
A
A= $stdout
Aresult
11
00
224 }
225 },
226 0 ▶ on_exception => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 0 my ($self, $exception, $errno, $exitcode) = @_;
228 0 $session->_history->[-1]{status} = 'error';
229 0 $future->done(_mcp_result("Error: $exception", 1));
230 },
231 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$dir && -d $dir ? : 0 0 0
Condition0%0/3
$dir and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir and -d $dir
$dir -d $dir
Truth table0%0/3
A and B
A= $dirB= -d $dir
ABresult
0X0
100
111
232  
233 0 $loop->add($process);
234  
235 0 return $future;
236 }
237  
238 10 ▶sub _mcp_result {
Subroutine _mcp_result: called CC=2 SCAR=6.9
239 10 my ($text, $is_error) = @_;
240 return {
241 10 ▶ content => [{ type => 'text', text => $text }],
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 1 9 10
242 is_error => $is_error ? 1 : 0,
243 };
244 }
245  
246 9 my @DANGEROUS_PATTERNS = (
247 { pattern => qr/\brm\s+(-[rf]+|--recursive|--force)/i,
248 reason => 'Recursive or forced file deletion' },
249 { pattern => qr/\bsudo\b/,
250 reason => 'Superuser command' },
251 { pattern => qr/\bmkfs\b/,
252 reason => 'Filesystem formatting' },
253 { pattern => qr/\bdd\b.*\bof=/,
254 reason => 'Direct disk write' },
255 { pattern => qr/>\s*\/dev\//,
256 reason => 'Writing to device file' },
257 { pattern => qr/\bchmod\s+(-R\s+)?777\b/,
258 reason => 'World-writable permissions' },
259 { pattern => qr/\bchown\s+-R\b.*\//,
260 reason => 'Recursive ownership change' },
261 { pattern => qr/\bkill\s+-9\b/,
262 reason => 'Forceful process termination' },
263 { pattern => qr/\b(reboot|shutdown|halt|poweroff)\b/,
264 reason => 'System shutdown/reboot' },
265 { pattern => qr/\bformat\b/,
266 reason => 'Disk formatting' },
267 { pattern => qr/:\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;/,
268 reason => 'Fork bomb detected' },
269 { pattern => qr/\bwget\b.*\|\s*(ba)?sh/i,
270 reason => 'Piping remote script to shell' },
271 { pattern => qr/\bcurl\b.*\|\s*(ba)?sh/i,
272 reason => 'Piping remote script to shell' },
273 );
274  
275 0 ▶sub _check_dangerous {
Subroutine _check_dangerous: not called CC=3 SCAR=24.8
276 0 my ($command) = @_;
277 0 for my $check (@DANGEROUS_PATTERNS) {
278 0 ▶ if ($command =~ $check->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /$check->{"pattern"}/u) 0 0 0
279 0 return $check;
280 }
281 }
282 0 return undef;
283 }
284  
285 0 ▶sub _choose_action {
Subroutine _choose_action: not called CC=5 SCAR=34.0
286 0 my ($colorful) = @_;
287 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) 0 0 0
288 0 ▶ return menu("Action", [
Condition0%0/2
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}]) // "x"
l!l
00
MC/DC0%0/1
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}]) // "x"
menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}])
Truth table0%0/2
A
A= menu("Action", [{"key", "a", "label", "Approve and run"}, {"key", "d", "label", "Dry-run (preview only)"}, {"key", "e", "label", "Edit command"}, {"key", "x", "label", "Cancel"}])
Aresult
11
00
289 { key => 'a', label => 'Approve and run' },
290 { key => 'd', label => 'Dry-run (preview only)' },
291 { key => 'e', label => 'Edit command' },
292 { key => 'x', label => 'Cancel' },
293 ]) // 'x';
294 }
295 0 print "Action: [a]pprove, [d]ry-run, [e]dit, [x] cancel > ";
296 0 my $answer = <STDIN>;
297 0 ▶ return 'x' unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 0 0
298 0 $answer =~ s/^\s+|\s+$//g;
299 0 ▶ return $answer =~ /^[adex]$/i ? lc $answer : 'x';
Branch: 1 branch
BranchTrueFalseTotal
$answer =~ /^[adex]$/iu ? : 0 0 0
300 }
301  
302 0 ▶sub _confirm_command {
Subroutine _confirm_command: not called CC=28 SCAR=67.0
303 0 my ($session, $command) = @_;
304  
305 0 my $colorful = $session->colorful;
306  
307 0 my $danger = _check_dangerous($command);
308  
309 0 print "\n";
310  
311 0 ▶ if ($danger && $session->safe_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($danger and $session->safe_mode) 0 0 0
Condition0%0/3
$danger and $session->safe_mode
!ll&&!rl&&r
000
MC/DC0%0/2
$danger and $session->safe_mode
$danger $session->safe_mode
Truth table0%0/3
A and B
A= $dangerB= $session->safe_mode
ABresult
0X0
100
111
312 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
313 0 status('warning', "Potentially dangerous command detected!");
314 0 print colored(['yellow'], " Reason: $danger->{reason}\n");
315 } else {
316 0 print "WARNING: Potentially dangerous command!\n";
317 0 print " Reason: $danger->{reason}\n";
318 }
319 0 print "\n";
320 }
321  
322 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
323 0 status('info', "Command: $command");
324 } else {
325 0 print "Command: $command\n";
326 }
327  
328 0 my $choice = _choose_action($colorful);
329  
330 0 ▶ if ($choice eq 'x') {
Branch: 3 branches
BranchTrueFalseTotal
if ($choice eq 'x') { } 0 0 0
elsif ($choice eq 'd') { } 0 0 0
elsif ($choice eq 'e') { } 0 0 0
331 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
332 0 status('warning', "Command cancelled");
333 } else {
334 0 print "Cancelled.\n";
335 }
336 0 return (0, undef);
337 }
338 elsif ($choice eq 'd') {
339 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
340 0 status('info', "[DRY-RUN] Would execute:");
341 0 print colored(['cyan'], " $command\n\n");
342 } else {
343 0 print "[DRY-RUN] Would execute: $command\n";
344 }
345 0 return (0, undef);
346 }
347 elsif ($choice eq 'e') {
348 0 my $new_cmd;
349 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
350 0 $new_cmd = prompt("Edit command:", $command);
351 } else {
352 0 print "Edit command [$command]: ";
353 0 $new_cmd = <STDIN>;
354 0 ▶ chomp $new_cmd if defined $new_cmd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_cmd 0 0 0
355 0 ▶ $new_cmd = $command unless length($new_cmd // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($new_cmd // "") 0 0 0
Condition0%0/2
$new_cmd // ""
l!l
00
MC/DC0%0/1
$new_cmd // ""
$new_cmd
Truth table0%0/2
A
A= $new_cmd
Aresult
11
00
356 }
357  
358 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
359 0 status('info', "Modified command:");
360 0 print colored(['bold', 'white'], " $new_cmd\n\n");
361 } else {
362 0 print "Modified: $new_cmd\n";
363 }
364  
365 0 ▶ if (_check_dangerous($new_cmd) && $session->safe_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (_check_dangerous($new_cmd) and $session->safe_mode) 0 0 0
Condition0%0/3
_check_dangerous($new_cmd) and $session->safe_mode
!ll&&!rl&&r
000
MC/DC0%0/2
_check_dangerous($new_cmd) and $session->safe_mode
_check_dangerous($new_cmd) $session->safe_mode
Truth table0%0/3
A and B
A= _check_dangerous($new_cmd)B= $session->safe_mode
ABresult
0X0
100
111
366 0 my $confirmed;
367 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
368 0 $confirmed = ask_yn("Are you SURE you want to run this command?", 'n');
369 } else {
370 0 print "Are you SURE? (y/N): ";
371 0 my $ans = <STDIN>;
372 0 ▶ chomp $ans if defined $ans;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ans 0 0 0
373 0 ▶ $confirmed = ($ans // '') =~ /^y/i;
Condition0%0/2
$ans // ""
l!l
00
MC/DC0%0/1
$ans // ""
$ans
Truth table0%0/2
A
A= $ans
Aresult
11
00
374 }
375 0 ▶ return (0, undef) unless $confirmed;
Branch: 1 branch
BranchTrueFalseTotal
unless $confirmed 0 0 0
376 }
377  
378 0 return (1, $new_cmd);
379 }
380  
381 0 ▶ if ($danger && $session->safe_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($danger and $session->safe_mode) 0 0 0
Condition0%0/3
$danger and $session->safe_mode
!ll&&!rl&&r
000
MC/DC0%0/2
$danger and $session->safe_mode
$danger $session->safe_mode
Truth table0%0/3
A and B
A= $dangerB= $session->safe_mode
ABresult
0X0
100
111
382 0 my $confirmed;
383 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
384 0 $confirmed = ask_yn("Are you SURE you want to run this dangerous command?", 'n');
385 } else {
386 0 print "Are you SURE? (y/N): ";
387 0 my $ans = <STDIN>;
388 0 ▶ chomp $ans if defined $ans;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ans 0 0 0
389 0 ▶ $confirmed = ($ans // '') =~ /^y/i;
Condition0%0/2
$ans // ""
l!l
00
MC/DC0%0/1
$ans // ""
$ans
Truth table0%0/2
A
A= $ans
Aresult
11
00
390 }
391  
392 0 ▶ unless ($confirmed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($confirmed) 0 0 0
393 0 ▶ if ($colorful) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colorful) { } 0 0 0
394 0 status('warning', "Command cancelled");
395 } else {
396 0 print "Cancelled.\n";
397 }
398 0 return (0, undef);
399 }
400 }
401  
402 0 return (1, undef);
403 }
404  
405 0 ▶sub _list_files {
Subroutine _list_files: not called CC=8 SCAR=42.8
406 0 my ($session, $params, $loop) = @_;
407  
408 0 ▶ my $path = $params->{path} // '.';
Condition0%0/2
$params->{"path"} // "."
l!l
00
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table0%0/2
A
A= $params->{"path"}
Aresult
11
00
409 0 ▶ my $pattern = $params->{pattern} // '';
Condition0%0/2
$params->{"pattern"} // ""
l!l
00
MC/DC0%0/1
$params->{"pattern"} // ""
$params->{"pattern"}
Truth table0%0/2
A
A= $params->{"pattern"}
Aresult
11
00
410 0 ▶ my $long = $params->{long_format} // 1;
Condition0%0/2
$params->{"long_format"} // 1
l!l
00
MC/DC0%0/1
$params->{"long_format"} // 1
$params->{"long_format"}
Truth table0%0/2
A
A= $params->{"long_format"}
Aresult
11
00
411 0 ▶ my $hidden = $params->{hidden} // 0;
Condition0%0/2
$params->{"hidden"} // 0
l!l
00
MC/DC0%0/1
$params->{"hidden"} // 0
$params->{"hidden"}
Truth table0%0/2
A
A= $params->{"hidden"}
Aresult
11
00
412  
413 0 my @opts;
414 0 ▶ push @opts, '-l' if $long;
Branch: 1 branch
BranchTrueFalseTotal
if $long 0 0 0
415 0 ▶ push @opts, '-a' if $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $hidden 0 0 0
416  
417 0 ▶ my $target = $pattern ? "$path/$pattern" : $path;
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 0 0 0
418 0 my $cmd = "ls @opts $target 2>/dev/null || ls @opts $path";
419  
420 0 return _execute_command($session, { command => $cmd }, $loop);
421 }
422  
423 0 ▶sub _read_file {
Subroutine _read_file: not called CC=3 SCAR=24.8
424 0 my ($session, $params, $loop) = @_;
425  
426 0 my $path = $params->{path};
427 0 my $lines = $params->{lines};
428 0 my $tail = $params->{tail};
429  
430 0 my $cmd;
431 0 ▶ if ($tail) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tail) { } 0 0 0
elsif ($lines) { } 0 0 0
432 0 $cmd = "tail -n $tail " . _shell_quote($path);
433 } elsif ($lines) {
434 0 $cmd = "head -n $lines " . _shell_quote($path);
435 } else {
436 0 $cmd = "cat " . _shell_quote($path);
437 }
438  
439 0 return _execute_command($session, { command => $cmd }, $loop);
440 }
441  
442 0 ▶sub _shell_quote {
Subroutine _shell_quote: not called CC=1 SCAR=6.9
443 0 my ($str) = @_;
444 0 $str =~ s/'/'\\''/g;
445 0 return "'$str'";
446 }
447  
448 0 ▶sub _timestamp {
Subroutine _timestamp: not called CC=1 SCAR=6.9
449 0 my @t = localtime;
450 0 return sprintf("%04d-%02d-%02d %02d:%02d:%02d",
451 $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0]);
452 }
453  
454 3 ▶sub _read_file_safe {
Subroutine _read_file_safe: called CC=14 SCAR=32.7
455 3 my ($session, $params, $loop) = @_;
456  
457 3 my $path = $params->{path};
458 3 my $lines = $params->{lines};
459 3 my $tail_lines = $params->{tail};
460  
461 3 my $future = $loop->new_future;
462  
463 3 my $full_path = $path;
464 3 ▶ if ($path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[^/]u) 0 3 3
465 0 ▶ $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path);
Condition0%0/3
$session->working_dir // getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
ABresult
1X1
011
000
466 }
467  
468 3 ▶ unless (-e $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $full_path) 1 2 3
469 1 $future->done(_mcp_result("Error: File not found: $path", 1));
470 1 return $future;
471 }
472  
473 2 ▶ unless (-r $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $full_path) 0 2 2
474 0 $future->done(_mcp_result("Error: Permission denied: $path", 1));
475 0 return $future;
476 }
477  
478 2 ▶ if (-d $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full_path) 0 2 2
479 0 $future->done(_mcp_result("Error: Path is a directory: $path", 1));
480 0 return $future;
481 }
482  
483 2 my $content;
484 2 ▶ if (open my $fh, '<', $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $full_path) { } 2 0 2
485 2 ▶ if ($lines) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lines) { } 1 1 2
elsif ($tail_lines) { } 0 1 1
486 1 my @lines;
487 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 0 2
488 2 push @lines, $line;
489 2 ▶ last if @lines >= $lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines >= $lines 1 1 2
490 }
491 1 $content = join('', @lines);
492 }
493 elsif ($tail_lines) {
494 0 my @buffer;
495 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
496 0 push @buffer, $line;
497 0 ▶ shift @buffer if @buffer > $tail_lines;
Branch: 1 branch
BranchTrueFalseTotal
if @buffer > $tail_lines 0 0 0
498 }
499 0 $content = join('', @buffer);
500 }
501 else {
502 1 local $/;
503 1 $content = <$fh>;
504 }
505 2 close $fh;
506 2 ▶ $future->done(_mcp_result($content // ''));
Condition50%1/2
$content // ""
l!l
20
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
507 }
508 else {
509 0 $future->done(_mcp_result("Error reading file: $!", 1));
510 }
511  
512 2 return $future;
513 }
514  
515 2 ▶sub _list_directory_safe {
Subroutine _list_directory_safe: called CC=13 SCAR=35.8
516 2 my ($session, $params, $loop) = @_;
517  
518 2 ▶ my $path = $params->{path} // '.';
Condition50%1/2
$params->{"path"} // "."
l!l
20
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table50%1/2
A
A= $params->{"path"}
Aresult
11
00
519 2 my $pattern = $params->{pattern};
520 2 my $long_format = $params->{long_format};
521 2 my $show_hidden = $params->{show_hidden};
522  
523 2 my $future = $loop->new_future;
524  
525 2 my $full_path = $path;
526 2 ▶ if ($path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[^/]u) 0 2 2
527 0 ▶ $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path);
Condition0%0/3
$session->working_dir // getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
ABresult
1X1
011
000
528 }
529  
530 2 ▶ unless (-d $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $full_path) 0 2 2
531 0 $future->done(_mcp_result("Error: Not a directory: $path", 1));
532 0 return $future;
533 }
534  
535 2 ▶ unless (opendir my $dh, $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $full_path) { } 2 0 2
536 0 $future->done(_mcp_result("Error: Cannot open directory: $!", 1));
537 0 return $future;
538 }
539 else {
540 2 my @entries = readdir($dh);
541 2 closedir $dh;
542  
543 2 ▶ unless ($show_hidden) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_hidden) { } 0 2 2
544 2 @entries = grep { !/^\./ } @entries;
545 }
546 else {
547 0 ▶ @entries = grep { $_ ne '.' && $_ ne '..' } @entries;
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
548 }
549  
550 2 ▶ if ($pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) 1 1 2
551 1 my $regex = _glob_to_regex($pattern);
552 1 @entries = grep { /$regex/ } @entries;
553 }
554  
555 2 @entries = sort @entries;
556  
557 2 my @output;
558 2 ▶ if ($long_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($long_format) { } 0 2 2
559 0 for my $entry (@entries) {
560 0 my $entry_path = File::Spec->catfile($full_path, $entry);
561 0 my @stat = stat($entry_path);
562 0 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) { } 0 0 0
563 0 my $size = $stat[7];
564 0 my $mtime = $stat[9];
565 0 my $mode = $stat[2];
566 0 ▶ my $type = -d $entry_path ? 'd' : '-';
Branch: 1 branch
BranchTrueFalseTotal
-d $entry_path ? : 0 0 0
567 0 my $perms = _format_perms($mode);
568 0 my $date = _format_date($mtime);
569 0 push @output, sprintf("%s%s %8d %s %s",
570 $type, $perms, $size, $date, $entry);
571 }
572 else {
573 0 push @output, $entry;
574 }
575 }
576 }
577 else {
578 2 @output = @entries;
579 }
580  
581 2 $future->done(_mcp_result(join("\n", @output)));
582 }
583  
584 2 return $future;
585 }
586  
587 2 ▶sub _search_files_safe {
Subroutine _search_files_safe: called CC=10 SCAR=26.6
588 2 my ($session, $params, $loop) = @_;
589  
590 2 my $pattern = $params->{pattern};
591 2 my $content = $params->{content};
592 2 ▶ my $path = $params->{path} // '.';
Condition50%1/2
$params->{"path"} // "."
l!l
20
MC/DC0%0/1
$params->{"path"} // "."
$params->{"path"}
Truth table50%1/2
A
A= $params->{"path"}
Aresult
11
00
593 2 my $max_depth = $params->{max_depth};
594  
595 2 my $future = $loop->new_future;
596  
597 2 ▶ unless ($pattern || $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern or $content) 0 2 2
Condition66%2/3
$pattern or $content
l!l&&r!l&&!r
110
MC/DC0%0/2
$pattern or $content
$pattern $content
Truth table66%2/3
A or B
A= $patternB= $content
ABresult
1X1
011
000
598 0 $future->done(_mcp_result("Error: Must specify 'pattern' (filename) or 'content' (text search)", 1));
599 0 return $future;
600 }
601  
602 2 my $full_path = $path;
603 2 ▶ if ($path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[^/]u) 0 2 2
604 0 ▶ $full_path = File::Spec->catfile($session->working_dir // getcwd(), $path);
Condition0%0/3
$session->working_dir // getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir getcwd()
Truth table0%0/3
A // B
A= $session->working_dirB= getcwd()
ABresult
1X1
011
000
605 }
606  
607 2 ▶ unless (-d $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $full_path) 0 2 2
608 0 $future->done(_mcp_result("Error: Not a directory: $path", 1));
609 0 return $future;
610 }
611  
612 2 my @results;
613 2 ▶ my $regex = $pattern ? _glob_to_regex($pattern) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 1 1 2
614 2 ▶ my $content_re = $content ? qr/\Q$content\E/i : undef;
Branch: 1 branch
BranchTrueFalseTotal
$content ? : 1 1 2
615  
616 2 _search_recursive($full_path, $full_path, $regex, $content_re, $max_depth, 0, \@results);
617  
618 2 ▶ if (@results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results) { } 2 0 2
619 2 $future->done(_mcp_result(join("\n", @results)));
620 }
621 else {
622 0 $future->done(_mcp_result("No matches found"));
623 }
624  
625 2 return $future;
626 }
627  
628 4 ▶sub _search_recursive {
Subroutine _search_recursive: called CC=19 SCAR=30.1
629 4 my ($base, $dir, $name_re, $content_re, $max_depth, $depth, $results) = @_;
630  
631 4 ▶ return if defined $max_depth && $depth > $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_depth and $depth > $max_depth 0 4 4
Condition33%1/3
defined $max_depth and $depth > $max_depth
!ll&&!rl&&r
400
MC/DC0%0/2
defined $max_depth and $depth > $max_depth
defined $max_depth $depth > $max_depth
Truth table33%1/3
A and B
A= defined $max_depthB= $depth > $max_depth
ABresult
0X0
100
111
632 4 ▶ return if @$results >= 100;
Branch: 1 branch
BranchTrueFalseTotal
if @$results >= 100 0 4 4
633  
634 4 ▶ opendir my $dh, $dir or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return 4 0 4
635 4 my @entries = readdir($dh);
636 4 closedir $dh;
637  
638 4 for my $entry (sort @entries) {
639 16 ▶ next if $entry eq '.' || $entry eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 8 8 16
Condition100%3/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
448
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
640  
641 8 my $path = File::Spec->catfile($dir, $entry);
642 8 my $rel_path = File::Spec->abs2rel($path, $base);
643  
644 8 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 2 6 8
elsif (-f $path) { } 6 0 6
645 2 _search_recursive($base, $path, $name_re, $content_re, $max_depth, $depth + 1, $results);
646 }
647 elsif (-f $path) {
648 6 ▶ my $name_match = !$name_re || $entry =~ $name_re;
Condition100%3/3
!$name_re || $entry =~ /$name_re/u
l!l&&r!l&&!r
321
MC/DC100%2/2
!$name_re || $entry =~ /$name_re/u
!$name_re $entry =~ /$name_re/u
Truth table100%3/3
not(A) || B
A= !$name_reB= $entry =~ /$name_re/u
ABresult
1X1
011
000
649  
650 6 ▶ if ($name_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_match) 5 1 6
651 5 ▶ if ($content_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_re) { } 3 2 5
652 3 ▶ if (open my $fh, '<', $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $path) 3 0 3
653 3 my $line_num = 0;
654 3 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 9 3 12
655 9 $line_num++;
656 9 ▶ if ($line =~ $content_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$content_re/u) 1 8 9
657 1 chomp $line;
658 1 ▶ $line = substr($line, 0, 100) . '...' if length($line) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $line > 100 0 1 1
659 1 push @$results, "$rel_path:$line_num: $line";
660 1 ▶ last if @$results >= 100;
Branch: 1 branch
BranchTrueFalseTotal
if @$results >= 100 0 1 1
661 }
662 }
663 3 close $fh;
664 }
665 }
666 else {
667 2 push @$results, $rel_path;
668 }
669 }
670 }
671  
672 8 ▶ last if @$results >= 100;
Branch: 1 branch
BranchTrueFalseTotal
if @$results >= 100 0 8 8
673 }
674 }
675  
676 2 ▶sub _get_system_info {
Subroutine _get_system_info: called CC=20 SCAR=31.2
677 2 my ($session, $params, $loop) = @_;
678  
679 2 ▶ my $info_type = $params->{info_type} // 'all';
Condition50%1/2
$params->{"info_type"} // "all"
l!l
20
MC/DC0%0/1
$params->{"info_type"} // "all"
$params->{"info_type"}
Truth table50%1/2
A
A= $params->{"info_type"}
Aresult
11
00
680 2 my $future = $loop->new_future;
681  
682 2 my @info;
683  
684 2 ▶ if ($info_type eq 'all' || $info_type eq 'os') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_type eq "all" or $info_type eq "os") 2 0 2
Condition66%2/3
$info_type eq "all" or $info_type eq "os"
l!l&&r!l&&!r
110
MC/DC0%0/2
$info_type eq "all" or $info_type eq "os"
$info_type eq "all" $info_type eq "os"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "os"
ABresult
1X1
011
000
685 2 push @info, "=== OS Information ===";
686 2 push @info, "System: $^O";
687 2 push @info, "Perl: $^V";
688 2 ▶ if (open my $fh, '<', '/etc/os-release') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/etc/os-release") 2 0 2
689 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 26 2 28
690 26 chomp;
691 26 ▶ push @info, $_ if /^(NAME|VERSION|PRETTY_NAME)=/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(NAME|VERSION|PRETTY_NAME)=/u 6 20 26
692 }
693 2 close $fh;
694 }
695 2 push @info, "";
696 }
697  
698 2 ▶ if ($info_type eq 'all' || $info_type eq 'disk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_type eq "all" or $info_type eq "disk") 1 1 2
Condition66%2/3
$info_type eq "all" or $info_type eq "disk"
l!l&&r!l&&!r
101
MC/DC50%1/2
$info_type eq "all" or $info_type eq "disk"
$info_type eq "all" $info_type eq "disk"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "disk"
ABresult
1X1
011
000
699 1 push @info, "=== Disk Usage ===";
700 1 ▶ my $cwd = $session->working_dir // getcwd();
Condition33%1/3
$session->working_dir // getcwd()
l!l&&r!l&&!r
100
MC/DC0%0/2
$session->working_dir // getcwd()
$session->working_dir getcwd()
Truth table33%1/3
A // B
A= $session->working_dirB= getcwd()
ABresult
1X1
011
000
701 1 push @info, "Working directory: $cwd";
702 1 push @info, "";
703 }
704  
705 2 ▶ if ($info_type eq 'all' || $info_type eq 'memory') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_type eq "all" or $info_type eq "memory") 1 1 2
Condition66%2/3
$info_type eq "all" or $info_type eq "memory"
l!l&&r!l&&!r
101
MC/DC50%1/2
$info_type eq "all" or $info_type eq "memory"
$info_type eq "all" $info_type eq "memory"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "memory"
ABresult
1X1
011
000
706 1 push @info, "=== Memory ===";
707 1 ▶ if ($^O eq 'darwin') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'darwin') { } 0 1 1
elsif (-r '/proc/meminfo') { } 1 0 1
708 0 push @info, "(Memory info requires system command on macOS)";
709 }
710 elsif (-r '/proc/meminfo') {
711 1 ▶ if (open my $fh, '<', '/proc/meminfo') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/proc/meminfo") 1 0 1
712 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 54 1 55
713 54 ▶ push @info, $_ if /^(MemTotal|MemFree|MemAvailable|Buffers|Cached):/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(MemTotal|MemFree|MemAvailable|Buffers|Cached):/u 5 49 54
714 }
715 1 close $fh;
716 }
717 }
718 1 push @info, "";
719 }
720  
721 2 ▶ if ($info_type eq 'all' || $info_type eq 'processes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info_type eq "all" or $info_type eq "processes") 1 1 2
Condition66%2/3
$info_type eq "all" or $info_type eq "processes"
l!l&&r!l&&!r
101
MC/DC50%1/2
$info_type eq "all" or $info_type eq "processes"
$info_type eq "all" $info_type eq "processes"
Truth table66%2/3
A or B
A= $info_type eq "all"B= $info_type eq "processes"
ABresult
1X1
011
000
722 1 push @info, "=== Current Process ===";
723 1 push @info, "PID: $$";
724 1 ▶ push @info, "User: " . (getpwuid($<) // $<);
Condition33%1/3
getpwuid $< // $<
l!l&&r!l&&!r
100
MC/DC0%0/2
getpwuid $< // $<
getpwuid $< $<
Truth table33%1/3
A // B
A= getpwuid $<B= $<
ABresult
1X1
011
000
725 1 push @info, "";
726 }
727  
728 2 $future->done(_mcp_result(join("\n", @info)));
729 2 return $future;
730 }
731  
732 2 ▶sub _glob_to_regex {
Subroutine _glob_to_regex: called CC=1 SCAR=0.0
733 2 my ($glob) = @_;
734 2 $glob =~ s/\./\\./g;
735 2 $glob =~ s/\*/.*/g;
736 2 $glob =~ s/\?/./g;
737 2 return qr/^$glob$/i;
738 }
739  
740 0 ▶sub _format_perms {
Subroutine _format_perms: not called CC=10 SCAR=47.0
741 0 my ($mode) = @_;
742 0 my $perms = '';
743 0 ▶ $perms .= ($mode & 0400) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 256 ? : 0 0 0
744 0 ▶ $perms .= ($mode & 0200) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 128 ? : 0 0 0
745 0 ▶ $perms .= ($mode & 0100) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 64 ? : 0 0 0
746 0 ▶ $perms .= ($mode & 0040) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 32 ? : 0 0 0
747 0 ▶ $perms .= ($mode & 0020) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 16 ? : 0 0 0
748 0 ▶ $perms .= ($mode & 0010) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 8 ? : 0 0 0
749 0 ▶ $perms .= ($mode & 0004) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 4 ? : 0 0 0
750 0 ▶ $perms .= ($mode & 0002) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 2 ? : 0 0 0
751 0 ▶ $perms .= ($mode & 0001) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$mode & 1 ? : 0 0 0
752 0 return $perms;
753 }
754  
755 0 ▶sub _format_date {
Subroutine _format_date: not called CC=1 SCAR=6.9
756 0 my ($time) = @_;
757 0 my @t = localtime($time);
758 0 return sprintf("%s %2d %02d:%02d",
759 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$t[4]],
760 $t[3], $t[2], $t[1]);
761 }
762  
763  
764 9 1;
765  
766 __END__
↑ Summary blib/lib/Acme/Claude/Shell/Query.pm »