Autonomous coding agents face a fundamental structural limitation when operating in open-loop, single-turn, or ungrounded generative modes: without dynamic feedback from runtime execution, large language models generate syntactically plausible code that frequently fails subtle interface contracts, breaks existing edge cases, or introduces silent regressions. While early benchmark evaluations relied heavily on zero-shot or few-shot code completion, modern production coding architectures such as SWE-agent, Claude Code, and e-Otter++ increasingly rely on closed-loop, Test-Driven Development (TDD) harnesses.
Adapting classical TDD to autonomous systems requires a specialized execution pipeline. Rather than generating a patch immediately from an issue description, an agentic TDD framework structures the software repair process into explicit phases: synthesizing an isolated reproduction test, verifying a baseline failure, applying incremental edits, evaluating compiler and test execution feedback, and running full regression suites.

The Agentic Red-Green-Refactor Lifecycle
Traditional software engineering defines TDD through the Red-Green-Refactor cycle. For autonomous AI systems, this cycle is mapped into five distinct agentic execution phases:
1. Issue Ingestion and Reproduction Test Synthesis (Red)
When presented with a bug report or feature request, the agent does not touch production source files. Instead, it inspects the problem statement, locates the relevant submodules using structural repository search tools (such as Language Server Protocol symbols or AST grep), and writes a standalone reproduction test case.
The goal of this phase is establishing a verifiable Fail-to-Pass (F2P) condition:
- The reproduction test must execute against the unmodified codebase and fail specifically due to the described defect.
- If the reproduction test passes immediately, the agent knows the test does not capture the reported bug, preventing false-positive development trajectories.
- If the reproduction test fails due to environment configuration issues, missing imports, or runtime crashes unrelated to the bug, the agent must iterate on the test fixture before attempting a code fix.
2. Localization and AST Grounding
Once a failing reproduction test is validated, the agent uses repository map tools and call-graph traversal to locate the precise class, method, or file responsible for the behavior. Constraining code search to the code paths exercised by the reproduction test significantly narrows the active context window, reducing token usage and hallucination risks.
3. Incremental Patch Application
The agent edits the targeted source files using targeted find-and-replace patches or ephemeral file system operations. Because the reproduction test is already in place, the agent can make speculative, localized modifications without needing to reason about global application state in a single step.
4. Sandbox Execution and Dynamic Error Ingestion (Green)
The agent executes the reproduction test inside an isolated, containerized environment (such as Docker or rootless OverlayFS). The test harness captures standard output, standard error, exit codes, and structured test runner reports (such as pytest or JUnit XML).
If the test fails, the error output is fed directly back into the agent context:
- Stack traces identify the exact file line and assertion failure.
- Type checker diagnostics (e.g., mypy, tsc) surface interface mismatches.
- The agent updates its hypothesis and modifies the patch iteratively until the reproduction test exits cleanly with a status code of zero.
5. Regression Suite Execution (Refactor & Verify)
Achieving a passing reproduction test is necessary but insufficient. A naive fix might satisfy the reproduction test by hardcoding a return value or breaking dependent modules. In this phase, the agent executes the broader existing test suite to verify Pass-to-Pass (P2P) stability, ensuring no collateral regressions were introduced.
The Fail-to-Pass vs. Pass-to-Pass Dilemma
Empirical evaluations on software engineering benchmarks illustrate why separating test synthesis from patch generation is essential.
Research on SWE-bench and SWE-bench Verified highlights that bug fixes are evaluated along two dimensions:
- Fail-to-Pass (F2P) tests: Tests that fail on the original commit but pass after applying the resolution patch.
- Pass-to-Pass (P2P) tests: The existing suite of tests that passed before the change and must continue passing afterward.
Studies using the SWT-Bench benchmark demonstrate that generating high-quality reproduction unit tests is a distinct and complementary capability to patch generation. An analysis of code agents found that while systems like SWE-agent achieve strong code repair results, their issue reproduction accuracy on complex open-source repositories varies widely.
Furthermore, researchers developing e-Otter++ showed that integrating execution feedback directly into the test generation loop substantially improves reproduction precision. When an agent receives real runtime traces showing why a generated test failed to compile or failed for the wrong reason, it can correct its test fixtures before passing them to the code generation phase.
Recent work on TDAD (Test-Driven Agentic Development) points out an important pitfall: simply prompting an LLM to follow TDD without structured impact analysis or execution tooling can increase regression rates. Smaller models prompted with ungrounded TDD instructions often overfit to the immediate bug description at the expense of wider codebase integrity. Combining TDD with graph-based impact analysis reduced test regressions by 70% in benchmark evaluations.
Execution Feedback Engineering in Production
Feeding raw terminal output into an LLM context window quickly exhausts token budgets and introduces noise. Production agent harnesses apply several optimization strategies to clean and structure test output:
- ANSI Escape Code Stripping: Raw terminal output contains control sequences for colorization, cursor movement, and progress indicators that inflate token counts without conveying semantic meaning.
- Traceback Extraction and Frame Filtering: Long test runs may produce thousands of lines of output. Harnesses isolate the failure summary and root-cause stack frames while collapsing boilerplate framework internals (such as internal pytest runner wrappers).
- Structured Assertion Diffs: For large object comparisons or complex JSON outputs, diff truncation limits output while preserving the mismatch boundaries.
- Circuit Breakers for Infinite Loops: Agents that enter repetitive error loops (e.g., repeatedly alternating between two incompatible type annotations) trigger heuristic stopping conditions or force the agent to revert to an earlier git commit.
Serving Economics and Performance Trade-Offs
Implementing agentic TDD introduces explicit operational trade-offs between task completion rates, latency, and inference spend:
- SWE-bench Verified Pass Rate: Zero-shot or single-turn patching typically resolves 15% to 25% of tasks, whereas closed-loop agentic TDD workflows achieve 45% to over 70% resolution.
- Inference Token Consumption: Single-turn completion consumes 5,000 to 15,000 tokens per issue, while interactive TDD loops consume 50,000 to over 300,000 tokens across iterative test runs.
- Interaction Turn Count: Single-turn generation completes in 1 to 2 turns, compared to 8 to 25 or more interactive tool-use turns in an agentic TDD session.
- Wall-Clock Latency: Direct generation returns in 5 to 20 seconds, whereas running containerized test suites and compiling patches takes 2 to 10 minutes per task.
- Regression Resistance: Zero-shot approaches have low regression resistance due to the absence of verification, while closed-loop TDD provides strong resistance through automated regression runs.
While single-turn generation remains suitable for trivial autocomplete and localized refactoring, production engineering tasks requiring multi-file modifications and complex dependency alignment require closed-loop execution. By constraining code modifications to verifiable Red-to-Green transitions backed by automated regression testing, agentic TDD provides the deterministic verification boundary necessary for reliable autonomous software engineering.
Sources
- SWE-bench: Can Language Models Resolve Real-World GitHub Issues?
- SWE-bench Verified
- SWT-Bench: Testing and Validating Real-World Bug-Fixes with Code Agents
- Execution-Feedback Driven Test Generation from SWE Issues (e-Otter++)
- SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering
- TDAD: Test-Driven Agentic Development – Reducing Code Regressions in AI Coding Agents



