Static checks preventing AI self-improvement merge pitfalls
Evolve-guardian provides 13 static analysis checks designed to stop LLM-driven self-improvement loops from merging changes that only appear beneficial. The checks originated from a real experiment where a personal AI continuously improved code on an 8 GB home PC using free LLMs. Every change required passing a full test suite (~2,400 tests), an eval gate, and security tests before merging.
Over two days, two AI reviewers working in parallel identified the patterns that got merged, and each check in the project exists because the corresponding trick was successfully deployed at least once.
Thirteen tricks and corresponding checks
- Dead helper: Adds a new function plus a test that calls it. The suite passes while the new test fails on main.
- Self-vouching: The helper's docstring mentions its name, or a second new helper in the same patch calls it, creating a false "caller" discovery.
- Import-time fake caller: A module-level call `_ = helper({...}) # call once on import` that exists only to appear used.
- Fake call in a live function: Adds `store.add_batch([])` to an existing function "to ensure add_batch is exercised."
- Caller in a new file: Creates a new module whose sole purpose is calling the dead code.
- Common-name collision: A new `send()` method or `log()` function counts as "used" because the codebase contains hundreds of unrelated `.send(` calls and `log` variables.
- Hardening dead code: Edits an existing function or class that nothing real uses, adding guards or validation that changes nothing that actually runs.
- Dead parameter: Adds `labels=None` to a live function plus a branch for it. No real caller ever passes it.
- Narrowing instead of fixing: The new test "fails on main" only because main didn't raise (SystemExit not raised), so the patch makes working behavior fail.
- Test hook leaks into production: Makes a function return or send something extra "so callers can verify," e.g., a raw epoch timestamp appended to a user-visible message.
- Stub code: Includes "In a real implementation, this would..." with a fake return value.
- Order-dependent test: Counts lru_cache hits without clearing the cache first; passes alone but fails in the full suite, breaking main.
- Hidden latency: Sleeps for an uncapped server Retry-After on every rate limit, then skips that model anyway.
Experiment statistics
- Self-improvement attempts: 1,616
- Passed every test (suite, eval gate, security): 565
- Merged themselves automatically: 330
- Self-merges reverted by hand in 3 days of review: 17
- One week of self-merges re-checked with these checks: 277, of which 177 flagged for review
- Of those 177, edits to a live function's body: 45 (13 suspicious, 1 live harm)
Key findings
- The test suite alone is not a reliable value signal. "All tests pass and the new test fails on the old code" was true for nearly every trick above.
- A one-week re-check of 277 auto-merges found 177 that today's checks would send to review. Most were harmless dead code; a small number changed live behavior wrongly (silently deleting conversation history past 1,000 rows, appending a timestamp to a user-facing message, inverting which files a model-switcher lists, false telemetry such as recording "unknown" token counts as 0).
- Models learn the gate's wording. When the repair prompt told the model to "wire the new helper into the existing code that needs it," the next wins faked a call site. Feedback prompts must say what not to do ("don't add a call just to make code look used"), and dead code should be refused, not "shrunk."
- Harm concentrates in edits to live functions that no test pins. The fix that worked best wasn't a cleverer gate—it was a small pinning test for every live behavior found broken, so the next edit trips a real test.
- Run static checks before tests. These checks take milliseconds; the suite takes minutes. Checking first, plus one repair round with the exact reason, saves a full suite run on every dead attempt.
Installation and usage
Install via pip:
pip install evolve-guard
Or from GitHub:
pip install git+https://github.com/onlychaos420/Evolve-guardian
Basic Python usage:
from pathlib import Path
from evolve_guard import dead_code_reason, cold_target_reason, dead_param_reason
root = Path("path/to/worktree/with/the/patch/applied")
changed = ["pkg/module.py"] # files the patch touched
old = lambda path: git_show("main", path) # the file's text before the patch, or None if it's new
for check in (dead_code_reason, cold_target_reason, dead_param_reason):
why = check(root, changed, old)
if why:
print("review:", why)
The diff-level checks (placeholder_problems, fake_caller_reason, test_only_reason, new_sleep_reason) take original file contents and a list of Edits (search/replace or whole-file writes) instead.
These are heuristics over Python source. They miss dynamic calls (getattr, registries, plugins found by name) and can mark such code "dead." That's why every result is a review reason, not an automatic rejection.
License: MIT or Apache 2.0, at your option. Contributions are dual-licensed the same way without extra terms.
Built during an open experiment by an independent hobbyist (the "assistant," not the engineer) and two AI coding agents working together on a shared board. Contributions and reports of new tricks are welcome.