From 4a9c2e03e9b13a73c924c5c8e652919d2963bf2b Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 29 Nov 2026 12:24:36 +0100 Subject: [PATCH] Keep free-best members big enough for a board turn OmniRoute caps a combo's input at its smallest member: one 32k model (SEA-LION) made every 35k pi board turn fail 'Input exceeds context window'. A member's limit is now the smaller of order.tsv's and OmniRoute's registry (/v1/models), and the combo floor is 100k. Bug-class-check: combo trusted stale order.tsv limits; registry limit now read live, floor enforced (todoImplementer T2628) Co-Authored-By: Claude Opus 5.5 --- .../omniroute_offload/_gw_api.py | 11 ++++++ .../omniroute_offload/_gw_combo.py | 32 ++++++++++++----- .../tests/test_gw_api_admin.py | 14 ++++++++ .../omniroute_offload/tests/test_gw_combo.py | 35 +++++++++++++++++-- 4 files changed, 82 insertions(+), 10 deletions(-) diff --git a/omniroute_offload/omniroute_offload/_gw_api.py b/omniroute_offload/omniroute_offload/_gw_api.py index d0d2359..1db65df 100644 --- a/omniroute_offload/omniroute_offload/_gw_api.py +++ b/omniroute_offload/omniroute_offload/_gw_api.py @@ -112,6 +112,17 @@ class Admin: got = self._call("/api/providers") return list(got.get("connections", got) if isinstance(got, dict) else got) + def contexts(self) -> dict[str, int]: + """Return each model's input limit as OmniRoute's registry knows it.""" + got = self._call("/v1/models") + rows = got.get("data", []) if isinstance(got, dict) else got + found: dict[str, int] = {} + for row in rows: + limit = row.get("max_input_tokens") or row.get("context_length") + if isinstance(limit, int) and not isinstance(limit, bool) and row.get("id"): + found[str(row["id"])] = limit + return found + def combo_members(self, name: str = COMBO) -> set[str]: """Return provider ids the combo routes to (empty when it is missing).""" got = self._call("/api/combos") diff --git a/omniroute_offload/omniroute_offload/_gw_combo.py b/omniroute_offload/omniroute_offload/_gw_combo.py index cb57c2e..7be5652 100644 --- a/omniroute_offload/omniroute_offload/_gw_combo.py +++ b/omniroute_offload/omniroute_offload/_gw_combo.py @@ -12,6 +12,7 @@ set changes, so a reconnected provider comes back by itself. from __future__ import annotations import csv +import math import sys from typing import TYPE_CHECKING, Any @@ -21,13 +22,17 @@ from omniroute_offload import _gw_health as health from omniroute_offload import _gw_notify as note if TYPE_CHECKING: - from collections.abc import Iterable + from collections.abc import Iterable, Mapping from pathlib import Path from omniroute_offload._gw_config import Config MIN_PROMPT = 16_000 MIN_TETRIS = 0.85 +# OmniRoute caps a combo's input at its smallest member's limit; a board turn sends +# 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 DEAD = frozenset({"expired", "unauthorized"}) @@ -42,9 +47,17 @@ def dead_providers(conns: Iterable[dict[str, Any]]) -> set[str]: return {name for name, live in usable.items() if not live} -def ranked_models(order: Path, dead: Iterable[str] = ()) -> list[str]: - """Return agent-capable models from order.tsv that pass the filters, best first.""" - gone = set(dead) +def ranked_models( + order: Path, + dead: Iterable[str] = (), + contexts: Mapping[str, int] | None = None, + floor: int = MIN_PROMPT, +) -> list[str]: + """Return agent-capable models from order.tsv that pass the filters, best first. + + A model's input limit: the smaller of order.tsv's measured one and OmniRoute's. + """ + gone, known = set(dead), contexts or {} with order.open(newline="") as handle: rows = list(csv.DictReader(handle, delimiter="\t")) return [ @@ -53,7 +66,8 @@ def ranked_models(order: Path, dead: Iterable[str] = ()) -> list[str]: if row.get("tools") == "yes" and row.get("tetris", "-") != "-" and float(row["tetris"]) >= MIN_TETRIS - and float(row.get("max_prompt") or 0) >= MIN_PROMPT + and min(float(row.get("max_prompt") or 0), known.get(row["model"], math.inf)) + >= floor and row["model"].split("/", 1)[0] not in gone ] @@ -73,11 +87,13 @@ def _ranked(cfg: Config) -> bool: def _wanted(cfg: Config, admin: api.Admin) -> tuple[list[str], set[str]] | None: """Return the combo and the dead providers left out; None (logged) if all dead.""" dead = dead_providers(admin.providers()) - models = ranked_models(cfg.conf_dir / "order.tsv", dead) + models = ranked_models( + cfg.conf_dir / "order.tsv", dead, admin.contexts(), MIN_CONTEXT + ) if not models: print( - "combo kept: no ranked provider has a live login" - f" ({', '.join(sorted(dead))})", + f"combo kept: no ranked model has a live login and {MIN_CONTEXT} input" + f" tokens (dead: {', '.join(sorted(dead)) or 'none'})", file=sys.stderr, ) return None diff --git a/omniroute_offload/omniroute_offload/tests/test_gw_api_admin.py b/omniroute_offload/omniroute_offload/tests/test_gw_api_admin.py index 2a705ac..1b7e415 100644 --- a/omniroute_offload/omniroute_offload/tests/test_gw_api_admin.py +++ b/omniroute_offload/omniroute_offload/tests/test_gw_api_admin.py @@ -109,6 +109,20 @@ def test_combo_models_lists_the_combo_in_priority_order( assert adm.combo_models() == [] +def test_contexts_reads_each_models_input_limit(admin: tuple[api.Admin, Net]) -> None: + adm, net = admin + rows = [ + {"id": "nv/a", "context_length": 128000}, + {"id": "sea/b", "context_length": 128000, "max_input_tokens": 32768}, + {"id": "x/none"}, + {"id": "x/flag", "context_length": True}, + {"context_length": 5}, + ] + net.replies = [{"data": rows}, []] + assert adm.contexts() == {"nv/a": 128000, "sea/b": 32768} + assert adm.contexts() == {} + + def test_reactivate_puts_active(admin: tuple[api.Admin, Net]) -> None: adm, net = admin net.replies = [{}] diff --git a/omniroute_offload/omniroute_offload/tests/test_gw_combo.py b/omniroute_offload/omniroute_offload/tests/test_gw_combo.py index 0e36458..33883d2 100644 --- a/omniroute_offload/omniroute_offload/tests/test_gw_combo.py +++ b/omniroute_offload/omniroute_offload/tests/test_gw_combo.py @@ -41,6 +41,7 @@ def cfg(tmp_path: Path) -> gwc.Config: CONNS: list[dict[str, Any]] = [] CURRENT: list[str] = [] +CONTEXTS: dict[str, int] = {} class FakeAdmin: @@ -54,6 +55,9 @@ class FakeAdmin: def combo_models(self, name: str = api.COMBO) -> list[str]: return CURRENT + def contexts(self) -> dict[str, int]: + return CONTEXTS + def upsert_combo(self, name: str, models: list[str]) -> None: self.upserts.append((name, models)) @@ -63,6 +67,7 @@ def admins(monkeypatch: pytest.MonkeyPatch) -> list[FakeAdmin]: made: list[FakeAdmin] = [] CONNS.clear() CURRENT.clear() + CONTEXTS.clear() def factory(cfg: gwc.Config, password: str) -> FakeAdmin: made.append(FakeAdmin(cfg, password)) @@ -101,7 +106,7 @@ def test_ranked_models_without_tetris_column_keeps_nothing(tmp_path: Path) -> No def test_refresh_upserts_filtered_combo( cfg: gwc.Config, admins: list[FakeAdmin], capsys: pytest.CaptureFixture[str] ) -> None: - _write(cfg, HEADER + "x/one\tyes\t0.9\t20000\nx/two\tno\t0.9\t20000\n") + _write(cfg, HEADER + "x/one\tyes\t0.9\t128000\nx/two\tno\t0.9\t128000\n") assert combo.refresh(cfg) is True assert admins[0].pw == "pw" assert admins[0].upserts == [(api.COMBO, ["x/one"])] @@ -161,7 +166,7 @@ def test_refresh_leaves_out_a_provider_whose_login_expired( HEADER + "nv/a\tyes\t1.0\t128000\n" + "kilo/auto\tyes\t1.0\t128000\n" - + "or/b\tyes\t0.9\t64000\n", + + "or/b\tyes\t0.9\t128000\n", ) CONNS.extend([_conn("nv"), _conn("kilo", "expired"), _conn("or")]) assert combo.refresh(cfg) is True @@ -235,3 +240,29 @@ def test_keep_live_survives_an_unreachable_api( monkeypatch.setattr(api, "Admin", refused) assert combo.keep_live(cfg) is False assert "combo sync skipped: refused" in capsys.readouterr().err + + +def test_refresh_leaves_out_a_model_under_the_context_floor( + cfg: gwc.Config, admins: list[FakeAdmin], capsys: pytest.CaptureFixture[str] +) -> None: + """OmniRoute caps a combo at its smallest member: a 32k one failed 35k turns.""" + _write( + cfg, + HEADER + + "nv/big\tyes\t1.0\t128000\n" + + "sea/small\tyes\t0.9\t128000\n" + + "groq/measured\tyes\t0.9\t16000\n" + + "or/unknown\tyes\t0.9\t128000\n", + ) + CONTEXTS.update({"nv/big": 128000, "sea/small": 32768, "groq/measured": 400000}) + assert combo.refresh(cfg) is True + assert admins[0].upserts == [(api.COMBO, ["nv/big", "or/unknown"])] + + +def test_refresh_keeps_the_combo_when_no_model_is_big_enough( + cfg: gwc.Config, admins: list[FakeAdmin], capsys: pytest.CaptureFixture[str] +) -> None: + _write(cfg, HEADER + "sea/small\tyes\t0.9\t32000\n") + assert combo.refresh(cfg) is False + assert admins[0].upserts == [] + assert "dead: none" in capsys.readouterr().err