fix: filter CLAUDECODE from subprocess environment#594
Open
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
Open
fix: filter CLAUDECODE from subprocess environment#594gspeter-max wants to merge 1 commit intoanthropics:mainfrom
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
Conversation
ROOT CAUSE: CLAUDECODE=1 environment variable leaks into child subprocess when SDK is used from within Claude Code (hooks, plugins, subagents). Child CLI detects this and rejects the session thinking it's a nested session. CHANGES: - Filter CLAUDECODE from parent environment before merging in SubprocessCLITransport.connect() - This prevents false nesting detection while allowing users to explicitly set CLAUDECODE via env option if needed - Add test_claudecode_env_var_is_filtered to verify CLAUDECODE is filtered - Add test_claudecode_can_be_explicitly_set to verify user override capability IMPACT: - Enables SDK usage from within Claude Code sessions (hooks, plugins, subagents) - Fixes primary use case for the SDK - Unblocks hook-based workflows (guardrails, prompt injection detection, etc.) TECHNICAL NOTES: - CLAUDECODE=1 is a safety check in Claude Code CLI to prevent actual nesting - SDK subprocesses are isolated and safe, but the variable causes false positive - Implementation filters CLAUDECODE from os.environ before merge - Users can still override via env option (self._options.env takes precedence) - Approach C chosen: filter before merge for clarity and flexibility FILES MODIFIED: - src/claude_agent_sdk/_internal/transport/subprocess_cli.py - tests/test_transport.py Fixes anthropics#573 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Fixes #573
Root Cause
CLAUDECODE=1 environment variable leaks into child subprocess when SDK is used from within Claude Code (hooks, plugins, subagents). Child CLI detects this and rejects the session thinking it's a nested session.
Changes
SubprocessCLITransport.connect()envoption if neededtest_claudecode_env_var_is_filteredto verify CLAUDECODE is filteredtest_claudecode_can_be_explicitly_setto verify user override capabilityImpact
Technical Notes
os.environbefore mergeenvoption (user-provided env takes precedence)Testing
test_claudecode_env_var_is_filteredtest_claudecode_can_be_explicitly_setCo-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com