blib/lib/MCP/Run/Compress.pm

filter: Statementstmt 78.1% 265 / 339 Branchbran 66.4% 113 / 170 Conditioncond 59.4% 63 / 106 MC/DCmcdc 33.3% 19 / 57 Subroutinesub 66.6% 18 / 27 Podpod 20.0% 1 / 5 totaltotal 68.0% 479 / 704 CC 145 SCAR 47.5
CC
145
Cov
72%
process
282.2
compress
120.0
register_filter
21.4
CRAP 115.7
↑ Summary bin/mcp-run-compress »
1 package MCP::Run::Compress;
2 24 our $VERSION = '0.106';
3 24 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Output compression for LLMs
6  
7  
8 24 ▶use Text::Trim qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 26 ▶has filters => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14  
15 43 ▶sub _parse_command {
Subroutine _parse_command: called CC=5 SCAR=17.1
16 43 my ($self, $command) = @_;
17 43 my @words = split /\s+/, $command;
18 43 ▶ return { program => '', subcommand => undef, flags => {}, args => [] } unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 1 42 43
19  
20 42 my $program = shift @words;
21 42 my ($subcommand, @remaining);
22  
23 # Find the subcommand (first word not starting with -)
24 42 for my $i (0 .. $#words) {
25 37 ▶ if ($words[$i] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($words[$i] =~ /^-/u) 26 11 37
26 26 $subcommand = $words[$i];
27 26 @remaining = @words[$i+1 .. $#words];
28 26 last;
29 }
30 }
31 42 ▶ @remaining = @words unless defined $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subcommand 16 26 42
32  
33 # Parse flags with Getopt::Long
34 42 my %flags;
35  
36 # Build a Getopt::Long spec for common flag types
37 my @spec = (
38 2 ▶ 'stat' => sub { $flags{stat} = 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 0 ▶ 'numstat' => sub { $flags{numstat} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 ▶ 'shortstat' => sub { $flags{shortstat} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 'w=i' => \$flags{w},
42 'width=i' => \$flags{width},
43 'stat-width=i' => \$flags{'stat-width'},
44 'stat-name-width=i' => \$flags{'stat-name-width'},
45 'M=s' => \$flags{M},
46 0 ▶ 'ignore-space-change' => sub { $flags{'ignore-space-change'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 ▶ 'ignore-all-space' => sub { $flags{'ignore-all-space'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ 'ignore-blank-lines' => sub { $flags{'ignore-blank-lines'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 'U=i' => \$flags{U},
50 'unified=i' => \$flags{unified},
51 0 ▶ 'color' => sub { $flags{color} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 ▶ 'no-color' => sub { $flags{color} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 ▶ 'cached' => sub { $flags{cached} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 ▶ 'no-pager' => sub { $flags{'no-pager'} = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 42 );
56  
57 # Use GetOptionsFromArray to parse - suppress warnings for unknown flags
58 42 my $warn_handler = $SIG{__WARN__};
59 42 ▶ local $SIG{__WARN__} = sub { }; # Suppress warnings during parsing
Subroutine __ANON__: called CC=1 SCAR=0.0
60 42 GetOptionsFromArray(\@remaining, @spec);
61  
62 return {
63 42 program => $program,
64 subcommand => $subcommand,
65 flags => \%flags,
66 args => \@remaining,
67 };
68 }
69  
70 1248 ▶sub register_filter {
Subroutine register_filter: called CC=21 SCAR=30.6
Pod undocumented
71 1248 my $self = shift;
72 1248 my %args = @_;
73  
74 1248 my $command = delete $args{command};
75 1248 my $parsed_command = delete $args{parsed_command};
76  
77 # Determine storage key: parsed commands use a special key format
78 1248 my $key;
79 1248 ▶ if ($parsed_command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_command) { } 962 286 1248
80 962 my @flag_parts;
81 962 ▶ for my $flag (sort keys %{$parsed_command->{flags} // {}}) {
Condition100%2/2
$$parsed_command{"flags"} // {}
l!l
26936
MC/DC100%1/1
$$parsed_command{"flags"} // {}
$$parsed_command{"flags"}
Truth table100%2/2
A
A= $$parsed_command{"flags"}
Aresult
11
00
82 26 ▶ push @flag_parts, "$flag=" . ($parsed_command->{flags}{$flag} // 1);
Condition50%1/2
$$parsed_command{"flags"}{$flag} // 1
l!l
260
MC/DC0%0/1
$$parsed_command{"flags"}{$flag} // 1
$$parsed_command{"flags"}{$flag}
Truth table50%1/2
A
A= $$parsed_command{"flags"}{$flag}
Aresult
11
00
83 }
84 962 ▶ $key = 'parsed:' . ($parsed_command->{program} // '') . ':' . ($parsed_command->{subcommand} // '') . ':' . join(',', @flag_parts);
Condition75%3/4
$$parsed_command{"program"} // ""
l!l
9620
$$parsed_command{"subcommand"} // ""
l!l
676286
MC/DC0%0/1
$$parsed_command{"program"} // ""
$$parsed_command{"program"}
MC/DC100%1/1
$$parsed_command{"subcommand"} // ""
$$parsed_command{"subcommand"}
Truth table50%1/2
A
A= $$parsed_command{"program"}
Aresult
11
00
Truth table100%2/2
A
A= $$parsed_command{"subcommand"}
Aresult
11
00
85 } else {
86 286 $key = $command;
87 }
88  
89 $self->filters->{$key} = {
90 command => $command,
91 parsed_command => $parsed_command,
92 strip_ansi => $args{strip_ansi} // 0,
93 strip_lines_matching => $args{strip_lines_matching} // [],
94 keep_lines_matching => $args{keep_lines_matching} // [],
95 truncate_lines_at => $args{truncate_lines_at} // 0,
96 max_lines => $args{max_lines} // 0,
97 tail_lines => $args{tail_lines} // 0,
98 head_lines => $args{head_lines} // 0,
99 on_empty => $args{on_empty} // '',
100 replace => $args{replace} // [],
101 match_output => $args{match_output} // [],
102 filter_stderr => $args{filter_stderr} // 0,
103 output_detect => $args{output_detect} // undef,
104 transform => $args{transform} // undef,
105 command_transform => $args{command_transform} // undef,
106 1248 ▶ };
Condition89%25/28
$args{"strip_ansi"} // 0
l!l
01248
$args{"strip_lines_matching"} // []
l!l
119652
$args{"keep_lines_matching"} // []
l!l
01248
$args{"truncate_lines_at"} // 0
l!l
520728
$args{"max_lines"} // 0
l!l
122226
$args{"tail_lines"} // 0
l!l
261222
$args{"head_lines"} // 0
l!l
261222
$args{"on_empty"} // ""
l!l
261222
$args{"replace"} // []
l!l
01248
$args{"match_output"} // []
l!l
521196
$args{"filter_stderr"} // 0
l!l
261222
$args{"output_detect"} // undef
l!l
261222
$args{"transform"} // undef
l!l
521196
$args{"command_transform"} // undef
l!l
261222
MC/DC0%0/1
$args{"strip_ansi"} // 0
$args{"strip_ansi"}
MC/DC100%1/1
$args{"strip_lines_matching"} // []
$args{"strip_lines_matching"}
MC/DC0%0/1
$args{"keep_lines_matching"} // []
$args{"keep_lines_matching"}
MC/DC100%1/1
$args{"truncate_lines_at"} // 0
$args{"truncate_lines_at"}
MC/DC100%1/1
$args{"max_lines"} // 0
$args{"max_lines"}
MC/DC100%1/1
$args{"tail_lines"} // 0
$args{"tail_lines"}
MC/DC100%1/1
$args{"head_lines"} // 0
$args{"head_lines"}
MC/DC100%1/1
$args{"on_empty"} // ""
$args{"on_empty"}
MC/DC0%0/1
$args{"replace"} // []
$args{"replace"}
MC/DC100%1/1
$args{"match_output"} // []
$args{"match_output"}
MC/DC100%1/1
$args{"filter_stderr"} // 0
$args{"filter_stderr"}
MC/DC100%1/1
$args{"output_detect"} // undef
$args{"output_detect"}
MC/DC100%1/1
$args{"transform"} // undef
$args{"transform"}
MC/DC100%1/1
$args{"command_transform"} // undef
$args{"command_transform"}
Truth table50%1/2
A
A= $args{"strip_ansi"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"strip_lines_matching"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"keep_lines_matching"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"truncate_lines_at"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"tail_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"head_lines"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"on_empty"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"replace"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"match_output"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"filter_stderr"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"output_detect"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"transform"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"command_transform"}
Aresult
11
00
107  
108 1248 return;
109 }
110  
111 1213 ▶sub _match_parsed_command {
Subroutine _match_parsed_command: called CC=13 SCAR=26.9
112 1213 my ($self, $filter_spec, $parsed) = @_;
113 1213 ▶ return 0 unless $parsed && $filter_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $filter_spec 0 1213 1213
Condition33%1/3
$parsed and $filter_spec
!ll&&!rl&&r
001213
MC/DC0%0/2
$parsed and $filter_spec
$parsed $filter_spec
Truth table33%1/3
A and B
A= $parsedB= $filter_spec
ABresult
0X0
100
111
114  
115 # Must match program
116 1213 ▶ return 0 if ($filter_spec->{program} // '') ne ($parsed->{program} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($$filter_spec{"program"} // "") ne ($$parsed{"program"} // "") 1153 60 1213
Condition50%2/4
$$filter_spec{"program"} // ""
l!l
12130
$$parsed{"program"} // ""
l!l
12130
MC/DC0%0/1
$$filter_spec{"program"} // ""
$$filter_spec{"program"}
MC/DC0%0/1
$$parsed{"program"} // ""
$$parsed{"program"}
Truth table50%1/2
A
A= $$filter_spec{"program"}
Aresult
11
00
Truth table50%1/2
A
A= $$parsed{"program"}
Aresult
11
00
117  
118 # Subcommand must match if specified
119 60 ▶ if (defined $filter_spec->{subcommand}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$filter_spec{"subcommand"}) 60 0 60
120 60 ▶ return 0 if ($filter_spec->{subcommand} // '') ne ($parsed->{subcommand} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($$filter_spec{"subcommand"} // "") ne ($$parsed{"subcommand"} // "") 56 4 60
Condition75%3/4
$$filter_spec{"subcommand"} // ""
l!l
600
$$parsed{"subcommand"} // ""
l!l
5010
MC/DC0%0/1
$$filter_spec{"subcommand"} // ""
$$filter_spec{"subcommand"}
MC/DC100%1/1
$$parsed{"subcommand"} // ""
$$parsed{"subcommand"}
Truth table50%1/2
A
A= $$filter_spec{"subcommand"}
Aresult
11
00
Truth table100%2/2
A
A= $$parsed{"subcommand"}
Aresult
11
00
121 }
122  
123 # All specified flags must be present and truthy
124 4 ▶ for my $flag (keys %{$filter_spec->{flags} // {}}) {
Condition100%2/2
$$filter_spec{"flags"} // {}
l!l
31
MC/DC100%1/1
$$filter_spec{"flags"} // {}
$$filter_spec{"flags"}
Truth table100%2/2
A
A= $$filter_spec{"flags"}
Aresult
11
00
125 3 ▶ return 0 unless $parsed->{flags}{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless $$parsed{"flags"}{$flag} 1 2 3
126 }
127  
128 3 return 1;
129 }
130  
131 26 ▶sub _build_default_filters {
Subroutine _build_default_filters: called CC=1 SCAR=0.0
132 26 my $self = shift;
133  
134 # ls: ultra-compact for long listing - only keep type + filename
135 $self->register_filter(
136 command => '^ls\b',
137 output_detect => qr(^[d-][rwx-]{9}.*\s+\d+\s+),
138 strip_lines_matching => [
139 qr(^\s*$),
140 qr(^total\s+\d+),
141 qr(^\s*Device:),
142 qr(^\s*Inode:),
143 qr(^\s*Birth:),
144 qr(^/node_modules/),
145 qr(^/\.git/),
146 qr(^/\.target/),
147 qr(^/\.next/),
148 qr(^/\.nuxt/),
149 qr(^/\.cache/),
150 qr(^/__pycache__/),
151 qr(^/\.DS_Store/),
152 qr(^/vendor/bundle/),
153 ],
154 20 ▶ transform => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
155 20 my ($line) = @_;
156 20 ▶ if ($line =~ m{^([d-])[rwx-]{9}\s+\d+\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+[\d:]+\s+(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([d-])[rwx-]{9}\s+\d+\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+[\d:]+\s+(.+)$/u) 13 7 20
157 13 return $1 . " " . $2;
158 }
159 7 return $line;
160 },
161 26 truncate_lines_at => 100,
162 max_lines => 50,
163 );
164  
165 # stat: strip device/inode/birth (Linux)
166 26 $self->register_filter(
167 command => '^stat\b',
168 strip_lines_matching => [
169 qr(^\s*$),
170 qr(^\s*Device:),
171 qr(^\s*Inode:),
172 qr(^\s*Birth:),
173 ],
174 truncate_lines_at => 120,
175 max_lines => 20,
176 );
177  
178 # grep: truncate lines, group by file
179 26 $self->register_filter(
180 command => '^grep\b',
181 truncate_lines_at => 150,
182 max_lines => 100,
183 );
184  
185 # make: strip entering/leaving directory
186 26 $self->register_filter(
187 command => '^make\b',
188 strip_lines_matching => [
189 qr(^\s*$),
190 qr(^make\[\d+\]:),
191 qr(^Entering directory),
192 qr(^Leaving directory),
193 qr(Nothing to be done),
194 qr(^make:\s+Nothing to be done),
195 ],
196 match_output => [
197 { pattern => qr(^make\[\d+\]:.*?make\[\d+\]:), message => 'make: circular dependency detected' },
198 { pattern => qr(^gcc.*?Error), message => 'make: compilation error' },
199 ],
200 max_lines => 50,
201 on_empty => 'make: ok',
202 );
203  
204 # git status: compact output
205 26 $self->register_filter(
206 parsed_command => {
207 program => 'git',
208 subcommand => 'status',
209 },
210 strip_lines_matching => [
211 qr(^\s*$),
212 qr(^On branch),
213 qr(^Your branch is),
214 qr(^Initial commit),
215 ],
216 max_lines => 30,
217 );
218  
219 # git diff: keep only diff content
220 26 $self->register_filter(
221 command => '^git\s+diff\b',
222 strip_lines_matching => [
223 qr(^\s*$),
224 qr(^diff --git),
225 qr(^index ),
226 qr(^---\s+a/),
227 qr(^\+\+\+\s+b/),
228 ],
229 truncate_lines_at => 150,
230 max_lines => 200,
231 );
232  
233 # git diff --stat: compact format "N+M- filename"
234 $self->register_filter(
235 parsed_command => {
236 program => 'git',
237 subcommand => 'diff',
238 flags => { stat => 1 },
239 },
240 4 ▶ transform => sub {
Subroutine __ANON__: called CC=5 SCAR=19.1
241 4 my ($line) = @_;
242 # Format: " 1 file changed, 3 insertions(+), 2 deletions(-)" or
243 # " file1.txt | 5 +++ ---"
244 # Transform to "N+M- filename" for file lines
245 4 ▶ if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s*\+(\d+)\s*-\s*(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s*\+(\d+)\s*-\s*(\d+)/u) 0 4 4
246 # "| 5 +++ ---" format -> "3+2- filename"
247 0 return "$2+$3-$1";
248 }
249 4 ▶ if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s+\+(\d+),?\s*(\d+)?\s*-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+)\s*\|\s*(\d+)\s+\+(\d+),?\s*(\d+)?\s*-/u) 0 4 4
250 # "| 5 +3 -2" format
251 0 ▶ my ($file, $changes, $add, $del) = ($1, $2, $3, $4 // 0);
Condition0%0/2
$4 // 0
l!l
00
MC/DC0%0/1
$4 // 0
$4
Truth table0%0/2
A
A= $4
Aresult
11
00
252 0 return "$add+$del-$file";
253 }
254 # Remove summary line "X files changed"
255 4 ▶ if ($line =~ /^\s*\d+\s+files?\s+changed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\d+\s+files?\s+changed/u) 2 2 4
256 2 return undef; # Skip this line
257 }
258 2 return $line;
259 },
260 26 strip_lines_matching => [
261 qr(^\s*$),
262 qr(^\s*\d+\s+files?\s+changed),
263 qr(^\s*\d+\s+insertions?\(\+\)),
264 qr(^\s*\d+\s+deletions?\(\-\)),
265 ],
266 max_lines => 100,
267 );
268  
269 # cat: detect and filter code
270 26 $self->register_filter(
271 command => '^cat\b',
272 strip_lines_matching => [qr(^\s*$)],
273 truncate_lines_at => 500,
274 max_lines => 100,
275 );
276  
277 # find: strip permission denied, limit results
278 26 $self->register_filter(
279 command => '^find\b',
280 strip_lines_matching => [
281 qr(^\s*$),
282 qr(^find:.*permission denied),
283 ],
284 max_lines => 50,
285 );
286  
287 # ps: compact output
288 26 $self->register_filter(
289 command => '^ps\b',
290 strip_lines_matching => [
291 qr(^\s*$),
292 ],
293 truncate_lines_at => 120,
294 max_lines => 30,
295 );
296  
297 # df: truncate wide columns
298 26 $self->register_filter(
299 command => '^df\b',
300 strip_lines_matching => [qr(^\s*$)],
301 truncate_lines_at => 80,
302 max_lines => 20,
303 );
304  
305 # docker ps: compact output
306 26 $self->register_filter(
307 parsed_command => {
308 program => 'docker',
309 subcommand => 'ps',
310 },
311 strip_lines_matching => [qr(^\s*$)],
312 truncate_lines_at => 120,
313 max_lines => 30,
314 );
315  
316 # docker images: compact output
317 26 $self->register_filter(
318 parsed_command => {
319 program => 'docker',
320 subcommand => 'images',
321 },
322 strip_lines_matching => [qr(^\s*$)],
323 truncate_lines_at => 120,
324 max_lines => 30,
325 );
326  
327 # terraform plan: strip refresh progress
328 26 $self->register_filter(
329 parsed_command => {
330 program => 'terraform',
331 subcommand => 'plan',
332 },
333 strip_lines_matching => [
334 qr(^\s*$),
335 qr(^Refreshing state\.\.\.),
336 qr(^Terraform used the),
337 qr(^tfe-outputs:),
338 ],
339 max_lines => 100,
340 );
341  
342 # terraform apply: strip progress
343 26 $self->register_filter(
344 parsed_command => {
345 program => 'terraform',
346 subcommand => 'apply',
347 },
348 strip_lines_matching => [
349 qr(^\s*$),
350 qr(^Refreshing state\.\.\.),
351 qr(^Terraform will perform),
352 qr(^Proceeding with the following),
353 qr(^tfe-outputs:),
354 ],
355 max_lines => 100,
356 );
357  
358 # docker build: strip build progress
359 26 $self->register_filter(
360 parsed_command => {
361 program => 'docker',
362 subcommand => 'build',
363 },
364 strip_lines_matching => [
365 qr(^\s*$),
366 qr(^#\s*\d+\s+\[\s*\d+\s+/\s*\d+\]),
367 qr(^Step \d+/\d+:),
368 ],
369 max_lines => 50,
370 );
371  
372 # docker run: compact output
373 26 $self->register_filter(
374 parsed_command => {
375 program => 'docker',
376 subcommand => 'run',
377 },
378 strip_lines_matching => [
379 qr(^\s*$),
380 qr(^Unable to find image),
381 qr(^Pulling from library/),
382 qr(^Digest:),
383 qr(^Status:),
384 ],
385 max_lines => 30,
386 );
387  
388 # kubectl get: compact output
389 26 $self->register_filter(
390 parsed_command => {
391 program => 'kubectl',
392 subcommand => 'get',
393 },
394 strip_lines_matching => [qr(^\s*$)],
395 truncate_lines_at => 150,
396 max_lines => 50,
397 );
398  
399 # kubectl describe: strip noise
400 26 $self->register_filter(
401 parsed_command => {
402 program => 'kubectl',
403 subcommand => 'describe',
404 },
405 strip_lines_matching => [
406 qr(^\s*$),
407 qr(^Name:\s+\w+),
408 qr(^Namespace:\s+\w+),
409 qr(^Labels:\s*$),
410 qr(^Annotations:\s*$/),
411 ],
412 truncate_lines_at => 200,
413 max_lines => 100,
414 );
415  
416 # cargo build: strip compile progress
417 26 $self->register_filter(
418 parsed_command => {
419 program => 'cargo',
420 subcommand => 'build',
421 },
422 strip_lines_matching => [
423 qr(^\s*$),
424 qr(^Compiling\s+\w+),
425 qr(^Fresh\s+\w+),
426 qr(^Finished\s+),
427 ],
428 max_lines => 50,
429 );
430  
431 # cargo test: compact output
432 26 $self->register_filter(
433 parsed_command => {
434 program => 'cargo',
435 subcommand => 'test',
436 },
437 strip_lines_matching => [
438 qr(^\s*$),
439 qr(^Compiling\s+\w+),
440 qr(^Running\s+/),
441 qr(^test result:),
442 ],
443 max_lines => 100,
444 );
445  
446 # cpanm: strip cpanm noise
447 26 $self->register_filter(
448 command => '^cpanm\b',
449 strip_lines_matching => [
450 qr(^\s*$),
451 qr(^--> ),
452 qr(^OK$),
453 qr(^FAIL$),
454 qr(^Working on),
455 qr(^Fetching),
456 qr(^Configuring),
457 qr(^Building and testing),
458 ],
459 match_output => [
460 { pattern => qr{^\s*Successfully installed}, message => 'cpanm: ok' },
461 ],
462 max_lines => 30,
463 );
464  
465 # npm install: strip noise
466 26 $self->register_filter(
467 parsed_command => {
468 program => 'npm',
469 subcommand => 'install',
470 },
471 strip_lines_matching => [
472 qr(^\s*$),
473 qr(^added\s+\d+\s+packages?),
474 qr(^found\s+\d+\s+packages?),
475 qr(^npm warn),
476 ],
477 max_lines => 30,
478 );
479  
480 # yarn install: similar to npm
481 26 $self->register_filter(
482 parsed_command => {
483 program => 'yarn',
484 subcommand => 'install',
485 },
486 strip_lines_matching => [
487 qr(^\s*$),
488 qr(^Done in\s+),
489 qr(^Resolving completed),
490 qr(^Linking completed),
491 ],
492 max_lines => 30,
493 );
494  
495 # yarn add: similar to npm
496 26 $self->register_filter(
497 parsed_command => {
498 program => 'yarn',
499 subcommand => 'add',
500 },
501 strip_lines_matching => [
502 qr(^\s*$),
503 qr(^Done in\s+),
504 qr(^Resolving completed),
505 qr(^Linking completed),
506 ],
507 max_lines => 30,
508 );
509  
510 # pnpm install: similar to npm
511 26 $self->register_filter(
512 parsed_command => {
513 program => 'pnpm',
514 subcommand => 'install',
515 },
516 strip_lines_matching => [
517 qr(^\s*$),
518 qr(^Done in\s+),
519 qr(^Resolving completed),
520 qr(^Linking completed),
521 ],
522 max_lines => 30,
523 );
524  
525 # pnpm add: similar to npm
526 26 $self->register_filter(
527 parsed_command => {
528 program => 'pnpm',
529 subcommand => 'add',
530 },
531 strip_lines_matching => [
532 qr(^\s*$),
533 qr(^Done in\s+),
534 qr(^Resolving completed),
535 qr(^Linking completed),
536 ],
537 max_lines => 30,
538 );
539  
540 # pip install: strip progress
541 26 $self->register_filter(
542 parsed_command => {
543 program => 'pip',
544 subcommand => 'install',
545 },
546 strip_lines_matching => [
547 qr(^\s*$),
548 qr(^Collecting\s+),
549 qr(^Downloading\s+),
550 qr(^Installing collected packages:),
551 qr(^Successfully installed),
552 ],
553 max_lines => 50,
554 );
555  
556 # pytest: compact output
557 26 $self->register_filter(
558 parsed_command => {
559 program => 'pytest',
560 },
561 strip_lines_matching => [
562 qr(^\s*$),
563 qr(^=+.*=+$/),
564 qr(^Coverage report:),
565 qr(^HTML report:),
566 ],
567 truncate_lines_at => 150,
568 max_lines => 100,
569 );
570  
571 # curl: strip headers
572 26 $self->register_filter(
573 parsed_command => {
574 program => 'curl',
575 },
576 filter_stderr => 1,
577 strip_lines_matching => [
578 qr(^\s*$),
579 qr(^ % Total),
580 qr(^ Resolving),
581 qr(^Connected to),
582 qr(^HTTP/\d[\d.]*\s+\d+),
583 ],
584 truncate_lines_at => 200,
585 max_lines => 50,
586 );
587  
588 # wget: strip progress
589 26 $self->register_filter(
590 parsed_command => {
591 program => 'wget',
592 },
593 strip_lines_matching => [
594 qr(^\s*$),
595 qr(^--\d{4}-\d{2}-\d{2}),
596 qr(^Resolving),
597 qr(^Connecting to),
598 qr(^Length:\s+\d+),
599 qr(^Saving to:),
600 qr(^\d+%\s+\[),
601 ],
602 truncate_lines_at => 200,
603 max_lines => 50,
604 );
605  
606 # helm install: strip progress
607 26 $self->register_filter(
608 parsed_command => {
609 program => 'helm',
610 subcommand => 'install',
611 },
612 strip_lines_matching => [
613 qr(^\s*$),
614 qr(^NAME:\s+\w+),
615 qr(^NAMESPACE:\s+\w+),
616 qr(^STATUS:\s+),
617 qr(^REVISION:\s+\d+),
618 qr(^NOTES:$),
619 ],
620 max_lines => 50,
621 );
622  
623 # helm upgrade: strip progress
624 26 $self->register_filter(
625 parsed_command => {
626 program => 'helm',
627 subcommand => 'upgrade',
628 },
629 strip_lines_matching => [
630 qr(^\s*$),
631 qr(^NAME:\s+\w+),
632 qr(^NAMESPACE:\s+\w+),
633 qr(^STATUS:\s+),
634 qr(^REVISION:\s+\d+),
635 qr(^NOTES:$),
636 ],
637 max_lines => 50,
638 );
639  
640 # ansible-playbook: strip progress
641 26 $self->register_filter(
642 parsed_command => {
643 program => 'ansible-playbook',
644 },
645 strip_lines_matching => [
646 qr(^\s*$),
647 qr(^PLAY\s+\[),
648 qr(^TASK\s+\[),
649 qr(^RUNNING HANDLER),
650 qr(^changed:\s+\[\d+\]),
651 qr(^ok:\s+\[\d+\]),
652 qr(^fatal:\s+\[\d+\]),
653 qr(^PLAY RECAP),
654 ],
655 max_lines => 100,
656 );
657  
658 # rsync: strip progress
659 26 $self->register_filter(
660 parsed_command => {
661 program => 'rsync',
662 },
663 strip_lines_matching => [
664 qr(^\s*$),
665 qr(^sent\s+\d+\s+bytes),
666 qr(^received\s+\d+\s+bytes),
667 qr(^total size is),
668 ],
669 max_lines => 30,
670 );
671  
672 # iptables -L: compact
673 26 $self->register_filter(
674 parsed_command => {
675 program => 'iptables',
676 },
677 strip_lines_matching => [qr(^\s*$)],
678 truncate_lines_at => 150,
679 max_lines => 50,
680 );
681  
682 # ping: strip progress
683 26 $self->register_filter(
684 parsed_command => {
685 program => 'ping',
686 },
687 strip_lines_matching => [
688 qr(^\s*$),
689 qr(^PING\s+),
690 qr(^64 bytes from),
691 qr(^---.*ping statistics---),
692 ],
693 max_lines => 20,
694 );
695  
696 # netstat: compact output for -tulpn
697 26 $self->register_filter(
698 parsed_command => {
699 program => 'netstat',
700 },
701 strip_lines_matching => [qr(^\s*$)],
702 truncate_lines_at => 150,
703 max_lines => 50,
704 );
705  
706 # ip addr: compact
707 26 $self->register_filter(
708 parsed_command => {
709 program => 'ip',
710 subcommand => 'addr',
711 },
712 strip_lines_matching => [qr(^\s*$)],
713 truncate_lines_at => 150,
714 max_lines => 50,
715 );
716  
717 # ip route: compact
718 26 $self->register_filter(
719 parsed_command => {
720 program => 'ip',
721 subcommand => 'route',
722 },
723 strip_lines_matching => [qr(^\s*$)],
724 max_lines => 30,
725 );
726  
727 # ip link: compact
728 26 $self->register_filter(
729 parsed_command => {
730 program => 'ip',
731 subcommand => 'link',
732 },
733 strip_lines_matching => [qr(^\s*$)],
734 max_lines => 30,
735 );
736  
737 # mount: strip noise
738 26 $self->register_filter(
739 parsed_command => {
740 program => 'mount',
741 },
742 strip_lines_matching => [qr(^\s*$)],
743 truncate_lines_at => 200,
744 max_lines => 50,
745 );
746  
747 # lsblk: compact block device listing
748 26 $self->register_filter(
749 parsed_command => {
750 program => 'lsblk',
751 },
752 strip_lines_matching => [qr(^\s*$)],
753 truncate_lines_at => 150,
754 max_lines => 50,
755 );
756  
757 # blkid: compact block device attributes
758 26 $self->register_filter(
759 parsed_command => {
760 program => 'blkid',
761 },
762 strip_lines_matching => [qr(^\s*$)],
763 truncate_lines_at => 200,
764 max_lines => 50,
765 );
766  
767 # git log: compact
768 26 $self->register_filter(
769 parsed_command => {
770 program => 'git',
771 subcommand => 'log',
772 },
773 strip_lines_matching => [
774 qr(^\s*$),
775 qr(^commit\s+[a-f0-9]+),
776 qr(^Author:\s+),
777 qr(^Date:\s+),
778 ],
779 head_lines => 20,
780 tail_lines => 10,
781 max_lines => 30,
782 );
783  
784 # git branch: compact
785 26 $self->register_filter(
786 parsed_command => {
787 program => 'git',
788 subcommand => 'branch',
789 },
790 strip_lines_matching => [qr(^\s*$)],
791 max_lines => 30,
792 );
793  
794 # git stash: compact
795 26 $self->register_filter(
796 parsed_command => {
797 program => 'git',
798 subcommand => 'stash',
799 },
800 strip_lines_matching => [qr(^\s*$)],
801 max_lines => 30,
802 );
803  
804 # git commit: transform Co-Authored-By. Match the raw string so we
805 # also catch compound commands like `cd foo && git add bar && git
806 # commit -m "..."`, where the parsed top-level program isn't `git`.
807 $self->register_filter(
808 command => '\bgit\b[^&|;]*?(?<![=])\bcommit\b',
809 15 ▶ command_transform => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
810 15 my ($cmd) = @_;
811 15 ▶ my $model = $ENV{CO_AUTHORED_BY} || $ENV{ANTHROPIC_MODEL};
Condition33%1/3
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
l!l&&r!l&&!r
0150
MC/DC0%0/2
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
$ENV{'CO_AUTHORED_BY'} $ENV{'ANTHROPIC_MODEL'}
Truth table33%1/3
A || B
A= $ENV{'CO_AUTHORED_BY'}B= $ENV{'ANTHROPIC_MODEL'}
ABresult
1X1
011
000
812 15 ▶ return $cmd unless $model;
Branch: 1 branch
BranchTrueFalseTotal
unless $model 0 15 15
813 15 ▶ if ($cmd =~ /Co-Authored-By:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /Co-Authored-By:/iu) 4 11 15
814 # [^\n"]+ instead of [^\n]+: when the trailer sits at the end of a
815 # -m "..." message, the greedy class used to swallow the closing
816 # quote too, leaving the shell with an unterminated string.
817 4 $cmd =~ s/Co-Authored-By: [^\n"]+/Co-Authored-By: $model/g;
818 4 return $cmd;
819 }
820 # Injection. The trailer is spliced after the closing quote of the
821 # last -m/--message argument inside the git commit invocation (the
822 # invocation runs from `commit` up to the next shell separator).
823 # The old code targeted the last quote of the whole command line, so
824 # a compound command like `git commit -m "x" && git config user.name
825 # "y"` had the trailer injected into user.name, corrupting the config
826 # value. Injecting inside the quote (rather than appending `-m
827 # "..."`) keeps the trailer part of the commit message: appended, the
828 # `<model@host>` would parse as a shell redirection and break the
829 # call. No -m argument (e.g. `git commit --amend` opening the editor)
830 # means no safe injection point, so nothing is rewritten.
831 11 my $tail = $cmd;
832 11 ▶ if ( $tail =~ /\bgit\b[^&|;]*?(?<![=])\bcommit\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\bgit\b[^&|;]*?(?<![=])\bcommit\b/u) 11 0 11
833 11 $tail =~ s/^.*\bgit\b[^&|;]*?(?<![=])\bcommit\b//;
834 11 my $origin = length($cmd) - length($tail);
835 11 $tail =~ s/[&|;].*$//;
836 # -[a-zA-Z]*m covers bundled short flags whose last letter is the
837 # message flag (-am, -sm, -anm), not just a bare -m. The lookbehind
838 # requires the dash to start an argument -- \b can't do that, since
839 # `-` is not a word character, so \b- never matches after a space.
840 # Without it, a path like /tmp/msg-m "x" would look like a flag.
841 11 ▶ if ( $tail
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /^(.*(?<![^\s])(?:--message|-[a-zA-Z]*m)\s+")((?:[^"\\]|\\.)*)(")/u) 8 3 11
842 =~ /^(.*(?<![^\s])(?:--message|-[a-zA-Z]*m)\s+")((?:[^"\\]|\\.)*)(")/ )
843 {
844 8 substr( $cmd, $origin + length($1) + length($2), 0 )
845 = "\n\nCo-Authored-By: $model";
846 }
847 }
848 11 return $cmd;
849 },
850 26 );
851  
852 26 return;
853 }
854  
855 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
856 26 my $self = shift->SUPER::new(@_);
857 26 $self->_build_default_filters;
858 26 return $self;
859 }
860  
861 20 ▶sub compress {
Subroutine compress: called CC=45 SCAR=47.9
Pod undocumented
862 20 my ($self, $command, $stdout, $stderr) = @_;
863  
864 # Normalize so downstream split/join/grep can't trip on undef inputs.
865 20 ▶ $stdout //= '';
Condition100%2/2
$stdout //= ""
l!l
191
MC/DC100%1/1
$stdout //= ""
$stdout
Truth table100%2/2
A
A= $stdout
Aresult
11
00
866 20 ▶ $stderr //= '';
Condition100%2/2
$stderr //= ""
l!l
191
MC/DC100%1/1
$stderr //= ""
$stderr
Truth table100%2/2
A
A= $stderr
Aresult
11
00
867  
868 20 my $matched_filter;
869 20 my $parsed = $self->_parse_command($command);
870  
871 20 for my $key (keys %{$self->filters}) {
872 958 my $filter = $self->filters->{$key};
873  
874 958 my $matches = 0;
875  
876 # Check parsed_command first if present
877 958 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $$filter{'parsed_command'}) { } 738 220 958
elsif ($command =~ /$key/u) { } 13 207 220
878 738 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 1 737 738
879 }
880 # Fall back to regex matching for legacy filters
881 elsif ($command =~ /$key/) {
882 13 $matches = 1;
883 }
884  
885 958 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 944 14 958
886  
887 # Check output_detect if present - only apply filter if output matches
888 14 ▶ if (my $detect = $filter->{output_detect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $detect = $$filter{"output_detect"}) 5 9 14
889 5 my @lines = split(/\n/, $stdout);
890 5 my $has_match = grep { /$detect/ } @lines;
891 5 ▶ unless ($has_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_match) 1 4 5
892 1 next; # Skip this filter, try next one
893 }
894 }
895  
896 # parsed_command filters win over legacy regex filters when both
897 # match — the parsed form is more specific (program + subcommand +
898 # flags) and is what users reach for when they want transform-style
899 # rewrites like `git diff --stat -> "5+2-file"`. Without this
900 # guard, hash iteration order decides the winner and `git diff
901 # --stat` would land on the bare `^git\s+diff\b` filter, which
902 # doesn't have the transform.
903 13 ▶ if (!$matched_filter) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $matched_filter) { } 12 1 13
elsif ($$filter{'parsed_command'} and not $$matched_filter{'parsed_command'}) { } 1 0 1
904 12 $matched_filter = $filter;
905 }
906 ▶ elsif ($filter->{parsed_command} && !$matched_filter->{parsed_command}) {
Condition33%1/3
$$filter{"parsed_command"} and not $$matched_filter{"parsed_command"}
!ll&&!rl&&r
001
MC/DC0%0/2
$$filter{"parsed_command"} and not $$matched_filter{"parsed_command"}
$$filter{"parsed_command"} not $$matched_filter{"parsed_command"}
Truth table33%1/3
A and not(B)
A= $$filter{"parsed_command"}B= not $$matched_filter{"parsed_command"}
ABresult
0X0
100
111
907 1 $matched_filter = $filter;
908 }
909 13 ▶ last if $matched_filter->{parsed_command};
Branch: 1 branch
BranchTrueFalseTotal
if $$matched_filter{"parsed_command"} 1 12 13
910 }
911  
912 # Apply command_transform if any filter has it and env var allows it
913 20 ▶ if ($matched_filter && $matched_filter->{command_transform} && !$ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}) 0 20 20
Condition50%3/6
$matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
!ll&&!rl&&r
2000
$matched_filter and $$matched_filter{"command_transform"}
!ll&&!rl&&r
8120
MC/DC0%0/3
$matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
$matched_filter $$matched_filter{"command_transform"} not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
Truth table50%2/4
A and B and not(C)
A= $matched_filterB= $$matched_filter{"command_transform"}C= not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
ABCresult
0XX0
10X0
1100
1111
914 0 $command = $matched_filter->{command_transform}->($command);
915 }
916  
917 20 ▶ if (!$matched_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched_filter) 8 12 20
918 8 return ($stdout, $stderr);
919 }
920  
921 12 ▶ my ($out, $err) = ($stdout, $stderr // '');
Condition50%1/2
$stderr // ""
l!l
120
MC/DC0%0/1
$stderr // ""
$stderr
Truth table50%1/2
A
A= $stderr
Aresult
11
00
922  
923 # Filter stderr into stdout if configured
924 12 ▶ if ($matched_filter->{filter_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"filter_stderr"}) 0 12 12
925 0 ▶ $out .= "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
926 0 $err = '';
927 }
928  
929 # Stage 1: Strip ANSI
930 12 ▶ if ($matched_filter->{strip_ansi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"strip_ansi"}) 0 12 12
931 0 $out =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
932 0 $err =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
933 }
934  
935 # Stage 2: Match output short-circuit
936 12 for my $match (@{$matched_filter->{match_output}}) {
937 4 ▶ if ($out =~ /$match->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /$$match{"pattern"}/u) 0 4 4
938 0 return ($match->{message}, $err);
939 }
940 }
941  
942 # Stage 3: Transform lines (if transform coderef provided).
943 # A transform may return undef to drop a line (e.g. git diff --stat's
944 # summary line) — filter those out so join() doesn't warn.
945 12 ▶ if ($matched_filter->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"transform"}) 5 7 12
946 5 $out = join("\n", grep { defined } map { $matched_filter->{transform}->($_) } split(/\n/, $out));
947 }
948  
949 # Stage 4: Strip lines matching
950 12 ▶ if (@{$matched_filter->{strip_lines_matching}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"strip_lines_matching"};}) 11 1 12
951 11 my @out_lines = split(/\n/, $out);
952 @out_lines = grep {
953 11 my $keep = 1;
954 112 for my $pattern (@{$matched_filter->{strip_lines_matching}}) {
955 451 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 17 434 451
956 17 $keep = 0;
957 17 last;
958 }
959 }
960 112 $keep;
961 } @out_lines;
962 11 $out = join("\n", @out_lines);
963 }
964  
965 # Stage 5: Keep lines matching (if any)
966 12 ▶ if (@{$matched_filter->{keep_lines_matching}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"keep_lines_matching"};}) 0 12 12
967 0 my @out_lines = split(/\n/, $out);
968 @out_lines = grep {
969 0 my $keep = 0;
970 0 for my $pattern (@{$matched_filter->{keep_lines_matching}}) {
971 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
972 0 $keep = 1;
973 0 last;
974 }
975 }
976 0 $keep;
977 } @out_lines;
978 0 $out = join("\n", @out_lines);
979 }
980  
981 # Stage 6: Truncate lines at N chars
982 12 ▶ if ($matched_filter->{truncate_lines_at} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"truncate_lines_at"} > 0) 8 4 12
983 8 my $max = $matched_filter->{truncate_lines_at};
984 8 ▶ $out = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $out));
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max ? : 1 36 37
985 8 ▶ $err = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $err)) if length $err;
Branch: 2 branches
BranchTrueFalseTotal
if length $err 0 8 8
length $_ > $max ? : 0 0 0
986 }
987  
988 # Stage 7: Head/Tail lines
989 12 ▶ if ($matched_filter->{head_lines} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$matched_filter{'head_lines'} > 0) { } 0 12 12
elsif ($$matched_filter{'tail_lines'} > 0) { } 0 12 12
990 0 my @lines = split(/\n/, $out);
991 0 my $head = $matched_filter->{head_lines};
992 0 ▶ my $tail = $matched_filter->{tail_lines} // 0;
Condition0%0/2
$$matched_filter{"tail_lines"} // 0
l!l
00
MC/DC0%0/1
$$matched_filter{"tail_lines"} // 0
$$matched_filter{"tail_lines"}
Truth table0%0/2
A
A= $$matched_filter{"tail_lines"}
Aresult
11
00
993 0 my $omit = @lines - $head - $tail;
994 0 ▶ if ($omit > 0 && $tail > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($omit > 0 and $tail > 0) { } 0 0 0
elsif ($omit > 0) { } 0 0 0
Condition0%0/3
$omit > 0 and $tail > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$omit > 0 and $tail > 0
$omit > 0 $tail > 0
Truth table0%0/3
A and B
A= $omit > 0B= $tail > 0
ABresult
0X0
100
111
995 0 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...\n" . (join("\n", @lines[-$tail..-1]));
996 }
997 elsif ($omit > 0) {
998 0 $out = join("\n", @lines[0..min($head, @lines)-1]);
999 }
1000 }
1001 elsif ($matched_filter->{tail_lines} > 0) {
1002 0 my @lines = split(/\n/, $out);
1003 0 $out = join("\n", @lines[-min($matched_filter->{tail_lines}, @lines)..-1]);
1004 }
1005  
1006 # Stage 8: Max lines
1007 12 ▶ if ($matched_filter->{max_lines} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"max_lines"} > 0) 12 0 12
1008 12 my @out_lines = split(/\n/, $out);
1009 12 ▶ if (@out_lines > $matched_filter->{max_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out_lines > $$matched_filter{"max_lines"}) 1 11 12
1010 1 $out = join("\n", @out_lines[0..$matched_filter->{max_lines}-1]) . "\n... " . (@out_lines - $matched_filter->{max_lines}) . " more lines ...";
1011 }
1012 }
1013  
1014 # Stage 9: On empty
1015 12 ▶ if (!length trim($out) && $matched_filter->{on_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length trim($out) and $$matched_filter{"on_empty"}) 1 11 12
Condition66%2/3
not length trim($out) and $$matched_filter{"on_empty"}
!ll&&!rl&&r
1101
MC/DC50%1/2
not length trim($out) and $$matched_filter{"on_empty"}
not length trim($out) $$matched_filter{"on_empty"}
Truth table66%2/3
not(A) and B
A= not length trim($out)B= $$matched_filter{"on_empty"}
ABresult
0X0
100
111
1016 1 $out = $matched_filter->{on_empty};
1017 }
1018  
1019 12 return ($out, $err);
1020 }
1021  
1022 2 ▶sub process {
Subroutine process: called CC=48 SCAR=56.4
Pod undocumented
1023 2 my ($self, $command, $stdout, $stderr) = @_;
1024  
1025 # Normalize so downstream split/join/grep can't trip on undef inputs.
1026 2 ▶ $stdout //= '';
Condition100%2/2
$stdout //= ""
l!l
11
MC/DC100%1/1
$stdout //= ""
$stdout
Truth table100%2/2
A
A= $stdout
Aresult
11
00
1027 2 ▶ $stderr //= '';
Condition50%1/2
$stderr //= ""
l!l
20
MC/DC0%0/1
$stderr //= ""
$stderr
Truth table50%1/2
A
A= $stderr
Aresult
11
00
1028  
1029 2 my $matched_filter;
1030 2 my $parsed = $self->_parse_command($command);
1031  
1032 2 for my $key (keys %{$self->filters}) {
1033 78 my $filter = $self->filters->{$key};
1034  
1035 78 my $matches = 0;
1036  
1037 78 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $$filter{'parsed_command'}) { } 58 20 78
elsif ($command =~ /$key/u) { } 2 18 20
1038 58 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 1 57 58
1039 }
1040 elsif ($command =~ /$key/) {
1041 2 $matches = 1;
1042 }
1043  
1044 78 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 75 3 78
1045  
1046 3 ▶ if (my $detect = $filter->{output_detect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $detect = $$filter{"output_detect"}) 1 2 3
1047 1 my @lines = split(/\n/, $stdout);
1048 1 my $has_match = grep { /$detect/ } @lines;
1049 1 ▶ unless ($has_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_match) 1 0 1
1050 1 next;
1051 }
1052 }
1053  
1054 # parsed_command wins over legacy regex when both match — see the
1055 # matching comment in compress().
1056 2 ▶ if (!$matched_filter) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $matched_filter) { } 1 1 2
elsif ($$filter{'parsed_command'} and not $$matched_filter{'parsed_command'}) { } 1 0 1
1057 1 $matched_filter = $filter;
1058 }
1059 ▶ elsif ($filter->{parsed_command} && !$matched_filter->{parsed_command}) {
Condition33%1/3
$$filter{"parsed_command"} and not $$matched_filter{"parsed_command"}
!ll&&!rl&&r
001
MC/DC0%0/2
$$filter{"parsed_command"} and not $$matched_filter{"parsed_command"}
$$filter{"parsed_command"} not $$matched_filter{"parsed_command"}
Truth table33%1/3
A and not(B)
A= $$filter{"parsed_command"}B= not $$matched_filter{"parsed_command"}
ABresult
0X0
100
111
1060 1 $matched_filter = $filter;
1061 }
1062 2 ▶ last if $matched_filter->{parsed_command};
Branch: 1 branch
BranchTrueFalseTotal
if $$matched_filter{"parsed_command"} 1 1 2
1063 }
1064  
1065 2 ▶ my ($out, $err) = ($stdout, $stderr // '');
Condition50%1/2
$stderr // ""
l!l
20
MC/DC0%0/1
$stderr // ""
$stderr
Truth table50%1/2
A
A= $stderr
Aresult
11
00
1066  
1067 2 ▶ if ($matched_filter && $matched_filter->{command_transform} && !$ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}) 0 2 2
Condition50%3/6
$matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
!ll&&!rl&&r
200
$matched_filter and $$matched_filter{"command_transform"}
!ll&&!rl&&r
110
MC/DC0%0/3
$matched_filter and $$matched_filter{"command_transform"} and not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
$matched_filter $$matched_filter{"command_transform"} not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
Truth table50%2/4
A and B and not(C)
A= $matched_filterB= $$matched_filter{"command_transform"}C= not $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"}
ABCresult
0XX0
10X0
1100
1111
1068 0 $command = $matched_filter->{command_transform}->($command);
1069 }
1070  
1071 2 ▶ if (!$matched_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched_filter) 1 1 2
1072 1 return { command => $command, stdout => $out, stderr => $err };
1073 }
1074  
1075 1 ▶ if ($matched_filter->{filter_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"filter_stderr"}) 0 1 1
1076 0 ▶ $out .= "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
1077 0 $err = '';
1078 }
1079  
1080 1 ▶ if ($matched_filter->{strip_ansi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"strip_ansi"}) 0 1 1
1081 0 $out =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1082 0 $err =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1083 }
1084  
1085 1 ▶ for my $match (@{$matched_filter->{match_output} // []}) {
Condition50%1/2
$$matched_filter{"match_output"} // []
l!l
10
MC/DC0%0/1
$$matched_filter{"match_output"} // []
$$matched_filter{"match_output"}
Truth table50%1/2
A
A= $$matched_filter{"match_output"}
Aresult
11
00
1086 0 ▶ if ($out =~ /$match->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /$$match{"pattern"}/u) 0 0 0
1087 0 return { command => $command, stdout => $match->{message}, stderr => $err };
1088 }
1089 }
1090  
1091 1 ▶ if ($matched_filter->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"transform"}) 1 0 1
1092 # A transform may return undef to drop a line — filter those out so
1093 # join() doesn't warn on undefined values.
1094 1 $out = join("\n", grep { defined } map { $matched_filter->{transform}->($_) } split(/\n/, $out));
1095 }
1096  
1097 1 ▶ if (@{$matched_filter->{strip_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"strip_lines_matching"} // [];}) 1 0 1
Condition50%1/2
$$matched_filter{"strip_lines_matching"} // []
l!l
10
MC/DC0%0/1
$$matched_filter{"strip_lines_matching"} // []
$$matched_filter{"strip_lines_matching"}
Truth table50%1/2
A
A= $$matched_filter{"strip_lines_matching"}
Aresult
11
00
1098 1 my @out_lines = split(/\n/, $out);
1099 @out_lines = grep {
1100 1 my $keep = 1;
1101 1 for my $pattern (@{$matched_filter->{strip_lines_matching}}) {
1102 4 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 4 4
1103 0 $keep = 0;
1104 0 last;
1105 }
1106 }
1107 1 $keep;
1108 } @out_lines;
1109 1 $out = join("\n", @out_lines);
1110 }
1111  
1112 1 ▶ if (@{$matched_filter->{keep_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"keep_lines_matching"} // [];}) 0 1 1
Condition50%1/2
$$matched_filter{"keep_lines_matching"} // []
l!l
10
MC/DC0%0/1
$$matched_filter{"keep_lines_matching"} // []
$$matched_filter{"keep_lines_matching"}
Truth table50%1/2
A
A= $$matched_filter{"keep_lines_matching"}
Aresult
11
00
1113 0 my @out_lines = split(/\n/, $out);
1114 @out_lines = grep {
1115 0 my $keep = 0;
1116 0 for my $pattern (@{$matched_filter->{keep_lines_matching}}) {
1117 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
1118 0 $keep = 1;
1119 0 last;
1120 }
1121 }
1122 0 $keep;
1123 } @out_lines;
1124 0 $out = join("\n", @out_lines);
1125 }
1126  
1127 1 ▶ if ($matched_filter->{truncate_lines_at} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"truncate_lines_at"} > 0) 0 1 1
1128 0 my $max = $matched_filter->{truncate_lines_at};
1129 0 ▶ $out = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $out));
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max ? : 0 0 0
1130 0 ▶ $err = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $err)) if length $err;
Branch: 2 branches
BranchTrueFalseTotal
if length $err 0 0 0
length $_ > $max ? : 0 0 0
1131 }
1132  
1133 1 ▶ if ($matched_filter->{head_lines} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$matched_filter{'head_lines'} > 0) { } 0 1 1
elsif ($$matched_filter{'tail_lines'} > 0) { } 0 1 1
1134 0 my @lines = split(/\n/, $out);
1135 0 my $head = $matched_filter->{head_lines};
1136 0 ▶ my $tail = $matched_filter->{tail_lines} // 0;
Condition0%0/2
$$matched_filter{"tail_lines"} // 0
l!l
00
MC/DC0%0/1
$$matched_filter{"tail_lines"} // 0
$$matched_filter{"tail_lines"}
Truth table0%0/2
A
A= $$matched_filter{"tail_lines"}
Aresult
11
00
1137 0 my $omit = @lines - $head - $tail;
1138 0 ▶ if ($omit > 0 && $tail > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($omit > 0 and $tail > 0) { } 0 0 0
elsif ($omit > 0) { } 0 0 0
Condition0%0/3
$omit > 0 and $tail > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$omit > 0 and $tail > 0
$omit > 0 $tail > 0
Truth table0%0/3
A and B
A= $omit > 0B= $tail > 0
ABresult
0X0
100
111
1139 0 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...\n" . (join("\n", @lines[-$tail..-1]));
1140 }
1141 elsif ($omit > 0) {
1142 0 $out = join("\n", @lines[0..min($head, @lines)-1]);
1143 }
1144 }
1145 elsif ($matched_filter->{tail_lines} > 0) {
1146 0 my @lines = split(/\n/, $out);
1147 0 $out = join("\n", @lines[-min($matched_filter->{tail_lines}, @lines)..-1]);
1148 }
1149  
1150 1 ▶ if ($matched_filter->{max_lines} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"max_lines"} > 0) 1 0 1
1151 1 my @out_lines = split(/\n/, $out);
1152 1 ▶ if (@out_lines > $matched_filter->{max_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out_lines > $$matched_filter{"max_lines"}) 0 1 1
1153 0 $out = join("\n", @out_lines[0..$matched_filter->{max_lines}-1]) . "\n... " . (@out_lines - $matched_filter->{max_lines}) . " more lines ...";
1154 }
1155 }
1156  
1157 1 ▶ if (!length trim($out) && $matched_filter->{on_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length trim($out) and $$matched_filter{"on_empty"}) 0 1 1
Condition33%1/3
not length trim($out) and $$matched_filter{"on_empty"}
!ll&&!rl&&r
100
MC/DC0%0/2
not length trim($out) and $$matched_filter{"on_empty"}
not length trim($out) $$matched_filter{"on_empty"}
Truth table33%1/3
not(A) and B
A= not length trim($out)B= $$matched_filter{"on_empty"}
ABresult
0X0
100
111
1158 0 $out = $matched_filter->{on_empty};
1159 }
1160  
1161 1 return { command => $command, stdout => $out, stderr => $err };
1162 }
1163  
1164 22 ▶sub transform_command {
Subroutine transform_command: called CC=8 SCAR=20.8
Pod undocumented
1165 22 my ($self, $command) = @_;
1166 22 ▶ return $command if $ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"} 1 21 22
1167  
1168 21 my $parsed = $self->_parse_command($command);
1169  
1170 21 for my $key (keys %{$self->filters}) {
1171 543 my $filter = $self->filters->{$key};
1172  
1173 543 my $matches = 0;
1174  
1175 543 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $$filter{'parsed_command'}) { } 417 126 543
elsif ($command =~ /$key/u) { } 18 108 126
1176 417 ▶ $matches = 1 if $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_parsed_command($pc, $parsed) 1 416 417
1177 }
1178 elsif ($command =~ /$key/) {
1179 18 $matches = 1;
1180 }
1181  
1182 543 ▶ next unless $matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 524 19 543
1183  
1184 19 ▶ if ($filter->{command_transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$filter{"command_transform"}) 15 4 19
1185 15 $command = $filter->{command_transform}->($command);
1186 15 last; # Only apply first matching command_transform
1187 }
1188 }
1189  
1190 21 return $command;
1191 }
1192  
1193 24 1;
1194  
1195 __END__
↑ Summary bin/mcp-run-compress »