Skip to content

The agent that grows with you

fix(bot-mode): keep Bot Chat resume on a proven compression tip

Brooklyn Nicholsonadb23c13cb6d
diff --git a/hermes_state.py b/hermes_state.py
index 831fcbc78dd..7e95137c9fd 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -10679,7 +10679,7 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
# the tip (== the row itself when uncompressed).
tip = row
try:
- tip_id = self.resolve_resume_session_id(session_id) or session_id
+ tip_id = self.get_compression_tip(session_id) or session_id
if tip_id != session_id:
tip = self.get_session(tip_id) or row
except Exception:
diff --git a/tests/tui_gateway/test_profiles_list_canonical_session.py b/tests/tui_gateway/test_profiles_list_canonical_session.py
index 58e6e4c38c6..17551d01489 100644
--- a/tests/tui_gateway/test_profiles_list_canonical_session.py
+++ b/tests/tui_gateway/test_profiles_list_canonical_session.py
@@ -76,6 +76,23 @@ def _row(profiles, name):
return next(p for p in profiles if p["name"] == name)
+def _resume(params, monkeypatch):
+ """Cold resume far enough to read the remapped stored id, no agent build."""
+ monkeypatch.setattr(srv, "_schedule_resume_hydration", lambda *a, **k: None)
+ monkeypatch.setattr(srv, "_schedule_session_cap_enforcement", lambda *a, **k: None)
+ monkeypatch.setattr(srv, "_enable_gateway_prompts", lambda: None)
+ known = set(srv._sessions)
+ try:
+ return srv._methods["session.resume"](1, {
+ **params,
+ "defer_history": True,
+ "omit_messages": True,
+ })
+ finally:
+ for sid in [s for s in srv._sessions if s not in known]:
+ srv._sessions.pop(sid, None)
+
+
# ---------------------------------------------------------------------------
# canonical_session resolution
# ---------------------------------------------------------------------------
@@ -355,6 +372,73 @@ def test_session_list_title_lookup_resolves_compression_tip(home, monkeypatch):
db.close()
+def test_canonical_session_resurrects_despite_unmarked_normal_child(home):
+ # Recoverability is judged at the compression tip, not the legacy resume
+ # walker. An unmarked side chat must not hide a reaped Bot Chat.
+ db = _db(home)
+ _add_session(db, "reaped3", title="Bot Chat", ts=1000,
+ text="surviving forever chat", hidden=True,
+ end_reason="ws_orphan_reap", archived=True)
+ _add_session(db, "normal3", title="Friendly greeting", ts=3000,
+ text="ordinary chat content", parent="reaped3")
+ db.close()
+
+ canonical = _row(_profiles({}), "default")["canonical_session"]
+ assert canonical is not None
+ assert canonical["id"] == "reaped3"
+ assert canonical["resolved_id"] == "reaped3"
+
+ db = _db(home)
+ try:
+ assert not db.get_session("reaped3")["archived"]
+ finally:
+ db.close()
+
+
+def test_session_resume_bot_chat_ignores_unmarked_normal_child(home, monkeypatch):
+ db = _db(home)
+ _add_session(db, "root4", title="Bot Chat", ts=1000,
+ text="canonical resume target", hidden=True)
+ _add_session(db, "normal4", title="Friendly greeting", ts=3000,
+ text="ordinary resume target", parent="root4")
+ monkeypatch.setattr(srv, "_get_db", lambda: db)
+
+ envelope = _resume({"session_id": "root4"}, monkeypatch)
+ assert "error" not in envelope, envelope
+ assert envelope["result"]["resumed"] == "root4"
+ assert envelope["result"]["session_key"] == "root4"
+ db.close()
+
+
+def test_session_resume_bot_chat_resolves_compression_tip(home, monkeypatch):
+ db = _db(home)
+ _add_session(db, "root5", title="Bot Chat", ts=1000,
+ text="pre-compression resume target", hidden=True,
+ end_reason="compression")
+ _add_session(db, "tip5", title="Bot Chat (continued)", ts=3000,
+ text="post-compression resume target", parent="root5")
+ monkeypatch.setattr(srv, "_get_db", lambda: db)
+
+ envelope = _resume({"session_id": "root5"}, monkeypatch)
+ assert "error" not in envelope, envelope
+ assert envelope["result"]["resumed"] == "tip5"
+ assert envelope["result"]["session_key"] == "tip5"
+ db.close()
+
+
+def test_session_resume_ordinary_chat_still_follows_unmarked_child(home, monkeypatch):
+ db = _db(home)
+ _add_session(db, "plain-root", title="Scratch", ts=1000, text="parent chat")
+ _add_session(db, "plain-child", title="Follow-up", ts=3000,
+ text="child chat", parent="plain-root")
+ monkeypatch.setattr(srv, "_get_db", lambda: db)
+
+ envelope = _resume({"session_id": "plain-root"}, monkeypatch)
+ assert "error" not in envelope, envelope
+ assert envelope["result"]["resumed"] == "plain-child"
+ db.close()
+
+
def test_deliberate_archive_after_resurrection_stays_archived(home):
# Resurrection must clear the accidental end stamp: if ws_orphan_reap
# survived on the row, a LATER deliberate archive (which writes no
diff --git a/tui_gateway/methods_profiles.py b/tui_gateway/methods_profiles.py
index 830eef07a90..9c194c85e08 100644
--- a/tui_gateway/methods_profiles.py
+++ b/tui_gateway/methods_profiles.py
@@ -95,10 +95,13 @@ def _(rid, params: dict) -> dict:
Exact-lookup semantics, deliberately different from the listing:
hidden rows still resolve (canonical chats are always hidden),
- compression lineages resolve to the live tip with the same resolver
- ``session.resume`` uses, and denied internal sources (tool/kanban)
- count as absent. The reported ``id`` stays the durable registry row
- while ``resolved_id`` names the live tip. Best-effort: any failure
+ compression lineages resolve to the live tip via
+ ``get_compression_tip`` (not the generic resume walker, whose
+ unmarked-child fallback can select an ordinary child).
+ ``session.resume`` uses that same tip resolver when the target is
+ titled ``Bot Chat``. Denied internal sources (tool/kanban) count as
+ absent. The reported ``id`` stays the durable registry row while
+ ``resolved_id`` names the live tip. Best-effort: any failure
degrades to None rather than failing the whole profiles.list call.
"""
if db is None:
@@ -172,7 +175,7 @@ def _(rid, params: dict) -> dict:
return False
tip = row
try:
- tip_id = db.resolve_resume_session_id(session_id) or session_id
+ tip_id = db.get_compression_tip(session_id) or session_id
if tip_id != session_id:
tip = db.get_session(tip_id) or row
except Exception:
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 12528d22f9a..6ff20b63025 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -560,10 +560,18 @@ def _(rid, params: dict) -> dict:
# here also re-anchors the fast path below so a still-live rotated session
# is reused (by its new key) instead of rebuilding a duplicate agent on the
# stale parent. Skipped for lazy watch windows, which intentionally attach
- # to the exact child branch they were opened on.
+ # to the exact child branch they were opened on. Bot Chat is a named
+ # registry row — stay on a proven compression edge so an unmarked
+ # side chat cannot steal the open (the title-lookup / profiles.list
+ # contract). Other sessions keep the legacy unmarked-child walker.
if found and not is_truthy_value(params.get("lazy", False)):
try:
- tip = db.resolve_resume_session_id(target)
+ from tools.bot_mode_probe import BOT_CHAT_TITLE
+
+ if (found.get("title") or "").strip() == BOT_CHAT_TITLE:
+ tip = db.get_compression_tip(target) or target
+ else:
+ tip = db.resolve_resume_session_id(target)
except Exception:
tip = target
if tip and tip != target: