@@ -166,18 +166,9 @@ export const make = Effect.gen(function* () {
166166 "--" ,
167167 "true" ,
168168 ] ) . pipe ( Effect . map ( ( result ) => result . _tag === "Some" && result . value . code === 0 ) ) ;
169- // Stop the whole scope when the kernel kills one of its processes, and
170- // record the result as oom-kill. Scopes take OOMPolicy from systemd 253.
171- // Older user managers get scopes without it: 252 still stops them on an
172- // OOM kill and records it, 251 and older do neither.
173- const stopOptions = [ STOP_TIMEOUT_OPTION ] ;
174- const oomOptions = [ ...stopOptions , "--property=OOMPolicy=stop" ] ;
175- const scopeOptions = ( yield * probe ( oomOptions ) )
176- ? oomOptions
177- : ( yield * probe ( stopOptions ) )
178- ? stopOptions
179- : undefined ;
180- if ( scopeOptions === undefined ) {
169+ // Scopes take OOMPolicy from systemd 253; older managers still get scopes.
170+ const supportsOomPolicy = yield * probe ( [ STOP_TIMEOUT_OPTION , "--property=OOMPolicy=stop" ] ) ;
171+ if ( ! supportsOomPolicy && ! ( yield * probe ( [ STOP_TIMEOUT_OPTION ] ) ) ) {
181172 yield * Effect . logInfo ( "Agent scopes are off: no systemd user manager" ) ;
182173 return undefined ;
183174 }
@@ -198,17 +189,13 @@ export const make = Effect.gen(function* () {
198189 yield * Effect . logInfo ( "Agent scopes are on" , {
199190 slice : AGENT_SLICE ,
200191 ...limits ,
201- oomPolicy : scopeOptions . length > 0 ,
192+ oomPolicy : supportsOomPolicy ,
202193 } ) ;
203194 return {
204195 systemdRun,
205196 systemctl,
206197 runtimeDir,
207- scopeOptions,
208- // A shared process keeps serving its other threads when the kernel
209- // kills one of its tools, so its scope keeps running instead.
210- sharedScopeOptions :
211- scopeOptions === oomOptions ? [ ...stopOptions , "--property=OOMPolicy=continue" ] : [ ] ,
198+ supportsOomPolicy,
212199 } ;
213200 } ) ,
214201 ) ;
@@ -330,22 +317,25 @@ export const make = Effect.gen(function* () {
330317 const scope = yield * systemd ;
331318 if ( scope === undefined ) return agentScopeCommand ( { command : resolved , args } ) ;
332319 const unit = `t3code-${ name } -${ NodeCrypto . randomUUID ( ) . slice ( 0 , 8 ) } .scope` ;
333- // With OOMPolicy=continue systemd never records `Result=oom-kill`, so a
334- // shared scope answers for no thread. Launching one still ends the
335- // thread's previous answer. Without OOMPolicy systemd may still stop
336- // the scope and record it, so the thread tracks it like any other.
337- const continuing = shared === true && scope . sharedScopeOptions . length > 0 ;
320+ // A continuing shared scope cannot attribute a tool's OOM to one thread.
321+ // Clear the launch thread's previous scope instead.
322+ const continuing = shared === true && scope . supportsOomPolicy ;
338323 const owned = threadId !== undefined && ! continuing ;
339324 if ( threadId !== undefined ) yield * track ( scope . systemctl , threadId , owned ? unit : undefined ) ;
340- const options = continuing ? scope . sharedScopeOptions : scope . scopeOptions ;
341325 const systemdScope : AgentSystemdScope = {
342326 systemdRun : scope . systemdRun ,
343327 runtimeDir : scope . runtimeDir ,
344328 slice : survivesRestart === true ? AGENT_SLICE : ownedSlice ,
345329 unit,
346330 // Without an owning thread nothing reads the result, so let systemd
347331 // unload the unit even when it fails.
348- options : owned ? options : [ ...options , "--collect" ] ,
332+ options : [
333+ STOP_TIMEOUT_OPTION ,
334+ ...( scope . supportsOomPolicy
335+ ? [ `--property=OOMPolicy=${ continuing ? "continue" : "stop" } ` ]
336+ : [ ] ) ,
337+ ...( owned ? [ ] : [ "--collect" ] ) ,
338+ ] ,
349339 } ;
350340 return agentScopeCommand ( { command : resolved , args, scope : systemdScope } ) ;
351341 } ) ,
0 commit comments