Skip to content

Commit 1947058

Browse files
committed
Respect STRICT_PERSISTENCE_ERRORS for config DI failures
1 parent 7357848 commit 1947058

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/core/persistence.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import os
34
from pathlib import Path
45
from typing import Any
56

@@ -38,6 +39,9 @@ def __init__(
3839

3940
def _should_raise_strict_errors(self) -> bool:
4041
"""Check if strict error handling is enabled via config."""
42+
env_value = os.getenv("STRICT_PERSISTENCE_ERRORS", "false").lower()
43+
if env_value in {"true", "1", "yes"}:
44+
return True
4145
if self.config:
4246
value = self.config.get("session.dangerous_command_prevention_enabled")
4347
return bool(value) if value is not None else False

tests/unit/test_config_persistence.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ def functional_backend() -> str:
1212
from fastapi import FastAPI
1313
from fastapi.testclient import TestClient
1414
from src.core.app.test_builder import build_test_app as build_app
15-
from src.core.common.exceptions import ConfigurationError, JSONParsingError
15+
from src.core.common.exceptions import (
16+
ConfigurationError,
17+
JSONParsingError,
18+
ServiceResolutionError,
19+
)
1620
from src.core.config.app_config import load_config
1721
from src.core.persistence import ConfigManager
1822

@@ -227,6 +231,34 @@ def test_apply_default_backend_invalid_backend_still_raises_with_cli_override(
227231
}
228232

229233

234+
def test_apply_default_backend_strict_errors_env(
235+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
236+
) -> None:
237+
"""STRICT_PERSISTENCE_ERRORS should force DI resolution failures to raise."""
238+
239+
class _StrictAppState(_DummyAppState):
240+
def set_backend_type(self, backend_type: str | None) -> None: # type: ignore[override]
241+
self.backend_type = backend_type
242+
243+
class _FailingProvider:
244+
def get_required_service(self, _service_type): # type: ignore[no-untyped-def]
245+
raise ServiceResolutionError("boom")
246+
247+
monkeypatch.setenv("STRICT_PERSISTENCE_ERRORS", "true")
248+
249+
manager = ConfigManager(
250+
FastAPI(),
251+
path=str(tmp_path / "config.json"),
252+
service_provider=_FailingProvider(),
253+
app_state=_StrictAppState(),
254+
)
255+
256+
with pytest.raises(ServiceResolutionError):
257+
manager._apply_default_backend("openai")
258+
259+
monkeypatch.delenv("STRICT_PERSISTENCE_ERRORS", raising=False)
260+
261+
230262
def test_load_raises_json_parsing_error_for_invalid_json(tmp_path: Path) -> None:
231263
cfg_path = tmp_path / "config.json"
232264
cfg_path.write_text("{not: valid json}")

0 commit comments

Comments
 (0)