---
title: 'Before Asking AI to Fix a Bug, Make the Failure Reproducible'
description: >-
  Reproduce a stale-response search bug with controlled Promise completion,
  separate evidence from hypotheses, and guard results, errors, loading, and
  cleared input. Includes a repair prompt and a complete offline test exercise.
canonical_url: 'https://onevium.com/blog/ai-debugging-reproduce-before-fixing'
language: en
published_at: '2026-11-01'
publisher: Onevium
---
# Before Asking AI to Fix a Bug, Make the Failure Reproducible

Reproduce a stale-response search bug with controlled Promise completion, separate evidence from hypotheses, and guard results, errors, loading, and cleared input. Includes a repair prompt and a complete offline test exercise.

## Make the failing order repeatable

The search box says `cat`, but the list shows results for `c`. Refreshing fixes it. An AI assistant adds a debounce, yet fast typing still occasionally produces the wrong list.

The most useful starting material for this bug is a test that fails every time: start old request A, then new request B; finish B first and A second. The final list must belong to B, regardless of network speed. Once that order is encoded, “seems fixed” becomes something you can check.

This article follows that single search race. You will get an offline, single-file exercise, a debugging prompt to copy, and regression tests for results, errors, and loading. The rule is precise: only the current request may write `rows`, `error`, or `loading`, including cleanup inside `finally`.

The reference is a manually authored teaching example actually run with Node.js and fictional data. The prompt is for you to try; no separate live AI repair experiment was performed, and these results do not measure any model or product. No API key, paid service, or browser is needed.

## Separate observations from explanations

Imagine maintaining a search box where each input calls `search(query)`, which uses an injected `fetchRows(query)` function. Define the behavior first: every new input clears rows and errors; nonempty input sets loading; clearing input sends no request and invalidates earlier work. Only the empty string counts as empty here. Whitespace normalization, caching, and retaining previous results are separate policies.

| Order | Observable event     | Expected state                    |
| ----- | -------------------- | --------------------------------- |
| 1     | Input `c`, start A   | Empty rows, loading true          |
| 2     | Input `cat`, start B | Wait for B; A is obsolete         |
| 3     | B returns `["cat"]`  | Show cat, no error, loading false |
| 4     | A returns `["c"]`    | Preserve the state from step 3    |

“The list switches back to c at step 4” is evidence. “The old request does not check whether it still owns the state” is a hypothesis to verify in code. Debounce timing, server caching, or repeated component mounts might be investigation leads, but this table alone does not establish any of them as the cause.

Keep two in-flight requests and three observable fields in the reproduction; remove the UI framework, real network, and database. If the reduced version no longer reproduces the original deviation, recover the missing condition. Do not quietly substitute a different failure just to make a test red. In a real project, first confirm that the requests and writes actually occur in this order.

![Request A starts before B but finishes last. Before the fix, stale c results overwrite cat; after the fix, only the current request may write.](https://onevium.com/assets/blog/developer-workflows/search-race-en.webp)

The test controls completion order. The same sequence must preserve the latest request's state after the fix.

## Control Promise completion instead of elapsed time

[Open the full-size diagram to read its labels](https://onevium.com/assets/blog/developer-workflows/search-race-en.webp).

Using `setTimeout` to make the old request take 200ms and the new one take 20ms still relies on timing. What matters is which operation completes first, not how many milliseconds the machine waits.

The single file below uses `deferred()` to retain each Promise's `resolve` and `reject`. The test completes B, then A. After each completion it awaits the Promise returned by the corresponding `search()` call before inspecting state. Resolving a Promise and immediately asserting can run the assertion before the async function resumes.

`oldDone` and `newDone` wait for their respective handlers to finish. Because `search()` catches request errors, even a simulated failure lets these Promises fulfill normally. Check `state.error` to establish correct error handling; “did not throw” is insufficient. Awaiting an old call also does not grant it permission to update current state.

This is a deterministic application regression test. Our [trace review article](https://onevium.com/blog/ai-evals-trace-review) explains how task records inform AI evaluation; this exercise uses ordinary assertions for one frontend failure whose expected behavior is already clear.

## A repair prompt you can copy

Save the complete file in the next section, then give it and this prompt to your existing coding assistant. You can also compare the two implementations manually and run exactly the same checks.

```text
Fix this fast-search race. Do not change tests or add dependencies first.

Inputs and contract:
- Read search.mjs; the repair target is createBrokenSearch.
- A=search("c"), B=search("cat"); start A→B, complete B→A.
- Only B's rows may remain; only the current request may write rows/error/loading.
- Every input clears rows/error. Empty input sends no request and invalidates old work.
- A late old failure must not set error; an old completion must not end B's loading.
- If B fails, show B's error, not A's late successful results.

First report observations, cause hypotheses, and a minimal discriminating reproduction.
Keep deferred-controlled completion order and all assertions; do not use timer sleeps.
Run SEARCH_IMPL=broken node --test search.mjs first and preserve failure evidence.
Then modify only createBrokenSearch and prove the fix with the same command.
Finally run node --test search.mjs to check the reference still passes.
Explain ownership at every asynchronous state write, including finally.
Report actual commands, exit codes, and remaining boundaries. If you cannot run it,
say explicitly that it was not run.
```

Providing both evidence and a bounded edit target lets a reviewer assess whether the assistant repaired the original failure. The reference implementation is already visible, making this a guided exercise rather than a blind model benchmark. To document an actual AI run, retain the model used, input-file version, patch, and command output separately. Do not count the manually authored reference results below as AI performance.

## Copy one complete offline exercise

Use Node.js 20 or later; no dependency installation is required. Save this entire block as `search.mjs` in a new directory. It contains the broken starting point, the reference implementation, and the same tests for either. Both language editions provide identical code.

The default command tests the reference:

```sh
node --test search.mjs
```

Expect all 10 tests to pass, exit code 0. To see the failure first, explicitly select broken mode with this macOS/Linux command. In PowerShell, set `$env:SEARCH_IMPL="broken"`, run the tests, then use `Remove-Item Env:SEARCH_IMPL` so later default runs do not retain the selection.

```sh
SEARCH_IMPL=broken node --test search.mjs
```

The original broken implementation should fail 8 tests and pass 2, exit code 1. That failure is intentional only when selecting the starting implementation; the default run passes. After repairing `createBrokenSearch`, make the explicit broken-mode command pass too. Use `SEARCH_IMPL=reference` to explicitly select the reference.

```js
import assert from "node:assert/strict";
import test from "node:test";

// Teaching policy: clear rows/error on every input; empty input sends no request.
export function createBrokenSearch(fetchRows) {
  const state = { rows: [], error: null, loading: false };
  return { state, async search(query) {
    Object.assign(state, { rows: [], error: null, loading: query !== "" });
    if (query === "") return;
    try {
      state.rows = await fetchRows(query);
    } catch (error) {
      state.error = error.message;
    } finally {
      state.loading = false;
    }
  } };
}

export function createReferenceSearch(fetchRows) {
  const state = { rows: [], error: null, loading: false };
  let generation = 0;
  return { state, async search(query) {
    const mine = ++generation; // Empty input also invalidates previous work.
    Object.assign(state, { rows: [], error: null, loading: query !== "" });
    if (query === "") return;
    try {
      const rows = await fetchRows(query);
      if (mine === generation) state.rows = rows;
    } catch (error) {
      if (mine === generation) state.error = error.message;
    } finally {
      if (mine === generation) state.loading = false;
    }
  } };
}

function deferred() {
  let resolve, reject;
  const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
  return { promise, resolve, reject };
}

const mode = process.env.SEARCH_IMPL ?? "reference";
if (!["reference", "broken"].includes(mode)) throw new Error("Invalid SEARCH_IMPL");
const createSearch = mode === "broken" ? createBrokenSearch : createReferenceSearch;
function setup() {
  const requests = [];
  const app = createSearch(query => {
    const request = { query, ...deferred() };
    requests.push(request);
    return request.promise;
  });
  return { ...app, requests };
}
const empty = { rows: [], error: null, loading: false };

test("new success, then old success: keep new rows", async () => {
  const app = setup();
  const oldDone = app.search("c");
  const newDone = app.search("cat");
  app.requests[1].resolve(["cat"]);
  await newDone;
  assert.deepEqual(app.state, { ...empty, rows: ["cat"] });
  app.requests[0].resolve(["c"]);
  await oldDone;
  assert.deepEqual(app.state, { ...empty, rows: ["cat"] });
});

test("old success while new pending: keep loading and empty rows", async () => {
  const app = setup();
  const oldDone = app.search("c");
  const newDone = app.search("cat");
  app.requests[0].resolve(["c"]);
  await oldDone;
  const during = { ...app.state };
  app.requests[1].resolve(["cat"]);
  await newDone;
  assert.deepEqual(during, { ...empty, loading: true });
  assert.deepEqual(app.state, { ...empty, rows: ["cat"] });
});

test("old failure while new pending: no stale error or early loading reset", async () => {
  const app = setup();
  const oldDone = app.search("c");
  const newDone = app.search("cat");
  app.requests[0].reject(new Error("old failure"));
  await oldDone;
  const during = { ...app.state };
  app.requests[1].resolve(["cat"]);
  await newDone;
  assert.deepEqual(during, { ...empty, loading: true });
  assert.deepEqual(app.state, { ...empty, rows: ["cat"] });
});

test("new success, then old failure: keep successful state", async () => {
  const app = setup();
  const oldDone = app.search("c");
  const newDone = app.search("cat");
  app.requests[1].resolve(["cat"]);
  await newDone;
  app.requests[0].reject(new Error("old failure"));
  await oldDone;
  assert.deepEqual(app.state, { ...empty, rows: ["cat"] });
});

test("new failure, then old success: preserve current error", async () => {
  const app = setup();
  const oldDone = app.search("c");
  const newDone = app.search("cat");
  app.requests[1].reject(new Error("new failure"));
  await newDone;
  assert.deepEqual(app.state, { ...empty, error: "new failure" });
  app.requests[0].resolve(["c"]);
  await oldDone;
  assert.deepEqual(app.state, { ...empty, error: "new failure" });
});

for (const outcome of ["resolve", "reject"]) {
  test(`clear input invalidates old ${outcome}`, async () => {
    const app = setup();
    const oldDone = app.search("c");
    await app.search("");
    assert.equal(app.requests.length, 1);
    assert.deepEqual(app.state, empty);
    if (outcome === "resolve") app.requests[0].resolve(["c"]);
    else app.requests[0].reject(new Error("old failure"));
    await oldDone;
    assert.deepEqual(app.state, empty);
  });
}

test("same query twice still creates distinct generations", async () => {
  const app = setup();
  const oldDone = app.search("cat");
  const newDone = app.search("cat");
  app.requests[1].resolve(["fresh"]);
  await newDone;
  app.requests[0].resolve(["stale"]);
  await oldDone;
  assert.deepEqual(app.state, { ...empty, rows: ["fresh"] });
});

test("normal success waits for completion and next search resets state", async () => {
  const app = setup();
  const firstDone = app.search("c");
  assert.deepEqual(app.state, { ...empty, loading: true });
  app.requests[0].resolve(["c"]);
  await firstDone;
  assert.deepEqual(app.state, { ...empty, rows: ["c"] });
  const nextDone = app.search("cat");
  assert.deepEqual(app.state, { ...empty, loading: true });
  app.requests[1].reject(new Error("new failure"));
  await nextDone;
  assert.deepEqual(app.state, { ...empty, error: "new failure" });
  await app.search("");
  assert.deepEqual(app.state, empty);
});

test("synchronous fetch error is handled", async () => {
  const app = createSearch(() => { throw new Error("sync failure"); });
  await app.search("cat");
  assert.deepEqual(app.state, { ...empty, error: "sync failure" });
});
```

## Guard ownership in finally, too

The reference increments `generation` for each input and captures the call's own value in `mine`. When asynchronous work finishes, it may write only if `mine === generation`. Comparing query text is insufficient: two consecutive searches for `cat` are distinct requests, and the older cat result can still be stale.

Increment before the empty-input early return. Otherwise `search("")` clears the list once, only for an old completion to populate it again. The synchronous reset at the start belongs to the latest input; success, failure, and cleanup each independently recheck ownership later.

Guarding only `rows` leaves two bugs. When A finishes while B is pending, an unconditional A `finally` sets loading false too early. When A fails after B succeeds, an unconditional A `catch` attaches an obsolete error to B's results. A screenshot showing cat at the end misses the intermediate loading failure.

| Test branch                            | Required behavior                                      |
| -------------------------------------- | ------------------------------------------------------ |
| B succeeds, then A succeeds            | Preserve B's rows                                      |
| A succeeds or fails while B is pending | Empty rows, null error, loading true                   |
| B succeeds, then A fails               | No obsolete error from A                               |
| B fails, then A succeeds               | Preserve B's error, empty rows, loading false          |
| Clear input, then A succeeds or fails  | Keep idle state; send no extra request                 |
| Search the same text twice             | Decide ownership by call generation, not text equality |

The exercise also checks normal success, state reset on the next call, current failure, and a synchronous request-function throw. These checks prevent a repair that simply rejects every write: refusing to update for the latest request breaks the normal path.

## Cancellation saves work; generations control writes

If your real transport supports `AbortController`, canceling old work on new input can reduce unnecessary activity. Cancellation does not automatically settle every state-ownership question. The old call may have finished the network read and started later parsing, or an injected request function may ignore the signal entirely. Cancellation errors can still run through error handling and cleanup.

This exercise deliberately leaves old requests running so each Promise can succeed or fail, proving that obsolete completions cannot write. If you add cancellation, retain the generation guards and test old calls that still finish after cancellation. Aborting does not guarantee that a server stops executing; a generation marker does not stop obsolete work from consuming resources.

The policy here applies to rows/error/loading owned by the latest input in one search controller. Retaining previous results, normalizing whitespace, merging pagination, deduplicating requests, or handling independent parallel regions requires an explicit ownership policy for each field. One global generation is not a universal rule for every business request.

This exercise has no UI and does not validate component unmounting, subscription cleanup, framework state commits, or effect lifecycles. A component that must forbid writes after unmount needs invalidation during cleanup or a disposal mechanism, plus relevant integration tests. Testing cleared input does not establish coverage of every unmount scenario.

## Bring specific evidence back to the component

The exercise was actually run with Node.js v23.11.1: reference mode passed 10/10 tests with exit code 0; the original broken mode failed 8 and passed 2 with exit code 1. These are fixed-order state-ownership checks of manually authored teaching code. There was no browser acceptance run or separate AI repair score.

When reconnecting the real component, establish that UI input reaches this controller and that the transport adapter routes success and failure through the same branches. Observe all three fields while B is waiting and after A arrives late. If the behavior still differs, inspect actual writes for duplicate controllers, additional updates, and lifecycle effects. A passing Node test alone does not establish that the entire page is fixed.

A useful review handoff can be small: the order that fails on the old implementation, the patch, the same assertions after the patch, and the boundaries not yet verified in integration. That gives an AI's suggested fix a concrete question to answer: can an obsolete request still overwrite the current search?
