Skip to content

The agent that grows with you

fix(sessions): pin the Bot Chat composer-pick reach through _apply_model_switch -> SessionDB -> session.resume

teknium178b5dbfb08be
diff --git a/tests/tui_gateway/test_custom_provider_session_persistence.py b/tests/tui_gateway/test_custom_provider_session_persistence.py
index 283641d969f..89b73fc9ef6 100644
--- a/tests/tui_gateway/test_custom_provider_session_persistence.py
+++ b/tests/tui_gateway/test_custom_provider_session_persistence.py
@@ -567,29 +567,82 @@ class TestRoomPlumbingRuntimeOverrides:
class TestFollowProfileConfigRuntimeOverrides:
- def test_composer_override_survives_while_profile_model_is_unchanged(self, monkeypatch):
- """An explicit Bot Chat pick outranks the unchanged profile default."""
+ def test_composer_pick_on_bot_chat_survives_resume_until_profile_model_changes(self, monkeypatch, tmp_path):
+ """Production path, A->B->A shape: a composer /model pick on a follow_profile_config Bot Chat under
+ profile B persists its provenance marker into B's real SessionDB row via ``_apply_model_switch``;
+ ``session.resume`` on the deferred (cold, agent-less) path restores the pin while B's config.yaml
+ model is unchanged, and drops it once B's profile model moves. Launch home A has a different
+ model the whole time, so the compare must run under B's scope, not the launch profile's."""
import tui_gateway.server as server
- monkeypatch.setattr(server, "_config_model_target", lambda: ("profile/default", "nous"))
- row = {
- "title": "Bot Chat",
- "model": "openai/gpt-5.6-luna-pro",
- "model_config": json.dumps(
- {
- "model": "openai/gpt-5.6-luna-pro",
- "provider": "nous",
- "reasoning_config": {"effort": "xhigh"},
- "follow_profile_config": True,
- "composer_override_profile": {"model": "profile/default", "provider": "nous"},
- }
- ),
- }
-
- overrides = server._stored_session_runtime_overrides(row)
-
- assert overrides["model_override"]["model"] == "openai/gpt-5.6-luna-pro"
- assert overrides["reasoning_config_override"] == {"effort": "xhigh"}
+ launch, secondary = tmp_path / "a", tmp_path / "b"
+ for home, model in ((launch, "launch/model"), (secondary, "profile/default")):
+ home.mkdir()
+ (home / "config.yaml").write_text(f"model:\n default: {model}\n provider: nous\n")
+ (home / ".env").write_text("")
+ stored = "20260919-000000-botc"
+ db = SessionDB(db_path=secondary / "state.db")
+ db.create_session(stored, "desktop", model="profile/default",
+ model_config={"model": "profile/default", "provider": "nous", "follow_profile_config": True})
+ db.append_message(stored, "user", "hi")
+ db.append_message(stored, "assistant", "hello")
+
+ class _FakeAgent:
+ model, provider, base_url, api_key, api_mode = "profile/default", "nous", "", "", ""
+ _session_db = db
+
+ def switch_model(self, **kw):
+ self.model, self.provider = kw["new_model"], kw["new_provider"]
+
+ monkeypatch.setenv("HERMES_HOME", str(launch))
+ monkeypatch.setattr(server, "_hermes_home", str(launch))
+ monkeypatch.setattr(server, "_profile_home", lambda p: secondary if p == "b" else None)
+ monkeypatch.setattr(server, "_get_db", lambda: SessionDB(db_path=launch / "state.db"))
+ monkeypatch.setattr(server, "_enable_gateway_prompts", lambda: None)
+ monkeypatch.setattr(server, "_schedule_resume_hydration", lambda *a, **k: None)
+ monkeypatch.setattr(server, "_schedule_session_cap_enforcement", lambda *a, **k: None)
+ monkeypatch.setattr(server, "_default_session_cwd", lambda *a, **k: str(tmp_path))
+ monkeypatch.setattr(server, "_emit", lambda *a, **k: None)
+ monkeypatch.setattr(server, "_restart_slash_worker", lambda *a, **k: None)
+ monkeypatch.setattr(server, "_session_info", lambda *a, **k: {})
+ live = {"agent": _FakeAgent(), "session_key": stored, "model_override": None,
+ "follow_profile_config": True, "profile_home": str(secondary)}
+ result = types.SimpleNamespace(success=True, error_message="", new_model="zai/glm-5.1", target_provider="zai", api_key="k", base_url="",
+ api_mode="", warning_message="", runtime_capabilities=None)
+ known = set(server._sessions)
+ try:
+ with (
+ patch("hermes_cli.model_switch.parse_model_flags", return_value=("glm-5.1", None, False, False, None)),
+ patch("hermes_cli.model_switch.resolve_persist_behavior", return_value=False),
+ patch("hermes_cli.model_switch.switch_model", return_value=result),
+ server._profile_build_scope(secondary),
+ ):
+ server._apply_model_switch("sid-live", live, "glm-5.1")
+
+ row = json.loads(db.get_session(stored)["model_config"])
+ assert row["composer_override_profile"] == {"model": "profile/default", "provider": "nous"}
+ assert row["model"] == "zai/glm-5.1"
+
+ def resume():
+ resp = server.handle_request({"id": "1", "method": "session.resume", "params": {
+ "session_id": stored, "source": "desktop", "profile": "b",
+ "defer_history": True, "omit_messages": True}})
+ assert "error" not in resp, resp
+ # Pop the live record so the next resume rebuilds from the stored row instead of reusing it.
+ with server._sessions_lock:
+ return server._sessions.pop(resp["result"]["session_id"])
+
+ record = resume()
+ assert record["model_override"]["model"] == "zai/glm-5.1"
+ assert record["composer_override_profile"] == {"model": "profile/default", "provider": "nous"}
+
+ (secondary / "config.yaml").write_text("model:\n default: profile/new-default\n provider: nous\n")
+ assert resume().get("model_override") is None
+ finally:
+ db.close()
+ with server._sessions_lock:
+ for sid in [s for s in server._sessions if s not in known]:
+ server._sessions.pop(sid, None)
def test_profile_model_change_supersedes_composer_override_on_resume_and_live(self, monkeypatch):
"""Changing the Bot profile invalidates both stored and live chat pins."""
@@ -733,9 +786,12 @@ class TestFollowProfileConfigRuntimeOverrides:
"session_key": "key-1",
"model_override": {"model": "glm-5.1", "provider": "ollama-cloud"},
"follow_profile_config": True,
+ "composer_override_profile": {"model": "profile/default", "provider": "nous"},
}
server._ensure_session_db_row(session)
assert captured["model_config"].get("follow_profile_config") is True
+ # A pick made before the first send rides the same first-write projection as the marker.
+ assert captured["model_config"].get("composer_override_profile") == {"model": "profile/default", "provider": "nous"}
def test_ensure_db_row_omits_marker_without_contract(self, monkeypatch):
"""Sessions without the contract do NOT get the marker — normal chats
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 3c9033b28dd..06290efc516 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -529,9 +529,8 @@ class _Resume:
``overrides`` restores the stored model/provider/reasoning/tier so the deferred build matches eager."""
if overrides is not None:
extra.update(model_override=overrides.get("model_override"), resume_runtime_overrides=overrides or None)
- model_config = _parse_model_config(self.found.get("model_config"), quiet=True) if self.found else {}
- title = str((self.found or {}).get("title") or "").strip()
- follows_profile = bool(model_config.get("follow_profile_config") or title == "Bot Chat")
+ model_config = _parse_model_config((self.found or {}).get("model_config"), quiet=True)
+ follows_profile = _row_follows_profile(self.found)
else:
model_config, follows_profile = {}, False
record = _deferred_session_record(
@@ -848,7 +847,7 @@ def _resume_eager(ctx: _Resume) -> dict:
if stored_runtime_overrides.get("model_override") is not None:
session["model_override"] = stored_runtime_overrides["model_override"]
model_config = _parse_model_config(ctx.found.get("model_config"), quiet=True)
- if model_config.get("follow_profile_config") or str(ctx.found.get("title") or "").strip() == "Bot Chat":
+ if _row_follows_profile(ctx.found):
session["follow_profile_config"] = True
session["composer_override_profile"] = (
model_config.get("composer_override_profile")
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index e4a5555ec0d..0df4529a1e8 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -1502,6 +1502,16 @@ def _parse_model_config(raw, *, quiet: bool = False) -> dict:
return {}
+def _row_follows_profile(row: dict | None) -> bool:
+ """Whether a stored row is a canonical Bot Chat whose runtime follows the member profile's config.
+ Identity is the persisted ``follow_profile_config`` marker; the bare title compare stays ONLY here as
+ the legacy fallback for rows written before the marker existed."""
+ if not row:
+ return False
+ model_config = _parse_model_config(row.get("model_config"), quiet=True)
+ return bool(model_config.get("follow_profile_config") or str(row.get("title") or "").strip() == "Bot Chat")
+
+
def _stored_session_runtime_overrides(row: dict | None) -> dict:
"""Runtime fields persisted with a stored session (model column, ``billing_provider``, JSON ``model_config``):
resume restores the model/provider/reasoning THAT chat used, not the global pick. Plugin-owned Bot-Mode
@@ -1513,13 +1523,12 @@ def _stored_session_runtime_overrides(row: dict | None) -> dict:
model_config = _parse_model_config(row.get("model_config"), quiet=True)
_row_title = str(row.get("title") or "").strip()
room_plumbing = model_config.get("room_plumbing") or (row.get("hidden") and _row_title.startswith("Group:"))
- follows_profile = model_config.get("follow_profile_config") or _row_title == "Bot Chat"
composer_profile = model_config.get("composer_override_profile")
composer_profile_matches = isinstance(composer_profile, dict) and (
str(composer_profile.get("model") or "").strip(),
str(composer_profile.get("provider") or "").strip(),
) == _config_model_target()
- if room_plumbing or (follows_profile and not composer_profile_matches):
+ if room_plumbing or (_row_follows_profile(row) and not composer_profile_matches):
return {}
overrides: dict = {}
field = lambda k: str(model_config.get(k) or "").strip()
diff --git a/website/docs/user-guide/bot-mode.md b/website/docs/user-guide/bot-mode.md
index 58c0630d86c..cc88f1292e8 100644
--- a/website/docs/user-guide/bot-mode.md
+++ b/website/docs/user-guide/bot-mode.md
@@ -51,7 +51,7 @@ An **Advanced** disclosure opens the full capabilities surface:
- **Clone from an existing profile** — start from another Bot's config, skills, SOUL, and memory, or pick **Fresh profile** for a clean start.
- **Create empty** — skip the bundled skills entirely for a minimal profile.
-- **Model & provider pin** — give the Bot its own model. Any provider/model pair Hermes knows about works, and different Bots can run on different models side by side. Leave it unset to inherit from the launch profile.
+- **Model & provider pin** — give the Bot its own model. Any provider/model pair Hermes knows about works, and different Bots can run on different models side by side. Leave it unset to inherit from the launch profile. Picking a model from the Bot Chat's composer sticks to that chat (it survives reopening the app) until you change the Bot's profile model, which takes over again.
- **Custom SOUL.md** — the Bot's persona and standing instructions.
- **Per-skill, per-toolset, and per-MCP-server enablement** — tick exactly the capabilities this specialist needs.
- **Copy API keys from the main profile** — on by default. Each Bot gets its own credential store: static API keys are copied in, while single-use OAuth logins (Anthropic, OpenAI Codex, xAI) are not copied — sign the Bot in itself with `hermes -p <name> auth add <provider>`. See [Every profile owns its credentials](./profiles.md#every-profile-owns-its-credentials).