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