Workflow guards
Part of the Workflow Modes reference.
Workflow guards (deterministic, at the tool-call seam)
Shipped in @gadhs/pi-workflow since ADR-004 — mode-independence is their
design, so the package whose organizing concept is per-mode variation was
the wrong home. They are documented beside the modes because the
enforcement story reads as one piece; their traces render through pi-modes'
status line and inline widgets when both are loaded, and their decisions do
not change when it is absent.
These run in every mode, yolo included, before any permission ask exists. They are not permissions and cannot be opted out of: yolo answers “can I run this command”, never “do the coding standards apply to me”. A mode that auto-approves asks still attributes its commits, still reflects on each staged diff, and still writes SPDX headers.
They read the session’s workspace, wherever it is (#491). In a local session
that is this machine’s checkout. In a remote session it is the
checkout on the SSH host, read through the same connection the tools use. That
covers the branch, the staged diff the review reads, the reviewer’s own reads,
declared Review-Context files, a -F message file and its containment, the
repository’s review rules and the approved-plan check. The same command meets
the same refusals and the same review either way. If the host cannot be asked,
a read answers "unknown", which holds the commit and never passes it.
The guards read bash. On Windows, pi also has a powershell tool, and a
command there that the guards would check in bash is refused with the fix
named: run it through the bash tool (Git Bash). That covers a commit and a
git command with --no-verify. glab is not refused there: glab’s stored
login is put out of reach for every command instead (#300). Anything else
in PowerShell, git status and git log included, passes untouched
(#214).
On a busy Windows machine, the powershell tool is also the fast path
(#650). Each bash call starts a Git Bash process, and Git Bash’s process
emulation slows under load: one start took 2-7 s at 85-100% CPU, the same
cost tools/lib/test-bounds.mjs records for the test suites. PowerShell is a native
Windows program and does not pay it. So run a read, a listing, git status
or git log in PowerShell. Anything the guards check still goes through
bash, and pays its start.
On Windows the bash tool runs Git Bash. From #652 to #907 a box could opt in to brush, a native bash-compatible shell; it was removed as not ready.
Monitors, jobs and ! run Git Bash too: a monitor’s and a job’s stop read
Git Bash’s MSYS process group (#553, #626).
In Windows PowerShell 5.1, give Invoke-WebRequest the -UseBasicParsing
switch (#657). Since Microsoft’s CVE-2025-54100 update (December 2025),
without it the command asks for confirmation before it parses a page. The
tool runs PowerShell non-interactively, so the prompt fails with
"Windows PowerShell is in NonInteractive mode". Basic parsing runs none of
the page’s script, which is also the safer default.
All plain code — never the judge — and each completes an action where the correct completion is unambiguous, rather than failing it:
| Guard | Behaviour |
|---|---|
Commit format |
|
Commit inside another construct |
A commit the guard can see only inside another construct can be neither
paused nor attributed, so it is refused with the remedy named — write it
as a plain |
|
Blocked, including clustered short flags ( |
` list or a pipeline
( |
|
Main-branch protection |
|
Co-Authored-By |
Appended mechanically as a |
Secret redaction |
Every tool’s text output is scanned before the model reads it (#271), in
every mode. A secret-shaped value is replaced with |
Reflection-Review |
The cold review’s verdict, as a second |
SPDX at write time |
A new source file missing the header gets it prepended (comment syntax per
language, shebang kept on line 1): |
Debt markers |
An added |
glab’s stored login out of reach |
No workflow guard blocks or parses glab (#300); the token-printing glab
commands stay mode deny rules. Instead, when pi starts with
no |
Reflection pause |
Every commit is reviewed once, before it lands, by a cold reader — a model call with no session history — against eight fixed questions; its findings come back as the block reason and the same diff commits on retry. Advisory, not a veto: The reflection pause. |
Setup drift |
At session start (agency-hooks repos only): warns when |
Changelog outgrown |
At session start, when |
Batch order |
pi runs the tool calls of one message in parallel, preparing every call
(these guards included) before running any. So |
These guards keep the agent on the agency workflow; they are convenience and early feedback, not the security boundary. Your repo’s git hooks still cover commits you make yourself.
How the guards read a command line
Structure, with the text as the floor
The guards see bash the way the shell does, not the way grep does: a
tree-sitter-bash parse hands them commands as structure — argv with
non-literal tokens marked unknown, wrapper chains (timeout 300 …)
unwrapped, redirects separated, byte spans preserved. It is the same WASM
parser, at the same version range, as the permission engine
(@gotgenes/pi-permission-system, consumed unmodified — see
architecture); the range equality is pinned by test,
since two parsers drifting apart would recreate the differential this
closes. The mode rules read a compound line with the same parser: pi-modes
and pi-workflow both take it from @gadhs/pi-bash, as the permission row
of the per-mode config says.
Until the parser is warm (once, at session start), and wherever structure
cannot vouch — an expansion that could hide a command, a global flag the
extractor does not model — every guard answers byte-identically to its
original text matcher. Structure may only narrow false positives, never
widen false negatives. Pins: parser range equality in pi-bash’s
structure.test.ts; the cold path’s byte-identity and the warm/cold
pair in git-guard.test.ts; and git-guard-chaos.test.ts, which runs both
implementations over hundreds of generated compounds and fails on any
divergence structure cannot prove is quoted prose.
Heredocs, and shells the parser does not open
-
A heredoc that recites commit commands (a commit message quoting the workflow, a test corpus containing
gitstrings) does not trip the guards. Even when a substitution elsewhere on the line makes structure decline, the parser still vouches for where the bodies are and whether their delimiter was quoted, so the text fallback reads the line with quoted bodies (<<'EOF', which the shell does not expand) blanked. -
An unquoted body stays text —
$(…)and backticks inside it run; a backtick there makes the line complex and its command a unit the rules read, because the grammar gives backticks in a body no node (#403) — and so does every body when a shell orevalis on the line. -
Code handed to a shell the parser never opens is unknowable in both its spellings: a
-cstring (sh -c 'git commit …') and a program on stdin (bash <<'EOF' …,cat <<'EOF' | sh,printf '…' | bash). Structure declines and the text floor decides, which does see the string and the body. -
A shell running a script file (
bash deploy.sh) runs a program on disk like any executable; the guard does not read it. -
The
--no-verifybackstop keeps its raw text form as a permanent floor whenever structure declines: it is the only thing between a hook bypass and the repository. The floor reads the line with inert (quoted-delimiter) heredoc bodies blanked, as the other text guards do (#306): a commit message saying-inf, or a file body that mentions the flag as prose, is not a flag. A body a shell orevalruns stays visible, sobash <<'EOF'still hides nothing. A redirect’s&(2>&1,&>) does not end the command for the floor; a list or background&does (#312).
Commits outside the session’s checkout
One repository per agent session. A commit in another checkout of the
session’s own repository, a linked worktree or the primary (any checkout
sharing its git common dir), is judged there (#679). cd ../wt && git
commit and git -C ../wt commit from a session in the primary, or the
reverse from a session in a worktree, are reviewed against that
checkout’s staged diff. Its -F message file, its Review-Context paths,
its branch (a worktree on main is refused as main is) and its project
rules are read there too. The plan approval stays the session’s. So a
repository whose long pre-push battery forbids edits in the primary
checkout can prepare the next change in a worktree without leaving the
session. A line that commits in two places (here and a worktree, or two
worktrees) is refused, since one review reads one index. Two checkouts
are one repository only when git’s own commondir pointer says so; a
clone, a submodule, or a repository under a directory named worktrees
is another repository.
Throwaway temp repositories (#945). A commit whose resolved repository sits
strictly inside the host’s temp directory (os.tmpdir() / /tmp, checked
through symlinks) with no configured git remotes (or created inline via
git init with no git clone or git remote on the line) is treated as a
throwaway experiment and passes without the reflection pause, branch check, or
stage-and-commit refusal. --no-verify, nested-shell commits, clones or repos
with a remote, and lines mixing a real-repo commit with a temp commit stay
refused.
A commit the guard can see leaving the session’s repository is refused
rather than misjudged: cd other && git commit and git -C other commit
into another repository are refused by name, because no review from here
can read that repository as its own session would — open a session there
instead (#462). This refusal comes before the message checks (#721): a
commit elsewhere with a relative -F was refused first as an unreadable
message, the path read from the session’s directory, and only the retry
named the real cause. The refusal names the ways there:
cd <checkout> && pi starts an interactive session in it, cd <checkout> && pi
--fork <id> carries this conversation over (with this session’s id
filled in), and cd <checkout> && pi -p "<task>" runs an unattended headless
session there — noting that any step requiring human approval fails fast in
headless mode instead of waiting on /pending (#955). In a session over SSH
those local commands would be wrong: pi runs on this machine, so they would
start a pi on the remote, and --fork cannot reach
a session kept here. There the refusal names /ssh <destination>:<checkout>
in pi, or pi '/ssh <destination>:<checkout>' from a shell. That opens the
checkout’s latest session, or a new one the first time (#524), and the refusal
says this conversation does not carry over (#519, #533). On a Windows machine’s own disk a cd
or git -C path is read as Git Bash reads it (/d/x is D:\x, /tmp the temp directory), and two
roots that differ only in case are one checkout (#538). A session started in a worktree commits there normally. A
relocation the guard cannot follow (cd "$X", pushd, cd -, env -C,
--git-dir, --work-tree) is refused as unknown rather than guessed, in
either spelling (--git-dir=x or --git-dir x) and with the parser warm
or cold: until #388 the cold path read --git-dir x’s `x as the
subcommand, found no commit, and let the line through unchecked. env’s
-C counts in a cluster or attached (env -iC dir, env -C/dir), and
builtin cd is followed as cd is (#421). Not seen: bare GIT_DIR=x / GIT_WORK_TREE=x prefixes, which the parser
drops, and a cd inside a shell’s -c string, which no walk opens.