@@ -19,6 +19,12 @@ import * as ServerConfig from "../config.ts";
1919import * as ProcessRunner from "../processRunner.ts" ;
2020
2121const AGENT_SLICE = "app-t3code-agents.slice" ;
22+ // Owns every agent slice and competes with the server's unit or scope under app.slice.
23+ const AGENTS_PARENT_SLICE = "app-t3code.slice" ;
24+ // Low weights yield CPU and disk to the server under contention, where the user manager
25+ // has those controllers: current systemd delegates cpu, rarely io. TasksMax keeps agents
26+ // below logind's 33% per-user task limit, leaving forks for the server.
27+ const AGENTS_PARENT_PROPERTIES = [ "CPUWeight=10" , "IOWeight=10" , "TasksMax=25%" ] ;
2228
2329export function ownedAgentSlice ( stateDir : string ) : string {
2430 const hash = NodeCrypto . createHash ( "sha256" ) . update ( stateDir ) . digest ( "hex" ) . slice ( 0 , 12 ) ;
@@ -173,20 +179,28 @@ export const make = Effect.gen(function* () {
173179 yield * Effect . logInfo ( "Agent scopes are off: no systemd user manager" ) ;
174180 return undefined ;
175181 }
182+ const setProperties = Effect . fn ( "agentScope.setProperties" ) ( function * (
183+ slice : string ,
184+ properties : ReadonlyArray < string > ,
185+ ) {
186+ const result = yield * run ( systemctl , [
187+ "--user" ,
188+ "set-property" ,
189+ "--runtime" ,
190+ slice ,
191+ ...properties ,
192+ ] ) ;
193+ if ( result . _tag === "None" || result . value . code !== 0 ) {
194+ yield * Effect . logWarning ( "Could not set resource limits on an agents slice" , {
195+ slice,
196+ properties,
197+ stderr : result . _tag === "Some" ? result . value . stderr . trim ( ) : undefined ,
198+ } ) ;
199+ }
200+ } ) ;
176201 const limits = agentSliceMemoryLimits ( NodeOS . totalmem ( ) ) ;
177- const limited = yield * run ( systemctl , [
178- "--user" ,
179- "set-property" ,
180- "--runtime" ,
181- AGENT_SLICE ,
182- `MemoryHigh=${ limits . high } ` ,
183- `MemoryMax=${ limits . max } ` ,
184- ] ) ;
185- if ( limited . _tag === "None" || limited . value . code !== 0 ) {
186- yield * Effect . logWarning ( "Could not set memory limits on the agents slice" , {
187- stderr : limited . _tag === "Some" ? limited . value . stderr . trim ( ) : undefined ,
188- } ) ;
189- }
202+ yield * setProperties ( AGENT_SLICE , [ `MemoryHigh=${ limits . high } ` , `MemoryMax=${ limits . max } ` ] ) ;
203+ yield * setProperties ( AGENTS_PARENT_SLICE , AGENTS_PARENT_PROPERTIES ) ;
190204 yield * Effect . logInfo ( "Agent scopes are on" , {
191205 slice : AGENT_SLICE ,
192206 ...limits ,
0 commit comments