Articles

When a Search Result Points Nowhere

A highlight offset that lands 50,000 characters from its own phrase, why eleven green acceptance tests missed it, and the one-line check that closes the class.

A full-text search endpoint returns three things for every hit: a score, a highlighted phrase, and an offset into the document. The first two are what you look at. The third is what you build on — it is how you page a long document without loading it whole.

That third number is also the one nobody verifies.

The bug

We returned a passage for a document. The highlight quoted a sentence from roughly two-thirds of the way in. The offset said 9,410 characters. The phrase actually sat at 60,313.

Both numbers came from the same response. Neither the client nor the server complained. The passage looked like a passage, the highlight looked like a highlight, and every test we had was green.

reported 9,410
actual 60,313

50,903 characters apart

Both numbers came from one document. The bars share a scale, so the gap between the marks is the gap in reality.

Why it was green

The acceptance suite for that fix had eleven tests. Six exercised the anchoring behaviour directly and all six passed. That is not a paradox — it is the signature of an instrument that cannot fail the way reality fails.

The defect lived in one decoding step. The search backend HTML-escapes apostrophes as '. Our decoder handled ' and '. So the longest candidate phrase — the one most likely to be unique, and therefore the one the anchoring logic preferred — failed an indexOf test and was silently dropped from the candidate list. The code then anchored on the next candidate that did match: a short phrase near the head of the document.

Nothing threw. Nothing logged. A candidate that fails to match is, from the code's point of view, indistinguishable from a candidate that was never needed.

The six tests passed because none of them contained an apostrophe in a position that mattered.

The one-line check

The fix was a decoder table. The real change was admitting that the offset had never been verified against anything.

The check that closes the class is a single substring test:

passage.includes(highlightPhrase)  // must be true

If the returned window does not contain the phrase the highlight quotes, the window is wrong. That is not a heuristic — it is a statement about what the offset is supposed to mean.

We now consider the offset deserving of the same suspicion the highlight never needed. A highlight is a claim about text. An offset is a claim about position, and positions are the kind of number that arrives formatted correctly and is therefore never questioned.

What generalises

Three things, and none of them are about this endpoint.

A green suite can be evidence about the tests rather than the code. Six passing assertions about anchoring told us the anchoring logic ran. They said nothing about whether it produced the right answer, because they were built from the same assumptions as the implementation.

An escape sequence is a correctness boundary that fails quietly. Any function that decodes one representation into another has a table, and the table has holes. A miss does not raise — it produces a plausible different result. Where a decode failure can only mean "this candidate is unusable", log it, or at minimum count it.

Verification belongs where the meaning is. The property we actually wanted was this passage contains that phrase. Stating it that way makes the test obvious. Stating it as "the offset is within the document" makes it trivially true and worthless — which is exactly what we had.


This is the first post here. It is written from our own incident record rather than from memory, which is a discipline worth naming: a bug you remember is a bug you have already started to tidy.