blib/lib/Langertha/Raider/Plugin/Trace.pm

filter: Statementstmt 78.3% 105 / 134 Branchbran 60.8% 28 / 46 Conditioncond 57.1% 8 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 90.4% 19 / 21 Podpod 100% 4 / 4 totaltotal 73.1% 166 / 227 CC 31 SCAR 19.7
CC
31
Cov
73%
_spinner_enabled
17.1
plugin_after_tool_call
13.2
_start_spinner
8.7
CRAP 7.2
« blib/lib/Langertha/Raider/Config.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Fetch.pm »
1 package Langertha::Raider::Plugin::Trace;
2 1 our $VERSION = '0.503';
3 # ABSTRACT: Live ANSI-colored progress output for a running Langertha::Raider raid
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::ANSIColor qw( colored );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Async::Timer::Periodic;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Langertha::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 extends 'Langertha::Plugin';
14  
15 1 my @SPINNER_FRAMES = ('⠋','⠙','⠹','⠸','⠼','⠴','⠦','⠧','⠇','⠏');
16  
17  
18 has color => (
19 is => 'ro',
20 isa => 'Bool',
21 0 ▶ default => sub { -t STDOUT ? 1 : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 0 0
22 1 );
23  
24  
25 has out => (
26 is => 'ro',
27 10 ▶ default => sub { \*STDOUT },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 );
29  
30  
31 1 has max_value_length => (
32 is => 'ro',
33 isa => 'Int',
34 default => 80,
35 );
36  
37  
38 has token_stats => (
39 is => 'rw',
40 isa => 'HashRef',
41 10 ▶ default => sub { { prompt => 0, completion => 0, total => 0, calls => 0 } },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 );
43  
44  
45 1 has loop => (
46 is => 'ro',
47 predicate => 'has_loop',
48 );
49  
50 1 has _spinner_timer => ( is => 'rw', clearer => '_clear_spinner_timer' );
51 1 has _spinner_idx => ( is => 'rw', isa => 'Int', default => 0 );
52 1 has _spinner_active=> ( is => 'rw', isa => 'Bool', default => 0 );
53  
54 2 ▶sub _spinner_enabled {
Subroutine _spinner_enabled: called CC=5 SCAR=28.4
55 2 my ($self) = @_;
56 2 ▶ return 0 unless $self->has_loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_loop 2 0 2
57 0 ▶ return 0 unless $self->color;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->color 0 0 0
58 0 ▶ return 0 if $ENV{ANSI_COLORS_DISABLED};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"ANSI_COLORS_DISABLED"} 0 0 0
59 0 ▶ return 0 unless -t $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless -t $self->out 0 0 0
60 0 return 1;
61 }
62  
63 1 ▶sub _start_spinner {
Subroutine _start_spinner: called CC=3 SCAR=21.6
64 1 my ($self) = @_;
65 1 ▶ return unless $self->_spinner_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_spinner_enabled 1 0 1
66 0 ▶ return if $self->_spinner_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_spinner_active 0 0 0
67 0 $self->_spinner_active(1);
68 0 $self->_spinner_idx(0);
69  
70 0 ▶ my $tick = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my $idx = $self->_spinner_idx;
72 0 my $frame = $SPINNER_FRAMES[$idx % @SPINNER_FRAMES];
73 0 $self->_spinner_idx($idx + 1);
74 0 local $| = 1;
75 0 print { $self->out } "\r", $self->_c(accent => $frame), ' ', $self->_c(iter => 'thinking…'), "\033[K";
76 0 };
77 0 $tick->();
78  
79 0 my $timer = IO::Async::Timer::Periodic->new(
80 interval => 0.1,
81 first_interval => 0.1,
82 on_tick => $tick,
83 );
84 0 $self->loop->add($timer);
85 0 $timer->start;
86 0 $self->_spinner_timer($timer);
87 }
88  
89 9 ▶sub _stop_spinner {
Subroutine _stop_spinner: called CC=3 SCAR=20.3
90 9 my ($self) = @_;
91 9 ▶ return unless $self->_spinner_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_spinner_active 9 0 9
92 0 $self->_spinner_active(0);
93 0 ▶ if (my $timer = $self->_spinner_timer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = $self->_spinner_timer) 0 0 0
94 0 $timer->stop;
95 0 eval { $self->loop->remove($timer) };
96 0 $self->_clear_spinner_timer;
97 }
98 0 local $| = 1;
99 0 print { $self->out } "\r\033[K";
100 }
101  
102 1 my %C = (
103 iter => 'bright_black',
104 tool => 'blue',
105 args => 'bright_black',
106 ok => 'bright_black',
107 err => 'red',
108 text => 'bright_black',
109 accent => 'yellow',
110 );
111  
112 10 ▶sub _c {
Subroutine _c: called CC=3 SCAR=11.0
113 10 my ($self, $key, @text) = @_;
114 10 my $text = join '', @text;
115 10 ▶ return $text unless $self->color && !$ENV{ANSI_COLORS_DISABLED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->color and not $ENV{"ANSI_COLORS_DISABLED"} 9 1 10
Condition100%3/3
$self->color and not $ENV{"ANSI_COLORS_DISABLED"}
!ll&&!rl&&r
811
MC/DC100%2/2
$self->color and not $ENV{"ANSI_COLORS_DISABLED"}
$self->color not $ENV{"ANSI_COLORS_DISABLED"}
Truth table100%3/3
A and not(B)
A= $self->colorB= not $ENV{"ANSI_COLORS_DISABLED"}
ABresult
0X0
100
111
116 1 return colored([$C{$key}], $text);
117 }
118  
119 11 ▶sub _truncate {
Subroutine _truncate: called CC=3 SCAR=11.0
120 11 my ($self, $s) = @_;
121 11 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 10 11
122 10 $s =~ s/\s+/ /g;
123 10 my $lim = $self->max_value_length;
124 10 ▶ return length($s) > $lim ? substr($s, 0, $lim - 1) . '…' : $s;
Branch: 1 branch
BranchTrueFalseTotal
length $s > $lim ? : 1 9 10
125 }
126  
127 6 ▶sub _summarize_args {
Subroutine _summarize_args: called CC=5 SCAR=16.1
128 6 my ($self, $input) = @_;
129 6 ▶ return '' unless ref $input eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 1 5 6
130 5 my @parts;
131 5 for my $k (sort keys %$input) {
132 4 my $v = $input->{$k};
133 4 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 1 3 4
134 1 ▶ $v = eval { JSON::MaybeXS->new(utf8 => 0, canonical => 1)->encode($v) } // '?';
Condition50%1/2
eval { // "?"
l!l
10
MC/DC0%0/1
eval { do { "JSON::MaybeXS"->new("utf8", 0, "canonical", 1)->encode($v) } } // "?"
eval { do { "JSON::MaybeXS"->new("utf8", 0, "canonical", 1)->encode($v) } }
Truth table50%1/2
A
A= eval { do { "JSON::MaybeXS"->new("utf8", 0, "canonical", 1)->encode($v) } }
Aresult
11
00
135 }
136 4 push @parts, "$k=" . $self->_truncate($v);
137 }
138 5 return join(' ', @parts);
139 }
140  
141 1 ▶async sub plugin_before_llm_call {
Subroutine plugin_before_llm_call: called CC=1 SCAR=0.0
Pod documented
142 1 my ($self, $conversation, $iteration) = @_;
143 1 $self->_start_spinner;
144 1 return $conversation;
145 1 }
146  
147 6 ▶async sub plugin_after_llm_response {
Subroutine plugin_after_llm_response: called CC=2 SCAR=6.9
Pod documented
148 6 my ($self, $data, $iteration) = @_;
149 6 $self->_stop_spinner;
150  
151 6 my $usage = Langertha::Usage->from_raw($data);
152 6 ▶ if ($usage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usage) 5 1 6
153 5 my $s = $self->token_stats;
154 5 $s->{prompt} += $usage->input_tokens;
155 5 $s->{completion} += $usage->output_tokens;
156 5 $s->{total} += $usage->total_tokens;
157 5 $s->{calls}++;
158 }
159  
160 6 return $data;
161 1 }
162  
163 3 ▶async sub plugin_before_tool_call {
Subroutine plugin_before_tool_call: called CC=2 SCAR=6.9
Pod documented
164 3 my ($self, $name, $input) = @_;
165 3 $self->_stop_spinner;
166 3 my $args = $self->_summarize_args($input);
167 3 ▶ print { $self->out }
Branch: 1 branch
BranchTrueFalseTotal
length $args ? : 1 2 3
168 $self->_c(tool => "> $name"),
169 (length $args ? ' ' . $self->_c(args => $args) : ''),
170 "\n";
171 3 return ($name, $input);
172 1 }
173  
174 3 ▶async sub plugin_after_tool_call {
Subroutine plugin_after_tool_call: called CC=12 SCAR=25.8
Pod documented
175 3 my ($self, $name, $input, $result) = @_;
176  
177 3 my $is_err = 0;
178 3 my $text = '';
179 3 ▶ if (ref $result eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'HASH') { } 2 1 3
elsif (not ref $result) { } 1 0 1
180 2 ▶ $is_err = $result->{isError} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$result{'isError'} ? : 1 1 2
181 2 ▶ if (ref $result->{content} eq 'ARRAY' && ref $result->{content}[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$result{"content"} eq "ARRAY" and ref $$result{"content"}[0] eq "HASH") 2 0 2
Condition33%1/3
ref $$result{"content"} eq "ARRAY" and ref $$result{"content"}[0] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $$result{"content"} eq "ARRAY" and ref $$result{"content"}[0] eq "HASH"
ref $$result{"content"} eq "ARRAY" ref $$result{"content"}[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $$result{"content"} eq "ARRAY"B= ref $$result{"content"}[0] eq "HASH"
ABresult
0X0
100
111
182 2 ▶ $text = $result->{content}[0]{text} // '';
Condition50%1/2
$$result{"content"}[0]{"text"} // ""
l!l
20
MC/DC0%0/1
$$result{"content"}[0]{"text"} // ""
$$result{"content"}[0]{"text"}
Truth table50%1/2
A
A= $$result{"content"}[0]{"text"}
Aresult
11
00
183 }
184 }
185 elsif (!ref $result) {
186 1 ▶ $text = $result // '';
Condition50%1/2
$result // ""
l!l
10
MC/DC0%0/1
$result // ""
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
187 }
188  
189 3 my $bytes = length $text;
190 3 ▶ my $first = (split /\n/, $text, 2)[0] // '';
Condition50%1/2
(split(/\n/, $text, 2))[0] // ""
l!l
30
MC/DC0%0/1
(split(/\n/, $text, 2))[0] // ""
(split(/\n/, $text, 2))[0]
Truth table50%1/2
A
A= (split(/\n/, $text, 2))[0]
Aresult
11
00
191 3 $first =~ s/\s+$//;
192  
193 3 ▶ my $key = $is_err ? 'err' : 'ok';
Branch: 1 branch
BranchTrueFalseTotal
$is_err ? : 1 2 3
194 3 ▶ my $lead = $is_err ? '! ' : '. ';
Branch: 1 branch
BranchTrueFalseTotal
$is_err ? : 1 2 3
195 3 ▶ print { $self->out }
Branch: 1 branch
BranchTrueFalseTotal
length $first ? : 3 0 3
196 $self->_c($key => $lead . "${bytes}b"),
197 (length $first ? ' ' . $self->_c(text => $self->_truncate($first)) : ''),
198 "\n";
199  
200 3 return $result;
201 1 }
202  
203 1 __PACKAGE__->meta->make_immutable;
204  
205 1 1;
206  
207 __END__
208  
209 =pod
210  
211 =encoding UTF-8
212  
213 =head1 NAME
214  
215 Langertha::Raider::Plugin::Trace - Live ANSI-colored progress output for a running Langertha::Raider raid
216  
217 =head1 VERSION
218  
219 version 0.503
220  
221 =head1 SYNOPSIS
222  
223 my $raider = Langertha::Raider->new(
224 engine => $engine,
225 plugins => ['+Langertha::Raider::Plugin::Trace'],
226 );
227  
228 =head1 DESCRIPTION
229  
230 Streaming trace plugin used by L<Langertha::Raider::CLI> to show live progress during a
231 raid: per-iteration markers, each tool call with a short argument summary, and
232 the tool result (ok / error / length). Palette is blue-dominant with yellow
233 accents to match the rest of the CLI.
234  
235 Set C<ANSI_COLORS_DISABLED=1> or construct with C<color =E<gt> 0> to render
236 without ANSI sequences.
237  
238 A "thinking..." spinner runs while a model request is in flight when a
239 L</loop> is given, colors are on and L</out> is a terminal.
240  
241 =head2 color
242  
243 Whether to emit ANSI colors. Defaults to true when STDOUT is a terminal.
244  
245 =head2 out
246  
247 Filehandle the trace is printed to. Defaults to C<STDOUT>; the raider CLI
248 passes C<STDERR> when stdout carries machine output.
249  
250 =head2 max_value_length
251  
252 Maximum characters shown per argument value in tool-call summaries. Longer
253 strings are truncated with an ellipsis. Defaults to 80.
254  
255 =head2 token_stats
256  
257 Running cumulative hashref: C<{ prompt, completion, total, calls }>.
258 Updated after every LLM response; read via the C<token_stats> accessor or
259 through L<Langertha::Raider::CLI/token_stats>.
260  
261 =head2 loop
262  
263 Optional L<IO::Async::Loop> used to drive a small "thinking…" spinner while
264 the LLM HTTP call is in flight. When absent, no spinner is shown.
265  
266 =head1 ENVIRONMENT
267  
268 =head2 ANSI_COLORS_DISABLED
269  
270 Set, the trace has no colors and no spinner, whatever L</color> says.
271  
272 =head1 SEE ALSO
273  
274 =over
275  
276 =item * L<Langertha::Raider::CLI>
277  
278 =item * L<Langertha::Plugin>
279  
280 =back
281  
282 =head1 SUPPORT
283  
284 =head2 Issues
285  
286 Please report bugs and feature requests on GitHub at
287 L<https://github.com/Getty/langertha-raider/issues>.
288  
289 =head2 IRC
290  
291 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
292  
293 =head1 CONTRIBUTING
294  
295 Contributions are welcome! Please fork the repository and submit a pull request.
296  
297 =head1 AUTHOR
298  
299 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 This software is copyright (c) 2026 by Torsten Raudssus.
304  
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307  
308 =cut
« blib/lib/Langertha/Raider/Config.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Fetch.pm »