Plan: a heredoc body is data to the rules

Closes: #264. Branch: fix/heredoc-rules. Status: Done (2026-09-30) — heredoc bodies cut from the rules' text; interpreter bodies left to the judge by the operator’s ruling; #284 filed for the nesting bypass

Erratum (2026-09-30): the pipelines never carried a body in the shapes tested - tree-sitter nests the rest of the line in the heredoc redirect, so the pipeline reads | grep x - and the whole line was the one candidate that mattered; the unitsOf cut stays as a guard. The spans are UTF-16 code units (web-tree-sitter’s indices), not bytes, so the cut is a string slice.

Erratum (2026-09-30): "a body is data" is false when the reader is an interpreter (bash <<EOF, python3 - <<PY): the body is code, and the rules no longer match it (the cold review’s finding). Kept by the operator’s ruling: that is deliberate routing around a rule, outside the threat model (honest mistakes); the judge still reads the body, and the rules never read a script run from a file. The docs say so.

Erratum (2026-09-30): cat <<'EOF' | sh was never denied, so its "still denied" test is not this plan’s: the nesting also made such a line read as one command (a rule-allow bypass), filed and fixed as #284.

TL;DR

Observed on 2026-09-30, in plan mode, while probing this very issue: a python3 - <<'PY' … PY line whose quoted heredoc body contained the string /.pi/agent/gadhs-pi-modes.json was refused by the rule /.pi/agent/gadhs-pi-.json* (a plan-mode deny). The command read nothing under that path; the body was a string in a Python program. The same shape is why patch scripts have been written to files and run by path all session: a heredoc that names a protected path trips the rule.

The structural view already gets this right. analyzeLine (pi-bash line.ts) collects each command node’s text, and tree-sitter puts a heredoc body beside the command, not inside it, so commands for cat > f <<'EOF' … EOF is ["cat"] (pinned in packages/pi-bash/test/line.test.ts, "a quoted heredoc body is data, not commands"). Two other texts reach the rules with the body still in them:

  • the whole line: evaluateRules (mode-policy.ts) matches details.fullCommand as the pipeline candidate, and restrictingHit adds evaluateRules(rules, details) to a compound line’s hits;

  • each pipeline: parsed.pipelines is the pipeline node’s text, which spans its commands and their redirects, heredoc body included, and unitsOf puts every pipeline among the units.

The fix erases heredoc bodies from those two texts before any rule sees them. The judge keeps the full raw command: it decides on effect, and a body that expands ($(…) in an unquoted heredoc) is its to weigh, as today; complex already marks such a line.

What is deliberately not changed

  • Quoted strings stay in the text. A path is routinely quoted (cat "$HOME/.pi/settings.json"), and several shipped rules exist to meet it there (>/.pi/settings.json*). Masking quotes would loosen those. So grep -rn 'overrid|bash' docs keeps tripping *|bash: that rule is a text floor for a pipe into a shell, and a quoted |bash is ambiguous enough (an argument to an outer sh -c runs) that the deny is the conservative answer. Not a defect of this plan; noted on #264 as the remaining false positive, with the workaround (a regex with the alternation the other way round, or grep -e over -e bash).

  • An unavailable parse changes nothing. With no spans to erase, the raw text is matched as today: never looser cold.

  • The structural units are unchanged. Their body-free reading is what this plan extends to the other two candidates.

Design

pi-bash: analyzeLine reports heredoc bodies

BashLine gains heredocBodies: HeredocBody[] (the type structure.ts already exports: { start, end, quoted }, byte offsets into the input). collect notes each heredoc_redirect the way StructureCollector does (structure.ts noteHeredoc: the heredoc_start child’s first character decides quoted; each heredoc_body child is one span). unavailable() returns [].

A new export, withoutHeredocBodies(line: string, parsed: BashLine): string, returns the line with every body span cut out, later spans first so earlier offsets hold. The operator, the delimiter and the terminator line stay, so the redirect still reads as one. Quoted and unquoted bodies are both cut: the rules are text matchers, and an unquoted body’s expansion is already the judge’s by complex.

pi-bash: unitsOf uses body-free pipelines

unitsOf(parsed, line) maps parsed.pipelines through the same cut before adding them to the units. The commands need nothing; the argv candidates need nothing. The text floor, used cold or beside a syntax error, is unchanged (cold has no spans; a syntax error’s floor is the over-splitting backstop and stays over-inclusive).

pi-modes: the whole-line candidate is body-free

evaluateLine (mode-policy.ts) parses the line, then builds the details the rules see with fullCommand (and command, when it is the same string) replaced by withoutHeredocBodies(line, parsed). Only the rule calls (evaluateRules, restrictingHit) get the masked details; the LineVerdict it returns, and everything after (the valve, the high-consequence list, the judge, the trace, the queue’s shown line) keep the raw text. One helper, rulesView(details, parsed, line), does the substitution, so the diff is one call site per rule call.

Risks

Risk Failure mode Mitigation

A body span is off by one: a character of the operator or terminator cut, or a character of the body kept

Fail-open if a kept character completes a pattern; fail-closed if a cut one breaks a legitimate match

Spans come from tree-sitter’s heredoc_body node, the same source structure.ts noteHeredoc uses for the commit guard. The pi-bash tests assert the cut text byte for byte, operator and terminator kept.

The parse is unavailable or faulted, so no spans exist

Fail-closed: today’s behaviour, the raw text is matched

Nothing is cut without a parsed span; the cold case is tested as still denied.

A rule the operator relies on matched a heredoc body on purpose

Silent: that match stops firing

No shipped rule targets a body; a body is data to the program reading it. The changelog and modes.adoc say bodies are not matched. The judge still sees the whole command, body included, so an unquoted body that expands (complex) is still weighed.

The judge’s view changes by accident

Fail-open if the judge lost the body

rulesView is applied to the rule calls only; the returned LineVerdict, the valve, the high-consequence list and the judge read the raw details. A test asserts the judge’s command fact still carries the body.

Tests

Each case carries its category: a regression names the case that would have caught the defect; a contract pins a boundary’s shape.

packages/pi-bash/test/line.test.ts:

  • (contract) a quoted and an unquoted heredoc each report one body span with the right quoted; two heredocs on one line report two, in order;

  • (contract) withoutHeredocBodies cuts exactly the bodies: operator, delimiter and terminator kept byte for byte; a line with no heredoc is returned as the same string;

  • (contract) unitsOf for cat <<'EOF' | grep x\nBODY\nEOF: no unit contains BODY, and the pipeline unit still reads cat <<'EOF' | grep x.

packages/pi-modes/test/rule-precedence.test.ts - it loads the shipped modes.json through loadModesConfig (line 23), and its bash(…​) helpers call evaluateLine with a mode’s rules (lines 88 and 119):

  • (regression: the observed case) a python3 - <<'PY' line whose body names /.pi/agent/gadhs-pi-modes.json is not denied by rule in plan or auto: evaluateLine(…​).verdict?.action is not "deny";

  • (contract) the same string as a command word (cat ~/.pi/agent/gadhs-pi-modes.json) and as a redirect target (echo x > ~/.pi/agent/gadhs-pi-modes.json) are still denied by /.pi/agent/gadhs-pi-.json* (modes.json:208);

  • (contract) a heredoc line that is itself a deny (cat <<'EOF' | sh with a body) is still denied by *|sh (modes.json:180): the pipeline unit keeps its operators;

  • (contract) with the parser reset (cold), the observed line is still denied by rule, as today: never looser cold;

  • (contract) the judge’s facts for the observed line still carry the body: the facts list in judge.ts builds fact("command", details.fullCommand ?? details.command) (judge.ts:392) from the raw details, not the rules' view.

The test file naming that path must be written with the write tool, not through a bash heredoc: the string in a bash command is exactly the false positive, and the rule refuses the command that writes the test.

The eval corpus is run once (node tools/task.mjs eval) to confirm nothing moves: it exercises the judge, not the rules, and no rubric changes, so it is a check rather than the gate.

Scope

  • In: the three files above (packages/pi-bash/line.ts, packages/pi-modes/mode-policy.ts, tests); modes.adoc’s compound-line paragraph (lines 160-182, "every command, and every pipeline, is matched against the rules …"), one sentence added: a heredoc body is not matched by the rules, a quoted string is; the changelog entry under `== Unreleased (CHANGELOG.adoc:7), === Fixed.

  • Check: node tools/task.mjs validate; then node tools/task.mjs eval once (tools/task.mjs:467) to confirm the judge corpus does not move.

  • Close: land with node tools/task.mjs land (the commit says Closes #264); a note on #264 recording the remaining false positive (*|bash on a quoted |bash) and why it stays; then the release that carries pi-bash and pi-modes.

  • Out: quoted-string masking (see above); the *|bash false positive (noted on #264 as remaining); any change to what the judge sees; any change to the text floor.

  • Version: pi-bash minor (a new field and export), pi-modes minor (what is refused changes), in the release that follows.

Edit this page · latest