11// @effect -diagnostics nodeBuiltinImport:off -- fake systemd binaries live in a temp dir.
2- import * as NodeChildProcess from "node:child_process" ;
3- import * as NodeCrypto from "node:crypto" ;
42import * as NodeFS from "node:fs" ;
53import * as NodeOS from "node:os" ;
64import * as NodePath from "node:path" ;
75
86import * as NodeServices from "@effect/platform-node/NodeServices" ;
97import { assert , describe , expect , it } from "@effect/vitest" ;
10- import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime" ;
118import { AgentScope } from "@t3tools/shared/AgentScope" ;
129import * as HostProcess from "@t3tools/shared/HostProcess" ;
13- import * as Deferred from "effect/Deferred" ;
1410import * as Effect from "effect/Effect" ;
1511import * as FileSystem from "effect/FileSystem" ;
1612import * as Layer from "effect/Layer" ;
@@ -21,7 +17,7 @@ import * as ServerConfig from "../config.ts";
2117import { makeCodexAppServerSpawnCommand } from "../orchestration-v2/Adapters/CodexAdapterV2.ts" ;
2218import * as ProcessRunner from "../processRunner.ts" ;
2319import * as ProviderProcesses from "./providerProcesses.ts" ;
24- import { classifyScope , layer , make , ownedAgentSlice , type ScopeState } from "./agentScope.ts" ;
20+ import { classifyScope , make , ownedAgentSlice , type ScopeState } from "./agentScope.ts" ;
2521
2622const running : ScopeState = {
2723 loadState : "loaded" ,
@@ -438,116 +434,3 @@ describe("AgentScope service", () => {
438434 ) ,
439435 ) ;
440436} ) ;
441-
442- // Real child processes stand in for servers and CLIs; only PIDs captured at spawn are signalled.
443- const ownedProcess = Effect . gen ( function * ( ) {
444- const ready = yield * Deferred . make < void > ( ) ;
445- const exited = yield * Deferred . make < void > ( ) ;
446- const child = yield * Effect . acquireRelease (
447- Effect . sync ( ( ) => {
448- const child = NodeChildProcess . spawn (
449- process . execPath ,
450- [ "-e" , 'process.stdin.resume(); process.send("ready");' ] ,
451- { stdio : [ "pipe" , "ignore" , "ignore" , "ipc" ] } ,
452- ) ;
453- child . once ( "message" , ( ) => Deferred . doneUnsafe ( ready , Effect . void ) ) ;
454- child . once ( "error" , ( error ) => Deferred . doneUnsafe ( ready , Effect . die ( error ) ) ) ;
455- child . once ( "exit" , ( ) => Deferred . doneUnsafe ( exited , Effect . void ) ) ;
456- return child ;
457- } ) ,
458- ( child ) =>
459- Effect . sync ( ( ) => child . kill ( "SIGKILL" ) ) . pipe ( Effect . andThen ( Deferred . await ( exited ) ) ) ,
460- ) ;
461- yield * Deferred . await ( ready ) ;
462- const pid = child . pid ;
463- assert ( pid !== undefined ) ;
464- const controller = AcpSessionRuntime . makePosixProcessTreeController ( "linux" ) ;
465- const identity = controller . identity ( pid ) ;
466- assert ( identity !== undefined ) ;
467- const boot = NodeFS . readFileSync ( "/proc/sys/kernel/random/boot_id" , "utf8" ) . trim ( ) ;
468- return {
469- pid,
470- startTime : `${ boot } :${ identity . startTime } ` ,
471- pgid : 0 ,
472- running : ( ) => {
473- const observed = controller . identity ( pid ) ;
474- return observed !== undefined && ! AcpSessionRuntime . posixProcessIsZombie ( observed ) ;
475- } ,
476- stop : Effect . sync ( ( ) => child . kill ( "SIGKILL" ) ) . pipe ( Effect . andThen ( Deferred . await ( exited ) ) ) ,
477- } ;
478- } ) ;
479-
480- describe . runIf ( HostProcess . Platform . defaultValue ( ) === "linux" ) (
481- "provider restart ownership" ,
482- ( ) => {
483- it . live ( "reaps only recorded CLIs of dead servers before startup completes" , ( ) =>
484- Effect . gen ( function * ( ) {
485- const config = yield * ServerConfig . ServerConfig ;
486- const directory = NodePath . join ( config . stateDir , "provider-processes" ) ;
487- NodeFS . mkdirSync ( directory , { recursive : true } ) ;
488- type Identity = { readonly pid : number ; readonly startTime : string } ;
489- const record = ( owner : Identity , processes : ReadonlyArray < Identity > ) => {
490- const instanceId = NodeCrypto . randomUUID ( ) ;
491- NodeFS . writeFileSync (
492- NodePath . join ( directory , `${ instanceId } .json` ) ,
493- JSON . stringify ( {
494- version : 1 ,
495- instanceId,
496- hostname : NodeOS . hostname ( ) ,
497- stateDir : config . stateDir ,
498- owner : { pid : owner . pid , startTime : owner . startTime } ,
499- processes : processes . map ( ( { pid, startTime } ) => ( { pid, startTime, pgid : 0 } ) ) ,
500- } ) ,
501- ) ;
502- } ;
503- const reused = ( identity : Identity ) => ( {
504- ...identity ,
505- startTime : identity . startTime . replace ( / : \d + $ / , ":1" ) ,
506- } ) ;
507- const deadOwner = yield * ownedProcess ;
508- const liveOwner = yield * ownedProcess ;
509- const reusedOwner = yield * ownedProcess ;
510- const liveProvider = yield * ownedProcess ;
511- const reusedProvider = yield * ownedProcess ;
512- const orphan = yield * ownedProcess ;
513- const orphanOfReusedOwner = yield * ownedProcess ;
514- const unrecorded = yield * ownedProcess ;
515- record ( liveOwner , [ liveProvider ] ) ;
516- record ( deadOwner , [ reused ( reusedProvider ) ] ) ;
517- record ( deadOwner , [ orphan ] ) ;
518- record ( reused ( reusedOwner ) , [ orphanOfReusedOwner ] ) ;
519- yield * deadOwner . stop ;
520-
521- yield * Layer . build ( layer ) . pipe (
522- Effect . provideService ( ServerConfig . ServerConfig , config ) ,
523- Effect . provide ( NodeServices . layer ) ,
524- ) ;
525-
526- expect ( {
527- orphan : orphan . running ( ) ,
528- orphanOfReusedOwner : orphanOfReusedOwner . running ( ) ,
529- liveProvider : liveProvider . running ( ) ,
530- reusedProvider : reusedProvider . running ( ) ,
531- reusedOwner : reusedOwner . running ( ) ,
532- unrecorded : unrecorded . running ( ) ,
533- } ) . toEqual ( {
534- orphan : false ,
535- orphanOfReusedOwner : false ,
536- liveProvider : true ,
537- reusedProvider : true ,
538- reusedOwner : true ,
539- unrecorded : true ,
540- } ) ;
541- } ) . pipe (
542- Effect . scoped ,
543- Effect . provideService ( HostProcess . Platform , "linux" ) ,
544- Effect . provideService ( HostProcess . Environment , { PATH : process . env . PATH } ) ,
545- Effect . provide (
546- ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-restart-ownership-" } ) . pipe (
547- Layer . provideMerge ( NodeServices . layer ) ,
548- ) ,
549- ) ,
550- ) ,
551- ) ;
552- } ,
553- ) ;
0 commit comments