Skip to content
Merged
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
96 changes: 78 additions & 18 deletions apps/server/src/preview/PortScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,11 @@ effectIt.effect.each(["linux", "darwin"] as const)(
}),
);
}
if (!input.args.includes("-E")) {
return Effect.succeed(
processOutput(" 4242 node server.js\n 5000 postgres -D /data\n"),
);
}
environmentReads += 1;
return Effect.succeed(
processOutput(
Expand Down Expand Up @@ -969,6 +974,7 @@ effectIt.effect("stops only a job's own process group", () =>
{ pid: 200, ppid: serverPid, pgid: 200 }, // agent CLI
{ pid: 210, ppid: 200, pgid: 200 }, // a server it spawned without a group of its own
{ pid: 300, ppid: 200, pgid: 300 }, // agent tool shell
{ pid: 301, ppid: 300, pgid: 300 }, // the server it pipes from
{ pid: 400, ppid: 300, pgid: 400 }, // `npm run dev &`
{ pid: 401, ppid: 400, pgid: 400 },
{ pid: 500, ppid: serverPid, pgid: 500 }, // terminal shell
Expand All @@ -980,17 +986,59 @@ effectIt.effect("stops only a job's own process group", () =>
for (const pid of [20, 210, 500, 999]) {
expect(PortScanner.stoppableProcessGroup(table, pid, serverPid)).toBeNull();
}
const commandsInGroup = (
variables: ReadonlyMap<number, ReadonlyMap<string, string>>,
group: number,
) =>
PortScanner.agentCommandsInGroup(
variables,
table.get(group)!,
rows.filter((row) => row.pgid === group).map((row) => row.pid),
);
// The tool shell is the command; its `npm run dev &` job only inherited the name.
const variables = new Map([
[200, new Map<string, string>()],
[300, new Map([["COMMAND", "a"]])],
[301, new Map([["COMMAND", "a"]])],
[400, new Map([["COMMAND", "a"]])],
]);
expect([...PortScanner.agentCommandsInGroup(variables, table.get(300)!)]).toEqual([
["COMMAND", "a"],
]);
expect(PortScanner.agentCommandsInGroup(variables, table.get(400)!).size).toBe(0);
expect(PortScanner.agentCommandsInGroup(variables, table.get(600)!).size).toBe(0);
expect([...commandsInGroup(variables, 300)]).toEqual([["COMMAND", "a"]]);
expect(commandsInGroup(variables, 400).size).toBe(0);
expect(commandsInGroup(variables, 600).size).toBe(0);
// macOS hides a system shell's environment: its name comes from the server it started, and a
// job under it cannot be told apart from the command.
const hiddenShell = new Map(variables);
hiddenShell.delete(300);
expect([...commandsInGroup(hiddenShell, 300)]).toEqual([["COMMAND", "a"]]);
expect(commandsInGroup(hiddenShell, 400).size).toBe(0);
}),
);

effectIt.effect("reads a macOS process's environment, never its arguments", () =>
Effect.sync(() => {
const withoutEnvironment = [
" 300 /bin/zsh -c COMMAND=spelled node server.js | cat",
" 301 /opt/homebrew/bin/node server.js COMMAND=argument",
" 302 /opt/homebrew/bin/node other.js",
" 303 /opt/homebrew/bin/node reused.js",
" 304 /opt/homebrew/bin/node server.js",
].join("\n");
const withEnvironment = [
" 300 /bin/zsh -c COMMAND=spelled node server.js | cat",
" 301 /opt/homebrew/bin/node server.js COMMAND=argument HOME=/u COMMAND=a",
" 302 /opt/homebrew/bin/node other.js HOME=/u",
" 303 /opt/homebrew/bin/node replaced.js COMMAND=b",
" 304 /opt/homebrew/bin/node server.json COMMAND=c",
].join("\n");
expect(
PortScanner.parsePsEnvironments(withEnvironment, withoutEnvironment, ["COMMAND"]),
).toEqual(
new Map([
[300, null],
[301, new Map([["COMMAND", "a"]])],
[302, new Map()],
]),
);
}),
);

Expand All @@ -1007,9 +1055,27 @@ server.listen(0, "127.0.0.1", () => {
const AGENT_COMMAND_ENV = "T3CODE_TEST_AGENT_COMMAND";

/**
* A real HTML server with an agent's environment, started the way an agent's tool shell starts a
* job: under a detached shell with job control, in a group of its own. Release kills only the pids
* it started.
* An agent CLI: it starts a command as a group leader named in the command's environment, the way
* Codex runs `zsh -lc`, and exits once the command has.
*/
const AGENT_CLI_SCRIPT = `
const command = require("node:child_process").spawn(
"/bin/sh",
["-c", '"$0" -e "$1" | cat', process.execPath, process.argv[1]],
{
detached: true,
env: { ...process.env, [process.argv[2]]: "command-live" },
stdio: ["ignore", "pipe", "ignore"],
},
);
command.stdout.pipe(process.stdout);
command.once("exit", () => process.exit());
`;

/**
* A real HTML server with an agent's environment, run by an agent CLI's command whose shell stays
* to pipe its output, so on macOS, which hides `/bin/sh`'s environment, the shell leading the
* group is unreadable. Release kills only the pids it started.
*/
const agentServer = () =>
Effect.acquireRelease(
Expand All @@ -1020,16 +1086,10 @@ const agentServer = () =>
readonly exited: Promise<void>;
}>((resume) => {
const env = { ...process.env, [PortScanner.AGENT_THREAD_ENV]: "thread-live" };
// dash ignores `set -m` without a terminal; bash, like agent tool shells, honors it.
const child = NodeChildProcess.spawn(
"bash",
[
"-c",
`set -m; ${AGENT_COMMAND_ENV}=command-live "$0" -e "$1" & wait`,
process.execPath,
AGENT_SERVER_SCRIPT,
],
{ detached: true, env, stdio: ["ignore", "pipe", "ignore"] },
process.execPath,
["-e", AGENT_CLI_SCRIPT, AGENT_SERVER_SCRIPT, AGENT_COMMAND_ENV],
{ env, stdio: ["ignore", "pipe", "ignore"] },
);
const exited = new Promise<void>((resolve) => child.once("exit", () => resolve()));
child.once("error", (error) => resume(Effect.die(error)));
Expand Down Expand Up @@ -1089,7 +1149,7 @@ effectIt.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(

yield* scanner.stopServer({ pid: job.pid, port: job.port });
expect(stops).toEqual([["command-live", true]]);
// The shell waits on its job, so it exits once the job has.
// The CLI exits once its command has.
yield* Effect.promise(() => job.exited);
expect(isRunning(job.pid)).toBe(false);
}).pipe(
Expand Down
127 changes: 81 additions & 46 deletions apps/server/src/preview/PortScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ const lsofProcessIds = (raw: string): ReadonlyArray<number> => {
return [...pids];
};

const AGENT_THREAD_ASSIGNMENT = new RegExp(`(?:^|\\s)${AGENT_THREAD_ENV}=(\\S+)`);

/** Reads `/proc/<pid>/environ`, whose entries are NUL-separated. */
const parseProcEnvironThread = (raw: string): ThreadId | null => {
const prefix = `${AGENT_THREAD_ENV}=`;
Expand All @@ -383,19 +381,38 @@ const parseProcEnvironThread = (raw: string): ThreadId | null => {
return value ? ThreadId.make(value) : null;
};

/**
* Reads macOS `ps -E -o pid=,command=`, which appends each process's launch environment to its
* argv with spaces, so an argument that spells the assignment out also matches.
*/
const parsePsEnvironmentThreads = (raw: string): ReadonlyMap<number, ThreadId | null> => {
const threads = new Map<number, ThreadId | null>();
const parsePsCommandLines = (raw: string): ReadonlyMap<number, string> => {
const lines = new Map<number, string>();
for (const line of raw.split("\n")) {
const match = /^\s*(\d+)\s(.*)$/.exec(line);
if (!match) continue;
const value = AGENT_THREAD_ASSIGNMENT.exec(match[2]!)?.[1];
threads.set(Number(match[1]), value ? ThreadId.make(value) : null);
if (match) lines.set(Number(match[1]), match[2]!);
}
return lines;
};

/**
* Each listed process's launch values for `names`, from macOS `ps -E -o pid=,command=`, which
* appends the environment to the argv `ps` prints without `-E`. Null when macOS shows no
* environment, as for its own binaries such as `/bin/zsh`.
*/
export const parsePsEnvironments = (
withEnvironment: string,
withoutEnvironment: string,
names: ReadonlyArray<string>,
): ReadonlyMap<number, ReadonlyMap<string, string> | null> => {
const argv = parsePsCommandLines(withoutEnvironment);
const environments = new Map<number, ReadonlyMap<string, string> | null>();
for (const [pid, line] of parsePsCommandLines(withEnvironment)) {
const args = argv.get(pid);
// A different argv is a reused pid.
if (args === undefined || (line !== args && !line.startsWith(`${args} `))) continue;
const environment = line.slice(args.length).trim();
environments.set(
pid,
environment === "" ? null : parseEnvironmentVariables(environment, /\s+/, names),
);
}
return threads;
return environments;
};

/** Splits `/proc/<pid>/environ` on NUL, or the environment macOS `ps -E` appends on spaces. */
Expand Down Expand Up @@ -423,12 +440,18 @@ const parseEnvironmentVariables = (
export const agentCommandsInGroup = (
variables: ReadonlyMap<number, ReadonlyMap<string, string>>,
leader: ProcessTableRow,
members: ReadonlyArray<number>,
): ReadonlyMap<string, string> => {
const commands = new Map<string, string>();
const parent = variables.get(leader.ppid);
// An unreadable parent cannot rule out an inherited name.
if (parent === undefined) return commands;
for (const [name, command] of variables.get(leader.pid) ?? []) {
// macOS hides the environment of the shell that stays to run a pipeline, so a hidden leader's
// name is read from a process it started in the group, which inherited it.
const own =
variables.get(leader.pid) ??
members.map((pid) => variables.get(pid)).find((member) => member !== undefined);
for (const [name, command] of own ?? []) {
if (command !== "" && parent.get(name) !== command) commands.set(name, command);
}
return commands;
Expand Down Expand Up @@ -535,6 +558,23 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
// servers it left may still run. So does startup, for agents that ran before a restart.
let agentScanPending = true;

const runPsCommandLines = (pids: ReadonlyArray<number>, flags: ReadonlyArray<string>) =>
processRunner.run({
command: "ps",
args: [...flags, "-ww", "-o", "pid=,command=", "-p", pids.join(",")],
timeout: Duration.millis(PS_TIMEOUT_MS),
maxOutputBytes: PS_MAX_OUTPUT_BYTES,
});

const readPsEnvironments = (pids: ReadonlyArray<number>, names: ReadonlyArray<string>) =>
Effect.all([runPsCommandLines(pids, ["-E"]), runPsCommandLines(pids, [])], {
concurrency: 2,
}).pipe(
Effect.map(([withEnvironment, withoutEnvironment]) =>
parsePsEnvironments(withEnvironment.stdout, withoutEnvironment.stdout, names),
),
);

const readAgentThreads = Effect.fn("PortDiscovery.readAgentThreads")(function* (
pids: ReadonlyArray<number>,
) {
Expand All @@ -556,20 +596,21 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
{ concurrency: 16 },
),
)
: yield* processRunner
.run({
command: "ps",
args: ["-E", "-ww", "-o", "pid=,command=", "-p", unread.join(",")],
timeout: Duration.millis(PS_TIMEOUT_MS),
maxOutputBytes: PS_MAX_OUTPUT_BYTES,
})
.pipe(
Effect.map((result) => parsePsEnvironmentThreads(result.stdout)),
Effect.orElseSucceed(() => {
agentScanPending = true;
return new Map();
}),
);
: yield* readPsEnvironments(unread, [AGENT_THREAD_ENV]).pipe(
Effect.map(
(environments) =>
new Map(
Array.from(environments, ([pid, variables]) => {
const value = variables?.get(AGENT_THREAD_ENV);
return [pid, value ? ThreadId.make(value) : null] as const;
}),
),
),
Effect.orElseSucceed(() => {
agentScanPending = true;
return new Map();
}),
);
const next = new Map<number, ThreadId | null>();
for (const pid of pids) {
const threadId = cached.has(pid) ? cached.get(pid) : read.get(pid);
Expand Down Expand Up @@ -1095,33 +1136,27 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
);
return new Map(read.flatMap(Option.toArray));
}
const result = yield* processRunner
.run({
command: "ps",
args: ["-E", "-ww", "-o", "pid=,command=", "-p", pids.join(",")],
timeout: Duration.millis(PS_TIMEOUT_MS),
maxOutputBytes: PS_MAX_OUTPUT_BYTES,
})
.pipe(Effect.option);
const variables = new Map<number, ReadonlyMap<string, string>>();
for (const line of Option.isSome(result) ? result.value.stdout.split("\n") : []) {
const match = /^\s*(\d+)\s(.*)$/.exec(line);
if (match) {
variables.set(Number(match[1]), parseEnvironmentVariables(match[2]!, /\s+/, names));
}
}
return variables;
const environments = yield* readPsEnvironments(pids, names).pipe(
Effect.orElseSucceed(() => new Map()),
);
return new Map(
Array.from(environments).flatMap(([pid, variables]) =>
variables === null ? [] : [[pid, variables] as const],
),
);
});

const agentCommandStops = Effect.fn("PortDiscovery.agentCommandStops")(function* (
leader: ProcessTableRow | undefined,
members: ReadonlyArray<number>,
) {
const watchers = [...agentCommandStopWatchers];
if (leader === undefined || watchers.length === 0) return [];
const names = [...new Set(watchers.map((watcher) => watcher.variable))];
const commands = agentCommandsInGroup(
yield* readEnvironmentVariables([leader.pid, leader.ppid], names),
yield* readEnvironmentVariables([leader.ppid, ...members], names),
leader,
members,
);
return watchers.flatMap((watcher) => {
const command = commands.get(watcher.variable);
Expand Down Expand Up @@ -1189,8 +1224,9 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
: Effect.fail(new PreviewStopLocalServerError({ reason: "signal-failed", cause }));
}
});
const members = [...table.values()].filter((row) => row.pgid === group).map((row) => row.pid);
// Told before the signal, since the CLI can report the exit before a later step runs.
const commandStops = yield* agentCommandStops(table.get(group));
const commandStops = yield* agentCommandStops(table.get(group), members);
yield* Effect.forEach(commandStops, ({ watcher, command }) => watcher.onStop(command), {
discard: true,
});
Expand All @@ -1204,7 +1240,6 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
}
// While one of the group's original members is still in it, the group has not been reused,
// so SIGKILL goes only to the job that ignored SIGTERM.
const members = [...table.values()].filter((row) => row.pgid === group).map((row) => row.pid);
const jobRunning = readProcessTable.pipe(
Effect.map((current) => members.some((pid) => current.get(pid)?.pgid === group)),
Effect.orElseSucceed(() => false),
Expand Down
Loading