blib/lib/Langertha/Raider/SessionStore.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 90.0% 36 / 40 Conditioncond 67.6% 23 / 34 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 100% 34 / 34 Podpod 84.2% 16 / 19 totaltotal 89.3% 236 / 264 CC 35 SCAR 11.9
CC
35
Cov
92%
resolve
7.0
create
6.8
is_ref
5.0
CRAP 3.3
« blib/lib/Langertha/Raider/Approval.pm ↑ Summary blib/lib/Langertha/Raider/ToolEffects.pm »
1 package Langertha::Raider::SessionStore;
2 # ABSTRACT: Internal store of the session journals of one scope
3 132 our $VERSION = '0.503';
4 132 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 132 ▶use Moose::Util::TypeConstraints qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 132 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 132 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 132 ▶use Fcntl qw( O_WRONLY O_CREAT O_EXCL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 132 ▶use POSIX qw( strftime );
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::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 132 ▶use Langertha::Raider::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 132 ▶use Langertha::Raider::Session::Journal;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 132 has scope => (
18 is => 'ro',
19 isa => enum([qw( project home )]),
20 default => 'project',
21 );
22  
23 132 has root => (
24 is => 'ro',
25 isa => 'Str',
26 predicate => 'has_root',
27 );
28  
29 132 has home => (
30 is => 'ro',
31 isa => 'Str',
32 lazy => 1,
33 builder => '_build_home',
34 );
35  
36 1 ▶sub _build_home { Langertha::Raider::Home->home_dir }
Subroutine _build_home: called CC=1 SCAR=0.0
37  
38  
39 132 has principal => (
40 is => 'ro',
41 isa => 'Str',
42 lazy => 1,
43 builder => '_build_principal',
44 );
45  
46 118 ▶sub _build_principal { scalar(getpwuid($<)) // $ENV{USER} // 'unknown' }
Subroutine _build_principal: called CC=3 SCAR=14.2
Condition40%2/5
(scalar getpwuid $< // $ENV{"USER"}) // "unknown"
l!l
1180
scalar getpwuid $< // $ENV{"USER"}
l!l&&r!l&&!r
11800
MC/DC0%0/2
(scalar getpwuid $< // $ENV{"USER"}) // "unknown"
scalar getpwuid $< $ENV{"USER"}
Truth table33%1/3
A // B
A= scalar getpwuid $<B= $ENV{"USER"}
ABresult
1X1
011
000
47  
48  
49 has clock => (
50 is => 'ro',
51 isa => 'CodeRef',
52 253 ▶ default => sub { \&Time::HiRes::time },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 132 );
54  
55 203 ▶sub session_class { 'Langertha::Raider::Session' }
Subroutine session_class: called CC=1 SCAR=0.0
Pod undocumented
56 35 ▶sub journal_class { 'Langertha::Raider::Session::Journal' }
Subroutine journal_class: called CC=1 SCAR=0.0
Pod undocumented
57  
58 254 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
59 254 my ( $self ) = @_;
60 254 ▶ croak __PACKAGE__.': the project scope needs a root' if $self->scope eq 'project' && !$self->has_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->scope eq "project" and not $self->has_root 1 253 254
Condition100%3/3
$self->scope eq "project" and not $self->has_root
!ll&&!rl&&r
12521
MC/DC100%2/2
$self->scope eq "project" and not $self->has_root
$self->scope eq "project" not $self->has_root
Truth table100%3/3
A and not(B)
A= $self->scope eq "project"B= not $self->has_root
ABresult
0X0
100
111
61 253 return;
62 }
63  
64  
65 970 ▶sub base {
Subroutine base: called CC=2 SCAR=6.9
Pod documented
66 970 my ( $self ) = @_;
67 970 ▶ return $self->scope eq 'project'
Branch: 1 branch
BranchTrueFalseTotal
$self->scope eq 'project' ? : 955 15 970
68 ? Langertha::Raider::Home->project_base($self->root)
69 : Langertha::Raider::Home->home_base($self->home);
70 }
71  
72 658 ▶sub dir { $_[0]->base->child('sessions') }
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
73  
74  
75 816 ▶sub is_id {
Subroutine is_id: called CC=3 SCAR=11.1
Pod documented
76 816 my ( $self, $id ) = @_;
77 816 ▶ return defined $id && $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $id && $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/ ? : 795 21 816
Condition66%2/3
defined $id and $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/
!ll&&!rl&&r
021795
MC/DC50%1/2
defined $id and $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/
defined $id $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/
Truth table66%2/3
A and B
A= defined $idB= $id =~ /\A\d{8}-\d{6}-[0-9a-f]{4}\z/
ABresult
0X0
100
111
78 }
79  
80  
81 459 ▶sub path_of {
Subroutine path_of: called CC=3 SCAR=11.1
Pod documented
82 459 my ( $self, $id ) = @_;
83 459 ▶ croak 'not a session id: '.($id // '(undef)') unless $self->is_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_id($id) 1 458 459
Condition50%1/2
$id // "(undef)"
l!l
10
MC/DC0%0/1
$id // "(undef)"
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
84 458 return $self->dir->child($id.'.jsonl');
85 }
86  
87  
88 206 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
89 206 my ( $self, $id ) = @_;
90 206 ▶ return $self->is_id($id) && -f $self->path_of($id) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_id($id) && -f $self->path_of($id) ? : 183 23 206
Condition100%3/3
$self->is_id($id) and -f $self->path_of($id)
!ll&&!rl&&r
149183
MC/DC100%2/2
$self->is_id($id) and -f $self->path_of($id)
$self->is_id($id) -f $self->path_of($id)
Truth table100%3/3
A and B
A= $self->is_id($id)B= -f $self->path_of($id)
ABresult
0X0
100
111
91 }
92  
93  
94 56 ▶sub ids {
Subroutine ids: called CC=2 SCAR=6.9
Pod documented
95 56 my ( $self ) = @_;
96 56 my $dir = $self->dir;
97 56 ▶ return () unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 10 46 56
98 85 return reverse sort grep { $self->is_id($_) }
99 46 map { $_->basename('.jsonl') } $dir->children(qr/\.jsonl\z/);
100 }
101  
102  
103 4 ▶sub latest { ( $_[0]->ids )[0] }
Subroutine latest: called CC=1 SCAR=0.0
Pod documented
104  
105  
106 89 ▶sub is_ref {
Subroutine is_ref: called CC=5 SCAR=16.1
Pod documented
107 89 my ( $self, $ref ) = @_;
108 89 ▶ return 0 unless defined $ref && length $ref >= 4 && length $ref <= 20;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and length $ref >= 4 and length $ref <= 20 9 80 89
Condition100%6/6
defined $ref and length $ref >= 4 and length $ref <= 20
!ll&&!rl&&r
8180
defined $ref and length $ref >= 4
!ll&&!rl&&r
3581
MC/DC100%3/3
defined $ref and length $ref >= 4 and length $ref <= 20
defined $ref length $ref >= 4 length $ref <= 20
Truth table100%4/4
A and B and C
A= defined $refB= length $ref >= 4C= length $ref <= 20
ABCresult
0XX0
10X0
1100
1111
109 80 ▶ return 1 if $ref =~ /\A[0-9a-f]{4}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /\A[0-9a-f]{4}\z/ 15 65 80
110 65 return $self->is_id($ref.substr('00000000-000000-0000', length $ref));
111 }
112  
113  
114 36 ▶sub resolve {
Subroutine resolve: called CC=7 SCAR=19.5
Pod documented
115 36 my ( $self, $ref ) = @_;
116 36 ▶ croak 'unknown session '.($ref // '(undef)') unless $self->is_ref($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_ref($ref) 1 35 36
Condition50%1/2
$ref // "(undef)"
l!l
10
MC/DC0%0/1
$ref // "(undef)"
$ref
Truth table50%1/2
A
A= $ref
Aresult
11
00
117 35 ▶ return $ref if $self->exists($ref);
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($ref) 17 18 35
118 18 ▶ my @ids = grep { index($_, $ref) == 0 || substr($_, -5) eq '-'.$ref } $self->ids;
Condition100%3/3
index($_, $ref) == 0 || substr($_, -5) eq '-' . $ref
l!l&&r!l&&!r
8627
MC/DC100%2/2
index($_, $ref) == 0 || substr($_, -5) eq '-' . $ref
index($_, $ref) == 0 substr($_, -5) eq '-' . $ref
Truth table100%3/3
A || B
A= index($_, $ref) == 0B= substr($_, -5) eq '-' . $ref
ABresult
1X1
011
000
119 18 ▶ croak 'unknown session '.$ref unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 7 11 18
120 11 ▶ croak 'session '.$ref.' is ambiguous: '.join(', ', @ids) if @ids > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ids > 1 3 8 11
121 8 return $ids[0];
122 }
123  
124  
125 154 ▶sub prepare_base {
Subroutine prepare_base: called CC=3 SCAR=11.0
Pod documented
126 154 my ( $self ) = @_;
127 154 $self->base->mkpath;
128 153 ▶ if ($self->scope eq 'project') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scope eq "project") 150 3 153
129 150 my $ignore = $self->base->child('.gitignore');
130 150 ▶ $ignore->spew_utf8("sessions/\nlib/\n") unless -e $ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $ignore 89 61 150
131 }
132 153 return;
133 }
134  
135 137 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod documented
136 137 my ( $self ) = @_;
137 137 $self->prepare_base;
138 136 $self->dir->mkpath;
139 136 return;
140 }
141  
142  
143 136 ▶sub new_id {
Subroutine new_id: called CC=1 SCAR=0.0
Pod documented
144 136 my ( $self ) = @_;
145 136 return strftime('%Y%m%d-%H%M%S', gmtime int $self->clock->()).'-'.sprintf('%04x', int rand 0x10000);
146 }
147  
148  
149 137 ▶sub create {
Subroutine create: called CC=6 SCAR=19.2
Pod documented
150 137 my ( $self, %fields ) = @_;
151 137 $self->prepare;
152 136 my ( $id, $path );
153 136 for my $try (1 .. 100) {
154 136 $id = $self->new_id;
155 136 $path = $self->path_of($id);
156 136 ▶ if (sysopen my $fh, $path, O_WRONLY | O_CREAT | O_EXCL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen my $fh, $path, 193) 136 0 136
157 136 close $fh;
158 136 last;
159 }
160 0 ▶ croak 'cannot create '.$path.': '.$! unless $!{EEXIST} && $try < 100;
Subroutine BEGIN: called CC=1 SCAR=2.6
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EEXIST"} and $try < 100 0 0 0
Condition0%0/3
$!{"EEXIST"} and $try < 100
!ll&&!rl&&r
000
MC/DC0%0/2
$!{"EEXIST"} and $try < 100
$!{"EEXIST"} $try < 100
Truth table0%0/3
A and B
A= $!{"EEXIST"}B= $try < 100
ABresult
0X0
100
111
161 }
162 136 my $session = $self->session_class->new(id => $id, path => $path, clock => $self->clock);
163 136 ▶ $session->append('session.created',
Branch: 1 branch
BranchTrueFalseTotal
$self->scope eq 'project' ? : 133 3 136
164 %fields,
165 id => $id,
166 scope => $self->scope,
167 root => $self->scope eq 'project' ? path($self->root)->absolute->stringify : $self->home,
168 principal => $self->principal,
169 raider => $VERSION,
170 );
171 136 return $session;
172 }
173  
174  
175 69 ▶sub open {
Subroutine open: called CC=3 SCAR=11.1
Pod documented
176 69 my ( $self, $id ) = @_;
177 69 ▶ croak 'unknown session '.($id // '(undef)') unless $self->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($id) 2 67 69
Condition50%1/2
$id // "(undef)"
l!l
20
MC/DC0%0/1
$id // "(undef)"
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
178 67 return $self->session_class->new(id => $id, path => $self->path_of($id), clock => $self->clock);
179 }
180  
181  
182 36 ▶sub read {
Subroutine read: called CC=3 SCAR=11.1
Pod documented
183 36 my ( $self, $id ) = @_;
184 36 ▶ croak 'unknown session '.($id // '(undef)') unless $self->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($id) 1 35 36
Condition50%1/2
$id // "(undef)"
l!l
10
MC/DC0%0/1
$id // "(undef)"
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
185 35 return $self->journal_class->load($self->path_of($id), id => $id);
186 }
187  
188  
189 8 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
190 8 my ( $self, $id ) = @_;
191 8 my $session = $self->open($id);
192 4 ▶ path($session->path)->remove or croak 'cannot remove '.$session->path.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
path($session->path)->remove or croak('cannot remove ' . $session->path . ': ' . $!) 4 0 4
193 4 path($session->lock_path)->remove;
194 4 $session->release;
195 4 return;
196 }
197  
198 132 __PACKAGE__->meta->make_immutable;
199  
200 132 1;
201  
202 __END__
203  
204 =pod
205  
206 =encoding UTF-8
207  
208 =head1 NAME
209  
210 Langertha::Raider::SessionStore - Internal store of the session journals of one scope
211  
212 =head1 VERSION
213  
214 version 0.503
215  
216 =head1 SYNOPSIS
217  
218 # Internal to Langertha-Raider -- no API promise.
219 my $store = Langertha::Raider::SessionStore->new(scope => 'project', root => $dir);
220  
221 my $session = $store->create; # new journal, locked for writing
222 my $run = $session->next_run; # 'r1'
223 $session->append('run.started', run => $run, engine => 'openai', model => 'gpt-4o');
224  
225 my @ids = $store->ids; # newest first
226 my $journal = $store->read($ids[0]); # no lock needed
227 my $again = $store->open($ids[0]); # croaks "... is in use" while $session lives
228  
229 =head1 DESCRIPTION
230  
231 B<Internal module.> Its interface may change without notice.
232  
233 The session journals of one scope (ADR 0003, ADR 0015): the directory
234 F<sessions/> under F<< <project>/.raider >> (scope C<project>) or
235 F<~/.raider> (scope C<home>), one F<< <id>.jsonl >> per session.
236 L</create> and L</open> hand out a L<Langertha::Raider::Session>, the one
237 writer of a journal, which holds its lock; L</read> gives a
238 L<Langertha::Raider::Session::Journal> without taking a lock.
239  
240 =head2 scope
241  
242 C<project> (the default) or C<home>.
243  
244 =head2 root
245  
246 The project directory. Required for the C<project> scope.
247  
248 =head2 home
249  
250 The home directory of the C<home> scope. Defaults to C<$ENV{HOME}>, then
251 the user's home from the password database.
252  
253 =head2 principal
254  
255 The local user name written into C<session.created>. Defaults to the name
256 of the real user id, then C<$ENV{USER}>.
257  
258 =head2 clock
259  
260 Code reference returning the current time as epoch seconds; the C<time> of
261 every event and the time in a new id. Defaults to L<Time::HiRes/time>.
262  
263 =head2 base
264  
265 The scope directory: F<< <root>/.raider >> or F<< <home>/.raider >>, as a
266 L<Path::Tiny>.
267  
268 =head2 dir
269  
270 The F<sessions> directory under L</base>.
271  
272 =head2 is_id
273  
274 $store->is_id('20260925-081500-3f2a'); # true
275  
276 Whether the string has the form of a session id,
277 C<YYYYMMDD-HHMMSS-xxxx>.
278  
279 =head2 path_of
280  
281 my $file = $store->path_of($id);
282  
283 The journal file of a session id. Croaks on a string that is no id, so no
284 path outside L</dir> can be built from user input.
285  
286 =head2 exists
287  
288 True when the session has a journal in this scope.
289  
290 =head2 ids
291  
292 The ids of the sessions in this scope, newest first. Empty when there is
293 no F<sessions> directory.
294  
295 =head2 latest
296  
297 The id of the newest session, or C<undef>.
298  
299 =head2 is_ref
300  
301 $store->is_ref('3f2a'); # true
302 $store->is_ref('20260925-0815'); # true
303  
304 Whether the string can name a session on the command line: a whole id, the
305 start of one (at least four characters), or the four hex digits at its end.
306 Also works as a class method. L</resolve> finds the session it names.
307  
308 =head2 resolve
309  
310 my $id = $store->resolve('3f2a');
311  
312 The id of the one session a reference (L</is_ref>) names: the session with
313 that id, else the one whose id starts with it or ends in C<-REF>. Croaks
314 C<unknown session REF> when there is none, and C<session REF is
315 ambiguous: ID, ID> (newest first) when there is more than one.
316  
317 =head2 prepare_base
318  
319 Creates L</base>. In the C<project> scope it also writes
320 F<.raider/.gitignore> excluding C<sessions/> and C<lib/> when there is
321 none, so journals and the local::lib of the Perl tools are never committed
322 by default. Whatever creates F<.raider/> goes through here.
323  
324 =head2 prepare
325  
326 L</prepare_base>, then creates L</dir>.
327  
328 =head2 new_id
329  
330 A fresh id for the current time: C<YYYYMMDD-HHMMSS> in UTC and four
331 random hex digits.
332  
333 =head2 create
334  
335 my $session = $store->create;
336 my $fork = $store->create(forked_from => $id);
337  
338 Starts a new session: L</prepare>, claims a fresh id (a file that already
339 exists is never reused), locks it and writes C<session.created> as line 1
340 -- C<id>, C<scope>, C<root> (the project, or the home directory),
341 C<principal> and C<raider> (the version), plus the given fields (which
342 never replace those).
343  
344 =head2 open
345  
346 my $session = $store->open($id);
347  
348 Opens an existing session for writing. Croaks C<unknown session ID> when it
349 has no journal here, and C<session ID is in use> when another writer holds
350 its lock -- at once, without waiting.
351  
352 =head2 read
353  
354 my $journal = $store->read($id);
355  
356 The L<Langertha::Raider::Session::Journal> of a session, read without a
357 lock. Croaks C<unknown session ID> when there is none.
358  
359 =head2 remove
360  
361 $store->remove($id);
362  
363 Deletes a session: its journal and its lock file. It takes the lock first,
364 so it croaks C<session ID is in use> -- at once -- while another writer has
365 the session open, and C<unknown session ID> when there is none. Nothing in
366 Raider removes a session on its own.
367  
368 =head1 SEE ALSO
369  
370 =over
371  
372 =item * L<Langertha::Raider::Session>
373  
374 =item * L<Langertha::Raider::Session::Journal>
375  
376 =back
377  
378 =head1 SUPPORT
379  
380 =head2 Issues
381  
382 Please report bugs and feature requests on GitHub at
383 L<https://github.com/Getty/langertha-raider/issues>.
384  
385 =head2 IRC
386  
387 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
388  
389 =head1 CONTRIBUTING
390  
391 Contributions are welcome! Please fork the repository and submit a pull request.
392  
393 =head1 AUTHOR
394  
395 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
396  
397 =head1 COPYRIGHT AND LICENSE
398  
399 This software is copyright (c) 2026 by Torsten Raudssus.
400  
401 This is free software; you can redistribute it and/or modify it under
402 the same terms as the Perl 5 programming language system itself.
403  
404 =cut
« blib/lib/Langertha/Raider/Approval.pm ↑ Summary blib/lib/Langertha/Raider/ToolEffects.pm »