Files
hermes-agent/tests/agent/test_content_policy_blocked.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

143 lines
6.0 KiB
Python

"""Regression guard for #18028: provider content-policy / safety-filter
blocks must classify as ``content_policy_blocked``, be non-retryable, and
trigger the ``is_client_error`` abort path so the loop jumps straight to a
configured fallback or surfaces a clear policy-block message — instead of
burning ``api_max_retries`` paid attempts on a deterministic refusal and
delivering "API failed after 3 retries" to Telegram/cron with no provider
context.
Real-world symptom from the issue:
``API call failed after 3 retries — This content was flagged for
possible cybersecurity risk... | provider=openai-codex model=gpt-5.5``
repeating across cron jobs and gateway sessions, with the user unable to
tell whether the gateway was broken, the model was down, or their prompt
was the problem.
"""
from __future__ import annotations
class TestContentPolicyBlockedClassification:
"""Verify classify_api_error returns the right shape so downstream
recovery (fallback activation, final_response wording) fires correctly.
"""
def test_openai_codex_cybersecurity_no_status(self):
"""The reported #18028 case — SDK raises without a status code."""
from agent.error_classifier import classify_api_error, FailoverReason
e = Exception(
"This content was flagged for possible cybersecurity risk. "
"If this seems wrong, try rephrasing your request. To get "
"authorized for security work, join the Trusted Access for "
"Cyber program."
)
result = classify_api_error(e, provider="openai-codex", model="gpt-5.5")
# Must NOT fall into the retryable ``unknown`` bucket — that's what
# caused the 3x retry burn.
assert result.reason == FailoverReason.content_policy_blocked
assert result.retryable is False
# Recovery is fallback model, not credential rotation or compression.
assert result.should_fallback is True
assert result.should_compress is False
assert result.should_rotate_credential is False
class TestContentPolicyTriggersClientErrorAbort:
"""Mirror the ``is_client_error`` predicate in
``agent/conversation_loop.py`` and verify
``FailoverReason.content_policy_blocked`` resolves to True so the loop
aborts (after attempting fallback) instead of falling into the
retry-backoff path.
"""
def _mirror_is_client_error(
self,
*,
classified_retryable: bool,
classified_reason,
classified_should_compress: bool = False,
is_local_validation_error: bool = False,
is_context_length_error: bool = False,
) -> bool:
"""Exact shape of conversation_loop.py's is_client_error check.
Kept in lock-step with the source. If you change one, change both.
"""
from agent.error_classifier import FailoverReason
return (
is_local_validation_error
or (
not classified_retryable
and not classified_should_compress
and classified_reason not in {
FailoverReason.rate_limit,
FailoverReason.overloaded,
FailoverReason.context_overflow,
FailoverReason.payload_too_large,
FailoverReason.long_context_tier,
FailoverReason.thinking_signature,
}
)
) and not is_context_length_error
def test_content_policy_blocked_triggers_abort(self):
"""Safety-filter block must reach is_client_error → fallback/abort."""
from agent.error_classifier import FailoverReason
# What classify_api_error returns for a content-policy block:
# reason=content_policy_blocked, retryable=False, should_compress=False
assert self._mirror_is_client_error(
classified_retryable=False,
classified_reason=FailoverReason.content_policy_blocked,
), (
"FailoverReason.content_policy_blocked must trigger the "
"is_client_error path so fallback fires immediately instead of "
"burning api_max_retries paid attempts on a deterministic "
"safety refusal — see #18028."
)
class TestContentPolicyPatternsAreNarrow:
"""Defensive guard: the safety-filter patterns must not collide with
benign error wording from billing / format / generic 400 errors. If
these regress to ``content_policy_blocked``, recovery will route to
the wrong code path (fallback model instead of credential rotation).
"""
def test_generic_400_format_error_not_misclassified(self):
from agent.error_classifier import classify_api_error, FailoverReason
class _Err(Exception):
def __init__(self, msg, status_code):
super().__init__(msg)
self.status_code = status_code
e = _Err("Invalid request: messages must be a non-empty list", status_code=400)
result = classify_api_error(e, provider="openai", model="gpt-4o")
assert result.reason != FailoverReason.content_policy_blocked
def test_openrouter_account_policy_block_stays_distinct(self):
"""``provider_policy_blocked`` (OpenRouter account-level data
policy) must remain a separate classification from
``content_policy_blocked`` (upstream model safety filter) — they
have different recovery strategies.
"""
from agent.error_classifier import classify_api_error, FailoverReason
class _Err(Exception):
def __init__(self, msg, status_code):
super().__init__(msg)
self.status_code = status_code
e = _Err(
"No endpoints available matching your guardrail restrictions "
"and data policy",
status_code=404,
)
result = classify_api_error(e, provider="openrouter", model="anthropic/claude-opus")
assert result.reason == FailoverReason.provider_policy_blocked
assert result.reason != FailoverReason.content_policy_blocked