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.

share: a session read that fails after the token lookup answers 503, as the lookup does, never a 500 or a gone link (#577)

github-actions[bot]1220e84b1b39
diff --git a/gateway/app.py b/gateway/app.py
index 85d9cf6b..ef214826 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -11176,7 +11176,13 @@ async def _share_sid(token: str) -> str | None:
return None
# A token lookup is not an authorization grant: another replica may have revoked the
# share since this one populated its cache (or a graph index may lag the point read).
- v = await _vertex_get(sid)
+ # The same rule as the lookup: a read that could not be performed is a 503, never the 404 a
+ # recipient reads as revoked, and it leaves the cached token as it was.
+ try:
+ v = await _vertex_get(sid)
+ except Exception as e: # noqa: BLE001
+ print(f"[share] the shared session could not be read: {type(e).__name__}", flush=True)
+ raise uhp_error(503, "server_error", _SHARE_UNREADABLE) from e
if (not v or str(v.get("shared") or "") != "1" or v.get("status") == "deleted"
or str(v.get("share_token") or "") != tok):
_SHARE_TOKEN_CACHE.pop(tok, None)
diff --git a/gateway/tests/test_share_selfhost.py b/gateway/tests/test_share_selfhost.py
index 205874e1..a2f1d8da 100644
--- a/gateway/tests/test_share_selfhost.py
+++ b/gateway/tests/test_share_selfhost.py
@@ -147,6 +147,33 @@ def test_a_transient_index_failure_does_not_404_the_link_or_poison_the_cache(api
"the failure must not be negative-cached: the retry after recovery has to resolve")
+def test_a_failed_point_read_after_the_lookup_answers_503_and_keeps_the_cache(api, monkeypatch):
+ """The lookup found the token; the session read that follows it could not be performed. That
+ is a 503 too, never a 404: before, the exception surfaced as a 500, which the page could tell
+ from gone only by accident. The cached token stays, so the retry after recovery resolves."""
+ sid = "hsessshare" + os.urandom(4).hex()
+ _session(sid)
+ url = api.post(f"/v1/sessions/{sid}/share").json()["url"]
+ tok = url.rsplit("/share/", 1)[-1]
+ assert api.get(url + "/meta").status_code == 200
+
+ real = gw.BACKING.graph.get
+ broken = {"on": True}
+
+ async def flaky(vid, label=None):
+ if broken["on"] and vid == sid:
+ raise RuntimeError("store unreachable")
+ return await real(vid, label)
+ monkeypatch.setattr(gw.BACKING.graph, "get", flaky)
+
+ r = api.get(url + "/meta")
+ assert r.status_code == 503, "a session read that failed must not be reported as a share that is gone"
+ assert gw._SHARE_TOKEN_CACHE.get(tok, (0, ""))[1] == sid, "the cached token is left as it was"
+
+ broken["on"] = False
+ assert api.get(url + "/meta").status_code == 200
+
+
def test_a_genuinely_unknown_token_is_still_404_and_still_cached(api):
"""The distinction the fix turns on: an empty strict answer IS "no such token", stays a 404,
and stays cacheable — only a failed read is excluded from the cache."""