Skip to content

Add pipeOutput option for non-terminal stdout and stderr - #107

Merged
zhenthebuilder merged 2 commits into
mainfrom
zhen/pipe-output
Oct 23, 2026
Merged

zhenthebuilder merged 2 commits into
mainfrom
zhen/pipe-output

Conversation

@zhenthebuilder

Copy link
Copy Markdown
Contributor

Why

Every child spawned by Pty gets the PTY as stdout, stderr, and controlling terminal. interactive: false only swaps stdin for /dev/null. Callers that run a command just to collect its output still get terminal behavior from programs that check isatty(stdout): progress lines on stderr, pagers, and prompts that read from /dev/tty.

For example, with stdout and stderr on a terminal, gh pr view <n> --json number --jq .number prints a Working... progress line before the number. With stdout on a pipe it prints only the number.

What changed

  • New PtyOptions.pipeOutput (default false). When set:
    • the child's stdout and stderr are both dup'd from the write end of one pipe, so their relative order is kept;
    • stdin is /dev/null;
    • the child still calls setsid(), but skips TIOCSCTTY, so it has no controlling terminal and opening /dev/tty fails.
  • The pipe's read end takes the controller fd's place and the write end takes the user fd's place. The synthetic EOF write after wait() and the fd-drop lifecycle are unchanged.
  • wrapper.ts reads the pipe with a read-only net.Socket instead of tty.ReadStream. A pipe ends with a normal EOF instead of EIO. resize on a pipe hits the existing ENOTTY path and is a no-op.
  • pipeOutput: true with interactive: true throws.
  • cgroup, sandbox, and AppArmor setup run in pre_exec as before.

Tests

New pipeOutput suite in tests/index.test.ts (50 repeats each):

  • stdout and stderr are not terminals; output has no CRLF translation
  • no controlling terminal (/dev/tty cannot be opened, Linux only)
  • exit code is captured and no fds leak
  • stdout and stderr stay in write order across 400 interleaved lines
  • 320 KiB of fast output is not truncated
  • the stream is read-only, and resize and close do not throw
  • combining with interactive: true throws

Run locally on x86_64 Linux with Node 22 and stable Rust.

~ written by ⠕ Replit

Programs that check isatty(stdout) print progress lines, open pagers, or
prompt when they run under the PTY, even when the caller only collects
their output. pipeOutput connects the child's stdout and stderr to one
pipe, keeps stdin on /dev/null, and skips TIOCSCTTY so the child has no
controlling terminal. The pipe's read end takes the controller fd's
place, so the synthetic EOF and fd-drop lifecycle stay the same.

Assisted-by: Replit
@zhenthebuilder
zhenthebuilder marked this pull request as ready for review October 22, 2026 23:27
@zhenthebuilder zhenthebuilder mentioned this pull request Oct 23, 2026
Release pipeOutput with this change so dependents can adopt it in one step.

Assisted-by: Replit
@zhenthebuilder
zhenthebuilder merged commit 31fa48c into main Oct 23, 2026
26 of 28 checks passed
@zhenthebuilder
zhenthebuilder deleted the zhen/pipe-output branch October 23, 2026 17:18
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.

2 participants