Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(pty): agents no longer inherit the parent Claude session's identity

Gastón Péchieue9a2310c927e
diff --git a/src/main/pty.ts b/src/main/pty.ts
index b5e6c4fb..5cdaa119 100644
--- a/src/main/pty.ts
+++ b/src/main/pty.ts
@@ -5,6 +5,7 @@ import { delimiter, join, win32 } from 'node:path';
import { spawnSync } from 'node:child_process';
import { ensureKilled } from './procKill';
import { expandTilde } from './fs';
+import { buildPtyEnv } from './ptyEnv';
import { captureFromLoginShell, userShellPath } from './shellEnv';
/** APPEND the hive's bundled-node dir (`<HIVE_ROOT>/bin/runtime`, which holds a
@@ -634,53 +635,15 @@ export class PtyManager {
);
}
}
- const childEnv = {
- ...process.env,
- PATH: userPath,
- TERM: 'xterm-256color',
- COLORTERM: 'truecolor',
- // Help apps that look for a real interactive shell
- FORCE_COLOR: '1',
- // A Finder/Dock-launched Electron app inherits NO locale from launchd
- // (`launchctl getenv LANG` is empty), so without this every child runs in
- // the C/POSIX locale — where macOS's CoreFoundation default text encoding
- // is Mac OS Roman (__CF_USER_TEXT_ENCODING=<uid>:0:0). Any locale-sensitive
- // tool an agent runs then decodes UTF-8 as MacRoman and paints mojibake
- // into the grid ("—" → "‚Äî"), which copy faithfully reproduces. This
- // terminal IS UTF-8 (xterm.js + Unicode11), so say so.
- //
- // LC_CTYPE only, deliberately: it is the character-encoding category. Using
- // LC_ALL would also override collation and date formatting for every user
- // who never exported a locale. A locale the user really did export wins.
- ...(process.platform === 'win32'
- ? {}
- : { LANG: process.env.LANG ?? 'en_US.UTF-8',
- LC_CTYPE: process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? 'en_US.UTF-8' }),
- // Per-agent hive identity (AGENT_ID, HIVE_ROOT, …) when provided.
- ...(opts.env ?? {})
- } as Record<string, string>;
- // The app itself is often launched from INSIDE a Claude Code session
- // (`npm run dev` typed into a claude terminal), so that session's
- // identity markers arrive via process.env and would flow into every
- // agent CLI. CLAUDE_CODE_CHILD_SESSION makes the agent believe it is a
- // child session and silently DISABLES transcript saving, which breaks
- // --resume for every agent of that run: their sessions never reach
- // disk. The session id / messaging socket+token likewise belong to the
- // PARENT session, never to a fresh agent. Agents are top-level
- // sessions regardless of how the app was launched.
- for (const k of [
- 'CLAUDE_CODE_CHILD_SESSION',
- 'CLAUDE_CODE_SESSION_ID',
- 'CLAUDE_PID',
- 'CLAUDE_CODE_MESSAGING_SOCKET',
- 'CLAUDE_CODE_MESSAGING_TOKEN'
- ]) delete childEnv[k];
const proc = pty.spawn(file, spawnArgs, {
name: 'xterm-256color',
cols: opts.cols ?? 100,
rows: opts.rows ?? 30,
cwd: opts.cwd,
- env: childEnv
+ // Inherited env minus the parent Claude session's identity markers,
+ // then the app's defaults and locale, then per-agent values — see
+ // ptyEnv.ts for why the strip exists and why it is prefix-based.
+ env: buildPtyEnv(process.env, userPath, opts.env)
});
// Capture THIS session object so the proc's callbacks can tell whether the
diff --git a/src/main/ptyEnv.ts b/src/main/ptyEnv.ts
new file mode 100644
index 00000000..46ba50d2
--- /dev/null
+++ b/src/main/ptyEnv.ts
@@ -0,0 +1,90 @@
+/**
+ * Environment construction for agent PTYs, as a pure function so the layering
+ * is testable off-process (the same trick `buildMissingCliScript` uses for its
+ * `platform` parameter).
+ *
+ * The layering rule, bottom to top:
+ * 1. the inherited environment, minus the parent Claude session's identity
+ * 2. the app's own defaults (PATH, terminal identity, locale)
+ * 3. per-agent values (`opts.env`) — always win, even over the strip below
+ */
+
+/**
+ * The app is often launched from INSIDE a Claude Code session (`npm run dev`
+ * typed into a claude terminal), so that session's identity markers arrive via
+ * `process.env` and would flow into every agent CLI. CLAUDE_CODE_CHILD_SESSION
+ * makes the agent believe it is a child session and silently DISABLES
+ * transcript saving ("Transcript saving is off — inherited
+ * CLAUDE_CODE_CHILD_SESSION marker"), which breaks --resume for every agent of
+ * that run: their sessions never reach disk (bit us live 2026-08-16/17 — no
+ * worker transcript ever existed). The session id, pid, messaging socket+token,
+ * effort, execpath and entrypoint likewise all describe the PARENT session,
+ * never a fresh agent.
+ *
+ * Stripped by PREFIX rather than by name: the CLI grows new markers faster
+ * than a hardcoded list keeps up (a five-name list was already seven short of
+ * a live session's dump when review caught it). Agents are top-level sessions
+ * regardless of how the app was launched, so they inherit NONE of the parent's
+ * Claude identity.
+ */
+const CLAUDE_MARKER_RE = /^CLAUDE(CODE|_)/;
+
+/**
+ * Configuration, not identity: these share the prefix but are the OPERATOR's
+ * own choices — where the CLI keeps its config, how it authenticates, which
+ * backend serves it. An operator who exported them wants agents to see them,
+ * and stripping them breaks agents in exactly the quiet way the strip above
+ * exists to prevent. Everything session-scoped stays out of this list.
+ */
+const CLAUDE_CONFIG_KEEP = new Set([
+ 'CLAUDE_CONFIG_DIR',
+ 'CLAUDE_CODE_OAUTH_TOKEN',
+ 'CLAUDE_CODE_USE_BEDROCK',
+ 'CLAUDE_CODE_USE_VERTEX'
+]);
+
+export function buildPtyEnv(
+ parentEnv: NodeJS.ProcessEnv,
+ userPath: string,
+ agentEnv?: Record<string, string>,
+ platform: NodeJS.Platform = process.platform
+): Record<string, string> {
+ // Layer 1 — inherit, minus the parent session's Claude identity. Only this
+ // layer is stripped: a marker set deliberately via `agentEnv` below survives,
+ // so per-agent environment overrides (and future per-agent env features)
+ // cannot be silently wiped by the strip.
+ const inherited: Record<string, string> = {};
+ for (const [k, v] of Object.entries(parentEnv)) {
+ if (v === undefined) continue;
+ if (CLAUDE_MARKER_RE.test(k) && !CLAUDE_CONFIG_KEEP.has(k)) continue;
+ inherited[k] = v;
+ }
+ return {
+ ...inherited,
+ PATH: userPath,
+ TERM: 'xterm-256color',
+ COLORTERM: 'truecolor',
+ // Help apps that look for a real interactive shell
+ FORCE_COLOR: '1',
+ // A Finder/Dock-launched Electron app inherits NO locale from launchd
+ // (`launchctl getenv LANG` is empty), so without this every child runs in
+ // the C/POSIX locale — where macOS's CoreFoundation default text encoding
+ // is Mac OS Roman (__CF_USER_TEXT_ENCODING=<uid>:0:0). Any locale-sensitive
+ // tool an agent runs then decodes UTF-8 as MacRoman and paints mojibake
+ // into the grid ("—" → "‚Äî"), which copy faithfully reproduces. This
+ // terminal IS UTF-8 (xterm.js + Unicode11), so say so.
+ //
+ // LC_CTYPE only, deliberately: it is the character-encoding category. Using
+ // LC_ALL would also override collation and date formatting for every user
+ // who never exported a locale. A locale the user really did export wins.
+ ...(platform === 'win32'
+ ? {}
+ : {
+ LANG: parentEnv.LANG ?? 'en_US.UTF-8',
+ LC_CTYPE:
+ parentEnv.LC_ALL ?? parentEnv.LC_CTYPE ?? parentEnv.LANG ?? 'en_US.UTF-8'
+ }),
+ // Per-agent hive identity (AGENT_ID, HIVE_ROOT, …) when provided.
+ ...(agentEnv ?? {})
+ };
+}
diff --git a/test/pty-env.test.cjs b/test/pty-env.test.cjs
new file mode 100644
index 00000000..f5922cd1
--- /dev/null
+++ b/test/pty-env.test.cjs
@@ -0,0 +1,111 @@
+'use strict';
+
+/**
+ * The app is often launched from inside a Claude Code session, and the parent
+ * session's identity markers used to flow into every agent PTY. One of them
+ * (CLAUDE_CODE_CHILD_SESSION) silently disables transcript saving, which broke
+ * --resume for every agent of a run — invisible until someone needed a resume.
+ * These tests pin the layering rule: inherited env is stripped of the parent's
+ * Claude identity by PREFIX, config-not-identity names survive, and per-agent
+ * values always win.
+ */
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const loadTs = require('./load-ts.cjs');
+
+const { buildPtyEnv } = loadTs('src/main/ptyEnv.ts');
+
+/** The twelve markers dumped from a live Claude Code session in review of the
+ * fix — the original hardcoded list caught only the first five. */
+const LIVE_SESSION_MARKERS = {
+ CLAUDE_CODE_CHILD_SESSION: 'true',
+ CLAUDE_CODE_SESSION_ID: 'abc-123',
+ CLAUDE_PID: '4242',
+ CLAUDE_CODE_MESSAGING_SOCKET: '/tmp/claude.sock',
+ CLAUDE_CODE_MESSAGING_TOKEN: 'tok',
+ CLAUDE_CODE_FORCE_SESSION_PERSISTENCE: '1',
+ CLAUDE_EFFORT: 'high',
+ CLAUDE_CODE_EXECPATH: '/usr/local/bin/claude',
+ CLAUDECODE: '1',
+ CLAUDE_CODE_ENTRYPOINT: 'cli',
+ CLAUDE_CODE_ENABLE_TELEMETRY: '1',
+ CLAUDE_REMOTE_CONTROL_SESSION_NAME_PREFIX: 'rc'
+};
+
+test('every live-session identity marker is stripped from the inherited env', () => {
+ const env = buildPtyEnv({ HOME: '/Users/x', ...LIVE_SESSION_MARKERS }, '/bin', undefined, 'darwin');
+ for (const k of Object.keys(LIVE_SESSION_MARKERS)) {
+ assert.ok(!(k in env), `${k} must not leak into an agent PTY`);
+ }
+ assert.equal(env.HOME, '/Users/x');
+});
+
+test('markers the CLI has not invented yet are stripped by the prefix rule', () => {
+ const env = buildPtyEnv(
+ { CLAUDE_CODE_SOME_FUTURE_THING: 'x', CLAUDE_NEXT_YEAR: 'y' },
+ '/bin', undefined, 'darwin'
+ );
+ assert.ok(!('CLAUDE_CODE_SOME_FUTURE_THING' in env));
+ assert.ok(!('CLAUDE_NEXT_YEAR' in env));
+});
+
+test('operator configuration sharing the prefix survives: config dir, auth, backend', () => {
+ const env = buildPtyEnv(
+ {
+ CLAUDE_CONFIG_DIR: '/Users/x/.claude-alt',
+ CLAUDE_CODE_OAUTH_TOKEN: 'oauth',
+ CLAUDE_CODE_USE_BEDROCK: '1',
+ CLAUDE_CODE_USE_VERTEX: '1',
+ ...LIVE_SESSION_MARKERS
+ },
+ '/bin', undefined, 'darwin'
+ );
+ assert.equal(env.CLAUDE_CONFIG_DIR, '/Users/x/.claude-alt');
+ assert.equal(env.CLAUDE_CODE_OAUTH_TOKEN, 'oauth');
+ assert.equal(env.CLAUDE_CODE_USE_BEDROCK, '1');
+ assert.equal(env.CLAUDE_CODE_USE_VERTEX, '1');
+ assert.ok(!('CLAUDE_CODE_SESSION_ID' in env), 'keep-list must not weaken the strip');
+});
+
+test('names that merely start with CLAUDE are not the prefix and survive', () => {
+ const env = buildPtyEnv({ CLAUDES_HOUSE: 'blue', ANTHROPIC_API_KEY: 'k' }, '/bin', undefined, 'darwin');
+ assert.equal(env.CLAUDES_HOUSE, 'blue');
+ assert.equal(env.ANTHROPIC_API_KEY, 'k');
+});
+
+test('per-agent env wins over the strip AND over the defaults', () => {
+ const env = buildPtyEnv(
+ LIVE_SESSION_MARKERS,
+ '/bin',
+ { CLAUDE_CODE_SESSION_ID: 'deliberate', TERM: 'vt100', AGENT_ID: 'a1' },
+ 'darwin'
+ );
+ // A marker set on purpose by the app (or a future per-agent env feature) is
+ // NOT wiped — only the inherited layer is stripped.
+ assert.equal(env.CLAUDE_CODE_SESSION_ID, 'deliberate');
+ assert.equal(env.TERM, 'vt100');
+ assert.equal(env.AGENT_ID, 'a1');
+});
+
+test('app defaults land: PATH, terminal identity, color', () => {
+ const env = buildPtyEnv({ PATH: '/stale' }, '/resolved/bin', undefined, 'darwin');
+ assert.equal(env.PATH, '/resolved/bin');
+ assert.equal(env.TERM, 'xterm-256color');
+ assert.equal(env.COLORTERM, 'truecolor');
+ assert.equal(env.FORCE_COLOR, '1');
+});
+
+test('locale: UTF-8 defaults on darwin, the user\'s exported locale wins, win32 untouched', () => {
+ const bare = buildPtyEnv({}, '/bin', undefined, 'darwin');
+ assert.equal(bare.LANG, 'en_US.UTF-8');
+ assert.equal(bare.LC_CTYPE, 'en_US.UTF-8');
+
+ const exported = buildPtyEnv({ LANG: 'es_ES.UTF-8', LC_ALL: 'fr_FR.UTF-8' }, '/bin', undefined, 'linux');
+ assert.equal(exported.LANG, 'es_ES.UTF-8');
+ assert.equal(exported.LC_CTYPE, 'fr_FR.UTF-8');
+
+ const win = buildPtyEnv({}, 'C:\\bin', undefined, 'win32');
+ assert.ok(!('LANG' in win));
+ assert.ok(!('LC_CTYPE' in win));
+});