diff --git a/apps/server/src/process/agentScope.test.ts b/apps/server/src/process/agentScope.test.ts index 49674354b2532..353ac65d73f78 100644 --- a/apps/server/src/process/agentScope.test.ts +++ b/apps/server/src/process/agentScope.test.ts @@ -65,11 +65,13 @@ const withFakeSystemd = ( { oomPolicy = true, stopFails = false, + memoryPolicy, leftovers = false, liveOwner = false, }: { readonly oomPolicy?: boolean; readonly stopFails?: boolean; + readonly memoryPolicy?: string; readonly leftovers?: boolean; readonly liveOwner?: boolean; } = {}, @@ -129,7 +131,11 @@ const withFakeSystemd = ( Effect.provideService(ProviderProcesses.ProviderProcesses, { hasOtherLiveOwner: liveOwner }), Effect.provideService(ProcessRunner.ProcessRunner, runner), Effect.provideService(HostProcess.Platform, "linux"), - Effect.provideService(HostProcess.Environment, { PATH: bin, XDG_RUNTIME_DIR: "/run/user/1" }), + Effect.provideService(HostProcess.Environment, { + PATH: bin, + XDG_RUNTIME_DIR: "/run/user/1", + ...(memoryPolicy === undefined ? {} : { T3CODE_AGENT_SLICE_MEMORY_POLICY: memoryPolicy }), + }), Effect.provideService(FileSystem.FileSystem, { ...fileSystem, readFileString: (path, encoding) => @@ -365,6 +371,54 @@ describe("AgentScope service", () => { ); }); + it.effect( + "leaves agent memory limits to a host-managed policy, even after a memory change", + () => { + memory.total = 16 * GiB; + return withFakeSystemd( + ({ scope, calls }) => + Effect.gen(function* () { + const launch = scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" }); + yield* launch; + memory.total = 22 * GiB; + yield* launch; + expect(calls.filter(([, verb]) => verb === "set-property")).toEqual([ + [ + "--user", + "set-property", + "--runtime", + "app-t3code.slice", + "CPUWeight=10", + "IOWeight=10", + "TasksMax=25%", + ], + ]); + }), + { memoryPolicy: "host-managed" }, + ); + }, + ); + + it.effect("warns about an invalid memory policy without logging its value", () => { + const memoryPolicy = "invalid-".repeat(256); + const logs: Array = []; + const logger = Logger.make(({ message }) => logs.push(message)); + memory.total = 16 * GiB; + return withFakeSystemd( + ({ scope, calls }) => + Effect.gen(function* () { + yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" }); + expect(calls.some((args) => args.includes(`MemoryMax=${12 * GiB}`))).toBe(true); + expect(logs).toContainEqual([ + "Unknown agent slice memory policy; using automatic limits", + { memoryPolicy: "unknown", memoryPolicyLength: memoryPolicy.length }, + ]); + expect(JSON.stringify(logs)).not.toContain(memoryPolicy); + }), + { memoryPolicy }, + ).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))); + }); + it.effect("stops agents a killed server left before startup completes", () => withFakeSystemd( ({ scope, calls, stateDir }) => diff --git a/apps/server/src/process/agentScope.ts b/apps/server/src/process/agentScope.ts index 1e52e680b6f8d..4fb33d1468c81 100644 --- a/apps/server/src/process/agentScope.ts +++ b/apps/server/src/process/agentScope.ts @@ -202,12 +202,25 @@ export const make = Effect.gen(function* () { }); return false; }); + const memoryPolicy = hostEnvironment.T3CODE_AGENT_SLICE_MEMORY_POLICY; + if ( + memoryPolicy !== undefined && + memoryPolicy !== "automatic" && + memoryPolicy !== "host-managed" + ) { + yield* Effect.logWarning("Unknown agent slice memory policy; using automatic limits", { + memoryPolicy: "unknown", + memoryPolicyLength: memoryPolicy.length, + }); + } + const hostManagedMemory = memoryPolicy === "host-managed"; // VMs and containers can resize RAM live, so each launch re-checks the total // the limits came from. A failed update is retried by the next launch. let appliedTotal: number | undefined; const memoryLimitsLock = yield* Semaphore.make(1); const applyMemoryLimits = memoryLimitsLock.withPermits(1)( Effect.gen(function* () { + if (hostManagedMemory) return; const total = NodeOS.totalmem(); if (total === appliedTotal) return; const limits = agentSliceMemoryLimits(total); @@ -231,7 +244,8 @@ export const make = Effect.gen(function* () { yield* setProperties(AGENTS_PARENT_SLICE, AGENTS_PARENT_PROPERTIES); yield* Effect.logInfo("Agent scopes are on", { slice: AGENT_SLICE, - ...agentSliceMemoryLimits(NodeOS.totalmem()), + memoryPolicy: hostManagedMemory ? "host-managed" : "automatic", + ...(hostManagedMemory ? {} : agentSliceMemoryLimits(NodeOS.totalmem())), oomPolicy: supportsOomPolicy, }); return { diff --git a/docs/user/background-service.md b/docs/user/background-service.md index 1bc41addf8c96..655b78ca6e273 100644 --- a/docs/user/background-service.md +++ b/docs/user/background-service.md @@ -99,6 +99,22 @@ of signing out, and keep the PC awake, for unattended remote access. Installing over SSH while nobody is signed in at the PC can fail at the final start step; the service is still installed and will start at the next sign-in. +### Host-managed agent memory on Linux + +By default, T3 Code applies automatic memory limits to the systemd user slice +`app-t3code-agents.slice`. If you manage that slice's memory budget yourself, set +`T3CODE_AGENT_SLICE_MEMORY_POLICY=host-managed` in the server's environment (or +its `service.env`) and restart the server. T3 Code then leaves `MemoryHigh` and +`MemoryMax` untouched while keeping per-thread scopes, cleanup and OOM reporting. +Unset the variable or set it to `automatic` to restore automatic limits on restart. + +The slice is shared by T3 servers running as the same OS user, so configure all +of them consistently. This option does not remove runtime overrides written by +earlier starts. Apply your intended limits with `systemctl --user set-property +--runtime app-t3code-agents.slice MemoryHigh=… MemoryMax=…`. Restarting the server +or user manager does not clear those runtime configuration files; they can remain +until the next reboot. + `t3 up` installs the service when you set up T3 Connect, but the two are managed separately. Signing out of T3 Connect does not stop or uninstall the service.