Skip to content

The agent that grows with you

fix(webapp): keep a managed webapp.port when Web Access picks a port

unsupportedpastels43349a8d0734
diff --git a/hermes_cli/webapp_access.py b/hermes_cli/webapp_access.py
index 910a0d380aa..4d868f1fb23 100644
--- a/hermes_cli/webapp_access.py
+++ b/hermes_cli/webapp_access.py
@@ -202,16 +202,21 @@ def _port_to_serve(home: Path, host: str) -> tuple[int, Optional[int]]:
def _own_port(home: Path) -> Optional[object]:
- """The ``webapp.port`` *home*'s ``config.yaml`` sets, valid or not (the gateway reports an invalid
- one by name), or None when it sets none. The file as written: the defaults name a port for everyone."""
+ """The ``webapp.port`` *home*'s gateway is configured with, valid or not (the gateway reports an
+ invalid one by name), or None when neither *home*'s ``config.yaml`` nor the managed scope sets one.
+ The file as written with the managed overlay on top, as the gateway loads it (a managed port wins, and
+ ``save_config`` never writes over it), but without the defaults: they name a port for everyone."""
from ruamel.yaml.error import YAMLError
+ from hermes_cli import managed_scope
from hermes_cli.config import read_user_config_raw
try:
document = read_user_config_raw(Path(home) / "config.yaml")
except (OSError, ValueError, YAMLError): # the gateway reports an unreadable config itself
return None
+ if isinstance(document, dict):
+ document = managed_scope.apply_managed_overlay(document)
block = document.get("webapp") if isinstance(document, dict) else None
return block.get("port") if isinstance(block, dict) else None
diff --git a/tests/hermes_cli/test_webapp_access_ports.py b/tests/hermes_cli/test_webapp_access_ports.py
index f01f7cdbac6..118ed5f5c12 100644
--- a/tests/hermes_cli/test_webapp_access_ports.py
+++ b/tests/hermes_cli/test_webapp_access_ports.py
@@ -109,6 +109,24 @@ def test_a_port_the_user_set_is_never_replaced(machine, configured):
assert _webapp_block(machine["solo"])["port"] == configured
+def test_a_port_the_managed_scope_sets_is_the_one_the_callback_names(machine, tmp_path):
+ """An administrator's ``webapp.port`` wins over the profile's config when the gateway loads it, and
+ saving never writes over it: Start registers the callback on that port and writes none."""
+ from gateway.webapp_listener import listener_plan
+ from hermes_cli import managed_scope
+
+ managed = _write(tmp_path / "managed", "webapp:\n port: 9555\n")
+ machine["monkeypatch"].setenv("HERMES_MANAGED_DIR", str(managed))
+ managed_scope.invalidate_managed_cache()
+ host, port, public_url, callback = webapp_access.plan("lan")
+ assert (port, callback) == (None, "http://192.168.1.50:9555/auth/callback")
+ assert machine["probed"] == []
+ webapp_access.serve(host, public_url, port)
+ assert "port" not in _webapp_block(machine["solo"])
+ bind_host, bind_port, mount, _reason = listener_plan(machine["solo"])
+ assert (bind_host, bind_port) == ("0.0.0.0", 9555) and mount is not None
+
+
def test_with_no_free_port_start_refuses_and_names_the_setting(machine):
machine["listening"].update(range(9121, 9221))
with pytest.raises(webapp_access.WebappAccessError) as refused: