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.

storage usage: a session index that could not be read is never answered as a zero total (#561)

Rudy Mizrahi Celekli0b914d62c708
diff --git a/gateway/app.py b/gateway/app.py
index f3d7ba65..7e83a394 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -9657,8 +9657,16 @@ async def storage_usage() -> dict:
# Sum checkpoint_bytes per tenant. Done client-side (not a backend aggregate): props are
# stored as strings, so a backend sum() may not coerce — this was already the fallback path
# and is the only one that behaves identically across backings.
+ # The index read is strict: find swallows a failure into [] (backing.py: "the normal read paths
+ # swallow errors and return empty"), which here is indistinguishable from an org that stores
+ # nothing, so a store that could not answer billed every org zero for that day. An empty index
+ # still answers {} — that is a true total; a failed read is not a total at all.
+ try:
+ sessions = await BACKING.graph.find_strict("HarnessSession", neq={"status": "deleted"})
+ except Exception as e: # noqa: BLE001
+ raise HTTPException(503, "session index unreadable; storage usage is unknown, retry") from e
by_org: dict[str, int] = {}
- for v in await BACKING.graph.find("HarnessSession", neq={"status": "deleted"}):
+ for v in sessions:
tenant = str(v.get("tenant") or "")
if not tenant:
continue
diff --git a/gateway/tests/test_storage_usage_total.py b/gateway/tests/test_storage_usage_total.py
new file mode 100644
index 00000000..484a7987
--- /dev/null
+++ b/gateway/tests/test_storage_usage_total.py
@@ -0,0 +1,81 @@
+"""The storage total the daily billing sweep reads is never zero because a read failed.
+
+/internal/storage-usage sums each non-deleted session's checkpoint_bytes per org, and its own
+docstring names its consumer: "the daily storage.gb_day billing sweep". It resolved the session
+index with `find`, which swallows a read failure into [] (backing.py: "the normal read paths
+swallow errors and return empty"). A store that could not answer therefore produced
+200 {"by_org": {}} — a zero storage total for every org, indistinguishable from an org that stores
+nothing. `find_strict` (added with BlobStore.get_or_raise) is the read for a caller that must not
+take a failure for nothing there. An index that is legitimately empty still answers {}: that is a
+true total, and the distinction is the point.
+"""
+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
+
+KEY = "internal-storage-key"
+HDR = {"x-harness-internal": KEY}
+
+
+def api(monkeypatch, tmp_path):
+ graph = backing.SqliteGraphStore(str(tmp_path / "graph.db"))
+ monkeypatch.setattr(gw.BACKING, "graph", graph)
+ monkeypatch.setattr(gw, "INTERNAL_KEY", KEY)
+ monkeypatch.setattr(gw.control_store, "enabled", lambda: False)
+ return TestClient(gw.app), graph
+
+
+def _session(graph, sid: str, org: str, nbytes: int, status: str = "done") -> None:
+ asyncio.run(graph.upsert("HarnessSession", sid, {
+ "tenant": org, "status": status, "checkpoint_bytes": str(nbytes)}))
+
+
+def test_a_populated_index_reports_its_real_totals(api):
+ c, graph = api
+ _session(graph, "hsessa", "org.acme", 1024)
+ _session(graph, "hsessb", "org.acme", 2048)
+ _session(graph, "hsessc", "org.other", 4096)
+ _session(graph, "hsessd", "org.acme", 8192, status="deleted") # not counted
+ r = c.get("/internal/storage-usage", headers=HDR)
+ assert r.status_code == 200, r.text
+ assert r.json() == {"by_org": {"org.acme": 3072, "org.other": 4096}}
+
+
+def test_an_empty_index_still_answers_an_empty_total(api):
+ c, _ = api
+ r = c.get("/internal/storage-usage", headers=HDR)
+ assert r.status_code == 200, r.text
+ assert r.json() == {"by_org": {}} # nothing stored is a true zero, not a failure
+
+
+def test_an_index_read_that_failed_is_never_answered_as_a_zero_total(api):
+ """The regression: the sweep must not read {} — a day it cannot measure is not a free day."""
+ c, graph = api
+ _session(graph, "hsesse", "org.acme", 1024)
+ graph._path = str(Path(graph._path).parent) # a directory: SQLite cannot open it
+ r = c.get("/internal/storage-usage", headers=HDR)
+ assert r.status_code >= 500, r.text
+ assert "by_org" not in r.text
+
+
+def test_an_index_read_that_raised_is_answered_503_retry(api, monkeypatch):
+ c, graph = api
+
+ async def find_strict(label, eq=None, neq=None):
+ raise RuntimeError("database is locked")
+ monkeypatch.setattr(graph, "find_strict", find_strict)
+ r = c.get("/internal/storage-usage", headers=HDR)
+ assert r.status_code == 503, r.text
+ assert "retry" in r.text and "by_org" not in r.text