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.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
Every OpenAI-shape opencode turn rides the loopback relay, so a Google key gets its thought signatures replayed (#97)
github-actions[bot]dfab98a20996
diff --git a/runner/server.py b/runner/server.pyindex 4bb90aa5..2e53fc1b 100644--- a/runner/server.py+++ b/runner/server.py@@ -3091,6 +3091,19 @@ def _opencode_mcp(servers: list[dict] | None) -> dict:return out+def _opencode_npm(auth: Auth, model: str, pr: str) -> str:+ """Which ai-sdk package serves this turn: the wire format the connection speaks."""+ if auth.api_format == "anthropic":+ return "@ai-sdk/anthropic"+ if auth.api_format == "openai":+ return "@ai-sdk/openai-compatible"+ if pr == "anthropic" or (pr == "tokenrouter" and _PI_CLAUDE_MODEL.search(model or "")):+ return "@ai-sdk/anthropic"+ if pr == "azure" or _HERMES_RESPONSES_API_MODEL.search(model or ""):+ return "@ai-sdk/openai" # /v1/responses+ return "@ai-sdk/openai-compatible" # /v1/chat/completions++def _opencode_config(auth: Auth, model: str, cwd: str, mcp_servers: list[dict] | None,skills_dir: str | None, tools_disabled: list[str] | None = None,pr: str = "") -> str:@@ -3107,16 +3120,7 @@ def _opencode_config(auth: Auth, model: str, cwd: str, mcp_servers: list[dict] |# chat/completions, and sending it to the openai-compatible package fails at the first call.# When api_format is set (custom integration), the user's explicit choice wins over any# model-family heuristic — the endpoint is the one they told us to reach.- if auth.api_format == "anthropic":- npm = "@ai-sdk/anthropic"- elif auth.api_format == "openai":- npm = "@ai-sdk/openai-compatible"- elif pr == "anthropic" or (pr == "tokenrouter" and _PI_CLAUDE_MODEL.search(model or "")):- npm = "@ai-sdk/anthropic"- elif pr == "azure" or _HERMES_RESPONSES_API_MODEL.search(model or ""):- npm = "@ai-sdk/openai" # /v1/responses- else:- npm = "@ai-sdk/openai-compatible" # /v1/chat/completions+ npm = _opencode_npm(auth, model, pr)# Every ai-sdk package appends its own resource to baseURL (@ai-sdk/anthropic "/messages",# @ai-sdk/openai "/responses", openai-compatible "/chat/completions") and expects the "/v1"# to be there already, the way pi's openai clients do (see _pi_models_json). A connection@@ -3192,11 +3196,15 @@ def _build_opencode(provider: str, auth: Auth, model: str, prompt: str, cwd: strpr = provider or "openai-api"if pr not in OPENCODE_PROVIDERS:raise HTTPException(400, f"unknown opencode provider '{pr}' (one of {sorted(OPENCODE_PROVIDERS)})")- if auth.api_format == "openai" and auth.base_url and auth.api_key:- # A custom OpenAI endpoint rides the loopback relay, for the same two reasons hermes- # does: request shapes ai-sdk emits but strict endpoints refuse are repaired in flight- # (Azure's gpt-5.x deployments 400 on max_tokens — captured live 2026-08-27), and the- # real key stays in this process; opencode's env gets a per-turn placeholder.+ if auth.base_url and auth.api_key and _opencode_npm(auth, model, pr) != "@ai-sdk/anthropic":+ # Every OpenAI-shape opencode turn rides the loopback relay, as pi's and qwen's do, not only+ # a custom endpoint: request shapes ai-sdk emits but strict endpoints refuse are repaired in+ # flight (Azure's gpt-5.x deployments 400 on max_tokens, captured live 2026-08-27), Gemini+ # 3's thought signatures are replayed (2026-09-06: with a Google key opencode reached Google+ # directly, and every artifact turn on a Gemini 3.x id failed while pi, dsh and qwen passed+ # through the relay), and the real key stays in this process; opencode's env gets a+ # per-turn placeholder. A Messages-shape turn keeps its direct base: the relay speaks+ # bearer auth, and Anthropic takes the key in x-api-key.relay_base, relay_tok = _hermes_relay_route(auth.base_url, auth.api_key)auth = auth.model_copy(update={"base_url": relay_base, "api_key": relay_tok})if auth.api_key:diff --git a/runner/tests/test_opencode_base_url.py b/runner/tests/test_opencode_base_url.pyindex 839c511d..945863d7 100644--- a/runner/tests/test_opencode_base_url.py+++ b/runner/tests/test_opencode_base_url.py@@ -25,3 +25,21 @@ def test_a_base_that_already_ends_in_v1_is_kept(tmp_path):def test_a_custom_endpoint_is_the_users_exact_url(tmp_path):p = _cfg(tmp_path, provider="custom", base_url="https://relay.example/api/coding/v3", api_key="k", api_format="anthropic")assert p["options"]["baseURL"] == "https://relay.example/api/coding/v3"+++def test_an_openai_shape_turn_rides_the_loopback_relay_and_a_messages_turn_does_not(tmp_path):+ # 2026-09-06: with a Google key opencode reached Google directly, so nothing replayed Gemini 3's+ # thought signatures and every artifact turn failed; through the relay pi, dsh and qwen passed.+ env = {}+ server._build_opencode("openai-api", server.Auth(provider="google", base_url="https://generativelanguage.googleapis.com/v1beta/openai", api_key="real"),+ "gemini-3.6-flash", "hi", str(tmp_path), env)+ p = json.load(open(tmp_path / ".harness" / "opencode.json"))["provider"]["hr"]+ assert p["npm"] == "@ai-sdk/openai-compatible"+ assert p["options"]["baseURL"].startswith("http://127.0.0.1:") and p["options"]["baseURL"].endswith("/v1")+ assert env[server._OPENCODE_KEY_ENV] != "real" # a per-turn placeholder; the key stays in this process+ env2 = {}+ server._build_opencode("anthropic", server.Auth(provider="anthropic", base_url="https://api.anthropic.com", api_key="real"),+ "claude-opus-5", "hi", str(tmp_path), env2)+ p2 = json.load(open(tmp_path / ".harness" / "opencode.json"))["provider"]["hr"]+ assert p2["npm"] == "@ai-sdk/anthropic" and p2["options"]["baseURL"] == "https://api.anthropic.com/v1"+ assert env2[server._OPENCODE_KEY_ENV] == "real"diff --git a/runner/tests/test_opencode_normalize.py b/runner/tests/test_opencode_normalize.pyindex ffec0f95..6fabcb4e 100644--- a/runner/tests/test_opencode_normalize.py+++ b/runner/tests/test_opencode_normalize.py@@ -234,13 +234,16 @@ def test_resume_finds_a_session_still_sitting_in_the_write_ahead_log():assert "--session" in cmd and "ses_wal" in cmd-def test_key_goes_to_the_environment_and_never_into_the_config_file():+def test_the_real_key_stays_in_the_runner_and_never_enters_the_config_or_the_env():+ # an OpenAI-shape turn rides the loopback relay: the env carries a per-turn placeholder the relay+ # resolves to the real key, the config references the env, and the real key is in neithercmd, d, env = _argv()- assert env["HR_OPENCODE_KEY"] == "sk-test"+ assert env["HR_OPENCODE_KEY"] != "sk-test" and env["HR_OPENCODE_KEY"]cfg = _json.loads(open(f"{d}/.harness/opencode.json").read())assert cfg["provider"]["hr"]["options"]["apiKey"] == "{env:HR_OPENCODE_KEY}"assert "sk-test" not in open(f"{d}/.harness/opencode.json").read()- assert cfg["provider"]["hr"]["options"]["baseURL"] == "https://relay.example/v1"+ assert cfg["provider"]["hr"]["options"]["baseURL"].startswith("http://127.0.0.1:")+ assert cfg["provider"]["hr"]["options"]["baseURL"].endswith("/v1")def test_a_prompt_starting_with_a_dash_is_not_read_as_a_flag():