Skip to content

The agent that grows with you

fix(gateway): a secondary-profile Webapp tab keeps a scale-to-zero instance awake

kshitijk4poorc2f14e48b89a
diff --git a/gateway/scale_to_zero.py b/gateway/scale_to_zero.py
index da8b4f54587..7d51b647889 100644
--- a/gateway/scale_to_zero.py
+++ b/gateway/scale_to_zero.py
@@ -36,7 +36,7 @@ _malformed_sleep_url_logged = False
# Short is safe: real work always blocks the suspend, resume is sub-second; longer bills idle RAM.
DEFAULT_IDLE_TIMEOUT_MINUTES = 2
_TRUTHY = {"1", "true", "yes", "on"}
-# Dashboard-client liveness marker, touched by the (separate) dashboard process on every /api/ws
+# Dashboard-client liveness marker, touched by every /api/ws client socket on each
# connect and inbound frame. Folded into the inbound clock so an open client holds the box awake —
# otherwise it suspends under the client, whose reconnect re-pokes the wake URL and the instance
# flaps every ~60s. Deliberately NO staleness cutoff: is_idle decides whether the mtime is recent.
@@ -90,10 +90,13 @@ def is_idle(*, active_work_count: int, seconds_since_last_inbound: float,
def dashboard_client_heartbeat_path(hermes_home: Optional[os.PathLike | str] = None):
- """Path of the dashboard-client liveness marker under HERMES_HOME."""
+ """Path of the dashboard-client liveness marker under the process's own HERMES_HOME.
+
+ One marker per process, never per profile scope: the gateway's idle watcher reads it with no
+ override, so a socket served in a secondary profile's scope must stamp the same file."""
if hermes_home is None:
- from hermes_constants import get_hermes_home
- hermes_home = get_hermes_home()
+ from hermes_constants import get_process_hermes_home
+ hermes_home = get_process_hermes_home()
return Path(hermes_home) / DASHBOARD_CLIENT_HEARTBEAT_REL
diff --git a/tests/gateway/test_scale_to_zero_dashboard_client.py b/tests/gateway/test_scale_to_zero_dashboard_client.py
index 8d9bafa646b..6f09bac3c40 100644
--- a/tests/gateway/test_scale_to_zero_dashboard_client.py
+++ b/tests/gateway/test_scale_to_zero_dashboard_client.py
@@ -379,9 +379,9 @@ def test_stalled_marker_write_coalesces_later_touches(hermes_home, monkeypatch):
assert calls["n"] == 2
-def test_marker_write_lands_in_the_callers_profile_home(hermes_home, monkeypatch):
- """The write runs on a worker thread, which does not inherit the caller's context-local
- HERMES_HOME override; the marker must still land in the caller's home, not the launch home."""
+def test_marker_write_lands_in_the_launch_home_under_a_profile_override(hermes_home, monkeypatch):
+ """The gateway's idle watcher reads the launch home's marker, so a socket running in another
+ profile's HERMES_HOME override must still stamp the launch home."""
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from tui_gateway import ws as ws_mod
@@ -393,5 +393,30 @@ def test_marker_write_lands_in_the_callers_profile_home(hermes_home, monkeypatch
finally:
reset_hermes_home_override(token)
_drain_marker_writes(ws_mod)
- assert s2z.dashboard_client_last_seen(s2z.dashboard_client_heartbeat_path(served)) is not None
- assert s2z.dashboard_client_last_seen() is None
+ assert s2z.dashboard_client_last_seen() is not None
+ assert not s2z.dashboard_client_heartbeat_path(served).exists()
+
+
+def test_secondary_profile_socket_keeps_a_multiplexed_gateway_awake(hermes_home, monkeypatch):
+ """A Webapp tab bound to a secondary profile runs handle_ws inside ``owner_scope`` of that
+ profile (``chat_ws.gateway_ws``). Its pings must keep the multiplexed gateway awake."""
+ from types import SimpleNamespace
+
+ from gateway.session_authorities import owner_scope
+ from tui_gateway import ws as ws_mod
+
+ _stub_ws_server(monkeypatch, ws_mod)
+ secondary = hermes_home / "profiles" / "b"
+ secondary.mkdir(parents=True)
+ runner = _runner(monkeypatch, last_inbound_at=time.time() - 600)
+ runner.config = SimpleNamespace(multiplex_profiles=True)
+ served = SimpleNamespace(runner=runner, profile_id=str(secondary))
+ frames = ['{"jsonrpc":"2.0","method":"gateway.ping","id":1}']
+
+ async def _tab():
+ with owner_scope(served):
+ await ws_mod.handle_ws(_FakeWS(frames))
+
+ asyncio.run(_tab())
+ _drain_marker_writes(ws_mod)
+ assert runner._scale_to_zero_is_idle() is False
diff --git a/tui_gateway/ws.py b/tui_gateway/ws.py
index e65a274f706..0083b9013f8 100644
--- a/tui_gateway/ws.py
+++ b/tui_gateway/ws.py
@@ -57,7 +57,7 @@ def _note_dashboard_client_activity(*, force: bool = False) -> None:
path = None
try:
from gateway.scale_to_zero import dashboard_client_heartbeat_path
- # Resolved here: the worker thread does not carry the caller's profile scope.
+ # The process's own home, whatever profile scope the socket runs in (the watcher reads it).
path = dashboard_client_heartbeat_path()
with _dashboard_client_touch_lock:
if path in _dashboard_client_touch_pending: