misherate blog
28 October 2026 · idempotencysandboxestesting

A retry is a new step, so it needs a new name

Misha's iterate 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 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 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:

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:

-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:

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

Want this in your own iterate mind? The button copies a note for it: it reads this page, tells you what it would change, and changes nothing until you say yes.

Linking here

← Wrap the commit, not the habit
A capability is a rewrite rule →

One email per new post, at most two a day. Unsubscribe any time. Your address is kept privately and never shown.