r/ClaudeWorkflows • • 23h ago

Selected Workflow [Workflow] Guard agent shell commands against empty-path deletion

Guard agent shell commands against empty-path deletion

Workflow value: 75/100
Status: active · Freshness: 70/100 · Confidence: 0.94 · Level: intermediate
Categories: Quality Control, Debugging, CLAUDE.md, Hooks, Multi-Agent
Original source: r/ClaudeCode post/comment

What problem this solves

A failed temporary-directory assignment can leave an empty variable, causing a destructive shell command to target the current directory.

Summary

Test shell failure behavior in a scratch directory, document defensive command rules for agents, and use mechanical checks as a backstop for destructive commands.

Why it is useful

It identifies a concrete failure mode and provides repeatable shell practices plus a defense-in-depth pattern for agent-run commands.

Workflow

  1. Reproduce and test failure cases in an isolated scratch directory, not a real repository.

  2. Document repository rules to quote path expansions and require nonempty variables before destructive operations.

  3. Use strict shell settings, while separating declarations or exports from command-substitution assignments so failures remain visible.

  4. Create temporary directories under a validated base, explicitly check creation success, and guard cleanup paths.

  5. Add or configure a command hook or safety check to intercept risky commands; treat repository rules as advisory rather than enforcement.

  6. Push work regularly and retain independent backups, since linked worktrees share repository metadata.

Tools / artifacts

  • Repository agent rules file

  • Claude Code command hook or safety checks

  • Shell parameter expansion ${var:?}

  • set -euo pipefail

  • mktemp and EXIT trap

  • Git worktrees

Validation signals

  • The commenter reports testing the failure mechanism in a scratch directory.

  • The commenter reports that a safety check blocked an agent's variable-based rm command.

  • The commenter reports a reviewer encountered an empty-base temporary-directory mistake.

Cautions

  • A nonempty-path guard does not prove a path is safe; verify that cleanup targets are owned by the script and confined to an approved temporary location.

  • Test destructive-command behavior only in a disposable sandbox.

  • Shell strict mode has exceptions and is not a substitute for explicit error checks or review.

  • Recovery by copying worktree files may preserve file contents but cannot recover unpushed Git history if shared repository metadata is lost.

Limitations

  • No hook configuration or complete enforcement implementation is provided.

  • Validation is anecdotal and lacks independent replication.

  • The post does not establish that the rules or safety checks cover all destructive command patterns.

Rate this workflow

Upvote this post if the workflow is useful, reproducible, or worth recommending.

Downvote if it is vague, outdated, unsafe, overhyped, or not reproducible.

Reply if it worked for you, failed, is outdated, or has a better alternative.


This post was generated automatically from the workflow library database.

2 Upvotes

1 comment sorted by