Most developers approach AI coding agents through iterative prompting: describe a task, inspect the diff, manually correct mistakes in chat, and repeat.
Coming from an Industrial Automation / PLC (Programmable Logic Controller) background, I approached this experiment differently. I gave the orchestrator agent three plain-text directives:
- “Decouple this 1,375-LOC monolithic poker engine (
game.py).” - “Keep core operations intact. Do not break legacy logic, and do not touch existing tests.”
- “Issue the commands yourself and direct the execution worker to refactor.”
The initial instructions were short. The engineering work was not.
The result was a decoupled poker engine whose game.py facade shrank from 1,375 to 394 lines, a 71.3% reduction, while remaining below the enforced 400-LOC limit.
The automated verification suite reported that 1,210 native unit test cases across 16 test files passed. A 500-hand simulation using a fixed random seed (seed(42)) reported zero observed behavioral divergence across 53,905 compared internal state data points.
Based on local filesystem creation and modification timestamps, the core decoupling phase took 49 minutes, from 15:19 to 16:08. No application logic was manually written by a human. Total release packaging and adversarial verification-gate hardening took approximately three hours.
The important question is not whether three sentences can magically refactor a legacy system. It is how a short task description, an execution agent, and an independently enforced verification process can work together—and what evidence is needed to trust the result.
1. Translating Human Intent into Rigid Boundaries
Brief instructions alone do not guarantee safe refactoring. Without independently enforced acceptance criteria, an agent may introduce unintended changes or weaken tests in an attempt to satisfy the task.
The verification pipeline was designed to detect these failure modes rather than rely solely on the agent’s own assurances.
-
Structural Boundary: A static AST rule required
game.pyto remain below 400 LOC while disallowing mutable default arguments and bareexcept:blocks. -
Anti-Tampering Interlock: All 16 test files, covering 1,210 test cases, were fingerprinted using SHA-256 before code generation. Unauthorized byte-level changes under
tests/were intended to trigger a non-zero exit code and reject the delivery. -
Differential State Oracle: Unit tests alone cannot guarantee continuity across every multi-way betting sequence. A 500-hand baseline was therefore used to compare sequential state transitions, including chip counts, pots, and action indices.
-
Public API Stability: The verification process checked that the 37 identified public methods and properties on
TexasHoldEmremained available through an in-situ facade delegating to internal domain modules.
The principle is straightforward: define acceptance criteria before execution, then verify the resulting artifacts independently of the agent’s claims.
2. How the Refactoring Was Executed
The orchestrator did not draft the implementation through interactive code suggestions in a conversation window. It analyzed the syntax tree of game.py, reported as containing 5,466 AST nodes, and directed the execution worker to extract four domain modules:
-
side_pot_math.py— 360 LOC
Pot splitting, all-in calculations, and chip allocation. -
round_scheduler.py— 215 LOC
Blind handling, betting rounds, and street transitions. -
action_validator.py— 185 LOC
Raise validation and legal-action checks, including the project’s WSOP Rule 96 logic. -
game_serializer.py— 140 LOC
PGN history replay parsing and game-state cloning.
The original game.py was then restructured into a 394-LOC facade delegating to these specialized modules.
The objective was not simply to reduce the line count. It was to separate responsibilities while preserving the existing behavior within the scope of the verification performed.
3. Adversarial Verification: Testing the Safety Relay
In physical automation, an emergency-stop relay is meaningful only if it responds correctly when a fault occurs.
The same principle applies to AI coding agents. A verification gate should not merely report success on a clean build; it should also reject deliberately corrupted or incomplete inputs.
The verification harness was tested against eight intentional failure scenarios:
| Failure scenario | Expected result |
|---|---|
| Test assertion modification causing a SHA-256 mismatch | REJECTED — Exit Code 1 |
| Missing fingerprint catalog | REJECTED — Exit Code 1 |
| Facade exceeding the 400-LOC boundary | REJECTED — Exit Code 1 |
Introduction of a bare except: block |
REJECTED — Exit Code 1 |
| One-chip discrepancy introduced into pot calculation | REJECTED — Exit Code 1 |
| Missing golden baseline dataset | REJECTED — Exit Code 1 |
| Breaking public API method signatures | REJECTED — Exit Code 1 |
| Unchecked subprocess failures | REJECTED — Exit Code 1 |
All eight scenarios were reported as rejected with non-zero exit codes.
On a clean release build running locally on Windows 11 and Python 3.10, the automated verification runner completed its validation gates in approximately 0.7 seconds.
That runtime refers to the verification runner, not the total time required to develop, refactor, or initially execute the full test and simulation workload.
These results demonstrate how explicit failure conditions can be built into the delivery process. They do not establish that every possible failure mode has been covered.
4. Can This Approach Be Replicated on Other Systems?
Potentially, yes—but not through basic prompt chat alone.
Applying this workflow to legacy systems requires at least three prerequisites.
-
Objective Ground Truth
The target system needs existing tests, a trusted executable baseline, or another measurable reference against which behavior can be compared. Without one, the agent may produce a plausible implementation without sufficient evidence that it preserves the original behavior.
-
Closed-Loop Execution
The agent must operate in an environment where it can execute commands, inspect concrete results and exit codes, and encounter enforced failure conditions. Generating code and describing what should happen is not equivalent to executing and verifying it.
-
A Control-System Mindset
The human defines the safety boundaries and acceptance criteria. The agent performs the implementation work within those boundaries. Verification determines whether the result is acceptable.
This is the distinction I wanted to explore from an industrial automation perspective: the agent performs the work, but its own assertion of success is not the acceptance criterion.
5. Scope and Limitations
This experiment does not establish that the resulting architecture is universally bug-free, nor does it claim that this approach is a silver bullet for every legacy codebase.
The evidence supports a narrower conclusion: bounded behavioral equivalence within the tested scope.
The reported results include 1,210 native unit test cases and 53,905 compared internal state data points from a 500-hand simulation, with no observed divergence in those comparisons.
That is useful evidence, but it is not a mathematical proof of equivalence across every possible input, execution path, or future change.
The 49-minute figure also describes the core decoupling phase based on local filesystem timestamps. It should not be interpreted as the total time required to design the constraints, harden the verification gates, package the release, and prepare the experiment for publication.
The broader lesson is that autonomous code generation becomes more credible when its output is evaluated against predefined, independently checked conditions—not simply when the code compiles or the agent reports success.
Code and Side-by-Side Comparison
The original implementation, refactored code, extracted modules, and release artifacts are available for inspection.
-
Original monolith —
game.py(1,375 LOC):
texasholdem/texasholdem/game/game.py at main · SirRender00/texasholdem · GitHub -
Refactored facade —
game.py(394 LOC):
texasholdem-decoupled/texasholdem/game/game.py at main · zxmn999/texasholdem-decoupled · GitHub -
Extracted domain modules:
texasholdem-decoupled/texasholdem/game at main · zxmn999/texasholdem-decoupled · GitHub -
Verified release —
v1.0.0-verified:
Release v1.0.0-verified: TexasHoldEm Monolith Decoupling (394 LOC, 0-Diff Verified) · zxmn999/texasholdem-decoupled · GitHub
Reproducing the Verification Gate
Clone the repository and run the release verification script:
git clone https://github.com/zxmn999/texasholdem-decoupled.git
cd texasholdem-decoupled
python verify_release.py
Review the repository and release instructions for any additional environment or dependency requirements.
I welcome fellow engineers to inspect the diffs, reproduce the verification process, and share how they approach deterministic boundaries when deploying autonomous coding agents on legacy codebases.