blib/lib/AnyEvent/MCP/Workdirs.pm

filter: Statementstmt 100% 55 / 55 Branchbran 92.8% 13 / 14 Conditioncond 73.3% 22 / 30 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 84.8% 101 / 119 CC 23 SCAR 22.9
CC
23
Cov
91%
_read
14.9
new
7.0
refresh
4.0
CRAP 9.9
« blib/lib/AnyEvent/MCP/Client/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/Client/JSONL.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Workdirs - persistent filesystem workdir IDs
4  
5 =head1 DESCRIPTION
6  
7 The registry assigns small positive integers to pathname strings.
8 C<mcp-fs> supplies logical absolute pathnames without following symlinks;
9 relative local workdir arguments are made absolute lexically. The same
10 logical pathname on two hosts gets the same ID even if its symlink resolves
11 to a different physical directory. IDs compress pathnames, not host
12 identities or permissions. The registry is shared by instances using the same file.
13 Permission-rule identities remain independent of these selectors.
14  
15 It is used by C<mcp-fs> so assistants can refer to a workdir by a short,
16 stable-while-retained number instead of repeating its pathname. It is
17 not a filesystem authorization mechanism: the selected workdir and the
18 permission engine still determine what an operation may access.
19 Applications that do not expose workdirs do not need this module.
20  
21 use AnyEvent::MCP::Workdirs;
22  
23 my $registry = AnyEvent::MCP::Workdirs->new (
24 file => $registry_file, paths => [$resolved_project_path],
25 );
26 my $id = $registry->{ids}[0];
27 $registry->refresh; # repeat periodically while the server runs
28  
29 See L<mcp-fs/SANDBOX GRANTS> for how the server interprets these IDs,
30 and L<AnyEvent::MCP/SMALL UTILITIES> for the locked state-file operations.
31  
32 =head1 METHODS
33  
34 =over
35  
36 =item $registry = AnyEvent::MCP::Workdirs->new (file => $file, paths => [$path...], expiry => $seconds)
37  
38 Resolve IDs for the supplied pathnames, reserving new allocations under
39 the file lock. C<expiry> defaults to seven days. C<ids> contains the assigned
40 IDs in path order and stays fixed for this object's lifetime. The caller
41 refreshes immediately and periodically while its server is running.
42  
43 =item $registry->refresh
44  
45 Lock and reread the JSON registry, overwrite this server's assigned IDs with
46 their paths and the current C<last_use>, expire old entries after C<expiry>
47 seconds, and atomically replace the file. This never resolves or reallocates
48 the server's IDs. Startup allocations reuse the smallest available positive
49 IDs; configured paths retain their previous IDs if nobody reused them.
50  
51 Entries are C<ID =E<gt> [workdir, last_use]>. The JSON uses UTF-8 with pretty printing.
52 Manual changes require restarting filesystem servers using the registry.
53  
54 =back
55  
56 =cut
57  
58 package AnyEvent::MCP::Workdirs;
59  
60 11 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 11 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62 11 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 11 our @ISA = qw(AnyEvent::MCP::Base);
65  
66 11 my $json = JSON::XS->new->utf8->canonical->pretty;
67  
68 22 ▶sub _read {
Subroutine _read: called CC=14 SCAR=27.0
69 22 my ($self, $lock) = @_;
70  
71 22 local $/;
72 22 my $text = <$lock>;
73 22 ▶ my $entries = length $text ? $json->decode ($text) : {};
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 15 7 22
74 22 ▶ ref $entries eq "HASH" or die "$self->{file}: expected workdir ID object\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $entries eq "HASH" or die qq[$self->{"file"}: expected workdir ID object\n] 21 1 22
75  
76 21 for my $id (keys %$entries) {
77 40 my $entry = $entries->{$id};
78 40 ▶ $id =~ /\A[1-9][0-9]*\z/ && ref $entry eq "ARRAY" && @$entry == 2
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u or die qq[$self->{"file"}: invalid workdir entry '${id}'\n] 37 3 40
Condition74%20/27
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
2137
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1]
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1]
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0]
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0]
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0]
!ll&&!rl&&r
2038
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2
!ll&&!rl&&r
1138
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY"
!ll&&!rl&&r
1039
MC/DC30%3/10
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u
$id =~ /\A[1-9][0-9]*\z/u ref $entry eq "ARRAY" @$entry == 2 defined $entry->[0] not ref $entry->[0] length $entry->[0] not $entry->[0] =~ /\0/u defined $entry->[1] not ref $entry->[1] $entry->[1] =~ /\A[0-9]+\z/u
Truth table36%4/11
A and B and C and D and not(E) and F and not(G) and H and not(I) and J
A= $id =~ /\A[1-9][0-9]*\z/uB= ref $entry eq "ARRAY"C= @$entry == 2D= defined $entry->[0]E= not ref $entry->[0]F= length $entry->[0]G= not $entry->[0] =~ /\0/uH= defined $entry->[1]I= not ref $entry->[1]J= $entry->[1] =~ /\A[0-9]+\z/u
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
79 && defined $entry->[0] && !ref $entry->[0] && length $entry->[0] && $entry->[0] !~ /\0/
80 && defined $entry->[1] && !ref $entry->[1] && $entry->[1] =~ /\A[0-9]+\z/
81 or die "$self->{file}: invalid workdir entry '$id'\n";
82 }
83  
84 $entries
85 18 }
86  
87 12 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
88 12 my ($class, %kv) = @_;
89  
90 12 my $self = $class->SUPER::new (expiry => 7 * 86400, %kv);
91 12 my $lock = AnyEvent::MCP::file_lock $self->{file};
92 12 my $entries = $self->_read ($lock);
93 12 my $now = time;
94 12 my %active = map { $_ => 1 } @{$self->{paths}};
95  
96 12 for my $id (keys %$entries) {
97 delete $entries->{$id}
98 26 ▶ unless $active{$entries->{$id}[0]} || $entries->{$id}[1] > $now - $self->{expiry};
Branch: 1 branch
BranchTrueFalseTotal
unless $active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"} 0 26 26
Condition66%2/3
$active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"}
l!l&&r!l&&!r
4220
MC/DC0%0/2
$active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"}
$active{$entries->{$id}[0]} $entries->{$id}[1] > $now - $self->{"expiry"}
Truth table66%2/3
A or B
A= $active{$entries->{$id}[0]}B= $entries->{$id}[1] > $now - $self->{"expiry"}
ABresult
1X1
011
000
99 }
100  
101 12 my %id = map { $entries->{$_}[0] => $_ } keys %$entries;
102 12 my @ids;
103 12 my $next = 1;
104  
105 12 for my $path (@{$self->{paths}}) {
106 16 ▶ unless (exists $id{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $id{$path}) 11 5 16
107 11 ▶ ++$next while exists $entries->{$next};
Branch: 1 branch
BranchTrueFalseTotal
exists $entries->{$next} and do { ++$next } 13 11 24
108 11 $id{$path} = $next++;
109 11 $entries->{$id{$path}} = [$path, $now];
110 }
111  
112 16 push @ids, 0 + $id{$path};
113 }
114  
115 # reserve allocations before another server can choose the same IDs.
116 12 AnyEvent::MCP::file_replace $self->{file}, $json->encode ($entries);
117 12 $self->{ids} = \@ids;
118  
119 12 $self
120 }
121  
122 10 ▶sub refresh {
Subroutine refresh: called CC=4 SCAR=13.9
Pod documented
123 10 my ($self) = @_;
124  
125 10 my $lock = AnyEvent::MCP::file_lock $self->{file};
126 10 my $entries = $self->_read ($lock);
127 6 my $now = time;
128  
129 6 for (0 .. $#{$self->{paths}}) {
130 9 $entries->{$self->{ids}[$_]} = [$self->{paths}[$_], $now];
131 }
132  
133 6 for my $id (keys %$entries) {
134 12 ▶ delete $entries->{$id} if $entries->{$id}[1] <= $now - $self->{expiry};
Branch: 1 branch
BranchTrueFalseTotal
if $entries->{$id}[1] <= $now - $self->{"expiry"} 1 11 12
135 }
136  
137 6 AnyEvent::MCP::file_replace $self->{file}, $json->encode ($entries);
138 }
139  
140 11 1;
« blib/lib/AnyEvent/MCP/Client/HTTP.pm ↑ Summary blib/lib/AnyEvent/MCP/Client/JSONL.pm »