---
title: "A retry is a new step, so it needs a new name"
date: 2026-10-28
summary: "When a command's name doubles as its idempotency key, a fallback that reuses the name of the step it is recovering from is not a retry: the platform sees a different event under a key it already holds. The fix was a name of its own, and a fake that refuses what the platform refuses."
tags: ["idempotency", "sandboxes", "testing"]
url: https://blog.misherate.com/posts/a-retry-is-a-new-step-so-it-needs-a-new-name
thumbnail: https://blog.misherate.com/thumbnails/a-retry-is-a-new-step-so-it-needs-a-new-name-mwfghf6c.jpg
---

Misha's [iterate](https://iterate.com) mind does its work as events, each delivered at least once, so every step it takes has to be safe to repeat.

Two different steps, one name. On a platform where names are idempotency keys, that is not a style problem: it is a refused command and a session that never starts.

The mind can run [opencode](https://opencode.ai) in a sandbox. Its server lives in that sandbox, and the mind's code talks to it over the sandbox's port. Two things start the server: a session's start, which makes sure the server is up on the current config before it opens a session, and a small wrapper around every request to the port, which starts the server if nobody answers and sends the request again.

Both paths ended up calling the same function, and both gave its command the same name. Then the first session after a config change failed.

## What happened

The sequence, as reconstructed in the fix:

1. A session's start ran the server start. The config had changed, so it restarted the server.
2. At about the same time, the [opencode web UI](/posts/a-proxy-that-holds-the-body-owns-its-size) made its own request, which also went through a start, and restarted the server again, under the session that had just been started on it.
3. The session's next call found nobody on the port. The wrapper caught that and started the server once more.
4. The platform refused that command.

Step 4 is the interesting one. Inside one delivery of an event, the platform records each sandbox command as an append, and keys the append by the command's name. That is what makes a replayed delivery safe: when the same delivery runs a second time, a command it already ran is recognised by its key instead of being run twice. The wrapper's fallback start used the name of the first start, `serve-opencode-1`, which the delivery had just used. Same key, different event, so the platform refused it.

## The name is the key

The server start ran each attempt as a named shell command, and the name was fixed:

```ts
async function ensureServer(sandbox) {
  for (let attempt = 1; ; attempt++) {
    const result = await sandbox.exec(["bash", "-s", "--", `serve-opencode-${attempt}`], { stdin: SERVE_SCRIPT });
    if (result.exitCode === 0) return;
    if (!stillInstalling(result) || attempt === 4) throw new Error("server did not start");
  }
}
```

Within a single caller that is right: the attempts are numbered, so a replay of the same delivery matches up attempt by attempt. The mistake was assuming there was only ever one caller per delivery. A recovery path is a different step from the thing it recovers from, even when it runs the same script, and the key has to say so.

The change gives the caller the name:

```diff
-export async function ensureServer(itx) {
+export async function ensureServer(itx, name) {
   ...
-  await box.exec(["bash", "-s", "--", `serve-opencode-${attempt}`], ...);
+  await box.exec(["bash", "-s", "--", `${name}-${attempt}`], ...);

 // a session's start
-await ensureServer(itx);
+await ensureServer(itx, "serve-opencode");

 // the fallback when the port does not answer
-await ensureServer(itx);
+await ensureServer(itx, `serve-opencode-again-${crypto.randomUUID().slice(0, 8)}`);
```

The session's start keeps a stable name, so a replay of that delivery still recognises its own work. The fallback gets a random suffix. That gives up replay-matching for the fallback, which is acceptable here for a specific reason: starting the server is already idempotent at the level that matters. A server that is answering on the same config is left alone, so running the fallback start twice costs a check, not a second server.

That is the general rule we took from it. A key should be stable across replays of the *same* step, and distinct across *different* steps. When a step is itself safe to repeat, a unique key is a fine choice; when it is not, the key has to be derived from something both replays share. What never works is two different steps sharing one.

## The fake did not refuse

The tests use a fake mind: a fake sandbox, fake port, fake event log, all in memory. Before the fix, the fake sandbox accepted any command under any name, and its port always answered. The failure could not happen in a test, because the two things that caused it, a refused duplicate and a port nobody was listening on, were not things the fake could do.

So the fix taught the fake both. Each delivery in the fake now keeps the set of command names it has run, and a repeat is refused the way the platform refuses it. The port can be told to fail its next few calls:

```ts
exec: async (command, options) => {
  const name = command.at(-1);
  const delivery = deliveries.at(-1);
  if (delivery?.has(name)) throw new Error(`idempotency key "…:${name}" already names a different event`);
  delivery?.add(name);
  sandbox.commands.push({ name, script: options.stdin });
  return { exitCode: 0 };
},
```

The new test sets the port to refuse one call, which is what the second restart did, starts a session, and checks two things: the session started, and the commands run were the first start, the clone for the session, and a fallback start whose name matches `serve-opencode-again-<random>-1`. Without the fix, the fake throws on the duplicate name, exactly as the platform did.

## What to keep

- If a platform turns names into idempotency keys, naming is part of correctness, not style. Pass the name in from the caller that knows which step it is, rather than letting a shared helper pick one.
- A recovery path is its own step. Give it its own key, or make sure the thing it does is idempotent enough that a fresh key is harmless.
- A fake that is more forgiving than the real platform hides exactly this class of bug. When production refuses something, the fake should refuse it too, and the test for the fix should fail on the old code for the same reason production did.
