Skip to content

feat(approval): CST shell classifier with a Forbidden tier; shell v2 environment (T6) - #40

Merged
lg2m merged 4 commits into
mainfrom
feat/tool-layer-t6-classifier
Sep 14, 2026
Merged

lg2m merged 4 commits into
mainfrom
feat/tool-layer-t6-classifier

Conversation

@lg2m

@lg2m lg2m commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

T6: a CST shell classifier with a Forbidden tier above every approval mode; shell v2 with a cleared environment

Spec: docs/plans/tool-layer.md § D5 (T6) Linear: ENG-776 ADR: docs/adr/0020-shell-forbidden-classifier.md Follows: #37 (T5)

First of the two stacked PRs for the v0.1.0 "safety" release. T7 (exec) is stacked on this branch.

What changed

Safety (user-visible)

  • Shell commands are parsed with tree-sitter-bash and judged on a three-tier lattice — Forbidden > Prompt > Allow — strictest verdict over every simple command anywhere in the tree: subshells, if/for bodies, $(…), function bodies, and wrappers (env -i, nice, nohup, time, timeout, stdbuf, xargs, sudo|doas|su, sh|bash|zsh -c STRING reparsed) are all seen.
  • Forbidden is refused under every mode, full included: rm -rf on root/home/outside the workspace, sudo, raw device writes, mkfs/fdisk, power state, git push --force|+ref|--delete|--mirror, chmod -R 777 /, curl | sh, dynamic eval, fork bomb, history -c/shred/crontab -r, writes to shell profiles/~/.ssh//etc, reverse shells, LD_PRELOAD/PATH/GIT_SSH_COMMAND-class env prefixes. The model gets a tool error naming the rule and an alternative. Only a grant quoting the exact command lifts it; prefix grants lift Prompt only.
  • auto now executes exactly what the classifier allows (build/test/inspect shapes with literal argv and workspace-relative operands) and asks for the rest — replacing a substring heuristic that a subshell walked past.
  • ShellCommandPreview gains verdict and reasons (rule ids) so the TUI can say why it is asking.

Shell v2

  • The child starts from a cleared environment + PATH HOME LANG TERM TMPDIR. A call names extra variables in env (≤ 16); each must be in policy.shell_env or the call fails env_not_allowed before anything runs. Server secrets no longer leak into children by accident.
  • policy.builtin_preference: off | hint | strict. hint (default) appends hint: use search instead of grep; … when the first program has a bounded built-in; strict refuses with use_builtin before execution — the ablation arm for measuring what shell habit costs.
  • New config keys layer like grants (shell_env is trust-gated, supports Remove(...)); builtin_preference may only tighten across layers. qq config show prints both.

Rules as data. approval/rules.rs is a static table; rule_examples_hold runs ~250 match/not_match examples (~90 Allow, ~75 Prompt, ~80 Forbidden) as one test, so intent and reach are checked together.

Plumbing

  • tree-sitter/tree-sitter-bash promoted to [workspace.dependencies] (qq-tui already had both) — no new package in the lock. Root request closed in root.md.
  • ShellPolicy travels Runtime → AgentProfile → CompiledAgentPlan → tool loop → run_shell, mirroring AuditPolicy.
  • AGENT_PROMPT_VERSION 10 → 11: T2–T6 changed the prompt text without a bump; this catches up once at the release boundary. Golden descriptor digest and tool-schema hash follow.
  • The old dangerous_shell_command tokenizer is removed.

How verified

  • cargo test --workspace: 1389 passed / 0 failed; provider minimal 162; fmt and clippy -D warnings clean.
  • New: 4 classifier unit tests, 4 rules tests (incl. the example table), forbidden_commands_are_refused_under_every_mode_unless_quoted_exactly, 2 shell tests (cleared env / allowlist / typed refusals; hint on/off), 1 config layering test + 4 validation cases, 2 shell_policy tests.
  • Updated: fixtures that used rm -rf / as a Prompt example now use rm -rf target (it is correctly Forbidden); the T4 end-to-end shell test runs under full; the headless smoke's printf > file became cp — a writing redirect now asks under auto, which is the intended behaviour.

Measurements

Gate Result
classify_command simple / pipeline / wrapped-forbidden 3.4 / 10.3 / 9.6 µs
classify_command 1 KiB one-liner (gate ≤ 200 µs) 166 µs
over the 16 KiB ceiling (no parse) 4 ns
tool_dispatch A/B (12 pairs, idle core, median) 46.9 → 47.5 µs (noise; the read loop never classifies)

Evidence: target/qq-perf/t6-2026-09-14/.

Docs

  • ADR-0020 (new); docs/design/tools.md § Shell Execution (env, nudge), § Shell Classification (new), § Approval Policy (auto/full), § Workspace Grant Configuration (new keys); ledger receipt.

Compatibility

  • Behaviour change under full: Forbidden shapes are now refused. Under auto: commands with $VAR, globs, or writing redirects now ask where the old heuristic ran them.
  • Shell children no longer inherit the server environment. A workflow that relied on e.g. AWS_PROFILE reaching a child must list it in policy.shell_env and the call's env. This is the intended hardening; the error names the fix.
  • Protocol: ShellCommandPreview.verdict/reasons are additive with skip_serializing_if; PROTOCOL_VERSION bump deferred to the release PR so one bump covers T6+T7. Store schema unchanged. Prompt version 11.

Deviations from the plan (ledger)

  • PolicyDecision::Forbidden { rules } as its own variant rather than Deny { reason }.
  • Allow table broader than the abridged plan list (linters, compilers, infra CLIs in read shapes) and stricter on argument shapes; cut … /etc/passwd prompts (outside workspace).
  • ~40 rule ids with argv predicates rather than ~120 flat rules.

Follow-ups

  • T7 (stacked): exec. T13: tune the Allow table from the measured auto prompt rate. Configurable rule overrides only when a user asks.

…pendencies for the shell classifier

qq-tui already depended on both; qq-core now shares the rows. No new
packages enter the lock. Starts tool-layer T6.
…ested rules (T6)

Shell commands are classified over a tree-sitter-bash parse instead of a
hand tokenizer. Two walks: word_only_sequence accepts only
`cmd (&& || ; |) cmd…` of literal words and is the sole path to Allow;
the collector finds every simple command anywhere (subshells, control
flow, $(…), function bodies), peels wrappers (env, nice, nohup, time,
timeout, stdbuf, xargs, sudo/doas/su, sh -c STRING reparsed), and
records redirect targets. Parse errors, dynamic words, unlisted programs,
and constructs classify Prompt. The lattice is Forbidden > Prompt >
Allow, strictest over every simple command.

Rules are a static table (approval/rules.rs) with ~250 match/not_match
examples run as one unit test: Allow for build/test/inspect shapes with
literal argv and workspace-relative operands; Prompt for deletions, git
mutations and remotes, mode changes, in-place edits, installs,
containers, downloads, signals, xargs/tee, redirects, inline
interpreters, paths outside the workspace; Forbidden for rm -rf on root
or home or outside the workspace, privilege escalation, raw device
writes, filesystem formatting, power state, git force-push, recursive
mode changes on root, download piped to an interpreter, dynamic eval,
fork bomb, history/shred, shell-profile and ~/.ssh writes, reverse
shells, and LD_PRELOAD/PATH/GIT_SSH_COMMAND-class env prefixes.

PolicyDecision gains Forbidden { rules }: refused under every mode
including full, unless a grant quotes the exact command string; the
model gets a tool error naming the rule and an alternative. Auto mode
now executes exactly what the classifier allows and asks for the rest.
ShellCommandPreview gains verdict and reasons (additive, protocol
version unchanged pending the release bump) so clients can say why.

tree-sitter and tree-sitter-bash are promoted to workspace dependencies
(qq-tui already had both); the lock gains no package. New
classify_command bench: 3.3 µs simple, 10 µs pipeline, 163 µs for a
1 KiB one-liner (≤ 200 µs gate), 7 ns past the 16 KiB ceiling.
…built-in preference (T6)

The shell child now starts from a cleared environment plus PATH HOME LANG
TERM TMPDIR; a call names further variables in `env` (≤ 16), each of
which must be in `policy.shell_env` or the call fails env_not_allowed
before anything runs. Secrets the server holds no longer reach children
by accident.

policy.shell_env layers like a grant (trust-gated, Remove(...) supported,
validated as variable names, ≤ 64). policy.builtin_preference is
off | hint (default) | strict and may only tighten across layers. hint
appends one `hint: use search instead of grep; …` line when a command's
first program has a bounded built-in; strict refuses it before execution
with use_builtin — the benchmark arm for measuring what shell habit costs.

ShellPolicy travels Runtime → AgentProfile → CompiledAgentPlan → the tool
loop → run_shell, mirroring AuditPolicy. qq config show prints both keys.

The base agent prompt moves to version 11 (the T2–T6 tool-layer series
changed its text without a bump); the golden descriptor digest and the
tool-schema hash follow.
@lg2m
lg2m force-pushed the feat/tool-layer-t6-classifier branch from 11a5398 to b0e58fe Compare September 14, 2026 21:51
@lg2m
lg2m merged commit 91809b2 into main Sep 14, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t6-classifier branch September 14, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant