session_mutation_compress.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. """Summary preparation has no canonical store; publication owns the only write."""
  2. import asyncio
  3. import json
  4. import logging
  5. from pathlib import Path
  6. from types import SimpleNamespace
  7. from hermes_state_runtime import RuntimeStoreError
  8. logger = logging.getLogger(__name__)
  9. def compress_request(payload):
  10. """``session.mutate(compress)`` payload → the shared ``CompressRequest``.
  11. A ``focus``-only payload is the raw ``/compress`` argument string (Ink/Desktop send it that
  12. way, like ``session.compress`` always has), so it goes through the parser every native surface
  13. uses: ``--preview`` stays read-only and ``here [N]`` stays a boundary instead of becoming a
  14. focus topic. ``--aggressive`` has no canonical implementation and is refused before any write.
  15. """
  16. from agent.conversation_compression_manual import CompressRequest, parse_compress_args
  17. if set(payload) <= {'focus'}:
  18. request = parse_compress_args(payload.get('focus', ''))
  19. else:
  20. request = CompressRequest(preview=payload.get('preview', False), partial=payload.get('partial', False),
  21. keep_last=payload.get('keep_last', 2), focus_topic=payload.get('focus'))
  22. if request.aggressive:
  23. raise RuntimeStoreError('unsupported_compress_options')
  24. return request
  25. async def prepare_compress(authority, live, payload, prepared):
  26. from gateway.session_policy import restore_policy, policy_scope
  27. from agent.context_compressor import ContextCompressor
  28. from agent.agent_init import _parse_config_int
  29. from agent.model_metadata import estimate_request_tokens_rough
  30. from hermes_cli.partial_compress import (
  31. rejoin_compressed_head_and_tail, split_history_for_partial_compress, summarize_compress_preview)
  32. from utils import is_truthy_value
  33. request = compress_request(payload)
  34. snapshot = prepared['snapshot']
  35. policy = restore_policy(snapshot['receipt']['policy'])
  36. config = policy.config(authority)
  37. options = config.get('compression') or {}
  38. if options.get('checkpoint_required'):
  39. raise RuntimeStoreError('compression_checkpoint_required')
  40. model, runtime = authority.runner._resolve_session_agent_runtime(source=live.source, session_key=live.route)
  41. if runtime.get('api_mode') == 'codex_app_server':
  42. raise RuntimeStoreError('runtime_coordination_required')
  43. history = authority.db.get_messages_as_conversation(snapshot['target'])
  44. messages = [m for m in history if m.get('role') in {'user', 'assistant', 'tool'}]
  45. head, tail = messages, []
  46. if request.partial:
  47. head, tail = split_history_for_partial_compress(messages, request.keep_last)
  48. if not tail: # degenerate split: nothing to keep verbatim → full compression
  49. head = messages
  50. if request.preview:
  51. # Read-only: the report is the whole result; the caller commits nothing.
  52. report = summarize_compress_preview(messages, request.partial, request.keep_last, request.focus_topic,
  53. estimate_request_tokens_rough(messages))
  54. return {'status': 'preview', 'lines': report['lines'], 'head_count': report['head_count'],
  55. 'tail_count': report['tail_count'], 'message_count': report['total']}
  56. # The session's resident agent owns its memory providers; give them the same pre-compress
  57. # turn (and the same best-effort contract) the in-process compressor does. No resident agent
  58. # (cold session, or a managed worker that runs out of process) means no provider to call.
  59. cached_agent = getattr(authority.runner, '_cached_agent_for', None)
  60. resident = cached_agent(live.route) if callable(cached_agent) else None
  61. def summarize():
  62. with policy_scope(policy, authority=authority):
  63. from agent.conversation_compression import _pre_compress_memory_context
  64. memory_context = _pre_compress_memory_context(resident, head, False) if resident is not None else ''
  65. engine = _plugin_engine(authority, policy, config, model, runtime, resident, snapshot['target'])
  66. if engine is not None:
  67. try:
  68. return _engine_compress(engine, head, tail, messages, request, memory_context), engine
  69. except BaseException:
  70. if resident is None:
  71. _end_engine(engine, snapshot['target'], messages)
  72. raise
  73. compressor = ContextCompressor(model, base_url=runtime.get('base_url') or '',
  74. api_key=runtime.get('api_key') or '', provider=runtime.get('provider') or '',
  75. api_mode=runtime.get('api_mode') or '', quiet_mode=True, abort_on_summary_failure=True,
  76. protect_first_n=options.get('protect_first_n', 3), protect_last_n=options.get('protect_last_n', 20),
  77. min_tail_user_messages=max(1, _parse_config_int(options.get('min_tail_user_messages', 1), 1)),
  78. custom_providers=config.get('custom_providers'))
  79. compressed = compressor.compress(json.loads(json.dumps(head)), force=True,
  80. focus_topic=request.focus_topic, memory_context=memory_context)
  81. if not any(m.get('_compressed_summary') for m in compressed):
  82. return None, compressor
  83. return rejoin_compressed_head_and_tail(compressed, tail), compressor
  84. compressed, compressor = await asyncio.to_thread(summarize)
  85. # The same report every native /compress prints ("Compressed: N → M messages").
  86. from agent.manual_compression_feedback import summarize_manual_compression
  87. if compressed is None:
  88. if resident is None and not isinstance(compressor, ContextCompressor):
  89. await asyncio.to_thread(_end_engine, compressor, snapshot['target'], messages)
  90. # Nothing removable (a short session keeps every message): main's read-only
  91. # "No changes from compression" report, estimated like main's (system prompt + tools).
  92. row = None if resident is not None else authority.db.get_session(snapshot['target']) # resident: row unused
  93. tokens = _request_tokens(resident, messages, row)
  94. return {'status': 'noop', 'summary': summarize_manual_compression(
  95. messages, messages, tokens, tokens, compression_state=compressor)}
  96. summary = summarize_manual_compression(messages, compressed, estimate_request_tokens_rough(messages),
  97. estimate_request_tokens_rough(compressed), compression_state=compressor)
  98. from agent.model_metadata import estimate_messages_tokens_rough
  99. hook = None if policy.safe_mode else { # same rough basis as the agent path's compaction record
  100. 'before_tokens': estimate_messages_tokens_rough(messages),
  101. 'after_tokens': estimate_messages_tokens_rough(compressed), 'messages_removed': len(messages) - len(compressed)}
  102. # The committed boundary is announced to the engine that compressed (``on_session_start``
  103. # with boundary_reason="compression"), as the agent path does after its own commit.
  104. engine_owner = SimpleNamespace(context_compressor=compressor, platform=_platform(live.source),
  105. _gateway_session_key=live.route,
  106. owned=resident is None and not isinstance(compressor, ContextCompressor))
  107. return dict(prepared, messages=compressed, summary=summary, post_compaction=hook, engine_owner=engine_owner,
  108. in_place=is_truthy_value(options.get('in_place'), default=True))
  109. def _engine_compress(engine, head, tail, messages, request, memory_context):
  110. """One forced ``compress()`` on a plugin engine; None when it changed nothing or would grow the
  111. conversation (the agent path refuses a growing result the same way)."""
  112. from agent.conversation_compression import _supported_compression_kwargs
  113. from agent.model_metadata import estimate_messages_tokens_rough, estimate_request_tokens_rough
  114. from hermes_cli.partial_compress import rejoin_compressed_head_and_tail
  115. engine._last_compress_refused_would_grow = False
  116. compressed = engine.compress(json.loads(json.dumps(head)), **_supported_compression_kwargs(
  117. engine.compress, current_tokens=estimate_request_tokens_rough(head),
  118. focus_topic=request.focus_topic, force=True, memory_context=memory_context))
  119. if compressed == head:
  120. return None
  121. compressed = rejoin_compressed_head_and_tail(compressed, tail)
  122. if estimate_messages_tokens_rough(compressed) > estimate_messages_tokens_rough(messages):
  123. engine._last_compress_refused_would_grow = True # read by the /compress report
  124. return None
  125. return compressed
  126. def _end_engine(engine, session_id, messages):
  127. try:
  128. engine.on_session_end(session_id, messages)
  129. except Exception: # health: allow BLE001 -- releasing a one-off engine must not fail /compress
  130. logger.warning('context engine on_session_end failed', exc_info=True)
  131. def _configured_context_length(config, model, provider, base_url, custom_providers):
  132. """The configured window agent init hands a plugin engine: ``model.context_length`` when it
  133. describes this runtime, else the matching custom provider's entry."""
  134. from agent.agent_init import _scope_context_length_to_default_runtime
  135. from hermes_cli.config import get_custom_provider_context_length
  136. model_cfg = config.get('model') if isinstance(config.get('model'), dict) else {}
  137. try:
  138. configured = int(model_cfg['context_length']) if model_cfg.get('context_length') is not None else None
  139. except (TypeError, ValueError):
  140. configured = None
  141. if configured is not None:
  142. route = SimpleNamespace(model=model, provider=provider, base_url=base_url)
  143. configured = _scope_context_length_to_default_runtime(
  144. route, config, model_cfg, custom_providers, configured, base_url)
  145. if configured is None and custom_providers:
  146. configured = get_custom_provider_context_length(
  147. model=model, base_url=base_url, custom_providers=custom_providers)
  148. return int(configured) if configured else None
  149. def _plugin_engine(authority, policy, config, model, runtime, resident, target):
  150. """The session's configured plugin context engine, or None for the built-in compressor.
  151. A resident agent's engine is the one bound to this session. A cold or managed-worker session
  152. gets the engine its frozen policy names, selected and bound the way agent init does, so a
  153. plugin engine compresses its own history instead of having it rewritten underneath it."""
  154. from agent.context_compressor import ContextCompressor
  155. if resident is not None:
  156. engine = getattr(resident, 'context_compressor', None)
  157. return None if engine is None or isinstance(engine, ContextCompressor) else engine
  158. if policy.safe_mode:
  159. return None
  160. from agent.agent_init import _select_context_engine
  161. engine = _select_context_engine(config)
  162. if engine is None:
  163. return None
  164. from agent.model_metadata import get_model_context_length
  165. thresholds = (config.get('compression') or {}).get('model_thresholds')
  166. if isinstance(thresholds, dict) and thresholds: # before update_model, like agent init
  167. engine.model_thresholds = {str(k): float(v) for k, v in thresholds.items()}
  168. from hermes_cli.config import get_compatible_custom_providers
  169. base_url, api_key = runtime.get('base_url') or '', runtime.get('api_key') or ''
  170. provider = runtime.get('provider') or ''
  171. providers = get_compatible_custom_providers(config)
  172. context_length = get_model_context_length(
  173. model, base_url=base_url, api_key=api_key, provider=provider, custom_providers=providers,
  174. config_context_length=_configured_context_length(config, model, provider, base_url, providers))
  175. engine.update_model(model=model, context_length=context_length, base_url=base_url, api_key=api_key,
  176. provider=provider, api_mode=runtime.get('api_mode') or '')
  177. bind = getattr(engine, 'bind_session_state', None)
  178. if callable(bind):
  179. bind(session_db=authority.db, session_id=target)
  180. return engine
  181. def _platform(source):
  182. platform = getattr(source, 'platform', None)
  183. return getattr(platform, 'value', platform) or 'cli'
  184. async def finish_compress_engine(authority, prepared, result):
  185. """After the receipt transaction: a committed ``/compress`` (``result``) gets the agent path's
  186. post-commit notification, the engine's compression boundary plus the relay/metrics segment
  187. hand-off. A cold session's engine was built for this compression only, so it gets the session
  188. end a resident agent's boundary eviction gives, committed or not. An exact retry (no
  189. ``engine_owner``) notifies nothing."""
  190. from agent.conversation_compression import _notify_context_engine_compression_complete
  191. from gateway.run import _async_profile_runtime_scope
  192. owner = (prepared or {}).get('engine_owner')
  193. if owner is None or (result is None and not owner.owned):
  194. return
  195. def notify():
  196. if result is not None:
  197. _notify_context_engine_compression_complete(owner, new_session_id=result['target_session_id'],
  198. old_session_id=result['previous_target_session_id'])
  199. if owner.owned:
  200. target = result['target_session_id'] if result is not None else prepared['snapshot']['target']
  201. _end_engine(owner.context_compressor, target, prepared['messages'])
  202. try:
  203. async with _async_profile_runtime_scope(Path(authority.profile_id)):
  204. await asyncio.to_thread(notify)
  205. except Exception: # health: allow BLE001 -- the compaction is committed; an engine failure must not fail the reply
  206. logger.warning('context engine compression notification failed', exc_info=True)
  207. async def fire_post_compaction_hook(authority, prepared, result):
  208. """``post_compaction`` for a committed ``/compress``: this path summarizes outside the agent's
  209. ``compress_context``, so it fires here, after the commit, in the session's profile."""
  210. from agent.compaction_events import fire_post_compaction
  211. from gateway.run import _async_profile_runtime_scope
  212. target, previous = result['target_session_id'], result['previous_target_session_id']
  213. try:
  214. async with _async_profile_runtime_scope(Path(authority.profile_id)):
  215. await asyncio.to_thread(
  216. fire_post_compaction, session_id=target, old_session_id='' if previous == target else previous,
  217. kind='summarize', reason='manual', **prepared['post_compaction'])
  218. except Exception: # the compaction is committed; a plugin-side failure must not fail the reply
  219. logger.warning('post_compaction hook delivery failed', exc_info=True)
  220. def _request_tokens(agent, messages, row=None):
  221. """Main's /compress request estimate: the agent's system prompt and tool schemas plus the messages.
  222. No resident agent (a cold session): the prompt and tools[] pin the session row stores, which the
  223. next agent restores and sends, so the figure matches what main's resident agent reported."""
  224. from agent.model_metadata import estimate_request_tokens_rough
  225. prompt = getattr(agent, '_cached_system_prompt', '') or ''
  226. tools = getattr(agent, 'tools', None) or None
  227. if agent is None and isinstance(row, dict):
  228. prompt = row.get('system_prompt') or ''
  229. try:
  230. pin = json.loads(row.get('tool_names') or 'null')
  231. except (TypeError, ValueError):
  232. pin = None # a pin hash whose row was swept resolves to itself
  233. from tools.mcp_tool_agent import pinned_tool_defs
  234. tools = pinned_tool_defs(pin) or None
  235. return estimate_request_tokens_rough(messages, system_prompt=prompt, tools=tools)