lib/Skilletor/State.pm

filter: Statementstmt 92.1% 352 / 382 Branchbran 74.7% 133 / 178 Conditioncond 64.0% 98 / 153 MC/DCmcdc 40.0% 34 / 85 Subroutinesub 93.9% 62 / 66 Podpod 95.4% 21 / 22 totaltotal 79.0% 700 / 886 CC 151 SCAR 24.0
CC
151
Cov
82%
_break_lock
53.6
_dead_owner
21.3
with_lock
20.5
CRAP 11.0
« lib/Skilletor/Gzip.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Module/Runtime.pm »
1 package Skilletor::State;
2 # ABSTRACT: Instance state under ~/.claude/skilletor – trust, the sync lock and the records a sync keeps
3  
4 413 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 413 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 413 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 413 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 413 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 413 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 413 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 413 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 413 ▶use Skilletor::Error::LockTimeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 413 ▶use Skilletor::Process ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 413 ▶use Skilletor::FSUtil qw( encode_text is_absolute path_join read_text real_or_resolved write_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 413 ▶use Skilletor::JSON qw( js_parse js_stringify js_ordered js_sort_keys js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 413 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 413 our $VERSION = '0.008000';
19  
20 413 has root => (
21 is => 'ro',
22 required => 1,
23 );
24  
25 # One run's view of the files (k250): a check, which only reads, or the holder of the sync lock,
26 # the one writer of the records a sync keeps, parses each file once.
27 413 has read_once => (
28 is => 'ro',
29 );
30  
31 # What a read_once state parsed, by file name. A write drops the file, so the next read parses
32 # what was written; with_lock empties it as it takes the lock and as it gives it up.
33 has _view => (
34 is => 'ro',
35 init_arg => undef,
36 27106 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 413 );
38  
39 27106 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
40 27106 my ( $self ) = @_;
41 27106 path( $self->root )->mkpath;
42 }
43  
44 1 ▶sub for_home {
Subroutine for_home: called CC=1 SCAR=0.0
Pod documented
45 1 my ( $class, $home ) = @_;
46 1 return $class->new( root => $class->default_root($home) );
47 }
48  
49 1740 ▶sub default_root {
Subroutine default_root: called CC=3 SCAR=11.0
Pod documented
50 1740 my ( $class, $home, $claude_config_dir ) = @_;
51 1740 ▶ return path_join( ( defined $claude_config_dir && length $claude_config_dir ) ? $claude_config_dir : path_join( $home, '.claude' ), 'skilletor' );
Branch: 1 branch
BranchTrueFalseTotal
defined $claude_config_dir && length $claude_config_dir ? : 189 1551 1740
Condition100%3/3
defined $claude_config_dir and length $claude_config_dir
!ll&&!rl&&r
1238313189
MC/DC100%2/2
defined $claude_config_dir and length $claude_config_dir
defined $claude_config_dir length $claude_config_dir
Truth table100%3/3
A and B
A= defined $claude_config_dirB= length $claude_config_dir
ABresult
0X0
100
111
52 }
53  
54 59682 ▶sub _file { path_join( $_[0]->root, $_[1] ) }
Subroutine _file: called CC=1 SCAR=0.0
55  
56 # A state file as an object; anything unreadable or no object reads as an empty one. A read_once
57 # state hands out the object it parsed first: what a caller changes of it, it writes.
58 54987 ▶sub _read_json {
Subroutine _read_json: called CC=5 SCAR=16.1
59 54987 my ( $self, $name ) = @_;
60 54987 my $view = $self->_view;
61 54987 ▶ return $view->{$name} if exists $view->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $view->{$name} 11426 43561 54987
62 43561 my $value = eval { js_parse( read_text( $self->_file($name) ) ) };
63 43561 ▶ $value = js_ordered() unless defined $value && js_type($value) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and js_type($value) eq "object" 31132 12429 43561
Condition100%3/3
defined $value and js_type($value) eq "object"
!ll&&!rl&&r
31129312429
MC/DC100%2/2
defined $value and js_type($value) eq "object"
defined $value js_type($value) eq "object"
Truth table100%3/3
A and B
A= defined $valueB= js_type($value) eq "object"
ABresult
0X0
100
111
64 43561 ▶ $view->{$name} = $value if $self->read_once;
Branch: 1 branch
BranchTrueFalseTotal
if $self->read_once 22272 21289 43561
65 43561 return $value;
66 }
67  
68 7435 ▶sub _write_json {
Subroutine _write_json: called CC=1 SCAR=0.0
69 7435 my ( $self, $name, $value ) = @_;
70 7435 delete $self->_view->{$name};
71 7435 write_text( $self->_file($name), js_stringify( $value, 2 )."\n" );
72 }
73  
74 #### Trust #############################################################
75  
76 212 ▶sub trust {
Subroutine trust: called CC=1 SCAR=0.0
Pod documented
77 212 my ( $self, $name, $backend ) = @_;
78 212 my $trust = $self->_read_json('trust.json');
79 212 $trust->{$name} = js_ordered( kind => $backend->{kind}, address => $backend->{address} );
80 212 $self->_write_json( 'trust.json', $trust );
81 }
82  
83 # A user-origin backend is trusted as is; a project-origin one needs an entry for exactly its
84 # kind and address. An entry written before k66 is the URL alone: it counts for a git or url
85 # backend at exactly that URL, never for a local one.
86 13365 ▶sub is_trusted {
Subroutine is_trusted: called CC=12 SCAR=25.4
Pod documented
87 13365 my ( $self, $name, $backend ) = @_;
88 13365 ▶ return 1 if ( $backend->{origin} // '' ) eq 'user';
Branch: 1 branch
BranchTrueFalseTotal
if ($backend->{"origin"} // "") eq "user" 12454 911 13365
Condition50%1/2
$backend->{"origin"} // ""
l!l
133650
MC/DC0%0/1
$backend->{"origin"} // ""
$backend->{"origin"}
Truth table50%1/2
A
A= $backend->{"origin"}
Aresult
11
00
89 911 my $trust = $self->_read_json('trust.json');
90 911 ▶ return 0 unless exists $trust->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $trust->{$name} 681 230 911
91 230 my $entry = $trust->{$name};
92 230 my $type = js_type($entry);
93 230 ▶ if ( $type eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 3 227 230
94 3 ▶ return ( $backend->{kind} ne 'local' && $entry eq $backend->{address} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$backend->{'kind'} ne 'local' && $entry eq $backend->{'address'} ? : 2 1 3
Condition66%2/3
$backend->{"kind"} ne "local" and $entry eq $backend->{"address"}
!ll&&!rl&&r
102
MC/DC50%1/2
$backend->{"kind"} ne "local" and $entry eq $backend->{"address"}
$backend->{"kind"} ne "local" $entry eq $backend->{"address"}
Truth table66%2/3
A and B
A= $backend->{"kind"} ne "local"B= $entry eq $backend->{"address"}
ABresult
0X0
100
111
95 }
96 227 ▶ return 0 unless $type eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "object" 0 227 227
97 return ( js_type( $entry->{kind} ) eq 'string' && $entry->{kind} eq $backend->{kind}
98 227 ▶ && js_type( $entry->{address} ) eq 'string' && $entry->{address} eq $backend->{address} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
js_type($entry->{'kind'}) eq 'string' && $entry->{'kind'} eq $backend->{'kind'} && js_type($entry->{'address'}) eq 'string' && $entry->{'address'} eq $backend->{'address'} ? : 226 1 227
Condition66%6/9
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string" and $entry->{"address"} eq $backend->{"address"}
!ll&&!rl&&r
10226
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string"
!ll&&!rl&&r
10226
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"}
!ll&&!rl&&r
01226
MC/DC25%1/4
js_type($entry->{"kind"}) eq "string" and $entry->{"kind"} eq $backend->{"kind"} and js_type($entry->{"address"}) eq "string" and $entry->{"address"} eq $backend->{"address"}
js_type($entry->{"kind"}) eq "string" $entry->{"kind"} eq $backend->{"kind"} js_type($entry->{"address"}) eq "string" $entry->{"address"} eq $backend->{"address"}
Truth table40%2/5
A and B and C and D
A= js_type($entry->{"kind"}) eq "string"B= $entry->{"kind"} eq $backend->{"kind"}C= js_type($entry->{"address"}) eq "string"D= $entry->{"address"} eq $backend->{"address"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
99 }
100  
101 #### Last check (the in-session check's clock) #########################
102  
103 # Absolute project keys are canonical only on Windows, where an 8.3 alias and the long path
104 # Git reports must share hook state. POSIX keeps persisted raw and symlink-spelled keys.
105 3047 ▶sub _project_state_key {
Subroutine _project_state_key: called CC=3 SCAR=13.1
106 3047 my ( $key ) = @_;
107 3047 ▶ return $^O eq 'MSWin32' && is_absolute($key) ? real_or_resolved($key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' && is_absolute($key) ? : 0 3047 3047
Condition33%1/3
$^O eq "MSWin32" and is_absolute($key)
!ll&&!rl&&r
304700
MC/DC0%0/2
$^O eq "MSWin32" and is_absolute($key)
$^O eq "MSWin32" is_absolute($key)
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= is_absolute($key)
ABresult
0X0
100
111
108 }
109  
110 2252 ▶sub _now_ms_number { 0 + int( Time::HiRes::time() * 1000 ) }
Subroutine _now_ms_number: called CC=1 SCAR=0.0
111  
112 643 ▶sub is_due {
Subroutine is_due: called CC=3 SCAR=11.0
Pod documented
113 643 my ( $self, $scope_key, $interval_seconds ) = @_;
114 643 $scope_key = _project_state_key($scope_key);
115 643 my $last = $self->_read_json('last-check.json')->{$scope_key};
116 643 ▶ return 1 unless js_type($last) eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($last) eq "number" 253 390 643
117 390 ▶ return _now_ms_number() - $last >= $interval_seconds * 1000 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_now_ms_number() - $last >= $interval_seconds * 1000 ? : 27 363 390
118 }
119  
120 # Mark a check now, dropping the marks of projects that are gone (k255): a key that is no absolute
121 # path, or whose directory – the project dir itself, .claude or not – no longer exists. The file is
122 # written anyway; is_due only reads it, and clear_checked follows a mark.
123 1520 ▶sub mark_checked {
Subroutine mark_checked: called CC=1 SCAR=0.0
Pod documented
124 1520 my ( $self, $scope_key ) = @_;
125 1520 $scope_key = _project_state_key($scope_key);
126 1520 my $checks = $self->_read_json('last-check.json');
127 1520 ▶ _drop_gone_scopes( $checks, $scope_key, sub { $_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
128 1520 $checks->{$scope_key} = _now_ms_number();
129 1520 $self->_write_json( 'last-check.json', $checks );
130 }
131  
132 44 ▶sub clear_checked {
Subroutine clear_checked: called CC=2 SCAR=6.9
Pod documented
133 44 my ( $self, $scope_key ) = @_;
134 44 $scope_key = _project_state_key($scope_key);
135 44 my $checks = $self->_read_json('last-check.json');
136 44 ▶ return unless exists $checks->{$scope_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $checks->{$scope_key} 11 33 44
137 33 delete $checks->{$scope_key};
138 33 $self->_write_json( 'last-check.json', $checks );
139 }
140  
141 # A sync that timed out on the sync lock synced nothing: drop the key's mark, so the next prompt is
142 # due and its background sync retries (k89). Best effort: never throws.
143 38 ▶sub due_again {
Subroutine due_again: called CC=1 SCAR=0.0
Pod documented
144 38 my ( $self, $key ) = @_;
145 38 eval { $self->clear_checked($key); 1 };
146 38 return;
147 }
148  
149 #### Unreached drift (spec §14.3, k70) #################################
150  
151 # The drift ids the last sync of a scope (keyed by its lock path) left in place although every
152 # source it needed was at hand: `check` does not count them again.
153 4080 ▶sub unreached {
Subroutine unreached: called CC=2 SCAR=6.9
Pod documented
154 4080 my ( $self, $scope_key ) = @_;
155 4080 my $ids = $self->_read_json('unreached.json')->{$scope_key};
156 4080 ▶ return [] unless js_type($ids) eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($ids) eq "array" 4072 8 4080
157 8 return [ grep { js_type($_) eq 'string' } @$ids ];
158 }
159  
160 # Replace a scope's unreached drift, dropping the records of scopes that are gone (k255, as
161 # put_sources_read); written only when the file changes.
162 4878 ▶sub put_unreached {
Subroutine put_unreached: called CC=5 SCAR=16.1
Pod documented
163 4878 my ( $self, $scope_key, $ids ) = @_;
164 4878 my $all = $self->_read_json('unreached.json');
165 4878 my %seen;
166 4878 my $next = [ js_sort_keys( grep { !$seen{$_}++ } @$ids ) ];
167 4878 my $dropped = _drop_gone_scopes( $all, $scope_key );
168 4878 ▶ return if !$dropped && js_stringify( $all->{$scope_key} // [] ) eq js_stringify($next);
Branch: 1 branch
BranchTrueFalseTotal
if not $dropped and js_stringify($all->{$scope_key} // []) eq js_stringify($next) 4717 161 4878
Condition100%5/5
not $dropped and js_stringify($all->{$scope_key} // []) eq js_stringify($next)
!ll&&!rl&&r
21594717
$all->{$scope_key} // []
l!l
14875
MC/DC100%2/2
not $dropped and js_stringify($all->{$scope_key} // []) eq js_stringify($next)
not $dropped js_stringify($all->{$scope_key} // []) eq js_stringify($next)
MC/DC100%1/1
$all->{$scope_key} // []
$all->{$scope_key}
Truth table100%3/3
not(A) and B
A= not $droppedB= js_stringify($all->{$scope_key} // []) eq js_stringify($next)
ABresult
0X0
100
111
Truth table100%2/2
A
A= $all->{$scope_key}
Aresult
11
00
169 161 ▶ if (@$next) { $all->{$scope_key} = $next }
Branch: 1 branch
BranchTrueFalseTotal
if (@$next) { } 160 1 161
170 1 else { delete $all->{$scope_key} }
171 161 $self->_write_json( 'unreached.json', $all );
172 }
173  
174 #### Sources read (spec §14.3, k80) ####################################
175  
176 8228 ▶sub _source_read {
Subroutine _source_read: called CC=3 SCAR=11.0
177 8228 my ( $r ) = @_;
178 8228 my $out = js_ordered( kind => $r->{kind}, address => $r->{address}, version => $r->{version} );
179 8228 ▶ $out->{ref} = $r->{ref} if defined $r->{ref};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"ref"} 6 8222 8228
180 8228 ▶ $out->{path} = $r->{path} if defined $r->{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"path"} 6 8222 8228
181 8228 return $out;
182 }
183  
184 # What the last sync of a scope read of each source, and through which backend: `check`
185 # compares a source with it. A malformed entry counts as none.
186 8850 ▶sub sources_read {
Subroutine sources_read: called CC=13 SCAR=28.9
Pod documented
187 8850 my ( $self, $scope_key ) = @_;
188 8850 my $record = $self->_read_json('sources-read.json')->{$scope_key};
189 8850 my $out = js_ordered();
190 8850 ▶ return $out unless js_type($record) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($record) eq "object" 5188 3662 8850
191 3662 for my $name ( keys %$record ) {
192 4083 my $v = $record->{$name};
193 4083 ▶ next unless js_type($v) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v) eq "object" 0 4083 4083
194 4083 ▶ next unless js_type( $v->{kind} ) eq 'string' && $v->{kind} =~ /\A(?:git|url|local)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/ 0 4083 4083
Condition33%1/3
js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/
!ll&&!rl&&r
004083
MC/DC0%0/2
js_type($v->{"kind"}) eq "string" and $v->{"kind"} =~ /\A(?:git|url|local)\z/
js_type($v->{"kind"}) eq "string" $v->{"kind"} =~ /\A(?:git|url|local)\z/
Truth table33%1/3
A and B
A= js_type($v->{"kind"}) eq "string"B= $v->{"kind"} =~ /\A(?:git|url|local)\z/
ABresult
0X0
100
111
195 4083 ▶ next unless js_type( $v->{address} ) eq 'string' && js_type( $v->{version} ) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string" 0 4083 4083
Condition33%1/3
js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string"
!ll&&!rl&&r
004083
MC/DC0%0/2
js_type($v->{"address"}) eq "string" and js_type($v->{"version"}) eq "string"
js_type($v->{"address"}) eq "string" js_type($v->{"version"}) eq "string"
Truth table33%1/3
A and B
A= js_type($v->{"address"}) eq "string"B= js_type($v->{"version"}) eq "string"
ABresult
0X0
100
111
196 4083 ▶ next if exists $v->{ref} && js_type( $v->{ref} ) ne 'string';
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"ref"} and js_type($v->{"ref"}) ne "string" 0 4083 4083
Condition66%2/3
exists $v->{"ref"} and js_type($v->{"ref"}) ne "string"
!ll&&!rl&&r
407760
MC/DC0%0/2
exists $v->{"ref"} and js_type($v->{"ref"}) ne "string"
exists $v->{"ref"} js_type($v->{"ref"}) ne "string"
Truth table66%2/3
A and B
A= exists $v->{"ref"}B= js_type($v->{"ref"}) ne "string"
ABresult
0X0
100
111
197 4083 ▶ next if exists $v->{path} && !( js_type( $v->{path} ) eq 'array' && !grep { js_type($_) ne 'string' } @{ $v->{path} } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};}) 0 4083 4083
Condition50%3/6
exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};})
!ll&&!rl&&r
407760
js_type($v->{'path'}) eq 'array' && !grep({js_type($_) ne 'string';} @{$$v{'path'};})
!ll&&!rl&&r
006
MC/DC0%0/3
exists $v->{"path"} and not js_type($v->{"path"}) eq "array" && !grep({js_type($_) ne "string";} @{$$v{"path"};})
exists $v->{"path"} js_type($v->{'path'}) eq 'array' !grep({js_type($_) ne 'string';} @{$$v{'path'};})
Truth table50%2/4
A and not(B && not(C))
A= exists $v->{"path"}B= js_type($v->{'path'}) eq 'array'C= !grep({js_type($_) ne 'string';} @{$$v{'path'};})
ABCresult
0XX0
1110
10X1
1101
198 4083 $out->{$name} = _source_read($v);
199 }
200 3662 return $out;
201 }
202  
203 # Drop the keys of scopes that are gone (k254) from a state file keyed by lock path, but $keep's: a
204 # key that is no absolute path – it names a checkout only from a cwd long changed – or whose lock's
205 # directory, the scope's .claude, no longer exists. The directory, not the lock: a sync that
206 # installed nothing leaves no lock, and its record still holds. A file keyed by another path names
207 # its directory with $dir_of (last-check.json: the project dir itself, k255). True when one was
208 # dropped. File::Basename's dirname, as Node's, takes either separator on Windows; a key with a NUL
209 # exists nowhere, silently, as for existsSync.
210 16073 ▶sub _drop_gone_scopes {
Subroutine _drop_gone_scopes: called CC=6 SCAR=17.9
211 16073 my ( $all, $keep, $dir_of ) = @_;
212 16073 ▶ $dir_of //= \&File::Basename::dirname;
Condition100%2/2
$dir_of //= \&File::Basename::dirname
l!l
152014553
MC/DC100%1/1
$dir_of //= \&File::Basename::dirname
$dir_of
Truth table100%2/2
A
A= $dir_of
Aresult
11
00
213 413 ▶ no warnings 'syscalls';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 16073 ▶ my @gone = grep { $_ ne $keep && !( is_absolute($_) && -e encode_text( $dir_of->($_) ) ) } keys %$all;
Condition100%6/6
$_ ne $keep && !(is_absolute($_) && -e encode_text(&$dir_of($_)))
!ll&&!rl&&r
4015437813
is_absolute($_) && -e encode_text(&$dir_of($_))
!ll&&!rl&&r
584378
MC/DC100%3/3
$_ ne $keep && !(is_absolute($_) && -e encode_text(&$dir_of($_)))
$_ ne $keep is_absolute($_) -e encode_text(&$dir_of($_))
Truth table100%4/4
A && not(B && C)
A= $_ ne $keepB= is_absolute($_)C= -e encode_text(&$dir_of($_))
ABCresult
0XX0
1110
10X1
1101
215 16073 delete $all->{$_} for @gone;
216 16073 ▶ return @gone ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@gone ? : 7 16066 16073
217 }
218  
219 # Replace a scope's record of sources read, dropping the records of scopes that are gone (k254);
220 # written only when the file changes.
221 4880 ▶sub put_sources_read {
Subroutine put_sources_read: called CC=6 SCAR=17.9
Pod documented
222 4880 my ( $self, $scope_key, $record ) = @_;
223 4880 my $all = $self->_read_json('sources-read.json');
224 4880 my $next = js_ordered();
225 4880 $next->{$_} = _source_read( $record->{$_} ) for js_sort_keys( keys %$record );
226 4880 my $dropped = _drop_gone_scopes( $all, $scope_key );
227 4880 ▶ return if !$dropped && js_stringify( $all->{$scope_key} // {} ) eq js_stringify($next);
Branch: 1 branch
BranchTrueFalseTotal
if not $dropped and js_stringify($all->{$scope_key} // {}) eq js_stringify($next) 2743 2137 4880
Condition100%5/5
not $dropped and js_stringify($all->{$scope_key} // {}) eq js_stringify($next)
!ll&&!rl&&r
221352743
$all->{$scope_key} // {}
l!l
17083170
MC/DC100%2/2
not $dropped and js_stringify($all->{$scope_key} // {}) eq js_stringify($next)
not $dropped js_stringify($all->{$scope_key} // {}) eq js_stringify($next)
MC/DC100%1/1
$all->{$scope_key} // {}
$all->{$scope_key}
Truth table100%3/3
not(A) and B
A= not $droppedB= js_stringify($all->{$scope_key} // {}) eq js_stringify($next)
ABresult
0X0
100
111
Truth table100%2/2
A
A= $all->{$scope_key}
Aresult
11
00
228 2137 ▶ if (%$next) { $all->{$scope_key} = $next }
Branch: 1 branch
BranchTrueFalseTotal
if (%$next) { } 2087 50 2137
229 50 else { delete $all->{$scope_key} }
230 2137 $self->_write_json( 'sources-read.json', $all );
231 }
232  
233 #### Render inputs (spec §14.3, k76) ###################################
234  
235 3174 ▶sub render_inputs {
Subroutine render_inputs: called CC=2 SCAR=6.9
Pod documented
236 3174 my ( $self, $scope_key ) = @_;
237 3174 my $hash = $self->_read_json('render-inputs.json')->{$scope_key};
238 3174 ▶ return js_type($hash) eq 'string' ? $hash : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($hash) eq 'string' ? : 1950 1224 3174
239 }
240  
241 # Replace a scope's render-inputs hash, or drop it (undef), dropping the records of scopes that are
242 # gone (k255, as put_sources_read); written only when the file changes.
243 4795 ▶sub put_render_inputs {
Subroutine put_render_inputs: called CC=8 SCAR=20.8
Pod documented
244 4795 my ( $self, $scope_key, $hash ) = @_;
245 4795 my $all = $self->_read_json('render-inputs.json');
246 4795 my $dropped = _drop_gone_scopes( $all, $scope_key );
247 4795 ▶ if ( defined $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash) { } 3825 970 4795
248 3825 ▶ return if !$dropped && exists $all->{$scope_key} && js_type( $all->{$scope_key} ) eq 'string' && $all->{$scope_key} eq $hash;
Branch: 1 branch
BranchTrueFalseTotal
if not $dropped and exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash 1557 2268 3825
Condition88%8/9
not $dropped and exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash
!ll&&!rl&&r
2186821557
not $dropped and exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string"
!ll&&!rl&&r
218601639
not $dropped and exists $all->{$scope_key}
!ll&&!rl&&r
121851639
MC/DC75%3/4
not $dropped and exists $all->{$scope_key} and js_type($all->{$scope_key}) eq "string" and $all->{$scope_key} eq $hash
not $dropped exists $all->{$scope_key} js_type($all->{$scope_key}) eq "string" $all->{$scope_key} eq $hash
Truth table80%4/5
not(A) and B and C and D
A= not $droppedB= exists $all->{$scope_key}C= js_type($all->{$scope_key}) eq "string"D= $all->{$scope_key} eq $hash
ABCDresult
0XXX0
10XX0
110X0
11100
11111
249 2268 $all->{$scope_key} = $hash;
250 }
251 else {
252 970 ▶ return if !$dropped && !exists $all->{$scope_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $dropped or exists $all->{$scope_key} 878 92 970
Condition100%3/3
$dropped or exists $all->{$scope_key}
l!l&&r!l&&!r
191878
MC/DC100%2/2
$dropped or exists $all->{$scope_key}
$dropped exists $all->{$scope_key}
Truth table100%3/3
A or B
A= $droppedB= exists $all->{$scope_key}
ABresult
1X1
011
000
253 92 delete $all->{$scope_key};
254 }
255 2360 $self->_write_json( 'render-inputs.json', $all );
256 }
257  
258 #### Pending report (spec §8) #########################################
259  
260 # A background sync's hook output for a project, for one of its next prompts to show.
261 92 ▶sub put_pending_report {
Subroutine put_pending_report: called CC=1 SCAR=0.0
Pod documented
262 92 my ( $self, $project_key, $report ) = @_;
263 92 $project_key = _project_state_key($project_key);
264 92 my $pending = $self->_read_json('pending-report.json');
265 92 $pending->{$project_key} = $report;
266 92 $self->_write_json( 'pending-report.json', $pending );
267 }
268  
269 # A project's pending report, taken out (undef: none); the file is written only when it held one.
270 748 ▶sub take_pending_report {
Subroutine take_pending_report: called CC=2 SCAR=6.9
Pod documented
271 748 my ( $self, $project_key ) = @_;
272 748 $project_key = _project_state_key($project_key);
273 748 my $pending = $self->_read_json('pending-report.json');
274 748 ▶ return undef unless exists $pending->{$project_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pending->{$project_key} 674 74 748
275 74 my $report = delete $pending->{$project_key};
276 74 $self->_write_json( 'pending-report.json', $pending );
277 74 return $report;
278 }
279  
280 #### Session cwd (spec §8, k149) #######################################
281  
282 # Sessions `session-cwd.json` keeps (a Hermes gateway runs for days).
283 413 ▶use constant SESSION_CWDS => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 # A field of a session's entry; undef for an entry that is no object.
286 1646 ▶sub _entry_field {
Subroutine _entry_field: called CC=2 SCAR=6.9
287 1646 my ( $entry, $field ) = @_;
288 1646 ▶ return js_type($entry) eq 'object' ? $entry->{$field} : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($entry) eq 'object' ? : 1347 299 1646
289 }
290  
291 # Where a session's hook last looked: the cwd of its last SessionStart or prompt. A malformed
292 # entry counts as none.
293 1052 ▶sub session_cwd {
Subroutine session_cwd: called CC=2 SCAR=6.9
Pod documented
294 1052 my ( $self, $session_id ) = @_;
295 1052 my $cwd = _entry_field( $self->_read_json('session-cwd.json')->{$session_id}, 'cwd' );
296 1052 ▶ return js_type($cwd) eq 'string' ? $cwd : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($cwd) eq 'string' ? : 752 300 1052
297 }
298  
299 # Record a session's cwd; written only when it changes, keeping the SESSION_CWDS sessions recorded
300 # last (by `at`, newest first; equal ones in the file's order) – one that lost its entry is looked
301 # at once more.
302 519 ▶sub put_session_cwd {
Subroutine put_session_cwd: called CC=6 SCAR=17.9
Pod documented
303 519 my ( $self, $session_id, $cwd ) = @_;
304 519 my $have = $self->session_cwd($session_id);
305 519 ▶ return if defined $have && $have eq $cwd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $have and $have eq $cwd 1 518 519
Condition100%3/3
defined $have and $have eq $cwd
!ll&&!rl&&r
2422761
MC/DC100%2/2
defined $have and $have eq $cwd
defined $have $have eq $cwd
Truth table100%3/3
A and B
A= defined $haveB= $have eq $cwd
ABresult
0X0
100
111
306 518 my $all = $self->_read_json('session-cwd.json');
307 518 my @others = grep { $_ ne $session_id } keys %$all;
308 518 ▶ my @at = map { my $t = _entry_field( $all->{$_}, 'at' ); js_type($t) eq 'number' ? $t : 0 } @others;
Branch: 1 branch
BranchTrueFalseTotal
js_type($t) eq 'number' ? : 594 0 594
309 518 ▶ my @newest = map { $others[$_] } sort { $at[$b] <=> $at[$a] || $a <=> $b } 0 .. $#others;
Condition66%2/3
$at[$b] <=> $at[$a] || $a <=> $b
l!l&&r!l&&!r
55310
MC/DC0%0/2
$at[$b] <=> $at[$a] || $a <=> $b
$at[$b] <=> $at[$a] $a <=> $b
Truth table66%2/3
A || B
A= $at[$b] <=> $at[$a]B= $a <=> $b
ABresult
1X1
011
000
310 518 delete $all->{$_} for @newest[ SESSION_CWDS - 1 .. $#newest ];
311 518 $all->{$session_id} = js_ordered( cwd => $cwd, at => _now_ms_number() );
312 518 $self->_write_json( 'session-cwd.json', $all );
313 }
314  
315 #### Hermes home (spec §16.2) ##########################################
316  
317 18590 ▶sub hermes_home {
Subroutine hermes_home: called CC=3 SCAR=11.0
Pod documented
318 18590 my ( $self ) = @_;
319 18590 my $home = $self->_read_json('hermes-home.json')->{home};
320 18590 ▶ return ( js_type($home) eq 'string' && length $home ) ? $home : undef;
Branch: 1 branch
BranchTrueFalseTotal
js_type($home) eq 'string' && length $home ? : 875 17715 18590
Condition66%2/3
js_type($home) eq "string" and length $home
!ll&&!rl&&r
177150875
MC/DC50%1/2
js_type($home) eq "string" and length $home
js_type($home) eq "string" length $home
Truth table66%2/3
A and B
A= js_type($home) eq "string"B= length $home
ABresult
0X0
100
111
321 }
322  
323 # Record the Hermes home, or drop the record (undef); written only when it changes.
324 2370 ▶sub put_hermes_home {
Subroutine put_hermes_home: called CC=7 SCAR=19.5
Pod documented
325 2370 my ( $self, $home ) = @_;
326 2370 my $have = $self->hermes_home;
327 2370 ▶ return if ( !defined $have && !defined $home ) || ( defined $have && defined $home && $have eq $home );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $have || defined $home or defined $have and defined $home and $have eq $home 1994 376 2370
Condition83%10/12
not defined $have || defined $home or defined $have and defined $home and $have eq $home
l!l&&r!l&&!r
19940376
defined $have || defined $home
l!l&&r!l&&!r
513251994
defined $have and defined $home and $have eq $home
!ll&&!rl&&r
37330
defined $have and defined $home
!ll&&!rl&&r
325483
MC/DC40%2/5
not defined $have || defined $home or defined $have and defined $home and $have eq $home
defined $have defined $home defined $have defined $home $have eq $home
Truth table44%4/9
not(A || B) or C and D and E
A= defined $haveB= defined $homeC= defined $haveD= defined $homeE= $have eq $home
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
328 376 delete $self->_view->{'hermes-home.json'}; # written or removed: read again
329 376 ▶ if ( defined $home ) { $self->_write_json( 'hermes-home.json', js_ordered( home => $home ) ) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $home) { } 328 48 376
330 48 else { unlink Skilletor::FSUtil::encode_text( $self->_file('hermes-home.json') ) }
331 }
332  
333 #### The sync lock (spec §6.5) #########################################
334  
335 413 ▶use constant LOCK_NAME => 'sync.lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
336  
337 # What a lock leaves beside it: a broken lock's tombstone, a released lock on its way out.
338 413 ▶use constant LEFTOVER => LOCK_NAME.'.';
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 11567 ▶sub _now_ms { int( Time::HiRes::time() * 1000 ) }
Subroutine _now_ms: called CC=1 SCAR=0.0
341  
342 # The stops a process can catch (k251): a harness cancelling a hook – Claude Code sends SIGTERM to
343 # the hook's process group, SIGKILL 1.5 s later –, Ctrl+C, a closed terminal. By name: _stopped
344 # looks a number up only when one comes, since a POSIX macro the platform lacks dies wherever it
345 # is evaluated – Windows' POSIX has no SIGHUP, and loading this module there must not die (k261).
346 413 ▶use constant STOPS => qw( TERM INT HUP );
Subroutine BEGIN: called CC=1 SCAR=0.0
347  
348 # The release of each sync lock this process takes or holds, by token, with the pid that took it: a
349 # child forked under the lock – the heartbeat, a spawn before its exec – holds none.
350 413 my %held;
351  
352 # Run $code holding `sync.lock/`, the one mutex over the locks, the target dirs and the source
353 # cache – shared with the reference, which takes the same directory the same way: mkdir, then
354 # `owner.json` created exclusively; a holder refreshes the record's mtime; a record not
355 # refreshed for the stale age – or for two refreshes when its pid on this host is gone – is a
356 # dead run's, an owner-less dir older than the grace time one that died before writing it. A stop
357 # the process can catch releases the lock before it ends the process (k251).
358 4478 ▶sub with_lock {
Subroutine with_lock: called CC=20 SCAR=30.2
Pod documented
359 4478 my ( $self, $code, %opts ) = @_;
360 4478 ▶ my $timeout_ms = $opts{timeout_ms} // 5_000;
Condition100%2/2
$opts{"timeout_ms"} // 5000
l!l
440573
MC/DC100%1/1
$opts{"timeout_ms"} // 5000
$opts{"timeout_ms"}
Truth table100%2/2
A
A= $opts{"timeout_ms"}
Aresult
11
00
361 4478 ▶ my $stale_ms = $opts{stale_ms} // 300_000;
Condition50%1/2
$opts{"stale_ms"} // 300000
l!l
04478
MC/DC0%0/1
$opts{"stale_ms"} // 300000
$opts{"stale_ms"}
Truth table50%1/2
A
A= $opts{"stale_ms"}
Aresult
11
00
362 4478 ▶ my $refresh_ms = $opts{refresh_ms} // ( $stale_ms / 3 < 10_000 ? int( $stale_ms / 3 ) : 10_000 );
Branch: 1 branch
BranchTrueFalseTotal
$stale_ms / 3 < 10000 ? : 0 4448 4448
Condition100%2/2
$opts{"refresh_ms"} // ($stale_ms / 3 < 10000 ? int $stale_ms / 3 : 10000)
l!l
304448
MC/DC100%1/1
$opts{"refresh_ms"} // ($stale_ms / 3 < 10000 ? int $stale_ms / 3 : 10000)
$opts{"refresh_ms"}
Truth table100%2/2
A
A= $opts{"refresh_ms"}
Aresult
11
00
363 4478 ▶ $refresh_ms = 1 if $refresh_ms < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $refresh_ms < 1 0 4478 4478
364 4478 ▶ my $grace_ms = $opts{grace_ms} // 2_000;
Condition50%1/2
$opts{"grace_ms"} // 2000
l!l
04478
MC/DC0%0/1
$opts{"grace_ms"} // 2000
$opts{"grace_ms"}
Truth table50%1/2
A
A= $opts{"grace_ms"}
Aresult
11
00
365 4478 ▶ my $poll_ms = $opts{poll_ms} // 25;
Condition50%1/2
$opts{"poll_ms"} // 25
l!l
04478
MC/DC0%0/1
$opts{"poll_ms"} // 25
$opts{"poll_ms"}
Truth table50%1/2
A
A= $opts{"poll_ms"}
Aresult
11
00
366 4478 ▶ my $win32 = $opts{win32} // $^O eq 'MSWin32';
Condition100%2/2
$opts{"win32"} // $^O eq "MSWin32"
l!l
24476
MC/DC100%1/1
$opts{"win32"} // $^O eq "MSWin32"
$opts{"win32"}
Truth table100%2/2
A
A= $opts{"win32"}
Aresult
11
00
367 4478 my $lock_dir = $self->_file(LOCK_NAME);
368 4478 my $owner = path_join( $lock_dir, 'owner.json' );
369 4478 my $token = join '', map { sprintf '%02x', int rand 256 } 1 .. 8;
370 4478 my $deadline = _now_ms() + $timeout_ms;
371  
372 # While this process takes or holds the lock, a stop that would end it – no handler of the
373 # program's own, not ignored; not on Windows, where a process is ended without a signal it can
374 # catch – gives every lock it holds up first (the reference's releaseOnStop). In place before
375 # the owner record is written, so a stop that comes once it is finds the handler; until then,
376 # the release gives nothing up – the token check. A lock taken inside another finds the outer
377 # one's handlers and adds none. A stop can never release a lock it was only waiting for, nor a
378 # SIGKILL any: the next run breaks a lock a dead owner left (_dead_owner).
379 0 ▶ local $held{$token} = [ $$, sub { $self->_release( $lock_dir, $owner, $token ) } ];
Subroutine __ANON__: not called CC=1 SCAR=1.2
380 4478 ▶ my @stops = $^O eq 'MSWin32' ? () : grep { ( $SIG{$_} // '' ) =~ /\A(?:DEFAULT)?\z/ } STOPS;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 4478 4478
Condition100%2/2
$SIG{$_} // ""
l!l
18613248
MC/DC100%1/1
$SIG{$_} // ""
$SIG{$_}
Truth table100%2/2
A
A= $SIG{$_}
Aresult
11
00
381 0 ▶ local @SIG{@stops} = map { my $name = $_; sub { _stopped($name) } } @stops;
Subroutine __ANON__: not called CC=1 SCAR=0.2
382  
383 4478 for ( ;; ) {
384 5758 ▶ last if $self->_acquire( $lock_dir, $owner, $token, $refresh_ms );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_acquire($lock_dir, $owner, $token, $refresh_ms) 4430 1328 5758
385 1328 ▶ if ( my $stale = $self->_judge( $lock_dir, $owner, $stale_ms, $grace_ms ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stale = $self->_judge($lock_dir, $owner, $stale_ms, $grace_ms)) 4 1324 1328
386 4 ▶ next if $self->_break_lock( $lock_dir, $owner, $stale );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_break_lock($lock_dir, $owner, $stale) 4 0 4
387 }
388 1324 ▶ Skilletor::Error::LockTimeout->throw( 'timed out acquiring sync lock at '.$lock_dir ) if _now_ms() >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if _now_ms() >= $deadline 48 1276 1324
389 1276 Time::HiRes::sleep( $poll_ms / 1000 );
390 }
391  
392 4430 %{ $self->_view } = (); # what was read before the lock is not what holds under it
393 4430 my $heartbeat = $self->_heartbeat( $owner, $refresh_ms, $win32 );
394 4430 my $want = wantarray;
395 4430 my @result;
396 4430 my $ok = eval {
397 4430 ▶ local $Skilletor::Process::ON_WAIT = $heartbeat if ref $heartbeat eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $heartbeat eq "CODE" 2 4428 4430
398 4430 $self->_sweep_leftovers($stale_ms);
399 4430 ▶ if ($want) { @result = $code->() }
Branch: 2 branches
BranchTrueFalseTotal
if ($want) { } 1 4429 4430
elsif (defined $want) { } 4353 76 4429
400 4353 elsif ( defined $want ) { $result[0] = $code->() }
401 76 else { $code->() }
402 3980 1;
403 };
404 4150 my $err = $@;
405 4150 $self->_stop_heartbeat($heartbeat);
406 4150 $self->_release( $lock_dir, $owner, $token );
407 4150 %{ $self->_view } = (); # nor what was read under it once another run may write
408 4150 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 170 3980 4150
409 3980 ▶ return $want ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
$want ? : 1 3979 3980
410 }
411  
412 # A stop while this process takes or holds a lock: every lock it holds given up, then the end the
413 # signal would have brought – by the signal itself, its default action back and Perl's block of it
414 # while its handler runs lifted; by its conventional status should that not end the process, which
415 # must not run on without its lock.
416 0 ▶sub _stopped {
Subroutine _stopped: not called CC=3 SCAR=24.8
417 0 my ($name) = @_;
418 0 ▶ $_->[0] == $$ and $_->[1]->() for values %held;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] == $$ and $_->[1]->() 0 0 0
419 0 %held = ();
420 0 my $number = POSIX->can( 'SIG'.$name )->();
421 0 $SIG{$name} = 'DEFAULT';
422 0 kill $name, $$;
423 0 POSIX::sigprocmask( POSIX::SIG_UNBLOCK(), POSIX::SigSet->new($number) );
424 0 POSIX::_exit( 128 + $number );
425 }
426  
427 5758 ▶sub _acquire {
Subroutine _acquire: called CC=6 SCAR=22.2
428 5758 my ( $self, $lock_dir, $owner, $token, $refresh_ms ) = @_;
429 5758 ▶ unless ( mkdir $lock_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $lock_dir) 1328 4430 5758
430 1328 ▶ return 0 if $!{EEXIST};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EEXIST"} 1328 0 1328
431 0 die 'mkdir '.$lock_dir.': '.$!."\n";
432 }
433 4430 my $record = js_ordered( pid => 0 + $$, host => Sys::Hostname::hostname(), token => $token, at => _now_ms(), refreshMs => 0 + $refresh_ms );
434 4430 my $fh;
435 4430 ▶ unless ( sysopen $fh, $owner, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $owner, 193) 0 4430 4430
436 0 ▶ return 0 if $!{EEXIST} || $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EEXIST"} or $!{"ENOENT"} 0 0 0
Condition0%0/3
$!{"EEXIST"} or $!{"ENOENT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EEXIST"} or $!{"ENOENT"}
$!{"EEXIST"} $!{"ENOENT"}
Truth table0%0/3
A or B
A= $!{"EEXIST"}B= $!{"ENOENT"}
ABresult
1X1
011
000
437 0 my $why = "$!";
438 0 unlink $owner;
439 0 rmdir $lock_dir;
440 0 die 'open '.$owner.': '.$why."\n";
441 }
442 4430 binmode $fh;
443 4430 print {$fh} Skilletor::FSUtil::encode_text( js_stringify($record) );
444 4430 close $fh;
445 4430 _touch($owner);
446 4430 return 1;
447 }
448  
449 # Is the lock stale? What the waiter saw of it when it is, else undef.
450 1328 ▶sub _judge {
Subroutine _judge: called CC=6 SCAR=18.0
451 1328 my ( $self, $lock_dir, $owner, $stale_ms, $grace_ms ) = @_;
452 1328 my @st = Time::HiRes::stat($owner);
453 1328 ▶ unless (@st) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@st) 1 1327 1328
454 1 ▶ my @dir = Time::HiRes::stat($lock_dir) or return undef; # released meanwhile
Branch: 1 branch
BranchTrueFalseTotal
my(@dir) = &Time::HiRes::stat($lock_dir) or return undef 1 0 1
455 1 ▶ return undef if _now_ms() - $dir[9] * 1000 <= $grace_ms;
Branch: 1 branch
BranchTrueFalseTotal
if _now_ms() - $dir[9] * 1000 <= $grace_ms 0 1 1
456 1 return { ownerless => 1, ino => $dir[1], mtime_ms => $dir[9] * 1000 };
457 }
458 1327 my $age = _now_ms() - $st[9] * 1000;
459 1327 ▶ return undef if $age <= $stale_ms && !$self->_dead_owner( $owner, $age );
Branch: 1 branch
BranchTrueFalseTotal
if $age <= $stale_ms and not $self->_dead_owner($owner, $age) 1324 3 1327
Condition100%3/3
$age <= $stale_ms and not $self->_dead_owner($owner, $age)
!ll&&!rl&&r
121324
MC/DC100%2/2
$age <= $stale_ms and not $self->_dead_owner($owner, $age)
$age <= $stale_ms not $self->_dead_owner($owner, $age)
Truth table100%3/3
A and not(B)
A= $age <= $stale_msB= not $self->_dead_owner($owner, $age)
ABresult
0X0
100
111
460 3 return { ownerless => 0, ino => $st[1], mtime_ms => $st[9] * 1000 };
461 }
462  
463 # Did the lock's owner die? Only for a record of this host whose pid is gone and that missed two
464 # of its own refreshes.
465 1326 ▶sub _dead_owner {
Subroutine _dead_owner: called CC=13 SCAR=30.6
466 1326 my ( $self, $owner, $age ) = @_;
467 1326 my $record = eval { js_parse( read_text($owner) ) };
468 1326 ▶ return 0 unless defined $record && js_type($record) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record and js_type($record) eq "object" 0 1326 1326
Condition33%1/3
defined $record and js_type($record) eq "object"
!ll&&!rl&&r
001326
MC/DC0%0/2
defined $record and js_type($record) eq "object"
defined $record js_type($record) eq "object"
Truth table33%1/3
A and B
A= defined $recordB= js_type($record) eq "object"
ABresult
0X0
100
111
469 1326 my ( $pid, $host, $refresh ) = @{$record}{qw( pid host refreshMs )};
470 1326 ▶ return 0 unless js_type($host) eq 'string' && $host eq Sys::Hostname::hostname();
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($host) eq "string" and $host eq Sys::Hostname::hostname() 0 1326 1326
Condition33%1/3
js_type($host) eq "string" and $host eq Sys::Hostname::hostname()
!ll&&!rl&&r
001326
MC/DC0%0/2
js_type($host) eq "string" and $host eq Sys::Hostname::hostname()
js_type($host) eq "string" $host eq Sys::Hostname::hostname()
Truth table33%1/3
A and B
A= js_type($host) eq "string"B= $host eq Sys::Hostname::hostname()
ABresult
0X0
100
111
471 1326 ▶ return 0 unless js_type($pid) eq 'number' && $pid == int($pid) && $pid > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($pid) eq "number" and $pid == int $pid and $pid > 0 0 1326 1326
Condition33%2/6
js_type($pid) eq "number" and $pid == int $pid and $pid > 0
!ll&&!rl&&r
001326
js_type($pid) eq "number" and $pid == int $pid
!ll&&!rl&&r
001326
MC/DC0%0/3
js_type($pid) eq "number" and $pid == int $pid and $pid > 0
js_type($pid) eq "number" $pid == int $pid $pid > 0
Truth table25%1/4
A and B and C
A= js_type($pid) eq "number"B= $pid == int $pidC= $pid > 0
ABCresult
0XX0
10X0
1100
1111
472 1326 ▶ return 0 unless js_type($refresh) eq 'number' && $refresh > 0 && $age > 2 * $refresh;
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh 1319 7 1326
Condition50%3/6
js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh
!ll&&!rl&&r
013197
js_type($refresh) eq "number" and $refresh > 0
!ll&&!rl&&r
001326
MC/DC33%1/3
js_type($refresh) eq "number" and $refresh > 0 and $age > 2 * $refresh
js_type($refresh) eq "number" $refresh > 0 $age > 2 * $refresh
Truth table50%2/4
A and B and C
A= js_type($refresh) eq "number"B= $refresh > 0C= $age > 2 * $refresh
ABCresult
0XX0
10X0
1100
1111
473 7 ▶ return 0 if kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 5 2 7
474 2 ▶ return $!{ESRCH} ? 1 : 0; # EPERM: alive, another user's
Branch: 1 branch
BranchTrueFalseTotal
$!{'ESRCH'} ? : 2 0 2
475 }
476  
477 # Break a lock judged stale; true when it is gone, so taking it is worth a try at once.
478 4 ▶sub _break_lock {
Subroutine _break_lock: called CC=11 SCAR=39.8
479 4 my ( $self, $lock_dir, $owner, $stale ) = @_;
480 4 ▶ if ( $stale->{ownerless} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stale->{"ownerless"}) 1 3 4
481 1 ▶ return 1 if rmdir $lock_dir;
Branch: 1 branch
BranchTrueFalseTotal
if rmdir $lock_dir 1 0 1
482 0 ▶ return 1 if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 0 0 0
483 0 ▶ return 0 if -e $owner; # its run wrote the record meanwhile: live
Branch: 1 branch
BranchTrueFalseTotal
if -e $owner 0 0 0
484 }
485 3 my $tomb = $self->_file( LEFTOVER.'broken-'.$stale->{ino}.'-'.int( $stale->{mtime_ms} ) );
486 3 _touch($lock_dir);
487 3 ▶ return 1 if rename $lock_dir, $tomb;
Branch: 1 branch
BranchTrueFalseTotal
if rename $lock_dir, $tomb 3 0 3
488 0 ▶ return 1 if $!{ENOENT};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOENT"} 0 0 0
489 0 ▶ return 0 if $!{ENOTEMPTY} || $!{EEXIST}; # another waiter broke it
Branch: 1 branch
BranchTrueFalseTotal
if $!{"ENOTEMPTY"} or $!{"EEXIST"} 0 0 0
Condition0%0/3
$!{"ENOTEMPTY"} or $!{"EEXIST"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"ENOTEMPTY"} or $!{"EEXIST"}
$!{"ENOTEMPTY"} $!{"EEXIST"}
Truth table0%0/3
A or B
A= $!{"ENOTEMPTY"}B= $!{"EEXIST"}
ABresult
1X1
011
000
490 0 ▶ return 0 if $^O eq 'MSWin32' && $!{EACCES};
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and $!{"EACCES"} 0 0 0
Condition0%0/3
$^O eq "MSWin32" and $!{"EACCES"}
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and $!{"EACCES"}
$^O eq "MSWin32" $!{"EACCES"}
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= $!{"EACCES"}
ABresult
0X0
100
111
491 0 die 'rename '.$lock_dir.': '.$!."\n";
492 }
493  
494 # Give the lock up – only this run's own; renamed away first, so no waiter sees it half removed.
495 4150 ▶sub _release {
Subroutine _release: called CC=6 SCAR=20.2
496 4150 my ( $self, $lock_dir, $owner, $token ) = @_;
497 4150 my $record = eval { js_parse( read_text($owner) ) };
498 4150 ▶ return unless defined $record && js_type($record) eq 'object' && ( $record->{token} // '' ) eq $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token 0 4150 4150
Condition37%3/8
defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token
!ll&&!rl&&r
004150
defined $record and js_type($record) eq "object"
!ll&&!rl&&r
004150
$record->{"token"} // ""
l!l
41500
MC/DC0%0/3
defined $record and js_type($record) eq "object" and ($record->{"token"} // "") eq $token
defined $record js_type($record) eq "object" $record->{"token"}
Truth table25%1/4
A and B and C
A= defined $recordB= js_type($record) eq "object"C= $record->{"token"}
ABCresult
0XX0
10X0
1100
1111
499 4150 my $gone = $self->_file( LEFTOVER.'released-'.$token );
500 4150 ▶ rename $lock_dir, $gone or return;
Branch: 1 branch
BranchTrueFalseTotal
rename $lock_dir, $gone or return 4150 0 4150
501 4150 eval { path($gone)->remove_tree( { safe => 0 } ) };
502 }
503  
504 # Remove what earlier locks left beside `sync.lock/` once older than the stale age.
505 4430 ▶sub _sweep_leftovers {
Subroutine _sweep_leftovers: called CC=7 SCAR=20.5
506 4430 my ( $self, $stale_ms ) = @_;
507 4430 ▶ opendir( my $dh, $self->root ) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->root or return 4430 0 4430
508 4430 my @names = grep { index( $_, LEFTOVER ) == 0 } readdir $dh;
509 4430 closedir $dh;
510 4430 for my $name (@names) {
511 7 my $p = $self->_file($name);
512 7 ▶ my @st = Time::HiRes::lstat($p) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = &Time::HiRes::lstat($p) or next 7 0 7
513 7 ▶ next unless _now_ms() - $st[9] * 1000 > $stale_ms;
Branch: 1 branch
BranchTrueFalseTotal
unless _now_ms() - $st[9] * 1000 > $stale_ms 5 2 7
514 0 ▶ if ( -l $p || !-d $p ) { unlink $p }
Branch: 1 branch
BranchTrueFalseTotal
if (-l $p or not -d $p) { } 0 2 2
Condition33%1/3
-l $p or not -d $p
l!l&&r!l&&!r
002
MC/DC0%0/2
-l $p or not -d $p
-l $p not -d $p
Truth table33%1/3
A or not(B)
A= -l $pB= not -d $p
ABresult
1X1
011
000
515 2 else { eval { path($p)->remove_tree( { safe => 0 } ) } }
516 }
517 }
518  
519 # Stamp a path's times with now. Time::HiRes has no utime on Windows (it dies there): the core's,
520 # which takes now from the system when given no times.
521 4484 ▶sub _touch {
Subroutine _touch: called CC=3 SCAR=11.0
522 4484 my ( $path, $hires ) = @_;
523 4484 ▶ return utime undef, undef, $path unless $hires // Time::HiRes::d_hires_utime();
Branch: 1 branch
BranchTrueFalseTotal
unless $hires // Time::HiRes::d_hires_utime() 1 4483 4484
Condition66%2/3
$hires // Time::HiRes::d_hires_utime()
l!l&&r!l&&!r
144830
MC/DC0%0/2
$hires // Time::HiRes::d_hires_utime()
$hires Time::HiRes::d_hires_utime()
Truth table66%2/3
A // B
A= $hiresB= Time::HiRes::d_hires_utime()
ABresult
1X1
011
000
524 4483 my $now = Time::HiRes::time();
525 4483 Time::HiRes::utime( $now, $now, $path );
526 }
527  
528 # The refreshes: Perl runs a resolve synchronously, so a child process stamps the owner record
529 # while the holder works, and stops when told or when the holder is gone. Windows has no fork to
530 # spare – Perl emulates one, as threads, with a clone of the interpreter whose end runs every
531 # object's DESTROY once more – so there the holder stamps the record itself, at most once a
532 # refresh, each time it waits on a program: a code ref for Skilletor::Process's ON_WAIT, which
533 # the caller installs while its code runs (a resolve's git runs; not an HTTP download).
534 4430 ▶sub _heartbeat {
Subroutine _heartbeat: called CC=6 SCAR=25.8
535 4430 my ( $self, $owner, $refresh_ms, $win32 ) = @_;
536 4430 ▶ return _wait_tick( $owner, $refresh_ms ) if $win32;
Branch: 1 branch
BranchTrueFalseTotal
if $win32 2 4428 4430
537 4428 my $parent = $$;
538 4428 my $pid = fork;
539 4428 ▶ return undef unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 4428 4428
540 4428 ▶ return $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 4428 0 4428
541 0 ▶ local $SIG{TERM} = sub { POSIX::_exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
542 0 for ( ;; ) {
543 0 Time::HiRes::sleep( $refresh_ms / 1000 );
544 0 ▶ last unless getppid() == $parent && -e $owner; # no refresh once the holder is gone (k251)
Branch: 1 branch
BranchTrueFalseTotal
unless getppid == $parent and -e $owner 0 0 0
Condition0%0/3
getppid == $parent and -e $owner
!ll&&!rl&&r
000
MC/DC0%0/2
getppid == $parent and -e $owner
getppid == $parent -e $owner
Truth table0%0/3
A and B
A= getppid == $parentB= -e $owner
ABresult
0X0
100
111
545 0 _touch($owner);
546 }
547 0 POSIX::_exit(0);
548 }
549  
550 2 ▶sub _wait_tick {
Subroutine _wait_tick: called CC=1 SCAR=0.0
551 2 my ( $owner, $refresh_ms ) = @_;
552 2 my $outer = $Skilletor::Process::ON_WAIT; # a lock held inside another's: refresh that one too
553 2 my $last = Time::HiRes::time();
554 209 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
555 209 my $now = Time::HiRes::time();
556 209 ▶ if ( ( $now - $last ) * 1000 >= $refresh_ms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($now - $last) * 1000 >= $refresh_ms) 50 159 209
557 50 $last = $now;
558 50 _touch($owner);
559 }
560 209 ▶ $outer->() if $outer;
Branch: 1 branch
BranchTrueFalseTotal
if $outer 69 140 209
561 2 };
562 }
563  
564 4150 ▶sub _stop_heartbeat {
Subroutine _stop_heartbeat: called CC=3 SCAR=11.0
565 4150 my ( $self, $pid ) = @_;
566 4150 ▶ return if !$pid || ref $pid; # none, or the Windows tick (gone with the code's scope)
Branch: 1 branch
BranchTrueFalseTotal
if not $pid or ref $pid 2 4148 4150
Condition66%2/3
not $pid or ref $pid
l!l&&r!l&&!r
024148
MC/DC50%1/2
not $pid or ref $pid
not $pid ref $pid
Truth table66%2/3
not(A) or B
A= not $pidB= ref $pid
ABresult
1X1
011
000
567 4148 kill 'TERM', $pid;
568 4148 waitpid $pid, 0;
569 }
570  
571 413 1;
572  
573 __END__
« lib/Skilletor/Gzip.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Module/Runtime.pm »