Skip to content

Commit 08de9a3

Browse files
feat: implement session/resume RPC for OpenCode
OpenCode uses `session/resume` as a separate ACP method instead of `resumeSessionId` in `session/new`. The Zod schema for session/new has no resumeSessionId field, so the parameter was silently ignored. Changes: - Add `supportsSessionResume()` hook to AcpClient (default false) - Add `resumeSession()` method that calls `session/resume` RPC with {sessionId, cwd, mcpServers} params - createSession() tries session/resume first when supported, falls back to session/new on failure - Only add resumeSessionId to session/new for agents that don't support the separate resume method - OpenCodeClient overrides supportsSessionResume() → true - Refactor processNewSessionResponse → processSessionResponse (sessionId assignment moved to caller for resume compatibility) - Update SESSION-RESUME.md with Bug 26 and protocol documentation Discovered via `strings` analysis of OpenCode v1.2.27 binary: - session/resume request: {sessionId, cwd, mcpServers?} - session/resume response: {configOptions?, models?, modes?} - Handler: agent.unstable_resumeSession() (unstable_ prefix) Related: anomalyco/opencode#8931, xsa-dev/anomalyco-opencode#1 Co-authored-by: Copilot <[email protected]>
1 parent 448bdc8 commit 08de9a3

3 files changed

Lines changed: 109 additions & 12 deletions

File tree

‎docs/SESSION-RESUME.md‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ When the user switches from agent A to agent B:
7575
identifier so the agent client knows to resume on next `session/new`.
7676

7777
3. **Agent client starts** and calls `AcpClient.createSession()`:
78-
- `buildNewSessionParams()` reads `resumeSessionId` from `GenericSettings`
79-
- If set, adds `"resumeSessionId": "<id>"` to the ACP `session/new` request
80-
- The ACP server (Copilot CLI, Kiro, etc.) loads the session by that ID
78+
- Loads `resumeSessionId` from `GenericSettings`
79+
- If the agent supports `session/resume` as a separate RPC method (e.g. OpenCode),
80+
sends `session/resume` with `{sessionId, cwd, mcpServers}` first. Falls back to
81+
`session/new` on failure.
82+
- Otherwise, adds `"resumeSessionId": "<id>"` to the ACP `session/new` request
83+
- `detectResumeFailed()` enables injection fallback if the agent ignored the ID
8184

8285
## Native Session Locations
8386

@@ -132,7 +135,7 @@ continuation of the exported session. The mechanism varies by client:
132135
| Junie | `resumeSessionId` in ACP `session/new` | `GenericSettings("junie", project)` |
133136
| Claude CLI | `--resume <id>` CLI flag | `cliSessionIds` map (in-memory only) + `cliResumeSessionId` property |
134137
| Codex | `codexThreadId` | `PropertiesComponent` (profile-prefixed) |
135-
| OpenCode | (no resume ID — exports directly to SQLite) | N/A |
138+
| OpenCode | `session/resume` ACP method (separate from `session/new`) | `GenericSettings("opencode", project)` |
136139

137140
**Critical:** If the exporter writes session files but does NOT set the resume ID,
138141
the new agent will start a blank session and the exported files are orphaned.
@@ -782,3 +785,30 @@ path and creates a symlink to it at `~/.copilot/session-state/` if the new locat
782785

783786
**Status**: Pending restart to verify.
784787

788+
### Bug 26: OpenCode `session/resume` uses separate RPC method
789+
790+
**Symptom**: OpenCode session resume never worked. The `resumeSessionId` parameter sent inside
791+
`session/new` was silently ignored by OpenCode's Zod schema validation, which strips unknown
792+
fields. The plugin's `detectResumeFailed()` correctly detected the failure and enabled the
793+
injection fallback, but the actual resume was never attempted via the correct method.
794+
795+
**Root cause**: OpenCode implements session resume via a **separate `session/resume` RPC method**
796+
(distinct from `session/new`). The schemas are:
797+
798+
- `session/new` request: `{cwd: string, mcpServers?: McpServer[]}` — no `resumeSessionId`
799+
- `session/resume` request: `{sessionId: string, cwd: string, mcpServers?: McpServer[]}`
800+
- `session/resume` response: `{configOptions?, models?, modes?}` — no `sessionId` (reuses provided ID)
801+
802+
The handler also guards with `if (!agent.unstable_resumeSession)` and the method prefix
803+
`unstable_` confirms this is still a non-finalized API in OpenCode v1.2.27.
804+
805+
**Fix**: Added `supportsSessionResume()` hook to `AcpClient` (default false). `OpenCodeClient`
806+
overrides it to return true. `createSession()` now tries `session/resume` first when a resume
807+
ID is available and the agent supports it. On failure, falls back to `session/new`.
808+
The `resumeSessionId` parameter is only added to `session/new` for agents that do NOT support
809+
the separate resume method (Copilot, Kiro, Junie).
810+
811+
**Related**: [anomalyco/opencode#8931](https://github.com/anomalyco/opencode/issues/8931),
812+
[xsa-dev/anomalyco-opencode#1](https://github.com/xsa-dev/anomalyco-opencode/pull/1)
813+
814+

‎plugin-core/src/main/java/com/github/catatafishen/ideagentforcopilot/acp/client/AcpClient.java‎

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public abstract class AcpClient extends AbstractAgentClient {
9494
private @Nullable String launchCwd;
9595
/**
9696
* Tracks the resume session ID requested in the current launch cycle.
97-
* Set in {@link #buildNewSessionParams}, checked after {@link #onSessionCreated}.
97+
* Set at the start of {@link #createSession}, used by {@link #resumeSession} and
98+
* {@link #detectResumeFailed}.
9899
*/
99100
private @Nullable String requestedResumeId;
100101
private final List<Model> availableModels = new ArrayList<>();
@@ -278,6 +279,19 @@ public final String createSession(String cwd) throws AgentSessionException {
278279
}
279280
try {
280281
beforeCreateSession(cwd);
282+
requestedResumeId = loadResumeSessionId();
283+
284+
// Agents that implement session/resume as a separate RPC method (e.g. OpenCode)
285+
// are tried first. On failure, fall back to the standard session/new path.
286+
if (requestedResumeId != null && supportsSessionResume()) {
287+
try {
288+
return resumeSession(cwd, requestedResumeId);
289+
} catch (Exception e) {
290+
LOG.warn(displayName() + ": session/resume failed for " + requestedResumeId
291+
+ ", falling back to session/new: " + e.getMessage());
292+
}
293+
}
294+
281295
JsonObject params = buildNewSessionParams(cwd);
282296

283297
CompletableFuture<JsonElement> future = transport.sendRequest("session/new", params);
@@ -288,7 +302,8 @@ public final String createSession(String cwd) throws AgentSessionException {
288302
LOG.debug(displayName() + ": session/new: " + (response.models() != null ? response.models().size() : 0) + " model(s), "
289303
+ (response.modes() != null ? response.modes().size() : 0) + " mode(s)");
290304

291-
processNewSessionResponse(response);
305+
currentSessionId = response.sessionId();
306+
processSessionResponse(response);
292307

293308
onSessionCreated(currentSessionId);
294309
detectResumeFailed(currentSessionId);
@@ -308,18 +323,22 @@ private JsonObject buildNewSessionParams(String cwd) {
308323
params.addProperty("cwd", cwd);
309324
customizeNewSession(cwd, mcpPort, params);
310325

311-
// Request continuation of the previous conversation if one was saved.
312-
requestedResumeId = loadResumeSessionId();
313-
if (requestedResumeId != null) {
326+
// Only add resumeSessionId to session/new for agents that don't support the
327+
// separate session/resume RPC method. Agents that do support it have already
328+
// been tried via resumeSession() before reaching this point.
329+
if (!supportsSessionResume() && requestedResumeId != null) {
314330
params.addProperty("resumeSessionId", requestedResumeId);
315331
LOG.info(displayName() + ": requesting resume of session " + requestedResumeId);
316332
}
317333
return params;
318334
}
319335

320-
private void processNewSessionResponse(NewSessionResponse response) {
321-
currentSessionId = response.sessionId();
322-
336+
/**
337+
* Processes the models, modes, and config options from a session response.
338+
* Used by both {@code session/new} and {@code session/resume} paths.
339+
* The caller is responsible for setting {@code currentSessionId} before calling this.
340+
*/
341+
private void processSessionResponse(NewSessionResponse response) {
323342
if (response.models() != null) {
324343
availableModels.clear();
325344
availableModels.addAll(response.models());
@@ -372,6 +391,49 @@ private void updateConfigOptions(NewSessionResponse response) {
372391
LOG.debug(displayName() + ": session/new: " + availableConfigOptions.size() + " config option(s)");
373392
}
374393

394+
/**
395+
* Whether this agent supports the {@code session/resume} RPC method as a separate call
396+
* from {@code session/new}.
397+
* <p>
398+
* When {@code true}, {@link #createSession(String)} will attempt {@code session/resume}
399+
* before falling back to {@code session/new}. When {@code false} (default), the
400+
* {@code resumeSessionId} parameter is passed inside {@code session/new} instead.
401+
*/
402+
protected boolean supportsSessionResume() {
403+
return false;
404+
}
405+
406+
/**
407+
* Resumes an existing session via the {@code session/resume} RPC method.
408+
* <p>
409+
* Only called for agents that override {@link #supportsSessionResume()} to return {@code true}.
410+
* The response has no {@code sessionId} — the provided ID is reused.
411+
*
412+
* @return the resumed session ID (same as {@code sessionId} param)
413+
* @throws Exception if the RPC call fails (caller falls back to {@code session/new})
414+
*/
415+
private String resumeSession(String cwd, String sessionId) throws Exception {
416+
JsonObject params = new JsonObject();
417+
params.addProperty(KEY_SESSION_ID, sessionId);
418+
params.addProperty("cwd", cwd);
419+
int mcpPort = resolveMcpPort();
420+
customizeNewSession(cwd, mcpPort, params);
421+
422+
LOG.info(displayName() + ": attempting session/resume for " + sessionId);
423+
CompletableFuture<JsonElement> future = transport.sendRequest("session/resume", params);
424+
JsonElement result = future.get(SESSION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
425+
LOG.debug(displayName() + ": session/resume raw response: " + result);
426+
427+
NewSessionResponse response = gson.fromJson(result, NewSessionResponse.class);
428+
currentSessionId = sessionId;
429+
processSessionResponse(response);
430+
431+
LOG.info(displayName() + ": successfully resumed session " + sessionId);
432+
onSessionCreated(sessionId);
433+
persistResumeSessionId(sessionId);
434+
return sessionId;
435+
}
436+
375437
@Override
376438
public final void cancelSession(String sessionId) {
377439
JsonObject params = new JsonObject();

‎plugin-core/src/main/java/com/github/catatafishen/ideagentforcopilot/acp/client/OpenCodeClient.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ protected boolean supportsAuthenticate() {
111111
return false;
112112
}
113113

114+
@Override
115+
protected boolean supportsSessionResume() {
116+
return true;
117+
}
118+
114119
@Override
115120
protected void customizeNewSession(String cwd, int mcpPort, JsonObject params) {
116121
// OpenCode requires mcpServers in session/new with type "http" (not "sse" or "local")

0 commit comments

Comments
 (0)