blib/lib/Langertha/Raider/Session/Journal.pm

filter: Statementstmt 98.0% 101 / 103 Branchbran 90.0% 36 / 40 Conditioncond 64.7% 33 / 51 MC/DCmcdc 35.7% 10 / 28 Subroutinesub 86.6% 13 / 15 Podpod 100% 8 / 8 totaltotal 82.0% 201 / 245 CC 49 SCAR 21.2
CC
49
Cov
88%
runs
15.7
session_history_messages
10.3
load
8.1
CRAP 8.3
« blib/lib/Langertha/Raider/CLI/REPL.pm ↑ Summary blib/lib/Langertha/Raider/Config/Migrate.pm »
1 package Langertha::Raider::Session::Journal;
2 # ABSTRACT: Internal read-only view of one session journal
3 132 our $VERSION = '0.503';
4 132 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 132 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 132 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 132 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 132 has id => ( is => 'ro', isa => 'Str' );
11 132 has path => ( is => 'ro' );
12 0 ▶has events => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 0 ▶has damaged => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 132 has unterminated => ( is => 'ro', isa => 'Bool', default => 0 );
15  
16  
17 228 ▶sub load {
Subroutine load: called CC=8 SCAR=20.9
Pod documented
18 228 my ( $self, $file, %args ) = @_;
19 228 $file = path($file);
20 228 ▶ my $raw = -e $file ? $file->slurp_raw : '';
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 228 0 228
21 228 my $json = JSON::MaybeXS->new(utf8 => 1);
22 228 my ( @events, @damaged );
23 228 my $n = 0;
24 228 for my $line (split /\n/, $raw) {
25 455 $n++;
26 455 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 455 455
27 455 my $event = eval { $json->decode($line) };
28 455 ▶ if (ref $event eq 'HASH' && defined $event->{type}) { push @events, $event }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq 'HASH' and defined $event->{'type'}) { } 447 8 455
Condition66%2/3
ref $event eq "HASH" and defined $event->{"type"}
!ll&&!rl&&r
80447
MC/DC50%1/2
ref $event eq "HASH" and defined $event->{"type"}
ref $event eq "HASH" defined $event->{"type"}
Truth table66%2/3
A and B
A= ref $event eq "HASH"B= defined $event->{"type"}
ABresult
0X0
100
111
29 8 else { push @damaged, $n }
30 }
31 228 ▶ return $self->new(
Branch: 1 branch
BranchTrueFalseTotal
length $raw && substr($raw, -1) ne "\n" ? : 3 225 228
Condition100%3/3
length $raw and substr($raw, -1) ne "\n"
!ll&&!rl&&r
136893
MC/DC100%2/2
length $raw and substr($raw, -1) ne "\n"
length $raw substr($raw, -1) ne "\n"
Truth table100%3/3
A and B
A= length $rawB= substr($raw, -1) ne "\n"
ABresult
0X0
100
111
32 %args,
33 path => $file,
34 events => \@events,
35 damaged => \@damaged,
36 unterminated => length $raw && substr($raw, -1) ne "\n" ? 1 : 0,
37 );
38 }
39  
40  
41 8 ▶sub created {
Subroutine created: called CC=3 SCAR=12.5
Pod documented
42 8 my ( $self ) = @_;
43 8 my $first = $self->events->[0];
44 8 ▶ return $first && $first->{type} eq 'session.created' ? $first : undef;
Branch: 1 branch
BranchTrueFalseTotal
$first && $first->{'type'} eq 'session.created' ? : 8 0 8
Condition33%1/3
$first and $first->{"type"} eq "session.created"
!ll&&!rl&&r
008
MC/DC0%0/2
$first and $first->{"type"} eq "session.created"
$first $first->{"type"} eq "session.created"
Truth table33%1/3
A and B
A= $firstB= $first->{"type"} eq "session.created"
ABresult
0X0
100
111
45 }
46  
47  
48 193 ▶sub last_seq {
Subroutine last_seq: called CC=5 SCAR=17.5
Pod documented
49 193 my ( $self ) = @_;
50 193 my $max = 0;
51 193 for my $e (@{ $self->events }) {
52 204 ▶ $max = $e->{seq} if ($e->{seq} // 0) =~ /\A\d+\z/ && $e->{seq} > $max;
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"seq"} // 0) =~ /\A\d+\z/ and $e->{"seq"} > $max 204 0 204
Condition40%2/5
($e->{"seq"} // 0) =~ /\A\d+\z/ and $e->{"seq"} > $max
!ll&&!rl&&r
00204
$e->{"seq"} // 0
l!l
2040
MC/DC0%0/2
($e->{"seq"} // 0) =~ /\A\d+\z/ and $e->{"seq"} > $max
$e->{"seq"} $e->{"seq"} > $max
Truth table33%1/3
A and B
A= $e->{"seq"}B= $e->{"seq"} > $max
ABresult
0X0
100
111
53 }
54 193 return $max;
55 }
56  
57  
58 194 ▶sub last_run_number {
Subroutine last_run_number: called CC=5 SCAR=16.1
Pod documented
59 194 my ( $self ) = @_;
60 194 my $max = 0;
61 194 for my $e (@{ $self->events }) {
62 218 ▶ my ( $n ) = ($e->{run} // '') =~ /\Ar(\d+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($n) = ($e->{"run"} // "") =~ /\Ar(\d+)\z/ or next 155 63 218
Condition100%2/2
$e->{"run"} // ""
l!l
15563
MC/DC100%1/1
$e->{"run"} // ""
$e->{"run"}
Truth table100%2/2
A
A= $e->{"run"}
Aresult
11
00
63 155 ▶ $max = $n if $n > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max 42 113 155
64 }
65 194 return $max;
66 }
67  
68  
69 64 ▶sub runs {
Subroutine runs: called CC=15 SCAR=27.5
Pod documented
70 64 my ( $self ) = @_;
71 64 my ( @runs, %run );
72 64 for my $e (@{ $self->events }) {
73 605 ▶ my $id = $e->{run} // next;
Condition100%2/2
$e->{"run"} // (next)
l!l
51689
MC/DC100%1/1
$e->{"run"} // (next)
$e->{"run"}
Truth table100%2/2
A
A= $e->{"run"}
Aresult
11
00
74 516 ▶ my $r = $run{$id} //= do { push @runs, { run => $id }; $runs[-1] };
Condition66%2/3
$run{$id} //= do {
l!l&&r!l&&!r
432840
MC/DC0%0/2
$run{$id} //= do { push @runs, {"run", $id}; $runs[-1] }
$run{$id} do { push @runs, {"run", $id}; $runs[-1] }
Truth table66%2/3
A //= B
A= $run{$id}B= do { push @runs, {"run", $id}; $runs[-1] }
ABresult
1X1
011
000
75 516 ▶ if ($e->{type} eq 'run.started') { $r->{started} = $e }
Branch: 3 branches
BranchTrueFalseTotal
if ($e->{'type'} eq 'run.started') { } 79 437 516
elsif ($e->{'type'} eq 'run.finished') { } 73 364 437
elsif ($e->{'type'} eq 'message') { } 149 215 364
76 73 elsif ($e->{type} eq 'run.finished') { $r->{finished} = $e }
77 elsif ($e->{type} eq 'message') {
78 149 ▶ $r->{prompt} //= $e->{content} if ($e->{role} // '') eq 'user';
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"role"} // "") eq "user" 84 65 149
Condition40%2/5
$e->{"role"} // ""
l!l
1490
$r->{"prompt"} //= $e->{"content"}
l!l&&r!l&&!r
0840
MC/DC0%0/1
$e->{"role"} // ""
$e->{"role"}
MC/DC0%0/2
$r->{"prompt"} //= $e->{"content"}
$r->{"prompt"} $e->{"content"}
Truth table50%1/2
A
A= $e->{"role"}
Aresult
11
00
Truth table33%1/3
A //= B
A= $r->{"prompt"}B= $e->{"content"}
ABresult
1X1
011
000
79 149 ▶ $r->{response} = $e->{content} if ($e->{role} // '') eq 'assistant';
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"role"} // "") eq "assistant" 65 84 149
Condition50%1/2
$e->{"role"} // ""
l!l
1490
MC/DC0%0/1
$e->{"role"} // ""
$e->{"role"}
Truth table50%1/2
A
A= $e->{"role"}
Aresult
11
00
80 }
81 }
82 64 ▶ $_->{status} = $_->{finished} ? ($_->{finished}{status} // 'unknown') : 'interrupted' for @runs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'finished'} ? : 73 11 84
Condition50%1/2
$_->{"finished"}{"status"} // "unknown"
l!l
730
MC/DC0%0/1
$_->{"finished"}{"status"} // "unknown"
$_->{"finished"}{"status"}
Truth table50%1/2
A
A= $_->{"finished"}{"status"}
Aresult
11
00
83 64 return \@runs;
84 }
85  
86  
87 27 ▶sub unknown_calls {
Subroutine unknown_calls: called CC=2 SCAR=7.0
Pod documented
88 27 my ( $self ) = @_;
89 27 ▶ my $key = sub { ($_[0]{run} // '').' '.($_[0]{call} // '') };
Subroutine __ANON__: called CC=3 SCAR=12.0
Condition50%2/4
$_[0]{"run"} // ""
l!l
940
$_[0]{"call"} // ""
l!l
940
MC/DC0%0/1
$_[0]{"run"} // ""
$_[0]{"run"}
MC/DC0%0/1
$_[0]{"call"} // ""
$_[0]{"call"}
Truth table50%1/2
A
A= $_[0]{"run"}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{"call"}
Aresult
11
00
90 27 my %done = map { $key->($_) => 1 } grep { $_->{type} eq 'tool.result' } @{ $self->events };
91 27 ▶ return [ grep { $_->{type} eq 'tool.call' && !$done{ $key->($_) } } @{ $self->events } ];
Condition100%3/3
$_->{'type'} eq 'tool.call' && !$done{&$key($_)}
!ll&&!rl&&r
207454
MC/DC100%2/2
$_->{'type'} eq 'tool.call' && !$done{&$key($_)}
$_->{'type'} eq 'tool.call' !$done{&$key($_)}
Truth table100%3/3
A && not(B)
A= $_->{'type'} eq 'tool.call'B= !$done{&$key($_)}
ABresult
0X0
100
111
92 }
93  
94  
95 26 ▶sub history_messages {
Subroutine history_messages: called CC=6 SCAR=17.9
Pod documented
96 26 my ( $self ) = @_;
97 26 my %answered = map { $_->{run} => 1 } grep { defined $_->{response} } @{ $self->runs };
98 26 my @messages;
99 26 for my $e (@{ $self->events }) {
100 233 ▶ if ($e->{type} eq 'history.cleared') { @messages = () }
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{'type'} eq 'history.cleared') { } 4 229 233
elsif ($e->{'type'} eq 'message' and !defined($e->{'run'}) || $answered{$e->{'run'}}) { } 60 333 393
101 ▶ elsif ($e->{type} eq 'message' && (!defined $e->{run} || $answered{ $e->{run} })) {
Condition100%6/6
$e->{"type"} eq "message" and !defined($e->{"run"}) || $answered{$e->{"run"}}
!ll&&!rl&&r
164560
not defined $e->{"run"} or $answered{$e->{"run"}}
l!l&&r!l&&!r
8525
MC/DC100%3/3
$e->{"type"} eq "message" and !defined($e->{"run"}) || $answered{$e->{"run"}}
$e->{"type"} eq "message" not defined $e->{"run"} $answered{$e->{"run"}}
Truth table100%4/4
A and not(B) or C
A= $e->{"type"} eq "message"B= not defined $e->{"run"}C= $answered{$e->{"run"}}
ABCresult
0XX0
1000
11X1
1011
102 60 push @messages, { role => $e->{role}, content => $e->{content} };
103 }
104 }
105 26 return \@messages;
106 }
107  
108  
109 11 ▶sub session_history_messages {
Subroutine session_history_messages: called CC=10 SCAR=23.3
Pod documented
110 11 my ( $self ) = @_;
111 11 ▶ my %unknown = map { ($_->{run} // '').' '.($_->{call} // '') => 1 } @{ $self->unknown_calls };
Condition50%2/4
$_->{"run"} // ""
l!l
10
$_->{"call"} // ""
l!l
10
MC/DC0%0/1
$_->{"run"} // ""
$_->{"run"}
MC/DC0%0/1
$_->{"call"} // ""
$_->{"call"}
Truth table50%1/2
A
A= $_->{"run"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"call"}
Aresult
11
00
112 11 my @entries;
113 11 for my $e (@{ $self->events }) {
114 96 ▶ if ($e->{type} eq 'message') {
Branch: 3 branches
BranchTrueFalseTotal
if ($e->{'type'} eq 'message') { } 27 69 96
elsif ($e->{'type'} eq 'tool.call') { } 17 52 69
elsif ($e->{'type'} eq 'tool.result') { } 16 36 52
115 27 push @entries, { role => $e->{role}, content => $e->{content} };
116 }
117 elsif ($e->{type} eq 'tool.call') {
118 push @entries, { role => 'assistant', content => [ {
119 type => 'tool_use', id => $e->{call}, name => $e->{name}, input => $e->{arguments},
120 17 } ] };
121 push @entries, { role => 'tool', name => $e->{name},
122 content => 'unknown: no result was recorded, the call is not run again' }
123 17 ▶ if $unknown{ ($e->{run} // '').' '.($e->{call} // '') };
Branch: 1 branch
BranchTrueFalseTotal
if $unknown{($e->{"run"} // "") . ' ' . ($e->{"call"} // "")} 1 16 17
Condition50%2/4
$e->{"run"} // ""
l!l
170
$e->{"call"} // ""
l!l
170
MC/DC0%0/1
$e->{"run"} // ""
$e->{"run"}
MC/DC0%0/1
$e->{"call"} // ""
$e->{"call"}
Truth table50%1/2
A
A= $e->{"run"}
Aresult
11
00
Truth table50%1/2
A
A= $e->{"call"}
Aresult
11
00
124 }
125 elsif ($e->{type} eq 'tool.result') {
126 16 push @entries, { role => 'tool', name => $e->{name}, content => $e->{content} };
127 }
128 }
129 11 return \@entries;
130 }
131  
132 132 __PACKAGE__->meta->make_immutable;
133  
134 132 1;
135  
136 __END__
137  
138 =pod
139  
140 =encoding UTF-8
141  
142 =head1 NAME
143  
144 Langertha::Raider::Session::Journal - Internal read-only view of one session journal
145  
146 =head1 VERSION
147  
148 version 0.503
149  
150 =head1 SYNOPSIS
151  
152 # Internal to Langertha-Raider -- no API promise.
153 my $journal = Langertha::Raider::Session::Journal->load($file, id => $id);
154  
155 for my $run (@{ $journal->runs }) {
156 say $run->{run}, ' ', $run->{status}; # 'interrupted' without run.finished
157 }
158 my @unknown = @{ $journal->unknown_calls }; # tool.call without tool.result
159 my @history = @{ $journal->history_messages }; # { role, content } to replay
160 warn 'damaged lines: '.join(', ', @{ $journal->damaged }) if @{ $journal->damaged };
161  
162 =head1 DESCRIPTION
163  
164 B<Internal module.> Its interface may change without notice.
165  
166 A session journal as read from disk (ADR 0015), with the crash rules
167 applied: a line that is not a complete JSON object is skipped and its
168 number listed in L</damaged>; a run without C<run.finished> counts as
169 C<interrupted>; a C<tool.call> without its C<tool.result> is C<unknown>.
170 Unknown event types and fields are kept but not interpreted. Beside the
171 event types of ADR 0015 it knows C<history.cleared> (no fields, no run):
172 the REPL's C</clear> emptied the working history there.
173  
174 =head2 id
175  
176 The session id.
177  
178 =head2 path
179  
180 The journal file.
181  
182 =head2 events
183  
184 The events in file order, each a hash reference.
185  
186 =head2 damaged
187  
188 The line numbers (1-based) that could not be read.
189  
190 =head2 unterminated
191  
192 True when the file does not end with a newline -- the last write was cut
193 off.
194  
195 =head2 load
196  
197 my $journal = Langertha::Raider::Session::Journal->load($file, id => $id);
198  
199 Reads the file. A missing file reads as an empty journal.
200  
201 =head2 created
202  
203 The C<session.created> event (line 1), or C<undef>.
204  
205 =head2 last_seq
206  
207 The highest C<seq> in the journal, C<0> for none.
208  
209 =head2 last_run_number
210  
211 The highest run number (C<r3> is 3) in the journal, C<0> for none.
212  
213 =head2 runs
214  
215 The runs in order, each a hash reference: C<run>, C<status> (from
216 C<run.finished>, or C<interrupted> without one), C<started> and
217 C<finished> (the events, C<finished> may be C<undef>), C<prompt> (the
218 user message) and C<response> (the assistant message, when there is one).
219  
220 =head2 unknown_calls
221  
222 The C<tool.call> events that have no C<tool.result> with the same C<run>
223 and C<call>: their outcome is unknown, and they are never run again.
224  
225 =head2 history_messages
226  
227 The working history to rebuild on resume, as C<< { role, content } >>
228 hashes: the user input and final assistant text of every run that has a
229 final assistant text. A run that failed or was interrupted before its
230 answer adds nothing, as a live raid adds nothing to C<history> then. A
231 C<history.cleared> event (C</clear> in the REPL) empties it: only the
232 messages after the last one count. A C<message> outside any run (the
233 history a fork took over) counts as it is.
234  
235 =head2 session_history_messages
236  
237 The full history to rebuild on resume, from all events, in the shapes
238 L<Langertha::Raider/session_history> renders: a C<message> as
239 C<< { role, content } >>, a C<tool.call> as an assistant C<tool_use>
240 block, a C<tool.result> as C<< { role => 'tool', name, content } >>, and
241 for a call without result an entry saying its outcome is unknown.
242  
243 =head1 SEE ALSO
244  
245 =over
246  
247 =item * L<Langertha::Raider::SessionStore>
248  
249 =item * L<Langertha::Raider::Session>
250  
251 =back
252  
253 =head1 SUPPORT
254  
255 =head2 Issues
256  
257 Please report bugs and feature requests on GitHub at
258 L<https://github.com/Getty/langertha-raider/issues>.
259  
260 =head2 IRC
261  
262 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
263  
264 =head1 CONTRIBUTING
265  
266 Contributions are welcome! Please fork the repository and submit a pull request.
267  
268 =head1 AUTHOR
269  
270 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
271  
272 =head1 COPYRIGHT AND LICENSE
273  
274 This software is copyright (c) 2026 by Torsten Raudssus.
275  
276 This is free software; you can redistribute it and/or modify it under
277 the same terms as the Perl 5 programming language system itself.
278  
279 =cut
« blib/lib/Langertha/Raider/CLI/REPL.pm ↑ Summary blib/lib/Langertha/Raider/Config/Migrate.pm »