AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/e10_fs.t  view on Meta::CPAN

   ok !$result->{isError}
      && $result->{content}[0]{text} eq "[range=1 truncated=1]"
      && MIME::Base64::decode_base64 ($result->{content}[1]{text})
         eq "1:" . ("X" x ((1 << 20) - 2)),
      "numbering counts prefix bytes against the 1 MiB limit";

}

$result = call "file-write" => { path => "new", data => MIME::Base64::encode_base64 ("hello\n", "") };
ok $result->{structuredContent}{bytes} == 6 && read_file ("$dir/work/new") eq "hello\n",
   "write receives module result";

# the write worker's different grants evict the cached read worker.
my $evicted = AnyEvent->condvar;
my $eviction_deadline = AnyEvent->timer (after => 30, cb => sub { die "Worker eviction timeout\n" });
my $eviction_poll = AnyEvent->timer (after => 0, interval => 0.01, cb => sub {
   $evicted->send unless kill 0 => $cached_pid;
});
$evicted->recv;
undef $eviction_poll;
undef $eviction_deadline;
ok !(kill 0 => $cached_pid), "evicting an incompatible cached worker terminates and reaps it";

SKIP: {
   skip "extended GNU find test (PERL_ANYEVENT_MCP_TESTS=1)", 3
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU find is unavailable", 3 unless $utility{find};

   $result = call list => {};
   ok $result->{content}[0]{text} =~ /\tnew\n/, "compact listing preserved";
   $result = call list => { path => "" };
   ok !$result->{isError} && $result->{content}[0]{text} =~ /\tnew\n/,
      "empty list path selects workdir";
   mkdir "$dir/work/0" or die $!;
   write_file "$dir/work/0/zero", "zero\n";
   $result = call list => { path => "0" };
   ok !$result->{isError} && $result->{content}[0]{text} =~ /\tzero\n/
      && $result->{content}[0]{text} !~ /\tnew\n/,
      "directory named 0 is not treated as empty";
}
SKIP: {
   skip "extended GNU find/grep tests (PERL_ANYEVENT_MCP_TESTS=1)", 9
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU find/grep are unavailable", 9 unless $utility{find} && $utility{grep};

   $result = call find => { name => "rea" };
   ok $prompts[-1]{tool} eq "find" && !exists $prompts[-1]{dimensions}{subject}{value}
      && !exists $prompts[-1]{subjects} && $prompts[-1]{paths}[0] eq "$dir/work"
      && $result->{content}[0]{text} =~ /\tread\n/,
      "filesystem prompts show affected paths without authorization scopes";
   $result = call find => { path => "", name => "rea" };
   ok !$result->{isError} && $result->{content}[0]{text} =~ /\tread\n/,
      "empty find path selects workdir";
   $result = call grep => { path => ".", pattern => "two" };
   ok $result->{structuredContent}{stdout} =~ /read:2:two/, "grep worker result";
   $result = call grep => { path => "", pattern => "two" };
   ok !$result->{isError} && $result->{structuredContent}{stdout} =~ /read:2:two/,
      "empty grep path selects workdir";
   $result = call grep => { path => "read", pattern => "absent-grep-pattern-2026" };
   ok !$result->{isError} && $result->{structuredContent}{stdout} eq ""
      && $result->{structuredContent}{stderr} eq "",
      "grep no match in one existing file is a successful empty result";
   $result = call grep => { path => ".", pattern => "absent-grep-pattern-2026" };
   ok !$result->{isError} && $result->{structuredContent}{stdout} eq "",
      "grep no matches in a directory is a successful empty result";
   $result = call grep => { path => "read", pattern => q{^absent-grep-pattern-2026$}, mode => "pcre" };
   ok !$result->{isError} && $result->{structuredContent}{stdout} eq "",
      "PCRE grep no matches is a successful empty result";
   $result = call grep => { path => "read", pattern => "[", mode => "pcre" };
   ok $result->{isError} && $result->{content}[0]{text} =~ /grep failed/,
      "malformed grep PCRE remains an error";
   $result = call grep => { path => "nonexistent-path", pattern => "two" };
   ok $result->{isError} && $result->{content}[0]{text} =~ /grep failed/,
      "find traversal failures remain errors";

}

my $before = @prompts;
$result = call "path-op" => { path => "new", rename => "renamed", reason => "Move the test file" };
ok !$result->{isError} && !-e "$dir/work/new" && -f "$dir/work/renamed", "rename succeeds with combined authority";
my $prompt_ok = @prompts == $before + 1 && join (",", @{$prompts[-1]{paths}}) eq "$dir/work/new,$dir/work/renamed";
ok $prompt_ok,
   "rename requests one approval displaying both affected paths";
my $reason_ok = $prompts[-1]{reason} eq "Move the test file\nPath: \"$dir/work/new\"\nOperations (in order):\n  rename: \"$dir/work/renamed\"";
ok $reason_ok,
   "rename prompt preserves the supplied reason and shows source, operation and resolved destination";
diag "rename: prompts=" . @prompts . ", expected=" . ($before + 1) . ", last=" . JSON::XS::encode_json ($prompts[-1])
   unless $prompt_ok && $reason_ok;
write_file "$dir/work/partial", "remove first\n";
$result = call "path-op" => { path => "partial", unlink => JSON::XS::true, chmod => "0600" };
ok $result->{isError} && !-e "$dir/work/partial",
   "path-op stops on a later error without rolling back successful unlink";
ok $prompts[-1]{reason} =~ /Operations \(in order\):\n  unlink\n  chmod: "0600"\z/,
   "path-op prompt shows every operation and argument in execution order";
$before = @prompts;
$result = call "path-op" => { path => "a/b", mkpath => "0700" };
ok -d "$dir/work/a/b" && @prompts == $before + 1, "mkpath requests combined authority once";
ok @{$prompts[-1]{paths}} == 1 && $prompts[-1]{paths}[0] eq "$dir/work/a/b",
   "mkpath authorizes the target without server-side ancestor probing";
$result = call "path-op" => { path => "deep/one/two", mkpath => "0755" };
ok !$result->{isError} && -d "$dir/work/deep/one/two"
   && ((stat "$dir/work/deep")[2] & 07777) == 0755
   && ((stat "$dir/work/deep/one/two")[2] & 07777) == 0755,
   "worker creates nested directories with the requested mode";
$result = call "path-op" => { path => "deep/one/two", mkpath => "0700" };
ok !$result->{isError} && ((stat "$dir/work/deep/one/two")[2] & 07777) == 0755,
   "mkpath leaves existing directories unchanged";
$result = call "path-op" => { path => "read/child", mkpath => "0700" };
ok $result->{isError} && !-e "$dir/work/read/child",
   "worker rejects a non-directory ancestor";

$before = @prompts;
$result = call stat => { paths => ["read", "renamed"] };
ok @{$result->{structuredContent}{result}} == 2 && @prompts == $before + 1,
   "multi-path stat requests combined authority once";
$before = @prompts;
$result = call "path-op" => { path => "renamed", rename => "$dir/home/moved" };
ok !$result->{isError} && !-e "$dir/work/renamed" && -f "$dir/home/moved",
   "rename executes across paths when the test sandbox permits it";
ok @prompts == $before + 1
   && join (",", @{$prompts[-1]{sandbox_grants}}) eq "homedir-write,workdir-write",

t/e10_fs.t  view on Meta::CPAN

$reduce = 0;
$deny = 1;
$result = call "path-op" => { path => "reduced", unlink => JSON::XS::true };
ok $result->{isError} && -f "$dir/work/reduced", "denial leaves file intact";
$deny = 0;

SKIP: {
   skip "extended GNU patch tests (PERL_ANYEVENT_MCP_TESTS=1)", 10
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU patch is unavailable", 10 unless $utility{patch};

   $result = call "file-patch" => { path => "read", diff => "@@ -2 @@\n-two\n+TWO\n" };
   ok $result->{structuredContent}{bytes} == 14 && read_file ("$dir/work/read") eq "one\nTWO\nthree\n",
      "shared diff normalization and standalone worker validation";

   write_file "$dir/work/unicode", "plain\n";
   $result = call "file-patch" => { path => "unicode", diff => "@@ -1 @@\n-plain\n+caf\x{e9}\n" };
   ok !$result->{isError} && read_file ("$dir/work/unicode") eq "caf\xc3\xa9\n",
      "Unicode unified diff is encoded as UTF-8 bytes";
   ok $prompts[-1]{details}{"View patch"} eq "File: $dir/work/unicode\n\n@@ -1 @@\n-plain\n+caf\x{e9}\n",
      "patch details include the target and original Unicode diff";

   write_file "$dir/work/binary", "a\0\xff\n";
   my $binary_diff = "@@ -1 @@\n-a\0\xff\n+b\0\xfe\n";
   $result = call "file-patch" => { path => "binary", diff => MIME::Base64::encode_base64 ($binary_diff, "") };
   ok !$result->{isError} && read_file ("$dir/work/binary") eq "b\0\xfe\n",
      "base64 binary diff patches NULs and non-UTF-8 bytes";
   ok $prompts[-1]{details}{"View patch"} =~ /Raw patch: .* bytes.*-a\\x\{00\}\\x\{FF\}\n\+b\\x\{00\}\\x\{FE\}/s,
      "base64 binary patch details show decoded bytes with explicit escaping";

   write_file "$dir/work/base64-unicode", "plain\n";
   $result = call "file-patch" => { path => "base64-unicode",
      diff => MIME::Base64::encode_base64 ("@@ -1 @@\n-plain\n+caf\xc3\xa9\n", "") };
   ok !$result->{isError} && $prompts[-1]{details}{"View patch"} =~ /\+caf\x{e9}\n/,
      "valid UTF-8 base64 patches have readable Unicode details";

   $result = call "file-patch" => { path => "binary", diff => "not-base64!" };
   ok $result->{isError} && read_file ("$dir/work/binary") eq "b\0\xfe\n",
      "invalid base64 diff rejected without modifying file";

   $result = call "file-patch" => { path => "read", diff => "@@ -20 @@\n-TWO\n+bad\n" };
   ok $result->{structuredContent}{error} =~ /offset/ && read_file ("$dir/work/read") eq "one\nTWO\nthree\n",
      "excessive patch relocation leaves original intact";
   write_file "$dir/work/stale", "one\ntwo\n";
   $result = call "file-patch" => { path => "stale", diff => "@@ -2 @@\n-two\n+TWO\n" };
   ok $result->{structuredContent}{error} =~ /changed/ && read_file ("$dir/work/stale") eq "one\ntwo\nchanged\n",
      "concurrent edit prevents patch replacement";
   ok !glob ("$dir/work/*~~new~~*"), "failed patch temporary files are removed";

}

# output selection is shared by arbitrary exec and fixed VCS operations.
my $output_argv = [$^X, "-e", 'print "one\nkeep two\nkeep three\nlast"; print STDERR "unfiltered\n"; exit 7'];
SKIP: {
   skip "extended GNU grep output tests (PERL_ANYEVENT_MCP_TESTS=1)", 4
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU grep is unavailable", 4 unless $utility{grep};

   $result = call exec => { argv => $output_argv, reason => "filter output", grep => "^keep", max_lines => 1 };
   ok $result->{structuredContent}{stdout} eq "keep two\n"
      && $result->{structuredContent}{stderr} eq "unfiltered\n", "exec filters stdout only before the line limit";
   ok $result->{structuredContent}{exit} == 7 && $result->{structuredContent}{stdout_truncated}
      && !$result->{structuredContent}{capture_truncated}, "postfilter limits preserve original exit status and capture completeness";
   my $output_subject = $prompts[-1]{dimensions}{subject}{value};
   $result = call exec => { argv => $output_argv, reason => "different output filter", grep => "^absent" };
   ok $result->{structuredContent}{stdout} eq "" && $result->{structuredContent}{exit} == 7,
      "no grep matches is distinct from command success";
   ok $prompts[-1]{dimensions}{subject}{value} eq $output_subject, "output options do not change the executable permission subject";
}

$result = call exec => { argv => [$^X, "-e", 'print "abcdef"; print STDERR "uvwxyz"'],
   reason => "independent byte budgets", max_bytes => 3 };
my $valid = $result->{structuredContent}{stdout} eq "abc" && $result->{structuredContent}{stderr} eq "uvw"
   && $result->{structuredContent}{stdout_truncated} && $result->{structuredContent}{stderr_truncated};
ok $valid,
   "stdout and stderr have independent return budgets";
diag JSON::XS::encode_json $result unless $valid;
$result = call exec => { argv => [$^X, "-e", 'binmode STDOUT; print "\xc3\xa4\xc3\xa4"'],
   reason => "UTF-8 clipping", max_bytes => 3 };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "\x{e4}" && $result->{structuredContent}{truncated},
   "byte clipping preserves a complete UTF-8 prefix";
SKIP: {
   skip "extended GNU grep output tests (PERL_ANYEVENT_MCP_TESTS=1)", 2
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU grep is unavailable", 2 unless $utility{grep};

   $result = call exec => { argv => [$^X, "-e", 'print "x" x 1100000; print STDERR "visible\n"; exit 9'],
      reason => "bounded capture before filtering", grep => "^absent" };
   ok $result->{structuredContent}{stdout} eq "" && $result->{structuredContent}{stderr} eq "visible\n"
      && $result->{structuredContent}{capture_truncated} && $result->{structuredContent}{exit} == 9,
      "capture truncation is explicit even when no captured line matches";
   $result = call exec => { argv => $output_argv, reason => "invalid filter", grep => "(" };
   ok $result->{isError} && $result->{structuredContent}{filter_error}
      && $result->{structuredContent}{exit} == 7, "filter failure retains the original command status";
}

my $before_output = @prompts;
$result = call exec => { argv => $output_argv, reason => "invalid limit", max_bytes => 0 };
ok $result->{isError} && @prompts == $before_output, "invalid output limits fail before permission or execution";

for my $backend (qw(git svn cvs hg)) {
   $result = call "vcs-diff" => { backend => $backend, files => ["-strange"] };
   ok !$result->{isError} && $result->{structuredContent}{exit} == 1
      && $result->{structuredContent}{stdout} eq "header\nkeep: $backend\ntrailer\n", "$backend diff preserves differences status and output";
   ok $prompts[-1]{action} eq "vcs-diff" && $prompts[-1]{dimensions}{subject}{value} eq $backend,
      "$backend diff uses a fixed-operation permission instead of arbitrary exec";
   ok join (",", sort @{$prompts[-1]{sandbox_grants}}) eq
      ($backend eq "cvs" ? "ssh-agent,workdir-read" : "workdir-read"),
      "$backend diff requests its fixed sandbox grants";
   my @log = split /\n/, read_file "$dir/vcs.log";
   my $argv = JSON::XS::decode_json $log[-1];
   ok $argv->[-1] eq ($backend eq "svn" ? "./-strange@" : "./-strange")
      && grep ($_ eq "--", @$argv), "$backend filenames cannot become options";
   $result = call "vcs-status" => { backend => $backend, max_lines => 1 };
   ok !$result->{isError} && $result->{structuredContent}{exit} == 0
      && $result->{structuredContent}{stdout} eq "header\n", "$backend status shares output limits";

   $result = call "vcs-log" => { backend => $backend, files => ["-strange"], max_lines => 2 };
   ok !$result->{isError} && $result->{structuredContent}{exit} == 0
      && $result->{structuredContent}{stdout} eq "header\nkeep: $backend\n"
      && $result->{structuredContent}{stdout_truncated}, "$backend log shares bounded output";
   ok $prompts[-1]{action} eq "vcs-log"
      && $prompts[-1]{dimensions}{subject}{value} eq $backend,
      "$backend log has its own backend-scoped permission action";
   ok join (",", sort @{$prompts[-1]{sandbox_grants}}) eq
      (($backend eq "cvs" || $backend eq "svn") ? "ssh-agent,workdir-read" : "workdir-read"),
      "$backend log requests appropriate network/SSH authority";
   @log = split /\n/, read_file "$dir/vcs.log";
   $argv = JSON::XS::decode_json $log[-1];
   ok $argv->[-1] eq ($backend eq "svn" ? "./-strange@" : "./-strange")
      && grep ($_ eq "--", @$argv), "$backend log file path remains a literal argument";
   ok (($backend eq "git" && grep ($_ eq "--no-decorate", @$argv))
      || ($backend eq "cvs" && grep ($_ eq "-N", @$argv))
      || ($backend eq "svn" && grep ($_ eq "--no-auth-cache", @$argv))
      || ($backend eq "hg" && grep ($_ eq "log", @$argv))),
      "$backend log uses fixed backend options";
   SKIP: {
      skip "extended GNU grep VCS test (PERL_ANYEVENT_MCP_TESTS=1)", 1
         unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
      skip "GNU grep is unavailable", 1 unless $utility{grep};

      $result = call "vcs-diff" => { backend => $backend, grep => "^keep" };
      ok !$result->{isError} && $result->{structuredContent}{exit} == 1
         && $result->{structuredContent}{stdout} eq "keep: $backend\n", "$backend diff shares postfiltering";
   }

}
File::Path::mkpath ("$dir/work/sub", 0, 0700);
for my $backend (qw(git svn cvs hg)) {
   $result = call "vcs-diff" => { backend => $backend, cwd => "sub", files => ["../-strange"] };
   my @log = split /\n/, read_file "$dir/vcs.log";
   my $argv = JSON::XS::decode_json $log[-1];
   ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/sub"
      && $argv->[-1] eq ($backend eq "svn" ? "./../-strange@" : "./../-strange")
      && $prompts[-1]{reason} =~ /\Q$dir\/work\/sub\E/,
      "$backend runs in cwd with relative filenames and displays command directory";
}
my $unicode_cwd = "dir-\x{e4}\x{1f431}";
my $unicode_file = "file-\x{e4}\x{1f431}";
my ($cwd_bytes, $file_bytes) = ($unicode_cwd, "./$unicode_file");
utf8::encode $_ for $cwd_bytes, $file_bytes;
mkdir "$dir/work/$cwd_bytes" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => $unicode_cwd, files => [$unicode_file] };
my @unicode_log = split /\n/, read_file "$dir/vcs.log";
my $unicode_argv = JSON::XS::decode_json $unicode_log[-1];
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/$cwd_bytes",
   "VCS wrapper encodes Unicode working directory before chdir";
ok $unicode_argv->[-1] eq $file_bytes,
   "VCS pathspec is passed as UTF-8 bytes alongside binary executable path";

ok $tool{"vcs-diff"}{inputSchema}{properties}{cwd}{default} eq "."
   && $tool{"vcs-status"}{inputSchema}{properties}{cwd}{default} eq "."
   && $tool{"vcs-log"}{inputSchema}{properties}{cwd}{default} eq ".",
   "all VCS tools advertise cwd default";
$result = call "vcs-status" => { backend => "git", cwd => "" };
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work",
   "empty VCS cwd selects workdir";
my $before_vcs = read_file "$dir/vcs.log";
$result = call "vcs-status" => { backend => "git", files => [""] };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
   "empty VCS filename remains invalid";
$result = call "vcs-status" => { backend => "git", cwd => "../home" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
   "lexical cwd escape is rejected before execution";
symlink "$dir/home", "$dir/work/external-cwd" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => "external-cwd" };
ok $result->{structuredContent}{exit} != 0 && read_file ("$dir/vcs.log") eq $before_vcs
   && $result->{structuredContent}{stderr} =~ /cwd must resolve within/,
   "worker rejects resolved cwd escape before starting VCS";
symlink "sub", "$dir/work/internal-cwd" or die $!;
$result = call "vcs-status" => { backend => "git", cwd => "internal-cwd" };
ok !$result->{isError} && read_file ("$dir/vcs-cwd.log") eq "$dir/work/sub",
   "internal cwd symlink is allowed";
$before_vcs = read_file "$dir/vcs.log";
$result = call "vcs-status" => { backend => "git", cwd => "missing-cwd" };
ok $result->{structuredContent}{exit} != 0 && read_file ("$dir/vcs.log") eq $before_vcs,
   "missing cwd cannot start VCS";
$result = call "vcs-diff" => { backend => "cvs", files => ["../outside"] };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs, "VCS paths cannot escape the selected workdir";
$reduce = 1;
$result = call "vcs-status" => { backend => "cvs" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs, "reduced approval cannot omit CVS agent/network authority";
$result = call "vcs-log" => { backend => "svn" };
ok $result->{isError} && read_file ("$dir/vcs.log") eq $before_vcs,
   "reduced approval cannot omit SVN log agent/network authority";
$reduce = 0;

$result = call exec => { argv => [$^X, "-e", 'print "worker\\n"'], reason => "test worker" };
ok $result->{structuredContent}{stdout} eq "worker\n" && !$result->{isError}, "arbitrary exec uses module permissions";
ok !exists $prompts[-1]{details}, "exec without stdin offers no viewer details";
ok $tool{exec}{inputSchema}{properties}{stdin_encoding}{enum}[2] eq "base64", "exec advertises stdin encoding choices";
my $short_stdin = "print qq(hello from stdin\n);\n";
$result = call exec => { argv => [$^X, "-"], stdin => $short_stdin,
   reason => "inspect stdin test" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "hello from stdin\n"
   && $prompts[-1]{details}{"View stdin"} eq $short_stdin,
   "real exec passes original source to display-only details and runs it unchanged";
my $large_stdin = "x" x 65537;
$result = call exec => { argv => [$^X, "-e", 'local $/; my $s = <STDIN>; print length $s'],
   stdin => $large_stdin, reason => "inspect long input" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "65537"
   && $prompts[-1]{details}{"View stdin"} eq $large_stdin,
   "long exec input reaches both the worker and display-only details intact";

my $hex_stdin = [$^X, "-e", 'binmode STDIN; local $/; print unpack "H*", <STDIN>'];
$result = call exec => { argv => $hex_stdin, stdin => "\x{e9}", reason => "UTF-8 input" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "c3a9",
   "exec stdin defaults to UTF-8 bytes";
$result = call exec => { argv => $hex_stdin, stdin => "\x{e9}\x{ff}\0",
   stdin_encoding => "latin1", reason => "Latin-1 input" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "e9ff00"
   && $prompts[-1]{details}{"View stdin"} =~ /latin1 stdin: 3 bytes.*\\x\{E9\}\\x\{FF\}\\x\{00\}/s,
   "Latin-1 converts Unicode code points losslessly and displays escaped bytes";
my $binary = pack "C*", 0, 255, 10, 92, 65;
$result = call exec => { argv => $hex_stdin, stdin => MIME::Base64::encode_base64 ($binary, ""),
   stdin_encoding => "base64", reason => "binary input" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "00ff0a5c41"
   && $prompts[-1]{details}{"View stdin"} =~ /base64 stdin: 5 bytes.*\\x\{00\}\\x\{FF\}\\x\{0A\}\\x\{5C\}A/s,
   "Base64 decodes exact bytes; preview escapes control bytes and backslash";

my $prompts_before = @prompts;
my $sandbox_before = read_file "$dir/sandbox.log";
for my $invalid (
   { stdin_encoding => "latin1", stdin => "\x{100}" },
   { stdin_encoding => "base64", stdin => "!!!" },
   { stdin_encoding => "base64", stdin => "YQ==\n" },
   { stdin_encoding => "something", stdin => "abc" },
) {
   $result = call exec => { argv => $hex_stdin, %$invalid, reason => "reject bad stdin" };
   ok $result->{isError} && @prompts == $prompts_before
      && read_file ("$dir/sandbox.log") eq $sandbox_before,
      "invalid stdin encoding/data rejected before permission and worker";
}

my $preview_input = "\x00" x 4100;
$result = call exec => { argv => [$^X, "-e", 'binmode STDIN; local $/; print length <STDIN>'],
   stdin => MIME::Base64::encode_base64 ($preview_input, ""), stdin_encoding => "base64",
   reason => "bounded preview" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "4100"
   && $prompts[-1]{details}{"View stdin"} =~ /4100 bytes.*limited to 4096 bytes/s
   && length ($prompts[-1]{details}{"View stdin"}) < 25000,
   "binary stdin preview is bounded without truncating worker input";

my $max_stdin = MIME::Base64::encode_base64 ("x" x (4 * 1024 * 1024), "");
$result = call exec => { argv => [$^X, "-e", 'binmode STDIN; local $/; print length <STDIN>'],
   stdin => $max_stdin, stdin_encoding => "base64", reason => "exact 4 MiB stdin" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq (4 * 1024 * 1024),
   "maximum decoded stdin fits through the increased MCP message limit";

my $too_large_stdin = MIME::Base64::encode_base64 ("x" x (4 * 1024 * 1024 + 1), "");
$prompts_before = @prompts;
$result = call exec => { argv => $hex_stdin, stdin => $too_large_stdin,
   stdin_encoding => "base64", reason => "reject decoded oversize" };
ok $result->{isError} && @prompts == $prompts_before
   && $result->{content}[0]{text} =~ /stdin exceeds 4 MiB/,
   "stdin size limit applies to decoded bytes before permission";

$result = call exec => { argv => [$^X, "-e", 'print "diff\n"; warn "message\n"; exit 1'], reason => "nonzero is data" };
$valid = !$result->{isError} && $result->{structuredContent}{exit} == 1
   && $result->{structuredContent}{stdout} eq "diff\n"
   && $result->{structuredContent}{stderr} eq "message\n";
ok $valid,
   "nonzero command exit returns ordinary structured data";
diag JSON::XS::encode_json $result unless $valid;
$result = call exec => {
   argv => [$^X, "-e", q{
      defined (my $pid = fork) or die $!;
      exit 7 if $pid;
      select undef, undef, undef, 0.1;
      print "late stdout\n";
      print STDERR "late stderr\n";
   }],
   reason => "test output after child exit",
};
$valid = !$result->{isError} && $result->{structuredContent}{exit} == 7
   && $result->{structuredContent}{stdout} eq "late stdout\n"
   && $result->{structuredContent}{stderr} eq "late stderr\n";
ok $valid,
   "completion waits for both output streams after the child exits";
diag JSON::XS::encode_json $result unless $valid;

$result = call exec => {
   argv => [$^X, "-e", q{
      close STDIN;
      close STDOUT;
      close STDERR;
      select undef, undef, undef, 0.1;
      exit 9;
   }],
   reason => "test child exit after output closes",
};
$valid = !$result->{isError} && $result->{structuredContent}{exit} == 9
   && $result->{structuredContent}{stdout} eq ""
   && $result->{structuredContent}{stderr} eq "";
ok $valid,
   "completion waits for child status after both output streams close";
diag JSON::XS::encode_json $result unless $valid;

$result = call exec => { argv => [$^X, "-e", 'kill TERM => $$'], reason => "signal is data" };
ok !$result->{isError} && $result->{structuredContent}{signal} == 15,
   "signal termination returns an ordinary process result";
SKIP: {
   skip "extended command timeout test (PERL_ANYEVENT_MCP_TESTS=1)", 1
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;

   $result = call exec => {
      argv => [$^X, "-e", '$| = 1; print "before timeout\n"; print STDERR "before timeout\n"; sleep 30'],
      timeout => 1, reason => "test timeout abandons output",
   };
   ok !$result->{isError} && $result->{structuredContent}{timed_out}
      && $result->{structuredContent}{truncated}
      && $result->{structuredContent}{exit} == 0
      && $result->{structuredContent}{signal} == 9
      && $result->{structuredContent}{stdout} eq "before timeout\n"
      && $result->{structuredContent}{stderr} eq "before timeout\n",
      "timeout returns captured output as truncated with the real child status";

}

my $sandbox = read_file "$dir/sandbox.log";
ok $sandbox =~ /--unshare-net/ && $sandbox =~ /--ro-bind/ && $sandbox =~ /--disable-userns/
   && $sandbox !~ /\/run\/anyevent-mcp\.pm/,
   "sandbox retains isolation flags without mounting application source";
ok $sandbox =~ /"--setenv","PERL_BADLANG","0"/,
   "sandbox suppresses Perl warnings if the requested UTF-8 locale is unavailable";
ok $sandbox =~ /"--file","3","\/etc\/passwd"/, "sandbox supplies passwd via inherited descriptor";
ok read_file ("$dir/passwd.log") =~ /\Asandbox:x:\d+:\d+:Sandbox:\Q$dir\/home\E:\/bin\/sh\n\z/,
   "synthetic passwd exposes user identity and configured HOME only";

$result = call exec => { argv => [$^X, "-e", "print 300"], timeout => 300, reason => "test normal timeout limit" };
ok !$result->{isError} && !grep ($_ eq "long-running", @{$prompts[-1]{sandbox_grants}}),
   "normal timeout limit needs no long-running authority";
$sandbox = read_file "$dir/sandbox.log";
$deny = 1;
$result = call exec => { argv => [$^X, "-e", "print 301"], timeout => 301, reason => "test denied long timeout" };
ok $result->{isError} && read_file ("$dir/sandbox.log") eq $sandbox,
   "denied long timeout never starts a worker";
ok join (",", sort @{$prompts[-1]{sandbox_grants}}) eq "long-running,workdir-read",
   "long timeout automatically requests authority alongside sandbox sandbox_grants";
$deny = 0;
$result = call exec => {
   argv => [$^X, "-e", "print 21600"], sandbox_grants => [], timeout => 21600, reason => "test six-hour limit",
};
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "21600"
   && join (",", @{$prompts[-1]{sandbox_grants}}) eq "long-running",
   "six-hour timeout is accepted with long-running authority and a minimal sandbox";
$sandbox = read_file "$dir/sandbox.log";
$reduce = 1;
$result = call exec => { argv => [$^X, "-e", "print 301"], timeout => 301, reason => "test reduced long timeout" };
ok $result->{isError} && $result->{content}[0]{text} =~ /reduced below long-running/
   && read_file ("$dir/sandbox.log") eq $sandbox,
   "approval without long-running authority never starts a worker";
$reduce = 0;
my $prompt_count = @prompts;
$result = call exec => { argv => [$^X, "-e", "print 21601"], timeout => 21601, reason => "test excessive timeout" };
ok $result->{isError} && @prompts == $prompt_count && read_file ("$dir/sandbox.log") eq $sandbox,
   "timeout above six hours is rejected before asking or starting a worker";
my $cancel_cv = AnyEvent->condvar;
my $running = $client->call (exec => {
   argv => [$^X, "-e", 'open my $fh, ">", $ARGV[0] or die $!; print $fh $$; close $fh; sleep 30', "$dir/work/pid"],
   timeout => 21600, reason => "test cancellation with long-running authority",
}, sub { $cancel_cv->send ([@_]) });
my $ready = AnyEvent->condvar;
my $deadline = AnyEvent->timer (after => 30, cb => sub { die "Worker startup timeout\n" });
my $poll = AnyEvent->timer (after => 0, interval => 0.01, cb => sub {
   $ready->send if -s "$dir/work/pid";
});
$ready->recv;
undef $poll;
undef $deadline;
my $pid = read_file "$dir/work/pid";
$client->cancel ($running);
my $cancel = $cancel_cv->recv;
ok !$cancel->[0] && $cancel->[1]{cancelled}, "cancelled tool completes locally once";
$ready = AnyEvent->condvar;

t/e10_fs.t  view on Meta::CPAN

   my $new = \&AnyEvent::MCP::Workdirs::new;
   *AnyEvent::MCP::Workdirs::new = sub {
      my ($class, %kv) = @_;
      $new->($class, %kv, expiry => 1)
   };
   $0 = "bin/mcp-fs";
   do "./bin/mcp-fs";
   die $@ if $@;
};
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [$^X, "-Ilib", "-e", $timer_server, "--", @command[3 .. $#command]], env => \%env,
);
$result = call workdirs => {};
my $registry_file = "$dir/config/anyevent-mcp/fs-workdir-ids.json";
my $registry = JSON::XS::decode_json read_file $registry_file;
my $registered = time;
$registry->{1}[1] = 1;

{
   my $lock = AnyEvent::MCP::file_lock $registry_file;
   AnyEvent::MCP::file_replace $registry_file, JSON::XS::encode_json ($registry);
}

wait_for (sub {
   my ($done) = @_;
   my $timer; $timer = AE::timer 1, 0, sub {
      undef $timer;
      $done->();
   };
});
$registry = JSON::XS::decode_json read_file $registry_file;
ok $registry->{1}[1] >= $registered,
   "running server timer refreshes its workdir without tool use";
$client->close;

splice @command, 4, 2; # Legacy calls use the default local helper.
$client = AnyEvent::MCP::Client::JSONL->new (
   command => \@command, env => \%env, versions => ["2025-11-25"],
);
$result = call "file-write" => { path => "legacy", data => MIME::Base64::encode_base64 ("legacy", "") };
ok $result->{structuredContent}{bytes} == 6 && -f "$dir/work/legacy", "legacy client uses external asking helper";
$client->close;

# Unsandboxed tools must request the authority they actually have, not a
# misleading workdir-only grant. Reduced grants prevent worker startup.
my (@direct_requests, $full_grant);
my $direct_answer = sub {
   my ($client, $req) = @_;
   my $request = $req->{params}{_meta}{"anyevent-mcp/permission"}{request};
   push @direct_requests, $request;
   $req->reply ({
      action => "accept", content => {
         decision => "allow-once",
         sandbox_grants => join ",", @{$full_grant ? $request->{sandbox_grants} : ["workdir-write"]},
      },
   });
};
my @captured_server = (
   $^X, "-Ilib", "-e",
   'open STDERR, ">>", shift @ARGV or die $!; do shift @ARGV; die $@ if $@;',
   "$dir/unsandboxed.stderr", "$root/bin/mcp-fs", "--name", "default", "--quiet",
);
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [@captured_server, "--stdio", "--ask", "mcp",
      "--sandbox=none", "--bwrap", "$dir/nonexistent-bwrap", "$dir/work"],
   env => \%env, client_capabilities => { elicitation => { form => {} } },
   on_request => $direct_answer,
);
$result = call "file-write" => { path => "direct-test", data => MIME::Base64::encode_base64 ("ok", "") };
ok $result->{isError} && !-e "$dir/work/direct-test",
   "unsandboxed file-write rejects insufficient sandbox grants";
my %requested = map { $_ => 1 } @{$direct_requests[-1]{sandbox_grants}};
ok $requested{"world-write"} && $requested{network} && $requested{"shared-tmp"}
   && $requested{"host-proc"} && $direct_requests[-1]{sandbox_implementation} =~ /UNSANDBOXED/,
   "unsandboxed permission prompt shows full host authority and implementation";
$full_grant = 1;
$result = call "file-write" => { path => "direct-test", data => MIME::Base64::encode_base64 ("ok", "") };
ok !$result->{isError} && read_file ("$dir/work/direct-test") eq "ok",
   "unsandboxed write executes only after approval of full authority";
$full_grant = 0;
my $direct_argv = [$^X, "-e", q{
   open my $fh, ">", "direct-exec-ran" or die $!;
   print {$fh} "ran";
   print "direct";
}];
$result = call exec => { argv => $direct_argv, sandbox_grants => [], reason => "direct test" };
ok $result->{isError} && !-e "$dir/work/direct-exec-ran",
   "explicitly minimal exec grants cannot narrow unsandboxed execution or start the command";
$full_grant = 1;
$result = call exec => { argv => $direct_argv, sandbox_grants => [], reason => "direct test" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "direct" && -f "$dir/work/direct-exec-ran",
   "unsandboxed exec runs after full approval even with a narrow client request";
$client->close;

# Persist exactly this command with the full authority direct execution receives.
my @saved_command = (
   $^X, "-Ilib", "-e",
   'open STDERR, ">>", shift @ARGV or die $!; do shift @ARGV; die $@ if $@;',
   "$dir/saved.stderr", "$root/bin/mcp-fs", "--name", "saved-max",
   "--stdio", "--ask", "mcp", "--sandbox=none", "$dir/work",
);
my $saved_argv = [$^X, "-e", 'open my $fh, ">", $ARGV[0] or die $!; print {$fh} "saved"; print "saved"', "$dir/work/saved-max"];
my @save_stages;
$client = AnyEvent::MCP::Client::JSONL->new (
   command => \@saved_command, env => \%env,
   client_capabilities => { elicitation => { form => {} } },
   on_request => sub {
      my ($client, $req) = @_;
      my $meta = $req->{params}{_meta}{"anyevent-mcp/permission"};
      push @save_stages, $meta->{stage};
      my $request = $meta->{request};
      $req->reply ({
         action => "accept", content => {
            decision => "allow-always",
            ($meta->{stage} eq "rule" ? (
               sandbox_grants => join (",", @{$request->{sandbox_grants}}),
               dim_subject => $request->{dimensions}{subject}{choices}[0]{match},
               dim_host => $request->{dimensions}{host}{choices}[0]{match},
            ) : ()),
         },
      });
   },
);
$result = call exec => { argv => $saved_argv, sandbox_grants => [], reason => "save maximal direct grant" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "saved"
   && read_file ("$dir/work/saved-max") eq "saved", "maximal direct grant executes the approved command";
is_deeply \@save_stages, ["choice", "rule"], "persistent direct approval uses both protected forms";
$client->close;
my $saved_rule = read_file "$dir/config/anyevent-mcp/rules/fs-saved-max.ini";
for my $grant (qw(world-write network shared-tmp host-proc)) {
   like $saved_rule, qr/^sandbox-grants\s*=.*\b\Q$grant\E\b/m, "saved direct grant retains $grant";
}
unlink "$dir/work/saved-max" or die $!;
$client = AnyEvent::MCP::Client::JSONL->new (
   command => \@saved_command, env => \%env,
   client_capabilities => { elicitation => { form => {} } },
   on_request => sub { die "Saved maximal grant unexpectedly prompted\n" },
);
$result = call exec => { argv => $saved_argv, sandbox_grants => [], reason => "reuse maximal direct grant" };
ok !$result->{isError} && $result->{structuredContent}{stdout} eq "saved"
   && read_file ("$dir/work/saved-max") eq "saved", "restarted server reuses the saved maximal direct grant without prompting";
$client->close;

# An existing bubblewrap executable that fails must never cause direct fallback.
write_file "$dir/failing-bwrap", "#!$^X\n" . q{
open my $fh, ">>", "$ENV{PERL_ANYEVENT_MCP_TEST_FS_DIR}/bwrap-failures" or die $!;
print {$fh} "attempt\n";
close $fh or die $!;
print STDERR "deliberate bubblewrap failure\n";
exit 42;
};
chmod 0700, "$dir/failing-bwrap" or die $!;
for my $mode (qw(bwrap auto)) {
   $client = AnyEvent::MCP::Client::JSONL->new (
      command => [$^X, "-e",
         'open STDERR, ">", shift @ARGV or die $!; exec {$ARGV[0]} @ARGV; die $!;',
         "$dir/failing-startup-$mode", $^X, "-Ilib", "bin/mcp-fs", "--stdio", "--ask", "mcp",
         "--sandbox=$mode", "--bwrap", "$dir/failing-bwrap", "$dir/work"],
      env => \%env, client_capabilities => { elicitation => { form => {} } },
      on_request => $direct_answer,
   );
   my ($listed) = wait_for (sub { $client->tools ($_[0]) });
   ok !$listed && !-e "$dir/work/fallback-exec-$mode",
      "$mode fails startup when sandbox Perl cannot run, without direct fallback";
   my $stderr = read_file "$dir/failing-startup-$mode";
   like $stderr, qr/\Adeliberate bubblewrap failure\nPerl 'perl' cannot run in the worker environment\n\z/,
      "$mode captures and checks the expected startup diagnostics without printing them";
   $client->close;
}

$client = AnyEvent::MCP::Client::JSONL->new (
   command => [@captured_server, "--stdio", "--ask", "mcp",
      "--sandbox=auto", "--bwrap", "$dir/nonexistent-bwrap", "$dir/work"],
   env => \%env, client_capabilities => { elicitation => { form => {} } },
   on_request => $direct_answer,
);
$result = call "file-write" => { path => "auto-test", data => MIME::Base64::encode_base64 ("auto", "") };
ok !$result->{isError} && read_file ("$dir/work/auto-test") eq "auto"
   && $direct_requests[-1]{sandbox_implementation} =~ /UNSANDBOXED/,
   "auto mode without bubblewrap requests full authority and runs directly";
$client->close;
my $unsandboxed_stderr = read_file "$dir/unsandboxed.stderr";
unlike $unsandboxed_stderr, qr/SANDBOX NONE|bubblewrap unavailable/,
   "explicit none and default-filtered auto modes produce no sandbox warning";

my @warn_server = @captured_server;
$warn_server[4] = "$dir/auto-warning.stderr";
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [@warn_server, "--stdio", "--ask", "mcp",
      "--sandbox=auto", "--bwrap", "$dir/nonexistent-bwrap", "$dir/work"],
   env => { %env, PERL_ANYEVENT_LOG => "filter=warn log=warn" },
   client_capabilities => { elicitation => { form => {} } },
   on_request => $direct_answer,
);
$result = call workdirs => {};
$client->close;
like read_file ("$dir/auto-warning.stderr"), qr/warn .*bubblewrap unavailable, running without sandbox/,
   "auto fallback emits a warning when logging warnings is enabled";

my $old_sandbox_log = read_file "$dir/sandbox.log";
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [$^X, "-Ilib", "bin/mcp-fs", "--stdio", "--ask", "mcp",
      "--sandbox=auto", "--bwrap", "$dir/bwrap", "$dir/work"],
   env => \%env, client_capabilities => { elicitation => { form => {} } },
   on_request => $direct_answer,
);
$result = call "file-read" => { path => "auto-test" };
ok !$result->{isError} && MIME::Base64::decode_base64 ($result->{structuredContent}{data}) eq "auto"
   && length (read_file "$dir/sandbox.log") > length $old_sandbox_log,
   "auto mode selects bubblewrap when present";
my $request_count = @direct_requests;
$result = call "file-write" => { path => "auto-write-test", data => MIME::Base64::encode_base64 ("ok", "") };
ok !$result->{isError} && @direct_requests == $request_count + 1
   && $direct_requests[-1]{sandbox_implementation} eq "bubblewrap"
   && !(grep $_ eq "world-write", @{$direct_requests[-1]{sandbox_grants}}),
   "auto mode with bubblewrap requests only sandboxed authority";
$client->close;

# The server instance name selects rules independently of the routing alias.
File::Path::mkpath ("$dir/config/anyevent-mcp/rules", 0, 0700);
write_file "$dir/config/anyevent-mcp/rules/fs-alias.ini", "[rule]\ndecision=deny\naction=^\nmatch.workdir=$dir/work\n";
chmod 0600, "$dir/config/anyevent-mcp/rules/fs-alias.ini" or die $!;
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [$^X, "-Ilib", "bin/mcp-fs", "--name", "alias", "--bwrap", "$dir/bwrap", "$dir/work"],
   env => { %env, PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS => "alias", PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "stdio" },
);
$result = call "file-write" => { path => "alias-denied", data => "" };
ok $result->{isError} && !-e "$dir/work/alias-denied", "named service instance selects the ordered deny policy";
$client->close;

SKIP: {
   skip "extended GNU patch metadata test (PERL_ANYEVENT_MCP_TESTS=1)", 1
      unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
   skip "GNU patch is unavailable", 1 unless $utility{patch};

   # Metadata protection remains independent of permission approval.
   link "$dir/work/read", "$dir/work/linked" or die $!;
   $client = AnyEvent::MCP::Client::JSONL->new (
      command => \@command, env => \%env, versions => ["2025-11-25"],
   );
   $result = call "file-patch" => { path => "read", diff => "@@ -2 @@\n-TWO\n+two\n" };
   ok $result->{structuredContent}{error} =~ /multiply-linked/, "patch refuses multiply-linked files";
   $client->close;

}

# Discover the URL independently of normal HTTP startup.
my $probe = IO::Socket::INET->new (
   LocalAddr => "127.0.0.1", LocalPort => 0, Proto => "tcp", Listen => 1,
) or die "allocate filesystem test port: $!\n";
my $port = $probe->sockport;
close $probe;
my $bind = "127.0.0.1:$port";
my ($url, $url_error);
{
   local %ENV = (%ENV, %env, PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "");
   my $cv = AnyEvent::Util::run_cmd (
      [$^X, "-Ilib", "bin/mcp-fs", "--name", "default", "--bind", $bind, "--print-url"],
      ">" => \$url, "2>" => \$url_error,
   );
   $cv->recv == 0 or die "mcp-fs --print-url: $url_error\n";
}
chomp $url;
my ($http_pid, $stderr, $stdout);
END { kill TERM => $http_pid if $http_pid }
my $server_cv;
{
   local %ENV = (%ENV, %env, PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "");
   $server_cv = AnyEvent::Util::run_cmd (
      [$^X, "-Ilib", "bin/mcp-fs", "--name", "default", "--bind", $bind,
         "--bwrap", "$dir/bwrap", "$dir/work"],
      "<" => "/dev/null", ">" => \$stdout, "2>" => \$stderr, '$$' => \$http_pid, close_all => 1,
   );
}
my $ready;
for (1 .. 1500) {
   $ready = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1);
   last if $ready;
   Time::HiRes::sleep (0.02);
}
$ready or die "filesystem listener did not start: $!\n";
close $ready;
my $web = AnyEvent::MCP::Client::HTTP->new (url => $url);
($success, $result) = wait_for (sub { $web->call (workdirs => {}, $_[0]) });
ok $success && $result->{structuredContent}{workdirs}{1} eq "$dir/work", "explicitly discovered HTTP URL is usable";
$web->close;
kill TERM => $http_pid;
$server_cv->recv;
undef $http_pid;
ok !$stdout, "filesystem HTTP startup keeps stdout empty";
ok $stderr =~ /Permission rules: \Q$dir\E\/config\/anyevent-mcp\/rules\/fs-default\.ini/,
   "HTTP startup identifies the live permission rule file";

# Program mounts are merged with named-instance overrides; no implicit /opt.
File::Path::mkpath (["$dir/shared", "$dir/only", "$dir/suppressed", "$dir/optional"], 0, 0700);
write_file "$dir/config/anyevent-mcp/global.ini", <<EOF;
[program mcp-fs]
prepend-path = $dir/bin
mount.aa = ro:$dir/suppressed
mount.bb = rw:$dir/shared
mount-try.missing = ro:$dir/optional-later
mount-try.present = rw:$dir/optional
mount-try.suppressed = ro:$dir/suppressed
env.aa = MCP_TEST_SUPPRESSED=program
env.bb = MCP_TEST_EXPLICIT=a value=with equals
env.cc = MCP_TEST_INHERIT
env.dd = SSH_AUTH_SOCK=/forbidden
executable.perl = $^X
[instance mounttest]
prepend-path =
mount.aa =
mount-try.suppressed =
env.aa =
env.bb = MCP_TEST_EXPLICIT=instance value=with equals
mount.cc = ro:$dir/only
EOF
chmod 0600, "$dir/config/anyevent-mcp/global.ini" or die $!;
$full_grant = 1;
$client = AnyEvent::MCP::Client::JSONL->new (
   command => [$^X, "-Ilib", "bin/mcp-fs", "--name", "mounttest", "--stdio",
      "--ask", "mcp", "--bwrap", "$dir/bwrap", "$dir/work"],
   env => \%env, client_capabilities => { elicitation => { form => {} } },
   on_request => $direct_answer,
);
sub sandbox_args {
   my @lines = split /\n/, read_file "$dir/sandbox.log";
   JSON::XS::decode_json $lines[-1]
}
sub mount_for {
   my ($argv, $path) = @_;
   my @mount;
   for my $i (0 .. $#$argv - 2) {
      push @mount, $argv->[$i]
         if $argv->[$i] =~ /\A--(?:ro-bind|bind)(?:-try)?\z/
            && $argv->[$i+1] eq $path && $argv->[$i+2] eq $path;
   }
   $mount[-1]
}
sub private_tmp {
   my ($argv, $path) = @_;
   $path //= "/tmp";
   for my $i (0 .. $#$argv - 1) {
      return $i >= 2 && $argv->[$i-2] eq "--perms" && $argv->[$i-1] eq "1777"
         if $argv->[$i] eq "--tmpfs" && $argv->[$i+1] eq $path;
   }
   0
}
$result = call workdirs => {};
ok join (",", map $_->[0], @{$result->{structuredContent}{mounts}})
      eq "mount-try.missing,mount-try.present,mount.bb,mount.cc"
   && !(grep $_ eq "/opt", @{$result->{structuredContent}{runtime_paths}}),



( run in 1.101 second using v1.01-cache-2.11-cpan-60884a8df76 )