diff --git a/CHANGELOG.md b/CHANGELOG.md index bc704e063..464907bf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -230,6 +230,12 @@ All notable changes to SkillOpt are documented here. This project adheres to instead of treating them as missing, and load integral floats such as `1776.0` as `1776`, so a correct integer answer is no longer scored wrong (thanks @Shizoqua). +- An empty `CLAUDE_SETTING_SOURCES` now reaches the Claude CLI as + `--setting-sources ""`, which loads no settings. The `claude_chat` backend + used to drop the flag for an empty value, and the CLI then loaded every + source, so the user's hooks ran and their `CLAUDE.md` loaded in every + rollout, and there was no way to ask for none. The variable is now + documented. ### Security - Prevent managed-identity credentials from being sent to non-Azure or diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 43467b22c..36478e574 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -201,6 +201,7 @@ Model credentials are loaded from environment variables: | `OPENAI_COMPATIBLE_MODEL` | `openai_compatible` | Shared provider model ID for direct library use; train/eval YAML role models take precedence | | `CLAUDE_CLI_BIN` | `claude_chat` | Optional path to the `claude` executable; defaults to `claude` | | `ANTHROPIC_API_KEY` | `claude_chat` | Optional authentication method understood by the Claude CLI, not a direct SkillOpt API client | +| `CLAUDE_SETTING_SOURCES` | `claude_chat` | Settings sources the Claude CLI loads, a comma-separated list of `user`, `project` and `local`; defaults to `user,project`, and an empty value loads none, so no user settings, hooks or `CLAUDE.md` reach rollouts | | `CURSOR_EXEC_PATH` | `cursor_exec` | Optional path to `cursor-agent`; defaults to `cursor-agent` | | `CURSOR_EXEC_SANDBOX` | `cursor_exec` | Cursor sandbox mode: `enabled` (default) or `disabled` | | `CURSOR_API_KEY` | `cursor_exec` | Optional authentication method understood directly by Cursor Agent | diff --git a/skillopt/model/claude_backend.py b/skillopt/model/claude_backend.py index 373811af3..d594aaaa0 100644 --- a/skillopt/model/claude_backend.py +++ b/skillopt/model/claude_backend.py @@ -16,7 +16,10 @@ CLAUDE_BIN = os.environ.get("CLAUDE_CLI_BIN", "claude") CLAUDE_PERMISSION_MODE = os.environ.get("CLAUDE_PERMISSION_MODE", "dontAsk") -CLAUDE_SETTING_SOURCES = os.environ.get("CLAUDE_SETTING_SOURCES", "user,project") +# Settings sources the CLI loads (--setting-sources). Unset means user and +# project settings; an empty value means none, so no user settings, hooks or +# CLAUDE.md reach the model. +CLAUDE_SETTING_SOURCES = os.environ.get("CLAUDE_SETTING_SOURCES", "user,project").strip() CLAUDE_ALLOW_ATTACHMENT_READ = os.environ.get("CLAUDE_ALLOW_ATTACHMENT_READ", "1").strip().lower() not in {"0", "false", "no"} OPTIMIZER_DEPLOYMENT = os.environ.get("OPTIMIZER_DEPLOYMENT", "claude-sonnet-4-6") @@ -254,8 +257,9 @@ def _run_claude_print(*, system: str, prompt: str, model: str, tools: list[dict[ cmd = [CLAUDE_BIN, "-p", "--output-format", "json", "--permission-mode", CLAUDE_PERMISSION_MODE, "--add-dir", temp_dir] if model: cmd.extend(["--model", model]) - if CLAUDE_SETTING_SOURCES: - cmd.extend(["--setting-sources", CLAUDE_SETTING_SOURCES]) + # Always pass the flag: without it the CLI loads every source, so an + # empty value would load the most settings instead of none. + cmd.extend(["--setting-sources", CLAUDE_SETTING_SOURCES]) if system: # Write the system prompt to a file, not argv: here the skill being # optimized IS the system prompt, and SkillOpt grows it over training, diff --git a/tests/test_claude_backend_setting_sources.py b/tests/test_claude_backend_setting_sources.py new file mode 100644 index 000000000..584d5afb9 --- /dev/null +++ b/tests/test_claude_backend_setting_sources.py @@ -0,0 +1,62 @@ +"""CLAUDE_SETTING_SOURCES must be able to ask the Claude CLI for no settings at all.""" +from __future__ import annotations + +import importlib.util +import json +from types import SimpleNamespace + +import pytest + +from skillopt.model import claude_backend + + +def _fresh_backend(monkeypatch, value: str | None): + if value is None: + monkeypatch.delenv("CLAUDE_SETTING_SOURCES", raising=False) + else: + monkeypatch.setenv("CLAUDE_SETTING_SOURCES", value) + spec = importlib.util.spec_from_file_location("_claude_backend_fresh", claude_backend.__file__) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def _cli_argv(monkeypatch, module) -> list[str]: + captured: dict = {} + + def fake_run(cmd, **kwargs): + captured["cmd"] = list(cmd) + payload = {"type": "result", "result": "ok", "usage": {}} + return SimpleNamespace(returncode=0, stdout=json.dumps(payload) + "\n", stderr="") + + monkeypatch.setattr(module.subprocess, "run", fake_run) + module._run_claude_print( + system="system", + prompt="prompt", + model="", + tools=None, + tool_choice=None, + return_message=False, + timeout=10, + ) + return captured["cmd"] + + +@pytest.mark.parametrize( + ("env_value", "expected"), + [ + (None, "user,project"), + # Omitting the flag makes the CLI load every source (user, project and + # local), so an empty value must reach it as an explicit empty list. + ("", ""), + (" ", ""), + ("user", "user"), + ], +) +def test_setting_sources_flag_follows_the_environment(monkeypatch, env_value, expected) -> None: + module = _fresh_backend(monkeypatch, env_value) + + cmd = _cli_argv(monkeypatch, module) + + assert cmd.count("--setting-sources") == 1 + assert cmd[cmd.index("--setting-sources") + 1] == expected