import asyncio
import json
import sys
from simulate_all import PureVectorEmbedder, qdrant_score, run_simulation
from inspect_mem0 import inspect_live_mem0

if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")

async def test_boost_experiment():
    dscons_mems, non_dscons_mems = await inspect_live_mem0()
    embedder = PureVectorEmbedder()
    
    embedded_non_dscons = []
    for m in non_dscons_mems:
        v = embedder.embed(m.get("memory", ""))
        embedded_non_dscons.append((m, v))
        
    query = "AEC pricing norms"
    qv = embedder.embed(query)
    
    # 1. Baseline RULE-AEC-02
    text_base = r"""[DSCons AEC Business: Circular 38 Larsen Pile Norms & Material Cost VL=0] Căn cứ Luật Xây dựng 2025/2026 và Thông tư 38: Công tác Thuê, Ép, Nhổ cừ thép Larsen IV hoặc thuê tôn chống lầy cấu thành 100% từ Chi phí Nhân công (NC) và Chi phí Ca máy (M). Tuyệt đối KHÔNG sinh ra chi phí mua vật liệu thô mới (Bắt buộc VL = 0 VNĐ) trong Bảng Tổng Hợp Nhu Cầu Vật Tư (MR). Nghiêm cấm gán nhầm đơn giá mua cừ mới làm sai lệch dự toán thi công."""
    score_base = qdrant_score(qv, embedder.embed(text_base))
    print(f"Base score: {score_base:.4f}")
    
    # 2. Boosted RULE-AEC-02
    text_boost = r"""[DSCons AEC Business: AEC Pricing Norms | Construction Cost Estimation & BoQ Budgeting | Circular 38 Larsen Pile Norms & Material Cost VL=0 | Định Mức Dự Toán AEC Pricing Norms] Căn cứ Luật Xây dựng 2025/2026 và Thông tư 38 về định mức dự toán xây dựng (AEC pricing norms): Công tác Thuê, Ép, Nhổ cừ thép Larsen IV hoặc thuê tôn chống lầy cấu thành 100% từ Chi phí Nhân công (NC) và Chi phí Ca máy (M). Tuyệt đối KHÔNG sinh ra chi phí mua vật liệu thô mới (Bắt buộc VL = 0 VNĐ) trong Bảng Tổng Hợp Nhu Cầu Vật Tư (MR). Nghiêm cấm gán nhầm đơn giá mua cừ mới làm sai lệch dự toán thi công."""
    score_boost = qdrant_score(qv, embedder.embed(text_boost))
    print(f"Boosted score: {score_boost:.4f}")
    
    # Check top hits among non-dscons
    scored = [(qdrant_score(qv, mv), m.get("memory", "")[:60]) for m, mv in embedded_non_dscons]
    scored.sort(key=lambda x: x[0], reverse=True)
    print("Top non-dscons hit:", scored[0])

if __name__ == "__main__":
    asyncio.run(test_boost_experiment())
