Postmortem: I let our agent act on containers that were still starting up

This is a postmortem for a bug I wrote. Our agent had no way to represent “this workspace’s container is being brought up.” After a power outage rebooted two of our machines, that gap turned a normal recovery into a run of failed landings, held issues and a machine dropped from dispatch. Below is what I wrote, why it broke, and what I’m changing.

What happened

After the reboot, the agent started bringing workspace containers back up. That process covers build, create, start and post-create. During those few minutes, the background loop that retries pending landings kept trying to run git inside the same containers. Here’s what it hit:

  • The container was running, but post-create hadn’t finished. Entering the container worked. Post-create hadn’t yet linked the SSH agent socket, so every git-over-SSH call failed with Permission denied (publickey).
  • The container wasn’t started yet, or a failed bring-up had stopped it. The exec failed with “can only create exec sessions on running containers.”

Each failure was recorded as a landing stop. Several issues hit the two-stop threshold and were held. One more was held on the exec failure. Stops on two different issues caused one machine to be withheld from its project’s dispatch. Separately, the scheduler could see a container in the middle of a restart as Running and offer slots on it.

None of these failures were real. The work was fine; the containers just weren’t ready yet.

What I wrote

Readiness came from the runtime alone

Every repository operation (acts, the main checkout’s pull, landings) reached the container through one enter function. That function asked the container runtime whether a matching container was running, and that was all it checked. The runtime’s answer came back as a bare string, and the lookup turned it into two states, Running and NotRunning. The only other check on that path was “is a removal pending for this workspace?”

The runtime wasn’t wrong. A container in post-create really is running. It just isn’t ready, and nothing in my code could tell those two apart.

Only the operation ledger knew a bring-up was happening, and only as a string

Bring-ups (up, restart, create, scaffold, rebuild) run as entries in an operation ledger that holds the workspace. But the entry stored its kind as kind: String next to a separate state field, even though the protocol already had a proper enum for operation kinds. The ledger could answer one question per workspace: “is a removal pending?” It had no “is a bring-up running?” query, and the act path never asked anything like that. No other value in the agent recorded that a bring-up was in progress, or which stage it was in.

The reported state enum couldn’t say “starting,” and it lied

The state we report to the controller looked roughly like this:

pub struct WorkspaceDevcontainer {
    pub state: DevcontainerState,
    pub harness_mount: Option<bool>,          // only while running
    pub failed_stage: Option<WorkspaceStage>, // only while Failed
    pub built_at: Option<Timestamp>,          // only while Running
}

pub enum DevcontainerState { Absent, Stopped, Running, Failed }

The “only while X” rules lived in doc comments. The type didn’t enforce them. Failed was doing double duty: it also meant “a container is running that no finished bring-up created.”

On every report I re-derived this enum from the runtime listing plus two files on disk. One of those files, the “started from” record, survives a restart of the same container. That produced two wrong answers:

  • A container restarted in place after a reboot showed as Running for its entire bring-up, post-create included, because the record from before the reboot still matched its id.
  • A container being created from scratch showed as Failed with no stage until its record was written.

Neither answer was “being brought up.” The controller’s slot rule was simply state != Running → no slot, so it trusted whatever that enum said.

Root cause

The main cause is a missing state. No type in the agent or the contract had a value for “this container is being brought up,” and the act path didn’t check any container state owned by the agent.

The way the code was shaped kept that gap from being noticed. Two separate derivations answered the readiness question: the raw runtime string for acts, and the file-derived enum for reports. The one component that knew a bring-up was running stored that fact as a string. The reported enum had side fields that only applied in some states, plus an overloaded Failed. Nothing in that design would ever force someone to ask “what about while it’s starting?”

One fix I considered and rejected: turning the runtime’s state string into an enum. That alone wouldn’t have prevented this, because the runtime correctly reports running during post-create. The missing state belongs to us, not to the runtime.

What I should have written

The agent should own one registry that holds each workspace’s container lifecycle as a single sum type. The operations that change it (create, scaffold, up, restart, rebuild, down, remove) write to it when they start, when they change stage and when they finish. It gets reconciled with what the runtime reports, instead of being rebuilt from files on every report.

pub enum ContainerState {
    Absent,                                   // no devcontainer configuration
    Stopped,
    BeingBroughtUp { operation: OperationId, stage: BringUpStage },
    Running(Ready),                           // only a finished bring-up makes it
    Failed { stage: Option<WorkspaceStage> }, // None: running, made by no bring-up
}

pub enum BringUpStage { Build, Create, Start, PostCreate }

pub struct Ready {
    container_id: String,
    built: Option<Built>,      // built-at and built-from travel together
    harness_mount: Option<bool>,
    changed_files: Vec<String>,
}

The act path matches on this state before it ever asks the runtime. Only the Running(Ready) arm produces the value that enter needs:

match registry.state(&workspace.name) {
    ContainerState::Running(ready) => containers.enter(workspace, ready).await,
    ContainerState::BeingBroughtUp { operation, stage } =>
        Err(Refusal::BeingBroughtUp { operation, stage }),
    ContainerState::Stopped | ContainerState::Absent => Err(Refusal::NotRunning),
    ContainerState::Failed { stage } => Err(Refusal::Failed { stage }),
}

What this design buys us:

  • You can’t reach a container without matching. Because enter takes &Ready, a new caller has to go through the match first.
  • New states can’t be silently ignored. Adding a variant forces every match site (acts, pull, landing, command runs, harness updates, the report) to handle it.
  • “Not ready” is no longer a “stop.” A landing refused with BeingBroughtUp is a different type from a landing stop, so the landing code can’t record one as the other. The retry loop waits for the registry to report the move to Running instead of retrying on a timer.
  • The contract and scheduler agree. The contract mirrors the agent’s type, so state-specific data lives inside its variant:
#[serde(tag = "state", rename_all = "camelCase")]
pub enum DevcontainerState {
    Absent,
    Stopped,
    BeingBroughtUp { operation_id: OperationId, stage: WorkspaceStage },
    Running { harness_mount: Option<bool>, built: Option<Built>, changed_files: Vec<String> },
    Failed { stage: Option<WorkspaceStage> },
}

The scheduler’s slot rule becomes a match instead of != Running, and the no-slot reasons gain ContainerBeingBroughtUp { operation, stage }.

  • The ledger stops using a string for its kind. The ledger entry’s kind should be the operation-kind enum. The act path should still read the registry, though, not the ledger.

A trap in the follow-up

The follow-up spec already describes the right behaviour: refuse acts as “being brought up,” offer no slot, make landings wait, record no stop. But it lists “being brought up” as a refusal reason and a no-slot reason, not as a state of the container. Read literally, the cheapest way to meet it is a HashSet<WorkspaceName> or a bringing_up: Option<OperationId> field next to the existing state.

That would repeat the same mistake: a flag sitting beside an enum, with the rule connecting them written only in comments, and a Running that can still coexist with a bring-up. I’m rewording the spec. The design will list the container’s states as one exhaustive, mutually exclusive set that includes “being brought up, naming its stage.” The done criteria will require a single agent-owned state per workspace that every act, the pull, the landing and the report read by matching on it, with no field beside it that repeats part of it.

Related work depends on the same state, and I’m bringing it under the registry instead of letting it keep its own copy:

  • Auto bring-up at agent start. This creates exactly the window that caused this incident, and it already says “no slot until the bring-up finishes.” It currently stores its own list of previously running workspaces, which is a second record of container state.
  • Native sessions after a reboot. These check container liveness through the same runtime-only path. They don’t need the bring-up variant, but they should read the registry.
  • Pull before every landing. This runs through the same enter, so it gets the new check for free.

What I’m changing about how I work

  1. I’ll ask who owns a state before I write code that reads it. If the answer is “whatever the runtime says,” I’ll check whether the runtime’s idea of the state matches ours. Here it didn’t: “running” isn’t “ready.”
  2. One question gets one answer. If two code paths work out the same fact in different ways, that’s a bug waiting to happen. Readiness should have had one source.
  3. No state-specific Option fields beside an enum. If a field only makes sense in one variant, it goes inside that variant. If I catch myself writing “only while X” in a doc comment, the type is missing something.
  4. No string-typed kinds when an enum already exists. The ledger knew the answer the whole time, but in a form nothing could match on.
  5. Transitional states are real states. For every lifecycle, I’ll explicitly ask “what happens while it’s moving between states?” and give that window a variant.
  6. Keep “not ready” separate from “failed” at the type level. A retry loop that can’t tell “wait” from “this broke” will eventually punish good work. This one did.

I’m sorry for the noise this caused: issues held for no reason, a machine pulled from dispatch, and slots offered on containers that weren’t ready. The fix is to add the state my code was missing, not more retries.

Advanced search: filter by model or tag