Strip CLAUDECODE from the Claude Code subprocess env - #956
Open
jhampton wants to merge 1 commit into
Open
Conversation
The Claude Code CLI refuses to start when the CLAUDECODE environment variable is set, treating it as an attempt to nest sessions. When the host process is itself launched from inside a Claude Code session it inherits CLAUDECODE, which causes buildClaudeSubprocessEnv() to hand the spawned CLI a poisoned environment and the subprocess never comes up. Delete CLAUDECODE from the subprocess env so standalone spawns are unaffected by the host's session context. Add a regression test covering the CLAUDECODE strip alongside the existing Bedrock routing strips. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Claude Code CLI refuses to start when the
CLAUDECODEenvironment variable is set, treating it as an attempt to nest a session inside another Claude Code session. When the host process that callsbuildClaudeSubprocessEnv()is itself launched from inside a Claude Code session, it inheritsCLAUDECODEfrom its own environment. That value is then spread into the spawned CLI's env, so the subprocess seesCLAUDECODEset, interprets it as a nested-session attempt, and never comes up — the SDK transport gets no valid handshake.This affects anyone running the app from inside a Claude Code session (a common development and automation setup).
Fix
Delete
CLAUDECODEfrom the subprocess environment inbuildClaudeSubprocessEnv(), right after the base environment is assembled and before it is handed to the SDK. The host's own session context no longer leaks into spawned CLIs; standalone spawns are unaffected.The change is one line plus an explanatory comment, adjacent to the existing Bedrock-routing strips in the same function.
Test plan
Adds
packages/shared/tests/claude-subprocess-env.test.ts:strips CLAUDECODE from the subprocess env— setsprocess.env.CLAUDECODEand asserts the built env omits it.strips Claude-specific Bedrock routing vars— regression guard for the existing Bedrock strips (previously untested).preserves unrelated environment variables— confirms the strip is surgical andenvOverridespass through.Validation:
bun test tests/claude-subprocess-env.test.tsinpackages/shared→ 3 pass, 0 fail.tsc --noEmitonpackages/shared→ 0 errors.