hermes_state_mutation_retirement.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. """Durable deletion fences outside prunable transcript rows."""
  2. import json
  3. import time
  4. from hermes_state_runtime import RuntimeStoreError, admission_fingerprint, mutation_receipt_key, _json, _text
  5. # This store's own retired session (its profile is the store's). Written as ``own`` since the
  6. # shared routing index also carries other profiles' fences; a marker without ``own`` predates
  7. # that and still fences every route in the index.
  8. RETIRED_PREFIX = 'gateway.retired_session.v1.'
  9. # A multiplexed secondary's retired session, in the routing index every served profile shares
  10. # (the launch home's state.db): ``<prefix><namespace>:<id>``, keyed by the ``agent:<namespace>:``
  11. # slot of that profile's routing keys. Only routing saves read it, so the launch profile's own
  12. # creates, binds and generation walks never see another profile's retirement.
  13. ROUTE_FENCE_PREFIX = 'gateway.retired_route.v1.'
  14. # A last viewer's close stored until the owner's idle point (gateway/session_viewer_close).
  15. VIEWER_CLOSE_PREFIX = 'gateway.viewer_close.v1:'
  16. def retired_in(conn, session_id):
  17. return conn.execute('SELECT 1 FROM state_meta WHERE key=?', (RETIRED_PREFIX + session_id,)).fetchone() is not None
  18. def retired_session(db, session_id):
  19. with db._read_ctx() as conn:
  20. return retired_in(conn, session_id)
  21. def create_conflict(conn, session_id):
  22. """``(code, message)`` refusing a create of the explicit *session_id*, else None: a live row, or a
  23. deleted (retired) id. A row recreated beside the fence would list fine and refuse every turn."""
  24. if conn.execute('SELECT 1 FROM sessions WHERE id=?', (session_id,)).fetchone():
  25. return 'session_exists', 'Session already exists'
  26. if retired_in(conn, session_id):
  27. return 'session_deleted', 'Session was deleted; its id cannot be reused'
  28. return None
  29. def first_generation(db, derive, *, pinned=None, spent=None):
  30. """``(n, derive(n))``: the first generation of a derived session id a NEW request may use. A
  31. user-deleted id stays retired for good, so a derived id (header-less chat, Bot Chat, hosted-room
  32. member, A2A context) whose session was deleted moves to its next generation, never recreated.
  33. ``pinned(conn, sid)`` keeps an exact retry on the retired generation holding its identity;
  34. ``spent(conn, sid)`` skips a live one used up another way (a renamed Bot Chat). Read from
  35. durable rows only, so a restart and every concurrent caller pick the same id."""
  36. generation = 0
  37. with db._read_ctx() as conn:
  38. while True:
  39. sid = derive(generation)
  40. if retired_in(conn, sid):
  41. if pinned is not None and pinned(conn, sid):
  42. return generation, sid
  43. elif spent is None or not spent(conn, sid):
  44. return generation, sid
  45. generation += 1
  46. def keep_passed_fences(conn, derive, session_id):
  47. """Make permanent the fences a walk (``derive`` as for :func:`first_generation`) passed on its
  48. way to *session_id*, inside the write that binds *session_id*: that successor now holds the
  49. conversation, so a sweep marker aging out would send a later walk back to the passed
  50. generation. False when a passed generation has lost its marker since the walk (a sweep's was
  51. reclaimed): a walk now stops there, so *session_id* must not be bound as its successor."""
  52. generation = 0
  53. while (sid := derive(generation)) != session_id:
  54. if not conn.execute('SELECT 1 FROM state_meta WHERE key=?', (RETIRED_PREFIX + sid,)).fetchone():
  55. return False
  56. conn.execute("UPDATE state_meta SET value=json_remove(value, '$.expires') WHERE key=? AND json_valid(value)",
  57. (RETIRED_PREFIX + sid,))
  58. generation += 1
  59. return True
  60. def has_mutation_receipt(db, principal_id, session_id, request_id):
  61. for value in (principal_id, session_id, request_id):
  62. _text(value)
  63. key, _digest = mutation_receipt_key(principal_id, session_id, request_id, operation='model', payload={},
  64. expected_revision=0)
  65. with db._read_ctx() as conn:
  66. return conn.execute('SELECT 1 FROM state_meta WHERE key=?', (key,)).fetchone() is not None
  67. _TRANSCRIPT_FIELDS = frozenset({'messages', 'last_reasoning', 'tools', '_turn_user_row'})
  68. def _compact_result(conn, admission_id):
  69. """A retired result keeps the exact-retry outcome (final response, flags, usage/accounting)
  70. but not the transcript copies a turn result carries (cumulative ``messages``, reasoning, the
  71. settled ``_turn_user_row``)."""
  72. from hermes_state_terminal import RESULT_PREFIX, encode_result, load_result
  73. saved = conn.execute('SELECT value FROM state_meta WHERE key=?', (RESULT_PREFIX + admission_id,)).fetchone()
  74. if saved is None:
  75. return
  76. value = load_result(saved[0])
  77. result = value.get('result')
  78. if isinstance(result, dict):
  79. value['result'] = {**{k: v for k, v in result.items() if k not in _TRANSCRIPT_FIELDS}, 'messages': []}
  80. conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json(encode_result(value)), RESULT_PREFIX + admission_id))
  81. def retire_terminal_receipts(conn, session_ids):
  82. from hermes_state_terminal import ADMISSION_PREFIX, WORKER_PREFIX, identity_key
  83. for sid in session_ids:
  84. admissions = conn.execute('SELECT * FROM session_admissions WHERE target_session_id=?', (sid,)).fetchall()
  85. workers = conn.execute('SELECT * FROM worker_executions WHERE session_id=?', (sid,)).fetchall()
  86. states = {r['status'] for r in [*admissions, *workers]} - {'terminal'}
  87. if states:
  88. raise RuntimeStoreError('unknown_execution' if 'unknown' in states else 'session_busy')
  89. for raw in admissions:
  90. row = dict(raw)
  91. from hermes_state_media import retire_media
  92. retire_media(conn, json.loads(row['payload_json']), row['request_id'])
  93. # Keep the digest for exact retries, not another copy of user input/history.
  94. row['payload_json'] = '{}'
  95. row['lineage_json'] = '[]'
  96. row['retired_at'] = time.time()
  97. _compact_result(conn, row['admission_id'])
  98. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)',
  99. (ADMISSION_PREFIX + row['admission_id'], _json(row)))
  100. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)',
  101. (identity_key(row['principal_id'], sid, row['request_id']), json.dumps(row['admission_id'])))
  102. for raw in workers:
  103. row = dict(raw)
  104. receipts = [dict(r) for r in conn.execute(
  105. 'SELECT sequence,payload_digest,result_json FROM worker_receipts WHERE execution_id=? ORDER BY sequence',
  106. (row['execution_id'],))]
  107. # A terminal worker can only replay an explicit ``execution.finish`` receipt. Every
  108. # other result (history/context reads) is user data that must not outlive the delete,
  109. # even when it is the last one: settlement terminalizes a failed worker without a
  110. # finish receipt. Digests stay so a late duplicate is still recognised as a conflict.
  111. closing = admission_fingerprint(canonical_target=sid,
  112. payload={'operation': 'execution.finish', 'payload': {}})
  113. for receipt in receipts:
  114. if receipt['payload_digest'] != closing:
  115. receipt['result_json'] = None
  116. row['receipts'] = receipts
  117. row['retired_at'] = time.time()
  118. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?)',
  119. (WORKER_PREFIX + row['execution_id'], _json(row)))
  120. conn.execute('DELETE FROM worker_receipts WHERE execution_id=?', (row['execution_id'],))
  121. conn.execute('DELETE FROM worker_executions WHERE session_id=?', (sid,))
  122. conn.execute('DELETE FROM session_admissions WHERE target_session_id=?', (sid,))
  123. _LIVE_LEDGER_SQL = """SELECT 1 FROM session_admissions WHERE target_session_id=? AND status IN ('queued','started','unknown')
  124. UNION ALL SELECT 1 FROM worker_executions WHERE session_id=? AND status IN ('registered','running','unknown') LIMIT 1"""
  125. def retire_sessions(conn, session_ids, *, expiring=()):
  126. """The complete deletion fence for ids that are being deleted in this transaction.
  127. Terminal receipts alone are not enough: without the ``RETIRED_PREFIX`` marker an exact
  128. retry of a settled request reports ``not_found`` instead of its terminal receipt, a late
  129. accounting/constructor backfill recreates the deleted row, and the same request is then
  130. admitted a second time against it. Every delete path (canonical mutate, legacy
  131. ``delete_session*``, prunes and sweeps) must publish the whole fence, so it lives here once.
  132. The marker of an id in *expiring* (a housekeeping sweep's) ages out at the retry horizon."""
  133. from hermes_state_media import retire_spillover
  134. retire_spillover(conn, session_ids) # before the caller's DELETEs take the pointers away
  135. retire_terminal_receipts(conn, session_ids)
  136. retire_mutation_receipts(conn, session_ids)
  137. retire_routes(conn, session_ids, expiring=expiring)
  138. from hermes_state_local import retire_local_receipts
  139. retire_local_receipts(conn, session_ids)
  140. # Mutation-receipt fields that copy user content: the rewound message, a compaction summary and
  141. # a user-set title (rename/sidebar).
  142. _MUTATION_TRANSCRIPT_FIELDS = ('target_message', 'summary', 'title')
  143. # Every session a receipt result can name. A local owner's receipt is keyed by its logical id, but
  144. # its rewind/compress content comes from the PHYSICAL target (a reset child): deleting only that
  145. # child must strip it too, or the surviving owner's exact retry replays the deleted text.
  146. _RECEIPT_SESSION_PATHS = ('$.result.session_id', '$.result.target_message.session_id',
  147. '$.result.target_session_id', '$.result.previous_target_session_id')
  148. def retire_mutation_receipts(conn, session_ids):
  149. """Keep each exact-retry mutation receipt (digest, ids, revision) but drop the user content
  150. a rewind/compress/rename result carries, so a retry after the delete cannot read it back."""
  151. if not session_ids:
  152. return
  153. named = ' OR '.join(f"json_extract(value,'{path}') IN (SELECT value FROM json_each(?1))"
  154. for path in _RECEIPT_SESSION_PATHS)
  155. rows = conn.execute(
  156. "SELECT key,value FROM state_meta WHERE key GLOB 'gateway.mutation.v1.*' AND "
  157. f"CASE WHEN json_valid(value) THEN ({named}) END", (json.dumps(list(session_ids)),)).fetchall()
  158. for key, raw in rows:
  159. receipt = json.loads(raw)
  160. result = receipt.get('result')
  161. if isinstance(result, dict) and any(f in result for f in _MUTATION_TRANSCRIPT_FIELDS):
  162. receipt['result'] = {k: (None if k in _MUTATION_TRANSCRIPT_FIELDS else v) for k, v in result.items()}
  163. conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json(receipt), key))
  164. # Creation ids of the local conversations a gateway door derives and walks by generation
  165. # (``first_generation``): Bot Chat and A2A contexts.
  166. _DERIVED_LOCAL_PREFIXES = ('bot-chat:', 'a2a:')
  167. # The API sessions whose ids a door derives: a hosted-room member (``bot_room``), and the
  168. # ``api-<16 hex>`` ids of a header-less chat (``_derive_chat_session_id``) and of a deleted chat's or
  169. # Responses/Runs chain's next generation (``implicit_api_session``). A caller-named ``api_server`` id
  170. # (``POST /api/sessions``, ``X-Hermes-Session-Id``) is never derived. Matched on the id, not on a
  171. # binding-receipt field, so it also holds for rows an older release bound. Any id can root a
  172. # Responses/Runs chain: its fence becomes permanent once a chain moves past it (``keep_passed_fences``).
  173. _DERIVED_API_SQL = ("(source='bot_room' OR (source='api_server' AND id GLOB 'api-" + '[0-9a-f]' * 16 + "'))")
  174. def _local_conversations(conn):
  175. """``(members, derived)``: member id -> every id of its local conversation (the creation id
  176. (policy, FIFO, generation), each receipt lineage segment and the current target), and the ids
  177. of every derived (Bot Chat, A2A) conversation. One prefix scan, not a lookup per swept id.
  178. Malformed receipts name nothing."""
  179. from hermes_state_local import POLICY_PREFIX
  180. members, derived = {}, set()
  181. for (raw,) in conn.execute('SELECT value FROM state_meta WHERE key GLOB ?', (POLICY_PREFIX + '*',)):
  182. try:
  183. receipt = json.loads(raw)
  184. except (TypeError, ValueError):
  185. continue
  186. if not isinstance(receipt, dict):
  187. continue
  188. entry, lineage = receipt.get('entry'), receipt.get('lineage')
  189. ids = tuple(x for x in [receipt.get('session_id'), *(lineage if isinstance(lineage, list) else []),
  190. entry.get('session_id') if isinstance(entry, dict) else None] if isinstance(x, str))
  191. members.update(dict.fromkeys(ids, ids))
  192. if str(receipt.get('request_id') or '').startswith(_DERIVED_LOCAL_PREFIXES):
  193. derived.update(ids)
  194. return members, derived
  195. def _derived_api_sessions(conn, session_ids):
  196. """The ids among *session_ids* of derived API sessions (``_DERIVED_API_SQL``)."""
  197. return {row[0] for row in conn.execute(
  198. f"SELECT id FROM sessions WHERE {_DERIVED_API_SQL} AND id IN (SELECT value FROM json_each(?))",
  199. (json.dumps(list(session_ids)),))}
  200. def retire_prunable(conn, session_ids):
  201. """Sweep variant of :func:`retire_sessions`: fence the idle sessions and return only those ids.
  202. A session with live or unknown work is skipped, so one busy row cannot abort a whole
  203. prune/empty-session sweep (explicit deletes still refuse with ``session_busy``). A local
  204. reset/compression conversation is swept only whole: while any of its ids is kept or has live
  205. work (the owner's queued admission continues on the current segment), every one is skipped.
  206. A sweep's markers age out at the retry horizon: housekeeping retires thousands of ids a pass
  207. (cron), and a permanent marker each would grow ``state_meta`` without bound. A derived id's
  208. stays (Bot Chat, A2A context, header-less chat, a chain's next generation, hosted-room
  209. member): its conversation resumes on the next generation, and the generation walk stops at the
  210. first unfenced id, so an aged-out marker would send the next turn back to the swept generation.
  211. A Bot Chat's mailbox receipts also outlive the horizon. A caller-named API id is not derived:
  212. its marker ages out, so the caller can use that id again, as for any other swept session."""
  213. swept, (conversations, derived) = set(session_ids), _local_conversations(conn)
  214. def removable(sid):
  215. whole = conversations.get(sid, (sid,))
  216. return swept.issuperset(whole) and not any(conn.execute(_LIVE_LEDGER_SQL, (x, x)).fetchone() for x in whole)
  217. quiet = [sid for sid in session_ids if removable(sid)]
  218. derived |= _derived_api_sessions(conn, quiet)
  219. retire_sessions(conn, quiet, expiring=[sid for sid in quiet if sid not in derived])
  220. return quiet
  221. def delete_in_transaction(db, conn, session_id, payload):
  222. from hermes_state_mutation_guards import require_idle, delete_targets
  223. targets = delete_targets(conn, session_id)
  224. require_idle(db, conn, targets)
  225. retire_sessions(conn, targets)
  226. for sid in targets:
  227. db._bump_conversation_generation(conn, sid, 'session_reset')
  228. conn.execute('UPDATE sessions SET parent_session_id=NULL, runtime_revision=runtime_revision+1 WHERE parent_session_id=?', (sid,))
  229. conn.execute('DELETE FROM messages WHERE session_id=?', (sid,))
  230. conn.executemany('DELETE FROM sessions WHERE id=?', [(sid,) for sid in targets])
  231. db._delete_unreferenced_system_prompts(conn)
  232. return set(), {'deleted_ids': targets}
  233. def entry_session_id(raw):
  234. """The ``session_id`` a stored routing/receipt JSON object names, else None. Unrelated rows
  235. are not validated here: a malformed one (an array, invalid JSON) names no target, so it can
  236. neither be retired nor abort the delete of a different session; the routing loader skips it too."""
  237. try:
  238. value = json.loads(raw)
  239. except (TypeError, ValueError):
  240. return None
  241. return value.get('session_id') if isinstance(value, dict) else None
  242. def _mark_retired(conn, key, *, expires, **fields):
  243. # Replace: the marker is a fact, not a receipt; re-retiring an id that was recreated
  244. # beside its tombstone must not abort the delete that removes it again (and restarts its horizon).
  245. # An ``expires`` (sweep) marker ages out at the horizon and never shortens a permanent one.
  246. # A permanent marker without ``own`` (an earlier release's, maybe another profile's shared
  247. # fence) stays unscoped: it is the only fence of that profile's route. An earlier release's
  248. # sweep marker came only from this store's own sweep, so it is replaced as written.
  249. value = {'retired_at': time.time(), **fields}
  250. if expires:
  251. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE '
  252. "SET value=excluded.value WHERE json_valid(state_meta.value) "
  253. "AND json_extract(state_meta.value, '$.expires')",
  254. (key, json.dumps({**value, 'expires': True})))
  255. else:
  256. conn.execute('INSERT INTO state_meta(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=CASE '
  257. "WHEN NOT json_valid(state_meta.value) THEN json_remove(excluded.value, '$.own') "
  258. "WHEN json_extract(state_meta.value, '$.own') IS NOT NULL "
  259. "OR json_extract(state_meta.value, '$.expires') THEN excluded.value "
  260. "ELSE json_remove(excluded.value, '$.own') END", (key, json.dumps(value)))
  261. def retire_routes(conn, session_ids, *, expiring=()):
  262. expiring = set(expiring)
  263. for sid in session_ids:
  264. _mark_retired(conn, RETIRED_PREFIX + sid, expires=sid in expiring, own=True)
  265. conn.execute('DELETE FROM state_meta WHERE key=?', ('gateway.api.settings.v1.' + sid,))
  266. conn.execute('DELETE FROM state_meta WHERE key=?', (VIEWER_CLOSE_PREFIX + sid,))
  267. targets = set(session_ids)
  268. for row in conn.execute('SELECT scope,session_key,entry_json FROM gateway_routing').fetchall():
  269. if entry_session_id(row['entry_json']) in targets:
  270. conn.execute('DELETE FROM gateway_routing WHERE scope=? AND session_key=?',
  271. (row['scope'], row['session_key']))
  272. def route_namespace(session_key):
  273. """The ``<ns>`` slot of an ``agent:<ns>:...`` routing key (``main`` for the launch profile, else
  274. the profile's ``_session_key_namespace`` slot); ``main`` for a key outside that shape."""
  275. parts = str(session_key or '').split(':')
  276. return parts[1] if len(parts) > 2 and parts[0] == 'agent' and parts[1] else 'main'
  277. def route_fence_key(namespace, session_id):
  278. return f'{ROUTE_FENCE_PREFIX}{namespace}:{session_id}'
  279. # True when a routing save must skip an entry, checked only against the profile the route belongs
  280. # to: this store's own marker for a launch (``main``) route, that profile's fence for a secondary's.
  281. # A launch-keyed route to an id this store holds no session of can only name another profile's
  282. # session, so a secondary's fence covers it too (``fence_shared_routes``). A sweep's fence stops
  283. # fencing at the horizon by itself: only the owner's own store sweeps the owner's marker, and nothing
  284. # guarantees the launch profile ever sweeps this one. A delete's fence (no ``expires``) never does:
  285. # its age test is NULL, which ``coalesce`` reads as not expired. An unscoped marker (no ``own``: an
  286. # earlier release's, written for either) still fences every route. So does a marker or fence that is
  287. # not valid JSON (no writer stores one): ``json_extract`` raises ``malformed JSON`` on it even behind
  288. # ``json_valid(...) AND``, so each JSON read sits in a ``CASE WHEN json_valid(...)`` branch.
  289. # Bound by name (``route_retired_params``): :entry is the route's JSON, :ns its key's
  290. # ``route_namespace``, :now the save's time.
  291. ROUTE_RETIRED_SQL = """(
  292. EXISTS (SELECT 1 FROM state_meta m
  293. WHERE m.key = :retired_prefix || json_extract(:entry, '$.session_id')
  294. AND (:ns = 'main' OR NOT CASE WHEN json_valid(m.value)
  295. THEN json_extract(m.value, '$.own') IS NOT NULL ELSE 0 END))
  296. OR EXISTS (SELECT 1 FROM state_meta f
  297. WHERE f.key = :fence_prefix || :ns || ':' || json_extract(:entry, '$.session_id')
  298. AND NOT CASE WHEN json_valid(f.value)
  299. THEN coalesce(json_extract(f.value, '$.expires')
  300. AND json_extract(f.value, '$.retired_at') < :now - :horizon, 0)
  301. ELSE 0 END
  302. AND (:ns != 'main' OR NOT EXISTS (
  303. SELECT 1 FROM sessions s WHERE s.id = json_extract(:entry, '$.session_id')))))"""
  304. def route_retired_params(session_key, entry_json, now):
  305. """The named parameters of ``ROUTE_RETIRED_SQL`` for one routing entry saved at *now*."""
  306. return {'entry': entry_json, 'ns': route_namespace(session_key), 'now': now, 'horizon': RETRY_HORIZON_S,
  307. 'retired_prefix': RETIRED_PREFIX, 'fence_prefix': ROUTE_FENCE_PREFIX}
  308. def route_retired(db, session_key, entry_json):
  309. """A routing save (or the legacy sessions.json import) must skip this entry."""
  310. with db._read_ctx() as conn:
  311. return bool(conn.execute('SELECT ' + ROUTE_RETIRED_SQL,
  312. route_retired_params(session_key, entry_json, time.time())).fetchone()[0])
  313. def expiring_retirements(db, session_ids):
  314. """The ids among *session_ids* whose own marker in *db* is a sweep's (ages out at the horizon)."""
  315. with db._read_ctx() as conn:
  316. return {key[len(RETIRED_PREFIX):] for (key,) in conn.execute(
  317. "SELECT key FROM state_meta WHERE key IN (SELECT ? || value FROM json_each(?)) "
  318. "AND json_valid(value) AND json_extract(value, '$.expires')",
  319. (RETIRED_PREFIX, json.dumps(list(session_ids))))}
  320. def fence_shared_routes(conn, namespace, session_ids, *, expiring=()):
  321. """In the shared routing index (*conn*), fence the routes of *session_ids*, retired in the
  322. profile whose routing keys use *namespace*, and drop them. The fence is keyed by (profile, id)
  323. and copies the owner's marker: permanent for a delete, aging out with an *expiring* sweep's.
  324. Unless this store holds a session of that id, the launch (``main``) namespace is fenced too: a
  325. launch-keyed route to it names the retired session. The sweep fences that aged out here are
  326. reclaimed in the same write, so the owner's prunes bound them without the launch profile's sweep."""
  327. _reclaim_expired(conn, ROUTE_FENCE_PREFIX, time.time() - RETRY_HORIZON_S)
  328. expiring, owned = set(expiring), {}
  329. for sid in session_ids:
  330. names = {namespace}
  331. if not conn.execute('SELECT 1 FROM sessions WHERE id=?', (sid,)).fetchone():
  332. names.add('main')
  333. for name in names:
  334. _mark_retired(conn, route_fence_key(name, sid), expires=sid in expiring)
  335. owned[sid] = names
  336. for row in conn.execute('SELECT scope,session_key,entry_json FROM gateway_routing').fetchall():
  337. if route_namespace(row['session_key']) in owned.get(entry_session_id(row['entry_json']), ()):
  338. conn.execute('DELETE FROM gateway_routing WHERE scope=? AND session_key=?',
  339. (row['scope'], row['session_key']))
  340. RETRY_HORIZON_S = 7 * 86400 # exact-retry horizon: a retry within it gets the byte-exact settled reply
  341. _EXPIRED = "json_valid(value) AND json_extract(value, '$.retired_at') < ?"
  342. def _reclaim_expired(conn, prefix, cutoff):
  343. """Drop the sweep (``expires``) markers under *prefix* retired before *cutoff*."""
  344. conn.execute(f"DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED} AND json_extract(value, '$.expires')",
  345. (prefix + '*', cutoff))
  346. def prune_retired_tombstones(conn, older_than=RETRY_HORIZON_S):
  347. """Reclaim deletion tombstones retired more than *older_than* seconds ago: the terminal admission
  348. with its identity pointer and compacted result, terminal workers and a sweep's (``expires``)
  349. retired-session marker or shared routing fence. A user delete's marker is the deletion fence itself and is never
  350. reclaimed: it holds only ``retired_at``, and without it a deleted id could be recreated,
  351. re-admitted or reissued as a Bot Chat generation.
  352. Tombstones written before ``retired_at`` existed are stamped now and age from this pass."""
  353. from hermes_state_terminal import ADMISSION_PREFIX, RESULT_PREFIX, WORKER_PREFIX, identity_key
  354. prefixes = (ADMISSION_PREFIX, WORKER_PREFIX)
  355. now = time.time()
  356. for prefix in prefixes:
  357. conn.execute("UPDATE state_meta SET value=json_set(value, '$.retired_at', ?) WHERE key GLOB ? "
  358. "AND json_valid(value) AND json_extract(value, '$.retired_at') IS NULL", (now, prefix + '*'))
  359. cutoff = now - older_than
  360. for key, raw in conn.execute(f'SELECT key,value FROM state_meta WHERE key GLOB ? AND {_EXPIRED}',
  361. (ADMISSION_PREFIX + '*', cutoff)).fetchall():
  362. row = json.loads(raw)
  363. conn.execute('DELETE FROM state_meta WHERE key IN (?,?,?)', (key, RESULT_PREFIX + row['admission_id'],
  364. identity_key(row['principal_id'], row['target_session_id'], row['request_id'])))
  365. conn.execute(f'DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED}', (WORKER_PREFIX + '*', cutoff))
  366. for prefix in (RETIRED_PREFIX, ROUTE_FENCE_PREFIX):
  367. _reclaim_expired(conn, prefix, cutoff)