---
id: "lib-2026-10-29-clean-room-rewrite-of-agent-patched-legacy"
type: "library"
title: "Clean-room rewrite of agent-patched legacy"
title_original: "Чистый контур — агентская прачечная для грязного легаси"
lang: "RU"
org: "blurman (Habr)"
source_type: "Practitioner post"
date: "2026-10-29"
week: "2026-W44"
url: "https://nosov.ai/library/lib-2026-10-29-clean-room-rewrite-of-agent-patched-legacy/"
original_url: "https://habr.com/ru/articles/1101314/"
tier: "B"
tier_name: "Useful, limited"
tier_reason: "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_basis: "full-text"
original_minutes: "5"
tags: ["ai technology", "legacy rewrite", "agents.md", "worked examples"]
---

# Clean-room rewrite of agent-patched legacy

Original title: Чистый контур — агентская прачечная для грязного легаси

Tier 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.

## 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 3: API calls in the inherited SaaS project that returned HTTP 500 before the rewrite
- 3 + 2 days: time for the analyst agent to write the transfer contract, then for the builder agent to write the new implementation
- 36 to 18: design-rule violations on DeepSeek V4 Flash with no guidance versus example plus AGENTS.md plus requirements in the prompt
- 21: places in the legacy code where a handler identified its own type by negation (not A and not B)

## 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

> Агенты видели эти подходы перед собой и продолжали по образцу наматывать нейросопли на этот клубок.

The agents saw these approaches in front of them and kept winding more of the same onto the tangle.

> Если одну вещь не удалось починить за три попытки, остановись и пересмотри ее устройство. Четвертую заплатку не делай.

If you have failed to fix one thing in three attempts, stop and rethink how it is built. Do not make a fourth patch.

> Без образца та же модель повторила то, что видела в старом коде: пятую проверку по cancel_reason.

Without an example, the same model repeated what it saw in the old code: a fifth check on cancel_reason.

Original: https://habr.com/ru/articles/1101314/
