From 640e2e190e3911c684b729f8955a921ead4cb18d Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Mon, 16 Nov 2026 19:39:22 -0800 Subject: [PATCH 1/2] fix(server): stopping an agent's piped server on macOS names its command macOS `ps -E` shows no environment for its own binaries, such as the `/bin/zsh` an agent CLI runs a command in, and joins argv and environment with spaces. The scanner read argv assignments as environment and read a hidden environment as an empty one, so a command whose shell stays to run a pipeline was never named when its server was stopped, and the agent was woken as if it had finished. Read only what follows argv, treat a hidden environment as unreadable, and take a hidden leader's command name from a process it started in its group. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/preview/PortScanner.test.ts | 94 ++++++++++++--- apps/server/src/preview/PortScanner.ts | 127 +++++++++++++------- 2 files changed, 157 insertions(+), 64 deletions(-) diff --git a/apps/server/src/preview/PortScanner.test.ts b/apps/server/src/preview/PortScanner.test.ts index d9b226e21f4fe..d81859aec904a 100644 --- a/apps/server/src/preview/PortScanner.test.ts +++ b/apps/server/src/preview/PortScanner.test.ts @@ -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( @@ -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 @@ -980,17 +986,57 @@ 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>, + 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()], [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", + ].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", + ].join("\n"); + expect( + PortScanner.parsePsEnvironments(withEnvironment, withoutEnvironment, ["COMMAND"]), + ).toEqual( + new Map([ + [300, null], + [301, new Map([["COMMAND", "a"]])], + [302, new Map()], + ]), + ); }), ); @@ -1007,9 +1053,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( @@ -1020,16 +1084,10 @@ const agentServer = () => readonly exited: Promise; }>((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((resolve) => child.once("exit", () => resolve())); child.once("error", (error) => resume(Effect.die(error))); @@ -1089,7 +1147,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( diff --git a/apps/server/src/preview/PortScanner.ts b/apps/server/src/preview/PortScanner.ts index d950cd636f4f5..6b1ba9247c674 100644 --- a/apps/server/src/preview/PortScanner.ts +++ b/apps/server/src/preview/PortScanner.ts @@ -370,8 +370,6 @@ const lsofProcessIds = (raw: string): ReadonlyArray => { return [...pids]; }; -const AGENT_THREAD_ASSIGNMENT = new RegExp(`(?:^|\\s)${AGENT_THREAD_ENV}=(\\S+)`); - /** Reads `/proc//environ`, whose entries are NUL-separated. */ const parseProcEnvironThread = (raw: string): ThreadId | null => { const prefix = `${AGENT_THREAD_ENV}=`; @@ -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 => { - const threads = new Map(); +const parsePsCommandLines = (raw: string): ReadonlyMap => { + const lines = new Map(); 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, +): ReadonlyMap | null> => { + const argv = parsePsCommandLines(withoutEnvironment); + const environments = new Map | null>(); + for (const [pid, line] of parsePsCommandLines(withEnvironment)) { + const args = argv.get(pid); + // A different argv is a reused pid. + if (args === undefined || !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//environ` on NUL, or the environment macOS `ps -E` appends on spaces. */ @@ -423,12 +440,18 @@ const parseEnvironmentVariables = ( export const agentCommandsInGroup = ( variables: ReadonlyMap>, leader: ProcessTableRow, + members: ReadonlyArray, ): ReadonlyMap => { const commands = new Map(); 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; @@ -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, flags: ReadonlyArray) => + 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, names: ReadonlyArray) => + 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, ) { @@ -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(); for (const pid of pids) { const threadId = cached.has(pid) ? cached.get(pid) : read.get(pid); @@ -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>(); - 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, ) { 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); @@ -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, }); @@ -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), From dbf1973d9fa509fe91ff8c43a10a76c6c97a6452 Mon Sep 17 00:00:00 2001 From: Zack Jackson <25274700+ScriptedAlchemy@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:21:29 -0800 Subject: [PATCH 2/2] fix(server): require argv boundary in macOS environment parsing --- apps/server/src/preview/PortScanner.test.ts | 2 ++ apps/server/src/preview/PortScanner.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/preview/PortScanner.test.ts b/apps/server/src/preview/PortScanner.test.ts index d81859aec904a..41fa743b0dcd7 100644 --- a/apps/server/src/preview/PortScanner.test.ts +++ b/apps/server/src/preview/PortScanner.test.ts @@ -1021,12 +1021,14 @@ effectIt.effect("reads a macOS process's environment, never its arguments", () = " 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"]), diff --git a/apps/server/src/preview/PortScanner.ts b/apps/server/src/preview/PortScanner.ts index 6b1ba9247c674..8f1e54f11b1ac 100644 --- a/apps/server/src/preview/PortScanner.ts +++ b/apps/server/src/preview/PortScanner.ts @@ -405,7 +405,7 @@ export const parsePsEnvironments = ( for (const [pid, line] of parsePsCommandLines(withEnvironment)) { const args = argv.get(pid); // A different argv is a reused pid. - if (args === undefined || !line.startsWith(args)) continue; + if (args === undefined || (line !== args && !line.startsWith(`${args} `))) continue; const environment = line.slice(args.length).trim(); environments.set( pid,