blib/lib/MCP/Run/Compress.pm

filter: Statementstmt 92.8% 298 / 321 Branchbran 90.9% 111 / 122 Conditioncond 78.4% 69 / 88 MC/DCmcdc 59.1% 29 / 49 Subroutinesub 74.2% 26 / 35 Podpod 20.0% 1 / 5 totaltotal 86.1% 534 / 620 CC 111 SCAR 27.6
CC
111
Cov
90%
_pipeline
37.7
register_filter
21.0
_match_parsed_command
14.1
CRAP 15.7
↑ Summary blib/lib/MCP/Run/Bash.pm »
1 package MCP::Run::Compress;
2 118 our $VERSION = '0.107';
3 118 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Output compression for LLMs
6  
7  
8 118 ▶use Text::Trim qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 118 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 118 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 86 ▶has filters => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14  
15 21056 ▶sub _parse_command {
Subroutine _parse_command: called CC=5 SCAR=17.1
16 21056 my ($self, $command) = @_;
17 21056 my @words = split /\s+/, $command;
18 21056 ▶ return { program => '', subcommand => undef, flags => {}, args => [] } unless @words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 131 20925 21056
19  
20 20925 my $program = shift @words;
21 20925 my ($subcommand, @remaining);
22  
23 # Find the subcommand (first word not starting with -)
24 20925 for my $i (0 .. $#words) {
25 18735 ▶ if ($words[$i] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($words[$i] =~ /^-/u) 17388 1347 18735
26 17388 $subcommand = $words[$i];
27 17388 @remaining = @words[$i+1 .. $#words];
28 17388 last;
29 }
30 }
31 20925 ▶ @remaining = @words unless defined $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subcommand 3537 17388 20925
32  
33 # Parse flags with Getopt::Long
34 20925 my %flags;
35  
36 # Build a Getopt::Long spec for common flag types
37 my @spec = (
38 267 ▶ '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 20925 );
56  
57 # Use GetOptionsFromArray to parse - suppress warnings for unknown flags
58 20925 my $warn_handler = $SIG{__WARN__};
59 20925 ▶ local $SIG{__WARN__} = sub { }; # Suppress warnings during parsing
Subroutine __ANON__: called CC=1 SCAR=0.0
60 20925 GetOptionsFromArray(\@remaining, @spec);
61  
62 return {
63 20925 program => $program,
64 subcommand => $subcommand,
65 flags => \%flags,
66 args => \@remaining,
67 };
68 }
69  
70 # Every attribute the pipeline reads, and nothing else -- register_filter
71 # refuses anything not on this list (karr #28). `replace` used to be stored
72 # here and read by no stage, which is not the harmless kind of dead code:
73 # after #25 a filter setting only `replace` counted as non-shaping, was
74 # never selected, and then tripped the "no dead configuration" invariant,
75 # so its author got a test failure instead of an explanation. It is gone
76 # rather than implemented because nothing was missing -- per-line rewriting
77 # is `transform`, whole-output replacement is `match_output`, dropping
78 # lines is `strip_lines_matching`. Refusing unknown names rather than only
79 # deleting the one attribute is the actual fix: silence is what let it sit
80 # here unnoticed, and the next typo would have sat here just as quietly.
81 118 my %FILTER_ATTRIBUTE = map { $_ => 1 } qw(
82 strip_ansi strip_lines_matching keep_lines_matching truncate_lines_at
83 max_lines tail_lines head_lines on_empty match_output filter_stderr
84 output_detect transform command_transform
85 );
86  
87 6370 ▶sub register_filter {
Subroutine register_filter: called CC=21 SCAR=30.5
Pod undocumented
88 6370 my $self = shift;
89 6370 my %args = @_;
90  
91 6370 my $command = delete $args{command};
92 6370 my $parsed_command = delete $args{parsed_command};
93  
94 6370 my @unknown = sort grep { !$FILTER_ATTRIBUTE{$_} } keys %args;
95 6370 ▶ die "register_filter: unknown filter attribute(s): @unknown\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 2 6368 6370
96  
97 # Determine storage key: parsed commands use a special key format
98 6368 my $key;
99 6368 ▶ if ($parsed_command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_command) { } 5333 1035 6368
100 5333 my @flag_parts;
101 5333 ▶ for my $flag (sort keys %{$parsed_command->{flags} // {}}) {
Condition100%2/2
$$parsed_command{"flags"} // {}
l!l
865247
MC/DC100%1/1
$$parsed_command{"flags"} // {}
$$parsed_command{"flags"}
Truth table100%2/2
A
A= $$parsed_command{"flags"}
Aresult
11
00
102 86 ▶ push @flag_parts, "$flag=" . ($parsed_command->{flags}{$flag} // 1);
Condition50%1/2
$$parsed_command{"flags"}{$flag} // 1
l!l
860
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
103 }
104 5333 ▶ $key = 'parsed:' . ($parsed_command->{program} // '') . ':' . ($parsed_command->{subcommand} // '') . ':' . join(',', @flag_parts);
Condition75%3/4
$$parsed_command{"program"} // ""
l!l
53330
$$parsed_command{"subcommand"} // ""
l!l
34401893
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
105 } else {
106 1035 $key = $command;
107 }
108  
109 my $filter = {
110 command => $command,
111 parsed_command => $parsed_command,
112 strip_ansi => $args{strip_ansi} // 0,
113 strip_lines_matching => $args{strip_lines_matching} // [],
114 keep_lines_matching => $args{keep_lines_matching} // [],
115 truncate_lines_at => $args{truncate_lines_at} // 0,
116 max_lines => $args{max_lines} // 0,
117 tail_lines => $args{tail_lines} // 0,
118 head_lines => $args{head_lines} // 0,
119 on_empty => $args{on_empty} // '',
120 match_output => $args{match_output} // [],
121 filter_stderr => $args{filter_stderr} // 0,
122 output_detect => $args{output_detect} // undef,
123 transform => $args{transform} // undef,
124 command_transform => $args{command_transform} // undef,
125 6368 ▶ };
Condition100%26/26
$args{"strip_ansi"} // 0
l!l
876281
$args{"strip_lines_matching"} // []
l!l
6107261
$args{"keep_lines_matching"} // []
l!l
16367
$args{"truncate_lines_at"} // 0
l!l
28393529
$args{"max_lines"} // 0
l!l
6109259
$args{"tail_lines"} // 0
l!l
1746194
$args{"head_lines"} // 0
l!l
886280
$args{"on_empty"} // ""
l!l
6895679
$args{"match_output"} // []
l!l
1736195
$args{"filter_stderr"} // 0
l!l
12915077
$args{"output_detect"} // undef
l!l
876281
$args{"transform"} // undef
l!l
3456023
$args{"command_transform"} // undef
l!l
876281
MC/DC100%1/1
$args{"strip_ansi"} // 0
$args{"strip_ansi"}
MC/DC100%1/1
$args{"strip_lines_matching"} // []
$args{"strip_lines_matching"}
MC/DC100%1/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/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 table100%2/2
A
A= $args{"strip_ansi"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"strip_lines_matching"}
Aresult
11
00
Truth table100%2/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 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
126  
127 # Derived, not configured: which of the two selections in _pipeline
128 # this filter is a candidate for. Computed once here because
129 # _match_filter asks it for every filter on every run.
130 6368 $filter->{_shapes_output} = $self->_shapes_output($filter);
131  
132 6368 $self->filters->{$key} = $filter;
133  
134 6368 return;
135 }
136  
137 798135 ▶sub _match_parsed_command {
Subroutine _match_parsed_command: called CC=13 SCAR=26.5
138 798135 my ($self, $filter_spec, $parsed) = @_;
139 798135 ▶ return 0 unless $parsed && $filter_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $filter_spec 0 798135 798135
Condition33%1/3
$parsed and $filter_spec
!ll&&!rl&&r
00798135
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
140  
141 # Must match program
142 798135 ▶ return 0 if ($filter_spec->{program} // '') ne ($parsed->{program} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($$filter_spec{"program"} // "") ne ($$parsed{"program"} // "") 769368 28767 798135
Condition50%2/4
$$filter_spec{"program"} // ""
l!l
7981350
$$parsed{"program"} // ""
l!l
7981350
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
143  
144 # Subcommand must match if specified
145 28767 ▶ if (defined $filter_spec->{subcommand}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$filter_spec{"subcommand"}) 22765 6002 28767
146 22765 ▶ return 0 if ($filter_spec->{subcommand} // '') ne ($parsed->{subcommand} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($$filter_spec{"subcommand"} // "") ne ($$parsed{"subcommand"} // "") 12055 10710 22765
Condition75%3/4
$$filter_spec{"subcommand"} // ""
l!l
227650
$$parsed{"subcommand"} // ""
l!l
2275510
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
147 }
148  
149 # All specified flags must be present and truthy
150 16712 ▶ for my $flag (keys %{$filter_spec->{flags} // {}}) {
Condition100%2/2
$$filter_spec{"flags"} // {}
l!l
52716185
MC/DC100%1/1
$$filter_spec{"flags"} // {}
$$filter_spec{"flags"}
Truth table100%2/2
A
A= $$filter_spec{"flags"}
Aresult
11
00
151 527 ▶ return 0 unless $parsed->{flags}{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless $$parsed{"flags"}{$flag} 262 265 527
152 }
153  
154 16450 return 1;
155 }
156  
157  
158 118 my $CO_AUTHORED_WORD = qr{[A-Za-z0-9][A-Za-z0-9._:/+-]*};
159 118 my $CO_AUTHORED_MAIL = qr{[A-Za-z0-9._%+-]+\@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+};
160 118 my $CO_AUTHORED_VALUE = qr{\A$CO_AUTHORED_WORD(?:[ ]$CO_AUTHORED_WORD)*(?:[ ]<$CO_AUTHORED_MAIL>)?\z};
161  
162 10522 ▶sub _co_authored_by {
Subroutine _co_authored_by: called CC=5 SCAR=16.1
163 10522 ▶ my $model = $ENV{CO_AUTHORED_BY} || $ENV{ANTHROPIC_MODEL};
Condition100%3/3
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
l!l&&r!l&&!r
10505161
MC/DC100%2/2
$ENV{'CO_AUTHORED_BY'} || $ENV{'ANTHROPIC_MODEL'}
$ENV{'CO_AUTHORED_BY'} $ENV{'ANTHROPIC_MODEL'}
Truth table100%3/3
A || B
A= $ENV{'CO_AUTHORED_BY'}B= $ENV{'ANTHROPIC_MODEL'}
ABresult
1X1
011
000
164 10522 ▶ return undef unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 1 10521 10522
Condition66%2/3
defined $model and length $model
!ll&&!rl&&r
1010521
MC/DC50%1/2
defined $model and length $model
defined $model length $model
Truth table66%2/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
165 10521 ▶ return undef unless $model =~ $CO_AUTHORED_VALUE;
Branch: 1 branch
BranchTrueFalseTotal
unless $model =~ /$CO_AUTHORED_VALUE/u 22 10499 10521
166 10499 return $model;
167 }
168  
169  
170 41216 ▶sub _collapse_cr {
Subroutine _collapse_cr: called CC=7 SCAR=19.5
171 41216 my ($text) = @_;
172 41216 ▶ return $text unless defined $text && index($text, "\r") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and index($text, "\r") != -1 32461 8755 41216
Condition66%2/3
defined $text and index($text, "\r") != -1
!ll&&!rl&&r
0324618755
MC/DC50%1/2
defined $text and index($text, "\r") != -1
defined $text index($text, "\r") != -1
Truth table66%2/3
A and B
A= defined $textB= index($text, "\r") != -1
ABresult
0X0
100
111
173  
174 8755 $text =~ s/\r\n/\n/g;
175 8755 ▶ return $text unless index($text, "\r") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($text, "\r") != -1 1214 7541 8755
176  
177 7541 my @lines = split /\n/, $text, -1;
178 7541 for my $line (@lines) {
179 25329 ▶ next unless index($line, "\r") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($line, "\r") != -1 17668 7661 25329
180 7661 my @states = grep { length } split /\r/, $line, -1;
181 7661 ▶ $line = @states ? $states[-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
@states ? : 7661 0 7661
182 }
183  
184 7541 return join "\n", @lines;
185 }
186  
187 86 ▶sub _build_default_filters {
Subroutine _build_default_filters: called CC=1 SCAR=0.0
188 86 my $self = shift;
189  
190 # ls: ultra-compact for long listing - only keep type + filename
191 $self->register_filter(
192 command => '^ls\b',
193 # The mode column is a type letter, nine rwx characters, and -- on some
194 # systems -- one more for the alternate access method: `.` for an SELinux
195 # context (Fedora, Rocky, RHEL), `+` for a POSIX ACL, `@` for macOS
196 # extended attributes, nothing at all on a plain Debian. That last case is
197 # the one this was written on, which is why a filter that did nothing
198 # whatsoever on the smokers' machines looked healthy here for years
199 # (GitHub #3, karr #35). The type letter is every type `ls -l` can print,
200 # not just file and directory: a listing of nothing but symlinks was not
201 # even recognized as a long listing before.
202 output_detect => qr(^[-bcdDlps][rwx-]{9}.*\s+\d+\s+),
203 strip_lines_matching => [
204 qr(^\s*$),
205 qr(^total\s+\d+),
206 qr(^\s*Device:),
207 qr(^\s*Inode:),
208 qr(^\s*Birth:),
209 qr(^/node_modules/),
210 qr(^/\.git/),
211 qr(^/\.target/),
212 qr(^/\.next/),
213 qr(^/\.nuxt/),
214 qr(^/\.cache/),
215 qr(^/__pycache__/),
216 qr(^/\.DS_Store/),
217 qr(^/vendor/bundle/),
218 ],
219 1448 ▶ transform => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
220 1448 my ($line) = @_;
221 # type, nine mode characters, the optional access-method character
222 # (see output_detect above), links, owner, group, size, date, name.
223 # The size is `major, minor` for a device, which is why it is not a
224 # plain \d+. What survives is the type letter and the name -- for a
225 # symlink that name is `link -> target`, kept whole on purpose: the
226 # target is the only thing a symlink actually says, and dropping it
227 # would leave an entry that names a file and hides what it is.
228 #
229 # The date is not one shape either. `ls --time-style=` picks it, and
230 # the formats do not agree on a single one: the locale default prints
231 # `Aug 14 08:44`, long-iso prints `2026-08-23 10:00`, full-iso adds
232 # seconds, nanoseconds and the zone offset, iso drops the year and
233 # prints `08-23 10:00`, and a strftime format like +%Y-%m-%d prints
234 # the date alone. So the date is an alternation over whole formats,
235 # not a loosened single pattern: each alternative describes one
236 # format completely, and a line that fits none of them stays
237 # uncollapsed rather than being cut at the wrong place (karr #36).
238 1448 ▶ if ($line =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / ^([-bcdDlps])[rwx-]{9}[.+\@]? \s+\d+ \s+\S+ \s+\S+ \s+\d+(?:,\s*\d+)? \s+(?: \d{4}-\d{2}-\d{2} \s+ \d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)? (?:\s+[+-]\d{2}:?\d{2})? # long-iso, full-iso | \d{4}-\d{2}-\d{2} # +%Y-%m-%d | \d{2}-\d{2} \s+ \d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)? # iso | \w+ \s+ \d+ \s+ [\d:]+ # locale ) \s+(.+)$ /ux) 1405 43 1448
239 ^([-bcdDlps])[rwx-]{9}[.+\@]?
240 \s+\d+ \s+\S+ \s+\S+ \s+\d+(?:,\s*\d+)?
241 \s+(?:
242 \d{4}-\d{2}-\d{2} \s+ \d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)? (?:\s+[+-]\d{2}:?\d{2})? # long-iso, full-iso
243 | \d{4}-\d{2}-\d{2} # +%Y-%m-%d
244 | \d{2}-\d{2} \s+ \d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)? # iso
245 | \w+ \s+ \d+ \s+ [\d:]+ # locale
246 )
247 \s+(.+)$
248 }x) {
249 1405 return $1 . " " . $2;
250 }
251 43 return $line;
252 },
253 86 truncate_lines_at => 100,
254 max_lines => 50,
255 );
256  
257 # stat: strip device/inode/birth (Linux)
258 86 $self->register_filter(
259 command => '^stat\b',
260 strip_lines_matching => [
261 qr(^\s*$),
262 qr(^\s*Device:),
263 qr(^\s*Inode:),
264 qr(^\s*Birth:),
265 ],
266 truncate_lines_at => 120,
267 max_lines => 20,
268 );
269  
270 # grep: truncate lines, group by file
271 86 $self->register_filter(
272 command => '^grep\b',
273 truncate_lines_at => 150,
274 max_lines => 100,
275 );
276  
277 # make: strip entering/leaving directory
278 86 $self->register_filter(
279 command => '^make\b',
280 strip_lines_matching => [
281 qr(^\s*$),
282 # Only the chatter, not every sub-make line (karr #32). This used to
283 # be a bare qr(^make\[\d+\]:), which did suppress the Entering/
284 # Leaving pair a recursive build prints per directory -- and also
285 # `make[1]: *** [Makefile:18: lexer.o] Error 1`, the only line that
286 # names the target that actually broke and the Makefile line it
287 # broke on. What survived was the top-level abort, and that one
288 # always says `all`. On a tree with twenty subdirectories the answer
289 # to "where" was being stripped out of every failed build.
290 #
291 # Narrowing lets the rest of make's vocabulary through, which is
292 # what we want: *** Error N, *** No rule to make target, Target not
293 # remade because of errors, Circular ... dependency dropped (the
294 # warning the removed short-circuit of #29 was meant to report),
295 # and the jobserver warnings. All of them are one-off and all of
296 # them say something.
297 qr(^make\[\d+\]:\s+(?:Entering|Leaving) directory),
298 # The one line of real volume the narrowing would otherwise let
299 # back in: a no-op recursive build prints this once per directory,
300 # and without it twenty subdirectories cost twenty lines instead of
301 # the single `make: ok` that on_empty gives below.
302 qr(^make\[\d+\]:\s+.*is up to date\.\s*$),
303 qr(^Entering directory),
304 qr(^Leaving directory),
305 qr(Nothing to be done),
306 qr(^make:\s+Nothing to be done),
307 ],
308 # Two match_output short-circuits removed here (karr #29), and adding
309 # the missing /m would not have rescued either of them.
310 #
311 # `^make\[\d+\]:.*?make\[\d+\]:` -> "circular dependency detected"
312 # never looked for the word Circular; it wanted two `make[N]:` on ONE
313 # line, because `.` does not cross newlines without /s. Real circular
314 # output is `make[1]: Circular a <- b dependency dropped.` -- one per
315 # line, so /m changes nothing. And it is a warning: make carries on,
316 # so replacing the whole build with that one note would hide how the
317 # build actually ended.
318 #
319 # `^gcc.*?Error` -> "compilation error" wants a line starting with gcc
320 # that also carries "Error". gcc writes `lexer.c:142:9: error:` --
321 # lowercase, and starting with the file. The echoed `gcc -c ...` line
322 # has no Error on it. So /m does not make it fire either, and if it
323 # ever did it would delete the compiler diagnosis, which is the entire
324 # reason someone reads a failed build.
325 #
326 # The honest short-circuit for make is on_empty below: it speaks only
327 # when the strip list left nothing, i.e. when there is nothing to say.
328 max_lines => 50,
329 on_empty => 'make: ok',
330 );
331  
332 # git status: compact output
333 86 $self->register_filter(
334 parsed_command => {
335 program => 'git',
336 subcommand => 'status',
337 },
338 strip_lines_matching => [
339 qr(^\s*$),
340 qr(^On branch),
341 qr(^Your branch is),
342 qr(^Initial commit),
343 ],
344 max_lines => 30,
345 );
346  
347 # git diff: keep only diff content
348 86 $self->register_filter(
349 command => '^git\s+diff\b',
350 strip_lines_matching => [
351 qr(^\s*$),
352 qr(^diff --git),
353 qr(^index ),
354 qr(^---\s+a/),
355 qr(^\+\+\+\s+b/),
356 ],
357 truncate_lines_at => 150,
358 max_lines => 200,
359 );
360  
361 # git diff --stat: compact format "N+M- filename"
362 $self->register_filter(
363 parsed_command => {
364 program => 'git',
365 subcommand => 'diff',
366 flags => { stat => 1 },
367 },
368 4041 ▶ transform => sub {
Subroutine __ANON__: called CC=5 SCAR=19.1
369 4041 my ($line) = @_;
370 # Format: " 1 file changed, 3 insertions(+), 2 deletions(-)" or
371 # " file1.txt | 5 +++ ---"
372 # Transform to "N+M- filename" for file lines
373 4041 ▶ 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 4041 4041
374 # "| 5 +++ ---" format -> "3+2- filename"
375 0 return "$2+$3-$1";
376 }
377 4041 ▶ 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 4041 4041
378 # "| 5 +3 -2" format
379 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
380 0 return "$add+$del-$file";
381 }
382 # Remove summary line "X files changed"
383 4041 ▶ if ($line =~ /^\s*\d+\s+files?\s+changed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\d+\s+files?\s+changed/u) 4 4037 4041
384 4 return undef; # Skip this line
385 }
386 4037 return $line;
387 },
388 86 strip_lines_matching => [
389 qr(^\s*$),
390 qr(^\s*\d+\s+files?\s+changed),
391 qr(^\s*\d+\s+insertions?\(\+\)),
392 qr(^\s*\d+\s+deletions?\(\-\)),
393 ],
394 max_lines => 100,
395 );
396  
397 # cat: detect and filter code
398 86 $self->register_filter(
399 command => '^cat\b',
400 strip_lines_matching => [qr(^\s*$)],
401 truncate_lines_at => 500,
402 max_lines => 100,
403 );
404  
405 # find: strip permission denied, limit results
406 86 $self->register_filter(
407 command => '^find\b',
408 strip_lines_matching => [
409 qr(^\s*$),
410 qr(^find:.*permission denied),
411 ],
412 max_lines => 50,
413 );
414  
415 # ps: compact output
416 86 $self->register_filter(
417 command => '^ps\b',
418 strip_lines_matching => [
419 qr(^\s*$),
420 ],
421 truncate_lines_at => 120,
422 max_lines => 30,
423 );
424  
425 # df: truncate wide columns
426 86 $self->register_filter(
427 command => '^df\b',
428 strip_lines_matching => [qr(^\s*$)],
429 truncate_lines_at => 80,
430 max_lines => 20,
431 );
432  
433 # docker ps: compact output
434 86 $self->register_filter(
435 parsed_command => {
436 program => 'docker',
437 subcommand => 'ps',
438 },
439 strip_lines_matching => [qr(^\s*$)],
440 truncate_lines_at => 120,
441 max_lines => 30,
442 );
443  
444 # docker images: compact output
445 86 $self->register_filter(
446 parsed_command => {
447 program => 'docker',
448 subcommand => 'images',
449 },
450 strip_lines_matching => [qr(^\s*$)],
451 truncate_lines_at => 120,
452 max_lines => 30,
453 );
454  
455 # terraform plan: strip refresh progress
456 86 $self->register_filter(
457 parsed_command => {
458 program => 'terraform',
459 subcommand => 'plan',
460 },
461 strip_lines_matching => [
462 qr(^\s*$),
463 qr(^Refreshing state\.\.\.),
464 qr(^Terraform used the),
465 qr(^tfe-outputs:),
466 ],
467 max_lines => 100,
468 );
469  
470 # terraform apply: strip progress
471 86 $self->register_filter(
472 parsed_command => {
473 program => 'terraform',
474 subcommand => 'apply',
475 },
476 strip_lines_matching => [
477 qr(^\s*$),
478 qr(^Refreshing state\.\.\.),
479 qr(^Terraform will perform),
480 qr(^Proceeding with the following),
481 qr(^tfe-outputs:),
482 ],
483 max_lines => 100,
484 );
485  
486 # docker build: strip build progress
487 86 $self->register_filter(
488 parsed_command => {
489 program => 'docker',
490 subcommand => 'build',
491 },
492 strip_lines_matching => [
493 qr(^\s*$),
494 qr(^#\s*\d+\s+\[\s*\d+\s+/\s*\d+\]),
495 qr(^Step \d+/\d+:),
496 ],
497 max_lines => 50,
498 );
499  
500 # docker run: compact output
501 86 $self->register_filter(
502 parsed_command => {
503 program => 'docker',
504 subcommand => 'run',
505 },
506 strip_lines_matching => [
507 qr(^\s*$),
508 qr(^Unable to find image),
509 qr(^Pulling from library/),
510 qr(^Digest:),
511 qr(^Status:),
512 ],
513 max_lines => 30,
514 );
515  
516 # kubectl get: compact output
517 86 $self->register_filter(
518 parsed_command => {
519 program => 'kubectl',
520 subcommand => 'get',
521 },
522 strip_lines_matching => [qr(^\s*$)],
523 truncate_lines_at => 150,
524 max_lines => 50,
525 );
526  
527 # kubectl describe: strip noise
528 86 $self->register_filter(
529 parsed_command => {
530 program => 'kubectl',
531 subcommand => 'describe',
532 },
533 strip_lines_matching => [
534 qr(^\s*$),
535 qr(^Name:\s+\w+),
536 qr(^Namespace:\s+\w+),
537 qr(^Labels:\s*$),
538 qr(^Annotations:\s*$/),
539 ],
540 truncate_lines_at => 200,
541 max_lines => 100,
542 );
543  
544 # cargo build: strip compile progress
545 86 $self->register_filter(
546 parsed_command => {
547 program => 'cargo',
548 subcommand => 'build',
549 },
550 strip_lines_matching => [
551 qr(^\s*$),
552 qr(^Compiling\s+\w+),
553 qr(^Fresh\s+\w+),
554 qr(^Finished\s+),
555 ],
556 max_lines => 50,
557 );
558  
559 # cargo test: compact output
560 86 $self->register_filter(
561 parsed_command => {
562 program => 'cargo',
563 subcommand => 'test',
564 },
565 strip_lines_matching => [
566 qr(^\s*$),
567 qr(^Compiling\s+\w+),
568 qr(^Running\s+/),
569 qr(^test result:),
570 ],
571 max_lines => 100,
572 );
573  
574 # cpanm: strip cpanm noise
575 86 $self->register_filter(
576 command => '^cpanm\b',
577 strip_lines_matching => [
578 qr(^\s*$),
579 qr(^--> ),
580 qr(^OK$),
581 qr(^FAIL$),
582 qr(^Working on),
583 qr(^Fetching),
584 qr(^Configuring),
585 qr(^Building and testing),
586 ],
587 # No match_output short-circuit to "cpanm: ok" (karr #29). It was
588 # written without /m, so its `^` anchored on the whole output and it
589 # never fired -- but /m is not the fix. The first realistic run it
590 # would fire on is `cpanm --installdeps .` where one distribution
591 # failed: cpanm prints "Successfully installed A" and then
592 # "! Installing B failed", and collapsing that to "cpanm: ok" reports
593 # a broken install as a clean one. The strip list above already leaves
594 # exactly the result lines -- naming the version, and the failure.
595 max_lines => 30,
596 );
597  
598 # npm install: strip noise
599 86 $self->register_filter(
600 parsed_command => {
601 program => 'npm',
602 subcommand => 'install',
603 },
604 strip_lines_matching => [
605 qr(^\s*$),
606 qr(^added\s+\d+\s+packages?),
607 qr(^found\s+\d+\s+packages?),
608 qr(^npm warn),
609 ],
610 max_lines => 30,
611 );
612  
613 # yarn install: similar to npm
614 86 $self->register_filter(
615 parsed_command => {
616 program => 'yarn',
617 subcommand => 'install',
618 },
619 strip_lines_matching => [
620 qr(^\s*$),
621 qr(^Done in\s+),
622 qr(^Resolving completed),
623 qr(^Linking completed),
624 ],
625 max_lines => 30,
626 );
627  
628 # yarn add: similar to npm
629 86 $self->register_filter(
630 parsed_command => {
631 program => 'yarn',
632 subcommand => 'add',
633 },
634 strip_lines_matching => [
635 qr(^\s*$),
636 qr(^Done in\s+),
637 qr(^Resolving completed),
638 qr(^Linking completed),
639 ],
640 max_lines => 30,
641 );
642  
643 # pnpm install: similar to npm
644 86 $self->register_filter(
645 parsed_command => {
646 program => 'pnpm',
647 subcommand => 'install',
648 },
649 strip_lines_matching => [
650 qr(^\s*$),
651 qr(^Done in\s+),
652 qr(^Resolving completed),
653 qr(^Linking completed),
654 ],
655 max_lines => 30,
656 );
657  
658 # pnpm add: similar to npm
659 86 $self->register_filter(
660 parsed_command => {
661 program => 'pnpm',
662 subcommand => 'add',
663 },
664 strip_lines_matching => [
665 qr(^\s*$),
666 qr(^Done in\s+),
667 qr(^Resolving completed),
668 qr(^Linking completed),
669 ],
670 max_lines => 30,
671 );
672  
673 # pip install: strip progress
674 86 $self->register_filter(
675 parsed_command => {
676 program => 'pip',
677 subcommand => 'install',
678 },
679 strip_lines_matching => [
680 qr(^\s*$),
681 qr(^Collecting\s+),
682 qr(^Downloading\s+),
683 qr(^Installing collected packages:),
684 qr(^Successfully installed),
685 ],
686 max_lines => 50,
687 );
688  
689 # pytest: compact output
690 86 $self->register_filter(
691 parsed_command => {
692 program => 'pytest',
693 },
694 strip_lines_matching => [
695 qr(^\s*$),
696 qr(^=+.*=+$/),
697 qr(^Coverage report:),
698 qr(^HTML report:),
699 ],
700 truncate_lines_at => 150,
701 max_lines => 100,
702 );
703  
704 # curl: strip headers
705 86 $self->register_filter(
706 parsed_command => {
707 program => 'curl',
708 },
709 filter_stderr => 1,
710 strip_lines_matching => [
711 qr(^\s*$),
712 qr(^ % Total),
713 qr(^ Resolving),
714 qr(^Connected to),
715 qr(^HTTP/\d[\d.]*\s+\d+),
716 ],
717 truncate_lines_at => 200,
718 max_lines => 50,
719 );
720  
721 # wget: strip progress
722 86 $self->register_filter(
723 parsed_command => {
724 program => 'wget',
725 },
726 strip_lines_matching => [
727 qr(^\s*$),
728 qr(^--\d{4}-\d{2}-\d{2}),
729 qr(^Resolving),
730 qr(^Connecting to),
731 qr(^Length:\s+\d+),
732 qr(^Saving to:),
733 qr(^\d+%\s+\[),
734 ],
735 truncate_lines_at => 200,
736 max_lines => 50,
737 );
738  
739 # helm install: strip progress
740 86 $self->register_filter(
741 parsed_command => {
742 program => 'helm',
743 subcommand => 'install',
744 },
745 strip_lines_matching => [
746 qr(^\s*$),
747 qr(^NAME:\s+\w+),
748 qr(^NAMESPACE:\s+\w+),
749 qr(^STATUS:\s+),
750 qr(^REVISION:\s+\d+),
751 qr(^NOTES:$),
752 ],
753 max_lines => 50,
754 );
755  
756 # helm upgrade: strip progress
757 86 $self->register_filter(
758 parsed_command => {
759 program => 'helm',
760 subcommand => 'upgrade',
761 },
762 strip_lines_matching => [
763 qr(^\s*$),
764 qr(^NAME:\s+\w+),
765 qr(^NAMESPACE:\s+\w+),
766 qr(^STATUS:\s+),
767 qr(^REVISION:\s+\d+),
768 qr(^NOTES:$),
769 ],
770 max_lines => 50,
771 );
772  
773 # ansible-playbook: strip progress
774 86 $self->register_filter(
775 parsed_command => {
776 program => 'ansible-playbook',
777 },
778 strip_lines_matching => [
779 qr(^\s*$),
780 qr(^PLAY\s+\[),
781 qr(^TASK\s+\[),
782 qr(^RUNNING HANDLER),
783 qr(^changed:\s+\[\d+\]),
784 qr(^ok:\s+\[\d+\]),
785 qr(^fatal:\s+\[\d+\]),
786 qr(^PLAY RECAP),
787 ],
788 max_lines => 100,
789 );
790  
791 # rsync: strip progress
792 86 $self->register_filter(
793 parsed_command => {
794 program => 'rsync',
795 },
796 strip_lines_matching => [
797 qr(^\s*$),
798 qr(^sent\s+\d+\s+bytes),
799 qr(^received\s+\d+\s+bytes),
800 qr(^total size is),
801 ],
802 max_lines => 30,
803 );
804  
805 # iptables -L: compact
806 86 $self->register_filter(
807 parsed_command => {
808 program => 'iptables',
809 },
810 strip_lines_matching => [qr(^\s*$)],
811 truncate_lines_at => 150,
812 max_lines => 50,
813 );
814  
815 # ping: strip progress
816 86 $self->register_filter(
817 parsed_command => {
818 program => 'ping',
819 },
820 strip_lines_matching => [
821 qr(^\s*$),
822 qr(^PING\s+),
823 qr(^64 bytes from),
824 qr(^---.*ping statistics---),
825 ],
826 max_lines => 20,
827 );
828  
829 # netstat: compact output for -tulpn
830 86 $self->register_filter(
831 parsed_command => {
832 program => 'netstat',
833 },
834 strip_lines_matching => [qr(^\s*$)],
835 truncate_lines_at => 150,
836 max_lines => 50,
837 );
838  
839 # ip addr: compact
840 86 $self->register_filter(
841 parsed_command => {
842 program => 'ip',
843 subcommand => 'addr',
844 },
845 strip_lines_matching => [qr(^\s*$)],
846 truncate_lines_at => 150,
847 max_lines => 50,
848 );
849  
850 # ip route: compact
851 86 $self->register_filter(
852 parsed_command => {
853 program => 'ip',
854 subcommand => 'route',
855 },
856 strip_lines_matching => [qr(^\s*$)],
857 max_lines => 30,
858 );
859  
860 # ip link: compact
861 86 $self->register_filter(
862 parsed_command => {
863 program => 'ip',
864 subcommand => 'link',
865 },
866 strip_lines_matching => [qr(^\s*$)],
867 max_lines => 30,
868 );
869  
870 # mount: strip noise
871 86 $self->register_filter(
872 parsed_command => {
873 program => 'mount',
874 },
875 strip_lines_matching => [qr(^\s*$)],
876 truncate_lines_at => 200,
877 max_lines => 50,
878 );
879  
880 # lsblk: compact block device listing
881 86 $self->register_filter(
882 parsed_command => {
883 program => 'lsblk',
884 },
885 strip_lines_matching => [qr(^\s*$)],
886 truncate_lines_at => 150,
887 max_lines => 50,
888 );
889  
890 # blkid: compact block device attributes
891 86 $self->register_filter(
892 parsed_command => {
893 program => 'blkid',
894 },
895 strip_lines_matching => [qr(^\s*$)],
896 truncate_lines_at => 200,
897 max_lines => 50,
898 );
899  
900 # git log: compact
901 86 $self->register_filter(
902 parsed_command => {
903 program => 'git',
904 subcommand => 'log',
905 },
906 strip_lines_matching => [
907 qr(^\s*$),
908 qr(^commit\s+[a-f0-9]+),
909 qr(^Author:\s+),
910 qr(^Date:\s+),
911 ],
912 head_lines => 20,
913 tail_lines => 10,
914 max_lines => 30,
915 );
916  
917 # git branch: compact
918 86 $self->register_filter(
919 parsed_command => {
920 program => 'git',
921 subcommand => 'branch',
922 },
923 strip_lines_matching => [qr(^\s*$)],
924 max_lines => 30,
925 );
926  
927 # git stash: compact
928 86 $self->register_filter(
929 parsed_command => {
930 program => 'git',
931 subcommand => 'stash',
932 },
933 strip_lines_matching => [qr(^\s*$)],
934 max_lines => 30,
935 );
936  
937 # git commit: transform Co-Authored-By. Match the raw string so we
938 # also catch compound commands like `cd foo && git add bar && git
939 # commit -m "..."`, where the parsed top-level program isn't `git`.
940 $self->register_filter(
941 command => '\bgit\b[^&|;]*?(?<![=])\bcommit\b',
942 10522 ▶ command_transform => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
943 10522 my ($cmd) = @_;
944 # Validated, not escaped: an unusable value means no trailer at all,
945 # for both the replacement and the injection below. See _co_authored_by.
946 10522 my $model = _co_authored_by();
947 10522 ▶ return $cmd unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 23 10499 10522
948 # Replacement of a trailer the command already carries. The
949 # substitution IS the condition: detection used to be a separate,
950 # case-insensitive regex while the replacement was case-sensitive, so
951 # a lower- or upper-cased trailer entered the branch, replaced
952 # nothing, and returned -- skipping the injection below as well and
953 # leaving the commit with no trailer at all (karr #18). With one
954 # regex instead of two, "detected but not replaced" cannot happen.
955 #
956 # The spelling is normalized to the canonical form rather than
957 # preserved: git matches trailer keys case-insensitively, so nothing
958 # is lost, both paths of this transform now emit the same shape, and
959 # one variable fewer is spliced into a shell command line.
960 #
961 # [^\n"]+ instead of [^\n]+: when the trailer sits at the end of a
962 # -m "..." message, the greedy class used to swallow the closing
963 # quote too, leaving the shell with an unterminated string.
964 10499 ▶ return $cmd if $cmd =~ s/Co-Authored-By:[ \t]*[^\n"]+/Co-Authored-By: $model/gi;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ s/Co-Authored-By:[ \t]*[^\n"]+/Co-Authored-By: $model/giu 274 10225 10499
965 # Injection. The trailer is spliced after the closing quote of the
966 # last -m/--message argument inside the git commit invocation (the
967 # invocation runs from `commit` up to the next shell separator).
968 # The old code targeted the last quote of the whole command line, so
969 # a compound command like `git commit -m "x" && git config user.name
970 # "y"` had the trailer injected into user.name, corrupting the config
971 # value. Injecting inside the quote (rather than appending `-m
972 # "..."`) keeps the trailer part of the commit message: appended, the
973 # `<model@host>` would parse as a shell redirection and break the
974 # call. No -m argument (e.g. `git commit --amend` opening the editor)
975 # means no safe injection point, so nothing is rewritten.
976 10225 my $tail = $cmd;
977 10225 ▶ if ( $tail =~ /\bgit\b[^&|;]*?(?<![=])\bcommit\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /\bgit\b[^&|;]*?(?<![=])\bcommit\b/u) 10225 0 10225
978 10225 $tail =~ s/^.*\bgit\b[^&|;]*?(?<![=])\bcommit\b//;
979 10225 my $origin = length($cmd) - length($tail);
980 10225 $tail =~ s/[&|;].*$//;
981 # -[a-zA-Z]*m covers bundled short flags whose last letter is the
982 # message flag (-am, -sm, -anm), not just a bare -m. The lookbehind
983 # requires the dash to start an argument -- \b can't do that, since
984 # `-` is not a word character, so \b- never matches after a space.
985 # Without it, a path like /tmp/msg-m "x" would look like a flag.
986 10225 ▶ if ( $tail
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ /^(.*(?<![^\s])(?:--message|-[a-zA-Z]*m)\s+")((?:[^"\\]|\\.)*)(")/u) 10222 3 10225
987 =~ /^(.*(?<![^\s])(?:--message|-[a-zA-Z]*m)\s+")((?:[^"\\]|\\.)*)(")/ )
988 {
989 10222 substr( $cmd, $origin + length($1) + length($2), 0 )
990 = "\n\nCo-Authored-By: $model";
991 }
992 }
993 10225 return $cmd;
994 },
995 86 );
996  
997 # ------------------------------------------------------------------
998 # karr #17: filters MCP::Run::Compress::Filters documented before they
999 # existed. Every strip pattern below is anchored on a literal prefix the
1000 # tool really prints, so a format change makes the filter do nothing
1001 # rather than eat a line it should have kept.
1002 # ------------------------------------------------------------------
1003  
1004 # du: the two directories that dominate any recursive disk usage run
1005 86 $self->register_filter(
1006 parsed_command => {
1007 program => 'du',
1008 },
1009 strip_lines_matching => [
1010 qr(^\s*$),
1011 qr(/\.git(?:/|$)),
1012 qr(/node_modules(?:/|$)),
1013 ],
1014 truncate_lines_at => 150,
1015 max_lines => 50,
1016 );
1017  
1018 # gcc: strip the include chain and the source echo, keep the diagnostics.
1019 # filter_stderr is not optional here: a compiler says everything on
1020 # stderr, and every stage below this one works on stdout, so without the
1021 # merge the filter would look at an empty string and do nothing.
1022 86 $self->register_filter(
1023 parsed_command => {
1024 program => 'gcc',
1025 },
1026 filter_stderr => 1,
1027 strip_lines_matching => [
1028 qr(^\s*$),
1029 qr(^In file included from ),
1030 qr(^\s+from \S),
1031 qr(:\d+:(?:\d+:)?\s+note:),
1032 qr(^\s*\d+\s*\|),
1033 qr(^\s*\|),
1034 ],
1035 truncate_lines_at => 200,
1036 max_lines => 100,
1037 );
1038  
1039 # g++: same output format as gcc, same treatment
1040 86 $self->register_filter(
1041 parsed_command => {
1042 program => 'g++',
1043 },
1044 filter_stderr => 1,
1045 strip_lines_matching => [
1046 qr(^\s*$),
1047 qr(^In file included from ),
1048 qr(^\s+from \S),
1049 qr(:\d+:(?:\d+:)?\s+note:),
1050 qr(^\s*\d+\s*\|),
1051 qr(^\s*\|),
1052 ],
1053 truncate_lines_at => 200,
1054 max_lines => 100,
1055 );
1056  
1057 # swift build: strip the build chatter, so that what is left is exactly
1058 # the warnings and errors -- and on_empty speaks when there are none.
1059 # The reference used to promise a short-circuit to "ok (build complete)"
1060 # *unless* warnings or errors are present; match_output cannot express
1061 # that negative, on_empty reaches the same result from the other end.
1062 86 $self->register_filter(
1063 parsed_command => {
1064 program => 'swift',
1065 subcommand => 'build',
1066 },
1067 filter_stderr => 1,
1068 strip_lines_matching => [
1069 qr(^\s*$),
1070 qr(^\[\d+/\d+\]\s),
1071 qr(^Compiling\s),
1072 qr(^Fetching\s),
1073 qr(^Fetched\s),
1074 qr(^Computing version for ),
1075 qr(^Computed \S+ at ),
1076 qr(^Creating working copy for ),
1077 qr(^Working copy of \S+ resolved at ),
1078 qr(^Planning build),
1079 qr(^Building for ),
1080 qr(^Build complete!),
1081 ],
1082 max_lines => 100,
1083 on_empty => 'swift build: ok (build complete)',
1084 );
1085  
1086 # mix compile: Elixir prints one line per compiled batch and one per app
1087 86 $self->register_filter(
1088 parsed_command => {
1089 program => 'mix',
1090 subcommand => 'compile',
1091 },
1092 filter_stderr => 1,
1093 strip_lines_matching => [
1094 qr(^\s*$),
1095 qr(^Compiling \d+ files?),
1096 qr(^Generated \S+ app),
1097 qr(^==> \S+$),
1098 qr(^Resolving Hex dependencies),
1099 qr(^Dependency resolution completed),
1100 ],
1101 max_lines => 50,
1102 on_empty => 'mix compile: ok',
1103 );
1104  
1105 # pio run: PlatformIO opens with a full environment report before the
1106 # build. The memory usage lines (RAM:/Flash:) are kept -- for an embedded
1107 # build that is the number people ran the command for.
1108 86 $self->register_filter(
1109 parsed_command => {
1110 program => 'pio',
1111 subcommand => 'run',
1112 },
1113 filter_stderr => 1,
1114 strip_lines_matching => [
1115 qr(^\s*$),
1116 qr(^-+$),
1117 qr(^Processing \S+),
1118 qr(^Verbose mode can be enabled),
1119 qr(^CONFIGURATION:),
1120 qr(^PLATFORM:),
1121 qr(^HARDWARE:),
1122 qr(^DEBUG:),
1123 qr(^PACKAGES:),
1124 qr(^\s*-\s+\S+ @ ),
1125 qr(^LDF:),
1126 qr(^LDF Modes:),
1127 qr(^Found \d+ compatible libraries),
1128 qr(^Scanning dependencies),
1129 qr(^Dependency Graph),
1130 qr(^\|--),
1131 qr(^No dependencies),
1132 qr(^Building in \w+ mode),
1133 qr(^(?:Compiling|Archiving|Indexing|Linking|Building|Checking size) \.?pio),
1134 qr(^Advanced Memory Usage is available),
1135 ],
1136 max_lines => 50,
1137 on_empty => 'pio run: ok',
1138 );
1139  
1140 # mvn: the [INFO] stream is mostly plugin bookkeeping, but not all of it
1141 # -- surefire reports test results on [INFO] too, and that is the line
1142 # people run maven for. Only the known noise goes, not every [INFO].
1143 86 $self->register_filter(
1144 parsed_command => {
1145 program => 'mvn',
1146 },
1147 strip_lines_matching => [
1148 qr(^\s*$),
1149 qr(^\[INFO\]\s*$),
1150 qr(^\[INFO\]\s*-{5,}),
1151 qr(^\[INFO\] Scanning for projects),
1152 qr(^\[INFO\] --- ),
1153 qr(^\[INFO\] (?:Downloading|Downloaded) from ),
1154 qr(^\[INFO\] Building jar:),
1155 qr(^\[INFO\] Installing \S+ to ),
1156 qr(^\[INFO\] (?:Total time|Finished at):),
1157 qr(^(?:Downloading|Downloaded) from ),
1158 qr(^Progress \(\d+\):),
1159 ],
1160 truncate_lines_at => 200,
1161 max_lines => 100,
1162 );
1163  
1164 # gradle: the per-task lines are noise only while they succeed. A task
1165 # line carrying FAILED keeps its line -- that is the whole message.
1166 86 $self->register_filter(
1167 parsed_command => {
1168 program => 'gradle',
1169 },
1170 strip_lines_matching => [
1171 qr(^\s*$),
1172 qr(^> Task :\S+(?:\s+(?:UP-TO-DATE|NO-SOURCE|SKIPPED|FROM-CACHE))?$),
1173 qr(^Download https?://),
1174 qr(^Starting a Gradle Daemon),
1175 qr(^Welcome to Gradle ),
1176 qr(^Daemon will be stopped),
1177 ],
1178 truncate_lines_at => 200,
1179 max_lines => 100,
1180 );
1181  
1182 # tofu plan: OpenTofu is a Terraform fork and prints the same lines
1183 86 $self->register_filter(
1184 parsed_command => {
1185 program => 'tofu',
1186 subcommand => 'plan',
1187 },
1188 strip_lines_matching => [
1189 qr(^\s*$),
1190 qr(^Refreshing state\.\.\.),
1191 qr(^OpenTofu used the),
1192 qr(^tfe-outputs:),
1193 ],
1194 max_lines => 100,
1195 );
1196  
1197 # tofu apply: mirrors the terraform apply filter above
1198 86 $self->register_filter(
1199 parsed_command => {
1200 program => 'tofu',
1201 subcommand => 'apply',
1202 },
1203 strip_lines_matching => [
1204 qr(^\s*$),
1205 qr(^Refreshing state\.\.\.),
1206 qr(^OpenTofu will perform),
1207 qr(^Proceeding with the following),
1208 qr(^tfe-outputs:),
1209 ],
1210 max_lines => 100,
1211 );
1212  
1213 # tofu init: a successful init carries no information beyond having
1214 # succeeded, so it short-circuits. Stripping the success boilerplate line by
1215 # line does not work -- it is wrapped prose, and a line-anchored strip leaves
1216 # the continuation lines behind as fragments. An init that fails does not
1217 # match the pattern and goes through the stages below.
1218 86 $self->register_filter(
1219 parsed_command => {
1220 program => 'tofu',
1221 subcommand => 'init',
1222 },
1223 match_output => [
1224 { pattern => qr(has been successfully initialized!)m, message => 'tofu init: ok' },
1225 ],
1226 strip_lines_matching => [
1227 qr(^\s*$),
1228 qr(^Initializing ),
1229 qr(^- (?:Finding|Installing|Installed|Using|Reusing) ),
1230 ],
1231 max_lines => 50,
1232 );
1233  
1234 # docker-compose (v1 binary): the lifecycle chatter goes, service logs stay
1235 86 $self->register_filter(
1236 parsed_command => {
1237 program => 'docker-compose',
1238 },
1239 filter_stderr => 1,
1240 strip_lines_matching => [
1241 qr(^\s*$),
1242 qr(^Creating (?:network|volume) ),
1243 qr(^Creating \S+ \.\.\. done$),
1244 qr(^Pulling \S+ ),
1245 qr(^Attaching to ),
1246 qr(^\S+ is up-to-date$),
1247 # v2 status table. Left unanchored on purpose: those lines start with
1248 # a check mark the compressor sees as raw bytes, not as a character.
1249 qr((?:Network|Volume|Container)\s+\S+\s+(?:Created|Creating|Started|Starting|Running|Healthy|Waiting|Recreated|Recreate|Stopped|Stopping|Removed|Removing)\s*$),
1250 ],
1251 truncate_lines_at => 200,
1252 max_lines => 100,
1253 );
1254  
1255 # docker compose (v2 subcommand): same output, different invocation
1256 86 $self->register_filter(
1257 parsed_command => {
1258 program => 'docker',
1259 subcommand => 'compose',
1260 },
1261 filter_stderr => 1,
1262 strip_lines_matching => [
1263 qr(^\s*$),
1264 qr(^Creating (?:network|volume) ),
1265 qr(^Creating \S+ \.\.\. done$),
1266 qr(^Pulling \S+ ),
1267 qr(^Attaching to ),
1268 qr(^\S+ is up-to-date$),
1269 qr((?:Network|Volume|Container)\s+\S+\s+(?:Created|Creating|Started|Starting|Running|Healthy|Waiting|Recreated|Recreate|Stopped|Stopping|Removed|Removing)\s*$),
1270 ],
1271 truncate_lines_at => 200,
1272 max_lines => 100,
1273 );
1274  
1275 # cpan: the shell narrates every step it takes
1276 86 $self->register_filter(
1277 parsed_command => {
1278 program => 'cpan',
1279 },
1280 strip_lines_matching => [
1281 qr(^\s*$),
1282 qr(^Reading '),
1283 qr(^Database was generated on ),
1284 qr(^Fetching with ),
1285 qr(^Checksum for ),
1286 qr(^Running (?:make|test|install) for ),
1287 qr(^Configuring \S+ with ),
1288 qr(^CPAN\.pm: Building ),
1289 qr(^\s*Has already been (?:unwrapped|made)),
1290 ],
1291 truncate_lines_at => 200,
1292 max_lines => 60,
1293 );
1294  
1295 # cpm: one DONE line per distribution, then the summary that matters
1296 86 $self->register_filter(
1297 parsed_command => {
1298 program => 'cpm',
1299 },
1300 filter_stderr => 1,
1301 strip_lines_matching => [
1302 qr(^\s*$),
1303 qr(^DONE (?:resolve|fetch|configure|install) ),
1304 ],
1305 max_lines => 30,
1306 on_empty => 'cpm: ok',
1307 );
1308  
1309 # composer install: repository loading, lock file and autoload bookkeeping
1310 86 $self->register_filter(
1311 parsed_command => {
1312 program => 'composer',
1313 subcommand => 'install',
1314 },
1315 strip_lines_matching => [
1316 qr(^\s*$),
1317 qr(^Loading composer repositories),
1318 qr(^(?:Updating|Installing) dependencies),
1319 qr(^Package operations:),
1320 qr(^\s*-\s+(?:Installing|Downloading|Locking|Upgrading|Removing) ),
1321 qr(^Writing lock file),
1322 qr(^Generating (?:optimized )?autoload files),
1323 ],
1324 max_lines => 50,
1325 on_empty => 'composer install: ok',
1326 );
1327  
1328 # brew install: downloads and bottle pouring. "==> Installing dependencies
1329 # for x" is noise, "==> Installing x" is the answer and stays.
1330 86 $self->register_filter(
1331 parsed_command => {
1332 program => 'brew',
1333 subcommand => 'install',
1334 },
1335 filter_stderr => 1,
1336 strip_lines_matching => [
1337 qr(^\s*$),
1338 qr(^==> (?:Downloading|Downloaded|Fetching|Pouring|Verifying)),
1339 qr(^==> Installing dependencies),
1340 qr(^==> (?:Auto-updat|Updat)),
1341 qr(^Already downloaded:),
1342 ],
1343 truncate_lines_at => 200,
1344 max_lines => 50,
1345 );
1346  
1347 # poetry install: one bullet line per package. The bullet itself is a
1348 # non-ASCII glyph the compressor never sees as a character, so the line is
1349 # recognised by its shape -- verb, package, parenthesised version.
1350 86 $self->register_filter(
1351 parsed_command => {
1352 program => 'poetry',
1353 subcommand => 'install',
1354 },
1355 strip_lines_matching => [
1356 qr(^\s*$),
1357 qr(^Installing dependencies from lock file),
1358 qr(^Resolving dependencies),
1359 qr(^Package operations:),
1360 qr(^\s*\S*\s*(?:Installing|Updating|Downgrading|Removing)\s+\S+\s+\(),
1361 qr(^Writing lock file),
1362 ],
1363 max_lines => 50,
1364 on_empty => 'poetry install: ok',
1365 );
1366  
1367 # uv sync: timings and the +/- package list; "Installed N packages" stays
1368 86 $self->register_filter(
1369 parsed_command => {
1370 program => 'uv',
1371 subcommand => 'sync',
1372 },
1373 filter_stderr => 1,
1374 strip_lines_matching => [
1375 qr(^\s*$),
1376 qr(^(?:Resolved|Prepared|Audited) \d+ packages? in ),
1377 qr(^\s*[+~-]\s+\S+==\S+$),
1378 qr(^Using CPython ),
1379 qr(^Creating virtual environment ),
1380 qr(^Downloading \S+ ),
1381 ],
1382 max_lines => 50,
1383 on_empty => 'uv sync: ok',
1384 );
1385  
1386 # systemctl status: keep the unit line and Active:, drop the accounting
1387 $self->register_filter(
1388 parsed_command => {
1389 program => 'systemctl',
1390 subcommand => 'status',
1391 },
1392 filter_stderr => 1,
1393 4181 ▶ transform => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1394 4181 my ($line) = @_;
1395 # The unit header opens with a state glyph in column 0. It is removed
1396 # as "leading run of non-ASCII bytes" rather than by naming it,
1397 # because the pipeline works on raw command bytes, not on decoded
1398 # text -- and because systemd has more than one such glyph. The state
1399 # is not lost with it: the Active: line spells it out.
1400 4181 $line =~ s/^[^\x00-\x7f]+\s*//;
1401 4181 return $line;
1402 },
1403 86 strip_lines_matching => [
1404 qr(^\s*$),
1405 qr(^\s*Loaded:),
1406 qr(^\s*Docs:),
1407 qr(^\s*Main PID:),
1408 qr(^\s*Tasks:),
1409 qr(^\s*Memory:),
1410 qr(^\s*CPU:),
1411 qr(^\s*CGroup:),
1412 # the indented process tree under CGroup:, drawn with box glyphs in a
1413 # UTF-8 locale and with |- / `- everywhere else
1414 qr(^\s{4,}(?:[^\x00-\x7f]|[|`]-)),
1415 ],
1416 truncate_lines_at => 200,
1417 max_lines => 30,
1418 );
1419  
1420 # journalctl: the journal's own bracket lines
1421 86 $self->register_filter(
1422 parsed_command => {
1423 program => 'journalctl',
1424 },
1425 strip_lines_matching => [
1426 qr(^\s*$),
1427 qr(^-- Reboot --),
1428 qr(^-- (?:Logs|Journal) begins? at ),
1429 qr(^-- No entries --),
1430 qr(^-- Boot \S+ --),
1431 ],
1432 truncate_lines_at => 300,
1433 max_lines => 100,
1434 );
1435  
1436 # fail2ban-client: blank lines, max 30
1437 86 $self->register_filter(
1438 parsed_command => {
1439 program => 'fail2ban-client',
1440 },
1441 strip_lines_matching => [qr(^\s*$)],
1442 max_lines => 30,
1443 );
1444  
1445 # ollama run: the output is the model's answer, so nothing is dropped,
1446 # truncated or line-limited here -- only the spinner is removed.
1447 $self->register_filter(
1448 parsed_command => {
1449 program => 'ollama',
1450 subcommand => 'run',
1451 },
1452 strip_ansi => 1,
1453 3808 ▶ transform => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1454 3808 my ($line) = @_;
1455 # Braille spinner frames, U+2800..U+28FF, matched as their UTF-8
1456 # bytes: the pipeline sees raw command output, never decoded text.
1457 # A frame at the start of a line takes its trailing blanks with it;
1458 # elsewhere only the frame goes, so indentation inside the answer --
1459 # a code block, a list -- survives untouched.
1460 3808 $line =~ s/\A(?:\xe2[\xa0-\xa3][\x80-\xbf][ \t]*)+//;
1461 3808 $line =~ s/\xe2[\xa0-\xa3][\x80-\xbf]//g;
1462 3808 return $line;
1463 },
1464 86 );
1465  
1466 # quarto render: a successful render says so in one line, and that line
1467 # is the entire result -- everything before it is progress.
1468 86 $self->register_filter(
1469 parsed_command => {
1470 program => 'quarto',
1471 subcommand => 'render',
1472 },
1473 filter_stderr => 1,
1474 match_output => [
1475 { pattern => qr(^Output created:\s*\S)m, message => 'quarto render: ok (output created)' },
1476 ],
1477 strip_lines_matching => [
1478 qr(^\s*$),
1479 qr(^processing file: ),
1480 qr(^(?:Validating|Resolving) ),
1481 qr(^\s*\|?[.\s]*\|?\s*\d+%\s*$),
1482 ],
1483 truncate_lines_at => 200,
1484 max_lines => 60,
1485 );
1486  
1487 # jj: Jujutsu ends most commands with a hint and a working copy report,
1488 # both on stderr
1489 86 $self->register_filter(
1490 parsed_command => {
1491 program => 'jj',
1492 },
1493 filter_stderr => 1,
1494 strip_lines_matching => [
1495 qr(^\s*$),
1496 qr(^Hint: ),
1497 qr(^Working copy now at: ),
1498 qr(^Parent commit\s*:),
1499 qr(^Rebased \d+ (?:descendant )?commits?),
1500 ],
1501 truncate_lines_at => 200,
1502 max_lines => 30,
1503 );
1504  
1505 # shopify theme push/pull: a long upload log whose only interesting part
1506 # is the tail (result and preview URL). Registered as a regex rather than
1507 # a parsed_command because the deciding word is the third one, and
1508 # _match_parsed_command only knows program and subcommand -- a
1509 # { shopify, theme } filter would also swallow `shopify theme list`,
1510 # whose output is nothing but the answer.
1511 86 $self->register_filter(
1512 command => '^shopify\s+theme\s+(?:push|pull)\b',
1513 filter_stderr => 1,
1514 strip_lines_matching => [qr(^\s*$)],
1515 tail_lines => 5,
1516 );
1517  
1518 86 return;
1519 }
1520  
1521 86 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
1522 86 my $self = shift->SUPER::new(@_);
1523 86 $self->_build_default_filters;
1524 86 return $self;
1525 }
1526  
1527  
1528 969526 ▶sub _filter_matches {
Subroutine _filter_matches: called CC=7 SCAR=19.5
1529 969526 my ($self, $key, $filter, $command, $parsed, $stdout) = @_;
1530  
1531 969526 ▶ if (my $pc = $filter->{parsed_command}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pc = $$filter{'parsed_command'}) { } 798133 171393 969526
elsif (not $command =~ /$key/u) { } 157927 13466 171393
1532 798133 ▶ return 0 unless $self->_match_parsed_command($pc, $parsed);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_match_parsed_command($pc, $parsed) 781685 16448 798133
1533 }
1534 # Fall back to regex matching for legacy filters
1535 elsif ($command !~ /$key/) {
1536 157927 return 0;
1537 }
1538  
1539 # Check output_detect if present - only apply filter if output matches
1540 29914 ▶ if (defined $stdout && (my $detect = $filter->{output_detect})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stdout and my $detect = $$filter{"output_detect"}) 289 29625 29914
Condition100%3/3
defined $stdout and my $detect = $$filter{"output_detect"}
!ll&&!rl&&r
1066818957289
MC/DC100%2/2
defined $stdout and my $detect = $$filter{"output_detect"}
defined $stdout my $detect = $$filter{"output_detect"}
Truth table100%3/3
A and B
A= defined $stdoutB= my $detect = $$filter{"output_detect"}
ABresult
0X0
100
111
1541 289 ▶ return 0 unless grep { /$detect/ } split(/\n/, $stdout);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/$detect/u;} split(/\n/u, $stdout, 0) 245 44 289
1542 }
1543  
1544 29669 return 1;
1545 }
1546  
1547  
1548 118 my @SHAPING_ATTRIBUTES = qw(
1549 filter_stderr strip_ansi match_output transform
1550 strip_lines_matching keep_lines_matching truncate_lines_at
1551 head_lines tail_lines max_lines on_empty
1552 );
1553  
1554 6368 ▶sub _shapes_output {
Subroutine _shapes_output: called CC=5 SCAR=16.1
1555 6368 my ($self, $filter) = @_;
1556  
1557 6368 for my $attribute (@SHAPING_ATTRIBUTES) {
1558 26774 my $value = $filter->{$attribute};
1559 26774 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4733 22041 26774
1560 22041 ▶ return 1 if ref $value eq "ARRAY" ? scalar(@$value) : $value;
Branch: 2 branches
BranchTrueFalseTotal
if ref $value eq "ARRAY" ? scalar @$value : $value 6282 15759 22041
ref $value eq 'ARRAY' ? : 9899 12142 22041
1561 }
1562  
1563 86 return 0;
1564 }
1565  
1566  
1567 20680 ▶sub _match_filter {
Subroutine _match_filter: called CC=9 SCAR=22.2
1568 20680 my ($self, $command, $stdout, $parsed) = @_;
1569  
1570 20680 ▶ $parsed //= $self->_parse_command($command);
Condition66%2/3
$parsed //= $self->_parse_command($command)
l!l&&r!l&&!r
20608720
MC/DC0%0/2
$parsed //= $self->_parse_command($command)
$parsed $self->_parse_command($command)
Truth table66%2/3
A //= B
A= $parsedB= $self->_parse_command($command)
ABresult
1X1
011
000
1571 20680 my $matched_filter;
1572  
1573 20680 for my $key (keys %{$self->filters}) {
1574 948541 my $filter = $self->filters->{$key};
1575 948541 ▶ next unless $filter->{_shapes_output};
Branch: 1 branch
BranchTrueFalseTotal
unless $$filter{"_shapes_output"} 10366 938175 948541
1576 938175 ▶ next unless $self->_filter_matches($key, $filter, $command, $parsed, $stdout);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_filter_matches($key, $filter, $command, $parsed, $stdout) 919174 19001 938175
1577  
1578 # parsed_command filters win over legacy regex filters when both
1579 # match — the parsed form is more specific (program + subcommand +
1580 # flags) and is what users reach for when they want transform-style
1581 # rewrites like `git diff --stat -> "5+2-file"`. Without this
1582 # guard, hash iteration order decides the winner and `git diff
1583 # --stat` would land on the bare `^git\s+diff\b` filter, which
1584 # doesn't have the transform.
1585 19001 ▶ if (!$matched_filter) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $matched_filter) { } 18996 5 19001
elsif ($$filter{'parsed_command'} and not $$matched_filter{'parsed_command'}) { } 5 0 5
1586 18996 $matched_filter = $filter;
1587 }
1588 ▶ elsif ($filter->{parsed_command} && !$matched_filter->{parsed_command}) {
Condition33%1/3
$$filter{"parsed_command"} and not $$matched_filter{"parsed_command"}
!ll&&!rl&&r
005
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
1589 5 $matched_filter = $filter;
1590 }
1591 19001 ▶ last if $matched_filter->{parsed_command};
Branch: 1 branch
BranchTrueFalseTotal
if $$matched_filter{"parsed_command"} 16324 2677 19001
1592 }
1593  
1594 20680 return $matched_filter;
1595 }
1596  
1597  
1598 20608 ▶sub _pipeline {
Subroutine _pipeline: called CC=34 SCAR=36.3
1599 20608 my ($self, $command, $stdout, $stderr) = @_;
1600  
1601 # Normalize so downstream split/join/grep can't trip on undef inputs.
1602 20608 ▶ $stdout //= '';
Condition100%2/2
$stdout //= ""
l!l
193321276
MC/DC100%1/1
$stdout //= ""
$stdout
Truth table100%2/2
A
A= $stdout
Aresult
11
00
1603 20608 ▶ $stderr //= '';
Condition100%2/2
$stderr //= ""
l!l
155185090
MC/DC100%1/1
$stderr //= ""
$stderr
Truth table100%2/2
A
A= $stderr
Aresult
11
00
1604  
1605 # Two independent decisions, and they used to be one lookup (karr #25):
1606 # how is the output compressed, and does the command get rewritten. A
1607 # compound command can call for both -- `cat msg.txt && git commit -m
1608 # "fix"` wants the cat filter AND the trailer -- and with a single winner
1609 # it got whichever one hash order handed it. The output filter is chosen
1610 # among filters that actually shape output; the rewrite is
1611 # transform_command's rule, unchanged and deliberately not copied here.
1612 20608 my $parsed = $self->_parse_command($command);
1613 20608 my $matched_filter = $self->_match_filter($command, $stdout, $parsed);
1614 20608 $command = $self->transform_command($command, $parsed);
1615  
1616 20608 my ($out, $err) = ($stdout, $stderr);
1617  
1618 20608 ▶ if (!$matched_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched_filter) 1683 18925 20608
1619 # No filter, no filtering -- with one exception: the \r collapse still
1620 # runs (karr #22). It used to sit inside the filtered part of the pipeline,
1621 # so a command no filter knows returned here and its progress bar went
1622 # through whole: bun, gradle, deno, mise and every self-written script
1623 # draw bars and have no filter, and a bar is one single line to every
1624 # line-based stage. Collapsing \r overwrites is not filtering, it is
1625 # rendering what the terminal showed -- nothing the user would have
1626 # seen is lost. Everything else about the pass-through stays untouched:
1627 # nothing is stripped, truncated or counted, and output without a \r
1628 # comes back byte for byte.
1629 1683 return ($command, _collapse_cr($out), _collapse_cr($err));
1630 }
1631  
1632 # Filter stderr into stdout if configured
1633 18925 ▶ if ($matched_filter->{filter_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"filter_stderr"}) 3871 15054 18925
1634 3871 ▶ $out .= "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 1920 1951 3871
1635 3871 $err = '';
1636 }
1637  
1638 # Stage 1: Strip ANSI
1639 18925 ▶ if ($matched_filter->{strip_ansi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"strip_ansi"}) 256 18669 18925
1640 256 $out =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1641 256 $err =~ s/\x1b\[[0-9;]*[a-zA-Z]//g;
1642 }
1643  
1644 # Stage 2: Collapse carriage-return overwrites (progress bars). Has to
1645 # run before every line-based stage below -- they all split on /\n/, so a
1646 # bar of thousands of \r-separated states is one single line to them and
1647 # slips through whole. Unconditional, not per filter: a filter that does
1648 # not opt in would reopen the hole (karr #16).
1649 18925 $out = _collapse_cr($out);
1650 18925 $err = _collapse_cr($err);
1651  
1652 # Stage 3: Match output short-circuit
1653 18925 ▶ for my $match (@{$matched_filter->{match_output} // []}) {
Condition50%1/2
$$matched_filter{"match_output"} // []
l!l
189250
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
1654 516 ▶ if ($out =~ /$match->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /$$match{"pattern"}/u) 0 516 516
1655 0 return ($command, $match->{message}, $err);
1656 }
1657 }
1658  
1659 # Stage 4: Transform lines (if transform coderef provided).
1660 # A transform may return undef to drop a line (e.g. git diff --stat's
1661 # summary line) — filter those out so join() doesn't warn.
1662 18925 ▶ if ($matched_filter->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"transform"}) 820 18105 18925
1663 820 $out = join("\n", grep { defined } map { $matched_filter->{transform}->($_) } split(/\n/, $out));
1664 }
1665  
1666 # Stage 5: Strip lines matching
1667 18925 ▶ if (@{$matched_filter->{strip_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"strip_lines_matching"} // [];}) 18407 518 18925
Condition50%1/2
$$matched_filter{"strip_lines_matching"} // []
l!l
189250
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
1668 18407 my @out_lines = split(/\n/, $out);
1669 @out_lines = grep {
1670 18407 my $keep = 1;
1671 288495 for my $pattern (@{$matched_filter->{strip_lines_matching}}) {
1672 1275032 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 6433 1268599 1275032
1673 6433 $keep = 0;
1674 6433 last;
1675 }
1676 }
1677 288495 $keep;
1678 } @out_lines;
1679 18407 $out = join("\n", @out_lines);
1680 }
1681  
1682 # Stage 6: Keep lines matching (if any)
1683 18925 ▶ if (@{$matched_filter->{keep_lines_matching} // []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$$matched_filter{"keep_lines_matching"} // [];}) 0 18925 18925
Condition50%1/2
$$matched_filter{"keep_lines_matching"} // []
l!l
189250
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
1684 0 my @out_lines = split(/\n/, $out);
1685 @out_lines = grep {
1686 0 my $keep = 0;
1687 0 for my $pattern (@{$matched_filter->{keep_lines_matching}}) {
1688 0 ▶ if (/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern/u) 0 0 0
1689 0 $keep = 1;
1690 0 last;
1691 }
1692 }
1693 0 $keep;
1694 } @out_lines;
1695 0 $out = join("\n", @out_lines);
1696 }
1697  
1698 # Stage 7: Truncate lines at N chars
1699 18925 ▶ if ($matched_filter->{truncate_lines_at} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"truncate_lines_at"} > 0) 8579 10346 18925
1700 8579 my $max = $matched_filter->{truncate_lines_at};
1701 8579 ▶ $out = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $out));
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max ? : 1009 132848 133857
1702 8579 ▶ $err = join("\n", map { length $_ > $max ? substr($_, 0, $max) . '...' : $_ } split(/\n/, $err)) if length $err;
Branch: 2 branches
BranchTrueFalseTotal
if length $err 3084 5495 8579
length $_ > $max ? : 0 4624 4624
1703 }
1704  
1705 # Stage 8: Head/Tail lines
1706 18925 ▶ if ($matched_filter->{head_lines} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$matched_filter{'head_lines'} > 0) { } 261 18664 18925
elsif ($$matched_filter{'tail_lines'} > 0) { } 260 18404 18664
1707 261 my @lines = split(/\n/, $out);
1708 261 my $head = $matched_filter->{head_lines};
1709 261 ▶ my $tail = $matched_filter->{tail_lines} // 0;
Condition50%1/2
$$matched_filter{"tail_lines"} // 0
l!l
2610
MC/DC0%0/1
$$matched_filter{"tail_lines"} // 0
$$matched_filter{"tail_lines"}
Truth table50%1/2
A
A= $$matched_filter{"tail_lines"}
Aresult
11
00
1710 261 my $omit = @lines - $head - $tail;
1711 261 ▶ if ($omit > 0 && $tail > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($omit > 0 and $tail > 0) { } 18 243 261
elsif ($omit > 0) { } 1 242 243
Condition100%3/3
$omit > 0 and $tail > 0
!ll&&!rl&&r
242118
MC/DC100%2/2
$omit > 0 and $tail > 0
$omit > 0 $tail > 0
Truth table100%3/3
A and B
A= $omit > 0B= $tail > 0
ABresult
0X0
100
111
1712 18 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...\n" . (join("\n", @lines[-$tail..-1]));
1713 }
1714 elsif ($omit > 0) {
1715 # Marker after the kept lines -- here it is the end that is missing.
1716 # Trailing is the exposed end: stage 9 keeps the FIRST max_lines
1717 # lines, so this marker survives only while max_lines > head_lines.
1718 # t/compress.t guards that config instead of teaching stage 9 to
1719 # recognise markers.
1720 1 $out = (join("\n", @lines[0..$head-1])) . "\n... $omit lines omitted ...";
1721 }
1722 }
1723 elsif ($matched_filter->{tail_lines} > 0) {
1724 # Was the one branch that cut without saying so, on the assumption
1725 # that no filter reaches it -- until `shopify theme push` did, and
1726 # started handing the model its last five lines as if they were the
1727 # whole output (karr #31).
1728 260 my @lines = split(/\n/, $out);
1729 260 my $tail = min($matched_filter->{tail_lines}, scalar @lines);
1730 260 my $omit = @lines - $tail;
1731 # Marker before the kept lines -- it is the beginning that is missing,
1732 # and leading is also the durable end: stage 9 cuts from the back, so
1733 # neither the marker nor the count it carries can be invalidated by
1734 # whatever max_lines does to the tail below.
1735 260 ▶ $out = ($omit > 0 ? "... $omit lines omitted ...\n" : '')
Branch: 1 branch
BranchTrueFalseTotal
$omit > 0 ? : 35 225 260
1736 . join("\n", @lines[-$tail..-1]);
1737 }
1738  
1739 # Stage 9: Max lines
1740 18925 ▶ if ($matched_filter->{max_lines} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$matched_filter{"max_lines"} > 0) 18408 517 18925
1741 18408 my @out_lines = split(/\n/, $out);
1742 18408 ▶ if (@out_lines > $matched_filter->{max_lines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out_lines > $$matched_filter{"max_lines"}) 1210 17198 18408
1743 1210 $out = join("\n", @out_lines[0..$matched_filter->{max_lines}-1]) . "\n... " . (@out_lines - $matched_filter->{max_lines}) . " more lines ...";
1744 }
1745 }
1746  
1747 # Stage 10: On empty
1748 18925 ▶ if (!length trim($out) && $matched_filter->{on_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length trim($out) and $$matched_filter{"on_empty"}) 267 18658 18925
Condition100%3/3
not length trim($out) and $$matched_filter{"on_empty"}
!ll&&!rl&&r
157762882267
MC/DC100%2/2
not length trim($out) and $$matched_filter{"on_empty"}
not length trim($out) $$matched_filter{"on_empty"}
Truth table100%3/3
not(A) and B
A= not length trim($out)B= $$matched_filter{"on_empty"}
ABresult
0X0
100
111
1749 267 $out = $matched_filter->{on_empty};
1750 }
1751  
1752 18925 return ($command, $out, $err);
1753 }
1754  
1755 10404 ▶sub compress {
Subroutine compress: called CC=1 SCAR=0.0
Pod undocumented
1756 10404 my ($self, $command, $stdout, $stderr) = @_;
1757 10404 my (undef, $out, $err) = $self->_pipeline($command, $stdout, $stderr);
1758 10404 return ($out, $err);
1759 }
1760  
1761 10204 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
1762 10204 my ($self, $command, $stdout, $stderr) = @_;
1763 10204 my ($transformed, $out, $err) = $self->_pipeline($command, $stdout, $stderr);
1764 10204 return { command => $transformed, stdout => $out, stderr => $err };
1765 }
1766  
1767 20842 ▶sub transform_command {
Subroutine transform_command: called CC=5 SCAR=16.1
Pod undocumented
1768 20842 my ($self, $command, $parsed) = @_;
1769 20842 ▶ return $command if $ENV{MCP_RUN_COMPRESS_NO_CO_AUTHORED};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MCP_RUN_COMPRESS_NO_CO_AUTHORED"} 3 20839 20842
1770  
1771 # $parsed is an optional shortcut for callers that already parsed the
1772 # command -- _pipeline does, and parsing runs Getopt::Long.
1773 20839 ▶ $parsed //= $self->_parse_command($command);
Condition66%2/3
$parsed //= $self->_parse_command($command)
l!l&&r!l&&!r
206082310
MC/DC0%0/2
$parsed //= $self->_parse_command($command)
$parsed $self->_parse_command($command)
Truth table66%2/3
A //= B
A= $parsedB= $self->_parse_command($command)
ABresult
1X1
011
000
1774  
1775 # Only the filters that rewrite commands are candidates, and the winner is
1776 # the first of them that matches. sort: with more than one such filter,
1777 # unsorted keys would pick the winner by Perl's per-process hash order --
1778 # the bug that #25 was in the output-filter selection. Skipping the rest
1779 # by hash lookup instead of by regex also keeps this cheap enough to call
1780 # from _pipeline on every single run.
1781 20839 for my $key (sort grep { $self->filters->{$_}{command_transform} } keys %{$self->filters}) {
1782 20839 my $filter = $self->filters->{$key};
1783  
1784 # undef stdout: there is no output here, so output_detect is skipped --
1785 # this rewrites the command, it does not filter anything. In the hook's
1786 # PreToolUse path the command has not even run yet.
1787 20839 ▶ next unless $self->_filter_matches($key, $filter, $command, $parsed, undef);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_filter_matches($key, $filter, $command, $parsed, undef) 10317 10522 20839
1788  
1789 10522 $command = $filter->{command_transform}->($command);
1790 10522 last; # Only apply first matching command_transform
1791 }
1792  
1793 20839 return $command;
1794 }
1795  
1796 118 1;
1797  
1798 __END__
↑ Summary blib/lib/MCP/Run/Bash.pm »