Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(runner): strip CLAUDE_CODE_CHILD_SESSION from interactive Claude Code

Jack Rudenko3bb5fee5d302
diff --git a/packages/cli/src/claude-runner-child-env.test.ts b/packages/cli/src/claude-runner-child-env.test.ts
new file mode 100644
index 00000000..f6636938
--- /dev/null
+++ b/packages/cli/src/claude-runner-child-env.test.ts
@@ -0,0 +1,42 @@
+/**
+ * What claude-runner deletes from the environment it hands Claude Code.
+ *
+ * `scrubChildEnv` is tested directly; `runClaudeWithProxy` itself cannot be run here
+ * (it spawns Claude Code), so a source guard pins that it calls the helper on the
+ * environment it spawns with, right after building it. The real-claudish pane test
+ * (Phase 3) checks the same property end to end.
+ */
+
+import { describe, expect, test } from "bun:test";
+import { readFileSync } from "node:fs";
+import { resolve } from "node:path";
+import { scrubChildEnv } from "./claude-runner.js";
+
+describe("scrubChildEnv", () => {
+ test("an interactive launch drops CLAUDE_CODE_CHILD_SESSION and CLAUDECODE", () => {
+ const env: Record<string, string | undefined> = {
+ CLAUDE_CODE_CHILD_SESSION: "1",
+ CLAUDECODE: "1",
+ KEEP: "me",
+ };
+ scrubChildEnv(env, { interactive: true });
+ expect(env).toEqual({ KEEP: "me" });
+ });
+
+ test("print mode keeps CLAUDE_CODE_CHILD_SESSION as before", () => {
+ const env: Record<string, string | undefined> = { CLAUDE_CODE_CHILD_SESSION: "1" };
+ scrubChildEnv(env, { interactive: false });
+ expect(env.CLAUDE_CODE_CHILD_SESSION).toBe("1");
+ });
+});
+
+describe("runClaudeWithProxy scrubs the environment it spawns with", () => {
+ const source = readFileSync(resolve(import.meta.dir, "claude-runner.ts"), "utf8");
+ const code = source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
+
+ test("the call follows the env literal, on that env, with config.interactive", () => {
+ const m = /const env: Record<string, string> = \{[\s\S]*?\n {2}\};\n\s*(.*)\n/.exec(code);
+ expect(m, "could not find the env literal in claude-runner.ts").toBeTruthy();
+ expect(m?.[1]).toBe("scrubChildEnv(env, { interactive: Boolean(config.interactive) });");
+ });
+});
diff --git a/packages/cli/src/claude-runner.ts b/packages/cli/src/claude-runner.ts
index 48176554..357cb3c4 100644
--- a/packages/cli/src/claude-runner.ts
+++ b/packages/cli/src/claude-runner.ts
@@ -17,7 +17,11 @@ import { isatty } from "node:tty";
import { lookupModelForProvider } from "./adapters/model-catalog.js";
import { classifierPassthroughEnabled } from "./classifier-passthrough.js";
import { ENV } from "./config.js";
-import { magmuxPaneCapability, planMagmuxWrap } from "./launcher/magmux-wrapper.js";
+import {
+ STRIPPED_CHILD_VARS,
+ magmuxPaneCapability,
+ planMagmuxWrap,
+} from "./launcher/magmux-wrapper.js";
// Aliased: runClaudeWithProxy declares its own local `log` (a quiet-aware
// console printer), and an unaliased import would be shadowed inside it.
import { log as debugLog, logStderr } from "./logger.js";
@@ -964,6 +968,26 @@ export function createTempSettingsFile(
return { path: tempPath, statusLine, tokenFilePath };
}
+/**
+ * Delete, from the environment built for Claude Code, the inherited variables that
+ * must not reach it. Called right after that environment is assembled.
+ *
+ * Interactive launches lose every `STRIPPED_CHILD_VARS` key. `CLAUDE_CODE_CHILD_SESSION`
+ * is set by Claude Code for its Bash-tool children; a claudish started from one (a
+ * user in a tool shell, a `team --grid` pane, an MCP pane) passed it on, and an
+ * interactive Claude Code that inherits it prints "Transcript saving is off" and
+ * writes no transcript. Only the magmux-wrap path stripped it before. Print mode is
+ * left as it was.
+ */
+export function scrubChildEnv(
+ env: Record<string, string | undefined>,
+ opts: { interactive: boolean }
+): void {
+ if (opts.interactive) {
+ for (const key of STRIPPED_CHILD_VARS) delete env[key];
+ }
+}
+
/**
* Build the claudish `--settings` overlay object. This loads at the CLI-args precedence
* tier, above the user/project/local settings files, so keys here override those three.
@@ -1643,6 +1667,7 @@ export async function runClaudeWithProxy(
// above then forwards the user's value untouched.
...advisorToolEnv.vars,
};
+ scrubChildEnv(env, { interactive: Boolean(config.interactive) });
// Can this launch put a recovery banner on screen at all? Asked HERE, before
// the child environment is finalised, because the answer gates the watchdog