fix(client): store metrics at HeadroomConfig.store_url

HeadroomClient copied config.store_url into self._store_url, which
validate_setup() reports, but opened storage with its own store_url
argument. Metrics went to <system temp>/headroom.db while the client
reported the configured path.

Open storage with self._store_url. HeadroomConfig.store_url now
defaults to None so a default config keeps the temp-directory location
instead of starting to write ./headroom.db.
This commit is contained in:
Program2113
2026-10-18 19:07:45 +05:30
parent fbf7bc3278
commit f96bc8e2ec
4 changed files with 93 additions and 3 deletions
+1 -1
View File
@@ -325,7 +325,7 @@ class HeadroomClient:
self._config.model_context_limits.update(model_context_limits)
# Initialize storage
self._storage = create_storage(store_url)
self._storage = create_storage(self._store_url)
# Initialize transform pipeline
self._pipeline = TransformPipeline(self._config, provider=self._provider)
+3 -1
View File
@@ -923,7 +923,9 @@ class PrefixFreezeConfig:
class HeadroomConfig:
"""Main configuration for HeadroomClient."""
store_url: str = "sqlite:///headroom.db"
# None defers to HeadroomClient: its ``store_url`` argument, else a
# headroom.db in the system temp directory.
store_url: str | None = None
default_mode: HeadroomMode = HeadroomMode.AUDIT
model_context_limits: dict[str, int] = field(
default_factory=lambda: DEFAULT_MODEL_CONTEXT_LIMITS.copy()
+88
View File
@@ -0,0 +1,88 @@
"""``HeadroomConfig.store_url`` must decide where ``HeadroomClient`` stores metrics.
The client copied ``config.store_url`` into ``self._store_url`` (which
``validate_setup()`` reports) but opened storage with its own ``store_url``
argument, so metrics went to the temp-directory default while the client
reported the configured path.
"""
from __future__ import annotations
import tempfile
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from headroom.client import HeadroomClient
from headroom.config import HeadroomConfig
from headroom.providers.openai import OpenAIProvider
@pytest.fixture
def temp_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Point the client's temp-directory default at an isolated folder."""
temp = tmp_path / "system-temp"
temp.mkdir()
monkeypatch.setattr(tempfile, "tempdir", str(temp))
return temp
def _client(**kwargs: object) -> HeadroomClient:
return HeadroomClient(
original_client=MagicMock(),
provider=OpenAIProvider(),
enable_cache_optimizer=False,
**kwargs,
)
def _storage_path(client: HeadroomClient) -> Path:
# "sqlite:////abs/path" parses to "//abs/path"; resolve() normalizes it.
return Path(client._storage.db_path).resolve()
def test_config_store_url_is_where_metrics_are_stored(tmp_path: Path, temp_dir: Path) -> None:
db = tmp_path / "mine.db"
client = _client(config=HeadroomConfig(store_url=f"sqlite:///{db}"))
assert _storage_path(client) == db.resolve()
assert db.exists()
assert not (temp_dir / "headroom.db").exists()
assert client.validate_setup()["storage"]["url"] == f"sqlite:///{db}"
def test_config_store_url_takes_precedence_over_the_argument(
tmp_path: Path, temp_dir: Path
) -> None:
from_config = tmp_path / "from-config.db"
from_argument = tmp_path / "from-argument.db"
client = _client(
store_url=f"sqlite:///{from_argument}",
config=HeadroomConfig(store_url=f"sqlite:///{from_config}"),
)
assert _storage_path(client) == from_config.resolve()
assert not from_argument.exists()
def test_argument_is_used_when_config_has_no_store_url(tmp_path: Path, temp_dir: Path) -> None:
from_argument = tmp_path / "from-argument.db"
config = HeadroomConfig()
client = _client(store_url=f"sqlite:///{from_argument}", config=config)
assert _storage_path(client) == from_argument.resolve()
assert config.store_url == f"sqlite:///{from_argument}"
def test_default_config_stores_in_temp_dir_not_cwd(
tmp_path: Path, temp_dir: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.chdir(cwd)
client = _client(config=HeadroomConfig())
assert _storage_path(client) == (temp_dir / "headroom.db").resolve()
assert not (cwd / "headroom.db").exists()
+1 -1
View File
@@ -151,7 +151,7 @@ class TestHeadroomConfig:
def test_default_values(self):
"""Default values are correctly set."""
config = HeadroomConfig()
assert config.store_url == "sqlite:///headroom.db"
assert config.store_url is None
assert config.default_mode == HeadroomMode.AUDIT
assert config.generate_diff_artifact is False
# Nested configs exist