Plan: one bash parser, in one package

Closes: #206. Branch: refactor/pi-bash. Status: Done (2026-09-26) — pi-bash 0.1.0; a release follows

TL;DR

Two copies exist because ADR-004 (2026-08-31) split policy (pi-modes) from workflow enforcement (pi-workflow), forbade one importing the other, and preferred copies held equal by test. pi-workflow’s bash-structure.ts (2026-09-01, #14) came first, for the commit guard. pi-modes needed a parser on 2026-09-25 for the compound-line fix (#205) and copied it as bash-line.ts. The ADR did not foresee a shared parser; for pi-modes' trace and status helpers it said a shared package would be "a new decision, not this one". This is such a decision, for the parser.

Neither copy is strictly better. bash-structure is the deeper view (argv with unknowable tokens marked, redirects, wrapper chains, spans, which heredoc bodies are inert). bash-line has three things it lacks (pipelines, "is this one statement", a text split for PowerShell). The library keeps the deeper parser and adds the three.

Design

The package

packages/pi-bash, @gadhs/pi-bash, a library: no pi field, no extension, no loader in the meta package (the meta’s loader test holds one loader per dependency, and this has nothing to load). It follows pi-guidance’s manifest shape otherwise: type: module, files of *.ts (not vitest.config.ts), README.md, LICENSE; exports "." to ./index.ts; the repository’s publishConfig. tools/ci-publish.mjs discovers publishable packages by publishConfig and orders them by name, meta last, so pi-bash publishes before pi-modes and pi-workflow with no CI change.

Dependencies move, they don’t multiply: tree-sitter-bash ^0.25.1 and web-tree-sitter ^0.26.9 leave both consumers' dependencies and become pi-bash’s. Each consumer takes "@gadhs/pi-bash": "workspace:*" as a regular dependency, which publishing pins to the exact version, as the meta’s members are. A regular dependency, not a peer: the parser is a private tool of each consumer, like tree-sitter was, and if npm ever resolved two copies each would simply warm its own, as the two copies do today.

The modules

parser.ts

warmBashParser, resetBashParser, and the module-level parser. One warm state; both consumers warm it at session start, as each warms its own today. The load contract is unchanged: cold or failed means unavailable, never an allow.

structure.ts

analyze, BashStructure, BashCommand, HeredocBody: pi-workflow’s bash-structure.ts, moved.

line.ts

analyzeLine, BashLine, unitsOf, textUnits, effectiveCommand: pi-modes' bash-line.ts, moved. effectiveCommand keeps its text-level stripper as it is (the quoted-value assignment regex, the -- terminator, the cap, the command -v lookup case), reading the one table below instead of its own.

wrappers.ts

ONE table, WRAPPER_VALUE_FLAGS and WRAPPER_NAMES, the union of today’s two: timeout, env (with -S/--split-string), nice, ionice, nohup, setsid, stdbuf, exec, command, time, with timeout’s one positional (the duration) recorded beside its flags. The two strippers stay where they are and both read this table: pi-workflow’s `stripWrappers walks a token array from its tokenizer; effectiveCommand walks a text unit. They differ because their inputs do. The drift that hurt (#62, and today) was between tables; the table is what becomes one.

index.ts

Re-exports the four.

The consumers

  • pi-workflow: delete bash-structure.ts; git-guard.ts, structural-guards.ts, commit-pause.ts, index.ts import from @gadhs/pi-bash; warmBashStructure becomes warmBashParser; TEXT_WRAPPERS goes (it was an alias of WRAPPER_NAMES), and stripWrappers stays in git-guard.ts reading the library’s table. Its commit guard now unwraps setsid, ionice and exec. Regression tests, in git-guard.test.ts: findCommits finds the commit in setsid git commit -m x, exec git commit -m x and ionice -c 3 git commit -m x, warm and cold, and guardGitCommit refuses setsid git commit --no-verify -m x.

  • pi-modes: delete bash-line.ts; mode-policy.ts, judge-wiring.ts, judge.ts import from @gadhs/pi-bash; warmBashLine becomes warmBashParser. Its effective commands now see env -S. Regression test, in pi-bash’s line tests: effectiveCommand("env -S 'CI=1' pnpm publish") is pnpm publish. The existing command -v npm pin stays and still holds.

  • Tests move with their modules: bash-structure.test.ts and bash-line.test.ts become pi-bash’s, under its own vitest config with the repository’s coverage bar. The two parity tests that read the other package’s package.json for tree-sitter ranges collapse into one in pi-bash that reads the engine’s (@gotgenes/pi-permission-system) as bash-structure.test.ts does today. Consumer tests that call resetBashLine/resetBashStructure call resetBashParser.

  • git-guard-chaos.test.ts’s parity pin between `TEXT_WRAPPERS and WRAPPER_NAMES is deleted: the two are one value.

  • packages/pi/test/shipped-imports.test.ts checks a package’s relative imports against its files list; a bare @gadhs/pi-bash import is outside its concern. What makes the import resolve on a consumer’s box is the dependencies entry, which the consumer simulation exercises.

The installed layout

The consumer simulation (tools/consumer-sim.mjs) checks pi-remote’s wasm loads from the installed root (#142) and never checked the bash parser’s. Moving the parser is the moment to: a check that imports @gadhs/pi-bash from the installed root, awaits warmBashParser, and asserts analyze("git commit -m x").commands[0].argv[0] === "git". A cold parser fails closed and quietly today; this makes it loud where it would first appear.

ADR-004, amended

A dated amendment: a library is a package species beside the extension packages. It ships no extension and needs no loader; the enforcement packages import it by name as a regular dependency. The criteria from the ADR apply (its change cadence is the parser’s, its review posture is enforcement, it is coupled to neither mode nor workflow). The mirror-constant preference stands for constants; it failed for a parser and its tables (#62, and the 2026-09-26 drift).

Docs and release note

  • packages.adoc: the package table gains pi-bash; the count of packages wherever a page states it (grep seven).

  • CHANGELOG.adoc == Unreleased, under Changed: the new package, the merged table, and the two behaviour changes it brings (the commit guard unwraps setsid, ionice, exec; pi-modes' effective commands see env -S).

  • Versions at release: pi-bash 0.1.0; pi-modes and pi-workflow minor.

Order of work

  1. The package, with the moved modules and tests, green on its own.

  2. pi-workflow switched over, its parity pin deleted, green.

  3. pi-modes switched over, green.

  4. The consumer-sim check, ADR amendment, docs, changelog.

  5. validate; the pack test in packages/pi; then the release.

  6. Close #206 with the merge SHA.

One MR. The ledger stays one parser at every step: a consumer is switched whole, never half.

Risks

Change Direction Why it is safe

The commit guard unwraps three more wrappers

Fail-closed

More lines read as commits, never fewer. A wrapper form the walk misreads leaves the unit unrecognised, and the raw-string --no-verify backstop behind it is unchanged.

One warm state for two consumers

Fail-closed

Cold or failed is unavailable, which grants no rule allow and puts the guards on their text floor, exactly today’s contract. Either consumer’s warm-up serves both.

The wasm loads from a new package location

Fail-closed, and today silent

A load failure is logged and the session runs cold. The consumer-sim check turns that into a red run at the one place it would first show.

npm resolves two copies of pi-bash

None

Each copy warms its own parser, as the two copies do today. Publishing pins exact versions, so this needs two consumers on different pi-bash versions, which one release never produces.

env -S now stripped in the line view

Fail-closed

One more candidate for the restricting checks; an allow is never taken from a unit.

Nothing here is silent: a cold parser is logged (bash_line.load_failed today, one event name after the merge), and every refusal is traced.

Scope

In: packages/pi-bash/*; in pi-workflow bash-structure.ts (deleted), git-guard.ts, structural-guards.ts, commit-pause.ts, index.ts, package.json, its tests; in pi-modes bash-line.ts (deleted), mode-policy.ts, judge-wiring.ts, judge.ts, package.json, its tests; tools/consumer-sim.mjs; adr-004-package-boundaries.adoc, packages.adoc, CHANGELOG.adoc.

Out:

  • Any change to what either parser reports beyond the table union. The structure view and the line view stay two functions over one tree; making them one view is a later simplification if anyone wants it.

  • The meta package: no loader, no dependency; pi-bash reaches consumers through pi-modes and pi-workflow.

  • pi-modes' textUnits for PowerShell stays text-only; a PowerShell parser is not this.

Edit this page · latest