blib/lib/Langertha/Raider/ToolArgs.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 88.8% 48 / 54 Conditioncond 72.0% 36 / 50 MC/DCmcdc 60.7% 17 / 28 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 85.7% 193 / 225 CC 47 SCAR 26.5
CC
47
Cov
88%
tool_args_problems
26.1
_type_includes
16.0
_type_of
7.0
CRAP 14.2
« blib/lib/Langertha/Raider.pm ↑ Summary blib/lib/Langertha/Raider/Detect.pm »
1 package Langertha::Raider::ToolArgs;
2 # ABSTRACT: Internal check of a tool call's arguments against the tool's inputSchema
3 98 our $VERSION = '0.503';
4  
5 98 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 98 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 98 ▶use JSON::MaybeXS qw( is_bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 98 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 98 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 98 our @EXPORT_OK = qw( tool_args_problems );
12  
13  
14 88 ▶sub tool_args_problems {
Subroutine tool_args_problems: called CC=26 SCAR=32.6
Pod documented
15 88 my ( $schema, $args ) = @_;
16 88 ▶ return unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 1 87 88
17 87 my $explicit_object = _type_includes($schema->{type}, 'object');
18 87 ▶ return if defined $schema->{type} && !$explicit_object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$schema{"type"} and not $explicit_object 1 86 87
Condition100%3/3
defined $$schema{"type"} and not $explicit_object
!ll&&!rl&&r
2841
MC/DC100%2/2
defined $$schema{"type"} and not $explicit_object
defined $$schema{"type"} not $explicit_object
Truth table100%3/3
A and not(B)
A= defined $$schema{"type"}B= not $explicit_object
ABresult
0X0
100
111
19  
20 86 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
851
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
21 86 ▶ unless (ref $args eq 'HASH' && !blessed $args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args eq "HASH" and not builtin::blessed($args)) 2 84 86
Condition66%2/3
ref $args eq "HASH" and not builtin::blessed($args)
!ll&&!rl&&r
2084
MC/DC50%1/2
ref $args eq "HASH" and not builtin::blessed($args)
ref $args eq "HASH" not builtin::blessed($args)
Truth table66%2/3
A and not(B)
A= ref $args eq "HASH"B= not builtin::blessed($args)
ABresult
0X0
100
111
22 2 ▶ return $explicit_object ? ( 'arguments must be an object, got '._type_of($args) ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$explicit_object ? : 1 1 2
23 }
24  
25 84 my ( @problems, %required );
26 84 ▶ if (ref $schema->{required} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$schema{"required"} eq "ARRAY") 71 13 84
27 71 for my $key (@{$schema->{required}}) {
28 71 ▶ next if !defined $key || ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or ref $key 0 71 71
Condition33%1/3
not defined $key or ref $key
l!l&&r!l&&!r
0071
MC/DC0%0/2
not defined $key or ref $key
not defined $key ref $key
Truth table33%1/3
not(A) or B
A= not defined $keyB= ref $key
ABresult
1X1
011
000
29 71 $required{$key} = 1;
30 71 ▶ push @problems, "missing required property '".$key."'" unless exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$args{$key} 8 63 71
31 }
32 }
33  
34 84 my $properties = $schema->{properties};
35 84 ▶ return @problems unless ref $properties eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $properties eq "HASH" 2 82 84
36 82 for my $key (sort keys %$args) {
37 106 my $prop = $properties->{$key};
38 106 ▶ next unless ref $prop eq 'HASH' && defined $prop->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prop eq "HASH" and defined $$prop{"type"} 8 98 106
Condition100%3/3
ref $prop eq "HASH" and defined $$prop{"type"}
!ll&&!rl&&r
7198
MC/DC100%2/2
ref $prop eq "HASH" and defined $$prop{"type"}
ref $prop eq "HASH" defined $$prop{"type"}
Truth table100%3/3
A and B
A= ref $prop eq "HASH"B= defined $$prop{"type"}
ABresult
0X0
100
111
39 98 my $value = $args->{$key};
40 98 ▶ next if !defined $value && !$required{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value or $required{$key} 4 94 98
Condition100%3/3
defined $value or $required{$key}
l!l&&r!l&&!r
9314
MC/DC100%2/2
defined $value or $required{$key}
defined $value $required{$key}
Truth table100%3/3
A or B
A= defined $valueB= $required{$key}
ABresult
1X1
011
000
41 94 ▶ my @types = ref $prop->{type} eq 'ARRAY' ? @{$prop->{type}} : ( $prop->{type} );
Branch: 1 branch
BranchTrueFalseTotal
ref $$prop{'type'} eq 'ARRAY' ? : 2 92 94
42 94 my $known = 0;
43 94 my $match = 0;
44 94 for my $type (@types) {
45 95 ▶ next if ref $type;
Branch: 1 branch
BranchTrueFalseTotal
if ref $type 0 95 95
46 95 ▶ my $check = _type_check($type) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $check = _type_check($type) or next 94 1 95
47 94 $known = 1;
48 94 ▶ if ($check->($value)) { $match = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (&$check($value)) 78 16 94
49 }
50 94 ▶ next if $match || !$known;
Branch: 1 branch
BranchTrueFalseTotal
if $match or not $known 79 15 94
Condition100%3/3
$match or not $known
l!l&&r!l&&!r
78115
MC/DC100%2/2
$match or not $known
$match not $known
Truth table100%3/3
A or not(B)
A= $matchB= not $known
ABresult
1X1
011
000
51 15 push @problems, "property '".$key."' must be ".join(' or ', grep { !ref } @types)
52 .', got '._type_of($value);
53 }
54 82 return @problems;
55 }
56  
57 # True when the schema's type (a name or a list of names) includes $want.
58 87 ▶sub _type_includes {
Subroutine _type_includes: called CC=8 SCAR=27.7
59 87 my ( $type, $want ) = @_;
60 87 ▶ return 0 unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 2 85 87
61 85 ▶ return ( grep { defined && !ref && $_ eq $want } @$type ) ? 1 : 0 if ref $type eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $type eq "ARRAY" 0 85 85
grep({defined $_ and not ref $_ and $_ eq $want;} @$type) ? : 0 0 0
Condition0%0/6
defined $_ && !ref($_) && $_ eq $want
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ && !ref($_) && $_ eq $want
defined $_ not ref $_ $_ eq $want
Truth table0%0/4
A and not(B) && C
A= defined $_B= not ref $_C= $_ eq $want
ABCresult
0XX0
10X0
1100
1111
62 85 ▶ return !ref $type && $type eq $want ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!ref($type) && $type eq $want ? : 84 1 85
Condition66%2/3
not ref $type and $type eq $want
!ll&&!rl&&r
0184
MC/DC50%1/2
not ref $type and $type eq $want
not ref $type $type eq $want
Truth table66%2/3
not(A) and B
A= not ref $typeB= $type eq $want
ABresult
0X0
100
111
63 }
64  
65 # A plain scalar: defined, no reference, no JSON boolean.
66 85 ▶sub _plain_scalar {
Subroutine _plain_scalar: called CC=3 SCAR=11.0
67 85 my ( $v ) = @_;
68 85 ▶ return defined $v && !ref $v && !is_bool($v);
Condition83%5/6
defined $v && !ref($v) && !is_bool($v)
!ll&&!rl&&r
5080
defined $v and not ref $v
!ll&&!rl&&r
1480
MC/DC66%2/3
defined $v && !ref($v) && !is_bool($v)
defined $v not ref $v !is_bool($v)
Truth table75%3/4
A and not(B) && not(C)
A= defined $vB= not ref $vC= !is_bool($v)
ABCresult
0XX0
10X0
1100
1111
69 }
70  
71 24 ▶sub _number {
Subroutine _number: called CC=4 SCAR=14.1
72 24 my ( $v ) = @_;
73 24 ▶ return _plain_scalar($v) && looks_like_number($v) && $v == $v && $v !~ /\A\s*[-+]?inf/i;
Condition77%7/9
_plain_scalar($v) && &looks_like_number($v) && $v == $v && !($v =~ /\A\s*[-+]?inf/i)
!ll&&!rl&&r
6018
_plain_scalar($v) and &looks_like_number($v) and $v == $v
!ll&&!rl&&r
6018
_plain_scalar($v) and &looks_like_number($v)
!ll&&!rl&&r
1518
MC/DC50%2/4
_plain_scalar($v) && &looks_like_number($v) && $v == $v && !($v =~ /\A\s*[-+]?inf/i)
_plain_scalar($v) &looks_like_number($v) $v == $v !($v =~ /\A\s*[-+]?inf/i)
Truth table60%3/5
A and B and C && not(D)
A= _plain_scalar($v)B= &looks_like_number($v)C= $v == $vD= !($v =~ /\A\s*[-+]?inf/i)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
74 }
75  
76 my %TYPE_CHECK = (
77 58 ▶ string => sub { _plain_scalar($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 13 ▶ number => sub { _number($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 11 ▶ integer => sub { _number($_[0]) && $_[0] == int $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
_number($_[0]) and $_[0] == int $_[0] 7 4 11
80 4 ▶ boolean => sub { is_bool($_[0]) || ( _plain_scalar($_[0]) && $_[0] =~ /\A[01]\z/ ) },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
is_bool($_[0]) or _plain_scalar($_[0]) and $_[0] =~ /\A[01]\z/ 1 3 4
Condition83%5/6
is_bool($_[0]) || _plain_scalar($_[0]) && $_[0] =~ /\A[01]\z/
l!l&&r!l&&!r
121
_plain_scalar($_[0]) and $_[0] =~ /\A[01]\z/
!ll&&!rl&&r
012
MC/DC66%2/3
is_bool($_[0]) || _plain_scalar($_[0]) && $_[0] =~ /\A[01]\z/
is_bool($_[0]) _plain_scalar($_[0]) $_[0] =~ /\A[01]\z/
Truth table75%3/4
A || B and C
A= is_bool($_[0])B= _plain_scalar($_[0])C= $_[0] =~ /\A[01]\z/
ABCresult
1XX1
0111
00X0
0100
81 3 ▶ object => sub { ref $_[0] eq 'HASH' },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 4 ▶ array => sub { ref $_[0] eq 'ARRAY' },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 ▶ null => sub { !defined $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 98 );
85  
86 95 ▶sub _type_check { $TYPE_CHECK{$_[0]} }
Subroutine _type_check: called CC=1 SCAR=0.0
87  
88 # The JSON type name of a decoded value, for the message.
89 16 ▶sub _type_of {
Subroutine _type_of: called CC=7 SCAR=19.5
90 16 my ( $v ) = @_;
91 16 ▶ return !defined $v ? 'null'
Branch: 6 branches
BranchTrueFalseTotal
!defined($v) ? : 1 15 16
is_bool($v) ? : 2 13 15
ref $v eq 'HASH' ? : 2 11 13
ref $v eq 'ARRAY' ? : 3 8 11
ref $v ? : 0 8 8
&looks_like_number($v) ? : 1 7 8
92 : is_bool($v) ? 'boolean'
93 : ref $v eq 'HASH' ? 'object'
94 : ref $v eq 'ARRAY' ? 'array'
95 : ref $v ? lc ref $v
96 : looks_like_number($v) ? 'number'
97 : 'string';
98 }
99  
100 98 1;
101  
102 __END__
103  
104 =pod
105  
106 =encoding UTF-8
107  
108 =head1 NAME
109  
110 Langertha::Raider::ToolArgs - Internal check of a tool call's arguments against the tool's inputSchema
111  
112 =head1 VERSION
113  
114 version 0.503
115  
116 =head1 DESCRIPTION
117  
118 B<Internal module.> Its interface may change without notice.
119  
120 The C<validate> step of the one execution path every tool call of a raid
121 takes (ADR 0005): before a tool runs, its arguments are held against the
122 C<inputSchema> the model was shown. Only what is clearly wrong is reported --
123 a C<required> key that is missing, or a top-level property whose value does
124 not have the C<type> the schema names. Everything JSON Schema says beyond that
125 (C<enum>, C<additionalProperties>, nested schemas, C<anyOf>, formats, ...) is
126 not checked here: the tool itself still answers for it.
127  
128 It stays lenient where Perl cannot tell or the model's slip does no harm:
129  
130 =over
131  
132 =item * A numeric string passes as C<number> / C<integer> (C<"5"> for an
133 integer), and a number passes as C<string>. Perl tools coerce either way, and
134 after any numeric use a Perl scalar no longer says which one it was.
135  
136 =item * C<0> / C<1> pass as C<boolean>, as Perl code writes booleans. The
137 strings C<"true"> / C<"false"> do not: C<"false"> is true in Perl.
138  
139 =item * C<null> for a property that is not C<required> counts as absent.
140  
141 =item * No schema, a schema whose C<type> is not C<object>, a property
142 without C<type>, and a type name not listed below: nothing is checked.
143  
144 =back
145  
146 =head2 tool_args_problems
147  
148 my @problems = tool_args_problems($input_schema, $arguments);
149  
150 Returns one message per violation, C<required> keys first (in schema order),
151 then property types (by name); the empty list when the arguments pass.
152 C<undef> arguments count as C<{}>. Known types: C<string>, C<number>,
153 C<integer>, C<boolean>, C<object>, C<array>, C<null>, also as a list
154 (C<< type => [ 'string', 'null' ] >>).
155  
156 =head1 SUPPORT
157  
158 =head2 Issues
159  
160 Please report bugs and feature requests on GitHub at
161 L<https://github.com/Getty/langertha-raider/issues>.
162  
163 =head2 IRC
164  
165 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
166  
167 =head1 CONTRIBUTING
168  
169 Contributions are welcome! Please fork the repository and submit a pull request.
170  
171 =head1 AUTHOR
172  
173 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
174  
175 =head1 COPYRIGHT AND LICENSE
176  
177 This software is copyright (c) 2026 by Torsten Raudssus.
178  
179 This is free software; you can redistribute it and/or modify it under
180 the same terms as the Perl 5 programming language system itself.
181  
182 =cut
« blib/lib/Langertha/Raider.pm ↑ Summary blib/lib/Langertha/Raider/Detect.pm »