The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(server): agent slice memory limits follow live memory changes (#30581)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Omar Espinosa committed
Nov 14, 2026 at 22:50 UTC
9bea6f7eb0dee2ce6aefc5a4f584351e510397a8
2 files changed
+83
-11
apps/server/src/process/agentScope.test.ts
+46
-1
@@ -12,6 +12,7 @@ import * as FileSystem from "effect/FileSystem";
12
import * as Layer from "effect/Layer";
13
import * as Logger from "effect/Logger";
14
import { ChildProcess, ChildProcessSpawner } from "effect/process";
15
+import { vi } from "vite-plus/test";
16
17
import * as ServerConfig from "../config.ts";
18
import { makeCodexAppServerSpawnCommand } from "../orchestration-v2/Adapters/CodexAdapterV2.ts";
@@ -19,6 +20,13 @@ import * as ProcessRunner from "../processRunner.ts";
20
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 }));
25
+vi.mock("node:os", async (importOriginal) => ({
26
+ ...(await importOriginal<typeof import("node:os")>()),
27
+ totalmem: () => memory.total,
28
+}));
29
+
30
const running: ScopeState = {
31
loadState: "loaded",
32
activeState: "active",
@@ -49,6 +57,7 @@ const withFakeSystemd = <A, E>(
57
readonly scope: Effect.Success<typeof make>;
58
readonly units: Map<string, FakeUnit>;
59
readonly calls: ReadonlyArray<ReadonlyArray<string>>;
60
+ readonly failingVerbs: Set<string>;
61
readonly stateDir: string;
62
readonly cgroupRoot: string;
63
}) => Effect.Effect<A, E>,
@@ -73,6 +82,7 @@ const withFakeSystemd = <A, E>(
82
const cgroupRoot = NodePath.join(bin, "cgroup");
83
const units = new Map<string, FakeUnit>();
84
const calls: Array<ReadonlyArray<string>> = [];
85
+ const failingVerbs = new Set<string>();
86
const output = (stdout: string, code = 0): ProcessRunner.ProcessRunOutput => ({
87
stdout,
88
stderr: "",
@@ -91,6 +101,7 @@ const withFakeSystemd = <A, E>(
101
return output("", 1);
102
}
103
const [, verb, unit = ""] = args;
104
+ if (failingVerbs.has(verb!)) return output("", 1);
105
if (stopFails && verb === "stop") {
106
return {
107
...output(""),
@@ -127,7 +138,7 @@ const withFakeSystemd = <A, E>(
138
fileSystem.writeFileString(path.replace("/sys/fs/cgroup", cgroupRoot), data, options),
139
}),
140
);
130
- return yield* body({ scope, units, calls, stateDir, cgroupRoot }).pipe(
141
+ return yield* body({ scope, units, calls, failingVerbs, stateDir, cgroupRoot }).pipe(
142
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(bin, { recursive: true, force: true }))),
143
);
144
}).pipe(
@@ -320,6 +331,40 @@ describe("AgentScope service", () => {
331
),
332
);
333
334
+ it.effect("follows a live memory change at the next launch", () => {
335
+ memory.total = 16 * GiB;
336
+ return withFakeSystemd(({ scope, calls, failingVerbs }) =>
337
+ Effect.gen(function* () {
338
+ const launch = scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
339
+ const memoryLimits = () =>
340
+ calls
341
+ .filter(
342
+ ([, verb, , slice]) => verb === "set-property" && slice === "app-t3code-agents.slice",
343
+ )
344
+ .map((args) => args.slice(4));
345
+ yield* launch;
346
+ yield* launch;
347
+ expect(memoryLimits()).toEqual([[`MemoryHigh=${10 * GiB}`, `MemoryMax=${12 * GiB}`]]);
348
+
349
+ // A VM balloon or `pct set --memory` grows RAM while the server keeps running.
350
+ memory.total = 22 * GiB;
351
+ failingVerbs.add("set-property");
352
+ yield* launch;
353
+ yield* launch;
354
+ failingVerbs.delete("set-property");
355
+ yield* launch;
356
+ yield* launch;
357
+ const grown = [`MemoryHigh=${16 * GiB}`, `MemoryMax=${18 * GiB}`];
358
+ expect(memoryLimits()).toEqual([
359
+ [`MemoryHigh=${10 * GiB}`, `MemoryMax=${12 * GiB}`],
360
+ grown,
361
+ grown,
362
+ grown,
363
+ ]);
364
+ }),
365
+ );
366
+ });
367
+
368
it.effect("stops agents a killed server left before startup completes", () =>
369
withFakeSystemd(
370
({ scope, calls, stateDir }) =>
apps/server/src/process/agentScope.ts
+37
-10
@@ -14,6 +14,7 @@ import * as Effect from "effect/Effect";
14
import * as FileSystem from "effect/FileSystem";
15
import * as Layer from "effect/Layer";
16
import * as Path from "effect/Path";
17
+import * as Semaphore from "effect/Semaphore";
18
19
import * as ServerConfig from "../config.ts";
20
import * as ProcessRunner from "../processRunner.ts";
@@ -193,20 +194,44 @@ export const make = Effect.gen(function* () {
194
slice,
195
...properties,
196
]);
196
- if (result._tag === "None" || result.value.code !== 0) {
197
- yield* Effect.logWarning("Could not set resource limits on an agents slice", {
198
- slice,
199
- properties,
200
- stderr: result._tag === "Some" ? result.value.stderr.trim() : undefined,
201
- });
202
- }
197
+ if (result._tag === "Some" && result.value.code === 0) return true;
198
+ yield* Effect.logWarning("Could not set resource limits on an agents slice", {
199
+ slice,
200
+ properties,
201
+ stderr: result._tag === "Some" ? result.value.stderr.trim() : undefined,
202
+ });
203
+ return false;
204
});
204
- const limits = agentSliceMemoryLimits(NodeOS.totalmem());
205
- yield* setProperties(AGENT_SLICE, [`MemoryHigh=${limits.high}`, `MemoryMax=${limits.max}`]);
205
+ // VMs and containers can resize RAM live, so each launch re-checks the total
206
+ // the limits came from. A failed update is retried by the next launch.
207
+ let appliedTotal: number | undefined;
208
+ const memoryLimitsLock = yield* Semaphore.make(1);
209
+ const applyMemoryLimits = memoryLimitsLock.withPermits(1)(
210
+ Effect.gen(function* () {
211
+ const total = NodeOS.totalmem();
212
+ if (total === appliedTotal) return;
213
+ const limits = agentSliceMemoryLimits(total);
214
+ const applied = yield* setProperties(AGENT_SLICE, [
215
+ `MemoryHigh=${limits.high}`,
216
+ `MemoryMax=${limits.max}`,
217
+ ]);
218
+ if (!applied) return;
219
+ if (appliedTotal !== undefined) {
220
+ yield* Effect.logInfo("Agent memory limits follow a memory change", {
221
+ slice: AGENT_SLICE,
222
+ previousTotal: appliedTotal,
223
+ total,
224
+ ...limits,
225
+ });
226
+ }
227
+ appliedTotal = total;
228
+ }),
229
+ );
230
+ yield* applyMemoryLimits;
231
yield* setProperties(AGENTS_PARENT_SLICE, AGENTS_PARENT_PROPERTIES);
232
yield* Effect.logInfo("Agent scopes are on", {
233
slice: AGENT_SLICE,
209
- ...limits,
234
+ ...agentSliceMemoryLimits(NodeOS.totalmem()),
235
oomPolicy: supportsOomPolicy,
236
});
237
return {
@@ -214,6 +239,7 @@ export const make = Effect.gen(function* () {
239
systemctl,
240
runtimeDir,
241
supportsOomPolicy,
242
+ applyMemoryLimits,
243
};
244
}),
245
);
@@ -403,6 +429,7 @@ export const make = Effect.gen(function* () {
429
if (resolved === null) return { command, args };
430
const scope = yield* systemd;
431
if (scope === undefined) return agentScopeCommand({ command: resolved, args });
432
+ yield* scope.applyMemoryLimits;
433
const unit = `t3code-${name}-${NodeCrypto.randomUUID().slice(0, 8)}.scope`;
434
// A continuing shared scope cannot attribute a tool's OOM to one thread.
435
// Clear the launch thread's previous scope instead.