Skip to content

Commit 0dea732

Browse files
piotr-rimthanclaude
andcommitted
Give each Codex app-server its thread's identity
The Codex bridge sets BB_THREAD_ID only through shell_environment_policy, so it reaches shell commands but not the app-server process. Stdio MCP servers inherit their environment from the app-server, so a bb-aware MCP server (for example one calling `bb thread ... --self`) cannot tell which thread it serves, even though each thread has its own app-server. Copy the thread identity variables (BB_THREAD_ID, BB_ENVIRONMENT_ID, BB_PROJECT_ID, BB_THREAD_STORAGE) from the session's environment into the app-server environment. Other inherited BB_ variables stay stripped, and maintenance and model-list children carry no thread identity. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VicbK3V2RFSCM4oSeK9ydx
1 parent 17b1971 commit 0dea732

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎plugins/provider-codex/skills/codex-provider/SKILL.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ tier to Codex as given. When the model does not list it, Codex runs the turn
2323
at its default tier and the thread shows its warning that the tier "is not
2424
advertised as supported" and was omitted.
2525

26+
Each thread runs its own Codex app-server. Its environment carries the
27+
thread's `BB_THREAD_ID`, `BB_ENVIRONMENT_ID`, `BB_PROJECT_ID` and
28+
`BB_THREAD_STORAGE`, so a stdio MCP server that lists them in its `env_vars`
29+
can act on the calling thread; shell commands see them too.
30+
2631
Use the core CLI skill for command syntax and official Codex guidance for
2732
upstream product behavior.

‎plugins/provider-codex/src/bridge/app-server-launch.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2-
import { resolveAppServerLaunch } from "./bridge.js";
2+
import { buildAppServerEnv, resolveAppServerLaunch } from "./bridge.js";
33

44
afterEach(() => vi.unstubAllEnvs());
55

@@ -61,3 +61,31 @@ describe("Codex Account Pool isolation", () => {
6161
},
6262
);
6363
});
64+
65+
describe("Codex app-server environment", () => {
66+
it("carries the thread identity so tools the app-server launches can bind per thread", () => {
67+
vi.stubEnv("BB_THREAD_ID", "thr_daemon");
68+
vi.stubEnv("BB_SERVER_URL", "http://127.0.0.1:1");
69+
const env = buildAppServerEnv({
70+
BB_THREAD_ID: "thr_one",
71+
BB_ENVIRONMENT_ID: "env_one",
72+
BB_PROJECT_ID: "proj_one",
73+
BB_THREAD_STORAGE: "/tmp/thr_one",
74+
BB_UNRELATED: "kept-out",
75+
});
76+
expect(env).toMatchObject({
77+
BB_THREAD_ID: "thr_one",
78+
BB_ENVIRONMENT_ID: "env_one",
79+
BB_PROJECT_ID: "proj_one",
80+
BB_THREAD_STORAGE: "/tmp/thr_one",
81+
});
82+
expect(env).not.toHaveProperty("BB_UNRELATED");
83+
expect(env).not.toHaveProperty("BB_SERVER_URL");
84+
});
85+
86+
it("gives a child without a thread no thread identity", () => {
87+
vi.stubEnv("BB_THREAD_ID", "thr_daemon");
88+
const env = buildAppServerEnv(undefined);
89+
expect(env).not.toHaveProperty("BB_THREAD_ID");
90+
});
91+
});

‎plugins/provider-codex/src/bridge/bridge.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,26 @@ function appServerLaunchEnv(
395395
};
396396
}
397397

398-
function buildAppServerEnv(
398+
const THREAD_IDENTITY_ENV_KEYS = [
399+
"BB_THREAD_ID",
400+
"BB_ENVIRONMENT_ID",
401+
"BB_PROJECT_ID",
402+
"BB_THREAD_STORAGE",
403+
] as const;
404+
405+
export function buildAppServerEnv(
399406
envVars: Readonly<Record<string, string>> | undefined,
400407
): NodeJS.ProcessEnv {
401-
return withoutBridgeRuntimeEnv(
408+
const env = withoutBridgeRuntimeEnv(
402409
sanitizeInheritedChildProcessEnv({
403410
env: appServerLaunchEnv(envVars),
404411
}),
405412
);
413+
for (const key of THREAD_IDENTITY_ENV_KEYS) {
414+
const value = envVars?.[key];
415+
if (value !== undefined) env[key] = value;
416+
}
417+
return env;
406418
}
407419

408420
function isCodexSpawnFailure(error: unknown): boolean {

0 commit comments

Comments
 (0)