trs

2026-11-29: the rewrite hook turned a cleanup into rm -rf /*

Status: fixed in 0.10.5 (#315); the whole class closed in #317. Impact: every user-owned app in /Applications deleted on the maintainer’s Mac. Home folder, ~/Library, /Library and system files were not reached.

What happened

An agent session, with the trs hook installed (0.10.4), ran:

S=<scratchpad dir> && du -sh $S && rm -rf $S/*

The hook rewrote it to:

S=<scratchpad dir> trs && TRS_AGENT=claude trs du -sh $S && TRS_AGENT=claude trs rm -rf $S/*

split_env_prefix ran before the && chain split and took the bare assignment S=... as an env prefix of the next word. That word was trs itself, so S went into trs’s environment and the shell never set it. $S expanded empty, rm -rf $S/* became rm -rf /*, and it ran as the user until it was stopped. Root-owned apps survived because the user could not delete them.

The bug had been there since 0.5.9 (2026-04-21). It only fires when a chain starts with a bare assignment (S=x && ...). Tests covered cd X && ... and VAR=x cmd, not that shape.

The class, not the case

The hook inserts trs before X, so trs runs X instead of the shell. Any difference between the shell running X and trs running X changes what the command does, silently. Reviewing #317 found the same class in places that predated the incident, all fixed there:

Shape What trs X did
S=x && rm -rf $S/* S went to trs; $S empty (the incident)
read S, declare S=…, eval S=…, : ${S:=…}, printf -v S set S in trs, empty in the shell
diff a b, tail, read, init, uninstall, help, … ran trs’s own subcommand; trs diff a b executes a
exec 2>/dev/null && … exec trs 2>/dev/null replaced the shell with a bare trs
git commit -m "a && trs b" agent tagging split inside quotes and edited the message
--crate-name=foo cargo build trs --crate-name=foo read it as a trs flag
vim f, less, top, bare python3/psql output became a pipe; editors broke, REPLs hung
! cmd, [[ … ]] the exit status was lost

What prevents it now

src/rewrite_guard.rs, described in agent-integrations.md:

  1. The lock. Every rewrite, including the final command with TRS_AGENT= tags, must be the original with only trs inserted at the head of a segment. Words are compared quote-aware. Anything else drops the rewrite and the command runs as written. A rewrite bug can now cost compression, never behaviour.
  2. Never-wrap rules. Shell builtins and keywords, trs subcommand names, bare redirects, interactive programs, and destructive programs (rm, mv, sudo, git reset --hard, …) are never wrapped.
  3. Tests that execute. rewrite_guard_tests.rs runs each command, its rewrite and the tagged rewrite under sh -u and zsh -f and requires the same output, status and files. A generated matrix of about 600 chain shapes checks the lock. Each rule was mutation-checked: removing it, or restoring the 0.10.4 bug, fails 2 to 18 tests.

Known limits

Lessons