Skip to content

The agent that grows with you

fix(auxiliary): preserve session in out-of-turn snapshots

fangliquande03c3423397
diff --git a/agent/turn_context.py b/agent/turn_context.py
index b4b75662721..1a9e7bbedb6 100644
--- a/agent/turn_context.py
+++ b/agent/turn_context.py
@@ -182,9 +182,7 @@ def _maybe_title_session_at_turn_start(agent: Any, messages: List[Any]) -> None:
return
# Snapshot runtime identity so the background titler can skip if the user
# switches models before it fires.
- main_runtime = {
- k: getattr(agent, k, None) for k in ("model", "provider", "base_url", "api_key", "api_mode")
- }
+ main_runtime = agent._current_main_runtime()
# See #19027.
maybe_auto_title(
session_db,
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 23ea8ad6eeb..eefeb2264b1 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -805,7 +805,11 @@ class GatewaySlashCommandsMixin(
model, rt = None, {}
if not rt.get("api_key"):
return t("gateway.btw.no_provider")
- main_runtime = {"model": model, **{k: rt.get(k) for k in ("provider", "base_url", "api_key", "api_mode")}}
+ main_runtime = {
+ "model": model,
+ **{k: rt.get(k) for k in ("provider", "base_url", "api_key", "api_mode")},
+ "session_id": session_entry.session_id,
+ }
history_snapshot = list(history)
# Prefer the cache-parity fork when a live cached AIAgent exists: it replays the snapshot
# against the warm provider prefix cache, giving FULL context at cache-read prices. With no
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index b7c0c9c0a9a..64ff4dd823d 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -2080,7 +2080,9 @@ class CLICommandsMixin:
runtime = turn_route["runtime"]
main_runtime = {
"model": turn_route["model"],
- **{k: runtime.get(k) for k in ("provider", "base_url", "api_key", "api_mode")}}
+ **{k: runtime.get(k) for k in ("provider", "base_url", "api_key", "api_mode")},
+ "session_id": getattr(parent_agent, "session_id", None),
+ }
preview = _ellipsize(question, 60)
_cp(f" 💬 Side question: \"{preview}\"",
" Answering from a snapshot of this conversation — the current work continues.\n")
diff --git a/tests/agent/test_opencode_session_affinity.py b/tests/agent/test_opencode_session_affinity.py
index 08ab155f056..6b500afd052 100644
--- a/tests/agent/test_opencode_session_affinity.py
+++ b/tests/agent/test_opencode_session_affinity.py
@@ -7,6 +7,7 @@ from types import SimpleNamespace
import pytest
from agent import auxiliary_client as aux
+from agent import title_generator, turn_context
from agent.chat_completion_helpers import build_api_kwargs
from run_agent import AIAgent
@@ -64,7 +65,7 @@ def test_auxiliary_calls_share_the_main_turn_session_key():
aux._RUNTIME_MAIN_CONTEXT.reset(token)
-def test_auxiliary_call_uses_explicit_main_runtime_session(monkeypatch):
+def test_auto_title_uses_explicit_main_runtime_session(monkeypatch):
captured = {}
class Completions:
@@ -87,13 +88,31 @@ def test_auxiliary_call_uses_explicit_main_runtime_session(monkeypatch):
),
)
monkeypatch.setattr(aux, "_get_cached_client", lambda *_args, **_kwargs: (client, "glm-5"))
-
- aux.call_llm(
- task="title_generation",
- messages=_MSGS,
- main_runtime=_agent(
- "opencode-zen", "glm-5", "https://opencode.ai/zen/v1"
- )._current_main_runtime(),
+ monkeypatch.setattr(title_generator, "_auto_title_enabled", lambda: True)
+ monkeypatch.setattr(title_generator, "_kanban_task_title", lambda: None)
+ monkeypatch.setattr(title_generator, "apply_instant_title", lambda *_args, **_kwargs: None)
+
+ def spawn_immediately(target, *, name, args, kwargs):
+ def start():
+ token = aux._RUNTIME_MAIN_CONTEXT.set(None)
+ try:
+ target(*args, **kwargs)
+ finally:
+ aux._RUNTIME_MAIN_CONTEXT.reset(token)
+
+ return SimpleNamespace(start=start)
+
+ monkeypatch.setattr("agent.memory_provider.spawn_context_thread", spawn_immediately)
+
+ session_db = SimpleNamespace(
+ get_session_title_source=lambda _session_id: None,
+ get_conversation_root=lambda session_id: session_id,
+ set_auto_title=lambda *_args, **_kwargs: True,
)
+ agent = _agent("opencode-zen", "glm-5", "https://opencode.ai/zen/v1")
+ agent._session_db = session_db
+ agent._session_db_created = True
+
+ turn_context._maybe_title_session_at_turn_start(agent, _MSGS)
assert captured["extra_headers"]["x-opencode-session"] == "sess-affinity-1"
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py
index dde0620f744..098a2b5c521 100644
--- a/tui_gateway/methods_prompt.py
+++ b/tui_gateway/methods_prompt.py
@@ -1012,9 +1012,7 @@ def _(rid, params: dict) -> dict:
return err
agent = session.get("agent")
snapshot = list(getattr(agent, "_session_messages", None) or session.get("history") or [])
- main_runtime = {
- k: getattr(agent, k, None)
- for k in ("model", "provider", "base_url", "api_key", "api_mode")}
+ main_runtime = agent._current_main_runtime()
def body():
from agent.side_question import answer_side_question