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.

4.9 KB
Raw Blame
"""Issue #149: a custom endpoint names its models its own way, so its rows are the map."""
import asyncio
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import app as gw  # noqa: E402


def test_the_custom_rows_are_the_map_and_a_blank_wire_id_is_the_canonical():
    integ = {"name": "proxy", "provider": "custom",
             "config": {"api_format": "responses", "base_url": "https://proxy.internal/openai/v1"},
             "models": [{"canonical": "claude-opus-4.8", "provider_id": "anthropic--claude-4.8-opus"},
                        {"canonical": "gpt-5.5", "provider_id": ""},
                        {"canonical": "gpt-5.4", "provider_id": "prod-gpt-v2"}]}
    assert gw._integration_models(integ) == {"claude-opus-4.8": "anthropic--claude-4.8-opus",
                                             "gpt-5.5": "gpt-5.5", "gpt-5.4": "prod-gpt-v2"}


def test_a_custom_integration_saved_with_one_model_id_still_reads_as_that_one_row():
    integ = {"name": "old", "provider": "custom", "config": {"api_format": "openai", "model_id": "my-llm"}}
    assert gw._integration_models(integ) == {"my-llm": "my-llm"}
    # rows win over the legacy field once they exist
    integ["models"] = [{"canonical": "gpt-5.5", "provider_id": "x"}]
    assert gw._integration_models(integ) == {"gpt-5.5": "x"}


def test_the_mapped_connection_sends_the_wire_id_verbatim(monkeypatch):
    integ = {"name": "proxy", "provider": "custom",
             "config": {"api_format": "responses", "base_url": "https://proxy.internal/openai/v1", "api_key": "k",
                        "namespace_tools": "1", "web_search": "1"},
             "models": [{"canonical": "gpt-5.5", "provider_id": "prod-gpt-v2"}]}
    async def mm(): return {"gpt-5.5": "proxy"}
    async def docs(): return [integ]
    monkeypatch.setattr(gw, "_effective_model_map", mm)
    monkeypatch.setattr(gw, "_integrations_doc", docs)
    conn = asyncio.run(gw._mapped_integration_conn("codex", "gpt-5.5"))
    assert conn and conn["provider"] == "tokenrouter" and conn["model"] == "prod-gpt-v2" and conn["_model_resolved"]
    assert conn["namespace_tools"] == "1" and conn["web_search"] == "1"
    monkeypatch.setattr(gw, "PUBLIC_BASE_URL", "https://public.example")
    forwarded = gw._auth_from_conn(conn, sid="sid")
    assert forwarded and forwarded["namespace_tools"] == "1" and forwarded["web_search"] == "1"
    # a Responses endpoint drives codex and nothing else; a chat one never drives codex
    assert gw._integration_serves_backend(integ, "codex") and not gw._integration_serves_backend(integ, "hermes")
    integ["config"]["api_format"] = "openai"
    assert not gw._integration_serves_backend(integ, "codex") and gw._integration_serves_backend(integ, "hermes")


def test_the_catalog_offers_the_canonicals_to_the_custom_form_and_disabled_tools_wherever_codex_runs():
    cat = {c["id"]: c for c in gw._provider_catalog_public()}
    custom = cat["custom"]
    assert "gpt-5.5" in custom["canonicals"] and "claude-opus-4.8" in custom["canonicals"]
    assert not any(f["key"] == "model_id" for f in custom["fields"])
    assert {"namespace_tools", "web_search"} <= {f["key"] for f in custom["fields"]}
    for pid, c in cat.items():
        has = any(f["key"] == "disabled_tools" for f in c["fields"])
        assert has == ("codex" in c["backends"]), pid
    assert "codex" in custom["backends"]


def test_the_connections_disabled_tools_join_the_harness_list_for_the_turn():
    # the union the turn body carries: the harness's list plus the connection's comma-separated field
    conn = {"provider": "tokenrouter", "disabled_tools": "web_search, image_generation"}
    conn_off = [t.strip() for t in str(conn.get("disabled_tools") or "").split(",") if t.strip()]
    assert sorted(set(["Bash"]) | set(conn_off)) == ["Bash", "image_generation", "web_search"]


def test_custom_format_metadata_matches_routing_compatibility():
    custom = next(c for c in gw._provider_catalog_public() if c['id'] == 'custom')
    assert custom['format_backends'] == {
        fmt: sorted(bases) for fmt, bases in gw._CUSTOM_FORMAT_BACKENDS.items()
    }


def test_uncatalogued_custom_model_is_available_only_on_compatible_bases(monkeypatch):
    integ = {'name': 'proxy', 'provider': 'custom',
             'config': {'api_format': 'openai', 'base_url': 'https://proxy.example/v1'},
             'models': [{'canonical': 'gpt-4.1-mini', 'provider_id': 'endpoint-mini'}]}

    async def docs(): return [integ]
    async def model_map(): return {'gpt-4.1-mini': 'proxy'}

    monkeypatch.setattr(gw, '_integrations_doc', docs)
    monkeypatch.setattr(gw, '_effective_model_map', model_map)
    assert gw._integration_models(integ) == {'gpt-4.1-mini': 'endpoint-mini'}
    conn = asyncio.run(gw._mapped_integration_conn('pi', 'gpt-4.1-mini'))
    assert conn and conn['model'] == 'endpoint-mini'
    assert asyncio.run(gw._mapped_integration_conn('codex', 'gpt-4.1-mini')) is None