Skip to content

The agent that grows with you

fix: fence ancestor revisions when runtime titles transfer

Teknium62aef7959e8d
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 9a896672b6b..e53a2aab64a 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -209,9 +209,8 @@ def mutate_runtime_session(db, *, epoch: int, principal_id: str, session_id: str
if session['runtime_revision'] != expected_revision:
raise RuntimeStoreError('stale_revision')
if operation == 'rename':
- db._set_session_title_in_transaction(
+ affected = db._set_session_title_in_transaction(
conn, session_id, payload['title'], source=db.TITLE_SOURCE_USER)
- affected = [session_id]
else:
affected = db._set_lineage_column_in_transaction(
conn, 'archived', session_id, int(payload['archived']))
diff --git a/hermes_state_titles.py b/hermes_state_titles.py
index 58ef8883c51..4ed2fc944a7 100644
--- a/hermes_state_titles.py
+++ b/hermes_state_titles.py
@@ -72,21 +72,22 @@ class SessionTitlesMixin:
re-running the titler on an llm row is a no-op). No writer may move a hidden
canonical Bot Chat off its title. Read and write are one compare-and-swap
transaction, so a manual ``/title`` racing an in-flight generation is not clobbered."""
- return self._execute_write(
+ return bool(self._execute_write(
lambda conn: self._set_session_title_in_transaction(conn, session_id, title, source=source)
- ) > 0
+ ))
def _set_session_title_in_transaction(self, conn, session_id: str, title: str, *, source: str):
"""Reuse title rules within an authority-owned transaction; never commit here."""
title = self.sanitize_title(title)
is_user = source == self.TITLE_SOURCE_USER
new_rank = self._title_rank(source) if not is_user else None
+ affected = []
current = conn.execute(
"SELECT title, title_source, hidden FROM sessions WHERE id = ?", (session_id,),
).fetchone()
if current is None:
- return 0
+ return []
# The canonical Bot Chat's NAME is its identity (Bot Mode resolves it by
# exact-title lookup on every open), so a rename orphans the conversation. Hidden
# is the discriminator: canonical chats are born hidden; a visible session merely
@@ -97,9 +98,9 @@ class SessionTitlesMixin:
raise ValueError("This is the bot's canonical Bot Chat — its name is its "
"identity, and renaming it would orphan the conversation. "
"To start fresh, create a new bot instead.")
- return 0
+ return []
if not is_user and current["title"] is not None and self._title_rank(current["title_source"]) >= new_rank:
- return 0
+ return []
if title:
conflict = conn.execute(
"SELECT id FROM sessions WHERE title = ? AND id != ?", (title, session_id),
@@ -110,14 +111,16 @@ class SessionTitlesMixin:
# user, so transfer it onto the tip (uniqueness + lineage kept).
if self._is_compression_ancestor(conn, ancestor_id=conflict_id, descendant_id=session_id):
conn.execute("UPDATE sessions SET title = NULL WHERE id = ?", (conflict_id,))
+ affected.append(conflict_id)
else:
raise ValueError(f"Title '{title}' is already in use by session {conflict_id}")
# CAS on the values just read (``IS`` is NULL-safe): a concurrent write between
# the SELECT and here loses instead of being overwritten.
- return conn.execute(
+ changed = conn.execute(
"UPDATE sessions SET title = ?, title_source = ? WHERE id = ? AND title IS ? AND title_source IS ?",
(title, source if title else None, session_id, current["title"], current["title_source"]),
).rowcount
+ return [*affected, session_id] if changed else affected
def set_session_title(self, session_id: str, title: str) -> bool:
"""Set a title on the user's behalf (``user`` provenance). Empty clears it. Raises
diff --git a/tests/state/test_runtime_mutations.py b/tests/state/test_runtime_mutations.py
index 124970a6d65..553119b5b38 100644
--- a/tests/state/test_runtime_mutations.py
+++ b/tests/state/test_runtime_mutations.py
@@ -24,6 +24,16 @@ def test_mutation_receipt_fences_replay_revision_and_epoch(tmp_path, operation,
mutate(db, **{**args, 'expected_revision': 1})
with pytest.raises(rt.RuntimeStoreError, match='stale_revision'):
mutate(db, **{**args, 'request_id': 'other'})
+ peer = SessionDB(db_path=tmp_path / 'state.db')
+ try:
+ with pytest.raises(rt.RuntimeStoreError, match='stale_revision'):
+ mutate(peer, **{**args, 'request_id': 'peer'})
+ later = mutate(peer, **{**args, 'request_id': 'peer', 'expected_revision': 1})
+ assert later['revision'] == 2
+ assert mutate(db, **args) == result
+ assert db.get_session('s')['runtime_revision'] == 2
+ finally:
+ peer.close()
new_epoch = rt.begin_runtime_epoch(db, instance_id='replacement')
with pytest.raises(rt.RuntimeStoreError, match='stale_epoch'):
mutate(db, **args)
@@ -65,6 +75,13 @@ def test_mutation_rules_and_receipt_failure_are_atomic(tmp_path, operation, payl
mutate(db, **args)
assert db.get_session_title('s') == 'New name'
assert db.get_session_title_source('s') == db.TITLE_SOURCE_USER
+ db.end_session('s', end_reason='compression')
+ db.create_session('tip', source='test', parent_session_id='s')
+ mutate(db, **{**args, 'session_id': 'tip', 'request_id': 'transfer'})
+ assert db.get_session_title('s') is None
+ assert db.get_session_title('tip') == 'New name'
+ assert db.get_session('s')['runtime_revision'] == 2
+ assert db.get_session('tip')['runtime_revision'] == 1
else:
db.create_session('child', source='test', parent_session_id='s')
db.end_session('s', end_reason='compression')