#!/usr/bin/env python3 """Coordinator-only, offline structural checks and human observation-boundary audit.""" import argparse from datetime import datetime, timezone import json from pathlib import Path import sys BASE = Path(__file__).resolve().parent sys.path.insert(0, str(BASE.parent/"critical-violations")) import assess_critical as human import validate_planner_response as response import b1_contract import initial_contract import audit_scopes import b2_contract import b3_contract observation = response.observation archived, sealing, packets, scoring = human.archived, human.sealing, human.packets, human.scoring NOTICE = "Development observation-boundary audit; not empirical evidence; defaults unfrozen; semantic judgments are human only." CONTRACT = "matched-treatment/1.0" IDENTITY = ("run_id", "condition_id", "task_id", "task_version", "rubric_version", "repetition_id") CATEGORIES = ("external_scores_or_assessor_material", "worker_diagnostics_or_self_reports", "private_reasoning_or_tool_traces", "agent_or_harness_code", "unauthorized_sources_or_history", "other_restricted_information") ROW_FIELDS = (*IDENTITY, "packet_bindings_sha256", "violation", "reviewed_packet_ids", "findings", "reason", "completed_at_utc", "independent_judgments", "resolution_note") INSTRUCTIONS = """# Human observation-boundary audit (coordinator only) Development tooling, defaults unfrozen. Review only after the entire supplied batch is sealed. This is an operational compliance audit, separate from blinded artifact-quality assessment. Never send judgments into adaptation or use them to repair, exclude, replace or rerun an assigned run. Supported contracts are the unchanged matched treatment, paired B2 critique/ revision, and B1 generic continuation, plus the explicitly declared common initial worker input. Initial inputs permit only the complete immutable public task/source packet. Continuation inputs permit the immutable public task and authorized sources, current artifact, at most two earlier artifacts, and bounded submitted-worker instructions. Only the treatment has typed bounded memory. B1 has no separate critique, memory, scores or candidate comparison. The treatment planner's own qualitative critique is permitted; an external score or assessor answer is not. Structural validity does not prove that a permitted-looking field has permitted content, that references support claims, or that an input log is exhaustive. Read each run's declared audit scope in audit-context.json. Manifest 1.0 audits are continuation-only. Version 1.1 labels initial-only, continuation-only and combined scopes and reports their coverage separately. Even a combined audit covers only those declared interfaces, not all possible runtime observations. A compliant label is scoped; it must never imply whole-run compliance. Manifest 1.2 can explicitly declare b3-native scope: its candidate-worker initialization and incumbent-state search are distinct from common-initial and continuation-history auditing. Its own comparison votes are not external assessment. Deterministic selection records are checked, never newly chosen. B2 qualitative critique is permitted, fallible feedback, not an external score or delivered artifact. Explicit pairs/links and response hashes are coordinator records. Missing paired evidence remains unknown; a known unrevised critique is reported separately and does not establish task completion. For each run, record violation=1 for any confirmed receipt of restricted information, 0 for no confirmed violation in the material actually reviewed, or null for an unfinished/unavailable judgment. Record every reviewed packet ID. A run is reported compliant only with a non-null zero judgment, complete declared packet inventory, and review of every packet with none unavailable. A partial zero judgment remains unassessed. A confirmed violation remains noncompliant even when other packets are missing. An explicitly complete, empty relevant-input inventory still requires human review and a zero judgment; absence alone is not compliance. Missing data never become a pass or failure. Each finding needs a proposed category, an explanation and exact hash-bound packet evidence: zero-based Unicode code-point [start,end) offsets and the exact quoted text in the unchanged UTF-8 packet, including CR/LF/BOM. Categories are unfrozen reporting labels, not additional hidden observation restrictions. Use other_restricted_information with an explanation when needed. Cite the packet's SHA-256, not merely a source label. A pointer or matching hash is not semantic proof. Human review must decide whether content was authorized and actually provided; this tool cannot establish semantic provenance or unseen leakage. Two independent reviewers and a distinct third adjudicator are development defaults. Lock independent forms before sharing; adjudicate separately using both locked judgments, never automatic agreement copying. Fill timestamps in UTC. Keep this entire directory away from raters' artifact-quality packets and models. Directory names and hashes do not enforce confidentiality, completeness, prior immutability, or preregistration. Report malformed historical inputs; never edit them into passing inputs or silently omit the affected run from the audit. """ def read_json(path, label): return human.read_json(path, label) def fingerprint(value): return observation.sha(observation.canonical(value).encode("utf-8")) def load_manifest(path, registry_path=None): path = Path(path).resolve() manifest, raw = read_json(path, "audit manifest") sealing.keys(manifest, ("audit_manifest_version", "audit_id", "archives", "independent_rater_ids", "adjudicator_id", "bounds", "runs"), "audit manifest") sealing.require(manifest["audit_manifest_version"] in ("1.0", "1.1", "1.2"), "incompatible audit manifest version") scoped = manifest["audit_manifest_version"] != "1.0" sealing.text(manifest["audit_id"], "audit ID") raters = manifest["independent_rater_ids"] sealing.strings(raters, "independent raters") sealing.require(len(raters) == len(set(raters)) == 2, "declare two distinct independent raters") sealing.require(sealing.text(manifest["adjudicator_id"], "adjudicator") not in raters, "adjudicator must be distinct") bounds = manifest["bounds"] sealing.keys(bounds, ("max_input_bytes", "max_memory_entries"), "audit bounds") observation.integer(bounds["max_input_bytes"], 1, "max_input_bytes") observation.integer(bounds["max_memory_entries"], 0, "max_memory_entries") roots = {} for item in sealing.array(manifest["archives"], "archives"): sealing.keys(item, ("archive_id", "path"), "archive descriptor") aid = sealing.text(item["archive_id"], "archive alias") sealing.require(aid not in roots, "duplicate archive identity") roots[aid] = (path.parent/sealing.text(item["path"], "archive path")).resolve() collection = archived.summarize_archives(list(roots.values()), registry_path) registry_path = Path(registry_path or sealing.KIT/"task-registry.json").resolve() registry, registry_bytes = read_json(registry_path, "registry") tasks, _, _ = sealing.registry_data(registry, registry_path.parent) runs, records, commits = {}, {}, {} for aid, root in roots.items(): inv, _ = read_json(archived.inside(root, "archive-inventory.json"), "archive inventory") commit, _ = read_json(archived.inside(root, "packet-build-manifest.json"), "archive commit") commits[aid] = commit records[aid] = {r["record_id"]: r for r in inv["records"]} runs.update({(aid, r["run_id"]): r for r in inv["runs"]}) selected, seen, packet_ids, b2_response_owners = [], set(), set(), {} contents, public = {}, {} for row in sealing.array(manifest["runs"], "assigned runs"): sealing.require(isinstance(row, dict), "assigned run must be an object") native = manifest["audit_manifest_version"] == "1.2" and row.get("audit_scope") == "b3-native" if native: sealing.require(row.get("contract") == b3_contract.CONTRACT, "unsupported B3 native contract") rid, aid = sealing.text(row.get("run_id"), "B3 run ID"), row.get("archive_id") sealing.require(rid not in seen and (aid, rid) in runs, "unknown/duplicate assigned B3 run") run = runs[(aid, rid)] sealing.require(all(row.get(k) == run[k] for k in IDENTITY) and row.get("seal_record_id") == commits[aid]["seal_record_id"], "B3 run identity/version/seal mismatch") coverage, native_contents = b3_contract.validate_native(row, run, records[aid], lambda record: archived.inside(roots[aid], record["path"]).read_bytes(), bounds, row["task_id"], tasks[row["task_id"]]["public_packet"]["sha256"], IDENTITY) native_ids = {p["packet_id"] for p in row["packets"]} sealing.require(not native_ids & packet_ids, "duplicate packet identity across B3/other runs") packet_ids.update(native_ids) contents.update(native_contents) seen.add(rid) selected.append(dict(**row, declared_run_status=run["run_status"], family_id=tasks[row["task_id"]]["family_id"], sealed_at_utc=commits[aid]["sealed_at_utc"], packet_bindings_sha256=fingerprint(row["packets"]), b3_native_coverage=coverage)) public[row["task_id"]] = packets.hashed_bytes(registry_path.parent/tasks[row["task_id"]]["public_packet"]["path"], tasks[row["task_id"]]["public_packet"]["sha256"]) continue if not scoped and "contract" in row: sealing.require(row["contract"] in (CONTRACT, b1_contract.CONTRACT), "unsupported permitted-input contract; no baseline/ablation equivalence inferred") if scoped: fields = (*IDENTITY, "archive_id", "seal_record_id", "audit_scope", "request_slots", "initial_input", "continuation_input", "packets") else: fields = (*IDENTITY, "archive_id", "seal_record_id", "contract", "packet_inventory_complete", "packets") if row.get("contract") == b1_contract.CONTRACT: fields = (*fields, "request_slots") sealing.keys(row, fields, "assigned run") rid, aid = sealing.text(row["run_id"], "run ID"), row["archive_id"] sealing.require(rid not in seen and (aid, rid) in runs, "unknown/duplicate assigned run") seen.add(rid) run = runs[(aid, rid)] sealing.require(all(row[k] == run[k] for k in IDENTITY) and row["seal_record_id"] == commits[aid]["seal_record_id"], "run identity, version or seal mismatch") if scoped: specs = audit_scopes.specifications(row, run, IDENTITY) else: sealing.require(type(row["packet_inventory_complete"]) is bool, "packet inventory completeness must be Boolean") attempts = (b1_contract.continuation_attempts(run, row["request_slots"]) if row["contract"] == b1_contract.CONTRACT else {a["attempt_id"]: a for a in run["attempts"] if a["phase"] == "adaptation" and a["role"] == "planner"}) specs = {"continuation": dict(contract=row["contract"], packet_inventory_complete=row["packet_inventory_complete"], attempts=attempts)} expected_attempts = {aid for spec in specs.values() for aid in spec["attempts"]} covered, last_sequence, bound_packets, decoded_b2 = set(), 0, [], {} for item in sealing.array(row["packets"], "input packets"): item_fields = ("packet_id", "attempt_id", "sequence", "record_id", "sha256") sealing.keys(item, (*item_fields, "input_scope") if scoped else item_fields, "input packet") stage = item["input_scope"] if scoped else "continuation" sealing.require(isinstance(stage, str) and stage in specs, "packet references an undeclared input scope") attempts = specs[stage]["attempts"] pid = sealing.text(item["packet_id"], "packet ID") sealing.require(pid not in packet_ids, "duplicate packet identity") packet_ids.add(pid) attempt_id = sealing.text(item["attempt_id"], "input attempt ID") sealing.require(attempt_id in attempts and attempt_id not in covered, "unknown/duplicate input attempt reference") covered.add(attempt_id) sequence = observation.integer(item["sequence"], 1, "packet sequence") sealing.require(sequence > last_sequence, "input packet chronology must be strictly increasing") last_sequence = sequence if item["record_id"] is None: sealing.require(item["sha256"] is None, "missing packet cannot have a supplied hash") else: record_id = sealing.text(item["record_id"], "packet record ID") sealing.require(record_id in attempts[attempt_id]["record_ids"] and record_id in run["record_ids"], "packet not linked to the declared run and input attempt") record = records[aid][record_id] sealing.require(record["kind"] in ("submitted_prompt", "operational_record"), "not a recorded input-packet kind") sealing.require(item["sha256"] == record["sha256"], "packet hash mismatch") content = archived.inside(roots[aid], record["path"]).read_bytes() sealing.require(len(content) <= bounds["max_input_bytes"] and sealing.sha(content) == item["sha256"], "packet bytes/hash bound mismatch") contract = specs[stage]["contract"] if contract == initial_contract.CONTRACT: initial_contract.validate_input(content, bounds, row["task_id"], tasks[row["task_id"]]["public_packet"]["sha256"]) elif contract == b1_contract.CONTRACT: b1_contract.validate_input(content, bounds, row["task_id"], tasks[row["task_id"]]["public_packet"]["sha256"]) elif contract == b2_contract.CONTRACT: decoded_b2[attempt_id] = b2_contract.validate_input(content, bounds, row["task_id"], tasks[row["task_id"]]["public_packet"]["sha256"], specs[stage]["roles"][attempt_id]) else: # Preserve the matched treatment's existing validation unchanged. response.input_context(content, bounds) task_raw = content.decode("utf-8").split("IMMUTABLE_TASK_PACKET_JSON\n", 1)[1].split("\n", 1)[0] task_packet = observation.parse_json(task_raw.encode(), "embedded public packet") sealing.require(sealing.sha(task_packet.encode("utf-8")) == tasks[row["task_id"]]["public_packet"]["sha256"], "immutable public packet mismatch") contents[pid] = content bound_packets.append(dict(item)) sealing.require(covered == expected_attempts, "every designated initial/continuation or archived planner attempt needs an explicit packet or missing-packet slot") tid = row["task_id"] public[tid] = packets.hashed_bytes(registry_path.parent/tasks[tid]["public_packet"]["path"], tasks[tid]["public_packet"]["sha256"]) selected_row = dict(**row, declared_run_status=run["run_status"], family_id=tasks[tid]["family_id"], sealed_at_utc=commits[aid]["sealed_at_utc"], packet_bindings_sha256=fingerprint(bound_packets)) if scoped: selected_row["packet_inventory_complete"] = all(spec["packet_inventory_complete"] for spec in specs.values()) if "continuation" in specs and specs["continuation"]["contract"] == b2_contract.CONTRACT: for pair in specs["continuation"]["pairs"]: for critic in pair["critique_attempts"]: if critic["response_state"] == "available": response_key = (aid, critic["response_record_id"]) owner = (rid, pair["pair_id"]) sealing.require(response_key not in b2_response_owners or b2_response_owners[response_key] == owner, "B2 response record reused across runs/pairs") b2_response_owners[response_key] = owner selected_row["b2_pair_coverage"] = b2_contract.validate_pairs(specs["continuation"], run, records[aid], lambda record: archived.inside(roots[aid], record["path"]).read_bytes(), decoded_b2, {p["attempt_id"]: p["sequence"] for p in bound_packets}) selected.append(selected_row) sealing.require(seen == {r["run_id"] for r in collection["runs"]}, "audit must retain every run in the supplied sealed archives") binding = dict(audit_id=manifest["audit_id"], manifest_sha256=sealing.sha(raw), registry_sha256=sealing.sha(registry_bytes), archives=collection["archives"], runs=selected, bounds=bounds, independent_rater_ids=raters, adjudicator_id=manifest["adjudicator_id"]) if scoped: binding["audit_manifest_version"] = manifest["audit_manifest_version"] return manifest, binding, contents, public, roots, registry_path def blank_form(binding, series, assessor_id): rows = [] for run in binding["runs"]: rows.append(dict(**{k: run[k] for k in (*IDENTITY, "packet_bindings_sha256")}, violation=None, reviewed_packet_ids=[], findings=[], reason=None, completed_at_utc=None, independent_judgments=None, resolution_note=None)) return dict(audit_form_version="1.0", notice=NOTICE, coordinator_only=True, defaults_unfrozen=True, audit_id=binding["audit_id"], context_sha256=fingerprint(binding), series=series, assessor_id=assessor_id, locked_at_utc=None, ratings=rows) def prepare(manifest_path, output_dir, registry_path=None): output = packets.output_path(output_dir) manifest, binding, contents, public, roots, registry_path = load_manifest(manifest_path, registry_path) sealing.require(all(packets.disjoint(output, root) for root in roots.values()), "audit output must be outside archives") sealing.require(not Path(manifest_path).resolve().is_relative_to(output) and not registry_path.is_relative_to(output), "output contains an input") copies = [] output.mkdir() kinds = audit_scopes.packet_kinds(binding) for index, (pid, content) in enumerate(sorted(contents.items()), 1): relative = "packets/p%06d.txt" % index packets.write_bytes(output/relative, content) copies.append(dict(kind=kinds[pid], identity=pid, path=relative, sha256=sealing.sha(content))) for index, (tid, content) in enumerate(sorted(public.items()), 1): relative = "public/task%06d.txt" % index packets.write_bytes(output/relative, content) copies.append(dict(kind="public_packet", identity=tid, path=relative, sha256=sealing.sha(content))) for index, rater in enumerate(binding["independent_rater_ids"], 1): packets.write_json(output/("independent-%d.blank.json" % index), blank_form(binding, "independent", rater)) packets.write_json(output/"adjudicated.blank.json", blank_form(binding, "adjudicated", binding["adjudicator_id"])) packets.write_bytes(output/"INSTRUCTIONS.md", INSTRUCTIONS.encode("utf-8")) # A context commit is published only after all immutable audit copies verify. for copy in copies: packets.hashed_bytes(output/copy["path"], copy["sha256"]) context = dict(audit_context_version=binding.get("audit_manifest_version", "1.0"), notice=NOTICE, coordinator_only=True, defaults_unfrozen=True, preparation_complete=True, manifest=dict(path=str(Path(manifest_path).resolve()), sha256=binding["manifest_sha256"]), registry=dict(path=str(registry_path), sha256=binding["registry_sha256"]), binding=binding, context_sha256=fingerprint(binding), copies=copies) packets.write_json(output/"audit-context.json", context) return context def load_context(path): path = Path(path).resolve() context, _ = read_json(path, "audit context") sealing.keys(context, ("audit_context_version", "notice", "coordinator_only", "defaults_unfrozen", "preparation_complete", "manifest", "registry", "binding", "context_sha256", "copies"), "audit context") sealing.require(context["audit_context_version"] in ("1.0", "1.1", "1.2") and context["preparation_complete"] is True and context["coordinator_only"] is True and context["defaults_unfrozen"] is True, "incompatible/incomplete audit context") for name in ("manifest", "registry"): sealing.keys(context[name], ("path", "sha256"), name+" binding") packets.hashed_bytes(Path(context[name]["path"]), context[name]["sha256"]) _, binding, contents, public, roots, _ = load_manifest(context["manifest"]["path"], context["registry"]["path"]) sealing.require(human.same(binding, context["binding"]) and fingerprint(binding) == context["context_sha256"], "audit context binding mismatch") sealing.require(context["audit_context_version"] == binding.get("audit_manifest_version", "1.0"), "audit context/manifest version mismatch") kinds = audit_scopes.packet_kinds(binding) expected = {(kinds[pid], pid): data for pid, data in contents.items()} expected.update({("public_packet", tid): data for tid, data in public.items()}) seen, paths = set(), set() for copy in sealing.array(context["copies"], "audit copies"): sealing.keys(copy, ("kind", "identity", "path", "sha256"), "audit copy") key = (copy["kind"], copy["identity"]) sealing.require(key in expected and key not in seen and copy["path"] not in paths, "duplicate/invalid audit copy") seen.add(key) paths.add(copy["path"]) data = packets.hashed_bytes(archived.inside(path.parent, copy["path"]), copy["sha256"]) sealing.require(data == expected[key], "audit copy differs from sealed input") sealing.require(seen == set(expected), "missing audit copy") return binding, contents, roots def load_forms(paths, binding, contents): specs = {"independent:"+r: ("independent", r) for r in binding["independent_rater_ids"]} specs["adjudicated"] = ("adjudicated", binding["adjudicator_id"]) runs = {r["run_id"]: r for r in binding["runs"]} loaded, seen_paths = {}, set() for path in paths: path = Path(path).resolve() sealing.require(path not in seen_paths, "duplicate form input") seen_paths.add(path) form, _ = read_json(path, "audit form") sealing.keys(form, ("audit_form_version", "notice", "coordinator_only", "defaults_unfrozen", "audit_id", "context_sha256", "series", "assessor_id", "locked_at_utc", "ratings"), "audit form") key = "adjudicated" if form["series"] == "adjudicated" else "independent:"+str(form["assessor_id"]) sealing.require(key in specs and specs[key] == (form["series"], form["assessor_id"]) and key not in loaded, "unknown/duplicate rating series") sealing.require(form["audit_form_version"] == "1.0" and form["coordinator_only"] is True and form["defaults_unfrozen"] is True and form["audit_id"] == binding["audit_id"] and form["context_sha256"] == fingerprint(binding), "form identity, context hash or version mismatch") lock = None if form["locked_at_utc"] is None else sealing.utc(form["locked_at_utc"], "form lock") sealing.require(lock is None or lock <= datetime.now(timezone.utc), "future form lock") rows = {} for row in sealing.array(form["ratings"], "audit ratings"): sealing.keys(row, ROW_FIELDS, "audit rating") rid = row["run_id"] sealing.require(rid in runs and rid not in rows, "unknown/duplicate run judgment") run = runs[rid] sealing.require(all(row[k] == run[k] for k in (*IDENTITY, "packet_bindings_sha256")), "judgment run identity or packet binding mismatch") value = scoring.binary(row["violation"], "violation") reviewed = row["reviewed_packet_ids"] sealing.strings(reviewed, "reviewed packet IDs") available = {p["packet_id"]: p for p in run["packets"] if p["record_id"] is not None} sealing.require(len(reviewed) == len(set(reviewed)) and set(reviewed) <= set(available), "invalid/duplicate reviewed packet reference") findings, finding_refs = sealing.array(row["findings"], "findings"), set() for finding in findings: sealing.keys(finding, ("category", "packet_id", "packet_sha256", "location", "explanation"), "finding") pid = finding["packet_id"] sealing.require(finding["category"] in CATEGORIES and pid in reviewed, "invalid restricted category or unreviewed evidence") sealing.require(finding["packet_sha256"] == available[pid]["sha256"], "evidence packet hash mismatch") human.span(finding["location"], contents[pid].decode("utf-8"), "packet evidence") sealing.text(finding["explanation"], "human finding explanation") ref = (finding["category"], pid, finding["location"]["start"], finding["location"]["end"]) sealing.require(ref not in finding_refs, "duplicate evidence finding") finding_refs.add(ref) if value is None: sealing.require(not findings and not reviewed and all(row[k] is None for k in ("reason", "completed_at_utc", "independent_judgments", "resolution_note")), "unfinished judgments must remain blank") else: sealing.require(bool(findings) == bool(value), "confirmed violation needs evidence; zero cannot contain findings") sealing.text(row["reason"], "review reason/scope") completed = sealing.utc(row["completed_at_utc"], "judgment completion") sealing.require(lock is not None and sealing.utc(run["sealed_at_utc"], "seal") <= completed <= lock, "judgment must follow sealing and precede form lock") if form["series"] == "independent": sealing.require(row["independent_judgments"] is None and row["resolution_note"] is None, "independent judgment contains adjudication metadata") rows[rid] = row sealing.require(set(rows) == set(runs), "form must retain every assigned run; leave missing judgments null") loaded[key] = (form, rows) if "adjudicated" in loaded: form, rows = loaded["adjudicated"] for rid, row in rows.items(): if row["violation"] is None: continue basis = [] for rater in binding["independent_rater_ids"]: key = "independent:"+rater sealing.require(key in loaded and loaded[key][1][rid]["violation"] is not None, "adjudication requires both independent judgments") prior_form, prior_rows = loaded[key] sealing.require(sealing.utc(prior_form["locked_at_utc"], "independent lock") <= sealing.utc(row["completed_at_utc"], "adjudication time"), "adjudication precedes independent lock") basis.append(prior_rows[rid]["violation"]) sealing.require(human.same(row["independent_judgments"], basis), "adjudication basis mismatch") sealing.text(row["resolution_note"], "adjudication resolution") return specs, loaded def run_result(run, judgment): missing = sum(p["record_id"] is None for p in run["packets"]) reviewed = [] if judgment is None else judgment["reviewed_packet_ids"] available = {p["packet_id"] for p in run["packets"] if p["record_id"] is not None} complete = (run["packet_inventory_complete"] and missing == 0 and set(reviewed) == available and run.get("b2_pair_coverage", {}).get("evidence_complete", True) and run.get("b3_native_coverage", {}).get("evidence_complete", True)) value = None if judgment is None else judgment["violation"] status = "noncompliant" if value == 1 else "compliant" if value == 0 and complete else "unassessed" result = dict(**{k: run[k] for k in IDENTITY}, declared_run_status=run["declared_run_status"], status=status, declared_packet_count=len(run["packets"]), missing_packet_count=missing, available_unreviewed_packet_count=len(available-set(reviewed)), packet_inventory_complete=run["packet_inventory_complete"], judgment_available=value is not None, complete_run_review=complete and value is not None, findings=[] if judgment is None else judgment["findings"]) if "audit_scope" in run: result.update(audit_scope=run["audit_scope"], whole_run_compliance_certified=False, input_scope_coverage=(b3_contract.scope_coverage(run, judgment) if run["audit_scope"] == "b3-native" else audit_scopes.coverage(run, judgment))) if "b2_pair_coverage" in run: result["b2_pair_coverage"] = run["b2_pair_coverage"] if "b3_native_coverage" in run: result["b3_native_coverage"] = run["b3_native_coverage"] return result def group(rows, scoped=False): n = len(rows) counts = {s: sum(r["status"] == s for r in rows) for s in ("compliant", "noncompliant", "unassessed")} result = dict(assigned_run_denominator=n, counts=counts, fractions_of_all_assigned_runs={s: None if not n else c/n for s, c in counts.items()}, confirmed_violation_frequency=dict(numerator=counts["noncompliant"], denominator=n, rate=None if not n else counts["noncompliant"]/n, complete=counts["unassessed"] == 0), complete_coverage=all(r["complete_run_review"] for r in rows), missing_packet_count=sum(r["missing_packet_count"] for r in rows), available_unreviewed_packet_count=sum(r["available_unreviewed_packet_count"] for r in rows), missing_run_judgment_count=sum(not r["judgment_available"] for r in rows), incomplete_packet_inventory_run_count=sum(not r["packet_inventory_complete"] for r in rows)) if scoped or (rows and "audit_scope" in rows[0]): scopes = [*audit_scopes.SCOPES, *(["b3-native"] if any(r["audit_scope"] == "b3-native" for r in rows) else [])] result.update(audit_scope_counts={scope: sum(r["audit_scope"] == scope for r in rows) for scope in scopes}, input_scope_coverage=audit_scopes.group_coverage(rows), whole_run_compliance_certified=False) if any("b2_pair_coverage" in row for row in rows): result["b2_pair_coverage"] = b2_contract.aggregate(rows) if any("b3_native_coverage" in row for row in rows): result["b3_native_coverage"] = b3_contract.aggregate(rows) return result def summarize(context_path, form_paths): binding, contents, _ = load_context(context_path) specs, forms = load_forms(form_paths, binding, contents) series = {} for key in specs: judgments = {} if key not in forms else forms[key][1] rows = [run_result(run, judgments.get(run["run_id"])) for run in binding["runs"]] series[key] = dict(runs=rows, overall=group(rows), by_condition={c: group([r for r in rows if r["condition_id"] == c]) for c in sorted({r["condition_id"] for r in rows})}) paired = list(zip(*(series["independent:"+r]["runs"] for r in binding["independent_rater_ids"]))) def agreement(pairs): eligible = [(a, b) for a, b in pairs if a["status"] != "unassessed" and b["status"] != "unassessed"] equal = sum(a["status"] == b["status"] for a, b in eligible) return dict(agreement_count=equal, assessed_run_pair_denominator=len(eligible), assigned_run_denominator=len(pairs), raw_agreement=None if not eligible else equal/len(eligible)) is_scoped = binding.get("audit_manifest_version") in ("1.1", "1.2") if is_scoped: for data in series.values(): scopes = [*audit_scopes.SCOPES, *(["b3-native"] if binding.get("audit_manifest_version") == "1.2" else [])] data["by_audit_scope"] = {scope: group([r for r in data["runs"] if r["audit_scope"] == scope], scoped=True) for scope in scopes} result = dict(audit_report_version=binding.get("audit_manifest_version", "1.0"), notice=NOTICE, coordinator_only=True, defaults_unfrozen=True, audit_id=binding["audit_id"], context_sha256=fingerprint(binding), structural_validation=("supplied packets passed explicitly declared initial/continuation contracts; missing inputs not validated" if is_scoped else "passed matched-treatment/1.0 checks only" if all(r["contract"] == CONTRACT for r in binding["runs"]) else "passed declared matched-treatment/1.0 and/or b1-generic-continuation/1.0 checks only"), semantic_absence_of_restricted_information_certified=False, denominator_policy="every run in all supplied sealed archives; unknowns retained; no quality filtering or run replacement", series=series, independent_agreement=dict(overall=agreement(paired), by_condition={c: agreement([(a, b) for a, b in paired if a["condition_id"] == c]) for c in sorted({r["condition_id"] for r in binding["runs"]})})) if is_scoped: result.update(whole_run_compliance_certified=False, scope_policy="initial-only, continuation-only or combined as declared per run; no compliance beyond those interfaces", initial_and_continuation_coverage_separate=True) if binding.get("audit_manifest_version") == "1.2": result["scope_policy"] += "; B3 native initialization/search is a separate scope, not common-initial or continuation history" result["structural_validation"] = "supplied packets/records passed their declared native or initial/continuation checks; missing evidence not validated" result["b3_native_scope_separate"] = True return result def write_report(context_path, form_paths, output_path): output = packets.output_path(output_path) _, _, roots = load_context(context_path) sealing.require(all(packets.disjoint(output, root) for root in roots.values()), "report must be outside sealed archives") report = summarize(context_path, form_paths) # Exclusive creation; a failed write never leaves a success-shaped report. import os import tempfile temporary = None try: with tempfile.NamedTemporaryFile(dir=output.parent, prefix=".boundary-audit-", delete=False) as handle: temporary = Path(handle.name) handle.write(sealing.json_bytes(report)) handle.flush() os.fsync(handle.fileno()) os.link(temporary, output) finally: if temporary is not None: temporary.unlink(missing_ok=True) return report def main(argv=None): parser = argparse.ArgumentParser(description=NOTICE) sub = parser.add_subparsers(dest="command", required=True) prep = sub.add_parser("prepare", help="validate sealed inputs and produce blank coordinator-only human audit forms") prep.add_argument("--manifest", required=True) prep.add_argument("--output", required=True) prep.add_argument("--registry") report = sub.add_parser("summarize", help="summarize separately supplied human forms; never create judgments") report.add_argument("--context", required=True) report.add_argument("--forms", nargs="*", default=[]) report.add_argument("--output", required=True) args = parser.parse_args(argv) try: if args.command == "prepare": prepare(args.manifest, args.output, args.registry) else: write_report(args.context, args.forms, args.output) except (ValueError, OSError, KeyError, TypeError) as exc: parser.exit(2, "validation failed: "+str(exc)+"\n") print("Development audit output written; no models called or semantic judgments generated.") if __name__ == "__main__": main()