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.

responses: a turn's record that could not be read is never answered response_not_found (#557)

Rudy Mizrahi Celekli1b379c3c2d86
diff --git a/gateway/app.py b/gateway/app.py
index 5e037d78..df5ec8af 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -10527,10 +10527,22 @@ async def harness_events(harness_id: str, request: Request):
"Connection": "keep-alive"})
+_RESPONSE_UNREADABLE = "This response could not be read just now. Try again in a moment."
+
+
@app.get("/v1/responses/{response_id}")
async def get_response(response_id: str, request: Request):
principal = await _principal(request)
- rec = await _resp_get(response_id)
+ # strict: this route is the ONLY completion signal a background poller has (see
+ # `_reconcile_response`), so a record the store could not read must never be answered
+ # response_not_found — a permanent answer to a transient failure. 503 says "ask again";
+ # a record that is genuinely not there is still a 404.
+ try:
+ rec = await _resp_get(response_id, strict=True)
+ except backing.BlobUnavailable as e:
+ # the store could not answer; any other exception is a fault in this route and stays a 500
+ print(f"[responses] a record could not be read rid={response_id}: {type(e).__name__}", flush=True)
+ raise uhp_error(503, "server_error", _RESPONSE_UNREADABLE) from e
if not rec:
raise uhp_error(404, "response_not_found", "No response with that id.", "response_id")
# Object-level ownership (LIVE-B): one org must not read another's response. 404 (not 403)
diff --git a/gateway/tests/test_calibration_scope.py b/gateway/tests/test_calibration_scope.py
index c7bdf89b..c32ea42c 100644
--- a/gateway/tests/test_calibration_scope.py
+++ b/gateway/tests/test_calibration_scope.py
@@ -60,7 +60,7 @@ def world(monkeypatch):
async def vertex_get(vid):
return dict(SESSIONS[vid]) if vid in SESSIONS else (dict(harnesses[vid]) if vid in harnesses else None)
- async def resp_get(rid):
+ async def resp_get(rid, *, strict=False):
return dict(RESPONSES[rid]) if rid in RESPONSES else None
async def list_by_org(label, org):
diff --git a/gateway/tests/test_response_read_failure_is_not_not_found.py b/gateway/tests/test_response_read_failure_is_not_not_found.py
new file mode 100644
index 00000000..62be0905
--- /dev/null
+++ b/gateway/tests/test_response_read_failure_is_not_not_found.py
@@ -0,0 +1,97 @@
+"""A turn's record that could not be read is never answered response_not_found.
+
+`GET /v1/responses/{id}` is, in `_reconcile_response`'s own words, "the ONLY completion signal a
+background poller has". It read the record through `_resp_get` without `strict`, and the normal read
+paths swallow errors and return empty (`backing.py`: "the normal read paths swallow errors and return
+empty"), so a store that could not answer looked exactly like a turn that does not exist and the route
+answered 404 response_not_found — a permanent answer to a transient failure.
+
+What these pin, on the self-hosted file blob store itself:
+ a record whose read FAILS answers 503 server_error, never 404 response_not_found;
+ a record that is genuinely NOT THERE still answers 404 response_not_found.
+The second is the point: the fix must not turn the legitimate not-found path into a failure.
+"""
+from __future__ import annotations
+
+import asyncio
+import os
+import sys
+from pathlib import Path
+
+import pytest
+from fastapi.testclient import TestClient
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+os.environ.setdefault("HR_BACKING", "local")
+import app as gw # noqa: E402
+import backing # noqa: E402
+
+ORG = "local"
+_n = [0]
+
+
+def stores(monkeypatch, tmp_path):
+ graph = backing.SqliteGraphStore(str(tmp_path / "graph.db"))
+ blob = backing.FileBlobStore(str(tmp_path / "blobs"))
+ monkeypatch.setattr(gw.BACKING, "graph", graph)
+ monkeypatch.setattr(gw.BACKING, "blob", blob)
+ monkeypatch.setattr(gw, "_prefix_from_vertex", lambda sid, v: None)
+ monkeypatch.setattr(gw, "VG_GATEWAY_URL", "")
+
+ async def principal(request):
+ return {"org": ORG, "member": "me@local"}
+ monkeypatch.setattr(gw, "_principal", principal)
+ return graph, blob, tmp_path
+
+
+def _turn() -> tuple[str, str]:
+ """One finished turn, written the way a turn writes it, with nothing cached in memory."""
+ _n[0] += 1
+ sid = f"hsessrespread{_n[0]}{os.urandom(3).hex()}"
+ rid = f"resp_respread{_n[0]}{os.urandom(3).hex()}"
+ rec = {"id": rid, "object": "response", "created_at": 1792560576, "status": "completed",
+ "error": None, "_session_id": sid, "_org": ORG,
+ "_input": [{"role": "user", "content": "ask"}],
+ "output": [{"type": "message", "role": "assistant",
+ "content": [{"type": "output_text", "text": "answer"}]}]}
+ asyncio.run(gw._resp_put(rid, rec, ORG, sid, None, "completed", 1792560576.0, True))
+ asyncio.run(gw._vertex_upsert(sid, {"tenant": ORG, "status": "done", "turn_status": "done",
+ "member_id": "me@local", "running_response_id": rid}))
+ gw._resp_cache_forget(rid)
+ return sid, rid
+
+
+def _unreadable(tmp_path: Path, rid: str) -> None:
+ """The record's file is there and cannot be read (a directory in its place: EISDIR, as EIO or
+ EMFILE would be) — not the same thing as a record that is not there."""
+ p = tmp_path / "blobs" / gw.RESP_BLOB_KB / "responses" / f"{rid}.json"
+ p.rename(p.with_suffix(".aside"))
+ p.mkdir()
+
+
+def test_a_turn_reads_back(stores):
+ _, rid = _turn()
+ r = TestClient(gw.app).get(f"/v1/responses/{rid}")
+ assert r.status_code == 200, r.text
+ assert r.json()["id"] == rid and r.json()["status"] == "completed"
+
+
+def test_a_record_that_cannot_be_read_is_not_answered_not_found(stores):
+ _, _, tmp = stores
+ _, rid = _turn()
+ _unreadable(tmp, rid)
+ r = TestClient(gw.app).get(f"/v1/responses/{rid}")
+ assert r.status_code == 503, r.text
+ err = r.json()["error"]
+ assert err["type"] == "server_error" and err["code"] == "server_error"
+ assert err["code"] != "response_not_found"
+ assert err["message"] == "This response could not be read just now. Try again in a moment."
+
+
+def test_a_response_that_is_not_there_is_still_not_found(stores):
+ _turn() # a real turn exists; this id is not it
+ r = TestClient(gw.app).get("/v1/responses/resp_nosuchrecordatall")
+ assert r.status_code == 404, r.text
+ err = r.json()["error"]
+ assert err["code"] == "response_not_found" and err["param"] == "response_id"
diff --git a/gateway/tests/test_workspace_scope.py b/gateway/tests/test_workspace_scope.py
index ae87ab6c..322239c0 100644
--- a/gateway/tests/test_workspace_scope.py
+++ b/gateway/tests/test_workspace_scope.py
@@ -55,7 +55,7 @@ def world(monkeypatch):
async def vertex_get(vid):
return dict(GRAPH[vid]) if vid in GRAPH else None
- async def resp_get(rid):
+ async def resp_get(rid, *, strict=False):
return dict(RESP[rid]) if rid in RESP else None
async def upsert(label, vid, props, **kw):