The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): one generation walk for derived session ids
unsupportedpastels8fbe279f5e6a
diff --git a/gateway/session_api.py b/gateway/session_api.pyindex 5758357c62b..7045159cee7 100644--- a/gateway/session_api.py+++ b/gateway/session_api.py@@ -33,17 +33,12 @@ def implicit_api_session(db, session_id, request_id=None):only, so every later turn of that conversation (same opening) lands on the same session. Anexact retry stays on the generation holding its request id's terminal identity; explicit idsnever come here."""- from hermes_state_mutation_retirement import RETIRED_PREFIX+ from hermes_state_mutation_retirement import first_generationfrom hermes_state_terminal import identity_key- candidate, generation = session_id, 0- with db._read_ctx() as conn:- while conn.execute('SELECT 1 FROM state_meta WHERE key=?', (RETIRED_PREFIX + candidate,)).fetchone():- if request_id and conn.execute('SELECT 1 FROM state_meta WHERE key=?',- (identity_key('api', candidate, request_id),)).fetchone():- break- generation += 1- candidate = 'api-' + hashlib.sha256(f'{session_id}\0{generation}'.encode()).hexdigest()[:16]- return candidate+ pinned = None if not request_id else lambda conn, sid: conn.execute(+ 'SELECT 1 FROM state_meta WHERE key=?', (identity_key('api', sid, request_id),)).fetchone() is not None+ derive = lambda n: session_id if n == 0 else 'api-' + hashlib.sha256(f'{session_id}\0{n}'.encode()).hexdigest()[:16]+ return first_generation(db, derive, pinned=pinned)[1]def _served_profile(authority):diff --git a/gateway/session_bot.py b/gateway/session_bot.pyindex e588e64f988..24132fd640f 100644--- a/gateway/session_bot.py+++ b/gateway/session_bot.py@@ -234,15 +234,16 @@ def _bot_chat_creation_id(authority, actor):concurrent deliveries (serialized by the titled-create lock) pick the same identity. Anexisting UNtitled session is the same creation interrupted before its title: it is reused."""from gateway.session_local_recovery import local_identity- from hermes_state_mutation_retirement import retired_session- base, generation = 'bot-chat:' + authority.profile_id, 0- while True:- request_id = base if generation == 0 else f'{base}:{generation}'- sid = local_identity(authority.profile_id, actor.subject, request_id)- row = authority.db.get_session(sid)- if not retired_session(authority.db, sid) and (row is None or not row.get('title')):- return request_id- generation += 1+ from hermes_state_mutation_retirement import first_generation+ base = 'bot-chat:' + authority.profile_id+ request_id = lambda generation: base if generation == 0 else f'{base}:{generation}'++ def titled(conn, sid):+ row = conn.execute('SELECT title FROM sessions WHERE id=?', (sid,)).fetchone()+ return row is not None and bool(row[0])+ generation, _sid = first_generation(+ authority.db, lambda n: local_identity(authority.profile_id, actor.subject, request_id(n)), spent=titled)+ return request_id(generation)def _admission_outcome(authority, admission_id, fallback=None):diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.pyindex 31cbc09a275..3fa444b4532 100644--- a/hermes_state_mutation_retirement.py+++ b/hermes_state_mutation_retirement.py@@ -8,10 +8,32 @@ RETIRED_PREFIX = 'gateway.retired_session.v1.'VIEWER_CLOSE_PREFIX = 'gateway.viewer_close.v1:'+def retired_in(conn, session_id):+ return conn.execute('SELECT 1 FROM state_meta WHERE key=?', (RETIRED_PREFIX + session_id,)).fetchone() is not None++def retired_session(db, session_id):with db._read_ctx() as conn:- return conn.execute('SELECT 1 FROM state_meta WHERE key=?',- (RETIRED_PREFIX + session_id,)).fetchone() is not None+ return retired_in(conn, session_id)+++def first_generation(db, derive, *, pinned=None, spent=None):+ """``(n, derive(n))``: the first generation of a derived session id a NEW request may use. A+ user-deleted id stays retired for good, so a derived id (header-less chat, Bot Chat, hosted-room+ member, A2A context) whose session was deleted moves to its next generation, never recreated.+ ``pinned(conn, sid)`` keeps an exact retry on the retired generation holding its identity;+ ``spent(conn, sid)`` skips a live one used up another way (a renamed Bot Chat). Read from+ durable rows only, so a restart and every concurrent caller pick the same id."""+ generation = 0+ with db._read_ctx() as conn:+ while True:+ sid = derive(generation)+ if retired_in(conn, sid):+ if pinned is not None and pinned(conn, sid):+ return generation, sid+ elif spent is None or not spent(conn, sid):+ return generation, sid+ generation += 1def has_mutation_receipt(db, principal_id, session_id, request_id):