Plan: a compound bash line is decided as a whole
Closes: #205. Branch: fix/compound-line. Status: Done (2026-09-25) — pi-modes 0.36.0; release 0.62.0 follows, and the live probe once it is installed
TL;DR
pi-permission-system splits a bash line into its commands, resolves each
against its own policy, and forwards one result: the most restrictive
unit, the first on a tie. Our seed policy is "*": "ask", so every unit
ties and our authorizer is asked about the line’s first command, with the
whole line attached as evidence. Our mode rules, the safety valve and the
high-consequence list all match that first command. A rule allow ends
adjudication. So cd /tmp && printenv | grep X runs where printenv | grep
X alone is denied by rule; cd . && git push --force walks past yolo’s
valve; and every cd-led glab write of the last two days was recorded
as rule:verdict allow cd * and never reached the judge. Observed live
on 2026-09-25 (the note on #205, confidential).
The operator’s ruling: the parts of a compound line cannot be assessed
separately - danger sits in the combination - so a compound line is the
judge’s to decide, as a whole. Rules keep their one legitimate job on it:
refusing. The fix is in pi-modes alone; nothing upstream changes.
The rule after this change
| Line | Decision |
|---|---|
One command ( |
As today: a rule deny denies, a rule allow allows, else the later stages. |
Several commands ( |
Every command, and every pipeline, is matched against the rules. Any deny denies the line, deterministically. A rule allow is never taken from a compound line. The valve and the high-consequence list are checked against every command, every pipeline and the line. Otherwise the line goes to the judge, whole. |
A line the parser reports complex ( |
Compound: deny rules, the valve and the high-consequence list are matched against whatever commands and pipelines the parse yields (none, when cold); no rule allow; the judge decides the whole line. |
A rule pattern containing a pipe (env | grep*) matches a pipeline
node’s text, which is why pipelines are matched as well as commands.
`evaluateRules’ pipeline check from #166 - a match against the full line
may restrict, never allow - stands as one case of the general rule.
Design
U1 - the parser in pi-modes (packages/pi-modes/bash-line.ts, new)
pi-modes gets tree-sitter-bash and web-tree-sitter as dependencies,
at the ranges packages/pi-workflow/package.json and the permission
engine declare (^0.25.1, ^0.26.9), and a module modelled on
packages/pi-workflow/bash-structure.ts: warmBashLine() at session
start (init once, retry on failure), analyzeLine(line) synchronous and
never throwing, returning:
{ status: "parsed" | "unavailable"; complex: boolean; commands: string[]; pipelines: string[] }
-
commands: the source text of everycommandnode, in order, across list operators, pipes, newlines and nesting.pipelines: the text of everypipelinenode. Both are what the mode rules match against. -
complex: any of the engine’s complex node types is present (command_substitution,process_substitution,subshell, the control-flow statements,function_definition; expansions do NOT make a line complex -echo $HOMEis one command). -
unavailableuntil warm or on a parse failure:commands: [],complex: true.
Why a parser here and not a splitter or a shared one:
-
Not pi-workflow’s
bash-structure.ts: ADR-004 (docs/modules/ROOT/pages/adrs/adr-004-package-boundaries.adoc) forbids either package importing the other, and the bridge carries observation only - "decisions never cross it" - so a parse that feeds a decision cannot ride it. Moving the analyzer to a package both may import is a refactor across three packages; an issue, not this fix. -
Not a hand-rolled splitter: `mode-policy.ts’s header says why - "a second command parser is exactly where this project has been wrong before." This is the same parser the engine and pi-workflow use, at the same ranges.
-
Parity pin: a test in
packages/pi-modes/test/bash-line.test.tsholds pi-modes' two ranges equal to the engine’s and to pi-workflow’s, aspackages/pi-workflow/test/bash-structure.test.ts"declares the same tree-sitter ranges gotgenes declares" does for pi-workflow. -
The WASM ships inside the two npm packages and is located with
require.resolve, asbash-structure.tsdoes; nothing is added to thefileslist.
U2 - line-level rule evaluation (packages/pi-modes/mode-policy.ts)
evaluateLine(rules, details): { verdict: RuleMatch | undefined; compound: boolean; units: string[] }
-
line=details.fullCommand ?? details.command. WhenanalyzeLine(line)isparsed, notcomplex, and yields exactly one command and no pipeline, the line is a single command: the result isevaluateRules(rules, details)unchanged,compound: false. -
Otherwise
compound: true. Every pipeline, then every command, is matched throughbestMatchon the bash surfaces. The most restrictive hit across all units is the verdict; anallowverdict is discarded (returned asundefined); adenyoraskis kept with the unit it hit. -
unitsis every pipeline and command found, for the valve and the high-consequence stage. -
Non-bash asks (read, write, edit, spawn) have no line and are unchanged.
-
The header’s "WHAT WE DO NOT REIMPLEMENT" and "A LIMIT, MEASURED THE HARD WAY" paragraphs are rewritten: the limit they describe is #205’s defect, and this module now matches every unit the engine’s own parser yields.
U3 - the authorizer (packages/pi-modes/authorize.ts, judge.ts, judge-wiring.ts)
-
authorizeAskcallsevaluateLine. Stage 3’s deny and the later allow read its verdict, soruleAllownever fires on a compound line. -
highConsequencematches its patterns against the line’s units as well as the full command, the unit and the path. -
hitsSafetyValve(judge.ts) matchesalwaysAskagainst every unit as well ascommand,valueandpath. The units travel onAskDetailsas a new optionalunits?: string[], populated bynormalizeAskinjudge-wiring.ts, so the corpus and the judge tests can feed it. -
warmBashLine()is called where pi-modes' session-start work runs (index.ts), fire-and-forget as pi-workflow warms its parser. -
Logging:
gadhs_mode_rule.verdictgainsunit(the command the rule hit) when it differs fromask.
U4 - the seed (packages/pi-modes/bootstrap.ts)
Unchanged. "*": "ask" is what routes every line to us; the tie is what
lets us see the line at all. One sentence in the seed’s comment says so,
so nobody "fixes" the tie by adding rules to the seed.
U5 - docs and release
-
docs/modules/ROOT/pages/modes.adoc: "Who decides what" (line 16) says a rule allow is for a single command and a compound line is the judge’s; "The safety valve" (line 337) says it checks every command; "How the guards read a command line" (line 465) gains one line that pi-modes parses with the same parser. -
CHANGELOG.adocunder Fixed: what changed, not how to exploit the old build (the issue is confidential).pi-modes0.36.0; release 0.62.0 on its own MR.
Cost
Every compound line that today auto-allows on its first command reaches
the judge: cd x && git status, cd x && ls. Auto’s 103 allow patterns
were largely written against a first-command match. About a second and
$0.001 per line; a session runs hundreds. Accepted by the operator’s
ruling. No shipped allow is added for the glab preflight prefix: the
glab write is the judge’s to see, and the operator’s standing approval
of it answers a judge denial. The WASM load at session start is the one
pi-workflow already pays (~1 MB, tens of ms), now twice per session.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
The parser is cold when the first asks arrive |
fail-closed |
|
Two parsers drift (pi-modes' and the engine’s) |
fail-open |
Same WASM at pinned-equal ranges (the parity test). And direction: a divergence can lose a deny, valve or high-consequence hit - the judge still sees the whole line - but never manufacture an allow, since a rule allow is taken only from a single-command line. |
A deny rule that used to fire on a single unit stops firing |
fail-open |
Single-command lines take the unchanged |
Judge friction rises enough that an operator reaches for yolo |
silent |
Named in the CHANGELOG and the docs; the valve now holds in yolo for every command, so the fall is smaller than it was. |
The engine stops attaching |
fail-open |
Already so for a single-unit line (no evidence when equal). For a
compound line without evidence, |
Tests
-
packages/pi-modes/test/bash-line.test.ts: each list operator, pipes,|&, newlines; operators inside single and double quotes stay one command; each complex node type setscomplex;unavailablebefore warm-up; a property test (fast-check) that arbitrary strings return the shape without throwing; the range-parity pin. -
packages/pi-modes/test/mode-policy.test.ts: a compound line with a deny on the second command is denied; a compound line of two allowed commands is not allowed (verdict undefined, compound true); a pipeline deny (printenv | grep*) fires from the second segment; a single-command line is byte-for-byte the old result. -
packages/pi-modes/test/authorize.test.ts(regression, one per bypass in #205’s note):cd /tmp && printenv | grep X→ deny by rule;cd . && git push --forceunder a valve naminggit push --force*→ defer by valve, in yolo; a compound line whose first unit is allowed and whose second is unruled → the judge stub is called; a high-consequence pattern on the second unit → defer. -
packages/pi-modes/test/judge.test.ts:hitsSafetyValvehits on a unit that is notcommand. -
Live: the probe from #205’s note re-run in auto after the fix - the prefixed line is denied by rule.
-
The judge corpus is unchanged: it exercises the judge, which already reads the full line.
Delivery
-
Branch
fix/compound-linefrom main; the plan committed alone. -
U1-U4 with their tests, one commit;
validategreen; the live probe. -
U5 (docs, CHANGELOG, version) in the same commit or a second.
-
MR
Closes #205, merged; release 0.62.0 on its own MR; the tag after the gate (MR merged, HEAD = merge SHA, versions right). Note on #205 with the merge SHA.
Scope
In: the parser module, line-level rule evaluation, the valve and high-consequence stages reading units, the warm-up, docs, the release.
Out (named): any change to the seed or to upstream; moving the analyzer
to a package both pi-modes and pi-workflow may import (an issue); a
shipped allow for the glab preflight prefix; tuning auto’s allow list
for compound lines (an issue if the friction proves too much); the judge
rubric.
Erratum ledger:
-
Erratum (2026-09-25, U1): "one command" is decided on structure - the program’s only statement is a command, redirected or not - not by counting command nodes:
[[ -f x ]] && git pushholds one command node and is compound. The rule table’sprintenv | grep Xexample is a pipeline, so compound; it is denied either way, now by its pipeline unit. -
Erratum (2026-09-25, U3): the warm-up runs in
judge-wiring.ts’s `onSessionStart, where the authorizer’s session work already runs, not inindex.ts.authorizeAskalso fillsunitsfor the judge stage when the caller did not, so the valve’s reach does not depend on the path an ask took;normalizeAskfills it as planned. -
Erratum (2026-09-25, tests): the live probe runs once the release is installed - this session’s authorizer is the installed package, not the tree. Until then a test feeds the engine’s exact payload shape (first command escalated, the line as evidence) through
adjudicateAsk. The suites that exercise bash rules warm the parser first, as a session does; the cold path has its own tests. -
Erratum (2026-09-25, U1/U3): cold or on a parser fault, the restricting checks - deny and ask rules, the valve, the high-consequence list - match a text floor (the line split on every operator,
unitsOf), not "none": with no units, yolo’s valve, which has no judge behind it, was blind tocd . && git push --forcefor the whole cold window, or for a session whose load failed. The floor can over-split; that only adds candidates to checks that restrict, and a cold line still takes no rule allow.