Bug: session/new fails with Internal error when called immediately after initialize in ACP stdio mode
Description
When using OpenCode as an ACP (Agent Client Protocol) agent via stdio, the session/new method fails with "Internal error" (code -32603) if called immediately after initialize returns.
Steps to Reproduce
# Using acpx (ACP client)
npx acpx opencode exec "your task"
# Or using direct stdio
(echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":1,"clientCapabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}'; sleep 0.1; echo '{"jsonrpc":"2.0","id":2,"method":"session/new","params":{"cwd":"/tmp","mcpServers":[]}}') | opencode acp
Expected Behavior
session/new should succeed after initialize returns successfully.
Actual Behavior
{"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"Internal error","data":{}}}
Root Cause Analysis
From the logs, OpenCode's initialize response is sent before internal initialization is fully complete. The server needs additional time (~2 seconds) to:
- Load user configuration
- Initialize providers
- Set up the default model
When session/new arrives during this initialization window, it fails because the required services aren't ready yet.
Workaround
Adding a 2-second delay between initialize response and session/new request resolves the issue:
// In acpx client
await connection.initialize({...});
await new Promise(resolve => setTimeout(resolve, 2000)); // workaround
await connection.newSession({...});
Testing Results
| Delay after initialize |
Result |
| 100ms - 1500ms |
❌ Internal error |
| 2000ms+ |
✅ Success |
Environment
- OpenCode version: 1.4.0
- Node version: 24.14.0
- OS: Linux (WSL2)
- Config:
model: "bailian-coding-plan/glm-5" with custom providers
Suggested Fix
One of these approaches would resolve the issue:
- Delay the
initialize response until all internal initialization is complete
- Queue early requests and process them after initialization finishes
- Return a specific error code (not generic Internal error) to help clients implement retry logic
Related
This issue affects any ACP client that follows the standard protocol flow of calling initialize then immediately calling session/new. The current behavior breaks ACP compatibility.
Bug:
session/newfails with Internal error when called immediately afterinitializein ACP stdio modeDescription
When using OpenCode as an ACP (Agent Client Protocol) agent via stdio, the
session/newmethod fails with "Internal error" (code -32603) if called immediately afterinitializereturns.Steps to Reproduce
Expected Behavior
session/newshould succeed afterinitializereturns successfully.Actual Behavior
{"jsonrpc":"2.0","id":2,"error":{"code":-32603,"message":"Internal error","data":{}}}Root Cause Analysis
From the logs, OpenCode's
initializeresponse is sent before internal initialization is fully complete. The server needs additional time (~2 seconds) to:When
session/newarrives during this initialization window, it fails because the required services aren't ready yet.Workaround
Adding a 2-second delay between
initializeresponse andsession/newrequest resolves the issue:Testing Results
Environment
model: "bailian-coding-plan/glm-5"with custom providersSuggested Fix
One of these approaches would resolve the issue:
initializeresponse until all internal initialization is completeRelated
This issue affects any ACP client that follows the standard protocol flow of calling
initializethen immediately callingsession/new. The current behavior breaks ACP compatibility.