Leanpub Header

Skip to main content

Claude Code Safety: Stop Your AI Agent from Destroying Your Work

The Claude Code hooks that stop a destructive rm, a force push, a dropped database and a discarded working tree — and the data loss runbook for when one gets through.

Claude Code Safety: Stop Your AI Agent from Destroying Your Work
This book is 100% completeLast updated on 2026-09-15
+11,319 words in the last 30 days

You leave Claude Code running and come back to something missing. This book is arranged by the accident, not the mechanism: databases dropped, files deleted, git history rewritten, the rewind command taking work with it. Three of the four get the exact hook that blocks the command before it runs, with the recovery if it already happened; no PreToolUse hook sees the rewind command, and the book gives the settings-level lever and names the one place a refusal could still live. Appendix B prints all 195 commands the guards were fired at, refusals and allowances both.

Minimum price

$5.99

$9.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

Buying multiple copies for your team? See below for a discount!

PDF
EPUB
WEB
APP
About

About

About the Book

You let Claude Code edit files, run shell commands, touch your database — maybe unattended, while you do something else. The question this book answers is the one in the back of your mind: what stops it from destroying something you cannot get back? That fear is not paranoia. An agent ran drizzle-kit push --force on a production Postgres and wiped more than sixty tables (#27063). Another escalated to prisma db push --force-reset and dropped all eighty-seven (#36183). A third was asked only to investigate a production error and ran ALTER TABLE ... DROP COLUMN twice on the live database (#46684). Every incident here is cited by issue number so you can read the original yourself — with one exception, a widely reported third-party case, which the book flags in the table as press-sourced rather than letting it sit unmarked among the others. Four classes of irreversible accident — databases, files, git history, and /rewind — each get the defence that applies and an ordered recovery runbook, with an honest line about what is genuinely gone. Three of the four get the exact PreToolUse hook that stops the command before it runs, ready to paste into settings.json; /rewind does not, because slash commands are not tool calls and no PreToolUse hook sees one, so that chapter gives the settings-level lever, the recovery order, and the one place a refusal could still live. The git-history chapter is built from the mechanism and the blockable list rather than from a single cited incident. What you get that a list of hooks does not give you: - A guard that was fired in both directions, and the results are printed. 195 commands: 123 that must be blocked, 72 ordinary ones that must not. The passing set is what decides whether you keep the guard — git stash push passes while git stash drop is blocked; git push origin main passes while git push --force is blocked; rm -rf ~/projects/app/node_modules passes while rm -rf ~/Documents is blocked; docker compose down passes while docker compose down -v is blocked. - The false positives, with the exact commands. You will know before you install it which harmless things it interrupts, so you can decide once instead of discovering them one irritation at a time. - The four things it genuinely cannot catch, also with commands — including the empty-variable accident that is Chapter 2's own headline story, because a hook sees the command before the shell expands it. Where the boundary is, and what covers the other side of it. - The one line that decides whether any of it runs. On a machine without jq, a guard like this exits 0 — which Claude Code reads as permission, with no error and no warning. The book shows the check that makes that condition announce itself instead, and why the exit code you choose determines whether the warning reaches a human at all. - A setting that removes /rewind's destructive option at the source, confirmed in the shipped 2.1.258 binary, with the part that was not tested kept clearly separate from the part that was. - Where each cited issue stands today. Five of the six closed for inactivity, not because anything was fixed. The sixth closed as "completed" and users kept reporting the same behaviour afterwards. The book explains why, in that table, "completed" is the weakest signal on the page — a habit you can apply to any tracker. Also: a chapter on why writing "never delete the database" in CLAUDE.md is not a control point you can check — with the documented case of exactly that rule being ignored, and with my own 48 runs measuring how often a written rule actually holds — and what to use instead. Written for the individual operator, not a security team. Fifteen minutes gets you a recoverable setup. The guards come from cc-safe-setup, the author's free MIT-licensed hook collection; this book is the organised depth on top of it, not the only way to stay safe. Independent, and not affiliated with Anthropic or any provider. The verification runs described in the first person were carried out by the author's Claude Code agent, on the author's machine, under the author's direction — which is also the setup this book is about. This book is also in Claude Code Operator's Library: Complete Edition - seven handbooks for $39, against $74.94 bought separately: https://leanpub.com/b/cc-operators-complete

Team Discounts

Team Discounts

Get a team discount on this book!

  • Up to 3 members

    Minimum price
    $14.00
    Suggested price
    $24.00
  • Up to 5 members

    Minimum price
    $23.00
    Suggested price
    $39.00
  • Up to 10 members

    Minimum price
    $41.00
    Suggested price
    $69.00
  • Up to 15 members

    Minimum price
    $59.00
    Suggested price
    $99.00
  • Up to 25 members

    Minimum price
    $89.00
    Suggested price
    $149

Bundle

Bundles that include this book

Author

About the Author

yurukusa

Everything I publish starts from something that actually broke.

I run Claude Code on real projects, often unattended, and I have lost work to it. Most of what the books catalogue did not happen to me, though. It happened to other people, in public: every incident is tied to a numbered issue you can open and read, including the ones that were closed without a fix. The hooks in cc-safe-setup (MIT, open source) exist because I wanted a floor under an agent nobody was watching, and I would rather show you a stranger's incident with a link than a story of my own without one.

Two things I hold myself to. If a number in one of my books is not something I measured, it says so. And if a command is printed, the output beside it came back from an actual run — failures included, because a check that cannot fail is not a check.

I am not selling certainty. Most of what I know here, I learned by breaking my own setup.

Contents

Table of Contents

Claude Code Safety

  1. Who this is for
  2. What’s inside
  3. How to use it
  4. A note on accuracy and honesty

Chapter 1 — When your agent destroys your database

  1. 1.1 What actually happened
  2. 1.2 Why an autonomous agent reaches for the destructive flag
  3. 1.3 Why a note in CLAUDE.md is not the control you need
  4. 1.4 The block — a hook that runs before the command does
  5. 1.5 If it already ran — the recovery order
  6. 1.6 The one-paragraph version

Chapter 2 — When your agent deletes your files

  1. 2.1 What actually happens
  2. 2.2 Why an agent does this
  3. 2.3 The block — stop the fatal target, recycle the rest
  4. 2.4 If it already ran — the recovery order
  5. 2.5 The one-paragraph version

Chapter 3 — When you lose your git history

  1. 3.1 What actually happens
  2. 3.2 Why an agent does this
  3. 3.3 The shield — save first, block the irreversible
  4. 3.4 If it already ran — the recovery order
  5. 3.5 The one-paragraph version

Chapter 4 — When /rewind eats your code

  1. 4.1 What actually happens
  2. 4.2 What /rewind cannot touch — your recovery map
  3. 4.3 If it already happened — the recovery order
  4. 4.4 Prevention — and why no hook can confirm the menu
  5. 4.5 The one-paragraph version

Chapter 5 — Why a written rule is not a control point, and a hook is

  1. 5.1 What a written rule does, and what it cannot promise
  2. 5.2 Why a clear instruction still gets ignored
  3. 5.3 What a hook is, and why it cannot be talked out of
  4. 5.4 Test your hook — or you have a paragraph with extra steps
  5. 5.5 The one-paragraph version

Chapter 6 — Building your safety net

  1. 6.1 The complete guard — one block, paste once
  2. 6.2 What it covers, and what it does not
  3. 6.3 The second layer — backups and checkpoints
  4. 6.4 The maintained set — install in one line
  5. 6.5 The discipline, in one line

Appendix — The incident catalog

  1. Database destruction
  2. File destruction
  3. /rewind and checkpoint data loss
  4. Widely reported industry incident
  5. How to read this table
  6. Where these issues stand now

Appendix B: The test set, in full

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

Also by the Author

Also by the Author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub