Skip to content

The agent that grows with you

fix(kanban): bind auto-subscriptions to the live session after a compaction fork

liuhao102433e0a29ca83f
diff --git a/tests/tools/test_kanban_tools.py b/tests/tools/test_kanban_tools.py
index 97ee24686df..e80d4bc9987 100644
--- a/tests/tools/test_kanban_tools.py
+++ b/tests/tools/test_kanban_tools.py
@@ -1187,6 +1187,83 @@ def test_create_does_not_subscribe_in_cli_session(monkeypatch, worker_env):
assert _list_subs_for_task(d["task_id"]) == []
+def test_create_tui_subscription_binds_to_live_session_after_compaction_fork(monkeypatch, worker_env, tmp_path):
+ """#110068: the inherited HERMES_SESSION_KEY can name a session already superseded
+ by a compaction fork. Auto-subscribe must bind to the live continuation tip resolved
+ from the session store, not the stale key the process was launched with."""
+ import hermes_state
+ from hermes_state import SessionDB
+ from tools import kanban_tools as kt
+
+ stale, live = "20260912_231110_stale", "20260913_000649_live"
+ db = SessionDB(tmp_path / "state.db")
+ db.create_session(stale, source="tui")
+ db.append_message(stale, role="user", content="pre-fork turn")
+ db.end_session(stale, "compression")
+ db.create_session(live, source="tui", parent_session_id=stale)
+ db.append_message(live, role="assistant", content="post-fork turn")
+
+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", tmp_path / "state.db")
+ monkeypatch.delenv("HERMES_SESSION_PLATFORM", raising=False)
+ monkeypatch.delenv("HERMES_SESSION_CHAT_ID", raising=False)
+ monkeypatch.setenv("HERMES_SESSION_KEY", stale)
+ monkeypatch.delenv("HERMES_SESSION_ID", raising=False)
+ monkeypatch.setenv("HERMES_PROFILE", "default")
+
+ out = kt._handle_create({
+ "title": "auto-sub tui post-fork",
+ "assignee": "peer",
+ })
+ d = json.loads(out)
+ assert d["ok"] is True
+ assert d["subscribed"] is True, d
+
+ subs = _sub_index(_list_subs_for_task(d["task_id"]))
+ assert len(subs) == 1
+ assert subs[0]["platform"] == "tui"
+ assert subs[0]["chat_id"] == live
+
+
+def test_live_tui_session_key_uses_profile_store(monkeypatch, tmp_path):
+ """A named-profile session's lineage lives in profiles/<x>/state.db, not the default
+ store: resolution must open THAT store."""
+ from hermes_cli import profiles as profiles_mod
+ from hermes_state import SessionDB
+ from tools import kanban_tools as kt
+
+ stale, live = "stale-teamx", "live-teamx"
+ store = tmp_path / "profiles" / "teamx"
+ store.mkdir(parents=True)
+ db = SessionDB(store / "state.db")
+ db.create_session(stale, source="tui")
+ db.append_message(stale, role="user", content="pre-fork turn")
+ db.end_session(stale, "compression")
+ db.create_session(live, source="tui", parent_session_id=stale)
+ db.append_message(live, role="user", content="post-fork turn")
+
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda name: name == "teamx")
+ monkeypatch.setattr(profiles_mod, "get_profile_dir", lambda name: str(store))
+ assert kt._live_tui_session_key(stale, "teamx") == live
+
+
+def test_live_tui_session_key_fails_open_to_original_key(monkeypatch):
+ """Store unavailability must degrade to current behaviour (subscribe to the inherited
+ key), never fail the kanban_create call."""
+ import hermes_state_registry as registry
+ from tools import kanban_tools as kt
+
+ def _boom(db_path=None):
+ raise RuntimeError("store unavailable")
+
+ monkeypatch.setattr(registry, "acquire", _boom)
+ assert kt._live_tui_session_key("stale-key", "default") == "stale-key"
+ assert kt._live_tui_session_key("stale-key", None) == "stale-key"
+ # An unresolvable profile also falls through to the default store path.
+ import hermes_cli.profiles as profiles_mod
+ monkeypatch.setattr(profiles_mod, "profile_exists", lambda name: False)
+ assert kt._live_tui_session_key("stale-key", "ghost-profile") == "stale-key"
+
+
def test_create_respects_auto_subscribe_on_create_false(monkeypatch, worker_env, tmp_path):
"""The config gate kanban.auto_subscribe_on_create=false must
suppress auto-subscription even when the session has a delivery
diff --git a/tools/kanban_tools.py b/tools/kanban_tools.py
index 525bd539b4d..c8b64c095c3 100644
--- a/tools/kanban_tools.py
+++ b/tools/kanban_tools.py
@@ -1154,6 +1154,32 @@ def _handle_create(args: dict, **kw) -> str:
subscribed=_maybe_auto_subscribe(conn, new_tid))
+def _live_tui_session_key(session_key: str, profile: Optional[str]) -> str:
+ """Re-resolve a TUI session key at subscribe time: the inherited ``HERMES_SESSION_KEY``
+ can name a session already superseded by a compaction fork, and a subscription bound to
+ the dead key silently drops every later completion notification (#110068). Maps the key
+ to its continuation tip via the session store's lineage walk. Best-effort, fail-open:
+ any error (or an unresolvable key) returns *session_key* unchanged."""
+ db_path = None
+ if profile and profile != "default":
+ try:
+ from pathlib import Path
+ from hermes_cli.profiles import get_profile_dir, profile_exists
+ if profile_exists(profile):
+ db_path = Path(get_profile_dir(profile)) / "state.db"
+ except Exception:
+ db_path = None
+ try:
+ from hermes_state_registry import acquire, release_or_close
+ db = acquire(db_path)
+ try:
+ return db.resolve_resume_session_id(session_key) or session_key
+ finally:
+ release_or_close(db)
+ except Exception:
+ return session_key
+
+
def _resolve_notify_target() -> Optional[dict[str, Any]]:
"""``kanban_db.add_notify_sub`` kwargs for the calling session, or None (CLI/cron/tests).
Gateway sessions: ``HERMES_SESSION_PLATFORM``/``CHAT_ID`` ContextVars. TUI/desktop:
@@ -1174,6 +1200,10 @@ def _resolve_notify_target() -> Optional[dict[str, Any]]:
if not notifier_profile:
from hermes_cli.profiles import current_profile_name
notifier_profile = current_profile_name("default")
+ if platform == "tui":
+ # The inherited key can be stale after a compaction fork (#110068): bind the
+ # subscription to the live continuation tip, not the key the process started with.
+ chat_id = _live_tui_session_key(chat_id, notifier_profile)
delivery_metadata: dict[str, Any] = {
k: v for k, v in (
("thread_id", thread_id), ("chat_type", chat_type),