Refresh stale benchmark data without blocking normal routing. Routing should never depend on a live network request, but benchmark priors should not silently become static constants either. The default path kicks off a daemon refresh and keeps serving the current cache for
(self, *, background: bool = True, force: bool = False)
| 319 | return False |
| 320 | |
| 321 | def refresh_if_stale(self, *, background: bool = True, force: bool = False) -> bool: |
| 322 | """Refresh stale benchmark data without blocking normal routing. |
| 323 | |
| 324 | Routing should never depend on a live network request, but benchmark |
| 325 | priors should not silently become static constants either. The default |
| 326 | path kicks off a daemon refresh and keeps serving the current cache for |
| 327 | this request; later requests see fresher data if the refresh succeeds. |
| 328 | """ |
| 329 | if not force and not self.needs_refresh(): |
| 330 | return False |
| 331 | |
| 332 | setting = os.environ.get("UNCOMMON_ROUTE_BENCHMARK_AUTO_REFRESH", "1").strip().lower() |
| 333 | if setting in {"0", "false", "no", "off"} and not force: |
| 334 | return False |
| 335 | |
| 336 | if not background: |
| 337 | asyncio.run(self.refresh(force=force)) |
| 338 | return True |
| 339 | |
| 340 | thread = self._refresh_thread |
| 341 | if thread is not None and thread.is_alive(): |
| 342 | return False |
| 343 | |
| 344 | def _runner() -> None: |
| 345 | try: |
| 346 | asyncio.run(self.refresh(force=force)) |
| 347 | except Exception as exc: |
| 348 | logger.warning("Benchmark background refresh failed: %s", exc) |
| 349 | |
| 350 | self._refresh_thread = threading.Thread( |
| 351 | target=_runner, |
| 352 | name="uncommon-route-benchmark-refresh", |
| 353 | daemon=True, |
| 354 | ) |
| 355 | self._refresh_thread.start() |
| 356 | return True |
| 357 | |
| 358 | def get_quality(self, model_id: str, category: str = "") -> float: |
| 359 | return self.get_quality_estimate(model_id, category).score |