Skip to content

Commit 425659b

Browse files
fix(server): detect shared Codex OOM policy explicitly
1 parent f6f0620 commit 425659b

3 files changed

Lines changed: 17 additions & 31 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,6 @@ export const makeCodexAppServerSpawnCommand = Effect.fn(
13661366
name: "codex",
13671367
threadId: input.threadId,
13681368
env: input.env,
1369-
// Every thread on a provider instance uses this app-server.
13701369
shared: true,
13711370
});
13721371
const spawnCommand = yield* resolveSpawnCommand(launch.command, launch.args, {

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,9 @@ export const make = Effect.gen(function* () {
166166
"--",
167167
"true",
168168
]).pipe(Effect.map((result) => result._tag === "Some" && result.value.code === 0));
169-
// Stop the whole scope when the kernel kills one of its processes, and
170-
// record the result as oom-kill. Scopes take OOMPolicy from systemd 253.
171-
// Older user managers get scopes without it: 252 still stops them on an
172-
// OOM kill and records it, 251 and older do neither.
173-
const stopOptions = [STOP_TIMEOUT_OPTION];
174-
const oomOptions = [...stopOptions, "--property=OOMPolicy=stop"];
175-
const scopeOptions = (yield* probe(oomOptions))
176-
? oomOptions
177-
: (yield* probe(stopOptions))
178-
? stopOptions
179-
: undefined;
180-
if (scopeOptions === undefined) {
169+
// Scopes take OOMPolicy from systemd 253; older managers still get scopes.
170+
const supportsOomPolicy = yield* probe([STOP_TIMEOUT_OPTION, "--property=OOMPolicy=stop"]);
171+
if (!supportsOomPolicy && !(yield* probe([STOP_TIMEOUT_OPTION]))) {
181172
yield* Effect.logInfo("Agent scopes are off: no systemd user manager");
182173
return undefined;
183174
}
@@ -198,17 +189,13 @@ export const make = Effect.gen(function* () {
198189
yield* Effect.logInfo("Agent scopes are on", {
199190
slice: AGENT_SLICE,
200191
...limits,
201-
oomPolicy: scopeOptions.length > 0,
192+
oomPolicy: supportsOomPolicy,
202193
});
203194
return {
204195
systemdRun,
205196
systemctl,
206197
runtimeDir,
207-
scopeOptions,
208-
// A shared process keeps serving its other threads when the kernel
209-
// kills one of its tools, so its scope keeps running instead.
210-
sharedScopeOptions:
211-
scopeOptions === oomOptions ? [...stopOptions, "--property=OOMPolicy=continue"] : [],
198+
supportsOomPolicy,
212199
};
213200
}),
214201
);
@@ -330,22 +317,25 @@ export const make = Effect.gen(function* () {
330317
const scope = yield* systemd;
331318
if (scope === undefined) return agentScopeCommand({ command: resolved, args });
332319
const unit = `t3code-${name}-${NodeCrypto.randomUUID().slice(0, 8)}.scope`;
333-
// With OOMPolicy=continue systemd never records `Result=oom-kill`, so a
334-
// shared scope answers for no thread. Launching one still ends the
335-
// thread's previous answer. Without OOMPolicy systemd may still stop
336-
// the scope and record it, so the thread tracks it like any other.
337-
const continuing = shared === true && scope.sharedScopeOptions.length > 0;
320+
// A continuing shared scope cannot attribute a tool's OOM to one thread.
321+
// Clear the launch thread's previous scope instead.
322+
const continuing = shared === true && scope.supportsOomPolicy;
338323
const owned = threadId !== undefined && !continuing;
339324
if (threadId !== undefined) yield* track(scope.systemctl, threadId, owned ? unit : undefined);
340-
const options = continuing ? scope.sharedScopeOptions : scope.scopeOptions;
341325
const systemdScope: AgentSystemdScope = {
342326
systemdRun: scope.systemdRun,
343327
runtimeDir: scope.runtimeDir,
344328
slice: survivesRestart === true ? AGENT_SLICE : ownedSlice,
345329
unit,
346330
// Without an owning thread nothing reads the result, so let systemd
347331
// unload the unit even when it fails.
348-
options: owned ? options : [...options, "--collect"],
332+
options: [
333+
STOP_TIMEOUT_OPTION,
334+
...(scope.supportsOomPolicy
335+
? [`--property=OOMPolicy=${continuing ? "continue" : "stop"}`]
336+
: []),
337+
...(owned ? [] : ["--collect"]),
338+
],
349339
};
350340
return agentScopeCommand({ command: resolved, args, scope: systemdScope });
351341
}),

‎packages/shared/src/AgentScope.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ export interface AgentScopeShape {
1515
*/
1616
readonly survivesRestart?: boolean | undefined;
1717
/**
18-
* Several threads use this process, as with the Codex app-server. On
19-
* systemd 253 or newer its scope runs with OOMPolicy=continue: it keeps
20-
* running when the kernel kills one of its processes for memory, and
21-
* `oomKilled` answers false for it at once. Older systemd treats it like
22-
* any other scope.
18+
* Keep a shared process running after a tool OOM when supported.
19+
* Falls back to ordinary scope behavior on older systemd.
2320
*/
2421
readonly shared?: boolean | undefined;
2522
}) => Effect.Effect<AgentScopeCommand>;

0 commit comments

Comments
 (0)