Skip to content

Strip CLAUDECODE from the Claude Code subprocess env - #956

Open
jhampton wants to merge 1 commit into
craft-ai-agents:mainfrom
Swagatar-LLC:contrib/claudecode-env-strip
Open

Strip CLAUDECODE from the Claude Code subprocess env#956
jhampton wants to merge 1 commit into
craft-ai-agents:mainfrom
Swagatar-LLC:contrib/claudecode-env-strip

Conversation

@jhampton

@jhampton jhampton commented Jul 8, 2026

Copy link
Copy Markdown

Problem

The Claude Code CLI refuses to start when the CLAUDECODE environment variable is set, treating it as an attempt to nest a session inside another Claude Code session. When the host process that calls buildClaudeSubprocessEnv() is itself launched from inside a Claude Code session, it inherits CLAUDECODE from its own environment. That value is then spread into the spawned CLI's env, so the subprocess sees CLAUDECODE set, 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 CLAUDECODE from the subprocess environment in buildClaudeSubprocessEnv(), 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 — sets process.env.CLAUDECODE and 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 and envOverrides pass through.

Validation:

  • bun test tests/claude-subprocess-env.test.ts in packages/shared → 3 pass, 0 fail.
  • tsc --noEmit on packages/shared → 0 errors.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant