2a95791992
run_agent.py: delete the `# noqa: F401` re-export block (agent.process_bootstrap
OpenAI/_SafeWriter/_get_proxy_*, model_tools get_tool_definitions/
handle_function_call/check_toolset_requirements, FailoverReason,
_qwen_portal_headers/_routermint_headers, session_persistence names,
estimate_request_tokens_rough, ContextCompressor + friends, jittered_backoff,
prompt_builder names, message_sanitization names, tool_dispatch_helpers
names) — 41 names run_agent never used itself — and the `_STREAM_DIAG_HEADERS`
back-compat class alias (no in-tree reader). run_agent now imports only what
it uses (get_toolset_for_tool, is_local_endpoint, coalesce/uniquify tool-call
ids, cleanup_vm/get_active_env from terminal_tool_lifecycle).
agent/*: `_ra().X` late-binds that only reached a re-export now import the
defining module directly (agent_runtime_helpers -> process_bootstrap.OpenAI,
model_tools.handle_function_call, session_persistence._safe_session_filename_component;
agent_init -> model_tools.get_tool_definitions/check_toolset_requirements,
_lazy_headers("agent.client_lifecycle", ...) for qwen/routermint;
system_prompt -> agent.prompt_builder / model_tools directly, dropping its
own _ra() shim and the `_r` parameter threading). `_ra()` stays for
run_agent-resident names (logger, AIAgent, _hermes_home, _set_interrupt, ...).
toolsets.py: remove resolve_multiple_toolsets (shim-only, restored by
34abf954bd); tests/test_toolsets.py pins the same union behavior via
resolve_toolset over each name.
providers/__init__.py: drop the OMIT_TEMPERATURE re-export (no callers via the
package); ProviderProfile stays because __init__ uses it for annotations —
2 tests repointed to providers.base.
agent/iteration_budget.py: drop the "run_agent re-exports the class"
docstring pointer; 4 tests import IterationBudget from its home.
model_tools.py (arg_coercion names), agent/tool_executor.py, and
hermes_cli/cli_session_mixin.py repoints landed via a sibling commit on this
shared worktree.
Callers repointed: gateway/run.py, hermes_cli/cli_chat_turn_mixin.py,
hermes_cli/cli_tui_mixin.py, tui_gateway/session_workdir.py,
agent/transports/codex.py (one-line imports) + comment pointers in
tools/file_state.py, tools/schema_sanitizer.py, scripts/tool_search_livetest.py.
Tests: patch("run_agent.X") / monkeypatch.setattr(run_agent, "X") /
`from run_agent import X` -> defining module across 99 test files.
179 lines
6.3 KiB
Python
179 lines
6.3 KiB
Python
"""Host compression timeout terminates the turn before provider re-entry (#98722).
|
|
|
|
Salvaged from #98741 and composed with the already-merged #98424 turn-start
|
|
fail-closed boundary:
|
|
|
|
- #98424 covers the TURN-START preflight (raises
|
|
``PreflightCompressionTimedOut`` before the loop starts).
|
|
- These tests pin the two consumers unique to #98741: the provider-overflow
|
|
recovery path must not re-enter compression / re-send the unchanged request
|
|
once the wait budget was spent, and the mid-turn pre-API pass must end the
|
|
turn with the typed ``compression_exhausted`` recovery contract.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import run_agent
|
|
from run_agent import AIAgent
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _no_compression_sleep(monkeypatch):
|
|
import time as _time
|
|
|
|
monkeypatch.setattr(_time, "sleep", lambda *_a, **_k: None)
|
|
monkeypatch.setattr("agent.retry_utils.jittered_backoff", lambda *a, **k: 0.0)
|
|
|
|
|
|
def _make_agent():
|
|
with (
|
|
patch("model_tools.get_tool_definitions", return_value=[]),
|
|
patch("model_tools.check_toolset_requirements", return_value={}),
|
|
patch("agent.process_bootstrap.OpenAI"),
|
|
):
|
|
agent = AIAgent(
|
|
api_key="test-key-1234567890",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
)
|
|
agent.client = MagicMock()
|
|
agent._cached_system_prompt = "You are helpful."
|
|
agent._use_prompt_caching = False
|
|
agent.tool_delay = 0
|
|
agent.save_trajectories = False
|
|
agent.compression_enabled = True
|
|
return agent
|
|
|
|
|
|
def _mock_response(content="Hello", finish_reason="stop"):
|
|
msg = SimpleNamespace(
|
|
content=content, tool_calls=None, reasoning_content=None, reasoning=None
|
|
)
|
|
choice = SimpleNamespace(message=msg, finish_reason=finish_reason)
|
|
resp = SimpleNamespace(choices=[choice], model="test/model")
|
|
resp.usage = None
|
|
return resp
|
|
|
|
|
|
def test_overflow_recovery_timeout_ends_turn_without_provider_reentry():
|
|
"""Provider 400 overflow + host-timed-out compression = typed terminal.
|
|
|
|
Before the fix, the timed-out pass was indistinguishable from an
|
|
ordinary no-op: the loop re-sent the unchanged oversized request, the
|
|
provider overflowed again, and compression was re-entered in the same
|
|
turn (#98722 "Summarizing" loop).
|
|
"""
|
|
agent = _make_agent()
|
|
|
|
err_400 = Exception(
|
|
"This model's maximum context length is 8192 tokens. However, "
|
|
"your messages resulted in 95000 tokens. Please reduce the length "
|
|
"of the messages."
|
|
)
|
|
err_400.status_code = 400
|
|
agent.client.chat.completions.create.side_effect = [err_400, err_400]
|
|
|
|
compression_calls = []
|
|
|
|
def _timed_out(messages, _system_message, **_kwargs):
|
|
compression_calls.append(1)
|
|
from agent.conversation_compression import (
|
|
mark_context_compression_timed_out,
|
|
reset_context_compression_timeout_outcome,
|
|
)
|
|
|
|
reset_context_compression_timeout_outcome(agent)
|
|
mark_context_compression_timed_out(agent)
|
|
return messages, agent._cached_system_prompt
|
|
|
|
agent._compress_context = _timed_out
|
|
|
|
history = [
|
|
{"role": "user", "content": "old request"},
|
|
{"role": "assistant", "content": "old response"},
|
|
]
|
|
with (
|
|
patch.object(agent, "_persist_session"),
|
|
patch.object(agent, "_save_trajectory"),
|
|
patch.object(agent, "_cleanup_task_resources"),
|
|
):
|
|
result = agent.run_conversation("continue", conversation_history=history)
|
|
|
|
# Exactly one doomed send proved the overflow; the timed-out recovery
|
|
# pass must not be followed by a second identical send.
|
|
assert compression_calls == [1]
|
|
assert agent.client.chat.completions.create.call_count == 1
|
|
assert result["failed"] is True
|
|
assert result["completed"] is False
|
|
assert result["compression_exhausted"] is True
|
|
assert "No messages were dropped" in result["final_response"]
|
|
assert "No messages were dropped" in result["error"]
|
|
|
|
|
|
def test_pre_api_compression_timeout_is_typed_terminal():
|
|
"""Mid-turn pre-API pass that hits the host timeout ends the turn."""
|
|
agent = _make_agent()
|
|
agent.context_compressor.protect_first_n = 0
|
|
agent.context_compressor.protect_last_n = 0
|
|
agent.context_compressor._threshold_tokens = 1
|
|
agent.context_compressor.should_compress = MagicMock(return_value=True)
|
|
agent.context_compressor.should_compress_info = MagicMock(
|
|
return_value=(True, None)
|
|
)
|
|
agent.context_compressor.should_defer_preflight_to_real_usage = MagicMock(
|
|
return_value=False
|
|
)
|
|
agent.context_compressor.get_active_compression_failure_cooldown = MagicMock(
|
|
return_value=None
|
|
)
|
|
|
|
compression_calls = []
|
|
|
|
def _timed_out(messages, _system_message, **_kwargs):
|
|
compression_calls.append(1)
|
|
from agent.conversation_compression import (
|
|
mark_context_compression_timed_out,
|
|
reset_context_compression_timeout_outcome,
|
|
)
|
|
|
|
reset_context_compression_timeout_outcome(agent)
|
|
mark_context_compression_timed_out(agent)
|
|
return messages, agent._cached_system_prompt
|
|
|
|
agent._compress_context = _timed_out
|
|
|
|
from agent.turn_context import PreflightCompressionTimedOut
|
|
|
|
history = [
|
|
{"role": "user", "content": "old request"},
|
|
{"role": "assistant", "content": "old response"},
|
|
]
|
|
with (
|
|
patch.object(agent, "_persist_session"),
|
|
patch.object(agent, "_save_trajectory"),
|
|
patch.object(agent, "_cleanup_task_resources"),
|
|
):
|
|
# The turn-start boundary (#98424) may fire first and raise; both
|
|
# outcomes satisfy the invariant under test: the unchanged oversized
|
|
# request never reaches the provider after a host timeout.
|
|
try:
|
|
result = agent.run_conversation(
|
|
"continue", conversation_history=history
|
|
)
|
|
except PreflightCompressionTimedOut:
|
|
result = None
|
|
|
|
assert compression_calls == [1]
|
|
agent.client.chat.completions.create.assert_not_called()
|
|
if result is not None:
|
|
assert result["failed"] is True
|
|
assert result["compression_exhausted"] is True
|
|
assert result["turn_exit_reason"] == "context_compression_timeout"
|