blib/lib/Langertha/Raid.pm

filter: Statementstmt 87.2% 82 / 94 Branchbran 43.7% 14 / 32 Conditioncond 34.7% 8 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 87.5% 14 / 16 Podpod 50.0% 1 / 2 totaltotal 67.2% 121 / 180 CC 28 SCAR 34.2
CC
28
Cov
70%
_normalize_result
100.0
_run_steps_sequentially_f
7.0
_validate_steps
4.3
CRAP 30.5
« blib/lib/Langertha/Raider/ACP/Client.pm ↑ Summary blib/lib/Langertha/Raider/CLI/Output.pm »
1 package Langertha::Raid;
2 # ABSTRACT: Base class for orchestrating Runnable steps
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 Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Langertha::Raider::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Langertha::RunContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Langertha::Role::Runnable';
13  
14  
15 has steps => (
16 is => 'ro',
17 isa => 'ArrayRef',
18 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 1 );
20  
21  
22 1 has name => (
23 is => 'ro',
24 isa => 'Str',
25 predicate => 'has_name',
26 );
27  
28  
29 21 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
30 21 my ( $self ) = @_;
31 21 $self->_validate_steps($self->steps);
32 }
33  
34 21 ▶sub _validate_steps {
Subroutine _validate_steps: called CC=4 SCAR=14.6
35 21 my ( $self, $steps ) = @_;
36 21 for my $idx (0..$#{$steps}) {
37 36 my $step = $steps->[$idx];
38 36 ▶ croak "Invalid step at index $idx: expected Runnable object"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($step) and $step->can("run_f") 0 36 36
Condition33%1/3
builtin::blessed($step) and $step->can("run_f")
!ll&&!rl&&r
0036
MC/DC0%0/2
builtin::blessed($step) and $step->can("run_f")
builtin::blessed($step) $step->can("run_f")
Truth table33%1/3
A and B
A= builtin::blessed($step)B= $step->can("run_f")
ABresult
0X0
100
111
39 unless blessed($step) && $step->can('run_f');
40 }
41 21 return;
42 }
43  
44 22 ▶sub _coerce_context {
Subroutine _coerce_context: called CC=3 SCAR=14.2
45 22 my ( $self, $ctx ) = @_;
46 22 ▶ return $ctx if blessed($ctx) && $ctx->isa('Langertha::RunContext');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext") 22 0 22
Condition33%1/3
builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext")
!ll&&!rl&&r
0022
MC/DC0%0/2
builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext")
builtin::blessed($ctx) $ctx->isa("Langertha::RunContext")
Truth table33%1/3
A and B
A= builtin::blessed($ctx)B= $ctx->isa("Langertha::RunContext")
ABresult
0X0
100
111
47 0 return Langertha::RunContext->new(input => $ctx);
48 }
49  
50 39 ▶sub _step_label {
Subroutine _step_label: called CC=3 SCAR=11.0
51 39 my ( $self, $step, $idx ) = @_;
52 39 ▶ return $step->name if $step->can('name') && defined $step->name;
Branch: 1 branch
BranchTrueFalseTotal
if $step->can("name") and defined $step->name 32 7 39
Condition100%3/3
$step->can("name") and defined $step->name
!ll&&!rl&&r
3432
MC/DC100%2/2
$step->can("name") and defined $step->name
$step->can("name") defined $step->name
Truth table100%3/3
A and B
A= $step->can("name")B= defined $step->name
ABresult
0X0
100
111
53 7 return ref($step) . "[$idx]";
54 }
55  
56 36 ▶sub _normalize_result {
Subroutine _normalize_result: called CC=12 SCAR=46.0
57 36 my ( $self, $raw ) = @_;
58  
59 36 ▶ if (blessed($raw) && $raw->isa('Langertha::Raider::Result')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($raw) and $raw->isa("Langertha::Raider::Result")) 36 0 36
Condition33%1/3
builtin::blessed($raw) and $raw->isa("Langertha::Raider::Result")
!ll&&!rl&&r
0036
MC/DC0%0/2
builtin::blessed($raw) and $raw->isa("Langertha::Raider::Result")
builtin::blessed($raw) $raw->isa("Langertha::Raider::Result")
Truth table33%1/3
A and B
A= builtin::blessed($raw)B= $raw->isa("Langertha::Raider::Result")
ABresult
0X0
100
111
60 36 return $raw;
61 }
62  
63 0 ▶ if (!ref($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $raw) 0 0 0
64 0 return Langertha::Raider::Result->final($raw);
65 }
66  
67 0 ▶ if (ref($raw) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw eq "HASH") 0 0 0
68 my %args = (
69 type => ($raw->{type} // 'final'),
70 (exists $raw->{text} ? (text => $raw->{text}) : ()),
71 (exists $raw->{content} ? (content => $raw->{content}) : ()),
72 (exists $raw->{options} ? (options => $raw->{options}) : ()),
73 0 ▶ (exists $raw->{data} ? (data => $raw->{data}) : ()),
Branch: 4 branches
BranchTrueFalseTotal
exists $raw->{'text'} ? : 0 0 0
exists $raw->{'content'} ? : 0 0 0
exists $raw->{'options'} ? : 0 0 0
exists $raw->{'data'} ? : 0 0 0
Condition0%0/2
$raw->{"type"} // "final"
l!l
00
MC/DC0%0/1
$raw->{"type"} // "final"
$raw->{"type"}
Truth table0%0/2
A
A= $raw->{"type"}
Aresult
11
00
74 );
75 0 return Langertha::Raider::Result->new(%args);
76 }
77  
78 0 ▶ croak "Unsupported step result type from ".(blessed($raw) || ref($raw) || 'scalar');
Condition0%0/5
builtin::blessed($raw) || ref $raw || 'scalar'
l!l
00
builtin::blessed($raw) or ref $raw
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::blessed($raw) || ref $raw || 'scalar'
builtin::blessed($raw) ref $raw
Truth table0%0/3
A or B
A= builtin::blessed($raw)B= ref $raw
ABresult
1X1
011
000
79 }
80  
81 32 ▶sub _with_context_result {
Subroutine _with_context_result: called CC=2 SCAR=9.2
82 32 my ( $self, $result, $ctx ) = @_;
83 32 ▶ return $result->with_context($ctx)
Branch: 1 branch
BranchTrueFalseTotal
if $result->can("with_context") 32 0 32
84 if $result->can('with_context');
85  
86 return Langertha::Raider::Result->new(
87 0 %{$result->as_hash},
88 context => $ctx,
89 );
90 }
91  
92 19 ▶sub _apply_final_result_to_context {
Subroutine _apply_final_result_to_context: called CC=3 SCAR=11.1
93 19 my ( $self, $ctx, $result, %extra ) = @_;
94 19 ▶ if ($result->has_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->has_text) 19 0 19
95 19 $ctx->input($result->text);
96 19 $ctx->state->{last_output} = $result->text;
97 }
98 19 $ctx->state->{last_result_type} = $result->type;
99 19 $ctx->state->{last_result} = $result->as_hash;
100 19 ▶ if ($extra{step_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra{"step_name"}) 19 0 19
101 19 $ctx->metadata->{last_step_name} = $extra{step_name};
102 }
103 19 return $ctx;
104 }
105  
106 18 ▶async sub _run_steps_sequentially_f {
Subroutine _run_steps_sequentially_f: called CC=7 SCAR=19.5
107 18 my ( $self, $ctx, %opts ) = @_;
108 18 my @steps = @{$self->steps};
109  
110 18 ▶ my $last_result = Langertha::Raider::Result->final($ctx->input // '');
Condition50%1/2
$ctx->input // ""
l!l
180
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table50%1/2
A
A= $ctx->input
Aresult
11
00
111  
112 18 for my $idx (0..$#steps) {
113 23 my $step = $steps[$idx];
114 23 my $step_name = $self->_step_label($step, $idx);
115 23 my $raw;
116  
117 23 my $ok = eval {
118 23 $raw = await $step->run_f($ctx);
119 21 1;
120 };
121  
122 23 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 21 23
123 2 ▶ my $err = $@ || 'Unknown step error';
Condition50%1/2
$@ || 'Unknown step error'
l!l
20
MC/DC0%0/1
$@ || 'Unknown step error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
124 2 chomp $err;
125 2 my $abort = Langertha::Raider::Result->abort("Step '$step_name' failed: $err");
126 2 $ctx->add_trace({
127 node => ref($self),
128 event => 'step_error',
129 step_index => $idx,
130 step_name => $step_name,
131 result_type => 'abort',
132 });
133 2 $ctx->state->{last_result_type} = 'abort';
134 2 return $self->_with_context_result($abort, $ctx);
135 }
136  
137 21 my $result = $self->_normalize_result($raw);
138 $ctx->add_trace({
139 node => ref($self),
140 event => 'step_result',
141 step_index => $idx,
142 step_name => $step_name,
143 result_type => $result->type,
144 21 ▶ ($opts{loop_iteration} ? (loop_iteration => $opts{loop_iteration}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'loop_iteration'} ? : 9 12 21
145 });
146  
147 21 ▶ if ($result->is_final) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_final) 17 4 21
148 17 $self->_apply_final_result_to_context($ctx, $result, step_name => $step_name);
149 17 $last_result = $result;
150 17 next;
151 }
152  
153 4 $ctx->state->{last_result_type} = $result->type;
154 4 $ctx->state->{last_result} = $result->as_hash;
155 4 return $self->_with_context_result($result, $ctx);
156 }
157  
158 12 return $self->_with_context_result($last_result, $ctx);
159 1 }
160  
161 0 ▶async sub run_f {
Subroutine run_f: not called CC=1 SCAR=6.9
Pod documented
162 0 croak ref($_[0])." is abstract; use a Raid subclass";
163 1 }
164  
165  
166 1 __PACKAGE__->meta->make_immutable;
167  
168 1 1;
169  
170 __END__
171  
172 =pod
173  
174 =encoding UTF-8
175  
176 =head1 NAME
177  
178 Langertha::Raid - Base class for orchestrating Runnable steps
179  
180 =head1 VERSION
181  
182 version 0.503
183  
184 =head1 SYNOPSIS
185  
186 my $raid = Langertha::Raid::Sequential->new(
187 steps => [ $raider_a, $raider_b ],
188 );
189  
190 my $ctx = Langertha::RunContext->new(input => 'start');
191 my $result = await $raid->run_f($ctx);
192  
193 =head1 DESCRIPTION
194  
195 Abstract orchestration base class for runnable workflows. Holds child steps
196 that implement C<run_f($ctx)> and provides shared helpers for context
197 coercion, step validation, result normalization, and sequential step driving.
198  
199 =head2 steps
200  
201 Ordered child nodes. Each step must implement C<run_f>.
202  
203 =head2 name
204  
205 Optional human-friendly node name used in traces and diagnostics.
206  
207 =head2 run_f
208  
209 Abstract execution method required by C<Langertha::Role::Runnable>.
210 Concrete subclasses must implement it.
211  
212 =head1 SUPPORT
213  
214 =head2 Issues
215  
216 Please report bugs and feature requests on GitHub at
217 L<https://github.com/Getty/langertha-raider/issues>.
218  
219 =head2 IRC
220  
221 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
222  
223 =head1 CONTRIBUTING
224  
225 Contributions are welcome! Please fork the repository and submit a pull request.
226  
227 =head1 AUTHOR
228  
229 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
230  
231 =head1 COPYRIGHT AND LICENSE
232  
233 This software is copyright (c) 2026 by Torsten Raudssus.
234  
235 This is free software; you can redistribute it and/or modify it under
236 the same terms as the Perl 5 programming language system itself.
237  
238 =cut
« blib/lib/Langertha/Raider/ACP/Client.pm ↑ Summary blib/lib/Langertha/Raider/CLI/Output.pm »