session: activate default_model — pin configured model after session/new
AgentSpec.default_model was declared but never consumed; the agent's
built-in default rotated (fledge-alpha-free -> exo-free) and several
free upstreams go dark ('Endpoint is unavailable'). After session/new,
set the configured model via session/set_config_option; failure is
non-fatal (warn + continue with agent default). Adds 4 tests (75 total).
This commit is contained in:
1 parent
d8cce95adf
commit
036b5641cf
2 files changed
+141
-4
No files matched your search
+26
-4
@@ -504,12 +504,34 @@ class SessionManager:
|
|||||||
session_id = (res or {}).get("sessionId")
|
session_id = (res or {}).get("sessionId")
|
||||||
if not session_id:
|
if not session_id:
|
||||||
raise SessionError(f"session/new returned no sessionId: {res!r}")
|
raise SessionError(f"session/new returned no sessionId: {res!r}")
|
||||||
self._update_session_config(
|
b = Binding(channel=k[0], chat_id=k[1], agent=agent_name, acp_session_id=session_id)
|
||||||
Binding(channel=k[0], chat_id=k[1], agent=agent_name, acp_session_id=session_id),
|
self._update_session_config(b, (res or {}).get("configOptions") or [])
|
||||||
(res or {}).get("configOptions") or [],
|
# Pin the agent's model when a default_model is configured. Free-tier
|
||||||
)
|
# agents rotate their built-in default (and some models' upstreams go
|
||||||
|
# dark), so we cannot rely on the agent's own choice.
|
||||||
|
if spec.default_model:
|
||||||
|
self._pin_default_model(client, b, spec.default_model)
|
||||||
return client, session_id
|
return client, session_id
|
||||||
|
|
||||||
|
def _pin_default_model(self, client: ACPClient, b: Binding, model: str) -> None:
|
||||||
|
"""Set the configured default model on a fresh session; warn on failure."""
|
||||||
|
try:
|
||||||
|
current = self._session_config(b).get("model", {}).get("current")
|
||||||
|
if current == model:
|
||||||
|
return
|
||||||
|
res = client.request(
|
||||||
|
"session/set_config_option",
|
||||||
|
{"sessionId": b.acp_session_id, "configId": "model", "value": model},
|
||||||
|
timeout=15,
|
||||||
|
)
|
||||||
|
self._update_session_config(b, (res or {}).get("configOptions") or [])
|
||||||
|
self._logger.info(f"[sessions] pinned default model {model!r} for session {b.acp_session_id}")
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
self._logger.warning(
|
||||||
|
f"[sessions] failed to pin default model {model!r} on session "
|
||||||
|
f"{b.acp_session_id}: {exc}; continuing with agent default"
|
||||||
|
)
|
||||||
|
|
||||||
def _client_for(self, agent_name: str, spec: AgentSpec | None = None) -> ACPClient:
|
def _client_for(self, agent_name: str, spec: AgentSpec | None = None) -> ACPClient:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
client = self._clients.get(agent_name)
|
client = self._clients.get(agent_name)
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
"""Tests for pinning the configured default model on a fresh ACP session.
|
||||||
|
|
||||||
|
The free-tier opencode agent rotates its built-in default model and some
|
||||||
|
models' upstreams go dark, so agenthub must set `default_model` itself
|
||||||
|
right after `session/new`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||||
|
|
||||||
|
from agenthub.session import AgentSpec, Binding, SessionManager # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
class StubClient:
|
||||||
|
"""Records JSON-RPC requests; returns canned session/new + configOptions."""
|
||||||
|
|
||||||
|
def __init__(self, current_model: str = "opencode/other-free", fail_set: bool = False):
|
||||||
|
self.calls: list[tuple[str, dict]] = []
|
||||||
|
self.current_model = current_model
|
||||||
|
self.fail_set = fail_set
|
||||||
|
|
||||||
|
def is_alive(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def request(self, method: str, params: dict, timeout: float = 30.0) -> dict:
|
||||||
|
self.calls.append((method, params))
|
||||||
|
if method == "session/new":
|
||||||
|
return {
|
||||||
|
"sessionId": "sess-1",
|
||||||
|
"configOptions": [
|
||||||
|
{
|
||||||
|
"id": "model",
|
||||||
|
"currentValue": self.current_model,
|
||||||
|
"options": [
|
||||||
|
{"value": self.current_model, "name": self.current_model},
|
||||||
|
{"value": "opencode/ling-3.1-flash-free", "name": "ling"},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
if method == "session/set_config_option":
|
||||||
|
if self.fail_set:
|
||||||
|
raise RuntimeError("boom")
|
||||||
|
self.current_model = params["value"]
|
||||||
|
return {
|
||||||
|
"configOptions": [
|
||||||
|
{
|
||||||
|
"id": "model",
|
||||||
|
"currentValue": self.current_model,
|
||||||
|
"options": [{"value": self.current_model, "name": self.current_model}],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
raise AssertionError(f"unexpected method {method}")
|
||||||
|
|
||||||
|
def set_calls(self) -> list[dict]:
|
||||||
|
return [p for m, p in self.calls if m == "session/set_config_option"]
|
||||||
|
|
||||||
|
|
||||||
|
def make_manager(model: str | None, stub: StubClient) -> SessionManager:
|
||||||
|
agents = {
|
||||||
|
"oc": AgentSpec(name="oc", cmd=["true"], default_model=model),
|
||||||
|
}
|
||||||
|
m = SessionManager(agents=agents, transport="ndjson")
|
||||||
|
m._client_for = lambda *a, **kw: stub # noqa: SLF001
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_model_is_pinned_on_new_session():
|
||||||
|
stub = StubClient(current_model="opencode/exo-free")
|
||||||
|
m = make_manager("opencode/ling-3.1-flash-free", stub)
|
||||||
|
client, sid = m._ensure_session(("wecom", "c1"), "oc", None) # noqa: SLF001
|
||||||
|
|
||||||
|
assert sid == "sess-1"
|
||||||
|
sets = stub.set_calls()
|
||||||
|
assert len(sets) == 1, sets
|
||||||
|
assert sets[0] == {
|
||||||
|
"sessionId": "sess-1",
|
||||||
|
"configId": "model",
|
||||||
|
"value": "opencode/ling-3.1-flash-free",
|
||||||
|
}
|
||||||
|
# snapshot reflects the pinned model, so /model reports it as current
|
||||||
|
snap = m._session_config(Binding("wecom", "c1", "oc", "sess-1")) # noqa: SLF001
|
||||||
|
assert snap["model"]["current"] == "opencode/ling-3.1-flash-free"
|
||||||
|
m.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_pin_when_default_model_unset():
|
||||||
|
stub = StubClient(current_model="opencode/exo-free")
|
||||||
|
m = make_manager(None, stub)
|
||||||
|
m._ensure_session(("wecom", "c1"), "oc", None) # noqa: SLF001
|
||||||
|
assert stub.set_calls() == []
|
||||||
|
m.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_pin_when_agent_default_already_matches():
|
||||||
|
stub = StubClient(current_model="opencode/ling-3.1-flash-free")
|
||||||
|
m = make_manager("opencode/ling-3.1-flash-free", stub)
|
||||||
|
m._ensure_session(("wecom", "c1"), "oc", None) # noqa: SLF001
|
||||||
|
assert stub.set_calls() == [], "must not re-set an already-current model"
|
||||||
|
m.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def test_pin_failure_is_non_fatal():
|
||||||
|
"""A dead/renamed model must not break session creation."""
|
||||||
|
stub = StubClient(current_model="opencode/exo-free", fail_set=True)
|
||||||
|
m = make_manager("opencode/does-not-exist", stub)
|
||||||
|
client, sid = m._ensure_session(("wecom", "c1"), "oc", None) # noqa: SLF001
|
||||||
|
assert sid == "sess-1", "session must still be usable"
|
||||||
|
assert len(stub.set_calls()) == 1
|
||||||
|
m.stop()
|
||||||
Reference in new issue
Block a user