Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

3.6 KB
Raw Blame
"""codex names its items two ways: snake_case on `codex exec --json`, camelCase on the app-server.
The record must read both the same, and a message is never a tool (2026-09-10)."""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from server import _codex_item_kind, _codex_to_claude, _codex_tool_item  # noqa: E402


def test_item_kinds_read_the_same_in_both_spellings():
    assert _codex_item_kind({"type": "mcpToolCall"}) == "mcp_tool_call"
    assert _codex_item_kind({"type": "mcp_tool_call"}) == "mcp_tool_call"
    assert _codex_item_kind({"type": "commandExecution"}) == "command_execution"
    assert _codex_item_kind({"type": "agentMessage"}) == "agent_message"


def test_an_mcp_call_is_named_by_its_server_and_tool_in_both_spellings():
    for it in ({"type": "mcpToolCall", "id": "m1", "server": "deepwiki", "tool": "read_wiki_structure", "arguments": {"repoName": "x"}},
               {"type": "mcp_tool_call", "id": "m1", "server": "deepwiki", "tool": "read_wiki_structure", "arguments": {"repoName": "x"}}):
        evs = _codex_tool_item(it)
        use = evs[0]["message"]["content"][0]
        assert use["type"] == "tool_use" and use["name"] == "deepwiki.read_wiki_structure" and use["input"] == {"repoName": "x"}


def test_messages_and_reasoning_are_not_tools():
    for kind in ("userMessage", "agentMessage", "reasoning", "agent_message"):
        assert _codex_tool_item({"type": kind, "id": "x", "text": "hello"}) == []


def test_a_command_reads_its_camel_case_fields():
    evs = _codex_tool_item({"type": "commandExecution", "id": "c1", "command": "ls", "aggregatedOutput": "a\n", "exitCode": 0})
    use, res = evs[0]["message"]["content"][0], evs[1]["message"]["content"][0]
    assert use["name"] == "Bash" and use["input"] == {"command": "ls"}
    assert res["type"] == "tool_result" and res["is_error"] is False and res["content"] == "a\n"


def test_codex_s_own_warning_is_logged_never_a_tool_call(capsys):
    """`codex exec --json` sends its own warning as an error item (#579): on 0.36.3 the response
    listed it as a function_call named "error" beside the model's real Bash call."""
    it = {"type": "error", "id": "item_0",
          "message": "Model metadata for `openai/gpt-6.1-sol` not found. Defaulting to fallback metadata"}
    assert _codex_tool_item(it) == []
    assert _codex_to_claude({"type": "item.completed", "item": it}, {}) == []
    assert _codex_to_claude({"type": "item.completed", "item": it}, {"partial": True}) == []
    assert "[codex] Model metadata for `openai/gpt-6.1-sol` not found" in capsys.readouterr().out


def test_a_codex_failure_is_still_the_turn_s_error():
    """Only codex's warnings are error ITEMS: in 0.154.0's exec processor every ErrorItem comes
    from a warning, config warning, deprecation notice or model reroute, and each leaves the turn
    running. A failure arrives as the top-level `error` event or as `turn.failed`, and it stays
    the turn's error, with codex's message."""
    warn = {"type": "item.completed", "item": {"type": "error", "id": "item_0", "message": "Model metadata not found"}}
    failed = {"type": "turn.failed", "error": {"message": "stream disconnected before completion"}}
    evs = _codex_to_claude(warn, {}) + _codex_to_claude(failed, {})
    assert evs == [{"type": "result", "subtype": "error", "is_error": True,
                    "result": "stream disconnected before completion"}]
    top = _codex_to_claude({"type": "error", "message": "unexpected status 401 Unauthorized"}, {})
    assert top == [{"type": "result", "subtype": "error", "is_error": True,
                    "result": "unexpected status 401 Unauthorized"}]