char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte Somtests: split the slowest files so that --parallel runs them side by sidebe80d8a

main
2.8 KiB69 linesraw
1import { assertEquals } from "@std/assert";
2import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context";
3import { rootedEnvironment, testExtension } from "@berry/api/testing.ts";
4import { onPath, tempDir, until } from "@berry/util";
5import mcp from "../../extensions/mcp/mod.ts";
6import { BOOTSTRAP, Connection, RemoteEnvironment } from "./connection.ts";
7
8for (const where of ["local", "remote"]) {
9  for (const mode of ["escaped", "quiet-child"]) {
10    Deno.test({
11      name: `${where} MCP disposal handles ${
12        mode === "escaped"
13          ? "escaped descendants holding stderr"
14          : "pipe-free descendants surviving their leader"
15      }`,
16      ignore: Deno.build.os !== "linux" || (mode === "escaped" && !onPath("setsid")),
17      async fn() {
18        await using dir = await tempDir({ prefix: "mcp-descendant-" });
19        await Deno.copyFile(
20          new URL("../../extensions/mcp/test-server.ts", import.meta.url),
21          dir.path + "/server.ts",
22        );
23        await Deno.writeTextFile(
24          dir.path + "/mcp.json",
25          JSON.stringify({
26            mcpServers: {
27              probe: { command: Deno.execPath(), args: ["run", "-A", "server.ts", mode] },
28            },
29          }),
30        );
31        const connection = where === "remote"
32          ? await Connection.open("descendant", [Deno.execPath(), "eval", "--quiet", BOOTSTRAP])
33          : undefined;
34        const env = connection
35          ? new RemoteEnvironment("descendant", connection, dir.path)
36          : rootedEnvironment(dir.path, { cwd: "/" });
37        const ext = testExtension(mcp, { env });
38        let pid: number | undefined;
39        try {
40          await ext.configure({ configFile: "mcp.json" });
41          const listed = await ext.call("mcp_list", {}) as { status: string }[];
42          assertEquals(listed[0].status, "connected");
43          pid = Number(await Deno.readTextFile(dir.path + "/descendant"));
44          let disposed = false;
45          const disposing = ext.dispose().then(() => disposed = true);
46          await until(() => disposed, {
47            message: "MCP disposal waited indefinitely for inherited stderr",
48          });
49          await disposing;
50          if (mode === "quiet-child") {
51            await until(async () => {
52              const stat = await Deno.readTextFile(`/proc/${pid}/stat`).catch(() => "");
53              return !stat || stat.slice(stat.lastIndexOf(")") + 2).startsWith("Z");
54            }, { message: "an in-group descendant survived MCP disposal" });
55          }
56          assertEquals((await env.readTextFile("mcp.json", context)).ok, true);
57        } finally {
58          if (pid) {
59            try {
60              Deno.kill(pid, "SIGKILL");
61            } catch { /* already gone */ }
62          }
63          await ext.dispose();
64          await connection?.close();
65        }
66      },
67    });
68  }
69}