Files
hermes-agent/tests/agent/test_codex_app_server_lifecycle.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

83 lines
2.7 KiB
Python

"""Codex app-server session lifecycle on hard agent teardown (#65260).
The Codex runtime drops ``agent._codex_session`` on turn crash and on
retirement (agent/codex_runtime.py), but ``AIAgent.close()`` — the hard
teardown for /new, /reset, and session expiry — had no owner for it, so
the app-server child process survived until interpreter exit.
"""
import threading
from run_agent import AIAgent
class _FakeCodexSession:
def __init__(self, raises: bool = False):
self.close_calls = 0
self._raises = raises
def close(self):
self.close_calls += 1
if self._raises:
raise RuntimeError("app-server already dead")
def _bare_agent(session_id: str) -> AIAgent:
"""Minimal agent shell exercising close() without a real build."""
agent = AIAgent.__new__(AIAgent)
agent.session_id = session_id
agent.client = None
agent._active_children_lock = threading.Lock()
agent._active_children = set()
agent._end_session_on_close = False
agent._session_messages = ["retained"]
return agent
def test_agent_close_releases_codex_app_server_session(monkeypatch):
agent = _bare_agent("test-codex-lifecycle")
codex_session = _FakeCodexSession()
agent._codex_session = codex_session
monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)
monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)
agent.close()
agent.close()
# Idempotent: the second close must not re-close a released session.
assert codex_session.close_calls == 1
assert agent._codex_session is None
assert agent._session_messages == []
def test_close_clears_reference_even_when_session_close_raises(monkeypatch):
"""A wedged app-server must not strand a stale session reference.
The attribute is cleared BEFORE close() precisely so a raising close
can't leave a dead session attached to the agent.
"""
agent = _bare_agent("test-codex-lifecycle-raises")
codex_session = _FakeCodexSession(raises=True)
agent._codex_session = codex_session
monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)
monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)
agent.close()
assert codex_session.close_calls == 1
assert agent._codex_session is None
def test_close_without_codex_session_is_a_noop(monkeypatch):
"""Non-Codex sessions (the common case) must be unaffected."""
agent = _bare_agent("test-no-codex")
monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)
monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)
agent.close()
assert getattr(agent, "_codex_session", None) is None