Skilletor/Lock.pm

filter: Statementstmt 93.6% 88 / 94 Branchbran 58.3% 21 / 36 Conditioncond 43.7% 14 / 32 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 73.3% 146 / 199 CC 33 SCAR 24.4
CC
33
Cov
76%
read_lock
17.8
serialize_lock
15.9
write_lock
9.3
CRAP 11.5
« Skilletor/FSUtil.pm ↑ Summary Skilletor/Gzip.pm »
1 package Skilletor::Lock;
2 # ABSTRACT: A scope's lock file (skilletor.lock.json) – reading, writing, its keys
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Skilletor::Error::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text node_fs_error write_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Skilletor::JSON qw( js_parse js_ordered js_sort_keys js_stringify js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 our $VERSION = '0.001';
16  
17 34 our @EXPORT_OK = qw( read_lock serialize_lock write_lock parse_lock_key key_to_type_name );
18  
19 # The lock key prefix of each harness; Claude's keys carry none.
20 34 my %HARNESS_OF_PREFIX = ( 'codex:' => 'codex', 'hermes:' => 'hermes' );
21 34 my %TYPE_OF_DIR = ( skills => 'skill', agents => 'agent', rules => 'rule' );
22  
23 # The lock at $path as an ordered hash of entries; a missing file is an empty lock.
24 550 ▶sub read_lock {
Subroutine read_lock: called CC=15 SCAR=28.8
Pod documented
25 550 my ( $path ) = @_;
26 550 my $bytes = encode_text($path);
27 550 my $text;
28 550 ▶ if ( -d $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $bytes) 0 550 550
29 0 Skilletor::Error::Lock->throw( $path.': cannot read lock (EISDIR: illegal operation on a directory, read)' );
30 }
31 550 ▶ if ( open my $fh, '<:raw', $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:raw', $bytes) { } 130 420 550
32 130 local $/;
33 130 ▶ $text = decode_utf8_lossy( <$fh> // '' );
Condition50%1/2
readline $fh // ""
l!l
1300
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
34 130 close $fh;
35 }
36 else {
37 420 ▶ return js_ordered() if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 420 0 420
38 0 Skilletor::Error::Lock->throw( $path.': cannot read lock ('.node_fs_error( 0 + $!, 'open', $path ).')' );
39 }
40 130 my $value;
41 130 my $ok = eval {
42 130 $value = js_parse($text);
43 130 ▶ die { message => 'expected an object' } unless js_type($value) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($value) eq "object" 0 130 130
44 # k145: `via` is matched against bundle labels exactly; anything but an array of strings is refused.
45 130 for my $key ( keys %$value ) {
46 171 my $entry = $value->{$key};
47 171 ▶ next unless js_type($entry) eq 'object' && exists $entry->{via};
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($entry) eq "object" and exists $entry->{"via"} 164 7 171
Condition66%2/3
js_type($entry) eq "object" and exists $entry->{"via"}
!ll&&!rl&&r
01647
MC/DC50%1/2
js_type($entry) eq "object" and exists $entry->{"via"}
js_type($entry) eq "object" exists $entry->{"via"}
Truth table66%2/3
A and B
A= js_type($entry) eq "object"B= exists $entry->{"via"}
ABresult
0X0
100
111
48 7 my $via = $entry->{via};
49 die { message => $key.': "via" must be an array of strings' }
50 7 ▶ unless js_type($via) eq 'array' && !grep { js_type($_) ne 'string' } @$via;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($via) eq "array" and not grep({js_type($_) ne "string";} @$via) 6 1 7
Condition100%3/3
js_type($via) eq "array" and not grep({js_type($_) ne "string";} @$via)
!ll&&!rl&&r
511
MC/DC100%2/2
js_type($via) eq "array" and not grep({js_type($_) ne "string";} @$via)
js_type($via) eq "array" not grep({js_type($_) ne "string";} @$via)
Truth table100%3/3
A and not(B)
A= js_type($via) eq "array"B= not grep({js_type($_) ne "string";} @$via)
ABresult
0X0
100
111
51 }
52 124 1;
53 };
54 130 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 6 124 130
55 6 my $err = $@;
56 6 ▶ my $msg = ref $err eq 'HASH' ? $err->{message} : blessed $err && $err->can('message') ? $err->message : "$err";
Branch: 2 branches
BranchTrueFalseTotal
ref $err eq 'HASH' ? : 6 0 6
builtin::blessed($err) && $err->can('message') ? : 0 0 0
Condition0%0/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table0%0/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
57 6 Skilletor::Error::Lock->throw( $path.': invalid lock JSON ('.$msg.')' );
58 }
59 124 return $value;
60 }
61  
62 # The lock's bytes: entries and files sorted, each entry's fields in the reference's order.
63 74 ▶sub serialize_lock {
Subroutine serialize_lock: called CC=11 SCAR=27.6
Pod documented
64 74 my ( $lock ) = @_;
65 74 my $out = js_ordered();
66 74 for my $key ( js_sort_keys( keys %$lock ) ) {
67 79 my $e = $lock->{$key};
68 79 ▶ my $files = $e->{files} // {};
Condition50%1/2
$e->{"files"} // {}
l!l
790
MC/DC0%0/1
$e->{"files"} // {}
$e->{"files"}
Truth table50%1/2
A
A= $e->{"files"}
Aresult
11
00
69 my $entry = js_ordered(
70 source => $e->{source},
71 version => $e->{version},
72 79 files => js_ordered( map { ( $_ => $files->{$_} ) } js_sort_keys( keys %$files ) ),
73 );
74 79 ▶ $entry->{skipped} = $e->{skipped} if $e->{skipped};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"skipped"} 0 79 79
75 79 ▶ $entry->{block} = JSON::MaybeXS::true() if $e->{block};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"block"} 0 79 79
76 79 my $via = $e->{via};
77 79 ▶ if ( ref $via eq 'ARRAY' ? @$via : ( defined $via && !ref $via && length $via ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $via eq "ARRAY" ? @$via : defined $via && !ref($via) && length $via) 0 79 79
ref $via eq 'ARRAY' ? : 0 79 79
Condition33%2/6
defined $via and not ref $via and length $via
!ll&&!rl&&r
7900
defined $via and not ref $via
!ll&&!rl&&r
7900
MC/DC0%0/3
defined $via and not ref $via and length $via
defined $via not ref $via length $via
Truth table25%1/4
A and not(B) and C
A= defined $viaB= not ref $viaC= length $via
ABCresult
0XX0
10X0
1100
1111
78 0 ▶ $entry->{via} = ref $via eq 'ARRAY' ? [@$via] : [ split //, $via ]; # [...entry.via]
Branch: 1 branch
BranchTrueFalseTotal
ref $via eq 'ARRAY' ? : 0 0 0
79 }
80 79 ▶ $entry->{partial} = JSON::MaybeXS::true() if $e->{partial};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"partial"} 9 70 79
81 79 $out->{$key} = $entry;
82 }
83 74 return js_stringify( $out, 2 )."\n";
84 }
85  
86 # Write the lock; one without entries deletes the file instead, never `{}`.
87 24 ▶sub write_lock {
Subroutine write_lock: called CC=4 SCAR=22.3
Pod documented
88 24 my ( $path, $lock ) = @_;
89 24 ▶ unless (%$lock) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$lock) 0 24 24
90 0 my $bytes = encode_text($path);
91 0 ▶ unlink $bytes or $!{ENOENT} or die 'unlink '.$path.': '.$!."\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $bytes or $!{"ENOENT"} or die 'unlink ' . $path . ': ' . $! . "\n" 0 0 0
Condition0%0/3
unlink $bytes or $!{"ENOENT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
unlink $bytes or $!{"ENOENT"}
unlink $bytes $!{"ENOENT"}
Truth table0%0/3
A or B
A= unlink $bytesB= $!{"ENOENT"}
ABresult
1X1
011
000
92 0 return;
93 }
94 24 write_text( $path, serialize_lock($lock) );
95 }
96  
97 # A lock key – `skills/foo`, `codex:agents/x` – as { harness, target, type, name }. A prefix no
98 # harness has leaves the harness undefined; a directory no type has reads as a rule.
99 317 ▶sub parse_lock_key {
Subroutine parse_lock_key: called CC=6 SCAR=18.5
Pod documented
100 317 my ( $key ) = @_;
101 317 my $colon = index $key, ':';
102 317 my $slash = index $key, '/';
103 317 my $harness = 'claude';
104 317 my $target = $key;
105 317 ▶ if ( $colon != -1 && ( $slash == -1 || $colon < $slash ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colon != -1 and $slash == -1 || $colon < $slash) 42 275 317
Condition50%3/6
$colon != -1 and $slash == -1 || $colon < $slash
!ll&&!rl&&r
275042
$slash == -1 or $colon < $slash
l!l&&r!l&&!r
0420
MC/DC33%1/3
$colon != -1 and $slash == -1 || $colon < $slash
$colon != -1 $slash == -1 $colon < $slash
Truth table50%2/4
A and B or C
A= $colon != -1B= $slash == -1C= $colon < $slash
ABCresult
0XX0
1000
11X1
1011
106 42 $harness = $HARNESS_OF_PREFIX{ substr $key, 0, $colon + 1 };
107 42 $target = substr $key, $colon + 1;
108 }
109 317 my ( $dir, @rest ) = split m{/}, $target, -1;
110 317 ▶ $dir //= '';
Condition50%1/2
$dir //= ""
l!l
3170
MC/DC0%0/1
$dir //= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
111 317 ▶ return { harness => $harness, target => $target, type => $TYPE_OF_DIR{$dir} // 'rule', name => join( '/', @rest ) };
Condition50%1/2
$TYPE_OF_DIR{$dir} // "rule"
l!l
3170
MC/DC0%0/1
$TYPE_OF_DIR{$dir} // "rule"
$TYPE_OF_DIR{$dir}
Truth table50%1/2
A
A= $TYPE_OF_DIR{$dir}
Aresult
11
00
112 }
113  
114 15 ▶sub key_to_type_name {
Subroutine key_to_type_name: called CC=1 SCAR=0.0
Pod documented
115 15 my ( $key ) = @_;
116 15 my $k = parse_lock_key($key);
117 15 return { type => $k->{type}, name => $k->{name} };
118 }
119  
120 34 1;
121  
122 __END__
« Skilletor/FSUtil.pm ↑ Summary Skilletor/Gzip.pm »