The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): keep a swept derived session's fence past the horizon
unsupportedpastelse1b52559b33a
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.pyindex ba35d1097b8..28875d31a4f 100644--- a/hermes_state_mutation_retirement.py+++ b/hermes_state_mutation_retirement.py@@ -167,13 +167,21 @@ def retire_mutation_receipts(conn, session_ids):conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json(receipt), key))+# Creation ids of the local conversations a gateway door derives and walks by generation+# (``first_generation``): Bot Chat and A2A contexts.+_DERIVED_LOCAL_PREFIXES = ('bot-chat:', 'a2a:')+# Stored sources of the API sessions whose ids a door derives: the header-less chat and a+# Responses/Runs chain (``api_server``) and a hosted-room member (``bot_room``).+_DERIVED_SOURCES = ('api_server', 'bot_room')++def _local_conversations(conn):- """``(members, bot_chats)``: member id -> every id of its local conversation (the creation id+ """``(members, derived)``: member id -> every id of its local conversation (the creation id(policy, FIFO, generation), each receipt lineage segment and the current target), and the ids- of every Bot Chat conversation. One prefix scan, not a lookup per swept id. Malformed receipts- name nothing."""+ of every derived (Bot Chat, A2A) conversation. One prefix scan, not a lookup per swept id.+ Malformed receipts name nothing."""from hermes_state_local import POLICY_PREFIX- members, bot_chats = {}, set()+ members, derived = {}, set()for (raw,) in conn.execute('SELECT value FROM state_meta WHERE key GLOB ?', (POLICY_PREFIX + '*',)):try:receipt = json.loads(raw)@@ -185,9 +193,16 @@ def _local_conversations(conn):ids = tuple(x for x in [receipt.get('session_id'), *(lineage if isinstance(lineage, list) else []),entry.get('session_id') if isinstance(entry, dict) else None] if isinstance(x, str))members.update(dict.fromkeys(ids, ids))- if str(receipt.get('request_id') or '').startswith('bot-chat:'):- bot_chats.update(ids)- return members, bot_chats+ if str(receipt.get('request_id') or '').startswith(_DERIVED_LOCAL_PREFIXES):+ derived.update(ids)+ return members, derived+++def _derived_api_sessions(conn, session_ids):+ """The ids among *session_ids* whose stored source is a derived API session's."""+ return {row[0] for row in conn.execute(+ f"SELECT id FROM sessions WHERE source IN ({','.join('?' * len(_DERIVED_SOURCES))}) "+ "AND id IN (SELECT value FROM json_each(?))", (*_DERIVED_SOURCES, json.dumps(list(session_ids))))}def retire_prunable(conn, session_ids):@@ -198,15 +213,19 @@ def retire_prunable(conn, session_ids):work (the owner's queued admission continues on the current segment), every one is skipped.A sweep's markers age out at the retry horizon: housekeeping retires thousands of ids a pass- (cron), and a permanent marker each would grow ``state_meta`` without bound. A Bot Chat's stays:- its mailbox receipts outlive the horizon and its generation walk would reissue the id."""- swept, (conversations, bot_chats) = set(session_ids), _local_conversations(conn)+ (cron), and a permanent marker each would grow ``state_meta`` without bound. A derived id's+ stays (Bot Chat, A2A context, header-less chat, Responses chain, hosted-room member): its+ conversation resumes on the next generation, and the generation walk stops at the first+ unfenced id, so an aged-out marker would send the next turn back to the swept generation. A+ Bot Chat's mailbox receipts also outlive the horizon."""+ swept, (conversations, derived) = set(session_ids), _local_conversations(conn)def removable(sid):whole = conversations.get(sid, (sid,))return swept.issuperset(whole) and not any(conn.execute(_LIVE_LEDGER_SQL, (x, x)).fetchone() for x in whole)quiet = [sid for sid in session_ids if removable(sid)]- retire_sessions(conn, quiet, expiring=[sid for sid in quiet if sid not in bot_chats])+ derived |= _derived_api_sessions(conn, quiet)+ retire_sessions(conn, quiet, expiring=[sid for sid in quiet if sid not in derived])return quietdiff --git a/tests/gateway/test_derived_generation_after_sweep.py b/tests/gateway/test_derived_generation_after_sweep.pynew file mode 100644index 00000000000..0eef9469fe7--- /dev/null+++ b/tests/gateway/test_derived_generation_after_sweep.py@@ -0,0 +1,88 @@+"""A derived conversation resumed after a housekeeping sweep stays on its new session.++A sweep (``prune_sessions``) retires a derived id: a header-less chat, a hosted-room member, an A2A+context. The next turn moves to the next generation of that id, and the conversation goes on+there. Once the swept id's retry horizon passes, the next turn must still land on that new+generation. If the swept id's fence aged out, the generation walk would fall back to generation 0+and split the conversation across two sessions.+"""+from types import SimpleNamespace++import pytest+from aiohttp.test_utils import TestClient, TestServer++from tests.gateway.test_api_implicit_session_after_delete import (+ _OPENING, _app, _content, _post, served)+from tests.gateway.test_derived_session_after_delete import _settle, a2a+++def _sweep(authority, sid):+ """A housekeeping prune retires the idle, ended session *sid*."""+ authority.db.end_session(sid, 'test')+ authority.db.prune_sessions(older_than_days=None)+ assert authority.db.get_session(sid) is None, 'the sweep kept the session'+ authority.sessions.pop(sid, None)+++def _past_the_horizon(authority):+ from hermes_state_mutation_retirement import prune_retired_tombstones+ authority.db._execute_write(lambda conn: prune_retired_tombstones(conn, older_than=0))+++async def test_a_header_less_chat_resumed_after_a_sweep_keeps_its_new_session(api, owner, served):+ async with TestClient(TestServer(_app(api))) as client:+ status, first, swept = await _post(client, _OPENING)+ assert status == 200, first+ _sweep(owner, swept)+ status, opened, resumed = await _post(client, _OPENING)+ assert status == 200, opened+ assert resumed != swept+ _past_the_horizon(owner)+ follow_up = [*_OPENING, {'role': 'assistant', 'content': _content(opened)}, {'role': 'user', 'content': 'next'}]+ status, second, landed = await _post(client, follow_up)+ assert status == 200, second+ assert landed == resumed, 'the next turn left the conversation for the swept generation'+++async def test_a_hosted_room_member_resumed_after_a_sweep_keeps_its_new_session(api, owner):+ from gateway.hosted_room_peer import HostedMemberDispatch+ from tests.gateway.test_hosted_room_execution_policy import _dispatch, _policy+ dispatch = _dispatch(_policy())+ swept = await api._ensure_hosted_member_session(dispatch)+ _sweep(owner, swept)++ def task(n):+ return HostedMemberDispatch.from_mapping(dict(dispatch.as_mapping(), task_id=f'task-{n}', execution_generation=n))+ resumed = await api._ensure_hosted_member_session(task(2))+ assert resumed != swept+ _past_the_horizon(owner)+ assert await api._ensure_hosted_member_session(task(3)) == resumed, 'the room member fell back to the swept session'+++async def test_an_a2a_context_resumed_after_a_sweep_keeps_its_new_session(a2a):+ authority, forward = a2a+ first = await forward('first', 'hello')+ swept = first['result']['session_id']+ _settle(authority, swept)+ _sweep(authority, swept)+ later = await forward('second', 'are you there?')+ assert 'result' in later, later+ resumed = later['result']['session_id']+ assert resumed != swept+ _past_the_horizon(authority)+ again = await forward('third', 'still there?')+ assert 'result' in again, again+ assert again['result']['session_id'] == resumed, 'the A2A context fell back to the swept session'+++def test_an_ordinary_swept_session_still_ages_out(owner):+ """Housekeeping on a cron-heavy store: an id no door derives keeps an expiring marker."""+ from hermes_state_mutation_retirement import RETIRED_PREFIX+ owner.db.create_session('cron_job_run_1', source='cron')+ _sweep(SimpleNamespace(db=owner.db, sessions={}), 'cron_job_run_1')+ assert owner.db.get_meta(RETIRED_PREFIX + 'cron_job_run_1') is not None+ _past_the_horizon(owner)+ assert owner.db.get_meta(RETIRED_PREFIX + 'cron_job_run_1') is None