hermes_state_compression.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. """Compression lineage, cooldown/streak counters, locks and turn leases for SessionDB.
  2. Mixin bound onto ``SessionDB`` via the MRO, built on its ``_read_ctx`` /
  3. ``_execute_write`` / ``_write_sql`` / ``_read_one`` primitives."""
  4. from __future__ import annotations
  5. import json
  6. import logging
  7. import sqlite3
  8. import time
  9. from typing import Any, Dict, List, Optional, Tuple
  10. from hermes_state_common import (
  11. _BOUNDARY_END_REASONS, _COMPRESSION_LOCK_ROW_SQL as _LOCK_ROW_SQL, _ENDED_ROW_SQL, _ended_by_compression,
  12. _RESET_CHILD_SQL, _non_continuation_child_sql, _sql_json_extract, _sql_session_last_active,
  13. is_automatic_end_reason)
  14. # Log-record parity with the origin module (caplog tests pin "hermes_state").
  15. logger = logging.getLogger("hermes_state")
  16. _COOLDOWN_ROW_SQL = (
  17. "SELECT compression_failure_cooldown_until, compression_failure_error FROM sessions WHERE id = ?"
  18. )
  19. # One forward step of get_compression_chain: the preferred continuation child of ``?``. A reset
  20. # fork is a separate user-visible conversation (_LISTABLE_CHILD_SQL already surfaces it as its
  21. # own row), so following it here would hijack the lineage tip projection onto the reset sibling
  22. # and make the real continuation invisible (#114271).
  23. _CHAIN_STEP_SQL = f"""
  24. SELECT child.id
  25. FROM sessions parent
  26. JOIN sessions child ON child.parent_session_id = parent.id
  27. WHERE parent.id = ?
  28. AND parent.end_reason = 'compression'
  29. AND {_sql_json_extract('child.model_config', '$._branched_from')} IS NULL
  30. AND {_sql_json_extract('child.model_config', '$._delegate_from')} IS NULL
  31. AND NOT ({_RESET_CHILD_SQL.format(a='child')})
  32. AND COALESCE(child.source, '') != 'tool'
  33. ORDER BY
  34. CASE
  35. WHEN child.end_reason = 'compression' THEN 0
  36. WHEN child.ended_at IS NULL THEN 1
  37. ELSE 2
  38. END,
  39. {_sql_session_last_active("child")} DESC,
  40. child.started_at DESC,
  41. child.id DESC
  42. LIMIT 1
  43. """
  44. # Turn-lease rows expired longer than this are swept by the next acquisition of any conversation.
  45. _TURN_LEASE_SWEEP_GRACE_S = 86400.0
  46. def _cooldown_row(exists: bool, cooldown_until, error) -> dict[str, Any]:
  47. return {"session_exists": exists,
  48. "cooldown_until": float(cooldown_until) if cooldown_until is not None else None, "error": error}
  49. def _claim_lease_row(conn, table: str, key_col: str, key: str, holder: str, now: float, expires_at: float,
  50. stale) -> tuple[bool, Optional[str]]:
  51. """Single-transaction lease claim: DELETE a stale holder's row (``stale(holder,
  52. expires_at)``), INSERT OR IGNORE ours, then SELECT to confirm ownership (INSERT OR
  53. IGNORE gives no rowcount signal). Returns ``(acquired, reclaimed_holder)``."""
  54. reclaimed_holder = None
  55. row = conn.execute(f"SELECT holder, expires_at FROM {table} WHERE {key_col} = ?", (key,)).fetchone()
  56. if row is not None and stale(row["holder"], row["expires_at"]):
  57. conn.execute(f"DELETE FROM {table} WHERE {key_col} = ? AND holder = ?", (key, row["holder"]))
  58. reclaimed_holder = row["holder"]
  59. conn.execute(
  60. f"INSERT OR IGNORE INTO {table} ({key_col}, holder, acquired_at, expires_at) VALUES (?, ?, ?, ?)",
  61. (key, holder, now, expires_at))
  62. owner = conn.execute(f"SELECT holder FROM {table} WHERE {key_col} = ?", (key,)).fetchone()
  63. return owner is not None and owner["holder"] == holder, reclaimed_holder
  64. # Defensive bound on the forward compression-chain walk; ``seen`` guards cycles.
  65. # 100 truncated real compression lineages (~180 deep), stranding root→tip walks
  66. # on a stale mid id (#125041). Named so tests can simulate the REAL walk.
  67. _CHAIN_CAP = 1000
  68. # model_config keys that belong to the whole conversation, not one compression segment: a rotation
  69. # carries them onto the child (per-segment keys such as usage anchors stay behind).
  70. # ``prompt_refresh_epoch`` too: without it a rotated child looks older than any past
  71. # ``hermes sessions refresh-prompts`` request and rebuilds its prompt once more after eviction.
  72. _CONVERSATION_MODEL_CONFIG_KEYS = ("onboarding_run", "prompt_refresh_epoch")
  73. def _walk_compression_chain(session_id: str, step) -> list[str]:
  74. """Root-first ids from *session_id* through its tip; ``step(id)`` returns the ``_CHAIN_STEP_SQL`` row."""
  75. current = session_id
  76. chain = [current] if current else []
  77. seen = set(chain)
  78. # Defensive bound; ``seen`` guards cycles. 100 truncated real
  79. # compression lineages (~180 deep), stranding root→tip walks on a
  80. # stale mid id (#125041).
  81. for _ in range(_CHAIN_CAP):
  82. row = step(current)
  83. child_id = row["id"] if row is not None else None
  84. if not child_id or child_id in seen:
  85. return chain
  86. seen.add(child_id)
  87. current = child_id
  88. chain.append(child_id)
  89. return chain
  90. # created_source values that are not creation provenance: unset, the accounting placeholder
  91. # ``unknown`` and the routing label ``local`` (the store-side copy of session_policy._NOT_A_SURFACE).
  92. # The creator upsert, the managed-worker heal and the ancestor stamp may replace them, nothing else.
  93. _UNSET_CREATED_SOURCES = frozenset({None, "", "unknown", "local"})
  94. _UNSET_CREATED_SQL = "COALESCE({col}, '') IN ('', 'unknown', 'local')"
  95. _CREATED_ROW_SQL = "SELECT parent_session_id, created_source FROM sessions WHERE id = ?"
  96. # Is any ancestor unset, walking up parent_session_id through compressed parents on the same
  97. # surface? One query, so a fully stamped chain is skipped without the per-hop validated walk below.
  98. # A parent that did not end by compression (a reset or branch source) is not a chain edge, so an
  99. # unset row behind one does not send every write down that walk.
  100. # CROSS JOIN keeps ``up`` the outer loop: without ANALYZE stats SQLite otherwise scans sessions
  101. # and probes ``up`` for each row, a full-table read on every stamped write.
  102. _UNSET_ANCESTOR_SQL = """
  103. WITH RECURSIVE up(pid, n) AS (
  104. SELECT parent_session_id, 1 FROM sessions WHERE id = ?
  105. UNION ALL
  106. SELECT s.parent_session_id, up.n + 1 FROM up JOIN sessions s ON s.id = up.pid
  107. WHERE s.end_reason = 'compression' AND s.created_source = ? AND up.n < ?
  108. )
  109. SELECT 1 FROM up CROSS JOIN sessions s ON s.id = up.pid
  110. WHERE s.end_reason = 'compression' AND """ + _UNSET_CREATED_SQL.format(col="s.created_source") + """
  111. LIMIT 1
  112. """
  113. def stamp_compression_ancestors(conn, session_id: str) -> None:
  114. """Copy *session_id*'s ``created_source`` onto compression ancestors that have none.
  115. A list page shows a compressed conversation as its root, painted as the tip, and filters on
  116. the root's ``created_source``. Automatic compression publishes the child before any writer
  117. knows the surface, so the turn's usage write stamps only the tip. Stamping the chain here,
  118. in that same transaction, keeps the root matchable without a list-time chain walk. Each hop
  119. must be the parent's preferred continuation (``_CHAIN_STEP_SQL``); ``unknown`` and ``local``
  120. count as unset, as in the creator upsert. An ancestor already on the same surface is walked through
  121. (a NULL root behind a stamped mid segment still gets stamped); another surface stops it.
  122. A chain with no unset ancestor returns after one query (``_UNSET_ANCESTOR_SQL``).
  123. """
  124. row = conn.execute(_CREATED_ROW_SQL, (session_id,)).fetchone()
  125. surface = row["created_source"] if row else None
  126. if surface in _UNSET_CREATED_SOURCES:
  127. return
  128. if conn.execute(_UNSET_ANCESTOR_SQL, (session_id, surface, _CHAIN_CAP)).fetchone() is None:
  129. return
  130. current, parent_id = session_id, row["parent_session_id"]
  131. for _ in range(_CHAIN_CAP):
  132. parent = parent_id and conn.execute(_CREATED_ROW_SQL, (parent_id,)).fetchone()
  133. if not parent or parent["created_source"] not in (*_UNSET_CREATED_SOURCES, surface):
  134. return
  135. step = conn.execute(_CHAIN_STEP_SQL, (parent_id,)).fetchone()
  136. if step is None or step["id"] != current:
  137. return
  138. if parent["created_source"] != surface:
  139. conn.execute("UPDATE sessions SET created_source = ? WHERE id = ?", (surface, parent_id))
  140. current, parent_id = parent_id, parent["parent_session_id"]
  141. # Compression edges whose parent is unset and whose continuation child carries a real surface: what
  142. # a store from before write-time stamping still hides from ``sessions list --source``. A child
  143. # forked from that parent (branch, delegate or reset marker naming it) or a tool child is no
  144. # continuation (as in prune) and never matches. A stamped store, or one whose unset chains have no
  145. # stamped segment, answers with no rows after one scan of sessions. A stamped child the stamp walk
  146. # does not follow (a non-preferred sibling, or one with a fork marker inherited from an earlier row)
  147. # still matches; the walk stops at its first hop.
  148. _UNSTAMPED_EDGE_CHILD_SQL = f"""
  149. SELECT child.id FROM sessions parent JOIN sessions child ON child.parent_session_id = parent.id
  150. WHERE parent.end_reason = 'compression' AND {_UNSET_CREATED_SQL.format(col="parent.created_source")}
  151. AND NOT {_UNSET_CREATED_SQL.format(col="child.created_source")}
  152. {_non_continuation_child_sql("child.", "parent.id")}"""
  153. class SessionCompressionMixin:
  154. """Compression lineage, cooldown/streak counters, locks and turn leases."""
  155. def reopen_if_explicitly_closed(
  156. self, session_id: str, *, provenance: str, patience_s: Optional[float] = None,
  157. ) -> Optional[str]:
  158. """Clear an explicit-close stamp (``tui_close``, ``cli_close``, ``webhook_complete``, ...) from a
  159. session a HOST has just proven is still routed to it, returning the reason cleared or None (#106459).
  160. Narrow twin of ``reopen_session()``: automatic stamps are left to publish (#88197); ``compression``,
  161. boundary (reset reasons, CLI ``new_session``) and stamps with a published continuation own lineage
  162. elsewhere and are never touched. The UPDATE is conditional on the exact stamp read, so a close
  163. landing between read and write survives.
  164. Only the routing host can make this call. Publication cannot: ``end_session()`` is first-stamp-wins,
  165. so a close made during a turn that began on a stale stamp is a no-op write. Turn-lease admission
  166. cannot: the TUI starts its worker before it reaches ``run_conversation()``, so ``session.close`` can
  167. stamp ``tui_close`` in between and a late lease would clear a deliberate close. Call it under the
  168. lock that makes the host's registry claim atomic with its teardown (#54878 on the routing table)."""
  169. if not session_id:
  170. return None
  171. def _do(conn):
  172. row = conn.execute(_ENDED_ROW_SQL, (session_id,)).fetchone()
  173. if row is None or row["ended_at"] is None:
  174. return None
  175. reason = row["end_reason"]
  176. if is_automatic_end_reason(reason) or reason == "compression" or reason in _BOUNDARY_END_REASONS:
  177. return None
  178. superseded = conn.execute(
  179. "SELECT 1 FROM sessions WHERE parent_session_id = ?"
  180. + self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="") + " LIMIT 1",
  181. (session_id,) * 4).fetchone()
  182. if superseded is not None:
  183. return None
  184. conn.execute(
  185. "UPDATE sessions SET ended_at = NULL, end_reason = NULL "
  186. "WHERE id = ? AND ended_at = ? AND end_reason = ?",
  187. (session_id, row["ended_at"], reason))
  188. return str(reason)
  189. reason = self._execute_write(_do, patience_s=patience_s)
  190. if reason is not None:
  191. logger.warning(
  192. "Session %s carried a stale %r end stamp while %s; cleared so the conversation can "
  193. "compress and a later close is recorded (#106459)", session_id, reason, provenance)
  194. return reason
  195. def stamp_legacy_compression_chains(self) -> int:
  196. """Stamp the unset ancestors of chains compressed before write-time stamping from their
  197. stamped segments, through ``stamp_compression_ancestors`` (same edge checks; a known value
  198. is never replaced). One write transaction and idempotent, so the owner runs it on every
  199. start: a store with nothing to repair pays one scan of sessions under the write lock, plus
  200. a one-hop walk per stamped child of an unset compression parent that the walk does not
  201. follow (a non-preferred sibling). Returns the rows stamped."""
  202. def _do(conn):
  203. before = conn.total_changes
  204. for row in conn.execute(_UNSTAMPED_EDGE_CHILD_SQL).fetchall():
  205. stamp_compression_ancestors(conn, row["id"])
  206. return conn.total_changes - before
  207. return self._execute_write(_do)
  208. def find_live_compression_child(self, parent_session_id: str) -> Optional[dict[str, Any]]:
  209. """The unique live direct child of a compression-ended session, else None. A stale
  210. agent whose parent was rotated elsewhere may recover only when the lineage names
  211. exactly one live continuation; more than one fails closed."""
  212. if not parent_session_id:
  213. return None
  214. with self._read_ctx() as conn:
  215. if not _ended_by_compression(conn.execute(_ENDED_ROW_SQL, (parent_session_id,)).fetchone()):
  216. return None
  217. rows = conn.execute(
  218. """
  219. SELECT s.*,
  220. COALESCE(sp.prompt, s.system_prompt)
  221. AS _system_prompt_resolved
  222. FROM sessions s
  223. LEFT JOIN system_prompts sp ON sp.hash = s.system_prompt_hash
  224. WHERE s.parent_session_id = ?
  225. AND s.ended_at IS NULL
  226. """
  227. + self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="s.")
  228. + """
  229. ORDER BY s.started_at ASC
  230. LIMIT 2
  231. """,
  232. (parent_session_id,) * 4,
  233. ).fetchall()
  234. return self._session_row_dict(rows[0]) if len(rows) == 1 else None
  235. def reopen_orphaned_compression_session(self, session_id: str) -> bool:
  236. """Reopen a compression parent only when no continuation was published (older
  237. builds could leave a closed parent after an interrupted handoff). Conservative:
  238. an active lease or any canonical child means another path owns the lineage."""
  239. if not session_id:
  240. return False
  241. def _do(conn):
  242. if not _ended_by_compression(conn.execute(_ENDED_ROW_SQL, (session_id,)).fetchone()):
  243. return False
  244. # Any non-branch/non-delegate/non-reset/non-tool child is a continuation, ended or not.
  245. child = conn.execute(
  246. """
  247. SELECT 1
  248. FROM sessions
  249. WHERE parent_session_id = ?
  250. """
  251. + self._NON_CONTINUATION_CHILD_FILTER_SQL.format(alias="")
  252. + """
  253. LIMIT 1
  254. """,
  255. (session_id,) * 4,
  256. ).fetchone()
  257. if child is not None:
  258. return False
  259. # refresh_compression_lock() lets an owner revive its own expired row, so reclaim
  260. # it inside this write txn: refresh-first makes the lease active and aborts
  261. # recovery; recovery-first deletes the holder so a refresh can't resurrect it.
  262. now = time.time()
  263. lock_row = conn.execute(_LOCK_ROW_SQL, (session_id,)).fetchone()
  264. if lock_row is not None:
  265. expires_at = lock_row["expires_at"]
  266. if expires_at is None or float(expires_at) >= now:
  267. return False
  268. deleted = conn.execute(
  269. "DELETE FROM compression_locks WHERE session_id = ? AND holder = ? AND expires_at = ?",
  270. (session_id, lock_row["holder"], expires_at))
  271. if deleted.rowcount != 1:
  272. return False
  273. updated = conn.execute(
  274. # A parent stamped ended by AUTOMATIC cleanup (tui_shutdown, ws_disconnect, orphan reap,
  275. # idle/LRU evict) while a live agent is publishing its rotation is stale by construction —
  276. # this writer holds the compression lease and is actively continuing the conversation the
  277. # stamp claims is over. Left in place it wedges rotation forever: every attempt aborts here,
  278. # nothing clears the stamp, and each attempt's pre-publish flush re-grows the parent until
  279. # the provider rejects the request (#88197: 303 unique messages → 2,611 rows → HTTP 400).
  280. # Clear it in this same transaction and proceed; the closure UPDATE below re-stamps the
  281. # parent with its true boundary (end_reason='compression'). Deliberate boundaries
  282. # (compression, session_reset, explicit close) still fail closed — those mean another path
  283. # owns lineage.
  284. "UPDATE sessions SET ended_at = NULL, end_reason = NULL "
  285. "WHERE id = ? AND ended_at IS NOT NULL AND end_reason = 'compression'",
  286. (session_id,))
  287. # rowcount==1 is guaranteed by the parent SELECT in this same txn. A False return added past
  288. # this point must raise instead: the lease DELETE above commits unless _do raises.
  289. return updated.rowcount == 1
  290. return bool(self._execute_write(_do))
  291. def _publish_child_session_row(self, conn, parent, *, parent_session_id, child_session_id, source,
  292. model, model_config, system_prompt, cwd, profile_name) -> None:
  293. """INSERT the compression child's ``sessions`` row copied from *parent*. Same contract as
  294. _insert_session_row's compression-fork backfill: the child stays on the parent's profile and keeps
  295. gateway routing/origin columns; no owner on either side -> this store's profile."""
  296. from hermes_state_sessions import _parse_model_config
  297. # Pin/archive and the conversation keys are compression-lineage state; canonical branch/reset
  298. # verbs also publish through here, and their child is its own conversation.
  299. continuation = not any(k in (model_config or {}) for k in ("_branched_from", "_reset_from"))
  300. carried = {key: value for key, value in _parse_model_config(parent["model_config"]).items()
  301. if continuation and key in _CONVERSATION_MODEL_CONFIG_KEYS}
  302. model_config = {**carried, **(model_config or {})}
  303. system_prompt_hash = self._store_system_prompt(conn, system_prompt)
  304. # Copy a real creating surface. Automatic compression passes the parent's live
  305. # label (``local`` after the peer refresh); reset, branch and the compress
  306. # mutation pass the policy label. ``local`` and ``unknown`` are not surfaces,
  307. # so those stay NULL. Compression sets ``_session_db_created`` and does not
  308. # create the child again. The turn's ``update_token_counts(source=...)`` fills
  309. # that NULL; auxiliary usage must not write ``unknown`` into it first.
  310. from gateway.session_policy import created_surface
  311. created_source = created_surface(parent["created_source"]) or created_surface(source)
  312. # The child continues the parent's tools[] pin (the compaction refresh re-pinned it just
  313. # before publish), or its first hop to another surface re-derives the array.
  314. conn.execute(
  315. """INSERT INTO sessions (
  316. id, source, created_source, model, model_config, system_prompt,
  317. system_prompt_hash, tool_names,
  318. parent_session_id, cwd, git_branch, git_repo_root,
  319. profile_name, user_id, session_key, chat_id, chat_type,
  320. thread_id, display_name, origin_json, pinned, started_at,
  321. archived, auto_archived
  322. ) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
  323. (
  324. child_session_id, source, created_source, model, json.dumps(model_config) if model_config else None,
  325. system_prompt_hash, parent["tool_names"], parent_session_id, cwd or parent["cwd"], parent["git_branch"],
  326. parent["git_repo_root"],
  327. profile_name or parent["profile_name"] or self._own_profile_name(),
  328. parent["user_id"], parent["session_key"], parent["chat_id"], parent["chat_type"],
  329. parent["thread_id"], parent["display_name"], parent["origin_json"],
  330. # The pin is lineage-wide (set_session_pinned); a segment published after it joins it.
  331. int(parent["pinned"] or 0) if continuation else 0, time.time(),
  332. # Inherit the lineage's archive state so a manually archived chat stays uniformly
  333. # archived (a mixed lineage let the sweep re-stamp its fresh tip as auto-archived).
  334. (parent["archived"] or 0) if continuation else 0,
  335. (parent["auto_archived"] or 0) if continuation else 0),
  336. )
  337. def publish_compression_child(
  338. self, *, parent_session_id: str, child_session_id: str, source: str,
  339. messages: list[dict[str, Any]], model: str | None = None, model_config: dict[str, Any] | None = None,
  340. system_prompt: str | None = None, cwd: str | None = None, profile_name: str | None = None,
  341. compression_lock_holder: str | None = None, require_compression_lease: bool = True,
  342. require_lease_refresh: bool = False, lease_ttl_seconds: float = 300.0,
  343. watermark: Optional[int] = None, watermark_ceiling: Optional[int] = None) -> None:
  344. """Atomically close a parent and publish its durable compression child: closure, child row, and
  345. handoff commit in one transaction, so readers see the live parent or a complete child, never an
  346. ended parent with a missing/empty child. *watermark* (the parent's highest row already represented
  347. in the handoff: its ``get_active_message_watermark`` at compression start, or the newest row of an
  348. adopted durable snapshot): parent rows with ``id > watermark`` — appends landed during the slow
  349. summary — are column-cloned into the child AFTER the handoff. *watermark_ceiling* bounds the clone:
  350. the rotation path flushes its OWN transcript to the parent just before publishing and those rows are
  351. already in the handoff, so only ``(watermark, watermark_ceiling]`` is foreign tail (``None`` =
  352. unbounded). *require_lease_refresh* +
  353. *compression_lock_holder* refreshes the lease on the same ``conn`` before the expiry check (no
  354. TOCTOU window), so a refresher that died on transient DB errors gets one last chance.
  355. See #75316.
  356. ``None`` = unbounded (no internal flush happened). See #47202.
  357. """
  358. self._execute_transcript_write(lambda conn: self._publish_compression_child_on_conn(
  359. conn, parent_session_id=parent_session_id, child_session_id=child_session_id, source=source,
  360. messages=messages, model=model, model_config=model_config, system_prompt=system_prompt,
  361. cwd=cwd, profile_name=profile_name, compression_lock_holder=compression_lock_holder,
  362. require_compression_lease=require_compression_lease, require_lease_refresh=require_lease_refresh,
  363. lease_ttl_seconds=lease_ttl_seconds, watermark=watermark, watermark_ceiling=watermark_ceiling), messages)
  364. def _publish_compression_child_on_conn(
  365. self, conn, *, parent_session_id: str, child_session_id: str, source: str,
  366. messages: list[dict[str, Any]], model: str | None = None, model_config: dict[str, Any] | None = None,
  367. system_prompt: str | None = None, cwd: str | None = None, profile_name: str | None = None,
  368. compression_lock_holder: str | None = None, require_compression_lease: bool = True,
  369. require_lease_refresh: bool = False, lease_ttl_seconds: float = 300.0,
  370. watermark: Optional[int] = None, watermark_ceiling: Optional[int] = None) -> None:
  371. """:meth:`publish_compression_child` on the caller's write transaction (never commits). The
  372. owner and the worker/canonical-mutation verbs share this one body so they cannot drift."""
  373. from hermes_state_errors import CompressionSessionBusyError
  374. if require_lease_refresh and compression_lock_holder:
  375. conn.execute(
  376. "UPDATE compression_locks SET expires_at = ? WHERE session_id = ? AND holder = ?",
  377. (time.time() + lease_ttl_seconds, parent_session_id, compression_lock_holder))
  378. lock_row = conn.execute(_LOCK_ROW_SQL, (parent_session_id,)).fetchone()
  379. if require_compression_lease and (
  380. lock_row is None or not compression_lock_holder
  381. or lock_row["holder"] != compression_lock_holder
  382. or float(lock_row["expires_at"]) <= time.time()
  383. ):
  384. raise CompressionSessionBusyError(
  385. f"Compression lease lost before publication: {parent_session_id}")
  386. parent = conn.execute(
  387. """SELECT ended_at, end_reason, cwd, git_branch, git_repo_root,
  388. user_id, session_key, chat_id, chat_type,
  389. thread_id, display_name, origin_json, profile_name, tool_names,
  390. archived, auto_archived, pinned, model_config, created_source
  391. FROM sessions WHERE id = ?""",
  392. (parent_session_id,),
  393. ).fetchone()
  394. if parent is None:
  395. raise RuntimeError(f"Compression parent not found: {parent_session_id}")
  396. if parent["ended_at"] is not None:
  397. # An AUTOMATIC end stamp (tui_shutdown, ws_disconnect, orphan reap, idle/LRU
  398. # evict) is stale by construction — this lease holder is still continuing the
  399. # conversation, and left alone it wedges rotation forever. Clear it; the closure
  400. # UPDATE below re-stamps end_reason='compression'. Deliberate boundaries fail closed.
  401. if not is_automatic_end_reason(parent["end_reason"]):
  402. raise RuntimeError(f"Compression parent already ended: {parent_session_id}")
  403. conn.execute(
  404. "UPDATE sessions SET ended_at = NULL, end_reason = NULL WHERE id = ?",
  405. (parent_session_id,))
  406. if not messages:
  407. raise RuntimeError("Compression child handoff must not be empty")
  408. self._publish_child_session_row(
  409. conn, parent, parent_session_id=parent_session_id, child_session_id=child_session_id,
  410. source=source, model=model, model_config=model_config, system_prompt=system_prompt,
  411. cwd=cwd, profile_name=profile_name)
  412. # Carried handoff tail rows arrive without a timestamp and would otherwise be stamped
  413. # `now`, breaking their _display_dedupe_key identity with the parent's durable originals
  414. # and duplicating them in the lineage display read (#59661).
  415. self._carry_parent_timestamps(conn, parent_session_id, messages)
  416. total_messages, total_tool_calls = self._insert_message_rows(conn, child_session_id, messages)
  417. if watermark is not None:
  418. # Clone the parent's concurrent tail into the child after the handoff;
  419. # originals stay in the closed parent for lineage recovery.
  420. bounded = watermark_ceiling is not None
  421. tail_ids, tail_tool_calls = self._tail_rows_after_watermark(
  422. conn, "SELECT id, tool_calls FROM messages "
  423. "WHERE session_id = ? AND active = 1 AND id > ?"
  424. f"{' AND id <= ?' if bounded else ''} ORDER BY id",
  425. [parent_session_id, int(watermark), *([int(watermark_ceiling)] if bounded else [])])
  426. if tail_ids:
  427. self._clone_message_rows(conn, tail_ids, session_id=child_session_id)
  428. total_messages += len(tail_ids)
  429. total_tool_calls += tail_tool_calls
  430. conn.execute(
  431. "UPDATE sessions SET message_count = ?, tool_call_count = ? WHERE id = ?",
  432. (total_messages, total_tool_calls, child_session_id))
  433. updated = conn.execute(
  434. "UPDATE sessions SET ended_at = ?, end_reason = 'compression' "
  435. "WHERE id = ? AND ended_at IS NULL", (time.time(), parent_session_id))
  436. if updated.rowcount != 1:
  437. raise RuntimeError(f"Compression parent changed during publication: {parent_session_id}")
  438. # A child on a real surface stamps the chain's unset ancestors, so compressing a conversation
  439. # whose root predates write-time stamping lists it again under its surface.
  440. stamp_compression_ancestors(conn, child_session_id)
  441. from hermes_state_local_lineage import advance_local_target
  442. advance_local_target(conn, parent_session_id, child_session_id)
  443. if parent["archived"]:
  444. # A live continuation under an idle-sweep archive re-activates the chat; after the
  445. # closure above the child is linked into the lineage walk (#117713).
  446. self._unarchive_auto_archived_lineage(conn, child_session_id)
  447. def _write_sql_logged(self, op: str, session_id: str, sql: str, params) -> None:
  448. """``_write_sql`` that logs (never raises) on ``sqlite3.Error``."""
  449. try:
  450. self._write_sql(sql, params)
  451. except sqlite3.Error as exc:
  452. logger.warning("%s(%s) failed: %s", op, session_id, exc)
  453. def record_compression_failure_cooldown(
  454. self, session_id: str, cooldown_until: float, error: Optional[str] = None) -> None:
  455. """Persist the active compression-failure cooldown. Merge-max with any longer live deadline so a
  456. later shorter write can't reopen the thrash window; error always takes the latest diagnostic."""
  457. if not session_id:
  458. return
  459. self._write_sql_logged(
  460. "record_compression_failure_cooldown", session_id,
  461. # Merge-max with any longer live deadline so a later shorter write cannot reopen the thrash
  462. # window (#96775). The error column always takes the latest diagnostic.
  463. "UPDATE sessions SET compression_failure_cooldown_until = CASE "
  464. "WHEN compression_failure_cooldown_until IS NOT NULL AND compression_failure_cooldown_until > ? "
  465. "THEN compression_failure_cooldown_until ELSE ? END, compression_failure_error = ? WHERE id = ?",
  466. (cooldown_until, cooldown_until, error, session_id))
  467. def get_compression_failure_cooldown(self, session_id: str) -> Optional[dict[str, Any]]:
  468. """Return the active (unexpired) compression-failure cooldown, or None."""
  469. now = time.time()
  470. row = self._read_one(_COOLDOWN_ROW_SQL, (session_id,)) if session_id else None
  471. if row is None or row[0] is None or float(row[0]) <= now:
  472. return None
  473. return {"cooldown_until": float(row[0]), "remaining_seconds": float(row[0]) - now, "error": row[1]}
  474. def get_compression_failure_cooldown_row(self, session_id: str) -> dict[str, Any]:
  475. """Exact stored cooldown columns, no expiry filtering, so compression cancellation
  476. can roll back an expired, partially-null, or absent row exactly."""
  477. row = self._read_one(_COOLDOWN_ROW_SQL, (session_id,)) if session_id else None
  478. return _cooldown_row(False, None, None) if row is None else _cooldown_row(True, row[0], row[1])
  479. def restore_compression_failure_cooldown_row(self, session_id: str, snapshot: dict[str, Any]) -> None:
  480. """Restore and verify an exact cooldown-row snapshot. Unlike record/clear this
  481. rollback API propagates write and verification failures: cancellation must not be
  482. reported mutation-free when compensation failed. The tolerated exception is a
  483. session row that vanished mid-attempt — before or after the compensating write —
  484. since its cooldown died with it and there is nothing left to restore (#106271)."""
  485. if not snapshot.get("session_exists", False):
  486. if self.get_compression_failure_cooldown_row(session_id).get("session_exists", False):
  487. raise RuntimeError("cannot restore absent compression cooldown row: session now exists")
  488. return
  489. deadline = snapshot.get("cooldown_until")
  490. error = snapshot.get("error")
  491. def _do(conn):
  492. cursor = conn.execute(
  493. "UPDATE sessions SET compression_failure_cooldown_until = ?, "
  494. "compression_failure_error = ? WHERE id = ?", (deadline, error, session_id))
  495. return cursor.rowcount == 1
  496. if not self._execute_write(_do):
  497. logger.warning("compression cooldown rollback session missing: %s", session_id)
  498. return
  499. actual = self.get_compression_failure_cooldown_row(session_id)
  500. expected = _cooldown_row(True, deadline, error)
  501. if actual != expected:
  502. if not actual.get("session_exists", False):
  503. logger.warning("compression cooldown rollback session missing after restore: %s", session_id)
  504. return
  505. raise RuntimeError(
  506. f"compression cooldown rollback verification failed: expected={expected!r}, actual={actual!r}")
  507. def clear_compression_failure_cooldown(self, session_id: str) -> None:
  508. """Clear any persisted compression-failure cooldown for a session."""
  509. if not session_id:
  510. return
  511. self._write_sql_logged(
  512. "clear_compression_failure_cooldown", session_id,
  513. "UPDATE sessions SET compression_failure_cooldown_until = NULL, "
  514. "compression_failure_error = NULL WHERE id = ?", (session_id,))
  515. def _read_session_number(self, column: str, session_id: str, cast: type, zero: Any) -> Any:
  516. """Read one numeric ``sessions`` column clamped at ``zero``; a missing session,
  517. NULL, or unparsable value also reads as ``zero``."""
  518. row = self._read_one(f"SELECT {column} FROM sessions WHERE id = ?", (session_id,)) if session_id else None
  519. try:
  520. return zero if row is None else max(zero, cast(row[0] or zero))
  521. except (TypeError, ValueError):
  522. return zero
  523. def _write_session_column(self, column: str, session_id: str, value: Any) -> None:
  524. self._write_sql(f"UPDATE sessions SET {column} = ? WHERE id = ?", (value, session_id))
  525. def get_compression_fallback_streak(self, session_id: str) -> int:
  526. """Return the persisted deterministic-fallback streak."""
  527. return self._read_session_number("compression_fallback_streak", session_id, int, 0)
  528. def set_compression_fallback_streak(self, session_id: str, streak: int) -> None:
  529. """Persist the deterministic-fallback streak for one session."""
  530. if session_id:
  531. self._write_session_column("compression_fallback_streak", session_id, max(0, int(streak)))
  532. def get_compression_ineffective_count(self, session_id: str) -> int:
  533. """Persisted ineffective-compaction strike count — the durable half of the built-in
  534. compressor's anti-thrash guard, so a fresh compressor bound to a resumed session
  535. inherits an armed/tripped guard across restarts."""
  536. return self._read_session_number("compression_ineffective_count", session_id, int, 0)
  537. def set_compression_ineffective_count(self, session_id: str, count: int) -> None:
  538. """Persist the ineffective-compaction strike count for one session."""
  539. if session_id:
  540. self._write_session_column("compression_ineffective_count", session_id, max(0, int(count)))
  541. def get_compression_recovery_deadline(self, session_id: str) -> float:
  542. """Persisted anti-thrash recovery deadline (epoch; ``0.0`` = not armed). Durable
  543. because the gateway rebuilds the compressor every turn / cache eviction.
  544. The deadline is the durable half of the 14694 recovery clock: the gateway rebuilds the compressor on
  545. every turn / cache eviction, so a process-local deadline restarted the wait on each rebuild and a
  546. tripped session never earned its probe (#100185).
  547. """
  548. return self._read_session_number("compression_recovery_deadline", session_id, float, 0.0)
  549. def set_compression_recovery_deadline(self, session_id: str, deadline: float) -> None:
  550. """Persist the anti-thrash recovery deadline; ``0`` / ``None`` disarms it."""
  551. if not session_id:
  552. return
  553. try:
  554. normalized = max(0.0, float(deadline or 0.0))
  555. except (TypeError, ValueError):
  556. normalized = 0.0
  557. self._write_session_column("compression_recovery_deadline", session_id, normalized or None)
  558. def get_compression_overload_streak(self, session_id: str) -> int:
  559. """Return the persisted sustained-overload abort streak (#123167)."""
  560. return self._read_session_number("compression_overload_streak", session_id, int, 0)
  561. def set_compression_overload_streak(self, session_id: str, streak: int) -> None:
  562. """Persist the sustained-overload abort streak for one session."""
  563. if session_id:
  564. self._write_session_column("compression_overload_streak", session_id, max(0, int(streak)))
  565. def increment_compression_overload_streak(self, session_id: str) -> Optional[int]:
  566. """Atomically bump the overload streak and return the new value (None when no row).
  567. One UPDATE ... RETURNING, so concurrent agents on one session cannot lose a strike."""
  568. if not session_id:
  569. return None
  570. def _do(conn):
  571. row = conn.execute(
  572. "UPDATE sessions SET compression_overload_streak = compression_overload_streak + 1"
  573. " WHERE id = ? RETURNING compression_overload_streak", (session_id,)).fetchone()
  574. return None if row is None else int(row[0])
  575. return self._execute_write(_do)
  576. def refresh_compression_lock(self, session_id: str, holder: str, ttl_seconds: float = 300.0) -> bool:
  577. """Extend the compression lock lease if ``holder`` still owns it. Ownership is decided by ``holder``
  578. alone, deliberately NOT ``expires_at``: a live owner whose refresher stalled past its TTL must be
  579. able to revive its still-unclaimed row, otherwise it keeps compressing with no lease — the window in
  580. which a competing path can fork the lineage. It cannot resurrect a lock someone else took: SQLite
  581. serialises writes, so the reclaim (DELETE-expired + INSERT OR IGNORE) never interleaves with this
  582. UPDATE."""
  583. if not session_id or not holder:
  584. return False
  585. expires_at = time.time() + ttl_seconds
  586. try:
  587. return self._write_rowcount(
  588. "UPDATE compression_locks SET expires_at = ? WHERE session_id = ? AND holder = ?",
  589. (expires_at, session_id, holder)) > 0
  590. except sqlite3.Error as exc:
  591. logger.warning("refresh_compression_lock(%s) failed: %s", session_id, exc)
  592. return False
  593. def try_acquire_compression_lock(self, session_id: str, holder: str, ttl_seconds: float = 300.0) -> bool:
  594. """Try to atomically acquire the compression lock for ``session_id``. ``False``: another holder owns
  595. a live lock and the caller MUST NOT compress (its rotation would split the lineage). Expired
  596. locks and structured holders whose local ``pid=`` is dead are reclaimed transparently."""
  597. from hermes_state import _compression_lock_holder_process_is_dead
  598. if not session_id:
  599. return False
  600. now = time.time()
  601. expires_at = now + ttl_seconds
  602. def _do(conn):
  603. return _claim_lease_row(
  604. conn, "compression_locks", "session_id", session_id, holder, now, expires_at,
  605. lambda h, e: e < now or _compression_lock_holder_process_is_dead(h))
  606. try:
  607. acquired, reclaimed_holder = self._execute_write(_do)
  608. if reclaimed_holder:
  609. logger.warning("Reclaimed stale compression lock for session=%s (holder=%s)",
  610. session_id, reclaimed_holder)
  611. return bool(acquired)
  612. except sqlite3.Error as exc:
  613. # False makes the caller skip compression — safe when the lock subsystem is broken.
  614. logger.warning("try_acquire_compression_lock(%s) failed: %s", session_id, exc)
  615. return False
  616. def release_compression_lock(self, session_id: str, holder: str) -> None:
  617. """Release the compression lock iff we own it; idempotent when gone/reclaimed."""
  618. if not session_id:
  619. return
  620. self._write_sql_logged(
  621. "release_compression_lock", session_id,
  622. "DELETE FROM compression_locks WHERE session_id = ? AND holder = ?",
  623. (session_id, holder))
  624. def _session_turn_lease_key_on_conn(self, conn, session_id: str) -> str:
  625. """Walk compression parents on ``conn`` to the conversation lease key. Must share
  626. the connection of the lease INSERT/UPDATE/DELETE: a failed lookup must not yield a
  627. child id the write then persists. Markers bind to ``parent_session_id``. Lock
  628. errors propagate so ``_execute_write`` can retry."""
  629. if not session_id:
  630. return session_id
  631. def _row(sid: str):
  632. row = conn.execute(
  633. "SELECT id, parent_session_id, source, model_config, end_reason FROM sessions WHERE id = ?",
  634. (sid,)).fetchone()
  635. return dict(row) if row else None
  636. current = _row(session_id)
  637. seen = {session_id}
  638. while current:
  639. parent_id = current.get("parent_session_id")
  640. if not parent_id or parent_id in seen or self._is_explicit_fork_child_row(current, include_reset=True):
  641. break
  642. parent = _row(parent_id)
  643. if not parent or parent.get("end_reason") != "compression":
  644. break
  645. seen.add(parent_id)
  646. current = parent
  647. return str(current.get("id") or session_id) if current else session_id
  648. def _session_turn_lease_key(self, session_id: str) -> str:
  649. """Stable serialization key for every compression segment (tests/diagnostics; the
  650. write paths resolve it inside their own txn). Does not swallow lock errors."""
  651. if not session_id:
  652. return session_id
  653. with self._read_ctx() as conn:
  654. return self._session_turn_lease_key_on_conn(conn, session_id)
  655. def try_acquire_session_turn_lease(
  656. self, session_id: str, holder: str, *, ttl_seconds: float = 300.0, patience_s: Optional[float] = None,
  657. ) -> bool:
  658. """Atomically acquire the cross-process turn lease for a conversation (keyed by the
  659. lineage root). The walk, the INSERT, and reclaim of expired or dead-local-PID leases
  660. share one write transaction."""
  661. from hermes_state import _compression_lock_holder_process_is_dead
  662. if not session_id or not holder:
  663. return False
  664. now = time.time()
  665. expires_at = now + max(0.1, float(ttl_seconds))
  666. def _do(conn):
  667. # Sweep rows that expired long ago: a holder that died without releasing leaves its row
  668. # behind, and nothing else revisits a conversation nobody resumes. The grace keeps the
  669. # recent expiries a still-live owner can renew from a starved refresher; it is also the
  670. # longest a suspended holder can go unrefreshed and still keep its lease.
  671. conn.execute("DELETE FROM session_turn_leases WHERE expires_at < ?",
  672. (now - _TURN_LEASE_SWEEP_GRACE_S,))
  673. conversation_id = self._session_turn_lease_key_on_conn(conn, session_id)
  674. return _claim_lease_row(
  675. conn, "session_turn_leases", "conversation_id", conversation_id, holder, now, expires_at,
  676. lambda h, e: float(e) <= now or _compression_lock_holder_process_is_dead(h),
  677. )[0]
  678. return bool(self._execute_write(_do, patience_s=patience_s))
  679. def acquire_session_turn_lease(
  680. self, session_id: str, holder: str, *, ttl_seconds: float = 300.0,
  681. wait_seconds: float = 1800.0, poll_interval_seconds: float = 1.0, on_wait=None,
  682. wait_notice_interval_seconds: float = 15.0, should_abort=None, acquire_patience_s: float = 0.5,
  683. on_contended=None,
  684. ) -> bool:
  685. """Wait for a cross-process turn lease without holding a SQLite lock. ``on_wait(elapsed)`` is
  686. best-effort: called when another holder has the lease and about every
  687. ``wait_notice_interval_seconds`` after. A busy database is not a holder: the attempt is
  688. retried at once with a longer write patience, and ``on_contended()`` is called instead,
  689. since the busy writer may be the holder's last flush. ``should_abort()`` True (e.g.
  690. ``/stop``) returns False at once."""
  691. from hermes_state import classify_persistence_error
  692. deadline = time.monotonic() + max(0.0, float(wait_seconds))
  693. patience = acquire_patience_s
  694. wait_started = None
  695. last_notice_at = None
  696. notice_every = max(0.0, float(wait_notice_interval_seconds))
  697. while True:
  698. if should_abort is not None:
  699. try:
  700. if should_abort():
  701. return False
  702. except Exception:
  703. logger.debug("session turn lease should_abort callback failed", exc_info=True)
  704. try:
  705. if self.try_acquire_session_turn_lease(
  706. session_id, holder, ttl_seconds=ttl_seconds, patience_s=patience):
  707. return True
  708. except sqlite3.Error as exc:
  709. # Another writer's transaction outlasted the write patience. That is not a lease
  710. # holder, so no poll sleep or "another process" notice; the retry waits on the write
  711. # lock itself (the poll interval moves into its patience) and wins it once free.
  712. if classify_persistence_error(exc) != "locked":
  713. raise
  714. if on_contended is not None:
  715. try:
  716. on_contended()
  717. except Exception:
  718. logger.debug("session turn lease on_contended callback failed", exc_info=True)
  719. patience = min(acquire_patience_s + max(0.0, float(poll_interval_seconds)),
  720. max(acquire_patience_s, deadline - time.monotonic()))
  721. if not self._sleep_before_write_retry(deadline, 0.0):
  722. return False
  723. continue
  724. patience = acquire_patience_s
  725. now = time.monotonic()
  726. remaining = deadline - now
  727. if remaining <= 0:
  728. return False
  729. if wait_started is None:
  730. wait_started = now
  731. if on_wait is not None and (
  732. last_notice_at is None or notice_every == 0.0 or (now - last_notice_at) >= notice_every
  733. ):
  734. try:
  735. on_wait(max(0.0, now - wait_started))
  736. except Exception:
  737. logger.debug("session turn lease on_wait callback failed", exc_info=True)
  738. last_notice_at = now
  739. time.sleep(min(max(0.01, float(poll_interval_seconds)), remaining))
  740. def session_turn_lease_expires_at(self, session_id: str, holder: str) -> Optional[float]:
  741. """Committed ``expires_at`` of ``holder``'s turn lease, or None when it does not hold it."""
  742. if not session_id or not holder:
  743. return None
  744. with self._read_ctx() as conn:
  745. row = conn.execute(
  746. "SELECT expires_at FROM session_turn_leases WHERE conversation_id = ? AND holder = ?",
  747. (self._session_turn_lease_key_on_conn(conn, session_id), holder),
  748. ).fetchone()
  749. return float(row[0]) if row else None
  750. def refresh_session_turn_lease(
  751. self, session_id: str, holder: str, *, ttl_seconds: float = 300.0, patience_s: Optional[float] = None,
  752. ) -> bool:
  753. """Extend a turn lease only while ``holder`` still owns it. ``expires_at`` is stamped before
  754. the write-lock wait, so a caller timestamp taken before this call never exceeds it."""
  755. if not session_id or not holder:
  756. return False
  757. expires_at = time.time() + max(0.1, float(ttl_seconds))
  758. def _do(conn):
  759. conversation_id = self._session_turn_lease_key_on_conn(conn, session_id)
  760. return conn.execute(
  761. "UPDATE session_turn_leases SET expires_at = ? "
  762. "WHERE conversation_id = ? AND holder = ?", (expires_at, conversation_id, holder),
  763. ).rowcount > 0
  764. return bool(self._execute_write(_do, patience_s=patience_s))
  765. def release_session_turn_lease(self, session_id: str, holder: str) -> None:
  766. """Release a turn lease iff ``holder`` still owns it; idempotent."""
  767. if not session_id or not holder:
  768. return
  769. def _do(conn):
  770. conversation_id = self._session_turn_lease_key_on_conn(conn, session_id)
  771. conn.execute(
  772. "DELETE FROM session_turn_leases WHERE conversation_id = ? AND holder = ?",
  773. (conversation_id, holder))
  774. self._execute_write(_do)
  775. def get_compression_lock_holder(self, session_id: str) -> Optional[str]:
  776. """Current (non-expired) holder for ``session_id``, or None. Diagnostic only."""
  777. if not session_id:
  778. return None
  779. row = self._read_one(
  780. "SELECT holder FROM compression_locks WHERE session_id = ? AND expires_at >= ?", (session_id, time.time()))
  781. return None if row is None else row[0]
  782. def finalize_orphaned_compression_sessions(self) -> int:
  783. """Mark orphaned compression continuations (parent ended by compression; child has
  784. messages, no end_reason/ended_at, api_call_count=0, older than 7 days) as
  785. ``orphaned_compression``. Non-destructive.
  786. Fix for #20001.
  787. """
  788. cutoff = time.time() - 604800 # 7 days
  789. return self._write_rowcount(
  790. """
  791. UPDATE sessions
  792. SET ended_at = ?,
  793. end_reason = 'orphaned_compression'
  794. WHERE api_call_count = 0
  795. AND end_reason IS NULL
  796. AND ended_at IS NULL
  797. AND started_at < ?
  798. AND parent_session_id IS NOT NULL
  799. AND EXISTS (
  800. SELECT 1 FROM sessions p
  801. WHERE p.id = sessions.parent_session_id
  802. AND p.end_reason = 'compression'
  803. AND p.ended_at IS NOT NULL
  804. )
  805. AND EXISTS (
  806. SELECT 1 FROM messages m
  807. WHERE m.session_id = sessions.id
  808. )
  809. """,
  810. (time.time(), cutoff),
  811. ) or 0
  812. def get_compression_chain(self, session_id: str) -> list[str]:
  813. """Walk the compression-continuation chain forward: root-first through the tip (``[session_id]``
  814. when no continuation); ``get_compression_tip`` is the last element. A continuation is a child of
  815. a session with ``end_reason='compression'``. The old ``child.started_at >= parent.ended_at`` test
  816. was too brittle (gateway + compression races insert the real continuation before ``ended_at`` is
  817. written, while a stale websocket later creates a sibling that passes it). Instead exclude
  818. branch/delegate/tool children and prefer children that continue the chain or are still live over
  819. stale closed siblings such as ``ws_orphan_reap``."""
  820. def step(current: str):
  821. with self._read_ctx() as conn:
  822. return conn.execute(_CHAIN_STEP_SQL, (current,)).fetchone()
  823. return _walk_compression_chain(session_id, step)
  824. def get_compression_tip(self, session_id: str) -> Optional[str]:
  825. """Live tip of a compression chain (``get_compression_chain`` semantics); the input
  826. id when no continuation exists."""
  827. chain = self.get_compression_chain(session_id)
  828. return chain[-1] if chain else session_id
  829. def _compression_tip_on(self, conn, session_id: str) -> str:
  830. """``get_compression_tip`` read on a writer's own connection: inside its transaction no rotation
  831. can publish between finding the tip and writing it."""
  832. return _walk_compression_chain(session_id, lambda current: conn.execute(_CHAIN_STEP_SQL, (current,)).fetchone())[-1]
  833. def _is_compression_child_row(self, child: dict[str, Any]) -> bool:
  834. parent_id = child.get("parent_session_id")
  835. # A reset fork of a compression-ended parent is its own conversation, not the continuation (#114271).
  836. if not parent_id or self._is_explicit_fork_child_row(child, include_reset=True):
  837. return False
  838. parent = self.get_session(parent_id)
  839. return bool(parent and parent.get("end_reason") == "compression")
  840. def get_compression_lineage(self, session_id: str) -> list[str]:
  841. """Return compression ancestors through tip in chronological order."""
  842. session = self.get_session(session_id)
  843. if not session or self._is_explicit_fork_child_row(session):
  844. return [session_id] if session else []
  845. root = session
  846. ancestors = {root["id"]}
  847. while self._is_compression_child_row(root):
  848. parent = self.get_session(root["parent_session_id"])
  849. if not parent or parent["id"] in ancestors:
  850. break
  851. root = parent
  852. ancestors.add(root["id"])
  853. lineage = [root["id"]]
  854. seen = {root["id"]}
  855. current = root
  856. while current.get("end_reason") == "compression":
  857. rows = self._read_all(
  858. """
  859. SELECT * FROM sessions
  860. WHERE parent_session_id = ?
  861. ORDER BY started_at ASC
  862. """, (current["id"],))
  863. next_child = next((dict(row) for row in rows if self._is_compression_child_row(dict(row))), None)
  864. if not next_child or next_child["id"] in seen:
  865. break
  866. lineage.append(next_child["id"])
  867. seen.add(next_child["id"])
  868. current = next_child
  869. # Later tips are included only when the requested session itself was compacted.
  870. return lineage if session_id in lineage else [session_id]