Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(channel): resolve the sessions directory from $HOME before homedir
Jack Rudenko1d87bfd7296d
diff --git a/packages/cli/src/channel/home-dir.test.ts b/packages/cli/src/channel/home-dir.test.tsnew file mode 100644index 00000000..2b4c91ea--- /dev/null+++ b/packages/cli/src/channel/home-dir.test.ts@@ -0,0 +1,79 @@+/**+ * The sessions directory and Claude Code's config directory follow `$HOME`,+ * by the same rule the magus plugin monitor uses to find the directory it+ * polls. Bun's `os.homedir()` ignores a runtime `HOME`, which is how claudish+ * wrote into the account home while the monitor watched a sandbox.+ *+ * Nothing here writes: the manager's resolved directory is read back, never+ * used, so a regression cannot touch the real ~/.claudish.+ */++import { afterEach, beforeEach, describe, expect, test } from "bun:test";+import { join } from "node:path";++import { sessionsDirFrom, userHomeFrom } from "./home-dir.js";+import { claudeConfigDir } from "./parent-proof.js";+import { SessionManager } from "./session-manager.js";++const SANDBOX_HOME = "/sandbox/home-dir-test";+const ACCOUNT_HOME = "/Users/account";+const accountHome = () => ACCOUNT_HOME;++describe("sessionsDirFrom / userHomeFrom", () => {+ test("CLAUDISH_SESSIONS_DIR wins over HOME", () => {+ expect(+ sessionsDirFrom({ CLAUDISH_SESSIONS_DIR: "/explicit", HOME: SANDBOX_HOME }, accountHome)+ ).toBe("/explicit");+ });++ test("HOME wins over os.homedir()", () => {+ expect(sessionsDirFrom({ HOME: SANDBOX_HOME }, accountHome)).toBe(+ join(SANDBOX_HOME, ".claudish", "sessions")+ );+ expect(userHomeFrom({ HOME: SANDBOX_HOME }, accountHome)).toBe(SANDBOX_HOME);+ });++ test("an empty CLAUDISH_SESSIONS_DIR or HOME counts as unset", () => {+ expect(sessionsDirFrom({ CLAUDISH_SESSIONS_DIR: "", HOME: "" }, accountHome)).toBe(+ join(ACCOUNT_HOME, ".claudish", "sessions")+ );+ expect(userHomeFrom({}, accountHome)).toBe(ACCOUNT_HOME);+ });+});++describe("claudish follows a runtime HOME", () => {+ const saved = {+ HOME: process.env.HOME,+ CLAUDISH_SESSIONS_DIR: process.env.CLAUDISH_SESSIONS_DIR,+ };++ beforeEach(() => {+ process.env.HOME = SANDBOX_HOME;+ delete process.env.CLAUDISH_SESSIONS_DIR;+ });++ afterEach(() => {+ for (const [key, value] of Object.entries(saved)) {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+ }+ });++ test("SessionManager resolves its sessions directory under $HOME", async () => {+ const manager = new SessionManager({ hostPid: 1 });+ try {+ const resolved = (manager as unknown as { sessionsDir: string }).sessionsDir;+ expect(resolved).toBe(join(SANDBOX_HOME, ".claudish", "sessions"));+ } finally {+ await manager.shutdownAll();+ }+ });++ test("the parent proof reads Claude Code's config under $HOME", () => {+ expect(claudeConfigDir({ HOME: SANDBOX_HOME })).toBe(join(SANDBOX_HOME, ".claude"));+ });++ test("CLAUDE_CONFIG_DIR still wins over HOME", () => {+ expect(claudeConfigDir({ HOME: SANDBOX_HOME, CLAUDE_CONFIG_DIR: "/cfg" })).toBe("/cfg");+ });+});diff --git a/packages/cli/src/channel/home-dir.ts b/packages/cli/src/channel/home-dir.tsnew file mode 100644index 00000000..8a4e8b37--- /dev/null+++ b/packages/cli/src/channel/home-dir.ts@@ -0,0 +1,33 @@+// ─── Where the sessions directory and Claude Code's config live ──────────────+//+// ONE rule, shared with the magus `claudish` plugin monitor+// (`plugins/claudish/scripts/session-monitor.ts` `sessionsDirFrom`), which+// polls the directory this process writes. If the two resolve it differently,+// every run is written where the monitor is not looking and nothing errors.+//+// The home directory is `$HOME` first, `os.homedir()` only when HOME is unset+// or empty. Bun's `os.homedir()` ignores a `HOME` changed at runtime (it reads+// the account's home from the password database), so a sandbox or launcher+// that sets HOME — madbench sets `HOME=<workDir>` — sent claudish to the real+// account home while the monitor, reading `$HOME`, watched the sandbox.++import { homedir } from "node:os";+import { join } from "node:path";++type Env = Record<string, string | undefined>;++/** `$HOME` when set and non-empty, else `os.homedir()`. */+export function userHomeFrom(env: Env, homedirFn: () => string = homedir): string {+ const home = env.HOME;+ return home && home.length > 0 ? home : homedirFn();+}++/**+ * `CLAUDISH_SESSIONS_DIR` when set and non-empty, else+ * `<home>/.claudish/sessions` with `<home>` from `userHomeFrom`.+ */+export function sessionsDirFrom(env: Env, homedirFn: () => string = homedir): string {+ const explicit = env.CLAUDISH_SESSIONS_DIR;+ if (explicit && explicit.length > 0) return explicit;+ return join(userHomeFrom(env, homedirFn), ".claudish", "sessions");+}diff --git a/packages/cli/src/channel/parent-proof.ts b/packages/cli/src/channel/parent-proof.tsindex 9ffef005..ce9fba12 100644--- a/packages/cli/src/channel/parent-proof.ts+++ b/packages/cli/src/channel/parent-proof.ts@@ -20,9 +20,10 @@// degrades to "absent", never to a wrong answer.import * as fsPromises from "node:fs/promises";-import { homedir } from "node:os";import { join } from "node:path";+import { userHomeFrom } from "./home-dir.js";+/** What a Claude Code session id or tool-use id may look like. Also the path-safety gate. */export const CLAUDE_ID_RE = /^[A-Za-z0-9_-]{8,128}$/;@@ -104,10 +105,14 @@ export function parentSessionIdFrom(env: Record<string, string | undefined>): streturn id;}-/** `CLAUDE_CONFIG_DIR`, else `~/.claude`. */+/**+ * `CLAUDE_CONFIG_DIR`, else `<home>/.claude`, with `<home>` resolved by the+ * same `$HOME`-first rule as the sessions directory (home-dir.ts): Claude Code+ * follows `$HOME`, and Bun's `os.homedir()` does not.+ */export function claudeConfigDir(env: Record<string, string | undefined>): string {const fromEnv = env.CLAUDE_CONFIG_DIR;- return fromEnv && fromEnv.length > 0 ? fromEnv : join(homedir(), ".claude");+ return fromEnv && fromEnv.length > 0 ? fromEnv : join(userHomeFrom(env), ".claude");}/** The subset of `node:fs/promises` the proof uses. Injectable for tests. */diff --git a/packages/cli/src/channel/session-manager.ts b/packages/cli/src/channel/session-manager.tsindex e7704865..e071edf7 100644--- a/packages/cli/src/channel/session-manager.ts+++ b/packages/cli/src/channel/session-manager.ts@@ -36,7 +36,6 @@ import {statSync,writeFileSync,} from "node:fs";-import { homedir } from "node:os";import { join, resolve, sep } from "node:path";import { StringDecoder } from "node:string_decoder";@@ -54,6 +53,7 @@ import {meaningfulStderr,} from "../team-orchestrator.js";import { readTokenStatsAt } from "../team-stats.js";+import { sessionsDirFrom } from "./home-dir.js";import { hostPidFrom } from "./parent-proof.js";import { ScrollbackBuffer } from "./scrollback-buffer.js";import {@@ -793,10 +793,9 @@ export class SessionManager {this.maxSessions = options?.maxSessions ?? DEFAULT_MAX_SESSIONS;this.scrollbackCapacity = options?.scrollbackCapacity ?? DEFAULT_SCROLLBACK;this.terminalRetentionMs = options?.terminalRetentionMs ?? TERMINAL_RETENTION_MS;- this.sessionsDir =- options?.sessionsDir ??- process.env.CLAUDISH_SESSIONS_DIR ??- join(homedir(), ".claudish", "sessions");+ // `$HOME` before `os.homedir()`, by the rule the plugin monitor shares —+ // see home-dir.ts. Diverging here makes every run invisible to it.+ this.sessionsDir = options?.sessionsDir ?? sessionsDirFrom(process.env);this.stallSeconds = options?.stallSeconds;this.onStateChange = options?.onStateChange;}diff --git a/packages/cli/src/mcp-e2e/runner.ts b/packages/cli/src/mcp-e2e/runner.tsindex af14aff8..63b18360 100644--- a/packages/cli/src/mcp-e2e/runner.ts+++ b/packages/cli/src/mcp-e2e/runner.ts@@ -29,8 +29,8 @@ import {unlinkSync,writeFileSync,} from "node:fs";-import { homedir } from "node:os";import { join, resolve } from "node:path";+import { userHomeFrom } from "../channel/home-dir.js";import { redactSecrets } from "../redact.js";import { REAL_CONFIG_PATH, buildArmConfig, buildArmEnv, checkPreconditions } from "./env.js";import { driveServer } from "./jsonrpc-client.js";@@ -40,7 +40,10 @@ import type { Observation, ObservationView, Scenario, SessionLog, Verdict } fromconst REPO_ROOT = resolve(import.meta.dir, "../../../..");const SERVER_ENTRY = resolve(REPO_ROOT, "packages/cli/src/index.ts");const LOG_ROOT = resolve(REPO_ROOT, "logs/mcp-e2e");-const SESSIONS_DIR = join(homedir(), ".claudish", "sessions");+// Where the server under test writes: env.ts passes HOME through and strips+// CLAUDISH_SESSIONS_DIR, so the server resolves `$HOME/.claudish/sessions` by+// the shared rule in channel/home-dir.ts. `os.homedir()` would ignore HOME.+const SESSIONS_DIR = join(userHomeFrom(process.env), ".claudish", "sessions");// ── args ─────────────────────────────────────────────────────────────────────