blib/lib/Langertha/Raider/CLI/Sessions.pm

filter: Statementstmt 99.0% 102 / 103 Branchbran 83.3% 40 / 48 Conditioncond 50.0% 16 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 15 / 15 Podpod 100% 8 / 8 totaltotal 81.5% 181 / 222 CC 47 SCAR 28.8
CC
47
Cov
86%
show
33.8
notes
7.3
list
7.1
CRAP 17.8
« blib/lib/Langertha/Raider/CLI/Output.pm ↑ Summary blib/lib/Langertha/Raider.pm »
1 package Langertha::Raider::CLI::Sessions;
2 # ABSTRACT: Internal session subcommands of the raider CLI: list, show, resume, fork, rm
3 55 our $VERSION = '0.503';
4 55 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 55 has app => (
10 is => 'ro',
11 isa => 'Langertha::Raider::Application',
12 required => 1,
13 );
14  
15  
16 18 ▶sub store { $_[0]->app->session_store }
Subroutine store: called CC=1 SCAR=0.0
Pod documented
17  
18 55 has output => (
19 is => 'ro',
20 isa => 'Langertha::Raider::CLI::Output',
21 required => 1,
22 );
23  
24 has _json => (
25 is => 'ro',
26 lazy => 1,
27 4 ▶ default => sub { JSON::MaybeXS->new(canonical => 1, allow_nonref => 1, allow_blessed => 1, convert_blessed => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 55 );
29  
30  
31 8 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.1
Pod documented
32 8 my ( $self, $id ) = @_;
33 8 my $journal = $self->store->read($id);
34 8 my $runs = $journal->runs;
35 8 my ( $first ) = grep { defined $_->{prompt} } @$runs;
36 8 my ( $date, $time ) = $id =~ /\A(\d{8})-(\d{6})/;
37 return {
38 id => $id,
39 path => ''.$journal->path,
40 created => join('-', unpack 'A4 A2 A2', $date).'T'.join(':', unpack 'A2 A2 A2', $time).'Z',
41 runs => scalar @$runs,
42 status => @$runs ? $runs->[-1]{status} : 'empty',
43 prompt => $first ? $first->{prompt} : undef,
44 8 ▶ damaged => scalar @{ $journal->damaged },
Branch: 2 branches
BranchTrueFalseTotal
@$runs ? : 8 0 8
$first ? : 8 0 8
45 };
46 }
47  
48  
49 3 ▶sub list {
Subroutine list: called CC=7 SCAR=19.7
Pod documented
50 3 my ( $self, $machine ) = @_;
51 3 my @summaries = map { $self->summary($_) } $self->store->ids;
52 3 ▶ return $machine->write({ version => $machine->version, sessions => \@summaries }) if $machine;
Branch: 1 branch
BranchTrueFalseTotal
if $machine 1 2 3
53 2 my $out = $self->output;
54 2 ▶ unless (@summaries) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@summaries) 1 1 2
55 1 $out->say_meta('no sessions in '.$self->store->dir);
56 1 return;
57 }
58 1 for my $s (@summaries) {
59 $out->emit($out->c(title => $s->{id}), ' ',
60 sprintf('%3d run%s %-11s', $s->{runs}, $s->{runs} == 1 ? ' ' : 's', $s->{status}), ' ',
61 $out->c(meta => $self->_short($s->{prompt} // '', 60)),
62 2 ▶ $s->{damaged} ? $out->c(warn => ' ('.$s->{damaged}.' damaged)') : '', "\n");
Branch: 2 branches
BranchTrueFalseTotal
$$s{'runs'} == 1 ? : 2 0 2
$$s{'damaged'} ? : 0 2 2
Condition50%1/2
$$s{"prompt"} // ""
l!l
20
MC/DC0%0/1
$$s{"prompt"} // ""
$$s{"prompt"}
Truth table50%1/2
A
A= $$s{"prompt"}
Aresult
11
00
63 }
64 1 return;
65 }
66  
67  
68 6 ▶sub show {
Subroutine show: called CC=28 SCAR=35.2
Pod documented
69 6 my ( $self, $id, $machine ) = @_;
70 6 my $journal = $self->store->read($id);
71 6 ▶ if ($machine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($machine) 2 4 6
72 return $machine->write({
73 version => $machine->version,
74 id => $id,
75 path => ''.$journal->path,
76 events => $journal->events,
77 damaged => $journal->damaged,
78 2 runs => [ map { { run => $_->{run}, status => $_->{status} } } @{ $journal->runs } ],
79 2 unknown => [ map { { run => $_->{run}, call => $_->{call}, name => $_->{name} } } @{ $journal->unknown_calls } ],
80 });
81 }
82 4 my $out = $self->output;
83 4 ▶ my $line = sub { $out->emit($out->c(meta => sprintf('%-11s ', $_[0])), @_[ 1 .. $#_ ], "\n") };
Subroutine __ANON__: called CC=1 SCAR=0.0
84 4 $line->('session', $out->c(title => $id));
85 4 $line->('file', $journal->path);
86 4 ▶ if (my $c = $journal->created) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $journal->created) 4 0 4
87 24 ▶ $line->('created', join ' ', grep { defined }
Condition50%3/6
$$c{"principal"} // "?"
l!l
40
$$c{"root"} // "?"
l!l
40
$$c{"raider"} // "?"
l!l
40
MC/DC0%0/1
$$c{"principal"} // "?"
$$c{"principal"}
MC/DC0%0/1
$$c{"root"} // "?"
$$c{"root"}
MC/DC0%0/1
$$c{"raider"} // "?"
$$c{"raider"}
Truth table50%1/2
A
A= $$c{"principal"}
Aresult
11
00
Truth table50%1/2
A
A= $$c{"root"}
Aresult
11
00
Truth table50%1/2
A
A= $$c{"raider"}
Aresult
11
00
88 $self->summary($id)->{created}, 'by', $c->{principal} // '?', 'in', $c->{root} // '?',
89 4 '(raider '.($c->{raider} // '?').')');
90 4 ▶ $line->('forked from', $c->{forked_from}) if defined $c->{forked_from};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$c{"forked_from"} 1 3 4
91 }
92 4 for my $e (@{ $journal->events }) {
93 45 ▶ my $run = defined $e->{run} ? $e->{run}.' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $$e{'run'} ? : 39 6 45
94 45 my $type = $e->{type};
95 45 ▶ if ($type eq 'run.started') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'run.started') { } 6 39 45
elsif ($type eq 'message') { } 13 26 39
elsif ($type eq 'tool.call') { } 9 17 26
elsif ($type eq 'tool.result') { } 8 9 17
elsif ($type eq 'run.finished') { } 5 4 9
96 6 $line->($run.'started', join ' ', grep { defined } $e->{engine}, $e->{model});
97 }
98 elsif ($type eq 'message') {
99 $line->($run.($e->{role} // '?'), $out->c(($e->{role} // '') eq 'assistant' ? 'agent' : 'title',
100 13 ▶ $self->_indent($e->{content} // '')));
Branch: 1 branch
BranchTrueFalseTotal
($$e{'role'} // '') eq 'assistant' ? : 6 7 13
Condition50%3/6
$$e{"role"} // "?"
l!l
130
$$e{"role"} // ""
l!l
130
$$e{"content"} // ""
l!l
130
MC/DC0%0/1
$$e{"role"} // "?"
$$e{"role"}
MC/DC0%0/1
$$e{"role"} // ""
$$e{"role"}
MC/DC0%0/1
$$e{"content"} // ""
$$e{"content"}
Truth table50%1/2
A
A= $$e{"role"}
Aresult
11
00
Truth table50%1/2
A
A= $$e{"role"}
Aresult
11
00
Truth table50%1/2
A
A= $$e{"content"}
Aresult
11
00
101 }
102 elsif ($type eq 'tool.call') {
103 $line->($run.'tool', ($e->{call} // '?').' ', $out->c(title => $e->{name} // '?'), ' ',
104 9 ▶ $self->_short($self->_json->encode($e->{arguments} // {}), 200));
Condition50%3/6
$$e{"call"} // "?"
l!l
90
$$e{"name"} // "?"
l!l
90
$$e{"arguments"} // {}
l!l
90
MC/DC0%0/1
$$e{"call"} // "?"
$$e{"call"}
MC/DC0%0/1
$$e{"name"} // "?"
$$e{"name"}
MC/DC0%0/1
$$e{"arguments"} // {}
$$e{"arguments"}
Truth table50%1/2
A
A= $$e{"call"}
Aresult
11
00
Truth table50%1/2
A
A= $$e{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $$e{"arguments"}
Aresult
11
00
105 }
106 elsif ($type eq 'tool.result') {
107 8 ▶ my $content = $e->{content} // '';
Condition50%1/2
$$e{"content"} // ""
l!l
80
MC/DC0%0/1
$$e{"content"} // ""
$$e{"content"}
Truth table50%1/2
A
A= $$e{"content"}
Aresult
11
00
108 8 ▶ $line->($run.'result', ($e->{call} // '?').' '.($e->{status} // '?').', '.length($content).' chars: ',
Condition50%2/4
$$e{"call"} // "?"
l!l
80
$$e{"status"} // "?"
l!l
80
MC/DC0%0/1
$$e{"call"} // "?"
$$e{"call"}
MC/DC0%0/1
$$e{"status"} // "?"
$$e{"status"}
Truth table50%1/2
A
A= $$e{"call"}
Aresult
11
00
Truth table50%1/2
A
A= $$e{"status"}
Aresult
11
00
109 $out->c(meta => $self->_short($content, 100)));
110 }
111 elsif ($type eq 'run.finished') {
112 20 ▶ $line->($run.'finished', join ', ', grep { defined } $e->{status},
Branch: 3 branches
BranchTrueFalseTotal
defined $$e{'elapsed'} ? : 4 1 5
defined $$e{'signal'} ? : 0 5 5
defined $$e{'error'} ? : 0 5 5
113 defined $e->{elapsed} ? $e->{elapsed}.'s' : undef,
114 defined $e->{signal} ? 'SIG'.$e->{signal} : undef,
115 5 defined $e->{error} ? 'error: '.$self->_short($e->{error}, 200) : undef);
116 }
117 }
118 4 $out->emit($out->c(warn => 'note: '.$_), "\n") for $self->notes($journal);
119 4 return;
120 }
121  
122  
123 13 ▶sub notes {
Subroutine notes: called CC=7 SCAR=19.9
Pod documented
124 13 my ( $self, $journal ) = @_;
125 13 my @notes;
126 13 push @notes, 'line '.$_.' of the journal is damaged and was skipped' for @{ $journal->damaged };
127 push @notes, 'run '.$_->{run}.' has no end: interrupted'
128 13 for grep { !$_->{finished} } @{ $journal->runs };
129 push @notes, 'tool call '.($_->{name} // '?').' ('.($_->{run} // '?').' '.($_->{call} // '?')
130 .') has no result: its outcome is unknown, it is not run again'
131 13 ▶ for @{ $journal->unknown_calls };
Condition50%3/6
$$_{"name"} // "?"
l!l
20
$$_{"run"} // "?"
l!l
20
$$_{"call"} // "?"
l!l
20
MC/DC0%0/1
$$_{"name"} // "?"
$$_{"name"}
MC/DC0%0/1
$$_{"run"} // "?"
$$_{"run"}
MC/DC0%0/1
$$_{"call"} // "?"
$$_{"call"}
Truth table50%1/2
A
A= $$_{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $$_{"run"}
Aresult
11
00
Truth table50%1/2
A
A= $$_{"call"}
Aresult
11
00
132 13 return @notes;
133 }
134  
135  
136 9 ▶sub restore {
Subroutine restore: called CC=2 SCAR=6.9
Pod documented
137 9 my ( $self, $session ) = @_;
138 9 my $journal = $self->app->replay_session($session);
139 9 my $history = $journal->history_messages;
140 9 my $runs = scalar @{ $journal->runs };
141 9 ▶ return ( 'resumed session '.$session->id.': '.$runs.' run'.($runs == 1 ? '' : 's').', '
Branch: 1 branch
BranchTrueFalseTotal
$runs == 1 ? : 4 5 9
142 .scalar(@$history).' messages in the history', $self->notes($journal) );
143 }
144  
145  
146 2 ▶sub fork_session {
Subroutine fork_session: called CC=3 SCAR=11.0
Pod documented
147 2 my ( $self, $id, $machine ) = @_;
148 2 my %doc = %{ $self->app->fork_session($id) };
149 2 ▶ if ($machine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($machine) { } 1 1 2
150 1 $machine->write({ version => $machine->version, %doc });
151 }
152 else {
153 $self->output->emit('forked session '.$id.' as '.$doc{id}.' ('.$doc{path}.'): '.$doc{messages}
154 1 ▶ .' message'.($doc{messages} == 1 ? '' : 's')."\n");
Branch: 1 branch
BranchTrueFalseTotal
$doc{'messages'} == 1 ? : 0 1 1
155 }
156 2 return $doc{id};
157 }
158  
159  
160 3 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
161 3 my ( $self, $id, $machine ) = @_;
162 3 my $path = $self->app->remove_session($id);
163 2 ▶ return $machine->write({ version => $machine->version, id => $id, path => $path, removed => JSON::MaybeXS->true })
Branch: 1 branch
BranchTrueFalseTotal
if $machine 1 1 2
164 if $machine;
165 1 $self->output->emit('removed session '.$id."\n");
166 1 return;
167 }
168  
169 19 ▶sub _short {
Subroutine _short: called CC=2 SCAR=6.9
170 19 my ( $self, $text, $max ) = @_;
171 19 $text =~ s/\s+/ /g;
172 19 $text =~ s/\A | \z//g;
173 19 ▶ return length $text > $max ? substr($text, 0, $max - 3).'...' : $text;
Branch: 1 branch
BranchTrueFalseTotal
length $text > $max ? : 4 15 19
174 }
175  
176 13 ▶sub _indent {
Subroutine _indent: called CC=1 SCAR=0.0
177 13 my ( $self, $text ) = @_;
178 13 $text =~ s/\n/\n /g;
179 13 return $text;
180 }
181  
182 55 __PACKAGE__->meta->make_immutable;
183  
184 55 1;
185  
186 __END__
187  
188 =pod
189  
190 =encoding UTF-8
191  
192 =head1 NAME
193  
194 Langertha::Raider::CLI::Sessions - Internal session subcommands of the raider CLI: list, show, resume, fork, rm
195  
196 =head1 VERSION
197  
198 version 0.503
199  
200 =head1 SYNOPSIS
201  
202 # Internal to Langertha-Raider -- no API promise.
203 my $sessions = Langertha::Raider::CLI::Sessions->new(app => $app, output => $out);
204 $sessions->list; # raider session list
205 $sessions->show($id); # raider session show ID
206 $sessions->show($id, $machine); # raider session show ID --json
207 my @notes = $sessions->restore($session); # --session ID, --continue
208 my $new = $sessions->fork_session($id); # raider session fork ID
209 $sessions->remove($id); # raider session rm ID
210  
211 =head1 DESCRIPTION
212  
213 B<Internal module.> Its interface may change without notice.
214  
215 What F<raider> does with the session journals of a project
216 (L<Langertha::Raider::SessionStore>, ADR 0015) beyond writing them: listing
217 them, showing one, replaying one into a raider to resume it, forking one
218 and removing one -- the changes through the
219 L<Langertha::Raider::Application>, this class presents them.
220  
221 =head2 app
222  
223 The L<Langertha::Raider::Application> of the project. Required.
224  
225 =head2 output
226  
227 The L<Langertha::Raider::CLI::Output> to print to. Required.
228  
229 =head2 store
230  
231 The L<Langertha::Raider::Application/session_store> of L</app>.
232  
233 =head2 summary
234  
235 my $s = $sessions->summary($id);
236  
237 One session in short: C<id>, C<path>, C<created> (ISO 8601, UTC, from the
238 id), C<runs> (how many), C<status> (of the last run, C<empty> without
239 one), C<prompt> (the first user input, or C<undef>) and C<damaged> (how
240 many lines could not be read).
241  
242 =head2 list
243  
244 $sessions->list; # human
245 $sessions->list($machine); # { version => 1, sessions => [ summary, ... ] }
246  
247 The sessions of the project, newest first, one L</summary> each.
248  
249 =head2 show
250  
251 $sessions->show($id); # human
252 $sessions->show($id, $machine); # the journal as one document
253  
254 Prints one session: every event in order, then the L</notes> of its
255 journal. With a machine, one document: C<version>, C<id>, C<path>,
256 C<events> (as in the file), C<damaged> (line numbers), C<runs> (C<run>
257 and C<status> each) and C<unknown> (the calls without result: C<run>,
258 C<call>, C<name>).
259  
260 =head2 notes
261  
262 my @notes = $sessions->notes($journal);
263  
264 What the crash rules of ADR 0015 found in a journal, one line each: the
265 lines that could not be read, the runs without C<run.finished> (they count
266 as C<interrupted>) and the tool calls without result (C<unknown>, never run
267 again).
268  
269 =head2 restore
270  
271 my @notes = $sessions->restore($session);
272  
273 Replays the journal the L<Langertha::Raider::Session> was opened with into
274 the raider of L</app> (L<Langertha::Raider::Application/replay_session>).
275 Returns a line saying what was resumed, then the L</notes>.
276  
277 =head2 fork_session
278  
279 $sessions->fork_session($id); # human
280 $sessions->fork_session($id, $machine); # { version, id, path, forked_from, messages }
281  
282 C<raider session fork ID>: a new session of the same project that takes
283 over the working history of the original
284 (L<Langertha::Raider::Application/fork_session>). From there it has a
285 journal of its own; the original is never changed, and the workspace
286 stays the same. Returns the new id.
287  
288 =head2 remove
289  
290 $sessions->remove($id); # human
291 $sessions->remove($id, $machine); # { version, id, path, removed }
292  
293 C<raider session rm ID>: deletes the session's journal and lock file
294 through L<Langertha::Raider::Application/remove_session>, which croaks
295 C<session ID is in use> while another raider has it open.
296  
297 =head1 SEE ALSO
298  
299 =over
300  
301 =item * L<Langertha::Raider::SessionStore>
302  
303 =item * L<Langertha::Raider::CLI::Main>
304  
305 =back
306  
307 =head1 SUPPORT
308  
309 =head2 Issues
310  
311 Please report bugs and feature requests on GitHub at
312 L<https://github.com/Getty/langertha-raider/issues>.
313  
314 =head2 IRC
315  
316 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
317  
318 =head1 CONTRIBUTING
319  
320 Contributions are welcome! Please fork the repository and submit a pull request.
321  
322 =head1 AUTHOR
323  
324 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
325  
326 =head1 COPYRIGHT AND LICENSE
327  
328 This software is copyright (c) 2026 by Torsten Raudssus.
329  
330 This is free software; you can redistribute it and/or modify it under
331 the same terms as the Perl 5 programming language system itself.
332  
333 =cut
« blib/lib/Langertha/Raider/CLI/Output.pm ↑ Summary blib/lib/Langertha/Raider.pm »