mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-21 16:18:55 +00:00
fix(context): preserve missing-key compression history
This commit is contained in:
parent
202ad1b8c9
commit
577beeb9b9
8 changed files with 251 additions and 17 deletions
|
|
@ -854,11 +854,19 @@ class TestAuthFailureAborts:
|
|||
def test_quota_classifier_accepts_explicit_provider_signals(self, message):
|
||||
assert _is_summary_access_or_quota_error(Exception(message)) is True
|
||||
|
||||
def test_missing_provider_api_key_is_terminal_access_failure(self):
|
||||
err = RuntimeError(
|
||||
"Provider 'opencode-zen' is set in config.yaml but no API key was "
|
||||
"found. Set the OPENCODE-ZEN_API_KEY environment variable."
|
||||
)
|
||||
assert _is_summary_access_or_quota_error(err) is True
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"message",
|
||||
[
|
||||
"billing portal is temporarily unavailable",
|
||||
"usage limit documentation could not be loaded",
|
||||
"API key documentation was not found",
|
||||
"rate limit exceeded; retry later",
|
||||
"quota exceeded, please retry after the window resets",
|
||||
"request timed out",
|
||||
|
|
@ -905,6 +913,34 @@ class TestAuthFailureAborts:
|
|||
assert c._last_compress_aborted is True
|
||||
assert c._last_summary_fallback_used is False
|
||||
|
||||
def test_missing_provider_api_key_preserves_original_messages(self):
|
||||
"""A configured auxiliary provider without a visible key preserves context."""
|
||||
err = RuntimeError(
|
||||
"Provider 'opencode-zen' is set in config.yaml but no API key was "
|
||||
"found. Set the OPENCODE-ZEN_API_KEY environment variable, or switch "
|
||||
"to a different provider with hermes model."
|
||||
)
|
||||
with patch(
|
||||
"agent.context_compressor.get_model_context_length", return_value=100000
|
||||
):
|
||||
c = ContextCompressor(
|
||||
model="test",
|
||||
quiet_mode=True,
|
||||
protect_first_n=2,
|
||||
protect_last_n=2,
|
||||
abort_on_summary_failure=False,
|
||||
)
|
||||
msgs = self._msgs(12)
|
||||
with patch("agent.context_compressor.call_llm", side_effect=err):
|
||||
result = c.compress(msgs, current_tokens=999999, force=True)
|
||||
|
||||
assert result == msgs
|
||||
assert c._last_summary_error == str(err)
|
||||
assert c._last_summary_auth_failure is True
|
||||
assert c._last_compress_aborted is True
|
||||
assert c._last_summary_fallback_used is False
|
||||
assert c._last_summary_dropped_count == 0
|
||||
|
||||
def test_402_quota_with_retry_uses_existing_fallback(self):
|
||||
"""A reset-window quota remains transient instead of aborting compression."""
|
||||
err = StubProviderError(
|
||||
|
|
|
|||
62
tests/agent/test_manual_compression_feedback.py
Normal file
62
tests/agent/test_manual_compression_feedback.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
"""Behavioral coverage for manual compression status messages."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from agent.manual_compression_feedback import summarize_manual_compression
|
||||
|
||||
|
||||
def _messages(count: int) -> list[dict[str, str]]:
|
||||
return [
|
||||
{"role": "user" if index % 2 == 0 else "assistant", "content": str(index)}
|
||||
for index in range(count)
|
||||
]
|
||||
|
||||
|
||||
def test_aborted_compression_reports_preserved_messages_and_reason():
|
||||
messages = _messages(12)
|
||||
state = SimpleNamespace(
|
||||
_last_compress_aborted=True,
|
||||
_last_summary_fallback_used=False,
|
||||
_last_summary_error=(
|
||||
"Provider 'opencode-zen' is set in config.yaml but no API key was found."
|
||||
),
|
||||
)
|
||||
|
||||
feedback = summarize_manual_compression(
|
||||
messages,
|
||||
list(messages),
|
||||
120_000,
|
||||
120_000,
|
||||
compression_state=state,
|
||||
)
|
||||
|
||||
assert feedback["aborted"] is True
|
||||
assert feedback["fallback_used"] is False
|
||||
assert feedback["headline"] == "Compression aborted: 12 messages preserved"
|
||||
assert "no messages were removed" in feedback["note"]
|
||||
assert "no API key was found" in feedback["note"]
|
||||
|
||||
|
||||
def test_fallback_compression_reports_dropped_message_count():
|
||||
before = _messages(12)
|
||||
after = before[:2] + before[-2:]
|
||||
state = SimpleNamespace(
|
||||
_last_compress_aborted=False,
|
||||
_last_summary_fallback_used=True,
|
||||
_last_summary_dropped_count=8,
|
||||
_last_summary_error="summary provider returned an invalid response",
|
||||
)
|
||||
|
||||
feedback = summarize_manual_compression(
|
||||
before,
|
||||
after,
|
||||
120_000,
|
||||
40_000,
|
||||
compression_state=state,
|
||||
)
|
||||
|
||||
assert feedback["aborted"] is False
|
||||
assert feedback["fallback_used"] is True
|
||||
assert feedback["headline"] == "Compressed with fallback: 12 → 4 messages"
|
||||
assert "removed 8 message(s)" in feedback["note"]
|
||||
assert "invalid response" in feedback["note"]
|
||||
|
|
@ -38,6 +38,32 @@ def test_manual_compress_reports_noop_without_success_banner(capsys):
|
|||
assert "Approx request size: ~100 tokens (unchanged)" in output
|
||||
|
||||
|
||||
def test_manual_compress_reports_aborted_summary_without_success_banner(capsys):
|
||||
shell = _make_cli()
|
||||
history = _make_history()
|
||||
shell.conversation_history = history
|
||||
shell.agent = MagicMock()
|
||||
shell.agent.compression_enabled = True
|
||||
shell.agent._cached_system_prompt = ""
|
||||
shell.agent.tools = None
|
||||
shell.agent.session_id = shell.session_id
|
||||
shell.agent.context_compressor._last_compress_aborted = True
|
||||
shell.agent.context_compressor._last_summary_fallback_used = False
|
||||
shell.agent.context_compressor._last_summary_error = (
|
||||
"Provider 'opencode-zen' is set in config.yaml but no API key was found."
|
||||
)
|
||||
shell.agent._compress_context.return_value = (list(history), "")
|
||||
|
||||
with patch("agent.model_metadata.estimate_request_tokens_rough", return_value=100):
|
||||
shell._manual_compress()
|
||||
|
||||
output = capsys.readouterr().out
|
||||
assert "⚠️ Compression aborted: 4 messages preserved" in output
|
||||
assert "no messages were removed" in output
|
||||
assert "no API key was found" in output
|
||||
assert "✅ Compressed:" not in output
|
||||
|
||||
|
||||
def test_manual_compress_explains_when_token_estimate_rises(capsys):
|
||||
shell = _make_cli()
|
||||
history = _make_history()
|
||||
|
|
|
|||
|
|
@ -4759,6 +4759,52 @@ def test_session_compress_uses_compress_helper(monkeypatch):
|
|||
emit.assert_any_call("status.update", "sid", {"kind": "status", "text": "ready"})
|
||||
|
||||
|
||||
def test_session_compress_reports_aborted_summary_without_success(monkeypatch):
|
||||
compression_state = types.SimpleNamespace(
|
||||
_last_compress_aborted=True,
|
||||
_last_summary_fallback_used=False,
|
||||
_last_summary_error=(
|
||||
"Provider 'opencode-zen' is set in config.yaml but no API key was found."
|
||||
),
|
||||
)
|
||||
agent = types.SimpleNamespace(
|
||||
context_compressor=compression_state,
|
||||
_cached_system_prompt="",
|
||||
tools=None,
|
||||
)
|
||||
history = [{"role": "user", "content": f"m{i}"} for i in range(6)]
|
||||
server._sessions["sid"] = _session(agent=agent, history=history)
|
||||
|
||||
monkeypatch.setattr(
|
||||
server,
|
||||
"_compress_session_history",
|
||||
lambda session, focus_topic=None, **_kw: (0, {"total": 42}),
|
||||
)
|
||||
monkeypatch.setattr(server, "_session_info", lambda _agent, *a: {"model": "x"})
|
||||
|
||||
try:
|
||||
with patch("tui_gateway.server._emit"):
|
||||
resp = server.handle_request(
|
||||
{
|
||||
"id": "1",
|
||||
"method": "session.compress",
|
||||
"params": {"session_id": "sid"},
|
||||
}
|
||||
)
|
||||
|
||||
result = resp["result"]
|
||||
assert result["status"] == "aborted"
|
||||
assert result["removed"] == 0
|
||||
assert result["summary"]["aborted"] is True
|
||||
assert result["summary"]["headline"] == (
|
||||
"Compression aborted: 6 messages preserved"
|
||||
)
|
||||
assert "no API key was found" in result["summary"]["note"]
|
||||
assert "Compressed:" not in result["summary"]["headline"]
|
||||
finally:
|
||||
server._sessions.pop("sid", None)
|
||||
|
||||
|
||||
def test_session_compress_syncs_session_key_after_rotation(monkeypatch):
|
||||
"""When AIAgent._compress_context rotates session_id (compression split),
|
||||
the gateway session_key must follow so subsequent approval routing,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue