From 4fd2c1537590cf079f744cb7d626ee78c2d09b87 Mon Sep 17 00:00:00 2001 From: cyyij Date: Fri, 13 Mar 2026 05:23:58 +0800 Subject: [PATCH] fix: clear CLAUDECODE env to prevent nested-session detection When claude-agent-acp is spawned from within a Claude Code session (e.g. via acpx or other orchestrators), the parent process sets CLAUDECODE=1. This env var is inherited by the subprocess, causing Claude Code to detect a "nested session" and exit immediately. The result is a generic -32603 "Internal error" with details "Query closed before response received" on every session/new call. Fix: clear CLAUDECODE in the subprocess environment so the spawned Claude Code instance starts normally. Also improve error handling for session/new: the v0.20.0 fix for "Query closed before response received" only covered the loadSession path (resume). The same error on newSession was left unhandled, producing an opaque "Internal error". Now it returns a meaningful message suggesting to check Claude Code installation. Co-Authored-By: Claude Opus 4.6 --- src/acp-agent.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/acp-agent.ts b/src/acp-agent.ts index 04badb0d..40375e6f 100644 --- a/src/acp-agent.ts +++ b/src/acp-agent.ts @@ -1227,6 +1227,7 @@ export class ClaudeAcpAgent implements Agent { ...userProvidedOptions, env: { ...process.env, + CLAUDECODE: "", // Prevent nested-session detection when spawned from within Claude Code ...userProvidedOptions?.env, ...createEnvForGateway(this.gatewayAuthMeta), }, @@ -1303,11 +1304,19 @@ export class ClaudeAcpAgent implements Agent { initializationResult = await q.initializationResult(); } catch (error) { if ( - creationOpts.resume && error instanceof Error && error.message === "Query closed before response received" ) { - throw RequestError.resourceNotFound(sessionId); + if (creationOpts.resume) { + throw RequestError.resourceNotFound(sessionId); + } + // For new sessions, provide a meaningful error instead of generic "Internal error" + throw RequestError.internalError( + undefined, + "Claude Code process exited before initialization completed. " + + "Ensure Claude Code is installed and accessible. " + + "You can set CLAUDE_CODE_EXECUTABLE to the path of the Claude CLI.", + ); } throw error; }