singularity-forge/packages/pi-coding-agent/src
Jeremy McSpadden a3ff25c668 fix(bash): rewrite background commands to prevent pipe-open hang
Root cause: when the LLM runs `cmd &`, bash forks the process and
exits immediately. The forked process inherits Node's piped stdout/
stderr FDs. Node.js waits for all holders of those FDs to close before
firing the 'close' event — so the tool hangs until the background
process exits (which for a server is never).

Fix: add rewriteBackgroundCommand() in bash.ts. Before exec, detect
commands with a trailing & background operator and inject
>/dev/null 2>&1 before the & when stdout is not already redirected.
This severs the pipe inheritance so Node gets 'close' immediately
when the shell exits.

Guards:
- Commands already redirecting stdout (>, >>, &>, |) are not rewritten
- && (logical AND) is not affected
- & inside single-quoted strings is not affected
- A brief onUpdate advisory is surfaced when rewrite happens so the
  LLM knows to prefer nohup/setsid for robust detachment

Export rewriteBackgroundCommand from pi-coding-agent for testability.

Tests: bash-background.test.ts — 12 cases covering no-op paths,
rewrite paths, compound commands, and already-safe nohup patterns.
Closes #733
2026-03-16 18:03:01 -05:00
..
cli feat: dynamic model discovery & provider management UX (#581) 2026-03-16 06:23:18 -06:00
core fix(bash): rewrite background commands to prevent pipe-open hang 2026-03-16 18:03:01 -05:00
modes fix: resolve 4 small issues reported in #663 2026-03-16 12:23:20 -06:00
resources/extensions/memory fix: Phase 1 quick wins — bug fixes, security hardening, and performance 2026-03-16 13:18:02 -05:00
utils feat: wire native Rust image module into image processing pipeline 2026-03-13 13:41:53 -06:00
cli.ts feat: vendor Pi source into workspace monorepo 2026-03-12 21:55:17 -06:00
config.ts perf: fix synchronous I/O in hot paths (#540) 2026-03-15 16:57:22 -06:00
index.ts fix(bash): rewrite background commands to prevent pipe-open hang 2026-03-16 18:03:01 -05:00
main.ts feat: dynamic model discovery & provider management UX (#581) 2026-03-16 06:23:18 -06:00
migrations.ts feat: vendor Pi source into workspace monorepo 2026-03-12 21:55:17 -06:00