← Back to library

Self-editing agent with snapshots and automatic rollback

PedroMFernandes built Miguel, an agent that rewrites its own source inside a Docker container. A protected runner on the host takes a git snapshot, hands over the next feature to add, then validates syntax, JSON schemas and imports, committing on success or rolling back on failure. Fixed rules it cannot edit stopped it from piling up thin wrapper tools.

  • OutcomeLet an agent rewrite itself in a container while a protected runner snapshots, validates and rolls back every change it makes.
  • SetupAn afternoon+
  • LevelAdvanced
  • CreditPedroMFernandes on Hacker News
View original
Works with
ClaudeCustom agent

Published Oct 23, 2026Updated Oct 23, 2026AI dry run on sample input: passed Oct 23, 2026Source verified Oct 23, 2026

When should you use it?

  • You want to experiment with an agent that improves itself
  • You can run Docker
  • You will read the commit history

Not for: Production systems; Anyone without a rollback plan

What do you need, and what do you get?

You need

  • Agent code and capability list
  • Docker setup
  • Validation checks and fixed rules

You get

  • Commits for each change
  • Automatic rollbacks
  • Growing capability list

How does it work, step by step?

  1. Split the trustKeep the runner, git and checks on the host, where the agent cannot touch them.
  2. Mount read-onlyLet the agent write only to its own code folder.
  3. Snapshot firstTake a git snapshot before each change.
  4. ValidateRun syntax, schema and import checks and roll back on any failure.
  5. Add fixed rulesGive it rules it can read but never edit, such as keeping tool count low.

The prompt

Our own prompt, inspired by PedroMFernandes on Hacker News and written to work in Claude and Custom agent. Values in lime are placeholders.

Prompt0 variables
Design a guarded runner for an agent that edits its own code. The runner lives outside the sandbox and must: snapshot with git, send the next task, run syntax, schema and import checks, commit on pass and roll back on fail.
List the fixed rules the agent can read but not edit, and how to cap context and spend per batch.

Inspired by PedroMFernandes on Hacker News: Comment in Miguel: An AI agent that modifies its own source code, sandboxed in Docker. The prompt above is written by playbooks.bot in our own words; nothing from the original is quoted.

What can go wrong?

  • Context runs out when the agent reads too many of its own files; budget it.
  • Agents add thin wrapper tools; each one costs context.
  • API spend sets the pace; cap it.

Our take

A self-modifying agent sounds like a bad idea until you see where PedroMFernandes put the trust boundary. Miguel can change its own files, but the snapshot, the checks and the decision to commit live on the host, out of reach, so a broken change simply vanishes. That separation is the transferable lesson for any agent that edits important things: let it work freely inside a sandbox, and keep the judge outside. The project's lessons are also practical. Miguel tended to pile up flimsy wrappers around existing tools, and every tool costs context, so fewer, broader tools worked better; fixed rules that the agent cannot rewrite were needed to stop the habit. The most dangerous failure was running out of context before doing the actual work. Keep API spend visible per batch, as the author did, so experiments stay cheap. If you try something similar, build the snapshot and rollback first, test that rollback works by deliberately breaking a change, and only then let the agent touch its own code.

Where the time goes back: Self-improvement experiments run safely because every change can be undone.

FAQ

What surprised the author?
After fixed rules were added, the agent ran a cleanup batch that shrank its prompt a lot while keeping features.
What was the worst failure?
Running out of context before writing code, per the author.

Source and credit

Inspired by PedroMFernandes on Hacker News. We describe the idea in our own words, link to the original, and wrote the prompt ourselves; nothing is quoted.

Inspired by
Comment in Miguel: An AI agent that modifies its own source code, sandboxed in Docker
Creator
PedroMFernandes on Hacker News
Published on
Hacker News
Original date
Mar 10, 2026
License
Not openly licensed (the poster keeps all rights), so we quote nothing and link to the original
Originally for
Custom agent
AI dry run
Oct 23, 2026: passed. Dry-run tested by our AI test agent on sample input. PASS: Gates held on desk check; no live sends, bookings or purchases.
AI dry run on sample data; not run with live connected accounts. The playbooks.bot test agent filled the prompt with synthetic inputs, produced the output and checked it against the page's stated outputs and guardrails.
Verified
Oct 23, 2026: source link resolves to the original, the credited creator published it, our excerpt or prompt matches it, and the license allows our use.

Are you the creator? Claim or correct this page.

More in IT & Security

All 82 IT & Security playbooks →

Related playbooks

IT & SecurityWorkflow✓ AI dry run

Distill agent sessions into shared memory every night

Save each completed agent session to its own transcript file, have one analyzer agent per transcript verify, enrich and organize small linked memory files, run an organizer once to merge duplicates and keep an index current, then attach the store to the agent's next session with read access only and repeat the pass nightly on new sessions.

Credit: Anthropic

An afternoon+
IT & SecurityWorkflow✓ AI dry run

Let coding agents fetch docs into local Markdown

Stop manually fetching docs for an agent by giving it a tool that crawls a documentation section into clean Markdown it can search later.

Credit: Jessica Doering

Under an hour
IT & SecurityPrompt✓ AI dry run

Question an agent's pull request instead of skimming reviews

Have a fresh agent list every concrete change in an agent-written pull request (schema, scheduled jobs, features, new services, settings, endpoints and removals) without judging it, then press it on each item you cannot explain, give it the product context it lacks, and cut whatever nobody can justify before you merge.

Credit: u/EstablishmentGood414 on r/codex

Minutes
IT & SecurityWorkflow✓ AI dry run

Let a coordinator agent watch merged PRs

Use a project-level coordinator agent that splits work among helper agents, keeps an eye on fresh merges for bugs, and reports progress with a recommendation when it needs you.

Credit: Chase Roberts (@chsrbrts)

An afternoon+
IT & SecurityWorkflow✓ AI dry run

Move heavy agent builds off your laptop

Let coding agents log in to remote build machines so heavy builds and tests run there instead of slowing your laptop during calls.

Credit: bentcorner on Hacker News

Under an hour