fix(memory): sweep legacy orphaned message vectors
Repair vector rows left behind by older databases at store startup. Keep the existing single-statement delete path from main and avoid reintroducing the redundant batched helper.
This commit is contained in:
+26
-1
@@ -251,6 +251,8 @@ class PersistentMemoryStore:
|
||||
"DELETE FROM settings WHERE key IN ('anthropic_api_key', 'escalation_model')"
|
||||
)
|
||||
|
||||
self._sweep_orphan_msg_vectors(conn)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -788,6 +790,29 @@ class PersistentMemoryStore:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _sweep_orphan_msg_vectors(self, conn) -> None:
|
||||
"""One-time repair for databases written before delete_conversation
|
||||
cleaned up after itself: drop vectors whose message is already gone."""
|
||||
try:
|
||||
ids = [r["message_id"] for r in conn.execute(
|
||||
"SELECT v.message_id FROM message_vectors v "
|
||||
"LEFT JOIN messages m ON m.id = v.message_id WHERE m.id IS NULL"
|
||||
).fetchall()]
|
||||
if ids:
|
||||
conn.execute(
|
||||
"DELETE FROM message_vectors WHERE message_id NOT IN "
|
||||
"(SELECT id FROM messages)"
|
||||
)
|
||||
if self.vec_enabled and conn.execute(
|
||||
"SELECT 1 FROM sqlite_master WHERE name = 'vec_messages'"
|
||||
).fetchone():
|
||||
for message_id in ids:
|
||||
conn.execute(
|
||||
"DELETE FROM vec_messages WHERE rowid = ?", (message_id,)
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _backfill_vec_msgs(self, conn, dim: int) -> None:
|
||||
"""Index any message_vectors rows missing from vec_messages."""
|
||||
try:
|
||||
@@ -1215,4 +1240,4 @@ class PersistentMemoryStore:
|
||||
from ..nexus_config import MEMORY_DB
|
||||
|
||||
DB_PATH = MEMORY_DB
|
||||
store = PersistentMemoryStore(DB_PATH)
|
||||
store = PersistentMemoryStore(DB_PATH)
|
||||
|
||||
Reference in New Issue
Block a user