web_server_sessions.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. """Read-only session browsing, latest-descendant lookup and owner maintenance.
  2. """
  3. import logging
  4. import asyncio
  5. import time
  6. from contextlib import contextmanager
  7. from pathlib import Path
  8. from typing import Dict, Optional
  9. from hermes_state_common import _RESET_CHILD_SQL, _sql_json_extract
  10. # Same logger the code used before extraction (record parity).
  11. _log = logging.getLogger("hermes_cli.web_server")
  12. _DESCENDANTS_SQL = f"""
  13. WITH RECURSIVE descendants(id, parent_session_id, started_at) AS (
  14. SELECT id, parent_session_id, started_at FROM sessions WHERE id = ?
  15. UNION
  16. SELECT s.id, s.parent_session_id, s.started_at
  17. FROM sessions s
  18. JOIN descendants d ON s.parent_session_id = d.id
  19. -- Continuation edges only (same predicate as the session list's chain CTE): a subagent run,
  20. -- a /branch fork, a /new reset child or a tool-owned row is its own conversation, and resuming
  21. -- INTO one parks the user's chat in a row the sidebar never lists (#115092).
  22. WHERE {_sql_json_extract('s.model_config', '$._delegate_from')} IS NULL
  23. AND {_sql_json_extract('s.model_config', '$._branched_from')} IS NULL
  24. AND NOT ({_RESET_CHILD_SQL.format(a='s')})
  25. AND COALESCE(s.source, '') != 'tool'
  26. )
  27. SELECT id, parent_session_id, started_at FROM descendants
  28. """
  29. # Desktop 0.22.2's ``readableError`` keeps the HTTP ``detail`` and shows its "app and backend are
  30. # on different versions" toast (with an Update action) for "out of sync (different versions)".
  31. CLIENT_UPGRADE_REQUIRED = ('client_upgrade_required: this app is out of sync (different versions) '
  32. 'with the Hermes backend; update the app to edit sessions')
  33. def _session_latest_descendant(session_id: str, db):
  34. """Resolve a session id to the newest child leaf session.
  35. /model may create child sessions; a dashboard refresh should continue the
  36. newest child instead of reopening the old parent. Returns ``(leaf, path)``.
  37. """
  38. sid = db.resolve_session_id(session_id)
  39. if not sid or not db.get_session(sid):
  40. return None, []
  41. conn = getattr(db, "_conn", None)
  42. if conn is not None:
  43. keys = ("id", "parent_session_id", "started_at")
  44. rows = [dict(zip(keys, row)) for row in conn.execute(_DESCENDANTS_SQL, (sid,)).fetchall()]
  45. else:
  46. rows = db.list_sessions_rich(limit=10000, offset=0, compact_rows=True)
  47. children = {}
  48. for row in rows:
  49. rid = row.get("id")
  50. parent = row.get("parent_session_id")
  51. if rid and parent:
  52. children.setdefault(parent, []).append(row)
  53. def started(row):
  54. try:
  55. return float(row.get("started_at") or 0)
  56. except Exception:
  57. return 0.0
  58. current = sid
  59. path = [sid]
  60. seen = {sid}
  61. while children.get(current):
  62. candidates = [r for r in children[current] if r.get("id") not in seen]
  63. if not candidates:
  64. break
  65. candidates.sort(key=started, reverse=True)
  66. current = candidates[0]["id"]
  67. path.append(current)
  68. seen.add(current)
  69. return current, path
  70. def _session_mutation_context(request, profile):
  71. """Use the same principal and profile boundary for prepare and apply."""
  72. from fastapi import HTTPException
  73. from gateway.session_contract import SessionActor
  74. from hermes_cli.web_server import _has_valid_session_token
  75. from hermes_cli.web_server_cron import _cron_profile_home
  76. from hermes_state import _default_db_path
  77. native = getattr(request.state, 'native_http_principal', None)
  78. session = getattr(request.state, 'session', None)
  79. if native is not None:
  80. subject = native['subject']
  81. elif session is not None:
  82. from gateway.owner_identity import principal_subject
  83. subject = principal_subject({"user_id": session.user_id,
  84. "provider": session.provider, "issuer": session.issuer})
  85. elif not getattr(request.app.state, 'auth_required', False) and _has_valid_session_token(request):
  86. from gateway.owner_identity import host_owner_subject
  87. subject = host_owner_subject()
  88. else:
  89. raise HTTPException(status_code=401, detail='Unauthorized')
  90. authority = getattr(request.app.state, 'session_authority', None)
  91. home = Path(_cron_profile_home(profile)[1]) if profile else Path(_default_db_path()).parent
  92. capabilities = frozenset({'session:read', 'session:control', 'session:create', 'session:operator'})
  93. if authority is None:
  94. if getattr(request.app.state, 'gateway_runner', None) is not None:
  95. raise HTTPException(status_code=503, detail='session_authority_unavailable')
  96. # Standalone ``serve``/``dashboard`` (no composed gateway; its owner cutover is a
  97. # follow-up): the store is the owner. Same principal id form as a gateway authority's
  98. # (``str(home)``), so import bindings written offline stay valid once one starts.
  99. return None, SessionActor(subject, str(home.resolve()), capabilities, 'http')
  100. # ``?profile=`` selects a served home; its own authority (not the launch one) owns the mutation.
  101. from gateway.session_authorities import authority_for_home
  102. runner = getattr(request.app.state, 'gateway_runner', None)
  103. owner = authority_for_home(runner, home) if runner is not None else None
  104. if owner is None and home.resolve() == Path(authority.db.db_path).parent.resolve():
  105. owner = authority
  106. if owner is None:
  107. raise HTTPException(status_code=403, detail='profile_mismatch')
  108. authority = owner
  109. if native is not None and native['profile_id'] != authority.profile_id:
  110. raise HTTPException(status_code=403, detail='profile_mismatch')
  111. if native is not None:
  112. # One transport per verified native request, so the grant's own profile can claim its old
  113. # local history exactly as over the native socket (``require_history_claim``); a shared
  114. # 'http' id never can.
  115. import uuid
  116. return authority, SessionActor(subject, authority.profile_id, capabilities, 'native-http:' + uuid.uuid4().hex)
  117. return authority, SessionActor(subject, authority.profile_id, capabilities, 'http')
  118. @contextmanager
  119. def _native_history_transport(request, authority, actor):
  120. """Bind a verified native HTTP actor as a native legacy transport for one mutation."""
  121. native = None
  122. if authority is not None and str(getattr(actor, 'transport_id', '')).startswith('native-http:'):
  123. native = getattr(request.state, 'native_http_principal', None)
  124. if native is None:
  125. yield
  126. return
  127. from gateway.session_local_migration import bind_native_transport, unbind_native_transport
  128. bind_native_transport(authority, actor, {'provider': 'local', 'profile_id': authority.profile_id,
  129. 'instance_id': native['instance_id']})
  130. try:
  131. yield
  132. finally:
  133. unbind_native_transport(authority, actor)
  134. def _mutate_offline_store(profile, actor, params):
  135. """Standalone edit: the receipt transaction on this process's single shared writer.
  136. Runs under the exact owner lock (409 while any gateway owns the store, never a
  137. second writer beside it); revision/generation/idle guards are the gateway's own.
  138. """
  139. from fastapi import HTTPException
  140. from hermes_state_runtime import begin_runtime_epoch, mutate_runtime_session
  141. def run():
  142. db = _open_session_db_for_profile(profile, read_only=False)
  143. try:
  144. write_guard = None
  145. if params['operation'] == 'import':
  146. normalized, errors = db._validate_import_payload(params['payload']['sessions'])
  147. if errors:
  148. raise HTTPException(status_code=400, detail={'errors': errors})
  149. from functools import partial
  150. from hermes_state_mutation_binding import import_history_control
  151. write_guard = partial(import_history_control, actor=actor,
  152. session_ids=tuple(item['session']['id'] for item in normalized))
  153. with db._read_ctx() as conn:
  154. row = conn.execute('SELECT epoch FROM runtime_epoch WHERE singleton=1').fetchone()
  155. # The reservation held here is the ownership lock begin_runtime_epoch requires.
  156. epoch = row[0] if row else begin_runtime_epoch(db, instance_id='dashboard-offline')
  157. return mutate_runtime_session(db, epoch=epoch, principal_id=actor.subject,
  158. expected_generation=params.get('expected_generation'), _authorize_write=write_guard,
  159. **{key: params[key] for key in ('session_id', 'request_id', 'expected_revision',
  160. 'operation', 'payload')})
  161. finally:
  162. db.close()
  163. return _with_session_maintenance(profile, run)
  164. async def _mutate_via_gateway_owner(profile, actor, params):
  165. """Forward a standalone dashboard mutation to the live gateway owner.
  166. While a gateway serves the same home, a separately launched ``hermes
  167. dashboard`` is not the store owner, so the offline path's
  168. exclusive-maintenance reservation can never win and every write action
  169. dies on a bare 409 (#138915). The owner exposes the same revision-fenced
  170. verb over the control socket. With no endpoint the offline store's reservation
  171. decides: no endpoint does not prove a live owner (a stale bad-mode socket, a parked
  172. secondary read ``inaccessible`` with the lock free). When the reservation loses and
  173. discovery read anything but ``absent`` (a control timeout, a profile mismatch, one
  174. starting or draining), the edit answers 503 with discovery's reason rather than the
  175. bare 409 (#144141).
  176. A dial, mint or RPC that dies on an endpoint discovery just identified is
  177. not owner-less (a slow first dial, or an owner restart between discovery
  178. and mint): the offline reservation would only lose to that same owner and
  179. answer with the bare 409 this forward exists to remove, so every transport
  180. failure surfaces a retryable 503. Only the owner's refusal frame
  181. (``GatewayRPCError``) is passed on as a verdict.
  182. The ticket is minted for the dashboard's authenticated principal, not the
  183. socket UID: ``authorize_history`` compares the actor subject against the
  184. row's stored owner, so a cold row owned by a dashboard account only takes
  185. the mutation when the forward acts as that account.
  186. """
  187. from fastapi import HTTPException
  188. from gateway.runtime_ownership import OwnershipConflict
  189. from hermes_cli.gateway_client import GatewayClientError, GatewayRPCError, connect_gateway
  190. from hermes_cli.gateway_runtime import discover_gateway_endpoint
  191. from hermes_cli.gateway_runtime_discovery import DiscoveryError
  192. from hermes_cli.web_server_cron import _cron_profile_home
  193. from hermes_state import _default_db_path
  194. home = Path(_cron_profile_home(profile)[1]) if profile else Path(_default_db_path()).parent
  195. # Discovery is read-only and never starts a gateway.
  196. discovery = await asyncio.to_thread(discover_gateway_endpoint, home, timeout=1.5)
  197. endpoint = discovery.endpoint
  198. if endpoint is None:
  199. try:
  200. return await asyncio.to_thread(_mutate_offline_store, profile, actor, params)
  201. except HTTPException as exc:
  202. if discovery.state == 'absent' or not isinstance(exc.__cause__, OwnershipConflict):
  203. raise
  204. known = ''.join(f', {value}' for value in (discovery.reason_code, discovery.detail) if value)
  205. _log.warning('session write for %s: gateway owner not reachable (%s%s)', home, discovery.state, known)
  206. reason = discovery.reason_code or discovery.state
  207. raise HTTPException(status_code=503, detail=f'owner_unavailable: {reason}: {exc.detail}') from exc
  208. if not getattr(endpoint, 'control_home', True):
  209. # The selected home answered on its own socket (a multiplexer-served profile already
  210. # names the multiplexer root). Pin it, so the ticket is minted on the socket that just
  211. # identified as this profile's owner, never on the dashboard launch home's.
  212. from dataclasses import replace
  213. endpoint = replace(endpoint, control_home=str(home.expanduser().resolve()))
  214. call = {key: params[key] for key in
  215. ('session_id', 'request_id', 'expected_revision', 'operation', 'payload') if key in params}
  216. if params.get('expected_generation') is not None:
  217. call['expected_generation'] = params['expected_generation']
  218. # A sign-in is the machine owner (owner_identity), which is this same-user socket's own peer
  219. # subject: let the socket mint it natively. The verifier lets a peer NAME only a structured
  220. # login subject, so forwarding the owner's OS subject explicitly would be refused.
  221. from gateway.owner_identity import host_owner_subject
  222. subject = None if actor.subject == host_owner_subject() else actor.subject
  223. try:
  224. async with connect_gateway(endpoint=endpoint, subject=subject) as client:
  225. return await client.rpc('session.mutate', **call)
  226. except GatewayRPCError:
  227. # The owner's own refusal frame: a verdict the caller maps to the direct path's status.
  228. raise
  229. except (GatewayClientError, DiscoveryError, OSError, TimeoutError) as exc:
  230. # Transport, not a verdict: a refused WebSocket dial (wrapped as GatewayClientError), a
  231. # control socket that refuses the mint after a restart (DiscoveryError), a raw socket error
  232. # or a timeout. Retrying is safe because the request reuses its request_id.
  233. raise HTTPException(status_code=503, detail='owner_unreachable') from exc
  234. # One reason -> HTTP status map for a session mutation refusal, shared by the direct path
  235. # (RuntimeStoreError) and the owner forward (GatewayClientError): a storage failure is a
  236. # server fault (503), never a session conflict.
  237. _MUTATION_REFUSAL_STATUS = {'permission_denied': 403, 'profile_mismatch': 403, 'not_found': 404,
  238. 'invalid_params': 400, 'runtime_draining': 503, 'storage_unavailable': 503}
  239. async def _mutate_session_request(request, profile, session_id, *, request_id,
  240. expected_revision, operation, payload, expected_generation=None):
  241. """Resolve the authenticated principal and owner; never open a second writer."""
  242. import sqlite3
  243. from fastapi import HTTPException
  244. from gateway.session_contract import SessionRef
  245. from gateway.session_mutations import mutate_session
  246. from hermes_cli.gateway_client import GatewayClientError
  247. from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
  248. from hermes_state_runtime import RuntimeStoreError
  249. authority, actor = _session_mutation_context(request, profile)
  250. if operation == 'import' and authority is not None:
  251. _, errors = authority.db._validate_import_payload(payload['sessions'])
  252. if errors:
  253. raise HTTPException(status_code=400, detail={'errors': errors})
  254. if request_id is None and expected_revision is None and expected_generation is None:
  255. # A pre-fence client (Desktop <= 0.22.2) sends none of the identity. The owner never
  256. # commits an unfenced write, so name the upgrade; the wording is what those builds'
  257. # toast summariser maps to "different versions, update" (#139742).
  258. raise HTTPException(status_code=409, detail=CLIENT_UPGRADE_REQUIRED)
  259. if request_id is None or expected_revision is None:
  260. raise HTTPException(status_code=409, detail='mutation_identity_required')
  261. params = dict(session_id=session_id, request_id=request_id, expected_revision=expected_revision,
  262. operation=operation, payload=payload)
  263. if expected_generation is not None:
  264. params['expected_generation'] = expected_generation
  265. try:
  266. if authority is None:
  267. # A live gateway owner answers first; a home without an endpoint takes
  268. # the offline store (whose reservation loses to any owner holding the lock).
  269. result = await _mutate_via_gateway_owner(profile, actor, params)
  270. else:
  271. with _native_history_transport(request, authority, actor):
  272. result = await mutate_session(authority, actor, SessionRef(authority.profile_id, session_id), params)
  273. return {'ok': True, **result}
  274. except RuntimeStoreError as exc:
  275. raise HTTPException(status_code=_MUTATION_REFUSAL_STATUS.get(exc.reason, 409), detail=exc.reason) from exc
  276. except GatewayClientError as exc:
  277. # The owner's own refusal verdicts (transport failures already became 503 above),
  278. # wire-shaped like RuntimeStoreError's reasons, so a forwarded write answers as the direct path.
  279. raise HTTPException(status_code=_MUTATION_REFUSAL_STATUS.get(str(exc), 409), detail=str(exc)) from exc
  280. except MUTATION_GUARD_REFUSALS as exc:
  281. raise HTTPException(status_code=409, detail='session_busy') from exc
  282. except ValueError as exc:
  283. raise HTTPException(status_code=400, detail=str(exc)) from exc
  284. except sqlite3.Error as exc:
  285. raise HTTPException(status_code=503, detail='storage_unavailable') from exc
  286. def _with_session_maintenance(profile, operation, *args):
  287. """Offline bulk actions reserve the exact owner lock through final DB close."""
  288. from fastapi import HTTPException
  289. from gateway.runtime_ownership import OwnershipConflict, exclusive_maintenance
  290. from hermes_cli.web_server_cron import _cron_profile_home
  291. from hermes_state import _default_db_path
  292. home = Path(_cron_profile_home(profile)[1]) if profile else Path(_default_db_path()).parent
  293. try:
  294. with exclusive_maintenance([home]):
  295. return operation(*args)
  296. except OwnershipConflict as exc:
  297. # uvicorn runs at log_level=warning, so a handled 4xx leaves no server-side trace and the
  298. # refusal reads as a UI bug (#138915). Bulk store maintenance has no owner verb: name it.
  299. _log.warning('session write refused for %s: %s', home, exc)
  300. raise HTTPException(status_code=409, detail=str(exc)) from exc
  301. def _session_db_read_probe_statements() -> tuple:
  302. """Probe the declared schema without reconciling it on a browsing request."""
  303. from hermes_state_schema import schema_read_probe_statements
  304. return schema_read_probe_statements()
  305. def _open_session_db_at_path(db_path: Path, *, read_only: bool):
  306. """Browsing never initializes or repairs the owner's session store.
  307. Owner startup reconciles schema. The one write a browse may do: a store that lags
  308. SCHEMA_SQL (missing table/column, not corruption) with NO gateway owning it gets the
  309. missing declared tables/columns once, in one short locked transaction (#140315); an
  310. owned store is refused by name instead. Explicit mutation APIs retain their writable
  311. acquisition until their separate owner-RPC migration.
  312. """
  313. import sqlite3
  314. from fastapi import HTTPException
  315. from hermes_state import SessionDB
  316. from hermes_state_errors import is_transient_sqlite_error
  317. from hermes_state_registry import acquire
  318. if not read_only:
  319. return acquire(db_path)
  320. try:
  321. initialized = db_path.stat().st_size > 0
  322. except FileNotFoundError:
  323. initialized = False
  324. if not initialized:
  325. raise HTTPException(
  326. status_code=503,
  327. detail="Session store is not initialized. Start this profile's gateway to initialize it.")
  328. from hermes_state_reconcile import (
  329. DeclarativeDriftError, is_declarative_drift_error, reconcile_drift_for_browse,
  330. )
  331. def _open_probed():
  332. db = SessionDB(db_path=db_path, read_only=True)
  333. try:
  334. conn = getattr(db, "_conn", None)
  335. if conn is not None:
  336. for statement in _session_db_read_probe_statements():
  337. conn.execute(statement).fetchone()
  338. return db
  339. except BaseException:
  340. db.close()
  341. raise
  342. try:
  343. try:
  344. return _open_probed()
  345. except sqlite3.OperationalError as exc:
  346. # A missing declared table/column is upgrade drift, not corruption (#140315): catch
  347. # an unowned store up once under its runtime lock, refuse by name when a gateway
  348. # owns it. Never destructive: additive DDL only, no repair from browse.
  349. if not is_declarative_drift_error(exc):
  350. raise
  351. try:
  352. reconcile_drift_for_browse(db_path)
  353. except DeclarativeDriftError as drift:
  354. _log.warning("session store at %s is behind SCHEMA_SQL: %s", db_path, drift)
  355. raise HTTPException(status_code=503, detail=str(drift)) from exc
  356. return _open_probed()
  357. except (sqlite3.DatabaseError, UnicodeDecodeError) as exc:
  358. from hermes_state_errors import is_malformed_db_error
  359. if isinstance(exc, sqlite3.DatabaseError) and is_malformed_db_error(exc):
  360. # Same structured 503 the analytics routers publish (``corrupt_store_as_status``): a
  361. # corrupt image names the repair (`hermes doctor`), never a generic "schema unavailable".
  362. from hermes_cli.web_routers._common import corrupt_store_status
  363. raise corrupt_store_status(db_path, exc) from exc
  364. if isinstance(exc, sqlite3.OperationalError) and is_transient_sqlite_error(exc):
  365. detail = "Session store is busy (disk I/O or lock). Retry; the list was not cleared."
  366. else:
  367. detail = (
  368. "Session store schema is unavailable or corrupt. Start or restart this profile's "
  369. "gateway to reconcile it; if it persists, run `hermes doctor` for diagnosis. "
  370. "Browsing does not repair the store.")
  371. raise HTTPException(status_code=503, detail=detail) from exc
  372. def _session_db_path_for_profile(profile: Optional[str]) -> Path:
  373. """state.db path for ``profile`` (None/empty = this process's own)."""
  374. from hermes_cli.web_server_cron import _cron_profile_home
  375. from hermes_state import _default_db_path
  376. if profile:
  377. _name, home = _cron_profile_home(profile)
  378. return Path(home) / "state.db"
  379. return Path(_default_db_path())
  380. def _open_session_db_for_profile(profile: Optional[str], *, read_only: bool):
  381. """Open a SessionDB for ``profile`` (None/empty = this process's own state.db).
  382. Access-mode semantics: see :func:`_open_session_db_at_path`.
  383. """
  384. return _open_session_db_at_path(_session_db_path_for_profile(profile), read_only=read_only)
  385. # In-process throttle for the opportunistic auto-archive trigger, keyed by
  386. # profile: bounds the config.yaml read to once per window; the sweep itself is
  387. # throttled far more coarsely by state_meta (sessions.min_interval_hours).
  388. _AUTO_ARCHIVE_CHECK_INTERVAL_S = 300.0
  389. _last_auto_archive_check: dict[str, float] = {}
  390. def _maybe_auto_archive_for_profile(profile: Optional[str]) -> None:
  391. """Config-gated owner-lifetime maintenance, never invoked by browsing.
  392. The standalone serve ticker maintains its own profile; a composed HTTP
  393. listener relies on its gateway owner's startup and housekeeping hooks.
  394. """
  395. try:
  396. key = profile or ""
  397. now = time.monotonic()
  398. last = _last_auto_archive_check.get(key)
  399. if last is not None and now - last < _AUTO_ARCHIVE_CHECK_INTERVAL_S:
  400. return
  401. _last_auto_archive_check[key] = now
  402. from hermes_cli.config import load_config as _load_full_config
  403. from hermes_constants import reset_hermes_home_override, set_hermes_home_override
  404. # The config that governs a store is the one in that store's OWN home. A zero-arg
  405. # load_config() resolves through the PROCESS HERMES_HOME, so the dashboard swept every
  406. # profile's sessions with the launch profile's sessions.auto_archive/auto_archive_days —
  407. # one profile's retention silently decided another's.
  408. profile_home = _session_db_path_for_profile(profile).parent
  409. _home_token = set_hermes_home_override(str(profile_home))
  410. try:
  411. cfg = (_load_full_config().get("sessions") or {})
  412. finally:
  413. reset_hermes_home_override(_home_token)
  414. if not cfg.get("auto_archive", False):
  415. return
  416. from hermes_cli.profiles import _check_gateway_running
  417. # A live gateway owns this profile's store and runs the same sweep on its own
  418. # housekeeping tick ("state.db maintenance tick" in gateway/run.py, profile-scoped so a
  419. # multiplexed secondary's store is swept too). Opening it WRITABLE from `hermes
  420. # serve` adds a second writer to a database another process is already archiving,
  421. # for zero extra coverage (#110405). `_check_gateway_running` is the canonical
  422. # per-profile predicate (`_maybe_run_skill_maintenance` below uses it): its
  423. # multiplexer rung catches a served secondary, which owns no gateway.pid or lock
  424. # of its own and a bare lock-file probe would report stopped.
  425. if _check_gateway_running(profile_home):
  426. return
  427. db = _open_session_db_for_profile(profile, read_only=False)
  428. try:
  429. db.maybe_auto_archive(
  430. idle_days=float(cfg.get("auto_archive_days", 3)),
  431. min_interval_hours=int(cfg.get("min_interval_hours", 24)))
  432. finally:
  433. db.close()
  434. except Exception as exc:
  435. _log.debug("opportunistic auto-archive skipped: %s", exc)
  436. def _skill_maintenance_idle_for(started_at: float) -> Optional[float]:
  437. """Measure chat inactivity, not socket inactivity (Desktop stays connected)."""
  438. import tui_gateway.server as gateway
  439. from hermes_constants import get_hermes_home
  440. home = get_hermes_home().resolve()
  441. with gateway._sessions_lock:
  442. sessions = [session for session in gateway._sessions.values()
  443. if Path(session.get("profile_home") or home).resolve() == home]
  444. if any(session.get("running") for session in sessions):
  445. return None
  446. last_active = max(
  447. [started_at, gateway._closed_session_activity.get(str(home), 0)]
  448. + [float(session.get("last_active") or started_at) for session in sessions])
  449. return max(0.0, time.time() - last_active)
  450. def _maybe_run_skill_maintenance(started_at: float) -> None:
  451. from hermes_constants import get_hermes_home
  452. from hermes_cli.profiles import _check_gateway_running
  453. # A live messaging gateway already owns these chores for this profile.
  454. if _check_gateway_running(get_hermes_home()):
  455. return
  456. from agent.curator import maybe_run_curator
  457. try:
  458. idle_for = _skill_maintenance_idle_for(started_at)
  459. if idle_for is not None:
  460. maybe_run_curator(idle_for_seconds=idle_for)
  461. except Exception as exc:
  462. _log.debug("serve curator tick skipped: %s", exc)
  463. async def _auto_archive_ticker_loop(
  464. interval_s: float = 3600.0, initial_delay_s: float = 90.0) -> None:
  465. """Poll maintenance for this serve profile, including Desktop-only installs.
  466. Individual chores own their config/interval gates. Curator additionally uses
  467. real chat inactivity; merely keeping a Desktop WebSocket open is not activity.
  468. """
  469. started_at = time.time()
  470. def _sweep() -> None:
  471. _maybe_auto_archive_for_profile(None)
  472. _maybe_run_skill_maintenance(started_at)
  473. await asyncio.sleep(initial_delay_s)
  474. while True:
  475. try:
  476. await asyncio.to_thread(_sweep)
  477. except Exception as exc:
  478. _log.debug("auto-archive tick skipped: %s", exc)
  479. await asyncio.sleep(interval_s)