conversation_compression_feasibility.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """When the compression feasibility probe runs: after a main-runtime change, once a request first reaches
  2. the smallest summariser window, and again when a warm agent meets a compression route other than the one
  3. it probed. The probe itself is ``check_compression_model_feasibility`` in ``agent.conversation_compression``."""
  4. from __future__ import annotations
  5. import logging
  6. from typing import Any
  7. logger = logging.getLogger("agent.conversation_compression")
  8. def _probe(agent: Any) -> None:
  9. from agent.conversation_compression import check_compression_model_feasibility
  10. check_compression_model_feasibility(agent)
  11. def _mark_checked(agent: Any, route: tuple) -> None:
  12. """Latch the probe as done for *route*, the compression route read before the probe ran: an edit that
  13. lands during the probe is then a moved route on the next request, not a measured one."""
  14. agent._compression_feasibility_checked = True
  15. agent._compression_feasibility_route = route
  16. def compression_route_identity() -> tuple:
  17. """The configured compression route a probe measures: the task's provider, model, URL and context
  18. length, the URL of a ``providers:`` / ``custom_providers:`` entry the task names, and the context pin
  19. a provider entry sets for the compression model at the route's URL. A config this
  20. cannot read yields ``("unreadable",)``: the main request carries on and the probe reports the problem."""
  21. try:
  22. return _route_identity()
  23. except Exception:
  24. logger.debug("Compression route unreadable for the feasibility latch", exc_info=True)
  25. return ("unreadable",)
  26. def _route_identity() -> tuple:
  27. from agent.auxiliary_task_config import _get_auxiliary_task_config
  28. cfg = _get_auxiliary_task_config("compression")
  29. route = tuple(str(cfg.get(k) or "") for k in ("provider", "model", "base_url", "context_length"))
  30. provider = route[0].strip()
  31. from hermes_cli.config import get_custom_provider_context_length
  32. if route[2]:
  33. url, model, named = route[2], route[1], ()
  34. elif not provider or provider in ("auto", "main", "custom"):
  35. return route
  36. else:
  37. from hermes_cli.runtime_provider_custom import _get_named_custom_provider
  38. entry = _get_named_custom_provider(provider, resolve_credentials=False) or {}
  39. url, model = str(entry.get("base_url") or ""), route[1] or str(entry.get("model") or "")
  40. named = (url, str(entry.get("model") or ""))
  41. # The context pin the probe reads for this model at this URL (a matching entry's per-model, else entry-level).
  42. pin = get_custom_provider_context_length(model=model, base_url=url) if url and model else None
  43. return route + named + (str(pin or ""),)
  44. def _drop_stale_ceiling(agent: Any) -> None:
  45. """A warm agent met a compression route other than the one it probed: the ceiling it installed and
  46. any failure cooldown belong to the old summariser, so the trigger returns to the main window and the
  47. cooldown is cleared (in memory and in the session row) before the new probe."""
  48. agent._compression_feasibility_checked = False
  49. compressor = agent.context_compressor
  50. clear_cooldown = getattr(compressor, "_clear_compression_failure_cooldown", None)
  51. if callable(clear_cooldown):
  52. clear_cooldown()
  53. if getattr(compressor, "_aux_context_ceiling", None) is not None:
  54. compressor._aux_context_ceiling = None
  55. compressor.update_model(compressor.model, compressor.context_length, base_url=compressor.base_url,
  56. api_key=compressor.api_key, provider=compressor.provider,
  57. api_mode=compressor.api_mode)
  58. def revalidate_compression_feasibility(agent: Any) -> None:
  59. """Re-run the aux feasibility probe after the main runtime changed (model switch, fallback activation,
  60. primary restore). ``update_model()`` already voided the previous ceiling; probing now clamps the trigger
  61. before the first compaction on the new window rather than after it (#114707). A probe failure leaves the
  62. latch unset so the lazy probe at the next compaction re-raises hard rejections."""
  63. agent._compression_feasibility_checked = False
  64. if not getattr(agent, "context_compressor", None):
  65. return
  66. route = compression_route_identity()
  67. try:
  68. _probe(agent)
  69. except Exception as exc:
  70. logger.debug("Compression feasibility re-check deferred to the next compaction: %s", exc, exc_info=True)
  71. return
  72. _mark_checked(agent, route)
  73. def ensure_compression_feasibility_checked(agent: Any, estimated_tokens: int) -> None:
  74. """Run the deferred aux feasibility probe once a request first reaches ``MINIMUM_CONTEXT_LENGTH`` — the
  75. smallest window any summariser may have — so an aux clamp lands before the first compaction fires on the
  76. main-window threshold instead of after it (#114707). Below that size no summariser can be too small, so
  77. short sessions keep the probe-free cold start (#28957). A probe failure leaves the latch unset for the
  78. lazy probe in ``compress_context`` to re-raise hard rejections. A warm agent whose compression route moved
  79. since its last probe (side tasks follow the profile's config.yaml) probes again."""
  80. if not getattr(agent, "context_compressor", None):
  81. return
  82. route = compression_route_identity()
  83. if getattr(agent, "_compression_feasibility_checked", False):
  84. probed = getattr(agent, "_compression_feasibility_route", None)
  85. if probed is None or route == probed:
  86. return
  87. _drop_stale_ceiling(agent)
  88. from agent.model_metadata import MINIMUM_CONTEXT_LENGTH
  89. if int(estimated_tokens or 0) < MINIMUM_CONTEXT_LENGTH:
  90. return
  91. try:
  92. _probe(agent)
  93. except Exception as exc:
  94. logger.debug("Compression feasibility probe deferred to the first compaction: %s", exc, exc_info=True)
  95. return
  96. _mark_checked(agent, route)
  97. def lazy_compression_feasibility_check(agent: Any) -> None:
  98. """The probe ``compress_context`` runs when nothing probed yet, or the route moved since the probe
  99. that recorded one; a hard rejection propagates to the caller. A latch with no recorded route (set
  100. before route tracking, or by a caller marking the agent checked) is trusted as it always was."""
  101. route = compression_route_identity()
  102. if getattr(agent, "_compression_feasibility_checked", False):
  103. probed = getattr(agent, "_compression_feasibility_route", None)
  104. if probed is None or route == probed:
  105. return
  106. _drop_stale_ceiling(agent)
  107. _probe(agent)
  108. _mark_checked(agent, route)