diff --git a/api/main.py b/api/main.py index 5cdc3a0..203ca4e 100644 --- a/api/main.py +++ b/api/main.py @@ -257,6 +257,13 @@ class DashboardState: self.watch_inject = watch_inject self._lock = threading.Lock() self._research_lock = threading.Lock() + # lazily-built, process-wide SSE change signal (shared by all clients) + self._change_signal: _ChangeSignal | None = None + # backtest replay cache (plan 015) — a DEDICATED lock, not self._lock, + # so a slow replay (10-30s) never blocks the live snapshot/SSE path. + # key = (since, until, repr(settings)) -> (result, monotonic timestamp). + self._backtest_lock = threading.Lock() + self._backtest_cache: dict = {} def _research(self): """The research DB connection, opened LAZILY and self-healing. It used @@ -276,13 +283,6 @@ class DashboardState: logger.warning("research DB open failed (charts empty): %s", exc) self.research_con = None return self.research_con - # lazily-built, process-wide SSE change signal (shared by all clients) - self._change_signal: _ChangeSignal | None = None - # backtest replay cache (plan 015) — a DEDICATED lock, not self._lock, - # so a slow replay (10-30s) never blocks the live snapshot/SSE path. - # key = (since, until, repr(settings)) -> (result, monotonic timestamp). - self._backtest_lock = threading.Lock() - self._backtest_cache: dict = {} def change_stamp(self): """SSE change token from the shared once-per-second signal (see _ChangeSignal).""" diff --git a/tests/unit/api/test_chart_endpoint.py b/tests/unit/api/test_chart_endpoint.py index be62ad2..a40de6a 100644 --- a/tests/unit/api/test_chart_endpoint.py +++ b/tests/unit/api/test_chart_endpoint.py @@ -93,7 +93,11 @@ def _get(state, path): # Step 3: DashboardState.chart() — bars, markers, levels # -------------------------------------------------------------------------- -def test_chart_no_research_con_reports_missing_db(): +def test_chart_no_research_con_reports_missing_db(monkeypatch): + # self-heal (2026-07-20): None con now REOPENS lazily; the "missing db" + # path is reached only when the reopen ALSO fails. + import api.main as m + monkeypatch.setattr(m, "open_research_db", lambda name: (_ for _ in ()).throw(RuntimeError("db down"))) out = DashboardState("momentum", research_con=None).chart("AAPL") assert out["bars"] == [] assert out.get("reason") == "no research db attached" diff --git a/tests/unit/api/test_dashboard_sparklines.py b/tests/unit/api/test_dashboard_sparklines.py index b08f91f..e4422d8 100644 --- a/tests/unit/api/test_dashboard_sparklines.py +++ b/tests/unit/api/test_dashboard_sparklines.py @@ -20,7 +20,11 @@ class _FakeResearchCon: return _FakeResult() -def test_bars_without_reader_reports_missing_db(): +def test_bars_without_reader_reports_missing_db(monkeypatch): + # self-heal (2026-07-20): None con reopens lazily; "missing db" only when + # the reopen also fails. + import api.main as m + monkeypatch.setattr(m, "open_research_db", lambda name: (_ for _ in ()).throw(RuntimeError("db down"))) out = DashboardState("momentum", research_con=None).bars("AAPL") assert out["bars"] == [] assert out.get("reason") == "no research db attached"