From e3142017c9f1ce0c8970a68d8ba7ece6361b5328 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 12 Sep 2026 13:34:51 +0000 Subject: [PATCH] fix(account-snapshot): treat timeout/http5xx as transient fallback failures Co-authored-by: genichin <4282227+genichin@users.noreply.github.com> --- tests/application/test_account_snapshot.py | 15 +++++++++++++-- trex_core/application/account_snapshot.py | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/application/test_account_snapshot.py b/tests/application/test_account_snapshot.py index 7a171e5..45332ec 100644 --- a/tests/application/test_account_snapshot.py +++ b/tests/application/test_account_snapshot.py @@ -75,10 +75,11 @@ def test_refresh_persists_independent_holdings_and_cash_capture_times(): assert second.cash_captured_at == first.cash_captured_at -def test_sell_falls_back_only_for_transport_failure_and_within_sell_ttl(): +@pytest.mark.parametrize("error_code", ["transport_failed", "timeout", "http_5xx"]) +def test_sell_falls_back_for_transient_live_read_failures_within_sell_ttl(error_code): broker, _, service, now = _wire() assert service.refresh() - broker.balance_error = "transport_failed" + broker.balance_error = error_code now[0] += timedelta(seconds=601) assert service.holdings_for_sell() == [BalanceItem("005930", 2)] @@ -126,6 +127,16 @@ def test_buy_guard_accounts_for_existing_pending_cash_and_never_renews_age(): ) +@pytest.mark.parametrize("error_code", ["transport_failed", "timeout", "http_5xx"]) +def test_buy_guard_keeps_existing_cash_snapshot_on_transient_cash_refresh_failure(error_code): + broker, _, service, _ = _wire() + assert service.refresh() + broker.cash_error = error_code + + assert service.refresh() is False + assert service.buy_guard(qty=2, limit_price=100_000) is None + + def test_current_account_hash_is_required_on_every_consume(): broker, _, service, _ = _wire() assert service.refresh() diff --git a/trex_core/application/account_snapshot.py b/trex_core/application/account_snapshot.py index c42439b..a31a82b 100644 --- a/trex_core/application/account_snapshot.py +++ b/trex_core/application/account_snapshot.py @@ -217,7 +217,7 @@ class AccountSnapshotService: try: return self._broker.get_balance() except BrokerBalanceError as exc: - if exc.code != "transport_failed": + if exc.code not in {"transport_failed", "timeout", "http_5xx"}: raise AccountSnapshotUnavailable( "live_read_failed", detail_code=exc.code ) from None @@ -253,7 +253,7 @@ class AccountSnapshotService: reserved_cash: float = 0.0, ) -> Optional[str]: """Return a stable rejection reason, or None when bounded admission passes.""" - if self._last_cash_error not in {None, "transport_failed"}: + if self._last_cash_error not in {None, "transport_failed", "timeout", "http_5xx"}: return "account_snapshot_unavailable" try: snapshot = self._load_current()