Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/persistence.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -38,6 +39,9 @@ def __init__(

def _should_raise_strict_errors(self) -> bool:
"""Check if strict error handling is enabled via config."""
env_value = os.getenv("STRICT_PERSISTENCE_ERRORS", "false").lower()
if env_value in {"true", "1", "yes"}:
return True
if self.config:
value = self.config.get("session.dangerous_command_prevention_enabled")
return bool(value) if value is not None else False
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/test_config_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def functional_backend() -> str:
from fastapi import FastAPI
from fastapi.testclient import TestClient
from src.core.app.test_builder import build_test_app as build_app
from src.core.common.exceptions import ConfigurationError, JSONParsingError
from src.core.common.exceptions import (
ConfigurationError,
JSONParsingError,
ServiceResolutionError,
)
from src.core.config.app_config import load_config
from src.core.persistence import ConfigManager

Expand Down Expand Up @@ -227,6 +231,34 @@ def test_apply_default_backend_invalid_backend_still_raises_with_cli_override(
}


def test_apply_default_backend_strict_errors_env(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""STRICT_PERSISTENCE_ERRORS should force DI resolution failures to raise."""

class _StrictAppState(_DummyAppState):
def set_backend_type(self, backend_type: str | None) -> None: # type: ignore[override]
self.backend_type = backend_type

class _FailingProvider:
def get_required_service(self, _service_type): # type: ignore[no-untyped-def]
raise ServiceResolutionError("boom")

monkeypatch.setenv("STRICT_PERSISTENCE_ERRORS", "true")

manager = ConfigManager(
FastAPI(),
path=str(tmp_path / "config.json"),
service_provider=_FailingProvider(),
app_state=_StrictAppState(),
)

with pytest.raises(ServiceResolutionError):
manager._apply_default_backend("openai")

monkeypatch.delenv("STRICT_PERSISTENCE_ERRORS", raising=False)


def test_load_raises_json_parsing_error_for_invalid_json(tmp_path: Path) -> None:
cfg_path = tmp_path / "config.json"
cfg_path.write_text("{not: valid json}")
Expand Down