hermes-agent/tests/plugins/test_hindsight_root_guard.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

76 lines
2.9 KiB
Python

"""Root-user guard for Hindsight local_embedded mode (issue #13125).
PostgreSQL's initdb refuses to run as root, so the embedded Hindsight daemon
can never initialize under root — without a guard it crash-restart loops
forever, burning RAM/CPU with no user-visible error. initialize() must detect
root up front, skip daemon startup, disable the provider, and warn the user.
"""
import importlib
import threading
import pytest
hindsight = importlib.import_module("plugins.memory.hindsight")
HindsightMemoryProvider = hindsight.HindsightMemoryProvider
def _make_local_embedded_provider(monkeypatch):
"""Build a provider wired for local_embedded with a passing runtime probe."""
monkeypatch.setattr(
hindsight,
"_load_config",
lambda: {"mode": "local_embedded", "profile": "hermes"},
)
# Pretend the local runtime imports cleanly so initialize() reaches the
# daemon-start branch instead of bailing on a missing `hindsight` package.
monkeypatch.setattr(hindsight, "_check_local_runtime", lambda: (True, None))
return HindsightMemoryProvider()
def _daemon_threads_alive() -> list[str]:
return [t.name for t in threading.enumerate() if t.name == "hindsight-daemon-start"]
def test_local_embedded_skips_daemon_as_root(monkeypatch, caplog):
"""As root, the daemon thread must NOT start and the mode is disabled."""
provider = _make_local_embedded_provider(monkeypatch)
monkeypatch.setattr(hindsight.os, "geteuid", lambda: 0, raising=False)
# If the guard fails, _start_daemon would call _get_client() — make that
# explode so a regression is loud rather than silently spawning a thread.
monkeypatch.setattr(
provider,
"_get_client",
lambda: pytest.fail("daemon startup attempted while running as root"),
)
before = set(_daemon_threads_alive())
with caplog.at_level("WARNING", logger="plugins.memory.hindsight"):
provider.initialize(session_id="s1")
assert provider._mode == "disabled"
assert set(_daemon_threads_alive()) == before # no new daemon thread
# The warning is surfaced to the user via the logger AND printed to
# stderr (E2E-verified in tests/plugins/test_hindsight_root_guard.py
# docstring rationale); capsys can't reliably capture the module-level
# sys.stderr write under the isolation harness, so assert on the log.
assert any("cannot run as root" in r.message for r in caplog.records)
def _fake_thread_factory(started: threading.Event):
"""Return a Thread replacement that records start() without running work."""
real_thread = threading.Thread
def _factory(*args, **kwargs):
if kwargs.get("name") == "hindsight-daemon-start":
started.set()
class _NoopThread:
def start(self):
pass
return _NoopThread()
return real_thread(*args, **kwargs)
return _factory