@@ -55,7 +55,12 @@ const withFakeSystemd = <A, E>(
5555 {
5656 oomPolicy = true ,
5757 stopFails = false ,
58- } : { readonly oomPolicy ?: boolean ; readonly stopFails ?: boolean } = { } ,
58+ memoryPolicy,
59+ } : {
60+ readonly oomPolicy ?: boolean ;
61+ readonly stopFails ?: boolean ;
62+ readonly memoryPolicy ?: string ;
63+ } = { } ,
5964) =>
6065 Effect . gen ( function * ( ) {
6166 const bin = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-agent-scope-" ) ) ;
@@ -105,7 +110,11 @@ const withFakeSystemd = <A, E>(
105110 const scope = yield * make . pipe (
106111 Effect . provideService ( ProcessRunner . ProcessRunner , runner ) ,
107112 Effect . provideService ( HostProcess . Platform , "linux" ) ,
108- Effect . provideService ( HostProcess . Environment , { PATH : bin , XDG_RUNTIME_DIR : "/run/user/1" } ) ,
113+ Effect . provideService ( HostProcess . Environment , {
114+ PATH : bin ,
115+ XDG_RUNTIME_DIR : "/run/user/1" ,
116+ ...( memoryPolicy === undefined ? { } : { T3CODE_AGENT_SLICE_MEMORY_POLICY : memoryPolicy } ) ,
117+ } ) ,
109118 Effect . provideService ( FileSystem . FileSystem , {
110119 ...fileSystem ,
111120 readFileString : ( path , encoding ) =>
@@ -167,6 +176,37 @@ const launchCodex = (scope: Effect.Success<typeof make>, threadId: string) =>
167176 ) ;
168177
169178describe ( "AgentScope service" , ( ) => {
179+ it . effect . each ( [ undefined , "automatic" , "host-managed" , "invalid" ] ) (
180+ "preserves scopes and OOM handling with memory policy %s" ,
181+ ( memoryPolicy ) =>
182+ withFakeSystemd (
183+ ( { scope, units, calls, stateDir } ) =>
184+ Effect . gen ( function * ( ) {
185+ const first = yield * scope . wrap ( {
186+ command : "agent" ,
187+ args : [ ] ,
188+ name : "agent" ,
189+ threadId : "a" ,
190+ } ) ;
191+ expect ( first . args ) . toContain ( `--slice=${ ownedAgentSlice ( stateDir ) } ` ) ;
192+ expect ( first . args ) . toContain ( "--property=OOMPolicy=stop" ) ;
193+ yield * scope . wrap ( { command : "agent" , args : [ ] , name : "agent" , threadId : "b" } ) ;
194+ const limits = calls . filter ( ( args ) => args . includes ( "set-property" ) ) ;
195+ expect ( limits ) . toHaveLength ( memoryPolicy === "host-managed" ? 0 : 1 ) ;
196+ if ( memoryPolicy !== "host-managed" ) {
197+ expect ( limits [ 0 ] ) . toContain ( "app-t3code-agents.slice" ) ;
198+ expect ( limits [ 0 ] ?. some ( ( arg ) => arg . startsWith ( "MemoryHigh=" ) ) ) . toBe ( true ) ;
199+ expect ( limits [ 0 ] ?. some ( ( arg ) => arg . startsWith ( "MemoryMax=" ) ) ) . toBe ( true ) ;
200+ }
201+ units . set ( unitOf ( first ) , { activeState : "failed" , result : "oom-kill" } ) ;
202+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( true ) ;
203+ yield * scope . clear ( "a" ) ;
204+ expect ( yield * scope . oomKilled ( "a" ) ) . toBe ( false ) ;
205+ } ) ,
206+ memoryPolicy === undefined ? { } : { memoryPolicy } ,
207+ ) ,
208+ ) ;
209+
170210 it . effect ( "labels a thread from its newest scope until its next session clears it" , ( ) =>
171211 withFakeSystemd ( ( { scope, units } ) =>
172212 Effect . gen ( function * ( ) {
0 commit comments