lib/Skilletor/Lock.pm

filter: Statementstmt 97.9% 97 / 99 Branchbran 80.9% 34 / 42 Conditioncond 50.0% 16 / 32 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 81.4% 171 / 210 CC 36 SCAR 24.2
CC
36
Cov
85%
read_lock
19.9
serialize_lock
11.6
parse_lock_key
6.4
CRAP 11.2
« lib/Skilletor/Briefing.pm ↑ Summary lib/Skilletor/Gzip.pm »
1 package Skilletor::Lock;
2 # ABSTRACT: A scope's lock file (skilletor.lock.json) – reading, writing, its keys
3  
4 467 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 467 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 467 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 467 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 467 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 467 ▶use Skilletor::Error::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 467 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 467 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text node_fs_error write_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 467 ▶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 467 our $VERSION = '0.008000';
16  
17 467 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 467 my %HARNESS_OF_PREFIX = ( 'codex:' => 'codex', 'hermes:' => 'hermes' );
21 467 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 34824 ▶sub read_lock {
Subroutine read_lock: called CC=18 SCAR=29.9
Pod documented
25 34824 my ( $path ) = @_;
26 34824 my $bytes = encode_text($path);
27 34824 my $text;
28 34824 ▶ if ( -d $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $bytes) 0 34824 34824
29 0 Skilletor::Error::Lock->throw( $path.': cannot read lock (EISDIR: illegal operation on a directory, read)' );
30 }
31 34824 ▶ if ( open my $fh, '<:raw', $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:raw', $bytes) { } 18528 16296 34824
32 18528 local $/;
33 18528 ▶ $text = decode_utf8_lossy( <$fh> // '' );
Condition50%1/2
readline $fh // ""
l!l
185280
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
34 18528 close $fh;
35 }
36 else {
37 16296 ▶ return js_ordered() if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 16296 0 16296
38 0 Skilletor::Error::Lock->throw( $path.': cannot read lock ('.node_fs_error( 0 + $!, 'open', $path ).')' );
39 }
40 18528 my $value;
41 18528 my $ok = eval {
42 18528 $value = js_parse($text);
43 18528 ▶ die { message => 'expected an object' } unless js_type($value) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($value) eq "object" 0 18528 18528
44 # Entries are records, but their fields stay lenient for historical and future lock formats.
45 # k145: `via` is matched against bundle labels exactly; anything but an array of strings is refused.
46 # `files` is the ownership boundary, though: every reader and serializer needs its complete,
47 # string-valued map. Refuse corruption rather than reinterpreting or dropping ownership.
48 18528 for my $key ( keys %$value ) {
49 25939 my $entry = $value->{$key};
50 25939 ▶ die { message => $key.': expected an object' } unless js_type($entry) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($entry) eq "object" 7 25932 25939
51 25932 ▶ if ( exists $entry->{via} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"via"}) 698 25234 25932
52 698 my $via = $entry->{via};
53 die { message => $key.': "via" must be an array of strings' }
54 698 ▶ 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) 7 691 698
Condition100%3/3
js_type($via) eq "array" and not grep({js_type($_) ne "string";} @$via)
!ll&&!rl&&r
61691
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
55 }
56 die { message => $key.': "files" must be an object' }
57 25925 ▶ unless exists $entry->{files} && js_type( $entry->{files} ) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{"files"} and js_type($entry->{"files"}) eq "object" 6 25919 25925
Condition100%3/3
exists $entry->{"files"} and js_type($entry->{"files"}) eq "object"
!ll&&!rl&&r
1525919
MC/DC100%2/2
exists $entry->{"files"} and js_type($entry->{"files"}) eq "object"
exists $entry->{"files"} js_type($entry->{"files"}) eq "object"
Truth table100%3/3
A and B
A= exists $entry->{"files"}B= js_type($entry->{"files"}) eq "object"
ABresult
0X0
100
111
58 die { message => $key.': "files" values must be strings' }
59 25919 ▶ if grep { js_type($_) ne 'string' } values %{ $entry->{files} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {js_type($_) ne "string";} values %{$$entry{"files"};} 6 25913 25919
60 }
61 18502 1;
62 };
63 18528 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 26 18502 18528
64 26 my $err = $@;
65 26 ▶ my $msg = ref $err eq 'HASH' ? $err->{message} : blessed $err && $err->can('message') ? $err->message : "$err";
Branch: 2 branches
BranchTrueFalseTotal
ref $err eq 'HASH' ? : 26 0 26
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
66 26 Skilletor::Error::Lock->throw( $path.': invalid lock JSON ('.$msg.')' );
67 }
68 18502 return $value;
69 }
70  
71 # The lock's bytes: entries and files sorted, each entry's fields in the reference's order.
72 11869 ▶sub serialize_lock {
Subroutine serialize_lock: called CC=11 SCAR=24.5
Pod documented
73 11869 my ( $lock ) = @_;
74 11869 my $out = js_ordered();
75 11869 for my $key ( js_sort_keys( keys %$lock ) ) {
76 10920 my $e = $lock->{$key};
77 10920 ▶ my $files = $e->{files} // {};
Condition50%1/2
$e->{"files"} // {}
l!l
109200
MC/DC0%0/1
$e->{"files"} // {}
$e->{"files"}
Truth table50%1/2
A
A= $e->{"files"}
Aresult
11
00
78 my $entry = js_ordered(
79 source => $e->{source},
80 version => $e->{version},
81 10920 files => js_ordered( map { ( $_ => $files->{$_} ) } js_sort_keys( keys %$files ) ),
82 );
83 10920 ▶ $entry->{skipped} = $e->{skipped} if $e->{skipped};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"skipped"} 82 10838 10920
84 10920 ▶ $entry->{block} = JSON::MaybeXS::true() if $e->{block};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"block"} 2345 8575 10920
85 10920 my $via = $e->{via};
86 10920 ▶ 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) 480 10440 10920
ref $via eq 'ARRAY' ? : 480 10440 10920
Condition33%2/6
defined $via and not ref $via and length $via
!ll&&!rl&&r
1044000
defined $via and not ref $via
!ll&&!rl&&r
1044000
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
87 480 ▶ $entry->{via} = ref $via eq 'ARRAY' ? [@$via] : [ split //, $via ]; # [...entry.via]
Branch: 1 branch
BranchTrueFalseTotal
ref $via eq 'ARRAY' ? : 480 0 480
88 }
89 10920 ▶ $entry->{partial} = JSON::MaybeXS::true() if $e->{partial};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"partial"} 136 10784 10920
90 10920 $out->{$key} = $entry;
91 }
92 11869 return js_stringify( $out, 2 )."\n";
93 }
94  
95 # Write the lock; one without entries deletes the file instead, never `{}`.
96 2458 ▶sub write_lock {
Subroutine write_lock: called CC=4 SCAR=14.3
Pod documented
97 2458 my ( $path, $lock ) = @_;
98 2458 ▶ unless (%$lock) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$lock) 43 2415 2458
99 43 my $bytes = encode_text($path);
100 43 ▶ unlink $bytes or $!{ENOENT} or die 'unlink '.$path.': '.$!."\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $bytes or $!{"ENOENT"} or die 'unlink ' . $path . ': ' . $! . "\n" 43 0 43
Condition33%1/3
unlink $bytes or $!{"ENOENT"}
l!l&&r!l&&!r
4300
MC/DC0%0/2
unlink $bytes or $!{"ENOENT"}
unlink $bytes $!{"ENOENT"}
Truth table33%1/3
A or B
A= unlink $bytesB= $!{"ENOENT"}
ABresult
1X1
011
000
101 43 return;
102 }
103 2415 write_text( $path, serialize_lock($lock) );
104 }
105  
106 # A lock key – `skills/foo`, `codex:agents/x` – as { harness, target, type, name }. A prefix no
107 # harness has leaves the harness undefined; a directory no type has reads as a rule.
108 68327 ▶sub parse_lock_key {
Subroutine parse_lock_key: called CC=6 SCAR=18.5
Pod documented
109 68327 my ( $key ) = @_;
110 68327 my $colon = index $key, ':';
111 68327 my $slash = index $key, '/';
112 68327 my $harness = 'claude';
113 68327 my $target = $key;
114 68327 ▶ if ( $colon != -1 && ( $slash == -1 || $colon < $slash ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colon != -1 and $slash == -1 || $colon < $slash) 33120 35207 68327
Condition50%3/6
$colon != -1 and $slash == -1 || $colon < $slash
!ll&&!rl&&r
35207033120
$slash == -1 or $colon < $slash
l!l&&r!l&&!r
0331200
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
115 33120 $harness = $HARNESS_OF_PREFIX{ substr $key, 0, $colon + 1 };
116 33120 $target = substr $key, $colon + 1;
117 }
118 68327 my ( $dir, @rest ) = split m{/}, $target, -1;
119 68327 ▶ $dir //= '';
Condition50%1/2
$dir //= ""
l!l
683270
MC/DC0%0/1
$dir //= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
120 68327 ▶ return { harness => $harness, target => $target, type => $TYPE_OF_DIR{$dir} // 'rule', name => join( '/', @rest ) };
Condition50%1/2
$TYPE_OF_DIR{$dir} // "rule"
l!l
683270
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
121 }
122  
123 2719 ▶sub key_to_type_name {
Subroutine key_to_type_name: called CC=1 SCAR=0.0
Pod documented
124 2719 my ( $key ) = @_;
125 2719 my $k = parse_lock_key($key);
126 2719 return { type => $k->{type}, name => $k->{name} };
127 }
128  
129 467 1;
130  
131 __END__
« lib/Skilletor/Briefing.pm ↑ Summary lib/Skilletor/Gzip.pm »