Reflexion coding agents use failed tests to guide the next code change - not to retrain the model. The agent records what went wrong, keeps 1–3 short lessons and uses them to plan a small, scoped retry.
The process starts with a saved baseline and an isolated workspace. Each change must stay within set permissions, pass targeted and regression checks, and go through authorised review. The prototype described here allows two repair attempts: if checks remain blocked, results conflict or the budget runs out, the agent stops and sends the work for human review.
::: @figure
{Reflexion Coding Agents: The Bounded Repair Loop}
:::
1. Set Up an Isolated Workspace and Test Baseline
Define the Task and Change Limits
Before granting edit access, define a task contract: permitted paths, approved imports, out-of-scope changes and when human approval is required. Enforce these limits through policy checks and tool permissions. A tight scope keeps each failure tied to one change. [2]
Set the retry budget upfront. The Bounded Reflexion Agent prototype allows two repair attempts. [2] Once the scope is fixed, isolate the run so the first failure provides a reliable test signal.
Isolate Execution and Record Baseline Failures
Preserve the starting revision. Run the task in a disposable Docker sandbox with no network access, strict resource limits and no production secrets. Keep the filesystem outside the work area read-only, including evaluator tests. [2]
Before editing, run the agreed checks and save the baseline failure output in a separate log that the agent cannot overwrite. Use this log as the fixed reference for each reflection, revision and regression check.
If baseline runs conflict, stop and escalate to a human reviewer. Reflexion cannot work with unreliable test signals, and an unreliable result is not evidence of a passing baseline.
2. Use Test Failures to Guide Code Revisions
Record Test Results and Write a Reflection Note
Once you’ve logged the baseline, let the first failure guide the next attempt. For a usage-limit defect, check the specification, usage-check function and boundary tests before editing. Focus the change on one boundary check. [1][4][5]
Record the failing test, expected result, actual result and relevant trace. Separate facts from diagnosis. Use the template below, and include a rejected hypothesis only when the evidence rules it out. [4][5]
Observation: [Failing test, expected and actual result, relevant trace.]
Likely cause: [Explanation supported by the inspected code and failure.]
Rejected hypothesis: [Only when supported by evidence.]
Next change: [One permitted revision, described without code.]
Validation: [Boundary tests, then agreed regression checks.]
Make One Small Revision per Attempt
If the failed run still rejects equality and inspection confirms a strict comparison against the limit, revise that comparison to permit equality, not the surrounding billing logic. Keep the revision scoped to the boundary check only, and enforce file, diff and scope limits outside the model. Retest below, at and above the limit: pass the first two cases and reject the third. [2][4][5]
Keep just 1–3 short, task-specific lessons and remove duplicates. These lessons change the agent’s memory and context, not its model parameters. [2][4][5]
Build the Retry Loop and Set Stop Conditions
Feed each reflection into the next attempt. Gate every action, including the first edit. Stop when repair, time or execution budgets run out; failures or lessons repeat unchanged; evidence conflicts or is incomplete; or the work reaches a scope boundary. A passing run goes to review, not automatic acceptance. [1][2][4]
Use this controller-gated loop to keep each retry within scope.
guarded(action):
controller checks prerequisites, evidence, permissions and budgets
controller stops if any check fails
perform action
guarded(inspect specification, code and tests)
guarded(apply scoped initial implementation)
repeat:
evidence = guarded(run focused boundary tests)
if focused tests pass:
evidence += guarded(run agreed regression checks)
guarded(record evidence against baseline)
if all required checks pass:
stop and send patch for review
if evidence conflicts, is incomplete, or repeats unchanged:
stop and escalate
if repair budget is exhausted:
stop and escalate
note = guarded(reflect on evidence)
if lesson repeats:
stop and escalate
guarded(retain up to 3 concise lessons)
patch = guarded(propose one small revision)
guarded(validate patch against file, diff and scope limits)
guarded(apply permitted patch; count one repair attempt)
3. Verify the Patch and Review the Risk
Run Targeted Tests and Regression Checks
Once the scoped patch is in place, verification determines whether it is safe to proceed. After each retry, test the latest patch against the baseline and agreed regression scope.
Start with the failing test. Then run related unit tests, the regression suite and static analysis. Compare the results with the baseline, reporting passed, failed and skipped checks separately. Investigate every new failure. Verification is incomplete if required checks are skipped or cannot run. Keep independent, read-only tests outside the agent’s control. [2][3]
Repeat the final checks in CI and, where possible, a production-like environment. Account for differences in runtime, dependencies, permissions and configuration. Passing in a sandbox alone does not show that the patch is ready for deployment. [1][3]
If the checks pass, compare the diff with the evidence before choosing the next step.
Choose Whether to Accept, Retry, Revert or Escalate
Check the patch’s scope and behaviour. High-impact changes still need human approval, even when checks pass. Reflection proposes the next attempt; verification determines whether that attempt holds up.
Base the decision on both the diff and the evidence.
| Test evidence | Diff risk | Required action |
|---|---|---|
| All required checks pass; no new failures | Low; confined to approved scope | Submit for authorised review |
| Failures remain, but evidence shows improvement | Within the agreed scope and budget | Allow another bounded attempt |
| New regressions or no improvement | Unintended effects or growing diff | Revert the attempt; retain its evidence |
| Required checks blocked or authority missing | Unknown or high impact | Stop and escalate to a human |
Require authorised review before accepting the patch. Deployment follows a separate release process.
Keep an Audit Trail and Measure Results
Treat failures as process evidence, not just test noise. Retain test outputs, reflection notes, patches, and approval or rollback decisions. Redact sensitive data, restrict access and apply retention limits. [6]
Track whether the patch fixes the issue or merely passes by removing, skipping or weakening assertions. Use those cases to tighten the evidence requirements and controls. The patch must meet the evidence, scope and review requirements before it can move forward. [6]
How to Create a Self Healing Code Agent
Conclusion: Accept Only Scoped, Tested and Reviewed Changes
Accept a patch only if it stays within scope: it changes only permitted files and imports, passes the required hidden targeted tests run in a read-only environment, and passes review. Escalate if baseline failures remain unresolved or required checks are unavailable. [2]
When the retry limit is reached, stop without accepting the patch and keep the evidence for human review. [2][3]
FAQs
How can I tell a faulty test from faulty code?
Use independent verification: coding agents can struggle to spot flaws in their own tests [1]. Faulty tests may reject correct code (false negatives), leading to unnecessary changes that could cause harm. Flaky tests or those with too little coverage may accept incorrect code (false positives) [1].
Hokstad Consulting recommends strict governance, with acceptance criteria stored in read-only files that the agent cannot change. This helps prevent drift [2][3][4].
How do I prevent misleading reflection notes?
Treat each reflection as a hypothesis, not a fact [1]. Check the agent’s behaviour and deliverables against a reference run rather than a fixed answer key [1]. Use unit tests to spot errors, but bear in mind that the agent may mistake faulty tests for problems with the code [2].
When guidance conflicts, check version dates and policy status, remove outdated notes from use, and flag contradictions for human review before active retrieval [1].
How can I measure whether reflection improves repairs?
Compare your coding agent with a reference run, not a fixed answer key. Track task success rates, regressions, cost in £ per successful task and total duration. Include every attempt and account for memory, retrieval and reflection costs.
Treat reflections as hypotheses, not facts. Reinforce lessons backed by metrics, review conflicting guidance and cap trials when progress stalls. Use held-out tests the agent never sees to check that gains reflect better performance, not gaming of the evaluator.