Skip to content

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

feat(pane): keep CLAUDISH_HOOK_INBOX and CLAUDISH_HOOK_SESSION in panes

Jack Rudenko3c5efb3e38d1
diff --git a/packages/cli/src/claude-runner-child-env.test.ts b/packages/cli/src/claude-runner-child-env.test.ts
index 538f8b1f..a5ff5396 100644
--- a/packages/cli/src/claude-runner-child-env.test.ts
+++ b/packages/cli/src/claude-runner-child-env.test.ts
@@ -40,6 +40,17 @@ describe("scrubChildEnv", () => {
scrubChildEnv(env, { interactive });
expect(env).toEqual({ CLAUDISH_PANE_CHILD: "1", CLAUDISH_TOKEN_FILE: "/run/a.json" });
});
+
+ test(`the hook inbox and session never reach Claude Code (interactive: ${interactive})`, () => {
+ // hook-system §2.3 step 2: the model's Bash tool must never see the inbox path
+ const env: Record<string, string | undefined> = {
+ CLAUDISH_HOOK_INBOX: "/s/hook-inbox.jsonl",
+ CLAUDISH_HOOK_SESSION: '{"kind":"channel","id":"a","role":null}',
+ KEEP: "me",
+ };
+ scrubChildEnv(env, { interactive });
+ expect(env).toEqual({ KEEP: "me" });
+ });
}
});
diff --git a/packages/cli/src/claude-runner.ts b/packages/cli/src/claude-runner.ts
index 6286b65f..c79418ee 100644
--- a/packages/cli/src/claude-runner.ts
+++ b/packages/cli/src/claude-runner.ts
@@ -998,11 +998,13 @@ export function createTempSettingsFile(
* writes no transcript. Only the magmux-wrap path stripped it before. Print mode is
* left as it was.
*
- * Every launch loses `CLAUDISH_PANE_ENV` and `CLAUDISH_PANE_CWD` (`PANE_LAUNCH_VARS`).
- * They describe THIS claudish (an MCP pane child), not Claude Code; inherited, a
- * claudish started from the slot's Bash tool would take itself for the pane child
- * (re-apply a stale environment snapshot, chdir). They stay in this process's own
- * `process.env`, which `isPaneChild()` and `magmuxPaneCapability()` read.
+ * Every launch loses `CLAUDISH_PANE_ENV`, `CLAUDISH_PANE_CWD`, `CLAUDISH_HOOK_INBOX`
+ * and `CLAUDISH_HOOK_SESSION` (`PANE_LAUNCH_VARS`). They describe THIS claudish (an
+ * MCP pane child), not Claude Code; inherited, a claudish started from the slot's Bash
+ * tool would take itself for the pane child (re-apply a stale environment snapshot,
+ * chdir), and the model's Bash tool would see the hook inbox path. They stay in this
+ * process's own `process.env`, which `isPaneChild()`, `magmuxPaneCapability()` and the
+ * child's proxy read.
*
* `CLAUDISH_PANE_CHILD` is KEPT. It says "this session was started by a claudish
* owner", which is true of everything the session starts: a claudish run from its
diff --git a/packages/cli/src/pane/child-env.test.ts b/packages/cli/src/pane/child-env.test.ts
index ba216558..495e2ab2 100644
--- a/packages/cli/src/pane/child-env.test.ts
+++ b/packages/cli/src/pane/child-env.test.ts
@@ -10,7 +10,11 @@ import { afterAll, describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
+import { HOOK_INBOX_ENV } from "../hooks/injection/sinks.js";
+import { HOOK_SESSION_ENV } from "../hooks/request-stage.js";
import {
+ HOOK_INBOX_VAR,
+ HOOK_SESSION_VAR,
NESTED_SESSION_REFUSAL,
PANE_CHILD_REFUSED_EXIT,
PANE_LAUNCH_VARS,
@@ -21,6 +25,7 @@ import {
nestedLaunchRefusal,
paneChildRefusal,
} from "./child-env.js";
+import { buildPaneEnv } from "./pane-launch.js";
const scratch = realpathSync(mkdtempSync(join(tmpdir(), "claudish-child-env-")));
afterAll(() => rmSync(scratch, { recursive: true, force: true }));
@@ -43,8 +48,13 @@ describe("MCP session and pane-child predicates", () => {
expect(isPaneChild({ CLAUDISH_PANE_CHILD: "true", CLAUDISH_PANE_CWD: "/w" })).toBe(false);
});
- test("T-U-4: launch variables exclude the inherited session flag", () => {
- expect([...PANE_LAUNCH_VARS]).toEqual(["CLAUDISH_PANE_ENV", "CLAUDISH_PANE_CWD"]);
+ test("T-U-4: launch variables exclude the inherited session flag and include the two hook markers", () => {
+ expect([...PANE_LAUNCH_VARS]).toEqual([
+ "CLAUDISH_PANE_ENV",
+ "CLAUDISH_PANE_CWD",
+ "CLAUDISH_HOOK_INBOX",
+ "CLAUDISH_HOOK_SESSION",
+ ]);
});
test("T-U-5: only a session descendant without a pane cwd is refused", () => {
@@ -63,6 +73,11 @@ describe("MCP session and pane-child predicates", () => {
"already inside a session started by the claudish MCP server; one level of nesting is the limit"
);
});
+
+ test("the hook marker names are the ones the child's proxy reads (one name per concept)", () => {
+ expect(HOOK_INBOX_VAR).toBe(HOOK_INBOX_ENV);
+ expect(HOOK_SESSION_VAR).toBe(HOOK_SESSION_ENV);
+ });
});
describe("applyPaneEnvSnapshot", () => {
@@ -117,6 +132,47 @@ describe("applyPaneEnvSnapshot", () => {
expect(env).toEqual({ CLAUDISH_PANE_CHILD: "1", CLAUDISH_PANE_CWD: "/w", A: "1" });
});
+ test("hook-system §2.3 step 2: the hook inbox and session survive a snapshot that lacks them", () => {
+ const env: Record<string, string | undefined> = {
+ CLAUDISH_PANE_CHILD: "1",
+ CLAUDISH_PANE_CWD: "/w",
+ CLAUDISH_HOOK_INBOX: "/s/hook-inbox.jsonl",
+ CLAUDISH_HOOK_SESSION: '{"kind":"channel","id":"abcd1234","role":null}',
+ DROPPED: "x",
+ CLAUDISH_PANE_ENV: JSON.stringify({ A: "1" }),
+ };
+ applyPaneEnvSnapshot(env);
+ expect(env).toEqual({
+ CLAUDISH_PANE_CHILD: "1",
+ CLAUDISH_PANE_CWD: "/w",
+ CLAUDISH_HOOK_INBOX: "/s/hook-inbox.jsonl",
+ CLAUDISH_HOOK_SESSION: '{"kind":"channel","id":"abcd1234","role":null}',
+ A: "1",
+ });
+ });
+
+ test("an owner's slotEnv hook markers reach the child through buildPaneEnv and applyPaneEnv", () => {
+ const { magmuxEnv } = buildPaneEnv({
+ parentEnv: { PATH: "/bin", HOME: "/h", CLAUDISH_HOOK_INBOX: "/stale-from-parent" },
+ slotEnv: {
+ CLAUDISH_HOOK_INBOX: "/sess/hook-inbox.jsonl",
+ CLAUDISH_HOOK_SESSION: '{"kind":"team","id":"r1:01","role":"judge"}',
+ },
+ cwd: "/w",
+ ctlDir: "/tmp/launch-abcdef",
+ configFile: null,
+ });
+ // the pane's shell profile may export anything; the snapshot is re-applied over it
+ const child: Record<string, string | undefined> = {
+ ...magmuxEnv,
+ CLAUDISH_HOOK_INBOX: "/from-a-profile",
+ };
+ applyPaneEnvSnapshot(child);
+ expect(child.CLAUDISH_HOOK_INBOX).toBe("/sess/hook-inbox.jsonl");
+ expect(child.CLAUDISH_HOOK_SESSION).toBe('{"kind":"team","id":"r1:01","role":"judge"}');
+ expect(child.CLAUDISH_PANE_ENV).toBeUndefined();
+ });
+
test("no snapshot leaves the environment alone", () => {
const env = { CLAUDISH_PANE_CHILD: "1", KEEP: "me" };
expect(applyPaneEnvSnapshot(env)).toEqual({ ok: true });
@@ -290,7 +346,7 @@ describe("the CLI entry asserts an interactive REPL after parseArgs", () => {
["print mode", ["--model", "claude-sonnet-4-5", "-p", "hi"], "-p/--print"],
] as const) {
test(`T-CLI-7: ${name} exits 64 with the pane-child refusal`, () => {
- const r = Bun.spawnSync([process.execPath, "run", "src/index.ts", ...argv], {
+ const r = Bun.spawnSync([process.execPath, "--no-env-file", "run", "src/index.ts", ...argv], {
cwd: cliDir,
env: hermeticEnv({ CLAUDISH_PANE_CHILD: "1", CLAUDISH_PANE_CWD: scratch }),
timeout: 60_000,
diff --git a/packages/cli/src/pane/child-env.ts b/packages/cli/src/pane/child-env.ts
index e3de3648..3b99cc2e 100644
--- a/packages/cli/src/pane/child-env.ts
+++ b/packages/cli/src/pane/child-env.ts
@@ -30,9 +30,16 @@
* Code derives the transcript directory from the cwd, so a profile `cd` would move
* the transcript out of the place the server reads it from.
*
- * This module is imported FIRST by the CLI entry (`index.ts`), before `.env` loading
- * and before any module that reads the environment at import time. Its one side
- * effect is `bootstrapPaneChild()` at the bottom, which is a no-op outside the pane child.
+ * The markers come only from the parent claudish that spawned the pane, through the
+ * spawn environment (`buildPaneEnv`). Nothing in the working directory can set them:
+ * Bun's env-file autoload is off on every launch path, `launch-guard.ts` drops them
+ * when a launch left it on, and claudish's own `.env` load never sets them
+ * (`launch-env.ts`). That is what lets `isPaneChild` read a plain variable.
+ *
+ * This module is imported by the CLI entry (`index.ts`) right after `launch-guard.ts`,
+ * before `.env` loading and before any module that reads the environment at import
+ * time. Its one side effect is `bootstrapPaneChild()` at the bottom, which is a no-op
+ * outside the pane child.
*
* After `parseArgs`, `assertPaneChildInteractive` refuses any flag combination that
* would make the pane child anything but an interactive Claude Code REPL. The server
@@ -49,13 +56,27 @@ import { ENV } from "../config.js";
/** Exit status of a refused pane child (EX_USAGE). */
export const PANE_CHILD_REFUSED_EXIT = 64;
+/** The owner-created hook inbox of this pane child (hook-system §2.3). */
+export const HOOK_INBOX_VAR = "CLAUDISH_HOOK_INBOX";
+/** `{"kind","id","role"}` of the session this pane child serves (hook-system §2.3). */
+export const HOOK_SESSION_VAR = "CLAUDISH_HOOK_SESSION";
+
/**
* The markers that describe the LAUNCHED claudish only. claude-runner deletes them
* from Claude Code's environment. `CLAUDISH_PANE_CHILD` is deliberately not here: it
* is inherited by everything the session starts, so a claudish or a claudish MCP
- * server started from inside it knows it is already one level deep.
+ * server started from inside it knows it is already one level deep. The two hook
+ * markers are here: the model's Bash tool never sees the hook inbox path, and a
+ * claudish started from inside the session never writes into its owner's inbox.
+ * They stay in this process's own `process.env`: the child's proxy writes the inbox
+ * named there.
*/
-export const PANE_LAUNCH_VARS = [ENV.CLAUDISH_PANE_ENV, ENV.CLAUDISH_PANE_CWD] as const;
+export const PANE_LAUNCH_VARS = [
+ ENV.CLAUDISH_PANE_ENV,
+ ENV.CLAUDISH_PANE_CWD,
+ HOOK_INBOX_VAR,
+ HOOK_SESSION_VAR,
+] as const;
/**
* Keys owned by the pane's shell and terminal, not by the server's environment.
@@ -146,7 +167,15 @@ export function applyPaneEnvSnapshot(env: Env): PaneBootResult {
for (const value of Object.values(snapshot)) {
if (typeof value !== "string") return { ok: false, reason: "bad env snapshot" };
}
- const keep = new Set<string>([ENV.CLAUDISH_PANE_CHILD, ENV.CLAUDISH_PANE_CWD]);
+ // The hook markers survive like the pane markers: the owner sets them beside the
+ // snapshot for every child (hook-system §2.3 step 2), and a snapshot that lacks them
+ // must not leave the child's proxy without its inbox.
+ const keep = new Set<string>([
+ ENV.CLAUDISH_PANE_CHILD,
+ ENV.CLAUDISH_PANE_CWD,
+ HOOK_INBOX_VAR,
+ HOOK_SESSION_VAR,
+ ]);
for (const key of Object.keys(env)) {
if (Object.hasOwn(snapshot, key) || keep.has(key) || isPaneShellManagedKey(key)) continue;
delete env[key];