Make AI-built search handle late responses
Give a coding agent a reproducible search test: reverse the response order, keep the current query authoritative, and verify the result in the browser.

A search box can look finished while showing the wrong answer. Someone searches for motion, then changes the query to type. The second reply arrives first and renders correctly. A moment later, the older reply replaces it. The input still says type; the results now describe motion.
When a coding agent builds this feature, give it an observable finish line: after every response has settled, the results must belong to the current query. A screenshot helps you inspect the mismatch, but a controlled sequence is what makes the bug repeatable. Here is a small original study you can use as a review pattern.
Make the failure visible before fixing it
Actual Chromium capture of our original fictional Fieldnotes study. The older motion response arrived last. The input and the displayed result disagree; no customer data or production latency is involved.The study contains two invented library entries and makes ordinary fetch requests to a local search route. Its browser harness supplies synthetic responses. It holds the motion request, accepts a newer type request, releases type, then releases motion. In the unguarded implementation, each reply is allowed to render, so the old one takes over.
This is a test of response ownership, not a simulation of realistic network speed. There are no latency numbers to optimize. You are asking a narrower question: can obsolete work still change the screen? That question also applies to read-only filters, autocomplete and detail panels that fetch data when selection changes.
Give the agent a reproducible scenario
Name the invariant
The input, result heading and result items must describe the same current query.
Control the order
Start motion, then type. Finish type first. Finish motion afterward, without changing the input again.
Wait for the old work to settle
Observe completion of the late response handler before checking the final UI. Otherwise the assertion can pass before the bug happens.
Keep the expectation fixed
The result remains Type that stays readable. Correct the implementation without relaxing that requirement.
Let the newest intent own the screen
Original code-native sequence diagram. The four steps show request and response order, not elapsed time. A response can finish successfully and still be too old to use.Assign a monotonically increasing number whenever the query changes. A request remembers its number. After parsing the response, compare that number with the latest one before rendering. Apply the same check to errors: an obsolete failure must not replace a newer success with a retry message. Clearing the field must advance the number too, even though it sends no request.
let latest = 0;
async function search(query) {
const mine = ++latest;
clearResults();
if (!query) return showIdle();
showLoading(query);
try {
const response = await fetch(
`/search?q=${encodeURIComponent(query)}`
);
if (!response.ok) throw new Error("request_failed");
const data = await response.json();
if (mine !== latest) return;
showResults(data.items, query);
} catch {
if (mine !== latest) return;
showRetry(query);
}
}
This excerpt isolates the ownership rule; the show functions stand for the study’s DOM rendering. The complete demo keeps the input mounted and clears old results while a new search is pending. Those are deliberate interface choices. If you retain previous results during loading instead, label which query they belong to so they cannot be mistaken for the new answer.
The HTTP check is separate from request ordering. MDN’s Fetch guide explains that a response such as HTTP 404 does not itself reject the fetch promise. Inspect response.ok before treating the body as a successful result. In our controlled failure case, HTTP 503 produces a retry state rather than a false empty result.
Actual capture after adding the request-number check. Both replies have settled; the current type result remains. The browser test also confirms that focus stays in the search field.Separate request scheduling from permission to render
Three decisions in an asynchronous search
Debounce
When should a request start?
- Wait for a pause in input if that suits the interaction.
- Requests already in flight can still finish out of order.
Cancel
Is older work still useful?
- An AbortController can cancel a fetch and response consumption.
- Treat cancellation as expected when a query is superseded.
Check ownership
May this result change the UI?
- Only the current request commits results or errors.
- Clearing the input invalidates pending work as well.
AbortController is useful when an earlier fetch is no longer needed. Our demo deliberately leaves both requests running so the ownership check can be tested on a real late reply. Adding cancellation later should preserve that rule. Do not carry this read-only pattern over to payments or other writes and assume that abandoning a browser request reverses an operation on the server.
Ask for evidence the agent can reproduce
Playwright’s network APIs let a test intercept requests and fulfill them with controlled responses. Register the route before interacting, wait until each intended request has been intercepted, then release the replies in the chosen order. This removes the need to guess how many milliseconds a fast computer or slow CI runner requires.
Use auto-retrying assertions for the visible result, input value and focus. Also establish that the late handler has finished before the final assertion. Our study exposes a small completion log for that purpose. Without this synchronization, checking that type is visible immediately after its response can miss the overwrite that happens next.
The acceptance matrix used for this original study| Scenario | Expected visible result | Observed |
|---|
| Old success arrives last | Current type result remains. | Passed in guarded mode at 1120px and 390px. |
| Old request fails last | New success stays visible; no stale retry. | Passed at 390px. |
| Clear input while pending | Idle prompt remains after the old reply. | Passed at 390px. |
| Current query has no matches | Explicit empty state, with no result card. | Passed at 390px. |
| Current request fails, then retry succeeds | Retry is reachable by keyboard; success replaces the error. | Passed at 390px. |
The current Playwright Test Agents documentation describes separate planning, generation and repair roles. It also notes that a repair run can leave a test skipped when it believes functionality is broken. Review the scenario and assertions yourself: a skipped race-condition test is unresolved evidence, not a working search. This study used a directly authored harness; it does not compare agent tools or measure their reliability.
We ran the original study in Chromium 153 at 1120px and 390px. The broken mode reproduced the mismatch at both widths; the guarded mode retained the current result. Four additional narrow-screen state checks passed. These are controlled functional checks, not a performance benchmark, real-backend validation or a complete accessibility audit. Keep a separate integration check against your actual search service.
Cover: original AI-generated conceptual illustration of messages taking different paths to a gate. All interface images are real captures of the authored study; the sequence diagram was drawn in code.
Back to the LOOKFILE blog