@@ -14,6 +14,7 @@ import * as ServerConfig from "../config.ts";
1414import * as ProcessRunner from "../processRunner.ts" ;
1515import {
1616 agentSliceMemoryLimits ,
17+ CgroupRoot ,
1718 classifyScope ,
1819 make ,
1920 ownedAgentSlice ,
@@ -61,20 +62,28 @@ describe("agentSliceMemoryLimits", () => {
6162 } ) ;
6263} ) ;
6364
65+ interface FakeUnit {
66+ activeState : string ;
67+ result : string ;
68+ controlGroup ?: string ;
69+ }
70+
6471const withFakeSystemd = < A , E > (
6572 body : ( input : {
6673 readonly scope : Effect . Success < typeof make > ;
67- readonly units : Map < string , { activeState : string ; result : string } > ;
74+ readonly units : Map < string , FakeUnit > ;
6875 readonly calls : ReadonlyArray < ReadonlyArray < string > > ;
6976 readonly stateDir : string ;
77+ readonly cgroupRoot : string ;
7078 } ) => Effect . Effect < A , E > ,
7179) =>
7280 Effect . gen ( function * ( ) {
7381 const bin = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-agent-scope-" ) ) ;
7482 for ( const name of [ "systemd-run" , "systemctl" , "agent" ] ) {
7583 NodeFS . writeFileSync ( NodePath . join ( bin , name ) , "" , { mode : 0o755 } ) ;
7684 }
77- const units = new Map < string , { activeState : string ; result : string } > ( ) ;
85+ const cgroupRoot = NodePath . join ( bin , "cgroup" ) ;
86+ const units = new Map < string , FakeUnit > ( ) ;
7887 const calls : Array < ReadonlyArray < string > > = [ ] ;
7988 const output = ( stdout : string ) : ProcessRunner . ProcessRunOutput => ( {
8089 stdout,
@@ -97,17 +106,19 @@ const withFakeSystemd = <A, E>(
97106 return output (
98107 state === undefined
99108 ? "LoadState=not-found\nActiveState=inactive\nResult=success\n"
100- : `LoadState=loaded\nActiveState=${ state . activeState } \nResult=${ state . result } \n` ,
109+ : `LoadState=loaded\nActiveState=${ state . activeState } \nResult=${ state . result } \n` +
110+ `ControlGroup=${ state . controlGroup ?? "" } \n` ,
101111 ) ;
102112 } ) ,
103113 } ) ;
104114 const scope = yield * make . pipe (
105115 Effect . provideService ( ProcessRunner . ProcessRunner , runner ) ,
106116 Effect . provideService ( HostProcess . Platform , "linux" ) ,
107117 Effect . provideService ( HostProcess . Environment , { PATH : bin , XDG_RUNTIME_DIR : "/run/user/1" } ) ,
118+ Effect . provideService ( CgroupRoot , cgroupRoot ) ,
108119 ) ;
109120 const { stateDir } = yield * ServerConfig . ServerConfig ;
110- return yield * body ( { scope, units, calls, stateDir } ) . pipe (
121+ return yield * body ( { scope, units, calls, stateDir, cgroupRoot } ) . pipe (
111122 Effect . ensuring ( Effect . sync ( ( ) => NodeFS . rmSync ( bin , { recursive : true , force : true } ) ) ) ,
112123 ) ;
113124 } ) . pipe (
@@ -125,6 +136,29 @@ const unitOf = (launch: { readonly args: ReadonlyArray<string> }) =>
125136const sliceOf = ( launch : { readonly args : ReadonlyArray < string > } ) =>
126137 launch . args . find ( ( arg ) => arg . startsWith ( "--slice=" ) ) ?. slice ( "--slice=" . length ) ?? "" ;
127138
139+ /** An active scope as systemd and the kernel report it, with the agent itself gone. */
140+ const exitedAgent = (
141+ units : Map < string , FakeUnit > ,
142+ cgroupRoot : string ,
143+ unit : string ,
144+ { populated, oomKills } : { readonly populated : boolean ; readonly oomKills : number } ,
145+ ) => {
146+ const controlGroup = `/${ unit } ` ;
147+ NodeFS . mkdirSync ( NodePath . join ( cgroupRoot , controlGroup ) , { recursive : true } ) ;
148+ NodeFS . writeFileSync (
149+ NodePath . join ( cgroupRoot , controlGroup , "cgroup.events" ) ,
150+ `populated ${ populated ? 1 : 0 } \nfrozen 0\n` ,
151+ ) ;
152+ NodeFS . writeFileSync (
153+ NodePath . join ( cgroupRoot , controlGroup , "memory.events" ) ,
154+ `low 0\nhigh 0\nmax 0\noom ${ oomKills } \noom_kill ${ oomKills } \noom_group_kill 0\n` ,
155+ ) ;
156+ units . set ( unit , { activeState : "active" , result : "success" , controlGroup } ) ;
157+ } ;
158+
159+ const stopsOf = ( calls : ReadonlyArray < ReadonlyArray < string > > ) =>
160+ calls . filter ( ( [ , verb ] ) => verb === "stop" ) ;
161+
128162describe ( "AgentScope service" , ( ) => {
129163 it . effect ( "labels a thread from its newest scope until its next session clears it" , ( ) =>
130164 withFakeSystemd ( ( { scope, units } ) =>
@@ -181,6 +215,46 @@ describe("AgentScope service", () => {
181215 ) ,
182216 ) ;
183217
218+ it . effect ( "stops an exited agent's scope with the daemons it left running" , ( ) =>
219+ withFakeSystemd ( ( { scope, units, calls, cgroupRoot } ) =>
220+ Effect . gen ( function * ( ) {
221+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
222+ expect ( agent . unit ) . toBe ( unitOf ( agent ) ) ;
223+ exitedAgent ( units , cgroupRoot , unitOf ( agent ) , { populated : true , oomKills : 0 } ) ;
224+ yield * scope . stop ( unitOf ( agent ) ) ;
225+ expect ( stopsOf ( calls ) ) . toEqual ( [ [ "--user" , "stop" , "--no-block" , unitOf ( agent ) ] ] ) ;
226+ } ) ,
227+ ) ,
228+ ) ;
229+
230+ it . effect ( "leaves a scope the kernel OOM-killed to systemd, so the thread keeps its label" , ( ) =>
231+ withFakeSystemd ( ( { scope, units, calls, cgroupRoot } ) =>
232+ Effect . gen ( function * ( ) {
233+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
234+ // The kernel has counted the kill; systemd has not handled it yet.
235+ exitedAgent ( units , cgroupRoot , unitOf ( agent ) , { populated : true , oomKills : 1 } ) ;
236+ yield * scope . stop ( unitOf ( agent ) ) ;
237+ expect ( stopsOf ( calls ) ) . toEqual ( [ ] ) ;
238+
239+ units . set ( unitOf ( agent ) , { activeState : "failed" , result : "oom-kill" } ) ;
240+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( true ) ;
241+ } ) ,
242+ ) ,
243+ ) ;
244+
245+ it . effect ( "does not stop an empty scope or a unit it did not start" , ( ) =>
246+ withFakeSystemd ( ( { scope, units, calls, cgroupRoot } ) =>
247+ Effect . gen ( function * ( ) {
248+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
249+ exitedAgent ( units , cgroupRoot , unitOf ( agent ) , { populated : false , oomKills : 0 } ) ;
250+ yield * scope . stop ( unitOf ( agent ) ) ;
251+ yield * scope . stop ( "session-2.scope" ) ;
252+ expect ( stopsOf ( calls ) ) . toEqual ( [ ] ) ;
253+ expect ( calls . some ( ( args ) => args . includes ( "session-2.scope" ) ) ) . toBe ( false ) ;
254+ } ) ,
255+ ) ,
256+ ) ;
257+
184258 it . effect ( "leaves a quiet slice alone" , ( ) =>
185259 withFakeSystemd ( ( { scope, calls } ) =>
186260 Effect . gen ( function * ( ) {
0 commit comments