Skip to content

Commit 8ed48ee

Browse files
galel12cursoragent
andcommitted
fix(server): use Delegate= controller list on agent scopes
systemd-run rejects DelegateControllers= and slices reject Delegate= via set-property. Pass controllers as Delegate= on scopes only so nested cgroups actually receive cpu/cpuset/io. Part of #21700 Co-authored-by: Cursor <[email protected]>
1 parent cd6a976 commit 8ed48ee

2 files changed

Lines changed: 16 additions & 45 deletions

File tree

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,16 @@ describe("AgentScope service", () => {
262262
),
263263
);
264264

265-
it.effect("delegates cgroup controllers to agent scopes and slices", () =>
266-
withFakeSystemd(({ scope, calls, stateDir }) =>
265+
it.effect("delegates cgroup controllers on agent scopes", () =>
266+
withFakeSystemd(({ scope, calls }) =>
267267
Effect.gen(function* () {
268268
const agent = yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
269-
expect(agent.args).toContain("--property=Delegate=yes");
270-
expect(agent.args).toContain("--property=DelegateControllers=cpu cpuset io memory pids");
269+
expect(agent.args).toContain("--property=Delegate=cpu cpuset io memory pids");
271270
expect(agent.args).toContain("--property=OOMPolicy=stop");
272-
273-
const setProperty = calls.filter(([, verb]) => verb === "set-property");
274-
const delegatedSlices = setProperty
275-
.filter((args) => args.includes("Delegate=yes"))
276-
.map((args) => args[3]);
277-
expect(delegatedSlices).toEqual([
278-
"app-t3code.slice",
279-
"app-t3code-agents.slice",
280-
ownedAgentSlice(stateDir),
281-
]);
271+
expect(calls.filter((args) => args.includes("Delegate=yes"))).toEqual([]);
272+
expect(
273+
calls.filter((args) => args.some((arg) => arg.startsWith("DelegateControllers="))),
274+
).toEqual([]);
282275
}),
283276
),
284277
);

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,9 @@ const STOP_TIMEOUT_OPTION = "--property=TimeoutStopSec=10s";
8888
// Nested agent scopes inherit only the controllers enabled on their parent
8989
// slices. Without Delegate, rootless containers (kind + Podman, Docker rootless)
9090
// 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";
91+
// Pass controllers via Delegate= (systemd-run rejects DelegateControllers=).
92+
// Systemd enables those controllers on ancestor slices when the scope starts.
93+
const DELEGATE_OPTION = "--property=Delegate=cpu cpuset io memory pids";
10294
const STOPPING_POLL_INTERVAL = "100 millis";
10395
const STOPPING_POLL_ATTEMPTS = 20;
10496
const CLEAR_FAILED_INTERVAL = "1 second";
@@ -187,9 +179,9 @@ export const make = Effect.gen(function* () {
187179
// Prefer Delegate-enabled options so nested cgroups can enable cpu/cpuset/io.
188180
// Fall back when the local systemd rejects those properties.
189181
const stopOptions = [STOP_TIMEOUT_OPTION];
190-
const stopWithDelegate = [...stopOptions, ...DELEGATE_OPTIONS];
182+
const stopWithDelegate = [...stopOptions, DELEGATE_OPTION];
191183
const oomOptions = [...stopOptions, "--property=OOMPolicy=stop"];
192-
const oomWithDelegate = [...oomOptions, ...DELEGATE_OPTIONS];
184+
const oomWithDelegate = [...oomOptions, DELEGATE_OPTION];
193185
const scopeOptions = (yield* probe(oomWithDelegate))
194186
? oomWithDelegate
195187
: (yield* probe(oomOptions))
@@ -217,33 +209,18 @@ export const make = Effect.gen(function* () {
217209
stderr: limited._tag === "Some" ? limited.value.stderr.trim() : undefined,
218210
});
219211
}
220-
// Delegate on parent and agent slices so controllers reach nested scopes.
221-
for (const slice of [PARENT_SLICE, AGENT_SLICE, ownedSlice]) {
222-
const delegated = yield* run(systemctl, [
223-
"--user",
224-
"set-property",
225-
"--runtime",
226-
slice,
227-
...DELEGATE_PROPERTIES,
228-
]);
229-
if (delegated._tag === "None" || delegated.value.code !== 0) {
230-
yield* Effect.logWarning("Could not enable cgroup Delegate on agents slice", {
231-
slice,
232-
stderr: delegated._tag === "Some" ? delegated.value.stderr.trim() : undefined,
233-
});
234-
}
235-
}
236212
yield* Effect.logInfo("Agent scopes are on", {
237213
slice: AGENT_SLICE,
238214
...limits,
239215
oomPolicy: scopeOptions.includes("--property=OOMPolicy=stop"),
240-
delegate: scopeOptions.includes("--property=Delegate=yes"),
216+
delegate: scopeOptions.includes(DELEGATE_OPTION),
241217
});
242218
return {
243219
systemdRun,
244220
systemctl,
245221
runtimeDir,
246-
supportsOomPolicy,
222+
supportsOomPolicy: scopeOptions.includes("--property=OOMPolicy=stop"),
223+
supportsDelegate: scopeOptions.includes(DELEGATE_OPTION),
247224
};
248225
}),
249226
);
@@ -382,6 +359,7 @@ export const make = Effect.gen(function* () {
382359
// unload the unit even when it fails.
383360
options: [
384361
STOP_TIMEOUT_OPTION,
362+
...(scope.supportsDelegate ? [DELEGATE_OPTION] : []),
385363
...(scope.supportsOomPolicy
386364
? [`--property=OOMPolicy=${continuing ? "continue" : "stop"}`]
387365
: []),

0 commit comments

Comments
 (0)