blib/lib/Langertha/Raider/Hall.pm

filter: Statementstmt 89.6% 605 / 675 Branchbran 77.3% 178 / 230 Conditioncond 64.9% 128 / 197 MC/DCmcdc 45.0% 50 / 111 Subroutinesub 90.7% 117 / 129 Podpod 40.0% 6 / 15 totaltotal 79.8% 1084 / 1357 CC 210 SCAR 22.0
CC
210
Cov
83%
_process_client_frames
110.0
_spawn_raider
20.0
__ANON__
20.0
CRAP 9.0
« blib/lib/Langertha/Raider/WebTools.pm ↑ Summary blib/lib/Langertha/Raider/CLI/Provider.pm »
1 package Langertha::Raider::Hall;
2 72 our $VERSION = '0.503';
3 # ABSTRACT: Hall daemon — spawns and manages raider processes
4  
5  
6 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 72 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 72 ▶use IO::Async::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 72 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 72 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 72 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 72 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 72 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 72 ▶use IO::Async::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 72 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 72 ▶use IO::Async::Timer::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 72 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 72 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 72 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 72 ▶use Langertha::Raider::Binary qw( packed_binary );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 72 ▶use Langertha::Raider::Hall::ACP;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 72 ▶use Langertha::Raider::Hall::Cron;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 72 ▶use Langertha::Raider::Hall::MCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 72 ▶use Langertha::Raider::Hall::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 72 ▶use Langertha::Raider::Hall::Raider;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 72 ▶use Langertha::Raider::Hall::Telegram;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 72 ▶use Langertha::Raider::SessionStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 72 has root => (
33 is => 'ro',
34 isa => 'Path::Tiny',
35 required => 1,
36 );
37  
38 72 has loop => (
39 is => 'ro',
40 lazy => 1,
41 builder => '_build_loop',
42 );
43  
44 has _loop => (
45 is => 'ro',
46 init_arg => undef,
47 110 ▶ default => sub { IO::Async::Loop->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 72 );
49  
50 60 ▶sub _build_loop { $_[0]->_loop }
Subroutine _build_loop: called CC=1 SCAR=0.0
51  
52 72 has config => (
53 is => 'ro',
54 isa => 'HashRef',
55 lazy => 1,
56 builder => '_build_config',
57 );
58  
59 69 ▶sub _build_config {
Subroutine _build_config: called CC=2 SCAR=6.9
60 69 my ($self) = @_;
61 69 my $yml_file = $self->root->child('.raider-hall.yml');
62 69 ▶ return {} unless -f $yml_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $yml_file 38 31 69
63 31 YAML::PP->new->load_string($yml_file->slurp_utf8);
64 }
65  
66 72 has socket_path => (
67 is => 'ro',
68 lazy => 1,
69 builder => '_build_socket_path',
70 );
71  
72 59 ▶sub _build_socket_path {
Subroutine _build_socket_path: called CC=1 SCAR=0.0
73 59 my ($self) = @_;
74 59 $self->root->child('.raider-hall.socket')->stringify;
75 }
76  
77 72 has mcpserver_socket_path => (
78 is => 'ro',
79 lazy => 1,
80 builder => '_build_mcpserver_socket_path',
81 );
82  
83 0 ▶sub _build_mcpserver_socket_path {
Subroutine _build_mcpserver_socket_path: not called CC=1 SCAR=6.9
84 0 my ($self) = @_;
85 0 $self->root->child('.raider-hall.mcp')->stringify;
86 }
87  
88 72 has longhouse_lib_path => (
89 is => 'ro',
90 lazy => 1,
91 builder => '_build_longhouse_lib_path',
92 );
93  
94 2 ▶sub _build_longhouse_lib_path {
Subroutine _build_longhouse_lib_path: called CC=1 SCAR=0.0
95 2 my ($self) = @_;
96 2 $self->root->child('longhouse', 'lib');
97 }
98  
99 72 has state_dir => (
100 is => 'ro',
101 lazy => 1,
102 builder => '_build_state_dir',
103 );
104  
105 88 ▶sub _build_state_dir {
Subroutine _build_state_dir: called CC=2 SCAR=6.9
106 88 my ($self) = @_;
107 88 my $d = $self->root->child('.raider-hall', 'state');
108 88 ▶ $d->mkpath unless -d $d;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d 72 16 88
109 88 $d;
110 }
111  
112 has raiders => (
113 is => 'ro',
114 110 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 72 );
116  
117  
118 31 ▶sub session_store_class { 'Langertha::Raider::SessionStore' }
Subroutine session_store_class: called CC=1 SCAR=0.0
Pod undocumented
119  
120 72 has session_store => (
121 is => 'ro',
122 lazy => 1,
123 builder => '_build_session_store',
124 );
125  
126 31 ▶sub _build_session_store {
Subroutine _build_session_store: called CC=1 SCAR=0.0
127 31 my ($self) = @_;
128 31 return $self->session_store_class->new(root => $self->root->stringify);
129 }
130  
131  
132 72 has session_bindings => (
133 is => 'ro',
134 isa => 'HashRef',
135 lazy => 1,
136 builder => '_build_session_bindings',
137 );
138  
139 78 ▶sub _session_bindings_file { $_[0]->state_dir->child('sessions.json') }
Subroutine _session_bindings_file: called CC=1 SCAR=0.0
140  
141 34 ▶sub _build_session_bindings {
Subroutine _build_session_bindings: called CC=3 SCAR=11.0
142 34 my ($self) = @_;
143 34 my $file = $self->_session_bindings_file;
144 34 ▶ return {} unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 30 4 34
145 4 my $map = eval { JSON::MaybeXS->new->decode($file->slurp_utf8) };
146 4 ▶ return ref $map eq 'HASH' ? $map : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $map eq 'HASH' ? : 4 0 4
147 }
148  
149 44 ▶sub _persist_session_bindings {
Subroutine _persist_session_bindings: called CC=1 SCAR=0.0
150 44 my ($self) = @_;
151 44 $self->_session_bindings_file->spew_utf8(
152 JSON::MaybeXS->new(canonical => 1)->encode($self->session_bindings));
153 }
154  
155  
156 58 ▶sub session_for {
Subroutine session_for: called CC=3 SCAR=11.0
Pod documented
157 58 my ($self, $binding) = @_;
158 58 my $bindings = $self->session_bindings;
159 58 my $store = $self->session_store;
160 58 my $id = $bindings->{$binding};
161 58 ▶ return $id if defined $id && $store->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $store->exists($id) 19 39 58
Condition100%3/3
defined $id and $store->exists($id)
!ll&&!rl&&r
38119
MC/DC100%2/2
defined $id and $store->exists($id)
defined $id $store->exists($id)
Truth table100%3/3
A and B
A= defined $idB= $store->exists($id)
ABresult
0X0
100
111
162 39 my $session = $store->create;
163 39 $id = $session->id;
164 39 $session->release;
165 39 $bindings->{$binding} = $id;
166 39 $self->_persist_session_bindings;
167 39 $self->_emit('session.bound', { binding => $binding, session => $id });
168 39 return $id;
169 }
170  
171  
172 5 ▶sub unbind_session {
Subroutine unbind_session: called CC=2 SCAR=6.9
Pod documented
173 5 my ($self, $binding) = @_;
174 5 ▶ return unless defined delete $self->session_bindings->{$binding};
Branch: 1 branch
BranchTrueFalseTotal
unless defined delete $self->session_bindings->{$binding} 1 4 5
175 4 $self->_persist_session_bindings;
176 4 return;
177 }
178  
179  
180 5 ▶sub reset_session {
Subroutine reset_session: called CC=4 SCAR=13.9
Pod documented
181 5 my ($self, $binding) = @_;
182 5 ▶ return { error => 'session_reset requires a binding' }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binding and length $binding 1 4 5
Condition66%2/3
defined $binding and length $binding
!ll&&!rl&&r
104
MC/DC50%1/2
defined $binding and length $binding
defined $binding length $binding
Truth table66%2/3
A and B
A= defined $bindingB= length $binding
ABresult
0X0
100
111
183 unless defined $binding && length $binding;
184 4 my $id = $self->session_bindings->{$binding};
185 4 ▶ return { error => 'no session bound to '.$binding } unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 2 2 4
186 2 $self->unbind_session($binding);
187 2 $self->_emit('session.reset', { binding => $binding, session => $id });
188 2 return { reset => 1, binding => $binding, session => $id };
189 }
190  
191 # ACP bindings live as long as their connection, which a hall restart
192 # ends: forget them and the prompts they left waiting. Journals stay.
193 3 ▶sub _forget_acp_bindings {
Subroutine _forget_acp_bindings: called CC=3 SCAR=11.0
194 3 my ($self) = @_;
195 3 my @acp = grep { /^acp:/ } keys %{$self->session_bindings};
196 3 delete @{$self->session_bindings}{@acp};
197 3 ▶ $self->_persist_session_bindings if @acp;
Branch: 1 branch
BranchTrueFalseTotal
if @acp 1 2 3
198 3 $self->drop_queued($_) for grep { /^acp:/ } $self->_waiting_bindings;
199 3 return;
200 }
201  
202 # Bindings of cron jobs no longer in the config are forgotten at a start,
203 # and the missions they left waiting are dropped. Journals stay.
204 2 ▶sub _forget_removed_cron_bindings {
Subroutine _forget_removed_cron_bindings: called CC=6 SCAR=18.1
205 2 my ($self) = @_;
206 1 ▶ my %job = map { ( 'cron:'.( $_->{id} // $_->{name} // '' ) => 1 ) }
Condition40%2/5
($_->{"id"} // $_->{"name"}) // ""
l!l
10
$_->{"id"} // $_->{"name"}
l!l&&r!l&&!r
100
MC/DC0%0/2
($_->{"id"} // $_->{"name"}) // ""
$_->{"id"} $_->{"name"}
Truth table33%1/3
A // B
A= $_->{"id"}B= $_->{"name"}
ABresult
1X1
011
000
207 2 ▶ @{ $self->config->{cron} // [] };
Condition100%2/2
$self->config->{"cron"} // []
l!l
11
MC/DC100%1/1
$self->config->{"cron"} // []
$self->config->{"cron"}
Truth table100%2/2
A
A= $self->config->{"cron"}
Aresult
11
00
208 2 ▶ my %gone = map { $_ => 1 } grep { /^cron:/ && !$job{$_} }
Condition100%3/3
/^cron:/ && !$job{$_}
!ll&&!rl&&r
112
MC/DC100%2/2
/^cron:/ && !$job{$_}
/^cron:/ !$job{$_}
Truth table100%3/3
A && not(B)
A= /^cron:/B= !$job{$_}
ABresult
0X0
100
111
209 2 keys %{$self->session_bindings}, $self->_waiting_bindings;
210 2 for my $binding (sort keys %gone) {
211 1 $self->unbind_session($binding);
212 1 $self->drop_queued($binding);
213 }
214 2 return;
215 }
216  
217 # The binding of a run: the one it was spawned with, else its numbered
218 # slot; a plain-name run has none.
219 98 ▶sub _binding_key {
Subroutine _binding_key: called CC=3 SCAR=11.0
220 98 my ($self, $slot, $binding) = @_;
221 98 ▶ return $binding if defined $binding;
Branch: 1 branch
BranchTrueFalseTotal
if defined $binding 31 67 98
222 67 ▶ return 'slot:'.$slot if $slot =~ /^\d+[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $slot =~ /^\d+[a-z]/ 16 51 67
223 51 return;
224 }
225  
226  
227 72 has singleton_queues => (
228 is => 'ro',
229 isa => 'HashRef',
230 lazy => 1,
231 builder => '_build_singleton_queues',
232 );
233  
234 78 ▶sub _build_singleton_queues {
Subroutine _build_singleton_queues: called CC=3 SCAR=11.0
235 78 my ($self) = @_;
236 78 my %queues;
237 78 for my $queue_file ($self->state_dir->children(qr/^\d+[a-z][-a-z0-9]*\.queue\.json$/)) {
238 8 my $slot = $queue_file->basename =~ s/\.queue\.json$//r;
239 8 my $q = eval { JSON::MaybeXS->new->decode($queue_file->slurp_utf8) };
240 8 ▶ $queues{$slot} = ref $q eq 'ARRAY' ? $q : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $q eq 'ARRAY' ? : 8 0 8
241 }
242 78 return \%queues;
243 }
244  
245 # Start the missions waiting for a numbered slot, once nothing runs in it.
246 # One whose binding is busy moves on to that binding's queue, and the next
247 # one gets the slot.
248 61 ▶sub _drain_singleton_queue {
Subroutine _drain_singleton_queue: called CC=4 SCAR=13.9
249 61 my ($self, $slot) = @_;
250 61 ▶ my $queue = $self->singleton_queues->{$slot} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $queue = $self->singleton_queues->{$slot} or return 31 30 61
251 31 my ($base) = $slot =~ /^\d+(.+)$/;
252 31 ▶ while (@$queue && !$self->_slot_busy($slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue and not $self->_slot_busy($slot)) 14 31 45
Condition100%3/3
@$queue and not $self->_slot_busy($slot)
!ll&&!rl&&r
24714
MC/DC100%2/2
@$queue and not $self->_slot_busy($slot)
@$queue not $self->_slot_busy($slot)
Truth table100%3/3
A and not(B)
A= @$queueB= not $self->_slot_busy($slot)
ABresult
0X0
100
111
253 14 my $next = shift @$queue;
254 14 $self->_persist_queue($slot);
255 14 $self->_spawn_next_in_queue($slot, $base, $next);
256 }
257 31 return;
258 }
259  
260 3 ▶sub _drain_singleton_queues {
Subroutine _drain_singleton_queues: called CC=2 SCAR=6.9
261 3 my ($self) = @_;
262 3 $self->_drain_singleton_queue($_) for sort keys %{$self->singleton_queues};
263 3 return;
264 }
265  
266  
267 6 ▶sub drop_queued {
Subroutine drop_queued: called CC=5 SCAR=16.1
Pod documented
268 6 my ($self, $binding) = @_;
269 6 my $dropped = 0;
270 6 ▶ if (my $queue = delete $self->binding_queues->{$binding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $queue = delete $self->binding_queues->{$binding}) 3 3 6
271 3 $dropped += @$queue;
272 3 $self->_persist_binding_queues;
273 }
274 6 my $slots = $self->singleton_queues;
275 6 for my $slot (sort keys %$slots) {
276 2 my $queue = $slots->{$slot};
277 2 ▶ my @keep = grep { ($_->{binding} // '') ne $binding } @$queue;
Condition100%2/2
$_->{"binding"} // ""
l!l
21
MC/DC100%1/1
$_->{"binding"} // ""
$_->{"binding"}
Truth table100%2/2
A
A= $_->{"binding"}
Aresult
11
00
278 2 ▶ next if @keep == @$queue;
Branch: 1 branch
BranchTrueFalseTotal
if @keep == @$queue 0 2 2
279 2 $dropped += @$queue - @keep;
280 2 @$queue = @keep;
281 2 $self->_persist_queue($slot);
282 }
283 6 return $dropped;
284 }
285  
286 # Every binding a waiting mission names, in the binding queues or the slot
287 # queues.
288 9 ▶sub _waiting_bindings {
Subroutine _waiting_bindings: called CC=3 SCAR=11.0
289 9 my ($self) = @_;
290 9 my %waiting = map { $_ => 1 } keys %{$self->binding_queues};
291 9 for my $queue (values %{$self->singleton_queues}) {
292 3 $waiting{$_->{binding}} = 1 for grep { defined $_->{binding} } @$queue;
293 }
294 9 return sort keys %waiting;
295 }
296  
297  
298 72 has binding_queues => (
299 is => 'ro',
300 isa => 'HashRef',
301 lazy => 1,
302 builder => '_build_binding_queues',
303 );
304  
305 95 ▶sub _binding_queues_file { $_[0]->state_dir->child('binding_queues.json') }
Subroutine _binding_queues_file: called CC=1 SCAR=0.0
306  
307 77 ▶sub _build_binding_queues {
Subroutine _build_binding_queues: called CC=3 SCAR=11.0
308 77 my ($self) = @_;
309 77 my $file = $self->_binding_queues_file;
310 77 ▶ return {} unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 68 9 77
311 9 my $queues = eval { JSON::MaybeXS->new->decode($file->slurp_utf8) };
312 9 ▶ return ref $queues eq 'HASH' ? $queues : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $queues eq 'HASH' ? : 9 0 9
313 }
314  
315 18 ▶sub _persist_binding_queues {
Subroutine _persist_binding_queues: called CC=1 SCAR=0.0
316 18 my ($self) = @_;
317 18 $self->_binding_queues_file->spew_utf8(
318 JSON::MaybeXS->new(canonical => 1)->encode($self->binding_queues));
319 }
320  
321 127 ▶sub _binding_busy {
Subroutine _binding_busy: called CC=2 SCAR=6.9
322 127 my ($self, $binding) = @_;
323 127 ▶ return scalar grep { $_->has_binding && $_->binding eq $binding } values %{$self->raiders};
Condition100%3/3
$_->has_binding && $_->binding eq $binding
!ll&&!rl&&r
12627
MC/DC100%2/2
$_->has_binding && $_->binding eq $binding
$_->has_binding $_->binding eq $binding
Truth table100%3/3
A && B
A= $_->has_bindingB= $_->binding eq $binding
ABresult
0X0
100
111
324 }
325  
326 5 ▶sub _queue_on_binding {
Subroutine _queue_on_binding: called CC=2 SCAR=6.9
327 5 my ($self, $binding, $entry) = @_;
328 5 ▶ my $queue = $self->binding_queues->{$binding} //= [];
Condition100%2/2
$self->binding_queues->{$binding} //= []
l!l
14
MC/DC100%1/1
$self->binding_queues->{$binding} //= []
$self->binding_queues->{$binding}
Truth table100%2/2
A
A= $self->binding_queues->{$binding}
Aresult
11
00
329 5 push @$queue, $entry;
330 5 $self->_persist_binding_queues;
331 $self->_emit('raider.queued', {
332 slot => $entry->{name},
333 5 binding => $binding,
334 queue_depth => scalar @$queue,
335 });
336 5 return { queued => 1, slot => $entry->{name}, binding => $binding, queue_depth => scalar @$queue };
337 }
338  
339 # Start the missions waiting for a binding, once nothing runs on it. One
340 # that has to wait for its slot moves on to the slot queue, and the next
341 # one follows it there.
342 78 ▶sub _drain_binding_queue {
Subroutine _drain_binding_queue: called CC=5 SCAR=16.1
343 78 my ($self, $binding) = @_;
344 78 my $queues = $self->binding_queues;
345 78 ▶ while (!$self->_binding_busy($binding)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_binding_busy($binding)) 65 22 87
346 65 ▶ my $queue = $queues->{$binding} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $queue = $queues->{$binding} or return 9 56 65
347 9 my $next = shift @$queue;
348 9 ▶ delete $queues->{$binding} unless @$queue;
Branch: 1 branch
BranchTrueFalseTotal
unless @$queue 6 3 9
349 9 $self->_persist_binding_queues;
350 9 ▶ $self->_spawn(%$next) if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 9 0 9
351 }
352 22 return;
353 }
354  
355 3 ▶sub _drain_binding_queues {
Subroutine _drain_binding_queues: called CC=2 SCAR=6.9
356 3 my ($self) = @_;
357 3 $self->_drain_binding_queue($_) for sort keys %{$self->binding_queues};
358 3 return;
359 }
360  
361 72 has cron_scheduler => (
362 is => 'ro',
363 lazy => 1,
364 builder => '_build_cron_scheduler',
365 );
366  
367 4 ▶sub _build_cron_scheduler {
Subroutine _build_cron_scheduler: called CC=1 SCAR=0.0
368 4 my ($self) = @_;
369 4 Langertha::Raider::Hall::Cron->new(hall => $self);
370 }
371  
372 72 has telegram => (
373 is => 'ro',
374 lazy => 1,
375 builder => '_build_telegram',
376 );
377  
378 3 ▶sub _build_telegram {
Subroutine _build_telegram: called CC=1 SCAR=0.0
379 3 my ($self) = @_;
380 3 Langertha::Raider::Hall::Telegram->new(hall => $self);
381 }
382  
383 72 has mcp_adapter => (
384 is => 'ro',
385 lazy => 1,
386 builder => '_build_mcp_adapter',
387 );
388  
389 2 ▶sub _build_mcp_adapter {
Subroutine _build_mcp_adapter: called CC=1 SCAR=0.0
390 2 my ($self) = @_;
391 2 Langertha::Raider::Hall::MCP->new(hall => $self);
392 }
393  
394 72 has acp_adapter => (
395 is => 'ro',
396 lazy => 1,
397 builder => '_build_acp_adapter',
398 );
399  
400 1 ▶sub _build_acp_adapter {
Subroutine _build_acp_adapter: called CC=6 SCAR=22.7
401 1 my ($self) = @_;
402 1 ▶ my $conf = $self->config->{acp} // {};
Condition50%1/2
$self->config->{"acp"} // {}
l!l
10
MC/DC0%0/1
$self->config->{"acp"} // {}
$self->config->{"acp"}
Truth table50%1/2
A
A= $self->config->{"acp"}
Aresult
11
00
403 Langertha::Raider::Hall::ACP->new(
404 hall => $self,
405 port => ($ENV{RAIDER_HALL_ACP_PORT} // $conf->{port} // 0),
406 1 ▶ host => ($ENV{RAIDER_HALL_ACP_HOST} // $conf->{host} // '127.0.0.1'),
Condition40%4/10
($ENV{"RAIDER_HALL_ACP_PORT"} // $conf->{"port"}) // 0
l!l
10
$ENV{"RAIDER_HALL_ACP_PORT"} // $conf->{"port"}
l!l&&r!l&&!r
010
($ENV{"RAIDER_HALL_ACP_HOST"} // $conf->{"host"}) // "127.0.0.1"
l!l
10
$ENV{"RAIDER_HALL_ACP_HOST"} // $conf->{"host"}
l!l&&r!l&&!r
010
MC/DC0%0/2
($ENV{"RAIDER_HALL_ACP_PORT"} // $conf->{"port"}) // 0
$ENV{"RAIDER_HALL_ACP_PORT"} $conf->{"port"}
MC/DC0%0/2
($ENV{"RAIDER_HALL_ACP_HOST"} // $conf->{"host"}) // "127.0.0.1"
$ENV{"RAIDER_HALL_ACP_HOST"} $conf->{"host"}
Truth table33%1/3
A // B
A= $ENV{"RAIDER_HALL_ACP_PORT"}B= $conf->{"port"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $ENV{"RAIDER_HALL_ACP_HOST"}B= $conf->{"host"}
ABresult
1X1
011
000
407 );
408 }
409  
410 72 has protocol => (
411 is => 'ro',
412 lazy => 1,
413 builder => '_build_protocol',
414 );
415  
416 6 ▶sub _build_protocol {
Subroutine _build_protocol: called CC=1 SCAR=0.0
417 6 my ($self) = @_;
418 6 my $p = Langertha::Raider::Hall::Protocol->new(hall => $self);
419 6 $p->setup_handlers;
420 6 $p;
421 }
422  
423 110 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
424 110 my ($self) = @_;
425 110 ▶ $self->root->mkpath unless -d $self->root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->root 0 110 110
426 }
427  
428 2 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod undocumented
429 2 my ($self) = @_;
430 2 my $loop = $self->loop;
431  
432 2 $self->_setup_socket;
433 2 ▶ $self->_setup_mcp_socket if $self->_want_mcp_socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_want_mcp_socket 0 2 2
434 2 $self->_setup_event_broadcaster;
435 2 $self->_setup_signal_handlers;
436 2 $self->protocol;
437 2 $self->_setup_cron;
438 2 $self->_setup_telegram;
439 2 $self->_setup_acp;
440 2 $self->_forget_acp_bindings;
441 2 $self->_forget_removed_cron_bindings;
442 2 $self->_drain_singleton_queues;
443 2 $self->_drain_binding_queues;
444  
445 2 $self->_emit('hall.started', { root => $self->root->stringify });
446  
447 2 $loop->run;
448 }
449  
450 2 ▶sub _want_mcp_socket {
Subroutine _want_mcp_socket: called CC=2 SCAR=7.6
451 2 my ($self) = @_;
452 2 my $conf = $self->config;
453 2 ▶ return $conf->{mcp} && $conf->{mcp}{enable};
Condition33%1/3
$conf->{'mcp'} && $conf->{'mcp'}{'enable'}
!ll&&!rl&&r
200
MC/DC0%0/2
$conf->{'mcp'} && $conf->{'mcp'}{'enable'}
$conf->{'mcp'} $conf->{'mcp'}{'enable'}
Truth table33%1/3
A && B
A= $conf->{'mcp'}B= $conf->{'mcp'}{'enable'}
ABresult
0X0
100
111
454 }
455  
456 1 ▶sub _setup_socket {
Subroutine _setup_socket: called CC=3 SCAR=11.5
457 1 my ($self) = @_;
458 1 my $loop = $self->loop;
459 1 my $path = $self->socket_path;
460  
461 1 ▶ my $sock = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::UNIX"->new("Local", $path, "Listen", 1) or die "Cannot create UNIX socket at ${path}: $!" 1 0 1
462 Local => $path,
463 Listen => 1,
464 ) or die "Cannot create UNIX socket at $path: $!";
465  
466 1 ▶ chmod 0600, $path or die "Cannot chmod 0600 $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod 0600, $path or die "Cannot chmod 0600 ${path}: $!" 1 0 1
467  
468 my $listener = IO::Async::Listener->new(
469 0 ▶ on_accept => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
470 0 my ($listener, $sock, $peeraddr) = @_;
471 0 $self->_handle_client($sock);
472 },
473 1 );
474  
475 1 $loop->add($listener);
476  
477 1 $listener->listen(handle => $sock);
478 1 $self->{_listener} = $listener;
479 }
480  
481 0 ▶sub _setup_mcp_socket {
Subroutine _setup_mcp_socket: not called CC=1 SCAR=6.9
482 0 my ($self) = @_;
483 }
484  
485 2 ▶sub _setup_event_broadcaster {
Subroutine _setup_event_broadcaster: called CC=1 SCAR=0.0
486 2 my ($self) = @_;
487 2 $self->{_subscribers} = [];
488 }
489  
490 1 ▶sub _setup_cron {
Subroutine _setup_cron: called CC=3 SCAR=15.1
491 1 my ($self) = @_;
492 1 ▶ return unless $self->config->{cron} && @{$self->config->{cron}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"cron"} and @{$self->config->{"cron"};} 1 0 1
Condition33%1/3
$self->config->{"cron"} and @{$self->config->{"cron"};}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->config->{"cron"} and @{$self->config->{"cron"};}
$self->config->{"cron"} @{$self->config->{"cron"};}
Truth table33%1/3
A and B
A= $self->config->{"cron"}B= @{$self->config->{"cron"};}
ABresult
0X0
100
111
493 0 $self->cron_scheduler->start;
494 }
495  
496 2 ▶sub _setup_telegram {
Subroutine _setup_telegram: called CC=3 SCAR=14.2
497 2 my ($self) = @_;
498 2 ▶ return unless $self->config->{telegram} && $self->config->{telegram}{bots};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"telegram"} and $self->config->{"telegram"}{"bots"} 2 0 2
Condition33%1/3
$self->config->{"telegram"} and $self->config->{"telegram"}{"bots"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->config->{"telegram"} and $self->config->{"telegram"}{"bots"}
$self->config->{"telegram"} $self->config->{"telegram"}{"bots"}
Truth table33%1/3
A and B
A= $self->config->{"telegram"}B= $self->config->{"telegram"}{"bots"}
ABresult
0X0
100
111
499 0 $self->telegram->setup_bots;
500 }
501  
502 2 ▶sub _setup_acp {
Subroutine _setup_acp: called CC=4 SCAR=14.0
503 2 my ($self) = @_;
504 my $enabled = $ENV{RAIDER_HALL_ACP_PORT}
505 2 ▶ || ($self->config->{acp} && $self->config->{acp}{port});
Condition66%4/6
$ENV{'RAIDER_HALL_ACP_PORT'} || $self->config->{'acp'} && $self->config->{'acp'}{'port'}
l!l&&r!l&&!r
011
$self->config->{"acp"} and $self->config->{"acp"}{"port"}
!ll&&!rl&&r
101
MC/DC33%1/3
$ENV{'RAIDER_HALL_ACP_PORT'} || $self->config->{'acp'} && $self->config->{'acp'}{'port'}
$ENV{'RAIDER_HALL_ACP_PORT'} $self->config->{"acp"} $self->config->{"acp"}{"port"}
Truth table50%2/4
A || B and C
A= $ENV{'RAIDER_HALL_ACP_PORT'}B= $self->config->{"acp"}C= $self->config->{"acp"}{"port"}
ABCresult
1XX1
0111
00X0
0100
506 2 ▶ return unless $enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 1 1 2
507 1 $self->acp_adapter->start;
508 }
509  
510 1 ▶sub _acp_running {
Subroutine _acp_running: called CC=3 SCAR=14.2
511 1 my ($self) = @_;
512 return $ENV{RAIDER_HALL_ACP_PORT}
513 1 ▶ || ($self->config->{acp} && $self->config->{acp}{port});
Condition33%2/6
$ENV{'RAIDER_HALL_ACP_PORT'} || $self->config->{'acp'} && $self->config->{'acp'}{'port'}
l!l&&r!l&&!r
010
$self->config->{"acp"} and $self->config->{"acp"}{"port"}
!ll&&!rl&&r
001
MC/DC0%0/3
$ENV{'RAIDER_HALL_ACP_PORT'} || $self->config->{'acp'} && $self->config->{'acp'}{'port'}
$ENV{'RAIDER_HALL_ACP_PORT'} $self->config->{"acp"} $self->config->{"acp"}{"port"}
Truth table25%1/4
A || B and C
A= $ENV{'RAIDER_HALL_ACP_PORT'}B= $self->config->{"acp"}C= $self->config->{"acp"}{"port"}
ABCresult
1XX1
0111
00X0
0100
514 }
515  
516 0 ▶sub _handle_client {
Subroutine _handle_client: not called CC=1 SCAR=6.9
517 0 my ($self, $sock) = @_;
518 my $stream = IO::Async::Stream->new(
519 handle => $sock,
520 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
521 0 my ($stream, $bufref, $eof) = @_;
522 0 $self->_process_client_frames($stream, $bufref, $eof);
523 },
524 0 ▶ on_closed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
525 0 my ($stream) = @_;
526 0 $self->_unsubscribe_stream($stream);
527 },
528 0 );
529 0 $self->loop->add($stream);
530 0 push @{$self->{_client_streams}}, $stream;
531 }
532  
533 0 ▶sub _process_client_frames {
Subroutine _process_client_frames: not called CC=10 SCAR=47.0
534 0 my ($self, $stream, $bufref, $eof) = @_;
535 0 ▶ return unless $$bufref =~ s/^(.*?)\n//;
Branch: 1 branch
BranchTrueFalseTotal
unless $$bufref =~ s/^(.*?)\n// 0 0 0
536 0 my $line = $1;
537 0 ▶ return if $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 0 0
538  
539 0 my $msg = eval { JSON::MaybeXS->new->decode($line) };
540 0 ▶ if (!$msg || $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $msg or $@) 0 0 0
Condition0%0/3
not $msg or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
not $msg or $@
not $msg $@
Truth table0%0/3
not(A) or B
A= not $msgB= $@
ABresult
1X1
011
000
541 0 my $err = JSON::MaybeXS->new->encode({error => "invalid JSON: $@"});
542 0 $stream->write("$err\n");
543 0 return;
544 }
545  
546 0 ▶ my $type = $msg->{type} // '';
Condition0%0/2
$msg->{"type"} // ""
l!l
00
MC/DC0%0/1
$msg->{"type"} // ""
$msg->{"type"}
Truth table0%0/2
A
A= $msg->{"type"}
Aresult
11
00
547 0 ▶ my $payload = $msg->{payload} // {};
Condition0%0/2
$msg->{"payload"} // {}
l!l
00
MC/DC0%0/1
$msg->{"payload"} // {}
$msg->{"payload"}
Truth table0%0/2
A
A= $msg->{"payload"}
Aresult
11
00
548  
549 0 ▶ if ($type eq 'subscribe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "subscribe") 0 0 0
550 0 ▶ push @{$self->{_subscribers}}, { stream => $stream, filter => $payload->{filter} // '' };
Condition0%0/2
$payload->{"filter"} // ""
l!l
00
MC/DC0%0/1
$payload->{"filter"} // ""
$payload->{"filter"}
Truth table0%0/2
A
A= $payload->{"filter"}
Aresult
11
00
551 0 return;
552 }
553  
554 0 ▶ if ($type eq 'command') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "command") 0 0 0
555 0 $self->_handle_command($stream, $payload);
556 0 return;
557 }
558  
559 0 my $err = JSON::MaybeXS->new->encode({error => "unknown message type: $type"});
560 0 $stream->write("$err\n");
561 }
562  
563 5 ▶sub _handle_command {
Subroutine _handle_command: called CC=4 SCAR=16.9
564 5 my ($self, $stream, $payload) = @_;
565 5 ▶ my $cmd = $payload->{cmd} // '';
Condition50%1/2
$payload->{"cmd"} // ""
l!l
50
MC/DC0%0/1
$payload->{"cmd"} // ""
$payload->{"cmd"}
Truth table50%1/2
A
A= $payload->{"cmd"}
Aresult
11
00
566  
567 5 my $handler = $self->{_cmd_handlers}{$cmd};
568 5 ▶ if (!$handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 0 5 5
569 0 my $err = JSON::MaybeXS->new->encode({error => "unknown command: $cmd"});
570 0 $stream->write("$err\n");
571 0 return;
572 }
573  
574 5 eval { $handler->($self, $stream, $payload) };
575 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
576 0 my $err = JSON::MaybeXS->new->encode({error => "command failed: $@"});
577 0 $stream->write("$err\n");
578 }
579 }
580  
581 261 ▶sub _emit {
Subroutine _emit: called CC=11 SCAR=24.3
582 261 my ($self, $type, $data) = @_;
583 261 $data->{type} = $type;
584 261 ▶ $data->{ts} //= time();
Condition33%1/3
$data->{"ts"} //= time
l!l&&r!l&&!r
02610
MC/DC0%0/2
$data->{"ts"} //= time
$data->{"ts"} time
Truth table33%1/3
A //= B
A= $data->{"ts"}B= time
ABresult
1X1
011
000
585  
586 261 my $json = JSON::MaybeXS->new->encode($data);
587 # Closed streams leave the list; a subscriber whose filter does not match
588 # stays, and so does one added while this event went out.
589 261 my %closed;
590 261 ▶ for my $sub (@{[ @{$self->{_subscribers} // []} ]}) {
Condition100%2/2
$self->{"_subscribers"} // []
l!l
20061
MC/DC100%1/1
$self->{"_subscribers"} // []
$self->{"_subscribers"}
Truth table100%2/2
A
A= $self->{"_subscribers"}
Aresult
11
00
591 39 my $stream = $sub->{stream};
592 39 ▶ unless ($stream && $stream->handle && $stream->handle->opened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stream and $stream->handle and $stream->handle->opened) 5 34 39
Condition50%3/6
$stream and $stream->handle and $stream->handle->opened
!ll&&!rl&&r
0534
$stream and $stream->handle
!ll&&!rl&&r
0039
MC/DC33%1/3
$stream and $stream->handle and $stream->handle->opened
$stream $stream->handle $stream->handle->opened
Truth table50%2/4
A and B and C
A= $streamB= $stream->handleC= $stream->handle->opened
ABCresult
0XX0
10X0
1100
1111
593 5 $closed{$sub} = 1;
594 5 next;
595 }
596 34 ▶ my $filter = $sub->{filter} // '';
Condition50%1/2
$sub->{"filter"} // ""
l!l
340
MC/DC0%0/1
$sub->{"filter"} // ""
$sub->{"filter"}
Truth table50%1/2
A
A= $sub->{"filter"}
Aresult
11
00
597 34 ▶ next if $filter ne '' && substr($type, 0, length($filter)) ne $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter ne "" and substr($type, 0, length $filter) ne $filter 1 33 34
Condition100%3/3
$filter ne "" and substr($type, 0, length $filter) ne $filter
!ll&&!rl&&r
11221
MC/DC100%2/2
$filter ne "" and substr($type, 0, length $filter) ne $filter
$filter ne "" substr($type, 0, length $filter) ne $filter
Truth table100%3/3
A and B
A= $filter ne ""B= substr($type, 0, length $filter) ne $filter
ABresult
0X0
100
111
598 33 eval { $stream->write("$json\n") };
599 }
600 261 ▶ $self->{_subscribers} = [ grep { !$closed{$_} } @{$self->{_subscribers} // []} ];
Condition100%2/2
$self->{"_subscribers"} // []
l!l
20061
MC/DC100%1/1
$self->{"_subscribers"} // []
$self->{"_subscribers"}
Truth table100%2/2
A
A= $self->{"_subscribers"}
Aresult
11
00
601 }
602  
603 0 ▶sub _broadcast_to_socket {
Subroutine _broadcast_to_socket: not called CC=1 SCAR=6.9
604 0 my ($self, $type, $data) = @_;
605 0 $self->_emit($type, $data);
606 }
607  
608 0 ▶sub _unsubscribe_stream {
Subroutine _unsubscribe_stream: not called CC=1 SCAR=6.9
609 0 my ($self, $stream) = @_;
610 0 @{$self->{_subscribers}} = grep { $_->{stream} ne $stream } @{$self->{_subscribers}};
611 0 @{$self->{_client_streams}} = grep { $_ ne $stream } @{$self->{_client_streams}};
612 }
613  
614 48 ▶sub _register_cmd {
Subroutine _register_cmd: called CC=1 SCAR=0.0
615 48 my ($self, $name, $handler) = @_;
616 48 $self->{_cmd_handlers}{$name} = $handler;
617 }
618  
619 34 ▶sub _persist_queue {
Subroutine _persist_queue: called CC=2 SCAR=7.0
620 34 my ($self, $slot) = @_;
621 34 ▶ my $queue = $self->singleton_queues->{$slot} // [];
Condition50%1/2
$self->singleton_queues->{$slot} // []
l!l
340
MC/DC0%0/1
$self->singleton_queues->{$slot} // []
$self->singleton_queues->{$slot}
Truth table50%1/2
A
A= $self->singleton_queues->{$slot}
Aresult
11
00
622 34 my $file = $self->state_dir->child("$slot.queue.json");
623 34 $file->spew_utf8(JSON::MaybeXS->new->encode($queue));
624 }
625  
626 1 ▶sub _setup_signal_handlers {
Subroutine _setup_signal_handlers: called CC=1 SCAR=0.0
627 1 my ($self) = @_;
628 1 my $loop = $self->loop;
629  
630 # CHLD is handled per-process via IO::Async::Process->on_finish; no
631 # global watcher needed (and mixing would double-reap).
632  
633 1 ▶ $loop->watch_signal(TERM => sub { $self->shutdown });
Subroutine __ANON__: called CC=1 SCAR=0.0
634 1 ▶ $loop->watch_signal(INT => sub { $self->shutdown });
Subroutine __ANON__: not called CC=1 SCAR=1.2
635 }
636  
637 97 ▶sub _reap_raider {
Subroutine _reap_raider: called CC=7 SCAR=19.5
638 97 my ($self, $pid, $status) = @_;
639 97 my $raider = $self->_find_raider_by_pid($pid);
640 97 ▶ return unless $raider;
Branch: 1 branch
BranchTrueFalseTotal
unless $raider 0 97 97
641  
642 97 my $slot = $raider->slot_name;
643 97 my $result = $self->_raider_result($raider, $status);
644 97 ▶ $result->{session} = $raider->session_id if $raider->has_session_id;
Branch: 1 branch
BranchTrueFalseTotal
if $raider->has_session_id 46 51 97
645 # Before the raider leaves the table: logs --follow stops once it is gone.
646 97 $self->_log_result($raider, $result);
647 97 ▶ $self->_emit('raider.done', {
Branch: 2 branches
BranchTrueFalseTotal
$status & 127 ? : 11 86 97
$raider->has_binding ? : 46 51 97
648 id => $raider->id,
649 slot => $slot,
650 pid => $pid,
651 exit_code => $status >> 8,
652 signaled => ($status & 127) ? 1 : 0,
653 %$result,
654 $raider->has_binding ? ( binding => $raider->binding ) : (),
655 });
656  
657 97 delete $self->raiders->{$raider->id};
658 97 $self->_prune_events($slot);
659  
660 97 ▶ $self->_drain_singleton_queue($slot) if $slot =~ /^\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $slot =~ /^\d+/ 28 69 97
661  
662 97 ▶ $self->_drain_binding_queue($raider->binding) if $raider->has_binding;
Branch: 1 branch
BranchTrueFalseTotal
if $raider->has_binding 46 51 97
663 }
664  
665 # The run's outcome for raider.done: status plus response or error, taken
666 # from the last run.finished event of the run. Without one (killed, crashed
667 # before it could write it) the run counts as failed, with an error that
668 # says so instead of whatever text the process left behind.
669 81 ▶sub _result_of {
Subroutine _result_of: called CC=6 SCAR=18.1
670 81 my ($self, $doc) = @_;
671 return {
672 status => $doc->{status} // 'failed',
673 defined $doc->{response} ? ( response => $doc->{response} ) : (),
674 defined $doc->{error} ? ( error => $doc->{error} ) : (),
675 ref $doc->{session} eq 'HASH' && defined $doc->{session}{id}
676 81 ▶ ? ( session => $doc->{session}{id} ) : (),
Branch: 3 branches
BranchTrueFalseTotal
defined $doc->{'response'} ? : 78 3 81
defined $doc->{'error'} ? : 1 80 81
ref $doc->{'session'} eq 'HASH' && defined $doc->{'session'}{'id'} ? : 38 43 81
Condition60%3/5
$doc->{"status"} // "failed"
l!l
810
ref $doc->{"session"} eq "HASH" and defined $doc->{"session"}{"id"}
!ll&&!rl&&r
43038
MC/DC0%0/1
$doc->{"status"} // "failed"
$doc->{"status"}
MC/DC50%1/2
ref $doc->{"session"} eq "HASH" and defined $doc->{"session"}{"id"}
ref $doc->{"session"} eq "HASH" defined $doc->{"session"}{"id"}
Truth table50%1/2
A
A= $doc->{"status"}
Aresult
11
00
Truth table66%2/3
A and B
A= ref $doc->{"session"} eq "HASH"B= defined $doc->{"session"}{"id"}
ABresult
0X0
100
111
677 };
678 }
679  
680 97 ▶sub _raider_result {
Subroutine _raider_result: called CC=6 SCAR=17.9
681 97 my ($self, $raider, $status) = @_;
682 97 ▶ if (my $doc = $raider->run_finished) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $raider->run_finished) 80 17 97
683 80 return $self->_result_of($doc);
684 }
685 # Exit 4: the session is held by another writer; raider ran nothing.
686 17 ▶ if (!($status & 127) && ($status >> 8) == 4 && $raider->has_session_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $status & 127 and $status >> 8 == 4 and $raider->has_session_id) 1 16 17
Condition83%5/6
not $status & 127 and $status >> 8 == 4 and $raider->has_session_id
!ll&&!rl&&r
1601
not $status & 127 and $status >> 8 == 4
!ll&&!rl&&r
971
MC/DC66%2/3
not $status & 127 and $status >> 8 == 4 and $raider->has_session_id
not $status & 127 $status >> 8 == 4 $raider->has_session_id
Truth table75%3/4
not(A) and B and C
A= not $status & 127B= $status >> 8 == 4C= $raider->has_session_id
ABCresult
0XX0
10X0
1100
1111
687 return {
688 1 status => 'failed',
689 error => 'session '.$raider->session_id.' of '.$raider->binding
690 .' is in use by another run; mission not run',
691 };
692 }
693 16 ▶ my $how = ($status & 127) ? 'killed by signal '.($status & 127)
Branch: 1 branch
BranchTrueFalseTotal
$status & 127 ? : 9 7 16
694 : 'exit code '.($status >> 8);
695 return {
696 16 status => 'failed',
697 error => 'raider '.$raider->id.' ended without a result ('.$how.')',
698 };
699 }
700  
701 # One human-readable line per finished run at the end of the slot log, so
702 # logs and logs --follow show the outcome next to the raider's stderr. The
703 # full response stays in raider.done and the events file.
704 97 ▶sub _log_result {
Subroutine _log_result: called CC=1 SCAR=0.0
705 97 my ($self, $raider, $result) = @_;
706 97 $raider->log_path->append_utf8($self->_result_line($raider->id, $result));
707 }
708  
709 98 ▶sub _result_line {
Subroutine _result_line: called CC=4 SCAR=13.9
710 98 my ($self, $id, $result) = @_;
711 98 ▶ my $text = $result->{status} eq 'completed' ? $result->{response} : $result->{error};
Branch: 1 branch
BranchTrueFalseTotal
$result->{'status'} eq 'completed' ? : 78 20 98
712 98 ▶ $text = join ' ', split ' ', $text // '';
Condition100%2/2
$text // ""
l!l
962
MC/DC100%1/1
$text // ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
713 98 ▶ $text = substr($text, 0, 300).'...' if length $text > 300;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 300 1 97 98
714 98 return '[hall] raider '.$id.' '.$result->{status}.': '.$text."\n";
715 }
716  
717  
718 72 has keep_events => (
719 is => 'ro',
720 lazy => 1,
721 builder => '_build_keep_events',
722 );
723  
724 58 ▶sub _build_keep_events {
Subroutine _build_keep_events: called CC=3 SCAR=11.0
725 58 my ($self) = @_;
726 58 ▶ my $logs = $self->config->{logs} // {};
Condition100%2/2
$self->config->{"logs"} // {}
l!l
355
MC/DC100%1/1
$self->config->{"logs"} // {}
$self->config->{"logs"}
Truth table100%2/2
A
A= $self->config->{"logs"}
Aresult
11
00
727 58 ▶ return $logs->{keep_events} // 20;
Condition100%2/2
$logs->{"keep_events"} // 20
l!l
256
MC/DC100%1/1
$logs->{"keep_events"} // 20
$logs->{"keep_events"}
Truth table100%2/2
A
A= $logs->{"keep_events"}
Aresult
11
00
728 }
729  
730  
731 72 has max_log_size => (
732 is => 'ro',
733 lazy => 1,
734 builder => '_build_max_log_size',
735 );
736  
737 61 ▶sub _build_max_log_size {
Subroutine _build_max_log_size: called CC=3 SCAR=11.0
738 61 my ($self) = @_;
739 61 ▶ my $logs = $self->config->{logs} // {};
Condition100%2/2
$self->config->{"logs"} // {}
l!l
556
MC/DC100%1/1
$self->config->{"logs"} // {}
$self->config->{"logs"}
Truth table100%2/2
A
A= $self->config->{"logs"}
Aresult
11
00
740 61 ▶ return $logs->{max_log_size} // 1024 * 1024;
Condition100%2/2
$logs->{"max_log_size"} // 1048576
l!l
358
MC/DC100%1/1
$logs->{"max_log_size"} // 1048576
$logs->{"max_log_size"}
Truth table100%2/2
A
A= $logs->{"max_log_size"}
Aresult
11
00
741 }
742  
743 299 ▶sub _log_dir { $_[0]->root->child('.raider-hall', 'logs') }
Subroutine _log_dir: called CC=1 SCAR=0.0
744  
745 102 ▶sub _rotate_log {
Subroutine _rotate_log: called CC=5 SCAR=16.1
746 102 my ($self, $slot) = @_;
747 102 my $max = $self->max_log_size;
748 102 ▶ return unless $max > 0 && !$self->_slot_busy($slot);
Branch: 1 branch
BranchTrueFalseTotal
unless $max > 0 and not $self->_slot_busy($slot) 9 93 102
Condition100%3/3
$max > 0 and not $self->_slot_busy($slot)
!ll&&!rl&&r
1893
MC/DC100%2/2
$max > 0 and not $self->_slot_busy($slot)
$max > 0 not $self->_slot_busy($slot)
Truth table100%3/3
A and not(B)
A= $max > 0B= not $self->_slot_busy($slot)
ABresult
0X0
100
111
749 93 my $log = $self->_log_dir->child("${slot}.log");
750 93 ▶ return unless -f $log && -s $log > $max;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log and -s $log > $max 91 2 93
Condition100%3/3
-f $log and -s $log > $max
!ll&&!rl&&r
61302
MC/DC100%2/2
-f $log and -s $log > $max
-f $log -s $log > $max
Truth table100%3/3
A and B
A= -f $logB= -s $log > $max
ABresult
0X0
100
111
751 2 $log->move($self->_log_dir->child("${slot}.log.1"));
752 }
753  
754 97 ▶sub _prune_events {
Subroutine _prune_events: called CC=7 SCAR=19.5
755 97 my ($self, $slot) = @_;
756 97 my $keep = $self->keep_events;
757 97 my $dir = $self->_log_dir;
758 97 ▶ return unless $keep > 0 && -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep > 0 and -d $dir 3 94 97
Condition100%3/3
$keep > 0 and -d $dir
!ll&&!rl&&r
1294
MC/DC100%2/2
$keep > 0 and -d $dir
$keep > 0 -d $dir
Truth table100%3/3
A and B
A= $keep > 0B= -d $dir
ABresult
0X0
100
111
759 # SLOT-TIME or SLOT-TIME.N: the hyphen before TIME keeps SLOT-x-TIME of
760 # another slot out.
761 110 ▶ my @runs = sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] }
Condition66%2/3
$Langertha::Raider::Hall::a->[1] <=> $Langertha::Raider::Hall::b->[1] || $Langertha::Raider::Hall::a->[2] <=> $Langertha::Raider::Hall::b->[2]
l!l&&r!l&&!r
81290
MC/DC0%0/2
$Langertha::Raider::Hall::a->[1] <=> $Langertha::Raider::Hall::b->[1] || $Langertha::Raider::Hall::a->[2] <=> $Langertha::Raider::Hall::b->[2]
$Langertha::Raider::Hall::a->[1] <=> $Langertha::Raider::Hall::b->[1] $Langertha::Raider::Hall::a->[2] <=> $Langertha::Raider::Hall::b->[2]
Truth table66%2/3
A || B
A= $Langertha::Raider::Hall::a->[1] <=> $Langertha::Raider::Hall::b->[1]B= $Langertha::Raider::Hall::a->[2] <=> $Langertha::Raider::Hall::b->[2]
ABresult
1X1
011
000
762 94 ▶ map { $_->basename =~ /^\Q$slot\E-(\d+)(?:\.(\d+))?\.events\.jsonl$/ ? [ $_, $1, $2 // 1 ] : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->basename =~ /^\Q$slot\E-(\d+)(?:\.(\d+))?\.events\.jsonl$/ ? : 184 140 324
Condition100%2/2
$2 // 1
l!l
23161
MC/DC100%1/1
$2 // 1
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
763 $dir->children;
764 94 $_->[0]->remove for @runs[ 0 .. $#runs - $keep ];
765 }
766  
767 153 ▶sub _slot_busy {
Subroutine _slot_busy: called CC=1 SCAR=0.0
768 153 my ($self, $slot) = @_;
769 153 return scalar grep { $_->slot_name eq $slot } values %{$self->raiders};
770 }
771  
772 # The slots with at least one running raider, each named once.
773 1 ▶sub _running_slots {
Subroutine _running_slots: called CC=1 SCAR=0.0
774 1 my ($self) = @_;
775 1 my %slots = map { $_->slot_name => 1 } values %{$self->raiders};
776 1 return [ sort keys %slots ];
777 }
778  
779 97 ▶sub _find_raider_by_pid {
Subroutine _find_raider_by_pid: called CC=4 SCAR=14.2
780 97 my ($self, $pid) = @_;
781 97 for my $r (values %{$self->raiders}) {
782 101 ▶ return $r if $r->pid && $r->pid == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $r->pid and $r->pid == $pid 97 4 101
Condition66%2/3
$r->pid and $r->pid == $pid
!ll&&!rl&&r
0497
MC/DC50%1/2
$r->pid and $r->pid == $pid
$r->pid $r->pid == $pid
Truth table66%2/3
A and B
A= $r->pidB= $r->pid == $pid
ABresult
0X0
100
111
783 }
784 0 return;
785 }
786  
787 15 ▶sub _spawn_next_in_queue {
Subroutine _spawn_next_in_queue: called CC=5 SCAR=17.3
788 15 my ($self, $slot, $base_name, $mission) = @_;
789 15 my ($telegram, $binding);
790 15 ▶ if (ref $mission eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mission eq "HASH") 15 0 15
791 15 $telegram = $mission->{telegram};
792 15 $binding = $mission->{binding};
793 15 $mission = $mission->{mission};
794 }
795 15 ▶ return $self->_queue_on_binding($binding, {
Branch: 2 branches
BranchTrueFalseTotal
if defined $binding and $self->_binding_busy($binding) 0 15 15
$telegram ? : 0 0 0
Condition66%2/3
defined $binding and $self->_binding_busy($binding)
!ll&&!rl&&r
1140
MC/DC0%0/2
defined $binding and $self->_binding_busy($binding)
defined $binding $self->_binding_busy($binding)
Truth table66%2/3
A and B
A= defined $bindingB= $self->_binding_busy($binding)
ABresult
0X0
100
111
796 name => $slot,
797 mission => $mission,
798 $telegram ? ( telegram => $telegram ) : (),
799 binding => $binding,
800 }) if defined $binding && $self->_binding_busy($binding);
801 15 $self->_spawn_raider($slot, $base_name, $mission, $telegram, $binding);
802 }
803  
804 97 ▶sub spawn {
Subroutine spawn: called CC=2 SCAR=6.9
Pod undocumented
805 97 my ($self, %args) = @_;
806 # Missions already waiting for the binding go first.
807 97 ▶ $self->_drain_binding_queue($args{binding}) if defined $args{binding};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"binding"} 31 66 97
808 97 return $self->_spawn(%args);
809 }
810  
811 106 ▶sub _spawn {
Subroutine _spawn: called CC=14 SCAR=26.7
812 106 my ($self, %args) = @_;
813 106 ▶ my $name = $args{name} // '';
Condition50%1/2
$args{"name"} // ""
l!l
1060
MC/DC0%0/1
$args{"name"} // ""
$args{"name"}
Truth table50%1/2
A
A= $args{"name"}
Aresult
11
00
814 106 ▶ my $mission = $args{mission} // '';
Condition50%1/2
$args{"mission"} // ""
l!l
1060
MC/DC0%0/1
$args{"mission"} // ""
$args{"mission"}
Truth table50%1/2
A
A= $args{"mission"}
Aresult
11
00
815 106 my $telegram = $args{telegram};
816 106 my $binding = $args{binding};
817  
818 106 my ($slot, $base_name) = $self->_parse_name($name);
819  
820 # Only numbered names have a slot here: singletons, queued while busy.
821 # A plain name runs in parallel. Missions already waiting go first.
822 106 ▶ $self->_drain_singleton_queue($slot) if $slot;
Branch: 1 branch
BranchTrueFalseTotal
if $slot 31 75 106
823 106 ▶ if ($slot && $self->_slot_busy($slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot and $self->_slot_busy($slot)) 18 88 106
Condition100%3/3
$slot and $self->_slot_busy($slot)
!ll&&!rl&&r
751318
MC/DC100%2/2
$slot and $self->_slot_busy($slot)
$slot $self->_slot_busy($slot)
Truth table100%3/3
A and B
A= $slotB= $self->_slot_busy($slot)
ABresult
0X0
100
111
824 18 ▶ push @{$self->singleton_queues->{$slot} //= []}, {
Branch: 2 branches
BranchTrueFalseTotal
$telegram ? : 1 17 18
defined $binding ? : 8 10 18
Condition100%2/2
$self->singleton_queues->{$slot} //= []
l!l
612
MC/DC100%1/1
$self->singleton_queues->{$slot} //= []
$self->singleton_queues->{$slot}
Truth table100%2/2
A
A= $self->singleton_queues->{$slot}
Aresult
11
00
825 mission => $mission,
826 $telegram ? ( telegram => $telegram ) : (),
827 defined $binding ? ( binding => $binding ) : (),
828 };
829 18 $self->_persist_queue($slot);
830 18 my $depth = scalar @{$self->singleton_queues->{$slot}};
831 18 $self->_emit('raider.queued', {
832 slot => $slot,
833 queue_depth => $depth,
834 });
835 18 return { queued => 1, slot => $slot, queue_depth => $depth };
836 }
837  
838 # One writer per session: a mission for a binding that is running waits
839 # for it. Parallel runs of a plain name need their own bindings.
840 88 ▶ return $self->_queue_on_binding($binding, {
Branch: 2 branches
BranchTrueFalseTotal
if defined $binding and $self->_binding_busy($binding) 5 83 88
$telegram ? : 0 5 5
Condition100%3/3
defined $binding and $self->_binding_busy($binding)
!ll&&!rl&&r
56275
MC/DC100%2/2
defined $binding and $self->_binding_busy($binding)
defined $binding $self->_binding_busy($binding)
Truth table100%3/3
A and B
A= defined $bindingB= $self->_binding_busy($binding)
ABresult
0X0
100
111
841 name => $name,
842 mission => $mission,
843 $telegram ? ( telegram => $telegram ) : (),
844 binding => $binding,
845 }) if defined $binding && $self->_binding_busy($binding);
846  
847 83 ▶ return $self->_spawn_raider($slot // $name, $base_name // $name, $mission, $telegram, $binding);
Condition50%3/6
$slot // $name
l!l&&r!l&&!r
13700
$base_name // $name
l!l&&r!l&&!r
8300
MC/DC0%0/2
$slot // $name
$slot $name
MC/DC0%0/2
$base_name // $name
$base_name $name
Truth table66%2/3
A // B
A= $slotB= $name
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $base_nameB= $name
ABresult
1X1
011
000
848 }
849  
850 108 ▶sub _parse_name {
Subroutine _parse_name: called CC=2 SCAR=6.9
851 108 my ($self, $name) = @_;
852 108 ▶ if ($name =~ /^(\d+)([a-z][-a-z0-9]*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(\d+)([a-z][-a-z0-9]*)$/) 32 76 108
853 32 return ($1.$2, $2);
854 }
855 76 return (undef, $name);
856 }
857  
858 98 ▶sub _spawn_raider {
Subroutine _spawn_raider: called CC=20 SCAR=30.0
859 98 my ($self, $slot, $base_name, $mission, $telegram, $binding) = @_;
860  
861 98 ▶ my $raider_config = $self->config->{raiders}{$base_name} // {};
Condition100%2/2
$self->config->{"raiders"}{$base_name} // {}
l!l
2672
MC/DC100%1/1
$self->config->{"raiders"}{$base_name} // {}
$self->config->{"raiders"}{$base_name}
Truth table100%2/2
A
A= $self->config->{"raiders"}{$base_name}
Aresult
11
00
862 # No engine configured: leave it to raider (.raider.yml, then key autodetection).
863 98 my $engine = $raider_config->{engine};
864 98 my $model = $raider_config->{model};
865 98 ▶ my @packs = @{ $raider_config->{packs} // [] };
Condition100%2/2
$raider_config->{"packs"} // []
l!l
593
MC/DC100%1/1
$raider_config->{"packs"} // []
$raider_config->{"packs"}
Truth table100%2/2
A
A= $raider_config->{"packs"}
Aresult
11
00
866 # The persona is one more pack.
867 98 my $persona = $raider_config->{persona};
868 push @packs, $persona
869 98 ▶ if defined $persona && length $persona && !grep { $_ eq $persona } @packs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $persona and length $persona and not grep({$_ eq $persona;} @packs) 6 92 98
Condition83%5/6
defined $persona and length $persona and not grep({$_ eq $persona;} @packs)
!ll&&!rl&&r
9116
defined $persona and length $persona
!ll&&!rl&&r
9107
MC/DC66%2/3
defined $persona and length $persona and not grep({$_ eq $persona;} @packs)
defined $persona length $persona not grep({$_ eq $persona;} @packs)
Truth table75%3/4
A and B and not(C)
A= defined $personaB= length $personaC= not grep({$_ eq $persona;} @packs)
ABCresult
0XX0
10X0
1100
1111
870 98 my $lib_target = $self->lib_target;
871  
872 98 my @cmd = ($self->_raider_command, '--stream-json');
873 98 ▶ push @cmd, '--engine', $engine if $engine;
Branch: 1 branch
BranchTrueFalseTotal
if $engine 2 96 98
874 98 ▶ push @cmd, '--model', $model if $model;
Branch: 1 branch
BranchTrueFalseTotal
if $model 1 97 98
875 98 push @cmd, '--pack', $_ for @packs;
876 98 push @cmd, '-o', 'preferred_lib_target='.$lib_target;
877 98 push @cmd, '--root', $self->root->stringify;
878 # A bound run resumes its binding's session; an unbound one gets a
879 # fresh session from the raider itself.
880 98 $binding = $self->_binding_key($slot, $binding);
881 98 my ($session_id, $session_error);
882 98 ▶ if (defined $binding) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binding) 47 51 98
883 47 $session_id = eval { $self->session_for($binding) };
884 47 ▶ unless (defined $session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session_id) 1 46 47
885 1 $session_error = { binding => $binding, error => $@ =~ s/\s+\z//r };
886 1 undef $binding;
887 }
888 }
889 98 ▶ push @cmd, '--session', $session_id if defined $session_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $session_id 46 52 98
890 # Mission is one single argv (bin/raider does join(' ', @ARGV)).
891 98 push @cmd, '--', $mission;
892  
893 98 my $log_dir = $self->_log_dir;
894 98 ▶ $log_dir->mkpath unless -d $log_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $log_dir 51 47 98
895  
896 # stderr is the human-readable log, appended across runs of the slot;
897 # stdout is the event stream, a fresh file per run. The ID is SLOT-TIME,
898 # SLOT-TIME.2 and up when that one is taken in the same second; touching
899 # the file reserves it before the child opens it.
900 98 $self->_rotate_log($slot);
901 98 my $log_path = $log_dir->child("${slot}.log");
902 98 my $id = my $base_id = "$slot-" . time;
903 98 my $n = 1;
904 98 ▶ $id = $base_id.'.'.++$n while -e $log_dir->child("${id}.events.jsonl");
Branch: 1 branch
BranchTrueFalseTotal
-e $log_dir->child("$id.events.jsonl") and do { $id = $base_id . '.' . ++$n } 15 98 113
905 98 my $events_path = $log_dir->child("${id}.events.jsonl");
906 98 $events_path->touch;
907 98 $log_path->append_utf8($self->_start_line($id));
908 98 $self->_note_ignored_keys($base_name, $raider_config, $log_path);
909  
910 # perl_cpanm installs with --local-lib, so the modules land in lib/perl5.
911 my $extra_perl5lib = join ':', path($lib_target)->child('lib', 'perl5')->stringify,
912 98 ▶ ($self->config->{longhouse} ? $self->longhouse_lib_path->stringify : ());
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{'longhouse'} ? : 1 97 98
913  
914 # The Telegram chat (and forum topic) this raider answers; telegram_reply
915 # is bound to it. Never inherited from the hall's own env.
916 98 my %env = %ENV;
917 98 delete @env{qw( RAIDER_HALL_TELEGRAM_BOT RAIDER_HALL_TELEGRAM_CHAT_ID RAIDER_HALL_TELEGRAM_THREAD_ID )};
918 98 ▶ if ($telegram) {
Branch: 1 branch
BranchTrueFalseTotal
if ($telegram) 3 95 98
919 3 $env{RAIDER_HALL_TELEGRAM_BOT} = $telegram->{bot};
920 3 $env{RAIDER_HALL_TELEGRAM_CHAT_ID} = $telegram->{chat_id};
921 $env{RAIDER_HALL_TELEGRAM_THREAD_ID} = $telegram->{message_thread_id}
922 3 ▶ if defined $telegram->{message_thread_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $telegram->{"message_thread_id"} 1 2 3
923 }
924  
925 my $process = IO::Async::Process->new(
926 command => \@cmd,
927 setup => [
928 stdin => [ 'open', '<', '/dev/null' ],
929 stdout => [ 'open', '>', "$events_path" ],
930 stderr => [ 'open', '>>', "$log_path" ],
931 env => {
932 %env,
933 RAIDER_HALL_MODE => '1',
934 RAIDER_HALL_ROOT => $self->root->stringify,
935 RAIDER_HALL_SLOT => $slot,
936 RAIDER_HALL_SOCKET => $self->socket_path,
937 196 ▶ PERL5LIB => join(':', grep { defined && length }
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
00196
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
938 ($ENV{PERL5LIB}, $extra_perl5lib)),
939 },
940 ],
941 95 ▶ on_finish => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
942 95 my ($proc, $exitcode) = @_;
943 95 my $pid = $proc->pid;
944 95 ▶ $self->loop->later(sub { $self->_reap_raider($pid, $exitcode) });
Subroutine __ANON__: called CC=1 SCAR=0.0
945 },
946 98 );
947  
948 96 $self->loop->add($process);
949  
950 96 ▶ my $raider = Langertha::Raider::Hall::Raider->new({
Branch: 1 branch
BranchTrueFalseTotal
defined $session_id ? : 46 50 96
951 id => $id,
952 pid => $process->pid,
953 slot_name => $slot,
954 base_name => $base_name,
955 log_path => $log_path,
956 events_path => $events_path,
957 mission => $mission,
958 defined $session_id ? ( session_id => $session_id, binding => $binding ) : (),
959 });
960 96 $self->raiders->{$id} = $raider;
961  
962 96 $self->_emit('raider.spawned', {
963 id => $id,
964 pid => $process->pid,
965 slot => $slot,
966 base_name => $base_name,
967 $self->_session_fields($raider),
968 });
969 # The binding's session could not be had: the run goes on unbound.
970 96 ▶ $self->_emit('hall.session_error', { %$session_error, id => $id }) if $session_error;
Branch: 1 branch
BranchTrueFalseTotal
if $session_error 1 95 96
971  
972 return {
973 96 id => $id, pid => $process->pid, slot => $slot,
974 events_path => $events_path->stringify,
975 $self->_session_fields($raider),
976 };
977 }
978  
979 # session and binding of a bound run, for events and replies.
980 196 ▶sub _session_fields {
Subroutine _session_fields: called CC=2 SCAR=6.9
981 196 my ($self, $raider) = @_;
982 196 ▶ return () unless $raider->has_session_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $raider->has_session_id 102 94 196
983 94 return ( session => $raider->session_id, binding => $raider->binding );
984 }
985  
986 # The argv prefix that starts a raider. A Perl script runs with this perl;
987 # inside the standalone binary there is no such perl ($^X is a bare
988 # "perl" off PATH, and "perl <binary>" dies on the ELF), so whatever
989 # _raider_bin names is exec'd directly.
990 102 ▶sub _raider_command {
Subroutine _raider_command: called CC=2 SCAR=6.9
991 102 my ($self) = @_;
992 102 my $raider_bin = $self->_raider_bin;
993 102 ▶ return packed_binary() ? ($raider_bin) : ($^X, $raider_bin);
Branch: 1 branch
BranchTrueFalseTotal
packed_binary() ? : 3 99 102
994 }
995  
996 102 ▶sub _raider_bin {
Subroutine _raider_bin: called CC=6 SCAR=25.4
997 102 my ($self) = @_;
998 # Explicit override wins — useful in tests and non-standard installs.
999 return $ENV{RAIDER_HALL_RAIDER_BIN}
1000 102 ▶ if $ENV{RAIDER_HALL_RAIDER_BIN} && -x $ENV{RAIDER_HALL_RAIDER_BIN};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"RAIDER_HALL_RAIDER_BIN"} and -x $ENV{"RAIDER_HALL_RAIDER_BIN"} 100 2 102
Condition66%2/3
$ENV{"RAIDER_HALL_RAIDER_BIN"} and -x $ENV{"RAIDER_HALL_RAIDER_BIN"}
!ll&&!rl&&r
20100
MC/DC50%1/2
$ENV{"RAIDER_HALL_RAIDER_BIN"} and -x $ENV{"RAIDER_HALL_RAIDER_BIN"}
$ENV{"RAIDER_HALL_RAIDER_BIN"} -x $ENV{"RAIDER_HALL_RAIDER_BIN"}
Truth table66%2/3
A and B
A= $ENV{"RAIDER_HALL_RAIDER_BIN"}B= -x $ENV{"RAIDER_HALL_RAIDER_BIN"}
ABresult
0X0
100
111
1001  
1002 # The standalone binary starts its raiders with itself, whatever it is
1003 # called and whatever raider is on PATH.
1004 2 my $packed = packed_binary();
1005 2 ▶ return $packed if defined $packed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $packed 2 0 2
1006  
1007 # Otherwise: next to the currently-running script (raider-hall lives
1008 # alongside raider in a normal install), then $PATH.
1009 0 my $here = path($0)->absolute;
1010 0 my $sibling = $here->parent->child('raider');
1011 0 ▶ return $sibling->stringify if -x $sibling;
Branch: 1 branch
BranchTrueFalseTotal
if -x $sibling 0 0 0
1012 0 my $which = File::Which::which('raider');
1013 0 ▶ return $which if $which;
Branch: 1 branch
BranchTrueFalseTotal
if $which 0 0 0
1014 0 die "Cannot find 'raider' binary (set RAIDER_HALL_RAIDER_BIN or put it in \$PATH)";
1015 }
1016  
1017  
1018 72 has lib_target => (
1019 is => 'ro',
1020 lazy => 1,
1021 builder => '_build_lib_target',
1022 );
1023  
1024 58 ▶sub _build_lib_target {
Subroutine _build_lib_target: called CC=2 SCAR=6.9
1025 58 my ($self) = @_;
1026 58 ▶ my $target = $self->config->{preferred_lib_target} // '.raider/lib';
Condition100%2/2
$self->config->{"preferred_lib_target"} // ".raider/lib"
l!l
157
MC/DC100%1/1
$self->config->{"preferred_lib_target"} // ".raider/lib"
$self->config->{"preferred_lib_target"}
Truth table100%2/2
A
A= $self->config->{"preferred_lib_target"}
Aresult
11
00
1027 58 return path($target)->absolute($self->root)->stringify;
1028 }
1029  
1030 # Raider entry keys the hall does not read. A config that still carries
1031 # one gets a note in the slot log, once per raider and hall process.
1032 has _ignored_keys_noted => (
1033 is => 'ro',
1034 110 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
1035 72 );
1036  
1037 98 ▶sub _note_ignored_keys {
Subroutine _note_ignored_keys: called CC=3 SCAR=11.0
1038 98 my ($self, $base_name, $raider_config, $log_path) = @_;
1039 98 ▶ return if $self->_ignored_keys_noted->{$base_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ignored_keys_noted->{$base_name}++ 35 63 98
1040 63 my @keys = grep { exists $raider_config->{$_} } qw( isolated mcp );
1041 63 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 60 3 63
1042 3 $log_path->append_utf8('[hall] raider '.$base_name.': '.join(', ', @keys)
1043 ." in .raider-hall.yml ignored\n");
1044 }
1045  
1046 3 ▶sub ps {
Subroutine ps: called CC=3 SCAR=11.2
Pod undocumented
1047 3 my ($self) = @_;
1048 3 my @list;
1049 3 ▶ for my $r (sort { $a->slot_name cmp $b->slot_name || $a->id cmp $b->id }
Condition33%1/3
$a->slot_name cmp $b->slot_name || $a->id cmp $b->id
l!l&&r!l&&!r
010
MC/DC0%0/2
$a->slot_name cmp $b->slot_name || $a->id cmp $b->id
$a->slot_name cmp $b->slot_name $a->id cmp $b->id
Truth table33%1/3
A || B
A= $a->slot_name cmp $b->slot_nameB= $a->id cmp $b->id
ABresult
1X1
011
000
1050 3 values %{$self->raiders}) {
1051 3 push @list, {
1052 slot => $r->slot_name,
1053 pid => $r->pid,
1054 base_name => $r->base_name,
1055 mission => $r->mission,
1056 id => $r->id,
1057 $self->_session_fields($r),
1058 };
1059 }
1060 3 return @list;
1061 }
1062  
1063 2 ▶sub attach {
Subroutine attach: called CC=4 SCAR=14.2
Pod undocumented
1064 2 my ($self, $id) = @_;
1065 2 for my $r (values %{$self->raiders}) {
1066 1 ▶ next unless $r->id eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->id eq $id 0 1 1
1067 return {
1068 1 ▶ id => $r->id,
Branch: 1 branch
BranchTrueFalseTotal
$r->has_events_path ? : 1 0 1
1069 slot => $r->slot_name,
1070 pid => $r->pid,
1071 log_path => $r->log_path->stringify,
1072 $r->has_events_path ? ( events_path => $r->events_path->stringify ) : (),
1073 $self->_session_fields($r),
1074 };
1075 }
1076 1 return { error => 'raider not found' };
1077 }
1078  
1079 2 ▶sub kill_raider {
Subroutine kill_raider: called CC=2 SCAR=6.9
Pod undocumented
1080 2 my ($self, $id) = @_;
1081 2 ▶ return $self->_signal_raider(TERM => $id) ? { killed => 1, id => $id } : { error => 'raider not found' };
Branch: 1 branch
BranchTrueFalseTotal
$self->_signal_raider('TERM', $id) ? : 1 1 2
1082 }
1083  
1084  
1085 6 ▶sub cancel_raider {
Subroutine cancel_raider: called CC=2 SCAR=7.0
Pod documented
1086 6 my ($self, $id) = @_;
1087 6 ▶ return { error => 'raider not found' } unless $self->_signal_raider(INT => $id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_signal_raider("INT", $id) 0 6 6
1088 6 $self->_escalate_cancel($id, $self->raiders->{$id}->pid, qw( TERM KILL ));
1089 6 return { cancelled => 1, id => $id };
1090 }
1091  
1092  
1093 72 has cancel_grace => (
1094 is => 'ro',
1095 lazy => 1,
1096 builder => '_build_cancel_grace',
1097 );
1098  
1099 8 ▶sub _build_cancel_grace { $_[0]->config->{cancel_grace} // 5 }
Subroutine _build_cancel_grace: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->config->{"cancel_grace"} // 5
l!l
53
MC/DC100%1/1
$_[0]->config->{"cancel_grace"} // 5
$_[0]->config->{"cancel_grace"}
Truth table100%2/2
A
A= $_[0]->config->{"cancel_grace"}
Aresult
11
00
1100  
1101 # After cancel_grace, sends the first of @signals to raider $id if that
1102 # process ($pid) still runs, and goes on with the rest.
1103 9 ▶sub _escalate_cancel {
Subroutine _escalate_cancel: called CC=4 SCAR=13.9
1104 9 my ($self, $id, $pid, @signals) = @_;
1105 9 my $grace = $self->cancel_grace;
1106 9 ▶ return unless @signals && $grace > 0 && $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless @signals and $grace > 0 and $pid 2 7 9
Condition83%5/6
@signals and $grace > 0 and $pid
!ll&&!rl&&r
207
@signals and $grace > 0
!ll&&!rl&&r
117
MC/DC66%2/3
@signals and $grace > 0 and $pid
@signals $grace > 0 $pid
Truth table75%3/4
A and B and C
A= @signalsB= $grace > 0C= $pid
ABCresult
0XX0
10X0
1100
1111
1107 my $timer = IO::Async::Timer::Countdown->new(
1108 delay => $grace,
1109 remove_on_expire => 1,
1110 5 ▶ on_expire => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
1111 5 ▶ my $r = $self->raiders->{$id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->raiders->{$id} or return 3 2 5
1112 3 ▶ return unless ($r->pid // 0) == $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->pid // 0) == $pid 0 3 3
Condition50%1/2
$r->pid // 0
l!l
30
MC/DC0%0/1
$r->pid // 0
$r->pid
Truth table50%1/2
A
A= $r->pid
Aresult
11
00
1113 3 my ($signal, @rest) = @signals;
1114 3 kill $signal, $pid;
1115 3 $self->_escalate_cancel($id, $pid, @rest);
1116 },
1117 7 );
1118 7 $self->loop->add($timer);
1119 7 $timer->start;
1120 }
1121  
1122 # Sends $signal to the process of raider $id; false when there is no such raider.
1123 8 ▶sub _signal_raider {
Subroutine _signal_raider: called CC=4 SCAR=14.1
1124 8 my ($self, $signal, $id) = @_;
1125 8 for my $r (values %{$self->raiders}) {
1126 7 ▶ next unless $r->id eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->id eq $id 0 7 7
1127 7 ▶ kill $signal, $r->pid if $r->pid;
Branch: 1 branch
BranchTrueFalseTotal
if $r->pid 7 0 7
1128 7 return 1;
1129 }
1130 1 return 0;
1131 }
1132  
1133  
1134 15 ▶sub logs {
Subroutine logs: called CC=6 SCAR=19.3
Pod documented
1135 15 my ($self, %args) = @_;
1136 15 ▶ my $id = $args{id} // '';
Condition50%1/2
$args{"id"} // ""
l!l
150
MC/DC0%0/1
$args{"id"} // ""
$args{"id"}
Truth table50%1/2
A
A= $args{"id"}
Aresult
11
00
1137 15 for my $r (values %{$self->raiders}) {
1138 1 ▶ next unless $r->id eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->id eq $id 0 1 1
1139 1 ▶ my $log = -f $r->log_path ? $r->log_path->slurp_utf8 : '';
Branch: 1 branch
BranchTrueFalseTotal
-f $r->log_path ? : 1 0 1
1140 1 ▶ return { log => $self->_run_section($log, $id) // $log };
Condition33%1/3
$self->_run_section($log, $id) // $log
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->_run_section($log, $id) // $log
$self->_run_section($log, $id) $log
Truth table33%1/3
A // B
A= $self->_run_section($log, $id)B= $log
ABresult
1X1
011
000
1141 }
1142 14 return $self->_ended_logs($id);
1143 }
1144  
1145 14 ▶sub _ended_logs {
Subroutine _ended_logs: called CC=9 SCAR=22.0
1146 14 my ($self, $id) = @_;
1147 14 my $missing = { error => 'raider not found' };
1148 # The ID names files in the log dir: no path parts, no leading dot.
1149 14 ▶ return $missing unless $id =~ /\A([^\/.][^\/]*)-\d+(?:\.\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ m[\A([^/.][^/]*)-\d+(?:\.\d+)?\z] 5 9 14
1150 9 my $slot = $1;
1151 9 my $dir = $self->_log_dir;
1152 9 my $log_path = $dir->child("${slot}.log");
1153 9 ▶ my $log = -f $log_path ? $log_path->slurp_utf8 : '';
Branch: 1 branch
BranchTrueFalseTotal
-f $log_path ? : 7 2 9
1154 9 my $section = $self->_run_section($log, $id);
1155 9 ▶ $log = $section if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 3 6 9
1156 9 my $logged = $log =~ /^\[hall\] raider \Q$id\E \w+: /m;
1157 9 my $events = $dir->child("${id}.events.jsonl");
1158 9 ▶ return $missing unless defined $section || $logged || -f $events;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section or $logged or -f $events 2 7 9
Condition83%5/6
defined $section or $logged or -f $events
l!l&&r!l&&!r
342
defined $section or $logged
l!l&&r!l&&!r
306
MC/DC66%2/3
defined $section or $logged or -f $events
defined $section $logged -f $events
Truth table75%3/4
A or B or C
A= defined $sectionB= $loggedC= -f $events
ABCresult
1XX1
01X1
0011
0000
1159 7 ▶ return { log => $log } if $logged;
Branch: 1 branch
BranchTrueFalseTotal
if $logged 3 4 7
1160 4 my $doc = Langertha::Raider::Hall::Raider->new(
1161 id => $id, slot_name => $slot, base_name => $slot, mission => '',
1162 log_path => $log_path, events_path => $events,
1163 )->run_finished;
1164 4 ▶ return { log => $log.( $doc ? $self->_result_line($id, $self->_result_of($doc)) : '' ) };
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 1 3 4
1165 }
1166  
1167 108 ▶sub _start_line {
Subroutine _start_line: called CC=1 SCAR=0.0
1168 108 my ($self, $id) = @_;
1169 108 return '[hall] raider '.$id." started\n";
1170 }
1171  
1172 # From the run's start line to its result line, or to the end of the log;
1173 # undef when the log has no start line for it.
1174 10 ▶sub _run_section {
Subroutine _run_section: called CC=2 SCAR=6.9
1175 10 my ($self, $log, $id) = @_;
1176 10 my $start = quotemeta $self->_start_line($id);
1177 10 ▶ return $1 if $log =~ /^($start.*?(?:^\[hall\] raider \Q$id\E \w+: [^\n]*\n|\z))/ms;
Branch: 1 branch
BranchTrueFalseTotal
if $log =~ /^($start.*?(?:^\[hall\] raider \Q$id\E \w+: [^\n]*\n|\z))/ms 4 6 10
1178 6 return;
1179 }
1180  
1181 1 ▶sub shutdown {
Subroutine shutdown: called CC=7 SCAR=25.0
Pod undocumented
1182 1 my ($self) = @_;
1183 1 $self->_emit('hall.stopping', {});
1184  
1185 1 ▶ if ($self->_acp_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_acp_running) 1 0 1
1186 1 eval { $self->acp_adapter->stop };
1187 }
1188  
1189 1 ▶ if ($self->telegram) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->telegram) 1 0 1
1190 1 eval { $self->telegram->stop };
1191 }
1192  
1193 1 for my $r (values %{$self->raiders}) {
1194 0 ▶ kill 'TERM', $r->pid if $r->pid && $r->pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r->pid and $r->pid > 0 0 0 0
Condition0%0/3
$r->pid and $r->pid > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$r->pid and $r->pid > 0
$r->pid $r->pid > 0
Truth table0%0/3
A and B
A= $r->pidB= $r->pid > 0
ABresult
0X0
100
111
1195 }
1196  
1197 my $timer = IO::Async::Timer::Countdown->new(
1198 delay => 5,
1199 0 ▶ on_expire => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1200 0 for my $r (values %{$self->raiders}) {
1201 0 ▶ kill 'KILL', $r->pid if $r->pid && $r->pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r->pid and $r->pid > 0 0 0 0
Condition0%0/3
$r->pid and $r->pid > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$r->pid and $r->pid > 0
$r->pid $r->pid > 0
Truth table0%0/3
A and B
A= $r->pidB= $r->pid > 0
ABresult
0X0
100
111
1202 }
1203 0 $self->loop->stop;
1204 },
1205 1 );
1206 1 $self->loop->add($timer);
1207 1 $timer->start;
1208  
1209 # With no children running, stop immediately rather than waiting the
1210 # full 5s grace period.
1211 1 ▶ if (!keys %{$self->raiders}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %{$self->raiders;}) 1 0 1
1212 1 ▶ $self->loop->later(sub { $self->loop->stop });
Subroutine __ANON__: called CC=1 SCAR=0.0
1213 }
1214 }
1215  
1216 0 ▶sub _write_pidfile {
Subroutine _write_pidfile: not called CC=1 SCAR=6.9
1217 0 my ($self) = @_;
1218 0 my $pidfile = $self->root->child('.raider-hall.pid');
1219 0 $pidfile->spew_utf8("$$\n");
1220 }
1221  
1222 105 ▶sub _remove_pidfile {
Subroutine _remove_pidfile: called CC=3 SCAR=11.4
1223 105 my ($self) = @_;
1224 105 ▶ return unless $self->root;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->root 0 105 105
1225 105 my $pidfile = $self->root->child('.raider-hall.pid');
1226 105 ▶ $pidfile->remove if -f $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $pidfile 0 105 105
1227 }
1228  
1229 105 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.2
Pod undocumented
1230 105 my ($self) = @_;
1231 105 ▶ $self->_remove_pidfile if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 105 0 105
1232 }
1233  
1234 72 __PACKAGE__->meta->make_immutable;
1235  
1236 72 1;
1237  
1238 __END__
1239  
1240 =pod
1241  
1242 =encoding UTF-8
1243  
1244 =head1 NAME
1245  
1246 Langertha::Raider::Hall - Hall daemon — spawns and manages raider processes
1247  
1248 =head1 VERSION
1249  
1250 version 0.503
1251  
1252 =head1 SYNOPSIS
1253  
1254 use Langertha::Raider::Hall;
1255 my $hall = Langertha::Raider::Hall->new(root => Path::Tiny::path('.'));
1256 $hall->run; # blocks on the IO::Async loop
1257  
1258 From the shell:
1259  
1260 raider hall init --name bjorn
1261 raider hall start --daemon --acp-port 38421
1262 raider hall spawn bjorn "summarise today's git log"
1263  
1264 =head1 DESCRIPTION
1265  
1266 Hall is the multi-raider daemon. It owns a UNIX command/event socket,
1267 spawns named raiders as child processes, enforces C<1name> singleton
1268 slots with persistent FIFO queueing, and wires in optional features:
1269  
1270 =over
1271  
1272 =item * Non-blocking L<Schedule::Cron> scheduler for timed raids.
1273  
1274 =item * Multi-bot Telegram long-poll with routing + per-chat history.
1275  
1276 =item * ACP (Agent Client Protocol) adapter on a TCP port for Zed and
1277 other ACP-capable clients — see L<Langertha::Raider::Hall::ACP>.
1278  
1279 =back
1280  
1281 The name picks the slot. A name with a leading number (C<1bjorn>) is a
1282 singleton: one run at a time, further missions wait in a queue that
1283 survives a hall restart. A plain name (C<bjorn>) runs every mission at
1284 once, in parallel; its runs share the slot log. The hall keeps its running
1285 raiders by run ID, so each run is reaped and reported on its own.
1286  
1287 An MCP adapter on C<.raider-hall.mcp> is B<not implemented>:
1288 L<Langertha::Raider::Hall::MCP> describes the hall tools, but no socket is
1289 opened and an C<mcp> section in the config has no effect.
1290  
1291 All state flows through the event bus (JSONL pub/sub). Clients
1292 subscribe with C<{type: subscribe, payload: {filter: 'raider.'}}> and
1293 commands are separate frames (C<{type: command, payload: {cmd: ...}}>).
1294  
1295 Each raider runs with C<--stream-json>. Its stdout goes to a file of its
1296 own per run, F<.raider-hall/logs/ID.events.jsonl>; its stderr is appended
1297 to the human-readable F<.raider-hall/logs/SLOT.log>. When the process
1298 ends, C<raider.done> carries C<status> and C<response> or C<error> from
1299 the run's last C<run.finished> event. A run that ended without one
1300 (killed, crashed) is C<failed>, with an error saying so.
1301 The hall then appends one line to the slot log,
1302 C<[hall] raider ID STATUS: TEXT> with the response or error cut to 300
1303 characters, so C<raider hall logs> shows the outcome.
1304  
1305 Each run starts its part of the slot log with C<[hall] raider ID started>.
1306 Run IDs are C<SLOT-TIME>, with C<.2>, C<.3>, ... appended when a run of the
1307 same slot started in the same second. Only the newest L</keep_events>
1308 events files per slot are kept. A slot log larger than L</max_log_size> is
1309 moved to F<SLOT.log.1> when the next run of the slot starts.
1310  
1311 Every run is recorded in a session journal (ADR 0015) under
1312 F<.raider/sessions/> of the hall root. A bound run -- a Telegram chat, a
1313 cron job, an ACP session, a numbered slot; see L</session_bindings> --
1314 is started with C<--session ID> and continues its binding's session; a
1315 plain-name run gets a fresh session. C<raider.spawned>, C<ps> and
1316 C<attach> name the C<session> and C<binding> of a bound run,
1317 C<raider.done> names the C<session> of every run that has one. A mission
1318 for a binding that already has a running raider waits in that binding's
1319 queue and starts when the run ends (see L</binding_queues>). Only when the
1320 session is held by a writer outside the hall does a bound run fail at
1321 once, as C<raider.done> with status C<failed> and an error naming the
1322 session and binding; its mission is not run. When the hall cannot get the
1323 binding's session at all, the run starts unbound, and
1324 C<hall.session_error> names the C<binding>, the C<error> and the run C<id>.
1325  
1326 C<raider hall logs ID> shows the part of the slot log from that run's start
1327 line to its result line, and works for ended runs too: the slot is taken
1328 from the ID; see L</logs>.
1329  
1330 The C<spawn> reply and C<attach> name the run's C<events_path>;
1331 C<raider hall attach ID> and C<raider hall spawn --attach> print that
1332 file as it grows, until the hall has reaped the raider.
1333  
1334 =head1 CONFIG FILE
1335  
1336 C<.raider-hall.yml> in the hall root:
1337  
1338 longhouse: false
1339 preferred_lib_target: .raider-hall/lib
1340 raiders:
1341 bjorn: { engine: anthropic, persona: caveman }
1342 lagertha:{ engine: openai, persona: polite, packs: [git-guru] }
1343 cron:
1344 - { name: 1bjorn, cron: '*/15 * * * *', mission: 'ping CI' }
1345 telegram:
1346 bots:
1347 ops: { token: '...', allowlist: [42], routing: { '*': lagertha } }
1348 acp: { port: 38421, host: 127.0.0.1 }
1349 logs: { keep_events: 20, max_log_size: 1048576 }
1350 cancel_grace: 5
1351  
1352 C<persona> on a raider entry is a pack used as the raider's persona: the
1353 hall passes it as one more C<--pack>, after those in C<packs>, unless
1354 C<packs> already names it. The hall ignores C<mcp> and C<isolated> on a
1355 raider entry; the first run of such a raider notes that in its slot log.
1356  
1357 C<preferred_lib_target> sets the hall's L</lib_target>. C<longhouse: true>
1358 adds F<longhouse/lib> of the hall root to every raider's C<PERL5LIB> as
1359 well.
1360  
1361 C<engine> on a raider entry is optional. Without it the hall passes no
1362 C<--engine> and the spawned raider decides itself: the engine from its
1363 F<.raider.yml> first, then autodetection from the API keys in the
1364 environment.
1365  
1366 =head1 ENVIRONMENT
1367  
1368 =head2 RAIDER_HALL_RAIDER_BIN
1369  
1370 The F<raider> executable the hall starts its raiders with. Without it, or
1371 when it is not executable, the standalone binary the hall itself runs
1372 from, else the F<raider> next to the running script, then the one on
1373 C<PATH>. From a standalone binary the hall execs it directly; otherwise
1374 it runs it with the perl that runs the hall.
1375  
1376 =head2 RAIDER_HALL_ACP_PORT
1377  
1378 A TCP port opens the ACP adapter on it, over C<acp: { port: ... }> of the
1379 config. C<raider hall start --acp-port N> sets it.
1380  
1381 =head2 RAIDER_HALL_ACP_HOST
1382  
1383 The address the ACP adapter binds to, over C<acp: { host: ... }>; default
1384 C<127.0.0.1>. C<raider hall start --acp-port N --acp-host H> sets it.
1385  
1386 Every raider the hall starts gets the hall's environment without the
1387 C<RAIDER_HALL_TELEGRAM_*> variables, plus:
1388  
1389 =over
1390  
1391 =item * C<RAIDER_HALL_SOCKET> -- the hall's control socket. F<raider>
1392 mounts the Hall tools (L<Langertha::Raider::HallTools>) when it is set.
1393  
1394 =item * C<RAIDER_HALL_ROOT>, C<RAIDER_HALL_SLOT> -- the hall root and the
1395 run's slot; C<RAIDER_HALL_MODE> is C<1>.
1396  
1397 =item * C<RAIDER_HALL_TELEGRAM_BOT>, C<RAIDER_HALL_TELEGRAM_CHAT_ID> and,
1398 for a forum topic, C<RAIDER_HALL_TELEGRAM_THREAD_ID> -- for a Telegram
1399 mission, the chat C<telegram_reply> answers.
1400  
1401 =item * C<PERL5LIB> -- extended by F<lib/perl5> of L</lib_target> (and
1402 F<longhouse/lib> with C<longhouse: true>).
1403  
1404 =back
1405  
1406 =head1 SEE ALSO
1407  
1408 L<raider-hall>, L<Langertha::Raider::CLI>, L<Langertha::Raider::Hall::ACP>,
1409 L<Langertha::Raider::HallTools>, L<Langertha::Raider::Hall::CLI>.
1410  
1411 =head2 session_store
1412  
1413 The L<Langertha::Raider::SessionStore> of the hall's runs: the hall root
1414 is their project, so journals live in F<.raider/sessions/> under it --
1415 where a spawned raider, started with C<--root> on the hall root, keeps
1416 them too.
1417  
1418 =head2 session_bindings
1419  
1420 The map from binding key to session id (ADR 0015, Hall bindings), kept in
1421 F<.raider-hall/state/sessions.json>. Keys:
1422  
1423 =over
1424  
1425 =item * C<telegram:BOT:CHAT_ID>, C<telegram:BOT:CHAT_ID:THREAD> -- a
1426 Telegram chat (or forum topic): the conversation continues across
1427 messages;
1428  
1429 =item * C<cron:ID> -- a cron job, its own session per job;
1430  
1431 =item * C<acp:SESSION> -- an ACP session, for as long as its connection
1432 lasts; a hall start forgets any left over;
1433  
1434 =item * C<slot:1NAME> -- a numbered slot, continued by each queued
1435 mission that has no binding of its own.
1436  
1437 =back
1438  
1439 A run of a plain name (C<bjorn>) without such a binding is not bound: the
1440 raider starts a fresh session of its own.
1441  
1442 Telegram, cron and slot bindings stay until they are reset (see
1443 L</reset_session>); a hall start also forgets the bindings of cron jobs
1444 no longer in the config, and drops the missions they left waiting.
1445 Journals are never deleted by the hall.
1446  
1447 =head2 session_for
1448  
1449 my $id = $hall->session_for('telegram:ops:42');
1450  
1451 The session id bound to a binding key. A key without a session -- or
1452 whose journal is gone -- gets a new session: the hall creates the journal
1453 (C<session.created> naming the hall root) and records the binding, so the
1454 raider it starts can resume it with C<--session ID>.
1455  
1456 =head2 unbind_session
1457  
1458 $hall->unbind_session('acp:acp-1f2e3d4c');
1459  
1460 Forgets a binding. The journal stays.
1461  
1462 =head2 reset_session
1463  
1464 my $res = $hall->reset_session('telegram:ops:42');
1465 # { reset => 1, binding => ..., session => OLD_ID } or { error => ... }
1466  
1467 Starts a binding over: its next mission gets a new session. The old
1468 journal stays, and a run still going on the binding finishes in it.
1469 Emits C<session.reset>. C<raider hall session reset BINDING> and C</new>
1470 in a Telegram chat end up here.
1471  
1472 =head2 singleton_queues
1473  
1474 The missions waiting for a busy numbered slot, as a map from slot to a
1475 FIFO list; each slot's list is kept in F<.raider-hall/state/SLOT.queue.json>.
1476 A hall start runs what a previous hall left waiting before any new
1477 mission, and a new mission for a slot never overtakes the missions
1478 already waiting for it.
1479  
1480 =head2 drop_queued
1481  
1482 my $n = $hall->drop_queued('acp:acp-1f2e3d4c');
1483  
1484 Drops every mission waiting for a binding, from the binding's queue and
1485 from the slot queues, and returns how many there were.
1486  
1487 =head2 binding_queues
1488  
1489 The missions waiting for a busy binding (ADR 0003: one writer per session,
1490 new input is queued), as a map from binding key to a FIFO list of spawn
1491 arguments; kept in F<.raider-hall/state/binding_queues.json>. A mission
1492 whose binding already has a running raider waits here and starts when
1493 that run ends, in the binding's session -- two quick Telegram messages to
1494 one chat run one after the other. A hall start runs what a previous hall
1495 left waiting. Missions without a binding never wait here.
1496  
1497 =head2 keep_events
1498  
1499 How many F<SLOT-TIME.events.jsonl> files the hall keeps per slot; older ones
1500 are removed when a run of that slot ends. From C<logs: { keep_events: N }>
1501 in F<.raider-hall.yml>, default 20; 0 keeps all of them. Slot logs
1502 (F<SLOT.log>) are not pruned; see L</max_log_size>.
1503  
1504 =head2 max_log_size
1505  
1506 Size in bytes above which a slot log F<SLOT.log> is moved to F<SLOT.log.1>
1507 (replacing an older one) when the next run of that slot starts -- never
1508 while any raider of the slot is still writing it. From
1509 C<logs: { max_log_size: N }> in F<.raider-hall.yml>, default 1048576
1510 (1 MiB); 0 never rotates.
1511  
1512 =head2 lib_target
1513  
1514 The local::lib the hall's raiders install into with C<perl_cpanm>:
1515 C<preferred_lib_target> of the config, relative to the hall root, default
1516 F<.raider/lib>. Each raider gets it as C<-o preferred_lib_target=...>, which
1517 beats that key in a F<.raider.yml>, and its F<lib/perl5> on C<PERL5LIB>.
1518  
1519 =head2 cancel_raider
1520  
1521 my $res = $hall->cancel_raider($id); # { cancelled => 1, id => $id } or { error => ... }
1522  
1523 Cancels the run of the running raider C<$id> with C<SIGINT>: F<raider>
1524 ends the run as C<cancelled> (its C<run.finished>, the session journal)
1525 and then dies of the signal. C<kill_raider> sends C<SIGTERM> instead, a
1526 stop that ends the run as C<interrupted>.
1527  
1528 A raider still running L</cancel_grace> seconds later -- stuck in a call
1529 that holds off the signal -- gets C<SIGTERM>, and C<SIGKILL> when it is
1530 still there after another L</cancel_grace>.
1531  
1532 =head2 cancel_grace
1533  
1534 Seconds L</cancel_raider> gives a raider to end on C<SIGINT> before it
1535 sends C<SIGTERM>, and again before C<SIGKILL>. From C<cancel_grace: N> in
1536 F<.raider-hall.yml>, default 5; 0 never escalates. The default leaves
1537 F<raider> time for its own cancel -- ending the tool subprocesses takes it
1538 up to two seconds (L<Langertha::Raider::CLI::Runner/terminate_children>)
1539 -- and matches the grace the hall gives its raiders on shutdown.
1540  
1541 =head2 logs
1542  
1543 my $res = $hall->logs(id => $id); # { log => $text } or { error => ... }
1544  
1545 The run's part of its slot log: from its C<[hall] raider ID started> line
1546 to its result line, or to the end of the log while it runs. Runs of a plain
1547 name in parallel share the slot log, so their lines can interleave. A log
1548 without that start line (written before the hall marked starts, or
1549 rotated away) is answered with the whole slot log.
1550  
1551 For an ended run the slot comes from the ID (C<SLOT-TIME> or
1552 C<SLOT-TIME.N>); the run counts as known while its events file or its
1553 lines in the slot log are there. When the log does not carry its result
1554 line (rotated or removed), the result line built from the events file is
1555 appended.
1556  
1557 =head1 SUPPORT
1558  
1559 =head2 Issues
1560  
1561 Please report bugs and feature requests on GitHub at
1562 L<https://github.com/Getty/langertha-raider/issues>.
1563  
1564 =head2 IRC
1565  
1566 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
1567  
1568 =head1 CONTRIBUTING
1569  
1570 Contributions are welcome! Please fork the repository and submit a pull request.
1571  
1572 =head1 AUTHOR
1573  
1574 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
1575  
1576 =head1 COPYRIGHT AND LICENSE
1577  
1578 This software is copyright (c) 2026 by Torsten Raudssus.
1579  
1580 This is free software; you can redistribute it and/or modify it under
1581 the same terms as the Perl 5 programming language system itself.
1582  
1583 =cut
« blib/lib/Langertha/Raider/WebTools.pm ↑ Summary blib/lib/Langertha/Raider/CLI/Provider.pm »