The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): keep other requests answering while Web Access builds and registers
emozilla33058aff8bd9
diff --git a/hermes_cli/web_routers/webapp_access.py b/hermes_cli/web_routers/webapp_access.pyindex fae6abd4fe0..f1518dc8d04 100644--- a/hermes_cli/web_routers/webapp_access.py+++ b/hermes_cli/web_routers/webapp_access.py@@ -17,7 +17,12 @@ from pydantic import BaseModelfrom hermes_cli import webapp_accessfrom hermes_cli.web_deps import late-from hermes_cli.web_routers._common import _profile_scope, config_write_scope, destructive_profile+from hermes_cli.web_routers._common import (+ _profile_scope,+ config_scoped_to_thread,+ config_write_scope,+ destructive_profile,+)_restart_gateway_after = late("_restart_gateway_after", "hermes_cli.web_server_gateway")@@ -66,6 +71,17 @@ async def _scoped(profile: Optional[str], fn: Callable[[Optional[str]], Any], *,raise HTTPException(status_code=400, detail=str(exc)) from exc+async def _in_gateway_home(gateway: Optional[str], fn: Callable[[], Any]) -> Any:+ """Run ``fn()`` off the loop in the home and secret scope of the *gateway* profile that+ ``_scoped`` resolved, without the skills lock ``_profile_scope`` holds, which every config,+ skills and tools request takes: for the slow steps (the bundle build, Portal registration),+ which never touch skills. Access errors become 400s."""+ try:+ return await config_scoped_to_thread(gateway, fn)+ except webapp_access.WebappAccessError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++def _restart(gateway_profile: Optional[str], what: str) -> dict:"""The status after asking the gateway to restart onto the new config. A refused restart (a livechat) keeps the saved config and reports ``restart_error``."""@@ -81,11 +97,11 @@ async def webapp_access_status(profile: Optional[str] = None):@router.post("/api/webapp-access/start")async def start_webapp_access(body: StartBody, profile: Optional[str] = None):profile = destructive_profile(profile, "POST /api/webapp-access/start")- host, public_url, callback = await _scoped(- profile, lambda _gateway: webapp_access.plan(body.mode, body.public_url))+ gateway, (host, public_url, callback) = await _scoped(+ profile, lambda gateway: (gateway, webapp_access.plan(body.mode, body.public_url)))# The bundle before anything lasting: a failed build registers, writes and restarts nothing.- await _scoped(profile, lambda _gateway: webapp_access.build_bundle())- await _scoped(profile, lambda _gateway: webapp_access.register(callback))+ await _in_gateway_home(gateway, webapp_access.build_bundle)+ await _in_gateway_home(gateway, lambda: webapp_access.register(callback))def _start(gateway):webapp_access.serve(host, public_url)diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.pyindex 373e084bdcb..2d3076c581b 100644--- a/tests/hermes_cli/test_webapp_access.py+++ b/tests/hermes_cli/test_webapp_access.py@@ -249,6 +249,40 @@ def test_a_failed_build_turns_nothing_on_and_says_how_to_build(client, restarts,assert not _config()["webapp"]["enabled"][email protected]("step", ["build", "registration"])+def test_other_requests_answer_while_start_builds_and_registers(client, restarts, nous, monkeypatch, step):+ """Start's slow steps hold no lock that ordinary requests take: while the bundle builds (it may+ compile for minutes or wait up to 30 minutes on another build) and while the Portal registers+ the callback (a network round trip), a config request answers at once."""+ import threading++ from hermes_cli import web_server, webapp, webapp_access++ answered_during_step: list[list[int]] = []++ def ask_for_config(answers: list[int]) -> None:+ other = TestClient(web_server.app, raise_server_exceptions=False)+ other.headers.update(client.headers)+ answers.append(other.get("/api/config").status_code)++ def slow_step(*_args, **_options):+ answers: list[int] = []+ request = threading.Thread(target=ask_for_config, args=(answers,), daemon=True)+ request.start()+ request.join(timeout=10)+ answered_during_step.append(list(answers))++ if step == "build":+ monkeypatch.setattr(webapp, "prepare_webapp_renderer", slow_step)+ else:+ monkeypatch.setattr(webapp_access, "_register_nous_sign_in", slow_step)++ response = client.post("/api/webapp-access/start", json={"mode": "lan"})+ assert response.status_code == 200, response.text+ assert answered_during_step == [[200]]+ assert restarts == ["turning on Web Access"]++def test_restart_applies_the_saved_config_and_does_nothing_else(client, restarts, nous, builds):"""The pane's "Restart gateway" (a saved change the gateway has not applied yet) restarts thegateway serving the profile and nothing else: no Portal registration, no build, no config