The shell guard

The gate decides whether a command may run. The shell guard (@gadhs/pi-shellcheck, #751) reads whether it is well-formed: it catches the shell mistakes that delete the wrong thing, and tells the agent about the ones that are merely bugs. It is deterministic, calls no model, and takes about five milliseconds a command.

Why

Each bash call is its own shell. A variable the line does not set is whatever the environment holds, or empty, and an empty variable changes what a delete deletes:

Written What the program receives Effect

rm -rf "$d"/*

-rf /*

from the root

rm -rf "$d/", rm -rf "$HOME/$d"

-rf /, -rf /home/you/

root, your home

find $d -delete, git clean -fdx $d

no path at all

the current directory, the whole repository

cd $d

no operand: succeeds, in your home

a following rm -rf * runs there

cd "$d"

fails; after ; the next command runs anyway

rm -rf * runs where the shell was

Unquoted and not empty, d="/a/my project" is two targets and d="*" is every entry of the current directory.

What happens to a command

Every bash tool call is read, in every mode, a helper’s and a codemode script’s included. Each thing found is in one of three categories:

Block. The command does not run. The agent gets the reason and the fix, and normally retries once with the fix applied:

shell guard (deterministic policy, not a model judgement): not run.
- delete-variable: `rm -rf "$out"/*` deletes recursively and takes "$out"/* from an
  expansion. Empty or unset, or holding a space or a glob, that deletes something
  else (`rm -rf "$dir"/*` becomes `rm -rf /*`). Fix: write "${out:?}", which stops
  the shell when it is empty.

The guard runs ahead of the permission system, so a refused command never reaches the judge or your /pending queue.

Warn. The command runs, and its result ends with a note:

shellcheck (advisory; the command has already run):
- SC2046: Quote this to prevent word splitting.

A note cannot arrive before the command it is about has run; that is why anything that must not run is a block.

Ignore. Dropped.

The defaults

Block

Id What it catches

delete-variable

A recursive delete - rm -r, find …​ -delete, git clean, rsync --delete, fd …​ -x rm or -X rm (#773) - with a word that holds a variable or a $(…​), quoted or not, unless the variable is guarded: "${dir:?}". One rule for both spellings: the fix is the same, and a rule with no distinction has none to get wrong.

cd-then-delete

A cd on a variable, then a recursive delete in the directory the shell is left in (cd $d; rm -rf *). Passes only when the variable is guarded (cd "${dir:?}") and every operator between the cd and each following delete is && (#967).

SC2115

rm -rf "$dir"/: an empty variable makes it /.

SC2114

A recursive delete of a system directory.

SC2094

Reading and overwriting one file in one pipeline (sort f > f empties f first).

The first two are this package’s own rules, read from the bash parser the harness already loads; they need no shellcheck. shellcheck itself has a delete-specific finding for only one of the forms the two delete rules cover (SC2115). Both see through sudo, doas and the wrappers (env, timeout, nice, …​).

Warn

SC2154 a variable never assigned · SC2046 unquoted $(…​) · SC2061 SC2062 an unquoted pattern for find -name or grep · SC2038 SC2044 SC2045 find | xargs, a loop over find or ls · SC2216 piping into a command that reads no input · SC2088 ~ inside quotes · SC2064 trap "…​$var" · SC2069 2>&1 > file · SC2089 SC2090 quotes stored in a variable · SC2095 ssh in a while read loop · SC2030 SC2031 a variable lost in a subshell · SC2024 sudo and a redirect · SC2144 SC2184 a glob with -e or unset · SC2125 a glob in an assignment · SC2188 a redirect with no command · SC2168 local outside a function · SC1083 SC1010 SC1112 a stray brace, a missing ; before done, a unicode quote.

Ignore

SC1090 SC1091 a sourced file it cannot follow · SC2086 an unquoted variable · SC2012 SC2010 ls where find would do · SC2164 cd without || exit · SC2034 an unused variable · SC2016 $ in single quotes · SC2126 SC2143 SC2181 SC2006 style · SC2015 SC2035 SC2059 · SC2155 SC2207 SC2166 SC2174 · SC2009 SC2013 SC2162 SC2029.

Anything not listed

shellcheck’s own level decides: a warning or an error is a warn, an info or a style is ignored. A syntax error lands here, as a warn: bash does run the lines before it, but shellcheck’s parser can reject bash that bash accepts, and a parse error cannot be waived inline.

Where the lists came from: shellcheck over 2,370 real bash calls warned on 5-8% of them, as often on commands that worked as on ones that failed. On those calls the defaults block about 0.8% (every one an rm -rf "$HOME/…​", one retry each) and note about 1.2%.

Change it

~/.pi/agent/gadhs-pi-shellcheck.json, every key optional:

{
  "block": ["SC2046"],
  "warn": ["SC2094"],
  "ignore": ["SC2154", "delete-variable"],
  "timeoutMs": 2000,
  "binary": "/usr/bin/shellcheck",
  "enabled": true
}

An id you name in a list is in that category, whatever its default. Ids are SC and four digits, delete-variable or cd-then-delete. timeoutMs bounds one shellcheck run. binary names a shellcheck to use instead of the downloaded one. enabled: false turns the whole guard off. The file is read when pi starts or reloads.

An unknown key, a malformed id, or an id in two lists is refused by name at session start, and the shipped defaults then apply whole: a typo never turns the guard off.

Only your agent directory is read. A repository cannot supply this file, and the shipped mode rules keep an agent from writing it.

When a block is wrong

For a shellcheck code, shellcheck’s own directive on the line above the command waives it, and the refusal says so:

# shellcheck disable=SC2094
sort f > f

The two delete rules take no waiver: their fix is always available.

The shellcheck binary

shellcheck v0.11.0 is downloaded from its GitHub release the first time a seat lacks it, checked against the release’s SHA-256, and kept under ~/.pi/agent/gadhs-pi-shellcheck/; it is checked again before use. Builds exist for Linux and macOS on x64 and arm64 and for Windows on x64. The download happens in the background, and the session says when its checks come on. The package ships no copy: shellcheck is GPL-3.0 and is fetched from its publisher.

Without a shellcheck - PI_OFFLINE, another platform, a failed download - the session says so once, the shellcheck codes are off, and the package’s own rules still apply: the two delete rules. binary in the settings points at one you installed.

What it does not do

  • It reads the command line, not what the command runs: nothing inside bash -c '…​', ssh host '…​', xargs rm, or a script file.

  • It reads bash, not powershell, and not the ! commands you type.

  • It models mistakes, not intent. cd /tmp/x; rm -rf * with a literal path is not refused; nor is a delete behind a program it does not know.

  • It never refuses by failing: a shellcheck that is missing, slow or broken is skipped, and so are the package’s own rules while the parser is not loaded. A shellcheck run that fails or times out is a shellcheck.skipped record in the diagnostics, as is the first line the cold parser cannot read; a shellcheck that is missing is one shellcheck.unavailable record.

  • Passing the guard allows nothing. The mode’s rules and the judge decide what may run, as before.

Diagnostics records: shellcheck.block and shellcheck.warn (the ids), shellcheck.skipped (which half, and why), shellcheck.unavailable.

Edit this page · latest