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(runner): opencode turns must synthesize a result at end of stream

richard-epsilla219b562ed515
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
index 52d372df..1d0f3dda 100644
--- a/docker/entrypoint.sh
+++ b/docker/entrypoint.sh
@@ -335,7 +335,9 @@ cleanup() { trap - TERM INT; for p in "${pids[@]:-}"; do kill "$p" 2>/dev/null |
trap cleanup TERM INT EXIT
avail=""; missing=""
-for b in claude codex hermes pi dsh; do
+# Derived from HR_BACKENDS, not a second hardcoded list. The literal list here missed opencode and
+# reported "backends available: none" on an instance that had it installed and working.
+for b in ${HR_BACKENDS//,/ }; do
wanted "$b" || continue
if [ -x "$(backend_bin "$b")" ]; then avail="$avail $b"; else missing="$missing $b"; fi
done
diff --git a/runner/server.py b/runner/server.py
index d7d10d7e..5eb71df9 100644
--- a/runner/server.py
+++ b/runner/server.py
@@ -2093,6 +2093,32 @@ def _hermes_prepare_env(provider: str | None, auth: Auth, cwd: str, env: dict,
return list(mcp)
+def _opencode_eof(state: dict, rc: int) -> list[dict]:
+ """Synthesize the turn's result at end of stream.
+
+ opencode emits only mid-turn events (text, reasoning, tool_use, step_start, step_finish,
+ error); the process exiting is the terminal signal. Status therefore comes from the error
+ event and the exit code together, and the final text is the last completed text part.
+
+ A non-zero exit with no error event and no text is the case that produced
+ "exit_code=1, no diagnostic output" on a live turn: say so explicitly rather than leaving the
+ trace blank."""
+ usage = state.get("_oc_usage") or {}
+ final = state.get("final", "")
+ err = state.get("_oc_error", "")
+ if err:
+ return [{"type": "result", "subtype": "error", "is_error": True,
+ "result": err, "usage": usage}]
+ if rc == 0:
+ return [{"type": "result", "subtype": "success", "is_error": False,
+ "result": final, "usage": usage}]
+ tools = state.get("_oc_tool_errors") or []
+ why = ("; ".join(t for t in tools if t)[:500]
+ or f"opencode exited {rc} without reporting an error")
+ return [{"type": "result", "subtype": "error", "is_error": True,
+ "result": final or why, "usage": usage}]
+
+
OPENCODE_PROVIDERS = {"anthropic", "openai", "azure", "openai-api", "tokenrouter"}
# opencode resolves `{env:VAR}` inside its config at load time, so the provider key is named in
@@ -2304,6 +2330,11 @@ def _opencode_to_claude(obj: dict, state: dict) -> list[dict]:
return pre
+# opencode's stream has no terminal event, so the normalizer carries an `eof` the run loop calls
+# when the process exits. See _run_turn_bg.
+_opencode_to_claude.eof = _opencode_eof # type: ignore[attr-defined]
+
+
# Registry — providers/default_model/normalize per backend. The cmd build + run loop is dispatched
# in turn(): claude/codex run through _run_turn_bg over stdout JSONL; hermes has its own driver
# (_run_hermes_bg — DB-polling, no stdout events), so it carries no normalizer.
@@ -2404,6 +2435,20 @@ def _run_turn_bg(turn_id: str, cmd: list[str], env: dict, cwd: str, normalize, m
finally:
killer.cancel()
rec["exit_code"] = rc
+ # Backends whose stream carries NO terminal event finish here: for them the process exiting IS
+ # the end of the turn. claude/codex/pi/dsh all emit something terminal of their own and set
+ # `result_ev` in the loop above, so they have no `eof` and this is skipped. opencode does not:
+ # its six event types are all mid-turn, and without this a SUCCESSFUL turn produced no final
+ # text, no usage, and a status inferred from the exit code alone.
+ eof = getattr(normalize, "eof", None)
+ if eof is not None and result_ev is None:
+ for ev in eof(state, rc):
+ ev["_ts"] = time.time()
+ with _turns_lock:
+ rec["events"].append(ev)
+ if ev.get("type") == "result":
+ result_ev = ev
+ state["final"] = state.get("final") or ev.get("result") or ""
rec["result"] = state.get("final", "")
rec["status"] = ("cancelled" if rec.get("cancelled")
else "timeout" if rec.get("capped")
diff --git a/runner/tests/test_opencode_normalize.py b/runner/tests/test_opencode_normalize.py
index d428d72a..092fe80e 100644
--- a/runner/tests/test_opencode_normalize.py
+++ b/runner/tests/test_opencode_normalize.py
@@ -270,3 +270,38 @@ def test_error_falls_back_to_the_flat_message_then_the_name():
assert s1["_oc_error"] == "flat form"
_, s2 = _run([_ev("error", error={"name": "APIError"})])
assert s2["_oc_error"] == "APIError"
+
+
+# ── end of stream: opencode has no terminal event, the process exiting is the signal ──
+from server import _opencode_eof, _opencode_to_claude # noqa: E402
+
+
+def test_the_run_loop_can_find_the_eof_hook():
+ """_run_turn_bg looks for `normalize.eof`. If this attribute goes missing the turn silently
+ loses its result event again."""
+ assert getattr(_opencode_to_claude, "eof", None) is _opencode_eof
+
+
+def test_clean_exit_reports_success_with_the_final_text_and_usage():
+ state = {"final": "the answer", "_oc_usage": {"input_tokens": 5}}
+ ev = _opencode_eof(state, 0)[0]
+ assert ev["subtype"] == "success" and ev["is_error"] is False
+ assert ev["result"] == "the answer" and ev["usage"] == {"input_tokens": 5}
+
+
+def test_an_error_event_wins_over_the_exit_code():
+ ev = _opencode_eof({"final": "", "_oc_error": "Authentication failed."}, 0)[0]
+ assert ev["is_error"] is True and ev["result"] == "Authentication failed."
+
+
+def test_a_bare_nonzero_exit_says_so_instead_of_going_blank():
+ """This is the live failure: exit 1, no error event, nothing on stderr. The trace showed
+ 'exit_code=1, no diagnostic output', which tells the user nothing."""
+ ev = _opencode_eof({"final": ""}, 1)[0]
+ assert ev["is_error"] is True
+ assert "exited 1" in ev["result"]
+
+
+def test_a_failing_tool_is_quoted_when_the_process_dies_without_an_error_event():
+ ev = _opencode_eof({"final": "", "_oc_tool_errors": ["bash: boom"]}, 1)[0]
+ assert "bash: boom" in ev["result"]