#!/usr/bin/env python3
"""
Prompt isolation KAT.

Proves: Vault-anchored generation ignores prompt content - anchor overrides.
Two prompts with same anchor → identical output.
"""
import os
import sys

os.environ["VAULT_DISABLED"] = "1"

from helix_cdc.cdc_decode import CDCDecoder

def main():
    print("=== Prompt Isolation KAT ===")

    # Test: Two different prompts, same seed → identical output
    # (because anchor is computed from seed, not prompt)
    print("\nTest: Different prompts, same seed → identical output")

    dec1 = CDCDecoder(seed_path=None)
    txt1, tr1 = dec1.generate_from_anchor("Write a sorting algorithm", max_new_tokens=128, temperature=0.2, seed=999)

    dec2 = CDCDecoder(seed_path=None)
    txt2, tr2 = dec2.generate_from_anchor("Explain quantum mechanics", max_new_tokens=128, temperature=0.2, seed=999)

    # Output should be identical (anchor ignores prompt)
    if txt1 == txt2 and tr1["raw_symbols"] == tr2["raw_symbols"]:
        print(f"✅ Prompt isolation: different prompts → same output")
        print(f"   Both produced: {txt1[:50]}...")
        print(f"   Symbols match: {len(tr1['raw_symbols'])} symbols")
    else:
        print(f"❌ Prompt isolation failed")
        print(f"   Prompt 1 output: {txt1[:50]}")
        print(f"   Prompt 2 output: {txt2[:50]}")
        return 1

    # Verify anchors were same
    anchor1 = tr1["tile_stream"][0]["anchor"] if tr1.get("tile_stream") else None
    anchor2 = tr2["tile_stream"][0]["anchor"] if tr2.get("tile_stream") else None

    if anchor1 and anchor2:
        if anchor1 == anchor2:
            print(f"✅ Anchors identical: {anchor1['kind']}:{anchor1['key']}")
        else:
            print(f"❌ Anchors differ (unexpected)")
            return 1

    print("\n✅ PASS: prompts don't affect vault-anchored decode")
    return 0

if __name__ == "__main__":
    sys.exit(main())
