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

git commit is parsed (argv-level: quotes, clusters, -F files, git -C), and git named by path (/usr/bin/git, git.exe) is git. A short cluster is read as git reads it: -am"fix: x" is -a -m "fix: x". Several -m join as paragraphs, the first the subject. The multi-line idiom -m "$(cat <<'EOF' … EOF)" is read: a quoted delimiter expands nothing, so the body is the message. A message the guard cannot read is refused before the review, with the fix named (#233): any other substitution in -m, a -F file written earlier on the same line, whose old contents are all the guard could read (that reason alone, also for a file the line creates: cat > m.txt <<EOF … EOF then the commit, #453), or a -F file it cannot read at all - missing, outside the repository and the scratch directories (/tmp, /var/tmp; on Windows the temp directory Git Bash calls /tmp, so -F /tmp/m.txt reads the file Git Bash wrote, #535), or over the message cap (#389; until then such a commit passed with no format check and no trailer). Write the file inside the repository or /tmp in its own step, or pass the message literally. A write by redirect or tee is recognised by name. Any other change earlier on the line, such as sed -i … m.txt && git commit -F m.txt, is not seen. So a -F message refused for its format, on a line that runs anything before the commit, also says the file is read before the line runs and should be edited in a separate call (#575). A message on stdin (-F -, #313) is read from a quoted-delimiter heredoc on the commit itself (git commit -F - <<'EOF', <← stripping tabs), also on a line whose structure the guard declines to judge, since the parse still vouches for the commit’s redirects; and on a line the parser cannot read at all, or before it is ready, from the text, as narrowly: the only heredoc on the commit’s line, quoted, inside the commit’s own unit, with a terminator line (#329). With two heredocs on the commit’s line it is unreadable in every path: their bodies interleave, and the parser can hand the commit both. From a pipe, an unquoted heredoc, another fd or a later stdin redirect, it is refused by name, because neither the check nor the attribution could see it. Subject must be type: imperative with type ∈ feat|fix|chore|refactor|docs|test, ≤72 chars. An optional scope, type(scope): …, is one or more words of letters, digits, ., /, - or _, separated by commas with an optional space after each: fix(api,cli): … (#574). A malformed scope is refused by that rule, not the type rule. Every violation is listed in one block reason, so one retry fixes all.

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 git commit on its own line: echo $(git commit …), cat <(git commit …), x=git commit … , (git commit …) (wrapped or not), and a commit in the body of an if/for/while/case or a function on the same line. That holds when a global option the guard does not model stands before commit ((git --no-replace-objects commit …), --git-dir="$PWD/.git"): the structure cannot name the subcommand, so the text decides, as it does with the parser cold (#418). A commit beside a substitution (echo "$(date)" && git commit …), or with one in its own message, is ordinary; when the parser declines, a quoted heredoc body that merely contains one errs closed.

--no-verify / -n

Blocked, including clustered short flags (-anm, and -anm"msg" with the message attached) the glob rule cannot see, wrapped forms (time git commit -n), and a flag written after a redirect (git commit -m x > /dev/null --no-verify, <<'EOF' -n): tree-sitter-bash hangs such words in the redirect, and pi-bash reads them back as the command’s, with a bare - the grammar drops before a heredoc (#312). A redirect after the last command of a &&/`

` list or a pipeline (cd x && git commit … > /dev/null --no-verify) is that command’s, as bash binds it, though the grammar hangs it on the whole chain (#315), and so is one after a negated command (! git commit … > /dev/null --no-verify, #401). A descriptor close (>&-) has no destination, so what follows it is the command’s too; a redirect’s 0 descriptor (0<f) and a here-string are never arguments (#318). Structural when the parse vouches; the raw text match is the permanent fallback floor. The floor reads a quoted flag too, so sh -c 'git commit "-n" …' is refused (#419); a message that starts with such a word is refused with it, the safe direction. Read as text, a background & ends a command, as ; does, so the commit after it is found and a trailing & is not a pathspec.

Main-branch protection

git commit on main/master blocks with the fix named — except the very first commit of a fresh repo (unborn branch), which is on main by definition. Worktree- and submodule-aware, by git’s own pointers (.git file, commondir) rather than the path’s shape, and read from the checkout’s root whichever directory the session started in (#236); file reads, no subprocess.

Co-Authored-By

Appended mechanically as a --trailer (model name from the live session, routing suffix stripped), spliced inside the commit unit — at the byte span the parser assigned the commit, before any && or redirect — so git push does not inherit it. Never duplicates an existing attribution - a Co-Authored-By: line in the message, or a --trailer naming it; a body that only mentions the words is not one (#398) - and leaves --amend alone. The address is coAuthorEmail in gadhs-pi-workflow.json, noreply@dhs.ga.gov by default (#454); a trusted repository may set its own (#558, Configuration Cookbook). Merge commits get no trailer. A merge records integration, not authorship: the merged commits carry their own, a GitLab merge is authored by the merging account, and the guard sends a sanctioned local merge to git merge.

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 [REDACTED <kind>], and a closing line says what was redacted so the model can ask you. The kinds: a private key block, an AWS access key id, GitHub, GitLab and Slack tokens, Google, Anthropic and OpenAI keys, a JWT, and a whole-line NAME=value env dump whose name says secret as a whole segment (GITLAB_TOKEN, not MAX_TOKENS). Precision over recall: code that names a secret (const API_KEY = process.env.X) is untouched. It is a net for an honest slip; a command that prints a secret in another shape is not caught. A refusal’s text (a block reason) is not a tool’s output and is not scanned. A tool’s structuredContent, what a codemode script reads in place of the text, is scanned the same way, string by string at any depth (#390), and is never dropped by a rewrite of the text: pi drops it from any result whose text a handler replaces unless the handler hands it back. An agent message’s subject and body (pi-remote’s msg_post, #565) are redacted the same way before it is sealed and leaves the box, with a line in the body saying so; the post is never blocked for it.

Reflection-Review

The cold review’s verdict, as a second --trailer beside Co-Authored-By on the commit that lands (#273), so it is in history where a reader of the log finds it: CLEAN (<reviewer>), CLEAN, partial (<reviewer>), FLAGS (n), answered (<reviewer>) (a FLAGS commit lands only with its answers), unreviewed, self-review (the reviewer could not be reached), or approved plan, reviewed at exit_plan_mode. A commit carrying declared data paths adds ; <n> data file(s) by count (#916). Never duplicated, never on --amend.

SPDX at write time

A new source file missing the header gets it prepended (comment syntax per language, shebang kept on line 1): .ts, .py, .go, .cs, .ps1, .rb, Gemfile, Rakefile and the like. A file opening with a comment in the header’s own style (Go’s // Package …, a Ruby class doc) gets a blank line between, so the licence never joins that doc comment (#314). Existing files are never retrofitted mid-overwrite. A file git ignores, or one outside the session’s repository, is not the repository’s source and gets no header (#557). git answers that from the file’s nearest existing directory, so a checkout reached through a symlink still gets its headers. A session in no repository adds them as before. The file judged is the one pi’s write tool writes: on Windows a Git Bash path (/d/x) is its drive’s, and over SSH from a Windows client a tool-drive path is the host’s (#567). The header names the repository’s licence, read from the LICENSE (or LICENSE.md, LICENSE.txt, LICENCE, LICENCE.md, LICENCE.txt, COPYING) at its root (#556). An SPDX-License-Identifier: line there is taken as written. Otherwise the standard text is recognised: MIT, Apache-2.0, BSD-2/3-Clause, ISC, MPL-2.0, the GNU family (as -or-later), Unlicense and CC0-1.0. With no such file, or one it does not recognise, the header is AGPL-3.0-or-later.

Debt markers

An added TODO/FIXME/XXX/HACK without #N or a debt-marker-ok: <reason> waiver blocks at authoring time, with all three remedies named. Carried-forward debt does not count, nor does a line only re-indented: lines are compared trimmed (#681).

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 GITLAB_TOKEN (or GITLAB_ACCESS_TOKEN) and no GLAB_CONFIG_DIR, pi-workflow points GLAB_CONFIG_DIR at a fresh empty per-process directory (gadhs-glab-<pid>-XXXXXX in the OS temp directory), removes it on process exit, and sweeps stale leftover directories from dead or pre-#932 runs on startup (#932). Without a token, glab would otherwise fall back silently to the login in ~/.config/glab-cli, which on a shared machine can be another team’s identity. With the empty directory, a glab call authenticates with a token loaded on its own line (set -a; . ./.env.local; set +a; glab …) or fails with 401.

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 core.hooksPath is unset or signing is off, each with the exact command to fix it. It reads the repository’s own config: in a linked worktree, the shared config and the worktree’s config.worktree (#391; it was silent in every worktree before). A setting in ~/.gitconfig is not read, so signing turned on only there still earns the warning.

Changelog outgrown

At session start, when CHANGELOG.adoc (or .md) has an Unreleased section holding more than 60 top-level entries: one warning naming the count, the size, and the last release heading (or that the repository has tags but no release heading, or that neither was found — a release heading is one that begins with a version). Never a block — the cadence is the operator’s; the agent’s part is the guidance’s: add the entry, and say a cut is due.

Batch order

pi runs the tool calls of one message in parallel, preparing every call (these guards included) before running any. So git add then git commit in one message had the commit reviewed before the add ran, and git commit then git push could push first (#261). pi-workflow declares the bash tool sequential, which makes pi run any batch holding a bash call in the order written. It is pi’s own bash tool, built for each call with the shell settings pi reads (shellPath, shellCommandPrefix), and a project’s settings apply only when the project is trusted. pi allows one extension per tool name, so in the distribution pi-ssh’s bash is the one, built the same way and sequential too. pi-workflow registers its own only when no package has claimed bash: installed alone, or with pi-ssh off (#511). A batch with no bash call (reads, greps) stays parallel. In that order, a git step that changes something (add, commit, push, tag, merge, and the like) after a git step that failed or was blocked in the same batch is refused, naming the failed step; a new turn starts clean. Pins: test/batch-order.test.ts (a real pi session on a scripted model: the order, the race without it, the refusal, the settings under each trust decision).

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 git strings) 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 or eval is on the line.

  • Code handed to a shell the parser never opens is unknowable in both its spellings: a -c string (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-verify backstop 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 or eval runs stays visible, so bash <<'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.

Edit this page · latest