From 1f52d5f6b3726ec571bbc86abe9de55f90339801 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 29 Nov 2026 12:40:26 +0100 Subject: [PATCH] Keep a logged-out provider out of free-best when OmniRoute resets its status OmniRoute set kilo-gateway's testStatus back from 'expired'; the next health tick re-synced it into the combo and its first request answered 401 again (2026-11-29 12:37). A login now also counts as gone while its keys read 'invalid' or its last error is 'unauthorized' and newer than its last success. Bug-class-check: dead-provider check read one field OmniRoute resets; now also the sticky key health and last error (todoImplementer T2629) Co-Authored-By: Claude Opus 5.5 --- .../omniroute_offload/_gw_combo.py | 29 +++++++++++++++++-- .../omniroute_offload/tests/test_gw_combo.py | 22 ++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/omniroute_offload/omniroute_offload/_gw_combo.py b/omniroute_offload/omniroute_offload/_gw_combo.py index 7be5652..914653e 100644 --- a/omniroute_offload/omniroute_offload/_gw_combo.py +++ b/omniroute_offload/omniroute_offload/_gw_combo.py @@ -33,15 +33,40 @@ MIN_TETRIS = 0.85 # 35k+ tokens and grows, so one 32k member failed every turn with "Input exceeds # context window" (todoImplementer T2628). MIN_CONTEXT = 100_000 -# testStatus of a login that is gone: kuhy reconnects it +# testStatus (or lastErrorType) of a login that is gone: kuhy reconnects it DEAD = frozenset({"expired", "unauthorized"}) +def _logged_out(conn: dict[str, Any]) -> bool: + """Say whether a connection's login is gone, by the marks OmniRoute does not reset. + + OmniRoute sets testStatus back from "expired" now and then; a sync then took the + dead kilo-gateway back, and its very next request answered 401 (2026-11-29 + 12:37). Its last error ("unauthorized", newer than its last success) and an + "invalid" key stay until kuhy reconnects. + """ + if conn.get("testStatus") in DEAD: + return True + data = conn.get("providerSpecificData") + keys = data.get("apiKeyHealth") if isinstance(data, dict) else None + keys = list(keys.values()) if isinstance(keys, dict) else [] + success = max( + (str(k.get("lastSuccess") or "") for k in keys if isinstance(k, dict)), + default="", + ) + if keys and all(isinstance(k, dict) and k.get("status") == "invalid" for k in keys): + return True + return ( + conn.get("lastErrorType") in DEAD + and str(conn.get("lastErrorAt") or "") > success + ) + + def dead_providers(conns: Iterable[dict[str, Any]]) -> set[str]: """Return providers with connections but none usable (inactive or logged out).""" usable: dict[str, bool] = {} for conn in conns: - live = bool(conn.get("isActive")) and conn.get("testStatus") not in DEAD + live = bool(conn.get("isActive")) and not _logged_out(conn) name = str(conn.get("provider")) usable[name] = usable.get(name, False) or live return {name for name, live in usable.items() if not live} diff --git a/omniroute_offload/omniroute_offload/tests/test_gw_combo.py b/omniroute_offload/omniroute_offload/tests/test_gw_combo.py index 33883d2..3578e6f 100644 --- a/omniroute_offload/omniroute_offload/tests/test_gw_combo.py +++ b/omniroute_offload/omniroute_offload/tests/test_gw_combo.py @@ -158,6 +158,28 @@ def test_dead_providers_are_those_with_no_usable_connection() -> None: assert combo.dead_providers(conns) == {"kilo", "off", "nope"} +def test_a_login_stays_dead_when_omniroute_resets_its_test_status() -> None: + """Live 2026-11-29 12:37: testStatus read active, the next kilo request got 401.""" + health = {"primary": {"status": "invalid", "lastSuccess": "2026-11-29T11:01"}} + invalid = {**_conn("kilo"), "providerSpecificData": {"apiKeyHealth": health}} + errored = { + **_conn("err"), + "lastErrorType": "unauthorized", + "lastErrorAt": "2026-11-29T11:37", + } + ok_key = {"primary": {"status": "active", "lastSuccess": "2026-11-29T12:00"}} + fixed = { + **errored, + "provider": "fixed", + "providerSpecificData": {"apiKeyHealth": ok_key}, + } + limited = {**_conn("busy"), "lastErrorType": "rate_limited", "lastErrorAt": "9"} + odd = {**_conn("odd"), "providerSpecificData": {"apiKeyHealth": "?"}} + conns = [invalid, errored, fixed, limited, odd] + # reconnected since its 401, or only rate limited: both stay + assert combo.dead_providers(conns) == {"kilo", "err"} + + def test_refresh_leaves_out_a_provider_whose_login_expired( cfg: gwc.Config, admins: list[FakeAdmin], capsys: pytest.CaptureFixture[str] ) -> None: