Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(agent): turn off claude code's own sandbox in cloud runs (#126198)

Alessandro Pogliaghi782ea89f9b91
diff --git a/packages/agent/packages/agent/src/adapters/claude/session/options.test.ts b/packages/agent/packages/agent/src/adapters/claude/session/options.test.ts
index a62dd19f21cc..af93698b2156 100644
--- a/packages/agent/packages/agent/src/adapters/claude/session/options.test.ts
+++ b/packages/agent/packages/agent/src/adapters/claude/session/options.test.ts
@@ -612,6 +612,16 @@ describe("buildSessionOptions", () => {
expect(env?.CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS).toBe("1");
});
+ it("turns off the Claude sandbox for a cloud subscription run", () => {
+ const options = buildSessionOptions({
+ ...makeParams(),
+ cloudMode: true,
+ machineAuth: { oauthToken: "sk-ant-oat01-fake-test-token" },
+ });
+
+ expect(options.settings).toMatchObject({ sandbox: { enabled: false } });
+ });
+
it("skips the pinned gateway-era fallback model", () => {
const options = buildSessionOptions({
...makeParams(),
@@ -1019,6 +1029,41 @@ describe("buildSessionOptions", () => {
});
});
+ it.each([
+ [true, { enabled: false }],
+ [false, undefined],
+ ])(
+ "pins the Claude sandbox off only in cloud runs (cloudMode %s)",
+ (cloudMode, sandbox) => {
+ const options = buildSessionOptions({
+ ...makeParams(),
+ gatewayEnv,
+ cloudMode,
+ });
+
+ expect(flagSettings(options).sandbox).toEqual(sandbox);
+ },
+ );
+
+ it.each([
+ ["the SDK settings option", { settings: "/tmp/caller-settings.json" }],
+ [
+ "the settings flag",
+ { extraArgs: { settings: "/tmp/caller-settings.json" } },
+ ],
+ ])(
+ "drops repo settings in a cloud run when %s holds a file path",
+ (_name, provided) => {
+ const options = buildSessionOptions({
+ ...makeParams(),
+ cloudMode: true,
+ userProvidedOptions: provided as Options,
+ });
+
+ expect(options.settingSources).toEqual(["user"]);
+ },
+ );
+
it("removes the pinned settings file and nothing outside its directory", () => {
const options = buildSessionOptions({ ...makeParams(), gatewayEnv });
const file = String(options.extraArgs?.settings);
diff --git a/packages/agent/packages/agent/src/adapters/claude/session/options.ts b/packages/agent/packages/agent/src/adapters/claude/session/options.ts
index 5b9e163ffc69..1c6547814966 100644
--- a/packages/agent/packages/agent/src/adapters/claude/session/options.ts
+++ b/packages/agent/packages/agent/src/adapters/claude/session/options.ts
@@ -686,6 +686,29 @@ function pinGatewayEnvSettings(
return true;
}
+function disableClaudeSandbox(options: Options): boolean {
+ const off: Settings = { sandbox: { enabled: false } };
+ if (typeof options.settings === "string") return false;
+ if (options.settings) {
+ options.settings = { ...options.settings, ...off };
+ return true;
+ }
+ const raw = options.extraArgs?.settings;
+ let base: Settings = {};
+ if (raw) {
+ try {
+ base = JSON.parse(raw) as Settings;
+ } catch {
+ return false;
+ }
+ }
+ options.extraArgs = {
+ ...options.extraArgs,
+ settings: JSON.stringify({ ...base, ...off }),
+ };
+ return true;
+}
+
function ensureLocalSettings(cwd: string): void {
const claudeDir = path.join(cwd, ".claude");
const localSettingsPath = path.join(claudeDir, "settings.local.json");
@@ -825,6 +848,12 @@ export function buildSessionOptions(params: BuildOptionsParams): Options {
}),
};
+ if (params.cloudMode && !disableClaudeSandbox(options)) {
+ options.settingSources = options.settingSources?.filter(
+ (source) => source !== "project" && source !== "local",
+ );
+ }
+
if (
params.gatewayEnv?.anthropicBaseUrl &&
!params.machineAuth &&