Skip to content

The agent that grows with you

fix(state): free a ghost's title at rename time instead of skipping it

kshitijk4poor3c17b9fd661a
diff --git a/hermes_state_titles.py b/hermes_state_titles.py
index 581d8259efe..7ed002bd275 100644
--- a/hermes_state_titles.py
+++ b/hermes_state_titles.py
@@ -112,8 +112,8 @@ class SessionTitlesMixin:
return 0
if title:
conflict = conn.execute(
- "SELECT id, archived, hidden FROM sessions WHERE title = ? AND id != ? AND message_count > 0",
- (title, session_id),
+ f"SELECT id, archived, hidden, {self._EMPTY_SESSION_WHERE} AS ghost "
+ "FROM sessions WHERE title = ? AND id != ?", (title, session_id),
).fetchone()
if conflict:
conflict_id = conflict["id"]
@@ -125,8 +125,12 @@ class SessionTitlesMixin:
# entry, not a live identity. Retire its name in the same title
# transaction so a replacement can become the sole canonical row;
# the old session remains archived and otherwise untouched.
+ # An ended, empty, visible ghost (abandoned new chat that listings filter
+ # out, so the user cannot find it to free the name) yields too (#81888).
+ # The full index stays: a ``message_count > 0`` partial index would make
+ # the ghost's first append_message fail with IntegrityError.
elif (title == self.CANONICAL_BOT_CHAT_TITLE and bool(conflict["archived"])
- and bool(conflict["hidden"])):
+ and bool(conflict["hidden"])) or (conflict["ghost"] and not conflict["hidden"]):
conn.execute(
"UPDATE sessions SET title = NULL, title_source = NULL WHERE id = ?",
(conflict_id,),
diff --git a/tests/hermes_state/test_hermes_state.py b/tests/hermes_state/test_hermes_state.py
index d5505103c2c..062ae8a2e70 100644
--- a/tests/hermes_state/test_hermes_state.py
+++ b/tests/hermes_state/test_hermes_state.py
@@ -1708,30 +1708,35 @@ class TestSessionTitle:
session = db.get_session("s1")
assert session["title"] is None
- def test_empty_ghost_session_does_not_block_title(self, db):
- """Empty sessions (0 messages) should not block title reuse (#81888).
-
- Regression test: ghost sessions created by interrupted flows (e.g., desktop
- session creation that never sent a message) would block rename attempts even
- though they were invisible in the UI.
- """
- # Create a ghost session (0 messages) with a title
- ghost_id = db.create_session("ghost", "desktop")
- db.set_session_title(ghost_id, "Canada")
- assert db.get_session(ghost_id)["message_count"] == 0
-
- # Create a real session with messages
- real_id = db.create_session("real", "desktop")
- db.append_message(real_id, "user", "Hello")
- db.append_message(real_id, "assistant", "Hi there")
- assert db.get_session(real_id)["message_count"] == 2
-
- # Should be able to rename the real session to "Canada" — ghost doesn't block
- db.set_session_title(real_id, "Canada")
- assert db.get_session(real_id)["title"] == "Canada"
-
- # Ghost session should still exist but be effectively invisible
- assert db.get_session(ghost_id) is not None
+ @pytest.mark.parametrize("holder_messages,holder_hidden,holder_ended,yields", [
+ (0, False, True, True), # ended empty visible ghost yields its title (#81888)
+ (0, False, False, False), # live empty session (/title before its first turn) keeps it
+ (0, True, True, False), # hidden empty row (fresh canonical Bot Chat) keeps it
+ (1, False, True, False), # a real conversation keeps it
+ ])
+ def test_empty_ghost_session_does_not_block_title(self, db, holder_messages, holder_hidden, holder_ended,
+ yields):
+ """An empty session the user cannot see must not reserve a title (#81888), and the
+ ghost must stay writable afterwards (no partial unique index to trip on append)."""
+ db.create_session("ghost", "desktop")
+ db.set_session_title("ghost", "Canada")
+ for i in range(holder_messages):
+ db.append_message("ghost", "user", f"m{i}")
+ db.set_session_hidden("ghost", holder_hidden)
+ if holder_ended:
+ db.end_session("ghost", "user_exit")
+ db.create_session("real", "desktop")
+ db.append_message("real", "user", "Hello")
+ if not yields:
+ with pytest.raises(ValueError, match="already in use"):
+ db.set_session_title("real", "Canada")
+ assert db.get_session("ghost")["title"] == "Canada"
+ return
+ assert db.set_session_title("real", "Canada")
+ assert db.get_session("ghost")["title"] is None
+ assert db.resolve_session_by_title("Canada") == "real"
+ db.append_message("ghost", "user", "late first message")
+ assert db.get_session("ghost")["message_count"] == 1
class TestSessionTitleIndexRepair: