From 3ac22a309bafa2ee754ca62dc7a724859e882b02 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Thu, 24 Sep 2026 17:58:13 +0530 Subject: [PATCH] refactor(agent): module-level _refund_api_call import; int verdict count (#77305) turn_context_compaction imports neither conversation_loop nor turn_empty_response, so the function-local import is not guarding a cycle. The helper's docstring now also covers the provider-switch fallback hop that reached the provider. --- agent/turn_context_compaction.py | 3 ++- agent/turn_empty_response.py | 2 +- agent/turn_final_response.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/agent/turn_context_compaction.py b/agent/turn_context_compaction.py index 939ecaa92d..54678deb4f 100644 --- a/agent/turn_context_compaction.py +++ b/agent/turn_context_compaction.py @@ -101,7 +101,8 @@ def _apply_grown_window(agent: Any, compressor: Any, grown: int) -> None: def _refund_api_call(agent: Any, api_call_count: int) -> int: - """A pass that never reached the provider refunds the call count and budget.""" + """Refund the call count and iteration budget for a pass that should not consume it: + one that never reached the provider (preflight) or a provider-switch fallback hop.""" # Host progress-aware timeout (#98722, salvaged from #98741): this preflight iteration never reached the # provider. Refund its provisional call/budget exactly like a successful pre-API compaction, then stop # before the unchanged oversized request reaches the provider — its overflow error would only invoke diff --git a/agent/turn_empty_response.py b/agent/turn_empty_response.py index 17ba5da230..92339b18d4 100644 --- a/agent/turn_empty_response.py +++ b/agent/turn_empty_response.py @@ -16,6 +16,7 @@ from typing import Any, Dict, List, Optional from agent import empty_response_guard as _empty_guard from agent.message_metadata import append_message +from agent.turn_context_compaction import _refund_api_call from agent.turn_failure_copy import site_copy from agent.turn_recovery import interruptible_backoff_sleep @@ -283,7 +284,6 @@ def recover_empty_response( _preflight_compression_blocked = False # The fallback hop is a provider switch, not a model turn: refund the empty # call so a mid-turn fallback doesn't eat the iteration budget (#77305). - from agent.turn_context_compaction import _refund_api_call api_call_count = _refund_api_call(agent, api_call_count) return _verdict("continue") diff --git a/agent/turn_final_response.py b/agent/turn_final_response.py index 421a1bc5b9..84fba083de 100644 --- a/agent/turn_final_response.py +++ b/agent/turn_final_response.py @@ -40,7 +40,7 @@ class FinalResponseVerdict: length_continue_retries: Any _pending_verification_response: Any _pending_verification_response_previewed: Any - api_call_count: Any + api_call_count: int result: Optional[Dict[str, Any]] = None