blib/lib/Langertha/Raider/Session.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 85.0% 17 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.7% 18 / 19 Podpod 62.5% 5 / 8 totaltotal 93.8% 123 / 131 CC 11 SCAR 8.9
CC
11
Cov
96%
BUILD
5.0
append
4.0
release
3.0
CRAP 2.4
« blib/lib/Langertha/Raider/CLI/Machine.pm ↑ Summary blib/lib/Langertha/Raider/Instructions.pm »
1 package Langertha::Raider::Session;
2 # ABSTRACT: Internal writer of one session journal, holding its lock
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 Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 132 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 132 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 132 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 132 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 132 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 132 ▶use Langertha::Raider::Session::Journal;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 132 has id => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1,
19 );
20  
21 132 has path => (
22 is => 'ro',
23 required => 1,
24 );
25  
26 has clock => (
27 is => 'ro',
28 isa => 'CodeRef',
29 0 ▶ default => sub { \&Time::HiRes::time },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 132 );
31  
32  
33 132 has journal => (
34 is => 'ro',
35 init_arg => undef,
36 writer => '_set_journal',
37 );
38  
39 132 has _lock_fh => ( is => 'rw', init_arg => undef );
40 132 has _fh => ( is => 'rw', init_arg => undef );
41 132 has _seq => ( is => 'rw', init_arg => undef, default => 0 );
42 132 has _runs => ( is => 'rw', init_arg => undef, default => 0 );
43 132 has _torn => ( is => 'rw', init_arg => undef, default => 0 );
44  
45 has _json => (
46 is => 'ro',
47 lazy => 1,
48 180 ▶ default => sub { JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1, allow_blessed => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 132 );
50  
51 193 ▶sub journal_class { 'Langertha::Raider::Session::Journal' }
Subroutine journal_class: called CC=1 SCAR=0.0
Pod undocumented
52  
53 203 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
54 203 my ( $self ) = @_;
55 203 my $path = path($self->path);
56 203 ▶ open my $lock, '>>', $self->lock_path or croak 'cannot open '.$self->lock_path.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open my $lock, ">>", $self->lock_path or croak('cannot open ' . $self->lock_path . ': ' . $!) 203 0 203
57 203 ▶ croak 'session '.$self->id.' is in use' unless flock $lock, LOCK_EX | LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
unless flock $lock, 6 10 193 203
58 193 $self->_lock_fh($lock);
59  
60 193 my $journal = $self->journal_class->load($path, id => $self->id);
61 193 $self->_set_journal($journal);
62 193 $self->_seq($journal->last_seq);
63 193 $self->_runs($journal->last_run_number);
64  
65 193 ▶ open my $fh, '>>:raw', $path or croak 'cannot append to '.$path.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>:raw", $path or croak('cannot append to ' . $path . ': ' . $!) 193 0 193
66 193 $fh->autoflush(1);
67 193 ▶ print {$fh} "\n" if $journal->unterminated;
Branch: 1 branch
BranchTrueFalseTotal
if $journal->unterminated 2 191 193
68 193 $self->_fh($fh);
69 193 return;
70 }
71  
72 203 ▶sub DEMOLISH { $_[0]->release }
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
73  
74  
75 208 ▶sub lock_path {
Subroutine lock_path: called CC=1 SCAR=0.0
Pod documented
76 208 my ( $self ) = @_;
77 208 return path($self->path)->sibling($self->id.'.lock')->stringify;
78 }
79  
80  
81 1 ▶sub is_open { $_[0]->_fh ? 1 : 0 }
Subroutine is_open: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_fh ? : 1 0 1
82  
83  
84 127 ▶sub next_run {
Subroutine next_run: called CC=1 SCAR=0.0
Pod documented
85 127 my ( $self ) = @_;
86 127 $self->_runs($self->_runs + 1);
87 127 return 'r'.$self->_runs;
88 }
89  
90  
91 706 ▶sub append {
Subroutine append: called CC=4 SCAR=13.9
Pod documented
92 706 my ( $self, $type, %fields ) = @_;
93 706 ▶ my $fh = $self->_fh or croak 'session '.$self->id.' is closed';
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->_fh or croak('session ' . $self->id . ' is closed') 705 1 706
94 705 $self->_seq($self->_seq + 1);
95 705 my $event = { %fields, v => 1, seq => $self->_seq, time => $self->clock->(), type => $type };
96 # After a failed write part of that line may be in the file: start anew.
97 705 ▶ my $line = ($self->_torn ? "\n" : '').$self->_json->encode($event)."\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->_torn ? : 1 704 705
98 705 ▶ unless (print {$fh} $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print {$fh;} $line) 1 704 705
99 1 $self->_torn(1);
100 1 croak 'cannot write '.$self->path.': '.$!;
101 }
102 704 $self->_torn(0);
103 704 return $event;
104 }
105  
106  
107 279 ▶sub release {
Subroutine release: called CC=3 SCAR=11.0
Pod documented
108 279 my ( $self ) = @_;
109 279 ▶ if (my $fh = $self->_fh) { close $fh; $self->_fh(undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->_fh) 193 86 279
110 279 ▶ if (my $lock = $self->_lock_fh) { close $lock; $self->_lock_fh(undef) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $lock = $self->_lock_fh) 193 86 279
111 279 return;
112 }
113  
114 132 __PACKAGE__->meta->make_immutable;
115  
116 132 1;
117  
118 __END__
119  
120 =pod
121  
122 =encoding UTF-8
123  
124 =head1 NAME
125  
126 Langertha::Raider::Session - Internal writer of one session journal, holding its lock
127  
128 =head1 VERSION
129  
130 version 0.503
131  
132 =head1 SYNOPSIS
133  
134 # Internal to Langertha-Raider -- no API promise.
135 # Made by Langertha::Raider::SessionStore->create / ->open.
136 my $run = $session->next_run; # 'r3' after two runs
137 $session->append('message', run => $run, role => 'user', content => 'hi');
138 say $session->id, ' ', $session->path;
139 $session->release; # or let it go out of scope
140  
141 =head1 DESCRIPTION
142  
143 B<Internal module.> Its interface may change without notice.
144  
145 The one writer of a session journal (ADR 0015). Constructing it takes an
146 exclusive, non-blocking C<flock> on F<< <id>.lock >> next to the journal
147 and fails at once with C<session ID is in use> when another writer --
148 in this process or another -- holds it. The lock is held until
149 L</release> or until the object is destroyed.
150  
151 On opening, the journal is read once (L</journal>): numbering continues
152 after its last C<seq> and its last run, and when the last line was cut off
153 (a crash mid-write) the next append starts on a fresh line.
154  
155 =head2 id
156  
157 The session id. Required.
158  
159 =head2 path
160  
161 The journal file, a L<Path::Tiny>. Required; it must exist.
162  
163 =head2 clock
164  
165 Code reference returning epoch seconds for the C<time> of an event.
166 Defaults to L<Time::HiRes/time>.
167  
168 =head2 journal
169  
170 The L<Langertha::Raider::Session::Journal> as it was when the session was
171 opened (read under the lock) -- what a resume replays.
172  
173 =head2 lock_path
174  
175 F<< <id>.lock >> next to the journal.
176  
177 =head2 is_open
178  
179 True until L</release>.
180  
181 =head2 next_run
182  
183 my $run = $session->next_run; # 'r1', 'r2', ...
184  
185 The id of the next run of this session, counting on from the runs already
186 in the journal.
187  
188 =head2 append
189  
190 my $event = $session->append('tool.call', run => 'r1', call => 'c1', name => 'bash', ...);
191  
192 Writes one event as one line and flushes it: the fields plus C<v> (1),
193 C<seq>, C<time> and C<type>. Returns the event. Croaks C<cannot write
194 FILE: ...> when the line cannot be written; the next event then starts on
195 a fresh line, so a partly written one damages only itself.
196  
197 =head2 release
198  
199 Closes the journal and gives up the lock. Appending afterwards croaks.
200  
201 =head1 SEE ALSO
202  
203 =over
204  
205 =item * L<Langertha::Raider::SessionStore>
206  
207 =back
208  
209 =head1 SUPPORT
210  
211 =head2 Issues
212  
213 Please report bugs and feature requests on GitHub at
214 L<https://github.com/Getty/langertha-raider/issues>.
215  
216 =head2 IRC
217  
218 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
219  
220 =head1 CONTRIBUTING
221  
222 Contributions are welcome! Please fork the repository and submit a pull request.
223  
224 =head1 AUTHOR
225  
226 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
227  
228 =head1 COPYRIGHT AND LICENSE
229  
230 This software is copyright (c) 2026 by Torsten Raudssus.
231  
232 This is free software; you can redistribute it and/or modify it under
233 the same terms as the Perl 5 programming language system itself.
234  
235 =cut
« blib/lib/Langertha/Raider/CLI/Machine.pm ↑ Summary blib/lib/Langertha/Raider/Instructions.pm »