Skip to content

The agent that grows with you

fix(webapp): report a host shell exit reaped during redial validation

unsupportedpastels4fe9e6273d83
diff --git a/hermes_cli/web_host_terminal_sessions.py b/hermes_cli/web_host_terminal_sessions.py
index 81323306328..714b7af68bb 100644
--- a/hermes_cli/web_host_terminal_sessions.py
+++ b/hermes_cli/web_host_terminal_sessions.py
@@ -152,10 +152,7 @@ class HostTerminalRegistry(PtySessionRegistry):
session = self._sessions.get(token)
owner = self.owners.get(token)
if session is None or owner is None:
- gone = self.exits.get(token)
- if gone is not None and gone[0] == identity:
- raise TerminalExited()
- raise TerminalExpired()
+ raise self._gone(token, identity)
if owner.identity != identity:
raise TerminalDenied()
@@ -168,13 +165,20 @@ class HostTerminalRegistry(PtySessionRegistry):
# No await between the final check and the caller's attach claim. The
# home was just checked off-loop; every input write checks it again.
if self._sessions.get(token) is not session:
- raise TerminalExpired()
+ # The reaper took the shell during validation; it may have exited first.
+ raise self._gone(token, identity)
if not closing and _exited(session):
raise TerminalExited()
if not closing and self.expired(session):
raise TerminalExpired()
return session, owner
+ def _gone(self, token: str, identity: tuple[str, str]) -> TerminalExpired:
+ """The refusal of a token with no registered shell: ``TerminalExited`` if *identity*'s shell
+ exited by itself, else ``TerminalExpired``."""
+ gone = self.exits.get(token)
+ return TerminalExited() if gone is not None and gone[0] == identity else TerminalExpired()
+
async def remove(self, token: str):
session = self._sessions.pop(token, None)
self.owners.pop(token, None)
diff --git a/tests/hermes_cli/test_pty_session.py b/tests/hermes_cli/test_pty_session.py
index 58812b3348b..5706090822f 100644
--- a/tests/hermes_cli/test_pty_session.py
+++ b/tests/hermes_cli/test_pty_session.py
@@ -374,6 +374,52 @@ async def test_a_reattach_that_replays_part_of_the_history_says_output_was_trimm
await registry.close_all()
+async def test_a_shell_reaped_while_its_redial_is_validated_closes_the_redial_as_an_exit(tmp_path, monkeypatch):
+ """A redial whose shell exited, and which the reaper takes while ``resolve`` checks the
+ profile off-loop, is still told the shell exited, so the tab closes instead of offering a new
+ shell."""
+ import secrets
+ import threading
+ from types import SimpleNamespace
+
+ from hermes_cli import pty_session, web_host_terminal_sessions
+ from hermes_cli.web_host_terminal_sessions import HostOwner, HostTerminalRegistry, TerminalExited
+
+ home = tmp_path / "home"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ identity = ("local", "owner")
+ registry = HostTerminalRegistry()
+ token = secrets.token_urlsafe(32)
+ bridge = FakeBridge([])
+ bridge.dead = True # the shell exited while no tab was attached
+ registry._sessions[token] = pty_session.PtySession(token, bridge, buffer_cap=1024, read_timeout=0.01)
+ registry.owners[token] = HostOwner(identity, home.resolve(), "sh", str(tmp_path))
+
+ validating, release = threading.Event(), threading.Event()
+ request_home = web_host_terminal_sessions._request_home
+
+ def held_request_home(profile):
+ validating.set()
+ release.wait(5)
+ return request_home(profile)
+
+ monkeypatch.setattr(web_host_terminal_sessions, "_request_home", held_request_home)
+ redial = SimpleNamespace(query_params={"attach": token})
+ try:
+ resolving = asyncio.ensure_future(registry.resolve(token, redial, identity))
+ assert await asyncio.to_thread(validating.wait, 5)
+ await registry.reap_idle()
+ assert token not in registry._sessions and token in registry.exits
+ release.set()
+ with pytest.raises(TerminalExited):
+ await resolving
+ finally:
+ release.set()
+ await registry.close_all()
+
+
@pytest.mark.asyncio
async def test_reattach_can_force_complete_tui_redraw_after_replay():
"""A fresh terminal cannot reconstruct a differential ANSI tail alone."""