Skip to content

shouldFallbackToNewSession does not handle -32602 from session/load, causing crash with cursor-agent #152

@lygshanwj

Description

@lygshanwj

Problem

When using acpx with cursor-agent (Cursor IDE CLI) in prompt mode (persistent sessions), session reconnection fails with:

AcpRuntimeError: acpx exited with code 1: code=ACP_TURN_FAILED

The exec mode works fine because it always creates a new session via session/new.

Root Cause

  1. cursor-agent reports agentCapabilities.loadSession = true during ACP initialize
  2. When acpx tries to restore a previous session via session/load, cursor-agent returns JSON-RPC error -32602 (Invalid params) — its session/load implementation appears incomplete
  3. shouldFallbackToNewSession() in src/session-runtime/connect-load.ts only handles -32603 (Internal Error) for the fallback-to-new-session logic, but does not handle -32602
  4. The -32602 error propagates up uncaught, causing the process to exit with code 1

Expected Behavior

When session/load returns -32602 (Invalid params) or -32601 (Method not found), acpx should gracefully fallback to session/new instead of crashing.

Suggested Fix

The constant SESSION_CONTROL_UNSUPPORTED_ACP_CODES (containing -32601 and -32602) already exists in the codebase but is not used in shouldFallbackToNewSession. The fix is to check it before the sessionHasAgentMessages guard:

 function shouldFallbackToNewSession(error, record) {
   if (error instanceof TimeoutError || error instanceof InterruptedError) return false;
   if (isAcpResourceNotFoundError(error)) return true;
+  const acpErr = extractAcpError(error);
+  if (acpErr && SESSION_CONTROL_UNSUPPORTED_ACP_CODES.has(acpErr.code)) return true;
   if (!sessionHasAgentMessages(record)) {
     if (isAcpQueryClosedBeforeResponseError(error)) return true;
-    if (extractAcpError(error)?.code === -32603) return true;
+    if (acpErr?.code === -32603) return true;
   }
   return false;
 }

Why this works

  • SESSION_CONTROL_UNSUPPORTED_ACP_CODES = new Set([-32601, -32602]) already exists in the codebase
  • -32601 = JSON-RPC Method not found (agent doesn't support the method at all)
  • -32602 = JSON-RPC Invalid params (method exists but params validation fails — this is what cursor-agent returns for session/load)
  • Checking these codes unconditionally (not gated behind sessionHasAgentMessages) ensures that even sessions with prior agent messages can gracefully recover when the agent simply doesn't support session/load
  • As a bonus, the extractAcpError(error) call is now cached in a local variable to avoid redundant extraction

Steps to Reproduce

  1. Configure acpx with cursor-agent as the backend
  2. Run acpx cursor exec "hello"works (uses session/new)
  3. Run acpx cursor prompt and send a message — works (first run, creates new session)
  4. Run acpx cursor prompt again — crashes (tries session/load on existing session, gets -32602)

Workaround

Manually patch the compiled dist/session-*.js file to add the SESSION_CONTROL_UNSUPPORTED_ACP_CODES check, or delete ~/.acpx/sessions/* before each run (forces fresh sessions every time).

Environment

  • acpx: 0.3.0
  • cursor-agent (Cursor CLI): 2026.03.11-6dfa30c
  • OS: Windows 10
  • Node.js: v24.14.0
  • Integration: OpenClaw gateway calling acpx in prompt mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions