fix(webui): keep version checks on the gateway event loop (#6376)
* fix(webui): keep version checks on the gateway event loop * fix(webui): keep update channel failures factual Note: CI failed, but it’s not related to this PR.
This commit is contained in:
1 parent
29d1600837
commit
4dc695ca76
15 files changed
+118
-51
No files matched your search
@@ -782,7 +782,7 @@ async def test_version_check_route_returns_stable_payload(
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.check_for_update",
|
||||
lambda: {"updateAvailable": update_info, "releaseStatus": {"state": "available" if update_info else "current"}},
|
||||
AsyncMock(return_value={"updateAvailable": update_info, "releaseStatus": {"state": "available" if update_info else "current"}}),
|
||||
)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
@@ -793,11 +793,82 @@ async def test_version_check_route_returns_stable_payload(
|
||||
assert json.loads(response.body) == expected
|
||||
|
||||
|
||||
async def test_version_check_and_mcp_requests_share_the_gateway_loop(monkeypatch, tmp_path):
|
||||
import httpx
|
||||
from packaging.tags import Tag
|
||||
|
||||
import nanobot
|
||||
from nanobot.release_feed import PYPI_URL, ReleaseFeed
|
||||
from nanobot.releases import release_filenames
|
||||
from nanobot.security import network
|
||||
from nanobot.update_channel import UpdateChannel
|
||||
from nanobot.webui import version_check
|
||||
|
||||
gateway_loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
finish = asyncio.Event()
|
||||
requests = []
|
||||
|
||||
def resolve(url, **_kwargs):
|
||||
if url == f"{PYPI_URL}/json":
|
||||
# Settle the gateway requests on the next tick of the checking loop,
|
||||
# after discovery has tried to acquire the already-contended lock.
|
||||
asyncio.get_running_loop().call_soon(gateway_loop.call_soon_threadsafe, finish.set)
|
||||
return True, "", ("93.184.216.34",)
|
||||
|
||||
async def respond(request):
|
||||
requests.append(str(request.url))
|
||||
if request.url.host == "mcp.example":
|
||||
entered.set()
|
||||
await finish.wait()
|
||||
return httpx.Response(200, json={})
|
||||
assert str(request.url) == f"{PYPI_URL}/json"
|
||||
return httpx.Response(200, json={
|
||||
"info": {"name": "nanobot-ai", "version": "0.3.5", "requires_python": ">=3.11"},
|
||||
"urls": [{"filename": name, "size": 123, "yanked": False,
|
||||
"digests": {"sha256": "c" * 64},
|
||||
"url": f"https://files.pythonhosted.org/packages/{name}"}
|
||||
for name in sorted(release_filenames("0.3.5"))
|
||||
if name.endswith((".whl", ".tar.gz"))],
|
||||
})
|
||||
|
||||
def transport():
|
||||
return network.PinnedDNSAsyncTransport(inner=httpx.MockTransport(respond))
|
||||
|
||||
monkeypatch.setattr(network.PinnedDNSAsyncTransport, "_resolver_lock", asyncio.Lock())
|
||||
monkeypatch.setattr(network, "resolve_url_target", resolve)
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.5")
|
||||
monkeypatch.setattr(version_check, "read_update_channel", UpdateChannel)
|
||||
monkeypatch.setattr(version_check, "installation_state_dir", lambda: tmp_path)
|
||||
monkeypatch.setattr(version_check, "ReleaseFeed", lambda directory: ReleaseFeed(
|
||||
directory, transport=transport(), supported_tags=[Tag("py3", "none", "win_amd64")],
|
||||
))
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
async with httpx.AsyncClient(transport=transport(), trust_env=False) as client:
|
||||
first = asyncio.create_task(client.get("https://mcp.example/first"))
|
||||
await asyncio.wait_for(entered.wait(), timeout=5)
|
||||
second = asyncio.create_task(client.get("https://mcp.example/second"))
|
||||
await asyncio.sleep(0) # Let the second gateway request wait on the shared lock.
|
||||
try:
|
||||
response = await asyncio.wait_for(_router().dispatch(None, request, request.path), timeout=5)
|
||||
finally:
|
||||
finish.set()
|
||||
await asyncio.gather(first, second)
|
||||
subsequent = await client.get("https://mcp.example/subsequent")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body) == {"updateAvailable": None}
|
||||
assert subsequent.status_code == 200
|
||||
assert requests == ["https://mcp.example/first", "https://mcp.example/second",
|
||||
f"{PYPI_URL}/json", "https://mcp.example/subsequent"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_version_check_route_enforces_auth_and_bounds_failures(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
check = MagicMock(side_effect=RuntimeError("upstream secret body"))
|
||||
check = AsyncMock(side_effect=RuntimeError("upstream secret body"))
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", check)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
@@ -811,6 +882,7 @@ async def test_version_check_route_enforces_auth_and_bounds_failures(
|
||||
assert failed.status_code == 500
|
||||
assert json.loads(failed.body) == {"error": "version check failed"}
|
||||
assert "upstream secret body" not in failed.body.decode()
|
||||
check.assert_awaited_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("state", ["waiting_stable", "withdrawn", "unavailable", "publishing", "unsupported"])
|
||||
@@ -818,7 +890,7 @@ async def test_channel_status_is_explicit_and_old_clients_never_report_false_suc
|
||||
result = {"updateAvailable": None, "releaseStatus": {
|
||||
"channel": "stable", "revision": 2, "currentVersion": "0.3.7.dev42", "state": state,
|
||||
}}
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", lambda: result)
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", AsyncMock(return_value=result))
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
router = _router()
|
||||
response = await router.dispatch(None, request, request.path)
|
||||
@@ -834,7 +906,7 @@ async def test_version_check_owner_is_additive_read_only_and_failure_is_not_a_re
|
||||
from nanobot.webui import settings_system
|
||||
|
||||
result = {"updateAvailable": None, "releaseStatus": {"state": "current"}}
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", lambda: result)
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", AsyncMock(return_value=result))
|
||||
inspect_owner = MagicMock(return_value=SimpleNamespace(owner="pipx", records={}))
|
||||
monkeypatch.setattr(settings_system, "inspect_update_installation", inspect_owner)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
@@ -73,60 +73,60 @@ def select(channel):
|
||||
return UpdateJobService().change_channel(UpdateChannelRequest(channel=channel, revision=current.revision))
|
||||
|
||||
|
||||
def test_stable_preview_exit_wait_and_final_release(releases, monkeypatch):
|
||||
assert version_check.check_for_update()["releaseStatus"]["state"] == "current"
|
||||
async def test_stable_preview_exit_wait_and_final_release(releases, monkeypatch):
|
||||
assert (await version_check.check_for_update())["releaseStatus"]["state"] == "current"
|
||||
assert CATALOGUE_URL not in releases.requests
|
||||
select("preview")
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["updateAvailable"]["latestVersion"] == "0.3.7.dev42"
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
|
||||
releases.preview, releases.generation = "0.3.7.dev43", 2
|
||||
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7.dev43"
|
||||
assert (await version_check.check_for_update())["updateAvailable"]["latestVersion"] == "0.3.7.dev43"
|
||||
select("stable")
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["releaseStatus"]["state"] == "waiting_stable"
|
||||
assert result["updateAvailable"] is None
|
||||
releases.stable, releases.generation = "0.3.7", 3
|
||||
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7"
|
||||
assert (await version_check.check_for_update())["updateAvailable"]["latestVersion"] == "0.3.7"
|
||||
assert read_update_channel().channel == "stable"
|
||||
|
||||
|
||||
def test_preview_subscription_survives_final_release_and_next_cycle(releases, monkeypatch):
|
||||
async def test_preview_subscription_survives_final_release_and_next_cycle(releases, monkeypatch):
|
||||
select("preview")
|
||||
releases.stable = "0.3.7"
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
|
||||
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.7"
|
||||
assert (await version_check.check_for_update())["updateAvailable"]["latestVersion"] == "0.3.7"
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.7")
|
||||
assert version_check.check_for_update()["releaseStatus"]["state"] == "current"
|
||||
assert (await version_check.check_for_update())["releaseStatus"]["state"] == "current"
|
||||
assert read_update_channel().channel == "preview"
|
||||
releases.preview, releases.generation = "0.3.8.dev1", 2
|
||||
assert version_check.check_for_update()["updateAvailable"]["latestVersion"] == "0.3.8.dev1"
|
||||
assert (await version_check.check_for_update())["updateAvailable"]["latestVersion"] == "0.3.8.dev1"
|
||||
|
||||
|
||||
def test_leaving_preview_can_find_a_newer_stable_when_catalogue_is_unavailable(releases, monkeypatch):
|
||||
async def test_leaving_preview_can_find_a_newer_stable_when_catalogue_is_unavailable(releases, monkeypatch):
|
||||
select("preview")
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
|
||||
select("stable")
|
||||
releases.missing = True
|
||||
releases.stable = "0.3.7"
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["updateAvailable"]["latestVersion"] == "0.3.7"
|
||||
assert result["releaseStatus"]["state"] == "available"
|
||||
assert read_update_channel().channel == "stable"
|
||||
# A broken Preview index cannot authorize a partial release or a downgrade,
|
||||
# or turn unverified withdrawal status into a green 'up to date'.
|
||||
releases.partial = True
|
||||
assert version_check.check_for_update()["updateAvailable"] is None
|
||||
assert (await version_check.check_for_update())["updateAvailable"] is None
|
||||
releases.partial = False
|
||||
releases.stable = "0.3.6"
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["updateAvailable"] is None
|
||||
assert result["releaseStatus"]["state"] == "unavailable"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("replacement", ["older", "newer", "incompatible"])
|
||||
@pytest.mark.parametrize("channel", ["stable", "preview"])
|
||||
def test_withdrawn_running_preview_is_not_reported_up_to_date(releases, monkeypatch, replacement, channel):
|
||||
async def test_withdrawn_running_preview_is_not_reported_up_to_date(releases, monkeypatch, replacement, channel):
|
||||
select(channel)
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.7.dev42")
|
||||
releases.withdrawn = {"0.3.7.dev42": "Startup regression"}
|
||||
@@ -134,30 +134,30 @@ def test_withdrawn_running_preview_is_not_reported_up_to_date(releases, monkeypa
|
||||
releases.preview = "0.3.7.dev43"
|
||||
releases.stable = "0.3.7"
|
||||
releases.incompatible = replacement == "incompatible"
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["releaseStatus"]["state"] == "withdrawn"
|
||||
assert (result["updateAvailable"] is not None) == (replacement == "newer")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("problem,state", [("partial", "publishing"), ("offline", "unavailable"),
|
||||
("incompatible", "unsupported"), ("missing", "unavailable")])
|
||||
def test_unavailable_checks_have_feedback_and_can_recover(releases, problem, state):
|
||||
async def test_unavailable_checks_have_feedback_and_can_recover(releases, problem, state):
|
||||
if problem == "missing":
|
||||
select("preview")
|
||||
setattr(releases, problem, True)
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["updateAvailable"] is None
|
||||
assert result["releaseStatus"]["state"] == state
|
||||
assert "credential" not in str(result)
|
||||
setattr(releases, problem, False)
|
||||
assert version_check.check_for_update()["releaseStatus"]["state"] in {"current", "available"}
|
||||
assert (await version_check.check_for_update())["releaseStatus"]["state"] in {"current", "available"}
|
||||
|
||||
|
||||
def test_current_dev_does_not_implicitly_opt_in_and_older_target_is_not_offered(releases, monkeypatch):
|
||||
async def test_current_dev_does_not_implicitly_opt_in_and_older_target_is_not_offered(releases, monkeypatch):
|
||||
monkeypatch.setattr(nanobot, "__version__", "0.3.8.dev1")
|
||||
assert read_update_channel().channel == "stable"
|
||||
assert version_check.check_for_update()["releaseStatus"]["state"] == "waiting_stable"
|
||||
assert (await version_check.check_for_update())["releaseStatus"]["state"] == "waiting_stable"
|
||||
select("preview")
|
||||
result = version_check.check_for_update()
|
||||
result = await version_check.check_for_update()
|
||||
assert result["updateAvailable"] is None
|
||||
assert result["releaseStatus"]["state"] == "ahead"
|
||||
Reference in new issue
Block a user