"""Independent, read-only audit of the offline mask-quality recovery experiment.

The new generator, fitter and metric modules are deliberately not imported.
Metrics use an independent scan-line pixel-center raster and SciPy morphology /
Euclidean distances. Only an exclusive verification (or failure) record is written.
Run this only after the producer's explicit completion notification.
"""
from __future__ import annotations

import argparse
import ast
from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
from functools import lru_cache
import hashlib
import json
import math
from pathlib import Path
import re
import sqlite3
import sys

sys.dont_write_bytecode = True

import cv2
import numpy as np
from scipy import ndimage

PROJECT = Path(__file__).resolve().parents[1]
OUTPUT = PROJECT/'outputs/pallet_mask_quality_recovery_v2'
PARENT = PROJECT/'outputs/pallet_mask_remaining_round5_v1'
POSE = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
GALLERY = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
SEAL = 'independent_verification.json'
FAILURE = 'independent_verification_failure.json'
PARENT_PINS = {
    'manifest.json': 'eced4c61119ee63bef7f80e4ae92d76db3d28f3228daef049fa00ad5da69e487',
    'verification.json': '5165170284205283adfaa6514c65cc8126773f49be54ed6398f1dba55f321631',
}
# Publication 3 predates manifest hashes in the independent seal schema. Its
# actual immutable manifest is pinned explicitly, not accepted on its own claim.
PUBLICATION3_MANIFEST_SHA256 = '6e2b625e8eaf38f3dfc0b1ed0ea4dabd16780594d3d25850a9f9811dcd31c3b8'
PUBLICATIONS = {
    3: 'pallet_mask_template_bulk_v5', 4: 'pallet_mask_template_refined_v2',
    5: 'pallet_mask_template_refined_v3', 6: 'pallet_mask_quota_completion_v2',
    7: 'pallet_mask_quota_100_v2', 8: 'pallet_mask_quota_100_continue_v1',
    9: 'pallet_mask_quota_100_complete_v1', 10: 'pallet_mask_remaining_expand_v1',
    11: 'pallet_mask_remaining_continue_v1', 12: 'pallet_mask_remaining_round2_v1',
    13: 'pallet_mask_remaining_round3_v1', 14: 'pallet_mask_remaining_round4_v1',
    15: 'pallet_mask_remaining_round5_v1',
}
NEW_CODE = ('pallet/mask_quality_recovery.py', 'pallet/mask_quality_metrics.py', 'pallet/mask_boundary_fit.py')
IDENTITY_FIELDS = ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')
FALSE_FLAGS = ('human_mask_confirmed', 'human_pose_confirmed', 'automatic_training_eligible',
    'robot_command_eligible', 'ground_truth_eligible', 'human_confirmed', 'metric_pose_confirmed')
METRIC_FIELDS = ('iou', 'assd_native_pixels', 'hd95_native_pixels', 'hd95_bbox_height_fraction',
    'boundary_f1_1px', 'reviewed_to_draft_area_ratio')
ATOL = 2e-5


def require(value, message):
    if not value:
        raise ValueError(message)


def canonical(value):
    return json.dumps(value, sort_keys=True, separators=(',', ':'), allow_nan=False).encode('utf-8')


def sha(value):
    return hashlib.sha256(canonical(value)).hexdigest()


def raw_sha(value):
    return hashlib.sha256(value.encode('utf-8')).hexdigest()


def digest(path):
    value = hashlib.sha256()
    with Path(path).open('rb') as stream:
        for block in iter(lambda: stream.read(4*1024*1024), b''):
            value.update(block)
    return value.hexdigest()


def _unique_object(pairs):
    result = {}
    for key, value in pairs:
        require(key not in result, 'Duplicate JSON object key: '+key)
        result[key] = value
    return result


def loads(raw):
    def reject(value):
        raise ValueError('Nonfinite JSON: '+value)
    return json.loads(raw, object_pairs_hook=_unique_object, parse_constant=reject)


def read(path):
    return loads(Path(path).read_text(encoding='utf-8'))


def read_jsonl(path):
    with Path(path).open(encoding='utf-8') as stream:
        return [loads(line) for line in stream if line.strip()]


def indexed(values, field='target_key'):
    result = {}
    for value in values:
        key = value[field]
        require(key not in result, 'Duplicate '+field+': '+str(key))
        result[key] = value
    return result


def member(root, name):
    root = Path(root).resolve()
    value = Path(name)
    require(not value.is_absolute() and '..' not in value.parts, 'Unsafe artifact path: '+str(name))
    path = root/value
    require(path.resolve().is_relative_to(root) and not any(p.is_symlink() for p in (path, *path.parents) if p.is_relative_to(root)),
        'Symlink/escaped artifact: '+str(name))
    return path


def inventory(root):
    root = Path(root)
    require(not root.is_symlink(), 'Symlink inventory root')
    result = {}
    for path in sorted(root.rglob('*')):
        require(not path.is_symlink(), 'Symlink in immutable evidence: '+str(path))
        if path.is_file():
            result[str(path.relative_to(root))] = digest(path)
    return result


def protected_inventory(formal):
    formal = Path(formal)
    files = set(formal.glob('*.json')) | set(formal.glob('*.jsonl')) | {formal/'checksums.sha256'}
    for name in ('annotations','labels','protocol'):
        files.update((formal/name).rglob('*'))
    files.update((formal/'images').rglob('*.json'))
    return {str(path.relative_to(formal)):digest(path) for path in sorted(files) if path.is_file()}


def equal(actual, expected, context='value', atol=ATOL):
    """Strict keys/types, finite floating tolerances; booleans are never numbers."""
    if isinstance(expected, dict):
        require(isinstance(actual, dict) and actual.keys() == expected.keys(), context+': object fields differ')
        for key in expected:
            equal(actual[key], expected[key], context+'.'+key, atol)
    elif isinstance(expected, (list, tuple)):
        require(isinstance(actual, (list, tuple)) and len(actual) == len(expected), context+': sequence differs')
        for i, (a, b) in enumerate(zip(actual, expected)):
            equal(a, b, context+'.'+str(i), atol)
    elif isinstance(expected, (float, np.floating)):
        require(type(actual) in (int, float) and math.isfinite(actual) and math.isfinite(expected)
            and abs(actual-expected) <= atol, context+': floating value differs')
    else:
        require(type(actual) is type(expected) and actual == expected, context+': exact value differs')


def polygon(value, size):
    p = np.asarray(value, dtype=np.float64)
    require(p.ndim == 2 and p.shape[1:] == (2,) and 3 <= len(p) <= 128 and np.isfinite(p).all(), 'Invalid polygon vertices')
    width, height = size
    require((p >= 0).all() and (p[:, 0] <= width).all() and (p[:, 1] <= height).all(), 'Polygon outside native image')
    q = np.roll(p, -1, axis=0)
    require((np.linalg.norm(q-p, axis=1) >= .01).all(), 'Duplicate adjacent vertices')
    require(abs(float(np.sum(p[:, 0]*q[:, 1]-p[:, 1]*q[:, 0])))/2 >= 1, 'Degenerate polygon')
    def cross(a, b, c):
        u, v = b-a, c-a
        return u[0]*v[1]-u[1]*v[0]
    for i in range(len(p)):
        a, b = p[i], q[i]
        for j in range(i+2, len(p)):
            if i == 0 and j == len(p)-1:
                continue
            c, d = p[j], q[j]
            if cross(a,b,c)*cross(a,b,d) <= 0 and cross(c,d,a)*cross(c,d,b) <= 0:
                require(any(max(min(a[k],b[k]),min(c[k],d[k])) > min(max(a[k],b[k]),max(c[k],d[k]))+1e-9
                    for k in (0, 1)), 'Self-intersecting polygon')
    return p


def raster(value, size):
    """Independent scan-line implementation: left included, right excluded."""
    p = polygon(value, size)
    width, height = size
    result = np.zeros((height, width), dtype=bool)
    starts, ends = p, np.roll(p, -1, axis=0)
    centers = np.arange(width, dtype=float)+.5
    for row in range(max(0, math.floor(p[:,1].min())), min(height, math.ceil(p[:,1].max()))):
        y = row+.5
        crosses = (starts[:,1] > y) != (ends[:,1] > y)
        a, b = starts[crosses], ends[crosses]
        intercepts = np.sort(a[:,0]+(y-a[:,1])*(b[:,0]-a[:,0])/(b[:,1]-a[:,1]))
        result[row] = np.searchsorted(intercepts, centers, side='right') % 2 == 1
    return result


def independent_metrics(draft, reviewed, size, bbox_height):
    a, b = raster(draft, size), raster(reviewed, size)
    require(a.any() and b.any(), 'Raster-empty contour')
    edge_a = a & ~ndimage.binary_erosion(a, structure=np.ones((3,3), bool), border_value=0)
    edge_b = b & ~ndimage.binary_erosion(b, structure=np.ones((3,3), bool), border_value=0)
    left = ndimage.distance_transform_edt(~edge_b)[edge_a]
    right = ndimage.distance_transform_edt(~edge_a)[edge_b]
    distances = np.concatenate((left, right))
    precision, recall = np.mean(left <= 1), np.mean(right <= 1)
    vertex = None
    if len(draft) == len(reviewed):
        p, q = np.asarray(draft), np.asarray(reviewed)
        vertex = min(float(np.mean(np.linalg.norm(p-np.roll(order,i,axis=0), axis=1)))
            for order in (q, q[::-1]) for i in range(len(p)))
    hd95 = float(np.quantile(distances, .95))
    return dict(iou=float(np.count_nonzero(a & b)/np.count_nonzero(a | b)),
        assd_native_pixels=float(np.mean(distances)), hd95_native_pixels=hd95,
        hd95_bbox_height_fraction=hd95/max(bbox_height,1),
        boundary_f1_1px=float(2*precision*recall/max(precision+recall,1e-12)),
        reviewed_to_draft_area_ratio=float(np.count_nonzero(b)/np.count_nonzero(a)),
        mean_vertex_distance_native_pixels=vertex, vertex_count_changed=len(draft) != len(reviewed),
        draft_vertex_count=len(draft), reviewed_vertex_count=len(reviewed), polygon_changed=draft != reviewed,
        saved_annotation_is_research_reference_not_confirmed_GT=True)


def summary(records):
    records = list(records)
    result = dict(count=len(records))
    for field in METRIC_FIELDS:
        result[field+'_median'] = float(np.median([r[field] for r in records])) if records else None
    result.update(iou_p10=float(np.quantile([r['iou'] for r in records], .1)) if records else None,
        iou_at_least_09=sum(r['iou'] >= .9 for r in records), iou_below_08=sum(r['iou'] < .8 for r in records),
        polygon_changed=sum(r['polygon_changed'] for r in records),
        vertex_count_changed=sum(r['vertex_count_changed'] for r in records))
    return result


def stratified(records, field):
    return {str(value): summary(r for r in records if r[field] == value)
        for value in sorted({r[field] for r in records}, key=str)}


def quality_gate(records, total):
    records = list(records)
    stats, n = summary(records), len(records)
    require(type(total) is int and total >= n, 'Invalid coverage denominator')
    reasons = []
    if n < 10:
        reasons.append('insufficient_accepted_group_held_out_samples')
    if not n or stats['iou_median'] < .95:
        reasons.append('median_iou_below_095')
    if not n or stats['iou_at_least_09']/n < .9:
        reasons.append('less_than_90pct_iou_at_least_09')
    if any(r['hd95_native_pixels'] > max(2, .1*r['bbox_height']) for r in records):
        reasons.append('boundary_error_above_native_or_relative_limit')
    return dict(state='blocked' if reasons else 'passed', reasons=reasons, accepted=n, total=total,
        delivery_rate=n/total if total else 0., quality=stats, production_publication_authorized=False,
        thresholds_are_predeclared_pilot_goals_not_accuracy_guarantees=True)


def read_store(path, kind, frozen=False):
    table = {'pose': 'annotations', 'gallery': 'reviews'}[kind]
    path = Path(path).resolve()
    require(path.is_file(), 'Absent read-only store: '+str(path))
    uri = path.as_uri()+'?mode=ro'+('&immutable=1' if frozen else '')
    with closing(sqlite3.connect(uri, uri=True)) as db:
        db.execute('PRAGMA query_only=ON')
        db.execute('BEGIN')
        schema = list(db.execute('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name'))
        tables = {}
        for row in schema:
            if row[0] == 'table':
                require(re.fullmatch(r'[A-Za-z0-9_]+', row[1]) is not None, 'Unsafe SQLite identifier')
                tables[row[1]] = sha(sorted(canonical(list(v)).decode() for v in db.execute('SELECT * FROM "'+row[1]+'"')))
        current = {}
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM '+table+' ORDER BY target_key'):
            value = loads(raw)
            require(key not in current and value.get('target_key') == key and type(revision) is int
                and type(value.get('revision')) is int and value['revision'] == revision, 'Current relational identity differs')
            current[key] = (revision, raw, value)
        events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        histories = defaultdict(list)
        for event_id, key, revision, raw in events:
            value = loads(raw)
            require(value.get('target_key') == key and type(value.get('revision')) is int and value['revision'] == revision,
                'Historical relational identity differs')
            require(not histories[key] or histories[key][-1][1] < revision, 'Duplicate/nonmonotone event revision')
            histories[key].append((event_id, revision, raw, value))
        metadata = dict(db.execute('SELECT key,value FROM metadata'))
    return dict(current=current, events=events, histories=dict(histories), metadata=metadata,
        fingerprint=dict(schema_sha256=sha(schema), tables=tables))


def exact_latest(store, key):
    require(key in store['current'] and key in store['histories'], 'Missing actual current/history pair')
    revision, raw, value = store['current'][key]
    last = store['histories'][key][-1]
    require(last[1:3] == (revision, raw), 'Current is not exact raw latest event: '+key)
    return value, last


def unconfirmed(value):
    require(value.get('state') not in ('deleted','deferred','confirmed'), 'Protected state used as contour reference')
    require(all(value.get(field, False) is False for field in FALSE_FLAGS), 'False GT/confirmation/eligibility claim')


def original_rectangle(value, row):
    if np.shape(value) != (4,2):
        return False
    x,y,w,h = (row['bbox'][field] for field in ('x','y','width','height'))
    return bool(np.allclose(sorted(value), sorted([[x,y],[x+w,y],[x+w,y+h],[x,y+h]]), rtol=0, atol=1e-5))


def load_formal(parent):
    formal = Path(read(PROJECT/'configs/active_formal_dataset.json')['dataset']).resolve()
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    require(digest(formal/'manifest.json') == parent['formal_manifest_sha256']
        and digest(classification) == parent['classification_sha256'], 'Formal/classification input changed')
    frames = indexed(read_jsonl(formal/'frames.jsonl'), 'frame_id')
    rows = indexed(read_jsonl(classification))
    expected = {}
    for annotation in read_jsonl(formal/'annotations/frames.jsonl'):
        frame = frames[annotation['frame_id']]
        require(annotation['split'] == frame['split'] and annotation['group_id'] == frame['group_id'], 'Formal group/split differs')
        for i, box in enumerate(annotation['boxes']):
            key = hashlib.sha256(f'{annotation["frame_id"]}::box:{i:04d}'.encode()).hexdigest()[:24]
            require(key not in expected, 'Duplicate formal target')
            row = rows[key]
            require(row['box_index'] == i and row['bbox'] == box and all(row[f] == annotation[f]
                for f in ('frame_id','split','group_id')) and row['policy_id'] == frame['sensor_policy']['policy_id']
                and row['source_pcd_sha256'] == frame['source_anchor_hash'], 'Formal target provenance differs: '+key)
            expected[key] = dict(row, size=frame['size'])
    require(expected.keys() == rows.keys() and len(rows) == 12344 and len(frames) == 10374, 'Fixed formal target/frame denominator differs')
    return formal, expected, frames


class NativeEvidence:
    def __init__(self, formal, rows, frames):
        self.formal, self.rows, self.frames = formal, rows, frames
        self.used = {}

    @lru_cache(maxsize=512)
    def image(self, frame_id):
        frame = self.frames[frame_id]
        name = frame['image']
        path = member(self.formal, name)
        actual = digest(path)
        require(actual == frame['files'][name], 'Native image bytes changed')
        image = cv2.imread(str(path), cv2.IMREAD_GRAYSCALE)
        require(image is not None and image.shape[::-1] == tuple(frame['size']), 'Invalid native image')
        self.used[name] = actual
        return image

    @lru_cache(maxsize=2048)
    def patch(self, key):
        row = self.rows[key]
        return native_patch(self.image(row['frame_id']), row['bbox'])


def patch_matrix(box):
    x,y,w,h = (box[k] for k in ('x','y','width','height'))
    sx, sy = 256/(w+2*.06*w), 112/(h+2*.22*h)
    return np.array([[sx,0,-(x-.06*w)*sx-.5],[0,sy,-(y-.22*h)*sy-.5]], np.float64)


def native_patch(image, box):
    matrix = patch_matrix(box)
    matrix[:,2] += matrix[:,:2] @ np.array([.5,.5])
    value = cv2.warpAffine(image, matrix, (256,112), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)
    lo, hi = np.percentile(value, [1,99])
    value = np.clip((value.astype(float)-lo)*255/max(hi-lo,1),0,255).astype(np.uint8)
    return cv2.GaussianBlur(value,(3,3),.65)


def ncc(a, b):
    a,b = np.asarray(a,float).ravel(), np.asarray(b,float).ravel()
    a,b = a-a.mean(), b-b.mean()
    return float(np.dot(a,b)/max(np.linalg.norm(a)*np.linalg.norm(b),1e-12))


def image_dhash(image, box):
    x,y,w,h = (box[k] for k in ('x','y','width','height'))
    crop = image[max(0,int(y)):min(image.shape[0],math.ceil(y+h)), max(0,int(x)):min(image.shape[1],math.ceil(x+w))]
    require(crop.size > 0, 'Empty native image crop')
    small = cv2.resize(crop,(17,8),interpolation=cv2.INTER_AREA)
    return int.from_bytes(np.packbits(small[:,1:] > small[:,:-1]).tobytes(),'big')


def independent_components(catalog, evidence):
    """Recompute complete connected components, including non-train sources."""
    keys = [r['target_key'] for r in catalog]
    parent = {key:key for key in keys}
    def find(key):
        while parent[key] != key:
            parent[key] = parent[parent[key]]
            key = parent[key]
        return key
    def union(a,b):
        a,b = find(a),find(b)
        if a != b:
            parent[max(a,b)] = min(a,b)
    seen, hashes, buckets = {}, {}, defaultdict(list)
    for record in catalog:
        key,row = record['target_key'],record['row']
        image = evidence.image(row['frame_id'])
        hashes[key] = image_dhash(image,row['bbox'])
        tokens = (('group',row['group_id']),('frame',row['frame_id']),('pcd',row['source_pcd_sha256']),
            ('image',sha(dict(size=image.shape, bytes_sha256=hashlib.sha256(image.tobytes()).hexdigest()))))
        for token in tokens:
            if token in seen:
                union(key,seen[token])
            else:
                seen[token] = key
        bucket = (row['final_class_id'],row['policy_id'])
        for other in buckets[bucket]:
            if find(key) != find(other) and (hashes[key] ^ hashes[other]).bit_count() <= 2:
                if ncc(evidence.patch(key),evidence.patch(other)) >= .995:
                    union(key,other)
        buckets[bucket].append(key)
    grouped = defaultdict(list)
    for key in keys:
        grouped[find(key)].append(key)
    return {key:sha(sorted(group)) for group in grouped.values() for key in group}


def fold_for(component):
    require(isinstance(component,str) and re.fullmatch('[0-9a-f]{64}',component) is not None, 'Invalid independent component hash')
    return int(component[:16],16) % 3


def eligible(target, source, components):
    a,b = target['row'],source['row']
    ca,cb = components[target['target_key']],components[source['target_key']]
    return (a['split'] == b['split'] == 'train' and ca != cb and fold_for(ca) != fold_for(cb)
        and a['final_class_id'] != 14 and a.get('viewpoint_candidate') not in (None,'unknown')
        and all(a.get(field) == b.get(field) for field in ('final_class_id','policy_id','viewpoint_candidate')))


def reference_budget(target, catalog, components, evidence, maximum=3):
    options = [r for r in catalog if eligible(target,r,components)]
    ranked = sorted(options, key=lambda r:(-ncc(evidence.patch(r['target_key']),evidence.patch(target['target_key'])),r['target_key']))
    selected, used = [],set()
    for record in ranked:
        component = components[record['target_key']]
        if component not in used:
            selected.append(record)
            used.add(component)
            if len(selected) == maximum:
                break
    return options,selected


def transform(points, matrix):
    points = np.asarray(points,dtype=float)
    return np.column_stack((points,np.ones(len(points)))) @ np.asarray(matrix).T


def reference_family(value, box):
    p = np.asarray(value,float)
    edges = np.roll(p,-1,axis=0)-p
    next_edges = np.roll(edges,-1,axis=0)
    signed_area = np.sum(p[:,0]*np.roll(p[:,1],-1)-p[:,1]*np.roll(p[:,0],-1))
    turns = (edges[:,0]*next_edges[:,1]-edges[:,1]*next_edges[:,0])*np.sign(signed_area)
    return [len(p),int(np.count_nonzero(turns < -.1)),'narrow' if box['width']/max(box['height'],1) < 2.5 else 'wide']


def replay_alignment(source, target, evidence, numbers):
    """Replay the declared photometric optimization, not the producer's function."""
    a,b = evidence.patch(source['target_key']),evidence.patch(target['target_key'])
    info = dict(initial_ncc=ncc(a,b))
    if info['initial_ncc'] < numbers['initial_ncc_min']:
        return None,dict(info,rejection='initial_similarity_below_original_gate')
    try:
        ecc,matrix = cv2.findTransformECC(a,b,np.eye(2,3,dtype=np.float32),cv2.MOTION_AFFINE,
            (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,100,1e-5),None,5)
    except cv2.error:
        return None,dict(info,rejection='affine_registration_failed')
    singular = np.linalg.svd(matrix[:,:2],compute_uv=False)
    if np.linalg.det(matrix[:,:2]) <= 0 or singular.min() < .7 or singular.max() > 1.4 or np.max(np.abs(matrix[:,2])) > 28:
        return None,dict(info,rejection='excessive_affine_deformation')
    source_patch_polygon = transform(source['polygon'],patch_matrix(source['row']['bbox']))
    aligned = cv2.warpAffine(b,matrix,(256,112),flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP)
    band = np.zeros(a.shape,np.uint8)
    cv2.polylines(band,[np.round(source_patch_polygon).astype(np.int32)],True,255,7)
    info.update(ecc=float(ecc),boundary_ncc=ncc(a[band > 0],aligned[band > 0]),affine_patch_transform=matrix.tolist())
    if ecc < numbers['ecc_min'] or info['boundary_ncc'] < numbers['boundary_ncc_min']:
        return None,dict(info,rejection='photometric_gates_not_met')
    initial = transform(transform(source_patch_polygon,matrix),cv2.invertAffineTransform(patch_matrix(target['row']['bbox'])))
    return initial,dict(info,rejection=None)


def native_sample(image, points):
    # OpenCV interpolation is quantized to 1/32 px; using the same primitive is
    # necessary to audit saved support exactly rather than change the algorithm.
    points = np.asarray(points,np.float32)
    return cv2.remap(image.astype(np.float32),points[...,0]-.5,points[...,1]-.5,
        cv2.INTER_LINEAR,borderMode=cv2.BORDER_REPLICATE)


def replay_edge(image, a, b, source_image, source_a, source_b, numbers):
    a,b,sa,sb = (np.asarray(v,float) for v in (a,b,source_a,source_b))
    vector,source_vector = b-a,sb-sa
    length,source_length = np.linalg.norm(vector),np.linalg.norm(source_vector)
    if length < 3:
        return None,dict(rejection='edge_too_short_for_independent_support')
    if source_length < 3:
        return None,dict(rejection='source_edge_too_short')
    tangent = vector/length
    normal = np.array([-tangent[1],tangent[0]])
    source_normal = np.array([-source_vector[1],source_vector[0]])/source_length
    fractions = np.linspace(.15,.85,max(12,min(96,int(length))))
    origins,source_origins = a+fractions[:,None]*vector,sa+fractions[:,None]*source_vector
    delta = (native_sample(source_image,(source_origins+source_normal).reshape(1,-1,2))
        -native_sample(source_image,(source_origins-source_normal).reshape(1,-1,2))).ravel()
    polarity = float(np.sign(np.median(delta)))
    if polarity == 0 or np.median(np.abs(delta)) < numbers['contrast_min']/2:
        return None,dict(rejection='weak_source_boundary_polarity')
    search = numbers['normal_search_native_pixels']
    offsets = np.arange(-search,search+.01,.25)
    coordinates = origins[:,None,:]+offsets[None,:,None]*normal
    if (coordinates[...,0].min() < 1 or coordinates[...,1].min() < 1
            or coordinates[...,0].max() > image.shape[1]-1 or coordinates[...,1].max() > image.shape[0]-1):
        return None,dict(rejection='boundary_search_touches_image_edge')
    contrast = (native_sample(image,coordinates+normal)-native_sample(image,coordinates-normal))*polarity
    score = contrast-.75*np.abs(offsets)[None,:]
    best = np.argmax(score,axis=1)
    strength,selected = contrast[np.arange(len(best)),best],offsets[best]
    kept = (strength >= numbers['contrast_min']) & (np.abs(selected) < search-.25)
    support = float(kept.mean())
    if support < numbers['support_fraction_min']:
        return None,dict(rejection='insufficient_boundary_support',support_fraction=support)
    points = origins[kept]+selected[kept,None]*normal
    fitted = cv2.fitLine(points.astype(np.float32),cv2.DIST_HUBER,0,.001,.001).ravel()
    direction,center = fitted[:2].astype(float),fitted[2:].astype(float)
    if direction @ tangent < 0:
        direction = -direction
    angle = float(np.degrees(np.arccos(np.clip(direction @ tangent,-1,1))))
    residuals = np.abs(direction[0]*(points[:,1]-center[1])-direction[1]*(points[:,0]-center[0]))
    p90 = float(np.quantile(residuals,.9))
    if angle > numbers['max_line_angle_degrees'] or p90 > numbers['fit_residual_p90_max']:
        return None,dict(rejection='ambiguous_or_nonstraight_boundary',angle_degrees=angle,residual_p90=p90,support_fraction=support)
    return (center,direction),dict(rejection=None,support_fraction=support,angle_degrees=angle,residual_p90=p90,
        median_contrast=float(np.median(strength[kept])),normal_offset_median=float(np.median(selected[kept])),support_points=points.tolist())


def replay_refinement(source, target, initial, evidence, numbers):
    source_image,target_image = evidence.image(source['row']['frame_id']),evidence.image(target['row']['frame_id'])
    p,q = np.asarray(source['polygon'],float),np.asarray(initial,float)
    diagnostics,lines = [],[]
    for i in range(len(p)):
        line,diagnostic = replay_edge(target_image,q[i],q[(i+1)%len(q)],source_image,p[i],p[(i+1)%len(p)],numbers)
        diagnostics.append(diagnostic)
        if line is None:
            return None,dict(rejection='native_edge_support_failed',failed_edge=i,edges=diagnostics)
        lines.append(line)
    points = []
    for i,(center,direction) in enumerate(lines):
        previous_center,previous_direction = lines[i-1]
        matrix = np.column_stack((previous_direction,-direction))
        if abs(np.linalg.det(matrix)) < .08:
            return None,dict(rejection='adjacent_edges_not_distinct',edges=diagnostics)
        points.append(previous_center+np.linalg.solve(matrix,center-previous_center)[0]*previous_direction)
    points = np.asarray(points)
    if np.max(np.linalg.norm(points-q,axis=1)) > max(3.,target['row']['bbox']['height']*.16):
        return None,dict(rejection='excessive_vertex_refinement',edges=diagnostics)
    try:
        points = polygon(points.tolist(),target['row']['size']).tolist()
    except ValueError:
        return None,dict(rejection='invalid_fitted_polygon',edges=diagnostics)
    ratio = abs(cv2.contourArea(np.asarray(points,np.float32)))/max(abs(cv2.contourArea(q.astype(np.float32))),1)
    if not .72 < ratio < 1.35:
        return None,dict(rejection='unstable_polygon_area',edges=diagnostics)
    return points,dict(rejection=None,edges=diagnostics,refinement_area_ratio=ratio,vertex_count_inherited=True,vertex_order_inherited=True)


def replay_consensus(candidates, numbers):
    minimum = numbers['minimum_independent_references']
    if len(candidates) < minimum:
        return None,dict(rejection='insufficient_independent_supported_references')
    for i,candidate in enumerate(candidates):
        support = [other for j,other in enumerate(candidates) if j != i
            and other['independent_component'] != candidate['independent_component'] and other['family'] == candidate['family']
            and np.max(np.linalg.norm(np.asarray(other['polygon'])-candidate['polygon'],axis=1)) <= numbers['consensus_native_pixels_max']]
        if len(support) >= minimum-1:
            return candidate,dict(rejection=None,support_reference_keys=[other['reference_key'] for other in support],
                agreement_limit_native_pixels=numbers['consensus_native_pixels_max'])
    return None,dict(rejection='multi_reference_geometry_disagreement')


def replay_prediction(target, budget, evidence, numbers, components):
    trials,candidates = [],[]
    for source in budget:
        key = source['target_key']
        initial,alignment = replay_alignment(source,target,evidence,numbers)
        trial = dict(reference_key=key,reference_payload_sha256=source['payload_sha256'],
            independent_component=components[key],alignment=alignment)
        if initial is not None:
            candidate,diagnostic = replay_refinement(source,target,initial,evidence,numbers)
            trial['native_fit'] = diagnostic
            if candidate is not None:
                candidates.append(dict(reference_key=key,independent_component=components[key],polygon=candidate,
                    family=reference_family(source['polygon'],source['row']['bbox'])))
        trials.append(trial)
    chosen,consensus = replay_consensus(candidates,numbers)
    if not budget:
        consensus = dict(rejection='no_compatible_independent_reference')
    diagnostic = dict(attempts=trials,consensus=consensus,rejection=consensus['rejection'],
        candidate_family=None if chosen is None else chosen['family'])
    return None if chosen is None else chosen['polygon'],diagnostic


def resolve_event(value, publications, registry):
    version,key,base = value.get('proposal_publication'),value.get('target_key'),value.get('base_proposal_id')
    require(type(version) is int and version in publications, 'Unknown or nonnumeric gallery publication')
    identity = version,base,key
    if identity in registry:
        return registry[identity]
    publication = publications[version]
    proposal = publication['proposals'].get(key)
    require(proposal is not None and sha(proposal) == base, 'Unknown canonical / independent publication-hash-key triple')
    return dict(kind='canonical_publication', namespace=None, proposal=proposal, path=publication['path'])


def registries(rows, formal_sha, pose):
    publications, registry, fingerprints = {},{},{}
    for version,name in PUBLICATIONS.items():
        path = PROJECT/'outputs'/name
        manifest,seal = read(path/'manifest.json'),read(path/'verification.json')
        require(manifest['publication_version'] == version and seal['state'] == 'passed'
            and (digest(path/'manifest.json') == PUBLICATION3_MANIFEST_SHA256 if version == 3
                else seal['manifest_sha256'] == digest(path/'manifest.json'))
            and seal['proposals_sha256'] == digest(path/'proposals.jsonl'), 'Unsealed / changed historical publication')
        publications[version] = dict(path=path, proposals=indexed(read_jsonl(path/'proposals.jsonl')),
            manifest_sha256=digest(path/'manifest.json'), proposals_sha256=digest(path/'proposals.jsonl'),
            verification_sha256=digest(path/'verification.json'))
    for number,version,expected in ((1,13,37),(2,14,35),(3,15,26)):
        path = PROJECT/f'outputs/pallet_mask_preannotated_requests_v{number}'
        manifest,seal = read(path/'manifest.json'),read(path/'verification.json')
        drafts,tasks = indexed(read_jsonl(path/'proposals.jsonl')),indexed(read_jsonl(path/'requests.jsonl'))
        require(drafts.keys() == tasks.keys() and len(drafts) == expected == manifest['request_count']
            and seal['state'] == 'passed' and seal['manifest_sha256'] == digest(path/'manifest.json')
            and seal['request_count'] == expected and digest(path/'verification.json') == digest(path/'independent_verification.json'),
            'Incomplete/changed independent prelabel task pack')
        for field,name in (('proposals_sha256','proposals.jsonl'),('requests_sha256','requests.jsonl')):
            require(manifest[field] == seal[field] == digest(path/name), 'Prelabel task/proposal byte binding differs')
        require(manifest['formal_manifest_sha256'] == formal_sha and all(manifest['parent_'+field] == publications[version][field]
            for field in ('manifest_sha256','proposals_sha256','verification_sha256')), 'Prelabel task main-publication binding differs')
        for assets in (manifest['public_assets'],seal['public_assets']):
            require(all(digest(member(path,name)) == expected_sha for name,expected_sha in assets.items()), 'Frozen prelabel asset changed')
        for key,draft in drafts.items():
            row,task = rows[key],tasks[key]
            require(draft['source'] == task['source'] == row and draft['class_id'] == task['class_id'] == row['final_class_id']
                and draft['polygon'] == task['assistant_polygon'] and draft['preannotation'] == task['preannotation'], 'Prelabel formal/task geometry differs')
            require(all(draft.get(field,False) is False for field in FALSE_FLAGS+('human_edited','production_saved','counts_as_completed_annotation','propagation_seed_eligible')),
                'Assistant draft promoted to human/GT/old strict seed')
            old = pose['current'].get(key)
            require(draft['target_expected_revision'] == (old[0] if old else 0)
                and draft['target_snapshot_payload_sha256'] == (sha(old[2]) if old else None), 'Prelabel protected pose snapshot differs')
            proof = draft['preannotation']
            require(proof['author_role'] == 'AI_assistant' and proof['human_saved'] is False
                and proof['polygon_sha256'] == sha(draft['polygon']) and proof['image_sha256'] == digest(path/'images'/f'{key}.png'), 'Prelabel raw image/polygon proof differs')
            identity = version,sha(draft),key
            require(identity not in registry, 'Duplicate independent registry triple')
            registry[identity] = dict(kind='independent_preannotation',namespace=f'v{number}',proposal=draft,path=path)
        fingerprints[str(path.relative_to(PROJECT))] = dict(manifest=digest(path/'manifest.json'),
            verification=digest(path/'verification.json'),proposals=digest(path/'proposals.jsonl'))
    return publications,registry,fingerprints


@lru_cache(maxsize=1)
def gallery_editor_version():
    editor_tree = ast.parse((PROJECT/'pallet/mask_gallery_review.py').read_text())
    versions = [ast.literal_eval(node.value) for node in editor_tree.body if isinstance(node,ast.Assign)
        and any(isinstance(target,ast.Name) and target.id == 'VERSION' for target in node.targets)]
    require(len(versions) == 1, 'Unresolvable frozen gallery editor version')
    return versions[0]


def check_gallery_event(value, row, reference, publication, formal_sha, evidence):
    proposal = reference['proposal']
    require(proposal['source'] == row and proposal['class_id'] == row['final_class_id']
        and proposal['formal_manifest_sha256'] == formal_sha, 'Event proposal formal identity differs')
    identity = {field:row[field] for field in IDENTITY_FIELDS}
    image_sha = digest(reference['path']/'images'/f'{row["target_key"]}.png')
    native_sha = evidence.frames[row['frame_id']]['files'][evidence.frames[row['frame_id']]['image']]
    require(image_sha == native_sha, 'Gallery source image is not actual native input')
    identity.update(formal_manifest_sha256=formal_sha,image_sha256=image_sha)
    require(value['source'] == identity and value['source_binding'] == sha(identity), 'Gallery event source/image proof differs')
    require(value['proposal_polygon'] == proposal['polygon'] and value['vertex_count'] == len(value['polygon'])
        and value['event_kind'] == 'human_mask_gallery_revision' and value['human_edited'] is True
        and value['pose'] is None, 'Fabricated machine/human event or polygon')
    require(value['binding'] == sha(dict(formal=formal_sha,proposals=publication['proposals_sha256'],editor_version=gallery_editor_version())), 'Event bound to wrong main editor publication')
    require(value.get('pose_store_written') is False and value.get('original_proposal_modified') is False, 'Event claims source/pose write')
    if reference['kind'] == 'independent_preannotation':
        expected = dict(assistant_draft=proposal['preannotation'],assistant_draft_not_human_edit=True,
            proposal_sha256=sha(proposal),worklist_manifest_sha256=digest(reference['path']/'manifest.json'))
        require(value.get('template_provenance') is None and value.get('human_source_provenance') is None
            and value.get('preannotation_provenance') == expected, 'Independent task triple provenance differs')
    elif proposal.get('schema') == 'saved_gallery_mask_entry_v1':
        require(value.get('template_provenance') is None and value.get('preannotation_provenance') is None
            and value.get('human_source_provenance') == proposal['human_source'], 'Canonical saved-gallery provenance differs')
    else:
        require(value.get('template_provenance') == proposal['template'] and value.get('preannotation_provenance') is None,
            'Historical template event provenance differs')
    unconfirmed(value)
    polygon(value['polygon'],row['size'])


def check_catalog(out, catalog, pose, gallery, rows, publications, registry, formal_sha, evidence):
    records = indexed(catalog)
    require(len(records) == 1484, 'Exact 1484 unique actual contour sources required')
    pose_sources = indexed(read_jsonl(PARENT/'all_manual_sources.jsonl'))
    require(len(pose_sources) == 482 and set(records) == set(pose_sources) | set(gallery['current'])
        and not set(pose_sources) & set(gallery['current']), 'Actual 482 pose / 1002 gallery source partition differs')
    require('bf59aa3d7d604db6fe49debd' not in records, 'Frozen quarantined source resurrected')
    def no_secrets(value):
        if isinstance(value,dict):
            require(not {'access_key','access_code','ruler','pose','selected_faces','current_payload_raw','configuration_sha256'} & value.keys(),
                'Source catalog contains access/configuration/pose/private raw payload')
            for item in value.values():
                no_secrets(item)
        elif isinstance(value,list):
            for item in value:
                no_secrets(item)
    no_secrets(catalog)
    for key,old in pose_sources.items():
        value,last = exact_latest(pose,key)
        require(pose['current'][key][1] == old['current_payload_raw'] and value == old['current_payload'], 'Protected 482 pose payload changed')
        unconfirmed(value)
        row = rows[key]
        require(not original_rectangle(value['polygon'],row), 'Original box counted as pose contour')
        for field in ('target_key','frame_id','split','group_id','box_index','bbox','size','policy_id','source_pcd_sha256'):
            require(value['source'][field] == row[field], 'Pose source formal identity differs')
        human = [e for e in pose['histories'][key] if e[0] == old['human_event_id']]
        require(len(human) == 1 and human[0][2] == old['human_event_payload_raw']
            and human[0][3]['polygon'] == value['polygon'] and human[0][3].get('event_kind') not in
            ('unit_normalization','axes_normalization','mask_template_proposal'), 'Pose source actual human event differs')
        expected = dict(target_key=key,row=row,polygon=value['polygon'],origin='saved_pose',revision=value['revision'],
            payload_sha256=sha(value),event_raw_sha256=raw_sha(human[0][2]),initial_polygon=None,batch='pose_manual_no_machine_pair')
        equal(records[key],expected,'pose_source.'+key)
    event_audit, references = [],{}
    for event_id,key,revision,raw in gallery['events']:
        value = loads(raw)
        require(key in rows, 'Foreign gallery source')
        reference = resolve_event(value,publications,registry)
        check_gallery_event(value,rows[key],reference,publications[value['proposal_publication']],formal_sha,evidence)
        references[event_id] = reference
        event_audit.append(dict(event_id=event_id,target_key=key,revision=revision,payload_raw_sha256=raw_sha(raw),
            publication=value['proposal_publication'],base_proposal_sha256=value['base_proposal_id'],source_registry=reference['kind']))
    require(gallery['histories'].keys() == gallery['current'].keys(), 'Gallery history/current denominator differs')
    for key in gallery['current']:
        value,last = exact_latest(gallery,key)
        first = gallery['histories'][key][0]
        reference,row = references[first[0]],rows[key]
        require(not original_rectangle(value['polygon'],row), 'Original rectangle counted as gallery contour')
        expected = dict(target_key=key,row=row,polygon=value['polygon'],origin='saved_gallery',revision=value['revision'],
            payload_sha256=sha(value),event_raw_sha256=raw_sha(last[2]),initial_polygon=first[3]['proposal_polygon'],
            batch='prelabel_'+reference['namespace'] if reference['kind'] == 'independent_preannotation' else 'historical_gallery',
            initial_publication=first[3]['proposal_publication'],history_revisions=len(gallery['histories'][key]))
        equal(records[key],expected,'gallery_source.'+key)
    equal(read_jsonl(out/'private/event_binding_audit.jsonl'),event_audit,'raw_event_binding_audit')
    batches = Counter(r['batch'] for r in catalog if r['origin'] == 'saved_gallery')
    require(batches == {'historical_gallery':904,'prelabel_v1':37,'prelabel_v2':35,'prelabel_v3':26}, 'All1002 historical/task batch denominator differs')
    task_changes = Counter(ref['namespace'] for ref in registry.values()
        if records[ref['proposal']['target_key']]['polygon'] != ref['proposal']['polygon'])
    require(task_changes == {'v1':36,'v2':35,'v3':26}, 'Actual independent task changed/unchanged counts differ')
    changed_topology = [ref for ref in registry.values() if ref['namespace'] == 'v3'
        and len(ref['proposal']['polygon']) != len(records[ref['proposal']['target_key']]['polygon'])]
    require(len(changed_topology) == 1, 'Actual third-task single topology correction differs')
    corrected_key = changed_topology[0]['proposal']['target_key']
    require(len(changed_topology[0]['proposal']['polygon']) == 10 and len(records[corrected_key]['polygon']) == 8
        and len(gallery['histories'][corrected_key][-1][3]['polygon']) == 8,
        'Real10-point draft to8-point saved correction must not be coerced to frozen draft topology')
    return records


def check_descriptive(out, catalog, result):
    actual = indexed(read_jsonl(out/'descriptive_records.jsonl'))
    expected = []
    for record in catalog:
        if record['initial_polygon'] is None:
            continue
        row,key = record['row'],record['target_key']
        metric = independent_metrics(record['initial_polygon'],record['polygon'],row['size'],row['bbox']['height'])
        value = dict(target_key=key,class_id=row['final_class_id'],batch=record['batch'],split=row['split'],
            bbox_height=row['bbox']['height'],reference_revision=record['revision'],**metric)
        require(key in actual, 'Omitted first-proposal/latest replay: '+key)
        equal(actual[key],value,'independent_descriptive.'+key)
        expected.append(value)
    require(actual.keys() == {r['target_key'] for r in expected} and len(expected) == 1002, 'All1002 descriptive replay records required')
    description = dict(all=summary(expected),by_batch=stratified(expected,'batch'),by_class=stratified(expected,'class_id'),
        by_split=stratified(expected,'split'),comparison_is_annotation_revision_agreement_not_accuracy=True,
        historical_val_test_descriptive_only_no_model_selection=True)
    equal(read(out/'descriptive_summary.json'),description,'independent_descriptive_summary')
    equal(result['descriptive_summary'],description,'result_descriptive_summary')
    for batch,median in (('historical_gallery',.9684),('prelabel_v1',.9033),('prelabel_v2',.8648),('prelabel_v3',.6507)):
        require(abs(description['by_batch'][batch]['iou_median']-median) < .0002, 'Observed batch median changed unexpectedly: '+batch)
    return expected,description


def check_component_links(links, sources, components, evidence):
    """Check the recorded union forest as evidence, not merely its final labels."""
    parent = {key:key for key in sources}
    def find(key):
        while parent[key] != key:
            parent[key] = parent[parent[key]]
            key = parent[key]
        return key
    require(len(links) == len(sources)-len(set(components.values())), 'Component union forest length differs')
    for link in links:
        left,right,reason = link['left'],link['right'],link['reason']
        require(left in sources and right in sources and left != right, 'Unknown/self component link')
        a,b = sources[left]['row'],sources[right]['row']
        if reason in ('group','frame','pcd'):
            field = {'group':'group_id','frame':'frame_id','pcd':'source_pcd_sha256'}[reason]
            valid = a[field] == b[field]
        elif reason == 'image':
            valid = np.array_equal(evidence.image(a['frame_id']),evidence.image(b['frame_id']))
        elif reason == 'native_crop_near_duplicate_dhash_and_ncc':
            valid = (a['final_class_id'] == b['final_class_id'] and a['policy_id'] == b['policy_id']
                and (image_dhash(evidence.image(a['frame_id']),a['bbox']) ^ image_dhash(evidence.image(b['frame_id']),b['bbox'])).bit_count() <= 2
                and ncc(evidence.patch(left),evidence.patch(right)) >= .995)
        else:
            valid = False
        require(valid and components[left] == components[right], 'Fabricated component link evidence: '+reason)
        aa,bb = find(left),find(right)
        require(aa != bb, 'Recorded component forest contains a cycle/duplicate union')
        parent[aa] = bb
    groups = defaultdict(list)
    for key in sources:
        groups[find(key)].append(key)
    actual = {key:sha(sorted(keys)) for keys in groups.values() for key in keys}
    equal(actual,components,'recorded_component_union_forest')


def check_static_isolation():
    """Audit explicit numerical API boundary; not a proof of all possible Python flows."""
    path = PROJECT/NEW_CODE[0]
    tree = ast.parse(path.read_text())
    functions = {node.name:node for node in tree.body if isinstance(node,(ast.FunctionDef,ast.AsyncFunctionDef))}
    require({'visible_input','predict','evaluate_train','eligible_reference'} <= functions.keys(), 'Missing explicit isolation boundary')
    visible = functions['visible_input']
    returns = [node for node in ast.walk(visible) if isinstance(node,ast.Return)]
    require(len(returns) == 1 and isinstance(returns[0].value,ast.Call)
        and isinstance(returns[0].value.func,ast.Name) and returns[0].value.func.id == 'dict', 'Target numerical input must be an explicit allowlist')
    require({kw.arg for kw in returns[0].value.keywords} == {'image','box','class_id','policy_id','known_view'}, 'Target numeric API exposes label/pose/identity/group')
    predict = functions['predict']
    require([arg.arg for arg in predict.args.args][:2] == ['target_input','references'], 'Unexpected numerical prediction API')
    accessed = {node.slice.value for node in ast.walk(predict) if isinstance(node,ast.Subscript)
        and isinstance(node.value,ast.Name) and node.value.id == 'target_input' and isinstance(node.slice,ast.Constant)}
    require(accessed <= {'image','box','class_id','policy_id','known_view'}, 'Target annotation/identity read by fitter')
    require(not any(isinstance(node,ast.Call) and isinstance(node.func,ast.Name)
        and node.func.id in ('compare_polygons','release_decision') for node in ast.walk(predict)), 'Target truth used for prediction/method selection')
    fitter = ast.parse((PROJECT/NEW_CODE[2]).read_text())
    for node in ast.walk(fitter):
        if isinstance(node,ast.ImportFrom):
            require(node.module not in ('mask_quality_recovery','mask_quality_metrics'), 'Numerical fitter imports scoring/target catalog')
        if isinstance(node,ast.Call) and isinstance(node.func,ast.Attribute):
            require(node.func.attr not in ('save_annotation','save_review','backup','connect'), 'Numerical fitter accesses a human store')
    return dict(target_numeric_input_fields=['image','box','class_id','policy_id','known_view'],
        static_api_allowlist_verified=True,static_check_is_not_general_python_information_flow_proof=True)


def check_predictions(out, catalog, components, evidence, settings, result):
    predictions = indexed(read_jsonl(out/'offline_predictions.jsonl'))
    scores = indexed(read_jsonl(out/'train_oof_metrics.jsonl'))
    targets = {r['target_key']:r for r in catalog if r['row']['split'] == 'train'}
    require(predictions.keys() == targets.keys(), 'New prediction inventory must contain all and only train sources')
    independent_scores = []
    for key,target in targets.items():
        item,row = predictions[key],target['row']
        require(item['target_key'] == key and item['class_id'] == row['final_class_id'] and item['split'] == 'train'
            and item['fold'] == fold_for(components[key]) and item['component'] == components[key], 'Target train/fold/class identity differs')
        require(all(item.get(field) is False for field in ('production_saved','human_confirmed','automatic_training_eligible','robot_command_eligible')),
            'Offline candidate promoted to human/production/training')
        options,budget = reference_budget(target,catalog,components,evidence,settings['numerical_settings']['maximum_references'])
        require(item['compatible_source_count'] == len(options), 'Compatible source denominator differs')
        trials = item['diagnostics']['attempts']
        require([trial['reference_key'] for trial in trials] == [r['target_key'] for r in budget], 'Image-only ranking / independent top3 reference budget differs')
        for trial,source in zip(trials,budget):
            require(eligible(target,source,components) and trial['independent_component'] == components[source['target_key']]
                and trial['reference_payload_sha256'] == source['payload_sha256'], 'Unsafe source split/class/view/policy/component or source payload')
        replayed,replayed_diagnostic = replay_prediction(target,budget,evidence,settings['numerical_settings'],components)
        equal(item['diagnostics'],replayed_diagnostic,'independent_registration_native_consensus.'+key)
        equal(item['polygon'],replayed,'independent_fitted_polygon.'+key)
        candidate = item['polygon']
        require((candidate is not None) == (item['status'] == 'offline_candidate_pending_quality_evaluation')
            and (candidate is not None) == (key in scores), 'Candidate/abstention/metric partition differs')
        if candidate is None:
            require(item['status'] == 'abstained' and item['diagnostics']['rejection'] is not None, 'False zero-candidate acceptance')
            continue
        require(row['final_class_id'] != 14 and row.get('viewpoint_candidate') not in (None,'unknown'), 'Ambiguous surface or unknown view accepted')
        metric = independent_metrics(candidate,target['polygon'],row['size'],row['bbox']['height'])
        metric.update(target_key=key,class_id=row['final_class_id'],bbox_height=row['bbox']['height'],fold=item['fold'])
        equal(scores[key],metric,'independent_train_metric.'+key)
        independent_scores.append(metric)
    require(scores.keys() <= targets.keys(), 'Val/test prediction score or unknown metric source')
    equal(result['train_metrics'],summary(independent_scores),'independent_train_summary')
    equal(result['by_class'],stratified(independent_scores,'class_id'),'independent_train_by_class')
    equal(result['release_gate'],quality_gate(independent_scores,len(targets)),'independent_release_gate')
    require(result['train_oof_targets'] == len(targets) and result['train_oof_candidates'] == len(independent_scores), 'Train coverage/count differs')
    equal(result['rejections'],dict(Counter(item['diagnostics']['rejection'] or 'accepted' for item in predictions.values())),'rejection_counts')
    coverage = {str(cls):dict(total=sum(p['class_id'] == cls for p in predictions.values()),
        accepted=sum(s['class_id'] == cls for s in independent_scores)) for cls in sorted({r['class_id'] for r in predictions.values()})}
    equal(result['by_class_delivery'],coverage,'per_class_delivery_including_zeros')
    equal(result['compatible_reference_targets'],sum(p['compatible_source_count'] > 0 for p in predictions.values()),'compatible_target_count')
    equal(result['attempted_source_pairs'],sum(len(p['diagnostics']['attempts']) for p in predictions.values()),'attempted_pair_count')
    reasons = Counter((trial.get('native_fit') or trial['alignment'])['rejection'] or 'fitted'
        for p in predictions.values() for trial in p['diagnostics']['attempts'])
    equal(result['pair_rejections'],dict(reasons),'all_actual_pair_rejections')
    return predictions,independent_scores


def frozen_legacy_attempt(target, templates, evidence):
    # This is the pinned, unchanged historical engine, never the new producer.
    # Its old >4-vertex authorization is independently enforced before this call.
    sys.path.insert(0,str(PROJECT))
    from pallet.mask_template_continue import attempt_target
    return attempt_target(target['target_key'],target['row'],templates,evidence)


def check_legacy(out, catalog, components, evidence, result):
    predictions = indexed(read_jsonl(out/'legacy_predictions.jsonl'))
    recorded_scores = indexed(read_jsonl(out/'legacy_train_oof_metrics.jsonl'))
    targets = {r['target_key']:r for r in catalog if r['row']['split'] == 'train'}
    require(predictions.keys() == targets.keys(), 'Matched legacy baseline must cover exactly all train targets')
    scores = []
    for key,target in targets.items():
        _,budget = reference_budget(target,[r for r in catalog if len(r['polygon']) > 4],components,evidence)
        templates = {r['target_key']:dict(row=r['row'],polygon=r['polygon']) for r in budget}
        require(all(eligible(target,source,components) and len(source['polygon']) > 4 for source in budget), 'Legacy quad/split/view/component qualification changed')
        accepted,trials,outcome = frozen_legacy_attempt(target,templates,evidence)
        item,row = predictions[key],target['row']
        expected = dict(target_key=key,class_id=row['final_class_id'],split='train',fold=fold_for(components[key]),component=components[key],
            status='offline_legacy_candidate' if accepted else 'abstained',polygon=accepted['polygon'] if accepted else None,
            eligible_reference_keys=list(templates),attempts=trials,rejection=None if accepted else outcome['reason'],
            method='frozen_original_with_explicit_three_independent_reference_budget_not_historical_full_scan')
        equal(item,expected,'independent_frozen_legacy_replay.'+key)
        require((accepted is not None) == (key in recorded_scores), 'Legacy accepted-score partition differs')
        if accepted is not None:
            metric = independent_metrics(accepted['polygon'],target['polygon'],row['size'],row['bbox']['height'])
            metric.update(target_key=key,class_id=row['final_class_id'],bbox_height=row['bbox']['height'],fold=expected['fold'])
            equal(recorded_scores[key],metric,'independent_legacy_metric.'+key)
            scores.append(metric)
    require(recorded_scores.keys() <= targets.keys(), 'Legacy scores contain held-out val/test or unknown targets')
    stats = dict(method='frozen_original_three_independent_reference_budget',count=len(targets),accepted=len(scores),
        delivery_rate=len(scores)/len(targets) if targets else 0.,quality=summary(scores),
        rejections=dict(Counter(p['rejection'] or 'accepted' for p in predictions.values())))
    equal(result['baseline'],stats,'matched_frozen_legacy_baseline')
    return predictions,scores


def check_settings(settings):
    require(settings['execution_domain'] == 'formal_train_only' and settings['evaluation'] == 'deterministic_connected_group_3fold_out_of_fold', 'New method must be train-only component 3fold OOF')
    for field in ('unknown_view_is_not_compatible','retrieval_uses_only_native_image_class_bbox_sensor_policy_and_known_view',
            'original_propagation_qualification_unchanged','experimental_four_point_boundary_fit_is_not_old_template_authorization',
            'reference_polygons_are_saved_research_annotations_not_GT'):
        require(settings.get(field) is True, 'Missing fail-closed setting: '+field)
    for field in ('original_engines_modified','automatic_publication','validation_or_test_labels_used_by_new_algorithm','pose_model_training'):
        require(settings.get(field) is False, 'Unsafe setting: '+field)
    require(settings['ambiguous_surface_classes'] == [14], 'Surface-ambiguous class14 must be completely blocked')
    equal(settings['near_duplicate'],dict(dhash_bits=128,hamming_max=2,normalized_patch_ncc_min=.995,same_class_and_policy_required=True),'near_duplicate_settings')
    equal(settings['quality_gate'],dict(minimum_accepted=10,iou_median_min=.95,fraction_iou_at_least_09_min=.9,
        boundary_max='HD95 <= max(2 native pixels, .10 * target bbox height)'),'predeclared_quality_gate')
    numbers = settings['numerical_settings']
    require(numbers['initial_ncc_min'] == .55 and numbers['ecc_min'] == .84 and numbers['boundary_ncc_min'] == .78
        and numbers['maximum_references'] == 3 and numbers['minimum_independent_references'] == 2
        and numbers['consensus_native_pixels_max'] == 1.5, 'Original photometric gates / independent consensus budget relaxed')
    tree = ast.parse((PROJECT/NEW_CODE[2]).read_text())
    classes = [node for node in tree.body if isinstance(node,ast.ClassDef) and node.name == 'Settings']
    require(len(classes) == 1, 'Settings dataclass declaration missing')
    declared = {node.target.id:ast.literal_eval(node.value) for node in classes[0].body if isinstance(node,ast.AnnAssign)
        and isinstance(node.target,ast.Name)}
    equal(numbers,declared,'actual_code_declared_numerical_settings')


def check_report(out, result):
    page = (out/'report_zh.html').read_text(encoding='utf-8')
    require(all(text in page for text in ('不是正确率','不自动发布','train','val/test','Class14','四点','1484','1002')),
        'Report omits descriptive-only / abstention / four-point research / source scope')
    require(not any(token in page for token in ('access_key','current_payload_raw','human_event_payload_raw','selected_faces','ruler','fetch(')),
        'Quality report exposes private source data or an online action')
    gate = result['release_gate']
    require(str(result['train_oof_targets']) in page and str(result['train_oof_candidates']) in page
        and gate['state'] in page, 'Report omits actual denominator / zero-candidate gate disposition')
    require(not list(out.glob('*.zip')) and not (out/'proposals.jsonl').exists(), 'Offline audit unexpectedly became a publication')


def audit(out=OUTPUT):
    out = Path(out)
    cv2.setNumThreads(1)
    before = inventory(out)
    result,settings = read(out/'result.json'),read(out/'settings.json')
    require(result['state'] == 'completed_offline_not_published', 'Producer did not finish offline-only result')
    require(result['settings_sha256'] == digest(out/'settings.json'), 'Actual numerical settings changed')
    for name,expected in result['output_artifacts'].items():
        require(before.get(name) == expected, 'Producer output artifact changed: '+name)
    require(set(before)-{SEAL,FAILURE,'result.json'} == set(result['output_artifacts']), 'Unbound / omitted output artifact')
    require(result['code_sha256'].keys() == set(NEW_CODE), 'New numerical/replay/metric code pins incomplete')
    for name,expected in result['code_sha256'].items():
        require(digest(member(PROJECT,name)) == expected, 'New code changed after evaluation: '+name)
    code_before = {name:digest(PROJECT/name) for name in (*NEW_CODE,'scripts/verify_mask_quality_recovery.py','tests/test_verify_mask_quality_recovery.py')}
    for name,expected in PARENT_PINS.items():
        require(digest(PARENT/name) == expected, 'Pinned immutable parent changed: '+name)
    parent = read(PARENT/'manifest.json')
    for field,name in (('all_manual_sources_sha256','all_manual_sources.jsonl'),
            ('snapshot_sha256','reviews_snapshot.sqlite3'),('gallery_snapshot_sha256','gallery_reviews_snapshot.sqlite3')):
        require(parent[field] == digest(PARENT/name), 'Pinned parent source/snapshot bytes changed: '+name)
    for name,expected in parent['frozen_code_sha256'].items():
        require(digest(member(PROJECT,name)) == expected, 'Frozen old engine / qualification changed: '+name)
    check_settings(settings)
    static = check_static_isolation()
    configuration = inventory(PROJECT/'configs')
    equal(result['configuration_sha256'],configuration,'unchanged_configurations')
    formal,rows,frames = load_formal(parent)
    protected = read(out/'private/protected_formal_before.json')
    require(len(protected) == 20768 and protected == read(PARENT/'protected_formal_after.json'), 'Protected20768 baseline differs')
    require(protected_inventory(formal) == protected, 'Protected20768 formal files added/removed/changed')
    pose = read_store(out/'private/pose_snapshot.sqlite3','pose',True)
    gallery = read_store(out/'private/gallery_snapshot.sqlite3','gallery',True)
    for name in ('private','private/pose_snapshot.sqlite3','private/gallery_snapshot.sqlite3','private/source_catalog.jsonl'):
        require((out/name).stat().st_mode & 0o077 == 0, 'Raw human sources / source catalog are not private: '+name)
    require((len(pose['current']),len(pose['events']),len(gallery['current']),len(gallery['events'])) == (4946,11926,1002,1088),
        'Actual4946/11926 pose and1002/1088 gallery evidence required')
    fingerprints = dict(pose=pose['fingerprint'],gallery=gallery['fingerprint'])
    equal(result['source_fingerprints'],fingerprints,'immutable_snapshot_fingerprints')
    for kind,path in (('pose',POSE),('gallery',GALLERY)):
        equal(read_store(path,kind)['fingerprint'],fingerprints[kind],'unchanged_live_'+kind)
    old_pose = read_store(PARENT/'reviews_snapshot.sqlite3','pose',True)
    old_gallery = read_store(PARENT/'gallery_reviews_snapshot.sqlite3','gallery',True)
    require(pose == old_pose, 'All original pose records/events changed')
    require(gallery['events'][:len(old_gallery['events'])] == old_gallery['events']
        and all(gallery['current'].get(key) == value for key,value in old_gallery['current'].items()), 'Original gallery raw history/current changed')
    require(len(gallery['events'])-len(old_gallery['events']) == 27 and len(gallery['current'])-len(old_gallery['current']) == 26,
        'Real third-task26 sources/27 save events required')
    formal_sha = parent['formal_manifest_sha256']
    binding = loads(pose['metadata']['binding'])
    require(binding['formal_manifest_sha256'] == formal_sha == gallery['metadata']['formal_manifest_sha256']
        and binding['formal_annotations_sha256'] == digest(formal/'annotations/frames.jsonl')
        and binding['formal_frames_sha256'] == digest(formal/'frames.jsonl'), 'SQLite formal source binding differs')
    evidence = NativeEvidence(formal,rows,frames)
    publications,registry,task_pins = registries(rows,formal_sha,pose)
    catalog = read_jsonl(out/'private/source_catalog.jsonl')
    sources = check_catalog(out,catalog,pose,gallery,rows,publications,registry,formal_sha,evidence)
    source_summary = result['source_summary']
    for field,expected in dict(pose_contours=482,gallery_contours=1002,total_contours=1484,pose_store_saved=4946,
            pose_store_events=11926,gallery_events=1088,old_review_only_rectangles=5,pending_original_rectangles=2).items():
        equal(source_summary[field],expected,'source_summary.'+field)
    equal(source_summary['task_inputs'],task_pins,'task_input_fingerprints')
    equal(source_summary['publication_inputs'],{str(version):{field:publication[field] for field in
        ('manifest_sha256','proposals_sha256','verification_sha256')} for version,publication in publications.items()},'all_actual_publication_fingerprints')
    require(source_summary['all_raw_event_source_bindings_verified'] is True
        and source_summary['rectangle_and_conversation_records_not_contour_references'] is True, 'Source provenance claims missing')
    descriptive,description = check_descriptive(out,catalog,result)
    components = independent_components(catalog,evidence)
    recorded_components = read(out/'private/leakage_components.json')
    equal(recorded_components['by_target'],components,'independent_components_all1484')
    check_component_links(recorded_components['links'],sources,components,evidence)
    require(result['independent_components'] == len(set(components.values())), 'Independent component count differs')
    predictions,scores = check_predictions(out,catalog,components,evidence,settings,result)
    legacy_predictions,legacy_scores = check_legacy(out,catalog,components,evidence,result)
    check_report(out,result)
    for field in ('automatic_publication','neural_network_training'):
        require(result.get(field) is False, 'Unsafe result claim: '+field)
    for field in ('production_annotation_writes','configuration_writes','service_writes'):
        equal(result[field],0,'zero_writes.'+field)
    require(result['protected_formal_files'] == 20768 and result['fixed_denominator'] == 12344
        and result['all_protected_sources_unchanged'] is True, 'Protected denominator/preservation claims differ')
    require(inventory(out) == before and inventory(PROJECT/'configs') == configuration, 'Protected output/config changed during audit')
    require(protected_inventory(formal) == protected, 'Formal protected input changed during audit')
    for kind,path in (('pose',POSE),('gallery',GALLERY)):
        equal(read_store(path,kind)['fingerprint'],fingerprints[kind],'live_source_changed_during_audit.'+kind)
    for name,expected in evidence.used.items():
        require(digest(member(formal,name)) == expected, 'Native image changed during verification: '+name)
    for name,expected in code_before.items():
        require(digest(PROJECT/name) == expected, 'Code changed during independent verification: '+name)
    for name,expected in parent['frozen_code_sha256'].items():
        require(digest(member(PROJECT,name)) == expected, 'Frozen original code changed during independent verification: '+name)
    for name,expected in PARENT_PINS.items():
        require(digest(PARENT/name) == expected, 'Frozen parent changed during independent verification: '+name)
    return dict(state='passed',audit_scope='independent_raw_source_metrics_components_reference_budget_and_all_numerical_attempts',
        full_registration_replay_performed=True,native_line_support_and_consensus_independently_replayed=True,
        limitations=['Saved human contours are unconfirmed research references, not independent ground truth.',
            'Known coarse viewpoint and reference topology do not establish physical surface identity.',
            'The implemented line test does not explicitly distinguish competing strong parallel transitions.',
            'All new candidates abstained: accepted native-fit/consensus boundary quality is not established on real data.',
            'Static numerical API allowlist checks are not a general Python information-flow proof.'],
        result_sha256=digest(out/'result.json'),settings_sha256=digest(out/'settings.json'),code_sha256=code_before,
        source_fingerprints=fingerprints,configuration_sha256=configuration,source_catalog_sha256=digest(out/'private/source_catalog.jsonl'),
        frozen_code_sha256=parent['frozen_code_sha256'],parent_pins=PARENT_PINS,native_images_verified=len(evidence.used),
        all_current_pose_records_preserved=4946,all_pose_events_preserved=11926,validated_pose_contours=482,
        gallery_contours_verified=1002,gallery_events_verified=1088,total_unique_sources=1484,
        first_proposal_vs_latest_replays_verified=len(descriptive),descriptive_summary=description,
        independent_components=len(set(components.values())),source_split_counts=dict(Counter(r['row']['split'] for r in sources.values())),
        train_oof_targets_verified=len(predictions),train_oof_candidates_verified=len(scores),release_gate=quality_gate(scores,len(predictions)),
        new_method_source_pairs_replayed=result['attempted_source_pairs'],pair_rejections=result['pair_rejections'],
        successful_native_fits=sum((a.get('native_fit') or {}).get('rejection') is None and 'native_fit' in a
            for p in predictions.values() for a in p['diagnostics']['attempts']),
        accepted_consensus_cases_observed=len(scores),accepted_candidate_quality_established=False if not scores else None,
        legacy_oof_targets_replayed=len(legacy_predictions),legacy_oof_candidates_verified=len(legacy_scores),legacy_baseline=result['baseline'],
        validation_test_new_predictions_or_scores=0,static_isolation=static,
        protected_formal_files_verified=20768,fixed_formal_targets=12344,protected_input_changes=0,
        production_annotation_writes=0,configuration_writes=0,service_writes=0,
        production_publication_authorized=False,verification_does_not_authorize_publication=True,
        verified_at=datetime.now(timezone.utc).isoformat())


def seal(out=OUTPUT, auditor=audit):
    out = Path(out)
    require(not out.is_symlink() and out.is_absolute() and out == out.resolve()
        and out.parent.resolve() == (PROJECT/'outputs').resolve()
        and re.fullmatch(r'pallet_mask_quality_recovery_v[1-9][0-9]*',out.name) is not None,
        'Only an exact fresh versioned recovery output may receive verification')
    require((out/'result.json').is_file() and not (out/'result.json').is_symlink(), 'Producer result is not complete')
    for name in (SEAL,FAILURE):
        path = out/name
        require(not path.exists() and not path.is_symlink(), 'Never overwrite an existing verification/failure record')
    try:
        result = auditor(out)
        require(result.get('state') == 'passed', 'Auditor did not complete successfully')
    except Exception as error:
        failure = dict(state='failed',error_type=type(error).__name__,error=str(error),
            result_sha256=digest(out/'result.json'),verification_script_sha256=digest(__file__),
            production_publication_authorized=False,failed_at=datetime.now(timezone.utc).isoformat())
        with (out/FAILURE).open('xb') as stream:
            stream.write(canonical(failure)+b'\n')
        (out/FAILURE).chmod(0o600)
        raise
    with (out/SEAL).open('xb') as stream:
        stream.write(canonical(result)+b'\n')
    (out/SEAL).chmod(0o600)
    return result


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--directory','--output',dest='directory',type=Path,default=OUTPUT)
    args = parser.parse_args()
    verified = seal(args.directory.resolve() if not args.directory.is_symlink() else args.directory)
    print(json.dumps({key:verified[key] for key in ('state','audit_scope','full_registration_replay_performed',
        'train_oof_targets_verified','train_oof_candidates_verified','release_gate')},ensure_ascii=False),flush=True)
