Sub topic 11
Failure modes: invented APIs, behavior drift, over-broad edits
This chapter assumes you can read a Git diff, run a test suite and a linter, and have used a coding agent before. It covers three failure modes that show up often when an agent changes existing code, and the guardrail that catches each one.
Invented APIs
An agent writes code from patterns it has learned, and it only sees the parts of your project it has read. Sometimes it calls a function, method, or option that does not exist. It may use a method from a newer or older version of a library, or guess a helper name in your own code.
Typical signs:
- An
AttributeError,ImportError, or "is not a function" error at run time. - A type checker error such as "module has no attribute".
- An import of a package that is not in your dependency file.
Guardrails:
- Run the type checker and tests after every step. Dynamic languages only reveal a missing name when the line runs, so tests must exercise the changed code.
- Tell the agent the exact library version, and point it to the docs or source in your repository.
- Ask it to cite where each unfamiliar API is defined. If it cannot, treat the call as suspect.
Behavior drift
A refactoring must keep observable behavior the same. Drift is a change in behavior that nobody asked for. It is usually small: a rounding rule, an error message, the order of results, or a default value.
The example below looks like a harmless cleanup. It is not.
# Before
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = round(subtotal * 0.2, 2)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}
# After: the agent "simplified" the tax step
def calculate_tax(subtotal):
return subtotal * 0.2
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = calculate_tax(subtotal)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}The round(..., 2) call was dropped. A subtotal of 10.05 now gives a tax of about 2.01 with extra decimal digits instead of a value rounded to two places. The code still runs, and the diff looks tidy. Only a test that checks the exact output will fail.
Guardrails:
- Write characterization tests before the change, and run them after each step.
- State in the task that the return values and the public signature must not change.
- Read the diff for lines that were removed, not only lines that were added.
Over-broad edits
An agent may touch more than you asked for. It may reformat a whole file, rename unrelated variables, update tests so they pass, or edit files outside the task. These changes hide the real change and make review slow.
The most serious case is a test edited to match new behavior. A green test run then proves nothing.
Guardrails:
- List the files in scope in the task description.
- Commit after each verified step, so the diff you review is small.
- Check
git diff --statfirst. A file you did not expect is a warning. - Treat any change to an existing test as needing an explicit reason.
git diff --stat
git diff -- tests/How the guardrails fit together
Choosing a response
When a check fails, discard the step with git restore . and repeat it with a tighter prompt. This is usually faster than asking the agent to patch its own mistake, because the patch adds more changes to review.
Exercise
An agent extracts a function and all tests pass. The diff shows that two existing assertions in tests/test_pricing.py were changed. Which failure mode is likely, and what do you do?
Show answer
This is likely behavior drift hidden by an over-broad edit. The agent changed the tests to match new behavior. Restore the original tests with git restore, rerun them against the agent's code, and fix the code until the original assertions pass.
Exercise
Name the check that would catch each of these: (a) a call to a method that the library does not have, (b) a changed rounding rule, (c) a reformatted unrelated file.
Show answer
(a) The type checker or a test that runs the call. (b) A characterization test with exact expected values. (c) Reviewing git diff --stat and the full diff.