AI Engineering
Give Coding Agents the Same Bug Twice: A JSON Replay Walkthrough
Replay initial state and commands instead of repeating a bug description. Run a duplicate-reservation failure, see why invariants miss it, and verify the corrected behavior.
When every attempted fix starts from a different state
A reservation button was pressed twice and the counts went wrong. A screenshot explains what happened, but the next attempt still needs to recreate it. A file containing initial state and ordered inputs gives both a developer and a coding agent the same failure to run again.
In Your Game Is A Harness, published October 10, 2026, Kyle Kukshtel discusses restoring game state and replaying serializable commands. This article applies that idea to an original Sambro inventory simulator. It does not copy his implementation or require a game engine. Original article: Your Game Is A Harness — Kyle Kukshtel
We executed the failure and corrected paths below in Node.js. We did not call a separate AI product or measure improvements in agent productivity or accuracy. What we verified is a development interface that reproduces and detects a deliberately introduced bug.
Three commands reproduce a duplicate reservation
Begin with five units in stock and none reserved. Request r1 reserves two units. A client that did not receive the response repeats the same r1, then request u1 releases one unit. Define the rule first: an identical request with the same ID takes effect once. Expected (stock, reserved) pairs are therefore (3, 2), (3, 2), and (4, 1).
retry.json contains initial state, ordered commands, and an expected state after each command. Those expected values come from the business rule, not from blindly recording the implementation's output. The runner stops at the first different state rather than leaving you to explain a wrong final total.
Save the executable, fixture, and tests as replay.mjs, retry.json, and replay.test.mjs in the same empty directory. Run them with Node.js 22 or later; no external packages or API keys are needed. Executable: agent replay — replay.mjs
Fixture: agent replay — retry.json
Automated tests: agent replay — replay.test.mjs
node replay.mjs retry.json --buggy
# exit 1: command 2, expected stock=3/reserved=2, actual stock=1/reserved=4
node replay.mjs retry.json
# exit 0: 3 commands, final stock=4/reserved=1
node --test replay.test.mjs
# 8 tests pass Why the invariants pass while the answer is wrong
The educational --buggy mode deliberately skips one duplicate guard. The first reservation succeeds. Applying r1 again leaves one unit in stock and four reserved. At command two, the runner reports EXPECTED_STATE and exits with code 1.
Neither count is negative, and their sum is still five. Checking only nonnegative quantities and conserved inventory would miss the bug. Invariants protect general boundaries; expected intermediate states check this scenario's business rule. Use both.
Repeated output alone also cannot establish correctness: a broken program can produce the same wrong answer every time. The example's SHA-256 digest is a comparison marker under the same runtime and serialization rules, not a correctness proof or security attestation.
Fix the meaning of an already-seen request ID
The normal path checks whether a previously seen ID carries the same operation and quantity. An identical retry returns the existing state. A reused ID with different contents fails with CONFLICTING_ID. A new ID is a separate request, and an unavailable quantity fails with INSUFFICIENT_QUANTITY.
step() returns a new state rather than mutating its input. Separating this small function from the screen, network, and database makes the failure inspectable as inputs, transitions, and expectations. The normal run completes all three commands with stock=4, reserved=1, and exit code 0.
The seen-request history is part of resumable application state too. This example starts every replay from the beginning, so it initializes seen as empty. It does not implement mid-run checkpoint restoration or concurrent database requests. A production system still needs atomic persistence, concurrency handling, and an ID-retention policy.
const signature = JSON.stringify([type, quantity]);
if (Object.hasOwn(state.seen, id)) {
if (state.seen[id] !== signature) throw Error('CONFLICTING_ID');
if (!buggy) return state;
} Give the agent a reproduction command and an independent oracle
Pass a coding agent the reproduction command, failing step, allowed edit scope, and regression checks. Changing expected values to match a broken implementation only hides the failure. This example ships both the normal implementation and an intentional fault. The handoff below is therefore a review task: preserve the educational --buggy branch and the tests that demonstrate it. For a real repair, use a separate working copy with fixed expectations and edit the affected product code.
The task below is a suggested handoff. We did not use it to measure another AI product's repair success rate. Providing a reproducible interface is different from demonstrating that a model fixes more bugs.
Reproduce: node replay.mjs retry.json --buggy
Expected failure: command 2 / EXPECTED_STATE
Inspect how the normal step() path prevents this failure.
Keep the educational --buggy mode and retry.json unchanged.
Run: node replay.mjs retry.json
Run: node --test replay.test.mjs
Verify CONFLICTING_ID and INSUFFICIENT_QUANTITY errors.
Report exit codes and state diffs; do not edit code for this review.
Do not claim the UI or a real database was tested. Start small and keep the untested boundaries visible
This example uses sequential integer operations and reads no clock, randomness, or external API. If those influence your application, make timestamps, random inputs, or external responses reproducible inputs too. A fixed seed alone does not control thread scheduling or network responses.
Extract one troublesome state transition rather than redesigning the whole application. Record harmless synthetic sequences for double clicks, retries, and cancellation, then retain failures as regression fixtures. Real logs can contain personal information or tokens; do not publish them as sample data.
Our automated checks cover the normal and intentionally broken paths, repeatability, input immutability, invalid quantities, conflicting IDs, and insufficient inventory. They do not cover rendering, accessibility, real database transactions, or asynchronous races. UI changes still need browser verification.
The useful artifact is a rule you can execute, not a longer bug description. With a fixed starting point and an independent expected result, each new change can be compared against the same behavior.