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.

fix(gateway): derive the per-harness URL prefix from the base catalog

richard-epsilla71124503e1e3
diff --git a/gateway/app.py b/gateway/app.py
index 20368aa5..955bdc56 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -325,12 +325,17 @@ app = FastAPI(title="harness-gateway", docs_url=None, redoc_url=None, openapi_ur
# by /v1/ is rewritten — every real route (/v1, /a, /w, /share, /healthz, /readyz, /internal) has a
# reserved first segment that cannot match a chrn_<32hex> / builtin slug, so this is a NO-OP behind
# today's nginx (paths already arrive stripped to /v1/*) and only activates once api. targets ACA.
-_HID_PREFIX_RE = re.compile(r"^/(chrn_[0-9a-f]{32}|codex|claude-code|pi|hermes)(/v1/.*)$")
+# Compiled below, AFTER _BASE_CATALOG exists, from the catalog itself. It was a hardcoded
+# alternation here, and it drifted twice: /dsh/v1/* and /opencode/v1/* answered FastAPI's bare 404
+# because nobody added the new base ids (found live on the hosted side, first /opencode/v1 call).
+# The middleware reads the global at request time, so the late assignment is safe — every
+# module-level statement runs before uvicorn serves a request.
+_HID_PREFIX_RE: re.Pattern | None = None
@app.middleware("http")
async def _harness_path_prefix(request: Request, call_next):
- m = _HID_PREFIX_RE.match(request.scope.get("path", ""))
+ m = _HID_PREFIX_RE.match(request.scope.get("path", "")) if _HID_PREFIX_RE else None
if m:
hid, rest = m.group(1), m.group(2)
request.scope["path"] = rest
@@ -11286,6 +11291,11 @@ _BASE_CATALOG: dict[str, dict] = {
# `claude` is accepted as an alias for `claude-code` because existing harnesses store it.
_SUPPORTED_BASES = tuple(_BASE_CATALOG) + ("claude",)
+# The per-harness URL (/{hid}/v1/*) accepts every base id the catalog declares, plus stored chrn
+# ids — DERIVED, so adding a base to _BASE_CATALOG routes its public URL with no second edit.
+_HID_PREFIX_RE = re.compile(
+ r"^/(chrn_[0-9a-f]{32}|" + "|".join(re.escape(b) for b in sorted(_SUPPORTED_BASES, key=len, reverse=True)) + r")(/v1/.*)$")
+
def _require_supported_base(base: str) -> str:
b = (base or "").strip().lower()
diff --git a/gateway/tests/test_hid_prefix.py b/gateway/tests/test_hid_prefix.py
new file mode 100644
index 00000000..ed5aa6d0
--- /dev/null
+++ b/gateway/tests/test_hid_prefix.py
@@ -0,0 +1,29 @@
+"""The per-harness URL must accept every base the catalog declares.
+
+The alternation was hardcoded and drifted twice: /dsh/v1/* and /opencode/v1/* answered a bare 404
+because nobody added the new ids (found live on the hosted side). It is now derived from
+_BASE_CATALOG, and this test is the tripwire: add a base, and its public URL routes with no
+second edit — or this fails naming the base.
+"""
+import pathlib
+import sys
+
+sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+from app import _BASE_CATALOG, _HID_PREFIX_RE, _SUPPORTED_BASES # noqa: E402
+
+
+def test_every_catalog_base_routes_on_the_per_harness_url():
+ for base in _BASE_CATALOG:
+ m = _HID_PREFIX_RE.match(f"/{base}/v1/responses")
+ assert m and m.group(1) == base, f"base '{base}' does not route on /{{hid}}/v1/*"
+
+
+def test_the_stored_alias_and_chrn_ids_route_too():
+ assert "claude" in _SUPPORTED_BASES
+ assert _HID_PREFIX_RE.match("/claude/v1/responses")
+ assert _HID_PREFIX_RE.match("/chrn_" + "a" * 32 + "/v1/responses")
+
+
+def test_a_non_base_prefix_does_not_match():
+ assert _HID_PREFIX_RE.match("/claudex/v1/responses") is None
+ assert _HID_PREFIX_RE.match("/chrn_short/v1/responses") is None