Repository navigation
Description
Summary
opencode service set env <KEY> <VALUE> hard-restarts the shared background service, and because tool calls and CLI requests execute through that service, the restart cancels the in-flight invocation that issued it. This makes the command unusable from an agent session or any long-running shell context unless it is detached.
Environment
- opencode version: 2.1.3
- OS: Linux 7.2.11-200.fc44.x86_64 (Fedora 44, x86_64)
- Terminal: xterm-256color, COLORTERM=truecolor
- Shell: /bin/bash
- Install/channel: latest (release)
- Active plugins: one local plugin at
~/.config/opencode/plugins/projectman.js— currently failing to load (V1→V2 export shape), so effectively none active. MCP servers configured: netbox (remote, connected), atlassian and playwright (local, failing —dockerabsent from host).
Reproduction
- Start an opencode session (or otherwise have in-flight work against the shared server).
- Run
opencode service set env MISTRAL_API_KEY <value>from that session or a shell whose tooling rides the same server. - The service restarts mid-command; the invoking tool call / CLI process is cancelled and reports "Command cancelled". The env var is not applied.
- Repeat: the failure is consistent (3/3 attempts cancelled). It only succeeds when the command is fully detached from the process tree, e.g.
setsid sh -c 'opencode service set env …' &with output redirected, then pollingopencode service get.
Expected Behavior
The environment variable is applied to the service without terminating the process that issued the command — either by applying it without a hard restart, or by deferring the restart until the CLI exits cleanly. At minimum, a hard restart should not cancel in-flight tool calls.
Actual Behavior
The shared background service restarts synchronously during service set env, killing the in-flight caller. The command exits with {"error":{"type":"unknown","message":"Command cancelled"}} and the value is not stored (opencode service get returns {}).
Additional Context
- This was hit while moving a provider API key out of
opencode.jsonintopassand referencing it via{env:...}— a documented, supported workflow (the models guide usesopencode service set env OPENCODE_DISABLE_FREE_MODELS 1as the example). - Workaround confirmed working: run detached via
setsidand pollopencode service get. I can share a one-line wrapper if useful. - Related observation (possibly a separate issue): under the V2
providerskey, deprecated V1 field names (npm,options) are silently dropped rather than normalized or rejected, unlike the V1providerkey where they are normalized topackage/settings. This silently broke two providers during aprovider→providersmigration with no error. Happy to file separately if preferred. - No secret is included here; the key involved was redacted.
Activity
opencode-agent commentedon Oct 22, 2026
Thanks for the detailed report. I was able to reproduce this on 2.1.3, and it still happens on the latest
dev(4a6dacec).Steps: start the background service, then have the service itself run
opencode service set env MISTRAL_API_KEY …through its shell endpoint. That is the same way the agent's shell tool runs commands. The service stops, theset envprocess is killed along with it, and nothing is written:opencode service getstill returns{}. The script below reproduces it in isolated config/state directories.The cause seems to be that
service set/unsetstop the service before saving the new config, so when the caller is a child of the service, the save never happens. Open PR #55957 changes that order. Related: #55065 (a value that is saved but never reaches the server) and #55967 (asking for approval before restarting while agents are running).Until this is fixed, running the command outside the service's process tree (from a separate terminal, or detached with
setsidas you found) is the way to go.Reproduction script (repro.sh)
Run it:
bash repro.sh