@@ -691,93 +691,6 @@ effectIt.effect.each(["linux", "darwin"] as const)(
691691 } ,
692692) ;
693693
694- effectIt . effect ( "finds a server an agent left before a restart on the first scan" , ( ) => {
695- const port = 43_127 ;
696- let lsofStdout = `p4242\ncnode\nn127.0.0.1:${ port } \n` ;
697- let lsofCalls = 0 ;
698- const missing = FileSystem . makeNoop ( { } ) ;
699- const layer = PortScanner . layer . pipe (
700- Layer . provide (
701- Layer . mergeAll (
702- FileSystem . layerNoop ( {
703- readFileString : ( path ) =>
704- path === "/proc/4242/environ"
705- ? Effect . succeed ( `${ PortScanner . AGENT_THREAD_ENV } =thread-agent\0` )
706- : missing . readFileString ( path ) ,
707- } ) ,
708- Layer . succeed ( ProcessRunner . ProcessRunner , {
709- run : ( ) => {
710- lsofCalls += 1 ;
711- return Effect . succeed ( processOutput ( lsofStdout ) ) ;
712- } ,
713- } ) ,
714- Layer . succeed ( HostProcess . Platform , "linux" ) ,
715- FetchHttpClient . layer . pipe (
716- Layer . provide (
717- Layer . succeed ( FetchHttpClient . Fetch , ( ) =>
718- Promise . resolve ( new Response ( "app" , { headers : { "content-type" : "text/html" } } ) ) ,
719- ) ,
720- ) ,
721- ) ,
722- ) ,
723- ) ,
724- ) ;
725- return Effect . gen ( function * ( ) {
726- const scanner = yield * PortScanner . PortDiscovery ;
727- // No agent has run and no terminal is open since this server started.
728- expect ( yield * scanner . scan ( ) ) . toMatchObject ( [
729- { port, pid : 4242 , terminal : null , agent : { threadId : "thread-agent" } } ,
730- ] ) ;
731- lsofStdout = "" ;
732- expect ( yield * scanner . scan ( ) ) . toEqual ( [ ] ) ;
733- const lsofCallsAfterExit = lsofCalls ;
734- expect ( yield * scanner . scan ( ) ) . toEqual ( [ ] ) ;
735- expect ( lsofCalls ) . toBe ( lsofCallsAfterExit ) ;
736- } ) . pipe ( Effect . provide ( layer ) ) ;
737- } ) ;
738-
739- effectIt . effect ( "forgets agent pids while no client scans, since they can be reused" , ( ) => {
740- const port = 43_126 ;
741- let environ = `${ PortScanner . AGENT_THREAD_ENV } =thread-agent\0` ;
742- const missing = FileSystem . makeNoop ( { } ) ;
743- const layer = PortScanner . layer . pipe (
744- Layer . provide (
745- Layer . mergeAll (
746- FileSystem . layerNoop ( {
747- readFileString : ( path ) =>
748- path === "/proc/4242/environ" ? Effect . succeed ( environ ) : missing . readFileString ( path ) ,
749- } ) ,
750- Layer . succeed ( ProcessRunner . ProcessRunner , {
751- run : ( ) => Effect . succeed ( processOutput ( `p4242\ncnode\nn127.0.0.1:${ port } \n` ) ) ,
752- } ) ,
753- Layer . succeed ( HostProcess . Platform , "linux" ) ,
754- FetchHttpClient . layer . pipe (
755- Layer . provide (
756- Layer . succeed ( FetchHttpClient . Fetch , ( ) =>
757- Promise . resolve ( new Response ( "app" , { headers : { "content-type" : "text/html" } } ) ) ,
758- ) ,
759- ) ,
760- ) ,
761- ) ,
762- ) ,
763- ) ;
764- return Effect . gen ( function * ( ) {
765- const scanner = yield * PortScanner . PortDiscovery ;
766- scanner . trackAgent ( ) ( ) ;
767- yield * Effect . scoped (
768- Effect . gen ( function * ( ) {
769- yield * scanner . retain ;
770- expect ( yield * scanner . scan ( ) ) . toMatchObject ( [
771- { pid : 4242 , agent : { threadId : "thread-agent" } } ,
772- ] ) ;
773- } ) ,
774- ) ;
775- // While nobody scanned, the server exited and an unrelated listener got its pid.
776- environ = "PATH=/usr/bin\0" ;
777- expect ( yield * scanner . scan ( ) ) . toEqual ( [ ] ) ;
778- } ) . pipe ( Effect . provide ( layer ) ) ;
779- } ) ;
780-
781694effectIt . effect ( "stops only a job's own process group" , ( ) =>
782695 Effect . sync ( ( ) => {
783696 const serverPid = 100 ;
@@ -813,11 +726,11 @@ server.listen(0, "127.0.0.1", () => {
813726` ;
814727
815728/**
816- * A real HTML server with an agent's environment. As a job it runs the way an agent's tool shell
817- * starts one : under a detached shell with job control, in a group of its own. Otherwise it runs
818- * directly under this process, in its group. Release kills only the pids it started.
729+ * A real HTML server with an agent's environment, started the way an agent's tool shell starts a
730+ * job : under a detached shell with job control, in a group of its own. Release kills only the pids
731+ * it started.
819732 */
820- const agentServer = ( options : { readonly job : boolean } ) =>
733+ const agentServer = ( ) =>
821734 Effect . acquireRelease (
822735 Effect . callback < {
823736 readonly child : NodeChildProcess . ChildProcess ;
@@ -826,17 +739,12 @@ const agentServer = (options: { readonly job: boolean }) =>
826739 readonly exited : Promise < void > ;
827740 } > ( ( resume ) => {
828741 const env = { ...process . env , [ PortScanner . AGENT_THREAD_ENV ] : "thread-live" } ;
829- const child = options . job
830- ? // dash ignores `set -m` without a terminal; bash, like agent tool shells, honors it.
831- NodeChildProcess . spawn (
832- "bash" ,
833- [ "-c" , 'set -m; "$0" -e "$1" & wait' , process . execPath , AGENT_SERVER_SCRIPT ] ,
834- { detached : true , env, stdio : [ "ignore" , "pipe" , "ignore" ] } ,
835- )
836- : NodeChildProcess . spawn ( process . execPath , [ "-e" , AGENT_SERVER_SCRIPT ] , {
837- env,
838- stdio : [ "ignore" , "pipe" , "ignore" ] ,
839- } ) ;
742+ // dash ignores `set -m` without a terminal; bash, like agent tool shells, honors it.
743+ const child = NodeChildProcess . spawn (
744+ "bash" ,
745+ [ "-c" , 'set -m; "$0" -e "$1" & wait' , process . execPath , AGENT_SERVER_SCRIPT ] ,
746+ { detached : true , env, stdio : [ "ignore" , "pipe" , "ignore" ] } ,
747+ ) ;
840748 const exited = new Promise < void > ( ( resolve ) => child . once ( "exit" , ( ) => resolve ( ) ) ) ;
841749 child . once ( "error" , ( error ) => resume ( Effect . die ( error ) ) ) ;
842750 child . stdout ?. once ( "data" , ( chunk ) => {
@@ -864,15 +772,14 @@ const isRunning = (pid: number): boolean => {
864772} ;
865773
866774effectIt . live . skipIf ( HostProcess . Platform . defaultValue ( ) === "win32" ) (
867- "stops an agent's server through its job's group and refuses a group shared with this server" ,
775+ "stops an agent's server through its job's group, and only a listed server" ,
868776 ( ) =>
869777 Effect . gen ( function * ( ) {
870778 const scanner = yield * PortScanner . PortDiscovery ;
871779 yield * Effect . acquireRelease ( Effect . sync ( scanner . trackAgent ) , ( release ) =>
872780 Effect . sync ( release ) ,
873781 ) ;
874- const job = yield * agentServer ( { job : true } ) ;
875- const shared = yield * agentServer ( { job : false } ) ;
782+ const job = yield * agentServer ( ) ;
876783
877784 expect ( yield * scanner . scan ( ) ) . toEqual (
878785 expect . arrayContaining ( [
@@ -881,21 +788,11 @@ effectIt.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
881788 port : job . port ,
882789 agent : { threadId : "thread-live" } ,
883790 } ) ,
884- expect . objectContaining ( {
885- pid : shared . pid ,
886- port : shared . port ,
887- agent : { threadId : "thread-live" } ,
888- } ) ,
889791 ] ) ,
890792 ) ;
891793
892794 const unlisted = yield * Effect . flip ( scanner . stopServer ( { pid : process . pid , port : job . port } ) ) ;
893795 expect ( unlisted . reason ) . toBe ( "not-found" ) ;
894- const refused = yield * Effect . flip (
895- scanner . stopServer ( { pid : shared . pid , port : shared . port } ) ,
896- ) ;
897- expect ( refused . reason ) . toBe ( "shared-group" ) ;
898- expect ( isRunning ( shared . pid ) ) . toBe ( true ) ;
899796
900797 yield * scanner . stopServer ( { pid : job . pid , port : job . port } ) ;
901798 // The shell waits on its job, so it exits once the job has.
0 commit comments