blib/lib/Langertha/Holmgang/Report.pm

filter: Statementstmt 99.7% 396 / 397 Branchbran 83.7% 191 / 228 Conditioncond 67.7% 107 / 158 MC/DCmcdc 44.8% 39 / 87 Subroutinesub 100% 39 / 39 Podpod 84.6% 11 / 13 totaltotal 84.9% 783 / 922 CC 194 SCAR 31.2
CC
194
Cov
89%
_shape
45.9
from_cai_platform
37.2
_cai_repetition
28.8
CRAP 22.7
« blib/lib/Langertha/Holmgang/Batch.pm ↑ Summary blib/lib/Langertha/Holmgang/CLI.pm »
1 package Langertha::Holmgang::Report;
2 # ABSTRACT: The JSON report of a Holmgang invocation, format holmgang_report 1
3  
4 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Digest::SHA qw( sha256_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Langertha ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Langertha::Holmgang ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 29 our $VERSION = '0.002';
17  
18 #### Format
19  
20 # The format version this module writes and the highest it reads (ADR 0006).
21 112 ▶sub format_version { 1 }
Subroutine format_version: called CC=1 SCAR=0.0
Pod undocumented
22  
23 # The shape of format 1. A field spec is a primitive name (int num str bool
24 # time url any), the name of an entry of this table, or a hash with `type`
25 # and optional `null => 0` (never null), `min`, `max`, `of` (array element)
26 # or `values` (enum). Every field may be null unless it says `null => 0`;
27 # array elements are never null. `optional => 1` marks a field added after
28 # version 1 came out: a reader takes it as null where an older report lacks it.
29 29 my %SPEC = (
30 report => { type => 'object', fields => {
31 holmgang_report => { type => 'int', null => 0 },
32 generator => 'generator',
33 started_at => 'time',
34 finished_at => 'time',
35 runs => { type => 'array', of => 'run', null => 0 },
36 } },
37 generator => { type => 'object', fields => {
38 holmgang => 'str',
39 langertha => 'str',
40 sandbox => { type => 'str', optional => 1, omit => 1 },
41 } },
42 run => { type => 'object', fields => {
43 target => 'target',
44 variant => 'variant',
45 suite => 'suite',
46 repeat => { type => 'int', min => 1, null => 0 },
47 status => { type => 'enum', values => [qw( complete cancelled aborted )], null => 0 },
48 error => 'str',
49 cases => { type => 'array', of => 'case', null => 0 },
50 } },
51 target => { type => 'object', null => 0, fields => {
52 label => 'str',
53 engine => 'str',
54 model => 'str',
55 url => 'url',
56 } },
57 variant => { type => 'object', null => 0, fields => {
58 name => 'str',
59 settings => 'any',
60 } },
61 suite => { type => 'object', null => 0, fields => {
62 id => { type => 'str', null => 0 },
63 version => 'str',
64 topic => 'str',
65 digest => 'str',
66 } },
67 case => { type => 'object', fields => {
68 case_id => { type => 'str', null => 0 },
69 title => 'str',
70 tags => { type => 'array', of => 'str' },
71 tier => 'str',
72 difficulty => { type => 'str', optional => 1 },
73 scenario => { type => 'str', optional => 1, omit => 1 },
74 weight => { type => 'num', min => 0 },
75 hard => 'bool',
76 untrusted => 'bool',
77 repetitions => { type => 'array', of => 'repetition', null => 0 },
78 } },
79 repetition => { type => 'object', fields => {
80 repetition => { type => 'int', min => 1, null => 0 },
81 seed => 'int',
82 status => { type => 'enum', values => [qw(
83 no_vision context_exceeded error turn_limit passed uncertain failed
84 )], null => 0 },
85 score => { type => 'num', min => 0, max => 1 },
86 error => 'str',
87 finish_reason => 'str',
88 duration_ms => { type => 'int', min => 0 },
89 rules => { type => 'array', of => 'rule' },
90 diff => { type => 'array', of => 'change' },
91 turns => { type => 'array', of => 'turn' },
92 tokens => 'tokens',
93 calls => { type => 'array', of => 'call' },
94 sim_gaps => { type => 'array', of => 'gap' },
95 forbidden_calls => { type => 'int', min => 0 },
96 injection_held => 'bool',
97 transcript => { type => 'array', of => 'any' },
98 } },
99 rule => { type => 'object', fields => {
100 title => 'str',
101 kind => 'str',
102 required => 'bool',
103 passed => 'bool',
104 reason => 'str',
105 sim_gap => 'str',
106 } },
107 change => { type => 'object', fields => {
108 path => 'str',
109 before => 'any',
110 after => 'any',
111 } },
112 turn => { type => 'object', fields => {
113 input_tokens => { type => 'int', min => 0 },
114 output_tokens => { type => 'int', min => 0 },
115 reasoning_tokens => { type => 'int', min => 0 },
116 duration_ms => { type => 'int', min => 0 },
117 finish_reason => 'str',
118 calls => { type => 'int', min => 0 },
119 } },
120 tokens => { type => 'object', null => 0, fields => {
121 input => { type => 'int', min => 0 },
122 output => { type => 'int', min => 0 },
123 reasoning => { type => 'int', min => 0 },
124 input_max => { type => 'int', min => 0 },
125 } },
126 call => { type => 'object', fields => {
127 tool => 'str',
128 args => 'any',
129 succeeded => 'bool',
130 sim_gap => 'str',
131 sandbox => { type => 'int', min => 0, optional => 1, omit => 1 },
132 } },
133 gap => { type => 'object', fields => {
134 call => { type => 'int', min => 0 },
135 tool => 'str',
136 gap => 'str',
137 } },
138 );
139  
140 # Query parameters of a target url that carry credentials.
141 29 my $SECRET_PARAM = qr/key|token|secret|pass|auth|sig|cred/i;
142  
143 # Image content in any form a provider takes it.
144 29 my $DATA_URL = qr{\bdata:(?:image/|[^,\s]*;base64,)}i;
145 29 my $BASE64_RUN = qr{\A[A-Za-z0-9+/]{1024,}={0,2}\z};
146  
147 #### Attributes
148  
149 29 has generator => ( is => 'ro', lazy_build => 1 );
150  
151 165 ▶sub _build_generator {
Subroutine _build_generator: called CC=1 SCAR=0.0
152 return {
153 165 holmgang => $Langertha::Holmgang::VERSION,
154 langertha => $Langertha::VERSION,
155 };
156 }
157  
158  
159 29 has started_at => ( is => 'ro', lazy_build => 1 );
160  
161 83 ▶sub _build_started_at { shift->now }
Subroutine _build_started_at: called CC=1 SCAR=0.0
162  
163  
164 29 has finished_at => ( is => 'ro', writer => '_set_finished_at' );
165  
166  
167 29 has with_transcript => ( is => 'ro', default => 0 );
168  
169  
170 170 ▶has runs => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
171  
172  
173 329 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
174 329 my ( $self ) = @_;
175 329 $self->generator;
176 329 $self->started_at;
177 329 my $runs = $self->runs;
178 329 ▶ croak __PACKAGE__.'->new runs must be an array' unless ref $runs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $runs eq "ARRAY" 0 329 329
179 329 @$runs = map { $self->_shape( 'run', $runs->[$_], 'runs['.$_.']', 0 ) } 0 .. $#$runs;
180 329 return;
181 }
182  
183 #### Building
184  
185 135 ▶sub now {
Subroutine now: called CC=2 SCAR=7.2
Pod documented
186 135 my ( $self, $epoch ) = @_;
187 135 ▶ return strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime( defined $epoch ? $epoch : time ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $epoch ? : 0 135 135
188 }
189  
190  
191 207 ▶sub add_run {
Subroutine add_run: called CC=20 SCAR=31.2
Pod documented
192 207 my ( $self, %arg ) = @_;
193 207 my $where = 'runs['.scalar( @{ $self->runs } ).']';
194 207 my $repeat = $arg{repeat};
195 207 ▶ my @cases = @{ $arg{cases} || croak __PACKAGE__.'->add_run needs cases' };
Condition50%1/2
$arg{'cases'} || croak('Langertha::Holmgang::Report->add_run needs cases')
l!l
2070
MC/DC0%0/1
$arg{'cases'} || croak('Langertha::Holmgang::Report->add_run needs cases')
$arg{'cases'}
Truth table50%1/2
A
A= $arg{'cases'}
Aresult
11
00
196 207 my ( %case, @order );
197 207 for my $i ( 0 .. $#cases ) {
198 384 my $c = $cases[$i];
199 croak __PACKAGE__.'->add_run '.$where.'.cases['.$i.'] has no case_id'
200 384 ▶ unless ref $c eq 'HASH' && defined $c->{case_id};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" and defined $c->{"case_id"} 0 384 384
Condition33%1/3
ref $c eq "HASH" and defined $c->{"case_id"}
!ll&&!rl&&r
00384
MC/DC0%0/2
ref $c eq "HASH" and defined $c->{"case_id"}
ref $c eq "HASH" defined $c->{"case_id"}
Truth table33%1/3
A and B
A= ref $c eq "HASH"B= defined $c->{"case_id"}
ABresult
0X0
100
111
201 croak __PACKAGE__.'->add_run '.$where.': case '.$c->{case_id}.' listed twice'
202 384 ▶ if $case{ $c->{case_id} };
Branch: 1 branch
BranchTrueFalseTotal
if $case{$c->{"case_id"}} 0 384 384
203 3072 ▶ $case{ $c->{case_id} } = { ( map { $_ => $c->{$_} } qw( case_id title tags tier difficulty weight hard untrusted ) ),
Branch: 1 branch
BranchTrueFalseTotal
defined $c->{'scenario'} ? : 12 372 384
204 384 ( defined $c->{scenario} ? ( scenario => $c->{scenario} ) : () ), repetitions => [] };
205 384 push @order, $c->{case_id};
206 }
207 207 my %seen;
208 207 ▶ for my $r ( @{ $arg{results} || [] } ) {
Condition100%2/2
$arg{'results'} || []
l!l
19512
MC/DC100%1/1
$arg{'results'} || []
$arg{'results'}
Truth table100%2/2
A
A= $arg{'results'}
Aresult
11
00
209 516 ▶ croak __PACKAGE__.'->add_run '.$where.': a result is not a hash' unless ref $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $r eq "HASH" 0 516 516
210 516 my $id = $r->{case_id};
211 croak __PACKAGE__.'->add_run '.$where.': result for unknown case '.( defined $id ? $id : '(none)' )
212 516 ▶ unless defined $id && $case{$id};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $id and $case{$id} 1 515 516
defined $id ? : 1 0 1
Condition66%2/3
defined $id and $case{$id}
!ll&&!rl&&r
01515
MC/DC50%1/2
defined $id and $case{$id}
defined $id $case{$id}
Truth table66%2/3
A and B
A= defined $idB= $case{$id}
ABresult
0X0
100
111
213 515 my $n = $r->{repetition};
214 515 ▶ croak __PACKAGE__.'->add_run '.$where.': case '.$id.' result without a repetition number'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A[1-9][0-9]*\z/ 0 515 515
Condition33%1/3
defined $n and $n =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
00515
MC/DC0%0/2
defined $n and $n =~ /\A[1-9][0-9]*\z/
defined $n $n =~ /\A[1-9][0-9]*\z/
Truth table33%1/3
A and B
A= defined $nB= $n =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
215 unless defined $n && $n =~ /\A[1-9][0-9]*\z/;
216 515 ▶ croak __PACKAGE__.'->add_run '.$where.': case '.$id.' repetition '.$n.' beyond repeat '.$repeat
Branch: 1 branch
BranchTrueFalseTotal
if defined $repeat and $repeat =~ /\A[0-9]+\z/ and $n > $repeat 1 514 515
Condition50%3/6
defined $repeat and $repeat =~ /\A[0-9]+\z/ and $n > $repeat
!ll&&!rl&&r
05141
defined $repeat and $repeat =~ /\A[0-9]+\z/
!ll&&!rl&&r
00515
MC/DC33%1/3
defined $repeat and $repeat =~ /\A[0-9]+\z/ and $n > $repeat
defined $repeat $repeat =~ /\A[0-9]+\z/ $n > $repeat
Truth table50%2/4
A and B and C
A= defined $repeatB= $repeat =~ /\A[0-9]+\z/C= $n > $repeat
ABCresult
0XX0
10X0
1100
1111
217 if defined $repeat && $repeat =~ /\A[0-9]+\z/ && $n > $repeat;
218 croak __PACKAGE__.'->add_run '.$where.': case '.$id.' repetition '.$n.' given twice'
219 514 ▶ if $seen{$id}{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$id}{$n}++ 1 513 514
220 513 push @{ $case{$id}{repetitions} }, $self->_repetition( $r, $where.' case '.$id.' repetition '.$n );
221 }
222 202 for my $c ( values %case ) {
223 374 @{ $c->{repetitions} } = sort { $a->{repetition} <=> $b->{repetition} } @{ $c->{repetitions} };
224 }
225 my $run = $self->_shape( 'run', {
226 1212 ( map { $_ => $arg{$_} } qw( target variant suite repeat status error ) ),
227 202 cases => [ map { $case{$_} } @order ],
228 }, $where, 0 );
229 192 push @{ $self->runs }, $run;
230 192 return $run;
231 }
232  
233  
234 513 ▶sub _repetition {
Subroutine _repetition: called CC=12 SCAR=24.9
235 513 my ( $self, $r, $where ) = @_;
236 513 my $turns = $self->_shape( { type => 'array', of => 'turn' }, $r->{turns}, $where.'.turns', 0 );
237 512 my $calls = $self->_shape( { type => 'array', of => 'call' }, $r->{calls}, $where.'.calls', 0 );
238 511 my %tokens = map { $_ => undef } qw( input output reasoning input_max );
239 511 my ( $duration, $finish );
240 511 ▶ if ( $turns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turns) 505 6 511
241 505 $duration = 0;
242 505 for my $t ( @$turns ) {
243 360 ▶ $duration += $t->{duration_ms} if defined $t->{duration_ms};
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{"duration_ms"} 358 2 360
244 360 for my $f (qw( input output reasoning )) {
245 1080 my $v = $t->{ $f.'_tokens' };
246 1080 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 986 94 1080
247 94 ▶ $tokens{$f} = ( $tokens{$f} || 0 ) + $v;
Condition100%2/2
$tokens{$f} || 0
l!l
3856
MC/DC100%1/1
$tokens{$f} || 0
$tokens{$f}
Truth table100%2/2
A
A= $tokens{$f}
Aresult
11
00
248 }
249 $tokens{input_max} = $t->{input_tokens}
250 360 ▶ if defined $t->{input_tokens} && ( !defined $tokens{input_max} || $t->{input_tokens} > $tokens{input_max} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{"input_tokens"} and !defined($tokens{"input_max"}) || $t->{"input_tokens"} > $tokens{"input_max"} 54 306 360
Condition66%4/6
defined $t->{"input_tokens"} and !defined($tokens{"input_max"}) || $t->{"input_tokens"} > $tokens{"input_max"}
!ll&&!rl&&r
306054
not defined $tokens{"input_max"} or $t->{"input_tokens"} > $tokens{"input_max"}
l!l&&r!l&&!r
35190
MC/DC33%1/3
defined $t->{"input_tokens"} and !defined($tokens{"input_max"}) || $t->{"input_tokens"} > $tokens{"input_max"}
defined $t->{"input_tokens"} not defined $tokens{"input_max"} $t->{"input_tokens"} > $tokens{"input_max"}
Truth table75%3/4
A and not(B) or C
A= defined $t->{"input_tokens"}B= not defined $tokens{"input_max"}C= $t->{"input_tokens"} > $tokens{"input_max"}
ABCresult
0XX0
1000
11X1
1011
251 }
252 505 ▶ $finish = $turns->[-1]{finish_reason} if @$turns;
Branch: 1 branch
BranchTrueFalseTotal
if @$turns 204 301 505
253 }
254 511 my $gaps;
255 511 ▶ if ( $calls ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calls) 206 305 511
256 19 $gaps = [ map { +{ call => $_, tool => $calls->[$_]{tool}, gap => $calls->[$_]{sim_gap} } }
257 206 grep { defined $calls->[$_]{sim_gap} } 0 .. $#$calls ];
258 }
259 return {
260 4599 ( map { $_ => $r->{$_} } qw( repetition seed status score error rules diff forbidden_calls injection_held ) ),
261 finish_reason => $finish,
262 duration_ms => $duration,
263 turns => $turns,
264 tokens => \%tokens,
265 calls => $calls,
266 sim_gaps => $gaps,
267 511 transcript => scalar $self->_transcript( $r->{transcript}, $where ),
268 };
269 }
270  
271 511 ▶sub _transcript {
Subroutine _transcript: called CC=8 SCAR=20.9
272 511 my ( $self, $messages, $where ) = @_;
273 511 ▶ return unless $self->with_transcript && defined $messages;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->with_transcript and defined $messages 502 9 511
Condition100%3/3
$self->with_transcript and defined $messages
!ll&&!rl&&r
49489
MC/DC100%2/2
$self->with_transcript and defined $messages
$self->with_transcript defined $messages
Truth table100%3/3
A and B
A= $self->with_transcriptB= defined $messages
ABresult
0X0
100
111
274 9 ▶ croak __PACKAGE__.' '.$where.'.transcript must be an array' unless ref $messages eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $messages eq "ARRAY" 0 9 9
275 9 my @out;
276 9 for my $m ( @$messages ) {
277 14 ▶ if ( ref $m eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $m eq "HASH") 14 0 14
278 14 ▶ next if defined $m->{role} && $m->{role} eq 'system';
Branch: 1 branch
BranchTrueFalseTotal
if defined $m->{"role"} and $m->{"role"} eq "system" 2 12 14
Condition100%3/3
defined $m->{"role"} and $m->{"role"} eq "system"
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $m->{"role"} and $m->{"role"} eq "system"
defined $m->{"role"} $m->{"role"} eq "system"
Truth table100%3/3
A and B
A= defined $m->{"role"}B= $m->{"role"} eq "system"
ABresult
0X0
100
111
279 12 my %copy = %$m;
280 12 delete $copy{untrusted};
281 12 push @out, \%copy;
282 12 next;
283 }
284 0 push @out, $m;
285 }
286 9 return \@out;
287 }
288  
289 134 ▶sub finish {
Subroutine finish: called CC=2 SCAR=6.9
Pod documented
290 134 my ( $self, $at ) = @_;
291 134 ▶ $self->_set_finished_at( defined $at ? $at : $self->now );
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 82 52 134
292 134 return $self;
293 }
294  
295  
296 1 ▶sub image_entry {
Subroutine image_entry: called CC=5 SCAR=19.4
Pod documented
297 1 my ( $self, $name, $bytes ) = @_;
298 1 ▶ croak __PACKAGE__.'->image_entry needs a name and the image bytes' unless defined $name && defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $bytes 0 1 1
Condition33%1/3
defined $name and defined $bytes
!ll&&!rl&&r
001
MC/DC0%0/2
defined $name and defined $bytes
defined $name defined $bytes
Truth table33%1/3
A and B
A= defined $nameB= defined $bytes
ABresult
0X0
100
111
299 1 ▶ croak __PACKAGE__.'->image_entry wants bytes, got characters' if utf8::is_utf8($bytes) && $bytes =~ /[^\x00-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) and $bytes =~ /[^\x00-\xff]/ 0 1 1
Condition33%1/3
utf8::is_utf8($bytes) and $bytes =~ /[^\x00-\xff]/
!ll&&!rl&&r
100
MC/DC0%0/2
utf8::is_utf8($bytes) and $bytes =~ /[^\x00-\xff]/
utf8::is_utf8($bytes) $bytes =~ /[^\x00-\xff]/
Truth table33%1/3
A and B
A= utf8::is_utf8($bytes)B= $bytes =~ /[^\x00-\xff]/
ABresult
0X0
100
111
300 1 return { image => ''.$name, sha256 => sha256_hex($bytes), bytes => length $bytes };
301 }
302  
303  
304 #### Writing and reading
305  
306 57 ▶sub as_data {
Subroutine as_data: called CC=1 SCAR=0.0
Pod documented
307 57 my ( $self ) = @_;
308 return {
309 57 holmgang_report => $self->format_version,
310 generator => $self->_shape( 'generator', $self->generator, 'generator', 0 ),
311 started_at => $self->_shape( 'time', $self->started_at, 'started_at', 0 ),
312 finished_at => $self->_shape( 'time', $self->finished_at, 'finished_at', 0 ),
313 runs => $self->runs,
314 };
315 }
316  
317  
318 229 ▶sub _json {
Subroutine _json: called CC=1 SCAR=0.0
319 229 return JSON::MaybeXS->new( utf8 => 1, canonical => 1, pretty => 1, convert_blessed => 1 );
320 }
321  
322 54 ▶sub to_json { my ( $self ) = @_; return $self->_json->encode( $self->as_data ) }
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
323  
324  
325 23 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
326 23 my ( $self, $file ) = @_;
327 23 ▶ croak __PACKAGE__.'->write needs a file' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 23 23
328 23 path($file)->spew_raw( $self->to_json );
329 22 return $self;
330 }
331  
332  
333 60 ▶sub from_json {
Subroutine from_json: called CC=9 SCAR=22.2
Pod documented
334 60 my ( $class, $bytes, %arg ) = @_;
335 60 ▶ my $from = defined $arg{source} ? $arg{source} : 'report';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'source'} ? : 48 12 60
336 60 my $data = eval { $class->_json->decode($bytes) };
337 60 ▶ croak __PACKAGE__.' '.$from.' is not JSON: '.( $@ =~ s/\s+\z//r ) unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 58 60
338 58 ▶ croak __PACKAGE__.' '.$from.' is not a Holmgang report' unless ref $data eq 'HASH' && exists $data->{holmgang_report};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and exists $data->{"holmgang_report"} 3 55 58
Condition66%2/3
ref $data eq "HASH" and exists $data->{"holmgang_report"}
!ll&&!rl&&r
0355
MC/DC50%1/2
ref $data eq "HASH" and exists $data->{"holmgang_report"}
ref $data eq "HASH" exists $data->{"holmgang_report"}
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= exists $data->{"holmgang_report"}
ABresult
0X0
100
111
339 55 my $v = $data->{holmgang_report};
340 55 ▶ croak __PACKAGE__.' '.$from.': holmgang_report must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v and $v =~ /\A[1-9][0-9]*\z/ 1 54 55
Condition50%3/6
defined $v and not ref $v and $v =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
0154
defined $v and not ref $v
!ll&&!rl&&r
0055
MC/DC33%1/3
defined $v and not ref $v and $v =~ /\A[1-9][0-9]*\z/
defined $v not ref $v $v =~ /\A[1-9][0-9]*\z/
Truth table50%2/4
A and not(B) and C
A= defined $vB= not ref $vC= $v =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
341 unless defined $v && !ref $v && $v =~ /\A[1-9][0-9]*\z/;
342 54 ▶ croak __PACKAGE__.' '.$from.': unknown report version '.$v.' (this reader knows up to '.$class->format_version.')'
Branch: 1 branch
BranchTrueFalseTotal
if $v > $class->format_version 1 53 54
343 if $v > $class->format_version;
344 53 my $doc = $class->_shape( 'report', $data, $from, 1 );
345 return $class->new(
346 generator => $doc->{generator},
347 started_at => $doc->{started_at},
348 finished_at => $doc->{finished_at},
349 runs => $doc->{runs},
350 47 );
351 }
352  
353  
354 43 ▶sub read {
Subroutine read: called CC=2 SCAR=7.1
Pod documented
355 43 my ( $class, $file ) = @_;
356 43 ▶ croak __PACKAGE__.'->read needs a file' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 43 43
357 43 return $class->from_json( path($file)->slurp_raw, source => ''.$file );
358 }
359  
360  
361 #### Merging
362  
363 115 ▶sub merge {
Subroutine merge: called CC=17 SCAR=28.3
Pod documented
364 115 my ( $class, @reports ) = @_;
365 115 ▶ croak __PACKAGE__.'->merge needs at least one report' unless @reports;
Branch: 1 branch
BranchTrueFalseTotal
unless @reports 0 115 115
366 115 my ( %run, @order );
367 115 my $json = $class->_json;
368 115 for my $rep ( @reports ) {
369 130 for my $run ( @{ $rep->runs } ) {
370 163 my $key = $json->encode( { map { $_ => $run->{$_} } qw( target variant suite ) } );
371 163 ▶ unless ( $run{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($run{$key}) 152 11 163
372 152 push @order, $key;
373 152 $run{$key} = { %$run, cases => [] , _case => {} };
374 }
375 163 my $m = $run{$key};
376 croak __PACKAGE__.'->merge: run of suite '.$run->{suite}{id}.' has repeat '.$m->{repeat}.' and '.$run->{repeat}
377 163 ▶ unless $m->{repeat} == $run->{repeat};
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"repeat"} == $run->{"repeat"} 1 162 163
378 162 $m->{status} = $class->_worse_status( $m->{status}, $run->{status} );
379 162 ▶ $m->{error} = $run->{error} unless defined $m->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"error"} 154 8 162
380 162 for my $c ( @{ $run->{cases} } ) {
381 269 my $mc = $m->{_case}{ $c->{case_id} };
382 269 ▶ unless ( $mc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mc) 260 9 269
383 260 $mc = $m->{_case}{ $c->{case_id} } = { %$c, repetitions => [] };
384 260 push @{ $m->{cases} }, $mc;
385 }
386 # a field added after version 1 is null in an older report: the other's value holds
387 269 for my $f ( $class->_optional_case_fields ) {
388 538 ▶ $mc->{$f} = $c->{$f} unless defined $mc->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mc->{$f} 496 42 538
389 }
390 269 my %meta_a = %$mc; delete $meta_a{repetitions};
391 269 my %meta_b = %$c; delete $meta_b{repetitions};
392 269 ▶ defined $meta_b{$_} or $meta_b{$_} = $meta_a{$_} for $class->_optional_case_fields;
Branch: 1 branch
BranchTrueFalseTotal
defined $meta_b{$_} or $meta_b{$_} = $meta_a{$_} 42 496 538
393 269 ▶ croak __PACKAGE__.'->merge: case '.$c->{case_id}.' of suite '.$run->{suite}{id}.' differs between reports'
Branch: 1 branch
BranchTrueFalseTotal
unless $json->encode(\%meta_a) eq $json->encode(\%meta_b) 2 267 269
394 unless $json->encode( \%meta_a ) eq $json->encode( \%meta_b );
395 267 for my $r ( @{ $c->{repetitions} } ) {
396 403 my ($have) = grep { $_->{repetition} == $r->{repetition} } @{ $mc->{repetitions} };
397 403 ▶ unless ( $have ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have) 397 6 403
398 397 push @{ $mc->{repetitions} }, $r;
399 397 next;
400 }
401 croak __PACKAGE__.'->merge: case '.$c->{case_id}.' repetition '.$r->{repetition}.' of suite '
402 6 ▶ .$run->{suite}{id}.' is in two reports with different results'
Branch: 1 branch
BranchTrueFalseTotal
unless $json->encode($class->_stable($have)) eq $json->encode($class->_stable($r)) 1 5 6
403 unless $json->encode( $class->_stable($have) ) eq $json->encode( $class->_stable($r) );
404 }
405 266 @{ $mc->{repetitions} } = sort { $a->{repetition} <=> $b->{repetition} } @{ $mc->{repetitions} };
406 }
407 }
408 }
409 111 my @runs = map { my $r = $run{$_}; delete $r->{_case}; $r } @order;
410 111 my @start = sort grep { defined } map { $_->started_at } @reports;
411 111 my @finish = sort grep { defined } map { $_->finished_at } @reports;
412 111 return $class->new(
413 generator => $reports[0]->generator,
414 started_at => $start[0],
415 finished_at => $finish[-1],
416 runs => \@runs,
417 );
418 }
419  
420  
421 538 ▶sub _optional_case_fields {
Subroutine _optional_case_fields: called CC=2 SCAR=6.9
422 538 my $f = $SPEC{case}{fields};
423 538 ▶ return grep { ref $f->{$_} && $f->{$_}{optional} } sort keys %$f;
Condition100%3/3
ref $f->{$_} && $f->{$_}{'optional'}
!ll&&!rl&&r
215221521076
MC/DC100%2/2
ref $f->{$_} && $f->{$_}{'optional'}
ref $f->{$_} $f->{$_}{'optional'}
Truth table100%3/3
A && B
A= ref $f->{$_}B= $f->{$_}{'optional'}
ABresult
0X0
100
111
424 }
425  
426 166 ▶sub _worse_status {
Subroutine _worse_status: called CC=2 SCAR=6.9
427 166 my ( $self, $x, $y ) = @_;
428 166 my %rank = ( complete => 0, cancelled => 1, aborted => 2 );
429 166 ▶ return $rank{$y} > $rank{$x} ? $y : $x;
Branch: 1 branch
BranchTrueFalseTotal
$rank{$y} > $rank{$x} ? : 1 165 166
430 }
431  
432 # A repetition without its volatile fields, for comparison.
433 14 ▶sub _stable {
Subroutine _stable: called CC=2 SCAR=6.9
434 14 my ( $self, $r ) = @_;
435 14 my %copy = %$r;
436 14 delete $copy{duration_ms};
437 14 ▶ $copy{turns} = [ map { my %t = %$_; delete $t{duration_ms}; \%t } @{ $r->{turns} } ] if $r->{turns};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"turns"} 14 0 14
438 14 return \%copy;
439 }
440  
441 #### Shape
442  
443 59503 ▶sub _shape {
Subroutine _shape: called CC=44 SCAR=38.3
444 59503 my ( $self, $spec, $value, $where, $strict ) = @_;
445 59503 ▶ $spec = $SPEC{$spec} || { type => $spec } unless ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec 25246 34257 59503
Condition100%2/2
$SPEC{$spec} || {'type', $spec}
l!l
299922247
MC/DC100%1/1
$SPEC{$spec} || {'type', $spec}
$SPEC{$spec}
Truth table100%2/2
A
A= $SPEC{$spec}
Aresult
11
00
446 59503 my $type = $spec->{type};
447 59503 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 20903 38600 59503
448 20903 ▶ croak __PACKAGE__.' '.$where.' must not be null' if defined $spec->{null} && !$spec->{null};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"null"} and not $spec->{"null"} 1 20902 20903
Condition66%2/3
defined $spec->{"null"} and not $spec->{"null"}
!ll&&!rl&&r
2090201
MC/DC50%1/2
defined $spec->{"null"} and not $spec->{"null"}
defined $spec->{"null"} not $spec->{"null"}
Truth table66%2/3
A and not(B)
A= defined $spec->{"null"}B= not $spec->{"null"}
ABresult
0X0
100
111
449 20902 return undef;
450 }
451 38600 ▶ if ( $type eq 'object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "object") 7933 30667 38600
452 7933 ▶ croak __PACKAGE__.' '.$where.' must be an object' unless ref $value eq 'HASH' && !blessed $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" and not builtin::blessed($value) 0 7933 7933
Condition33%1/3
ref $value eq "HASH" and not builtin::blessed($value)
!ll&&!rl&&r
007933
MC/DC0%0/2
ref $value eq "HASH" and not builtin::blessed($value)
ref $value eq "HASH" not builtin::blessed($value)
Truth table33%1/3
A and not(B)
A= ref $value eq "HASH"B= not builtin::blessed($value)
ABresult
0X0
100
111
453 7933 my %out;
454 7933 for my $f ( sort keys %{ $spec->{fields} } ) {
455 54205 my $fs = $spec->{fields}{$f};
456 # a field that exists only when it says something (sandbox): absent, not null
457 54205 ▶ next if ref $fs && $fs->{omit} && !defined $value->{$f};
Branch: 1 branch
BranchTrueFalseTotal
if ref $fs and $fs->{"omit"} and not defined $value->{$f} 1393 52812 54205
Condition100%6/6
ref $fs and $fs->{"omit"} and not defined $value->{$f}
!ll&&!rl&&r
52776361393
ref $fs and $fs->{"omit"}
!ll&&!rl&&r
24621281551429
MC/DC100%3/3
ref $fs and $fs->{"omit"} and not defined $value->{$f}
ref $fs $fs->{"omit"} not defined $value->{$f}
Truth table100%4/4
A and B and not(C)
A= ref $fsB= $fs->{"omit"}C= not defined $value->{$f}
ABCresult
0XX0
10X0
1100
1111
458 croak __PACKAGE__.' '.$where.'.'.$f.' is missing'
459 52812 ▶ if $strict && !exists $value->{$f} && !( ref $fs && $fs->{optional} );
Branch: 1 branch
BranchTrueFalseTotal
if $strict and not exists $value->{$f} and not ref $fs && $fs->{"optional"} 3 52809 52812
Condition100%9/9
$strict and not exists $value->{$f} and not ref $fs && $fs->{"optional"}
!ll&&!rl&&r
5280543
$strict and not exists $value->{$f}
!ll&&!rl&&r
4661561907
ref $fs && $fs->{'optional'}
!ll&&!rl&&r
214
MC/DC100%4/4
$strict and not exists $value->{$f} and not ref $fs && $fs->{"optional"}
$strict not exists $value->{$f} ref $fs $fs->{'optional'}
Truth table100%5/5
A and not(B) and not(C && D)
A= $strictB= not exists $value->{$f}C= ref $fsD= $fs->{'optional'}
ABCDresult
0XXX0
10XX0
11110
110X1
11101
460 52809 $out{$f} = $self->_shape( $fs, $value->{$f}, $where.'.'.$f, $strict );
461 }
462 7881 return \%out;
463 }
464 30667 ▶ if ( $type eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array") 5406 25261 30667
465 5406 ▶ croak __PACKAGE__.' '.$where.' must be an array' unless ref $value eq 'ARRAY' && !blessed $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" and not builtin::blessed($value) 0 5406 5406
Condition33%1/3
ref $value eq "ARRAY" and not builtin::blessed($value)
!ll&&!rl&&r
005406
MC/DC0%0/2
ref $value eq "ARRAY" and not builtin::blessed($value)
ref $value eq "ARRAY" not builtin::blessed($value)
Truth table33%1/3
A and not(B)
A= ref $value eq "ARRAY"B= not builtin::blessed($value)
ABresult
0X0
100
111
466 5406 my $of = $spec->{of};
467 5406 ▶ my $el = ref $of ? { %$of, null => 0 } : $SPEC{$of} ? { %{ $SPEC{$of} }, null => 0 } : { type => $of, null => 0 };
Branch: 2 branches
BranchTrueFalseTotal
ref $of ? : 0 5406 5406
$SPEC{$of} ? : 4964 442 5406
468 5406 return [ map { $self->_shape( $el, $value->[$_], $where.'['.$_.']', $strict ) } 0 .. $#$value ];
469 }
470 25261 ▶ return $self->_any( $value, $where, $strict ) if $type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" 1575 23686 25261
471 23686 ▶ if ( $type eq 'bool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bool") 3524 20162 23686
472 3524 ▶ if ( JSON::MaybeXS::is_bool($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (JSON::MaybeXS::is_bool($value)) 2066 1458 3524
473 2066 ▶ return $value ? JSON::MaybeXS::true() : JSON::MaybeXS::false();
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1323 743 2066
474 }
475 1458 ▶ croak __PACKAGE__.' '.$where.' must be a JSON boolean' if $strict || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if $strict or ref $value 1 1457 1458
Condition66%2/3
$strict or ref $value
l!l&&r!l&&!r
101457
MC/DC50%1/2
$strict or ref $value
$strict ref $value
Truth table66%2/3
A or B
A= $strictB= ref $value
ABresult
1X1
011
000
476 1457 ▶ return $value ? JSON::MaybeXS::true() : JSON::MaybeXS::false();
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 882 575 1457
477 }
478 20162 ▶ croak __PACKAGE__.' '.$where.' must be a scalar' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 20162 20162
479 20162 ▶ if ( $type eq 'int' || $type eq 'num' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int" or $type eq "num") 7779 12383 20162
Condition100%3/3
$type eq "int" or $type eq "num"
l!l&&r!l&&!r
6543123612383
MC/DC100%2/2
$type eq "int" or $type eq "num"
$type eq "int" $type eq "num"
Truth table100%3/3
A or B
A= $type eq "int"B= $type eq "num"
ABresult
1X1
011
000
480 7779 ▶ my $re = $type eq 'int' ? qr/\A-?[0-9]+\z/ : qr/\A-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'int' ? : 6543 1236 7779
481 7779 ▶ croak __PACKAGE__.' '.$where.' must be '.( $type eq 'int' ? 'an integer' : 'a number' ).", got '".$value."'"
Branch: 2 branches
BranchTrueFalseTotal
unless $value =~ /$re/ 1 7778 7779
$type eq 'int' ? : 1 0 1
482 unless $value =~ $re;
483 7778 my $n = 0 + $value;
484 7778 ▶ croak __PACKAGE__.' '.$where.' must be at least '.$spec->{min} if defined $spec->{min} && $n < $spec->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"min"} and $n < $spec->{"min"} 0 7778 7778
Condition66%2/3
defined $spec->{"min"} and $n < $spec->{"min"}
!ll&&!rl&&r
6477140
MC/DC0%0/2
defined $spec->{"min"} and $n < $spec->{"min"}
defined $spec->{"min"} $n < $spec->{"min"}
Truth table66%2/3
A and B
A= defined $spec->{"min"}B= $n < $spec->{"min"}
ABresult
0X0
100
111
485 7778 ▶ croak __PACKAGE__.' '.$where.' must be at most '.$spec->{max} if defined $spec->{max} && $n > $spec->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec->{"max"} and $n > $spec->{"max"} 1 7777 7778
Condition100%3/3
defined $spec->{"max"} and $n > $spec->{"max"}
!ll&&!rl&&r
73114661
MC/DC100%2/2
defined $spec->{"max"} and $n > $spec->{"max"}
defined $spec->{"max"} $n > $spec->{"max"}
Truth table100%3/3
A and B
A= defined $spec->{"max"}B= $n > $spec->{"max"}
ABresult
0X0
100
111
486 7777 return $n;
487 }
488 12383 my $text = ''.$value;
489 12383 $self->_check_text( $text, $where );
490 12382 ▶ if ( $type eq 'enum' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'enum') { } 1546 10836 12382
elsif ($type eq 'time') { } 193 10643 10836
elsif ($type eq 'url') { } 103 10540 10643
491 1546 ▶ croak __PACKAGE__.' '.$where.": unknown value '".$text."'" unless grep { $_ eq $text } @{ $spec->{values} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $text;} @{$$spec{"values"};} 3 1543 1546
492 }
493 elsif ( $type eq 'time' ) {
494 193 ▶ croak __PACKAGE__.' '.$where.": not an ISO-8601 time '".$text."'"
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:\.[0-9]+)?)?(?:Z|[-+][0-9]{2}:?[0-9]{2})\z/ 0 193 193
495 unless $text =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:\.[0-9]+)?)?(?:Z|[-+][0-9]{2}:?[0-9]{2})\z/;
496 }
497 elsif ( $type eq 'url' ) {
498 103 return $self->_clean_url($text);
499 }
500 12276 return $text;
501 }
502  
503 # Deep copy of free data (arguments, diff values, settings, transcript):
504 # booleans normalised, objects and image content refused.
505 2394 ▶sub _any {
Subroutine _any: called CC=12 SCAR=24.9
506 2394 my ( $self, $value, $where, $strict ) = @_;
507 2394 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 2393 2394
508 2393 ▶ return $value ? JSON::MaybeXS::true() : JSON::MaybeXS::false() if JSON::MaybeXS::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 22 2371 2393
$value ? : 15 7 22
509 2371 ▶ croak __PACKAGE__.' '.$where.' holds an object ('.ref($value).'); only plain data goes into a report'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) 1 2370 2371
510 if blessed $value;
511 2370 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 998 1372 2370
512 croak __PACKAGE__.' '.$where.' holds base64 image content'
513 if ( ref $value->{source} eq 'HASH' && ( $value->{source}{type} // '' ) eq 'base64' )
514 998 ▶ || grep { exists $value->{$_} } qw( inline_data inlineData b64_json );
Branch: 1 branch
BranchTrueFalseTotal
if ref $value->{"source"} eq "HASH" and ($value->{"source"}{"type"} // "") eq "base64" or grep {exists $value->{$_};} "inline_data", "inlineData", "b64_json" 2 996 998
Condition75%6/8
ref $value->{"source"} eq "HASH" and ($value->{"source"}{"type"} // "") eq "base64" or grep {exists $value->{$_};} "inline_data", "inlineData", "b64_json"
l!l&&r!l&&!r
11996
ref $value->{"source"} eq "HASH" and ($value->{"source"}{"type"} // "") eq "base64"
!ll&&!rl&&r
99701
$value->{"source"}{"type"} // ""
l!l
10
MC/DC66%2/3
ref $value->{"source"} eq "HASH" and ($value->{"source"}{"type"} // "") eq "base64" or grep {exists $value->{$_};} "inline_data", "inlineData", "b64_json"
ref $value->{"source"} eq "HASH" $value->{"source"}{"type"} grep {exists $value->{$_};} "inline_data", "inlineData", "b64_json"
Truth table60%3/5
A and B or C
A= ref $value->{"source"} eq "HASH"B= $value->{"source"}{"type"}C= grep {exists $value->{$_};} "inline_data", "inlineData", "b64_json"
ABCresult
11X1
0X11
1011
0X00
1000
515 996 return { map { $_ => $self->_any( $value->{$_}, $where.'.'.$_, $strict ) } keys %$value };
516 }
517 1372 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 38 1334 1372
518 38 return [ map { $self->_any( $value->[$_], $where.'['.$_.']', $strict ) } 0 .. $#$value ];
519 }
520 1334 ▶ croak __PACKAGE__.' '.$where.' holds a '.ref($value).' reference' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 1334 1334
521 1334 $self->_check_text( $value, $where );
522 1330 return $value;
523 }
524  
525 13717 ▶sub _check_text {
Subroutine _check_text: called CC=4 SCAR=13.9
526 13717 my ( $self, $text, $where ) = @_;
527 13717 ▶ croak __PACKAGE__.' '.$where.' holds a data: URL; images go into a report only as { image, sha256, bytes }'
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$DATA_URL/ 4 13713 13717
528 if $text =~ $DATA_URL;
529 13713 ▶ croak __PACKAGE__.' '.$where.' holds base64 content; images go into a report only as { image, sha256, bytes }'
Branch: 1 branch
BranchTrueFalseTotal
if length $text >= 1024 and $text =~ /$BASE64_RUN/ 1 13712 13713
Condition100%3/3
length $text >= 1024 and $text =~ /$BASE64_RUN/
!ll&&!rl&&r
1371021
MC/DC100%2/2
length $text >= 1024 and $text =~ /$BASE64_RUN/
length $text >= 1024 $text =~ /$BASE64_RUN/
Truth table100%3/3
A and B
A= length $text >= 1024B= $text =~ /$BASE64_RUN/
ABresult
0X0
100
111
530 if length $text >= 1024 && $text =~ $BASE64_RUN;
531 13712 return;
532 }
533  
534 103 ▶sub _clean_url {
Subroutine _clean_url: called CC=8 SCAR=20.9
535 103 my ( $self, $text ) = @_;
536 103 my $uri = URI->new($text);
537 103 ▶ return $text unless $uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 0 103 103
538 103 $uri->fragment(undef);
539 103 ▶ $uri->userinfo(undef) if $uri->can('userinfo');
Branch: 1 branch
BranchTrueFalseTotal
if $uri->can("userinfo") 103 0 103
540 103 ▶ if ( $uri->can('query_form') && defined $uri->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("query_form") and defined $uri->query) 24 79 103
Condition66%2/3
$uri->can("query_form") and defined $uri->query
!ll&&!rl&&r
07924
MC/DC50%1/2
$uri->can("query_form") and defined $uri->query
$uri->can("query_form") defined $uri->query
Truth table66%2/3
A and B
A= $uri->can("query_form")B= defined $uri->query
ABresult
0X0
100
111
541 24 my @q = $uri->query_form;
542 24 my @keep;
543 24 ▶ while ( my ( $k, $v ) = splice @q, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@q, 0, 2)) 37 24 61
544 37 ▶ push @keep, $k, $v unless $k =~ $SECRET_PARAM;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /$SECRET_PARAM/ 22 15 37
545 }
546 24 $uri->query_form( \@keep );
547 24 ▶ $uri->query(undef) unless @keep;
Branch: 1 branch
BranchTrueFalseTotal
unless @keep 2 22 24
548 }
549 103 return $uri->as_string;
550 }
551  
552 #### cai-platform
553  
554 5 ▶sub from_cai_platform {
Subroutine from_cai_platform: called CC=32 SCAR=36.2
Pod documented
555 5 my ( $class, $data, %arg ) = @_;
556 croak __PACKAGE__.'->from_cai_platform needs the decoded --report hash'
557 5 ▶ unless ref $data eq 'HASH' && ref $data->{runs} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and ref $data->{"runs"} eq "ARRAY" 1 4 5
Condition66%2/3
ref $data eq "HASH" and ref $data->{"runs"} eq "ARRAY"
!ll&&!rl&&r
014
MC/DC50%1/2
ref $data eq "HASH" and ref $data->{"runs"} eq "ARRAY"
ref $data eq "HASH" ref $data->{"runs"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= ref $data->{"runs"} eq "ARRAY"
ABresult
0X0
100
111
558 4 ▶ my $grading = $arg{grading} || {};
Condition100%2/2
$arg{'grading'} || {}
l!l
13
MC/DC100%1/1
$arg{'grading'} || {}
$arg{'grading'}
Truth table100%2/2
A
A= $arg{'grading'}
Aresult
11
00
559 4 ▶ my $suites = $arg{suites} || {};
Condition100%2/2
$arg{'suites'} || {}
l!l
13
MC/DC100%1/1
$arg{'suites'} || {}
$arg{'suites'}
Truth table100%2/2
A
A= $arg{'suites'}
Aresult
11
00
560 4 ▶ my %model = map { $_ => 1 } @{ $data->{models} || [] };
Condition50%1/2
$data->{'models'} || []
l!l
40
MC/DC0%0/1
$data->{'models'} || []
$data->{'models'}
Truth table50%1/2
A
A= $data->{'models'}
Aresult
11
00
561 4 my ( %group, @order );
562 4 for my $row ( @{ $data->{runs} } ) {
563 8 ▶ my $key = join "\0", map { defined $_ ? $_ : '' } $row->{target}, $row->{suite_id};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 16 0 16
564 8 ▶ push @order, $key unless $group{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $group{$key} 4 4 8
565 8 push @{ $group{$key} }, $row;
566 }
567 4 my %run_status = ( done => 'complete', cancelled => 'cancelled', failed => 'aborted' );
568 4 my @runs;
569 4 for my $key ( @order ) {
570 4 my @rows = @{ $group{$key} };
571 4 my $first = $rows[0];
572 4 my $sid = $first->{suite_id};
573 4 ▶ my $meta = $suites->{$sid} || {};
Condition100%2/2
$suites->{$sid} || {}
l!l
13
MC/DC100%1/1
$suites->{$sid} || {}
$suites->{$sid}
Truth table100%2/2
A
A= $suites->{$sid}
Aresult
11
00
574 4 ▶ my $repeat = ( $data->{repeat_by_suite} || {} )->{$sid};
Condition50%1/2
$data->{'repeat_by_suite'} || {}
l!l
40
MC/DC0%0/1
$data->{'repeat_by_suite'} || {}
$data->{'repeat_by_suite'}
Truth table50%1/2
A
A= $data->{'repeat_by_suite'}
Aresult
11
00
575 4 ▶ $repeat = @rows unless defined $repeat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $repeat 0 4 4
576 4 ▶ croak __PACKAGE__.'->from_cai_platform: '.scalar(@rows).' runs of '.$first->{target}.'/'.$sid.' for repeat '.$repeat
Branch: 1 branch
BranchTrueFalseTotal
if @rows > $repeat 1 3 4
577 if @rows > $repeat;
578 3 my $status = 'complete';
579 3 my $error;
580 3 my ( %case, @case_order );
581 3 for my $i ( 0 .. $#rows ) {
582 5 my $row = $rows[$i];
583 my $s = $run_status{ $row->{status} // '' }
584 5 ▶ or croak __PACKAGE__.'->from_cai_platform: unknown run status '.( $row->{status} // '(none)' );
Branch: 1 branch
BranchTrueFalseTotal
my $s = $run_status{$row->{"status"} // ""} or croak('Langertha::Holmgang::Report->from_cai_platform: unknown run status ' . ($row->{"status"} // "(none)")) 4 1 5
Condition50%2/4
$row->{"status"} // ""
l!l
50
$row->{"status"} // "(none)"
l!l
10
MC/DC0%0/1
$row->{"status"} // ""
$row->{"status"}
MC/DC0%0/1
$row->{"status"} // "(none)"
$row->{"status"}
Truth table50%1/2
A
A= $row->{"status"}
Aresult
11
00
Truth table50%1/2
A
A= $row->{"status"}
Aresult
11
00
585 4 $status = $class->_worse_status( $status, $s );
586 4 ▶ $error = $row->{error} unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 4 0 4
587 4 ▶ for my $c ( @{ $row->{cases} || [] } ) {
Condition50%1/2
$row->{'cases'} || []
l!l
40
MC/DC0%0/1
$row->{'cases'} || []
$row->{'cases'}
Truth table50%1/2
A
A= $row->{'cases'}
Aresult
11
00
588 7 my $id = $c->{case_id};
589 7 ▶ unless ( $case{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($case{$id}) 5 2 7
590 5 ▶ my $cm = ( $meta->{cases} || {} )->{$id} || {};
Condition100%4/4
+($meta->{'cases'} || {})->{$id} || {}
l!l
23
$meta->{'cases'} || {}
l!l
23
MC/DC100%1/1
+($meta->{'cases'} || {})->{$id} || {}
$meta->{'cases'}
Truth table100%2/2
A
A= $meta->{'cases'}
Aresult
11
00
591 $case{$id} = {
592 case_id => $id,
593 title => $c->{title},
594 difficulty => defined $c->{difficulty} ? $c->{difficulty} : $cm->{difficulty},
595 25 ▶ ( map { $_ => $cm->{$_} } qw( tags tier weight hard untrusted ) ),
Branch: 1 branch
BranchTrueFalseTotal
defined($c->{'scenario'} // $cm->{'scenario'}) ? : 0 5 5
Condition16%1/6
$c->{"scenario"} // $cm->{"scenario"}
l!l&&r!l&&!r
005
$c->{"scenario"} // $cm->{"scenario"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"scenario"} // $cm->{"scenario"}
$c->{"scenario"} $cm->{"scenario"}
MC/DC0%0/2
$c->{"scenario"} // $cm->{"scenario"}
$c->{"scenario"} $cm->{"scenario"}
Truth table33%1/3
A // B
A= $c->{"scenario"}B= $cm->{"scenario"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $c->{"scenario"}B= $cm->{"scenario"}
ABresult
1X1
011
000
596 5 ▶ ( defined( $c->{scenario} // $cm->{scenario} ) ? ( scenario => $c->{scenario} // $cm->{scenario} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $c->{'difficulty'} ? : 3 2 5
597 repetitions => [],
598 };
599 5 push @case_order, $id;
600 }
601 7 ▶ my $g = defined $row->{id} ? ( $grading->{ $row->{id} } || {} )->{$id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{'id'} ? : 7 0 7
Condition100%2/2
$$grading{$row->{'id'}} || {}
l!l
25
MC/DC100%1/1
$$grading{$row->{'id'}} || {}
$$grading{$row->{'id'}}
Truth table100%2/2
A
A= $$grading{$row->{'id'}}
Aresult
11
00
602 7 push @{ $case{$id}{repetitions} }, $class->_cai_repetition( $c, $g, $i + 1, $repeat );
603 }
604 }
605 push @runs, {
606 target => {
607 label => $first->{target},
608 engine => undef,
609 model => $model{ $first->{target} // '' } ? $first->{target} : undef,
610 url => $data->{base_url},
611 },
612 variant => { name => undef, settings => undef },
613 suite => { id => $sid, version => $meta->{version}, topic => $first->{topic}, digest => $meta->{digest} },
614 repeat => $repeat,
615 status => $status,
616 error => $error,
617 1 ▶ cases => [ map { $case{$_} } @case_order ],
Branch: 1 branch
BranchTrueFalseTotal
$model{$first->{'target'} // ''} ? : 1 0 1
Condition50%1/2
$first->{"target"} // ""
l!l
10
MC/DC0%0/1
$first->{"target"} // ""
$first->{"target"}
Truth table50%1/2
A
A= $first->{"target"}
Aresult
11
00
618 };
619 }
620 1 return $class->new( started_at => undef, runs => \@runs );
621 }
622  
623  
624 7 ▶sub _cai_repetition {
Subroutine _cai_repetition: called CC=25 SCAR=33.6
625 7 my ( $self, $c, $g, $n, $repeat ) = @_;
626 7 ▶ my $exceeded = $c->{exceeded} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c->{'exceeded'} ? : 3 4 7
627 7 ▶ my $st = $c->{status} // '';
Condition50%1/2
$c->{"status"} // ""
l!l
70
MC/DC0%0/1
$c->{"status"} // ""
$c->{"status"}
Truth table50%1/2
A
A= $c->{"status"}
Aresult
11
00
628 my $status = ( $exceeded && ( $st eq 'failed' || $st eq 'ungraded' ) ) ? 'turn_limit'
629 : $st eq 'ungraded' ? 'uncertain'
630 12 ▶ : ( grep { $_ eq $st } qw( passed failed error context_exceeded ) ) ? $st
Branch: 1 branch
BranchTrueFalseTotal
length $st ? : 1 0 1
631 7 ▶ : croak __PACKAGE__.'->from_cai_platform: unknown case status '.( length $st ? $st : '(none)' ).' for '.$c->{case_id};
Branch: 3 branches
BranchTrueFalseTotal
$exceeded && ($st eq 'failed' || $st eq 'ungraded') ? : 3 4 7
$st eq 'ungraded' ? : 1 3 4
grep({$_ eq $st;} 'passed', 'failed', 'error', 'context_exceeded') ? : 2 1 3
Condition66%4/6
$exceeded and $st eq "failed" || $st eq "ungraded"
!ll&&!rl&&r
403
$st eq "failed" or $st eq "ungraded"
l!l&&r!l&&!r
120
MC/DC33%1/3
$exceeded and $st eq "failed" || $st eq "ungraded"
$exceeded $st eq "failed" $st eq "ungraded"
Truth table75%3/4
A and B or C
A= $exceededB= $st eq "failed"C= $st eq "ungraded"
ABCresult
0XX0
1000
11X1
1011
632 6 my ( $rules, $diff, $score );
633 6 ▶ my @calls = map { +{ tool => undef, args => undef, succeeded => undef, sim_gap => undef } } 1 .. ( $c->{tool_calls} || 0 );
Condition50%1/2
$c->{'tool_calls'} || 0
l!l
60
MC/DC0%0/1
$c->{'tool_calls'} || 0
$c->{'tool_calls'}
Truth table50%1/2
A
A= $c->{'tool_calls'}
Aresult
11
00
634 6 my $gaps;
635 6 ▶ if ( ref $g eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $g eq 'HASH') { } 1 5 6
636 $rules = [ map { +{
637 title => $_->{title},
638 kind => $_->{kind},
639 required => $_->{required} ? 1 : 0,
640 passed => $_->{passed} ? 1 : 0,
641 reason => $_->{detail},
642 sim_gap => $_->{sim_gap},
643 1 ▶ } } grep { ( $_->{kind} // '' ) ne 'turns' } @{ $g->{rules} || [] } ];
Branch: 2 branches
BranchTrueFalseTotal
$_->{'required'} ? : 2 0 2
$_->{'passed'} ? : 1 1 2
Condition50%2/4
$_->{"kind"} // ""
l!l
30
$g->{'rules'} || []
l!l
10
MC/DC0%0/1
$_->{"kind"} // ""
$_->{"kind"}
MC/DC0%0/1
$g->{'rules'} || []
$g->{'rules'}
Truth table50%1/2
A
A= $_->{"kind"}
Aresult
11
00
Truth table50%1/2
A
A= $g->{'rules'}
Aresult
11
00
644 1 my $passed = grep { $_->{passed} } @$rules;
645 1 ▶ $score = @$rules ? 0 + sprintf( '%.4f', $passed / @$rules ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$rules ? : 1 0 1
646 1 ▶ $diff = [ map { +{ path => $_->{path}, before => $_->{before}, after => $_->{after} } } @{ $g->{diff} || [] } ];
Condition50%1/2
$g->{'diff'} || []
l!l
10
MC/DC0%0/1
$g->{'diff'} || []
$g->{'diff'}
Truth table50%1/2
A
A= $g->{'diff'}
Aresult
11
00
647 1 ▶ $gaps = [ map { +{ call => $_->{index}, tool => $_->{tool}, gap => $_->{gap} } } @{ $g->{sim_gaps} || [] } ];
Condition50%1/2
$g->{'sim_gaps'} || []
l!l
10
MC/DC0%0/1
$g->{'sim_gaps'} || []
$g->{'sim_gaps'}
Truth table50%1/2
A
A= $g->{'sim_gaps'}
Aresult
11
00
648 1 for my $gap ( @$gaps ) {
649 1 ▶ my $call = defined $gap->{call} ? $calls[ $gap->{call} ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $gap->{'call'} ? : 1 0 1
650 1 ▶ next unless $call;
Branch: 1 branch
BranchTrueFalseTotal
unless $call 0 1 1
651 1 $call->{tool} = $gap->{tool};
652 1 $call->{sim_gap} = $gap->{gap};
653 }
654 }
655 else {
656 5 ▶ $score = $exceeded ? undef : $c->{score};
Branch: 1 branch
BranchTrueFalseTotal
$exceeded ? : 2 3 5
657 5 ▶ $gaps = [ map { +{ call => undef, tool => undef, gap => $_ } } @{ $c->{sim_gaps} || [] } ];
Condition50%1/2
$c->{'sim_gaps'} || []
l!l
50
MC/DC0%0/1
$c->{'sim_gaps'} || []
$c->{'sim_gaps'}
Truth table50%1/2
A
A= $c->{'sim_gaps'}
Aresult
11
00
658 }
659 6 ▶ my $tok = sub { my ( $v ) = @_; return $v ? $v : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 12 6 18
660 return {
661 repetition => $n,
662 seed => $repeat > 1 ? $n : undef,
663 status => $status,
664 score => $score,
665 error => undef,
666 finish_reason => undef,
667 duration_ms => $c->{duration_ms},
668 rules => $rules,
669 diff => $diff,
670 14 ▶ turns => [ map { +{ map { $_ => undef } qw( input_tokens output_tokens reasoning_tokens duration_ms finish_reason calls ) } }
Condition50%1/2
$c->{'turns'} || 0
l!l
60
MC/DC0%0/1
$c->{'turns'} || 0
$c->{'turns'}
Truth table50%1/2
A
A= $c->{'turns'}
Aresult
11
00
671 1 .. ( $c->{turns} || 0 ) ],
672 tokens => {
673 input => $tok->( $c->{prompt_tokens} ),
674 output => $tok->( $c->{completion_tokens} ),
675 reasoning => undef,
676 input_max => $tok->( $c->{prompt_tokens_max} ),
677 },
678 calls => \@calls,
679 sim_gaps => $gaps,
680 forbidden_calls => $c->{forbidden_calls},
681 injection_held => $c->{injection_held},
682 6 ▶ transcript => undef,
Branch: 1 branch
BranchTrueFalseTotal
$repeat > 1 ? : 6 0 6
683 };
684 }
685  
686 29 __PACKAGE__->meta->make_immutable;
687  
688  
689 29 1;
690  
691 __END__
« blib/lib/Langertha/Holmgang/Batch.pm ↑ Summary blib/lib/Langertha/Holmgang/CLI.pm »