[FEATURE]: Attach CLI and ACP to the running local service
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.
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 acpalways starts its own private server (Standalone.start()inpackages/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 apialready uses the background service by default and accepts--server <url>. It resolves the service throughServerConnection.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.
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 apidefault: callsServerConnection.resolve({ mismatch: "ignore" }), which goes throughService.ensure({ ...options, version: undefined }). When no healthy service is registered,Service.ensurespawnsopencode 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 andOPENCODE_PASSWORDseparately.autostart: false(used byopencode run): usesService.discover({ ...options, version: undefined })so it does not start a background service, but whendiscoverreturnsundefinedit falls back toStandalone.start()rather than failing fast.opencode acp: always callsStandalone.start()directly (packages/cli/src/commands/handlers/acp.ts) withoutServerFlagsorService.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.
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.
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):
- Deltas from an already-running turn and subsequent external turns stream live via
session/update. _session/steeringworks mid-turn while interactive permissions/elicitations stay with the running app.session/closedetaches the observer cleanly without closing the server session.
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.)