Files
hermes-agent/tests/agent/test_codex_sdk_transform_bypass.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

158 lines
5.5 KiB
Python

"""Regression tests for the SDK request-transform bypass (#93650).
``responses.create`` re-walks the whole request body against the
``ResponseCreateParams`` union graph client-side, holding the GIL. #93650
documents that walk wedging for 12+ hours on a ~1.4 MB conversation and
freezing the entire agent — no in-process watchdog can fire while the GIL
is held, and no socket kill helps a pre-network hang. Bulk wire-format
fields are therefore routed through ``extra_body``, which the SDK merges
into the JSON body *after* the transform.
"""
import sys
import types
from types import SimpleNamespace
from unittest.mock import MagicMock
sys.modules.setdefault("fire", types.SimpleNamespace(Fire=lambda *a, **k: None))
sys.modules.setdefault("firecrawl", types.SimpleNamespace(Firecrawl=object))
sys.modules.setdefault("fal_client", types.SimpleNamespace())
from agent.codex_runtime import (
_bypass_sdk_request_transform,
_is_plain_json_data,
)
def _wire_kwargs():
return {
"model": "gpt-5.6-sol",
"instructions": "You are Hermes.",
"input": [
{"role": "user", "content": [{"type": "input_text", "text": "Ping"}]},
{"type": "function_call_output", "call_id": "c1", "output": "ok"},
],
"tools": [{"type": "function", "name": "terminal", "parameters": {}}],
"store": False,
"stream": True,
"timeout": 1800.0,
}
class TestIsPlainJsonData:
def test_accepts_nested_wire_payloads(self):
assert _is_plain_json_data(_wire_kwargs()["input"])
def test_rejects_non_json_leaves(self):
assert not _is_plain_json_data([{"role": "user", "content": object()}])
def test_rejects_non_string_dict_keys(self):
assert not _is_plain_json_data({1: "a"})
def test_rejects_generators(self):
assert not _is_plain_json_data((item for item in ()))
class TestBypassSdkRequestTransform:
def test_moves_bulk_fields_to_extra_body(self):
kwargs = _wire_kwargs()
original_input = kwargs["input"]
bypassed = _bypass_sdk_request_transform(kwargs)
assert "input" not in bypassed
assert "tools" not in bypassed
assert bypassed["extra_body"]["input"] is original_input
assert bypassed["extra_body"]["tools"] == kwargs["tools"]
# Scalar configuration stays on the typed path.
assert bypassed["model"] == "gpt-5.6-sol"
assert bypassed["stream"] is True
assert bypassed["timeout"] == 1800.0
# The caller's mapping is untouched.
assert kwargs["input"] is original_input
assert "extra_body" not in kwargs
def test_merges_with_existing_extra_body_and_keeps_caller_precedence(self):
kwargs = _wire_kwargs()
caller_extra = {"prompt_cache_retention": "24h", "input": "explicit-wins"}
kwargs["extra_body"] = caller_extra
bypassed = _bypass_sdk_request_transform(kwargs)
# An explicit extra_body entry wins, exactly as the SDK's
# post-transform merge would have resolved the collision.
assert bypassed["extra_body"]["input"] == "explicit-wins"
assert bypassed["extra_body"]["prompt_cache_retention"] == "24h"
assert bypassed["extra_body"]["tools"] == kwargs["tools"]
assert caller_extra == {
"prompt_cache_retention": "24h",
"input": "explicit-wins",
}
def test_non_json_field_stays_on_typed_sdk_path(self):
kwargs = _wire_kwargs()
kwargs["input"] = [{"role": "user", "content": object()}]
bypassed = _bypass_sdk_request_transform(kwargs)
assert bypassed["input"] == kwargs["input"]
assert bypassed["extra_body"] == {"tools": kwargs["tools"]}
def test_string_input_stays_in_place(self):
kwargs = _wire_kwargs()
kwargs["input"] = "plain prompt"
kwargs.pop("tools")
bypassed = _bypass_sdk_request_transform(kwargs)
assert bypassed is kwargs
def test_env_escape_hatch_restores_passthrough(self, monkeypatch):
monkeypatch.setenv("HERMES_CODEX_SDK_TRANSFORM", "1")
kwargs = _wire_kwargs()
assert _bypass_sdk_request_transform(kwargs) is kwargs
class TestRunCodexStreamRoutesPayloadViaExtraBody:
def _make_agent(self):
from run_agent import AIAgent
agent = AIAgent(
api_key="test-key",
base_url="https://chatgpt.com/backend-api/codex",
model="gpt-5.6-sol",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
agent._interrupt_requested = False
return agent
def test_create_receives_input_via_extra_body(self):
from agent.codex_runtime import run_codex_stream
agent = self._make_agent()
events = [
SimpleNamespace(
type="response.completed",
response=SimpleNamespace(
id="r1",
status="completed",
output=[],
usage=None,
),
)
]
mock_client = MagicMock()
mock_client.responses.create.return_value = iter(events)
run_codex_stream(agent, _wire_kwargs(), client=mock_client)
create_kwargs = mock_client.responses.create.call_args.kwargs
assert "input" not in create_kwargs
assert "tools" not in create_kwargs
assert create_kwargs["stream"] is True
assert create_kwargs["extra_body"]["input"][0]["role"] == "user"
assert create_kwargs["extra_body"]["tools"][0]["name"] == "terminal"