Skip to content

fix: clear CLAUDECODE env to prevent nested-session detection#413

Open
cyyij wants to merge 1 commit intozed-industries:mainfrom
cyyij:fix/clear-claudecode-env
Open

fix: clear CLAUDECODE env to prevent nested-session detection#413
cyyij wants to merge 1 commit intozed-industries:mainfrom
cyyij:fix/clear-claudecode-env

Conversation

@cyyij
Copy link

@cyyij cyyij commented Mar 12, 2026

Problem

When claude-agent-acp is spawned from within an existing Claude Code session (e.g. via acpx or other agent orchestrators), the parent process sets CLAUDECODE=1. This environment variable is inherited by the spawned subprocess, causing Claude Code to detect a "nested session" and exit immediately during initialization.

The result is a generic -32603 Internal error with data.details: "Query closed before response received" on every session/new call. This affects both exec (one-shot) and persistent session modes.

Reproduction:

# From inside a Claude Code session:
acpx claude exec 'hello'
# -> [error] RUNTIME: Internal error

Root Cause

CLAUDECODE=1 is set by Claude Code in its shell environment. When claude-agent-acp spawns a new Claude Code subprocess via the SDK's query(), it passes ...process.env which includes CLAUDECODE=1. The child process sees this and refuses to start (nested-session protection).

Fix

  1. Clear CLAUDECODE in subprocess env — Set CLAUDECODE: "" in the env object passed to query(), so the spawned instance starts normally. This mirrors what other Claude Code wrappers do (e.g. cmux's wrapper script uses unset CLAUDECODE).

  2. Improve error handling for session/new — The v0.20.0 fix for "Query closed before response received" (PR fix: throw resourceNotFound when loadSession fails to resume #363) only covered the loadSession path (creationOpts.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.

Testing

Verified on macOS arm64 with:

  • acpx 0.3.0
  • Claude Code CLI 2.1.74
  • Node v25.8.0

Before fix: acpx claude exec always fails with Internal error when run from within Claude Code.
After fix: works correctly, Claude responds normally.

@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

We require contributors to sign our Contributor License Agreement, and we don't have @sunnyyi-mac on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

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 <noreply@anthropic.com>
@cyyij cyyij force-pushed the fix/clear-claudecode-env branch from e88667d to 4fd2c15 Compare March 12, 2026 21:32
@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

We require contributors to sign our Contributor License Agreement, and we don't have @cyyij on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cyyij
Copy link
Author

cyyij commented Mar 12, 2026

@cla-bot check

@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

We require contributors to sign our Contributor License Agreement, and we don't have @cyyij on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

The cla-bot has been summoned, and re-checked this pull request!

@cyyij
Copy link
Author

cyyij commented Mar 12, 2026

@cla-bot check

@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

We require contributors to sign our Contributor License Agreement, and we don't have @cyyij on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot
Copy link

cla-bot bot commented Mar 12, 2026

The cla-bot has been summoned, and re-checked this pull request!

@cyyij
Copy link
Author

cyyij commented Mar 13, 2026

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label Mar 13, 2026
@cla-bot
Copy link

cla-bot bot commented Mar 13, 2026

The cla-bot has been summoned, and re-checked this pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant