# 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](https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/87bycvv0drlpbwxjppyi.png)](https://www.patreon.com/c/AheadKit)
