opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Attach CLI and ACP to the running local service

Open liuchong opened this issue 1 week ago • 5 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

I'd like a script or ACP client to reconnect to the exact session already open in OpenCode Desktop. A second server can see the same database but miss live events from the first server, as described in #55098.

Could opencode api and opencode acp each offer --attach? It would use the existing authenticated local service registration and exit clearly if no service is running. It would never start, replace or stop the service. For ACP, I propose keeping live session permissions with the running app so an editor cannot introduce extra directory grants when it loads a session.

If the design fits, I can send a small CLI PR first and a separate ACP PR after that.

liuchong avatar Nov 21 '26 10:11 liuchong

Thanks for the detailed proposal, @liuchong. As I read it, you're asking for an attach-only mode for opencode api and opencode acp. It would use the existing authenticated local service registration, exit with a clear error if no service is running, and never start, replace or stop the service. For ACP, the running app would also keep the live session permissions.

What I found in the current code:

  • opencode acp always starts its own private server (Standalone.start() in packages/cli/src/commands/handlers/acp.ts), so it is always a second server next to Desktop. It has no flag to point it at the running service.
  • opencode api already uses the background service by default and accepts --server <url>. It resolves the service through ServerConnection.resolve. I didn't check whether that starts a service when none is running, which is where a strict "attach only, never start" mode would differ.

Related issues:

  • #55098: the problem you cite (a second server sharing the database misses live events). A recent comment there says the same thing about opencode acp.
  • #62986: a request to control the port of opencode acp's internal server.
  • #52075: an earlier PR that added opencode acp --attach <url>. It was closed unmerged when the V1 branch was retired, with a note that it could be ported to V2.

Maintainers will look at the design. They may want to agree on it before you open the CLI and ACP PRs.

opencodejr[bot] avatar Nov 21 '26 10:11 opencodejr[bot]

Thanks! I checked ServerConnection.resolve (packages/cli/src/services/server-connection.ts) and Service (packages/client/src/effect/service.ts) on dev to fill in that gap:

  • opencode api default: calls ServerConnection.resolve({ mismatch: "ignore" }), which goes through Service.ensure({ ...options, version: undefined }). When no healthy service is registered, Service.ensure spawns opencode serve --service (and terminates/recovers an unresponsive registered PID after 3 probe timeouts).
  • --server <url>: skips the local service registration file, so callers have to supply both the URL and OPENCODE_PASSWORD separately.
  • autostart: false (used by opencode run): uses Service.discover({ ...options, version: undefined }) so it does not start a background service, but when discover returns undefined it falls back to Standalone.start() rather than failing fast.
  • opencode acp: always calls Standalone.start() directly (packages/cli/src/commands/handlers/acp.ts) without ServerFlags or Service.discover.

So a strict attach-only path would call Service.discover({ ...options, version: undefined }) and exit with a clear error when undefined, without calling Service.ensure or Standalone.start(). Happy to wait for maintainers' feedback on the flag and ACP permission model before opening any PRs.

liuchong avatar Nov 21 '26 17:11 liuchong

A small control for the attach acceptance test: on OpenCode 1.18.29, I held a turn open in a local Open Agent API fixture and started a second ACP process. The second could list and resume the same session ID, but only the first received the live assistant text. The second got the text only after an explicit session/load.

Reproduction. This is the older ACP v1 path, not a test of the V2 Desktop service. For --attach, I'd assert that the second client gets the update from the already running turn before asking to load history. Listing the same ID isn't enough.

jiangxingfan1-coder avatar Nov 23 '26 14:11 jiangxingfan1-coder

Great control test — thank you for pointing that out!

That distinction is crucial for cross-process / multi-agent coordination too: attaching to the running service keeps both clients on the same event bus, and an attached ACP process must also start observing live turns on attach (session/resume / session/new / session/fork) — syncing session.active() immediately — rather than waiting for session/load or an explicit session/prompt.

In our V2 attach design/acceptance check, we'll explicitly assert that after session/resume (before any session/load call):

  1. Deltas from an already-running turn and subsequent external turns stream live via session/update.
  2. _session/steering works mid-turn while interactive permissions/elicitations stay with the running app.
  3. session/close detaches the observer cleanly without closing the server session.

liuchong avatar Nov 25 '26 12:11 liuchong

Thanks for spelling out the attach boundary — receiving a live update before loading history is exactly the distinction the older repro exposed. A follow-on case worth considering is a reconnect while a turn is streaming: check the handoff between any replayed history and live deltas for omissions or duplicates, and surface a gap if the service cannot prove continuity.

The reproduction was against OpenCode 1.18.29’s older ACP path; I have not tested the proposed V2 attach implementation. (Codex assistant, Open Agent API.)

jiangxingfan1-coder avatar Nov 25 '26 12:11 jiangxingfan1-coder