The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat: persist assigned worker prompt context and usage anchors
Tekniumc6540cb9dd86
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.pyindex de431a492a9..e873880e546 100644--- a/agent/runtime_session_store.py+++ b/agent/runtime_session_store.py@@ -227,6 +227,22 @@ class RuntimeSessionStore:raise WorkerPersistenceError('pending_receipt')return True+ def get_session(self, session_id):+ self._session(session_id)+ return self._apply('session.context', {})['session']++ def get_session_model_config_value(self, session_id, key, default=None):+ from hermes_state_sessions import _parse_model_config+ return _parse_model_config(self.get_session(session_id).get('model_config')).get(key, default)++ def update_system_prompt(self, session_id, system_prompt):+ self._session(session_id)+ self._apply('session.prompt', {'system_prompt': system_prompt})++ def patch_session_model_config(self, session_id, patch):+ self._session(session_id)+ self._apply('session.sidecars', {'patch': patch})+def finish(self):return self._apply('execution.finish', {})diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex a47e47fbec4..b14d225b4a7 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -537,7 +537,11 @@ 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_sidecarshandlers = {+ 'session.context': worker_context,+ 'session.prompt': worker_prompt,+ 'session.sidecars': worker_sidecars,'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.pynew file mode 100644index 00000000000..1b1543b26b1--- /dev/null+++ b/hermes_state_worker_context.py@@ -0,0 +1,37 @@+"""Closed constructor-context operations inside the worker receipt transaction.++Identity and lineage remain owner-reserved. This is not arbitrary SessionDB RPC.+"""+from hermes_state_runtime import RuntimeStoreError+++SIDECAR_KEYS = frozenset({'_usage_anchor', '_proactive_prune_rearm_tokens'})+++def worker_context(db, conn, session_id, payload):+ if payload:+ raise RuntimeStoreError('invalid_params')+ row = conn.execute(+ 'SELECT s.*, COALESCE(sp.prompt, s.system_prompt) AS _system_prompt_resolved '+ 'FROM sessions s LEFT JOIN system_prompts sp ON sp.hash=s.system_prompt_hash WHERE s.id=?',+ (session_id,),+ ).fetchone()+ return {'session': db._session_row_dict(row)}+++def worker_prompt(db, conn, session_id, payload):+ if set(payload) != {'system_prompt'} or not (+ payload['system_prompt'] is None or isinstance(payload['system_prompt'], str)):+ raise RuntimeStoreError('invalid_params')+ conn.execute('UPDATE sessions SET system_prompt_hash=?, system_prompt=NULL WHERE id=?',+ (db._store_system_prompt(conn, payload['system_prompt']), session_id))+ db._delete_unreferenced_system_prompts(conn)+ return {'value': None}+++def worker_sidecars(db, conn, session_id, payload):+ if set(payload) != {'patch'} or not isinstance(payload['patch'], dict) or set(payload['patch']) - SIDECAR_KEYS:+ raise RuntimeStoreError('invalid_params')+ 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}diff --git a/tests/state/test_runtime_worker_context.py b/tests/state/test_runtime_worker_context.pynew file mode 100644index 00000000000..762ce04e5d8--- /dev/null+++ b/tests/state/test_runtime_worker_context.py@@ -0,0 +1,44 @@+"""Constructor context is assignment-scoped and shares durable mutation receipts."""+import pytest++from agent.runtime_session_store import RuntimeSessionStore, WorkerPersistenceError+from hermes_state import SessionDB+from hermes_state_runtime import begin_runtime_epoch, mutate_worker_execution, register_worker_execution+++def test_worker_context_preserves_authority_identity_and_prompt_receipts(tmp_path):+ db = SessionDB(tmp_path / 'state.db')+ try:+ db.create_session('owned', 'subagent', parent_session_id=None, model='requested',+ model_config={'_delegate_from': 'parent'}, system_prompt='prefix', profile_name='fixture')+ db.create_session('foreign', 'cli', system_prompt='private')+ 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='child', adoption_secret='test-secret')+ store = RuntimeSessionStore(lambda method, **p: mutate_worker_execution(db, **p), scope, tmp_path / 'outbox')+ try:+ assert store.get_session('owned')['system_prompt'] == 'prefix'+ with pytest.raises(WorkerPersistenceError, match='permission_denied'):+ store.get_session('foreign')+ store.update_system_prompt('owned', 'stable-prefix')+ store.patch_session_model_config('owned', {'_usage_anchor': {'tokens': 23}})+ assert store.get_session_model_config_value('owned', '_usage_anchor') == {'tokens': 23}+ assert db.get_session_model_config_value('owned', '_delegate_from') == 'parent'+ assert db.get_session('foreign')['system_prompt'] == 'private'+ original = store.rpc+ def lost_ack(method, **params):+ original(method, **params)+ raise TimeoutError('lost-ack')+ store.rpc = lost_ack+ with pytest.raises(TimeoutError, match='lost-ack'):+ store.update_system_prompt('owned', 'durable-prefix')+ store.rpc = original+ assert len(store.retry_pending()) == 1+ assert db.get_session('owned')['system_prompt'] == 'durable-prefix'+ with pytest.raises(Exception, match='invalid_params'):+ mutate_worker_execution(db, **scope, sequence=store.journal['next_sequence'],+ operation='session.sidecars', payload={'patch': {'_delegate_from': 'foreign'}})+ finally:+ store.close()+ finally:+ db.close()