blib/lib/Langertha/Raider/Approval.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 18 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 100% 89 / 89 CC 13 SCAR 13.1
CC
13
Cov
100%
_canonical_value
7.0
covers
4.0
for_call
3.0
CRAP 3.7
« blib/lib/Langertha/Raid/Loop.pm ↑ Summary blib/lib/Langertha/Raider/SessionStore.pm »
1 package Langertha::Raider::Approval;
2 # ABSTRACT: Internal approval of one exact tool call, bound to session, run, tool, arguments and policy revision
3 2 our $VERSION = '0.503';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Digest::SHA qw( sha256_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS qw( JSON is_bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 has session_id => ( is => 'ro', isa => 'Str', required => 1 );
12  
13  
14 2 has run_id => ( is => 'ro', isa => 'Str', required => 1 );
15  
16  
17 2 has tool => ( is => 'ro', isa => 'Str', required => 1 );
18  
19  
20 2 has source => ( is => 'ro', isa => 'Str', required => 1 );
21  
22  
23 2 has arguments_sha256 => ( is => 'ro', isa => 'Str', required => 1 );
24  
25  
26 2 has policy_revision => ( is => 'ro', isa => 'Str', required => 1 );
27  
28  
29 27 ▶sub _bound_fields { qw( session_id run_id tool source arguments_sha256 policy_revision ) }
Subroutine _bound_fields: called CC=1 SCAR=0.0
30  
31 # The binding of a call in its context, as constructor arguments. A missing
32 # field stays undef; the callers decide what that means.
33 29 ▶sub _binding {
Subroutine _binding: called CC=2 SCAR=6.9
34 29 my ( $self, %arg ) = @_;
35 29 my $call = $arg{call};
36 29 ▶ croak __PACKAGE__.' call must be a hash of name, source and arguments'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $call eq "HASH" 2 27 29
37 unless ref $call eq 'HASH';
38 return (
39 session_id => $arg{session_id},
40 run_id => $arg{run_id},
41 policy_revision => $arg{policy_revision},
42 tool => $call->{name},
43 source => $call->{source},
44 27 arguments_sha256 => $self->digest_arguments($call->{arguments}),
45 );
46 }
47  
48 9 ▶sub for_call {
Subroutine for_call: called CC=3 SCAR=11.0
Pod documented
49 9 my ( $class, %arg ) = @_;
50 9 my %binding = $class->_binding(%arg);
51 8 for my $field ($class->_bound_fields) {
52 34 ▶ croak __PACKAGE__.'->for_call needs '.$field unless defined $binding{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binding{$field} 5 29 34
53 }
54 3 return $class->new(%binding);
55 }
56  
57  
58 20 ▶sub covers {
Subroutine covers: called CC=4 SCAR=13.9
Pod documented
59 20 my ( $self, %arg ) = @_;
60 20 my %binding = $self->_binding(%arg);
61 19 for my $field ($self->_bound_fields) {
62 82 ▶ return unless defined $binding{$field} && $binding{$field} eq $self->$field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binding{$field} and $binding{$field} eq $self->$field 15 67 82
Condition100%3/3
defined $binding{$field} and $binding{$field} eq $self->$field
!ll&&!rl&&r
21367
MC/DC100%2/2
defined $binding{$field} and $binding{$field} eq $self->$field
defined $binding{$field} $binding{$field} eq $self->$field
Truth table100%3/3
A and B
A= defined $binding{$field}B= $binding{$field} eq $self->$field
ABresult
0X0
100
111
63 }
64 4 return 1;
65 }
66  
67  
68 421 ▶sub digest_arguments {
Subroutine digest_arguments: called CC=1 SCAR=0.0
Pod documented
69 421 my ( $self, $arguments ) = @_;
70 421 my $json = JSON::MaybeXS->new( utf8 => 1, canonical => 1, allow_nonref => 1 );
71 421 return sha256_hex( $json->encode( $self->_canonical_value($arguments) ) );
72 }
73  
74  
75 # A copy of $value that encodes the same whatever Perl last did with its
76 # scalars (see digest_arguments). Returns undef as a value, not an empty list:
77 # it lands in a hash or array slot.
78 2602 ▶sub _canonical_value {
Subroutine _canonical_value: called CC=7 SCAR=19.5
79 2602 my ( $self, $value ) = @_;
80 2602 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 101 2501 2602
81 2501 ▶ return $value ? JSON->true : JSON->false if is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if is_bool($value) 201 2300 2501
$value ? : 197 4 201
82 2300 my $ref = ref $value;
83 2300 ▶ return "$value" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 1384 916 2300
84 916 ▶ return { map { $_ => $self->_canonical_value($value->{$_}) } keys %$value } if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 899 17 916
85 17 ▶ return [ map { $self->_canonical_value($_) } @$value ] if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 15 2 17
86 2 croak __PACKAGE__.' arguments can hold no '.$ref;
87 }
88  
89 2 __PACKAGE__->meta->make_immutable;
90  
91 2 1;
92  
93 __END__
94  
95 =pod
96  
97 =encoding UTF-8
98  
99 =head1 NAME
100  
101 Langertha::Raider::Approval - Internal approval of one exact tool call, bound to session, run, tool, arguments and policy revision
102  
103 =head1 VERSION
104  
105 version 0.503
106  
107 =head1 SYNOPSIS
108  
109 # Internal to Langertha-Raider -- no API promise.
110 my $approval = Langertha::Raider::Approval->for_call(
111 session_id => $session->id,
112 run_id => $run,
113 policy_revision => $revision,
114 call => { name => 'write_file', source => 'engine:1', arguments => \%args },
115 );
116  
117 # later, for the call about to run -- same named arguments:
118 run_it() if $approval->covers(
119 session_id => $session->id, run_id => $run, policy_revision => $revision, call => $call,
120 );
121  
122 =head1 DESCRIPTION
123  
124 B<Internal module.> Its interface may change without notice.
125  
126 An approval as ADR 0005 binds it: to one session, one run, one tool (by name
127 and source) and one set of arguments, under one policy revision. Any change of
128 any of these invalidates it -- fixture F20: arguments changed after the
129 approval are not covered by it. The arguments are held as the SHA-256 of their
130 canonical JSON (L</digest_arguments>), never by reference, so a hash changed in
131 place after the approval is seen as changed.
132  
133 The call is the canonical call the tool gate sees
134 (C<< { name => ..., source => ..., arguments => ... } >>, see C<_tool_gate> in
135 L<Langertha::Raider>). This class is pure logic: nothing asks for an approval
136 or consults one yet, and an approval lives only as long as the run that holds
137 it -- nothing persists it.
138  
139 =head2 session_id
140  
141 The session the approval was given in. Required.
142  
143 =head2 run_id
144  
145 The run the approval was given in. Required.
146  
147 =head2 tool
148  
149 The name of the approved tool. Required.
150  
151 =head2 source
152  
153 Where the approved tool runs, as the tool gate names it (C<raider>,
154 C<inline>, C<engine:N>, C<catalog:NAME>). Required: the same tool name from
155 another source is another tool.
156  
157 =head2 arguments_sha256
158  
159 The L</digest_arguments> of the approved arguments. Required.
160  
161 =head2 policy_revision
162  
163 The revision of the policy the approval was given under. Required; compared
164 as a string.
165  
166 =head2 for_call
167  
168 my $approval = Langertha::Raider::Approval->for_call(
169 session_id => $sid, run_id => $rid, policy_revision => $rev,
170 call => { name => $tool, source => $source, arguments => $args },
171 );
172  
173 Builds the approval of C<call> in its context. Croaks when C<call> is not a
174 hash, or when the session id, run id, policy revision, tool name or source is
175 missing. C<arguments> may be absent; it is then bound as C<null>, which is not
176 the same as C<{}>.
177  
178 =head2 covers
179  
180 if ( $approval->covers( session_id => $sid, run_id => $rid,
181 policy_revision => $rev, call => $call ) ) { ... }
182  
183 True when the call in its context is exactly the approved one: same session,
184 run, tool, source, policy revision and canonical arguments. False on any
185 difference, and false when any of them is missing -- a missing field never
186 matches. Croaks only when C<call> is not a hash.
187  
188 =head2 digest_arguments
189  
190 my $hex = Langertha::Raider::Approval->digest_arguments(\%args);
191  
192 The SHA-256 (hex) of the canonical JSON of C<$arguments>: hash keys sorted at
193 every depth, arrays in order, UTF-8. Before encoding, every plain scalar is
194 taken by its string form, so C<5> and C<"5"> are the same argument -- the
195 digest does not depend on whether Perl last used a value as a number or a
196 string, nor on how the JSON backend writes numbers. C<"1.0"> and C<"1"> stay
197 different. Booleans (L<JSON::MaybeXS/is_bool>) stay booleans and C<undef>
198 stays C<null>, so C<true>, C<1>, C<"true">, C<false>, C<0>, C<"">, and
199 C<null> are all different. Croaks on anything JSON arguments cannot hold:
200 code refs, objects other than booleans, scalar refs.
201  
202 =head1 SUPPORT
203  
204 =head2 Issues
205  
206 Please report bugs and feature requests on GitHub at
207 L<https://github.com/Getty/langertha-raider/issues>.
208  
209 =head2 IRC
210  
211 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
212  
213 =head1 CONTRIBUTING
214  
215 Contributions are welcome! Please fork the repository and submit a pull request.
216  
217 =head1 AUTHOR
218  
219 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 This software is copyright (c) 2026 by Torsten Raudssus.
224  
225 This is free software; you can redistribute it and/or modify it under
226 the same terms as the Perl 5 programming language system itself.
227  
228 =cut
« blib/lib/Langertha/Raid/Loop.pm ↑ Summary blib/lib/Langertha/Raider/SessionStore.pm »