# How to build a coding agent that fixes a failing test

Reproduce a shipping-fee bug in a sandbox, give the agent the failing assertion, and return a reviewable diff after the boundary tests pass.

Canonical URL: https://getfloo.com/blog/build-a-coding-agent-that-fixes-failing-tests

## TL;DR

An order for exactly $50 qualifies for free shipping. The checkout still adds $5.99. Give a coding agent a way to reproduce that single failure and verify the change it proposes.

- Keep the original source and the three test cases on the host.
- Send the model the observed failure and let it propose a source edit.
- Run the check again and return a diff for review.

## Put the boundary in the test

The fixture uses integer cents. Shipping is 599 cents below the threshold and zero from 5,000 cents up. The bug is easy to miss in a demo because orders on either side already work.

```python
def shipping_cents(subtotal):
    return 0 if subtotal > 5000 else 599
```

Test 4,999, 5,000, and 5,001 cents. The middle case distinguishes “at least” from “more than.” The case immediately below it prevents a superficially successful fix that makes shipping free for everyone. [Repair agent source](https://getfloo.com/examples/sandbox-agents/repair_agent.py) · [Host helper](https://getfloo.com/examples/sandbox-agents/agent_tools.py)

## Run the repair

Use Python 3.10 or later, a signed-in Claude Code CLI, and a floo account with sandbox credits. Sign in with the floo CLI or set FLOO_API_KEY in your shell. Run these commands in an empty directory.

```bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install floo-sandbox==0.2.0
export FLOO_API_KEY="${FLOO_API_KEY:-$(floo auth token)}"
base=https://getfloo.com/examples/sandbox-agents
curl -fsSLO "$base/agent_tools.py"
curl -fsSLO "$base/repair_agent.py"
python repair_agent.py
```

The script writes `shipping.py` and `check.py` into one sandbox. It runs the original code first. The host requires a failure with the expected three values before it asks the model to edit anything.

```text
Reproduced: AssertionError: (5000, 0, 599)
```

Read the tuple as input, expected fee, and actual fee. A missing interpreter or a broken import produces a different failure. Sending that to a model as evidence of a shipping bug would send it after the wrong problem.

## Give the agent one file to change

The host sends Claude the pricing rule, original function, test, and captured failure. Claude returns replacement Python. The host writes the candidate to the workspace and invokes the same check with bytecode caching disabled. That detail matters for tiny edits made within the same second: the next test should load the bytes the agent just wrote.

If a proposal fails, the second attempt sees the candidate and its error. Two failed proposals end the run. On a pass the host computes a unified diff against the original source. Here's the change from our run:

```diff
-    return 0 if subtotal > 5000 else 599
+    return 0 if subtotal >= 5000 else 599
```

The check printed `3 cases passed`. A reviewer can connect that one character to a business rule, three inputs, and an observed failure. Nothing in this example grants the model repository write access.

## Adapt it to an actual bug

Pick one failing test in your project and copy the smallest source tree and dependency lockfile that reproduce it. Record the exact command and expected failure. Replace the toy fixture only after that command fails for the same reason in the sandbox and in your normal test environment.

The tutorial checks one behavior. Before accepting a patch to your project, apply the diff to a clean checkout and run your usual suite in CI. A model proposal and its sandbox output are inputs to the review. The independent checkout is what verifies the patch you would merge. [Sandbox execution guide](https://getfloo.com/docs/guides/sandboxes)
