Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix: do not report a seeded stored setting as an env override (#4326)

Vaibhav Hajareb1aaf5b36660
diff --git a/headroom/settings_store.py b/headroom/settings_store.py
index 64161766d..ffc0c73a3 100644
--- a/headroom/settings_store.py
+++ b/headroom/settings_store.py
@@ -999,13 +999,32 @@ def save(values: dict[str, Any]) -> None:
_atomic_write_text(paths.settings_path(), payload)
+# The env vars Headroom itself inserted at startup, mapped to the value it
+# inserted. This is provenance, not value equality: an operator export that
+# happens to carry the same text as the settings file is still an operator
+# export, and Headroom's own seed stays Headroom's own even after the file
+# changes underneath it.
+_SEEDED_ENV: dict[str, str] = {}
+
+
+def seeded_environment() -> dict[str, str]:
+ """Return the env vars Headroom inserted at startup, with their values."""
+ return dict(_SEEDED_ENV)
+
+
def apply_to_environ(values: dict[str, Any]) -> None:
"""``setdefault`` each stored value into ``os.environ`` (explicit export wins)."""
for key, value in values.items():
field = _BY_KEY.get(key)
if field is None or value is None:
continue
- os.environ.setdefault(field.env, _serialize(field, value))
+ if field.env in os.environ:
+ # An explicit export is already there and keeps precedence, so
+ # Headroom did not insert this one.
+ continue
+ serialized = _serialize(field, value)
+ os.environ[field.env] = serialized
+ _SEEDED_ENV[field.env] = serialized
def _coerce_runtime_values(runtime_values: dict[str, Any] | None) -> dict[str, Any]:
@@ -1079,6 +1098,13 @@ def to_schema(
effective = {**environment_effective, **runtime}
fields: list[dict[str, Any]] = []
for field in SETTINGS:
+ # An env var is an operator export only when Headroom did not put it
+ # there. The startup seed is recorded when it happens, so the answer
+ # does not depend on how the value compares with the settings file:
+ # a value saved after the seed is still Headroom's own seed, and an
+ # operator export that matches the stored text is still an export.
+ env_raw = os.environ.get(field.env)
+ seeded_env = _SEEDED_ENV.get(field.env)
fields.append(
{
"key": field.key,
@@ -1094,7 +1120,8 @@ def to_schema(
"minimum": field.minimum,
"maximum": field.maximum,
"tier": field.tier,
- "env_override": bool(os.environ.get(field.env))
+ "env_override": bool(env_raw)
+ and env_raw != seeded_env
and field.env not in env_override_exclusions,
"runtime_override": field.key in runtime
and runtime[field.key] != environment_effective[field.key],
diff --git a/tests/test_proxy/test_settings_store.py b/tests/test_proxy/test_settings_store.py
index 0a352499c..1f2553ff9 100644
--- a/tests/test_proxy/test_settings_store.py
+++ b/tests/test_proxy/test_settings_store.py
@@ -26,6 +26,9 @@ def workspace(tmp_path, monkeypatch):
def _clear_env(monkeypatch):
for field in settings_store.SETTINGS:
monkeypatch.delenv(field.env, raising=False)
+ # The startup seed is process state, so it has to be reset with the
+ # environment it describes.
+ monkeypatch.setattr(settings_store, "_SEEDED_ENV", {})
class TestRoundTrip:
@@ -214,6 +217,55 @@ class TestEffectiveValues:
assert lossless["runtime_override"] is False
assert lossless["env_override"] is True
+ def test_schema_does_not_call_a_seeded_stored_value_an_env_override(
+ self, workspace, monkeypatch
+ ):
+ # Issue #4320: apply_to_environ seeds the stored value into os.environ at
+ # startup, which made the GUI's own saved setting look like an operator
+ # export and locked it. It is only an override when Headroom did not
+ # insert the value.
+ _clear_env(monkeypatch)
+ settings_store.save({"mode": "token"})
+
+ settings_store.apply_to_environ(settings_store.load())
+ field = next(f for f in settings_store.to_schema()["fields"] if f["key"] == "mode")
+ assert field["env_override"] is False
+
+ monkeypatch.setenv("HEADROOM_MODE", "cache")
+ field = next(f for f in settings_store.to_schema()["fields"] if f["key"] == "mode")
+ assert field["env_override"] is True
+
+ def test_schema_keeps_a_seed_an_override_out_after_a_later_save(self, workspace, monkeypatch):
+ # The seed stays Headroom's own value when the file changes under it.
+ # Comparing the env value with the stored value would report the
+ # stale seed as an operator export and lock the setting until restart.
+ _clear_env(monkeypatch)
+ settings_store.save({"mode": "token"})
+ settings_store.apply_to_environ(settings_store.load())
+
+ settings_store.save({"mode": "cache"})
+
+ field = next(f for f in settings_store.to_schema()["fields"] if f["key"] == "mode")
+ assert field["env_override"] is False
+ # The startup seed is still the effective value until a restart.
+ assert field["value"] == "token"
+
+ def test_schema_reports_an_equal_valued_external_export_as_an_override(
+ self, workspace, monkeypatch
+ ):
+ # An operator export that carries the same text as the settings file
+ # still wins at startup, so the dashboard has to keep the setting
+ # locked rather than offer an edit that cannot take effect.
+ _clear_env(monkeypatch)
+ settings_store.save({"mode": "token"})
+ monkeypatch.setenv("HEADROOM_MODE", "token")
+
+ settings_store.apply_to_environ(settings_store.load())
+
+ field = next(f for f in settings_store.to_schema()["fields"] if f["key"] == "mode")
+ assert field["env_override"] is True
+ assert settings_store.seeded_environment().get("HEADROOM_MODE") is None
+
class TestSecretMasking:
def test_schema_and_stored_mask_secret(self, workspace, monkeypatch):