# Reproduce the synthetic teaching calculation

These are invented requests and labels, not customer data, a vendor benchmark or a simulation of either vendor classifier.

Download `support-cohort.json` from the same dossier. Save the Python block below as `recalculate-support.py` in the same directory. Python 3.10 or later and its standard library are sufficient. From that directory run:

```sh
python3 recalculate-support.py support-cohort.json --self-check
```

The output reports counts, denominators and nine discriminating checks. The local runtime version and hashes are in `calculation-receipt.json`. The seven-day follow-up starts at the first AI answer; one late answer is still in observation. The 24-hour handoff deadline is a teaching choice, not a universal threshold or a vendor default. Accepted handoff is not human resolution. Initial request IDs are the counting unit; subsequent conversations remain linked events. Unknown audit evidence and incomplete follow-up remain distinct.

```python
"""Teaching arithmetic on explicitly synthetic support requests; no vendor simulation."""
import argparse
import copy
import hashlib
import json
from datetime import datetime, timedelta
from pathlib import Path


def timestamp(value):
    return datetime.fromisoformat(value.replace("Z", "+00:00"))


def calculate(data):
    start, end, freeze = (timestamp(data[k]) for k in ("intake_start", "intake_end", "freeze"))
    window = timedelta(days=data["followup_days"])
    cases = [x for x in data["requests"] if start <= timestamp(x["started_at"]) < end]
    ids = [x["request_id"] for x in cases]
    if len(ids) != len(set(ids)):
        raise ValueError("Duplicate initial request IDs")
    opportunity = [x for x in cases if x["ai_opportunity"]]
    labels = [x for x in opportunity if x["generic_resolved_like"]]
    recurrent, verified, unknown, pending, known_non_success = [], [], [], [], []
    for x in opportunity:
        anchor = timestamp(x["followup_anchor"])
        complete = freeze >= anchor + window
        returns = [e for e in x["return_events"] if e["same_issue_still_open"] and anchor <= timestamp(e["at"]) <= min(freeze, anchor + window)]
        if returns and x["generic_resolved_like"]:
            recurrent.append(x["request_id"])
        if not complete:
            pending.append(x["request_id"])
        elif not x["identity_and_issue_observable"] or x["audit_result"] == "unknown":
            unknown.append(x["request_id"])
        elif x["audit_result"] == "met" and not x["human_needed"] and not returns:
            verified.append(x["request_id"])
        else:
            known_non_success.append(x["request_id"])
    required = [x for x in cases if x["handoff_required"]]
    accepted = [x for x in required if x["handoff_correct_team"] and x["handoff_accepted_at"] and timestamp(x["handoff_requested_at"]) <= timestamp(x["handoff_accepted_at"]) <= min(freeze, timestamp(x["handoff_requested_at"]) + timedelta(hours=data["handoff_deadline_hours"]))]
    n, e, r, g, u, p, k = map(len, (cases, opportunity, labels, verified, unknown, pending, recurrent))
    ratio = lambda numerator, denominator: round(100 * numerator / denominator, 6) if denominator else None
    return {
        "N": n, "E": e, "R": r, "G": g, "unknown_evidence": u,
        "pending_followup": p, "U_including_pending": u + p,
        "known_non_autonomous": len(known_non_success), "K_unique_requests": k,
        "coverage_pct": ratio(e, n), "generic_label_among_opportunities_pct": ratio(r, e),
        "generic_label_all_requests_pct": ratio(r, n), "verified_autonomous_all_requests_pct": ratio(g, n),
        "unknown_or_pending_all_requests_pct": ratio(u + p, n),
        "same_issue_return_among_initial_labels_pct": ratio(k, r),
        "required_handoffs": len(required), "accepted_correct_handoffs": len(accepted),
        "accepted_correct_handoff_pct": ratio(len(accepted), len(required)),
        "request_sets": {"verified": verified, "unknown": unknown, "pending": pending, "returns": recurrent},
    }


def self_check(data):
    original = calculate(data)
    assert tuple(original[k] for k in ("N", "E", "R", "G", "U_including_pending", "K_unique_requests")) == (100, 60, 40, 28, 5, 10)
    assert original["unknown_evidence"] == 4 and original["pending_followup"] == 1
    assert (original["required_handoffs"], original["accepted_correct_handoffs"]) == (15, 12)
    reversed_handoff = copy.deepcopy(data)
    reversed_handoff["requests"][40]["handoff_accepted_at"] = "2026-10-01T10:00:00Z"
    assert calculate(reversed_handoff)["accepted_correct_handoffs"] == 11
    silent_success = next(x for x in data["requests"] if x["request_id"] == "C015")
    confirmed_return = next(x for x in data["requests"] if x["request_id"] == "C029")
    assert not silent_success["customer_confirmation"] and "C015" in original["request_sets"]["verified"]
    assert confirmed_return["customer_confirmation"] and "C029" not in original["request_sets"]["verified"]
    narrower = copy.deepcopy(data)
    for x in narrower["requests"]:
        if 41 <= int(x["request_id"][1:]) <= 50:
            x["ai_opportunity"] = False
    changed = calculate(narrower)
    assert changed["generic_label_among_opportunities_pct"] == 80
    assert changed["verified_autonomous_all_requests_pct"] == original["verified_autonomous_all_requests_pct"] == 28
    duplicate = copy.deepcopy(data)
    duplicate["requests"][28]["return_events"] *= 2
    assert calculate(duplicate)["K_unique_requests"] == original["K_unique_requests"]
    lost = copy.deepcopy(data)
    lost["requests"][0]["audit_result"] = "unknown"
    check = calculate(lost)
    assert check["R"] == 40 and check["G"] == 27 and check["U_including_pending"] == 6
    late = next(x for x in data["requests"] if x["request_id"] == "C060")
    assert timestamp(late["started_at"]) < timestamp(data["intake_end"]) < timestamp(late["followup_anchor"])
    assert "C060" in original["request_sets"]["pending"]
    empty = copy.deepcopy(data)
    for x in empty["requests"]:
        x["ai_opportunity"] = False
    assert calculate(empty)["generic_label_among_opportunities_pct"] is None
    return ["baseline manual counts", "handoff cannot be accepted before it is requested", "silence with audit evidence can be success", "confirmation with unresolved return is not verified success", "scope change 66.7 to 80 with verified share unchanged", "repeat-event deduplication", "audit evidence lost remains unknown", "late answer preserves incomplete follow-up", "zero denominator is undefined"]


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("dataset", type=Path)
    parser.add_argument("--self-check", action="store_true")
    args = parser.parse_args()
    raw = args.dataset.read_bytes()
    data = json.loads(raw)
    result = {"scope": "Synthetic teaching arithmetic, not observed performance or a vendor dashboard", "dataset_sha256": hashlib.sha256(raw).hexdigest(), "metrics": calculate(data)}
    if args.self_check:
        result["checks_passed"] = self_check(data)
    print(json.dumps(result, ensure_ascii=False, indent=2))
```
