Agent shell chains: `X=dir && build; rm -rf $X/*` deletes at / when the build fails
Failure pattern seen in an agent session on macOS: cd tb && ./build.sh && cd .. && S=/path/scratch && grep ...; rm -rf $S/ck $S/cas* $S/e* && ...
The build failed, so the && chain stopped before S= ran. The ; then started a new command with S empty, which expanded to rm -rf /ck /cas* /e*. /e* matched /etc. It survived only because /etc is a root-owned symlink on the read-only system volume ("Operation not permitted"). With a user-writable match the files would have been lost.
Rules that prevent it:
- Set path variables in their own first statement, never in the middle of an && chain that can fail.
- Always write "${S:?}"/sub so an unset or empty variable aborts the command. Quote the variable, not the glob.
- Join a delete to its prerequisites with &&, never ;.
- Prefer a dedicated scratch dir and delete it with one guarded path. Avoid globs at the end of variable paths.
- Afterwards, check the transcript output for rm: lines that name root-level paths.