Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ jobs:
- name: the documents say what the code does about procedural memory
run: ./scripts/docs-match-code.sh

- name: no pipe ends in a grep that leaves early
run: ./scripts/no-reader-leaves-a-pipe-early.sh

# Last, because it mutates tracked files and restores them. It needs a
# clean tree and says so plainly, so it also reports any earlier step
# that starts writing into the checkout.
Expand Down
3 changes: 3 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pytest
./scripts/local-first.sh
./scripts/features-are-bound.sh
./scripts/docs-match-code.sh
./scripts/no-reader-leaves-a-pipe-early.sh # invariant 16
./scripts/gates-have-teeth.sh # invariant 9; needs a clean tree and the package installed
```

Expand Down Expand Up @@ -204,6 +205,8 @@ an absent invariant.
except to say it is not built. *(gate: `scripts/docs-match-code.sh`, which
holds the number of mentions per document and the refusal in
`engram/mcp_server.py`, teeth in `scripts/gates-have-teeth.sh`)*
16. **A check says what the text holds, not which of a writer and its reader finished first.** `@claude 2026-11-06`, carried from stack-k8s#226 and stack-single#167; it records no decision of the owner. Under `set -o pipefail` a pipe into `grep -q` reports the writer too: grep leaves at its first match, a writer with more to write takes SIGPIPE, the pipeline returns 141, and a line that is there reads as missing. In the negative form, `if ... | grep -q bad`, the 141 reads as clean and a refused line passes. The rate follows the load on the machine. Here the one place was `scripts/gates-have-teeth.sh`, which reads a gate's output to judge a failure, so no gate here carried it; this one is carried so the next check written here does not bring the construct in. Every such pipe in live shell code here moved, 1 in one file: to a here-string, `grep -q x <<<"$text"`, or to a pipe into `grep -c x >/dev/null`.
*(gate: `scripts/no-reader-leaves-a-pipe-early.sh`: a control that breaks a real pipe, known lines through the reader, then every tracked shell file and workflow `run:` step masked down to live code. It refuses to report OK on no shell file, no pipe, no step or a file it lost its place in. Eighteen cases in `scripts/gates-have-teeth.sh`. Not held: `| head`, the same race, which here takes a few lines from a writer of a few lines.)*

## Decisions that have no gate yet

Expand Down
93 changes: 92 additions & 1 deletion scripts/gates-have-teeth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ run_case() {
# turns "it did not fail at all" into "it failed for the wrong reason",
# which sends the reader to look at prose when the gate is toothless.
if [ "$expect" = fail ] && [ "$rc" -ne 0 ] && [ -n "$needle" ] &&
! printf '%s' "$out" | grep -qF -- "$needle"; then
! grep -qF -- "$needle" <<<"$out"; then
printf 'WRONG REASON %s\n it failed, but not saying: %s\n' "$name" "$needle"
failures=$((failures + 1))
return
Expand Down Expand Up @@ -297,6 +297,97 @@ run_case "features-are-bound: the scenarios are gone" fail \
subprocess.run(["git", "rm", "-rq", "features"], check=True)')" \
"not a pass"

# Invariant 16. Under pipefail a pipe into grep -q fails when the writer is still writing, so a healthy tree
# goes red at random on a loaded machine. The first cases plant the construct; the middle ones are what
# must not be flagged; the last ones break the gate itself, because a reader that has stopped matching
# reports what a clean tree reports.

run_case "no-reader-leaves-a-pipe-early: a gate pipes its text into grep -q again" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nprintf \x27%s\\n\x27 \"$x\" | grep -q y\n")')" \
'scripts/local-first.sh:'

run_case "no-reader-leaves-a-pipe-early: the pipe is carried over a line" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nprintf \x27%s\\n\x27 \"$x\" \\\n | grep -q y\n")')" \
'scripts/local-first.sh:'

run_case "no-reader-leaves-a-pipe-early: a workflow step reads through the pipe" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit(".github/workflows/ci.yml", "run: ./scripts/no-network-at-write.sh", "run: printf x | grep -q x")')" \
'.github/workflows/ci.yml:'

run_case "no-reader-leaves-a-pipe-early: the words are only in a comment" pass \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\n# printf x | grep -q x\n")')"

run_case "no-reader-leaves-a-pipe-early: the words are a command handed to another shell" pass \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nsh -c \"printf x | grep -q x\"\n")')"

run_case "no-reader-leaves-a-pipe-early: the words are in a here-document" pass \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\ncat <<\x27EOF\x27\nprintf x | grep -q x\nEOF\n")')"

run_case "no-reader-leaves-a-pipe-early: grep -c reads to the end of a pipe" pass \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nprintf x | grep -c x >/dev/null\n")')"

run_case "no-reader-leaves-a-pipe-early: a here-string has no pipe to leave" pass \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\ngrep -q x <<<x\n")')"

run_case "no-reader-leaves-a-pipe-early: a substitution in quotes reads through the pipe" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nx=\"$(printf y | grep -m1 y)\"\n")')" \
'scripts/local-first.sh:2 pipes into `grep -m1`'

run_case "no-reader-leaves-a-pipe-early: a long option leaves as early as a short one" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/local-first.sh", "#!/usr/bin/env bash\n", "#!/usr/bin/env bash\nprintf y | grep --quiet y\n")')" \
'scripts/local-first.sh:2 pipes into `grep --quiet`'

run_case "no-reader-leaves-a-pipe-early: a quote left open loses the reader its place" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 's = open("scripts/local-first.sh").read()
open("scripts/local-first.sh", "w").write(s + "echo \"open\n")')" \
'lost its place'

run_case "no-reader-leaves-a-pipe-early: the reader stops knowing -q" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "if ch in \"qmlL\":", "if ch in \"mlL\":")')" \
'the reader missed'

run_case "no-reader-leaves-a-pipe-early: the reader stops reporting what it finds" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "found.append((m.start(), flag))", "pass")')" \
'the reader missed'

run_case "no-reader-leaves-a-pipe-early: the reader starts reading quoted text" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "blank(i, end + 1)", "pass")')" \
'the reader flagged'

run_case "no-reader-leaves-a-pipe-early: the control writes less than a pipe holds" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "WRITER = \"seq 1 400000\"", "WRITER = \"seq 1 3\"")')" \
'it must fail every time'

run_case "no-reader-leaves-a-pipe-early: no pipe is read" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "pipes += len(PIPE.findall(masked))", "pipes += 0")')" \
'measured NOTHING'

run_case "no-reader-leaves-a-pipe-early: no shell file is read" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "if path.endswith(\".sh\") or path.startswith(\".githooks/\") or (first.startswith(\"#!\") and \"bash\" in first):", "if False:")')" \
'measured NOTHING'

run_case "no-reader-leaves-a-pipe-early: no workflow step is read" fail \
'./scripts/no-reader-leaves-a-pipe-early.sh' \
"$(py 'edit("scripts/no-reader-leaves-a-pipe-early.sh", "elif path.startswith(\".github/workflows/\") and", "elif path.startswith(\".github/flows/\") and")')" \
'measured NOTHING'

echo
if [ -n "$(git status --porcelain)" ]; then
printf 'FAIL: this script left the tree dirty, so it cannot be trusted about anything above\n'
Expand Down
Loading
Loading