Coverage for execution/runtime_config_io.py: 100%
16 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""IO layer for reading phase_v2.runtime.mode from config.json.
3Mirrors risk/manual_override.py::read_manual_trade_lock_override()'s
4pattern exactly, including the mtime-gated cache (see
5execution/config_cache.py). Used by retraining/worker.py's
6auto-promote-in-live safety net (Phase V2-22), whose poll loop calls this
7far more often than config.json actually changes - a process outside
8main.py (which has its own copy of this same read, see main.py's
9runtime_mode resolution in initialize()/_refresh_risk_state()) needs its
10own read since it never shares main.py's in-memory state.
11"""
13from __future__ import annotations
15import json
16from pathlib import Path
18from execution.config_cache import read_cached
19from execution.order_gate import resolve_runtime_mode
21_PHASE_V2_KEY = "phase_v2"
22_RUNTIME_KEY = "runtime"
25def read_runtime_mode(config_path: Path) -> str:
26 """Mtime-gated read of phase_v2.runtime.mode, normalized via
27 resolve_runtime_mode() (fails safe to 'observation' if the file is
28 missing or the value is absent/unknown) - never raises."""
29 return read_cached(config_path, _read_runtime_mode_uncached)
32def _read_runtime_mode_uncached(config_path: Path) -> str:
33 if not config_path.exists():
34 return resolve_runtime_mode(None)
35 with config_path.open("r", encoding="utf-8") as f:
36 config = json.load(f)
37 raw_mode = config.get(_PHASE_V2_KEY, {}).get(_RUNTIME_KEY, {}).get("mode")
38 return resolve_runtime_mode(raw_mode)