fix(opencode): remove an exited container at launch #87

Merged
claudiomaradonna merged 3 commits from fix/opencode-exited-container into master 2026-11-23 11:16:43 +01:00
Collaborator

Why

An OpenCode launch names its container after the workspace, so that one
container at a time serves a workspace's state. A session that ends
badly can leave that container behind, exited, despite --rm. The
launch then refused every later run on the workspace with
container '...' already serves this workspace until someone removed
the container by hand, a step an unattended supervisor cannot take: it
retried in a loop.

Closes #86

What changed

On Linux, a launch that finds its workspace's container asks Podman for
its state. A container that is exited or stopped is removed with an
unforced podman rm, the launch says so on standard error with
opencode: removed exited container '<name>', and it runs. Before, it
stopped with exit 1.

Any other state still stops the launch with the same message as before:
a created container may be another launch about to start, and a
paused one is a live session.

When the removal fails, the launch stops before the run, lets the
engine's error through, and prints
container '<name>' has exited and could not be removed: remove it with 'podman rm <name>'.

macOS is unchanged: the launch stops while the name exists. Apple
container reports only running and stopped containers, so an exited
container cannot be told from one about to start.

The check lives in _azk_refuse_second_container (bin/azkaban), and
the state query in azk_host_container_exited (lib/azk-host.sh).

How to verify

The tests run against the vendored Podman stub, from a fresh checkout
with no setup. Base is b6c8dc8, branch is f59aef7.

On the branch:

$ make test TESTS=opencode_state FILTER='exited_container'
    ok  test_launch_removes_the_exited_container_of_its_workspace
    ok  test_launch_stops_when_the_exited_container_cannot_be_removed
  2 test(s), 0 failure(s)

On the base's code with the branch's tests:

$ git worktree add --detach ../base b6c8dc8
$ git -C ../base checkout f59aef7 -- tests
$ make -C ../base test TESTS=opencode_state FILTER='exited_container'

Both tests fail: the first with exit 1 and
already serves this workspace, the second for lack of the removal
error. The pin of the kept refusal passes on both revisions:

$ make test TESTS=opencode_state FILTER='has_not_exited'
    ok  test_second_launch_on_a_workspace_stops_while_its_container_has_not_exited

All three were run with these results.

Untested: the scenario on a real host, since the machine that built
this has no Podman. On Linux with rootless Podman, with the workspace's
container left Exited:

$ opencode ~/projects/example --identity example -- run "Reply with one word."; echo "exit=$?"

Expected on the branch: the removed exited container line, a reply,
and exit=0; on the base, already serves this workspace and
exit=1. A second launch while one runs is expected to stop as before.

make lint && .agents/hooks/shellcheck.sh repo && make test passed on
the branch: 343 tests, 0 failures.

Considerations

  • The real answers of podman container inspect --format '{{.State.Status}}'
    and of an unforced podman rm rest on the stub until the host
    scenario above is run.
  • Why --rm leaves the container when a supervisor stops the session
    is still unknown, so supervisors will keep leaving exited containers,
    which the next launch now removes.
  • An exited container that its own --rm removes at the instant the
    launch removes it makes the removal fail; rerunning is enough.
  • The refusal message is kept word for word. For a paused container
    its podman rm command probably needs --force; this was not
    checked.
  • Set aside: a reviewer's request for a macOS note in docs/macos.md,
    because the rule already has its home in docs/configuration.md.
## Why An OpenCode launch names its container after the workspace, so that one container at a time serves a workspace's state. A session that ends badly can leave that container behind, exited, despite `--rm`. The launch then refused every later run on the workspace with `container '...' already serves this workspace` until someone removed the container by hand, a step an unattended supervisor cannot take: it retried in a loop. Closes #86 ## What changed On Linux, a launch that finds its workspace's container asks Podman for its state. A container that is `exited` or `stopped` is removed with an unforced `podman rm`, the launch says so on standard error with `opencode: removed exited container '<name>'`, and it runs. Before, it stopped with exit 1. Any other state still stops the launch with the same message as before: a `created` container may be another launch about to start, and a `paused` one is a live session. When the removal fails, the launch stops before the run, lets the engine's error through, and prints `container '<name>' has exited and could not be removed: remove it with 'podman rm <name>'`. macOS is unchanged: the launch stops while the name exists. Apple container reports only running and stopped containers, so an exited container cannot be told from one about to start. The check lives in `_azk_refuse_second_container` (`bin/azkaban`), and the state query in `azk_host_container_exited` (`lib/azk-host.sh`). ## How to verify The tests run against the vendored Podman stub, from a fresh checkout with no setup. Base is `b6c8dc8`, branch is `f59aef7`. On the branch: ```console $ make test TESTS=opencode_state FILTER='exited_container' ok test_launch_removes_the_exited_container_of_its_workspace ok test_launch_stops_when_the_exited_container_cannot_be_removed 2 test(s), 0 failure(s) ``` On the base's code with the branch's tests: ```console $ git worktree add --detach ../base b6c8dc8 $ git -C ../base checkout f59aef7 -- tests $ make -C ../base test TESTS=opencode_state FILTER='exited_container' ``` Both tests fail: the first with exit 1 and `already serves this workspace`, the second for lack of the removal error. The pin of the kept refusal passes on both revisions: ```console $ make test TESTS=opencode_state FILTER='has_not_exited' ok test_second_launch_on_a_workspace_stops_while_its_container_has_not_exited ``` All three were run with these results. Untested: the scenario on a real host, since the machine that built this has no Podman. On Linux with rootless Podman, with the workspace's container left `Exited`: ```console $ opencode ~/projects/example --identity example -- run "Reply with one word."; echo "exit=$?" ``` Expected on the branch: the `removed exited container` line, a reply, and `exit=0`; on the base, `already serves this workspace` and `exit=1`. A second launch while one runs is expected to stop as before. `make lint && .agents/hooks/shellcheck.sh repo && make test` passed on the branch: 343 tests, 0 failures. ## Considerations - The real answers of `podman container inspect --format '{{.State.Status}}'` and of an unforced `podman rm` rest on the stub until the host scenario above is run. - Why `--rm` leaves the container when a supervisor stops the session is still unknown, so supervisors will keep leaving exited containers, which the next launch now removes. - An exited container that its own `--rm` removes at the instant the launch removes it makes the removal fail; rerunning is enough. - The refusal message is kept word for word. For a `paused` container its `podman rm` command probably needs `--force`; this was not checked. - Set aside: a reviewer's request for a macOS note in `docs/macos.md`, because the rule already has its home in `docs/configuration.md`.
The second launch on a workspace stops whether its container is
running, created, or paused. The Podman stub gains a state for the
names it holds, so the fix for an exited container can tell them
apart.

Refs: #86
A session that ended badly can leave its container behind despite
--rm, and the launch refused every later run on that workspace until
someone removed it by hand, which an unattended supervisor cannot do.

On Linux the launch now removes a container Podman reports as exited
or stopped, unforced, and says so. Any other state still stops the
launch: a created container may be another launch about to start.
macOS keeps refusing while the name exists, since Apple container
does not tell an exited container from one about to start.

Refs: #86
Refs: #86
claudiomaradonna deleted branch fix/opencode-exited-container 2026-11-23 11:16:52 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
claudiomaradonna/azkaban!87
No description provided.