Refactor: Consolidate ExCmd into CommandPort#5
Merged
dbernheisel merged 5 commits intotv-labs:mainfrom Mar 27, 2026
Merged
Conversation
Forward stderr from temporary OutputCollectors in pipeline execution and command substitution to the session's stderr sink instead of discarding it. Resolve relative file paths in while loop input redirects against the session's working directory. Handle :exec return value in SessionCase test helper.
Expand CommandPort to be the single interface for all user-facing OS process execution. Add thin delegates for ExCmd.Process lifecycle operations (os_pid, read, write, close_stdin, close_stdout, await_exit) and gateway functions with restricted-mode enforcement (start_link, stream, system_cmd). Internal helpers now call public delegates instead of ExCmd.Process directly.
Replace direct ExCmd.Process and System.cmd calls in JobProcess, Coproc, Pipeline, and Command builtin with CommandPort delegates. CommandPort is now the single gateway for all user-facing OS process execution. Add restricted-mode guard to external_command? in Pipeline to prevent streaming bypass.
5 tasks
dbernheisel
reviewed
Mar 26, 2026
Collaborator
dbernheisel
left a comment
There was a problem hiding this comment.
Looks great -- Just one suggestion to move restricted? to Session because that feels like a session concern that the CommandPort needs to adhere to. Then I'm ready to merge.
6 tasks
Per review feedback, restricted mode is a session concern — not a process-spawning concern. Moves the helper and updates all callers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
External process spawning is spread across multiple modules —
CommandPort,JobProcess,Coproc,Pipeline, and thecommandbuiltin each callExCmd.Process.*,ExCmd.stream, orSystem.cmddirectly. This makes it impossible to enforce a single policy (e.g. restricted mode) without patching every call site individually. It also makes the dependency on ExCmd implicit and hard to audit.Changes
Consolidate ExCmd into CommandPort
CommandPortbecomes the single interface for all user-facing OS process execution. It now exposes:os_pid,read,write,close_stdin,close_stdout,await_exit(used byJobProcessandCoprocwhich manage process lifecycles directly)stream/3wrappingExCmd.stream(used byPipeline)system_cmd/4(used bycommandbuiltin)start_link/3,stream/3, andsystem_cmd/4accept arestrictedboolean and return{:error, :restricted}when activegraph TD AST[AST.Command] --> CP[CommandPort] JP[JobProcess] --> CP CO[Coproc] --> CP PL[Pipeline] --> CP CM[Command builtin] --> CP CP -->|restricted?| ERR["{:error, :restricted}"] CP -->|allowed| EX[ExCmd / System.cmd]Internal plumbing (
System.cmdforkill,mkfifo,hostname,uname) is intentionally exempt.After this change,
ExCmdappears only inlib/bash/command_port.ex.Bug fixes
OutputCollectorprocesses but discarded their stderr. Now stderr is forwarded to the session's stderr sink. This also fixes the 4 pre-existing golden test failures on main.while read line; do ...; done < relative_filenow resolves againstsession_state.working_dirinstead of the BEAM's cwd.:execreturn handling —SessionCase.run_script/2now handles the{:exec, result}return fromSession.execute/2.Test plan
test/bash/command_port_test.exs— low-level API delegates, streaming, system_cmd, restricted mode errors,restricted?/1helpertest/bash/stderr_forwarding_test.exs— stderr from$(), backticks, and pipeline temp collectors reaches the sessiontest/bash/control_flow_test.exs— while loop with relative path input redirectgrep -r "ExCmd\." lib/ --include="*.ex"returns onlycommand_port.ex