"""Independently audit the refined-gallery expansion, then issue its publication seal.

Run only after generation has finished. All inputs, including both SQLite stores,
are opened read-only. The only writes are validation_report.json and, after every
check passes, verification.json in --output. No services or active config change.
"""
from __future__ import annotations

import argparse
from collections import Counter
from contextlib import closing
from datetime import datetime, timezone
import hashlib
from html.parser import HTMLParser
import json
import math
from numbers import Real
from pathlib import Path
import re
import sqlite3
import sys

import cv2
import numpy as np
from PIL import Image

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest, load_dataset, protected_snapshot, target_key
from pallet.mask_boundary_review import affine_reference, beam_boundary_audit
from pallet.mask_template_transfer import ImageEvidence, annotation_inventory, compatible, correlate
from pallet.mask_template_transfer import transfer, unchanged_rectangle
from pallet.pose_review_geometry import canonical, polygon_value, raster_mask

CLASSES = (1, 2, 9, 10, 15)
LIMIT = 50
QUARANTINE = 'bf59aa3d7d604db6fe49debd'
ORIGIN = 'mask_gallery_human_revision'
PARENT = PROJECT / 'outputs/pallet_mask_template_bulk_v5'
# The already-published v3 baseline, not hashes supplied by the new generator.
PINNED = {
    'proposals.jsonl': 'a589db3bd183d732d3336d05051fda6b84a5ade79deca23b5adce9512ab83c8d',
    'manifest.json': '6e2b625e8eaf38f3dfc0b1ed0ea4dabd16780594d3d25850a9f9811dcd31c3b8',
    'verification.json': '3036186bd226db161f18630d6cb7740f3bf4e93766009dad5403e7cbfa5a49ef',
}
ORIGINAL_CODE = {
    'pallet/mask_template_transfer.py': '62ac43c84e63a5f474b6e33f309dc40b0a383073de06999f8b94f91a01f35292',
    'pallet/mask_boundary_review.py': 'b6d1b7bb60c610f10b34f478bc30caf4e56fee0d4013a4663d7ba3b58413d357',
}
ATOL = 1e-6


def require(condition, message):
    # Never use assert: a publication audit must also work under python -O.
    if not condition:
        raise ValueError(message)


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


def read_json(path):
    return json.loads(Path(path).read_text())


def read_jsonl(path):
    return [json.loads(line) for line in Path(path).read_text().splitlines() if line.strip()]


def indexed(values, field='target_key'):
    result = {value[field]: value for value in values}
    require(len(result) == len(values), f'Duplicate {field}')
    return result


def equal_metrics(actual, expected, name='metrics'):
    """Compare every leaf, including affine matrices and boundary section arrays."""
    if isinstance(expected, dict):
        require(isinstance(actual, dict) and actual.keys() == expected.keys(), f'{name}: keys differ')
        for key in expected:
            equal_metrics(actual[key], expected[key], f'{name}.{key}')
    elif isinstance(expected, list):
        require(isinstance(actual, list) and len(actual) == len(expected), f'{name}: length differs')
        for i, (a, e) in enumerate(zip(actual, expected)):
            equal_metrics(a, e, f'{name}[{i}]')
    elif isinstance(expected, Real) and not isinstance(expected, bool):
        require(isinstance(actual, Real) and not isinstance(actual, bool)
                and math.isfinite(actual) and math.isfinite(expected)
                and math.isclose(actual, expected, rel_tol=0, abs_tol=ATOL), f'{name}: numeric mismatch')
    else:
        require(type(actual) is type(expected) and actual == expected, f'{name}: value differs')


def read_store(path, kind, frozen=False):
    """Read a consistent transaction without opening a writable review application."""
    path = Path(path).resolve()
    require(path.is_file(), f'Missing {kind} database: {path}')
    if frozen:
        wal = Path(str(path) + '-wal')
        require(not wal.exists() or wal.stat().st_size == 0, f'Uncheckpointed snapshot: {path}')
    uri = path.as_uri() + ('?mode=ro&immutable=1' if frozen else '?mode=ro')
    table = {'pose': 'annotations', 'gallery': 'reviews'}[kind]
    with closing(sqlite3.connect(uri, uri=True)) as db:
        db.execute('PRAGMA query_only=ON')
        db.execute('BEGIN')
        require(db.execute('PRAGMA quick_check').fetchone()[0] == 'ok', f'{kind} DB integrity failed')
        metadata = dict(db.execute('SELECT key,value FROM metadata'))
        events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        current_rows = list(db.execute(f'SELECT target_key,revision,payload FROM {table}'))
        current, latest = {}, {}
        for event_id, key, revision, raw in events:
            value = json.loads(raw)
            require(value['target_key'] == key and value['revision'] == revision,
                    f'{kind} event identity differs: {event_id}')
            require(key not in latest or revision > latest[key][0], f'{kind} event order differs: {key}')
            latest[key] = (revision, value)
        for key, revision, raw in current_rows:
            value = json.loads(raw)
            require(value['target_key'] == key and value['revision'] == revision,
                    f'{kind} current identity differs: {key}')
            require(key in latest and latest[key][0] == revision
                    and canonical(latest[key][1]) == canonical(value), f'{kind} current is not latest event: {key}')
            current[key] = value
        require(set(current) == set(latest), f'{kind} event/current coverage differs')
    return dict(path=path, metadata=metadata, events=events, current=current,
                history_keys={event[1] for event in events})


def preserve_events(frozen, live):
    live_events = {event[0]: event for event in live['events']}
    for event in frozen['events']:
        require(live_events.get(event[0]) == event,
                f'Original {frozen["path"].name} event changed or disappeared: {event[0]}')
    require(frozen['metadata'] == live['metadata'], 'Review database metadata changed')


def pose_status(store, rows):
    """Preserve mask history, not unchanged-rectangle pose/ruler-only saves."""
    edited = set()
    for _, key, _, raw in store['events']:
        require(key in rows, f'Unknown pose history target: {key}')
        value = json.loads(raw)
        polygon = value.get('polygon')
        if polygon and not unchanged_rectangle(polygon, rows[key]):
            edited.add(key)
    status = {}
    for key in rows:
        value = store['current'].get(key, {})
        status[key] = ('deleted' if value.get('state') == 'deleted' else
                       'human_mask_history_protected' if key in edited else
                       'deferred_or_confirmed_protected' if value.get('state') == 'deferred'
                       or value.get('human_mask_confirmed') or value.get('human_pose_confirmed') else 'eligible')
    return status


def require_target_eligible(key, pose, gallery, rows):
    require(key != QUARANTINE, 'Quarantined target selected')
    require(key not in gallery['history_keys'] and key not in gallery['current'],
            f'Gallery history target selected: {key}')
    require(pose[key] == 'eligible', f'Protected pose target selected: {key}')
    require(rows[key]['final_class_id'] in CLASSES, f'Unapproved class selected: {key}')


def count_frames(proposals):
    require(len({p['source']['frame_id'] for p in proposals}) == len(proposals), 'Repeated frame across classes')
    require({p['class_id'] for p in proposals} <= set(CLASSES), 'Unexpected proposal class')
    result = {}
    for cls in CLASSES:
        items = [p for p in proposals if p['class_id'] == cls]
        frames = {p['source']['frame_id'] for p in items}
        require(len(frames) == len(items), f'Class {cls}: repeated frame')
        require(len(frames) <= LIMIT, f'Class {cls}: exceeds {LIMIT} cumulative frames')
        result[str(cls)] = dict(proposals=len(items), distinct_frames=len(frames), shortfall=LIMIT-len(frames))
    return result


def find_snapshots(out, manifest):
    """Discover by schema, not mutable filenames; reject ambiguous snapshots."""
    found = {}
    for path in sorted(out.glob('*.sqlite3')):
        with closing(sqlite3.connect(path.resolve().as_uri() + '?mode=ro&immutable=1', uri=True)) as db:
            tables = {r[0] for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
        for kind, table in (('pose', 'annotations'), ('gallery', 'reviews')):
            if {table, 'events', 'metadata'} <= tables:
                require(kind not in found, f'Ambiguous {kind} snapshot')
                found[kind] = path
    require(set(found) == {'pose', 'gallery'}, 'Need exactly one pose and one gallery snapshot')
    for kind, path in found.items():
        fields = ('snapshot_sha256', 'pose_snapshot_sha256') if kind == 'pose' else ('gallery_snapshot_sha256', 'mask_gallery_snapshot_sha256')
        values = [manifest[field] for field in fields if field in manifest]
        require(values and all(value == digest(path) for value in values), f'{kind} snapshot hash mismatch')
        require(path.stat().st_mode & 0o222 == 0, f'{kind} snapshot must have no write permission bits')
        backup = out/'backups'/path.name
        require(backup.is_file() and backup.stat().st_mode & 0o222 == 0
                and digest(backup) == digest(path), f'{kind} read-only backup differs')
    return found


def formal_rows(formal, classification):
    annotations, sources = load_dataset(formal)
    rows = indexed(read_jsonl(classification))
    identities = {target_key(a['frame_id'], i): (a, i, box)
                  for a in annotations for i, box in enumerate(a['boxes'])}
    require(set(rows) == set(identities), 'Classification does not cover formal identities')
    for key, row in rows.items():
        annotation, i, box = identities[key]
        source = sources[annotation['frame_id']]
        require(row['box_index'] == i and row['bbox'] == box
                and all(row[field] == annotation[field] for field in ('frame_id', 'split', 'group_id')),
                f'Formal target identity differs: {key}')
        require(row['source_pcd_sha256'] == source['source_anchor_hash']
                and row['policy_id'] == source['sensor_policy']['policy_id'], f'Formal source differs: {key}')
        row['size'] = source['size']
    return rows, sources


def gallery_template(template, pose, gallery, rows, old, parent, formal_sha, snapshot_sha):
    key = template['row']['target_key']
    require(key in old and template['row'] == rows[key], f'Template formal identity differs: {key}')
    value = gallery['current'][key]
    require(value['event_kind'] == 'human_mask_gallery_revision' and value['human_edited'] is True,
            f'Template is not a saved gallery edit: {key}')
    source = {field: rows[key][field] for field in
              ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
    source.update(formal_manifest_sha256=formal_sha, image_sha256=digest(parent/'images'/f'{key}.png'))
    require(value['source'] == source and value['source_binding'] == sha(source), f'Gallery source binding differs: {key}')
    require(value['base_proposal_id'] == sha(old[key]) and value['proposal_publication'] == 3
            and value['proposal_polygon'] == old[key]['polygon'], f'Gallery base proposal differs: {key}')
    from pallet.mask_gallery_review import VERSION as EDITOR_VERSION
    require(value['binding'] == sha(dict(formal=formal_sha, proposals=PINNED['proposals.jsonl'], editor_version=EDITOR_VERSION)),
            f'Gallery publication binding differs: {key}')
    require(value['template_provenance'] == old[key]['template'], f'Gallery template history differs: {key}')
    polygon_value(value['polygon'], *rows[key]['size'])
    require(len(value['polygon']) == value['vertex_count'] > 4 and template['polygon'] == value['polygon'],
            f'Template polygon is not latest gallery polygon: {key}')
    for field in ('human_mask_confirmed', 'human_pose_confirmed', 'automatic_training_eligible',
                  'robot_command_eligible', 'pose_store_written', 'original_proposal_modified'):
        require(value[field] is False, f'Gallery review {field} must remain false: {key}')
    require(value['pose'] is None, f'Gallery review claims pose: {key}')
    expected = dict(target_key=key, revision=value['revision'], payload_sha256=sha(value),
                    source_binding=sha(source), polygon_sha256=sha(value['polygon']), snapshot_sha256=snapshot_sha)
    require(template['origin'] == ORIGIN and template['gallery_review'] == expected,
            f'Frozen gallery provenance differs: {key}')
    pose_value = pose['current'].get(key)
    require(template['revision'] == (pose_value['revision'] if pose_value else 0)
            and template['current_payload_sha256'] == (sha(pose_value) if pose_value else None),
            f'Pose template binding differs: {key}')
    require(template['human_polygon_revision'] == value['revision']
            and template['human_event_sha256'] == sha(value) and template['human_mask_confirmed'] is False,
            f'Gallery revision/event binding differs: {key}')
    return expected


def verify_assets(out, proposal, row, evidence):
    key = proposal['target_key']
    require(re.fullmatch('[0-9a-f]{24}', key) is not None, 'Unsafe target asset key')
    require(proposal['source'] == row and proposal['class_id'] == row['final_class_id'], f'Proposal source differs: {key}')
    require(len(proposal['polygon']) == proposal['vertex_count'] > 4, f'Invalid vertex count: {key}')
    polygon_value(proposal['polygon'], *row['size'])
    for field in ('human_mask_confirmed', 'automatic_training_eligible', 'robot_command_eligible', 'production_saved'):
        require(proposal[field] is False, f'Machine proposal {field} must be false: {key}')
    require(proposal['status'] == 'machine_draft_pending_visual_review', f'Machine status differs: {key}')
    for folder, ext in (('images', 'png'), ('masks', 'png'), ('labelme', 'json'), ('cases', 'png')):
        path = out/folder/f'{key}.{ext}'
        require(path.is_file() and path.resolve().is_relative_to(out), f'Missing/unsafe asset: {path}')
    with Image.open(out/'images'/f'{key}.png') as image:
        require(image.format == 'PNG' and image.mode == 'L' and image.size == tuple(row['size']), f'Native image differs: {key}')
        np.testing.assert_array_equal(np.asarray(image), evidence.image(row['frame_id']))
    with Image.open(out/'masks'/f'{key}.png') as mask:
        require(mask.format == 'PNG' and mask.mode == 'L' and mask.size == tuple(row['size']), f'Native mask differs: {key}')
        np.testing.assert_array_equal(np.asarray(mask), raster_mask(proposal['polygon'], *row['size']).astype(np.uint8)*255)
    with Image.open(out/'cases'/f'{key}.png') as image:
        image.verify()
    label = read_json(out/'labelme'/f'{key}.json')
    require(label['imageWidth'] == row['size'][0] and label['imageHeight'] == row['size'][1]
            and label['imagePath'] == f'../images/{key}.png' and label['imageData'] is None, f'LabelMe image differs: {key}')
    require(len(label['shapes']) == 1, f'LabelMe must contain exactly one object: {key}')
    shape = label['shapes'][0]
    require(shape['points'] == proposal['polygon'] and shape['shape_type'] == 'polygon'
            and shape['group_id'] == row['box_index'] and shape['label'] == 'pallet', f'LabelMe polygon differs: {key}')
    for flags in (label['flags'], shape['flags']):
        require(flags['human_confirmed'] is False and flags['machine_draft'] is True, f'LabelMe flags differ: {key}')
    require(label['mask_template_provenance'] == proposal, f'LabelMe provenance differs: {key}')


def recompute(proposal, template, evidence):
    key = proposal['target_key']
    polygon, metrics = transfer(evidence.patch(template['row']['target_key']), evidence.patch(key),
                                template['polygon'], template['row'], proposal['source'])
    require(polygon is not None, f'Original transfer rejected {key}: {metrics}')
    require(metrics['initial_ncc'] >= .55 and metrics['ecc'] >= .84 and metrics['boundary_ncc'] >= .78
            and max(metrics['forward_backward_error_patch_pixels']) <= 1.5, f'Original quality gates failed: {key}')
    np.testing.assert_allclose(polygon, proposal['polygon'], rtol=0, atol=ATOL)
    reference = affine_reference(dict(source=proposal['source'], metrics=metrics), template)
    guard = beam_boundary_audit(evidence.image(proposal['source']['frame_id']), polygon, reference)
    require(guard['state'] == 'passed', f'Original boundary gate rejected: {key}')
    metrics['boundary_guard'] = guard
    equal_metrics(proposal['metrics'], metrics, f'{key}.metrics')
    return metrics


def check_diversity(old, accepted, new, outcomes):
    """Independently replay deterministic group diversity and distinct-frame caps."""
    selected, frames = set(), {p['source']['frame_id'] for p in old.values()}
    for cls in CLASSES:
        previous = [p for p in old.values() if p['class_id'] == cls]
        groups = Counter(p['source']['group_id'] for p in previous)
        remaining = LIMIT-len(previous)
        pending = [p for p in accepted.values() if p['class_id'] == cls]
        while pending:
            candidate = min(pending, key=lambda p: (groups[p['source']['group_id']],
                            -p['metrics']['boundary_ncc'], -p['metrics']['ecc'], p['target_key']))
            pending.remove(candidate)
            key, frame = candidate['target_key'], candidate['source']['frame_id']
            if frame in frames:
                reason = 'accepted_duplicate_frame_not_selected'
            elif remaining == 0:
                reason = 'accepted_quota_full_not_selected'
            else:
                reason = 'new_proposal'
                frames.add(frame)
                groups[candidate['source']['group_id']] += 1
                selected.add(key)
                remaining -= 1
            require(outcomes[key]['reason'] == reason, f'Diversity disposition differs: {key}')
    require(selected == set(new), 'Selected candidates do not match diversity/quota policy')


def audit_attempts(out, rows, status, gallery, old, templates, accepted, outcomes, evidence):
    attempts = read_jsonl(out/'registration_attempts.jsonl')
    bytarget = {}
    for attempt in attempts:
        key = attempt['target_key']
        require(key in rows and attempt['class_id'] == rows[key]['final_class_id'], 'Attempt identity differs')
        bytarget.setdefault(key, []).append(attempt)
    eligible = set()
    for key, row in rows.items():
        protected = ('previously_quarantined' if key == QUARANTINE else 'already_published' if key in old else
                     'gallery_history_protected' if key in gallery['history_keys'] else status[key])
        if protected == 'eligible' and row['final_class_id'] not in CLASSES:
            protected = 'class_not_requested'
        if protected != 'eligible':
            require(outcomes[key]['reason'] == protected and key not in bytarget and key not in accepted,
                    f'Protected outcome/attempt differs: {key}')
            continue
        eligible.add(key)
        choices = [k for k, t in templates.items() if compatible(t['row'], row)]
        if not choices:
            require(outcomes[key]['reason'] == 'no_compatible_template' and key not in bytarget,
                    f'False no-compatible-template outcome: {key}')
            continue
        ranked = sorted(((correlate(evidence.patch(k), evidence.patch(key)), k) for k in choices), reverse=True)
        passing = [k for similarity, k in ranked if similarity >= .55]
        trials = bytarget.get(key, [])
        if key in accepted:
            source = accepted[key]['template']['target_key']
            require(source in passing, f'Accepted template below initial gate: {key}')
            expected = passing[:passing.index(source)+1]
            continuation = accepted[key]['continuation']
            require(continuation['template_rank'] == next(i for i, (_, k) in enumerate(ranked, 1) if k == source)
                    and continuation['batch'] == 4 and continuation['all_compatible_targets_scanned'] is True
                    and continuation['recursive_candidate_propagation'] is False, f'Attempt rank/provenance differs: {key}')
        else:
            expected = passing
            reason = 'registration_or_boundary_rejected' if passing else 'image_similarity_below_gate'
            require(outcomes[key]['reason'] == reason, f'False rejection outcome: {key}')
        require([trial['template_key'] for trial in trials] == expected, f'Compatible attempt coverage/order differs: {key}')
        for i, trial in enumerate(trials):
            if key in accepted and i == len(trials)-1:
                equal_metrics(trial['metrics'], accepted[key]['metrics'], f'{key}.accepted_attempt')
            else:
                require(trial['metrics'].get('rejection') in {
                    'affine_registration_failed', 'excessive_affine_deformation', 'unreliable_vertex_tracking',
                    'insufficient_image_correspondence', 'vertex_refinement_too_large', 'invalid_transferred_polygon',
                    'unstable_polygon_area', 'local_beam_boundary_review_required'}, f'Unexplained rejected attempt: {key}')
    require(set(bytarget) <= eligible and set(accepted) <= eligible, 'Attempt/candidate outside eligible targets')
    return eligible


class ReportInventory(HTMLParser):
    def __init__(self):
        super().__init__()
        self.articles, self.cells = [], []
        self.cell = None

    def handle_starttag(self, tag, attrs):
        if tag == 'article':
            self.articles.append(dict(attrs))
        if tag == 'td':
            self.cell = ''

    def handle_data(self, data):
        if self.cell is not None:
            self.cell += data

    def handle_endtag(self, tag):
        if tag == 'td' and self.cell is not None:
            self.cells.append(self.cell.strip())
            self.cell = None


def verify_report(out, bykey, new, counts, old):
    from fontTools.ttLib import TTFont
    page = (out/'report_zh.html').read_text()
    parsed = ReportInventory()
    parsed.feed(page)
    articles = indexed(parsed.articles, 'id')
    require(set(articles) == {f'target-{key}' for key in bykey}, 'Report target inventory differs')
    for key, proposal in bykey.items():
        article = articles[f'target-{key}']
        require(article['data-class'] == str(proposal['class_id'])
                and article['data-batch'] == ('new' if key in new else 'previous'), f'Report card identity differs: {key}')
    cells = []
    for cls in CLASSES:
        old_count = sum(p['class_id'] == cls for p in old.values())
        new_count = sum(p['class_id'] == cls for p in new.values())
        cells.extend(map(str, (cls, old_count, new_count, counts[str(cls)]['distinct_frames'], counts[str(cls)]['shortfall'])))
    require(parsed.cells == cells, 'Published report counts or shortfalls differ')
    with TTFont(out/'report-font.otf') as font:
        require({ord(c) for c in page if '\u3400' <= c <= '\u9fff'} <= set(font.getBestCmap()), 'Report Chinese font coverage incomplete')
    require((out/'FONT_LICENSE.txt').is_file(), 'Missing font license')


def check_artifacts(out, hashes):
    require(hashes, 'Missing frozen artifact hash inventory')
    for relative, expected in hashes.items():
        path = out/relative
        require(path.resolve().is_relative_to(out) and path.is_file() and not path.is_symlink(), f'Unsafe/missing artifact: {relative}')
        require(digest(path) == expected, f'Artifact changed: {relative}')


def audit(out, pose_live, gallery_live):
    manifest = read_json(out/'manifest.json')
    artifacts = read_json(out/'artifact_hashes.json')
    check_artifacts(out, artifacts)
    required_artifacts = {str(path.relative_to(out)) for path in out.rglob('*') if path.is_file()
                          and path.name not in ('artifact_hashes.json', 'validation_report.json')}
    require(required_artifacts == set(artifacts), 'Artifact hash inventory does not cover all generated files')
    require(manifest['publication_version'] == 4, 'Expected publication_version 4')
    for name, expected in PINNED.items():
        require(digest(PARENT/name) == expected, f'Frozen parent changed: {name}')
    parent_manifest = read_json(PARENT/'manifest.json')
    require((PROJECT/manifest['parent']).resolve() == PARENT.resolve(), 'Unexpected parent publication')
    require(manifest['parent_proposals_sha256'] == PINNED['proposals.jsonl']
            and manifest['parent_verification_sha256'] == PINNED['verification.json'], 'Parent binding differs')
    for name, expected in ORIGINAL_CODE.items():
        require(digest(PROJECT/name) == expected, f'Original quality-gate code changed: {name}')
    require(manifest['transfer_code_sha256'] == ORIGINAL_CODE['pallet/mask_template_transfer.py']
            and manifest['boundary_code_sha256'] == ORIGINAL_CODE['pallet/mask_boundary_review.py'], 'Gate code provenance differs')
    require(digest(PROJECT/'pallet/mask_template_refined.py') == manifest['code_sha256'], 'Generator provenance differs')
    require(digest(PROJECT/'pallet/mask_template_continue.py') == manifest['attempt_code_sha256'] == parent_manifest['code_sha256'],
            'Original attempt-selection code changed')
    for field in ('thresholds_relaxed', 'recursive_candidate_propagation', 'automatic_human_confirmation',
                  'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible'):
        require(manifest[field] is False, f'Manifest {field} must be false')
    require(manifest['production_database_writes'] == 0 and manifest['cross_split_template_transfers'] == 0,
            'Manifest claims prohibited writes/transfers')
    for name, field in (('proposals.jsonl', 'proposals_sha256'), ('new_proposals.jsonl', 'new_proposals_sha256')):
        require(digest(out/name) == manifest[field], f'{name} hash differs')
    old = indexed(read_jsonl(PARENT/'proposals.jsonl'))
    proposals = read_jsonl(out/'proposals.jsonl')
    bykey = indexed(proposals)
    new = indexed(read_jsonl(out/'new_proposals.jsonl'))
    require(len(old) == manifest['previous_proposals'] == 56 and len(proposals) == manifest['proposals']
            and len(new) == manifest['new_proposals'], 'Proposal counts differ')
    require(set(old) <= set(bykey) and set(new) == set(bykey)-set(old), 'Cumulative/new target sets differ')
    require(QUARANTINE not in bykey and QUARANTINE in manifest['preserved_quarantined_targets'], 'Quarantine not preserved')
    for key, proposal in old.items():
        require(canonical(bykey[key]) == canonical(proposal), f'Old canonical proposal changed: {key}')
        for folder in ('images', 'masks'):
            require(digest(out/folder/f'{key}.png') == digest(PARENT/folder/f'{key}.png'), f'Old {folder} changed: {key}')
    for key, proposal in new.items():
        require(canonical(proposal) == canonical(bykey[key]), f'New proposal differs from cumulative record: {key}')
    accepted = indexed(read_jsonl(out/'accepted_candidates.jsonl'))
    require(set(new) <= set(accepted) and len(accepted) == manifest['accepted_before_diversity'], 'Accepted candidate inventory differs')
    for key, proposal in new.items():
        require(canonical(proposal) == canonical(accepted[key]), f'Selected candidate changed: {key}')
    preserved_assets = read_json(out/'preserved_parent_assets.json')
    required_parent_assets = {f'{folder}/{key}.{extension}' for key in old for folder, extension in
                              (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json'))}
    required_parent_assets |= {str(path.relative_to(PARENT)) for path in (PARENT/'templates').glob('*.json')}
    required_parent_assets.add('vertex2_before_after.png')
    require(set(preserved_assets) == required_parent_assets, 'Preserved parent asset inventory differs')
    for name, expected in preserved_assets.items():
        require(digest(PARENT/name) == expected == digest(out/name), f'Historical asset changed: {name}')
    counts = count_frames(proposals)
    formal = Path(read_json(PROJECT/'configs/active_formal_dataset.json')['dataset']).resolve()
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    formal_sha = digest(formal/'manifest.json')
    require(formal_sha == manifest['formal_manifest_sha256'] == parent_manifest['formal_manifest_sha256'], 'Formal manifest differs')
    require(digest(classification) == manifest['classification_sha256'] == parent_manifest['classification_sha256'], 'Classification changed')
    rows, sources = formal_rows(formal, classification)
    protected = read_json(out/'protected_formal_before.json')
    require(protected == read_json(PARENT/'protected_formal_before.json') == protected_snapshot(formal), 'Protected formal files changed')
    require(read_json(out/'protected_formal_after.json') == protected, 'Generation formal before/after differs')
    snapshots = find_snapshots(out, manifest)
    frozen = {kind: read_store(path, kind, frozen=True) for kind, path in snapshots.items()}
    pose, gallery = frozen['pose'], frozen['gallery']
    require(json.loads(pose['metadata']['binding'])['formal_manifest_sha256'] == formal_sha
            and gallery['metadata']['formal_manifest_sha256'] == formal_sha, 'Review dataset binding differs')
    require(gallery['history_keys'] <= set(rows), 'Unknown gallery history identity')
    for kind, prefix in (('pose', 'snapshot'), ('gallery', 'gallery_snapshot')):
        require(manifest[prefix+'_saved'] == len(frozen[kind]['current'])
                and manifest[prefix+'_events'] == len(frozen[kind]['events']), f'{kind} snapshot counts differ')
    status = pose_status(pose, rows)
    with closing(sqlite3.connect(snapshots['pose'].resolve().as_uri()+'?mode=ro&immutable=1', uri=True)) as db:
        db.execute('PRAGMA query_only=ON')
        _, _, original_status = annotation_inventory(db, rows)
    require(status == original_status, 'Independent mask-history status differs from original annotation_inventory')
    status = original_status
    inventory = read_json(out/'template_inventory.json')
    require(digest(out/'template_inventory.json') == manifest['template_inventory_sha256'], 'Template inventory binding differs')
    indexed(inventory['templates'], 'rendering_id')
    refined = indexed([dict(t, target_key=t['row']['target_key']) for t in inventory['templates'] if t.get('origin') == ORIGIN])
    historical = [t for t in inventory['templates'] if t.get('origin') != ORIGIN]
    parent_templates = read_json(PARENT/'template_inventory.json')['templates']
    require(historical == [dict(t, rendering_id='historical:'+t['row']['target_key']) for t in parent_templates],
            'Historical rendering template inventory changed')
    require(inventory['generation_template_keys'] == sorted(refined)
            and inventory['gallery_human_templates'] == manifest['generation_templates'] == len(refined)
            and inventory['historical_templates'] == len(historical), 'Template counts/keys differ')
    require(inventory['eligibility'] == status and inventory['classes'] == manifest['classes'], 'Inventory eligibility/class counts differ')
    require(inventory['snapshot_saved'] == len(pose['current']) and inventory['snapshot_events'] == len(pose['events']), 'Inventory snapshot counts differ')
    require(set(refined) == set(old), 'Expected exactly the 56 newly refined gallery templates')
    for key, proposal in bykey.items():
        expected = ('refined:' if key in new else 'historical:')+proposal['template']['target_key']
        require(inventory['rendering_by_target'][key] == expected, f'Rendering namespace differs: {key}')
    require(set(inventory['rendering_by_target']) == set(bykey), 'Rendering target inventory differs')
    require(inventory['conflicting_template_keys'] == sorted(set(refined) & {t['row']['target_key'] for t in historical}),
            'Template namespace collision inventory differs')
    gallery_sha = digest(snapshots['gallery'])
    pose_sha = digest(snapshots['pose'])
    evidence = ImageEvidence(formal, rows, sources)
    refined_annotations = indexed(read_jsonl(out/'refined_annotations.jsonl'))
    require(digest(out/'refined_annotations.jsonl') == manifest['refined_annotations_sha256']
            and refined_annotations == {key: gallery['current'][key] for key in refined}, 'Frozen refined annotation export differs')
    for key, template in refined.items():
        gallery_template(template, pose, gallery, rows, old, PARENT, formal_sha, gallery_sha)
        exported = read_json(out/'refined/templates'/f'{key}.json')
        require(exported == {k: v for k, v in template.items() if k not in ('rendering_id', 'target_key')}, f'Exported refined template differs: {key}')
        require(template['rendering_id'] == 'refined:'+key, f'Refined rendering namespace differs: {key}')
        label = read_json(out/'refined/labelme'/f'{key}.json')
        require(label['mask_gallery_review'] == gallery['current'][key] and len(label['shapes']) == 1
                and label['shapes'][0]['points'] == template['polygon'] and label['shapes'][0]['shape_type'] == 'polygon'
                and label['shapes'][0]['group_id'] == rows[key]['box_index']
                and label['imageWidth'] == rows[key]['size'][0] and label['imageHeight'] == rows[key]['size'][1]
                and label['imagePath'] == f'../../images/{key}.png' and label['imageData'] is None,
                f'Refined LabelMe polygon/identity differs: {key}')
        require(label['flags']['human_confirmed'] is False and label['shapes'][0]['flags']['human_confirmed'] is False
                and label['flags']['automatic_training_eligible'] is False and label['flags']['robot_command_eligible'] is False,
                f'Refined LabelMe claims confirmation/eligibility: {key}')
        # Decode and hash the original source, not only the exported gallery PNG.
        with Image.open(PARENT/'images'/f'{key}.png') as image:
            np.testing.assert_array_equal(np.asarray(image), evidence.image(rows[key]['frame_id']))
    for proposal in proposals:
        key = proposal['target_key']
        verify_assets(out, proposal, rows[key], evidence)
        require(proposal['formal_manifest_sha256'] == formal_sha
                and proposal['class_assignments_sha256'] == manifest['classification_sha256'], f'Proposal dataset binding differs: {key}')
    for key, proposal in accepted.items():
        require_target_eligible(key, status, gallery, rows)
        require(proposal['source'] == rows[key] and proposal['class_id'] == rows[key]['final_class_id'], f'Accepted identity differs: {key}')
        polygon_value(proposal['polygon'], *rows[key]['size'])
        require(proposal['status'] == 'machine_draft_pending_visual_review'
                and proposal['formal_manifest_sha256'] == formal_sha
                and proposal['class_assignments_sha256'] == manifest['classification_sha256'], f'Accepted status/source differs: {key}')
        for field in ('human_mask_confirmed', 'human_pose_confirmed', 'automatic_training_eligible',
                      'robot_command_eligible', 'production_saved', 'ground_truth_eligible'):
            require(proposal[field] is False, f'Accepted proposal {field} must be false: {key}')
        binding = proposal['template']
        require(binding['target_key'] in refined, f'New proposal uses a non-gallery template: {key}')
        template = refined[binding['target_key']]
        require(compatible(template['row'], rows[key]), f'Cross-class/split/policy/view or same-frame transfer: {key}')
        require(binding['origin'] == ORIGIN and binding['gallery_review'] == template['gallery_review'], f'Gallery proposal provenance differs: {key}')
        require(binding['class_id'] == proposal['class_id'] and binding['split'] == rows[key]['split']
                and binding['vertex_count'] == proposal['vertex_count'] == len(proposal['polygon']) == len(template['polygon']) > 4,
                f'Class/split/topology binding differs: {key}')
        require(binding['snapshot_revision'] == template['revision']
                and binding['current_payload_sha256'] == template['current_payload_sha256']
                and binding['human_polygon_revision'] == template['human_polygon_revision']
                and binding['human_event_sha256'] == template['human_event_sha256']
                and binding['template_human_confirmed'] is False, f'Template revision binding differs: {key}')
        previous = pose['current'].get(key)
        require(proposal['snapshot_sha256'] == pose_sha
                and proposal['target_expected_revision'] == (previous['revision'] if previous else 0)
                and proposal['target_snapshot_payload_sha256'] == (sha(previous) if previous else None), f'Target snapshot binding differs: {key}')
        recompute(proposal, template, evidence)
    outcomes = indexed(read_jsonl(out/'outcomes.jsonl'))
    require(set(outcomes) == set(rows), 'Outcomes must account for every formal target exactly once')
    require({key for key, value in outcomes.items() if value['reason'] == 'new_proposal'} == set(new), 'Accepted outcome set differs')
    require(manifest['outcomes'] == dict(Counter(v['reason'] for v in outcomes.values())), 'Outcome totals differ')
    require(manifest['new_by_class'] == {str(k): v for k, v in Counter(p['class_id'] for p in new.values()).items()}, 'New class counts differ')
    require(manifest['new_by_vertices'] == {str(k): v for k, v in Counter(p['vertex_count'] for p in new.values()).items()}, 'New vertex counts differ')
    for key, value in outcomes.items():
        require(value['class_id'] == rows[key]['final_class_id'], f'Outcome class differs: {key}')
    eligible = audit_attempts(out, rows, status, gallery, old, refined, accepted, outcomes, evidence)
    require(manifest['scanned_eligible_targets'] == len(eligible), 'Eligible scan count differs')
    check_diversity(old, accepted, new, outcomes)
    protected_targets = read_json(out/'protected_targets.json')
    require(protected_targets['gallery_saved_or_history'] == sorted(gallery['history_keys'])
            and protected_targets['quarantine'] == [QUARANTINE] and protected_targets['eligibility'] == status,
            'Protected target inventory differs')
    for field, keys in protected_targets.items():
        if field.startswith('pose_') and isinstance(keys, list):
            require(len(keys) == len(set(keys)) and all(key in status and status[key] != 'eligible' for key in keys),
                    f'Protected pose inventory includes unchanged-rectangle eligible saves: {field}')
    shortfalls = read_json(out/'shortfalls.json')
    require(set(manifest['classes']) == set(shortfalls) == set(counts), 'Class/shortfall coverage differs')
    for cls, values in counts.items():
        n = int(cls)
        class_outcomes = Counter(value['reason'] for value in outcomes.values() if value['class_id'] == n)
        old_count = sum(p['class_id'] == n for p in old.values())
        expected = dict(saved_fine_edits=old_count, new_candidates=values['proposals']-old_count,
                        total=values['proposals'], missing=values['shortfall'], distinct_frames=values['distinct_frames'],
                        goal=LIMIT, templates=sum(t['row']['final_class_id'] == n for t in refined.values()),
                        eligible=sum(rows[key]['final_class_id'] == n for key in eligible),
                        compatible_targets=sum(class_outcomes[reason] for reason in ('new_proposal', 'image_similarity_below_gate',
                            'registration_or_boundary_rejected', 'accepted_duplicate_frame_not_selected', 'accepted_quota_full_not_selected')),
                        preview=values['proposals'], outcomes=dict(class_outcomes))
        require(manifest['classes'][cls] == expected, f'Class {cls} counts or shortfall differ')
        require(shortfalls[cls]['missing'] == values['shortfall'] and shortfalls[cls]['outcomes'] == dict(class_outcomes),
                f'Class {cls} shortfall report differs')
    verify_report(out, bykey, new, counts, old)
    used = read_json(out/'used_image_hashes.json')
    for name, expected in evidence.used.items():
        require(used.get(name) == expected, f'Used image hash missing/different: {name}')
    for name, expected in used.items():
        path = (formal/name).resolve()
        require(path.is_relative_to(formal) and digest(path) == expected, f'Used image changed: {name}')
    require(protected_snapshot(formal) == protected, 'Formal files changed during verification')
    for kind, path in snapshots.items():
        require(digest(path) == (pose_sha if kind == 'pose' else gallery_sha), f'{kind} snapshot changed during verification')
    check_artifacts(out, artifacts)
    for name, expected in ORIGINAL_CODE.items():
        require(digest(PROJECT/name) == expected, f'Original gate code changed during verification: {name}')
    for name, expected in PINNED.items():
        require(digest(PARENT/name) == expected, f'Parent changed during verification: {name}')
    require(digest(out/'proposals.jsonl') == manifest['proposals_sha256'], 'Proposals changed during verification')
    # Last expensive check: unrelated appended edits are allowed, but no stale
    # template or newly protected target may receive a publication seal.
    live = {'pose': read_store(pose_live, 'pose'), 'gallery': read_store(gallery_live, 'gallery')}
    for kind in frozen:
        preserve_events(frozen[kind], live[kind])
    current_status = pose_status(live['pose'], rows)
    for key in refined:
        for kind in ('pose', 'gallery'):
            require(canonical(live[kind]['current'].get(key)) == canonical(frozen[kind]['current'].get(key)),
                    f'{kind} template changed since snapshot; regenerate before publication: {key}')
    for key in new:
        require_target_eligible(key, current_status, live['gallery'], rows)
        require(canonical(live['pose']['current'].get(key)) == canonical(pose['current'].get(key)),
                f'Target pose revision changed since snapshot: {key}')
    return dict(state='passed', publication_version=4, proposals_verified=len(proposals), new_verified=len(new),
                old_canonical_objects_preserved=56, proposals_sha256=manifest['proposals_sha256'],
                new_proposals_sha256=manifest['new_proposals_sha256'], manifest_sha256=digest(out/'manifest.json'),
                classes=counts, all_new_registrations_and_metrics_recomputed=True, metric_absolute_tolerance=ATOL,
                thresholds_relaxed=False, forced_fill=False, templates_verified=len(refined),
                accepted_before_diversity_recomputed=len(accepted), extra_public_images=['vertex2_before_after.png'],
                gallery_snapshot_sha256=gallery_sha, pose_snapshot_sha256=pose_sha,
                original_events_preserved={kind: len(store['events']) for kind, store in frozen.items()},
                outcomes_verified=len(outcomes), protected_formal_files_verified=len(protected),
                production_database_writes=0, human_confirmation_inferred=False,
                verification_script_sha256=digest(__file__), verified_at=datetime.now(timezone.utc).isoformat())


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=PROJECT/'outputs/pallet_mask_template_refined_v2')
    parser.add_argument('--pose-live', type=Path, default=PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3')
    parser.add_argument('--gallery-live', type=Path, default=PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3')
    args = parser.parse_args(argv)
    out = args.output.resolve()
    require(out.is_dir() and out.is_relative_to(PROJECT/'outputs'), 'Use an existing completed outputs directory')
    require(out != PARENT.resolve(), 'Cannot verify over the frozen parent')
    require(out != (PROJECT/'outputs/pallet_mask_template_refined_v1').resolve(), 'v1 is an unreleased diagnostic, not a publication candidate')
    require(not (out/'verification.json').exists(), 'Refusing to overwrite a frozen verification')
    require((out/'manifest.json').is_file(), 'Generation is not complete: manifest.json missing')
    require(not (out/'validation_report.json').is_symlink(), 'Unsafe validation report path')
    cv2.setNumThreads(1)
    try:
        result = audit(out, args.pose_live, args.gallery_live)
    except Exception as exc:
        report = dict(state='failed', publication_version=4, error_type=type(exc).__name__, error=str(exc),
                      verification_script_sha256=digest(__file__))
        (out/'validation_report.json').write_bytes(canonical(report)+b'\n')
        raise
    (out/'validation_report.json').write_bytes(canonical(result)+b'\n')
    result['validation_report_sha256'] = digest(out/'validation_report.json')
    # Exclusive creation is the last operation: failures never create a seal.
    with (out/'verification.json').open('xb') as stream:
        stream.write(canonical(result)+b'\n')
    print(json.dumps(result, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
