[PR #11812] fix: run --attach agent validation and forward cwd #13940

Open
opened 2026-02-16 18:18:46 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11812

State: open
Merged: No


Update (08.02.2026): I edited the PR description following https://github.com/anomalyco/opencode/pull/11812#issuecomment-3864074183. The new fix intentionally does not forward the client cwd when attaching, since the client and server may be on different machines/filesystems. The crash is fixed by validating --agent against the attached server.

Release Note

Fix crash when running opencode run --attach ... --agent ... by validating agents on the attached server.

What changed

opencode run no longer crashes when used with both --attach and --agent.

This PR fixes a client-side crash that occurred before any request was sent to the attached server.

Problem

The issues were raised in #6489 and #8094. This PR fixes those issues.

Here below is a short summary of the issues:

Running opencode run with --attach + --agent would fail with:

Error: No context found for instance

Root cause:

  • The --attach path in packages/opencode/src/cli/cmd/run.ts does not run bootstrap(...), so no Instance async-local context is created.
  • The CLI tried to validate --agent via Agent.get(...), which depends on Instance.state(...) and requires that missing context.
  • Result: immediate crash.

Implementation

Changes are contained to packages/opencode/src/cli/cmd/run.ts:

  1. When --attach is used, validate --agent against the attached server (via GET /agent, SDK: sdk.app.agents()), instead of calling local Agent.get(...).
  • If the agent doesn’t exist (or is mode: "subagent"), print the existing warning and fall back to the default agent.
  • If the agent list can’t be fetched, warn and fall back.

How to reproduce (before) / verify (after)

Start a server:

opencode server --port 4096

Assuming you have an agent called shell_cmd_generator, run:

  opencode run \
  --attach http://localhost:4096 \
  --agent shell_cmd_generator \
  --model 'google/gemini-2.5-flash' \
  --log-level DEBUG \
  --format json \
  '<user><request>How to list the files in this directory?</request></user>'

In practice, replace shell_cmd_generator with one of the primary agents available for you.

Before: crashes with No context found for instance.

After: completes successfully and uses the requested agent (or gracefully falls back if unavailable).

Notes

  • This keeps existing CLI behavior for unknown/subagent agents: warn + fall back.
  • For attached runs, the CLI intentionally does not assume the client’s process.cwd() matches the server’s filesystem.
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11812 **State:** open **Merged:** No --- **Update (08.02.2026): I edited the PR description following https://github.com/anomalyco/opencode/pull/11812#issuecomment-3864074183. The new fix intentionally does not forward the client `cwd` when attaching, since the client and server may be on different machines/filesystems. The crash is fixed by validating `--agent` against the attached server.** ## Release Note Fix crash when running `opencode run --attach ... --agent ...` by validating agents on the attached server. ## What changed `opencode run` no longer crashes when used with both `--attach` and `--agent`. This PR fixes a client-side crash that occurred before any request was sent to the attached server. ## Problem The issues were raised in #6489 and #8094. This PR fixes those issues. Here below is a short summary of the issues: Running `opencode run` with `--attach` + `--agent` would fail with: ``` Error: No context found for instance ``` Root cause: - The `--attach` path in `packages/opencode/src/cli/cmd/run.ts` does not run `bootstrap(...)`, so no `Instance` async-local context is created. - The CLI tried to validate `--agent` via `Agent.get(...)`, which depends on `Instance.state(...)` and requires that missing context. - Result: immediate crash. ## Implementation Changes are contained to `packages/opencode/src/cli/cmd/run.ts`: 1. When `--attach` is used, validate `--agent` against the attached server (via `GET /agent`, SDK: `sdk.app.agents()`), instead of calling local `Agent.get(...)`. - If the agent doesn’t exist (or is `mode: "subagent"`), print the existing warning and fall back to the default agent. - If the agent list can’t be fetched, warn and fall back. ## How to reproduce (before) / verify (after) Start a server: ```sh opencode server --port 4096 ``` Assuming you have an agent called `shell_cmd_generator`, run: ```sh opencode run \ --attach http://localhost:4096 \ --agent shell_cmd_generator \ --model 'google/gemini-2.5-flash' \ --log-level DEBUG \ --format json \ '<user><request>How to list the files in this directory?</request></user>' ``` In practice, replace `shell_cmd_generator` with one of the primary agents available for you. Before: crashes with `No context found for instance`. After: completes successfully and uses the requested agent (or gracefully falls back if unavailable). ## Notes - This keeps existing CLI behavior for unknown/subagent agents: warn + fall back. - For attached runs, the CLI intentionally does not assume the client’s `process.cwd()` matches the server’s filesystem.
yindo added the pull-request label 2026-02-16 18:18:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13940