blib/lib/Langertha/Raid/Parallel.pm

filter: Statementstmt 100% 62 / 62 Branchbran 62.5% 5 / 8 Conditioncond 72.7% 8 / 11 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 91.4% 86 / 94 CC 11 SCAR 18.7
CC
11
Cov
93%
run_f
10.1
__ANON__
2.0
BEGIN
1.0
CRAP 6.5
« blib/lib/Langertha/Raider/Packs/Collection.pm ↑ Summary blib/lib/Langertha/Raider/CLI.pm »
1 package Langertha::Raid::Parallel;
2 # ABSTRACT: Parallel Raid orchestrator with branched context isolation
3 1 our $VERSION = '0.503';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Langertha::Raider::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Langertha::Raid';
11  
12  
13 1 has merge_slot => (
14 is => 'ro',
15 isa => 'Str',
16 default => 'parallel_branches',
17 );
18  
19  
20 8 ▶async sub run_f {
Subroutine run_f: called CC=10 SCAR=23.1
Pod documented
21 8 my ( $self, $ctx ) = @_;
22 8 $ctx = $self->_coerce_context($ctx);
23  
24 8 my @steps = @{$self->steps};
25 8 ▶ return Langertha::Raider::Result->final($ctx->input // '')->with_context($ctx)
Branch: 1 branch
BranchTrueFalseTotal
unless @steps 0 8 8
Condition0%0/2
$ctx->input // ""
l!l
00
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table0%0/2
A
A= $ctx->input
Aresult
11
00
26 unless @steps;
27  
28 8 $ctx->add_trace({
29 node => ref($self),
30 event => 'parallel_start',
31 steps => scalar @steps,
32 });
33  
34 8 my @futures;
35 8 for my $idx (0..$#steps) {
36 16 my $step = $steps[$idx];
37 16 my $step_name = $self->_step_label($step, $idx);
38 16 my $branch_ctx = $ctx->branch(metadata => {
39 parallel_index => $idx,
40 parallel_step => $step_name,
41 });
42  
43 15 ▶ my $future = $step->run_f($branch_ctx)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 15 my ( $raw_result ) = @_;
45 15 my $result = $self->_normalize_result($raw_result);
46 15 Future->done({
47 index => $idx,
48 step_name => $step_name,
49 result => $result,
50 context => $branch_ctx,
51 });
52 1 ▶ })->else(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
53 1 my ( $err ) = @_;
54 1 ▶ chomp($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 1 0 1
55 1 Future->done({
56 index => $idx,
57 step_name => $step_name,
58 result => Langertha::Raider::Result->abort("Parallel step '$step_name' failed: $err"),
59 context => $branch_ctx,
60 });
61 16 });
62  
63 16 push @futures, $future;
64 }
65  
66 8 my @outcomes = await Future->needs_all(@futures);
67 8 @outcomes = sort { $a->{index} <=> $b->{index} } @outcomes;
68  
69 8 for my $outcome (@outcomes) {
70 16 my $slot_name = sprintf('%02d_%s', $outcome->{index}, $outcome->{step_name});
71 16 $ctx->merge_branch($outcome->{context}, slot => $self->merge_slot, name => $slot_name);
72 $ctx->add_trace({
73 node => ref($self),
74 event => 'parallel_branch_result',
75 step_index => $outcome->{index},
76 step_name => $outcome->{step_name},
77 result_type => $outcome->{result}->type,
78 16 });
79 }
80  
81 my $winner =
82 16 (grep { $_->{result}->is_abort } @outcomes)[0]
83 11 || (grep { $_->{result}->is_cancelled } @outcomes)[0]
84 6 || (grep { $_->{result}->is_question } @outcomes)[0]
85 8 ▶ || (grep { $_->{result}->is_pause } @outcomes)[0];
Condition88%8/9
(grep {$_->{'result'}->is_abort;} @outcomes)[0] || (grep {$_->{'result'}->is_cancelled;} @outcomes)[0] || (grep {$_->{'result'}->is_question;} @outcomes)[0] || (grep {$_->{'result'}->is_pause;} @outcomes)[0]
l!l&&r!l&&!r
602
(grep {$_->{"result"}->is_abort;} @outcomes)[0] or (grep {$_->{"result"}->is_cancelled;} @outcomes)[0] or (grep {$_->{"result"}->is_question;} @outcomes)[0]
l!l&&r!l&&!r
512
(grep {$_->{"result"}->is_abort;} @outcomes)[0] or (grep {$_->{"result"}->is_cancelled;} @outcomes)[0]
l!l&&r!l&&!r
323
MC/DC75%3/4
(grep {$_->{'result'}->is_abort;} @outcomes)[0] || (grep {$_->{'result'}->is_cancelled;} @outcomes)[0] || (grep {$_->{'result'}->is_question;} @outcomes)[0] || (grep {$_->{'result'}->is_pause;} @outcomes)[0]
(grep {$_->{"result"}->is_abort;} @outcomes)[0] (grep {$_->{"result"}->is_cancelled;} @outcomes)[0] (grep {$_->{"result"}->is_question;} @outcomes)[0] (grep {$_->{'result'}->is_pause;} @outcomes)[0]
Truth table80%4/5
A or B or C || D
A= (grep {$_->{"result"}->is_abort;} @outcomes)[0]B= (grep {$_->{"result"}->is_cancelled;} @outcomes)[0]C= (grep {$_->{"result"}->is_question;} @outcomes)[0]D= (grep {$_->{'result'}->is_pause;} @outcomes)[0]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
86  
87 8 ▶ if ($winner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($winner) 6 2 8
88 6 $ctx->state->{last_result_type} = $winner->{result}->type;
89 6 $ctx->state->{last_result} = $winner->{result}->as_hash;
90 6 return $self->_with_context_result($winner->{result}, $ctx);
91 }
92  
93 my @texts = map {
94 2 $_->{result}->has_text ? $_->{result}->text : ()
95 4 ▶ } @outcomes;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'result'}->has_text ? : 4 0 4
96  
97 2 my $joined = join("\n", @texts);
98 2 my $final = Langertha::Raider::Result->final($joined, data => {
99 branch_count => scalar @outcomes,
100 });
101  
102 $ctx->state->{parallel_results} = [
103 2 map { $_->{result}->as_hash } @outcomes
104 ];
105 2 $self->_apply_final_result_to_context($ctx, $final, step_name => ref($self));
106  
107 2 return $self->_with_context_result($final, $ctx);
108 1 }
109  
110  
111 1 __PACKAGE__->meta->make_immutable;
112  
113 1 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 Langertha::Raid::Parallel - Parallel Raid orchestrator with branched context isolation
124  
125 =head1 VERSION
126  
127 version 0.503
128  
129 =head1 SYNOPSIS
130  
131 my $raid = Langertha::Raid::Parallel->new(
132 steps => [ $a, $b, $c ],
133 );
134  
135 my $result = await $raid->run_f($ctx);
136  
137 =head1 DESCRIPTION
138  
139 Runs child steps concurrently using Futures. Each branch receives a cloned
140 context (via C<< $ctx->branch >>), so branch mutations are isolated.
141 Branch snapshots are merged back into parent artifacts under C<merge_slot>.
142  
143 Aggregation strategy:
144  
145 =over 4
146  
147 =item * If any branch aborts, first abort is propagated.
148  
149 =item * Otherwise first C<cancelled> result is propagated
150 (L<Langertha::Raider/cancel>): the parallel raid is cancelled as a whole, and
151 the text of branches that finished is not folded into a final result.
152  
153 =item * Otherwise first question is propagated.
154  
155 =item * Otherwise first pause is propagated.
156  
157 =item * Otherwise final branch texts are concatenated with newlines.
158  
159 =back
160  
161 =head2 merge_slot
162  
163 Artifact slot name where merged branch snapshots are stored. Defaults to
164 C<parallel_branches>.
165  
166 =head2 run_f
167  
168 my $result = await $raid->run_f($ctx);
169  
170 Executes all child steps concurrently with branched context isolation and
171 deterministic result aggregation.
172  
173 =head1 SUPPORT
174  
175 =head2 Issues
176  
177 Please report bugs and feature requests on GitHub at
178 L<https://github.com/Getty/langertha-raider/issues>.
179  
180 =head2 IRC
181  
182 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
183  
184 =head1 CONTRIBUTING
185  
186 Contributions are welcome! Please fork the repository and submit a pull request.
187  
188 =head1 AUTHOR
189  
190 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
191  
192 =head1 COPYRIGHT AND LICENSE
193  
194 This software is copyright (c) 2026 by Torsten Raudssus.
195  
196 This is free software; you can redistribute it and/or modify it under
197 the same terms as the Perl 5 programming language system itself.
198  
199 =cut
« blib/lib/Langertha/Raider/Packs/Collection.pm ↑ Summary blib/lib/Langertha/Raider/CLI.pm »