@@ -8,7 +8,9 @@ import { assert, describe, expect, it } from "@effect/vitest";
88import { AgentScope } from "@t3tools/shared/AgentScope" ;
99import * as HostProcess from "@t3tools/shared/HostProcess" ;
1010import * as Effect from "effect/Effect" ;
11+ import * as FileSystem from "effect/FileSystem" ;
1112import * as Layer from "effect/Layer" ;
13+ import * as Logger from "effect/Logger" ;
1214import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1315
1416import * as ServerConfig from "../config.ts" ;
@@ -35,21 +37,33 @@ describe("classifyScope", () => {
3537 } ) ;
3638} ) ;
3739
40+ interface FakeUnit {
41+ activeState : string ;
42+ result : string ;
43+ controlGroup ?: string ;
44+ }
45+
3846const withFakeSystemd = < A , E > (
3947 body : ( input : {
4048 readonly scope : Effect . Success < typeof make > ;
41- readonly units : Map < string , { activeState : string ; result : string } > ;
49+ readonly units : Map < string , FakeUnit > ;
4250 readonly calls : ReadonlyArray < ReadonlyArray < string > > ;
4351 readonly stateDir : string ;
52+ readonly cgroupRoot : string ;
4453 } ) => Effect . Effect < A , E > ,
45- { oomPolicy = true } : { readonly oomPolicy ?: boolean } = { } ,
54+ // A user manager older than systemd 253 rejects OOMPolicy on scopes.
55+ {
56+ oomPolicy = true ,
57+ stopFails = false ,
58+ } : { readonly oomPolicy ?: boolean ; readonly stopFails ?: boolean } = { } ,
4659) =>
4760 Effect . gen ( function * ( ) {
4861 const bin = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-agent-scope-" ) ) ;
4962 for ( const name of [ "systemd-run" , "systemctl" , "agent" ] ) {
5063 NodeFS . writeFileSync ( NodePath . join ( bin , name ) , "" , { mode : 0o755 } ) ;
5164 }
52- const units = new Map < string , { activeState : string ; result : string } > ( ) ;
65+ const cgroupRoot = NodePath . join ( bin , "cgroup" ) ;
66+ const units = new Map < string , FakeUnit > ( ) ;
5367 const calls : Array < ReadonlyArray < string > > = [ ] ;
5468 const output = ( stdout : string , code = 0 ) : ProcessRunner . ProcessRunOutput => ( {
5569 stdout,
@@ -69,23 +83,37 @@ const withFakeSystemd = <A, E>(
6983 return output ( "" , 1 ) ;
7084 }
7185 const [ , verb , unit = "" ] = args ;
86+ if ( stopFails && verb === "stop" ) {
87+ return {
88+ ...output ( "" ) ,
89+ stderr : `Failed to stop ${ args . at ( - 1 ) } : Unit not loaded.` ,
90+ code : ChildProcessSpawner . ExitCode ( 5 ) ,
91+ } ;
92+ }
7293 if ( verb === "reset-failed" ) units . delete ( unit ) ;
7394 if ( verb !== "show" ) return output ( "" ) ;
7495 const state = units . get ( unit ) ;
7596 return output (
7697 state === undefined
7798 ? "LoadState=not-found\nActiveState=inactive\nResult=success\n"
78- : `LoadState=loaded\nActiveState=${ state . activeState } \nResult=${ state . result } \n` ,
99+ : `LoadState=loaded\nActiveState=${ state . activeState } \nResult=${ state . result } \n` +
100+ `ControlGroup=${ state . controlGroup ?? "" } \n` ,
79101 ) ;
80102 } ) ,
81103 } ) ;
104+ const fileSystem = yield * FileSystem . FileSystem ;
82105 const scope = yield * make . pipe (
83106 Effect . provideService ( ProcessRunner . ProcessRunner , runner ) ,
84107 Effect . provideService ( HostProcess . Platform , "linux" ) ,
85108 Effect . provideService ( HostProcess . Environment , { PATH : bin , XDG_RUNTIME_DIR : "/run/user/1" } ) ,
109+ Effect . provideService ( FileSystem . FileSystem , {
110+ ...fileSystem ,
111+ readFileString : ( path , encoding ) =>
112+ fileSystem . readFileString ( path . replace ( "/sys/fs/cgroup" , cgroupRoot ) , encoding ) ,
113+ } ) ,
86114 ) ;
87115 const { stateDir } = yield * ServerConfig . ServerConfig ;
88- return yield * body ( { scope, units, calls, stateDir } ) . pipe (
116+ return yield * body ( { scope, units, calls, stateDir, cgroupRoot } ) . pipe (
89117 Effect . ensuring ( Effect . sync ( ( ) => NodeFS . rmSync ( bin , { recursive : true , force : true } ) ) ) ,
90118 ) ;
91119 } ) . pipe (
@@ -103,6 +131,31 @@ const unitOf = (launch: { readonly args: ReadonlyArray<string> }) =>
103131const sliceOf = ( launch : { readonly args : ReadonlyArray < string > } ) =>
104132 launch . args . find ( ( arg ) => arg . startsWith ( "--slice=" ) ) ?. slice ( "--slice=" . length ) ?? "" ;
105133
134+ /** An active scope as systemd and the kernel report it, with the agent itself gone. */
135+ const exitedAgent = (
136+ units : Map < string , FakeUnit > ,
137+ cgroupRoot : string ,
138+ unit : string ,
139+ { populated, oomKills } : { readonly populated : boolean ; readonly oomKills : number | undefined } ,
140+ ) => {
141+ const controlGroup = `/${ unit } ` ;
142+ NodeFS . mkdirSync ( NodePath . join ( cgroupRoot , controlGroup ) , { recursive : true } ) ;
143+ NodeFS . writeFileSync (
144+ NodePath . join ( cgroupRoot , controlGroup , "cgroup.events" ) ,
145+ `populated ${ populated ? 1 : 0 } \nfrozen 0\n` ,
146+ ) ;
147+ // An undefined count leaves memory.events unreadable.
148+ if ( oomKills !== undefined ) {
149+ NodeFS . writeFileSync (
150+ NodePath . join ( cgroupRoot , controlGroup , "memory.events" ) ,
151+ `low 0\nhigh 0\nmax 0\noom ${ oomKills } \noom_kill ${ oomKills } \noom_group_kill 0\n` ,
152+ ) ;
153+ }
154+ units . set ( unit , { activeState : "active" , result : "success" , controlGroup } ) ;
155+ } ;
156+
157+ const stopsOf = ( calls : ReadonlyArray < ReadonlyArray < string > > ) =>
158+ calls . filter ( ( [ , verb ] ) => verb === "stop" ) ;
106159const launchCodex = ( scope : Effect . Success < typeof make > , threadId : string ) =>
107160 makeCodexAppServerSpawnCommand ( { command : "agent" , args : [ "app-server" ] , threadId } ) . pipe (
108161 Effect . provideService ( AgentScope , scope ) ,
@@ -194,4 +247,90 @@ describe("AgentScope service", () => {
194247 } ) ,
195248 ) ,
196249 ) ;
250+
251+ it . effect ( "stops leftover processes only in an active agent scope" , ( ) =>
252+ withFakeSystemd ( ( { scope, units, calls, cgroupRoot } ) =>
253+ Effect . gen ( function * ( ) {
254+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
255+ const unit = unitOf ( agent ) ;
256+ expect ( agent . unit ) . toBe ( unit ) ;
257+
258+ yield * scope . stop ( "session-2.scope" ) ;
259+ expect ( calls . some ( ( args ) => args . includes ( "session-2.scope" ) ) ) . toBe ( false ) ;
260+ yield * scope . stop ( unit ) ;
261+ exitedAgent ( units , cgroupRoot , unit , { populated : false , oomKills : 0 } ) ;
262+ yield * scope . stop ( unit ) ;
263+ exitedAgent ( units , cgroupRoot , unit , { populated : true , oomKills : 0 } ) ;
264+ units . get ( unit ) ! . activeState = "deactivating" ;
265+ yield * scope . stop ( unit ) ;
266+ expect ( stopsOf ( calls ) ) . toEqual ( [ ] ) ;
267+
268+ units . get ( unit ) ! . activeState = "active" ;
269+ yield * scope . stop ( unit ) ;
270+ expect ( stopsOf ( calls ) ) . toEqual ( [ [ "--user" , "stop" , "--no-block" , unit ] ] ) ;
271+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( false ) ;
272+ } ) ,
273+ ) ,
274+ ) ;
275+
276+ it . effect . each ( [ true , false ] ) (
277+ "cleans up an OOM exit without losing its diagnosis (OOMPolicy=%s)" ,
278+ ( oomPolicy ) =>
279+ withFakeSystemd (
280+ ( { scope, units, calls, cgroupRoot } ) =>
281+ Effect . gen ( function * ( ) {
282+ const agent = yield * scope . wrap ( {
283+ command : "agent" ,
284+ args : [ ] ,
285+ name : "t" ,
286+ threadId : "a" ,
287+ } ) ;
288+ const unit = unitOf ( agent ) ;
289+ exitedAgent ( units , cgroupRoot , unit , { populated : true , oomKills : undefined } ) ;
290+ yield * scope . stop ( unit ) ;
291+ expect ( stopsOf ( calls ) ) . toEqual (
292+ oomPolicy ? [ ] : [ [ "--user" , "stop" , "--no-block" , unit ] ] ,
293+ ) ;
294+
295+ // The kernel records the kill before systemd records the OOM result.
296+ exitedAgent ( units , cgroupRoot , unit , { populated : true , oomKills : 1 } ) ;
297+ yield * scope . stop ( unit ) ;
298+ expect ( stopsOf ( calls ) ) . toHaveLength ( oomPolicy ? 0 : 2 ) ;
299+
300+ if ( oomPolicy ) {
301+ units . set ( unit , { activeState : "failed" , result : "oom-kill" } ) ;
302+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( true ) ;
303+ units . delete ( unit ) ;
304+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( true ) ;
305+ }
306+ } ) ,
307+ { oomPolicy } ,
308+ ) ,
309+ ) ;
310+
311+ it . effect ( "reports failed daemon cleanup without failing session teardown" , ( ) => {
312+ const warnings : Array < unknown > = [ ] ;
313+ const logger = Logger . make ( ( { message } ) => {
314+ warnings . push ( message ) ;
315+ } ) ;
316+ return withFakeSystemd (
317+ ( { scope, units, cgroupRoot } ) =>
318+ Effect . gen ( function * ( ) {
319+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
320+ exitedAgent ( units , cgroupRoot , unitOf ( agent ) , { populated : true , oomKills : 0 } ) ;
321+ yield * scope . stop ( unitOf ( agent ) ) ;
322+ expect ( warnings . flat ( ) ) . toContain ( "Could not stop an exited agent's scope" ) ;
323+ } ) . pipe ( Effect . provide ( Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ) ,
324+ { stopFails : true } ,
325+ ) ;
326+ } ) ;
327+
328+ it . effect ( "leaves a quiet slice alone" , ( ) =>
329+ withFakeSystemd ( ( { scope, calls } ) =>
330+ Effect . gen ( function * ( ) {
331+ yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
332+ expect ( calls . filter ( ( [ , verb ] ) => verb === "stop" ) ) . toEqual ( [ ] ) ;
333+ } ) ,
334+ ) ,
335+ ) ;
197336} ) ;
0 commit comments