char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
0575637
main
1import { assert , assertEquals , assertStringIncludes } from "@std/assert" ; 2import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context" ; 3import { onPath , tempDir , until } from "@berry/util" ; 4import { BOOTSTRAP , Connection , RemoteEnvironment } from "./connection.ts" ; 5 6Deno . test ({ 7name :"remote process exit is independent of inherited pipes, which close within a bounded drain" , 8ignore :Deno . build . os !== "linux" || ! onPath ( "setsid" ), 9async fn () { 10await usingdir = await tempDir ({ prefix :"remote-pipes-" }); 11const connection = await Connection . open ( "pipes" , [ 12Deno . execPath (), 13"eval" , 14"--quiet" , 15BOOTSTRAP , 16]); 17const env = new RemoteEnvironment ( "pipes" , connection , dir . path ); 18let pid :number | undefined ; 19try { 20const child = await env . spawn ( 21[ 22"sh" , 23"-c" , 24"setsid sh -c 'echo $$ > descendant; exec sleep 300' </dev/null >/dev/null & exit 0" , 25], 26{ group :true }, 27context , 28); 29await until (() => Deno . stat ( dir . path + "/descendant" ). then (() => true , () => false )); 30pid = Number ( await Deno . readTextFile ( dir . path + "/descendant" )); 31let exited = false ; 32void child . exited . then (() => exited = true ); 33await until (() => exited , { 34timeout :500 , 35message :"process exit was held up by inherited stderr" , 36}); 37assertEquals ( await child . exited , 0 ); 38assertEquals ( await new Response ( child . stdout ). text (), "" ); 39let ended = false ; 40const stderr = new Response ( child . stderr ). text (). then (( text ) => { 41ended = true ; 42return text ; 43}); 44await until (() => ended , { message :"inherited stderr never finished draining" }); 45assertEquals ( await stderr , "" ); 46assertEquals (( await env . readTextFile ( "descendant" , context )). ok , true ); 47} finally { 48if ( pid ) { 49try { 50Deno . kill ( pid , "SIGKILL" ); 51} catch { /* already gone */ } 52} 53await connection . close (); 54} 55}, 56}); 57 58Deno . test ({ 59name :"ordinary remote spawn preserves delayed descendant output after its leader exits" , 60ignore :Deno . build . os !== "linux" , 61async fn () { 62const connection = await Connection . open ( "late" , [ 63Deno . execPath (), 64"eval" , 65"--quiet" , 66BOOTSTRAP , 67]); 68const env = new RemoteEnvironment ( "late" , connection , Deno . cwd ()); 69try { 70// ignored before the fork, so the leader's exit cannot hang up a child yet to ignore it itself 71const argv = [ "sh" , "-c" , "trap '' HUP; (sleep 1.5; echo late) & echo early" ]; 72const program = await env . spawn ( argv , {}, context ); 73assertEquals ( await program . exited , 0 ); 74const output = await new Response ( program . stdout ). text (); 75assertStringIncludes ( output , "early" ); 76assertStringIncludes ( output , "late" ); 77} finally { 78await connection . close (); 79} 80}, 81}); 82 83Deno . test ({ 84name :"closing a remote transport does not wait for descendants retaining its stderr" , 85ignore :Deno . build . os !== "linux" , 86async fn () { 87await usingdir = await tempDir ({ prefix :"transport-stderr-" }); 88const connection = await Connection . open ( "stderr" , [ 89"sh" , 90"-c" , 91'sleep 300 </dev/null >/dev/null & echo $! > "$1"; shift; exec "$@"' , 92"sh" , 93dir . path + "/descendant" , 94Deno . execPath (), 95"eval" , 96"--quiet" , 97BOOTSTRAP , 98]); 99const pid = Number ( await Deno . readTextFile ( dir . path + "/descendant" )); 100try { 101let closed = false ; 102const closing = connection . close (). then (() => closed = true ); 103await until (() => closed , { 104timeout :1500 , 105message :"transport close waited for descendant stderr EOF" , 106}); 107await closing ; 108assert ( connection . lifetime . aborted ); 109} finally { 110try { 111Deno . kill ( pid , "SIGKILL" ); 112} catch { /* already gone */ } 113await connection . close (); 114} 115}, 116});