Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(show): harden public dep cache keys

cyh0734aec9961f
diff --git a/tests/test_ui_show_pages.py b/tests/test_ui_show_pages.py
index 3ff716530..1669da495 100644
--- a/tests/test_ui_show_pages.py
+++ b/tests/test_ui_show_pages.py
@@ -22,6 +22,13 @@ from vibe import remote_access
from vibe.ui_server import app
[email protected](autouse=True)
+def _clear_show_runtime_public_dep_registry():
+ from vibe import ui_server
+
+ ui_server._SHOW_RUNTIME_PUBLIC_DEP_REGISTRY.clear()
+
+
class _FakeShowRuntimeManager:
def __init__(
self,
@@ -448,6 +455,72 @@ def test_show_runtime_public_dep_proxy_registers_sibling_chunks(monkeypatch, tmp
assert manager.calls[-1][1] == "/sessions/ses123/app/node_modules/.vite/deps/chunk-OUYO74D4.js?v=108951fb"
+def test_show_runtime_public_dep_proxy_derives_chunk_paths_when_registry_is_cold(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config(tmp_path)
+ _create_show_page("ses123", "private")
+ manager = _FakeShowRuntimeManager(
+ body=b"export default {}",
+ extra_headers={
+ "content-type": "text/javascript",
+ "cache-control": "no-cache",
+ },
+ )
+ set_show_runtime_manager_for_tests(manager)
+ try:
+ original = app.test_client().get(
+ "/show/ses123/node_modules/.vite/deps/react-dom_client.js?v=d6d38251",
+ base_url="http://127.0.0.1:5123",
+ )
+ chunk = app.test_client().get(
+ "/_show-runtime/deps/d6d38251/chunk-OUYO74D4.js?v=108951fb",
+ base_url="http://127.0.0.1:5123",
+ )
+ finally:
+ set_show_runtime_manager_for_tests(None)
+
+ assert original.status_code == 302
+ assert chunk.status_code == 200
+ assert manager.calls[-1][1] == "/sessions/ses123/app/node_modules/.vite/deps/chunk-OUYO74D4.js?v=108951fb"
+
+
+def test_show_runtime_unversioned_vendor_deps_stay_session_scoped(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config(tmp_path)
+ _create_show_page("ses123", "private")
+ manager = _FakeShowRuntimeManager(
+ body=b"export default {}",
+ extra_headers={
+ "content-type": "text/javascript",
+ "cache-control": "no-cache",
+ },
+ )
+ set_show_runtime_manager_for_tests(manager)
+ try:
+ response = app.test_client().get(
+ "/show/ses123/node_modules/.vite/deps/react.js",
+ base_url="http://127.0.0.1:5123",
+ )
+ finally:
+ set_show_runtime_manager_for_tests(None)
+
+ assert response.status_code == 200
+ assert response.headers["cache-control"] == "no-cache"
+ assert "location" not in response.headers
+
+
+def test_show_runtime_public_dep_rejects_unversioned_cache_key(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config(tmp_path)
+
+ response = app.test_client().get(
+ "/_show-runtime/deps/unversioned/react.js",
+ base_url="http://127.0.0.1:5123",
+ )
+
+ assert response.status_code == 404
+
+
def test_show_runtime_relocated_vendor_deps_are_cacheable(monkeypatch, tmp_path):
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
_save_config(tmp_path)
@@ -580,6 +653,60 @@ def test_show_runtime_source_rewrites_dep_imports_to_public_paths(monkeypatch, t
assert public_dep.headers["cache-control"] == "public, max-age=31536000, immutable"
+def test_show_runtime_source_preserves_dot_vite_dep_import_paths(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config(tmp_path)
+ _create_show_page("ses123", "private")
+ manager = _FakeShowRuntimeManager(
+ body=b'import "/.vite/deps/react.js?v=d6d38251";',
+ extra_headers={
+ "content-type": "text/javascript",
+ "cache-control": "no-cache",
+ },
+ )
+ set_show_runtime_manager_for_tests(manager)
+ try:
+ response = app.test_client().get(
+ "/show/ses123/src/main.tsx?t=1780732068677",
+ base_url="http://127.0.0.1:5123",
+ )
+ public_dep = app.test_client().get(
+ "/_show-runtime/deps/d6d38251/react.js?v=d6d38251",
+ base_url="http://127.0.0.1:5123",
+ )
+ finally:
+ set_show_runtime_manager_for_tests(None)
+
+ assert response.status_code == 200
+ assert public_dep.status_code == 200
+ assert manager.calls[-1][1] == "/sessions/ses123/app/.vite/deps/react.js?v=d6d38251"
+
+
+def test_show_runtime_source_keeps_unversioned_dep_imports_session_scoped(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config(tmp_path)
+ _create_show_page("ses123", "private")
+ manager = _FakeShowRuntimeManager(
+ body=b'import "/node_modules/.vite/deps/react.js";',
+ extra_headers={
+ "content-type": "text/javascript",
+ "cache-control": "no-cache",
+ },
+ )
+ set_show_runtime_manager_for_tests(manager)
+ try:
+ response = app.test_client().get(
+ "/show/ses123/src/main.tsx?t=1780732068677",
+ base_url="http://127.0.0.1:5123",
+ )
+ finally:
+ set_show_runtime_manager_for_tests(None)
+
+ assert response.status_code == 200
+ assert response.content == b'import "/node_modules/.vite/deps/react.js";'
+ assert response.headers["cache-control"] == "no-cache"
+
+
def test_show_runtime_source_keeps_partial_js_responses_unmodified(monkeypatch, tmp_path):
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
_save_config(tmp_path)
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index 98cae539c..ae62c0368 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -5401,11 +5401,11 @@ async def show_session_prewarm(session_id: str):
@app.route("/_show-runtime/deps/<version>/<path:asset_name>", methods=["GET", "HEAD"])
async def show_runtime_public_dep(version: str, asset_name: str):
- if not re.fullmatch(r"[A-Za-z0-9_-]+", version or ""):
+ if not re.fullmatch(r"[A-Za-z0-9_-]+", version or "") or version == "unversioned":
return _show_page_not_found_response()
if not re.fullmatch(r"[[email protected]]+", asset_name or ""):
return _show_page_not_found_response()
- runtime_path = _SHOW_RUNTIME_PUBLIC_DEP_REGISTRY.get((version, asset_name))
+ runtime_path = _public_show_runtime_dep_runtime_path(version, asset_name)
if not runtime_path:
return _show_page_not_found_response()
if request._request.url.query and "?" not in runtime_path:
@@ -5499,7 +5499,12 @@ async def _show_page_runtime_response(
else:
if proxied.status_code == 200:
content = _rewrite_show_runtime_public_deps(content, response_headers, session_id=session_id)
- _apply_show_runtime_cache_headers(asset_path, response_headers, status_code=proxied.status_code)
+ _apply_show_runtime_cache_headers(
+ asset_path,
+ response_headers,
+ status_code=proxied.status_code,
+ query=starlette_request.url.query,
+ )
return FastAPIResponse(content=content, status_code=proxied.status_code, headers=response_headers)
@@ -5522,7 +5527,7 @@ def _strip_mutated_show_runtime_headers(headers: dict[str, str]) -> None:
headers["Cache-Control"] = "no-store"
-def _apply_show_runtime_cache_headers(asset_path: str, headers: dict[str, str], *, status_code: int) -> None:
+def _apply_show_runtime_cache_headers(asset_path: str, headers: dict[str, str], *, status_code: int, query: str | None) -> None:
relative = (asset_path or "").strip("/")
if not relative or relative == "index.html":
_remove_response_header(headers, "cache-control")
@@ -5531,6 +5536,8 @@ def _apply_show_runtime_cache_headers(asset_path: str, headers: dict[str, str],
if status_code < 200 or status_code >= 300:
return
if _is_show_runtime_immutable_asset(relative):
+ if not _show_runtime_dep_version(query):
+ return
_remove_response_header(headers, "cache-control")
_remove_response_header(headers, "set-cookie")
headers["Cache-Control"] = _SHOW_RUNTIME_IMMUTABLE_CACHE_CONTROL
@@ -5541,6 +5548,7 @@ def _should_redirect_to_public_show_runtime_dep(asset_path: str, starlette_reque
starlette_request.method == "GET"
and 200 <= status_code < 300
and _is_show_runtime_immutable_asset_path(asset_path)
+ and bool(_show_runtime_dep_version(starlette_request.url.query))
and bool(_public_show_runtime_dep_path(asset_path, starlette_request.url.query))
)
@@ -5552,6 +5560,20 @@ def _register_public_show_runtime_dep(asset_path: str, query: str | None, runtim
return public_path
+def _public_show_runtime_dep_runtime_path(version: str, asset_name: str) -> str | None:
+ runtime_path = _SHOW_RUNTIME_PUBLIC_DEP_REGISTRY.get((version, asset_name))
+ if runtime_path:
+ return runtime_path
+ if not re.fullmatch(r"chunk-[A-Za-z0-9_-]+\.js", asset_name or ""):
+ return None
+ for (registered_version, _registered_name), registered_path in _SHOW_RUNTIME_PUBLIC_DEP_REGISTRY.items():
+ if registered_version != version:
+ continue
+ base_path = registered_path.split("?", 1)[0].rsplit("/", 1)[0]
+ return f"{base_path}/{quote(asset_name, safe='')}"
+ return None
+
+
def _public_show_runtime_dep_path(asset_path: str, query: str | None = None) -> str:
version, name = _public_show_runtime_dep_key(asset_path, query)
if not version or not name:
@@ -5564,7 +5586,9 @@ def _public_show_runtime_dep_key(asset_path: str, query: str | None = None) -> t
if not _is_show_runtime_immutable_asset_path(relative):
return "", ""
name = Path(relative).name
- version = _show_runtime_dep_version(query) or "unversioned"
+ version = _show_runtime_dep_version(query)
+ if not version:
+ return "", ""
return version, name
@@ -5590,10 +5614,12 @@ def _rewrite_show_runtime_public_deps(content: bytes, headers: dict[str, str], *
return content
def replace(match: re.Match[str]) -> str:
- version = match.group("version") or "unversioned"
+ version = match.group("version")
+ if not version:
+ return match.group(0)
name = Path(match.group("path")).name
public_path = f"{_SHOW_RUNTIME_PUBLIC_DEP_PREFIX}/{quote(version, safe='')}/{quote(name, safe='')}"
- dep_path = match.group("path").lstrip("./")
+ dep_path = _normalize_show_runtime_dep_import_path(match.group("path"))
_SHOW_RUNTIME_PUBLIC_DEP_REGISTRY[(version, name)] = f"/sessions/{quote(session_id, safe='')}/app/{quote(dep_path, safe='/@:-._~')}"
return f"{match.group('quote')}{public_path}{match.group('rest')}{match.group('quote')}"
@@ -5604,6 +5630,14 @@ def _rewrite_show_runtime_public_deps(content: bytes, headers: dict[str, str], *
return rewritten.encode("utf-8")
+def _normalize_show_runtime_dep_import_path(dep_path: str) -> str:
+ if dep_path.startswith("/"):
+ return dep_path[1:]
+ if dep_path.startswith("./"):
+ return dep_path[2:]
+ return dep_path
+
+
def _register_public_show_runtime_dep_siblings(
content: bytes,
headers: dict[str, str],
@@ -5622,7 +5656,7 @@ def _register_public_show_runtime_dep_siblings(
for match in _SHOW_RUNTIME_PUBLIC_DEP_SIBLING_RE.finditer(text):
sibling_name = match.group("name")
_SHOW_RUNTIME_PUBLIC_DEP_REGISTRY.setdefault(
- (version or "unversioned", sibling_name),
+ (version, sibling_name),
f"{base_runtime_path}/{quote(sibling_name, safe='')}",
)