The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep native clients a listener when the Webapp credential cannot be kept
Tekniumc1be276af972
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex 20e36a834d2..3d6bb43904a 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -131,9 +131,11 @@ class GatewayDashboardMount:raise RuntimeError("gateway API stopped")if getattr(web.app.state, "gateway_runner", None) not in (None, self.runner):raise RuntimeError("gateway API already started")- self._bind_state(web)- lifespan = web.app.router.lifespan_context(web.app)try:+ # Binding may fail too (the Webapp's browser credential cannot be read or written);+ # its app state and swapped session token roll back with a failed lifespan's.+ self._bind_state(web)+ lifespan = web.app.router.lifespan_context(web.app)await lifespan.__aenter__()if self._closed: # close() won while the lifespan started; it never saw this mountawait lifespan.__aexit__(None, None, None)diff --git a/gateway/run_runtime.py b/gateway/run_runtime.pyindex 45cbb49aa09..f705bf013e3 100644--- a/gateway/run_runtime.py+++ b/gateway/run_runtime.py@@ -515,6 +515,8 @@ async def start_gateway_runtime_api(runner):# this gateway, so it keeps main's private listener and reports why the Webapp is not served.if isinstance(exc, SystemExit):reason = str(exc.code).strip() # the gate's own message names the fix+ elif isinstance(exc, OSError) and exc.filename: # the browser credential's file, not the port+ reason = f"could not use {exc.filename} ({exc.strerror or exc})"else:cause = (exc.strerror or exc) if isinstance(exc, OSError) else excreason = f"could not listen on {host}:{port} ({cause})"diff --git a/tests/gateway/fixtures/webapp_fallback_peer.py b/tests/gateway/fixtures/webapp_fallback_peer.pyindex aeb9db09782..d5d45f4b2f1 100644--- a/tests/gateway/fixtures/webapp_fallback_peer.py+++ b/tests/gateway/fixtures/webapp_fallback_peer.py@@ -8,6 +8,8 @@ Run as ``python webapp_fallback_peer.py '<spec json>'`` with an isolated ``HERME- ``fail_after_mount``: the listener's startup fails once, after the dashboard mount is up, as whenanother process starts listening on the port first;- ``hold_port``: another socket already listens on the Webapp's port;+- ``credential_unusable``: a file stands where the browser credential's folder belongs, so the+ credential can be neither read nor written;- ``rival_listens_during_mount``: another socket bound the port with SO_REUSEADDR before the gatewayand starts listening while the gateway mounts the dashboard (Linux only);- ``connect_during_mount``: a client connects to the Webapp's port while the dashboard mounts;@@ -186,9 +188,12 @@ async def probe(spec):home = Path(os.environ["HERMES_HOME"])(home / "config.yaml").write_text(_config(spec), encoding="utf-8")# Seeded user-only, as the gateway writes it: a stored token other users can read is replaced.- (home / "webapp").mkdir(mode=0o700, exist_ok=True)- (home / "webapp" / "session-token").write_text(SEEDED_TOKEN + "\n", encoding="utf-8")- (home / "webapp" / "session-token").chmod(0o600)+ if spec.get("credential_unusable"):+ (home / "webapp").write_text("not a folder\n", encoding="utf-8")+ else:+ (home / "webapp").mkdir(mode=0o700, exist_ok=True)+ (home / "webapp" / "session-token").write_text(SEEDED_TOKEN + "\n", encoding="utf-8")+ (home / "webapp" / "session-token").chmod(0o600)dist = home.parent / "dist-webapp"dist.mkdir(exist_ok=True)if spec.get("built", True):@@ -279,7 +284,8 @@ async def probe(spec):built = await client.get("/")report["index_once_built"] = {"status": built.status_code,"is_webapp": "WEBAPP_BUNDLE rebuilt" in built.text}- report["token_rotated"] = (home / "webapp" / "session-token").read_text().strip() != SEEDED_TOKEN+ if not spec.get("credential_unusable"):+ report["token_rotated"] = (home / "webapp" / "session-token").read_text().strip() != SEEDED_TOKENfinally:await stop_gateway_api(api)if rival is not None:diff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.pyindex e1dfe4507e3..39a6754a3ce 100644--- a/tests/gateway/test_webapp_listener_fallback.py+++ b/tests/gateway/test_webapp_listener_fallback.py@@ -108,3 +108,18 @@ def test_a_webapp_that_needs_sign_in_it_cannot_have_falls_back_to_the_private_liassert report["native_session_list"] is Trueassert report["status"]["state"] == "unavailable"assert report["state"]["auth_required"] is False+++def test_a_webapp_whose_browser_credential_cannot_be_kept_leaves_native_clients_a_listener(tmp_path):+ # A file where the credential's folder belongs fails the Webapp's mount while it binds: every+ # change that bind made is undone, so main's private listener mounts and native clients connect.+ port = _free_port()+ report = _run(tmp_path, {"webapp": {"enabled": True, "port": port}, "credential_unusable": True})+ assert report["start_error"] is None, report.get("start_traceback")+ assert report["api_origin"] != f"http://127.0.0.1:{port}"+ assert report["native_session_list"] is True+ assert report["status"]["state"] == "unavailable"+ assert "webapp" in report["status"]["reason"] and "could not listen" not in report["status"]["reason"]+ assert report["state"]["surface"] == "<absent>"+ assert report["state"]["web_dist_set"] is False+