mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
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).
171 lines
6.5 KiB
Python
171 lines
6.5 KiB
Python
"""Tests for issue #860 — SQLite session transcript deduplication.
|
|
|
|
Verifies that:
|
|
1. _flush_messages_to_session_db uses _last_flushed_db_idx to avoid re-writing
|
|
2. Multiple _persist_session calls don't duplicate messages
|
|
3. append_to_transcript(skip_db=True) skips SQLite but writes JSONL
|
|
4. The gateway doesn't double-write messages the agent already persisted
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: _flush_messages_to_session_db only writes new messages
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestFlushDeduplication:
|
|
"""Verify _flush_messages_to_session_db tracks what it already wrote."""
|
|
|
|
def _make_agent(self, session_db):
|
|
"""Create a minimal AIAgent with a real session DB."""
|
|
with patch.dict(os.environ, {"OPENROUTER_API_KEY": "test-key"}):
|
|
from run_agent import AIAgent
|
|
agent = AIAgent(
|
|
api_key="test-key",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
model="test/model",
|
|
quiet_mode=True,
|
|
session_db=session_db,
|
|
session_id="test-session-860",
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
)
|
|
# Simulate lazy session creation (normally done by run_conversation)
|
|
agent._ensure_db_session()
|
|
return agent
|
|
|
|
def test_flush_writes_only_new_messages(self):
|
|
"""First flush writes all new messages, second flush writes none."""
|
|
from hermes_state import SessionDB
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
db_path = Path(tmpdir) / "test.db"
|
|
db = SessionDB(db_path=db_path)
|
|
try:
|
|
agent = self._make_agent(db)
|
|
|
|
conversation_history = [
|
|
{"role": "user", "content": "old message"},
|
|
]
|
|
messages = list(conversation_history) + [
|
|
{"role": "user", "content": "new question"},
|
|
{"role": "assistant", "content": "new answer"},
|
|
]
|
|
|
|
# First flush — should write 2 new messages
|
|
agent._flush_messages_to_session_db(messages, conversation_history)
|
|
|
|
rows = db.get_messages(agent.session_id)
|
|
assert len(rows) == 2, f"Expected 2 messages, got {len(rows)}"
|
|
|
|
# Second flush with SAME messages — should write 0 new messages
|
|
agent._flush_messages_to_session_db(messages, conversation_history)
|
|
|
|
rows = db.get_messages(agent.session_id)
|
|
assert len(rows) == 2, f"Expected still 2 messages after second flush, got {len(rows)}"
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
|
|
def test_flush_reset_after_compression(self):
|
|
"""After compression creates a new session, flush index resets."""
|
|
from hermes_state import SessionDB
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
db_path = Path(tmpdir) / "test.db"
|
|
db = SessionDB(db_path=db_path)
|
|
try:
|
|
agent = self._make_agent(db)
|
|
|
|
# Write some messages
|
|
messages = [
|
|
{"role": "user", "content": "msg1"},
|
|
{"role": "assistant", "content": "reply1"},
|
|
]
|
|
agent._flush_messages_to_session_db(messages, [])
|
|
|
|
old_session = agent.session_id
|
|
assert agent._last_flushed_db_idx == 2
|
|
|
|
# Simulate what _compress_context does: new session, reset idx
|
|
agent.session_id = "compressed-session-new"
|
|
db.create_session(session_id=agent.session_id, source="test")
|
|
agent._last_flushed_db_idx = 0
|
|
|
|
# Now flush compressed messages to new session
|
|
compressed_messages = [
|
|
{"role": "user", "content": "summary of conversation"},
|
|
]
|
|
agent._flush_messages_to_session_db(compressed_messages, [])
|
|
|
|
new_rows = db.get_messages(agent.session_id)
|
|
assert len(new_rows) == 1
|
|
|
|
# Old session should still have its 2 messages
|
|
old_rows = db.get_messages(old_session)
|
|
assert len(old_rows) == 2
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: append_to_transcript skip_db parameter
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestAppendToTranscriptSkipDb:
|
|
"""Verify skip_db=True skips the SQLite write."""
|
|
|
|
def test_skip_db_prevents_sqlite_write(self, tmp_path):
|
|
"""With skip_db=True and a real DB, message does NOT appear in SQLite."""
|
|
from gateway.config import GatewayConfig
|
|
from gateway.session import SessionStore
|
|
from hermes_state import SessionDB
|
|
|
|
db_path = tmp_path / "test_skip.db"
|
|
db = SessionDB(db_path=db_path)
|
|
|
|
config = GatewayConfig()
|
|
with patch("gateway.session.SessionStore._ensure_loaded"):
|
|
store = SessionStore(sessions_dir=tmp_path, config=config)
|
|
store._db = db
|
|
store._loaded = True
|
|
|
|
session_id = "test-skip-db-real"
|
|
db.create_session(session_id=session_id, source="test")
|
|
|
|
msg = {"role": "assistant", "content": "hello world"}
|
|
store.append_to_transcript(session_id, msg, skip_db=True)
|
|
|
|
# SQLite should NOT have the message
|
|
rows = db.get_messages(session_id)
|
|
assert len(rows) == 0, f"Expected 0 DB rows with skip_db=True, got {len(rows)}"
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Test: _last_flushed_db_idx initialization
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestFlushIdxInit:
|
|
"""Verify _last_flushed_db_idx is properly initialized."""
|
|
|
|
def test_init_zero(self):
|
|
"""Agent starts with _last_flushed_db_idx = 0."""
|
|
with patch.dict(os.environ, {"OPENROUTER_API_KEY": "test-key"}):
|
|
from run_agent import AIAgent
|
|
agent = AIAgent(
|
|
api_key="test-key",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
model="test/model",
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
)
|
|
assert agent._last_flushed_db_idx == 0
|
|
|