"""Independently seal publication 9's saved-human quota completion.

No generation or live application is invoked. Only the new validation report
and an exclusive successful seal may be written. Historical transfer evidence
is inherited through the pinned publication-8 independent seal, never rerun.
Conversation-authorized no-change reviews are not fabricated SQLite edits,
saved human masks, confirmation, or permission to propagate machine contours.
"""
from __future__ import annotations

import argparse
from collections import Counter
from contextlib import closing
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import re
import sqlite3
import sys
import zipfile

import cv2

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from scripts import verify_mask_quota_100_continue as previous
from pallet.cluster_targets import digest, protected_snapshot
from pallet.mask_template_transfer import ImageEvidence, unchanged_rectangle
from pallet.pose_review_geometry import canonical, polygon_value

prior, v1 = previous.prior, previous.v1
require, sha = prior.require, prior.sha
read_json, read_jsonl, indexed = prior.read_json, prior.read_jsonl, prior.indexed
CLASSES, FALSE_FLAGS = previous.CLASSES, previous.FALSE_FLAGS
POSE_ORIGIN, GALLERY_ORIGIN = previous.POSE_ORIGIN, previous.GALLERY_ORIGIN
PARENT = PROJECT/'outputs/pallet_mask_quota_100_continue_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_quota_100_complete_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_quota_100_final_requests_v2'
DISPOSITION_INPUT = PROJECT/'outputs/pallet_mask_quota_100_continue_deployment_v1/missing_previous_batch_reviews.jsonl'
DISPOSITION_INPUT_SHA = '934a5bc89c53a878fc47725d05faee580c7b7fffd44e71acee992b3aed10636a'
GENERATOR = 'pallet/mask_quota_100_complete.py'
GENERATOR_SHA = '580b59ab25fac3db09393c0f95af247db3373c2d72ec4fcb54c20f2545bf62ce'
NAMESPACE = 'quota100_complete_generation'
FOUR_VERTEX_KEY = '239e97f62fdb8078c7e04ca6'
FOUR_VERTEX_RAW_SHA = '063fc86c714758527c614a69c4664d5e848c04db269b670fc894aad28a00dbc0'
OLD_FOUR_VERTEX_KEY = previous.FOUR_VERTEX_KEY
MISSING_CURRENT_KEY = 'f50f4c811248071f83eddf2b'
MISSING_CURRENT_FRAME = '2024_4_8/08-04-2024_16-20-48#default'
IDENTITY = ('target_key', 'target_id', 'frame_id', 'split', 'group_id', 'box_index',
    'bbox', 'size', 'policy_id', 'source_pcd_sha256')
PARENT_PINS = {
    'manifest.json': 'e59a6656529832e1ab0f246922b5c4bcca80b73895cc59434d2c41808c4aa70a',
    'verification.json': 'd9e04d2f9ea9627d1f7abfcf5215c9d5cb1cc25c5d790fe57d60708513281915',
    'proposals.jsonl': '90885a9979ee092e81b5472b8c388f504b012861e4d5b341d3a01d99f29b13bd',
    'artifact_hashes.json': 'a60fdfea33f52152edefa04688afcd696a0d57eca44c1b3dff31eda0d9d11cba',
    'template_inventory.json': '174807db103b776b3ecd9cd92c32fd44b65fffff5d1a0a6dcad0d6e5b47d911f',
    'all_manual_sources.jsonl': '85abc9f9f8ac53ab57053edb70b8eff96d7b9092b9d98b7b01f74ae60029b703',
    'manual_proposals.jsonl': '35ce84323a630dbd218888759fdf93d1d60a8bb16a3dbe7b8a4a39df9ac98a24',
    'manual_source_records.jsonl': 'af43e05568432384ef28b50f6a18e9773f9093577652d7dab7cde90b5f199967',
    'new_proposals.jsonl': 'dbf94e49f3a12fa6de77155e48890a1721072e2aead268cbb7afa2ca8ec95c29',
    'validation_report.json': '0c0c932802cb374dedd78934e39dbb37ce391d17519141a1ce61fc5d1f8ddaca',
}
REQUEST_PINS = {
    'manifest.json': 'ea0e60ccba84fea14ea86a1c4abbe6aea60cfedcfb36335d6f159f5ac4e01529',
    'verification.json': 'ca571f8dfad145e3a368538587669a2f0eec2ad3dedc226594522945f75eb6fc',
    'requests.jsonl': '06b3fc715c70c879378960fe288b3406fa0a3fd093acb33f8268bc471d92629e',
}
REQUEST_KEYS = frozenset({
    '479324efab142ad3198a23a4', 'aa23762e41ea45aa367f1e4a', FOUR_VERTEX_KEY,
    '7091a9bcbf2d003a74babff7', 'e7103af15d4fa3417b5a419c', 'f908d7f22f6316df7e0cff10',
    '33cac6accc38d034e3391cb4', '5ac944dd0056eb9a9ef0caf4', '432edcb94446ff1a377a3a77',
    '5724dc413fac25c907a8647f', '32bb5c18e38e0b5da9eb0afb', '84062539966b58e456e9f550',
    '9ed65e6b506ba09c29b50160', '97b05c5d45cbad318b3369c0', '9b075848fd8f1427d5dd793f',
    '9e29f59a72c2261d01bae149', '94f18b7f6eb66ca7ff4ea2ba', '6ea59d41d1e759e13c1346cf',
    '44e56d8e82817befbd5cae27', 'e3c8a55cd3e44d36ba1575d5', '00c407cc263a926192cf6b2b',
    'ab7ec342147eae54a4dc58b9', 'fced9d1b4ccae275dde57fde', '132343e63c7880dad68ab550',
    '795e9eee4422b2212f34753f',
})


def check_frozen_inputs():
    publications = previous.check_frozen_inputs()
    for name, expected in PARENT_PINS.items():
        require(digest(PARENT/name) == expected, f'Frozen publication 8 changed: {name}')
    for name, expected in REQUEST_PINS.items():
        require(digest(REQUESTS/name) == expected, f'Frozen 25-target requests changed: {name}')
    require(digest(DISPOSITION_INPUT) == DISPOSITION_INPUT_SHA, 'Frozen exact 45-target no-change scope changed')
    manifest, seal = read_json(PARENT/'manifest.json'), read_json(PARENT/'verification.json')
    require(manifest['publication_version'] == seal['publication_version'] == 8
        and seal['state'] == 'passed' and manifest['proposals'] == seal['proposals_verified'] == 475
        and seal['new_verified'] == 66 and seal['all_manual_sources_verified'] == 118,
        'Publication 8 independent seal inventory differs')
    require(seal['manifest_sha256'] == PARENT_PINS['manifest.json']
        and seal['proposals_sha256'] == manifest['proposals_sha256'] == PARENT_PINS['proposals.jsonl']
        and seal['artifact_hashes_sha256'] == PARENT_PINS['artifact_hashes.json']
        and seal['generator_code_sha256'] == previous.GENERATOR_SHA
        and seal['verification_script_sha256'] == digest(previous.__file__)
        and seal['validation_report_sha256'] == PARENT_PINS['validation_report.json'],
        'Publication 8 independent hash/code binding differs')
    require(read_json(PARENT/'validation_report.json') == {k: v for k, v in seal.items() if k != 'validation_report_sha256'},
        'Parent report is not the independently sealed successful audit')
    for field in ('all_new_registrations_and_metrics_recomputed', 'all_rejected_attempts_and_metrics_recomputed',
            'manual_exact_sqlite_payloads_and_human_events_verified', 'inherited_manual_current_raw_records_verified',
            'live_dependencies_unchanged', 'authorization_dependencies_unchanged'):
        require(seal[field] is True, f'Publication 8 lacks full inherited audit: {field}')
    require(seal['method'] == 'original' and seal['forced_fill'] is False and seal['thresholds_relaxed'] is False
        and seal['production_database_writes'] == 0, 'Parent transfer policy was not independently verified')
    for name, expected in manifest['frozen_code_sha256'].items():
        require(digest(PROJECT/name) == expected, f'Frozen original code changed: {name}')
    artifacts = read_json(PARENT/'artifact_hashes.json')
    prior.check_artifacts(PARENT, artifacts)
    require(set(previous.file_inventory(PARENT)) == set(artifacts) | {'artifact_hashes.json', 'verification.json',
        'validation_report.json', 'delivery_verification.json', 'pallet_mask_refined_review.zip'},
        'Parent frozen file inventory coverage differs')
    old = indexed(read_jsonl(PARENT/'proposals.jsonl'))
    require(len(old) == 475 and Counter(p['class_id'] for p in old.values()) == {1: 100, 2: 100, 9: 90, 10: 85, 15: 100},
        'Expected every one of the 475 parent canonical objects')
    require(sum(p.get('schema') == 'saved_pose_mask_gallery_entry_v1' for p in old.values()) == 118,
        'All 118 parent manual sources are required, not only its latest additions')
    publications[8] = dict(path=PARENT, proposals=old, manifest=manifest)
    requests, request_seal = indexed(read_jsonl(REQUESTS/'requests.jsonl')), read_json(REQUESTS/'verification.json')
    require(set(requests) == REQUEST_KEYS and Counter(r['class_id'] for r in requests.values()) == {9: 10, 10: 15}
        and request_seal['state'] == 'passed' and request_seal['request_count'] == 25
        and request_seal['requests_sha256'] == REQUEST_PINS['requests.jsonl'], 'Exact 25-target completed request scope differs')
    prior.check_artifacts(REQUESTS, request_seal['public_assets'])
    return publications


def check_partition(old, manual, new, combined, requests):
    require(not new, 'Publication 9 must not add machine proposals')
    require(set(manual) == set(requests) and len(manual) == 25, 'All and only the 25 completed requests must be added')
    require(not set(old) & set(manual) and set(combined) == set(old) | set(manual) and len(combined) == 500,
        'Expected parent 475 plus actual manual 25, without substitutions')
    for source in (old, manual):
        for key, proposal in source.items():
            require(canonical(combined[key]) == canonical(proposal), f'Canonical object changed: {key}')
    counts = previous.count_frames(list(combined.values()))
    require(all(c['distinct_frames'] == 100 and c['shortfall'] == 0 for c in counts.values()),
        'Each of the five classes needs exactly 100 globally distinct frames')
    return counts


def check_manual_sources(pose, rows, keys):
    """Recover actual human events, tolerating absent legacy false-only flags."""
    direct = previous.independent_human_sources(pose, rows, allow_four=True)
    events = {(key, revision): json.loads(raw) for _, key, revision, raw in pose['events']}
    for key in keys:
        require(key in direct and key in rows, f'Missing current actual manual source: {key}')
        value, source = pose['current'][key], direct[key]
        human = events[(key, source['human_polygon_revision'])]
        require(value.get('state') == human.get('state') == 'draft'
            and value['polygon'] == human['polygon'] == source['polygon'], f'Manual source is not an actual current human draft: {key}')
        for payload in (value, human):
            require(not any(payload.get(f, False) is not False for f in FALSE_FLAGS), f'Manual source falsely confirmed/eligible: {key}')
            require(all(payload.get('source', {}).get(f) == rows[key][f] for f in IDENTITY), f'Manual original source identity differs: {key}')
        require(not unchanged_rectangle(value['polygon'], rows[key]), f'Unchanged default rectangle is not a manual edit: {key}')
    return direct


def check_manual(proposal, request, pose, direct, row, pose_sha, formal_sha, classification_sha):
    key = proposal['target_key']
    require(key in direct and key in pose['current'], f'No actual saved human source: {key}')
    value, source = pose['current'][key], direct[key]
    four = len(value['polygon']) == 4
    require(proposal['source'] == row and proposal['class_id'] == row['final_class_id'] == request['class_id']
        and request['target_key'] == key and all(request['source'].get(f) == row[f] for f in IDENTITY+('viewpoint_candidate',)),
        f'Manual request/class/original source differs: {key}')
    require(proposal['formal_manifest_sha256'] == request['source']['formal_manifest_sha256'] == formal_sha
        and proposal['class_assignments_sha256'] == classification_sha, f'Manual dataset binding differs: {key}')
    require(proposal['polygon'] == value['polygon'] == source['polygon']
        and proposal['vertex_count'] == len(value['polygon']) >= 4
        and not unchanged_rectangle(value['polygon'], row), f'Manual exact human polygon/topology differs: {key}')
    polygon_value(proposal['polygon'], *row['size'])
    require(value.get('state') == 'draft' and proposal['schema'] == 'saved_pose_mask_gallery_entry_v1'
        and proposal['origin'] == POSE_ORIGIN and proposal['human_edited'] is True
        and proposal['status'] == 'human_edited_draft_pending_confirmation'
        and proposal['source_label_kind'] == 'human_mask_source_manual_template_draft'
        and proposal['manual_source_kind'] == 'completed_manual_request', f'Manual status/provenance differs: {key}')
    v1.no_confirmation(proposal, f'Manual {key}')
    for field in ('pose_store_written', 'gallery_store_written', 'machine_transfer_performed', 'new_human_edit_created'):
        require(proposal[field] is False, f'Manual falsely claims a write, transfer or new edit: {key}/{field}')
    require(proposal.get('production_saved', False) is False and proposal['pose_snapshot_saved'] is True
        and proposal['manual_request_sha256'] == sha(request) and proposal['original_pose_url'] == request['pose_annotation_url'],
        f'Manual request/save binding differs: {key}')
    require(proposal['manual_four_point_source'] is four and proposal['propagation_seed_eligible'] is (not four),
        f'Manual coverage versus seed policy differs: {key}')
    field = 'human_source' if four else 'template'
    require(field in proposal and ('template' not in proposal if four else 'human_source' not in proposal),
        f'Four-point source must not pretend to be a template: {key}')
    expected = dict(source, origin=POSE_ORIGIN, pose_review=v1.pose_provenance(source, pose_sha))
    require(proposal[field] == previous.template_binding(expected)
        and proposal['rendering_id'] == NAMESPACE+(':manual_source:' if four else ':')+key,
        f'Manual self-source human-event/rendering binding differs: {key}')
    require(proposal['snapshot_sha256'] == pose_sha and proposal['target_expected_revision'] == value['revision']
        and proposal['target_snapshot_payload_sha256'] == sha(value), f'Manual latest snapshot binding differs: {key}')
    require(proposal['metrics'] == {} and 'continuation' not in proposal
        and 'manual_source_not_machine_transfer' in proposal['flags'], f'Manual invents transfer evidence: {key}')


def check_source_records(records, manual, snapshot, pose_sha, direct):
    """Compare raw SQLite bytes and event IDs, not just equivalent JSON."""
    require(set(records) == set(manual), 'Exact manual source freeze must cover every counted source')
    core = {}
    with closing(sqlite3.connect(snapshot.resolve().as_uri()+'?mode=ro&immutable=1', uri=True)) as db:
        db.execute('PRAGMA query_only=ON')
        for key, proposal in manual.items():
            current = db.execute('SELECT revision,payload FROM annotations WHERE target_key=?', (key,)).fetchall()
            events = db.execute('SELECT id,revision,payload FROM events WHERE target_key=? AND revision=?',
                (key, direct[key]['human_polygon_revision'])).fetchall()
            require(len(current) == len(events) == 1, f'Ambiguous exact manual SQLite source: {key}')
            revision, raw = current[0]
            event_id, event_revision, event_raw = events[0]
            value = json.loads(raw)
            expected = dict(schema='saved_pose_mask_source_freeze_v1', target_key=key, snapshot_sha256=pose_sha,
                current_revision=revision, current_payload_sha256=sha(value), current_payload_raw=raw, current_payload=value,
                current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), human_event_id=event_id,
                human_polygon_revision=event_revision, human_event_sha256=hashlib.sha256(event_raw.encode()).hexdigest(),
                human_event_payload_raw=event_raw, human_event_payload=json.loads(event_raw), polygon_sha256=sha(value['polygon']))
            record = {k: v for k, v in records[key].items()
                if k not in ('inherited_manual_source', 'legacy_source_audit', 'latest_human_source')}
            if 'vertex_count' in record or 'manual_four_point_source' in record or 'propagation_seed_eligible' in record:
                expected.update(vertex_count=len(value['polygon']), manual_four_point_source=len(value['polygon']) == 4,
                    propagation_seed_eligible=len(value['polygon']) > 4)
            require(record == expected, f'Exact raw SQLite current payload/human event freeze differs: {key}')
            require(direct[key]['human_event_sha256'] == expected['human_event_sha256']
                and direct[key]['polygon'] == value['polygon'], f'Frozen record is not the latest actual human source: {key}')
            if len(value['polygon']) == 4:
                require(proposal.get('manual_four_point_source') is True and proposal.get('propagation_seed_eligible') is False
                    and 'human_source' in proposal and 'template' not in proposal, f'Four-point manual source falsely used as a template: {key}')
            core[key] = record
    return core


def check_inherited_manual(out, inherited, pose, direct, records):
    audits = indexed(read_jsonl(out/'inherited_manual_source_audit.jsonl'))
    require(set(audits) == set(inherited) and len(inherited) == 118, 'Every one of the 118 inherited manual sources needs a current audit')
    changed = []
    for key, proposal in inherited.items():
        value = pose['current'][key]
        # Old canonical records legitimately predate the explicit GT flag.
        require(not any(proposal.get(f, False) is not False for f in FALSE_FLAGS), f'Inherited manual falsely confirmed/eligible: {key}')
        expected = dict(target_key=key, original_proposal_sha256=sha(proposal), historical_revision=proposal['target_expected_revision'],
            current_revision=value['revision'], current_payload_sha256=sha(value), latest_active_direct_human=True,
            source_changed=sha(value) != proposal['target_snapshot_payload_sha256'], polygon_changed=value['polygon'] != proposal['polygon'],
            historical_assets_preserved=True, vertex_count=len(value['polygon']),
            propagation_seed_eligible=len(value['polygon']) > 4, canonical_proposal_overwritten=False)
        require(key in direct and audits[key] == expected, f'Inherited manual current/historical audit differs: {key}')
        require(records[key].get('inherited_manual_source') is True and records[key].get('legacy_source_audit') == expected,
            f'Inherited manual current-source overlay differs: {key}')
        source = direct[key]
        metadata = previous.template_binding(dict(source, origin=POSE_ORIGIN,
            pose_review=v1.pose_provenance(source, records[key]['snapshot_sha256'])))
        require(records[key].get('latest_human_source') == metadata, f'Inherited latest actual human provenance differs: {key}')
        if expected['source_changed']:
            changed.append(key)
    return sorted(changed)


def independent_gallery(gallery, pose, rows, old, publications, formal_sha, gallery_sha):
    """Bind each current human save to its actual historical publication."""
    from pallet.mask_gallery_review import VERSION as EDITOR_VERSION
    result = {}
    for key, value in gallery['current'].items():
        version = value['proposal_publication']
        require(type(version) is int and version in publications and key in old and key in rows,
            f'Unknown gallery historical source: {key}')
        pub, row = publications[version], rows[key]
        proposal = pub['proposals'].get(key)
        require(proposal is not None and proposal == old[key] and proposal['source'] == row, f'Gallery historical canonical source differs: {key}')
        proposal_sha = digest(pub['path']/'proposals.jsonl')
        source = {f: row[f] for f in ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
        source.update(formal_manifest_sha256=formal_sha, image_sha256=digest(pub['path']/'images'/f'{key}.png'))
        require(value['base_proposal_id'] == sha(proposal) and value['proposal_polygon'] == proposal['polygon']
            and value['template_provenance'] == proposal['template']
            and value['binding'] == sha(dict(formal=formal_sha, proposals=proposal_sha, editor_version=EDITOR_VERSION)),
            f'Gallery actual publication/proposal binding differs: {key}')
        require(value['source'] == source and value['source_binding'] == sha(source)
            and digest(PARENT/'images'/f'{key}.png') == source['image_sha256'], f'Gallery original image/source binding differs: {key}')
        require(value['event_kind'] == 'human_mask_gallery_revision' and value['human_edited'] is True
            and value.get('state') not in ('deleted', 'deferred') and value['vertex_count'] == len(value['polygon']) > 4,
            f'Gallery record is not an active actual saved human contour: {key}')
        for flag in (*FALSE_FLAGS, 'pose_store_written', 'original_proposal_modified'):
            require(value.get(flag, False) is False, f'Gallery source falsely confirmed/eligible/written: {key}/{flag}')
        require(value['pose'] is None and pose['current'].get(key, {}).get('state') not in ('deleted', 'deferred'),
            f'Gallery source has invalid pose/source state: {key}')
        polygon_value(value['polygon'], *row['size'])
        current = pose['current'].get(key)
        result[key] = dict(row=row, polygon=value['polygon'], revision=current['revision'] if current else 0,
            current_payload_sha256=sha(current) if current else None, human_polygon_revision=value['revision'],
            human_event_sha256=sha(value), human_mask_confirmed=False, origin=GALLERY_ORIGIN,
            gallery_review=dict(target_key=key, revision=value['revision'], payload_sha256=sha(value), source_binding=sha(source),
                polygon_sha256=sha(value['polygon']), snapshot_sha256=gallery_sha, proposal_publication=version,
                publication_proposals_sha256=proposal_sha))
    return result


def check_review_dispositions(records, scope, old84, old, gallery, statement_sha=None):
    """Scope is the frozen 45, not a generic statement about every unsaved mask."""
    require(len(scope) == 45 and set(scope) == set(old84)-set(gallery['current']) and len(old84) == 84,
        'No-change scope is not exactly the unsaved 45 of publication 7 new84')
    require(set(records) == set(scope), 'No-change dispositions must cover exactly the explicitly authorized old 45')
    for key, value in records.items():
        proposal, original = old[key], scope[key]
        require(proposal == old84[key] and original['class_id'] == proposal['class_id']
            and original['frame_id'] == proposal['source']['frame_id'] and original['status'] == 'no_server_review_saved'
            and key not in gallery['history_keys'], f'No-change source is not the frozen never-saved historical candidate: {key}')
        require(value['target_key'] == key and value['class_id'] == proposal['class_id']
            and value['frame_id'] == proposal['source']['frame_id'], f'No-change disposition identity differs: {key}')
        require(value['disposition'] == 'user_reviewed_no_change'
            and value['origin'] == 'explicit_user_statement_in_conversation' and value['human_edited'] is False,
            f'No-change statement falsely claims a saved human edit: {key}')
        for field in (*FALSE_FLAGS, 'propagation_seed_eligible', 'seed_authorization', 'creates_gallery_revision',
                'pose_store_written', 'gallery_store_written'):
            require(value.get(field, False) is False, f'No-change review grants unauthorized confirmation/seed/write: {key}/{field}')
        require(value['source_publication'] == 7 and value['source_proposal_id'] == value['source_proposal_sha256'] == sha(proposal)
            and value['polygon_sha256'] == sha(proposal['polygon']) and value['source_missing_review_record_sha256'] == sha(original),
            f'No-change review does not bind the exact unchanged parent proposal/polygon: {key}')
        if statement_sha is not None:
            require(value['user_statement_sha256'] == statement_sha, f'No-change record statement binding differs: {key}')
        require(not any(field in value for field in ('human_event_id', 'human_event_sha256', 'human_polygon_revision', 'sqlite_event_id')),
            f'Conversation statement must not fabricate a SQLite human event: {key}')


def expected_counts(old, inherited, manual, gallery, dispositions):
    combined = {**old, **manual}
    require(not set(inherited) & set(gallery['current']), 'Gallery and pose human coverage must be key-deduplicated')
    result = {}
    for cls in CLASSES:
        keys = {k for k, p in combined.items() if p['class_id'] == cls}
        old_keys = keys & set(old)
        pose_keys = keys & (set(inherited) | set(manual))
        gallery_keys = keys & set(gallery['current'])
        actual = pose_keys | gallery_keys
        no_change = keys & set(dispositions)
        require(not actual & no_change, 'No-change reviews are not newly edited/saved human masks')
        four = {k for k in pose_keys if len(combined[k]['polygon']) == 4}
        result[str(cls)] = dict(old_total=len(old_keys), manual_added=len(keys & set(manual)),
            manual_requested=len(keys & set(manual)), inherited_manual_sources=len(keys & set(inherited)),
            saved_gallery_human=len(gallery_keys), saved_pose_human=len(pose_keys), saved_human=len(actual),
            saved_fine_edits=len(actual-four), manual_four_point_sources=len(four),
            user_reviewed_no_change=len(no_change), old_pending_machine=len(keys-actual), pending_machine_raw=len(keys-actual),
            no_new_server_review=len(keys-actual-no_change),
            new_candidates=0, new_machine_pending=0, total=len(keys), preview=len(keys), distinct_frames=len(keys),
            missing=100-len(keys), goal=100, target_per_class=100)
    return result


class ReportControls(prior.ReportInventory):
    def __init__(self):
        super().__init__()
        self.inputs, self.selects, self.options, self.scripts, self.card_text = {}, {}, {}, [], {}
        self.select, self.article, self.in_script, self.option = None, None, False, None

    def handle_starttag(self, tag, attrs):
        super().handle_starttag(tag, attrs)
        attrs = dict(attrs)
        if tag == 'input' and 'id' in attrs:
            require(attrs['id'] not in self.inputs, 'Duplicate report input ID')
            self.inputs[attrs['id']] = attrs
        if tag == 'select':
            self.select = attrs.get('id')
            self.selects[self.select] = attrs
            self.options[self.select] = []
        if tag == 'option' and self.select:
            attrs['_text'] = ''
            self.options[self.select].append(attrs)
            self.option = attrs
        if tag == 'article':
            self.article = attrs['id']
            self.card_text[self.article] = ''
        if tag == 'script':
            self.in_script = True

    def handle_data(self, data):
        super().handle_data(data)
        if self.article:
            self.card_text[self.article] += data
        if self.in_script:
            self.scripts.append(data)
        if self.option is not None:
            self.option['_text'] += data

    def handle_endtag(self, tag):
        super().handle_endtag(tag)
        if tag == 'select':
            self.select = None
        if tag == 'article':
            self.article = None
        if tag == 'script':
            self.in_script = False
        if tag == 'option':
            self.option = None


def check_report_controls(page, combined, manual, inherited, gallery, dispositions):
    parsed = ReportControls()
    parsed.feed(page)
    cards = indexed(parsed.articles, 'id')
    require(set(cards) == {'target-'+k for k in combined}, 'Report must contain all 500 cumulative cards')
    only = parsed.inputs.get('only-new', {})
    require(only.get('type') == 'checkbox' and 'checked' not in only and 'disabled' not in only,
        'Report must default to all cards, only-new unchecked')
    options = parsed.options.get('filter', [])
    require(options and options[0].get('value') == '' and not any('selected' in o for o in options[1:]),
        'Report class filter must default to ALL')
    require([o['_text'].strip() for o in options[1:]] == [str(cls) for cls in CLASSES],
        'Report class control must offer exactly the five requested classes')
    for key, proposal in combined.items():
        card = cards['target-'+key]
        require(card['data-class'] == str(proposal['class_id']) and 'hidden' not in card
            and 'display:none' not in card.get('style', '').replace(' ', '').lower(), f'Report card is mislabeled or hidden by default: {key}')
        require(card['data-kind'] == ('manual' if key in manual or key in inherited else 'machine')
            and card['data-batch'] == ('new' if key in manual else 'previous'), f'Report manual/batch tag differs: {key}')
        if key in manual:
            require(re.search(r'本轮计入(?:已保存)?人工', parsed.card_text['target-'+key]) is not None,
                f'Newly counted human card lacks its honest label: {key}')
    script = re.sub(r'\s+', '', ''.join(parsed.scripts))
    expected_script = ("const cls=document.querySelector('#filter'),only=document.querySelector('#only-new');"
        "function filter(){document.querySelectorAll('article').forEach(a=>a.hidden="
        "(!!cls.value&&a.dataset.class!==cls.value)||(only.checked&&a.dataset.batch!=='new'));}"
        "cls.onchange=only.onchange=filter;filter();")
    require(script == re.sub(r'\s+', '', expected_script),
        'Report controls must implement ALL-by-default and only-new selecting exactly the 25 manual cards')
    return parsed


def check_report(out, combined, manual, inherited, gallery, dispositions, counts):
    from fontTools.ttLib import TTFont
    page = (out/'report_zh.html').read_text()
    parsed = check_report_controls(page, combined, manual, inherited, gallery, dispositions)
    total = lambda field: sum(c[field] for c in counts.values())
    parent66 = indexed(read_jsonl(PARENT/'new_proposals.jsonl'))
    reviewed66 = len(set(parent66) & set(gallery['current']))
    require(all(text in page for text in ('500', '143', '45', f'{reviewed66}/{len(parent66)}',
        MISSING_CURRENT_KEY, '未确认真值', '四点', '不作传播模板')), 'Report omits honest saved/no-change/unsaved or four-point status')
    summaries = (f'已保存人工轮廓 {total("saved_human")} 份 = 图册 {total("saved_gallery_human")} + 原位姿 {total("saved_pose_human")}',
        f'本轮计入人工 {len(manual)} 份', f'原始数 {total("pending_machine_raw")} 份',
        f'{len(dispositions)} 份为用户已复核无需修改，另 {total("no_new_server_review")} 份未见新服务器复核修订')
    require(all(text in page for text in summaries), 'Report saved-human versus no-change/unsaved summaries differ from actual snapshots')
    cells = []
    for cls in CLASSES:
        values = counts[str(cls)]
        cells.extend(str(n) for n in (cls, *(values[f] for f in ('old_total', 'manual_added', 'saved_gallery_human',
            'saved_pose_human', 'saved_human', 'user_reviewed_no_change', 'no_new_server_review', 'total', 'missing'))))
    require(parsed.cells == cells, 'Report exact actual-human/no-change/unsaved per-class table differs')
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    with TTFont(out/'report-font.otf') as font:
        require(required <= set(font.getBestCmap()), 'Chinese report font coverage incomplete')
    coverage = read_json(out/'font_coverage.json')
    require(coverage['all_report_Chinese_present'] is True and coverage['chinese_glyphs'] == len(required)
        and coverage['font_sha256'] == digest(out/'report-font.otf'), 'Report font evidence differs')
    require((out/'FONT_LICENSE.txt').is_file(), 'Missing Chinese report font license')
    return parsed


def check_preserved_assets(out, old):
    inventory = read_json(out/'preserved_parent_assets.json')
    expected = {f'{folder}/{key}.{ext}' for key in old for folder, ext in
        (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json'))}
    namespaces = ('templates', 'refined', 'generation', 'quota_generation', 'quota100_generation', 'quota100_continue_generation')
    expected |= {str(p.relative_to(PARENT)) for name in namespaces for p in (PARENT/name).rglob('*') if p.is_file()}
    expected.update(('vertex2_before_after.png', 'FONT_LICENSE.txt'))
    require(set(inventory) == expected, 'Preserved parent 475 assets/all historical namespaces inventory differs')
    for name, expected_sha in inventory.items():
        require(digest(out/name) == digest(PARENT/name) == expected_sha, f'Historical parent asset changed: {name}')
    return len(inventory)


def check_archives(out, publications, manifest):
    require(read_json(out/'parent_file_inventory.json') == previous.file_inventory(PARENT), 'Full frozen parent-file inventory differs')
    archive = previous.file_inventory(out/'parent_evidence')
    required = {'manifest.json', 'verification.json', 'proposals.jsonl', 'manual_proposals.jsonl',
        'manual_source_records.jsonl', 'all_manual_sources.jsonl', 'new_proposals.jsonl', 'template_inventory.json'}
    parent_files = previous.file_inventory(PARENT)
    require(required <= set(archive) <= set(parent_files) and all(parent_files[n] == h for n, h in archive.items()),
        'Frozen parent canonical/source/seal archive differs')
    require(read_json(out/'configuration_input_hashes.json') == previous.file_inventory(PROJECT/'configs'),
        'Configuration dependencies changed during completion')
    require(read_json(out/'request_file_inventory.json') == previous.file_inventory(REQUESTS), 'Frozen exact manual requests changed')
    require(read_json(out/'source_publication_file_inventories.json') ==
        {str(v): previous.file_inventory(pub['path']) for v, pub in publications.items() if v != 8},
        'Frozen historical gallery publications changed')
    code = manifest['frozen_code_sha256']
    require(previous.file_inventory(out/'frozen_code') == code and code.get(GENERATOR) == digest(PROJECT/GENERATOR),
        'Frozen completion code archive differs')
    require(set(read_json(PARENT/'manifest.json')['frozen_code_sha256']) | {GENERATOR} <= set(code),
        'Frozen completion code omits inherited original engine dependencies')
    for name, expected in code.items():
        require(digest(PROJECT/name) == expected, f'Completion code dependency changed: {name}')


def check_snapshot_manifest(out, manifest, snapshots, frozen, publications, formal_sha):
    from pallet.mask_gallery_review import VERSION as EDITOR_VERSION
    value = read_json(out/'snapshot_manifest.json')
    require(value['formal_manifest_sha256'] == formal_sha, 'Snapshot manifest formal binding differs')
    for kind, path in snapshots.items():
        store = frozen[kind]
        expected = dict(path=path.name, backup='backups/'+path.name, sha256=digest(path),
            saved=len(store['current']), events=len(store['events']))
        require(value[kind] == expected, f'{kind} snapshot/backup manifest differs')
        prefix = 'snapshot' if kind == 'pose' else 'gallery_snapshot'
        require(manifest[prefix+'_saved'] == expected['saved'] and manifest[prefix+'_events'] == expected['events'],
            f'{kind} frozen snapshot counters differ')
    require(value['manual_requests'] == dict(directory=str(REQUESTS), count=25,
        frozen_input_sha256=previous.file_inventory(REQUESTS)), 'Snapshot exact 25-target request binding differs')
    expected = {}
    for version, pub in publications.items():
        proposal_sha = digest(pub['path']/'proposals.jsonl')
        expected[str(version)] = dict(path=str(pub['path']), proposals_sha256=proposal_sha,
            manifest_sha256=digest(pub['path']/'manifest.json'), verification_sha256=digest(pub['path']/'verification.json'),
            binding=sha(dict(formal=formal_sha, proposals=proposal_sha, editor_version=EDITOR_VERSION)))
    require(value['source_publications'] == expected, 'Snapshot exact historical gallery-publication bindings differ')


def check_gallery_source_records(out, snapshot, gallery, gallery_sha):
    records = indexed(read_jsonl(out/'gallery_source_records.jsonl'))
    require(set(records) == set(gallery['current']), 'Exact saved-gallery source freeze inventory differs')
    with closing(sqlite3.connect(snapshot.resolve().as_uri()+'?mode=ro&immutable=1', uri=True)) as db:
        db.execute('PRAGMA query_only=ON')
        for key, value in gallery['current'].items():
            current = db.execute('SELECT revision,payload FROM reviews WHERE target_key=?', (key,)).fetchall()
            events = db.execute('SELECT id,payload FROM events WHERE target_key=? AND revision=?', (key, value['revision'])).fetchall()
            require(len(current) == len(events) == 1, f'Ambiguous exact gallery SQLite source: {key}')
            revision, raw = current[0]
            event_id, event_raw = events[0]
            expected = dict(target_key=key, revision=revision, current_payload_raw=raw,
                current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), current_payload=json.loads(raw),
                human_event_id=event_id, human_event_payload_raw=event_raw,
                human_event_sha256=hashlib.sha256(event_raw.encode()).hexdigest(), snapshot_sha256=gallery_sha)
            require(records[key] == expected and json.loads(event_raw) == value, f'Exact raw gallery current/event bytes differ: {key}')


def check_statement(out, scope, manifest):
    statement = read_json(out/'review_disposition_statement.json')
    text = statement['user_statement']
    require(isinstance(text, str) and bool(text.strip()), 'Explicit conversation statement must not be fabricated or empty')
    statement_sha = hashlib.sha256(text.encode()).hexdigest()
    require(statement['schema'] == 'explicit_conversation_review_statement_v1'
        and statement['origin'] == 'explicit_user_statement_in_conversation'
        and statement['user_statement_sha256'] == statement_sha
        and type(statement['user_statement_is_verbatim']) is bool
        and statement['summary_is_not_verbatim_quotation'] is True,
        'Conversation statement origin/hash/quotation honesty differs')
    require(statement['scope_count'] == 45 and statement['scope_target_keys'] == sorted(scope)
        and statement['scope_source'] == str(DISPOSITION_INPUT.relative_to(PROJECT))
        and statement['scope_source_sha256'] == DISPOSITION_INPUT_SHA
        and statement['source_publication'] == 7 and statement['source_batch_size'] == 84
        and statement['no_other_targets_authorized'] is True, 'Conversation authorization must not expand beyond exact old45')
    for field in ('creates_gallery_revision', 'human_edited', 'human_mask_confirmed', 'seed_authorization'):
        require(statement[field] is False, f'Conversation statement cannot create edits/confirmation/seed rights: {field}')
    require(statement['production_database_writes'] == 0, 'Conversation statement must not create SQLite events')
    require((out/'disposition_scope_source.jsonl').read_bytes() == DISPOSITION_INPUT.read_bytes()
        and manifest['review_disposition_statement_sha256'] == digest(out/'review_disposition_statement.json'),
        'Conversation exact scope/statement artifact binding differs')
    return statement_sha


def check_saved_review_status(out, manifest, inventory, old, inherited, manual, gallery, dispositions, parent66):
    saved = set(gallery['current']) | set(inherited) | set(manual)
    raw_pending = set(old)-saved
    pending = set(parent66)-set(gallery['current'])
    expected = dict(parent_publication=8, parent_new_machine_candidates=len(parent66),
        actually_saved=len(set(parent66) & set(gallery['current'])), pending=len(pending),
        saved_keys=sorted(set(parent66) & set(gallery['current'])), pending_keys=sorted(pending),
        current_batch_user_reported_reviewed=True, current_batch_claim_is_not_save_evidence=True,
        older_pending_excluding_acknowledged=sorted(raw_pending-set(dispositions)-pending),
        no_new_server_review_keys=sorted(raw_pending-set(dispositions)), pending_machine_raw=len(raw_pending),
        user_reviewed_no_change=len(dispositions), no_new_server_review=len(raw_pending-set(dispositions)), unsaved_candidates_are_seeds=False,
        user_completion_claim_used_as_saved_mask_evidence=False, no_inference_user_did_not_view=True)
    require(read_json(out/'saved_review_status.json') == manifest['saved_review_status'] == inventory['saved_review_status'] == expected,
        'Current actual saved/unsaved and old45/other historical review dispositions must remain distinct and honest')
    return expected


def check_previously_missing_save(gallery, parent66):
    """A late real save is evidence, unlike a claimed or synthesized event."""
    key = MISSING_CURRENT_KEY
    require(key in parent66 and parent66[key]['class_id'] == 15
        and parent66[key]['source']['frame_id'] == MISSING_CURRENT_FRAME, 'Known original late-save candidate identity differs')
    if key not in gallery['current']:
        return False
    first = [json.loads(raw) for _, k, revision, raw in gallery['events'] if k == key and revision == 1]
    require(len(first) == 1 and first[0]['event_kind'] == 'human_mask_gallery_revision'
        and first[0]['human_edited'] is True and first[0]['proposal_publication'] == 8
        and first[0]['base_proposal_id'] == sha(parent66[key])
        and first[0]['proposal_polygon'] == parent66[key]['polygon'],
        'Previously missing current-batch save must have an actual revision-1 human gallery event bound to its parent proposal')
    for field in FALSE_FLAGS:
        require(first[0].get(field, False) is False, f'Late gallery save must not infer confirmation/eligibility: {field}')
    return True


def check_public_archive(out, manifest, combined):
    names = {'report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt', 'preview_all_classes.png',
        'proposals.jsonl', 'new_proposals.jsonl', 'manual_proposals.jsonl', 'review_dispositions.jsonl', 'vertex2_before_after.png'}
    names |= {f'{directory}/{key}.{ext}' for key in combined for directory, ext in
        (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json'))}
    path = out/'pallet_mask_refined_review.zip'
    expected = {name: digest(out/name) for name in sorted(names)}
    with zipfile.ZipFile(path) as archive:
        require(len(archive.namelist()) == len(names) and set(archive.namelist()) == names and archive.testzip() is None,
            'Public ZIP must use the exact allowlist, with no SQLite/events/keys/backups/configuration')
        for name, expected_sha in expected.items():
            require(hashlib.sha256(archive.read(name)).hexdigest() == expected_sha, f'Public ZIP file bytes differ: {name}')
    require(read_json(out/'public_archive_inventory.json') == dict(files=expected, archive=path.name,
        archive_sha256=digest(path), private_files_included=False, sqlite_included=False), 'Public ZIP audited inventory differs')
    require(manifest['archive_sha256'] == digest(path) and manifest['archive_public_files'] == len(names)
        and manifest['archive_private_files'] == 0, 'Manifest public archive inventory differs')


def check_live_stability(frozen, live, source_keys):
    """Append-only unrelated user work is allowed; counted sources must be stable."""
    for kind in frozen:
        prior.preserve_events(frozen[kind], live[kind])
        require(set(frozen[kind]['current']) <= set(live[kind]['current']), f'{kind} current record disappeared')
        for key, value in frozen[kind]['current'].items():
            require(live[kind]['current'].get(key) == value, f'{kind} source current payload changed since snapshot: {key}')
    for key in source_keys:
        for kind in frozen:
            require(live[kind]['current'].get(key) == frozen[kind]['current'].get(key),
                f'{kind} counted human/review dependency changed at seal: {key}')
    # Newly appended gallery saves change coverage/dispositions even on old keys
    # absent from the original snapshot. Unrelated new records are harmless.
    require(not ((set(live['gallery']['current'])-set(frozen['gallery']['current'])) & set(source_keys)),
        'New gallery save changes a counted review disposition; compose a fresh snapshot')


def audit(out, pose_live, gallery_live):
    verifier_sha, config_hashes = digest(__file__), v1.configuration_hashes()
    pose_config = read_json(PROJECT/'configs/active_pose_review.json')
    gallery_config = read_json(PROJECT/'configs/active_mask_gallery.json')
    require(Path(pose_live).resolve() == (PROJECT/pose_config['database']).resolve()
        and Path(gallery_live).resolve() == (PROJECT/gallery_config['review_directory']/'mask_reviews.sqlite3').resolve(),
        'Live inputs must be the configured production read-only review stores')
    require((PROJECT/gallery_config['directory']).resolve() == PARENT.resolve() and gallery_config['publication_version'] == 8
        and gallery_config['verification_sha256'] == PARENT_PINS['verification.json'], 'Parent gallery must remain active until publication-9 sealing')
    request_config = read_json(PROJECT/'configs/active_mask_template_requests.json')
    require((PROJECT/request_config['directory']).resolve() == REQUESTS.resolve(), 'Exact 25-item request queue must remain active during audit')
    publications = check_frozen_inputs()
    old, parent_manifest = publications[8]['proposals'], publications[8]['manifest']
    manifest, artifacts = read_json(out/'manifest.json'), read_json(out/'artifact_hashes.json')
    artifact_sha = digest(out/'artifact_hashes.json')
    prior.check_artifacts(out, artifacts)
    files = {str(p.relative_to(out)) for p in out.rglob('*') if p.is_file()
        and str(p.relative_to(out)) not in ('artifact_hashes.json', 'validation_report.json')}
    require(files == set(artifacts), 'Artifact hash inventory must cover every generated file')
    require(manifest['publication_version'] == 9 and manifest['target_per_class'] == 100 and manifest['target_total'] == 500,
        'Expected publication 9 exact 500-frame quota completion')
    require((PROJECT/manifest['parent']).resolve() == PARENT.resolve()
        and manifest['parent_manifest_sha256'] == PARENT_PINS['manifest.json']
        and manifest['parent_proposals_sha256'] == PARENT_PINS['proposals.jsonl']
        and manifest['parent_verification_sha256'] == PARENT_PINS['verification.json'], 'Publication 8 parent binding differs')
    require(manifest['code_sha256'] == digest(PROJECT/GENERATOR) == GENERATOR_SHA, 'Reviewed completion generator code binding differs')
    check_archives(out, publications, manifest)
    for flag in ('thresholds_relaxed', 'recursive_candidate_propagation', 'automatic_human_confirmation',
            'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible', 'forced_fill',
            'manual_source_snapshots_imported_into_gallery_store'):
        require(manifest[flag] is False, f'Completion manifest {flag} must remain false')
    for field in ('production_database_writes', 'configuration_writes', 'service_writes', 'new_human_edits_created',
            'research_candidates_added', 'registration_attempts', 'scanned_eligible_targets', 'accepted_before_diversity',
            'cross_split_template_transfers'):
        require(manifest[field] == 0, f'Completion must not create writes/edits/research/transfers: {field}')
    require(manifest['method'] == 'saved_human_composition_only' and manifest['machine_transfer_performed'] is False
        and manifest['template_inventory_is_source_audit_only'] is True and manifest['original_splits_preserved'] is True
        and manifest['review_dispositions_are_saved_human_masks'] is False
        and manifest['review_dispositions_seed_authorization'] is False,
        'Completion must not claim machine transfer or conversation-as-saved-human evidence')
    for name in ('proposals', 'new_proposals', 'manual_proposals', 'manual_source_records', 'all_manual_sources',
            'inherited_manual_source_audit', 'requests', 'refined_annotations', 'review_dispositions', 'template_inventory',
            'gallery_source_records', 'registration_attempts', 'accepted_candidates'):
        extension = '.json' if name == 'template_inventory' else '.jsonl'
        require(digest(out/(name+extension)) == manifest[name+'_sha256'], f'Manifest artifact hash differs: {name}')
    combined, manual, new = (indexed(read_jsonl(out/n)) for n in ('proposals.jsonl', 'manual_proposals.jsonl', 'new_proposals.jsonl'))
    requests = indexed(read_jsonl(REQUESTS/'requests.jsonl'))
    counts = check_partition(old, manual, new, combined, requests)
    require((out/'new_proposals.jsonl').read_bytes() == b'', 'New machine sidecar must be genuinely empty')
    for name in ('accepted_candidates.jsonl', 'registration_attempts.jsonl'):
        if (out/name).exists():
            require((out/name).read_bytes() == b'', 'Completion must not perform any new machine registration')
    inherited = {k: p for k, p in old.items() if p.get('schema') == 'saved_pose_mask_gallery_entry_v1'}
    all_manual = {**inherited, **manual}
    require(len(inherited) == 118 and len(all_manual) == 143, 'Expected all 118 inherited plus actual 25 manual sources')
    require((out/'requests.jsonl').read_bytes() == (REQUESTS/'requests.jsonl').read_bytes(), 'Copied exact 25-target requests differ')
    for folder, ext in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
        require({p.name for p in (out/folder).iterdir()} == {k+'.'+ext for k in combined}, f'Published 500-frame asset inventory differs: {folder}')
    preserved = check_preserved_assets(out, old)
    formal = (PROJECT/read_json(PROJECT/'configs/active_formal_dataset.json')['dataset']).resolve()
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    formal_sha, classification_sha = digest(formal/'manifest.json'), digest(classification)
    require(formal_sha == manifest['formal_manifest_sha256'] == parent_manifest['formal_manifest_sha256']
        and classification_sha == manifest['classification_sha256'] == parent_manifest['classification_sha256'], 'Formal/classification binding changed')
    protected = read_json(out/'protected_formal_before.json')
    require(len(protected) == 20768 and protected == read_json(PARENT/'protected_formal_before.json')
        == protected_snapshot(formal) == read_json(out/'protected_formal_after.json'), 'Protected 20,768 formal files changed')
    rows, sources = prior.formal_rows(formal, classification)
    require(len(rows) == 12344, 'Expected all original 12,344 formal identities')
    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']
    pose_sha, gallery_sha = digest(snapshots['pose']), digest(snapshots['gallery'])
    require(len(pose['current']) >= 4792 and len(pose['events']) >= 10962
        and len(gallery['current']) >= parent_manifest['gallery_snapshot_saved']
        and len(gallery['events']) >= parent_manifest['gallery_snapshot_events'], 'Snapshots predate completed actual gallery/manual saves')
    require(json.loads(pose['metadata']['binding'])['formal_manifest_sha256'] == formal_sha
        and gallery['metadata']['formal_manifest_sha256'] == formal_sha, 'Snapshot formal dataset binding differs')
    check_snapshot_manifest(out, manifest, snapshots, frozen, publications, formal_sha)
    for kind, path in prior.find_snapshots(PARENT, parent_manifest).items():
        prior.preserve_events(prior.read_store(path, kind, frozen=True), frozen[kind])
    direct = check_manual_sources(pose, rows, all_manual)
    for key, proposal in manual.items():
        check_manual(proposal, requests[key], pose, direct, rows[key], pose_sha, formal_sha, classification_sha)
        frame_source = sources[rows[key]['frame_id']]
        require(requests[key]['source']['image'] == frame_source['image']
            and requests[key]['source']['image_sha256'] == digest(formal/frame_source['image']), f'Manual request original image differs: {key}')
    records = indexed(read_jsonl(out/'all_manual_sources.jsonl'))
    current_records = check_source_records(records, all_manual, snapshots['pose'], pose_sha, direct)
    added_records = indexed(read_jsonl(out/'manual_source_records.jsonl'))
    require(added_records == {k: current_records[k] for k in manual}
        and added_records == {k: records[k] for k in manual}, 'New manual raw-source partition differs')
    changed = check_inherited_manual(out, inherited, pose, direct, records)
    require(manifest['inherited_manual_source_revisions_changed'] == changed, 'Inherited manual source revision audit differs')
    for key, revision, raw_sha in ((FOUR_VERTEX_KEY, 8, FOUR_VERTEX_RAW_SHA),
            (OLD_FOUR_VERTEX_KEY, 6, previous.FOUR_VERTEX_RAW_SHA)):
        require(len(direct[key]['polygon']) == 4 and direct[key]['revision'] == direct[key]['human_polygon_revision'] == revision
            and current_records[key]['human_event_sha256'] == current_records[key]['current_payload_raw_sha256'] == raw_sha,
            f'Known actual modified four-point source/revision/raw bytes differ: {key}')
    require({k for k in all_manual if len(pose['current'][k]['polygon']) == 4} == {FOUR_VERTEX_KEY, OLD_FOUR_VERTEX_KEY},
        'Expected actual manual four-point sources only, without invented vertices')
    gallery_templates = independent_gallery(gallery, pose, rows, old, publications, formal_sha, gallery_sha)
    require(len(gallery_templates) == len(gallery['current']) and indexed(read_jsonl(out/'refined_annotations.jsonl')) == gallery['current'],
        'Actual current saved gallery contour inventory differs')
    check_gallery_source_records(out, snapshots['gallery'], gallery, gallery_sha)
    old84 = indexed(read_jsonl(previous.PARENT/'new_proposals.jsonl'))
    dispositions = indexed(read_jsonl(out/'review_dispositions.jsonl'))
    scope = indexed(read_jsonl(DISPOSITION_INPUT))
    statement_sha = check_statement(out, scope, manifest)
    check_review_dispositions(dispositions, scope, old84, old, gallery, statement_sha)
    parent66 = indexed(read_jsonl(PARENT/'new_proposals.jsonl'))
    require(len(parent66) == 66, 'Latest parent machine batch must retain all exact 66 candidates')
    late_save_verified = check_previously_missing_save(gallery, parent66)
    classes = expected_counts(old, inherited, manual, gallery, dispositions)
    total = lambda field: sum(c[field] for c in classes.values())
    require(total('saved_human') == len(all_manual)+len(gallery['current'])
        and total('pending_machine_raw') == len(combined)-total('saved_human')
        and total('no_new_server_review') == total('pending_machine_raw')-len(dispositions)
        and total('user_reviewed_no_change') == 45,
        'Actual saved human, exact old45 no-change and remaining non-saved machine masks must partition all 500')
    inventory = read_json(out/'template_inventory.json')
    # Rendering/seed inventory checks are independent of whether a transfer was run.
    templates = check_rendering(out, inventory, read_json(PARENT/'template_inventory.json'), pose, gallery_templates,
        rows, old, inherited, manual, direct, pose_sha, dispositions)
    require(manifest['generation_templates'] == len(templates) and manifest['generation_template_namespace'] == NAMESPACE
        and manifest['generation_templates_by_origin'] == dict(Counter(t['origin'] for t in templates.values())),
        'Manifest direct-human source-audit inventory differs')
    review_status = check_saved_review_status(out, manifest, inventory, old, inherited, manual, gallery, dispositions, parent66)
    check_protection(out, manifest, inventory, pose, gallery, rows, old, all_manual, manual)
    evidence = ImageEvidence(formal, rows, sources)
    for key, proposal in combined.items():
        require(proposal['source'] == rows[key] and proposal['class_id'] == rows[key]['final_class_id']
            and proposal['formal_manifest_sha256'] == formal_sha
            and proposal['class_assignments_sha256'] == classification_sha, f'Cumulative original source binding differs: {key}')
        if key in manual:
            v1.check_assets(out, proposal, rows[key], evidence, manual=True)
    check_counts(manifest, inventory, classes, combined, inherited, manual, gallery, dispositions)
    check_report(out, combined, manual, inherited, gallery, dispositions, classes)
    check_public_archive(out, manifest, combined)
    used = read_json(out/'used_image_hashes.json')
    for name, expected in {**read_json(PARENT/'used_image_hashes.json'), **evidence.used}.items():
        require(used.get(name) == expected, f'Original image evidence omitted or changed: {name}')
    for name, expected in used.items():
        path = (formal/name).resolve()
        require(path.is_relative_to(formal) and digest(path) == expected, f'Used original image changed: {name}')
    require(protected_snapshot(formal) == protected and digest(classification) == classification_sha, 'Protected formal inputs changed during audit')
    for kind, path in snapshots.items():
        require(digest(path) == (pose_sha if kind == 'pose' else gallery_sha), f'{kind} immutable snapshot changed during audit')
    prior.find_snapshots(out, manifest)
    prior.check_artifacts(out, artifacts)
    require(digest(out/'artifact_hashes.json') == artifact_sha, 'Artifact hash inventory changed during audit')
    check_frozen_inputs()
    check_archives(out, publications, manifest)
    live = {'pose': prior.read_store(pose_live, 'pose'), 'gallery': prior.read_store(gallery_live, 'gallery')}
    check_live_stability(frozen, live, combined)
    previous.previous.inherited.check_live_manual_records(pose_live, current_records)
    require(manifest['source_current_stable'] is True and read_json(out/'source_stability_check.json') ==
        dict(state='stable', source_current_stable=True, current_payloads_and_events_unchanged=True, production_database_writes=0),
        'Composition must establish stable current-source evidence')
    require(v1.configuration_hashes() == config_hashes and digest(__file__) == verifier_sha,
        'Authorization/configuration/independent verifier changed during audit')
    return dict(state='passed', publication_version=9, proposals_verified=500, proposals_sha256=manifest['proposals_sha256'],
        manifest_sha256=digest(out/'manifest.json'), artifact_hashes_sha256=artifact_sha, new_verified=0, manual_verified=25,
        manual_added=25, inherited_manual_verified=118, all_manual_sources_verified=143, old_canonical_objects_preserved=475,
        classes=counts, saved_human_sources_verified=total('saved_human'), saved_gallery_human_verified=len(gallery['current']), saved_pose_human_verified=143,
        user_reviewed_no_change_verified=45, old_unreviewed_server_candidates=len(review_status['older_pending_excluding_acknowledged']),
        pending_machine_raw=total('pending_machine_raw'), no_new_server_review=total('no_new_server_review'),
        current_batch_unsaved=review_status['pending'], current_batch_saved=review_status['actually_saved'],
        current_batch_total=66, current_batch_unsaved_keys=review_status['pending_keys'],
        previously_missing_gallery_save_actual_revision1_verified=late_save_verified,
        no_change_reviews_not_human_edits=True, no_change_reviews_not_seed_authorization=True,
        no_change_review_origin='explicit_user_statement_in_conversation', human_confirmation_inferred=False,
        no_change_review_statement_sha256=statement_sha, templates_verified=len(templates),
        manual_exact_sqlite_payloads_and_human_events_verified=True, inherited_manual_current_raw_records_verified=True,
        four_vertex_manual_sources_verified=sorted([FOUR_VERTEX_KEY, OLD_FOUR_VERTEX_KEY]), four_vertex_sources_used_as_seeds=0,
        original_registration_audit_inherited=True, registrations_recomputed_by_completion=0,
        new_machine_proposals_generated=0, manual_sources_not_machine_transfers=True,
        manual_snapshots_not_imported_into_gallery_store=True, inherited_assets_verified=preserved,
        protected_formal_files_verified=20768, original_events_preserved={k: len(v['events']) for k, v in frozen.items()},
        pose_snapshot_sha256=pose_sha, gallery_snapshot_sha256=gallery_sha,
        ground_truth_eligible=False, automatic_training_eligible=False, robot_command_eligible=False,
        production_database_writes=0, configuration_writes=0, service_writes=0, new_human_edits_created=0,
        thresholds_relaxed=False, forced_fill=False, live_dependencies_unchanged=True, authorization_dependencies_unchanged=True,
        extra_public_images=['vertex2_before_after.png'], verification_script_sha256=verifier_sha,
        verified_at=datetime.now(timezone.utc).isoformat())


def check_rendering(out, inventory, parent, pose, gallery_templates, rows, old, inherited, manual, direct, pose_sha, dispositions):
    historical = indexed(parent['templates'], 'rendering_id')
    actual = indexed(inventory['templates'], 'rendering_id')
    require(all(actual.get(k) == v for k, v in historical.items()), 'Historical rendering namespaces/payloads changed')
    pose_seeds = {k: dict(t, origin=POSE_ORIGIN, pose_review=v1.pose_provenance(t, pose_sha))
        for k, t in direct.items() if len(t['polygon']) > 4}
    templates = {**pose_seeds, **gallery_templates}
    templates.pop(previous.QUARANTINE, None)
    pending = set(old)-set(inherited)-set(gallery_templates)
    require(not pending & set(templates) and not set(dispositions) & set(templates),
        'Unreviewed/no-change machine contour used as a direct-human seed')
    additions = {NAMESPACE+':'+k: dict(t, rendering_id=NAMESPACE+':'+k) for k, t in templates.items()}
    require(set(actual) == set(historical) | set(additions) and not set(historical) & set(additions),
        'Latest direct-human versus historical rendering namespace inventory differs')
    for rendering_id, value in additions.items():
        key = value['row']['target_key']
        require(actual[rendering_id] == value and read_json(out/NAMESPACE/'templates'/f'{key}.json') == templates[key],
            f'Latest exact direct-human rendering source differs: {key}')
    require({p.name for p in (out/NAMESPACE/'templates').iterdir()} == {k+'.json' for k in templates},
        'Latest human seed files contain an omitted or non-human/four-point source')
    mapping = dict(parent['rendering_by_target'])
    mapping.update({k: p['rendering_id'] for k, p in manual.items()})
    require(inventory['rendering_by_target'] == mapping, '500-target rendering map changed old mappings or omitted manual sources')
    for key in (FOUR_VERTEX_KEY, OLD_FOUR_VERTEX_KEY):
        require(key not in templates and not any(t['row']['target_key'] == key for t in actual.values()),
            'Actual four-point human source must never enter template/seed inventory')
    four_sources = parent['manual_source_renderings']+[dict(target_key=k, rendering_id=p['rendering_id'], polygon=p['polygon'],
        human_source=p['human_source'], manual_four_point_source=True, propagation_seed_eligible=False)
        for k, p in sorted(manual.items()) if len(p['polygon']) == 4]
    require(inventory['manual_source_renderings'] == four_sources
        and inventory['deferred_manual_template_keys'] == sorted(v['target_key'] for v in four_sources),
        'Both inherited and new four-point manual-only rendering sources must be preserved')
    require(inventory['historical_templates'] == len(historical) and inventory['generation_template_namespace'] == NAMESPACE
        and inventory['generation_template_keys'] == sorted(templates) and inventory['generation_templates'] == len(templates)
        and inventory['generation_templates_by_origin'] == dict(Counter(t['origin'] for t in templates.values())),
        'Direct-human-only source-audit template inventory counts differ')
    require(inventory['template_inventory_is_source_audit_only'] is True and inventory['machine_transfer_performed'] is False,
        'A template source audit must not pretend it performed propagation')
    return templates


def check_protection(out, manifest, inventory, pose, gallery, rows, old, all_manual, manual):
    status = prior.pose_status(pose, rows)
    four = sorted([FOUR_VERTEX_KEY, OLD_FOUR_VERTEX_KEY])
    expected = dict(previous=sorted(old), counted_manual_source_keys=sorted(all_manual),
        newly_counted_manual_source_keys=sorted(manual), requested_manual_keys=sorted(manual),
        deferred_manual_template_keys=four, quarantine=[previous.QUARANTINE], gallery_saved_or_history=sorted(gallery['history_keys']),
        pose_mask_history_or_noneligible=sorted(k for k, value in status.items() if value != 'eligible'),
        eligibility=status, conversation_dispositions_not_seed_authorization=True)
    require(read_json(out/'protected_targets.json') == expected and inventory['eligibility'] == status,
        'All original history, quarantine and exact manual sources must remain protected')
    require(manifest['preserved_quarantined_targets'] == [previous.QUARANTINE] and previous.QUARANTINE not in old
        and manifest['manual_four_point_sources'] == 2 and manifest['deferred_manual_template_keys'] == four,
        'Manifest quarantine/four-point source preservation differs')
    require(read_json(out/'shortfalls.json').keys() == {str(cls) for cls in CLASSES}
        and all(v['missing'] == 0 for v in read_json(out/'shortfalls.json').values()), 'Exact completed quota shortfalls must be zero')


def check_counts(manifest, inventory, classes, combined, inherited, manual, gallery, dispositions):
    for obj in (manifest, inventory):
        require(obj['previous_proposals'] == 475
            and obj['manual_proposals'] == 25 and obj['inherited_manual_sources'] == 118 and obj['all_manual_sources'] == 143,
            'Completion parent/manual/machine counters differ')
        require(obj['counted_manual_source_keys'] == sorted(set(inherited) | set(manual)), 'All-143 explicit manual key inventory differs')
        require(obj['classes'] == classes, 'Honest actual-human/no-change/unsaved per-class quota counters differ')
    total = lambda field: sum(c[field] for c in classes.values())
    require(manifest['saved_human'] == total('saved_human') and manifest['saved_gallery_human'] == len(gallery['current'])
        and manifest['saved_pose_human'] == len(inherited)+len(manual),
        'No-change reviews must not inflate actual saved-human coverage')
    status = manifest['saved_review_status']
    require(manifest['proposals'] == 500 and manifest['new_proposals'] == 0
        and manifest['pending_machine_raw'] == manifest['old_pending_machine'] == total('pending_machine_raw')
        and manifest['no_new_server_review'] == total('no_new_server_review')
        and manifest['older_no_new_server_review'] == len(status['older_pending_excluding_acknowledged'])
        and manifest['current_batch_missing_gallery_saves'] == status['pending'] and manifest['user_reviewed_no_change'] == 45,
        'Manifest must distinguish actual non-saved-human masks, explicit no-change and genuinely unsaved historical/current sources')
    require(manifest['manual_by_class'] == {'9': 10, '10': 15} and manifest['new_by_class'] == {}, 'Manual-only completion class partition differs')


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=OUTPUT)
    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()
    allowed = out == OUTPUT.absolute() or (out.parent == PROJECT/'outputs' and re.fullmatch(r'pallet_mask_quota_100_complete_v[0-9]+', out.name))
    require(not args.output.is_symlink() and not OUTPUT.is_symlink() and allowed and out.is_dir(),
        'Use the completed publication-9 quota-100 outputs directory only')
    require(out not in {PARENT.resolve(), previous.PARENT.resolve()}, 'Cannot verify over a frozen parent')
    require(not (out/'verification.json').exists(), 'Refusing to overwrite a frozen verification')
    require((out/'manifest.json').is_file(), 'Composition is not complete: manifest.json missing')
    require(not (out/'validation_report.json').is_symlink() and not (out/'verification.json').is_symlink(), 'Unsafe validation report/seal path')
    cv2.setNumThreads(1)
    try:
        result = audit(out, args.pose_live, args.gallery_live)
    except Exception as exc:
        result = dict(state='failed', publication_version=9, error_type=type(exc).__name__, error=str(exc), verification_script_sha256=digest(__file__))
        (out/'validation_report.json').write_bytes(canonical(result)+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()
