Skip to content

Your lightweight open-source personal agent.

fix(remote): preserve pairing on failed forget and respect asset cache policy

Xubin Renfd9681ef8bc4
diff --git a/nanobot/webui/remote_instances.py b/nanobot/webui/remote_instances.py
index 7c72f56d2..7bef0c7b6 100644
--- a/nanobot/webui/remote_instances.py
+++ b/nanobot/webui/remote_instances.py
@@ -400,15 +400,19 @@ class RemoteInstances:
self._unknown_hosts.discard(key)
return {"trusted": True}
if action in {"disconnect", "remove"}:
- connection = self.connections.pop(key, None)
- if connection:
- await connection.proxy.pause()
+ pair_id = profiles[key].pair_id
if action == "remove":
self._remember_origins(profiles)
- if profiles[key].pair_id:
- self.pairing.forget(profiles[key].pair_id)
del profiles[key]
+ # Commit the directory before destroying its credentials.
+ # A failed save must leave the listed connection usable.
self._write(profiles)
+ connection = self.connections.pop(key, None)
+ if connection:
+ await connection.proxy.pause()
+ if action == "remove":
+ if pair_id:
+ self.pairing.forget(pair_id)
self._known_hosts(key).unlink(missing_ok=True)
self._unknown_hosts.discard(key)
self._unverified.pop(key, None)
diff --git a/nanobot/webui/remote_proxy.py b/nanobot/webui/remote_proxy.py
index 5d90a6bfd..30fbf9d98 100644
--- a/nanobot/webui/remote_proxy.py
+++ b/nanobot/webui/remote_proxy.py
@@ -313,10 +313,11 @@ class RemoteProxy:
and not request.rel_url.query and "set-cookie" not in upstream.headers
and "public" in upstream_cache and "immutable" in upstream_cache
and "no-store" not in upstream_cache and "private" not in upstream_cache
+ and "no-cache" not in upstream_cache and "must-revalidate" not in upstream_cache
and "application/json" not in upstream.headers.get("content-type", "")):
# Only versioned public build assets, never HTML, API data,
# credentials or media capabilities. Keep caches browser-local.
- outgoing["Cache-Control"] = "private, max-age=31536000, immutable"
+ outgoing["Cache-Control"] = re.sub(r"\bpublic\b", "private", upstream_cache)
if "application/json" in upstream.headers.get("content-type", ""):
body = bytearray()
async for chunk in upstream.aiter_bytes():
diff --git a/tests/webui/test_remote_pairing.py b/tests/webui/test_remote_pairing.py
index d806c3464..b681eafb7 100644
--- a/tests/webui/test_remote_pairing.py
+++ b/tests/webui/test_remote_pairing.py
@@ -256,6 +256,34 @@ async def test_api_pair_finalization_pins_host_and_hides_credentials(tmp_path):
assert manager.snapshot()["profiles"] == []
+async def test_failed_forget_preserves_saved_pair_for_restart_and_retry(tmp_path, monkeypatch):
+ manager = RemoteInstances(tmp_path)
+ request = read_request((await manager.action("pair_start", {}))["command"].split()[-1])
+ value = receipt(request)
+ await manager.action("pair_finish", {"id": request.id, "code": seal(request, value)})
+ directory_before = manager.path.read_bytes()
+ identity_path = manager.pairing.path(request.id) / "identity"
+ identity_before = identity_path.read_bytes()
+
+ def fail_save(_profiles):
+ raise OSError("disk full")
+
+ monkeypatch.setattr(manager, "_write", fail_save)
+ with pytest.raises(OSError, match="disk full"):
+ await manager.action("remove", {"id": request.id})
+
+ restarted = RemoteInstances(tmp_path)
+ assert restarted.path.read_bytes() == directory_before
+ assert restarted.snapshot()["profiles"][0]["id"] == request.id
+ assert identity_path.read_bytes() == identity_before
+ assert restarted.pairing.connection(request.id)["secret"] == value.secret
+ assert restarted._known_hosts(request.id).exists()
+ await restarted.action("rename", {"id": request.id, "name": "Still usable"})
+ await restarted.action("remove", {"id": request.id})
+ assert restarted.snapshot()["profiles"] == []
+ assert not identity_path.exists()
+
+
async def test_repeated_pairing_groups_one_instance_without_deleting_authorizations(tmp_path):
manager = RemoteInstances(tmp_path)
first = read_request((await manager.action("pair_start", {}))["command"].split()[-1])
diff --git a/tests/webui/test_remote_proxy.py b/tests/webui/test_remote_proxy.py
index eae80a831..65342921b 100644
--- a/tests/webui/test_remote_proxy.py
+++ b/tests/webui/test_remote_proxy.py
@@ -39,7 +39,8 @@ async def remote(tmp_path, monkeypatch):
runtime_surface="browser", runtime_capabilities_overrides=None,
)
seen, children = [], []
- state = SimpleNamespace(block=asyncio.Event(), entered=asyncio.Event(), ws_redirect=False)
+ state = SimpleNamespace(block=asyncio.Event(), entered=asyncio.Event(), ws_redirect=False,
+ asset_cache="public, max-age=31536000, immutable")
async def process(connection, request):
seen.append(request)
@@ -47,7 +48,7 @@ async def remote(tmp_path, monkeypatch):
if path.startswith("/assets/"):
response = connection.respond(200, "fixture asset")
response.headers["Content-Type"] = "application/javascript"
- response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
+ response.headers["Cache-Control"] = state.asset_cache
if "private" in request.path:
response.headers["Cache-Control"] = "private, no-store"
if "cookie" in request.path:
@@ -140,6 +141,20 @@ async def test_cache_only_versioned_public_build_assets(remote, path, cacheable)
assert "set-cookie" not in response.headers
[email protected]("directive", ["no-cache", 'no-cache="Content-Type"', "must-revalidate"])
+async def test_proxy_preserves_upstream_asset_revalidation(remote, directive):
+ remote.state.asset_cache = f"public, max-age=0, immutable, {directive}"
+ response = await remote.client.get(remote.proxy.origin + "/assets/index-AbcD1234.js")
+ assert response.status_code == 200
+ assert response.headers["cache-control"] == "no-store"
+
+
+async def test_proxy_does_not_extend_upstream_asset_freshness(remote):
+ remote.state.asset_cache = "public, max-age=60, immutable"
+ response = await remote.client.get(remote.proxy.origin + "/assets/index-AbcD1234.js")
+ assert response.headers["cache-control"] == "private, max-age=60, immutable"
+
+
async def test_remote_credentials_never_leave_the_backend(remote):
first, second = await asyncio.gather(bootstrap(remote), bootstrap(remote))
assert first["token"] != second["token"]