Skip to content

Commit 3bfedf6

Browse files
committed
fix(agent): keep cloud credentials in secondary checkouts
1 parent c688e59 commit 3bfedf6

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

packages/agent/src/adapters/codex-app-server/spawn.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ describe("buildAppServerArgs", () => {
126126
expect(args).toContain('model_verbosity="low"');
127127
});
128128

129+
it("pins the cloud BASH_ENV into tool shells for secondary checkouts", () => {
130+
const args = buildAppServerArgs(
131+
{ binaryPath: "/bundle/codex" },
132+
{ IS_SANDBOX: "1", BASH_ENV: "/tmp/agentsh-bash-env.sh" },
133+
);
134+
135+
expect(args).toContain(
136+
'shell_environment_policy.set.BASH_ENV="/tmp/agentsh-bash-env.sh"',
137+
);
138+
});
139+
140+
it("does not override BASH_ENV outside a managed sandbox", () => {
141+
const args = buildAppServerArgs(
142+
{ binaryPath: "/bundle/codex" },
143+
{ BASH_ENV: "/Users/example/.bash-env" },
144+
);
145+
146+
expect(
147+
args.some((arg) =>
148+
arg.startsWith("shell_environment_policy.set.BASH_ENV="),
149+
),
150+
).toBe(false);
151+
});
152+
129153
it("does not set instructions at spawn (developer_instructions are per-thread)", () => {
130154
const args = buildAppServerArgs({
131155
binaryPath: "/bundle/codex",

packages/agent/src/adapters/codex-app-server/spawn.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function tomlInlineTable(entries: Record<string, string>): string {
9191

9292
export function buildAppServerArgs(
9393
options: CodexAppServerProcessOptions,
94+
environment: NodeJS.ProcessEnv = process.env,
9495
): string[] {
9596
const args: string[] = ["app-server"];
9697

@@ -130,6 +131,16 @@ export function buildAppServerArgs(
130131
// still override it via configOverrides, which the trailing loop appends last.
131132
args.push("-c", `approvals_reviewer="user"`);
132133

134+
// Codex snapshots shell state only for the thread's initial cwd. Cloud tasks
135+
// can work in additional checkouts, so pin the backend-controlled BASH_ENV
136+
// path into every tool shell instead of relying on snapshot restoration.
137+
if (environment.IS_SANDBOX && environment.BASH_ENV) {
138+
args.push(
139+
"-c",
140+
`shell_environment_policy.set.BASH_ENV=${tomlBasicString(environment.BASH_ENV)}`,
141+
);
142+
}
143+
133144
// Disable the user's ambient ~/.codex MCP servers so the adapter only exposes
134145
// MCP servers PostHog injects per-thread; otherwise codex fails connecting to them.
135146
for (const name of new CodexSettingsManager(
@@ -199,7 +210,7 @@ export function spawnCodexAppServerProcess(
199210
}
200211
env.PATH = `${dirname(options.binaryPath)}${delimiter}${env.PATH ?? ""}`;
201212

202-
const args = buildAppServerArgs(options);
213+
const args = buildAppServerArgs(options, env);
203214

204215
logger.info("Spawning codex app-server process", {
205216
command: options.binaryPath,

0 commit comments

Comments
 (0)