Skip to content

fix(server): stopping an agent's piped server on macOS names its command - #141

Merged
ScriptedAlchemy merged 2 commits into
mainfrom
t3/macos-portscanner-env-attribution-fix
Nov 17, 2026
Merged

ScriptedAlchemy merged 2 commits into
mainfrom
t3/macos-portscanner-env-attribution-fix

Conversation

@ScriptedAlchemy

Copy link
Copy Markdown
Owner

Problem. On macOS, PortScanner.test.ts > stops an agent's server… failed on main, and the product had the same gap. macOS ps -E shows no environment for Apple's own binaries (/bin/zsh, /bin/bash, /bin/sh, /usr/bin/ruby), and it joins argv and environment with spaces. The scanner read argv assignments as environment, and read a hidden environment as an empty one. When Codex runs a command whose shell stays as group leader (npm run dev | tee log, … & wait), stopping its server named no command, so after the turn had settled Codex was woken as if the command had finished. The test failed for a related reason: its stand-in CLI was a bash -c whose argv spelled out the assignment, so the parent looked like it carried the name.

Fix. On macOS, the environment is now what ps -E prints after the argv that plain ps prints. A hidden environment counts as unreadable, so the existing "an unreadable parent cannot rule out an inherited name" rule now applies on macOS too. A hidden group leader's command name is read from a process it started in its group, which inherited it. The thread-attribution reader uses the same parser, so a spelled-out T3CODE_THREAD_ID= argument no longer attributes a listener. The live test now has the Codex shape: a readable CLI starts sh -c 'server | cat' as its own group, with the command named in its environment. It runs on both platforms.

Proof (macOS 27.2, Apple silicon).

  • With the Codex app-server's own tree, zsh -lc execs a single command, so the leader is readable (node, Homebrew/Xcode python). For pipes and & wait, zsh stays as leader and ps -E shows none of its environment.
  • The test file against main's PortScanner.ts: 3 failed, 29 passed. The live test fails at expect(stops).toEqual([["command-live", true]]) with []. With the fix: 32 passed.
  • vp run check:changed origin/main: all pass.
  • Linux: the /proc paths are unchanged. The leader fallback only applies when the leader's environment is unreadable, which on Linux means another user's process. CI runs the live test on Linux.

What remains. A server that is itself an Apple binary (/usr/bin/ruby -run -e httpd, nc -l) is still not attributed to its thread on macOS, and a fully hidden group still names no command. Closing that needs the command recorded when it starts rather than read back from the process. That's a design choice, not part of this PR.

Opus 5.5 via Claude Code in T3 Code.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: dbf1973

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ScriptedAlchemy ScriptedAlchemy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second-pass review: I fixed a reproducible argv-prefix collision in dbf1973. With plain ps reporting node server.js and ps -E reporting node server.json COMMAND=c, the parser previously treated the changed command as the same argv and attributed COMMAND=c. The existing parser test now includes that case; it failed before the fix and passes with an exact argv/space boundary.

Validation on the fixed tree: 32 PortScanner tests and 2 AgentContextBudget tests passed, including the real Linux pipeline/group-stop case. Formatting, lint, both Knip passes, GraphQL, and direct server/evals/scripts typechecks passed. The task-runner typecheck wrapper cannot start under this environment's seccomp_unotify restriction; the direct compiler commands completed successfully. The combined tree with #139 merges cleanly and preserves its injected server PID fixtures.

Validation still outstanding: I cannot execute native macOS ps on this Linux host. This pass covers the macOS parser fixtures and real Linux process behavior; it does not independently rerun the native macOS results reported in the PR description. Please retain the native piped-server and hidden-parent checks when verifying on macOS.

The new-head Fork CI run 44016630121 is queued, so I am holding the merge until CI completes successfully.

@ScriptedAlchemy
ScriptedAlchemy merged commit 11a851b into main Nov 17, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant