Skip to content

Expose foreground process group PID on Unix#918

Open
srid wants to merge 1 commit intomicrosoft:mainfrom
juspay:feat/foreground-pid-upstream
Open

Expose foreground process group PID on Unix#918
srid wants to merge 1 commit intomicrosoft:mainfrom
juspay:feat/foreground-pid-upstream

Conversation

@srid
Copy link
Copy Markdown

@srid srid commented May 4, 2026

Unix terminals can now report the foreground process group PID directly. process already resolves the foreground process name by calling tcgetpgrp(3) internally; this exposes the same process-group id as foregroundPid for callers that need to identify the active foreground program without reverse-mapping from a process title.

The property is optional on the public IPty type because Windows/ConPTY does not expose an equivalent concept. On Unix it returns number | undefined, matching the native lookup behavior when the pty has already exited or tcgetpgrp fails.

Verification

  • npm run build
  • On macOS arm64: npm test -- --grep "foreground process group"
  • Full npm test on the same macOS host currently reaches 23 passing tests and then times out in the existing should return the name of the sub process test; the new foregroundPid test passes before that unrelated timeout.

Generated by Codex (model gpt-5).

Add a `foregroundPid` getter that returns `tcgetpgrp(fd)` on the pty
master fd, exposing the pid of the foreground process group attached
to the pty (or `undefined` on failure).

This is the same syscall the existing `pty_getproc` already invokes
internally to resolve the foreground process *name* — it's just
returning the pid instead of throwing it away.

Useful for callers that need to identify which process is currently
in the foreground of a terminal (e.g. to detect that an agent like
claude-code is running, where the process name resolves to "node").

Cross-platform: works on both Linux and macOS via tcgetpgrp(3).
Not exposed on Windows (no equivalent ConPTY concept).
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