mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description `headroom memory reindex` clears `memory_fts` and rebuilds it with one `index_memory()` call per record. `FTS5TextIndex.index_raw()` commits per call, so rebuilding a 1,000-memory store costs 1,000 transactions and 1,000 fsyncs. The adapter already ships `index_batch_memories()`; this uses it. Closes # N/A ## Type of Change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [x] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `reindex_memories()` now calls `index_batch_memories()` once per page, reusing the existing `_REINDEX_PAGE_SIZE` constant rather than introducing a new knob. No adapter changes. - On a failed page, the previous per-record loop runs **for that page only**, so valid records still land and the failing record is still named in the warning. - A page that fails and then fully recovers does not change the exit status. `ok` stays tied to whether records actually got indexed, exactly as before. - Added `benchmarks/memory_fts_reindex_benchmark.py` for reproduction. **Why per page rather than one batch over the whole store.** A single transaction would be marginally faster, but the reindex deliberately clears the index before rebuilding (`DELETE FROM memory_fts`, `headroom/cli/memory.py:1208-1210`). One failed statement would then roll the whole rebuild back to an *empty* index — silently destroying search coverage while reporting a clean-looking failure. A page bounds both journal growth and blast radius, and keeps essentially all of the speedup. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed Six new tests in `tests/test_cli_memory_index_sync.py`, reusing that file's existing helpers and its `_REINDEX_PAGE_SIZE` monkeypatch pattern: 1. `..._batches_pages_instead_of_indexing_record_by_record` — asserts the observed page sizes are `[2, 2, 1]` and fails outright if the per-record path is touched. 2. `..._batched_output_matches_per_record_output` — every stored column (`memory_id`, `content`, `user_id`, `session_id`, `category`) and the FTS query result ordering are identical either way. 3. `..._empty_store_indexes_nothing_and_succeeds`. 4. `..._indexes_more_records_than_one_page`. 5. `..._failed_batch_still_indexes_valid_records_in_that_page` — the failure mode the design is built around. 6. `..._failed_record_is_named_and_exits_nonzero` — per-record diagnosability and exit status survive batching. Tests 1 and 5 fail on the base commit and pass with this change. ### Test Output ```text $ pytest tests/test_cli_memory_index_sync.py tests/test_memory/ -q 477 passed, 145 skipped in 10.38s $ pytest tests/test_memory/ tests/test_cli_memory_index_sync.py tests/test_cli/ -q 1212 passed, 146 skipped, 2 failed # Both failures (test_unwrap_claude_stops_claude_owned_persistent_deployment, # test_wrap_codex_prepare_only_updates_config) also fail on the base commit in # this environment: they read ambient CODEX_HOME / ANTHROPIC_BASE_URL from the # developer's shell rather than isolating it. Unrelated to this change. # The two behavioral tests against the base commit: $ git stash push headroom/cli/memory.py $ pytest tests/test_cli_memory_index_sync.py -q -k "reindex_batches or failed_batch" 2 failed $ pre-commit run # via commit hooks ruff (legacy alias)......................................................Passed ruff format..............................................................Passed mypy.....................................................................Passed ``` ## Real Behavior Proof - Environment: macOS 15 (arm64), Python 3.13.11, stdlib `sqlite3` with FTS5, worktree off `upstream/main` @ `e67b3c8a`. - Exact command / steps: `python benchmarks/memory_fts_reindex_benchmark.py --records 1000 --runs 3` for the indexing operation; separately, a `CliRunner` harness timing the whole `memory reindex` command over a 1,000-memory store, run on this branch and then with `headroom/cli/memory.py` stashed. Each run uses a fresh temporary database. - Observed result: the indexing operation alone goes from **759.7 ms to 8.1 ms** (median of 3, ~94x). The **whole `memory reindex` command** goes from **733.5 ms to 15.8 ms** (median of 3, ~46x) — reported separately because the command also pages the primary store and does vector-orphan cleanup, which this change does not touch. The audit that motivated this measured 778.69 ms vs 5.60 ms for the operation; reproduced within noise. - Not tested: real-world stores larger than 1,000 records, concurrent access during reindex, and behavior on network filesystems where fsync costs differ. Synthetic records (~40 tokens each) rather than production memory content. No end-to-end proxy or agent-latency claim is made. ## Runtime Rollout Safety - Rollout-managed feature(s): none; no flag or channel gates this. - Minimum rollout channel: N/A. - Stable/default behavior changed: no. Same records indexed, same fields, same FTS results, same output lines, same exit status. Only the transaction granularity changes. - Kill switch / disable path: none needed. If the batch path raises for any reason, the command falls back to the previous per-record behavior automatically, per page. - Unsafe override required: none. - Qualification impact: none. - Rollback path: revert the commit. `reindex` is idempotent — it clears and rebuilds the index from the primary store — so re-running it on any build produces the same result regardless of which version wrote the index. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` ## Additional Notes - No user-facing docs describe this internal rebuild path, so "documentation update" is satisfied by the comment explaining why the batch is bounded to a page rather than the whole store — that constraint is the non-obvious part of this diff. - Follow-up, deliberately not in this PR: the two failing tests noted above depend on the developer's ambient `CODEX_HOME` and `ANTHROPIC_BASE_URL`, so they pass or fail depending on whose shell runs them. That is a test-isolation bug worth its own change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
660 lines
23 KiB
Python
660 lines
23 KiB
Python
"""Tests for memory CLI index synchronization (issue #2856).
|
|
|
|
Verifies that headroom memory delete/prune/purge/edit remove stale entries
|
|
from the FTS5 and vector search indexes, not just from the primary store.
|
|
|
|
Vector index tests require sqlite-vec and are skipped when it is not installed.
|
|
They exercise the real SQLiteVectorIndex schema (vec0 virtual table) so that
|
|
the extension-aware connection path in _remove_from_search_indexes and
|
|
_clear_all_search_indexes is exercised rather than a plain-table stand-in.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
import headroom.cli.memory as memory_cli
|
|
from headroom.cli.main import main
|
|
from headroom.cli.memory import (
|
|
_clear_all_search_indexes,
|
|
_remove_from_search_indexes,
|
|
)
|
|
from headroom.memory.adapters.fts5 import FTS5TextIndex
|
|
from headroom.memory.adapters.sqlite import SQLiteMemoryStore
|
|
from headroom.memory.models import Memory
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# sqlite-vec availability guard
|
|
# ---------------------------------------------------------------------------
|
|
|
|
try:
|
|
from headroom.memory.adapters.sqlite_vector import (
|
|
SQLiteVectorIndex,
|
|
is_sqlite_vec_available,
|
|
)
|
|
|
|
SQLITE_VEC_AVAILABLE = is_sqlite_vec_available()
|
|
except ImportError:
|
|
SQLITE_VEC_AVAILABLE = False
|
|
SQLiteVectorIndex = None # type: ignore[assignment,misc]
|
|
|
|
requires_sqlite_vec = pytest.mark.skipif(
|
|
not SQLITE_VEC_AVAILABLE, reason="sqlite-vec not available"
|
|
)
|
|
|
|
_VEC_DIM = 4 # small dimension keeps test seeding fast
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _make_memory(memory_id: str, content: str = "test content") -> Memory:
|
|
return Memory(
|
|
id=memory_id,
|
|
content=content,
|
|
user_id="test-user",
|
|
)
|
|
|
|
|
|
def _seed_fts(db_path: Path, memories: list[Memory]) -> None:
|
|
"""Index memories into the FTS5 table."""
|
|
fts = FTS5TextIndex(db_path=str(db_path))
|
|
for mem in memories:
|
|
asyncio.run(fts.index_memory(mem))
|
|
|
|
|
|
def _seed_vector(db_path: Path, memory_ids: list[str]) -> None:
|
|
"""Seed the vector DB using the real SQLiteVectorIndex (requires sqlite-vec).
|
|
|
|
Creates the true vec0 virtual-table schema so the helpers under test
|
|
exercise the extension-aware connection path.
|
|
"""
|
|
vector_db = db_path.parent / f"{db_path.stem}_vectors.db"
|
|
index = SQLiteVectorIndex(dimension=_VEC_DIM, db_path=str(vector_db))
|
|
for mid in memory_ids:
|
|
embedding = list(
|
|
np.random.default_rng(abs(hash(mid))).standard_normal(_VEC_DIM).astype(float)
|
|
)
|
|
mem = Memory(id=mid, content="test", user_id="u", embedding=embedding)
|
|
asyncio.run(index.index(mem))
|
|
|
|
|
|
def _fts_count(db_path: Path) -> int:
|
|
with sqlite3.connect(str(db_path)) as conn:
|
|
return conn.execute("SELECT COUNT(*) FROM memory_fts").fetchone()[0]
|
|
|
|
|
|
def _fts_ids(db_path: Path) -> set[str]:
|
|
with sqlite3.connect(str(db_path)) as conn:
|
|
rows = conn.execute("SELECT memory_id FROM memory_fts").fetchall()
|
|
return {r[0] for r in rows}
|
|
|
|
|
|
def _vector_ids(db_path: Path) -> set[str]:
|
|
"""Read surviving memory_ids from the metadata table (regular, no extension needed)."""
|
|
vector_db = db_path.parent / f"{db_path.stem}_vectors.db"
|
|
if not vector_db.exists():
|
|
return set()
|
|
with sqlite3.connect(str(vector_db)) as conn:
|
|
rows = conn.execute("SELECT memory_id FROM vec_metadata").fetchall()
|
|
return {r[0] for r in rows}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _remove_from_search_indexes — FTS5 (no sqlite-vec required)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_remove_from_search_indexes_clears_fts_entries(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
mems = [_make_memory(f"id-{i}") for i in range(3)]
|
|
_seed_fts(db_path, mems)
|
|
assert _fts_count(db_path) == 3
|
|
|
|
_remove_from_search_indexes(str(db_path), ["id-0", "id-2"])
|
|
|
|
assert _fts_ids(db_path) == {"id-1"}
|
|
|
|
|
|
def test_remove_from_search_indexes_no_vector_db_is_noop(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
mems = [_make_memory("id-0")]
|
|
_seed_fts(db_path, mems)
|
|
|
|
# No vector DB → should not raise
|
|
_remove_from_search_indexes(str(db_path), ["id-0"])
|
|
|
|
assert _fts_count(db_path) == 0
|
|
|
|
|
|
def test_remove_from_search_indexes_empty_list_is_noop(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [_make_memory("id-0")])
|
|
assert _fts_count(db_path) == 1
|
|
|
|
_remove_from_search_indexes(str(db_path), [])
|
|
|
|
assert _fts_count(db_path) == 1
|
|
|
|
|
|
def test_remove_from_search_indexes_absent_optional_indexes_is_noop(tmp_path):
|
|
"""A primary-only store must not fail after its mutation already succeeded."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
asyncio.run(store.save(_make_memory("id-0")))
|
|
|
|
assert _remove_from_search_indexes(str(db_path), ["id-0"]) is True
|
|
assert _clear_all_search_indexes(str(db_path)) is True
|
|
|
|
|
|
def test_empty_uninitialized_vector_database_is_noop_without_sqlite_vec(tmp_path, monkeypatch):
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
asyncio.run(store.save(_make_memory("id-0")))
|
|
(tmp_path / "memory_vectors.db").touch()
|
|
monkeypatch.setitem(sys.modules, "sqlite_vec", None)
|
|
|
|
assert _remove_from_search_indexes(str(db_path), ["id-0"]) is True
|
|
assert _clear_all_search_indexes(str(db_path)) is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _remove_from_search_indexes — vector index (real vec0 schema, requires sqlite-vec)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_remove_from_search_indexes_clears_vector_entries(tmp_path):
|
|
"""Exercise the real vec0 virtual-table schema so the extension-aware
|
|
connection path in _remove_from_search_indexes is covered."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, []) # ensure memory.db exists
|
|
_seed_vector(db_path, ["id-0", "id-1", "id-2"])
|
|
assert _vector_ids(db_path) == {"id-0", "id-1", "id-2"}
|
|
|
|
_remove_from_search_indexes(str(db_path), ["id-0", "id-2"])
|
|
|
|
assert _vector_ids(db_path) == {"id-1"}
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_remove_from_search_indexes_no_vector_rows_to_delete_is_noop(tmp_path):
|
|
"""IDs not present in the vector index must be silently skipped."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [])
|
|
_seed_vector(db_path, ["id-0"])
|
|
assert _vector_ids(db_path) == {"id-0"}
|
|
|
|
_remove_from_search_indexes(str(db_path), ["id-99"]) # not in index
|
|
|
|
assert _vector_ids(db_path) == {"id-0"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _clear_all_search_indexes — FTS5 (no sqlite-vec required)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_clear_all_search_indexes_removes_all_fts_entries(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [_make_memory(f"id-{i}") for i in range(5)])
|
|
assert _fts_count(db_path) == 5
|
|
|
|
_clear_all_search_indexes(str(db_path))
|
|
|
|
assert _fts_count(db_path) == 0
|
|
|
|
|
|
def test_clear_all_search_indexes_no_vector_db_is_noop(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [_make_memory("id-0")])
|
|
|
|
_clear_all_search_indexes(str(db_path))
|
|
|
|
assert _fts_count(db_path) == 0 # FTS cleared; no vector DB is fine
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _clear_all_search_indexes — vector index (real vec0 schema, requires sqlite-vec)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_clear_all_search_indexes_removes_all_vector_entries(tmp_path):
|
|
"""Exercise the real vec0 virtual-table schema so the extension-aware
|
|
connection path in _clear_all_search_indexes is covered."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [])
|
|
_seed_vector(db_path, ["id-0", "id-1"])
|
|
assert _vector_ids(db_path) == {"id-0", "id-1"}
|
|
|
|
_clear_all_search_indexes(str(db_path))
|
|
|
|
assert _vector_ids(db_path) == set()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Integration: CLI commands wire up index sync correctly
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_delete_command_removes_from_fts(tmp_path):
|
|
"""Simulate delete command: delete_batch then _remove_from_search_indexes."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
mem = _make_memory("abc123")
|
|
asyncio.run(store.save(mem))
|
|
_seed_fts(db_path, [mem])
|
|
assert _fts_count(db_path) == 1
|
|
|
|
asyncio.run(store.delete_batch(["abc123"]))
|
|
_remove_from_search_indexes(str(db_path), ["abc123"])
|
|
|
|
assert _fts_count(db_path) == 0
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_delete_command_removes_from_vector_index(tmp_path):
|
|
"""Simulate delete command end-to-end with the real vec0 schema."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
mem = _make_memory("abc123")
|
|
asyncio.run(store.save(mem))
|
|
_seed_fts(db_path, [mem])
|
|
_seed_vector(db_path, ["abc123"])
|
|
assert _vector_ids(db_path) == {"abc123"}
|
|
|
|
asyncio.run(store.delete_batch(["abc123"]))
|
|
_remove_from_search_indexes(str(db_path), ["abc123"])
|
|
|
|
assert _fts_count(db_path) == 0
|
|
assert _vector_ids(db_path) == set()
|
|
|
|
|
|
def test_purge_command_clears_fts(tmp_path):
|
|
"""Simulate purge command: clear_all then _clear_all_search_indexes."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
for i in range(3):
|
|
asyncio.run(store.save(_make_memory(f"id-{i}")))
|
|
_seed_fts(db_path, [_make_memory(f"id-{i}") for i in range(3)])
|
|
assert _fts_count(db_path) == 3
|
|
|
|
asyncio.run(store.clear_all())
|
|
_clear_all_search_indexes(str(db_path))
|
|
|
|
assert _fts_count(db_path) == 0
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_purge_command_clears_vector_index(tmp_path):
|
|
"""Simulate purge command end-to-end with the real vec0 schema."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
for i in range(3):
|
|
asyncio.run(store.save(_make_memory(f"id-{i}")))
|
|
_seed_fts(db_path, [_make_memory(f"id-{i}") for i in range(3)])
|
|
_seed_vector(db_path, [f"id-{i}" for i in range(3)])
|
|
assert _vector_ids(db_path) == {"id-0", "id-1", "id-2"}
|
|
|
|
asyncio.run(store.clear_all())
|
|
_clear_all_search_indexes(str(db_path))
|
|
|
|
assert _fts_count(db_path) == 0
|
|
assert _vector_ids(db_path) == set()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Failure-path: return value and exit-code impact
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_remove_from_search_indexes_fts_failure_returns_false(tmp_path):
|
|
"""When FTS5 delete raises, the function returns False (not True)."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [_make_memory("id-0")])
|
|
|
|
# sqlite3 is imported locally inside the helper so we patch the global module.
|
|
original_connect = sqlite3.connect
|
|
call_count = [0]
|
|
|
|
def failing_connect(path, **kwargs):
|
|
call_count[0] += 1
|
|
if call_count[0] == 1: # first call is the FTS5 db open
|
|
raise sqlite3.OperationalError("simulated FTS5 failure")
|
|
return original_connect(path, **kwargs)
|
|
|
|
with patch("sqlite3.connect", side_effect=failing_connect):
|
|
result = _remove_from_search_indexes(str(db_path), ["id-0"])
|
|
|
|
assert result is False
|
|
|
|
|
|
def test_remove_from_search_indexes_sqlite_vec_missing_returns_false(tmp_path):
|
|
"""When sqlite_vec is absent and a vector DB exists, returns False."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [])
|
|
# Create a non-empty vector DB file so the code doesn't short-circuit.
|
|
vector_db = tmp_path / "memory_vectors.db"
|
|
vector_db.write_bytes(b"placeholder")
|
|
|
|
# Remove sqlite_vec from sys.modules so `import sqlite_vec` raises ImportError.
|
|
with patch.dict(sys.modules, {"sqlite_vec": None}):
|
|
result = _remove_from_search_indexes(str(db_path), ["id-0"])
|
|
|
|
assert result is False
|
|
|
|
|
|
def test_clear_all_search_indexes_fts_failure_returns_false(tmp_path):
|
|
"""When FTS5 DELETE raises, _clear_all_search_indexes returns False."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [_make_memory("id-0")])
|
|
|
|
original_connect = sqlite3.connect
|
|
call_count = [0]
|
|
|
|
def failing_connect(path, **kwargs):
|
|
call_count[0] += 1
|
|
if call_count[0] == 1:
|
|
raise sqlite3.OperationalError("simulated FTS5 failure")
|
|
return original_connect(path, **kwargs)
|
|
|
|
with patch("sqlite3.connect", side_effect=failing_connect):
|
|
result = _clear_all_search_indexes(str(db_path))
|
|
|
|
assert result is False
|
|
|
|
|
|
def test_clear_all_search_indexes_sqlite_vec_missing_returns_false(tmp_path):
|
|
"""When sqlite_vec is absent and a vector DB exists, returns False."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seed_fts(db_path, [])
|
|
vector_db = tmp_path / "memory_vectors.db"
|
|
vector_db.write_bytes(b"placeholder")
|
|
|
|
with patch.dict(sys.modules, {"sqlite_vec": None}):
|
|
result = _clear_all_search_indexes(str(db_path))
|
|
|
|
assert result is False
|
|
|
|
|
|
def test_reindex_pages_through_complete_store(tmp_path, monkeypatch):
|
|
"""Records beyond the first page remain represented in rebuilt FTS."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memories = [_make_memory(f"id-{i}", f"content {i}") for i in range(5)]
|
|
for memory in memories:
|
|
asyncio.run(store.save(memory))
|
|
_seed_fts(db_path, memories[:2])
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 2)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert _fts_ids(db_path) == {memory.id for memory in memories}
|
|
assert "Re-indexed 5/5 memories" in result.output
|
|
|
|
|
|
@requires_sqlite_vec
|
|
def test_reindex_keeps_valid_vectors_beyond_first_page(tmp_path, monkeypatch):
|
|
"""Complete primary IDs, not one page, determine vector orphans."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memories = [_make_memory(f"id-{i}", f"content {i}") for i in range(5)]
|
|
for memory in memories:
|
|
asyncio.run(store.save(memory))
|
|
_seed_vector(db_path, [memory.id for memory in memories] + ["orphan"])
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 2)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert _vector_ids(db_path) == {memory.id for memory in memories}
|
|
|
|
|
|
def test_delete_command_exits_nonzero_when_index_sync_fails(tmp_path, monkeypatch):
|
|
"""The real Click command must not report a partially synced delete as success."""
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memory = _make_memory("abc123")
|
|
asyncio.run(store.save(memory))
|
|
monkeypatch.setattr(memory_cli, "_remove_from_search_indexes", lambda *_args: False)
|
|
|
|
result = CliRunner().invoke(
|
|
main,
|
|
["memory", "delete", memory.id, "--force", "--db-path", str(db_path)],
|
|
)
|
|
|
|
assert result.exit_code == 1
|
|
assert asyncio.run(store.get(memory.id)) is None
|
|
assert "index sync incomplete" in result.output
|
|
|
|
|
|
def test_edit_command_exits_nonzero_when_index_sync_fails(tmp_path, monkeypatch):
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memory = _make_memory("abc123", "before")
|
|
asyncio.run(store.save(memory))
|
|
monkeypatch.setattr(memory_cli, "_remove_from_search_indexes", lambda *_args: False)
|
|
|
|
result = CliRunner().invoke(
|
|
main,
|
|
[
|
|
"memory",
|
|
"edit",
|
|
memory.id,
|
|
"--content",
|
|
"after",
|
|
"--db-path",
|
|
str(db_path),
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 1
|
|
assert asyncio.run(store.get(memory.id)).content == "after"
|
|
assert "index sync incomplete" in result.output
|
|
|
|
|
|
def test_prune_command_exits_nonzero_when_index_sync_fails(tmp_path, monkeypatch):
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memory = _make_memory("abc123")
|
|
asyncio.run(store.save(memory))
|
|
monkeypatch.setattr(memory_cli, "_remove_from_search_indexes", lambda *_args: False)
|
|
|
|
result = CliRunner().invoke(
|
|
main,
|
|
[
|
|
"memory",
|
|
"prune",
|
|
"--low-importance",
|
|
"1.0",
|
|
"--force",
|
|
"--db-path",
|
|
str(db_path),
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 1
|
|
assert asyncio.run(store.get(memory.id)) is None
|
|
assert "index sync incomplete" in result.output
|
|
|
|
|
|
def test_purge_command_exits_nonzero_when_index_sync_fails(tmp_path, monkeypatch):
|
|
db_path = tmp_path / "memory.db"
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memory = _make_memory("abc123")
|
|
asyncio.run(store.save(memory))
|
|
monkeypatch.setattr(memory_cli, "_clear_all_search_indexes", lambda *_args: False)
|
|
|
|
result = CliRunner().invoke(
|
|
main,
|
|
["memory", "purge", "--confirm", "--db-path", str(db_path)],
|
|
input="y\n",
|
|
)
|
|
|
|
assert result.exit_code == 1
|
|
assert asyncio.run(store.get(memory.id)) is None
|
|
assert "index sync incomplete" in result.output
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# reindex batching
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _always_raises(message: str):
|
|
async def _raise(*_args, **_kwargs):
|
|
raise RuntimeError(message)
|
|
|
|
return _raise
|
|
|
|
|
|
def _fts_rows(db_path: Path) -> dict[str, tuple[str, str, str, str]]:
|
|
with sqlite3.connect(str(db_path)) as conn:
|
|
rows = conn.execute(
|
|
"SELECT memory_id, content, user_id, session_id, category FROM memory_fts"
|
|
).fetchall()
|
|
return {r[0]: (r[1], r[2], r[3], r[4]) for r in rows}
|
|
|
|
|
|
def _seeded_store(db_path: Path, count: int) -> list[Memory]:
|
|
store = SQLiteMemoryStore(str(db_path))
|
|
memories = []
|
|
for i in range(count):
|
|
mem = _make_memory(f"id-{i}", f"content {i}")
|
|
mem.session_id = f"session-{i % 3}"
|
|
memories.append(mem)
|
|
asyncio.run(store.save(mem))
|
|
return memories
|
|
|
|
|
|
def test_reindex_batches_pages_instead_of_indexing_record_by_record(tmp_path, monkeypatch):
|
|
"""The batch API does the work; the per-record path is only a fallback."""
|
|
db_path = tmp_path / "memory.db"
|
|
_seeded_store(db_path, 5)
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 2)
|
|
|
|
batch_sizes: list[int] = []
|
|
real_batch = FTS5TextIndex.index_batch_memories
|
|
|
|
async def spy_batch(self, memories):
|
|
batch_sizes.append(len(memories))
|
|
return await real_batch(self, memories)
|
|
|
|
monkeypatch.setattr(FTS5TextIndex, "index_batch_memories", spy_batch)
|
|
monkeypatch.setattr(
|
|
FTS5TextIndex,
|
|
"index_memory",
|
|
lambda *_a, **_k: pytest.fail("reindex fell back to per-record indexing"),
|
|
)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert batch_sizes == [2, 2, 1]
|
|
assert "Re-indexed 5/5 memories" in result.output
|
|
|
|
|
|
def test_reindex_batched_output_matches_per_record_output(tmp_path, monkeypatch):
|
|
"""Every indexed field and FTS query result is identical either way."""
|
|
batched_db = tmp_path / "batched.db"
|
|
per_record_db = tmp_path / "per_record.db"
|
|
for db_path in (batched_db, per_record_db):
|
|
_seeded_store(db_path, 7)
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 3)
|
|
|
|
batched = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(batched_db)])
|
|
assert batched.exit_code == 0, batched.output
|
|
|
|
# Force the per-record path by making every batch fail.
|
|
monkeypatch.setattr(
|
|
FTS5TextIndex,
|
|
"index_batch_memories",
|
|
_always_raises("batching disabled for parity check"),
|
|
)
|
|
per_record = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(per_record_db)])
|
|
assert per_record.exit_code == 0, per_record.output
|
|
|
|
assert _fts_rows(batched_db) == _fts_rows(per_record_db)
|
|
batched_hits = FTS5TextIndex(db_path=str(batched_db)).search("content", k=10)
|
|
per_record_hits = FTS5TextIndex(db_path=str(per_record_db)).search("content", k=10)
|
|
assert [h.memory_id for h in batched_hits] == [h.memory_id for h in per_record_hits]
|
|
|
|
|
|
def test_reindex_empty_store_indexes_nothing_and_succeeds(tmp_path):
|
|
db_path = tmp_path / "memory.db"
|
|
SQLiteMemoryStore(str(db_path))
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert _fts_ids(db_path) == set()
|
|
assert "Re-indexed 0/0 memories" in result.output
|
|
|
|
|
|
def test_reindex_indexes_more_records_than_one_page(tmp_path, monkeypatch):
|
|
db_path = tmp_path / "memory.db"
|
|
memories = _seeded_store(db_path, 7)
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 3)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert _fts_ids(db_path) == {m.id for m in memories}
|
|
assert "Re-indexed 7/7 memories" in result.output
|
|
|
|
|
|
def test_reindex_failed_batch_still_indexes_valid_records_in_that_page(tmp_path, monkeypatch):
|
|
"""A rolled-back page must not leave a hole: valid records still land."""
|
|
db_path = tmp_path / "memory.db"
|
|
memories = _seeded_store(db_path, 4)
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 4)
|
|
monkeypatch.setattr(
|
|
FTS5TextIndex, "index_batch_memories", _always_raises("simulated batch failure")
|
|
)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 0, result.output
|
|
assert _fts_ids(db_path) == {m.id for m in memories}
|
|
assert "page rolled back" in result.output
|
|
assert "Re-indexed 4/4 memories" in result.output
|
|
|
|
|
|
def test_reindex_failed_record_is_named_and_exits_nonzero(tmp_path, monkeypatch):
|
|
"""Per-record diagnosability survives batching."""
|
|
db_path = tmp_path / "memory.db"
|
|
memories = _seeded_store(db_path, 3)
|
|
monkeypatch.setattr(memory_cli, "_REINDEX_PAGE_SIZE", 3)
|
|
monkeypatch.setattr(
|
|
FTS5TextIndex, "index_batch_memories", _always_raises("simulated batch failure")
|
|
)
|
|
|
|
broken = memories[1].id
|
|
real_index_memory = FTS5TextIndex.index_memory
|
|
|
|
async def index_or_fail(self, memory):
|
|
if memory.id == broken:
|
|
raise RuntimeError("simulated record failure")
|
|
await real_index_memory(self, memory)
|
|
|
|
monkeypatch.setattr(FTS5TextIndex, "index_memory", index_or_fail)
|
|
|
|
result = CliRunner().invoke(main, ["memory", "reindex", "--db-path", str(db_path)])
|
|
|
|
assert result.exit_code == 1
|
|
assert f"failed to index {broken[:8]}" in result.output
|
|
assert _fts_ids(db_path) == {memories[0].id, memories[2].id}
|
|
assert "Re-indexed 2/3 memories" in result.output
|