@devries-cameron20 / t3code-1 / commits / f9e45e4363
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): agents on hosts without swap are OOM-killed instead of throttled for hours (#39287)

Julius Marminge committed Nov 19, 2026 at 12:44 UTC f9e45e436317417bea136f3461662a0f51ea10b9
2 files changed +28 -12
apps/server/src/process/agentScope.test.ts
+12 -3
@@ -21,7 +21,7 @@ import * as ProviderProcesses from "./providerProcesses.ts";
21 import { classifyScope, make, ownedAgentSlice, type ScopeState } from "./agentScope.ts";
22
23 const GiB = 1024 ** 3;
24 -const memory = vi.hoisted(() => ({ total: 16 * 1024 ** 3 }));
24 +const memory = vi.hoisted(() => ({ total: 16 * 1024 ** 3, swapKb: 2 * 1024 ** 2 }));
25 vi.mock("node:os", async (importOriginal) => ({
26 ...(await importOriginal<typeof import("node:os")>()),
27 totalmem: () => memory.total,
@@ -133,7 +133,9 @@ const withFakeSystemd = <A, E>(
133 Effect.provideService(FileSystem.FileSystem, {
134 ...fileSystem,
135 readFileString: (path, encoding) =>
136 - fileSystem.readFileString(path.replace("/sys/fs/cgroup", cgroupRoot), encoding),
136 + path === "/proc/meminfo"
137 + ? Effect.sync(() => `MemTotal: 16384 kB\nSwapTotal: ${memory.swapKb} kB\n`)
138 + : fileSystem.readFileString(path.replace("/sys/fs/cgroup", cgroupRoot), encoding),
139 writeFileString: (path, data, options) =>
140 fileSystem.writeFileString(path.replace("/sys/fs/cgroup", cgroupRoot), data, options),
141 }),
@@ -331,8 +333,9 @@ describe("AgentScope service", () => {
333 ),
334 );
335
334 - it.effect("follows a live memory change at the next launch", () => {
336 + it.effect("follows a live memory or swap change at the next launch", () => {
337 memory.total = 16 * GiB;
338 + memory.swapKb = 2 * 1024 ** 2;
339 return withFakeSystemd(({ scope, calls, failingVerbs }) =>
340 Effect.gen(function* () {
341 const launch = scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
@@ -361,6 +364,12 @@ describe("AgentScope service", () => {
364 grown,
365 grown,
366 ]);
367 +
368 + // Without swap, MemoryHigh only throttles; MemoryMax must OOM-kill the runaway.
369 + memory.swapKb = 0;
370 + yield* launch;
371 + expect(memoryLimits().at(-1)).toEqual(["MemoryHigh=infinity", `MemoryMax=${18 * GiB}`]);
372 + memory.swapKb = 2 * 1024 ** 2;
373 }),
374 );
375 });
apps/server/src/process/agentScope.ts
+16 -9
@@ -36,14 +36,13 @@ export function ownedAgentSlice(stateDir: string): string {
36 const GiB = 1024 ** 3;
37
38 /**
39 - * Agents together leave about 6 GB free before they are throttled and 4 GB
40 - * before the kernel kills one, so the server, sshd, and tailscaled keep
41 - * working. Small machines still give agents at least half of RAM before
42 - * throttling and three quarters before a kill.
39 + * Agents leave about 6 GB free before throttling and 4 GB before an OOM kill (at
40 + * least half and three quarters of RAM go to agents) so the server and sshd keep
41 + * working. Without swap, MemoryHigh stalls every agent and never reaches the kill.
42 */
44 -function agentSliceMemoryLimits(totalBytes: number) {
43 +function agentSliceMemoryLimits(totalBytes: number, hasSwap: boolean) {
44 return {
46 - high: Math.max(totalBytes - 6 * GiB, Math.floor(totalBytes / 2)),
45 + high: hasSwap ? Math.max(totalBytes - 6 * GiB, Math.floor(totalBytes / 2)) : "infinity",
46 max: Math.max(totalBytes - 4 * GiB, Math.floor(totalBytes * 0.75)),
47 };
48 }
@@ -204,13 +203,20 @@ export const make = Effect.gen(function* () {
203 });
204 // VMs and containers can resize RAM live, so each launch re-checks the total
205 // the limits came from. A failed update is retried by the next launch.
206 + // An unreadable meminfo keeps the throttle, as before swap was checked.
207 + const readHasSwap = fileSystem.readFileString("/proc/meminfo").pipe(
208 + Effect.map((text) => /^SwapTotal:\s*0 kB$/m.exec(text) === null),
209 + Effect.orElseSucceed(() => true),
210 + );
211 let appliedTotal: number | undefined;
212 + let appliedHasSwap: boolean | undefined;
213 const memoryLimitsLock = yield* Semaphore.make(1);
214 const applyMemoryLimits = memoryLimitsLock.withPermits(1)(
215 Effect.gen(function* () {
216 const total = NodeOS.totalmem();
212 - if (total === appliedTotal) return;
213 - const limits = agentSliceMemoryLimits(total);
217 + const hasSwap = yield* readHasSwap;
218 + if (total === appliedTotal && hasSwap === appliedHasSwap) return;
219 + const limits = agentSliceMemoryLimits(total, hasSwap);
220 const applied = yield* setProperties(AGENT_SLICE, [
221 `MemoryHigh=${limits.high}`,
222 `MemoryMax=${limits.max}`,
@@ -225,13 +231,14 @@ export const make = Effect.gen(function* () {
231 });
232 }
233 appliedTotal = total;
234 + appliedHasSwap = hasSwap;
235 }),
236 );
237 yield* applyMemoryLimits;
238 yield* setProperties(AGENTS_PARENT_SLICE, AGENTS_PARENT_PROPERTIES);
239 yield* Effect.logInfo("Agent scopes are on", {
240 slice: AGENT_SLICE,
234 - ...agentSliceMemoryLimits(NodeOS.totalmem()),
241 + ...agentSliceMemoryLimits(NodeOS.totalmem(), yield* readHasSwap),
242 oomPolicy: supportsOomPolicy,
243 });
244 return {