d10bb2ab6f
`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.
71 lines
3.3 KiB
Python
71 lines
3.3 KiB
Python
"""Regression test for #49287 — the CLI memory-provider ``on_session_end``
|
|
hook stopped firing on ``/exit`` after the god-file Phase 4 refactor
|
|
(094aa85c37) moved agent construction into ``CLIAgentSetupMixin``.
|
|
|
|
``_run_cleanup`` (in ``cli.py``) gates the memory-shutdown call on the
|
|
module global ``cli._active_agent_ref``. The mixin used to set it with a
|
|
bare ``global _active_agent_ref`` — correct while the code lived in
|
|
``cli.py``, but after extraction that ``global`` binds the *mixin module's*
|
|
namespace, leaving ``cli._active_agent_ref`` ``None`` forever. The cleanup
|
|
``if _active_agent_ref:`` branch was then dead, so ``shutdown_memory_provider``
|
|
(and therefore every provider's ``on_session_end``) never ran on CLI exit.
|
|
|
|
The fix writes the reference onto the ``cli`` module explicitly. These tests
|
|
assert that contract — the existing shutdown tests pass only because they
|
|
hand-assign ``cli._active_agent_ref``, which is exactly what masked the bug.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
|
|
|
|
def test_mixin_writes_active_agent_ref_to_cli_module():
|
|
"""The mixin's agent-setup code must publish the agent reference where
|
|
``_run_cleanup`` reads it — on the ``cli`` module, not the mixin module."""
|
|
import cli as cli_mod
|
|
from hermes_cli import cli_agent_setup_mixin as mixin_mod
|
|
|
|
sentinel = object()
|
|
prev_cli = getattr(cli_mod, "_active_agent_ref", None)
|
|
prev_mixin = getattr(mixin_mod, "_active_agent_ref", "<unset>")
|
|
try:
|
|
# Reproduce the exact assignment the mixin performs after building
|
|
# the agent (see CLIAgentSetupMixin near the AIAgent(...) construction).
|
|
import cli as _cli
|
|
_cli._active_agent_ref = sentinel
|
|
|
|
# The cleanup path reads cli._active_agent_ref — it must see the value.
|
|
assert cli_mod._active_agent_ref is sentinel
|
|
finally:
|
|
cli_mod._active_agent_ref = prev_cli
|
|
if prev_mixin == "<unset>":
|
|
if hasattr(mixin_mod, "_active_agent_ref"):
|
|
delattr(mixin_mod, "_active_agent_ref")
|
|
else:
|
|
mixin_mod._active_agent_ref = prev_mixin
|
|
|
|
|
|
def test_mixin_does_not_use_bare_global_for_active_agent_ref():
|
|
"""Guard against a regression to ``global _active_agent_ref`` inside the
|
|
mixin: a bare module-local global would write the wrong namespace and
|
|
silently re-break CLI memory shutdown. The source must target ``cli``."""
|
|
from hermes_cli import cli_agent_setup_mixin as mixin_mod
|
|
|
|
src = inspect.getsource(mixin_mod)
|
|
assert "_active_agent_ref = self.agent" in src, (
|
|
"mixin no longer publishes the agent reference for atexit cleanup"
|
|
)
|
|
# The assignment must go through the cli module, not a bare module global.
|
|
# Inspect executable lines only (a bare ``global _active_agent_ref``
|
|
# statement), ignoring prose in comments/docstrings that mention it.
|
|
code_lines = [ln.split("#", 1)[0].strip() for ln in src.splitlines()]
|
|
assert "global _active_agent_ref" not in code_lines, (
|
|
"bare `global _active_agent_ref` in the mixin binds the wrong module "
|
|
"namespace — cli._active_agent_ref stays None and memory shutdown dies "
|
|
"(#49287). Write `cli._active_agent_ref = self.agent` instead."
|
|
)
|
|
assert "_cli._active_agent_ref = self.agent" in src, (
|
|
"expected the agent reference to be published onto the cli module"
|
|
)
|