| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 130 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 130 | ▶ | $text = decode_utf8_lossy( <$fh> // '' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
readline $fh // ""
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 130 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 420 | ▶ | return js_ordered() if $!{ENOENT}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
js_type($entry) eq "object" and exists $entry->{"via"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
js_type($via) eq "array" and not grep({js_type($_) ne "string";} @$via)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 124 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 130 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
builtin::blessed($err) and $err->can("message")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$e->{"files"} // {}
$e->{"files"}
Truth table50%1/2
A
A= $e->{"files"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 79 | ▶ | $entry->{block} = JSON::MaybeXS::true() if $e->{block}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 79 | my $via = $e->{via}; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 79 | ▶ | if ( ref $via eq 'ARRAY' ? @$via : ( defined $via && !ref $via && length $via ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
defined $via and not ref $via and length $via
defined $via and not ref $via
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $entry->{via} = ref $via eq 'ARRAY' ? [@$via] : [ split //, $via ]; # [...entry.via] | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 79 | ▶ | $entry->{partial} = JSON::MaybeXS::true() if $e->{partial}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $bytes = encode_text($path); | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | unlink $bytes or $!{ENOENT} or die 'unlink '.$path.': '.$!."\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
unlink $bytes or $!{"ENOENT"}
MC/DC0%0/2
unlink $bytes or $!{"ENOENT"}
unlink $bytes
$!{"ENOENT"}
Truth table0%0/3
A or B
A= unlink $bytesB= $!{"ENOENT"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
$colon != -1 and $slash == -1 || $colon < $slash
$slash == -1 or $colon < $slash
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 //= ""
MC/DC0%0/1
$dir //= ""
$dir
Truth table50%1/2
A
A= $dir
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 317 | ▶ | return { harness => $harness, target => $target, type => $TYPE_OF_DIR{$dir} // 'rule', name => join( '/', @rest ) }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$TYPE_OF_DIR{$dir} // "rule"
MC/DC0%0/1
$TYPE_OF_DIR{$dir} // "rule"
$TYPE_OF_DIR{$dir}
Truth table50%1/2
A
A= $TYPE_OF_DIR{$dir}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||