feat(opencode)!: keep server state per workspace #84

Merged
claudiomaradonna merged 3 commits from feat/opencode-workspace-state into master 2026-11-22 17:06:50 +01:00
Collaborator

Why

opencode runs OpenCode in a container of its own per launch, with the
identity's state folder mounted into it. OpenCode 2.x expects one
background server per user to own sessions, permissions, and tool
execution, and it keeps that server's registration and database in the
state every launch shared. A launch therefore met a registration that
points into another container, or ran a second server on the same
database, and the session lists of every project mixed, since each
workspace appears as /workspace. The only way to keep projects apart
was an --account per project, each with a login of its own.

Closes #83

What changed

Each workspace gets a folder of its own in the state folder in use, the
identity's or the account's: workspaces/<name>-<hash>, named after the
workspace folder and a 12-character prefix of the SHA-256 of its path.
It holds the server's registration (state) and its database (db,
which the launcher names through OPENCODE_DB). Login, configuration,
and cache stay shared, so a new project needs no login. A launch names
the folder on standard error.

One container at a time serves a workspace's folder. The container is
named azkaban-opencode-<hash> after that folder, and any other launch
on the workspace stops with a message that names the container and the
command that removes one a session left behind. This replaces the
podman exec join the issue proposed, which would have run the second
launch with the first one's mounts, network, and variables.

The adapter contract gains two optional fields, described in
docs/adding-a-client.md: AZK_CLIENT_CONTAINER_NAME, the name the
core gives the container and checks with the engine before a real run,
and AZK_CLIENT_STATE_ENV, a variable the adapter sets and the core
drops from every environment file with the warning it gives for the
workspace marker. OPENCODE_DB in an identity's env or a workspace's
.env.opencode is dropped that way.

Breaking: sessions OpenCode wrote before this change stay in
data/opencode.db of the state folder, which launches no longer read. A
workspace that is moved or renamed starts with an empty history.

How to verify

Setup is the one in AGENTS.md: the repository needs nothing installed
for the first scenario. Base 6b5e210, branch tip e3cc683.

The launch a workspace gets, run on both revisions, from a checkout
of each:

mkdir -p /tmp/try/bin /tmp/try/home /tmp/try/one/app
ln -sf "$PWD/bin/azkaban" /tmp/try/bin/opencode
HOME=/tmp/try/home /tmp/try/bin/opencode --dry-run /tmp/try/one/app \
    -- session list | tr ' ' '\n' | grep -E 'local/state|opencode-db|OPENCODE_DB|--name|azkaban-opencode'

On the base it prints one line, the state every workspace shares:
/tmp/try/home/.azkaban/state/default/opencode/state:/home/opencode/.local/state/opencode.
On the branch it prints the workspace's own state and db mounts
below .../opencode/workspaces/app-<hash>/, --name,
azkaban-opencode-<hash>, and
OPENCODE_DB=/home/opencode/.local/share/opencode-db/opencode.db. Run
as written on both revisions, with those results.

On a host with rootless Podman and the OpenCode 2.x image, with
opencode linked to the branch's bin/azkaban as above:

  1. Open a session in each of two projects, then run
    opencode <project-a> -- session list: it lists only the session of
    project A.
  2. Run opencode <new-project> -- auth list: it shows the login the
    state folder already holds.
  3. With project A still open, run opencode <project-a> in a second
    terminal: it stops with
    container 'azkaban-opencode-...' already serves this workspace.

The maintainer ran these on a host at f319d38, before the review
fixes, and reported that all worked; the output was not kept, and they
were not run again on the tip.

Untested: make test-e2e, the release gate, whose OpenCode state now
carries the db mount and OPENCODE_DB; the machine the work ran on
has no Podman. Expected: it passes. Untested: macOS with Apple
container. Expected: the same behavior as on Linux.

Checks run on the tip: make lint && .agents/hooks/shellcheck.sh repo && make test, 337 tests, 0 failures. Each regression test the change adds,
extends, or rewrites fails on 6b5e210 at its assertion.

Considerations

  • The stop of a second launch covers every launch on a workspace, a
    headless run included, so a supervisor that starts two at once on
    one workspace now sees the second one stop. Review suggested
    documenting different accounts as the way to run them together; the
    maintainer chose to leave that out.
  • The documentation says where earlier sessions stay and gives no way to
    bring them back, since none was tried on a host; the maintainer chose
    to leave the text at that.
  • Apple container's command reference gives --name as the container's
    ID and does not say that it refuses an ID in use. Until that is tried
    on a Mac, two launches that start together there rest on the
    launcher's own check alone.
## Why `opencode` runs OpenCode in a container of its own per launch, with the identity's state folder mounted into it. OpenCode 2.x expects one background server per user to own sessions, permissions, and tool execution, and it keeps that server's registration and database in the state every launch shared. A launch therefore met a registration that points into another container, or ran a second server on the same database, and the session lists of every project mixed, since each workspace appears as `/workspace`. The only way to keep projects apart was an `--account` per project, each with a login of its own. Closes #83 ## What changed Each workspace gets a folder of its own in the state folder in use, the identity's or the account's: `workspaces/<name>-<hash>`, named after the workspace folder and a 12-character prefix of the SHA-256 of its path. It holds the server's registration (`state`) and its database (`db`, which the launcher names through `OPENCODE_DB`). Login, configuration, and cache stay shared, so a new project needs no login. A launch names the folder on standard error. One container at a time serves a workspace's folder. The container is named `azkaban-opencode-<hash>` after that folder, and any other launch on the workspace stops with a message that names the container and the command that removes one a session left behind. This replaces the `podman exec` join the issue proposed, which would have run the second launch with the first one's mounts, network, and variables. The adapter contract gains two optional fields, described in `docs/adding-a-client.md`: `AZK_CLIENT_CONTAINER_NAME`, the name the core gives the container and checks with the engine before a real run, and `AZK_CLIENT_STATE_ENV`, a variable the adapter sets and the core drops from every environment file with the warning it gives for the workspace marker. `OPENCODE_DB` in an identity's `env` or a workspace's `.env.opencode` is dropped that way. **Breaking:** sessions OpenCode wrote before this change stay in `data/opencode.db` of the state folder, which launches no longer read. A workspace that is moved or renamed starts with an empty history. ## How to verify Setup is the one in `AGENTS.md`: the repository needs nothing installed for the first scenario. Base `6b5e210`, branch tip `e3cc683`. **The launch a workspace gets**, run on both revisions, from a checkout of each: ```bash mkdir -p /tmp/try/bin /tmp/try/home /tmp/try/one/app ln -sf "$PWD/bin/azkaban" /tmp/try/bin/opencode HOME=/tmp/try/home /tmp/try/bin/opencode --dry-run /tmp/try/one/app \ -- session list | tr ' ' '\n' | grep -E 'local/state|opencode-db|OPENCODE_DB|--name|azkaban-opencode' ``` On the base it prints one line, the state every workspace shares: `/tmp/try/home/.azkaban/state/default/opencode/state:/home/opencode/.local/state/opencode`. On the branch it prints the workspace's own `state` and `db` mounts below `.../opencode/workspaces/app-<hash>/`, `--name`, `azkaban-opencode-<hash>`, and `OPENCODE_DB=/home/opencode/.local/share/opencode-db/opencode.db`. Run as written on both revisions, with those results. **On a host with rootless Podman and the OpenCode 2.x image**, with `opencode` linked to the branch's `bin/azkaban` as above: 1. Open a session in each of two projects, then run `opencode <project-a> -- session list`: it lists only the session of project A. 2. Run `opencode <new-project> -- auth list`: it shows the login the state folder already holds. 3. With project A still open, run `opencode <project-a>` in a second terminal: it stops with `container 'azkaban-opencode-...' already serves this workspace`. The maintainer ran these on a host at `f319d38`, before the review fixes, and reported that all worked; the output was not kept, and they were not run again on the tip. Untested: `make test-e2e`, the release gate, whose OpenCode state now carries the `db` mount and `OPENCODE_DB`; the machine the work ran on has no Podman. Expected: it passes. Untested: macOS with Apple container. Expected: the same behavior as on Linux. Checks run on the tip: `make lint && .agents/hooks/shellcheck.sh repo && make test`, 337 tests, 0 failures. Each regression test the change adds, extends, or rewrites fails on `6b5e210` at its assertion. ## Considerations - The stop of a second launch covers every launch on a workspace, a headless `run` included, so a supervisor that starts two at once on one workspace now sees the second one stop. Review suggested documenting different accounts as the way to run them together; the maintainer chose to leave that out. - The documentation says where earlier sessions stay and gives no way to bring them back, since none was tried on a host; the maintainer chose to leave the text at that. - Apple container's command reference gives `--name` as the container's ID and does not say that it refuses an ID in use. Until that is tried on a Mac, two launches that start together there rest on the launcher's own check alone.
Every OpenCode launch of an identity mounted one state folder, so the
single server OpenCode 2.x expects per user met another container's
registration and database, and every project's sessions mixed
(#83). Each workspace now gets a folder of its own below the state in
use, named after the workspace folder and a prefix of the SHA-256 of
its path, holding the server's registration and, through OPENCODE_DB,
its database. Login, configuration, and cache stay in the state folder
or account, so a new workspace needs no login of its own.

BREAKING CHANGE: sessions OpenCode wrote before this change stay in
data/opencode.db of the state folder, which launches no longer read.
Two launches on one workspace would still start two OpenCode servers on
that workspace's database (#83). The adapter now names the container
after its workspace state folder through a new optional adapter field,
AZK_CLIENT_CONTAINER_NAME; the core passes it as --name and stops a
real launch whose name the engine already holds, saying how to remove
a container a session left behind. The engine refuses a duplicate name
itself, which covers two launches that start together.
Tell an OpenCode user where a project's sessions live, what every
workspace shares, where earlier history stays, and what the message of
a second launch on a workspace asks of them.
claudiomaradonna deleted branch feat/opencode-workspace-state 2026-11-22 17:06:59 +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!84
No description provided.