fix: Phase 2 integrity review - v2.0.0 rewrite & bugfixes
Memory Consolidation Plugin (828 -> 465 lines): - Replace SentenceTransformer with cat.embedder.embed_query() for vector consistency - Fix per-user fact isolation: source=user_id instead of global - Add duplicate fact detection (_is_duplicate_fact, score_threshold=0.85) - Remove ~350 lines of dead async run_consolidation() code - Remove duplicate declarative search in before_cat_sends_message - Unify trivial patterns into TRIVIAL_PATTERNS frozenset - Remove all sys.stderr.write debug logging - Remove sentence-transformers from requirements.txt (no external deps) Loguru Fix (cheshire-cat/cat/log.py): - Patch Cat v1.6.2 loguru format to provide default extra fields - Fixes KeyError: 'original_name' from third-party libs (fastembed) - Mounted via docker-compose volume Discord Bridge: - Copy discord_bridge.py to cat-plugins/ (was empty directory) Test Results (6/7 pass, 100% fact recall): - 11 facts extracted, per-user isolation working - Duplicate detection effective (+2 on 2nd run) - 5/5 natural language recall queries correct
This commit is contained in:
@@ -1,196 +1,254 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Full Pipeline Test for Memory Consolidation System
|
||||
Tests all phases: Storage → Consolidation → Fact Extraction → Recall
|
||||
Full Pipeline Test for Memory Consolidation System v2.0.0
|
||||
"""
|
||||
|
||||
import requests
|
||||
import time
|
||||
import json
|
||||
import sys
|
||||
|
||||
BASE_URL = "http://localhost:1865"
|
||||
CAT_URL = "http://localhost:1865"
|
||||
QDRANT_URL = "http://localhost:6333"
|
||||
CONSOLIDATION_TIMEOUT = 180
|
||||
|
||||
def send_message(text):
|
||||
"""Send a message to Miku and get response"""
|
||||
resp = requests.post(f"{BASE_URL}/message", json={"text": text})
|
||||
return resp.json()
|
||||
|
||||
def get_qdrant_count(collection):
|
||||
"""Get count of items in Qdrant collection"""
|
||||
resp = requests.post(
|
||||
f"http://localhost:6333/collections/{collection}/points/scroll",
|
||||
json={"limit": 1000, "with_payload": False, "with_vector": False}
|
||||
)
|
||||
return len(resp.json()["result"]["points"])
|
||||
def send_message(text, timeout=30):
|
||||
try:
|
||||
resp = requests.post(f"{CAT_URL}/message", json={"text": text}, timeout=timeout)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except requests.exceptions.Timeout:
|
||||
return {"error": "timeout", "content": ""}
|
||||
except Exception as e:
|
||||
return {"error": str(e), "content": ""}
|
||||
|
||||
|
||||
def qdrant_scroll(collection, limit=200, filt=None):
|
||||
body = {"limit": limit, "with_payload": True, "with_vector": False}
|
||||
if filt:
|
||||
body["filter"] = filt
|
||||
resp = requests.post(f"{QDRANT_URL}/collections/{collection}/points/scroll", json=body)
|
||||
return resp.json()["result"]["points"]
|
||||
|
||||
|
||||
def qdrant_count(collection):
|
||||
return len(qdrant_scroll(collection))
|
||||
|
||||
|
||||
def section(title):
|
||||
print(f"\n{'=' * 70}")
|
||||
print(f" {title}")
|
||||
print(f"{'=' * 70}")
|
||||
|
||||
|
||||
print("=" * 70)
|
||||
print("🧪 FULL PIPELINE TEST - Memory Consolidation System")
|
||||
print(" FULL PIPELINE TEST - Memory Consolidation v2.0.0")
|
||||
print("=" * 70)
|
||||
|
||||
try:
|
||||
requests.get(f"{CAT_URL}/", timeout=5)
|
||||
except Exception:
|
||||
print("ERROR: Cat not reachable"); sys.exit(1)
|
||||
try:
|
||||
requests.get(f"{QDRANT_URL}/collections", timeout=5)
|
||||
except Exception:
|
||||
print("ERROR: Qdrant not reachable"); sys.exit(1)
|
||||
|
||||
episodic_start = qdrant_count("episodic")
|
||||
declarative_start = qdrant_count("declarative")
|
||||
print(f"\nStarting state: {episodic_start} episodic, {declarative_start} declarative")
|
||||
|
||||
results = {}
|
||||
|
||||
# TEST 1: Trivial Message Filtering
|
||||
print("\n📋 TEST 1: Trivial Message Filtering")
|
||||
print("-" * 70)
|
||||
section("TEST 1: Trivial Message Filtering")
|
||||
|
||||
trivial_messages = ["lol", "k", "ok", "haha", "xd"]
|
||||
important_message = "My name is Alex and I live in Seattle"
|
||||
|
||||
print("Sending trivial messages (should be filtered out)...")
|
||||
trivial_messages = ["lol", "k", "ok", "haha", "xd", "brb"]
|
||||
print(f"Sending {len(trivial_messages)} trivial messages...")
|
||||
for msg in trivial_messages:
|
||||
send_message(msg)
|
||||
time.sleep(0.5)
|
||||
time.sleep(0.3)
|
||||
|
||||
print("Sending important message...")
|
||||
send_message(important_message)
|
||||
time.sleep(1)
|
||||
# Count only USER episodic memories (exclude Miku's responses)
|
||||
user_episodic = qdrant_scroll("episodic", filt={
|
||||
"must_not": [{"key": "metadata.speaker", "match": {"value": "miku"}}]
|
||||
})
|
||||
trivial_user_stored = len(user_episodic) - episodic_start
|
||||
episodic_after_trivial = qdrant_count("episodic")
|
||||
|
||||
episodic_count = get_qdrant_count("episodic")
|
||||
print(f"\n✅ Episodic memories stored: {episodic_count}")
|
||||
if episodic_count < len(trivial_messages):
|
||||
print(" ✓ Trivial filtering working! (some messages were filtered)")
|
||||
# discord_bridge filters trivial user messages, but Miku still responds
|
||||
# so we only check user-side storage
|
||||
if trivial_user_stored < len(trivial_messages):
|
||||
print(f" PASS - Only {trivial_user_stored}/{len(trivial_messages)} user trivial messages stored")
|
||||
print(f" (Total episodic incl. Miku responses: {episodic_after_trivial})")
|
||||
results["trivial_filtering"] = True
|
||||
else:
|
||||
print(" ⚠️ Trivial filtering may not be active")
|
||||
print(f" WARN - All {trivial_user_stored} trivial messages stored")
|
||||
results["trivial_filtering"] = False
|
||||
|
||||
# TEST 2: Miku's Response Storage
|
||||
print("\n📋 TEST 2: Miku's Response Storage")
|
||||
print("-" * 70)
|
||||
# TEST 2: Important Message Storage
|
||||
section("TEST 2: Important Message Storage")
|
||||
|
||||
print("Sending message and checking if Miku's response is stored...")
|
||||
resp = send_message("Tell me a very short fact about music")
|
||||
miku_said = resp["content"]
|
||||
print(f"Miku said: {miku_said[:80]}...")
|
||||
time.sleep(2)
|
||||
|
||||
# Check for Miku's messages in episodic
|
||||
resp = requests.post(
|
||||
"http://localhost:6333/collections/episodic/points/scroll",
|
||||
json={
|
||||
"limit": 100,
|
||||
"with_payload": True,
|
||||
"with_vector": False,
|
||||
"filter": {"must": [{"key": "metadata.speaker", "match": {"value": "miku"}}]}
|
||||
}
|
||||
)
|
||||
miku_messages = resp.json()["result"]["points"]
|
||||
print(f"\n✅ Miku's messages in memory: {len(miku_messages)}")
|
||||
if miku_messages:
|
||||
print(f" Example: {miku_messages[0]['payload']['page_content'][:60]}...")
|
||||
print(" ✓ Bidirectional memory working!")
|
||||
else:
|
||||
print(" ⚠️ Miku's responses not being stored")
|
||||
|
||||
# TEST 3: Add Rich Personal Information
|
||||
print("\n📋 TEST 3: Adding Personal Information")
|
||||
print("-" * 70)
|
||||
|
||||
personal_info = [
|
||||
personal_facts = [
|
||||
"My name is Sarah Chen",
|
||||
"I'm 28 years old",
|
||||
"I work as a data scientist at Google",
|
||||
"My favorite color is blue",
|
||||
"I love playing piano",
|
||||
"I live in Seattle, Washington",
|
||||
"I work as a software engineer at Microsoft",
|
||||
"My favorite color is forest green",
|
||||
"I love playing piano and have practiced for 15 years",
|
||||
"I'm learning Japanese, currently at N3 level",
|
||||
"I have a cat named Luna",
|
||||
"I'm allergic to peanuts",
|
||||
"I live in Tokyo, Japan",
|
||||
"My hobbies include photography and hiking"
|
||||
"My birthday is March 15th",
|
||||
"I graduated from UW in 2018",
|
||||
"I enjoy hiking on weekends",
|
||||
]
|
||||
|
||||
print(f"Adding {len(personal_info)} messages with personal information...")
|
||||
for info in personal_info:
|
||||
send_message(info)
|
||||
print(f"Sending {len(personal_facts)} personal info messages...")
|
||||
for i, fact in enumerate(personal_facts, 1):
|
||||
resp = send_message(fact)
|
||||
status = "OK" if "error" not in resp else "ERR"
|
||||
print(f" [{i}/{len(personal_facts)}] {status} {fact[:50]}")
|
||||
time.sleep(0.5)
|
||||
|
||||
episodic_after = get_qdrant_count("episodic")
|
||||
print(f"\n✅ Total episodic memories: {episodic_after}")
|
||||
print(f" ({episodic_after - episodic_count} new memories added)")
|
||||
time.sleep(1)
|
||||
episodic_after_personal = qdrant_count("episodic")
|
||||
personal_stored = episodic_after_personal - episodic_after_trivial
|
||||
print(f"\n Episodic memories from personal info: {personal_stored}")
|
||||
results["important_storage"] = personal_stored >= len(personal_facts)
|
||||
print(f" {'PASS' if results['important_storage'] else 'FAIL'} - Expected >={len(personal_facts)}, got {personal_stored}")
|
||||
|
||||
# TEST 4: Memory Consolidation
|
||||
print("\n📋 TEST 4: Memory Consolidation & Fact Extraction")
|
||||
print("-" * 70)
|
||||
# TEST 3: Miku Response Storage
|
||||
section("TEST 3: Bidirectional Memory (Miku Response Storage)")
|
||||
|
||||
print("Triggering consolidation...")
|
||||
resp = send_message("consolidate now")
|
||||
consolidation_result = resp["content"]
|
||||
print(f"\n{consolidation_result}")
|
||||
miku_points = qdrant_scroll("episodic", filt={
|
||||
"must": [{"key": "metadata.speaker", "match": {"value": "miku"}}]
|
||||
})
|
||||
print(f" Miku's memories in episodic: {len(miku_points)}")
|
||||
if miku_points:
|
||||
print(f" Sample: \"{miku_points[0]['payload']['page_content'][:70]}\"")
|
||||
results["miku_storage"] = True
|
||||
print(" PASS")
|
||||
else:
|
||||
results["miku_storage"] = False
|
||||
print(" FAIL - No Miku responses in episodic memory")
|
||||
|
||||
time.sleep(2)
|
||||
# TEST 4: Per-User Source Tagging
|
||||
section("TEST 4: Per-User Source Tagging")
|
||||
|
||||
# Check declarative facts
|
||||
declarative_count = get_qdrant_count("declarative")
|
||||
print(f"\n✅ Declarative facts extracted: {declarative_count}")
|
||||
user_points = qdrant_scroll("episodic", filt={
|
||||
"must": [{"key": "metadata.source", "match": {"value": "user"}}]
|
||||
})
|
||||
print(f" Points with source='user': {len(user_points)}")
|
||||
|
||||
if declarative_count > 0:
|
||||
# Show sample facts
|
||||
resp = requests.post(
|
||||
"http://localhost:6333/collections/declarative/points/scroll",
|
||||
json={"limit": 5, "with_payload": True, "with_vector": False}
|
||||
)
|
||||
facts = resp.json()["result"]["points"]
|
||||
print("\nSample facts:")
|
||||
for i, fact in enumerate(facts[:5], 1):
|
||||
print(f" {i}. {fact['payload']['page_content']}")
|
||||
global_points = qdrant_scroll("episodic", filt={
|
||||
"must": [{"key": "metadata.source", "match": {"value": "global"}}]
|
||||
})
|
||||
print(f" Points with source='global' (old bug): {len(global_points)}")
|
||||
|
||||
# TEST 5: Fact Recall
|
||||
print("\n📋 TEST 5: Declarative Fact Recall")
|
||||
print("-" * 70)
|
||||
results["user_tagging"] = len(user_points) > 0 and len(global_points) == 0
|
||||
print(f" {'PASS' if results['user_tagging'] else 'FAIL'}")
|
||||
|
||||
queries = [
|
||||
"What is my name?",
|
||||
"How old am I?",
|
||||
"Where do I work?",
|
||||
"What's my favorite color?",
|
||||
"What am I allergic to?"
|
||||
]
|
||||
# TEST 5: Memory Consolidation
|
||||
section("TEST 5: Memory Consolidation & Fact Extraction")
|
||||
|
||||
print("Testing fact recall with queries...")
|
||||
correct_recalls = 0
|
||||
for query in queries:
|
||||
resp = send_message(query)
|
||||
answer = resp["content"]
|
||||
print(f"\n❓ {query}")
|
||||
print(f"💬 Miku: {answer[:150]}...")
|
||||
|
||||
# Basic heuristic: check if answer contains likely keywords
|
||||
keywords = {
|
||||
"What is my name?": ["Sarah", "Chen"],
|
||||
"How old am I?": ["28"],
|
||||
"Where do I work?": ["Google", "data scientist"],
|
||||
"What's my favorite color?": ["blue"],
|
||||
"What am I allergic to?": ["peanut"]
|
||||
}
|
||||
|
||||
if any(kw.lower() in answer.lower() for kw in keywords[query]):
|
||||
print(" ✓ Correct recall!")
|
||||
correct_recalls += 1
|
||||
else:
|
||||
print(" ⚠️ May not have recalled correctly")
|
||||
|
||||
print(f" Triggering consolidation (timeout={CONSOLIDATION_TIMEOUT}s)...")
|
||||
t0 = time.time()
|
||||
resp = send_message("consolidate now", timeout=CONSOLIDATION_TIMEOUT)
|
||||
elapsed = time.time() - t0
|
||||
|
||||
if "error" in resp:
|
||||
print(f" WARN - HTTP issue: {resp['error']} ({elapsed:.0f}s)")
|
||||
print(" Waiting 60s for background completion...")
|
||||
time.sleep(60)
|
||||
else:
|
||||
print(f" Completed in {elapsed:.1f}s")
|
||||
content = resp.get("content", "")
|
||||
print(f" Response: {content[:120]}...")
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
declarative_after = qdrant_count("declarative")
|
||||
new_facts = declarative_after - declarative_start
|
||||
print(f"\n Declarative facts: {declarative_start} -> {declarative_after} (+{new_facts})")
|
||||
|
||||
results["consolidation"] = new_facts >= 5
|
||||
print(f" {'PASS' if results['consolidation'] else 'FAIL'} - {'>=5 facts' if results['consolidation'] else f'only {new_facts}'}")
|
||||
|
||||
all_facts = qdrant_scroll("declarative")
|
||||
print(f"\n All declarative facts ({len(all_facts)}):")
|
||||
for i, f in enumerate(all_facts, 1):
|
||||
content = f["payload"]["page_content"]
|
||||
meta = f["payload"].get("metadata", {})
|
||||
source = meta.get("source", "?")
|
||||
ftype = meta.get("fact_type", "?")
|
||||
print(f" {i}. [{source}|{ftype}] {content}")
|
||||
|
||||
# TEST 6: Duplicate Detection
|
||||
section("TEST 6: Duplicate Detection (2nd consolidation)")
|
||||
|
||||
facts_before_2nd = qdrant_count("declarative")
|
||||
print(f" Facts before: {facts_before_2nd}")
|
||||
print(f" Running consolidation again...")
|
||||
|
||||
resp = send_message("consolidate now", timeout=CONSOLIDATION_TIMEOUT)
|
||||
time.sleep(3)
|
||||
|
||||
facts_after_2nd = qdrant_count("declarative")
|
||||
new_dupes = facts_after_2nd - facts_before_2nd
|
||||
print(f" Facts after: {facts_after_2nd} (+{new_dupes})")
|
||||
|
||||
results["dedup"] = new_dupes <= 2
|
||||
print(f" {'PASS' if results['dedup'] else 'FAIL'} - {new_dupes} new facts (<=2 expected)")
|
||||
|
||||
# TEST 7: Fact Recall
|
||||
section("TEST 7: Fact Recall via Natural Language")
|
||||
|
||||
queries = {
|
||||
"What is my name?": ["sarah", "chen"],
|
||||
"How old am I?": ["28"],
|
||||
"Where do I live?": ["seattle"],
|
||||
"Where do I work?": ["microsoft", "software engineer"],
|
||||
"What am I allergic to?": ["peanut"],
|
||||
}
|
||||
|
||||
correct = 0
|
||||
for question, keywords in queries.items():
|
||||
resp = send_message(question)
|
||||
answer = resp.get("content", "")
|
||||
hit = any(kw.lower() in answer.lower() for kw in keywords)
|
||||
if hit:
|
||||
correct += 1
|
||||
icon = "OK" if hit else "??"
|
||||
print(f" {icon} Q: {question}")
|
||||
print(f" A: {answer[:150]}")
|
||||
time.sleep(1)
|
||||
|
||||
print(f"\n✅ Fact recall accuracy: {correct_recalls}/{len(queries)} ({correct_recalls/len(queries)*100:.0f}%)")
|
||||
accuracy = correct / len(queries) * 100
|
||||
results["recall"] = correct >= 3
|
||||
print(f"\n Recall: {correct}/{len(queries)} ({accuracy:.0f}%)")
|
||||
print(f" {'PASS' if results['recall'] else 'FAIL'} (threshold: >=3)")
|
||||
|
||||
# TEST 6: Conversation History Recall
|
||||
print("\n📋 TEST 6: Conversation History (Episodic) Recall")
|
||||
print("-" * 70)
|
||||
# FINAL SUMMARY
|
||||
section("FINAL SUMMARY")
|
||||
|
||||
print("Asking about conversation history...")
|
||||
resp = send_message("What have we talked about today?")
|
||||
summary = resp["content"]
|
||||
print(f"💬 Miku's summary:\n{summary}")
|
||||
total = len(results)
|
||||
passed = sum(1 for v in results.values() if v)
|
||||
print()
|
||||
for name, ok in results.items():
|
||||
print(f" [{'PASS' if ok else 'FAIL'}] {name}")
|
||||
|
||||
# Final Summary
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 FINAL SUMMARY")
|
||||
print("=" * 70)
|
||||
print(f"✅ Episodic memories: {get_qdrant_count('episodic')}")
|
||||
print(f"✅ Declarative facts: {declarative_count}")
|
||||
print(f"✅ Miku's messages stored: {len(miku_messages)}")
|
||||
print(f"✅ Fact recall accuracy: {correct_recalls}/{len(queries)}")
|
||||
print(f"\n Score: {passed}/{total}")
|
||||
print(f" Episodic: {qdrant_count('episodic')}")
|
||||
print(f" Declarative: {qdrant_count('declarative')}")
|
||||
|
||||
# Overall verdict
|
||||
if declarative_count >= 5 and correct_recalls >= 3:
|
||||
print("\n🎉 PIPELINE TEST: PASS")
|
||||
print(" All major components working correctly!")
|
||||
if passed == total:
|
||||
print("\n ALL TESTS PASSED!")
|
||||
elif passed >= total - 1:
|
||||
print("\n MOSTLY PASSING - minor issues only")
|
||||
else:
|
||||
print("\n⚠️ PIPELINE TEST: PARTIAL PASS")
|
||||
print(" Some components may need adjustment")
|
||||
print("\n SOME TESTS FAILED - review above")
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
|
||||
Reference in New Issue
Block a user