session_history_mount.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Read-only mount of stored history no receipt, route or binding restores (#143547 RT-30b/48/49).
  2. Pre-authority cron runs, subagent transcripts, API rows without a binding, and rows a third-party
  3. WebUI or the foreign-session importer wrote: before #106742 a viewer opened any of them straight
  4. from ``state.db``. Nothing can run them (no frozen policy, no lane, and adopting a WebUI row as
  5. writable would legitimise its second writer), so the owner mounts them for reading only. The mount
  6. is keyed by no lane's route, so nothing route-keyed acts through it, and every capability but
  7. ``session:read`` is refused by name. Archive, pin and delete still work as for any read-only mount
  8. (gateway/session_mutations.py), but only a leaf row deletes. Old local CLI/TUI/Desktop rows are not mounted
  9. here: resume adopts them as writable conversations (gateway/session_local_migration.py), as it does a phone
  10. app's self-labelled chat (``mobile``, ``ios``) for its native owner; anyone else still reads that one here.
  11. """
  12. from hermes_state_runtime import RuntimeStoreError
  13. # Never a lane key, so no lane publishing a conversation takes this mount over.
  14. _ROUTE_PREFIX = 'history:'
  15. def _reader(authority, actor, sid):
  16. """The importer an import binding names, else the profile's owner: its native peer (the evidence
  17. legacy adoption takes) or an operator. Knowing an old id is not ownership."""
  18. from hermes_state_mutation_binding import binding_names, history_binding
  19. with authority.db._read_ctx() as conn:
  20. binding = history_binding(conn, sid)
  21. if binding is not None:
  22. return binding_names(binding, actor.profile_id, actor.subject)
  23. return ('session:operator' in actor.capabilities
  24. or getattr(authority, '_native_legacy_transports', {}).get(actor.transport_id) == actor)
  25. def mount_history(authority, actor, sid):
  26. """Mount *sid* read-only for its owner; False when another loader owns it or the actor may not."""
  27. from hermes_state_local import API_BINDING_PREFIX
  28. from hermes_state_local_migration import LEGACY_LOCAL_SOURCES
  29. row = authority.db.get_session(sid)
  30. if (row['source'] in LEGACY_LOCAL_SOURCES | {'local'}
  31. or row.get('profile_name') not in (None, '', authority.db._own_profile_name() or 'default')
  32. or not _reader(authority, actor, sid)):
  33. return False
  34. with authority.db._read_ctx() as conn:
  35. if conn.execute('SELECT 1 FROM state_meta WHERE key=?', (API_BINDING_PREFIX + sid,)).fetchone():
  36. return False # restore_api_session's
  37. authority._require_admission_open()
  38. from gateway.session_authority import LiveSession
  39. authority.sessions.setdefault(sid, LiveSession(None, _ROUTE_PREFIX + sid))
  40. return True
  41. def is_history_mount(live):
  42. return live.source is None and live.route.startswith(_ROUTE_PREFIX)
  43. def require_history_leaf(authority, sid):
  44. """A delete takes its whole compression chain, every chain row's delegate subtree, and detaches
  45. their other children, and a history mount shows the owner only the chain, so it deletes only a
  46. chain nothing else hangs off (as #140692 by @pnpwin did for subagents)."""
  47. if not is_history_mount(authority.sessions[sid]):
  48. return
  49. from hermes_state_mutation_guards import delete_targets
  50. chain = set(authority.db.get_compression_lineage(sid))
  51. with authority.db._read_ctx() as conn:
  52. targets = delete_targets(conn, sid)
  53. children = conn.execute(f"SELECT id FROM sessions WHERE parent_session_id IN ({','.join('?' * len(targets))})",
  54. targets).fetchall()
  55. if not chain.issuperset([*targets, *(row[0] for row in children)]):
  56. raise RuntimeStoreError('session_has_children', field='session_id',
  57. detail='this stored conversation has child sessions a delete would take with it')
  58. def read_only():
  59. return RuntimeStoreError('session_read_only', field='session_id',
  60. detail='this stored conversation can be read but not continued; start a new chat')
  61. def require_history_access(authority, actor, sid, capability):
  62. if not _reader(authority, actor, sid):
  63. raise RuntimeStoreError('not_found')
  64. if capability != 'session:read':
  65. raise read_only()