Skip to content

session/new fails with Internal error when called immediately after initialize in ACP stdio mode #2587

@yudaiyan

Description

@yudaiyan

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:

  1. Load user configuration
  2. Initialize providers
  3. 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:

  1. Delay the initialize response until all internal initialization is complete
  2. Queue early requests and process them after initialization finishes
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions