Skip redundant model switch

This commit is contained in:
IAvecilla 2026-06-12 11:59:15 -03:00 committed by Teknium
parent 8c3c08c50b
commit bc3f4ed70f
2 changed files with 38 additions and 0 deletions

View file

@ -1133,6 +1133,37 @@ def test_config_sync_noop_when_config_unchanged(monkeypatch):
server._sync_agent_model_with_config("sid", session)
def test_config_sync_adopts_baseline_when_agent_already_on_target(monkeypatch):
# Branched/resumed sessions reach their first sync with no snapshot but
# an agent already built from config; that must not trigger a switch.
_patch_config_model(monkeypatch, "old/model")
session = _sync_test_session()
monkeypatch.setattr(
server,
"_apply_model_switch",
lambda *a, **k: pytest.fail("agent already on target must not switch"),
)
server._sync_agent_model_with_config("sid", session)
assert session["config_model_seen"] == ("old/model", "")
def test_config_sync_switches_when_only_provider_differs(monkeypatch):
_patch_config_model(monkeypatch, "old/model", provider="nous")
session = _sync_test_session(config_model_seen=("old/model", ""))
calls = []
monkeypatch.setattr(
server,
"_apply_model_switch",
lambda sid, sess, raw, **kw: calls.append(raw),
)
server._sync_agent_model_with_config("sid", session)
assert calls == ["old/model --provider nous"]
def test_config_sync_failure_emits_error_once_per_edit(monkeypatch):
_patch_config_model(monkeypatch, "broken/model")
session = _sync_test_session(config_model_seen=("old/model", ""))

View file

@ -2036,6 +2036,13 @@ def _sync_agent_model_with_config(sid: str, session: dict) -> None:
if target == seen:
return
model, provider = target
# Already running the configured model (branched/resumed session before
# its first sync, or a config revert after a failed switch): adopt the
# baseline without a redundant switch.
if model == getattr(agent, "model", "") and (
not provider or provider == getattr(agent, "provider", "")
):
return
raw = f"{model} --provider {provider}" if provider else model
try:
_apply_model_switch(