The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(pty): the channel marker dict is empty after every PTY lifecycle end
Austin Pickett3178ef3e21f8
diff --git a/tests/hermes_cli/test_pty_keepalive_ws.py b/tests/hermes_cli/test_pty_keepalive_ws.pyindex f73b5f6c426..cd4d6fa2454 100644--- a/tests/hermes_cli/test_pty_keepalive_ws.py+++ b/tests/hermes_cli/test_pty_keepalive_ws.py@@ -147,3 +147,115 @@ async def test_attach_token_reuses_default_chat_after_active_session_fallback(ws2.send_bytes(b"again")assert pty_keepalive_harness == [["x", "fresh"]]+++# --- #63553: pty_active_session_files must not grow without bound -----------++def _pty_marker_dict():+ from hermes_cli.web_server import _get_pty_active_session_files++ return _get_pty_active_session_files(web_server.app)+++async def test_channel_marker_leaves_no_dict_entry_after_pty_lifecycle(pty_keepalive_harness):+ """The channel→marker mapping is gone once the keep-alive PTY is closed.++ Regression for #63553: `_active_session_file_for_channel` inserted a+ channel→tempfile entry that nothing ever popped, so every dashboard+ reconnect leaked one dict entry plus a forgotten tempfile+inode.+ """+ from starlette.testclient import TestClient++ markers = _pty_marker_dict()+ markers.clear()+ client = TestClient(web_server.app)++ with client.websocket_connect("/api/pty?attach=TOK63553&channel=CHAN63553") as ws:+ ws.send_bytes(b"hi")+ assert "CHAN63553" in markers # marker registered while the PTY is live++ # End the session: closing the registry (same path as TTL reap or child+ # EOF) must drop both the tempfile and the dict entry.+ marker_path = markers["CHAN63553"]+ await web_server.PTY_REGISTRY.close_all()+ assert marker_path.exists() is False+ assert "CHAN63553" not in markers+++async def test_channel_marker_reconnect_reuses_one_entry(pty_keepalive_harness):+ """Repeated same-channel reconnects keep one mapping, not one per hop."""+ from starlette.testclient import TestClient++ markers = _pty_marker_dict()+ markers.clear()+ client = TestClient(web_server.app)+ attach = "TOK63553R"+ try:+ for _ in range(3):+ with client.websocket_connect(f"/api/pty?attach={attach}&channel=CHAN63553R") as ws:+ ws.send_bytes(b"hi")+ assert list(markers) == ["CHAN63553R"]+ finally:+ await web_server.PTY_REGISTRY.close_all()+ assert markers == {}+++async def test_legacy_channel_connect_pops_marker_on_disconnect(pty_keepalive_harness):+ """A 1:1 (no attach token) PTY dies with its socket; its marker must too."""+ from starlette.testclient import TestClient++ markers = _pty_marker_dict()+ markers.clear()+ client = TestClient(web_server.app)++ with client.websocket_connect("/api/pty?channel=LEGACY63553") as ws:+ ws.send_bytes(b"hi")+ assert "LEGACY63553" in markers++ # The handler pops the marker after _legacy_pump returns, which can lag the+ # client-side context exit by a tick — poll instead of asserting immediately.+ import time++ deadline = time.monotonic() + 5.0+ while "LEGACY63553" in markers and time.monotonic() < deadline:+ time.sleep(0.01)+ assert "LEGACY63553" not in markers+++async def test_fresh_start_pops_stale_marker_and_reregisters_child_path(+ pty_keepalive_harness, tmp_path, monkeypatch+):+ """`?fresh=1` drops the stale breadcrumb AND keeps the new child's marker.++ Covers the resume regression flagged in #63563 review: popping the stale+ mapping must not leave the fresh child's path unmapped, or the next+ same-channel reconnect allocates a new path and loses the breadcrumb.+ """+ from starlette.testclient import TestClient++ markers = _pty_marker_dict()+ markers.clear()+ client = TestClient(web_server.app)++ with client.websocket_connect("/api/pty?attach=TOKFRESH&channel=CHANFRESH") as ws:+ ws.send_bytes(b"hi")+ stale_path = markers["CHANFRESH"]+ stale_path.write_text(json.dumps({"session_id": "stale"}))++ # Fresh start on the same channel: new PTY, new breadcrumb.+ with client.websocket_connect("/api/pty?attach=TOKFRESH2&channel=CHANFRESH&fresh=1") as ws:+ ws.send_bytes(b"hi")++ assert stale_path.exists() is False # stale file removed+ assert markers["CHANFRESH"] is stale_path # same mapping kept for the fresh child+ # The kept mapping is what lets the next same-channel reconnect resume the+ # fresh child instead of allocating a new unmapped path (#63563 review).+ with client.websocket_connect("/api/pty?attach=TOKFRESH2&channel=CHANFRESH") as ws:+ ws.send_bytes(b"hi")+ assert markers["CHANFRESH"] is stale_path+ await web_server.PTY_REGISTRY.close_all()+ assert markers == {}