fix(vector): get_collection no longer silently deletes collection on error; memory builder skips full rebuild when index hash matches

This commit is contained in:
Research Assistant 2026-05-18 22:18:23 +08:00
parent 5902f6cd4b
commit baa6c758f5
2 changed files with 23 additions and 13 deletions

View file

@ -22,17 +22,10 @@ def get_collection(vault: Path):
db_path = get_vector_db_path(vault)
db_path.mkdir(parents=True, exist_ok=True)
client = chroma.PersistentClient(path=str(db_path))
try:
return client.get_or_create_collection(
name="paperforge_fulltext",
metadata={"hnsw:space": "cosine"},
)
except Exception:
client.delete_collection("paperforge_fulltext")
return client.create_collection(
name="paperforge_fulltext",
metadata={"hnsw:space": "cosine"},
)
return client.get_or_create_collection(
name="paperforge_fulltext",
metadata={"hnsw:space": "cosine"},
)
def delete_paper_vectors(vault: Path, zotero_key: str) -> int:

View file

@ -129,7 +129,7 @@ def _import_correction_log(conn, vault: Path) -> int:
def build_from_index(vault: Path) -> dict:
"""Read formal-library.json and build/rebuild paperforge.db.
Returns a dict with counts for reporting.
"""
envelope = read_index(vault)
@ -137,7 +137,6 @@ def build_from_index(vault: Path) -> dict:
raise FileNotFoundError(
"Canonical index not found. Run paperforge sync --rebuild-index."
)
# Legacy format: bare list of entries (pre-envelope)
if isinstance(envelope, list):
items = envelope
generated_at = ""
@ -147,6 +146,24 @@ def build_from_index(vault: Path) -> dict:
canonical_hash = compute_hash(items) if isinstance(items, list) and items and isinstance(items[0], dict) else ""
db_path = get_memory_db_path(vault)
# fast-path: if index hash matches, nothing changed
if canonical_hash and db_path.exists():
try:
conn = get_connection(db_path, read_only=False)
cached = conn.execute("SELECT value FROM meta WHERE key='canonical_index_hash'").fetchone()
stored_version = get_schema_version(conn)
if cached and cached[0] == canonical_hash and stored_version == CURRENT_SCHEMA_VERSION:
papers_count = conn.execute("SELECT COUNT(*) FROM papers").fetchone()[0]
conn.close()
return {
"papers_indexed": papers_count,
"db_path": str(db_path),
"hash_match": True,
}
conn.close()
except Exception:
pass
conn = get_connection(db_path, read_only=False)
try:
stored_version = get_schema_version(conn)