"""Exhaustive Class 10 multiscale diagnostic; never publish or write live stores.

The 175 seeds are the quota_generation inventory, not its 67 new machine drafts
or 43 historical unreviewed proposals. Replay is a frozen-input reproducibility
check, NOT independent publication verification or a mask-accuracy claim.
"""
from __future__ import annotations

import argparse
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from contextlib import closing
from datetime import datetime, timezone
import hashlib
import html
import json
from pathlib import Path
import shutil
import sys

import cv2
import numpy as np
from PIL import Image, ImageDraw

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest, load_dataset, protected_snapshot, target_key
from pallet.mask_quota_completion import (GALLERY_ORIGIN, POSE_ORIGIN, SOURCE_PUBLICATIONS,
    gallery_templates, seed_union, template_metadata)
from pallet.mask_template_continue import sha
from pallet.mask_template_refined import QUARANTINE, read_jsonl, readonly, select_diverse, write_jsonl
from pallet.mask_template_transfer import (ImageEvidence, annotation_inventory, compatible,
    correlate, overlay, write_json)
from pallet.mask_transfer_multiscale import SETTINGS, VERSION as METHOD_VERSION, transfer
from pallet.pose_review_geometry import canonical, polygon_value

VERSION = 'class10_direct_human_multiscale_completion_diagnostic_v1'
QUOTA = PROJECT/'outputs/pallet_mask_quota_completion_v1'
OUT = PROJECT/'outputs/pallet_mask_class10_completion_diagnostic_v1'
POSE_LIVE = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
GALLERY_LIVE = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
EXISTING_HUMAN_KEYS = ('5a50c8a3f3b203f4a73302c5', '769e10f2f395d177eee6ee8f',
    '77c50239649469197cae6b04', '82570f9f6b30bb2c1d696306',
    '8515501d58bc1f637a826f4b', 'b62526c40d7f972094b41bdb')
CODE = ('scripts/diagnose_class10_completion.py', 'pallet/mask_transfer_multiscale.py',
    'pallet/mask_template_transfer.py', 'pallet/mask_boundary_review.py',
    'pallet/mask_template_continue.py', 'pallet/mask_template_refined.py',
    'pallet/mask_quota_completion.py', 'pallet/mask_gallery_review.py',
    'pallet/cluster_targets.py', 'pallet/pose_review_geometry.py')


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


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


def store_state(path, kind, immutable=False):
    """A consistent read transaction; hash raw payload bytes, including all events."""
    table = 'annotations' if kind == 'pose' else 'reviews'
    with closing(readonly(path, immutable)) as db:
        db.execute('BEGIN')
        data = dict(metadata=db.execute('SELECT key,value FROM metadata ORDER BY key').fetchall(),
            current=db.execute(f'SELECT target_key,revision,payload FROM {table} ORDER BY target_key').fetchall(),
            events=db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id').fetchall())
        db.rollback()
    return dict(sha256=sha(data), saved=len(data['current']), events=len(data['events']))


def dataset_rows(formal, classification):
    annotations, sources = load_dataset(formal)
    values = read_jsonl(classification)
    rows = {r['target_key']: r for r in values}
    identities = {target_key(a['frame_id'], i): (a, b)
        for a in annotations for i, b in enumerate(a['boxes'])}
    require(len(rows) == len(values) and set(rows) == set(identities), 'Target coverage mismatch')
    for key, row in rows.items():
        a, box = identities[key]
        source = sources[row['frame_id']]
        require(row['bbox'] == box and all(row[k] == a[k] for k in ('frame_id', 'split', 'group_id'))
            and row['source_pcd_sha256'] == source['source_anchor_hash']
            and row['policy_id'] == source['sensor_policy']['policy_id'], 'Formal source identity mismatch')
        row['size'] = source['size']
    return rows, sources


def recover_templates(out, rows, manifest, inventory):
    pose_snapshot, gallery_snapshot = out/'reviews_snapshot.sqlite3', out/'gallery_reviews_snapshot.sqlite3'
    require(digest(pose_snapshot) == manifest['snapshot_sha256'] and
        digest(gallery_snapshot) == manifest['gallery_snapshot_sha256'], 'Frozen review snapshot hash mismatch')
    with closing(readonly(pose_snapshot, True)) as db:
        current, pose, status = annotation_inventory(db, rows)
        require(read_json_value(db, 'binding')['formal_manifest_sha256'] ==
            manifest['formal_manifest_sha256'], 'Pose formal binding mismatch')
    with closing(readonly(gallery_snapshot, True)) as db:
        reviews = {k: json.loads(raw) for k, raw in db.execute('SELECT target_key,payload FROM reviews')}
        events = [json.loads(raw) for raw, in db.execute('SELECT payload FROM events ORDER BY id')]
        require(db.execute("SELECT value FROM metadata WHERE key='formal_manifest_sha256'").fetchone()[0]
            == manifest['formal_manifest_sha256'], 'Gallery formal binding mismatch')
    parent = Path(manifest['parent'])
    gallery = gallery_templates(read_jsonl(parent/'proposals.jsonl'), reviews, events, current,
        rows, parent, manifest['formal_manifest_sha256'], manifest['gallery_snapshot_sha256'], SOURCE_PUBLICATIONS)
    quarantine = QUARANTINE | set(manifest.get('preserved_quarantined_targets', []))
    recovered = seed_union(pose, gallery, manifest['snapshot_sha256'], quarantine)
    frozen = {t['row']['target_key']: t for t in inventory['templates']
        if t['rendering_id'].startswith('quota_generation:')}
    require(len(frozen) == len(recovered) == 175 and set(frozen) == set(inventory['generation_template_keys']),
        'Expected exactly 175 distinct quota_generation seeds')
    require(Counter(t['origin'] for t in recovered.values()) == {POSE_ORIGIN: 78, GALLERY_ORIGIN: 97},
        'Direct human origin counts differ')
    for key, t in frozen.items():
        require(t['rendering_id'] == 'quota_generation:'+key and t['row'] == rows[key] and
            {k: v for k, v in t.items() if k != 'rendering_id'} == recovered[key],
            f'Frozen seed differs from explicit saved human provenance: {key}')
        require(key not in quarantine and not current.get(key, {}).get('state') in ('deleted', 'deferred'),
            'Quarantined/deleted/deferred seed')
        polygon_value(t['polygon'], *t['row']['size'])
    return frozen, current, status, reviews, events, quarantine


def read_json_value(db, key):
    return json.loads(db.execute('SELECT value FROM metadata WHERE key=?', (key,)).fetchone()[0])


def source_records(out, templates):
    result = []
    with closing(readonly(out/'reviews_snapshot.sqlite3', True)) as pose, \
            closing(readonly(out/'gallery_reviews_snapshot.sqlite3', True)) as gallery:
        for key, t in sorted(templates.items()):
            store = pose if t['origin'] == POSE_ORIGIN else gallery
            table = 'annotations' if t['origin'] == POSE_ORIGIN else 'reviews'
            current_raw, = store.execute(f'SELECT payload FROM {table} WHERE target_key=?', (key,)).fetchone()
            matches = store.execute('SELECT id,payload FROM events WHERE target_key=? AND revision=?',
                (key, t['human_polygon_revision'])).fetchall()
            require(len(matches) == 1, 'Ambiguous human event')
            event_id, event_raw = matches[0]
            event, current = json.loads(event_raw), json.loads(current_raw)
            event_hash = hashlib.sha256(event_raw.encode()).hexdigest() if store is pose else sha(event)
            require(event_hash == t['human_event_sha256'] and
                event['polygon'] == current['polygon'] == t['polygon'], 'Human event bytes/polygon mismatch')
            require(event.get('event_kind') not in ('mask_template_proposal', 'unit_normalization', 'axes_normalization'),
                'Machine/automatic event used as human source')
            require(event.get('state') != 'deleted' and current.get('state') not in ('deleted', 'deferred'),
                'Invalid human source state')
            if store is gallery:
                require(event['event_kind'] == 'human_mask_gallery_revision' and event['human_edited'] is True,
                    'Not explicit gallery-human origin')
            result.append(dict(target_key=key, rendering_id=t['rendering_id'], origin=t['origin'],
                template_sha256=sha(t), current_payload_raw=current_raw, current_payload_sha256=sha(current),
                current_payload_raw_sha256=hashlib.sha256(current_raw.encode()).hexdigest(),
                human_event_id=event_id, human_event_payload_raw=event_raw,
                human_event_raw_sha256=hashlib.sha256(event_raw.encode()).hexdigest(),
                human_event_sha256=event_hash, polygon_sha256=sha(t['polygon'])))
    return result


def inventory_work(rows, previous, status, reviews, events, quarantine, seeds, frozen_protected):
    previous_keys = {p['target_key'] for p in previous}
    gallery_keys = set(reviews) | {v['target_key'] for v in events}
    mask_history = {key for key, reason in status.items() if reason != 'eligible'}
    mask_history |= set(frozen_protected['pose_mask_history_or_noneligible'])
    gallery_keys |= set(frozen_protected['gallery_saved_or_history'])
    quarantine |= set(frozen_protected['quarantine'])
    excluded = previous_keys | gallery_keys | mask_history | quarantine | set(seeds)
    outcomes, work = [], []
    for key, row in sorted(rows.items()):
        if row['final_class_id'] != 10:
            continue
        reason = ('previous_quota_target_excluded' if key in previous_keys else
            'previously_quarantined' if key in quarantine else
            'gallery_history_protected' if key in gallery_keys else
            status[key] if key in mask_history else 'human_seed_target_protected' if key in seeds else 'eligible')
        if reason == 'eligible':
            work.append((key, row))
        else:
            outcomes.append(dict(target_key=key, class_id=10, reason=reason))
    protection = dict(previous_quota_targets=sorted(previous_keys), gallery_history=sorted(gallery_keys),
        pose_mask_history_or_noneligible=sorted(mask_history), quarantine=sorted(quarantine),
        human_seed_targets=sorted(seeds), excluded_targets=sorted(excluded), eligibility=status,
        eligibility_policy='Unchanged annotation_inventory: any non-original mask history, deleted, deferred, '
            'confirmed and all gallery history protected; original-rectangle-only pose/ruler saves remain sidecar eligible.')
    return work, outcomes, protection


def existing_human_reuse(out, templates, current, previous, manifest):
    """Explicit requested existing human entries, never self-transfers or new edits."""
    frames = {p['source']['frame_id'] for p in previous}
    previous_keys = {p['target_key'] for p in previous}
    result = []
    with closing(readonly(out/'reviews_snapshot.sqlite3', True)) as db:
        for key in EXISTING_HUMAN_KEYS:
            t, value = templates[key], current[key]
            row = t['row']
            require(t['origin'] == POSE_ORIGIN and row['final_class_id'] == 10 and
                key not in previous_keys and row['frame_id'] not in frames, 'Existing human reuse is not novel/direct')
            require(value.get('state') not in ('deleted', 'deferred') and
                value['polygon'] == t['polygon'] and len(t['polygon']) > 4 and
                sha(value) == t['current_payload_sha256'], 'Existing human reuse current state/payload differs')
            for field in ('target_key', 'target_id', 'frame_id', 'group_id', 'split', 'box_index',
                    'bbox', 'size', 'source_pcd_sha256', 'policy_id'):
                require(value['source'][field] == row[field], f'Existing human source identity differs: {key}/{field}')
            require(not any(value.get(flag) for flag in ('human_mask_confirmed', 'human_pose_confirmed',
                'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible')),
                'Existing human source unexpectedly confirmed/eligible')
            current_raw, = db.execute('SELECT payload FROM annotations WHERE target_key=?', (key,)).fetchone()
            history = [dict(id=event_id, revision=revision, payload_raw=raw,
                payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest())
                for event_id, revision, raw in db.execute('SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,))]
            human = [event for event in history if event['revision'] == t['human_polygon_revision']]
            require(len(human) == 1 and human[0]['payload_raw_sha256'] == t['human_event_sha256'] and
                json.loads(human[0]['payload_raw'])['polygon'] == t['polygon'], 'Existing human source event binding differs')
            frames.add(row['frame_id'])
            result.append(dict(schema='existing_saved_pose_mask_reuse_diagnostic_v1', target_key=key,
                class_id=10, source=row, polygon=value['polygon'], vertex_count=len(value['polygon']),
                origin=POSE_ORIGIN, source_label_kind='existing_human_mask_NOT_machine_transfer',
                status='existing_human_edited_draft_pending_confirmation', human_edited=True,
                template=template_metadata(t), rendering_id=t['rendering_id'], metrics={},
                current_payload=value, current_payload_raw=current_raw, current_payload_sha256=sha(value),
                current_payload_raw_sha256=hashlib.sha256(current_raw.encode()).hexdigest(),
                human_polygon_revision=t['human_polygon_revision'], human_event_id=human[0]['id'],
                human_event_sha256=t['human_event_sha256'], source_event_history=history,
                snapshot_sha256=manifest['snapshot_sha256'], formal_manifest_sha256=manifest['formal_manifest_sha256'],
                class_assignments_sha256=manifest['classification_sha256'],
                human_mask_confirmed=False, human_pose_confirmed=False, automatic_training_eligible=False,
                robot_command_eligible=False, ground_truth_eligible=False, pose_snapshot_saved=True,
                pose_store_written=False, gallery_store_written=False, new_human_edit_created=False,
                machine_transfer_performed=False, published=False))
    require(len(result) == 6, 'Expected six existing human source entries')
    return result


def attempt_all(item, templates, evidence):
    """Every compatible template, including trials after success; never a cap."""
    key, row = item
    ranked = sorted(((correlate(evidence.patch(k), evidence.patch(key)), k)
        for k, t in templates.items() if compatible(t['row'], row)), reverse=True)
    trials, accepted = [], None
    for rank, (similarity, source_key) in enumerate(ranked, 1):
        t = templates[source_key]
        polygon, metrics = transfer(evidence.patch(source_key), evidence.patch(key),
            t['polygon'], t['row'], row, evidence.image(row['frame_id']))
        require(metrics['initial_ncc'] == similarity, 'Ranking/transfer similarity differs')
        trial = dict(target_key=key, template_key=source_key, class_id=10, template_rank=rank,
            polygon=polygon, metrics=metrics)
        trials.append(trial)
        if polygon is not None and accepted is None:
            accepted = trial
    reason = ('research_candidate' if accepted else 'all_compatible_templates_rejected' if ranked else
        'no_compatible_template')
    return accepted, trials, dict(target_key=key, class_id=10, reason=reason,
        compatible_templates=len(ranked), accepted_pairs=sum(t['polygon'] is not None for t in trials))


def candidate_record(trial, row, templates, current, inputs):
    t = templates[trial['template_key']]
    require(len(trial['polygon']) == len(t['polygon']) and compatible(t['row'], row), 'Vertex/compatibility violation')
    old = current.get(row['target_key'])
    return dict(schema=VERSION, target_key=row['target_key'], class_id=10, source=row,
        polygon=trial['polygon'], vertex_count=len(trial['polygon']),
        template=dict(template_metadata(t), rendering_id=t['rendering_id'], canonical_template_sha256=sha(t)),
        metrics={k: v for k, v in trial['metrics'].items() if k != 'configurations'},
        template_rank=trial['template_rank'], target_expected_revision=old['revision'] if old else 0,
        target_snapshot_payload_sha256=sha(old) if old else None,
        snapshot_sha256=inputs['pose_snapshot_sha256'], gallery_snapshot_sha256=inputs['gallery_snapshot_sha256'],
        formal_manifest_sha256=inputs['formal_manifest_sha256'], class_assignments_sha256=inputs['classification_sha256'],
        frozen_inputs_sha256=inputs['frozen_inputs_sha256'],
        generation_method=dict(name='multiscale_alternative_NOT_original', version=METHOD_VERSION,
            code_sha256=inputs['code_sha256']['pallet/mask_transfer_multiscale.py'],
            settings_sha256=inputs['settings_sha256'], all_compatible_templates_attempted=True,
            target_choice='first passing template in descending initial NCC / key order; all other trials retained'),
        status='research_candidate_pending_independent_validation_and_human_review',
        human_mask_confirmed=False, human_pose_confirmed=False, automatic_training_eligible=False,
        robot_command_eligible=False, ground_truth_eligible=False, production_saved=False,
        recursive_candidate_propagation=False, quality_metrics_are_not_accuracy_probabilities=True)


def audit_gates(trial, template, target):
    """Independent inequalities and consensus replay, not the module's consensus()."""
    metrics = trial['metrics']
    configurations = metrics['configurations']
    valid = [c for c in configurations if not c['baseline'] and c['metrics']['rejection'] is None]
    chosen, supports = None, []
    for c in valid:
        m = c['metrics']
        affine = np.asarray(m['affine_patch_transform'])
        singular = np.linalg.svd(affine[:, :2], compute_uv=False)
        require(m['initial_ncc'] >= .55 and m['ecc'] >= .84 and m['boundary_ncc'] >= .78,
            'Photometric gate violation')
        require(np.linalg.det(affine[:, :2]) > 0 and singular.min() >= .7 and singular.max() <= 1.4
            and np.max(np.abs(affine[:, 2])) <= 28, 'Affine gate violation')
        require(all(m['forward_status']) and all(m['backward_status']) and
            np.isfinite(m['forward_backward_error_patch_pixels']).all() and
            max(m['forward_backward_error_patch_pixels']) <= 1.5, 'FB gate violation')
        require(m['same_vertex_count'] and len(c['polygon']) == len(template['polygon']) and
            m['max_vertex_refinement_native_pixels'] <= max(3., target['bbox']['height']*.16) and
            .72 < m['refinement_area_ratio'] < 1.35, 'Polygon gate violation')
        polygon_value(c['polygon'], *target['size'])
        require(m['boundary_guard']['state'] == 'passed', 'Beam guard violation')
        local = m['local_image_support']
        require(local['passed'] and len(local['vertices']) == len(template['polygon']) and
            all(v['fully_inside_patch'] and v['ncc'] >= .55 and v['gradient_ncc'] >= .25 and
                min(v['source_std'], v['target_std']) >= 3. for v in local['vertices']), 'Local support gate violation')
        support = []
        for other in valid:
            if c['window'] == other['window']:
                continue
            distances = np.linalg.norm(np.asarray(c['forward_patch_vertices'])-
                np.asarray(other['forward_patch_vertices']), axis=1)
            if distances.max() <= 1.5:
                support.append(dict(window=other['window'], max_level=other['max_level'],
                    per_vertex_distance_patch_pixels=distances.tolist()))
        if support and chosen is None:
            chosen, supports = c, support
    require((chosen is None) == (trial['polygon'] is None), 'False cross-window success/failure')
    if chosen is not None:
        require(trial['polygon'] == chosen['polygon'] and metrics['selected_configuration'] ==
            dict(window=chosen['window'], max_level=chosen['max_level']) and
            metrics['cross_window_support'] == supports, 'Fixed-priority configuration/support differs')


def render(out, accepted, selected, templates, evidence, result):
    (out/'cases').mkdir()
    selected_keys = {p['target_key'] for p in selected}
    cards = []
    for p in accepted:
        key = p['target_key']; t = templates[p['template']['target_key']]
        panel = Image.new('RGB', (1480, 305), '#121923')
        panel.paste(overlay(evidence.image(t['row']['frame_id']), t['row'], t['polygon'], '#55ceff'), (0, 30))
        panel.paste(overlay(evidence.image(p['source']['frame_id']), p['source'], p['polygon'], '#ffc247'), (740, 30))
        draw = ImageDraw.Draw(panel)
        draw.text((10, 8), 'Class 10 | Direct saved human template -> NEW multiscale research draft | NOT GT', fill='white')
        draw.text((10, 285), f"{t['row']['target_key']} -> {key} | selected={key in selected_keys} | {p['metrics']['selected_configuration']}", fill='white')
        panel.save(out/'cases'/f'{key}.png')
        cards.append(f'<h2>{html.escape(key)} / {p["vertex_count"]} vertices / selected={key in selected_keys}</h2>'
            f'<img loading="lazy" width="1480" src="cases/{key}.png">')
    (out/'report.html').write_text('<!doctype html><meta charset="utf-8"><title>Class 10 diagnostic</title>'
        '<h1>Class 10 alternative multiscale diagnostic — NOT published / NOT GT</h1>'
        f'<p>Existing human reuse {result["existing_human_reuse_frames"]}; accepted {len(accepted)} targets; '
        f'selected {len(selected)} novel machine frames; gap {result["remaining_shortfall"]}. '
        f'Without human reuse: selected {result["selected_frames_without_human_reuse"]}; gap '
        f'{result["remaining_shortfall_without_human_reuse"]}.</p>'
        '<p>Strict original gates plus unchanged local support and two-distinct-window consensus. '
        'This is a separate algorithm, not an original-method metric or accuracy claim. '
        'Every compatible template was attempted. Human review and independent validation remain required.</p>'+
        ''.join(cards))


def generate(workers):
    require(not OUT.exists(), 'Diagnostic directory already exists; never overwrite')
    manifest = read_json(QUOTA/'manifest.json')
    inventory = read_json(QUOTA/'template_inventory.json')
    previous = read_jsonl(QUOTA/'proposals.jsonl')
    require(manifest['publication_version'] == 6 and len(previous) == 231 and
        len({p['target_key'] for p in previous}) == len({p['source']['frame_id'] for p in previous}) == 231 and
        Counter(p['class_id'] for p in previous) == {1: 50, 2: 50, 9: 50, 10: 31, 15: 50},
        'Quota base inventory differs')
    require(digest(QUOTA/'template_inventory.json') == manifest['template_inventory_sha256'] and
        digest(QUOTA/'proposals.jsonl') == manifest['proposals_sha256'], 'Quota inventory/proposal hash differs')
    require(all(digest(PROJECT/name) == value for name, value in manifest['frozen_code_sha256'].items()),
        'Original gate/quota dependency changed')
    formal = Path(read_json(PROJECT/'configs/active_formal_dataset.json')['dataset'])
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    require(digest(formal/'manifest.json') == manifest['formal_manifest_sha256'] and
        digest(classification) == manifest['classification_sha256'], 'Dataset/classification identity changed')
    rows, sources = dataset_rows(formal, classification)
    OUT.mkdir(parents=True)
    # Copy frozen sources, never change quota output or live SQLite journal modes.
    for name, expected in (('reviews_snapshot.sqlite3', manifest['snapshot_sha256']),
            ('gallery_reviews_snapshot.sqlite3', manifest['gallery_snapshot_sha256'])):
        require(digest(QUOTA/name) == expected, 'Quota snapshot changed')
        shutil.copyfile(QUOTA/name, OUT/name)
        (OUT/name).chmod(0o400)
    write_json(OUT/'settings.json', SETTINGS)
    frozen_code = {name: digest(PROJECT/name) for name in CODE}
    for name in CODE:
        destination = OUT/'frozen_code'/name
        destination.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(PROJECT/name, destination)
        destination.chmod(0o400)
    protected = protected_snapshot(formal)
    write_json(OUT/'protected_formal_before.json', protected)
    templates, current, status, reviews, events, quarantine = recover_templates(OUT, rows, manifest, inventory)
    states = dict(pose=store_state(OUT/'reviews_snapshot.sqlite3', 'pose', True),
        gallery=store_state(OUT/'gallery_reviews_snapshot.sqlite3', 'gallery', True))
    require(store_state(POSE_LIVE, 'pose') == states['pose'] and
        store_state(GALLERY_LIVE, 'gallery') == states['gallery'], 'Latest live logical data differs from frozen quota inputs')
    work, outcomes, protection = inventory_work(rows, previous, status, reviews, events,
        quarantine, templates, read_json(QUOTA/'protected_targets.json'))
    write_jsonl(OUT/'human_templates.jsonl', [templates[k] for k in sorted(templates)])
    write_jsonl(OUT/'seed_source_records.jsonl', source_records(OUT, templates))
    human_reuse = existing_human_reuse(OUT, templates, current, previous, manifest)
    write_jsonl(OUT/'existing_human_reuse.jsonl', human_reuse)
    write_jsonl(OUT/'previous_quota_proposals.jsonl', previous)
    write_jsonl(OUT/'work_targets.jsonl', [row for _, row in work])
    write_json(OUT/'protected_targets.json', protection)
    write_json(OUT/'quota_manifest.json', manifest)
    write_json(OUT/'quota_template_inventory.json', inventory)
    # Protect every existing quota artifact; a separate verifier may append new files.
    quota_files = {str(p.relative_to(PROJECT)): digest(p) for p in sorted(QUOTA.rglob('*')) if p.is_file()}
    inputs = dict(schema=VERSION, formal_dataset=str(formal), classification=str(classification),
        formal_manifest_sha256=manifest['formal_manifest_sha256'], classification_sha256=manifest['classification_sha256'],
        pose_snapshot_sha256=manifest['snapshot_sha256'], gallery_snapshot_sha256=manifest['gallery_snapshot_sha256'],
        code_sha256=frozen_code, settings_sha256=digest(OUT/'settings.json'), live_logical_states=states,
        quota_files_sha256=quota_files, frozen_files_sha256={name: digest(OUT/name) for name in
            ('human_templates.jsonl', 'seed_source_records.jsonl', 'previous_quota_proposals.jsonl',
             'work_targets.jsonl', 'protected_targets.json', 'quota_manifest.json', 'quota_template_inventory.json',
             'protected_formal_before.json', 'settings.json', 'reviews_snapshot.sqlite3', 'gallery_reviews_snapshot.sqlite3',
             'existing_human_reuse.jsonl')})
    write_json(OUT/'frozen_inputs.json', inputs)
    inputs['frozen_inputs_sha256'] = digest(OUT/'frozen_inputs.json')
    evidence = ImageEvidence(formal, rows, sources)
    for key, t in templates.items():
        evidence.patch(key)
        if t['origin'] == GALLERY_ORIGIN:
            with Image.open(QUOTA/'images'/f'{key}.png') as image:
                require(np.array_equal(np.asarray(image), evidence.image(t['row']['frame_id'])), 'Human gallery source pixels differ')
    coverage = [dict(target_key=key, compatible_template_keys=sorted(k for k, t in templates.items()
        if compatible(t['row'], row)), novel_quota_frame=row['frame_id'] not in
            {p['source']['frame_id'] for p in previous}) for key, row in work]
    write_jsonl(OUT/'target_compatibility.jsonl', coverage)
    print(json.dumps(dict(state='scanning', seeds=175, class10_seeds=sum(t['row']['final_class_id'] == 10
        for t in templates.values()), class10_targets=len(work), compatible_pairs=sum(len(v['compatible_template_keys'])
            for v in coverage), all_compatible_templates=True, workers=workers)), flush=True)
    accepted, pair_rejections, config_rejections = [], Counter(), Counter()
    pair_count = accepted_pairs = 0
    photometric_targets, original_config_targets, local_config_targets = set(), set(), set()
    def worker(item):
        return item, attempt_all(item, templates, evidence)
    with (OUT/'registration_attempts.jsonl').open('wb') as audit, ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (match, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            outcomes.append(outcome)
            for trial in trials:
                audit.write(canonical(trial)+b'\n')
                pair_count += 1
                accepted_pairs += trial['polygon'] is not None
                pair_rejections[str(trial['metrics']['rejection'])] += 1
                if trial['metrics']['configurations']:
                    photometric_targets.add(key)
                for config in trial['metrics']['configurations']:
                    cm = config['metrics']
                    config_rejections[str(cm['rejection'])] += 1
                    if not config['baseline'] and cm['original_transfer_and_beam_gates_passed']:
                        original_config_targets.add(key)
                    if not config['baseline'] and cm['rejection'] is None:
                        local_config_targets.add(key)
                audit_gates(trial, templates[trial['template_key']], row)
            if match:
                accepted.append(candidate_record(match, row, templates, current, inputs))
            if number % 50 == 0:
                audit.flush()
                print(json.dumps(dict(scanned=number, accepted=len(accepted), attempts=pair_count)), flush=True)
    selected_without_reuse, dispositions_without_reuse = select_diverse(previous, accepted)
    selected, dispositions = select_diverse(previous+human_reuse, accepted)
    require(len(selected) <= 13 and len({p['source']['frame_id'] for p in previous+human_reuse+selected}) == 237+len(selected),
        'Quota selection repeats a frame or exceeds 13 after human reuse')
    for outcome in outcomes:
        if outcome['target_key'] in dispositions:
            outcome['selection_disposition'] = dispositions[outcome['target_key']]
            outcome['selection_disposition_without_human_reuse'] = dispositions_without_reuse[outcome['target_key']]
    for name, values in (('accepted_candidates', accepted), ('selected_candidates', selected), ('outcomes', outcomes)):
        write_jsonl(OUT/f'{name}.jsonl', values)
    write_jsonl(OUT/'selected_without_human_reuse.jsonl', selected_without_reuse)
    write_json(OUT/'used_image_hashes.json', evidence.used)
    after = protected_snapshot(formal)
    write_json(OUT/'protected_formal_after.json', after)
    require(after == protected, 'Protected formal data changed')
    require(all(digest(PROJECT/name) == value for name, value in frozen_code.items()), 'Frozen dependency changed')
    require(all(digest(PROJECT/name) == value for name, value in quota_files.items()), 'Frozen quota artifact changed')
    require(store_state(POSE_LIVE, 'pose') == states['pose'] and
        store_state(GALLERY_LIVE, 'gallery') == states['gallery'], 'Live review data changed during diagnostic')
    for name, value in evidence.used.items():
        require(digest(formal/name) == value, 'Used formal image changed')
    old_frames = {p['source']['frame_id'] for p in previous}
    novel = lambda keys: len({rows[k]['frame_id'] for k in keys if rows[k]['frame_id'] not in old_frames})
    result = dict(schema=VERSION, method_version=METHOD_VERSION,
        state='diagnostic_complete_pending_independent_validation_and_human_review',
        created_at=datetime.now(timezone.utc).isoformat(), frozen_inputs_sha256=inputs['frozen_inputs_sha256'],
        method_code_sha256=frozen_code['pallet/mask_transfer_multiscale.py'], code_sha256=frozen_code,
        settings_sha256=inputs['settings_sha256'], generation_templates=175,
        templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        class10_templates=sum(t['row']['final_class_id'] == 10 for t in templates.values()),
        existing_quota_frames=231, existing_class10_frames=31, requested_class10_additions=19,
        scanned_class10_targets=len(work), compatible_pairs=pair_count,
        photometric_gate_passing_targets=len(photometric_targets), photometric_gate_passing_novel_frames=novel(photometric_targets),
        original_plus_beam_admissible_alt_config_targets=len(original_config_targets),
        locally_supported_alt_config_targets=len(local_config_targets),
        accepted_pairs=accepted_pairs, accepted_targets=len(accepted),
        accepted_novel_frames=novel({p['target_key'] for p in accepted}), selected_frames=len(selected),
        existing_human_reuse_frames=len(human_reuse), class10_base_with_existing_human_reuse=31+len(human_reuse),
        machine_additions_needed_with_human_reuse=19-len(human_reuse),
        attainable_class10_frames=31+len(human_reuse)+len(selected), remaining_shortfall=19-len(human_reuse)-len(selected),
        selected_frames_without_human_reuse=len(selected_without_reuse),
        attainable_class10_frames_without_human_reuse=31+len(selected_without_reuse),
        remaining_shortfall_without_human_reuse=19-len(selected_without_reuse),
        pair_rejections=dict(pair_rejections), configuration_rejections=dict(config_rejections),
        outcomes=dict(Counter(v['reason'] for v in outcomes)), workers=workers,
        opencv_version=cv2.__version__, numpy_version=np.__version__, opencv_threads=cv2.getNumThreads(),
        full_compatible_pair_scan=True, strict_gates_and_cross_window_support_unchanged=True,
        original_method_accuracy_claim=False, thresholds_relaxed=False, recursive_candidate_propagation=False,
        production_database_writes=0, configuration_writes=0, published=False,
        independent_publication_verified=False, ground_truth_eligible=False,
        automatic_training_eligible=False, robot_command_eligible=False,
        accepted_candidates_sha256=digest(OUT/'accepted_candidates.jsonl'),
        selected_candidates_sha256=digest(OUT/'selected_candidates.jsonl'),
        registration_attempts_sha256=digest(OUT/'registration_attempts.jsonl'))
    render(OUT, accepted, selected, templates, evidence, result)
    write_json(OUT/'summary.json', result)
    write_json(OUT/'artifact_hashes.json', {str(p.relative_to(OUT)): digest(p)
        for p in sorted(OUT.rglob('*')) if p.is_file()})
    print(json.dumps(result), flush=True)
    return result


def replay(expected_frozen_hash, workers):
    """Fresh process, explicit external input hash, no live/config reads or writes."""
    require(digest(OUT/'frozen_inputs.json') == expected_frozen_hash, 'External frozen input hash differs')
    require(not (OUT/'replay.json').exists(), 'Never overwrite a replay result')
    inputs, result = read_json(OUT/'frozen_inputs.json'), read_json(OUT/'summary.json')
    require(result['frozen_inputs_sha256'] == expected_frozen_hash, 'Summary input binding differs')
    for name, value in read_json(OUT/'artifact_hashes.json').items():
        require(digest(OUT/name) == value, f'Diagnostic artifact changed: {name}')
    for name, value in inputs['code_sha256'].items():
        require(digest(PROJECT/name) == digest(OUT/'frozen_code'/name) == value, f'Code hash mismatch: {name}')
    for name, value in inputs['frozen_files_sha256'].items():
        require(digest(OUT/name) == value, f'Frozen input changed: {name}')
    require(read_json(OUT/'settings.json') == SETTINGS, 'Method settings changed')
    formal, classification = Path(inputs['formal_dataset']), Path(inputs['classification'])
    require(digest(formal/'manifest.json') == inputs['formal_manifest_sha256'] and
        digest(classification) == inputs['classification_sha256'], 'Replay dataset binding differs')
    rows, sources = dataset_rows(formal, classification)
    manifest, inventory = read_json(OUT/'quota_manifest.json'), read_json(OUT/'quota_template_inventory.json')
    templates, current, status, reviews, events, quarantine = recover_templates(OUT, rows, manifest, inventory)
    require(source_records(OUT, templates) == read_jsonl(OUT/'seed_source_records.jsonl'), 'Seed provenance replay differs')
    previous = read_jsonl(OUT/'previous_quota_proposals.jsonl')
    human_reuse = existing_human_reuse(OUT, templates, current, previous, manifest)
    require(human_reuse == read_jsonl(OUT/'existing_human_reuse.jsonl'), 'Existing human source reuse replay differs')
    frozen_protected = dict(pose_mask_history_or_noneligible=read_json(OUT/'protected_targets.json')['pose_mask_history_or_noneligible'],
        gallery_saved_or_history=read_json(OUT/'protected_targets.json')['gallery_history'], quarantine=sorted(quarantine))
    work, outcomes, protection = inventory_work(rows, previous, status, reviews, events,
        quarantine, templates, frozen_protected)
    require([row for _, row in work] == read_jsonl(OUT/'work_targets.jsonl') and
        protection == read_json(OUT/'protected_targets.json'), 'Replay eligibility coverage differs')
    evidence = ImageEvidence(formal, rows, sources)
    inputs['frozen_inputs_sha256'] = expected_frozen_hash
    accepted, pair_count = [], 0
    audit_hash = hashlib.sha256()
    def worker(item):
        return item, attempt_all(item, templates, evidence)
    with (OUT/'registration_attempts.jsonl').open('rb') as original, ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (match, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            outcomes.append(outcome)
            for trial in trials:
                raw = canonical(trial)+b'\n'
                require(raw == original.readline(), f'Exact metric/polygon replay differs: {key}/{trial["template_key"]}')
                audit_hash.update(raw)
                audit_gates(trial, templates[trial['template_key']], row)
                pair_count += 1
            if match:
                accepted.append(candidate_record(match, row, templates, current, inputs))
            if number % 100 == 0:
                print(json.dumps(dict(state='replaying', scanned=number, attempts=pair_count)), flush=True)
        require(original.readline() == b'', 'Unreplayed trailing trial')
    require(audit_hash.hexdigest() == result['registration_attempts_sha256'], 'Full audit replay hash differs')
    selected_without_reuse, dispositions_without_reuse = select_diverse(previous, accepted)
    selected, dispositions = select_diverse(previous+human_reuse, accepted)
    for outcome in outcomes:
        if outcome['target_key'] in dispositions:
            outcome['selection_disposition'] = dispositions[outcome['target_key']]
            outcome['selection_disposition_without_human_reuse'] = dispositions_without_reuse[outcome['target_key']]
    require(accepted == read_jsonl(OUT/'accepted_candidates.jsonl') and
        selected == read_jsonl(OUT/'selected_candidates.jsonl') and outcomes == read_jsonl(OUT/'outcomes.jsonl') and
        selected_without_reuse == read_jsonl(OUT/'selected_without_human_reuse.jsonl'),
        'Accepted/selected/outcome replay differs')
    require(protected_snapshot(formal) == read_json(OUT/'protected_formal_before.json'), 'Protected formal data differs')
    for name, value in read_json(OUT/'used_image_hashes.json').items():
        require(digest(formal/name) == value, 'Frozen image changed')
    replay_result = dict(state='passed_reproducibility_only_NOT_independent_publication_verification',
        frozen_inputs_sha256=expected_frozen_hash, replayed_targets=len(work), replayed_pairs=pair_count,
        all_metric_leaves_and_polygons_exact=True, independent_gate_inequalities_and_consensus_checked=True,
        accepted_targets=len(accepted), selected_frames=len(selected), existing_human_reuse_frames=len(human_reuse),
        remaining_shortfall=19-len(human_reuse)-len(selected),
        selected_frames_without_human_reuse=len(selected_without_reuse),
        remaining_shortfall_without_human_reuse=19-len(selected_without_reuse),
        registration_attempts_sha256=audit_hash.hexdigest(), live_database_or_configuration_reads=0,
        live_database_or_configuration_writes=0, independent_publication_verified=False,
        published=False, ground_truth_eligible=False)
    write_json(OUT/'replay.json', replay_result)
    print(json.dumps(replay_result), flush=True)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--workers', type=int, default=4)
    parser.add_argument('--replay-frozen-sha256', help='Replay only, requiring externally supplied frozen_inputs.json hash')
    args = parser.parse_args()
    require(1 <= args.workers <= 8, 'Workers must be 1–8')
    cv2.setNumThreads(1)
    if args.replay_frozen_sha256:
        replay(args.replay_frozen_sha256, args.workers)
    else:
        generate(args.workers)


if __name__ == '__main__':
    main()
