@@ -85,6 +85,20 @@ function parseCgroupCounter(text: string, key: string): number | undefined {
8585// A stop sends SIGTERM, then SIGKILL after this long. systemd's 90 s default
8686// would outlast the wait for a previous server's agents below.
8787const STOP_TIMEOUT_OPTION = "--property=TimeoutStopSec=10s" ;
88+ // Nested agent scopes inherit only the controllers enabled on their parent
89+ // slices. Without Delegate, rootless containers (kind + Podman, Docker rootless)
90+ // fail cgroup setup when tools like make deploy run inside an agent session.
91+ const DELEGATE_OPTIONS = [
92+ "--property=Delegate=yes" ,
93+ "--property=DelegateControllers=cpu cpuset io memory pids" ,
94+ ] as const ;
95+ const DELEGATE_PROPERTIES = [
96+ "Delegate=yes" ,
97+ "DelegateControllers=cpu cpuset io memory pids" ,
98+ ] as const ;
99+ // systemd derives parent slice names from the leaf name, so
100+ // app-t3code-agents.slice always lives under app-t3code.slice.
101+ const PARENT_SLICE = "app-t3code.slice" ;
88102const STOPPING_POLL_INTERVAL = "100 millis" ;
89103const STOPPING_POLL_ATTEMPTS = 20 ;
90104const CLEAR_FAILED_INTERVAL = "1 second" ;
@@ -169,13 +183,21 @@ export const make = Effect.gen(function* () {
169183 // Stop the whole scope when the kernel kills one of its processes, and
170184 // record the result as oom-kill. Scopes take OOMPolicy from systemd 253;
171185 // older user managers still get scopes, without the OOM label.
186+ // Prefer Delegate-enabled options so nested cgroups can enable cpu/cpuset/io.
187+ // Fall back when the local systemd rejects those properties.
172188 const stopOptions = [ STOP_TIMEOUT_OPTION ] ;
189+ const stopWithDelegate = [ ...stopOptions , ...DELEGATE_OPTIONS ] ;
173190 const oomOptions = [ ...stopOptions , "--property=OOMPolicy=stop" ] ;
174- const scopeOptions = ( yield * probe ( oomOptions ) )
175- ? oomOptions
176- : ( yield * probe ( stopOptions ) )
177- ? stopOptions
178- : undefined ;
191+ const oomWithDelegate = [ ...oomOptions , ...DELEGATE_OPTIONS ] ;
192+ const scopeOptions = ( yield * probe ( oomWithDelegate ) )
193+ ? oomWithDelegate
194+ : ( yield * probe ( oomOptions ) )
195+ ? oomOptions
196+ : ( yield * probe ( stopWithDelegate ) )
197+ ? stopWithDelegate
198+ : ( yield * probe ( stopOptions ) )
199+ ? stopOptions
200+ : undefined ;
179201 if ( scopeOptions === undefined ) {
180202 yield * Effect . logInfo ( "Agent scopes are off: no systemd user manager" ) ;
181203 return undefined ;
@@ -194,10 +216,27 @@ export const make = Effect.gen(function* () {
194216 stderr : limited . _tag === "Some" ? limited . value . stderr . trim ( ) : undefined ,
195217 } ) ;
196218 }
219+ // Delegate on parent and agent slices so controllers reach nested scopes.
220+ for ( const slice of [ PARENT_SLICE , AGENT_SLICE , ownedSlice ] ) {
221+ const delegated = yield * run ( systemctl , [
222+ "--user" ,
223+ "set-property" ,
224+ "--runtime" ,
225+ slice ,
226+ ...DELEGATE_PROPERTIES ,
227+ ] ) ;
228+ if ( delegated . _tag === "None" || delegated . value . code !== 0 ) {
229+ yield * Effect . logWarning ( "Could not enable cgroup Delegate on agents slice" , {
230+ slice,
231+ stderr : delegated . _tag === "Some" ? delegated . value . stderr . trim ( ) : undefined ,
232+ } ) ;
233+ }
234+ }
197235 yield * Effect . logInfo ( "Agent scopes are on" , {
198236 slice : AGENT_SLICE ,
199237 ...limits ,
200- oomPolicy : scopeOptions . length > 0 ,
238+ oomPolicy : scopeOptions . includes ( "--property=OOMPolicy=stop" ) ,
239+ delegate : scopeOptions . includes ( "--property=Delegate=yes" ) ,
201240 } ) ;
202241 return {
203242 systemdRun,
0 commit comments