Skip to content

The agent that grows with you

fix: retain recoverable worker title conflicts as durable outcomes

Tekniumb7d14c758e04
diff --git a/agent/runtime_session_lifecycle.py b/agent/runtime_session_lifecycle.py
index 4236270e341..371dc6d34da 100644
--- a/agent/runtime_session_lifecycle.py
+++ b/agent/runtime_session_lifecycle.py
@@ -20,19 +20,27 @@ class RuntimeSessionLifecycleMixin:
def set_session_title(self, session_id, title):
self._session(session_id)
- return self._apply('session.title', {'title': title, 'source': 'user'})['value']
+ return self._title_result(self._apply('session.title', {'title': title, 'source': 'user'}))
def set_auto_title(self, session_id, title, *, source):
if source not in ('derived', 'llm'):
raise ValueError(f'invalid automatic title source: {source!r}')
self._session(session_id)
- return self._apply('session.title', {'title': title, 'source': source})['value']
+ return self._title_result(self._apply('session.title', {'title': title, 'source': source}))
+
+ @staticmethod
+ def _title_result(result):
+ if result.get('error'):
+ raise ValueError(result['error'])
+ return result['value']
def get_session_title_source(self, session_id):
row = self.get_session(session_id)
return row.get('title_source') if row.get('title') is not None else None
def set_session_title_source(self, session_id, source):
+ if source not in ('user', 'derived', 'llm'):
+ raise ValueError(f'invalid title source: {source!r}')
self._session(session_id)
return self._apply('session.title_source', {'source': source})['value']
diff --git a/hermes_state_worker_lifecycle.py b/hermes_state_worker_lifecycle.py
index 63046adef4b..4675b546e65 100644
--- a/hermes_state_worker_lifecycle.py
+++ b/hermes_state_worker_lifecycle.py
@@ -58,8 +58,14 @@ def worker_title(db, conn, session_id, payload):
if (set(payload) != {'title', 'source'} or payload['source'] not in ('user', 'derived', 'llm')
or not (payload['title'] is None or isinstance(payload['title'], str))):
raise RuntimeStoreError('invalid_params')
- return {'value': bool(db._set_session_title_in_transaction(
- conn, session_id, payload['title'], source=payload['source']))}
+ try:
+ value = bool(db._set_session_title_in_transaction(
+ conn, session_id, payload['title'], source=payload['source']))
+ except ValueError:
+ # Expected validation/conflict outcomes must not poison the retry journal.
+ # Keep candidate authority in the receipt, not an in-memory worker claim.
+ return {'value': False, 'error': 'Title unavailable or invalid', 'title_candidate': payload['title']}
+ return {'value': value}
def worker_title_source(db, conn, session_id, payload):
@@ -76,13 +82,22 @@ def worker_next_title(db, conn, session_id, payload):
if set(payload) != {'base_title'} or not isinstance(payload['base_title'], str):
raise RuntimeStoreError('invalid_params')
title = conn.execute('SELECT title FROM sessions WHERE id=?', (session_id,)).fetchone()[0]
- # This is an assigned title continuation, not a profile-wide title oracle.
- if not title or payload['base_title'] != title:
+ # Only an assigned title or a receipted title attempt authorizes dedupe.
+ # Concurrent activity/usage receipts must not invalidate that title attempt.
+ candidate = payload['base_title']
+ attempted = conn.execute(
+ "SELECT 1 FROM worker_receipts r JOIN worker_executions w ON w.execution_id=r.execution_id "
+ "WHERE w.session_id=? AND w.status='running' "
+ "AND json_extract(r.result_json,'$.title_candidate')=? LIMIT 1",
+ (session_id, candidate)).fetchone()
+ if candidate != title and attempted is None:
raise RuntimeStoreError('permission_denied')
- match = _NUMBERED_TITLE_RE.match(title)
- base = match.group(1) if match else title
+ match = _NUMBERED_TITLE_RE.match(candidate)
+ base = match.group(1) if match else candidate
rows = conn.execute("SELECT title FROM sessions WHERE title=? OR title LIKE ? ESCAPE '\\'",
(base, f'{escape_like(base)} #%')).fetchall()
+ if not rows:
+ return {'value': base}
numbers = [int(m.group(2)) for m in (_NUMBERED_TITLE_RE.match(row['title']) for row in rows) if m]
return {'value': f'{base} #{max([1, *numbers]) + 1}'}
diff --git a/tests/state/test_runtime_worker_title_retry.py b/tests/state/test_runtime_worker_title_retry.py
new file mode 100644
index 00000000000..f4cf6a7d392
--- /dev/null
+++ b/tests/state/test_runtime_worker_title_retry.py
@@ -0,0 +1,29 @@
+"""Expected title conflicts are committed outcomes, not failed persistence."""
+import pytest
+
+from agent.title_generator import _persist_session_title
+from hermes_state_runtime import RuntimeStoreError, mutate_worker_execution
+from tests.state.test_runtime_worker_lifecycle import setup_store
+
+
+def test_worker_title_collision_can_dedupe_without_poisoning_persistence(tmp_path):
+ db, store, scope = setup_store(tmp_path)
+ try:
+ db.set_session_title('foreign', 'shared-title')
+ # Call the real consumer: it catches ValueError, gets a suffix, then retries.
+ assert _persist_session_title(store, 'owned', 'shared-title', source='llm') == 'shared-title #2'
+ assert db.get_session('owned')['title_source'] == 'llm'
+ assert db.get_session('foreign')['title'] == 'shared-title'
+ with pytest.raises(ValueError):
+ store.set_session_title('owned', 'x' * (db.MAX_TITLE_LENGTH + 1))
+ with pytest.raises(ValueError):
+ store.set_session_title_source('owned', 'invalid')
+ assert store.append_messages_batch('owned', [{'role': 'assistant', 'content': 'still-running'}]) == 1
+ # An unrelated title query is not an execution capability.
+ with pytest.raises(RuntimeStoreError, match='permission_denied'):
+ mutate_worker_execution(db, **scope, sequence=store.journal['next_sequence'],
+ operation='session.next_title', payload={'base_title': 'unrelated'})
+ assert store.finish()['status'] == 'terminal'
+ finally:
+ store.close()
+ db.close()