---
title: "Delivery order is not send order: read back the stream"
date: 2026-11-04
summary: "Queueing a chat's messages in the order their deliveries arrived still split one request across three agents when the deliveries came last first. Each message now reads back its stream and holds a place for the ones sent before it that haven't arrived yet."
tags: ["ordering", "events", "queues", "agents"]
url: https://blog.misherate.com/posts/delivery-order-is-not-send-order-read-back-the-stream
thumbnail: https://blog.misherate.com/thumbnails/delivery-order-is-not-send-order-read-back-the-stream-mwfgi8eh.jpg
---

Misha's [iterate](https://iterate.com) mind is built on event streams, and the platform delivers each event at least once, in any order.

Send "Start a shopping list", "Milk", "Eggs x6", two seconds apart, and the eggs can arrive first.

The mind decides, for every message in a chat, [whether it belongs to a conversation already under way](/posts/which-conversation-is-this-ask-it-as-a-multiple-choice) or starts a new agent. People send in bursts: a request, then a detail, then another. If each of those is decided alone, each gets its own agent, and the second one asks a question the first message already answered.

So the day before, a chat's messages started going through a queue: decided one at a time, so a later message can see the agent an earlier one started and join it. The queue kept them in the order their deliveries reached the mind.

That is not the order they were sent.

## What broke

Right after that change was deployed, three test messages sent two seconds apart reached the mind about ten seconds late, last first. The messages were on the event stream in the right order; only their deliveries were shuffled. The queue faithfully decided the last one first, with nothing to join, and each message got its own agent. The one that was only a detail asked what it was a detail of.

The platform delivers events at least once and in any order. A queue keyed by arrival only serialises; it doesn't order.

## Reading back

The stream itself knows the order. So when a message takes its place in its chat's queue, it reads back the stream for that chat's messages just before it (within 20 seconds, at most 50 events back). For each one that no delivery has been through yet, it holds a place ahead of its own. When those deliveries arrive, they fill the places that were held for them.

```ts
// simplified from the mind's book
reserveDecision(chat: string, input: string, earlier: string[], now: number) {
  const handled = this.handled(chat);
  const queue = this.decisions(chat);
  for (const key of earlier) {
    if (handled.includes(key) || this.routed(key)) continue; // already through, or decided
    queue.reserve(key, now); // an empty place, filled by that message's own delivery
  }
  queue.reserve(input, now);
}
```

The point of reading back, rather than waiting a while for stragglers, is that it costs nothing when there is nothing to wait for. A message only waits behind messages that are already on the stream. A lone message is no slower than before.

With an invented burst:

```
sent:       "Start a shopping list", "Milk", "Eggs x6"
delivered:  eggs, milk, list

before:  eggs → new agent, milk → new agent, list → new agent
after:   list → new agent, then milk and eggs join it, in the order sent
```

## Places that nothing fills

A held place is a promise that a message is coming as an input. Not every message is one: some are prompts for an agent in a sandbox, replies to a sandbox session, or the bot being claimed. Those deliveries never put anything in the queue, and a place held for them would block the chat.

So every message delivery, whatever path it took, ends by marking its message handled. That gives up any empty place held for it, and lets whatever was waiting behind it go on:

```ts
messageHandled(chat: string, input: string) {
  this.kv.put(`handled:${chat}`, [...this.handled(chat).filter((k) => k !== input), input].slice(-50));
  this.decisions(chat).release(input); // drops the place only if it never got a value
}
```

`release` is new on the durable queue, distinct from `remove`: it drops a place with no value in it, and leaves alone another worker's lease on a place that has one. A delivery that dies before it gets there leaves the place for its 20-second reservation, as an input's own place already did.

One detail came up while building it. Skipping only handled messages was not enough: a message could be decided by another delivery's loop before its own delivery had finished, and then a later message held a second place for it and waited for a retry six seconds later. The read-back now also skips anything already routed.

## What reading back can't fix

Telegram can save a message after one sent later, when two of its [webhook calls](https://core.telegram.org/bots/api#setwebhook) overlap. Then the stream itself is out of order, and no amount of reading back helps. Telegram's message ids go up within a chat, though, so a message can see that one sent after it got onto the stream first. When that happens, its words say so, for the agent that reads them:

```
(sent before "Milk", but arrived after it) Start a shopping list
```

The note is worked out from the stream before the message, which never changes, so a redelivery writes the same words. The agent gets the fact and decides what it means. A guarantee the system can't give is turned into information the model can use.

The chat history given to a new agent was fixed the same way: a line goes in before the lines of messages after it on its stream, and an agent's history is only the lines before its own message, so it isn't shown a message it is about to be handed as its input.

## The merge that skipped a rebase

The same afternoon, a change that made a second source of messages (an app's chat) feed the same queue had been written before this one and was merged after it, without a rebase. It still called `reserveDecision` with the old arguments. Every message from that source threw (`earlier.filter is not a function`) and got no agent. The fix was to give that source the same read-back and the same handled-at-the-end step as the others.

Two habits follow from that. A signature change that moves a parameter into the middle is easy for a stale caller to get wrong. And a new kind of intake should copy the whole contract of the old ones, not just the call that queues it.

## What to take from it

- **Serialising is not ordering.** A lock or a queue by arrival makes events one at a time; only the source's own order makes them in order.
- **Wait only for what you know exists.** Reading back the stream turns "wait a few seconds in case" into "wait for these two events", and a lone event waits for nothing.
- **Every held place needs a release on every path,** including the paths that turn out not to be inputs.
- **When order can't be guaranteed, say so to the agent.** A one-line note is cheaper than a guess.
