context_compressor_summary.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Summary-hook dispatch and cancellation rollback for context compression."""
  2. from __future__ import annotations
  3. import inspect
  4. from typing import TYPE_CHECKING, Any, Dict, List, Optional
  5. from agent.auxiliary_client import AuxiliaryExplicitCancellation
  6. if TYPE_CHECKING:
  7. from agent.context_compressor import _HandoffScan
  8. def _accepts_keyword_argument(callable_obj: Any, name: str) -> bool:
  9. """Return whether an inspectable callable accepts ``name`` as a keyword."""
  10. try:
  11. parameters = inspect.signature(callable_obj).parameters
  12. except (TypeError, ValueError):
  13. return False
  14. if any(parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters.values()):
  15. return True
  16. parameter = parameters.get(name)
  17. return parameter is not None and parameter.kind in (
  18. inspect.Parameter.POSITIONAL_OR_KEYWORD,
  19. inspect.Parameter.KEYWORD_ONLY,
  20. )
  21. class SummaryDispatchMixin:
  22. def _apply_summary_route(self, call_kwargs: dict, pinned: Optional[dict[str, Any]] = None) -> None:
  23. """Pin the summary route onto ``call_kwargs`` for both compression entry points.
  24. After a fallback, an omitted route is NOT "use the main model": ``call_llm`` re-resolves
  25. ``auxiliary.compression`` from config — the model that just failed — and a second fallback
  26. is refused, so every later attempt aborts (#123362). Name the main runtime explicitly.
  27. A stall-fallback ``pinned`` route replaces the whole route: merged over the main runtime, the
  28. main api_key/base_url would ride into the fallback entry's call (to its host, or instead of it).
  29. The main route is complete even with empty fields: an authoritative task keeps compression
  30. policy but never fills an unset endpoint/key/wire mode from the failed aux route (#130895).
  31. """
  32. if pinned:
  33. # Clear first: a keyless pin (local server) resolves its own credential, never the main one.
  34. for key in ("provider", "model", "base_url", "api_key", "api_mode"):
  35. call_kwargs.pop(key, None)
  36. call_kwargs.update(pinned)
  37. return
  38. if self.summary_model:
  39. call_kwargs["model"] = self.summary_model
  40. return
  41. if not getattr(self, "_summary_model_fallen_back", False):
  42. return
  43. from agent.auxiliary_task_config import authoritative_auxiliary_task
  44. call_kwargs["task"] = authoritative_auxiliary_task(call_kwargs.get("task") or "compression")
  45. for key, value in (
  46. ("provider", self.provider),
  47. ("model", self.model),
  48. ("base_url", self.base_url),
  49. ("api_key", self.api_key),
  50. ("api_mode", getattr(self, "api_mode", "")),
  51. ):
  52. if value:
  53. call_kwargs[key] = value
  54. def _summarize_window(
  55. self, messages: list[dict[str, Any]], turns_to_summarize: list[dict[str, Any]], scan: _HandoffScan,
  56. focus_topic: Optional[str], memory_context: str, bypass_cooldown: bool,
  57. ) -> Optional[str]:
  58. """Run the summary LLM; a cancellation rolls back the handoff scan's self-heal mutation first.
  59. A deterministic pin (repeated stall, #112420) skips the LLM: ``None`` lets Phase 3 insert the static
  60. fallback summary, or abort under ``abort_on_summary_failure`` exactly like a failed summary call."""
  61. from agent.context_compressor import take_deterministic_summary_pin
  62. if take_deterministic_summary_pin():
  63. # A detached stale attempt must not stamp error state or mutate the shared telemetry dict
  64. # the fallback owns; unwind as a cancellation before any write lands.
  65. from agent.conversation_compression import _raise_if_stale_attempt
  66. _raise_if_stale_attempt(self)
  67. # Surfaces through the fallback summary's reason line and the host's one-shot user warning.
  68. self._last_summary_error = (
  69. "summary model stalled on every route; deterministic fallback summary inserted"
  70. )
  71. telemetry = getattr(self, "_active_compression_telemetry", None)
  72. if isinstance(telemetry, dict):
  73. telemetry["failure_class"] = "stall_deterministic_fallback"
  74. return None
  75. # Focus-topic derivation scans user turns; only pay when a summary is generated.
  76. summary_kwargs: dict[str, Any] = {
  77. "focus_topic": focus_topic or self._derive_auto_focus_topic(messages),
  78. "memory_context": memory_context,
  79. }
  80. if _accepts_keyword_argument(self._generate_summary, "bypass_cooldown"):
  81. summary_kwargs["bypass_cooldown"] = bypass_cooldown
  82. try:
  83. return self._generate_summary(turns_to_summarize, **summary_kwargs)
  84. except AuxiliaryExplicitCancellation:
  85. # Cancellation is a true no-op: restore the scan's mutation before the exception escapes.
  86. # Guard by THIS attempt's ownership: a detached stale attempt (a fallback already claimed
  87. # and is working the compressor) must not revert summary state the newer attempt advanced.
  88. # The caller's own generation rides a ContextVar because shared compressor attributes can
  89. # only name the current owner, never the caller's attempt. Working-attempt comparison (not
  90. # the entry generation) so a no-op claim does not suppress the owning attempt's rollback.
  91. from agent.conversation_compression import _caller_attempt_is_current
  92. if _caller_attempt_is_current(self):
  93. self._previous_summary = scan.previous_summary_before
  94. self._summary_has_user_turn = scan.has_user_turn_before
  95. raise