Skip to content

The agent that grows with you

fix(webapp): a gateway older than the Webapp is named with the restart that updates it

emozilla8032fa7eca56
diff --git a/hermes_cli/webapp_access.py b/hermes_cli/webapp_access.py
index b475f832d06..733a6c69a0d 100644
--- a/hermes_cli/webapp_access.py
+++ b/hermes_cli/webapp_access.py
@@ -222,14 +222,17 @@ def _write_config(*, enabled: bool, host: str = "", public_url: str = "") -> Non
def _gateway_state(home: Path) -> dict:
- """What *home*'s gateway serves now (``serving``, ``off``, ``unavailable`` with a ``reason``, or
- ``stopped`` when none answers). It reads the config at start, so it can lag until a restart."""
- from hermes_cli.webapp_launcher import GatewayUnreachable, _control
+ """What *home*'s gateway serves now (``serving``, ``starting``, ``off``, ``unavailable`` with a
+ ``reason``, or ``stopped`` when none answers). It reads the config at start, so it can lag until a
+ restart; a gateway older than the Webapp serves none (``off``, with the restart as the reason)."""
+ from hermes_cli.webapp_launcher import OLDER_GATEWAY, GatewayUnreachable, _control, older_gateway
try:
response = _control(home, "webapp-status")
except GatewayUnreachable:
return {"state": "stopped", "url": None, "reason": None}
+ if older_gateway(response):
+ return {"state": "off", "url": None, "reason": OLDER_GATEWAY}
if not response.get("ok"):
return {"state": "unavailable", "url": None, "reason": str(response.get("error") or "")}
return dict(response["result"])
diff --git a/hermes_cli/webapp_launcher.py b/hermes_cli/webapp_launcher.py
index 076b4ded557..c8940d13c7c 100644
--- a/hermes_cli/webapp_launcher.py
+++ b/hermes_cli/webapp_launcher.py
@@ -20,6 +20,10 @@ RESTART_COMMAND = "hermes gateway restart"
# The gateway reads the Webapp's settings and sign-in providers only when it starts.
RETRY_AFTER_FIX = f"The gateway reads these settings when it starts: after fixing them, run {RESTART_COMMAND}."
STARTED_BEFORE_ON = f"The gateway started before the Webapp was turned on. Restart it to serve the Webapp: {RESTART_COMMAND}"
+# A gateway answers a verb it does not know this way; the Webapp's are registered before its control
+# socket answers, so the gateway running predates them (an upgrade it has not restarted onto).
+OLDER_GATEWAY = ("The running gateway is older than the Webapp and does not know its commands. Restart it "
+ f"onto this version of Hermes: {RESTART_COMMAND}")
# How long --sign-out waits for a gateway that is still starting to answer (30 s).
_START_WAIT_ATTEMPTS, _START_WAIT_INTERVAL = 60, 0.5
@@ -94,7 +98,14 @@ def _profile_choice(home: Path, target: Path) -> dict:
return {"profile": name} if name and name != "default" else {}
+def older_gateway(response: dict) -> bool:
+ """Whether *response* is a gateway's answer to a verb it does not know: it predates the Webapp."""
+ return not response.get("ok") and str(response.get("error") or "").startswith("unknown verb:")
+
+
def _refusal(response: dict) -> str:
+ if older_gateway(response):
+ return OLDER_GATEWAY
error = str(response.get("error") or "The gateway refused the request.")
kind, _, message = error.partition(": ")
return message if kind.endswith("Error") and message else error
diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.py
index e422d1016ce..98b24b1f54f 100644
--- a/tests/hermes_cli/test_webapp_access.py
+++ b/tests/hermes_cli/test_webapp_access.py
@@ -801,3 +801,19 @@ def test_web_access_changes_run_one_at_a_time(client, restarts, nous, monkeypatc
else:
assert nous == ["http://192.168.1.50:9120/auth/callback", f"{public_url}/auth/callback"]
assert (block["enabled"], block.get("public_url")) == (True, public_url)
+
+
+def test_a_gateway_older_than_the_webapp_reads_as_serving_none_until_it_restarts(monkeypatch, tmp_path):
+ """A gateway that predates the Webapp answers its status with "unknown verb". It serves no Webapp,
+ so Web Access reports it ``off`` with a reason that names the restart (the pane then offers
+ Restart gateway), never ``unavailable``, which the pane's restart wait takes as the final answer."""
+ from pathlib import Path
+
+ from hermes_cli import webapp_access, webapp_launcher
+
+ answer = {"ok": False, "error": "unknown verb: 'webapp-status'", "protocol": 1,
+ "supported_verbs": ["identify", "session-ticket", "status"]}
+ monkeypatch.setattr(webapp_launcher, "_control", lambda _home, _verb, params=None: answer)
+ state = webapp_access._gateway_state(Path(tmp_path))
+ assert state["state"] == "off", state
+ assert webapp_launcher.RESTART_COMMAND in state["reason"]
diff --git a/tests/hermes_cli/test_webapp_launcher.py b/tests/hermes_cli/test_webapp_launcher.py
index e78956f64a0..4da318f70a3 100644
--- a/tests/hermes_cli/test_webapp_launcher.py
+++ b/tests/hermes_cli/test_webapp_launcher.py
@@ -182,6 +182,23 @@ def test_status_of_a_gateway_still_starting_the_webapp_says_so(world, capsys):
assert "still starting" in out and "Unavailable" not in out and launcher.RESTART_COMMAND not in out
+OLDER = {"webapp-status": {"ok": False, "error": "unknown verb: 'webapp-status'", "supported_verbs": ["identify"]},
+ "webapp-launch": {"ok": False, "error": "unknown verb: 'webapp-launch'", "supported_verbs": ["identify"]},
+ "webapp-sign-out": {"ok": False, "error": "unknown verb: 'webapp-sign-out'", "supported_verbs": ["identify"]}}
+
+
[email protected]("action", [{"status": True}, {}, {"sign_out": True}])
+def test_a_gateway_older_than_the_webapp_is_named_with_the_restart_that_updates_it(world, capsys, action):
+ """A gateway started before an upgrade (a git pull, a service manager that kept the old process)
+ does not know the Webapp's commands and answers "unknown verb". The command says the running
+ gateway is older and names the restart that loads this version, not the raw protocol error."""
+ world["answers"].update(OLDER)
+ world["states"][:] = ["ready"]
+ assert launcher.cmd_webapp(_args(**action)) == 1
+ out = capsys.readouterr().out
+ assert "unknown verb" not in out and "older" in out and launcher.RESTART_COMMAND in out
+
+
def test_status_with_no_gateway_running_says_so(world, capsys):
world["answers"]["webapp-status"] = launcher.GatewayUnreachable("no gateway")
assert launcher.cmd_webapp(_args(status=True)) == 1