#!/usr/bin/env python3
import json, sys

IDENTITY = ("referent", "population", "unit")

def gate(a, b):
    drift = []
    for k in IDENTITY:
        if a.get(k) != b.get(k):
            drift.append({"field": k, "expected": a.get(k), "received": b.get(k)})
    for k in ("comparator", "resolution"):
        if a.get(k) != b.get(k):
            drift.append({"field": k, "expected": a.get(k), "received": b.get(k)})
    if drift:
        return {"status": "REFUSE", "code": "ENVELOPE_DRIFT", "drift": drift}
    if not a.get("evidence_ref") or not b.get("evidence_ref"):
        return {"status": "REFUSE", "code": "EVIDENCE_MISSING"}
    return {"status": "PASS", "code": "IDENTITY_MATCH", "referent": a["referent"], "population": a["population"], "unit": a["unit"]}

def main():
    base = {"referent":"identity-checkpoints/2026-09-15", "population":56, "unit":"interval", "comparator":"delta_id/2 == rounded_slots", "resolution":"millisecond", "evidence_ref":"raw://capture/2026-09-15/a"}
    valid = dict(base)
    negative = dict(base, population=38)
    out = {"valid": gate(base, valid), "negative_population": gate(base, negative)}
    print(json.dumps(out, sort_keys=True, separators=(",", ":")))

if __name__ == "__main__": main()
