Skip to content

Commit 4da5e5c

Browse files
committed
feat(server): 允许宿主管理代理切片内存限额
通过显式环境配置跳过自动 MemoryHigh 和 MemoryMax 覆盖,保持默认自动策略。 保留 systemd 作用域、清理与 OOM 识别,并说明共享切片和旧运行时覆盖的处理。 新增四种配置的服务回归验证,针对性检查通过。
1 parent 90c455f commit 4da5e5c

3 files changed

Lines changed: 82 additions & 15 deletions

File tree

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ const withFakeSystemd = <A, E>(
5555
{
5656
oomPolicy = true,
5757
stopFails = false,
58-
}: { readonly oomPolicy?: boolean; readonly stopFails?: boolean } = {},
58+
memoryPolicy,
59+
}: {
60+
readonly oomPolicy?: boolean;
61+
readonly stopFails?: boolean;
62+
readonly memoryPolicy?: string;
63+
} = {},
5964
) =>
6065
Effect.gen(function* () {
6166
const bin = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-agent-scope-"));
@@ -105,7 +110,11 @@ const withFakeSystemd = <A, E>(
105110
const scope = yield* make.pipe(
106111
Effect.provideService(ProcessRunner.ProcessRunner, runner),
107112
Effect.provideService(HostProcess.Platform, "linux"),
108-
Effect.provideService(HostProcess.Environment, { PATH: bin, XDG_RUNTIME_DIR: "/run/user/1" }),
113+
Effect.provideService(HostProcess.Environment, {
114+
PATH: bin,
115+
XDG_RUNTIME_DIR: "/run/user/1",
116+
...(memoryPolicy === undefined ? {} : { T3CODE_AGENT_SLICE_MEMORY_POLICY: memoryPolicy }),
117+
}),
109118
Effect.provideService(FileSystem.FileSystem, {
110119
...fileSystem,
111120
readFileString: (path, encoding) =>
@@ -167,6 +176,37 @@ const launchCodex = (scope: Effect.Success<typeof make>, threadId: string) =>
167176
);
168177

169178
describe("AgentScope service", () => {
179+
it.effect.each([undefined, "automatic", "host-managed", "invalid"])(
180+
"preserves scopes and OOM handling with memory policy %s",
181+
(memoryPolicy) =>
182+
withFakeSystemd(
183+
({ scope, units, calls, stateDir }) =>
184+
Effect.gen(function* () {
185+
const first = yield* scope.wrap({
186+
command: "agent",
187+
args: [],
188+
name: "agent",
189+
threadId: "a",
190+
});
191+
expect(first.args).toContain(`--slice=${ownedAgentSlice(stateDir)}`);
192+
expect(first.args).toContain("--property=OOMPolicy=stop");
193+
yield* scope.wrap({ command: "agent", args: [], name: "agent", threadId: "b" });
194+
const limits = calls.filter((args) => args.includes("set-property"));
195+
expect(limits).toHaveLength(memoryPolicy === "host-managed" ? 0 : 1);
196+
if (memoryPolicy !== "host-managed") {
197+
expect(limits[0]).toContain("app-t3code-agents.slice");
198+
expect(limits[0]?.some((arg) => arg.startsWith("MemoryHigh="))).toBe(true);
199+
expect(limits[0]?.some((arg) => arg.startsWith("MemoryMax="))).toBe(true);
200+
}
201+
units.set(unitOf(first), { activeState: "failed", result: "oom-kill" });
202+
expect(yield* scope.oomKilled("a")).toBe(true);
203+
yield* scope.clear("a");
204+
expect(yield* scope.oomKilled("a")).toBe(false);
205+
}),
206+
memoryPolicy === undefined ? {} : { memoryPolicy },
207+
),
208+
);
209+
170210
it.effect("labels a thread from its newest scope until its next session clears it", () =>
171211
withFakeSystemd(({ scope, units }) =>
172212
Effect.gen(function* () {

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,34 @@ export const make = Effect.gen(function* () {
172172
yield* Effect.logInfo("Agent scopes are off: no systemd user manager");
173173
return undefined;
174174
}
175-
const limits = agentSliceMemoryLimits(NodeOS.totalmem());
176-
const limited = yield* run(systemctl, [
177-
"--user",
178-
"set-property",
179-
"--runtime",
180-
AGENT_SLICE,
181-
`MemoryHigh=${limits.high}`,
182-
`MemoryMax=${limits.max}`,
183-
]);
184-
if (limited._tag === "None" || limited.value.code !== 0) {
185-
yield* Effect.logWarning("Could not set memory limits on the agents slice", {
186-
stderr: limited._tag === "Some" ? limited.value.stderr.trim() : undefined,
187-
});
175+
const memoryPolicy = hostEnvironment.T3CODE_AGENT_SLICE_MEMORY_POLICY;
176+
if (
177+
memoryPolicy !== undefined &&
178+
memoryPolicy !== "automatic" &&
179+
memoryPolicy !== "host-managed"
180+
) {
181+
yield* Effect.logWarning("Unknown agent slice memory policy; using automatic limits");
182+
}
183+
const limits =
184+
memoryPolicy === "host-managed" ? undefined : agentSliceMemoryLimits(NodeOS.totalmem());
185+
if (limits !== undefined) {
186+
const limited = yield* run(systemctl, [
187+
"--user",
188+
"set-property",
189+
"--runtime",
190+
AGENT_SLICE,
191+
`MemoryHigh=${limits.high}`,
192+
`MemoryMax=${limits.max}`,
193+
]);
194+
if (limited._tag === "None" || limited.value.code !== 0) {
195+
yield* Effect.logWarning("Could not set memory limits on the agents slice", {
196+
stderr: limited._tag === "Some" ? limited.value.stderr.trim() : undefined,
197+
});
198+
}
188199
}
189200
yield* Effect.logInfo("Agent scopes are on", {
190201
slice: AGENT_SLICE,
202+
memoryPolicy: limits === undefined ? "host-managed" : "automatic",
191203
...limits,
192204
oomPolicy: supportsOomPolicy,
193205
});

‎docs/user/background-service.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ service is still installed and will start at the next login.
8787

8888
Windows background services are not supported.
8989

90+
### Host-managed agent memory on Linux
91+
92+
By default, T3 Code applies automatic memory limits to the systemd user slice
93+
`app-t3code-agents.slice`. If you manage that slice's memory budget yourself, set
94+
`T3CODE_AGENT_SLICE_MEMORY_POLICY=host-managed` in the server's environment (or
95+
its `service.env`) and restart the server. T3 Code then leaves `MemoryHigh` and
96+
`MemoryMax` untouched while keeping per-thread scopes, cleanup and OOM reporting.
97+
Unset the variable or set it to `automatic` to restore automatic limits on restart.
98+
99+
The slice is shared by T3 servers running as the same OS user, so configure all
100+
of them consistently. This option does not remove runtime overrides written by
101+
earlier starts. Apply your intended limits with `systemctl --user set-property
102+
--runtime app-t3code-agents.slice MemoryHigh=… MemoryMax=…`, or restart the user
103+
manager to clear runtime overrides and load your persistent slice configuration.
104+
90105
T3 Connect can offer service installation during setup, but the two are managed
91106
separately. Signing out of T3 Connect does not stop or uninstall the service.
92107

0 commit comments

Comments
 (0)