"""Opt-in audit manifest 1.1 scope composition; development defaults unfrozen. Version 1.0 input schemas remain owned by the existing audit implementation. """ import b1_contract import initial_contract import b2_contract import b3_contract observation = b1_contract.observation SCOPES = {"initial-only": {"initial"}, "continuation-only": {"continuation"}, "combined": {"initial", "continuation"}} MATCHED = "matched-treatment/1.0" def specifications(row, run, identity_fields): observation.keys(row, (*identity_fields, "archive_id", "seal_record_id", "audit_scope", "request_slots", "initial_input", "continuation_input", "packets"), "scoped assigned run") scope = row["audit_scope"] observation.require(isinstance(scope, str) and scope in SCOPES, "unsupported audit scope") specs = {} for stage in ("initial", "continuation"): value = row[stage+"_input"] if stage not in SCOPES[scope]: observation.require(value is None, "out-of-scope input declaration must be null") continue allowed = (initial_contract.CONTRACT,) if stage == "initial" else (MATCHED, b1_contract.CONTRACT, b2_contract.CONTRACT) observation.require(isinstance(value, dict) and value.get("contract") in allowed, "unsupported permitted-input contract for "+stage) fields = ("contract", "packet_inventory_complete") observation.keys(value, (*fields, "pairs") if value["contract"] == b2_contract.CONTRACT else fields, stage+" input declaration") observation.require(type(value["packet_inventory_complete"]) is bool, stage+" completeness must be Boolean") specs[stage] = dict(value) needs_worker_slots = "initial" in specs or specs["continuation"]["contract"] in (b1_contract.CONTRACT, b2_contract.CONTRACT) if needs_worker_slots: initials = initial_contract.initial_attempts(run, row["request_slots"]) else: observation.require(row["request_slots"] is None, "matched continuation-only scope requires null worker request_slots") initials = {} if "initial" in specs: specs["initial"]["attempts"] = initials if "continuation" in specs: if specs["continuation"]["contract"] == b2_contract.CONTRACT: specs["continuation"]["attempts"] = b2_contract.designated_attempts(run, specs["continuation"], row["request_slots"]) specs["continuation"]["roles"] = b2_contract.request_roles(specs["continuation"]) else: specs["continuation"]["attempts"] = (b1_contract.continuation_attempts(run, row["request_slots"]) if specs["continuation"]["contract"] == b1_contract.CONTRACT else {a["attempt_id"]: a for a in run["attempts"] if a["phase"] == "adaptation" and a["role"] == "planner"}) return specs def packet_kinds(binding): result = {} for run in binding["runs"]: if run.get("audit_scope") == "b3-native": stages = b3_contract.attempt_map(run) result.update({p["packet_id"]: "b3_"+stages[p["attempt_id"]]+"_input" for p in run["packets"]}) continue for packet in run["packets"]: contract = (run[packet["input_scope"]+"_input"]["contract"] if "audit_scope" in run else run["contract"]) if contract == b2_contract.CONTRACT: result[packet["packet_id"]] = "b2_"+b2_contract.request_roles(run["continuation_input"])[packet["attempt_id"]]+"_input" else: result[packet["packet_id"]] = ("common_initial_input" if contract == initial_contract.CONTRACT else "b1_continuation_input" if contract == b1_contract.CONTRACT else "planner_input") return result def coverage(run, judgment): """Coverage only: no per-stage pass inferred from a run-level violation flag.""" reviewed = set() if judgment is None else set(judgment["reviewed_packet_ids"]) result = {} for stage in ("initial", "continuation"): declaration = run[stage+"_input"] selected = [p for p in run["packets"] if p["input_scope"] == stage] available = {p["packet_id"] for p in selected if p["record_id"] is not None} missing = len(selected)-len(available) result[stage] = dict(in_scope=declaration is not None, contract=None if declaration is None else declaration["contract"], declared_attempt_count=len(selected), missing_packet_count=missing, available_packet_count=len(available), reviewed_packet_count=len(available & reviewed), available_unreviewed_packet_count=len(available-reviewed), packet_inventory_complete=None if declaration is None else declaration["packet_inventory_complete"], judgment_available=judgment is not None and judgment["violation"] is not None, review_coverage_complete=(declaration is not None and declaration["packet_inventory_complete"] and missing == 0 and available <= reviewed and judgment is not None and judgment["violation"] is not None)) if stage == "continuation" and "b2_pair_coverage" in run: result[stage]["review_coverage_complete"] &= run["b2_pair_coverage"]["evidence_complete"] return result def group_coverage(rows): result = {} for stage in ("initial", "continuation"): coverage = [r["input_scope_coverage"][stage] for r in rows] included = [r for r in coverage if r["in_scope"]] attempts = sum(r["declared_attempt_count"] for r in included) available = sum(r["available_packet_count"] for r in included) reviewed = sum(r["reviewed_packet_count"] for r in included) result[stage] = dict(assigned_run_denominator=len(rows), in_scope_run_count=len(included), out_of_scope_run_count=len(rows)-len(included), complete_review_run_count=sum(r["review_coverage_complete"] for r in included), incomplete_inventory_run_count=sum(not r["packet_inventory_complete"] for r in included), missing_run_judgment_count=sum(not r["judgment_available"] for r in included), declared_attempt_denominator=attempts, available_packet_count=available, reviewed_packet_count=reviewed, missing_packet_count=attempts-available, available_unreviewed_packet_count=available-reviewed, packet_availability_fraction=None if attempts == 0 else available/attempts, packet_review_fraction=None if attempts == 0 else reviewed/attempts) return result