Skip to content

Commit 3832199

Browse files
galel12cursoragent
andcommitted
fix(server): delegate cgroup controllers to agent scopes
Agent systemd scopes only inherited memory/pids, so rootless kind/Podman (and similar nested cgroups) failed inside agent sessions. Enable Delegate on parent/agent slices and scope units so cpu/cpuset/io are available to nested containers. Co-authored-by: Cursor <[email protected]>
1 parent 1451e7c commit 3832199

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎apps/server/src/process/agentScope.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,25 @@ describe("AgentScope service", () => {
189189
}),
190190
),
191191
);
192+
193+
it.effect("delegates cgroup controllers to agent scopes and slices", () =>
194+
withFakeSystemd(({ scope, calls, stateDir }) =>
195+
Effect.gen(function* () {
196+
const agent = yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
197+
expect(agent.args).toContain("--property=Delegate=yes");
198+
expect(agent.args).toContain("--property=DelegateControllers=cpu cpuset io memory pids");
199+
expect(agent.args).toContain("--property=OOMPolicy=stop");
200+
201+
const setProperty = calls.filter(([, verb]) => verb === "set-property");
202+
const delegatedSlices = setProperty
203+
.filter((args) => args.includes("Delegate=yes"))
204+
.map((args) => args[3]);
205+
expect(delegatedSlices).toEqual([
206+
"app-t3code.slice",
207+
"app-t3code-agents.slice",
208+
ownedAgentSlice(stateDir),
209+
]);
210+
}),
211+
),
212+
);
192213
});

‎apps/server/src/process/agentScope.ts‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
8787
const 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";
88102
const STOPPING_POLL_INTERVAL = "100 millis";
89103
const STOPPING_POLL_ATTEMPTS = 20;
90104
const 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

Comments
 (0)