#!/usr/bin/env python3
# Python mirror of control-plane/src/verify/split-rule.ts (SPLIT_RULE_VERSION = 1).
#
# Trainer Charter §7 makes split_ratios and split_seed SERVER-MINTED: ONE labelled archive is
# uploaded and the SERVER decides which images train the head and which grade it. This file lets
# the head and scorer DERIVE that split from the seed and assert it matches the declaration --
# so no list of 100,000 filenames travels, and the check is ABLE to fail.
#
# THE RULE:
#   1. label(name) is the component before the final '/' -- parts[-2], what the containers use.
#   2. key(name) = sha256(decimal_seed_ascii + 0x00 + stored_name_bytes).
#   3. WITHIN EACH LABEL GROUP, sort by (key bytes, stored name bytes).
#   4. n_test = n * test_bps // 10000 taken FIRST, then n_val, remainder train.
#   5. Refuse a class that yields no test image.

import hashlib
import json
import sys

SPLIT_RULE_VERSION = 1
BPS = 10000


class SplitRefusal(Exception):
    def __init__(self, reason, detail):
        super().__init__("%s -- %s" % (reason, detail))
        self.reason = reason
        self.detail = detail


# THE LABEL RULE THIS RULE STRATIFIES BY, published so the containers can refuse a job that
# declares a different one (Finding 96). label_of below takes NO rule parameter and neither
# does the server's labelOfName: both are the parent folder. run_head accepted 'top' as well,
# and could not honour it -- a job declaring 'top' trains on top-level labels while every
# per-class guarantee here, class_has_no_test_image above all, was computed over parent-folder
# classes, and the plan's class count would disagree with the head's n_classes.
#
# Supporting another rule means giving label_of one, which changes the GROUPING and therefore
# the assignment and all three digests. That is a SPLIT_RULE_VERSION bump and a product
# decision, not a branch in a container.
LABEL_RULE = "parent"


def label_of(name):
    """'train/cats/a.jpg' -> b'cats'. A trailing-slash directory entry never reaches here:
    is_image() in slice_rule.py already excluded it."""
    if not isinstance(name, bytes):
        raise TypeError("label_of takes stored name BYTES, got %r" % type(name))
    parts = name.split(b"/")
    if len(parts) < 2 or parts[-2] == b"":
        raise SplitRefusal("unlabelled_image",
                           "%r has no parent folder, so it carries no label" % name[:80])
    return parts[-2]


def seed_bytes(seed):
    """Integer only. '7', 7.0 and 7 would hash three different ways, so no coercion."""
    if isinstance(seed, bool) or not isinstance(seed, int):
        raise SplitRefusal("bad_seed", "split seed must be an integer, got %r" % type(seed))
    return str(seed).encode("ascii")


def key_of(sb, name):
    """The NUL stops the seed's digits running into a name that begins with a digit."""
    h = hashlib.sha256()
    h.update(sb)
    h.update(b"\x00")
    h.update(name)
    return h.digest()


def counts(n, test_bps, val_bps):
    """Integer basis points, TEST FIRST. Returns (n_test, n_val, n_train)."""
    if isinstance(test_bps, bool) or isinstance(val_bps, bool) \
            or not isinstance(test_bps, int) or not isinstance(val_bps, int):
        raise SplitRefusal("bad_ratios", "ratios must be integer basis points")
    # FINDING 93. val_bps >= 1, not >= 0. A val-less split was minted happily while val was
    # a set nothing consumed; now it carries lambda selection, and run_score refuses it with
    # no_val_rows -- AFTER extraction and head training have been paid for. The refusal
    # belongs at admission, where it costs nothing.
    #
    # This does NOT bump SPLIT_RULE_VERSION, deliberately: it only NARROWS what is
    # accepted. Every declaration that was valid before produces byte-identical digests,
    # so no existing artifact becomes unreproducible. run_score keeps its no_val_rows guard
    # as defence in depth, for a job that did not come from here.
    if test_bps < 1 or val_bps < 1:
        raise SplitRefusal("bad_ratios",
                           "test_bps and val_bps must both be >= 1, got %r/%r"
                           % (test_bps, val_bps))
    if test_bps + val_bps >= BPS:
        raise SplitRefusal("bad_ratios",
                           "test_bps + val_bps must leave a training remainder, got %d + %d"
                           % (test_bps, val_bps))
    n_test = (n * test_bps) // BPS
    n_val = (n * val_bps) // BPS
    return n_test, n_val, n - n_test - n_val


def assign(names, seed, test_bps, val_bps):
    """names: an iterable of STORED NAME BYTES, already image-filtered by slice_rule.is_image.
    Returns each split BYTE-WISE SORTED, which is the canonical order the digests cover."""
    sb = seed_bytes(seed)
    groups = {}
    for n in names:
        groups.setdefault(label_of(n), []).append(n)

    out = {"train": [], "val": [], "test": []}
    for lbl in sorted(groups):
        members = groups[lbl]
        if len(set(members)) != len(members):
            raise SplitRefusal("duplicate_entry",
                               "label %r holds a repeated member name" % lbl[:60])
        # THE KEY IS COMPARED AS BYTES. Comparing all 32 means the truncation width never has
        # to be argued about; the name is the tiebreak so the order is total regardless.
        members.sort(key=lambda m: (key_of(sb, m), m))
        n_test, n_val, n_train = counts(len(members), test_bps, val_bps)
        if n_test == 0:
            raise SplitRefusal(
                "class_has_no_test_image",
                "label %r holds %d image(s); at %d bps that is no test image, so its recall "
                "would be undefined rather than low" % (lbl[:60], len(members), test_bps))
        if n_train == 0:
            # UNREACHABLE while counts() refuses test_bps + val_bps >= BPS. Proven by
            # enumeration in the cross-language suite, kept as ADR-0064 second-line defence.
            raise SplitRefusal(
                "class_has_no_training_image",
                "label %r holds %d image(s) and none would remain to learn it from"
                % (lbl[:60], len(members)))
        # TEST FIRST: test membership depends on (seed, test_bps) alone, so changing k_folds or
        # val_bps cannot move it. Charter §4b made structural instead of remembered.
        out["test"].extend(members[:n_test])
        out["val"].extend(members[n_test:n_test + n_val])
        out["train"].extend(members[n_test + n_val:])

    # FINDING 93, GLOBALLY AND NOT PER CLASS. The first version of this refused any class with
    # no val image -- which at the default 1000 bps rejects a class of 9, while the TEST gate at
    # 2000 bps accepts a class of 5. That makes val STRICTER than test, which is backwards: val
    # decides which lambda is chosen, test decides the number reported. Refusing a customer
    # archive because one class holds 7 images is not a correctness win, and it broke eight
    # existing plan tests that were right.
    #
    # What genuinely cannot work is an EMPTY val set: lambda cannot be chosen at all, and
    # run_score refuses with no_val_rows after the GPU time is spent. val_bps >= 1 does not
    # prevent it -- 40 members at 1 bps is floor(40/10000) = 0 -- so the count is checked here,
    # once, over the whole assignment. A LOPSIDED val set stays legal and stays visible: the
    # artifact carries per_head_val[].confusion, so a reader can see which classes it covered.
    if not out["val"]:
        raise SplitRefusal(
            "empty_val_set",
            "%d image(s) at %d val bps yields no validation image at all, so lambda could "
            "only be chosen on the graded rows" % (sum(len(v) for v in groups.values()),
                                                  val_bps))
    for k in out:
        out[k].sort()  # membership is a SET; the shuffle is only how it was selected
    return out


def split_digest(names):
    """Identical canon to slice_rule.slice_digest. The empty set is 9a271f2a916b0b6e..., the
    constant the slice suite already pins -- which is what proves the two share one canon."""
    b = [str(len(names)).encode("ascii"), b"\n"]
    for n in names:
        b.append(n if isinstance(n, bytes) else n.encode("utf-8"))
        b.append(b"\x00")
    return hashlib.sha256(b"".join(b)).hexdigest()


def declare(names, seed, test_bps, val_bps):
    a = assign(names, seed, test_bps, val_bps)
    return {"rule_version": SPLIT_RULE_VERSION, "seed": seed,
            "test_bps": test_bps, "val_bps": val_bps,
            "n_train": len(a["train"]), "n_val": len(a["val"]), "n_test": len(a["test"]),
            "digest_train": split_digest(a["train"]),
            "digest_val": split_digest(a["val"]),
            "digest_test": split_digest(a["test"])}


def select(names, split):
    """The VERIFIER half. The server DECLARES a split; the container re-derives it and proves
    it reconstructs the same three sets, byte for byte, before a single row is used.

    The container never receives a membership list -- only a seed, two ratios and three
    digests -- so a truncated, reordered or tampered declaration cannot survive this, and a
    container whose copy of the rule has drifted from the server's finds out HERE rather than
    by training on the wrong rows and returning a plausible answer.

    It also pins the INPUT SET as a side effect worth naming: hand this a subset of the
    archive, or one extra member, and `assign` produces different sets whose digests cannot
    match. Completeness is checked by the same arithmetic that checks membership.

    NO COUNT CHECK, deliberately. split_digest hashes str(len(names)) first, so a matching
    digest already implies a matching count; a guard that could only fire once the digest
    check had is decoration (run_score.py draws the same distinction about n_test_slices).
    """
    if not isinstance(split, dict):
        raise SplitRefusal("bad_split",
                           "params_json.split must be an object, got %r" % type(split))
    got_version = split.get("rule_version")
    if got_version != SPLIT_RULE_VERSION:
        raise SplitRefusal("split_rule_version",
                           "the declaration is rule_version %r; this image implements %d"
                           % (got_version, SPLIT_RULE_VERSION))
    for k in ("seed", "test_bps", "val_bps"):
        v = split.get(k)
        if isinstance(v, bool) or not isinstance(v, int):
            raise SplitRefusal("bad_split",
                               "split.%s must be an integer, got %r" % (k, v))
    a = assign(names, split["seed"], split["test_bps"], split["val_bps"])
    for k in ("train", "val", "test"):
        want = split.get("digest_" + k)
        got = split_digest(a[k])
        if want != got:
            raise SplitRefusal("split_digest_mismatch",
                               "the %s set re-derives to %s, the declaration says %r"
                               % (k, got, want))
    return a


if __name__ == "__main__":
    req = json.load(sys.stdin)
    names = [n.encode("utf-8") for n in req["names"]]
    try:
        if req.get("op") == "select":
            a = select(names, req["split"])
            d = {"ok": True, "n_train": len(a["train"]),
                 "n_val": len(a["val"]), "n_test": len(a["test"])}
        else:
            d = declare(names, req["seed"], req["test_bps"], req["val_bps"])
    except SplitRefusal as e:
        d = {"refused": e.reason}
    print(json.dumps(d, ensure_ascii=True, sort_keys=True, separators=(",", ":")))
