ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/AnyEvent-MCP/lib/AnyEvent/MCP/Workdirs.pm
Revision: 1.6
Committed: Sun Nov 29 14:30:04 2026 UTC (2 days, 4 hours ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +1 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 use common::sense;
61 use AnyEvent::MCP ();
62
63 our @ISA = qw(AnyEvent::MCP::Base);
64
65 my $json = AnyEvent::MCP::json->utf8->canonical->pretty;
66
67 sub _read {
68 my ($self, $lock) = @_;
69
70 local $/;
71 my $text = <$lock>;
72 my $entries = length $text ? $json->decode ($text) : {};
73 ref $entries eq "HASH" or die "$self->{file}: expected workdir ID object\n";
74
75 for my $id (keys %$entries) {
76 my $entry = $entries->{$id};
77 $id =~ /\A[1-9][0-9]*\z/ && ref $entry eq "ARRAY" && @$entry == 2
78 && defined $entry->[0] && !ref $entry->[0] && length $entry->[0] && $entry->[0] !~ /\0/
79 && defined $entry->[1] && !ref $entry->[1] && $entry->[1] =~ /\A[0-9]+\z/
80 or die "$self->{file}: invalid workdir entry '$id'\n";
81 }
82
83 $entries
84 }
85
86 sub new {
87 my ($class, %kv) = @_;
88
89 my $self = $class->SUPER::new (expiry => 7 * 86400, %kv);
90 my $lock = AnyEvent::MCP::file_lock $self->{file};
91 my $entries = $self->_read ($lock);
92 my $now = time;
93 my %active = map { $_ => 1 } @{$self->{paths}};
94
95 for my $id (keys %$entries) {
96 delete $entries->{$id}
97 unless $active{$entries->{$id}[0]} || $entries->{$id}[1] > $now - $self->{expiry};
98 }
99
100 my %id = map { $entries->{$_}[0] => $_ } keys %$entries;
101 my @ids;
102 my $next = 1;
103
104 for my $path (@{$self->{paths}}) {
105 unless (exists $id{$path}) {
106 ++$next while exists $entries->{$next};
107 $id{$path} = $next++;
108 $entries->{$id{$path}} = [$path, $now];
109 }
110
111 push @ids, 0 + $id{$path};
112 }
113
114 # reserve allocations before another server can choose the same IDs.
115 AnyEvent::MCP::file_replace $self->{file}, $json->encode ($entries);
116 $self->{ids} = \@ids;
117
118 $self
119 }
120
121 sub refresh {
122 my ($self) = @_;
123
124 my $lock = AnyEvent::MCP::file_lock $self->{file};
125 my $entries = $self->_read ($lock);
126 my $now = time;
127
128 for (0 .. $#{$self->{paths}}) {
129 $entries->{$self->{ids}[$_]} = [$self->{paths}[$_], $now];
130 }
131
132 for my $id (keys %$entries) {
133 delete $entries->{$id} if $entries->{$id}[1] <= $now - $self->{expiry};
134 }
135
136 AnyEvent::MCP::file_replace $self->{file}, $json->encode ($entries);
137 }
138
139 1;