forked from enderofwings/NexusOS
fix(runtime): improve local service reliability
Close SQLite handles safely on Windows, clean orphaned vectors, normalize Ollama endpoints, and surface model errors without leaking reasoning tags. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -97,6 +97,61 @@ def test_conversation_recall_uses_vec_and_matches_brute_force():
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_delete_conversation_removes_message_vectors():
|
||||
"""Deleting a conversation must take its embeddings with it — orphaned
|
||||
message_vectors rows are invisible to recall but grow the DB forever."""
|
||||
s = _store()
|
||||
|
||||
async def run():
|
||||
s.create_conversation("c1")
|
||||
s.add_message("c1", "user", "tell me about lego star wars")
|
||||
s.add_message("c1", "assistant", "lego star wars is a fun game")
|
||||
s.create_conversation("c2")
|
||||
s.add_message("c2", "user", "gpu vega vram notes")
|
||||
|
||||
# the first search lazily backfills a vector for every message
|
||||
await s.semantic_search_conversations("lego star wars", _fake_embed, limit=2, min_score=0.1)
|
||||
conn = s._connect()
|
||||
assert conn.execute("SELECT COUNT(*) FROM message_vectors").fetchone()[0] == 3
|
||||
conn.close()
|
||||
|
||||
s.delete_conversation("c1")
|
||||
|
||||
conn = s._connect()
|
||||
orphans = conn.execute(
|
||||
"SELECT COUNT(*) FROM message_vectors v "
|
||||
"LEFT JOIN messages m ON m.id = v.message_id WHERE m.id IS NULL"
|
||||
).fetchone()[0]
|
||||
assert orphans == 0
|
||||
assert conn.execute("SELECT COUNT(*) FROM message_vectors").fetchone()[0] == 1 # c2 untouched
|
||||
if s.vec_enabled: # the ANN mirror is pruned too, not just the JSON table
|
||||
assert conn.execute("SELECT COUNT(*) FROM vec_messages").fetchone()[0] == 1
|
||||
conn.close()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_startup_sweeps_pre_existing_orphan_vectors():
|
||||
"""Databases written before delete_conversation cleaned up after itself are
|
||||
repaired the next time the store opens them."""
|
||||
import json
|
||||
path = Path(tempfile.mkdtemp()) / "t.db"
|
||||
s = PersistentMemoryStore(path)
|
||||
s.create_conversation("c1")
|
||||
mid = s.add_message("c1", "user", "lego star wars")
|
||||
conn = s._connect()
|
||||
conn.execute("INSERT INTO message_vectors (message_id, embedding) VALUES (?, ?)",
|
||||
(mid, json.dumps([1.0, 0.0])))
|
||||
conn.execute("DELETE FROM messages WHERE id = ?", (mid,)) # the old leaky delete
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
reopened = PersistentMemoryStore(path)
|
||||
conn = reopened._connect()
|
||||
assert conn.execute("SELECT COUNT(*) FROM message_vectors").fetchone()[0] == 0
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_projects_scope_documents_and_survive_delete():
|
||||
s = _store()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user