fix(sdk): strip nested-session env vars before SDK query#41
Open
MateusJuni0 wants to merge 1 commit intoearlyaidopters:mainfrom
Open
fix(sdk): strip nested-session env vars before SDK query#41MateusJuni0 wants to merge 1 commit intoearlyaidopters:mainfrom
MateusJuni0 wants to merge 1 commit intoearlyaidopters:mainfrom
Conversation
When ClaudeClaw runs inside or downstream of another Claude Code session (common with PM2/systemd services started from an interactive CC terminal), the inherited env contains CLAUDECODE=1 plus other CLAUDE_CODE_* markers. The vendored CLI in claude-agent-sdk aborts with "Claude Code cannot be launched inside another Claude Code session" and the bot replies "subprocess failed to start" for every message. Strip 7 nested-session marker vars from sdkEnv before passing it to query() so the SDK subprocess runs clean. Repro: start PM2 from inside a Claude Code terminal, send any message to the bot; logs show exit code 1 at sdk.mjs. After this fix: bot responds normally.
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
When ClaudeClaw is launched from inside an active Claude Code session (e.g. spawned by PM2 from a shell where Claude Code was running, or invoked as a child of another CC session), the vendored CLI aborts with:
This is because
@anthropic-ai/claude-agent-sdk'squery()inheritsprocess.env, which includes markers that the CLI reads to detect nested sessions:CLAUDECODECLAUDE_CODE_ENTRYPOINTCLAUDE_CODE_EXECPATHCLAUDE_AGENT_SDK_VERSIONCLAUDE_CODE_SDK_HAS_OAUTH_REFRESHCLAUDE_CODE_PROVIDER_MANAGED_BY_HOSTCLAUDE_INTERNAL_FC_OVERRIDESSince ClaudeClaw is itself an orchestrator that spawns the SDK CLI, those markers are stale — they describe the parent shell, not the ClaudeClaw runtime.
Fix
Build a sanitized
sdkEnvby filtering those keys out ofprocess.envbefore passing toquery(). Small, surgical change; no behavior change for normal (non-nested) launches.Test plan
Context
Found while deploying ClaudeClaw v1.1.0 as a Telegram bot (`@timecm_BOT`) on a Windows workstation where Claude Code was the parent shell. Root cause confirmed by inspecting the vendored `node_modules/@anthropic-ai/claude-code/cli.js` nested-session check.