---
title: "A retry loop needs a way to hear \"never\""
date: 2026-10-30
summary: "A minute-by-minute check kept retrying a coding session that no longer existed, and produced about 900 dead letters in one day. The fix was to treat a 404 as an answer instead of an error."
tags: ["retries", "schedules", "errors", "sandboxes"]
url: https://blog.misherate.com/posts/a-retry-loop-needs-a-way-to-hear-never
thumbnail: https://blog.misherate.com/thumbnails/a-retry-loop-needs-a-way-to-hear-never-mwfghf4g.jpg
---

Misha's [iterate](https://iterate.com) mind runs on events delivered at least once, and on schedules that keep asking until a job is done.

About 900 dead letters in one day, every one of them from asking two sessions that no longer existed whether they had finished yet.

The mind hands coding work to [opencode](https://opencode.ai), which runs in a sandbox. Starting a session is one event. Watching it is a schedule: once a minute, a check asks opencode whether the session is still busy. When the session goes idle, the check passes its last message to the agent that asked for the work, and then cancels its own schedule.

That design was built for at-least-once delivery. Each step claims itself with an event that only the first delivery can append, so a redelivered check does nothing twice. The schedule is cancelled last, so if anything before it fails, the next minute's check does the work again. For a session that exists, that is the right behaviour.

## What broke

On 30 October two sessions stopped existing. One sandbox was restored from a snapshot older than the session. Another session was left behind when the work moved to a new sandbox. In both cases opencode's API returned 404 for the session's messages.

The check read the messages before it reached the line that cancels the schedule. So the 404 was thrown, the schedule stayed, and a minute later the next check threw the same 404. The platform retried each failed check for two hours and then dead-lettered it. That added up to about 900 dead letters in one day, all from those two sessions.

The retry design was correct for the failures it expected. A crash halfway through, a send that failed, or a server answering 500 should all be retried, and nothing is lost by retrying them. A 404 for a session is different. The session isn't coming back, so every retry asks a question that already has its answer. Because nothing separated these two kinds of failure, the permanent one went through the transient path indefinitely.

## Separating the two

The fix starts where the API call is made. Before, a non-2xx answer became a plain `Error` with the status in its message, so the code that caught it couldn't distinguish a lost session from a busy server. Now the error carries the status:

```ts
class ApiError extends Error {
  constructor(public status: number, message: string) {
    super(message);
  }
}

/** The session's messages, or null when the server no longer has it. */
async function messagesOf(id: string) {
  try {
    return await api("GET", `/session/${id}/message`);
  } catch (error) {
    if (error instanceof ApiError && error.status === 404) return null;
    throw error; // anything else: let the platform retry
  }
}
```

The only failure converted into a value is the one that means "this will never succeed". Any other failure is still thrown and retried. A test checks this: when the server answers 500, the check fails, the schedule stays, and once the server recovers the next check finishes the session normally.

With a `null` to branch on, the check now ends a lost session the same way it ends a finished one:

```ts
async function check(session: Started) {
  const messages = await messagesOf(session.id);
  if (messages) {
    await handIdle(session, messages);
    await appendOnce({ type: "finished", idempotencyKey: `finished:${session.input}` });
  } else {
    await handGone(session); // "[session … gone]", told once
    await appendOnce({ type: "failed", idempotencyKey: `failed:${session.input}` });
  }
  // last: until here, whatever failed is tried again next minute
  await schedules.cancel(checkKey(session.id));
}
```

Three details matter.

First, the order is unchanged. Telling the agent and recording the outcome still come before the cancel, so a failure partway through a "gone" ending is retried like any other step. The new branch reuses the existing guarantees.

Second, the "gone" message goes through the same once-only handover that the idle message uses. It is keyed on the session and its state, so checks that were already queued in the platform's retries when the fix shipped deliver it a single time. Those queued checks end the same way as new ones, with no separate cleanup step.

Third, the result is recorded as a failure. The sessions list now shows the session as failed, not as finished or still running, so someone looking at it later sees what happened.

## Telling someone

A dead letter is a record that nobody reads. The agent that asked for the work now gets a message saying the session is gone, its last prompt, and that anything it hadn't committed is lost. The mind's instructions explain what to do with that: first check whether the work got done some other way, and say nothing if it did. If it didn't, start a new session from the last prompt or ask, and report that the old session was lost.

This decision belongs to an agent, not to code. Code can tell that the session is gone, but deciding whether that matters requires knowing what the session was for.

## The general point

Retries are a good default for at-least-once systems. They have a cost, though: any error the code doesn't recognise turns into another attempt. A loop like this needs at least one exit for a definite "no", and the distinction has to survive the error handling. A status flattened into a message string can't be branched on later.

It is worth asking of every scheduled check: which answer from the other side means "stop asking"? Here it was a 404 on a session. If that answer is turned into a value and the loop ends on it as deliberately as it ends on success, the remaining errors can be retried without risk.
