Skip to content

shell: a reader that stops early no longer fails a piped command - #2189

Merged
santoshkumarradha merged 4 commits into
app/v3from
c34/pipe-reader
Nov 2, 2026
Merged

santoshkumarradha merged 4 commits into
app/v3from
c34/pipe-reader

Conversation

@santoshkumarradha

@santoshkumarradha santoshkumarradha commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Behaviour

Since every bash door started running with -o pipefail, a piped command whose reader stops early reported failure. For example, in git log | head the writer dies of SIGPIPE and the command reported status 141. Now such a command succeeds when its last stage succeeded and every earlier failing stage ended with 141. Any other failing stage still fails the command.

  • One helper, shell.PipefailArgs, builds the arguments for every bash door: the foreground shell (internal/exec/tools.go), background jobs and services (detachedCommand in internal/exec/services.go, used by jobs.go), and the bare tool and streaming shell (internal/exec/bare).
  • Pipefail stays on. A short EXIT trap reads PIPESTATUS of the last pipeline and recalculates the pipefail result, counting a non-final 141 as success.
  • An ERR trap marks each pipeline whose failure ended a list. The trap changes the result only for a pipeline that has this mark. So in yes | head -1 && false the 141 skipped the false and the command still exits 141. If the pipeline had counted as a success, the false would have run, so 0 would be a false answer.
  • The status stays as it is today when the statuses cannot be read honestly: under set -e; after an explicit exit; for a subshell or a function; and when the command sets its own EXIT or ERR trap. In those cases the command's own trap runs as before. Every return in the check names its status, so a command's own ERR trap does not fire a second time.
  • A command with no | runs exactly as written. The sh program gets no pipefail and no prefix, as before.

Evidence

Red on base app/v3 (the new tests copied onto the base tree):

  • TestBashBeltTruncationWritesHeadTailAndPath, TestBashBeltTruncationCapFollowsTheWindow (internal/session): FAIL
  • TestForegroundShellForgivesOnlyAnEarlyReader, TestBackgroundJobForgivesOnlyAnEarlyReader, TestDetachedCommandForgivesOnlyAnEarlyReader (internal/exec): FAIL
  • TestAnEarlyReaderIsNotAFailureButEveryOtherStageIs (internal/exec/bare): FAIL

Green on this branch:

  • go vet ./internal/shell/ ./internal/exec/... ./internal/session/: EXIT=0
  • gofmt -l internal cmd: empty, EXIT=0
  • go test ./internal/shell/: EXIT=0. TestPipefailForgivesOnlyAReaderThatStoppedEarly covers these cases: yes x | head -1 gives 0, false | tail -20 gives 1, false | yes | head -1 gives 1, yes x | head -1 | false gives 1, yes x | head -1 && false gives 141, a user trap ... EXIT still runs, a user ERR trap fires once, and set -e/set -u/set -x behave as before. TestPipefailLeavesShAndPipeFreeCommandsAsWritten covers sh and commands with no pipe.
  • go test ./internal/exec/...: EXIT=0
  • go test ./internal/session/ -run 'Belt|Bash': both bash belt tests now PASS. The run still exits 1 because of TestUserBashChangesReachTheTurnEndSnapshot, which also fails on base (that build has no seal engine).
  • go test ./internal/session/: EXIT=1. Every failure also fails on base: two prefix budgets, the stage verbs budget, ProgramHold, two NoRoad tests, and the three sealed-tree tests that need a seal engine.
  • make test-laws BASE=origin/app/v3: EXIT=2. Every failure also fails on base: TestSourceComments (it flags files this PR does not touch), TestOnlyTheTaxonomyTurnsAStatusIntoAMove, and two NoRoad tests.
  • make changelog-check BASE=origin/app/v3: EXIT=0

Limits

  • The ERR mark is a string of stage statuses and nothing clears it. Suppose a command ends with a pipeline whose 141 decided an && or || list, and an earlier pipeline in the same command failed with exactly the same statuses at the end of a list. Then the stale mark matches and the result is eased to 0 (for example yes | head -1; yes | head -1 && false). This needs the same statuses twice and is rare.
  • Any non-final stage that exits 141 is forgiven, not only one killed by SIGPIPE. Bash reports both as 141.
  • Inside the command, $? after the pipeline still reads 141. Only the command's final exit status changes.
  • The prefix defines a shell function and a variable, both with the __codeaf_ prefix.

🤖 Generated with Claude Code

santoshkumarradha and others added 3 commits November 2, 2026 07:02
Every bash door now runs under shell.PipefailArgs: pipefail stays on, and
an EXIT trap reads PIPESTATUS of the last pipeline. A non-final stage that
died of SIGPIPE (141) counts as success when the last stage succeeded, so
`git log | head` exits 0 while `false | tail` still exits 1. An ERR-trap
mark limits the easing to a pipeline whose status ended a list, so
`yes | head -1 && false` keeps today's 141. errexit, explicit exits,
subshells, functions, and a command's own EXIT or ERR trap keep today's
status. sh and commands without a pipe run as written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@santoshkumarradha
santoshkumarradha marked this pull request as ready for review November 2, 2026 13:35
@santoshkumarradha
santoshkumarradha merged commit 4ba0461 into app/v3 Nov 2, 2026
@santoshkumarradha
santoshkumarradha deleted the c34/pipe-reader branch November 2, 2026 13:35
santoshkumarradha added a commit that referenced this pull request Nov 2, 2026
santoshkumarradha added a commit that referenced this pull request Nov 2, 2026
* shell: a reader that stops early no longer fails a piped command

Every bash door now runs under shell.PipefailArgs: pipefail stays on, and
an EXIT trap reads PIPESTATUS of the last pipeline. A non-final stage that
died of SIGPIPE (141) counts as success when the last stage succeeded, so
`git log | head` exits 0 while `false | tail` still exits 1. An ERR-trap
mark limits the easing to a pipeline whose status ended a list, so
`yes | head -1 && false` keeps today's 141. errexit, explicit exits,
subshells, functions, and a command's own EXIT or ERR trap keep today's
status. sh and commands without a pipe run as written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* shell: plain-case comments for the early-reader exception

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* changes: entry for the early-reader fix (#2189)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* session: watch ticks and automation commands run under pipefail

jobShell now takes the command and returns the full argument list from
shell.PipefailArgs, so a failing stage of a pipe fails a watch tick or an
automation command, as it does at every other bash door. A reader that
stops early (head) still succeeds, and sh gets the command unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* changes: entry for the job-shell pipefail fix (#2191)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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