diff --git a/v2/sidecar/agent-runner-deno.ts b/v2/sidecar/agent-runner-deno.ts index 0808e36..6758d22 100644 --- a/v2/sidecar/agent-runner-deno.ts +++ b/v2/sidecar/agent-runner-deno.ts @@ -66,8 +66,13 @@ function handleQuery(msg: QueryMessage) { log(`Starting agent session ${sessionId}: claude ${args.join(" ")}`); - const env = { ...Deno.env.toObject() }; - delete env.CLAUDECODE; + // Strip all CLAUDE* env vars to prevent nesting detection by claude CLI + const env: Record = {}; + for (const [key, value] of Object.entries(Deno.env.toObject())) { + if (!key.startsWith("CLAUDE")) { + env[key] = value; + } + } const command = new Deno.Command("claude", { args, diff --git a/v2/sidecar/agent-runner.ts b/v2/sidecar/agent-runner.ts index b82bace..ce22c09 100644 --- a/v2/sidecar/agent-runner.ts +++ b/v2/sidecar/agent-runner.ts @@ -89,13 +89,18 @@ function handleQuery(msg: QueryMessage) { log(`Starting agent session ${sessionId}: claude ${args.join(' ')}`); + // Strip all CLAUDE* env vars to prevent nesting detection by claude CLI. + // When BTerminal is launched from a Claude Code terminal, these leak in. + const cleanEnv: Record = {}; + for (const [key, value] of Object.entries(process.env)) { + if (!key.startsWith('CLAUDE') && value !== undefined) { + cleanEnv[key] = value; + } + } + const child = spawn('claude', args, { cwd: cwd || process.cwd(), - env: { - ...process.env, - // Unset CLAUDECODE to avoid nesting detection - CLAUDECODE: undefined, - }, + env: cleanEnv, stdio: ['pipe', 'pipe', 'pipe'], });