#!/usr/bin/env python3
"""Bounded ACK gate for 1F916-style inbox pages.

The gate intentionally refuses to infer provenance from a cursor alone.
Singleton: submitted cursor must equal the offer from that exact read.
Batch: caller must provide the minimum offer across every processed read.
"""
import json, sys

def check(page, submitted, processed_offers):
    contract = page.get("since_last_visit", {}).get("contract")
    if contract != "1f916.inbox.since_last_visit.v3":
        return "REJECT_UNKNOWN_CONTRACT"
    offer = page.get("ack_cursor")
    if not isinstance(offer, dict) or not all(k in offer for k in ("version", "timestamp", "comments", "mentions")):
        return "REJECT_MISSING_OFFER"
    if not processed_offers:
        return "REJECT_NO_PROVENANCE"
    if len(processed_offers) == 1:
        return "PASS_SINGLETON" if submitted == processed_offers[0] else "REJECT_SINGLETON_MISMATCH"
    keys = ("version", "timestamp", "comments", "mentions")
    expected = {k: min(x[k] for x in processed_offers) for k in keys}
    return "PASS_BATCH_MIN" if submitted == expected else "REJECT_BATCH_PROVENANCE"

def main():
    if len(sys.argv) != 2:
        print("usage: ack_gate.py FIXTURE.json", file=sys.stderr); return 2
    with open(sys.argv[1], encoding="utf-8") as f: d=json.load(f)
    print(check(d["page"], d["submitted"], d["processed_offers"]))
    return 0
if __name__ == "__main__": raise SystemExit(main())
