session_tui_reads.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """Main's TUI text for the session reads its sidecar answered itself: /status, /context.
  2. On main the TUI's ``slash.exec`` never reached the messaging gateway's handlers: ``/status`` was
  3. ``session.status`` (``Hermes TUI Status``), ``/context`` was ``_format_live_context_output`` (the
  4. conversation counts, then the desktop gauge's category table). A TUI session on the shared owner
  5. gets the same lines here, read from the owner's live agent and stored row; every other surface keeps
  6. the gateway's own handlers (``gateway/session_commands.py``). ``/cron``, the classic CLI's handler
  7. that main's TUI ran in its slash worker, is gateway/session_cron_commands.py.
  8. """
  9. from __future__ import annotations
  10. import asyncio
  11. import contextlib
  12. import json
  13. from collections import Counter
  14. from pathlib import Path
  15. # The TUI surfaces whose /status and /context read as main's TUI sidecar did.
  16. _TUI_SOURCES = frozenset({'tui'})
  17. def tui_read(policy, name: str, arg: str):
  18. """The renderer for a TUI session's read of *name*, or None to keep the gateway handler."""
  19. if policy is None or policy.source not in _TUI_SOURCES:
  20. return None
  21. if name == 'status':
  22. return _status
  23. if name == 'context':
  24. return _context
  25. return None
  26. def _stored_id(authority, ref) -> str:
  27. with contextlib.suppress(Exception):
  28. from hermes_state_local import local_receipt
  29. return local_receipt(authority.db, ref.session_id)['entry']['session_id'] or ref.session_id
  30. return ref.session_id
  31. def _context_id(authority, ref) -> str:
  32. """The compression tip the session's model context lives in; the runtime id may still name a parent."""
  33. stored = _stored_id(authority, ref)
  34. return authority.db.get_compression_tip(stored) or stored
  35. def _model_history(db, tip) -> list:
  36. """The tip's working context: its active rows as live replay loads them, minus the ``session_meta``
  37. bookkeeping the gateway strips before the model sees history. Superseded segments are not read."""
  38. return [m for m in db.get_messages_as_conversation(tip, repair_alternation=True, include_row_ids=True)
  39. if m['role'] != 'session_meta']
  40. def _status(authority, ref, live, policy, agent, arg) -> str:
  41. """``tui_gateway/methods_session.py`` ``session.status`` on main, for the owner's session."""
  42. from hermes_cli.status_report import build_status_fields, status_lines
  43. from tui_gateway import server
  44. stored = _stored_id(authority, ref)
  45. row = {}
  46. with contextlib.suppress(Exception):
  47. row = authority.db.get_session(stored) or {}
  48. running = authority._handle(ref).execution_state != 'idle'
  49. fields = build_status_fields(
  50. stored, agent, row, model=policy.current_model(authority) if agent is None else None,
  51. provider=policy.provider if agent is None else None,
  52. tokens=getattr(agent, 'session_total_tokens', 0) if agent is not None else None,
  53. agent_running=running, home=authority.profile_id)
  54. if fields['provider'] == 'custom': # the adapter's billing class: show the entry the session runs on
  55. from gateway.run import _profile_runtime_scope
  56. from hermes_cli.runtime_provider_custom import session_provider_identity
  57. with _profile_runtime_scope(Path(authority.profile_id)):
  58. fields['provider'] = session_provider_identity('custom', getattr(agent, 'requested_provider', None),
  59. getattr(agent, 'base_url', None) or policy.base_url, fields['model'])
  60. project = server._project_info_for_cwd(policy.cwd)
  61. return '\n'.join([
  62. 'Hermes TUI Status', '', *status_lines(fields, 'session_id', 'path'),
  63. *([f"Project: {project['name']}"] if project else []),
  64. *status_lines(fields, 'title', 'model', 'created', 'last_activity', 'tokens', 'agent_running')])
  65. def _context(authority, ref, live, policy, agent, arg) -> str:
  66. """Main's ``_format_live_context_output``: counts by role, model/provider, then the category
  67. table off the live agent (``render_context_breakdown_lines(grid=False)``), else the stored usage."""
  68. stored = _context_id(authority, ref)
  69. rows = []
  70. with contextlib.suppress(Exception):
  71. rows = authority.db.get_messages_as_conversation(stored, include_ancestors=True, include_row_ids=True)
  72. from gateway.session_controls import display_projection
  73. messages = display_projection(rows, authority.profile_id) if rows else []
  74. breakdown: list[str] = []
  75. if agent is not None:
  76. with contextlib.suppress(Exception):
  77. from agent.context_breakdown import (
  78. compute_context_details, compute_session_context_breakdown, render_context_breakdown_lines)
  79. payload = compute_session_context_breakdown(agent, _model_history(authority.db, stored))
  80. details = None
  81. if arg.strip().lower() in {'all', 'full', 'details'}:
  82. with contextlib.suppress(Exception):
  83. details = compute_context_details(agent)
  84. breakdown = render_context_breakdown_lines(payload, details=details, grid=False)
  85. lines = [f'Conversation: {len(messages)} messages' if messages else 'Conversation is empty (no messages yet).']
  86. roles = Counter(str(msg.get('role') or 'unknown') for msg in messages)
  87. lines.append(' ' + ', '.join(f'{r}: {roles.get(r, 0)}' for r in ('user', 'assistant', 'tool', 'system')))
  88. if model := (getattr(agent, 'model', '') if agent is not None else '') or policy.current_model(authority):
  89. lines.append(f'Model: {model}')
  90. # Main read the provider off the compute-host metadata mirror, which a local session never
  91. # filled, so its TUI always printed ``auto`` here; kept as main's text.
  92. lines.append('Provider: auto')
  93. if breakdown:
  94. return '\n'.join([*lines, '', *breakdown])
  95. compressor = getattr(agent, 'context_compressor', None) if agent is not None else None
  96. used, maximum = (int(getattr(compressor, name, 0) or 0) for name in ('last_prompt_tokens', 'context_length'))
  97. if used and maximum:
  98. lines.append(f'Context usage: {used:,} / {maximum:,} tokens ({used / maximum * 100:.1f}%)')
  99. elif used:
  100. lines.append(f'Context usage: {used:,} tokens')
  101. if count := int(getattr(compressor, 'compression_count', 0) or 0):
  102. lines.append(f'Compressions: {count:,}')
  103. return '\n'.join(lines)
  104. async def run_tui_read(authority, ref, live, policy, render, arg) -> dict:
  105. """Render off the owner loop, inside the owning profile and the session's workspace."""
  106. from gateway.session_authorities import owner_scope
  107. from gateway.session_commands import _session_workspace
  108. agent = authority.agent(ref)
  109. def build():
  110. with owner_scope(authority), _session_workspace(authority.runner, live.source):
  111. return render(authority, ref, live, policy, agent, arg)
  112. return {'type': 'exec', 'output': await asyncio.to_thread(build)}
  113. async def context_breakdown(connection, ref, params) -> dict:
  114. """``session.context_breakdown`` on the owner: the category estimate (``compute_session_context_
  115. breakdown``) and context-file manifest of this session's resident agent over its compression tip's
  116. model history, built in the session's workspace. With no resident agent (not run since the gateway
  117. started, or reaped): the same categories from what the session froze (``_stored_breakdown``) beside
  118. its stored usage."""
  119. from gateway.session_busy_controls import authorize
  120. from hermes_state_runtime import RuntimeStoreError
  121. if set(params) - {'session_id', 'profile'} or not isinstance(ref.session_id, str) or not ref.session_id:
  122. raise RuntimeStoreError('invalid_params')
  123. authorize(connection, ref, params, 'session:read')
  124. authority = connection.authority
  125. live = authority.sessions.get(ref.session_id)
  126. if live is None:
  127. raise RuntimeStoreError('not_found')
  128. agent = authority.agent(ref)
  129. usage = None
  130. if agent is None:
  131. from gateway.session_usage import stats
  132. usage = await stats(connection, ref, {'session_id': ref.session_id})
  133. usage = usage.get('usage') if isinstance(usage.get('usage'), dict) else usage
  134. from gateway.session_authorities import owner_scope
  135. from gateway.session_commands import _session_workspace
  136. def build():
  137. from agent.context_breakdown import compute_session_context_breakdown
  138. from agent.context_file_sources import context_file_sources_for_agent
  139. with owner_scope(authority), _session_workspace(authority.runner, live.source):
  140. tip = _context_id(authority, ref)
  141. # Agentless, the categories come from the frozen prompt and tools pin, which a compute worker
  142. # can rewrite (session.prompt / session.tools) without moving the transcript.
  143. row = {} if agent is not None else authority.db.get_session(tip) or {}
  144. # A revision probe is two indexed reads; the rebuild walks the whole transcript and the
  145. # system prompt. Reuse the payload while neither the transcript nor the agent moved.
  146. key = (id(agent), _agent_context_marks(agent), authority.db.conversation_revision(tip),
  147. row.get('system_prompt'), row.get('tool_names'))
  148. cached = live.context_breakdown
  149. if cached is not None and cached[0] == key:
  150. return dict(cached[1])
  151. conversation = _model_history(authority.db, tip)
  152. if agent is None:
  153. payload = _stored_breakdown(row, conversation, usage)
  154. else:
  155. payload = compute_session_context_breakdown(agent, conversation)
  156. payload['context_files'] = context_file_sources_for_agent(agent)
  157. live.context_breakdown = (key, payload)
  158. return dict(payload)
  159. return await asyncio.to_thread(build)
  160. def _stored_breakdown(row, conversation, usage) -> dict:
  161. """``stored_context_breakdown`` over the session row's frozen system prompt and tools[] pin, beside its
  162. stored usage. Tools are measured as the next agent restores them (``pinned_tool_defs``)."""
  163. from agent.context_breakdown import stored_context_breakdown
  164. from tools.mcp_tool_agent import pinned_tool_defs
  165. breakdown = {'categories': [], 'estimated_total': 0}
  166. # No prompt until an agent first built one: nothing to measure yet, the contract's pre-build shape.
  167. if prompt := row.get('system_prompt'):
  168. try:
  169. pin = json.loads(row.get('tool_names') or 'null')
  170. except (TypeError, ValueError):
  171. pin = None # a pin hash whose row was swept resolves to itself
  172. breakdown = stored_context_breakdown(prompt, pinned_tool_defs(pin), conversation)
  173. return {**breakdown, 'context_max': int(usage.get('context_max') or 0),
  174. 'context_percent': int(usage.get('context_percent') or 0), 'context_used': int(usage.get('context_used') or 0),
  175. 'context_estimated': bool(usage.get('context_estimated', False)),
  176. 'context_source': str(usage.get('context_source') or 'provider_usage'), 'model': str(usage.get('model') or '')}
  177. def _agent_context_marks(agent) -> tuple:
  178. """What the breakdown reads off the agent besides the transcript: model, toolset size, window,
  179. measured prompt tokens, compressions and the usage anchors."""
  180. comp = getattr(agent, 'context_compressor', None)
  181. return (getattr(agent, 'model', None), getattr(agent, 'provider', None), len(getattr(agent, 'tools', None) or ()),
  182. *(getattr(comp, name, None) for name in ('context_length', 'last_prompt_tokens', 'compression_count')),
  183. repr(getattr(agent, '_turn_base_usage_anchor', None)), repr(getattr(agent, '_usage_anchor', None)))