mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-30 06:05:35 +01:00
Some checks are pending
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
omniroute_offload pre-commit / pre-commit (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
The health tick re-syncs the combo when a login dies or comes back, so a dead kilo-gateway (401) no longer reaches free turns as a hard error. Bug-class-check: combo built from order.tsv alone trusted every ranked provider; now gated on live connections (T2624) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
4.7 KiB
Python
132 lines
4.7 KiB
Python
"""Rebuild the free-best combo from the benchmark's order.tsv in conf_dir.
|
|
|
|
Keeps tool-capable models with a big enough prompt window and Tetris score, in
|
|
the file's rank order (the combo is a priority chain), and drops every model of
|
|
a provider whose connections are all inactive or logged out: OmniRoute hands
|
|
such a member's 401 to the caller instead of the next model's answer (a
|
|
kilo-gateway login expired on 2026-11-29 and failed a third of the board's free
|
|
turns). `keep_live` runs after each health tick and re-syncs the combo when that
|
|
set changes, so a reconnected provider comes back by itself.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import sys
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from omniroute_offload import _gw_api as api
|
|
from omniroute_offload import _gw_config as gwc
|
|
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 pathlib import Path
|
|
|
|
from omniroute_offload._gw_config import Config
|
|
|
|
MIN_PROMPT = 16_000
|
|
MIN_TETRIS = 0.85
|
|
# testStatus of a login that is gone: kuhy reconnects it
|
|
DEAD = frozenset({"expired", "unauthorized"})
|
|
|
|
|
|
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
|
|
name = str(conn.get("provider"))
|
|
usable[name] = usable.get(name, False) or live
|
|
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)
|
|
with order.open(newline="") as handle:
|
|
rows = list(csv.DictReader(handle, delimiter="\t"))
|
|
return [
|
|
row["model"]
|
|
for row in rows
|
|
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 row["model"].split("/", 1)[0] not in gone
|
|
]
|
|
|
|
|
|
def _ranked(cfg: Config) -> bool:
|
|
"""Say whether order.tsv exists and ranks a model; log why not (needs no login)."""
|
|
order = cfg.conf_dir / "order.tsv"
|
|
if not order.exists():
|
|
print(f"combo kept: no {order}", file=sys.stderr)
|
|
return False
|
|
if not ranked_models(order):
|
|
print(f"combo kept: no model in {order} passes the filter", file=sys.stderr)
|
|
return False
|
|
return True
|
|
|
|
|
|
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)
|
|
if not models:
|
|
print(
|
|
"combo kept: no ranked provider has a live login"
|
|
f" ({', '.join(sorted(dead))})",
|
|
file=sys.stderr,
|
|
)
|
|
return None
|
|
return models, dead
|
|
|
|
|
|
def refresh(cfg: Config) -> bool:
|
|
"""Upsert the combo; log and return False when order.tsv is missing or empty."""
|
|
if not _ranked(cfg):
|
|
return False
|
|
admin = api.Admin(cfg, health.secret(cfg, "admin_password"))
|
|
wanted = _wanted(cfg, admin)
|
|
if wanted is None:
|
|
return False
|
|
models, dead = wanted
|
|
admin.upsert_combo(api.COMBO, models)
|
|
left_out = f" (left out, no live login: {', '.join(sorted(dead))})" if dead else ""
|
|
print(f"{api.COMBO}: {' > '.join(models)}{left_out}", file=sys.stderr)
|
|
return True
|
|
|
|
|
|
def keep_live(cfg: Config) -> bool:
|
|
"""Re-sync the combo when a provider login died or came back; True if it changed.
|
|
|
|
Skips while an update holds the instance lock; an unreachable API changes nothing.
|
|
"""
|
|
with gwc.locked(cfg, blocking=False) as got:
|
|
if not got or not _ranked(cfg):
|
|
return False
|
|
try:
|
|
admin = api.Admin(cfg, health.secret(cfg, "admin_password"))
|
|
wanted = _wanted(cfg, admin)
|
|
if wanted is None or wanted[0] == admin.combo_models():
|
|
return False
|
|
models, dead = wanted
|
|
admin.upsert_combo(api.COMBO, models)
|
|
except (api.ApiError, OSError) as err:
|
|
print(f"combo sync skipped: {err}", file=sys.stderr)
|
|
return False
|
|
left_out = (
|
|
f"; no live login: {', '.join(sorted(dead))} (reconnect in the dashboard)"
|
|
if dead
|
|
else ""
|
|
)
|
|
print(f"combo synced: {' > '.join(models)}{left_out}", file=sys.stderr)
|
|
note.notify(
|
|
cfg,
|
|
f"free-best re-synced{left_out or ': every provider is back'}",
|
|
urgency="low",
|
|
)
|
|
return True
|