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): reading a session file by path must survive workspace reaping

richard-epsillac7b908a9d190
diff --git a/gateway/app.py b/gateway/app.py
index bdfdbab5..5baeae2d 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -6833,6 +6833,28 @@ async def _ws_files(sid: str) -> dict[str, bytes] | None:
return out
+async def _cfile_by_name(sid: str) -> dict[str, str]:
+ """filename -> captured `cfile_…` id for this session, from the blob metas.
+
+ ONE resolver for both file routes. The listing built this inline while the by-path read knew
+ nothing about it, which is how a session could LIST dashboard.json and 404 it in the same
+ breath once the live workspace aged out."""
+ out: dict[str, str] = {}
+ listing = await _blob_list(f"containers/{sid}/", limit=200, kb=RESP_BLOB_KB)
+ metas = [i for i in (listing.get("items") or []) if str(i.get("file_id", "")).endswith(".meta")]
+ for it in metas[:200]:
+ meta_b = await _blob_get(it["file_id"], kb=RESP_BLOB_KB)
+ if not meta_b:
+ continue
+ try:
+ fname = json.loads(meta_b).get("filename")
+ except Exception: # noqa: BLE001
+ continue
+ if fname:
+ out[str(fname)] = it["file_id"].rsplit("/", 1)[-1][: -len(".meta")]
+ return out
+
+
async def _container_file_bytes(container_id: str, file_id: str) -> tuple[bytes, str, str] | None:
"""→ (data, media_type, filename) for either id form: a captured `cfile_…` blob, or a synthetic
`wf_…` workspace-path id extracted from the session checkpoint."""
@@ -6964,6 +6986,27 @@ async def read_session_file(sid: str, path: str, request: Request) -> Response:
await _owned_session(request, sid)
data = await BACKING.workspace.read(sid, path)
if data is None:
+ # FALL BACK TO THE DURABLE COPY. The live workspace is reaped after HR_WORKSPACE_TTL_HOURS,
+ # but a file this session PRODUCED was captured to a blob and is still listed by the route
+ # next door — so the listing showed dashboard.json while this route 404'd it, and the
+ # dashboard kit (which fetches by path) said "no dashboard has been built for this
+ # conversation" about a dashboard that existed. Seen live on a 12-day-old session.
+ # Also accepts a file id as the path, so an id from the listing round-trips here.
+ got = None
+ if path.startswith(("cfile_", "wf_")):
+ got = await _container_file_bytes(sid, path)
+ else:
+ try:
+ byname = await _cfile_by_name(sid)
+ except Exception: # noqa: BLE001 — blob store unavailable: keep the honest 404
+ byname = {}
+ cid = byname.get(path) or byname.get(path.rsplit("/", 1)[-1])
+ if cid:
+ got = await _container_file_bytes(sid, cid)
+ if got is not None:
+ data, media, _fname = got
+ return Response(content=data, media_type=media,
+ headers={"cache-control": "no-store"})
raise uhp_error(404, "file_not_found",
f"No file '{path}' in this session's workspace.", "path")
media = mimetypes.guess_type(path)[0] or "application/octet-stream"
diff --git a/gateway/tests/test_session_file_fallback.py b/gateway/tests/test_session_file_fallback.py
new file mode 100644
index 00000000..9e2c737e
--- /dev/null
+++ b/gateway/tests/test_session_file_fallback.py
@@ -0,0 +1,65 @@
+"""Reading a session file by PATH must survive the workspace being reaped.
+
+The live workspace is garbage-collected after HR_WORKSPACE_TTL_HOURS (72h by default), but a file
+the session PRODUCED was captured to a durable blob and stays in the listing. Before this, the
+by-path route read only the live directory, so one session could LIST dashboard.json and 404 it in
+the same breath — and the dashboard kit, which fetches by path, told the user "no dashboard has
+been built for this conversation" about a dashboard that existed. Seen live on a 12-day-old
+session on the public VM.
+"""
+import pathlib
+import sys
+
+import pytest
+
+sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
+import app as A # noqa: E402
+
+
+async def test_by_path_falls_back_to_the_captured_blob(monkeypatch):
+ sid, body = "hsess_reaped", b'{"panels":[1,2,3]}'
+
+ class _Gone:
+ async def read(self, *_a, **_k):
+ return None # workspace reaped
+
+ monkeypatch.setattr(A.BACKING, "workspace", _Gone(), raising=False)
+ monkeypatch.setattr(A, "_owned_session", _ok := (lambda *a, **k: _noop()), raising=False)
+
+ async def _noop():
+ return ("org", {})
+
+ async def _byname(_sid):
+ assert _sid == sid
+ return {"dashboard.json": "cfile_abc"}
+
+ async def _bytes(_sid, fid):
+ return (body, "application/json", "dashboard.json") if fid == "cfile_abc" else None
+
+ monkeypatch.setattr(A, "_cfile_by_name", _byname)
+ monkeypatch.setattr(A, "_container_file_bytes", _bytes)
+
+ r = await A.read_session_file(sid, "dashboard.json", request=None)
+ assert r.status_code == 200 and r.body == body
+
+
+async def test_a_file_that_never_existed_still_404s(monkeypatch):
+ """The fallback must not turn every miss into a success."""
+ class _Gone:
+ async def read(self, *_a, **_k):
+ return None
+
+ async def _noop():
+ return ("org", {})
+
+ monkeypatch.setattr(A.BACKING, "workspace", _Gone(), raising=False)
+ monkeypatch.setattr(A, "_owned_session", lambda *a, **k: _noop(), raising=False)
+ monkeypatch.setattr(A, "_cfile_by_name", lambda _s: _empty())
+
+ async def _empty():
+ return {}
+
+ with pytest.raises(Exception):
+ await A.read_session_file("hsess_x", "nope.json", request=None)