Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Stop unsloth start codex and openclaw from inheriting provider API keys (#10316)

Nilay05d98ea651f2
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 148ae91f2e5..7d7b9eea316 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -193,6 +193,10 @@ _CLAUDE_ENV_UNSET = (
"CLAUDE_CODE_USE_MANTLE",
)
_CODEX_ENV_UNSET = ("OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN")
+# OpenClaw tries CODEX_API_KEY before OPENAI_API_KEY, so dropping only the latter still embeds
+# via OpenAI. CODEX_ACCESS_TOKEN is not a provider candidate but its host exec tool inherits it
+# (it is absent from host-env-security-policy.json), and the codex CLI on PATH logs in with it.
+_OPENCLAW_ENV_UNSET = ("OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN")
# Shared by every agent command; only the config/env/command differ.
# Help is grouped into rich panels so `--help` reads as Model / Server / Session
@@ -4714,6 +4718,8 @@ def write_openclaw_config(
search.update({"provider": "none", "fallback": "none"})
search.pop("model", None)
search.pop("remote", None)
+ # ORed with OPENCLAW_LOAD_SHELL_ENV: a persisted true re-enables the login-shell key import.
+ _subdict(_subdict(config, "env"), "shellEnv")["enabled"] = False
# Pin a default model, else OpenClaw drops into its setup agent ("no models available").
agents = _subdict(config, "agents")
defaults = _subdict(agents, "defaults")
@@ -5319,7 +5325,15 @@ def codex(
with _session_config("codex", launch, persist = persist) as home:
write_codex_config(base, entry, home)
env = {_CODEX_ENV_KEY: key, "CODEX_HOME": str(home)}
- _run(base, entry, env, command, launch = launch, install_hint = install_hint)
+ _run(
+ base,
+ entry,
+ env,
+ command,
+ launch = launch,
+ install_hint = install_hint,
+ unset_env = _CODEX_ENV_UNSET,
+ )
@start_app.command("openclaw", cls = _PassthroughCommand, context_settings = _PASSTHROUGH)
@@ -5406,7 +5420,12 @@ def openclaw(
embedding_model = _studio_embedding_model(base, key),
)
# Scope both config and state so OpenClaw never touches the user's ~/.openclaw.
- env = {"OPENCLAW_CONFIG_PATH": str(config_path), "OPENCLAW_STATE_DIR": str(cfg)}
+ # Off, else OpenClaw re-imports any provider key it cannot see from a login shell.
+ env = {
+ "OPENCLAW_CONFIG_PATH": str(config_path),
+ "OPENCLAW_STATE_DIR": str(cfg),
+ "OPENCLAW_LOAD_SHELL_ENV": "0",
+ }
_run(
base,
entry,
@@ -5414,6 +5433,7 @@ def openclaw(
command,
launch = launch,
install_hint = install_hint,
+ unset_env = _OPENCLAW_ENV_UNSET,
cwd_env = ("OPENCLAW_WORKSPACE_DIR",),
)
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 13ab52fa7a7..7fb7063fe24 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -45,6 +45,11 @@ def _assert_env_unset(output: str, name: str) -> None:
assert needle in output, f"{needle!r} not found in:\n{output}"
+def _assert_env_kept(output: str, name: str) -> None:
+ needle = f"Remove-Item Env:{name}" if os.name == "nt" else f"unset {name}"
+ assert needle not in output, f"{needle!r} unexpectedly found in:\n{output}"
+
+
def _assert_env_cwd(output: str, name: str) -> None:
needle = f"$env:{name} = (Get-Location).Path" if os.name == "nt" else f'export {name}="$PWD"'
assert needle in output, f"{needle!r} not found in:\n{output}"
@@ -2286,6 +2291,8 @@ def test_connect_claude_no_launch_windows_shim_from_wsl_prints_wslenv(
def test_connect_codex_no_launch(fake_studio, tmp_path):
result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])
assert result.exit_code == 0, result.output
+ for name in start._CODEX_ENV_UNSET:
+ _assert_env_unset(result.output, name)
_assert_env_set(result.output, "UNSLOTH_STUDIO_AUTH_TOKEN", "sk-unsloth-feedfacefeedface")
assert "codex --oss --profile unsloth_api" in result.output
# Config lands in the session-scoped CODEX_HOME, not the user's ~/.codex.
@@ -2320,6 +2327,8 @@ def test_connect_codex_as_subagent_preserves_cloud_parent(fake_studio, tmp_path,
assert "--model" not in command
parent_home = tmp_path / "agents" / "codex-subagent" / "parent"
_assert_env_set(result.output, "CODEX_HOME", str(parent_home))
+ for name in start._CODEX_ENV_UNSET:
+ _assert_env_kept(result.output, name)
assert start._CODEX_ENV_KEY not in result.output
assert "sk-unsloth-feedfacefeedface" not in result.output
home = tmp_path / "agents" / "codex-subagent"
@@ -4708,6 +4717,8 @@ def test_connect_openclaw_no_launch(fake_studio, tmp_path, monkeypatch):
# Config + state are scoped to the session dir, not the user's ~/.openclaw.
_assert_env_set(result.output, "OPENCLAW_CONFIG_PATH", str(config_path))
_assert_env_set(result.output, "OPENCLAW_STATE_DIR", str(tmp_path / "agents" / "openclaw"))
+ for name in start._OPENCLAW_ENV_UNSET:
+ _assert_env_unset(result.output, name)
config = json.loads(config_path.read_text())
assert config["models"]["providers"]["unsloth"]["apiKey"] == "sk-unsloth-feedfacefeedface"
assert config["agents"]["defaults"]["model"]["primary"] == f"unsloth/{MODEL['id']}"
@@ -8741,6 +8752,55 @@ def test_a_status_body_without_is_gguf_still_launches(fake_studio, monkeypatch,
assert "needs a GGUF model" not in result.output
+ ("agent", "unset"),
+ [
+ ("codex", ("OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN")),
+ ("openclaw", ("OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN")),
+ ],
+)
+def test_launch_drops_provider_credentials(agent, unset, fake_studio, monkeypatch):
+ monkeypatch.setattr(start.shutil, "which", lambda _: f"/usr/local/bin/{agent}")
+ for name in unset:
+ monkeypatch.setenv(name, "sk-stale")
+ captured = _capture_launch(monkeypatch, [agent])
+ for name in unset:
+ assert name not in captured["env"]
+
+
[email protected]("enabled", ["1", "true", "yes", "on"])
+def test_openclaw_launch_disables_the_login_shell_key_fallback(enabled, fake_studio, monkeypatch):
+ # openclaw 2026.9.2 reads dropped keys back from a login shell (src/infra/shell-env.ts).
+ monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/openclaw")
+ monkeypatch.setenv("OPENCLAW_LOAD_SHELL_ENV", enabled)
+ captured = _capture_launch(monkeypatch, ["openclaw"])
+ assert captured["env"]["OPENCLAW_LOAD_SHELL_ENV"] == "0"
+
+
+def test_openclaw_no_launch_recipe_disables_the_login_shell_key_fallback(fake_studio):
+ result = CliRunner().invoke(start.start_app, ["openclaw", "--no-launch"])
+ assert result.exit_code == 0, result.output
+ _assert_env_set(result.output, "OPENCLAW_LOAD_SHELL_ENV", "0")
+
+
+def test_openclaw_state_dir_is_a_real_path_not_a_blank(fake_studio, monkeypatch):
+ # An empty state dir sends OpenClaw back to the user's real home, undoing the scoping.
+ monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/openclaw")
+ captured = _capture_launch(monkeypatch, ["openclaw"])
+ assert captured["env"]["OPENCLAW_STATE_DIR"].strip()
+
+
+def test_openclaw_config_pins_the_shell_env_fallback_off(fake_studio, tmp_path, monkeypatch):
+ monkeypatch.chdir(tmp_path)
+ config_path = tmp_path / "agents" / "openclaw" / "openclaw.json"
+ config_path.parent.mkdir(parents = True)
+ config_path.write_text(json.dumps({"env": {"shellEnv": {"enabled": True}}}))
+ result = CliRunner().invoke(start.start_app, ["openclaw", "--no-launch"])
+ assert result.exit_code == 0, result.output
+ config = json.loads(config_path.read_text())
+ assert config["env"]["shellEnv"]["enabled"] is False
+
+
def _openclaw_without_the_settings_route(monkeypatch, exc = RuntimeError("404")):
real = start._http_json