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.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
fix(files): invalidate PDF previews when source bytes change (#291)
Yominmafdbf9b3c4b75
diff --git a/gateway/app.py b/gateway/app.pyindex bb4eec0c..814a07c3 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -9641,11 +9641,10 @@ def _convert_to_pdf(data: bytes, ext: str) -> bytes | None:async def container_file_pdf(container_id: str, file_id: str, request: Request):"""Return a PDF rendering of an office file (cached). Lets the UI preview pptx/ppt/odp inline."""await _owned_session(request, container_id) # V1C02-004: container_id IS the session id- cache_key = f"previews/{container_id}/{file_id}.pdf"- cached = await _blob_get(cache_key, kb=RESP_BLOB_KB)- if cached is not None:- return Response(content=cached, media_type="application/pdf")- got = await _container_file_bytes(container_id, file_id) # cfile_… blob or wf_… workspace path+ # File ids are stable across writes to the same path. Resolve the current source before+ # consulting the preview cache, then key the PDF by its bytes so a replacement cannot reuse+ # the previous rendering and a deleted source cannot be kept alive by its preview.+ got = await _container_file_bytes(container_id, file_id)if got is None:raise HTTPException(404, "file not found")data, _media, fname = got@@ -9654,6 +9653,12 @@ async def container_file_pdf(container_id: str, file_id: str, request: Request):return Response(content=data, media_type="application/pdf")if ext not in _PDF_CONVERTIBLE:raise HTTPException(415, f"no pdf preview for .{ext}")+ source_sha = hashlib.sha256(data).hexdigest()+ cache_prefix = f"previews/{container_id}/{file_id}/"+ cache_key = f"{cache_prefix}{source_sha}.pdf"+ cached = await _blob_get(cache_key, kb=RESP_BLOB_KB)+ if cached is not None:+ return Response(content=cached, media_type="application/pdf")if not _SOFFICE:# Distinguish "this build cannot preview documents" from "this document failed to# convert" — they need different actions, and one generic error told the user neither.@@ -9661,7 +9666,21 @@ async def container_file_pdf(container_id: str, file_id: str, request: Request):pdf = await asyncio.to_thread(_convert_to_pdf, data, ext)if pdf is None:raise HTTPException(502, f"could not convert this .{ext} file")- await _blob_put(cache_key, pdf, kb=RESP_BLOB_KB) # cache so repeat previews are instant+ stored = await _blob_put(cache_key, pdf, kb=RESP_BLOB_KB)+ if stored:+ # Content-addressed keys avoid races through a mutable "current preview" pointer. Remove+ # older revisions after a successful write so repeated edits do not leave unbounded PDF+ # copies behind. This is storage hygiene only; cache cleanup is best-effort.+ try:+ objects = await _blob_list_all(cache_prefix, kb=RESP_BLOB_KB, hard_cap=1000)+ stale = [str(item.get("file_id") or "") for item in objects+ if item.get("file_id") and item.get("file_id") != cache_key]+ legacy_key = f"previews/{container_id}/{file_id}.pdf"+ stale.append(legacy_key)+ await asyncio.gather(*[_blob_delete(key, kb=RESP_BLOB_KB) for key in stale],+ return_exceptions=True)+ except Exception: # noqa: BLE001 — cleanup must never fail an otherwise valid preview+ passreturn Response(content=pdf, media_type="application/pdf")diff --git a/gateway/tests/test_pdf_preview_cache.py b/gateway/tests/test_pdf_preview_cache.pynew file mode 100644index 00000000..977a292a--- /dev/null+++ b/gateway/tests/test_pdf_preview_cache.py@@ -0,0 +1,139 @@+"""PDF previews follow the current source when stable file IDs are replaced."""+import asyncio+import hashlib+import threading++import pytest+from fastapi import HTTPException++import app as gw+++async def test_replacement_uses_new_pdf_and_deleted_source_does_not_hit_cache(monkeypatch):+ sid, fid = "hsess_preview", "wf_c2xpZGVzLm9kcA"+ source = [b"version A"]+ filename = ["slides.odp"]+ cache = {}+ conversions = []+ deleted = []++ async def owned(*_args):+ return None++ async def file_bytes(_sid, _fid):+ if source[0] is None:+ return None+ return source[0], "application/vnd.oasis.opendocument.presentation", filename[0]++ async def blob_get(key, kb=None):+ assert kb == gw.RESP_BLOB_KB+ return cache.get(key)++ async def blob_put(key, data, kb=None):+ assert kb == gw.RESP_BLOB_KB+ cache[key] = data+ return True++ async def blob_list(prefix, kb=None, hard_cap=None):+ assert kb == gw.RESP_BLOB_KB and hard_cap == 1000+ return [{"file_id": key} for key in cache if key.startswith(prefix)]++ async def blob_delete(key, kb=None):+ assert kb == gw.RESP_BLOB_KB+ deleted.append(key)+ cache.pop(key, None)+ return True++ def convert(data, _ext):+ conversions.append(data)+ return b"pdf:" + data++ monkeypatch.setattr(gw, "_owned_session", owned)+ monkeypatch.setattr(gw, "_container_file_bytes", file_bytes)+ monkeypatch.setattr(gw, "_blob_get", blob_get)+ monkeypatch.setattr(gw, "_blob_put", blob_put)+ monkeypatch.setattr(gw, "_blob_list_all", blob_list)+ monkeypatch.setattr(gw, "_blob_delete", blob_delete)+ monkeypatch.setattr(gw, "_convert_to_pdf", convert)+ monkeypatch.setattr(gw, "_SOFFICE", "soffice")++ first = await gw.container_file_pdf(sid, fid, request=None)+ assert first.body == b"pdf:version A"+ source[0] = b"version B"+ second = await gw.container_file_pdf(sid, fid, request=None)+ assert second.body == b"pdf:version B"+ again = await gw.container_file_pdf(sid, fid, request=None)+ assert again.body == b"pdf:version B"+ assert conversions == [b"version A", b"version B"]+ assert any(key.endswith(".pdf") and key == f"previews/{sid}/{fid}.pdf" for key in deleted)+ assert len(cache) == 1 and next(iter(cache.values())) == b"pdf:version B"++ filename[0] = "slides.txt"+ with pytest.raises(HTTPException) as unsupported:+ await gw.container_file_pdf(sid, fid, request=None)+ assert unsupported.value.status_code == 415++ source[0] = None+ with pytest.raises(HTTPException) as exc:+ await gw.container_file_pdf(sid, fid, request=None)+ assert exc.value.status_code == 404+++async def test_concurrent_versions_never_overwrite_each_others_cache(monkeypatch):+ sid, fid = "hsess_race", "wf_c2xpZGVzLm9kcA"+ source = [b"version A"]+ cache = {}+ a_started = threading.Event()+ release_a = threading.Event()++ async def owned(*_args):+ return None++ async def file_bytes(_sid, _fid):+ return source[0], "application/vnd.oasis.opendocument.presentation", "slides.odp"++ async def blob_get(key, kb=None):+ return cache.get(key)++ async def blob_put(key, data, kb=None):+ cache[key] = data+ return True++ async def blob_list(prefix, kb=None, hard_cap=None):+ return [{"file_id": key} for key in cache if key.startswith(prefix)]++ async def blob_delete(key, kb=None):+ cache.pop(key, None)+ return True++ def convert(data, _ext):+ if data == b"version A":+ a_started.set()+ assert release_a.wait(timeout=10)+ return b"pdf:" + data++ monkeypatch.setattr(gw, "_owned_session", owned)+ monkeypatch.setattr(gw, "_container_file_bytes", file_bytes)+ monkeypatch.setattr(gw, "_blob_get", blob_get)+ monkeypatch.setattr(gw, "_blob_put", blob_put)+ monkeypatch.setattr(gw, "_blob_list_all", blob_list)+ monkeypatch.setattr(gw, "_blob_delete", blob_delete)+ monkeypatch.setattr(gw, "_convert_to_pdf", convert)+ monkeypatch.setattr(gw, "_SOFFICE", "soffice")++ stale_request = asyncio.create_task(gw.container_file_pdf(sid, fid, request=None))+ assert await asyncio.to_thread(a_started.wait, 10)+ source[0] = b"version B"+ fresh = await gw.container_file_pdf(sid, fid, request=None)+ assert fresh.body == b"pdf:version B"+ release_a.set()+ assert (await stale_request).body == b"pdf:version A"++ # A finishing last may evict B's cached object as best-effort cleanup, but it cannot replace+ # B's bytes under B's key. A later request therefore always returns B, even if reconversion is needed.+ latest = await gw.container_file_pdf(sid, fid, request=None)+ assert latest.body == b"pdf:version B"+ current_key = f"previews/{sid}/{fid}/{hashlib.sha256(b'version B').hexdigest()}.pdf"+ assert cache[current_key] == b"pdf:version B"