"""Independently seal publication 5 after generation from all 97 saved gallery edits.

The frozen publication-4 verifier supplies unchanged low-level audit helpers only;
this verifier never imports the next generator. SQLite inputs are read-only. The
CLI writes only validation_report.json and, on success, verification.json.
"""
from __future__ import annotations

import argparse
from collections import Counter
from contextlib import closing
from datetime import datetime, timezone
from pathlib import Path
import json
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 scripts import verify_mask_template_refined as prior
from pallet.cluster_targets import digest, protected_snapshot
from pallet.mask_boundary_review import affine_reference, beam_boundary_audit
from pallet.mask_template_transfer import ImageEvidence, annotation_inventory, compatible, correlate, transfer
from pallet.pose_review_geometry import canonical, polygon_value

require = prior.require
read_json = prior.read_json
read_jsonl = prior.read_jsonl
indexed = prior.indexed
sha = prior.sha
equal_metrics = prior.equal_metrics
CLASSES, LIMIT, ORIGIN, QUARANTINE = prior.CLASSES, prior.LIMIT, prior.ORIGIN, prior.QUARANTINE
PARENT = PROJECT/'outputs/pallet_mask_template_refined_v2'
PINNED = {
    'manifest.json': '8f279afa51fed759fd3c4dddc385c7d44961dee7b62b5dca3006235857920eb0',
    'proposals.jsonl': 'd5acfd00196d5905b132d2b08652eb5dac82f9794df19e53e14be3b3c46c0f9e',
    'verification.json': '34eb35b055db3bcd5daf20c51cc5064f8502c34b10797c8ff7f0177f133ad0c9',
}
ORIGINAL_CODE = {
    **prior.ORIGINAL_CODE,
    'pallet/mask_template_continue.py': '0d3ad230945762d4f35ee9b692f06b71e200da1f3038a464a742675c2255f1f3',
    'pallet/mask_template_refined.py': 'cf29652656a7a8266530c38455356129f4a7a5a03e8e2033cc6e284e351ac76b',
    'scripts/verify_mask_template_refined.py': 'f71e7b899eba56a75e708b693aee30bfdc075c63ec8d48d5229651613b78199d',
}
PUBLICATIONS = {3: (prior.PARENT, prior.PINNED), 4: (PARENT, PINNED)}


def check_frozen_inputs():
    publications = {}
    for version, (path, pins) in PUBLICATIONS.items():
        for name, expected in pins.items():
            require(digest(path/name) == expected, f'Frozen publication {version} changed: {name}')
        manifest, verification = read_json(path/'manifest.json'), read_json(path/'verification.json')
        require(verification['state'] == 'passed' and verification['publication_version'] == version
                and verification['proposals_sha256'] == manifest['proposals_sha256'] == pins['proposals.jsonl'],
                f'Publication {version} lacks its matching independent seal')
        # Publication 3 predates the manifest_sha256 seal field; its manifest is
        # independently pinned above, while publication 4 binds both hashes.
        if version == 4:
            require(verification['manifest_sha256'] == pins['manifest.json'], 'Parent manifest seal differs')
        proposals = indexed(read_jsonl(path/'proposals.jsonl'))
        require(len(proposals) == verification['proposals_verified'] == (56 if version == 3 else 97),
                f'Publication {version} verified inventory differs')
        publications[version] = dict(path=path, proposals=proposals, manifest=manifest)
    for name, expected in ORIGINAL_CODE.items():
        require(digest(PROJECT/name) == expected, f'Frozen gate/verifier code changed: {name}')
    return publications


def review_publication(value, old, formal_sha, publications):
    """Resolve a review against the actual sealed publication it was saved from."""
    from pallet.mask_gallery_review import VERSION as EDITOR_VERSION
    version, key = value['proposal_publication'], value['target_key']
    require(type(version) is int and version in publications, f'Unknown review publication: {key}')
    publication = publications[version]
    proposal = publication['proposals'].get(key)
    require(proposal is not None and canonical(proposal) == canonical(old[key]),
            f'Review publication proposal differs: {key}')
    require(value['base_proposal_id'] == sha(proposal) and value['proposal_polygon'] == proposal['polygon']
            and value['template_provenance'] == proposal['template'], f'Gallery base proposal differs: {key}')
    require(value['binding'] == sha(dict(formal=formal_sha,
            proposals=PUBLICATIONS[version][1]['proposals.jsonl'], editor_version=EDITOR_VERSION)),
            f'Gallery publication binding differs: {key}')
    return publication['path']


def gallery_template(template, pose, gallery, rows, old, formal_sha, snapshot_sha, publications):
    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}')
    publication = review_publication(value, old, formal_sha, publications)
    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(publication/'images'/f'{key}.png'))
    require(value['source'] == source and value['source_binding'] == sha(source), f'Gallery source binding differs: {key}')
    require(digest(PARENT/'images'/f'{key}.png') == source['image_sha256'], f'Historical source image 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 saved 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}')


def audit_attempts(out, rows, status, gallery, old, templates, accepted, outcomes, evidence):
    """Replay rankings, every rejection and first-success stopping independently."""
    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 and key not in accepted,
                    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'] == 5 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:
                # Accepted polygons and every metric leaf were independently recomputed below.
                equal_metrics(trial['metrics'], accepted[key]['metrics'], f'{key}.accepted_attempt')
                continue
            template = templates[trial['template_key']]
            polygon, metrics = transfer(evidence.patch(trial['template_key']), evidence.patch(key),
                                        template['polygon'], template['row'], row)
            if polygon is not None:
                reference = affine_reference(dict(source=row, metrics=metrics), template)
                guard = beam_boundary_audit(evidence.image(row['frame_id']), polygon, reference)
                metrics['boundary_guard'] = guard
                if guard['state'] != 'passed':
                    polygon = None
                    metrics['rejection'] = 'local_beam_boundary_review_required'
            require(polygon is None, f'Passing attempt falsely rejected: {key}/{trial["template_key"]}')
            equal_metrics(trial['metrics'], metrics, f'{key}.{trial["template_key"]}.rejected_attempt')
    require(set(bytarget) <= eligible and set(accepted) <= eligible, 'Attempt/candidate outside eligible targets')
    accepted_reasons = {'new_proposal', 'accepted_duplicate_frame_not_selected', 'accepted_quota_full_not_selected'}
    require({key for key, value in outcomes.items() if value['reason'] in accepted_reasons} == set(accepted),
            'Accepted outcomes must cover every accepted candidate exactly once')
    return eligible, len(attempts)


def template_payload(template):
    return {k: v for k, v in template.items() if k not in ('rendering_id', 'target_key')}


def rendering_inventory(inventory, parent_inventory, old, new, gallery_sha):
    """Validate namespace contents rather than trusting a renderer's prefix labels.

    Historical entries may rename rendering_id, but preserve every payload field;
    latest seeds are identified by the current immutable gallery snapshot binding.
    This supports historical/refined/generation without conflating equal keys.
    """
    all_templates = indexed(inventory['templates'], 'rendering_id')
    latest, historical = {}, []
    for rendering_id, template in all_templates.items():
        require(isinstance(rendering_id, str) and rendering_id, 'Empty rendering namespace')
        key = template['row']['target_key']
        if template.get('origin') == ORIGIN and template.get('gallery_review', {}).get('snapshot_sha256') == gallery_sha:
            require(key not in latest, f'Duplicate latest gallery template: {key}')
            latest[key] = template
        else:
            historical.append(template)
    require(Counter(sha(template_payload(t)) for t in historical) ==
            Counter(sha(template_payload(t)) for t in parent_inventory['templates']),
            'Historical rendering template payload inventory changed')
    require(set(latest) == set(old), 'Expected exactly all 97 latest saved direct gallery seeds')
    require(inventory['generation_template_keys'] == sorted(latest)
            and inventory['gallery_human_templates'] == len(latest)
            and inventory['historical_templates'] == len(historical), 'Template counts/keys differ')
    require(inventory['conflicting_template_keys'] == sorted(set(latest) & {t['row']['target_key'] for t in historical}),
            'Template namespace collision inventory differs')
    mapping = inventory['rendering_by_target']
    require(set(mapping) == set(old) | set(new), 'Rendering target inventory differs')
    parent_templates = indexed(parent_inventory['templates'], 'rendering_id')
    for key, proposal in old.items():
        expected = parent_templates[parent_inventory['rendering_by_target'][key]]
        actual = all_templates.get(mapping[key])
        require(actual is not None and actual in historical
                and template_payload(actual) == template_payload(expected), f'Historical rendering changed: {key}')
    for key, proposal in new.items():
        require(mapping[key] == latest[proposal['template']['target_key']]['rendering_id'], f'Latest rendering differs: {key}')
    return latest


def audit(out, pose_live, gallery_live):
    publications = check_frozen_inputs()
    parent_manifest = publications[4]['manifest']
    manifest = read_json(out/'manifest.json')
    artifacts = read_json(out/'artifact_hashes.json')
    prior.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'] == 5, 'Expected publication_version 5')
    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')
    require(manifest['parent_generator_code_sha256'] == ORIGINAL_CODE['pallet/mask_template_refined.py'],
            'Parent generator provenance differs')
    for field, name in (('transfer_code_sha256', 'pallet/mask_template_transfer.py'),
                        ('boundary_code_sha256', 'pallet/mask_boundary_review.py'),
                        ('attempt_code_sha256', 'pallet/mask_template_continue.py')):
        require(manifest[field] == ORIGINAL_CODE[name], f'Frozen gate provenance differs: {field}')
    generator_sha = digest(PROJECT/'pallet/mask_template_refined_next.py')
    require(generator_sha == manifest['code_sha256'], 'Generator provenance differs')
    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
            and manifest['original_splits_preserved'] is True, 'Manifest writes/split policy differs')
    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 = publications[4]['proposals']
    proposals = read_jsonl(out/'proposals.jsonl')
    bykey, new = indexed(proposals), indexed(read_jsonl(out/'new_proposals.jsonl'))
    require(len(old) == manifest['previous_proposals'] == 97 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 key, proposal in new.items():
        require(canonical(proposal) == canonical(bykey[key]), f'New cumulative proposal differs: {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 directory in ('templates', 'refined')
                              for path in (PARENT/directory).rglob('*') if path.is_file()}
    required_parent_assets.add('vertex2_before_after.png')
    require(required_parent_assets == set(preserved_assets), 'Preserved parent asset inventory differs')
    for name, expected in preserved_assets.items():
        source, destination = PARENT/name, out/name
        require(source.resolve().is_relative_to(PARENT) and destination.resolve().is_relative_to(out), 'Unsafe historical asset path')
        require(digest(source) == expected == digest(destination), f'Historical asset changed: {name}')
    counts = prior.count_frames(proposals)
    active_formal = PROJECT/'configs/active_formal_dataset.json'
    active_formal_sha = digest(active_formal)
    formal = Path(read_json(active_formal)['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 = prior.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 = prior.find_snapshots(out, manifest)
    frozen = {kind: prior.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(gallery['current']) == set(old)
            and len(gallery['events']) == 104, 'Expected 97 saved gallery edits and 104 original events')
    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')
    parent_snapshots = prior.find_snapshots(PARENT, parent_manifest)
    for kind, path in parent_snapshots.items():
        prior.preserve_events(prior.read_store(path, kind, frozen=True), frozen[kind])
    status = prior.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 frozen annotation_inventory')
    gallery_sha, pose_sha = digest(snapshots['gallery']), digest(snapshots['pose'])
    inventory = read_json(out/'template_inventory.json')
    require(digest(out/'template_inventory.json') == manifest['template_inventory_sha256'], 'Template inventory binding differs')
    require(digest(PARENT/'template_inventory.json') == parent_manifest['template_inventory_sha256'], 'Parent template inventory changed')
    require(inventory['generation_template_namespace'] == manifest['generation_template_namespace'] == 'generation',
            'Latest generation namespace differs')
    refined = rendering_inventory(inventory, read_json(PARENT/'template_inventory.json'), old, new, gallery_sha)
    require(manifest['generation_templates'] == len(refined) == 97, 'Generation must use all 97 latest saved direct seeds')
    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')
    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 == gallery['current'], 'Frozen latest annotation export differs')
    for key, template in refined.items():
        gallery_template(template, pose, gallery, rows, old, formal_sha, gallery_sha, publications)
        require(template['rendering_id'] == 'generation:'+key, f'Latest rendering namespace differs: {key}')
        exported = read_json(out/'generation/templates'/f'{key}.json')
        require(exported == template_payload(template), f'Exported latest template differs: {key}')
        label = read_json(out/'generation/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'Latest 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'Latest LabelMe claims confirmation/eligibility: {key}')
        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']
        prior.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():
        prior.require_target_eligible(key, status, gallery, rows)
        require(key not in old and 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'Candidate uses a non-latest-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}')
        prior.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), 'New 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, attempts_count = audit_attempts(out, rows, status, gallery, old, refined, accepted, outcomes, evidence)
    require(manifest['scanned_eligible_targets'] == len(eligible), 'Eligible scan count differs')
    prior.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')
    require(protected_targets['pose_mask_history_or_noneligible'] == sorted(k for k, v in status.items() if v != 'eligible'),
            'Protected pose inventory must cover mask history without excluding unchanged-rectangle pose-only saves')
    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')
    prior.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 and digest(active_formal) == active_formal_sha, 'Formal files/config changed during verification')
    require(digest(classification) == manifest['classification_sha256'], 'Classification 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')
    prior.check_artifacts(out, artifacts)
    check_frozen_inputs()
    require(digest(PROJECT/'pallet/mask_template_refined_next.py') == generator_sha, 'Generator changed during verification')
    live = {'pose': prior.read_store(pose_live, 'pose'), 'gallery': prior.read_store(gallery_live, 'gallery')}
    for kind in frozen:
        prior.preserve_events(frozen[kind], live[kind])
    current_status = prior.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 accepted:
        prior.require_target_eligible(key, current_status, live['gallery'], rows)
        require(canonical(live['pose']['current'].get(key)) == canonical(pose['current'].get(key)),
                f'Candidate pose revision changed since snapshot: {key}')
    return dict(state='passed', publication_version=5, proposals_verified=len(proposals), new_verified=len(new),
                old_canonical_objects_preserved=97, 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=prior.ATOL,
                thresholds_relaxed=False, forced_fill=False, templates_verified=len(refined),
                accepted_before_diversity_recomputed=len(accepted), registration_attempts_verified=attempts_count,
                all_rejected_attempts_and_metrics_recomputed=True, 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_v3')
    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 not in {PARENT.resolve(), prior.PARENT.resolve(),
            (PROJECT/'outputs/pallet_mask_template_refined_v1').resolve()}, 'Cannot verify over a frozen/diagnostic publication')
    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=5, 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')
    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()
