Skip to content

The agent that grows with you

fix(workers): managed workers keep the session's tools[] prefix pinned across turns

teknium19891fdf33731
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.py
index 1e6457eda16..89ac68eaee8 100644
--- a/agent/runtime_session_store.py
+++ b/agent/runtime_session_store.py
@@ -305,6 +305,12 @@ class RuntimeSessionStore(RuntimeSessionCompressionMixin, RuntimeSessionLifecycl
self._session(session_id)
self._apply('session.sidecars', {'patch': patch})
+ def update_session_tool_names(self, session_id, tool_names):
+ # The tools[] freeze pin: without it every fresh worker re-probes check_fns and a
+ # config flip between turns silently forks the cached prefix (in-process stays pinned).
+ self._session(session_id)
+ self._apply('session.tools', {'tool_names': None if tool_names is None else list(tool_names)})
+
def finish(self):
return self._apply('execution.finish', {})
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 5ecac71c0cf..67e4534adcd 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -564,7 +564,7 @@ def _worker_finish(db, conn, session_id, payload):
def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,
sequence, operation, payload):
"""One closed durable mutation and receipt; never call a self-committing API here."""
- from hermes_state_worker_context import worker_context, worker_prompt, worker_sidecars
+ from hermes_state_worker_context import worker_context, worker_prompt, worker_sidecars, worker_tool_names
from hermes_state_worker_compression import WORKER_COMPRESSION_HANDLERS, worker_receipt_assignment
from hermes_state_worker_lifecycle import WORKER_LIFECYCLE_HANDLERS
handlers = {
@@ -573,6 +573,7 @@ def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,
'session.context': worker_context,
'session.prompt': worker_prompt,
'session.sidecars': worker_sidecars,
+ 'session.tools': worker_tool_names,
'transcript.append': _worker_append,
'execution.finish': _worker_finish,
'usage.main': _worker_usage,
diff --git a/hermes_state_worker_context.py b/hermes_state_worker_context.py
index 1b1543b26b1..c9cb1316cb5 100644
--- a/hermes_state_worker_context.py
+++ b/hermes_state_worker_context.py
@@ -2,6 +2,8 @@
Identity and lineage remain owner-reserved. This is not arbitrary SessionDB RPC.
"""
+import json
+
from hermes_state_runtime import RuntimeStoreError
@@ -35,3 +37,13 @@ def worker_sidecars(db, conn, session_id, payload):
merged = db._merge_model_config_json(conn, session_id, payload['patch'], on_missing='raise')
conn.execute('UPDATE sessions SET model_config=? WHERE id=?', (merged, session_id))
return {'value': None}
+
+
+def worker_tool_names(db, conn, session_id, payload):
+ names = payload.get('tool_names') if set(payload) == {'tool_names'} else False
+ if names is False or not (names is None or (isinstance(names, list) and len(names) <= 4096
+ and all(isinstance(n, str) and 0 < len(n) <= 256 for n in names))):
+ raise RuntimeStoreError('invalid_params')
+ conn.execute('UPDATE sessions SET tool_names=? WHERE id=?',
+ (None if names is None else json.dumps(names), session_id))
+ return {'value': None}
diff --git a/tests/agent/test_runtime_session_store.py b/tests/agent/test_runtime_session_store.py
index 2f76c15c282..6635c35af96 100644
--- a/tests/agent/test_runtime_session_store.py
+++ b/tests/agent/test_runtime_session_store.py
@@ -81,3 +81,34 @@ def test_outbox_filesystem_failure_is_sticky(tmp_path, monkeypatch):
assert store.failure == 'disk unavailable'
finally:
store._outbox_owner.close()
+
+
+def test_worker_store_persists_and_restores_the_tools_prefix_pin(tmp_path):
+ """persist_agent_tool_names writes the pin through the worker facade and a rebuilt worker
+ agent reads it back from the same row (F26). Without the facade method the pin stays NULL
+ and a config flip between turns silently forks the cached tools[] prefix."""
+ from types import SimpleNamespace
+ from hermes_state import SessionDB
+ from hermes_state_runtime import begin_runtime_epoch, mutate_worker_execution, register_worker_execution
+ from tools.mcp_tool_agent import persist_agent_tool_names
+ db = SessionDB(tmp_path / 'state.db')
+ try:
+ db.create_session('owned', 'cli', system_prompt='prefix')
+ epoch = begin_runtime_epoch(db, instance_id='fixture')
+ scope = dict(epoch=epoch, execution_id='worker', session_id='owned', generation=0)
+ register_worker_execution(db, **scope, kind='compute', adoption_secret='test-secret')
+ store = RuntimeSessionStore(lambda method, **p: mutate_worker_execution(db, **p), scope, tmp_path / 'outbox')
+ try:
+ tools = [{'type': 'function', 'function': {'name': n, 'parameters': {}}}
+ for n in ('terminal', 'tool_search', 'tool_describe', 'tool_call')]
+ persist_agent_tool_names(SimpleNamespace(_session_db=store, session_id='owned', tools=tools))
+ assert json.loads(db.get_session('owned')['tool_names']) == ['terminal', 'tool_search', 'tool_describe', 'tool_call']
+ assert json.loads(store.get_session('owned')['tool_names']) == ['terminal', 'tool_search', 'tool_describe', 'tool_call']
+ with pytest.raises(WorkerPersistenceError, match='permission_denied'):
+ store.update_session_tool_names('foreign', ['terminal'])
+ with pytest.raises(Exception, match='invalid_params'):
+ store.update_session_tool_names('owned', [{'not': 'a name'}])
+ finally:
+ store._outbox_owner.close()
+ finally:
+ db.close()