# Darwin-Gödel Self-Rewrite

> An agent rewrites its own code across generations, archives every successful variant, and samples parents from the archive for diversity — escaping the local optima that greedy self-rewrite hits.

- **Category**: Agentic AI
- **Subcategory**: Verification & Reflection
- **Canonical URL**: https://designpattern.fyi/patterns/darwin_godel_self_rewrite/

---

## Description
**Intent**: Let the agent self-improve over many generations without getting stuck on the first plateau it finds.

**Context**: You're running a research agent that can read and rewrite parts of its own implementation. Greedy self-rewrite — always mutating from the current best — has plateaued. You have a benchmark to score variants and want the agent to keep improving without human edits.

**Solution**: Maintain a versioned archive of self-modifications. Each generation: (1) sample a parent from the archive using a diversity-aware policy (not strictly the best); (2) propose a mutation; (3) run a viability gate (compiles, passes safety checks, smoke test); (4) score on the objective; (5) add viable variants to the archive with score and lineage. Sampling from the archive — not just the current best — lets low-scoring but novel variants become parents of future high-scoring ones.


## Use Cases
- The agent can safely rewrite its own implementation (code, prompt, scaffolding).
- A clear objective score is available per variant.
- Greedy self-rewrite has empirically plateaued.





## Trade-offs


### Advantages

- Escapes local optima that greedy self-rewrite can't get past.

- Archive preserves lineage, making regressions debuggable.

- Diversity-weighted sampling reuses old branches as stepping-stones.

- Viability gate keeps the archive populated with runnable variants only.




### Considerations & Drawbacks

- Archive storage and bookkeeping grows with generations.

- Diversity metric is a design choice — a bad one biases the search wrong.

- Viability gate is a single point of failure; a bug there lets broken variants in.

- Self-modifying agents are inherently harder to audit and safety-check.







---
**Reference**: [Original Source](https://www.agentpatternscatalog.org/patterns/darwin-godel-self-rewrite/)

