"""Verify two complete witnesses in the fixed-axis PlotQA lookup world class. The question/program compilation, source truth, source generator distribution, and correctness of the drawing primitives remain assumptions. No model, network, credential, file identity, or persisted digest participates in acceptance. """ from __future__ import annotations import base64 import copy from fractions import Fraction from .plot_observation import pixels, render_direct, visible_projection def build_bounded_plot_proof( *, group_id, question, program, full_world, changed_world, observed_world, observed_png, renderer, ): """Construct a proof from a group's existing complete and missing views. This supplements the historical release without changing its images or gold. Invalid source pairs fail instead of falling back to arbitrary completions. """ row = { "schema_version": 1, "world_class": "plotqa_fixed_axis_lookup_v1", "group_id": group_id, "question": question, "program": copy.deepcopy(program), "complete_witness_a": copy.deepcopy(full_world), "complete_witness_b": copy.deepcopy(changed_world), "observed_world": copy.deepcopy(observed_world), "observed_png_base64": base64.b64encode(observed_png).decode("ascii"), "renderer": {key: renderer[key] for key in ("renderer_id", "width", "height")}, } result = verify_bounded_plot(row) if not result["ok"]: raise ValueError("Cannot construct bounded proof: " + "; ".join(result["errors"])) return row def verify_bounded_plot(row): import numpy as np from ..executors.plot import PlotExecutor from ..executors.reference_plot import ReferencePlotExecutor from .build import program_from_dict if not isinstance(row, dict): return {"group_id": None, "ok": False, "answers": [], "errors": ["record_is_not_object"]} errors, answers = [], [] if ( row.get("schema_version", 1) != 1 or row.get("world_class", "plotqa_fixed_axis_lookup_v1") != "plotqa_fixed_axis_lookup_v1" ): return { "group_id": row.get("group_id"), "ok": False, "answers": [], "errors": ["unsupported_proof_contract"], } try: if not isinstance(row.get("question"), str) or not row["question"].strip(): raise ValueError("question_missing") if not isinstance(row.get("group_id"), str) or not row["group_id"]: raise ValueError("group_id_missing") record = dict(row["program"]) if record["dsl"] != "plotqa_dsl_v1" or record["compile_status"] != "compiled": raise ValueError("unsupported_program_contract") # Legacy dataclass envelope fields do not enter semantic execution. record.update(question_sha256="", choices_sha256="") program = program_from_dict(record) observed, renderer = row["observed_world"], row["renderer"] if any( type(renderer[k]) is not int or not 220 <= renderer[k] <= 4096 for k in ("width", "height") ): raise ValueError("unsupported_canvas") mask = observed["_node_visibility"] if not mask or any(flag != "hidden" for flag in mask.values()): raise ValueError("invalid_visibility_mask") hidden = set(mask) points = [p for s in observed["series"] for p in s["points"]] if not hidden <= {p["id"] for p in points}: raise ValueError("hidden_point_missing") if any(p["y"] is not None for p in points if p["id"] in hidden): raise ValueError("observed_hidden_value_present") image = pixels(base64.b64decode(row["observed_png_base64"], validate=True)) if image.shape != (renderer["height"], renderer["width"], 4): raise ValueError("observed_image_dimensions") primary, reference = PlotExecutor(), ReferencePlotExecutor() missing = [ex.execute(program, world=observed).status for ex in (primary, reference)] if missing != ["MISSING_INFORMATION", "MISSING_INFORMATION"]: errors.append("observed_program_not_missing") if not np.array_equal(pixels(render_direct(observed, renderer)["image"]), image): errors.append("observed_world_pixels_mismatch") for key in ("complete_witness_a", "complete_witness_b"): world = copy.deepcopy(row[key]) if world.get("_node_visibility"): errors.append("complete_world_is_hidden") if world["render_domain"] != observed["render_domain"]: errors.append("fixed_axis_mismatch") lo, hi = (Fraction(world["render_domain"][k]) for k in ("y_min", "y_max")) if lo >= hi: errors.append("invalid_axis_interval") ids = [] for series in world["series"]: ids.append(series["id"]) xs = [str(p["x"]) for p in series["points"]] if len(xs) != len(set(xs)): errors.append("duplicate_x_label") for point in series["points"]: ids.append(point["id"]) if not lo <= Fraction(point["y"]) <= hi: errors.append("complete_value_outside_axis") if len(ids) != len(set(ids)): errors.append("duplicate_node_id") # Full visibility and label layout must actually render successfully. render_direct(world, renderer) a, b = (ex.execute(program, world=world) for ex in (primary, reference)) if a.status != "UNIQUE" or (a.status, a.answer_canonical) != ( b.status, b.answer_canonical, ): errors.append("executor_failure") answers.append(a.answer_canonical) world["_node_visibility"] = copy.deepcopy(mask) if visible_projection(world) != observed: errors.append("visible_projection_mismatch") if not np.array_equal(pixels(render_direct(world, renderer)["image"]), image): errors.append("observed_pixels_mismatch") if len(answers) != 2 or answers[0] == answers[1]: errors.append("answers_not_distinct") except Exception as exc: errors.append(f"{type(exc).__name__}:{exc}") return { "group_id": row.get("group_id"), "ok": not errors, "answers": answers, "errors": sorted(set(errors)), }