turn_preflight_gate.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Pre-API pressure gate for the conversation turn loop: the Ollama runtime-context floor,
  2. the provider-overflow re-check arming, the insufficient-progress blocker (compares fully
  3. assembled requests, not raw ``messages``) and the call into
  4. ``turn_preflight.run_preflight_compression``. Nothing here imports
  5. ``agent.conversation_loop`` at module level (cycle)."""
  6. from __future__ import annotations
  7. import logging
  8. from contextlib import suppress
  9. from typing import Any
  10. from agent.message_metadata import append_message
  11. from agent.turn_context import _compression_warrants_another_preflight_pass
  12. from agent.turn_preflight import PreflightGateVerdict, run_preflight_compression
  13. logger = logging.getLogger("agent.conversation_loop")
  14. def run_preflight_gate(
  15. agent: Any, *, request_pressure_tokens: Any, _moa_prepared_request: Any,
  16. pending_moa_prepared_request: Any, messages: Any, system_message: Any, user_message: Any,
  17. active_system_prompt: Any, conversation_history: Any, api_call_count: Any,
  18. compression_attempts: Any, max_compression_attempts: Any, effective_task_id: Any,
  19. final_response: Any, failed: Any, _turn_exit_reason: Any, _compression_timeout_exhausted: Any,
  20. _preflight_compression_blocked: Any, _provider_overflow_recovery_pending: Any,
  21. _last_preflight_pressure: Any,
  22. ) -> PreflightGateVerdict:
  23. """Run the pre-API guard chain in the original order. ``_last_preflight_pressure`` is
  24. consumed here (set to None) and re-armed only by a compression pass, so a blocked
  25. preflight never compares against a stale figure."""
  26. from agent.conversation_loop import _ollama_context_limit_error
  27. v = PreflightGateVerdict(
  28. action="fallthrough", pending_moa_prepared_request=pending_moa_prepared_request,
  29. messages=messages, active_system_prompt=active_system_prompt,
  30. conversation_history=conversation_history, api_call_count=api_call_count,
  31. compression_attempts=compression_attempts, final_response=final_response, failed=failed,
  32. _turn_exit_reason=_turn_exit_reason,
  33. _compression_timeout_exhausted=_compression_timeout_exhausted,
  34. _preflight_compression_blocked=_preflight_compression_blocked,
  35. _provider_overflow_recovery_pending=_provider_overflow_recovery_pending,
  36. _last_preflight_pressure=None,
  37. )
  38. _runtime_context_error = _ollama_context_limit_error(agent, request_pressure_tokens)
  39. if _runtime_context_error:
  40. v.final_response = _runtime_context_error
  41. v.failed = True
  42. v._turn_exit_reason = "ollama_runtime_context_too_small"
  43. append_message(messages, {"role": "assistant", "content": v.final_response})
  44. agent._emit_diagnostic_status("❌ Ollama runtime context is too small for Hermes tool use")
  45. v.api_call_count -= 1
  46. agent._api_call_count = v.api_call_count
  47. with suppress(Exception):
  48. agent.iteration_budget.refund()
  49. v.action = "break"
  50. return v
  51. # Pre-API pressure check: tool results grow a turn and last_prompt_tokens lags
  52. # them. Mirror the turn-prologue guard chain: defer on noisy estimate, skip in
  53. # failure cooldown, then should_compress().
  54. _compressor = agent.context_compressor
  55. _preflight_threshold = int(getattr(_compressor, "threshold_tokens", 0) or 0)
  56. _provider_overflow_preflight = _provider_overflow_recovery_pending and (
  57. _preflight_threshold <= 0 or request_pressure_tokens >= _preflight_threshold
  58. )
  59. if _provider_overflow_recovery_pending and not _provider_overflow_preflight:
  60. # The outer-loop rebuild includes system prompt, request-only injections and
  61. # tool schemas; only that full request with output runway may be sent.
  62. v._provider_overflow_recovery_pending = False
  63. # Compare fully assembled requests, not raw ``messages`` (which omit
  64. # api_content, plugin injections, prefills, MoA context, ephemeral system text).
  65. if (
  66. _last_preflight_pressure is not None
  67. and request_pressure_tokens >= _preflight_threshold
  68. and not _compression_warrants_another_preflight_pass(
  69. _last_preflight_pressure, request_pressure_tokens, _preflight_threshold
  70. )
  71. ):
  72. # Stop proactive retries this turn without consuming the shared overflow-
  73. # recovery budget; the provider's error handler may still compact.
  74. v._preflight_compression_blocked = True
  75. logger.warning(
  76. "Pre-API compression made insufficient progress: ~%s -> "
  77. "~%s request tokens; skipping additional preflight passes",
  78. f"{_last_preflight_pressure:,}",
  79. f"{request_pressure_tokens:,}",
  80. )
  81. return run_preflight_compression(
  82. agent, v, compressor=_compressor, request_pressure_tokens=request_pressure_tokens,
  83. provider_overflow_preflight=_provider_overflow_preflight,
  84. # An anchored figure is real usage + delta: never deferred. Only a whole-context rough
  85. # estimate waits for the provider's count.
  86. defer_preflight=(
  87. (lambda _t: False) if getattr(agent, "_request_pressure_anchored", False)
  88. else getattr(_compressor, "should_defer_preflight_to_real_usage", lambda _t: False)
  89. ),
  90. moa_prepared_request=_moa_prepared_request, system_message=system_message,
  91. user_message=user_message, max_compression_attempts=max_compression_attempts,
  92. effective_task_id=effective_task_id,
  93. )