#!/usr/bin/env python3
"""
Synapse Layer Cookbook — 06: Trust Quotient: Filter Noise, Keep Signal
https://synapselayer.org/docs

Demonstrates why Trust Quotient matters:
- Store 10 memories of varying quality
- Recall WITHOUT filtering → returns everything (including noise)
- Recall WITH min_tq=0.7 → returns only high-confidence memories

This is the argument for Synapse over Mem0, Zep, and LangMem:
without Trust Quotient, your agent drowns in noise.

Prerequisites:
    pip install synapse-layer
    export SYNAPSE_API_KEY=sk_live_...
"""

from synapse_layer import SynapseClient
import os

client = SynapseClient(api_key=os.environ["SYNAPSE_API_KEY"])

# --- Store 10 memories of varying quality ---
memories_to_store = [
    # High-quality, specific memories
    {"content": "User is VP of Engineering at DataFlow Inc. Reports to CTO.",
     "memory_type": "profile", "tags": ["role", "company"]},
    {"content": "User's team has 12 engineers focused on real-time data pipelines.",
     "memory_type": "context", "tags": ["team", "focus"]},
    {"content": "User requires sub-100ms latency for all API calls. Non-negotiable.",
     "memory_type": "requirement", "tags": ["performance", "sla"]},
    {"content": "User's budget: $50K/quarter for infrastructure tools.",
     "memory_type": "context", "tags": ["budget"]},
    {"content": "User prefers Terraform for IaC. Has 200+ modules in production.",
     "memory_type": "preference", "tags": ["tools", "iac"]},
    # Medium-quality, somewhat vague
    {"content": "User mentioned something about a deadline.",
     "memory_type": "context", "tags": ["vague"]},
    {"content": "User might be interested in the enterprise plan.",
     "memory_type": "context", "tags": ["maybe"]},
    # Low-quality, noise
    {"content": "ok",
     "memory_type": "context", "tags": ["noise"]},
    {"content": "User said thanks.",
     "memory_type": "context", "tags": ["noise"]},
    {"content": "test test test",
     "memory_type": "context", "tags": ["noise"]},
]

print("=== Storing 10 memories of varying quality ===\n")
for m in memories_to_store:
    stored = client.store(
        content=m["content"],
        agent="demo-agent",
        memory_type=m["memory_type"],
        tags=m["tags"],
    )
    quality = (
        "HIGH" if stored.trust_quotient >= 0.7
        else "MED" if stored.trust_quotient >= 0.4
        else "LOW"
    )
    print(f"  [{quality}] TQ:{stored.trust_quotient:.2f} — {m['content'][:60]}")

# --- Recall WITHOUT Trust Quotient filter ---
print("\n=== Recall WITHOUT TQ filter (min_tq=0.0) ===")
all_results = client.recall(
    query="Tell me about the user and their requirements.",
    limit=10,
    min_tq=0.0,
)
print(f"Results: {len(all_results)} memories")
for m in all_results:
    print(f"  [TQ:{m.trust_quotient:.2f}] {m.content[:70]}")

# --- Recall WITH Trust Quotient filter ---
print("\n=== Recall WITH TQ filter (min_tq=0.7) ===")
filtered_results = client.recall(
    query="Tell me about the user and their requirements.",
    limit=10,
    min_tq=0.7,
)
print(f"Results: {len(filtered_results)} memories (noise filtered out)")
for m in filtered_results:
    print(f"  [TQ:{m.trust_quotient:.2f}] {m.content[:70]}")

# --- The difference ---
print(f"\n=== IMPACT ===")
print(f"Without TQ: {len(all_results)} results (includes noise)")
print(f"With TQ>=0.7: {len(filtered_results)} results (signal only)")
print(f"Noise reduction: {len(all_results) - len(filtered_results)} memories filtered")
print(f"\nYour agent makes better decisions with less context.")
print(f"Fewer tokens in, higher accuracy out.")


"""
Expected output:
    === Storing 10 memories of varying quality ===

      [HIGH] TQ:0.93 — User is VP of Engineering at DataFlow Inc. Reports to CTO.
      [HIGH] TQ:0.91 — User's team has 12 engineers focused on real-time data pi
      [HIGH] TQ:0.95 — User requires sub-100ms latency for all API calls. Non-ne
      [HIGH] TQ:0.90 — User's budget: $50K/quarter for infrastructure tools.
      [HIGH] TQ:0.88 — User prefers Terraform for IaC. Has 200+ modules in produ
      [MED]  TQ:0.52 — User mentioned something about a deadline.
      [MED]  TQ:0.48 — User might be interested in the enterprise plan.
      [LOW]  TQ:0.12 — ok
      [LOW]  TQ:0.18 — User said thanks.
      [LOW]  TQ:0.08 — test test test

    === Recall WITHOUT TQ filter (min_tq=0.0) ===
    Results: 10 memories
      [TQ:0.93] User is VP of Engineering at DataFlow Inc. Reports to CTO.
      [TQ:0.95] User requires sub-100ms latency for all API calls. Non-negotiable.
      ... (all 10, including noise)

    === Recall WITH TQ filter (min_tq=0.7) ===
    Results: 5 memories (noise filtered out)
      [TQ:0.93] User is VP of Engineering at DataFlow Inc. Reports to CTO.
      [TQ:0.95] User requires sub-100ms latency for all API calls. Non-negotiable.
      [TQ:0.91] User's team has 12 engineers focused on real-time data pipelines.
      [TQ:0.90] User's budget: $50K/quarter for infrastructure tools.
      [TQ:0.88] User prefers Terraform for IaC. Has 200+ modules in production.

    === IMPACT ===
    Without TQ: 10 results (includes noise)
    With TQ>=0.7: 5 results (signal only)
    Noise reduction: 5 memories filtered

    Your agent makes better decisions with less context.
    Fewer tokens in, higher accuracy out.
"""
