Repository navigation
Description
Follow-up to #1449 (merged as efb6c271), which closed six fail-open paths in
plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py. A seventh survives, reached from
the one stream that work did not cover.
The defect
_decide writes its decision with print, which only buffers. A closed or lost stdout pipe
therefore raises nowhere inside main — main returns its intended code, and the failure surfaces
at interpreter shutdown instead. CPython reports a failed shutdown flush by replacing the process
exit status with 120.
120 is "any other exit code" under the PreToolUse contract
(hooks reference, fetched 2026-07-26: "Claude Code treats
exit code 1 as a non-blocking error and proceeds with the action … If your hook is meant to enforce
a policy, use exit 2"), so the destructive command runs even though the guard had decided to
deny it. Same shape as #1423.
Reproduction (measured on merged efb6c271)
Real subprocess, deny decision, stdout wired to a pipe whose reader is closed:
deny + broken stdout -> exit 120
stderr: Exception ignored while flushing sys.stdout:
OSError: [Errno 22] Invalid argument
#1449's in-process GuardTests helpers cannot reproduce this: they call guard.main() directly and
never reach interpreter shutdown, which is where the failure lands. Only a real process with a real
closed pipe shows it.
Fix
Branch fix/1449-followup-stdout-failopen, commit 2de5f36e — pushed, no PR opened yet.
The module tail flushes stdout itself, so an undeliverable decision is caught while there is still a
decision to make: deny at exit 2 with a diagnostic, because a decision the host never received is
not a decision. It then flushes stderr best-effort and os._exits the resolved code, so a shutdown
flush can no longer rewrite it. #1449's null-device fallback is reused rather than duplicated —
extracted from _write_diagnostic as _discard_stream.
test_undeliverable_stdout_decision_denies_at_exit_2_in_a_real_process spawns the real script
against a genuinely closed stdout pipe and asserts exit 2 plus the diagnostic; it exits 120
against the pre-fix tail. 212 tests pass, ruff check clean, markdownlint clean.
Why this is an issue and not already a PR
Found while driving #1449, after that PR had already been merged. Filing rather than opening a PR
unprompted so the merge queue stays under its owner's control; the branch is pushed and ready to
open at any time.
Activity
kyle-sexton commentedon Jul 26, 2026
Branch updated:
26c0b7f8drops a redundant_discard_stream(sys.stdout)frommain's deny handler. It was measured NOT to be what closed the hole — that configuration still exited 120 with the call present; only the module tail's explicit flush closed it — so the comment justifying the call was wrong and the call was one mechanism too many. The tail owns the stdout deadline alone. Also confirmed neitherdestructive_guard.pynorkillswitch_configregisters anatexithandler or__del__, which the tail'sos._exitwould otherwise skip. Re-verified exit 2; 212 tests pass.kyle-sexton commentedon Jul 26, 2026
Grounding the
120in the official CPython contract rather than the observed exit code, and it alsoexplains why this defect is invisible where the sibling stderr one was not.
sys.exit(), fetched 2026-07-26:So
120is not incidental — it is the documented status for exactly this failure, and thedestructive_guard.pymodule tail reached it throughraise SystemExit(main()).The same page explains why stdout behaves differently from stderr here:
A PreToolUse hook's stdout is a pipe, not a tty, so it is block-buffered:
_decide's decisionprintlands in the buffer and returns cleanly, and the broken pipe is not discovered until theshutdown flush.
stderris line-buffered unconditionally, so the diagnosticprintraisesimmediately, inside a handler that can react — which is precisely why #1449's fix caught the stderr
half and structurally could not catch this one.
The fix's
os._exitsidesteps the quoted clause by never letting the interpreter reach that cleanuppath, after flushing both streams as far as they can go.
Consequence confirmed against the harness contract
(hooks reference, fetched 2026-07-26):
120is neither0nor2, so a guard that decided to deny lets the destructive command run.kyle-sexton commentedon Jul 26, 2026
kyle-sexton commentedon Jul 26, 2026
This was generated by AI during work-loop execution.
Claimed and worked as part of the autonomous work-loop lane. PR opened: #1535.
The pushed branch this issue named (
fix/1449-followup-stdout-failopen, commits2de5f36e/26c0b7f8) already had the fix implemented and tested; this PR rebases it onto currentmainand adds one thing on top: a real-subprocess regression test proving the fix also structurally
closes #1526 (a distinct, not-yet-triaged report of the same exit-
120fail-open symptom, triggeredby fd 2 being closed outright rather than merely broken). Details in the PR body's Summary and
Related sections. Not merging — that's the babysit-prs lane or a human, per this lane's authority.