refactor: fold simplify-code review findings

- extract _commit_registry/_note_refresh_failure shared by the background
  worker and foreground stage-4 (identical 4-step success + failure paths
  were duplicated); worker now commits under _models_dev_fetch_lock so a
  failing background refresh can never re-arm the backoff immediately
  after a successful force_refresh committed (unsynchronized-write race)
- add should_clear_context_pin_async to hermes_cli/route_identity.py
  (matching the get_model_context_length_async precedent) and use it at
  the 4 async gateway sites instead of inline asyncio.to_thread wraps;
  the sync _format_session_info site keeps the sync call (already
  off-loop via its callers' to_thread)
- test the background-refresh success path (the PR's primary new
  behavior): disk saved, mem cache swapped, backoff cleared, in_flight
  reset — mutation-checked
- replace the race-prone spin-wait on _models_dev_refresh_in_flight with
  a named-thread join in the backoff test
This commit is contained in:
kshitijk4poor
2026-07-29 16:32:59 +05:00
committed by kshitij
parent ccf7129ed0
commit 222ea2b6c9
5 changed files with 109 additions and 52 deletions
+28
View File
@@ -74,3 +74,31 @@ def should_clear_context_pin(
)
except Exception:
return True
async def should_clear_context_pin_async(
configured_model: Any,
active_model: Any,
configured_base_url: Any,
active_base_url: Any,
configured_provider: Any,
active_provider: Any,
) -> bool:
"""Async wrapper for ``should_clear_context_pin``.
Offloads the route comparison to a worker thread so async gateway
handlers never run it on the event loop — the resolution chain is
cache-only (``allow_network=False``) but can still do cold-start disk
I/O. Shares all logic with the sync version — no code duplication.
"""
import asyncio
return await asyncio.to_thread(
should_clear_context_pin,
configured_model,
active_model,
configured_base_url,
active_base_url,
configured_provider,
active_provider,
)