"""Validate copy/paste facts from externally authenticated semantic events. This module does not record events, authenticate agent-written files, or infer operations from matching final values. The verifier/harness supplies a trust callback that authenticates the entire event envelope outside the submission. """ from collections.abc import Callable, Mapping def evaluate_copy(envelope: Mapping, *, authenticate: Callable | None = None, source_id: str | None = None, target_id: str | None = None, require_final_effective: bool = True) -> dict: base = {"status": "needs_review", "evidence": [], "copied": None} if authenticate is None: return {**base, "reason": "No external collector authentication is available"} try: trusted = authenticate(envelope) except Exception as exc: return {**base, "status": "verifier_error", "reason": type(exc).__name__} if trusted is not True: return {**base, "reason": "Envelope did not authenticate; candidate-written events are not authoritative"} events = envelope.get("events") session = envelope.get("session_id") if not isinstance(events, list) or not session: return {**base, "status": "verifier_error", "reason": "Malformed authenticated event envelope"} seqs = [e.get("sequence") for e in events if isinstance(e, dict)] if len(seqs) != len(events) or any(type(s) is not int for s in seqs) or seqs != sorted(set(seqs)): return {**base, "status": "verifier_error", "reason": "Events require unique increasing integer sequences"} if any(e.get("session_id") != session or e.get("actor") != "agent-session" for e in events): return {**base, "status": "verifier_error", "reason": "Session/actor mismatch in authenticated events"} by_seq = {e["sequence"]: e for e in events} commit_events = {e.get("transaction_id"):e["sequence"] for e in events if e.get("event") == "transaction.commit" and e.get("result") == "succeeded"} commits=set(commit_events) effective = {tx: True for tx in commits} for event in events: if event.get("event") in ("transaction.undo", "transaction.redo") and event.get("result") == "succeeded": effective[event.get("target_transaction_id")] = event["event"] == "transaction.redo" facts = [] for event in events: if event.get("event") != "property.paste" or event.get("result") != "succeeded": continue copy = by_seq.get(event.get("clipboard_event_sequence"), {}) tx = event.get("transaction_id") if (copy.get("event") != "property.copy" or copy.get("result") != "succeeded" or copy.get("sequence", event["sequence"]) >= event["sequence"] or tx not in commits or commit_events.get(tx,-1)<=event["sequence"]): continue if source_id is not None and copy.get("source_property_id") != source_id: continue if target_id is not None and event.get("target_property_id") != target_id: continue if event.get("source_property_id") != copy.get("source_property_id"): continue if not copy.get("payload_hash") or event.get("payload_hash") != copy["payload_hash"]: continue if require_final_effective and not effective.get(tx, False): continue facts.append({"copy_sequence": copy["sequence"], "paste_sequence": event["sequence"], "transaction_id": tx, "final_effective": effective.get(tx, False)}) return {"status": "pass" if facts else "fail", "copied": bool(facts), "evidence": facts, "reason": "Authenticated committed copy/paste" if facts else "No matching successful committed copy/paste"}