After a few months of daily work with Claude Code, I have hit six recurring failure modes: deleting the wrong files, editing the wrong files, leaking secrets, getting stuck in infinite loops, breaking batch jobs mid-flight, and working in the wrong repository. None of these were one-off flukes—they happen over and over unless you put guardrails in place.

This post walks through each incident, explains why it happened, and gives you copy-pasteable configurations to block it. Drop these safeguards into your repo and you will dodge half the pitfalls up front.


1. File Operations: Wrong File Deleted & Wrong Project Modified

Meltdown 1: Claude Deleted Something It Shouldn’t Have

I asked Claude to clean up “unused files” in a project. It ran rm -rf on a config directory whose name included deprecated—assuming it was safe to remove—and wiped out a local override file I maintained by hand.

Recovery: Luckily it had been committed earlier, so a single command brought it back:

1
2
3
4
# Restore a deleted file or directory
git restore path/to/deleted-file
# Or restore the entire directory
git restore path/to/deprecated-config/

Prevention: Intercept dangerous commands with a PreToolUse Hook. Add this to .claude/settings.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "cat /dev/stdin | jq -r '.tool_input.command // empty' | grep -qP 'rm\\s+(-[a-zA-Z]*f[a-zA-Z]*\\s+|.*--recursive)' && echo 'BLOCKED: dangerous rm command detected' >&2 && exit 2; exit 0"
}
]
}
]
}
}

Now any rm -rf attempt hits a hard block via exit 2. (Note: only exit code 2 blocks tool execution and sends stderr back to Claude; exit 1 is treated as a non-blocking warning and will not stop the command.)

Pair it with a hard rule in CLAUDE.md:

1
2
# Add to CLAUDE.md:
- Confirm before any irreversible action (deleting or overwriting unrecoverable content)

Meltdown 2: Working in the Wrong Project Directory

I had two terminals open—one in my blog repo, one in a work repo. I launched Claude in the wrong window, and it obediently created blog files inside the work project.

Prevention: Put the project name and path at the top of CLAUDE.md so Claude reads and anchors on it right at startup:

1
2
3
# Top of CLAUDE.md:
# Project: Hippo's Tech Blog (hippo0913.github.io)
# Local path: ~/hippo/blog/hippo0913.github.io

Also build the habit of checking your working directory before launching Claude:

1
2
# Verify the current directory before launching claude
pwd && ls package.json _config.yml 2>/dev/null && claude

2. Session Management: Context Overflow & Infinite Loops

Meltdown 3: Context Overflow Causing “Ghost Edits”

Deep into a long session, I asked Claude to update config file A. It wrote the changes into file B instead. After dozens of turns, it had lost track of which file we were supposed to touch.

How to spot context filling up: The Claude Code status line displays context usage as a percentage. Once it crosses 70%, compact or start a fresh session.

Key commands:

1
2
3
4
5
# Context at 50-70%: compact to retain key state
/compact

# Context above 70%: wipe it and start fresh
/clear

Another habit that pays off immediately: always include full file paths in your prompt. Compare the two:

1
2
3
4
5
# Error-prone: vague reference to "the config file"
"Change the port in the config file to 8080"

# Reliable: explicit full file path
"Edit /home/user/hippo/blog/hippo0913.github.io/_config.yml and change port from 4000 to 8080"

Meltdown 4: Claude Stuck in an Infinite Loop

I asked Claude to fix a failing test. It went: search → edit → test → fail → search → edit somewhere else → test → fail again—for 8 rounds straight, patching minor side issues each time without ever touching the root cause.

Prevention: Set a turn cap when launching non-interactive runs:

1
2
# Cap a single non-interactive task at 10 turns (--max-turns requires -p)
claude -p --max-turns 10 "Fix the failing tests in src/auth.test.ts"

And steer the prompt so it diagnoses before editing:

1
2
3
4
Fix the failing tests in src/auth.test.ts. Rules:
1. Read both the test file and the code under test first, and diagnose the root cause
2. If tests still fail after 3 consecutive fix attempts, stop, output your analysis, and wait for my input
3. Fix the root cause, not the symptoms

3. Security: Leaking Secrets to GitHub

Meltdown 5: Pushing Secrets to GitHub

I asked Claude to write a Feishu notification script. It hardcoded the webhook URL straight into the source file and ran git push. .gitignore only covered .env—it could not catch a secret sitting inside a .js file.

Bad vs. good:

1
2
3
4
5
6
7
8
9
// Bad: hardcoded webhook URL
const FEISHU_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx";

// Good: read from an environment variable
const FEISHU_WEBHOOK = process.env.FEISHU_WEBHOOK_URL;
if (!FEISHU_WEBHOOK) {
console.error("Missing environment variable: FEISHU_WEBHOOK_URL");
process.exit(1);
}

**Security rules in CLAUDE.md**—writing them down explicitly is far more reliable than hoping Claude remembers:

1
2
3
4
## Security Rules
- Store all secrets, tokens, and webhook URLs in environment variables; never hardcode them
- Never commit .env, credentials.json, or other secret files to git
- Inspect git diff before pushing to confirm no sensitive data is included

Pre-push check:

1
2
# Check the latest commit diff for potential secrets before pushing
git --no-pager diff HEAD~1 | grep -iE '(api_key|secret|token|password|webhook)' && echo "WARNING: Possible secret detected!" || echo "OK"

4. Orchestration: Subagent Batch Jobs Going Half-Baked

Meltdown 6: Inconsistent Batch Job State

I used Subagents to rewrite 20 articles in parallel. Several agents hit API rate limits (429), the orchestrator failed to handle partial errors cleanly, and the repo ended up half old, half new.

Safeguard 1: A progress file. Track each subtask’s state in JSON:

1
2
3
4
5
6
7
8
{
"tasks": [
{"file": "article-01.md", "status": "completed", "score": 85},
{"file": "article-02.md", "status": "failed", "error": "429 rate limit", "retry_count": 0},
{"file": "article-03.md", "status": "pending"}
],
"updated_at": "2026-04-07T10:30:00Z"
}

Safeguard 2: Spell out the failure policy in the orchestration prompt:

1
2
3
4
5
Run the batch article rewrite. Rules:
- Process at most 3 articles per batch; finish one batch before starting the next
- Single-article failure: mark as failed and move on to the next article without retrying
- Full-batch failure (e.g., 429): wait 30 seconds, retry once, and abort if it still fails
- Update .claude/progress.json after each article completes

Safeguard 3: Run in small batches:

1
2
3
4
5
# Run in batches of 3 to avoid rate limits
for i in {1..7}; do
claude -p --max-turns 15 "Process batch $i of the article rewrite using .claude/progress.json"
sleep 60
done

5. Cheat Sheet: Failure Modes & Safeguards

Failure Mode Root Cause Recovery Command Prevention Setup
1. Wrong file deleted Shallow judgment on “unused” files git restore <path> PreToolUse Hook blocking rm -rf
2. Wrong project modified Mixed-up working directories Delete the stray files Project name in CLAUDE.md + pwd check
3. Ghost edits Context overflow Fix the wrong file manually /compact + full paths in prompts
4. Infinite loop Patching symptoms instead of root cause Press Esc to interrupt Prompt requiring root-cause analysis
5. Secret leaked Missing security guardrails Rotate the secret immediately Environment variables + git diff check
6. Broken batch job Missing failure handling Roll back via progress file Batches of 3 + JSON progress file

All six meltdowns share the same takeaway: the problem wasn’t Claude—it was missing constraints. The next post dives into context management for long-running tasks, showing how to prevent ghost edits and forgotten rules.


Series Navigation