Agent Legacy Code Rewritten in 5 Days Without Showing the New Agent the Old Code
Agents spent years fixing bugs by adding new "if" conditions, and each following agent copied those crutches: in 21 places, the code identified an object's type through "not A and not B." The author of the Habr write-up stopped patching. One agent described the old module's behavior in scenario cards, and a second agent wrote new code from those cards without ever seeing the old code. It took 5 days: 3 for the contract cards and 2 for the implementation. The takeaway: to keep an agent from repeating someone else's mess, hide the old code from it.
AI-processed from Habr AI; edited by Hamidun News
If you have spent months telling an agent "find and fix the bug," you know how it ends: every fix is one more "if" condition stacked on the previous ones. A new agent sees those conditions, takes them as a model, and adds the next one. The author of the Habr write-up got exactly this kind of project, and instead of another patch, he rewrote the problematic module in 5 days: 3 days to describe what the code should do, and 2 days to write a new implementation. He called the method a "clean circuit": an environment where the agent writes new code without seeing the old.
What agent legacy looks like
The project was a B2B SaaS two weeks before release, and two of its three API calls returned HTTP 500 errors. Several generations of agent fixes had accumulated in the code. Object state was determined from indirect signals, and in 21 places the handler recognized its own type through negation: "not A and not B, so it's mine." According to the author, Claude produced an unusually high number of hallucinations on this material even with a small context and could not answer half the questions coherently.
After two days of this work, the author concluded that fixing symptoms was pointless. He rewrote the entire module, reconstructing the original authors' intent from sparse documentation and working code.
How the clean circuit works
The idea is borrowed from clean room design. In 1984, Phoenix Technologies built a BIOS compatible with the IBM PC: one team studied the original and wrote a specification, and another team implemented it without seeing the original code. Here, two agents split the roles.
- The analyst reads the old code, runs the system, and captures snapshots of inputs, outputs, and database changes. It records the results as scenario cards, one per operation. Each card lists the input, the expected result, states, and artifacts, and every line is tagged [measured], [code], or [gap].
- The implementer writes new code only from the cards. It never sees the old code: this is prohibited in the task, the work runs in a separate session, and two linter rules block calls between the old and new code. In addition, a "borrowing guard" compares code trees and reports only the places where they match.
Defects in working functions are carried over as is and fixed in the new circuit, while unused functions are not carried over. The team takes a complete user scenario, switches the calling code to the new implementation after verification, and deletes the old path after acceptance.
The second project was a Rust backend and five Python services in separate repositories. A small feature required coordinated changes in several places, and the contracts in the two languages drifted apart even though the tests for all six components were green. Using the same method, the author moved the scenarios into a single Python monolith in 3 days. The six repositories were archived, HTTP-with-JSON calls between services became ordinary function calls, and mismatches are now caught by mypy type checking.
A charter of 15 rules and the role of the example
Even in a clean environment, an agent tends to hammer in crutches, so the implementer is given a charter of 15 rules. Among them: state is stored in an explicit field and changed by one function according to a transition table; a failure has its own code rather than parsing error text; the kind of object is stored explicitly rather than guessed from a prefix; the server validates whether an action is permitted. There is also an "antigoal rule": if a problem cannot be fixed in three attempts, stop and reconsider the design instead of making a fourth patch.
The name is a response to Anthropic's /goal command, which, according to the author, burns tokens in pursuit of a goal. Some of the rules overlap with Uncle Bob's ub-mode skill, but the charter lives permanently in the repository, while a skill is invoked for a specific task.
The most practical part was an experiment with a weak model. The author ran DeepSeek V4 Flash 36 times, and Claude Sonnet counted the rule violations blind. Without hints there were 36 review comments; with a code example, 31; with the example and the rules in AGENTS.md, 25; and with the example, AGENTS.md, and the requirements stated directly in the prompt, 18.
A telling example: in the old code, an order had no status field, and cancellation was determined by whether the cancel_reason field was filled, in four places. The task was: a cancelled order must not receive new picking assignments. Without rules, the model added a fifth check on cancel_reason. With four rules in the prompt but no example, the result was the same. Only when it received the rules, a sample state machine, and the phrase "Repeat the design, not the names" did an OrderStatus with four values (new, paid, shipped, cancelled), a transition table, and a single validation function appear.
What follows from this
The example works like a seed crystal: the model reproduces what it sees. In a dirty repository it reproduces the dirt, and with one good example it reproduces that example's design. For a weak model, the rules are worth putting into the prompt together with the task, rather than hoping it will read AGENTS.md on its own.
There are limitations too. The method requires several days to write the contract, some of the rules are contested by the author himself, and the DeepSeek conclusions rest on a small sample of 36 runs on one type of task. But the principle can be checked quickly: if agents in your project have spent a second week adding conditions instead of fixing the data model, it is time to change the context, not the number of attempts.
- Dec 2, 2026Ai2 Releases MolmoWeb2 Web Agents: 27B Model Solves 75.3% of Tasks
- Dec 2, 202650 AI Models Beat 62 Teams at 'Zakovat': 48.3% Correct Answers vs 39.8%
- Dec 2, 2026Claude Fable 5.2 ran 1,000 agents for eight days and built an operating system
- Dec 2, 2026Amazon Releases Ergo Open Weights for Coding: 1.1 Trillion Parameters and 2,200 Tokens per Second
Key Questions Answered
What agent legacy looks like?
The project was a B2B SaaS two weeks before release, and two of its three API calls returned HTTP 500 errors. Several generations of agent fixes had accumulated in the code. Object state was determined from indirect signals, and in 21 places the handler recognized its own type through negation: "not A and not B, so it's mine." According to the author, Claude produced an unusually high number of hallucinations on this material even with a small context and could not answer half the questions coherently.
How the clean circuit works?
The idea is borrowed from clean room design. In 1984, Phoenix Technologies built a BIOS compatible with the IBM PC: one team studied the original and wrote a specification, and another team implemented it without seeing the original code. Here, two agents split the roles. The analyst reads the old code, runs the system, and captures snapshots of inputs, outputs, and database changes. It records the results as scenario cards, one per operation.
What are the key details on A charter of 15 rules and the role of the example?
Even in a clean environment, an agent tends to hammer in crutches, so the implementer is given a charter of 15 rules. Among them: state is stored in an explicit field and changed by one function according to a transition table; a failure has its own code rather than parsing error text; the kind of object is stored explicitly rather than guessed from a prefix; the server validates whether an action is permitted.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
