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.
126 lines
4.7 KiB
Python
126 lines
4.7 KiB
Python
"""Regression tests for #94736 — append_message after close() raced a live writer.
|
|
|
|
Subagent/cron sessions were dying mid-run with ``Session DB append_message
|
|
failed: 'NoneType' object has no attribute 'execute'``: a teardown owner
|
|
(cron ``run_job``'s ``finally`` block, a delegate timeout owner abandoning
|
|
its worker, ``AIAgent.close()``) called ``SessionDB.close()`` — which nulls
|
|
``_conn`` — while a still-unwinding worker thread had one more transcript
|
|
flush to land. ``_execute_write`` then hit ``None.execute`` and the
|
|
conversation loop force-ended the turn as ``session_persistence_failed``,
|
|
silently dropping the tail of the session.
|
|
|
|
The fix self-heals at the shared persistence boundary: when ``_conn`` is
|
|
``None`` (only possible after an explicit ``close()``), the writer reopens
|
|
a connection to the same database file with a loud WARNING, and the write
|
|
lands. These tests exercise the REAL SessionDB against a real sqlite file —
|
|
no mocks of the code under test.
|
|
"""
|
|
|
|
import logging
|
|
import threading
|
|
|
|
import pytest
|
|
|
|
from hermes_state import SessionDB
|
|
|
|
|
|
@pytest.fixture
|
|
def db(tmp_path):
|
|
d = SessionDB(db_path=tmp_path / "state.db")
|
|
yield d
|
|
d.close()
|
|
|
|
|
|
class TestAppendAfterClose:
|
|
def test_append_message_after_close_reopens_and_lands(self, db, caplog):
|
|
"""The exact #94736 shape: close() then one more transcript flush."""
|
|
db.create_session("s1", "cli")
|
|
db.append_message("s1", "user", content="hello")
|
|
|
|
db.close()
|
|
assert db._conn is None
|
|
|
|
with caplog.at_level(logging.WARNING, logger="hermes_state"):
|
|
msg_id = db.append_message(
|
|
"s1", "assistant", content="flushed after teardown"
|
|
)
|
|
|
|
assert isinstance(msg_id, int) and msg_id > 0
|
|
rows = db.get_messages("s1")
|
|
assert [r["role"] for r in rows] == ["user", "assistant"]
|
|
assert rows[-1]["content"] == "flushed after teardown"
|
|
# The recovery is loud, not silent.
|
|
assert any("reopening" in r.message for r in caplog.records)
|
|
|
|
def test_reopened_connection_survives_subsequent_writes_and_close(self, db):
|
|
"""The reopened handle is a full writer: more appends work, and a
|
|
second close() releases it cleanly (idempotent contract)."""
|
|
db.create_session("s1", "cli")
|
|
db.close()
|
|
|
|
db.append_message("s1", "user", content="a")
|
|
db.append_message("s1", "assistant", content="b")
|
|
assert len(db.get_messages("s1")) == 2
|
|
|
|
db.close()
|
|
assert db._conn is None
|
|
db.close() # idempotent
|
|
|
|
def test_read_after_close_recovers_too(self, db):
|
|
"""The locked-read fallback path shares the same guard: a read that
|
|
lands after close() must not die on None.execute either."""
|
|
db.create_session("s1", "cli")
|
|
db.append_message("s1", "user", content="hello")
|
|
db.close()
|
|
|
|
rows = db.get_messages("s1")
|
|
assert len(rows) == 1
|
|
|
|
def test_concurrent_close_during_flush_loses_no_writes(self, tmp_path):
|
|
"""Race a teardown close() against a worker mid-flush (the cron
|
|
inactivity-timeout shape): every append must land or raise loudly —
|
|
never vanish into a swallowed NoneType error."""
|
|
db = SessionDB(db_path=tmp_path / "state.db")
|
|
db.create_session("s1", "cli")
|
|
|
|
n_writes = 40
|
|
start = threading.Event()
|
|
errors: list = []
|
|
|
|
def _worker():
|
|
start.wait()
|
|
for i in range(n_writes):
|
|
try:
|
|
db.append_message("s1", "tool", content=f"result {i}")
|
|
except Exception as exc: # pragma: no cover - failure path
|
|
errors.append(exc)
|
|
|
|
t = threading.Thread(target=_worker)
|
|
t.start()
|
|
start.set()
|
|
# Teardown owner closes mid-flight, twice for good measure.
|
|
db.close()
|
|
db.close()
|
|
t.join(timeout=30)
|
|
assert not t.is_alive()
|
|
|
|
assert errors == [], f"appends died during teardown race: {errors!r}"
|
|
rows = db.get_messages("s1")
|
|
assert len(rows) == n_writes
|
|
db.close()
|
|
|
|
def test_read_only_handle_still_refuses_after_close(self, tmp_path):
|
|
"""A read-only cross-profile handle must NOT silently reopen — it
|
|
raises an explicit error naming the closed handle."""
|
|
# Initialise a real DB first with a writable handle.
|
|
writer = SessionDB(db_path=tmp_path / "state.db")
|
|
writer.create_session("s1", "cli")
|
|
writer.append_message("s1", "user", content="hello")
|
|
writer.close()
|
|
|
|
ro = SessionDB(db_path=tmp_path / "state.db", read_only=True)
|
|
ro.close()
|
|
with pytest.raises(Exception) as excinfo:
|
|
ro.get_messages("s1")
|
|
assert "closed" in str(excinfo.value).lower()
|