Clean-room rewrite of agent-patched legacy
Чистый контур — агентская прачечная для грязного легаси
B · Useful, limited A first-hand Habr opinion piece by developer Sergey Blekher with a concrete clean-room method, a 15-rule charter and a small blind-scored experiment; useful practice, but a single author's projects and a tiny sample.
Summary based on the full text·Original: about 5 minutes to read
In short
A Habr post (29 October 2026) by Sergey Blekher on rescuing a B2B SaaS module that several generations of agent fixes had turned into a tangle: each bug got another conditional, and agents copied the pattern. Rather than patch, he split the work between two agents. An analyst agent studied the old code, ran it and recorded scenario cards (inputs, expected results, states, codes, evidence). A builder agent wrote new code from the cards without ever seeing the old code, the way Phoenix Technologies cloned the IBM PC BIOS in 1984. A 15-rule charter and a small experiment show that a weak model follows design rules mainly when given a worked example.
Key numbers
- 2 of 3API calls in the inherited SaaS project that returned HTTP 500 before the rewrite
- 3 + 2 daystime for the analyst agent to write the transfer contract, then for the builder agent to write the new implementation
- 36 to 18design-rule violations on DeepSeek V4 Flash with no guidance versus example plus AGENTS.md plus requirements in the prompt
- 21places in the legacy code where a handler identified its own type by negation (not A and not B)
As reported by the source.
Russian·Web page·opens habr.com
What it reports
- Starting point: "almost done, release in two weeks", with 2 of 3 API calls returning HTTP 500. Claude hallucinated heavily on this code and could not answer half the questions.
- Method: analyst agent 3 days on the transfer contract; builder agent 2 days on the new implementation. Each card line is tagged as measured, from code, or a gap.
- Isolation of the builder: a ban in the task, a separate session, two linter rules (new code never calls old code and vice versa), and a borrowing guard that diffs code trees and shows only matches.
- Defects in working functions were carried over as-is and fixed in the clean code; unused functions were dropped. Whole user scenarios were moved, verified, switched over, and the old path deleted after acceptance.
- Second project: a Rust backend and five Python services whose contracts diverged while all six had green tests; moved into one Python monolith in three days; six repositories archived; mypy now catches contract drift.
- Charter of 15 rules (state stored in one declared field, one function changes state, timeouts for every wait, typed failure codes, no silent success, new tests must fail once on the defect, server checks permissions) plus an "antigoal" rule: after three failed fixes, stop and redesign.
- Experiment on DeepSeek V4 Flash, 36 runs, violations counted blind by Claude Sonnet: nothing 36, example 31, example plus AGENTS.md 25, example plus AGENTS.md plus prompt requirements 18.
- In a cancellation task, four rules in the prompt without an example produced the same guess-from-a-field fix as no rules; rules plus a state-machine example produced an explicit status enum, a transition table and one check.
How they got it
The author's own two projects, described with timings, plus a small controlled comparison scored by another model without knowledge of the condition.
Limits
- One author, two projects; timings are self-reported and the codebases are not public.
- The experiment is small; the post does not say how the 36 runs split across the four conditions.
- Violation counts come from an LLM judge.
- Labelled an opinion piece on Habr.
Why it matters for companies
Agent-written code that has been patched by agents accumulates its own style of debt, and further agents will copy it. The clean-room split (one agent documents behaviour, another builds from the documentation without seeing the old code) is a practical way out that also produces a behavioural spec. For teams using cheaper models, the result to note is that rule files alone do little; a single good example of the intended structure does most of the work.
Key passages in translation
Russian original
Агенты видели эти подходы перед собой и продолжали по образцу наматывать нейросопли на этот клубок.
The agents saw these approaches in front of them and kept winding more of the same onto the tangle.
Russian original
Если одну вещь не удалось починить за три попытки, остановись и пересмотри ее устройство. Четвертую заплатку не делай.
If you have failed to fix one thing in three attempts, stop and rethink how it is built. Do not make a fourth patch.
Russian original
Без образца та же модель повторила то, что видела в старом коде: пятую проверку по cancel_reason.
Without an example, the same model repeated what it saw in the old code: a fifth check on cancel_reason.
Summary based on the full text. Read the original for the full argument and data.Read the original on habr.com
- Tags
- Agents.md