Skip to main content

The Steps Ran in Order. Generation Still Read the Wrong Document.

Updated 6 min readMarkdown
The Steps Ran in Order. Generation Still Read the Wrong Document.

raju_dandigam left a comment on my retrieval piece that stuck with me. The order check I had in place only proves the steps ran in sequence. It cannot tell me whether generation actually used what retrieval returned. His suggestion was plain: record the retrieval result's ID and the ID actually handed to generation, then compare the two in an application assertion. If the IDs ever disagree, something swapped the result between the two steps, no matter how tidy the timestamps look.

I built the check into a small retrieval setup and ran it. The result is the kind of thing that makes you distrust every green ordering check you have ever shipped.

The setup is a keyword retriever over six support documents for a small shop: refund policy, shipping times, warranty, return labels, privacy, order records. Two versions of the refund policy sit in the set on purpose, a superseded one promising 30 days and the current one promising 14. Generation is a consumer that quotes the document it is handed. Nothing here calls a model. The retriever picks by keyword overlap, ties break to the newest version, and generation quotes whatever document it receives. That is the whole system, and it is enough to show the failure.

The failure needs one more piece. In the version of the flow I tested first, generation does not receive the retrieval result directly. It reads a shared "current document" slot, and retrieval writes that slot. That shape is everywhere in real code: a module-level variable, a context object, a cache entry that the next step trusts. It works fine until anything else writes to the same slot between the two steps.

So I ran eight fixed questions through three versions of the flow. First, the clean version: retrieval writes the slot, generation reads it, nothing else touches it. Eight questions, zero mismatches. The ID assertion passes on all eight, and so does the timing check. Nothing surprising.

Then the version with the swap. On four of the eight questions, a second retrieval for an unrelated question lands between retrieval and generation and overwrites the shared slot. Retrieval still runs before generation on every single question. The timestamps are in the right order. The timing check passes on all eight.

The ID assertion fires on four of the eight. Half the answers came from the wrong document.

The worst one is the kind of mistake a user would catch and a log would not. The question was "how many days can I get a refund". Retrieval returned the current refund policy, 14 days. Generation quoted the shipping document instead: 3–5 business days. A refund question answered with shipping times, delivered by a flow whose steps ran in exactly the right order. Two of the other mismatches did the same thing in the other direction: a return-labels question and another refund question both got the shipping document. The fourth handed a warranty question the superseded refund policy. Every one of them passed the timing check.

That is the point raju_dandigam was making, and the numbers make it hard to argue with. The timing check was silent on 4 out of 4 mismatches. The ID assertion caught 4 out of 4. Ordering proves sequence. It proves nothing about consumption, because consumption is a question about which object the next step held, and a timestamp cannot answer that.

The fix is the part that surprised me most, because it is small. In the third version I kept the same four swaps in place. The same second retrieval landed between the steps, the same shared slot got overwritten, and I changed one thing: generation consumes the retrieval result it was handed instead of reading the slot. Eight questions, zero mismatches. The swaps still happened. They just stopped mattering, because nothing generation quoted could be overwritten after retrieval returned it.

There is a temptation to read that as "the slot was the bug, delete the slot", and that is mostly right. But the deeper lesson is about where the check lives. The timing check lives outside the flow, watching timestamps. It can only ever say the steps ran in order, and order was never the thing that broke. The ID assertion lives inside the flow, on the actual objects passed between steps. It compares the ID retrieval returned with the ID generation consumed, and it fires the moment those two disagree, whatever the cause: a second retrieval, a stale cache entry, a variable reused for the next question before the last one finished.

That last one is worth naming, because it is the shape I expect to see most in real code. Two questions in flight at once, one slot, and the second question's retrieval lands a fraction of a second before the first question's generation. No error anywhere. The first answer quotes the second question's document, and the timing check signs off on all of it.

I am keeping the assertion. It is two IDs and one comparison, and it caught the only failure that mattered. The timing check stays too. It catches a different failure, steps running out of order, but I no longer trust it to say anything about what generation read. If you run a retrieval setup of your own, ask it one question: does the step that writes the answer hold the same ID retrieval returned? If you cannot answer that from inside the flow, you are trusting the timestamps. And the timestamps just told me four wrong answers were fine.

What has broken for you in this shape — the right order, the wrong document? I am curious how often it shows up outside my little test set.


P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam

Buy me a coffee

"An order check only proves the steps ran in sequence" is a useful thing to have written down, and comparing the retrieval ID with the ID generation actually received is such a cheap assertion that it is surprising it is not standard.

At Ascendra Ventures we built a research assistant for a global nonprofit that tests a theory of change against published evidence before committing money. It fans a query out across sources such as Google Scholar and the World Bank, builds an editable evidence library, and drafts an impact pathway with documents tagged to each branch. Keeping each document attached to the branch it supports is what lets the researcher stay in charge: they prune subtopics, add documents, fire follow up queries and regenerate with RAG, and they can always see which source sits behind which part of the answer.

The superseded 30 day refund policy is a perfect test document. Has the ID comparison caught anything in a real system yet, or only in the setup you built for it?

Only in the setup so far, honest answer — the 4 out of 4 was my little six-document rig, not a real system. Your research assistant sounds like the shape where it'd actually matter though, documents tagged to branches is basically the ID check done by hand. Do the branches ever share a slot like that, or does each one carry its own documents from the start?

They solve different halves of the problem. An immutable bundle prevents in-place overwrites between steps, so concurrent requests or shared references cannot silently mutate the document while generation is waiting to run.

The ID check is still worth keeping as a boundary assertion even with immutable objects, because it catches wiring mistakes—like a retry loop or response cache pairing the wrong immutable bundle with the current request ID. Prevention and verification work best together.

Prevention and verification together is the honest answer — I was treating them as either-or and they're not. The response cache pairing the wrong bundle is the concrete one, that's a wiring mistake no freeze can see. Have you actually watched a cache do that, or is it the failure you're guarding against?

Having four out of eight queries silently answer with the wrong document while every timestamp assertion stays green is a great demonstration of why ordering logs are not provenance. In concurrent web workers, module-level caches or request-scoped globals repurposed across async hops create this exact overwrite hazard.

Passing an immutable retrieval bundle by value directly into generation removes the shared slot completely, and comparing the retrieved document ID against the consumed ID at the boundary turns any accidental mutation into a hard test failure rather than a plausible-looking wrong answer.

The ID comparison is already in the piece, it caught 4 out of 4 while the timing check stayed green on all of them — so that half of your fix is proven. The by-value bundle is the part I haven't tried, my third version just hands generation the object instead of the slot, and the swaps stop mattering. Going to run the by-value version against a mutation between the steps and see if it buys anything over holding the object. In your setup, is the bundle doing the work, or is the ID check?

More from this blog