Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions apps/server/src/process/agentScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, expect, it } from "@effect/vitest";
import { AgentScope } from "@t3tools/shared/AgentScope";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -186,8 +187,12 @@ const exitedAgent = (

const stopsOf = (calls: ReadonlyArray<ReadonlyArray<string>>) =>
calls.filter(([, verb]) => verb === "stop");
const launchCodex = (scope: Effect.Success<typeof make>, threadId: string) =>
makeCodexAppServerSpawnCommand({ command: "agent", args: ["app-server"], threadId }).pipe(
const launchCodex = (
scope: Effect.Success<typeof make>,
threadId: string,
env?: NodeJS.ProcessEnv,
) =>
makeCodexAppServerSpawnCommand({ command: "agent", args: ["app-server"], threadId, env }).pipe(
Effect.provideService(AgentScope, scope),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.map((command) => {
Expand Down Expand Up @@ -227,7 +232,14 @@ describe("AgentScope service", () => {
units.set(unitOf(agent), { activeState: "failed", result: "oom-kill" });
expect(yield* scope.oomKilled("a")).toBe(true);

const codex = yield* launchCodex(scope, "a");
const executable = String(agent.args.at(-1));
const codex = yield* launchCodex(scope, "a", {
PATH: NodePath.relative(process.cwd(), NodePath.dirname(executable)),
});
// Anchored to the cwd for systemd-run, with its ".." kept; it names the same file.
const anchored = String(codex.args.at(-2));
expect(NodePath.isAbsolute(anchored)).toBe(true);
expect(NodePath.resolve(anchored)).toBe(executable);
expect(codex.args).toContain("--property=OOMPolicy=continue");
expect(codex.args).not.toContain("--property=OOMPolicy=stop");
expect(codex.args).toContain("--property=TimeoutStopSec=10s");
Expand All @@ -243,6 +255,33 @@ describe("AgentScope service", () => {
),
);

it.effect.skipIf(!symlinksSupported)(
"keeps a symlinked parent in absolute and relative executable paths",
() =>
withFakeSystemd(({ scope }) =>
Effect.gen(function* () {
const plain = yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "s" });
const bin = NodePath.dirname(String(plain.args.at(-1)));
NodeFS.mkdirSync(NodePath.join(bin, "real", "sub"), { recursive: true });
NodeFS.writeFileSync(NodePath.join(bin, "real", "agent"), "", { mode: 0o755 });
NodeFS.symlinkSync(NodePath.join(bin, "real", "sub"), NodePath.join(bin, "link"));
// The kernel follows the link before "..", so this names real/agent, not bin/agent.
const command = `${NodePath.join(bin, "link")}/../agent`;
const wrapped = yield* scope.wrap({ command, args: [], name: "t", threadId: "s" });
expect(wrapped.args.at(-1)).toBe(command);
// A relative path is anchored to the cwd the same way, without collapsing "..".
const relative = `${NodePath.relative(process.cwd(), NodePath.join(bin, "link"))}/../agent`;
const anchored = yield* scope.wrap({
command: relative,
args: [],
name: "t",
threadId: "s",
});
expect(anchored.args.at(-1)).toBe(`${process.cwd()}${NodePath.sep}${relative}`);
}),
),
);

it.effect.each([{ oomPolicy: true }, { oomPolicy: false }])(
"Force stop kills a thread's own scope but never a shared Codex scope (OOM policy $oomPolicy)",
({ oomPolicy }) =>
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/process/agentScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,15 @@ export const make = Effect.gen(function* () {
...(owned ? [] : ["--collect"]),
],
};
return { ...agentScopeCommand({ command: resolved, args, scope: systemdScope }), unit };
// systemd-run does not search the caller's cwd, so anchor a relative path to it. Keep ".."
// as spelled: collapsing it would skip a symlinked parent directory.
const executable = path.isAbsolute(resolved)
? resolved
: `${process.cwd()}${path.sep}${resolved}`;
return {
...agentScopeCommand({ command: executable, args, scope: systemdScope }),
unit,
};
}),
oomKilled,
stop,
Expand Down
Loading
Loading