Autonomous Coding Agents: Building Reliable Self-Healing Workflows That Fix Bugs and Submit Test-Passing Pull Requests

    Autonomous coding agents move defect repair from manual investigation to an automated closed loop: understanding a problem, locating it, patching the code, verifying the result, and opening a pull request. This article explains, from an engineering perspective, how to build a reliable agent workflow without tying it to any specific model or product. It covers the task loop an agent should own, the real-world challenges of large repositories, and five key capabilities: context engineering and repository understanding, task planning and reasoning control, tool invocation and execution environments, root cause analysis, and high-quality patch generation. It also addresses the verification loop, merge request creation, security and governance, cost optimization, evaluation, and a phased rollout strategy. Readers will learn how to turn an agent from a demo tool into stable R&D infrastructure that frees engineers to focus on architecture, tradeoffs, and high-quality review.

    Introduction: From Code Completion to Autonomous Closed-Loop Repair

    In traditional software development, fixing defects usually depends on developers working manually: reading error logs, tracing call paths, understanding business context, modifying local code, running test cases, and finally submitting for review. As systems grow more complex—especially with microservices, distributed tasks, containerized deployments, and multi-cloud environments becoming common—the paths that trigger problems are increasingly hard to reproduce. A single error message is often just a symptom; the real root cause may be hidden in upper-layer calls, configuration changes, dependency versions, or the runtime environment.

    The value of an intelligent coding agent lies in turning “understand the problem—locate the problem—modify the code—verify the result—submit the change” from a manual process into an automatically executable closed loop. It does more than generate a snippet of plausible-looking code. It can read repository context, execute commands, observe failure feedback, and refine the fix across multiple rounds based on test results. A mature agent should be able to run code safely in an isolated environment, locate defects, produce a patch, and—once tests pass—output a well-formed pull request for human review.

    This article is not about a specific model or product. Instead, it discusses from an engineering practice perspective how to build a reliable automated repair workflow for coding agents, so that it can consistently support R&D productivity, defect governance, and quality gates.

    1. Target Scenarios: What Tasks the Agent Should Own

    A coding agent aimed at R&D automation typically covers the following task loop:

    1. Problem intake: Receive defect information from failing tests, production alerts, static scans, or human descriptions.
    2. Context building: Extract relevant information from the repository, commit history, dependency configuration, log files, and test cases.
    3. Reproduction: Run the failing case in an isolated environment to confirm whether the error occurs consistently.
    4. Root cause analysis: Analyze call chains, data flow, configuration items, and dependencies to form candidate hypotheses.
    5. Code repair: Generate a minimal patch and avoid unrelated changes.
    6. Regression verification: Run relevant unit tests, integration tests, static checks, and formatting checks.
    7. Change submission: Create a branch, open a merge request, and attach a description of the changes, test results, and a risk assessment.
    8. Human review: A maintainer reviews the change and decides whether to merge it into the main branch.

    So a coding agent is not a simple “code generator.” It is an execution unit that must work alongside the repository, test systems, permission systems, and review systems.

    2. Core Challenges: Real Repositories Are Far More Complex Than Sample Code

    With sample or demo code, an agent generating a fix may look straightforward. In real repositories, however, automated repair faces several challenges.

    2.1 Scattered Context

    A defect may span multiple modules: frontend pages, API services, message queues, database scripts, configuration centers, and deployment manifests. If the agent only sees a single file or function, it is hard to determine the root cause.

    2.2 Unstable Failure Information

    Some errors do not reproduce every time—for example, concurrency races, cache penetration, resource exhaustion, network jitter, timezone differences, and random seeds. The agent needs the ability to “observe—hypothesize—verify” rather than drawing conclusions from a single failure.

    2.3 Hard-to-Judge Blast Radius

    Fixing one place in the code may break other modules. For example, changing a return structure may affect frontend parsing, adjusting null handling may affect downstream analytics, and optimizing performance may change cache semantics. The agent needs to assess the compatibility of its changes.

    2.4 Complex Test Environment Dependencies

    Automated repair depends on the execution environment. The agent may need to install dependencies, start services, initialize a database, load test data, and even simulate external systems. Whether the environment builds reliably directly affects the repair success rate.

    2.5 Balancing Cost and Latency

    If the agent runs expensive reasoning at every step, it consumes substantial resources. In practice, tasks should be scheduled by complexity: simple problems handled quickly, complex problems given stronger reasoning and multi-round verification.

    3. Key Capability One: Context Engineering and Repository Understanding

    Whether a coding agent can reliably fix problems depends first on whether it can understand the repository.

    3.1 Multi-Level Context Retrieval

    The agent should not read only the file where the error appears. It should proactively retrieve related content, including:

    • Source files related to the error;
    • Shared modules being called;
    • Corresponding unit tests;
    • Recent commit records;
    • Configuration files and deployment scripts;
    • Error logs and monitoring summaries;
    • Dependency versions and interface documentation.

    Through retrieval, the agent can build a complete association among “problem—code—test—configuration.”

    3.2 Structured Repository Map

    For large repositories, the agent needs to understand the directory structure, module boundaries, and dependency directions. You can build a simplified repository map for it, for example:

    1. repository/
    2. ├── services/
    3. │ ├── user-service/
    4. │ ├── order-service/
    5. │ └── payment-service/
    6. ├── shared/
    7. │ ├── auth/
    8. │ ├── logging/
    9. │ └── database/
    10. ├── configs/
    11. ├── deploy/
    12. └── tests/

    When locating a problem, the agent can first determine whether the fault belongs to business logic, a shared component, configuration management, or the test environment.

    3.3 Leveraging Historical Information

    Many defects are not new problems but historical regressions. The agent can refer to past commit messages, defect-fix records, and review comments to understand how similar problems were handled.

    For example, if history shows that “null pointer exceptions are often caused by inconsistent third-party dependency versions,” the agent can prioritize checking dependency lock files, compatibility changes, and rollback records.

    4. Key Capability Two: Task Planning and Reasoning Control

    When handling defects, a coding agent usually needs multiple steps, so a clear planning mechanism must be designed.

    4.1 Task Decomposition

    The agent should break “fix the failing test” into subtasks:

    1. Read the failing case;
    2. Determine the failure type;
    3. Locate the relevant module;
    4. Propose a repair hypothesis;
    5. Modify the code;
    6. Run the tests;
    7. Continue adjusting based on the results.

    This decomposition prevents the agent from directly producing large, uncontrollable blocks of code.

    4.2 Plan–Execute–Feedback Loop

    A practical closed loop looks like this:

    1. while not all_tests_passed and attempt < max_attempts:
    2. analyze_failure
    3. generate_fix_hypothesis
    4. modify_code
    5. run_tests
    6. record_new_failures

    Here, max_attempts should be determined by task complexity, resource budget, and risk-control strategy.

    4.3 Matching Reasoning Strength to Task Difficulty

    In complex defect repair, the agent needs a more thorough thought process—for example, multi-round analysis, counterexample checks, and change-impact assessment. Simple formatting or low-risk small changes can start from a lightweight strategy.

    Engineering teams can adopt a tiered strategy:

    • Low-risk tasks: fast generation with automatic verification;
    • Medium-risk tasks: expanded test scope plus human spot review;
    • High-risk tasks: mandatory human review, with limits on the production resources the agent can access directly.

    4.4 Self-Verification Mechanism

    Before submitting a change, the agent should proactively answer several questions:

    • Does the current fix only address the surface symptom?
    • Is there a more sound root-cause fix?
    • Does it affect public interfaces?
    • Does it introduce new performance or security risks?
    • Is there corresponding test coverage for the fixed scenario?

    Self-verification significantly reduces the chance of “looks fixed, actually regressed.”

    5. Key Capability Three: Tool Invocation and Execution Environment

    A coding agent must be able to call tools in order to move from “advisor” to “executor.” Common tool capabilities include file read/write, command execution, test running, log viewing, and change submission.

    5.1 Sandboxed Execution Environment

    To prevent the agent from modifying the real production environment, provide it with an isolated sandbox. A sandbox typically includes:

    • An independent copy of the code;
    • A containerized runtime environment;
    • Simulated dependency services;
    • A test database instance;
    • Temporary credentials with least-privilege tokens;
    • Audit logs of operations.

    This lets the agent safely run builds, tests, and scripts without accidentally touching real systems.

    5.2 Least-Privilege Tool Permissions

    The agent should not have full repository-admin permissions by default. Follow the principle of least privilege:

    • Read-only browsing of the production repository;
    • Writes allowed only on designated development branches;
    • No direct merges into the main branch;
    • No access to unmasked production secrets;
    • No deletion of long-term retained data;
    • High-risk commands require approval.

    5.3 Parsing Execution Results

    The agent should not look only at whether a command exits successfully; it must parse the output. For example:

    1. FAILED test_user_service.py::test_login
    2. AssertionError: expected 200, got 401

    The agent needs to extract key information from the failure: the test case, the assertion mismatch, the request method, the return status, and the call stack. It then correlates this with code logic, authentication configuration, and dependency versions.

    6. Key Capability Four: From Failure Information to Root Cause

    The hardest part of automatic repair is not changing code but determining the root cause. The agent can use the following methods.

    6.1 Minimal Reproduction

    First, confirm whether the failure reproduces reliably. If it does not, collect more execution samples. The agent can run the test several times, observe the failure frequency, and record any random factors.

    6.2 Call-Chain Tracing

    For an interface failure, the agent needs to reconstruct the call chain:

    1. user request
    2. → gateway routing
    3. → authentication check
    4. → business service
    5. → data access layer
    6. → cache/database

    By inspecting layer by layer, it can determine whether the problem lies in parameter parsing, permission checks, transaction handling, serialization format, or resource access.

    6.3 Binary Search Localization

    If the problem scope is large, the agent can use a binary-search approach:

    • Compare recent commits;
    • Check configuration changes;
    • Roll back suspicious dependencies;
    • Switch runtime parameters;
    • Isolate the failing module.

    For example, the agent can first determine whether the issue was introduced by a new commit, then whether it was caused by a code change or a dependency change.

    6.4 Counterexample Verification

    After proposing a repair hypothesis, the agent should try to construct counterexamples. For example:

    • After the fix, is empty input still handled safely?
    • Under concurrent calls, does it remain idempotent?
    • With very large data volumes, does it still pass?
    • If a downstream service times out, will it trigger a cascading failure?

    Counterexample verification effectively avoids local patches that “only fix one test case.”

    7. Key Capability Five: High-Quality Patch Generation

    After locating the root cause, the patch the agent generates should meet engineering standards.

    7.1 Minimal-Change Principle

    The agent should prefer the minimal necessary change and avoid large-scale refactoring. For example, a null pointer issue should not lead to rewriting the entire module along the way.

    7.2 Preserving Interface Compatibility

    If a public interface is involved, assess compatibility:

    • Have parameter meanings changed?
    • Has the return structure changed?
    • Have error codes changed?
    • Do callers need to upgrade in sync?
    • Are there legacy clients or canary versions depending on it?

    7.3 Defensive Fixes

    For external input, the agent should strengthen validation rather than simply swallowing exceptions. For example:

    1. def resolve_user(user_id: str):
    2. if not user_id:
    3. raise ValueError("user_id must not be empty")
    4. user = fetch_user(user_id)
    5. if user is None:
    6. raise LookupError(f"user not found: {user_id}")
    7. return user

    This kind of fix not only resolves the current failure but also improves code robustness.

    7.4 Adding Tests

    The agent should not only change code but also add corresponding tests. Tests can cover:

    • Normal inputs;
    • Boundary inputs;
    • Exceptional inputs;
    • Concurrency scenarios;
    • Compatibility scenarios;
    • Regression scenarios.

    A fix without tests should have a lower auto-merge confidence.

    8. The Verification Loop: Let Tests Prove the Fix Works

    The core of automated repair is not that “the agent thinks it fixed the problem,” but that “tests prove the fix is effective.”

    8.1 Layered Verification

    At minimum, run the following types of checks:

    1. Syntax check: Confirm the code compiles or parses.
    2. Static analysis: Check types, conventions, potential null values, and unused variables.
    3. Unit tests: Verify local logic.
    4. Integration tests: Verify that modules work together.
    5. Regression tests: Confirm existing functionality is not broken.
    6. Security checks: Detect leaked secrets, dangerous commands, and dependency vulnerabilities.

    8.2 Re-Repair After Test Failures

    If tests fail, the agent should not repeat the same fix. It should adjust its strategy based on the new failure information. For example:

    • If an assertion fails, check the business rules;
    • If a connection times out, check the resource pool or timeout configuration;
    • If data is inconsistent, check transaction boundaries;
    • If compilation fails, check types and dependency interfaces.

    8.3 Recording Test Results

    Each verification should be retained for review. It can be recorded as a structured summary:

    1. repair_summary:
    2. issue: "user login returns 401 unexpectedly"
    3. root_cause: "missing normalization for email input"
    4. files_changed:
    5. - services/auth/user_service.py
    6. - tests/auth/test_user_service.py
    7. checks:
    8. unit_test: passed
    9. integration_test: passed
    10. static_check: passed
    11. risk_level: low
    12. reviewer_notes: "requires attention on input validation"

    Such a summary is far more useful for human review than simply submitting code.

    9. Submitting the Merge Request: From Code Change to Reviewable Deliverable

    Once the agent completes a fix and passes verification, it should produce a well-formed merge request rather than merging directly into the main branch.

    9.1 Branch Management

    Create a separate branch for the agent, for example:

    1. fix/auto-login-auth-error

    This avoids the agent operating directly on the main branch and makes human review and rollback easier.

    9.2 Merge Request Content

    The merge request submitted by the agent should include:

    • Problem background;
    • Root cause analysis;
    • List of changed files;
    • Explanation of key code changes;
    • Added or adjusted tests;
    • Verification results;
    • Potential impact;
    • Rollback plan;
    • Areas that need focused review.

    9.3 Writing for Human Reviewers

    The description the agent generates should avoid exaggeration and instead clearly state “what was done” and “why it was fixed this way.” For example:

    1. ## Change Description
    2. Fixes an authentication failure caused by an empty email value not being validated during user login.
    3. ## Root Cause
    4. The email field was not trimmed uniformly before lookup, so inputs with leading or trailing spaces failed to match.
    5. ## Changes
    6. - Added email normalization in the login flow.
    7. - Added unit tests for empty and space-padded inputs.
    8. ## Verification
    9. - Unit tests passed.
    10. - Integration tests for the auth module passed.
    11. - Static analysis passed.
    12. ## Risk
    13. Low risk. The change affects only input normalization and does not alter external interfaces.

    This description is closer to a high-quality engineering deliverable than a simple machine-generated patch.

    10. Security and Governance: Automated Agents Must Be Controllable and Auditable

    Once a coding agent holds execution permissions, security governance becomes especially important.

    10.1 Permission Boundaries

    The agent’s permissions should be strictly bound to the task scope:

    • Test environments may run automatically;
    • Production is read-only by default;
    • Changes may only enter the review queue;
    • Sensitive repositories require additional approval;
    • High-risk commands need human confirmation.

    10.2 Operation Auditing

    All agent actions should be logged, including:

    • Which files were accessed;
    • Which commands were executed;
    • Which code was modified;
    • Which tests were run;
    • Which external services were called;
    • Which changes were ultimately submitted.

    Auditing not only helps trace problems but also helps optimize the agent’s strategy.

    10.3 Managing Secrets and Sensitive Information

    The agent should not handle plaintext secrets. Recommended practices include:

    • Using temporary credentials;
    • Injecting secrets through environment management;
    • Masking logs;
    • Masking test data;
    • Prohibiting sensitive information from being written into patches.

    10.4 Rollback Mechanism

    The agent’s fixes may be wrong. Fast rollback support is therefore essential:

    • Keep snapshots of the original branch;
    • Record patch versions;
    • Support one-click undo;
    • Automatically generate an alternative fix when a merge request fails.

    11. Cost Optimization: Balancing Efficiency, Quality, and Resources

    Automated repair by a coding agent is not free. The larger the repository, the more tests, and the deeper the reasoning, the higher the resource consumption. Engineering-level cost control is therefore needed.

    11.1 Tiered Scheduling

    Choose different processing paths based on problem complexity:

    • Simple failures: automatic fix with fast verification;
    • Medium complexity: more context retrieval and multi-round testing;
    • High complexity: stronger reasoning strategies, human review, or staged execution.

    11.2 Context Compression

    Not all code needs to be fed into the agent’s context. Information can be compressed through summaries, symbol indexes, call-relationship graphs, and failure summaries.

    11.3 Cache Reuse

    Reusable results include:

    • Repository index results;
    • Common error-fix patterns;
    • Already-verified patches;
    • Test environment initialization results;
    • Dependency install caches.

    Reasonable caching reduces repeated execution.

    11.4 Learning from Failed Tasks

    After each failed repair, record the failure reason and build a knowledge base. Later similar problems can refer to it directly, reducing repeated trial and error.

    12. Evaluation System: How to Judge Whether an Agent Is Truly Usable

    To decide whether a coding agent fits a team, build your own evaluation set rather than judging only by demo results.

    12.1 Evaluation Set Composition

    Include:

    • Unit test failure cases;
    • Configuration error cases;
    • Dependency conflict cases;
    • Missing boundary-condition cases;
    • Concurrency and idempotency cases;
    • Historical defect regression cases;
    • High-risk change cases.

    12.2 Evaluation Metrics

    Track:

    • Repair success rate;
    • Average repair time;
    • Test pass rate;
    • Human rework rate;
    • Patch change size;
    • Regression introduction rate;
    • Security violation rate;
    • Per-task resource cost.

    12.3 Continuous Feedback

    Evaluation should not be a one-time effort. As the repository evolves, the evaluation set must be updated too. Real failure cases can be continuously added to the set, forming a closed-loop optimization process.

    13. Rollout Recommendations: Adopt Automated Repair in Phases

    For teams that want to adopt coding-agent repair capabilities, advance in stages.

    Phase One: Read-Only Analysis

    The agent only analyzes the problem and the repository and does not modify code. Its output includes root-cause hypotheses, impact scope, and repair suggestions, used to observe whether the agent’s judgment is reliable.

    Phase Two: Automated Repair in the Test Environment

    The agent may modify code and run tests in an isolated branch, but can only submit to an evaluation branch or demo branch.

    Phase Three: Automated Submission for Low and Medium Risk

    For low-risk defects, the agent can automatically generate merge requests, which humans review and merge.

    Phase Four: Tiered Automated Governance

    Set rules based on historical data: some problem types can be merged automatically, some must be human-approved, and some high-risk problems prohibit agent handling.

    14. A Typical Workflow

    The following is a simplified view of a coding-agent repair workflow:

    1. workflow:
    2. input:
    3. - failed_test
    4. - repository_branch
    5. - issue_description
    6. steps:
    7. - collect_context:
    8. - source_files
    9. - related_tests
    10. - recent_commits
    11. - configuration
    12. - reproduce:
    13. - run_failed_test
    14. - collect_logs
    15. - analyze:
    16. - identify_root_cause
    17. - evaluate_impact
    18. - patch:
    19. - modify_code
    20. - add_or_update_tests
    21. - verify:
    22. - run_unit_tests
    23. - run_integration_tests
    24. - run_static_checks
    25. - submit:
    26. - create_branch
    27. - generate_merge_request
    28. - attach_test_report
    29. fallback:
    30. - if tests_fail: iterate_analysis
    31. - if risk_high: request_human_review

    The key to this workflow is not the steps themselves but that each step should have a verifiable output. Only when every step can be observed, recorded, and evaluated does the agent have a solid foundation for entering the development process reliably.

    15. Conclusion: Making the Agent Part of the Engineering System

    When a coding agent autonomously investigates problems and submits test-passing merge requests, it essentially turns the “diagnose, fix, verify, deliver” stages of the development process into automatically executable engineering capabilities. It does not replace engineers; it frees them from repetitive investigation, low-value patches, and mechanical verification, so humans can focus on architecture design, complex tradeoffs, and high-quality review.

    To truly land this capability, teams must care not only about whether the agent can generate code, but also about whether it has reliable context understanding, tool execution, a test loop, security governance, and cost constraints. Only under clear permission boundaries, a solid evaluation system, and traceable auditing can a coding agent move from a demo tool to stable R&D infrastructure.