File size: 2,609 Bytes
7302343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Engine settings — loaded from environment / .env.

Spec: docs/Specs.md §5, docs/13-Infra.md
Strict-mode refusal (no GEMINI_API_KEY → exit) lands in F-0.7.
"""

from functools import lru_cache
from typing import Literal

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
    )

    # === Runtime ===
    env: Literal["development", "staging", "production"] = "development"
    log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"

    # === LLM ===
    gemini_api_key: str = ""
    model_reasoner: str = "gemini-2.5-pro"
    model_summarizer: str = "gemini-2.5-flash"

    # === Datastores ===
    database_url: str = "postgresql+asyncpg://modpilot:modpilot@localhost:5432/modpilot"
    redis_url: str = "redis://localhost:6379/0"

    # === HMAC ===
    engine_shared_secret: str = ""

    # === Budgets ===
    daily_spend_cap_usd: float = Field(default=15.0, ge=0)
    daily_spend_cap_per_sub_usd: float = Field(default=2.0, ge=0)

    # === Feature flags ===
    enable_live_llm_tests: bool = False

    @property
    def is_dev(self) -> bool:
        return self.env == "development"

    @property
    def hmac_enforced(self) -> bool:
        """In dev with no secret set, middleware logs and lets requests through."""
        return bool(self.engine_shared_secret) and not self.is_dev

    def validate_for_runtime(self) -> None:
        """Refuse to boot in prod when load-bearing secrets are missing (F-0.7).

        In dev we warn but proceed — keeps the local loop fast. In staging/prod
        the engine fail-closes per docs/Specs.md §13.1 rather than serving
        verdicts against an unconfigured LLM provider.
        """
        if not self.gemini_api_key:
            if self.is_dev:
                # Dev: deferred — the GeminiClient itself raises when actually
                # instantiated, so health probes and unit tests still pass.
                return
            raise RuntimeError(
                f"GEMINI_API_KEY is required in env={self.env}. Set it in engine/.env "
                "or via deployment secrets. See docs/13-Infra.md."
            )
        if not self.is_dev and not self.engine_shared_secret:
            raise RuntimeError(
                f"ENGINE_SHARED_SECRET is required in env={self.env} (HMAC enforcement)."
            )


@lru_cache(maxsize=1)
def get_settings() -> Settings:
    return Settings()