fix(gateway): prefer active status model override

This commit is contained in:
fangliquanflq
2026-09-13 01:23:55 +08:00
committed by Teknium
parent 280ede95a7
commit 08a0cbe305
2 changed files with 69 additions and 6 deletions
+12 -5
View File
@@ -80,11 +80,13 @@ def _quiet_sync(call, default=None):
return default
def _status_model_route(status_agent, persisted_route: dict, session_row: dict, session_entry):
def _status_model_route(
status_agent, active_override: dict, persisted_route: dict, session_row: dict, session_entry
):
"""``(model, provider, context_used, context_total)`` for /status.
Order: live/cached agent route -> persisted recent route -> SessionDB row -> gateway config
(only loaded when something is still missing).
Order: live/cached agent route -> active session override -> persisted recent route ->
SessionDB row -> gateway config (only loaded when something is still missing).
"""
from gateway.run import _AGENT_PENDING_SENTINEL, _load_gateway_config, _resolve_gateway_model
context_used = context_total = 0
@@ -96,6 +98,8 @@ def _status_model_route(status_agent, persisted_route: dict, session_row: dict,
if ctx is not None:
context_used = max(0, _int_value(getattr(ctx, "last_prompt_tokens", 0)))
context_total = _int_value(getattr(ctx, "context_length", 0))
routes.append((_clean_str(active_override.get("model")),
_clean_str(active_override.get("provider"))))
routes.append((_clean_str(persisted_route.get("model")),
_clean_str(persisted_route.get("billing_provider"))))
row_route = (_clean_str(session_row.get("model")), _clean_str(session_row.get("billing_provider")))
@@ -231,10 +235,13 @@ class GatewayStatusCommandsMixin:
session_entry.session_id
)
# Prefer the live or cached agent (actual runtime route + context compressor); fall back
# to SessionDB metadata + last_prompt_tokens so /status stays useful between turns.
# to an active /model override, then SessionDB metadata + last_prompt_tokens so /status
# stays useful between turns. Rehydrate first so this precedence survives gateway restarts.
status_agent = agent if is_running else self._cached_agent_for(session_key)
self._rehydrate_session_model_override(session_key)
active_override = self._session_model_override(session_key) or {}
model_name, provider_name, context_used, context_total = _status_model_route(
status_agent, persisted_route, session_row, session_entry
status_agent, active_override, persisted_route, session_row, session_entry
)
fields = build_status_fields(
+57 -1
View File
@@ -10,7 +10,13 @@ import pytest
from gateway.config import GatewayConfig, Platform, PlatformConfig
from gateway.platforms.event import MessageEvent
from gateway.session import SessionEntry, SessionSource, build_session_key
from gateway.session import (
AsyncSessionStore,
SessionEntry,
SessionSource,
SessionStore,
build_session_key,
)
def _make_source(platform: Platform = Platform.TELEGRAM) -> SessionSource:
@@ -189,6 +195,56 @@ async def test_status_command_uses_most_recent_persisted_model_route(tmp_path):
db.close()
@pytest.mark.asyncio
async def test_status_command_prefers_rehydrated_session_model_override(tmp_path):
"""A committed /model switch is current before the selected model records usage."""
source = _make_source()
store = SessionStore(sessions_dir=tmp_path / "sessions", config=GatewayConfig())
session_entry = store.get_or_create_session(source)
runner = _make_runner(session_entry)
runner.session_store = store
runner._async_session_store = AsyncSessionStore(store)
db = SessionDB(db_path=tmp_path / "state.db")
runner._session_db = AsyncSessionDB(db)
try:
db.create_session(session_entry.session_id, "telegram", model="model-a")
db.update_token_counts(
session_entry.session_id,
model="model-a",
billing_provider="provider-a",
input_tokens=480,
api_call_count=48,
)
result = SimpleNamespace(
new_model="model-b",
target_provider="provider-b",
provider_label="Provider B",
api_key="secret",
base_url="https://b.example/v1",
api_mode=None,
request_overrides={},
runtime_capabilities={},
)
switch_ctx = SimpleNamespace(
session_key=session_entry.session_key,
current_model="model-a",
persist_global=False,
restore_snapshot=None,
)
await runner._record_model_switch(
result, switch_ctx, source=source, one_turn=False, picker=False
)
# Simulate a restart: /status must lazily recover the durable override.
runner._session_state(session_entry.session_key).conversation.model_override = None
status = await runner._handle_message(_make_event("/status"))
assert "**Model:** `model-b` (provider-b)" in status
assert "**Model:** `model-a` (provider-a)" not in status
finally:
db.close()
@pytest.mark.asyncio
async def test_agents_command_reports_active_agents_and_processes(monkeypatch):
session_key = build_session_key(_make_source())