Langertha/RunContext.pm

filter: Statementstmt 86.6% 52 / 60 Branchbran 50.0% 13 / 26 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 73.2% 82 / 112 CC 17 SCAR 18.7
CC
17
Cov
72%
merge_branch
11.4
_clone_data
10.4
branch
5.3
CRAP 6.5
« Langertha/Role/PluginHost.pm ↑ Summary Langertha/Role/Tools.pm »
1 package Langertha::RunContext;
2 # ABSTRACT: Shared execution context for Raid and Raider runs
3 7 our $VERSION = '0.502';
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 7 has input => (
11 is => 'rw',
12 );
13  
14  
15 has state => (
16 is => 'rw',
17 isa => 'HashRef',
18 13 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 7 );
20  
21  
22 has artifacts => (
23 is => 'rw',
24 isa => 'HashRef',
25 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 7 );
27  
28  
29 has metadata => (
30 is => 'rw',
31 isa => 'HashRef',
32 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 7 );
34  
35  
36 has trace => (
37 is => 'rw',
38 isa => 'ArrayRef',
39 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 7 );
41  
42  
43 7 has history => (
44 is => 'rw',
45 isa => 'ArrayRef',
46 predicate => 'has_history',
47 );
48  
49  
50 sub add_trace {
51 56 ▶ my ( $self, $entry ) = @_;
Subroutine add_trace: called CC=2 SCAR=7.0
Pod documented
52 56 ▶ croak "trace entry must be a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 56 56
53 unless ref($entry) eq 'HASH';
54 56 push @{$self->trace}, $entry;
55 56 return $self;
56 }
57  
58  
59 sub branch {
60 9 ▶ my ( $self, %opts ) = @_;
Subroutine branch: called CC=5 SCAR=16.7
Pod documented
61 9 my $metadata = _clone_data($self->metadata);
62 9 ▶ if ($opts{metadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"metadata"}) 9 0 9
63 croak "branch metadata must be a hashref"
64 9 ▶ unless ref($opts{metadata}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"metadata"} eq "HASH" 0 9 9
65 9 $metadata = { %{$metadata}, %{$opts{metadata}} };
66 }
67  
68 my $branch = __PACKAGE__->new(
69 9 ▶ input => _clone_data(exists $opts{input} ? $opts{input} : $self->input),
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'input'} ? : 0 9 9
$self->has_history ? : 0 9 9
70 state => _clone_data($self->state),
71 artifacts => _clone_data($self->artifacts),
72 metadata => $metadata,
73 trace => _clone_data($self->trace),
74 ($self->has_history ? (history => _clone_data($self->history)) : ()),
75 );
76  
77 9 return $branch;
78 }
79  
80  
81 sub merge_branch {
82 9 ▶ my ( $self, $branch, %opts ) = @_;
Subroutine merge_branch: called CC=8 SCAR=24.3
Pod documented
83 9 ▶ croak "merge_branch expects a Langertha::RunContext"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($branch) and $branch->isa(__PACKAGE__) 0 9 9
Condition33%1/3
builtin::blessed($branch) and $branch->isa(__PACKAGE__)
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($branch) and $branch->isa(__PACKAGE__)
builtin::blessed($branch) $branch->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($branch)B= $branch->isa(__PACKAGE__)
ABresult
0X0
100
111
84 unless blessed($branch) && $branch->isa(__PACKAGE__);
85  
86 9 ▶ my $slot = $opts{slot} // 'branches';
Condition50%1/2
$opts{"slot"} // "branches"
l!l
90
MC/DC0%0/1
$opts{"slot"} // "branches"
$opts{"slot"}
Truth table50%1/2
A
A= $opts{"slot"}
Aresult
11
00
87 my $name = defined $opts{name}
88 ? $opts{name}
89 9 ▶ : scalar(keys %{$self->artifacts->{$slot} // {}});
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'name'} ? : 9 0 9
Condition0%0/2
$self->artifacts->{$slot} // {}
l!l
00
MC/DC0%0/1
$self->artifacts->{$slot} // {}
$self->artifacts->{$slot}
Truth table0%0/2
A
A= $self->artifacts->{$slot}
Aresult
11
00
90  
91 $self->artifacts->{$slot} = {}
92 9 ▶ unless exists $self->artifacts->{$slot};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->artifacts->{$slot} 5 4 9
93 croak "artifact slot '$slot' must be a hashref"
94 9 ▶ unless ref($self->artifacts->{$slot}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->artifacts->{$slot} eq "HASH" 0 9 9
95  
96 9 $self->artifacts->{$slot}{$name} = {
97 input => _clone_data($branch->input),
98 state => _clone_data($branch->state),
99 artifacts => _clone_data($branch->artifacts),
100 metadata => _clone_data($branch->metadata),
101 trace => _clone_data($branch->trace),
102 };
103  
104 9 return $self;
105 }
106  
107  
108 sub _clone_data {
109 90 ▶ my ( $data ) = @_;
Subroutine _clone_data: called CC=5 SCAR=23.4
110 90 ▶ return $data unless ref($data);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 18 72 90
111  
112 72 my $copy = eval { dclone($data) };
113 72 ▶ return $copy if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 72 0 72
114  
115 0 ▶ if (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 0 0 0
116 0 return { %{$data} };
117 }
118 0 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 0 0 0
119 0 return [ @{$data} ];
120 }
121 0 return $data;
122 }
123  
124 7 __PACKAGE__->meta->make_immutable;
125  
126 7 1;
127  
128 __END__
129  
130 =pod
131  
132 =encoding UTF-8
133  
134 =head1 NAME
135  
136 Langertha::RunContext - Shared execution context for Raid and Raider runs
137  
138 =head1 VERSION
139  
140 version 0.502
141  
142 =head1 SYNOPSIS
143  
144 my $ctx = Langertha::RunContext->new(
145 input => 'start',
146 state => { counter => 0 },
147 );
148  
149 my $branch = $ctx->branch(metadata => { path => 'left' });
150 $ctx->merge_branch($branch, slot => 'parallel', name => 'left');
151  
152 =head1 DESCRIPTION
153  
154 Carries structured execution state through runnable nodes. It is designed to
155 be safely propagated across sequential steps and branched for parallel steps.
156  
157 =head2 input
158  
159 Current input payload for the next runnable step.
160  
161 =head2 state
162  
163 Mutable workflow state shared across sequential execution.
164  
165 =head2 artifacts
166  
167 Structured outputs and intermediate artifacts produced by runs.
168  
169 =head2 metadata
170  
171 Execution metadata (labels, counters, step info, etc.).
172  
173 =head2 trace
174  
175 Chronological trace entries describing orchestration progress.
176  
177 =head2 history
178  
179 Optional conversation/history payload mirrored from Raider runs.
180  
181 =head2 add_trace
182  
183 $ctx->add_trace({ event => 'step_result', step => 1 });
184  
185 Appends a trace hashref.
186  
187 =head2 branch
188  
189 my $branch_ctx = $ctx->branch(metadata => { branch => 0 });
190  
191 Creates an isolated context clone for parallel execution.
192  
193 =head2 merge_branch
194  
195 $ctx->merge_branch($branch_ctx, slot => 'parallel', name => 'step_a');
196  
197 Stores an immutable snapshot of a branch context in the parent context.
198  
199 =head1 SUPPORT
200  
201 =head2 Issues
202  
203 Please report bugs and feature requests on GitHub at
204 L<https://github.com/Getty/langertha/issues>.
205  
206 =head2 IRC
207  
208 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
209  
210 =head1 CONTRIBUTING
211  
212 Contributions are welcome! Please fork the repository and submit a pull request.
213  
214 =head1 AUTHOR
215  
216 Torsten Raudssus <getty@cpan.org>
217  
218 =head1 COPYRIGHT AND LICENSE
219  
220 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
221  
222 This is free software; you can redistribute it and/or modify it under
223 the same terms as the Perl 5 programming language system itself.
224  
225 =cut
« Langertha/Role/PluginHost.pm ↑ Summary Langertha/Role/Tools.pm »