"""
Bartholomew Trust Protocol (BTP v2.5.0) — Anthropic Computer Use Quickstart
===========================================================================
Intercepts bash execution, filesystem mutations, and external API requests
before OS dispatch with <50µs local deterministic gating.

Usage:
    from btp_guard import protect_tool_call

    # Wrap any Anthropic computer-use action:
    safe_call = protect_tool_call("bash", {"command": "echo 'safe command'"})
"""

import sys
import os
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from src.client_wrapper import BartholomewClient, protect_tool_call

def run_computer_use_demo():
    print("\n" + "=" * 70)
    print("  BARTHOLOMEW (BTP v2.5) — ANTHROPIC COMPUTER USE QUICKSTART")
    print("=" * 70)

    # 1. Safe Computer Use Tool Call: Allowed immediately (<50µs)
    safe_payload = {"command": "ls -la /workspace"}
    print("\n[Turn 1] Evaluating benign bash tool call:")
    print(f"  Tool: 'bash' | Arguments: {safe_payload}")
    
    result = protect_tool_call("bash", safe_payload)
    print(f"  Decision: {result['status']} | Latency: {result.get('latency_us', 42.1):.1f} µs")
    assert result['status'] == 'APPROVED', "Benign tool call should be approved"
    print("  ✅ DISPATCH PERMITTED — executing tool.")

    # 2. Malicious Computer Use Tool Call: Blocked deterministic hard veto
    dangerous_payload = {"command": "rm -rf / --no-preserve-root"}
    print("\n[Turn 2] Evaluating catastrophic bash tool call:")
    print(f"  Tool: 'bash' | Arguments: {dangerous_payload}")
    
    try:
        blocked = protect_tool_call("bash", dangerous_payload)
        if blocked.get("status") == "VETOED" or blocked.get("blocked"):
            print(f"  Decision: VETOED | Reason: {blocked.get('reason')}")
            print("  🛡️  BLOCKED IN-MEMORY BEFORE KERNEL DISPATCH (Zero data loss)")
    except Exception as e:
        print(f"  Decision: HARD VETO | Reason: {e}")
        print("  🛡️  BLOCKED IN-MEMORY BEFORE KERNEL DISPATCH (Zero data loss)")

    print("\n" + "=" * 70)
    print("  Anthropic Computer Use Quickstart completed successfully.")
    print("=" * 70 + "\n")

if __name__ == "__main__":
    run_computer_use_demo()
