"""Read-only remaining-class population audit and exact saved-human freeze.

This is neither publication 10 nor a transfer engine or manual-request builder.
Only a previously absent output directory is written. Original classifications
are candidate assignments, not confirmed truth; four-point human edits remain
four points and are coverage sources, never propagation templates.
"""
from __future__ import annotations

import argparse
from collections import Counter, defaultdict
from contextlib import closing
import hashlib
import json
from pathlib import Path

import numpy as np

from . import mask_quota_100_continue as prior
from .cluster_targets import digest, protected_snapshot
from .mask_quota_saved_extension import file_inventory, load_formal_evidence
from .mask_template_continue import sha
from .mask_template_refined import (QUARANTINE, readonly, read_jsonl,
    snapshot_database, verify_history_preserved, write_jsonl)
from .mask_template_transfer import (AUTO_EVENTS, PROJECT, annotation_inventory,
    rectangle, write_json)
from .pose_review_geometry import canonical, polygon_value

VERSION = 'remaining_classes_saved_human_audit_v1'
CLASSES = (3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22)
TARGET_PER_CLASS = 100
GENERATION_NAMESPACE = 'remaining_classes_generation'
PARENT = PROJECT/'outputs/pallet_mask_quota_100_complete_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_classes_v1'
MANUAL_SCHEMA = prior.MANUAL_SCHEMA
FALSE_FLAGS = prior.FALSE_FLAGS
CONFIRMATION_FLAGS = FALSE_FLAGS + ('metric_pose_confirmed', 'human_confirmed')
PARENT_PINS = {
    'manifest.json': 'f2ef2f8c29ff2f6ab4968acab9e151ab11abb66f319d17dd979792e7a6e20d5b',
    'verification.json': '303d693278d95b4b4e54fdfae04cbbf1e6e7313ef59421df1cf9fd0e4c0bc172',
    'proposals.jsonl': 'f85012a0b23279de8f7b68fcd390f35eab3ada284910f9a2b84b651a2fad1145',
    'artifact_hashes.json': '3c5c1f848e93d42500cccac62086914123a488cd62097eb80ecf643301f1dcfa',
    'pallet_mask_refined_review.zip': '7502def93cd65d6c8e440fe8bed19f3a858970269b1e307aa2e7c7a9c4d9e345',
}


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


def original_rectangle(polygon, row):
    """Also reject a cyclically shifted/reversed original box, not just its order."""
    if polygon is None or np.shape(polygon) != (4, 2):
        return False
    return bool(np.allclose(sorted(polygon), sorted(rectangle(row)), rtol=0, atol=1e-5))


def confirmed(value):
    return bool(value and (value.get('state') == 'confirmed' or
        any(value.get(f, False) is not False for f in CONFIRMATION_FLAGS)))


def machine_marked(value):
    return (value.get('event_kind') == 'mask_template_proposal' or
        any(value.get(f) is True for f in ('machine_draft', 'machine_transfer_performed',
            'automatic_generated', 'machine_generated')) or
        value.get('origin') not in (None, prior.POSE_ORIGIN))


def validated_manual_source(db, key, row, snapshot_sha, binding=None):
    """Use the frozen arbitrary-class helper, with additional fail-closed guards."""
    value, metadata, record = prior.exact_manual_source(db, key, row, snapshot_sha)
    if original_rectangle(value['polygon'], row) or confirmed(value) or machine_marked(value):
        raise ValueError('Original rectangle, machine or confirmed target is not a new human source')
    history = [(i, r, raw, json.loads(raw)) for i, r, raw in db.execute(
        'SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,))]
    if (not history or history[-1][1] != value['revision'] or history[-1][3] != value):
        raise ValueError('Current manual payload is not its latest exact event')
    event = record['human_event_payload']
    if any(machine_marked(old) for event_id, _, _, old in history if event_id > record['human_event_id']):
        raise ValueError('A newer machine event superseded the actual human source')
    if event.get('event_kind') in AUTO_EVENTS or machine_marked(event) or confirmed(event):
        raise ValueError('Automatic/machine/confirmed event is not a direct human source')
    if event.get('human_edited') is False:
        raise ValueError('Event explicitly denies a human edit')
    for v in (value, event):
        if binding is not None and v.get('binding') != binding:
            raise ValueError('Manual source formal binding changed')
        for field in ('final_class_id', 'viewpoint_candidate'):
            if field in v['source'] and v['source'][field] != row.get(field):
                raise ValueError('Manual source classification identity changed')
    # A machine proposal followed only by an unchanged save is not a new
    # genuinely modified human contour, even if someone labels that save human.
    last_machine = None
    for event_id, _, _, old in history:
        if event_id > record['human_event_id']:
            break
        if machine_marked(old):
            last_machine = old
    if last_machine and last_machine.get('polygon') == event['polygon']:
        raise ValueError('Unchanged machine polygon plus save is not a human modification')
    polygon_value(value['polygon'], *row['size'])
    return value, metadata, dict(record, source=row,
        source_identity_sha256=sha(row), classification_is_candidate_not_truth=True)


def verified_parent(parent):
    hashes = file_inventory(parent)
    if any(hashes.get(name) != expected for name, expected in PARENT_PINS.items()):
        raise ValueError('Pinned verified publication 9 changed')
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 9 or manifest.get('proposals') != 500 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 9 or
            seal.get('proposals_verified') != 500 or
            seal.get('manifest_sha256') != hashes['manifest.json'] or
            seal.get('proposals_sha256') != hashes['proposals.jsonl'] or
            seal.get('artifact_hashes_sha256') != hashes['artifact_hashes.json'] or
            manifest.get('archive_sha256') != hashes['pallet_mask_refined_review.zip']):
        raise ValueError('Matching independent publication-9 seal required')
    for name, expected in read_json(parent/'artifact_hashes.json').items():
        if hashes.get(name) != expected:
            raise ValueError('Frozen publication-9 resource changed: '+name)
    for name, expected in manifest['frozen_code_sha256'].items():
        if digest(PROJECT/name) != expected:
            raise ValueError('Frozen original source changed: '+name)
    return manifest, hashes


def snapshot_rows(db, table, rows):
    """Relational identities for *all* current records and all historical events."""
    result = {}
    for key, revision, raw in db.execute(f'SELECT target_key,revision,payload FROM {table}'):
        value = json.loads(raw)
        if key not in rows or value.get('target_key') != key or value.get('revision') != revision:
            raise ValueError('Snapshot relational source identity mismatch: '+table)
        if table != 'events':
            if key in result:
                raise ValueError('Duplicate current snapshot target')
            result[key] = value
    return result


def audit_sources(db, rows, snapshot_sha, binding, previous_frames, quarantine, gallery_keys):
    current, _, status = annotation_inventory(db, rows)
    candidates, audit = {}, []
    for key, value in sorted(current.items()):
        row = rows[key]
        if row['final_class_id'] not in CLASSES:
            continue
        item = dict(target_key=key, class_id=row['final_class_id'], frame_id=row['frame_id'],
            current_revision=value['revision'], vertex_count=len(value.get('polygon') or []),
            current_payload_sha256=sha(value), exact_human_validated=False,
            reused=False, classification_is_candidate_not_truth=True)
        reason = ('deleted' if value.get('state') == 'deleted' else
            'deferred' if value.get('state') == 'deferred' else
            'confirmed_protected' if confirmed(value) else
            'original_rectangle_not_human_mask' if original_rectangle(value.get('polygon'), row) else None)
        if reason is None:
            try:
                v, metadata, record = validated_manual_source(db, key, row, snapshot_sha, binding)
            except ValueError as exc:
                reason = 'invalid_exact_human_provenance'
                item['validation_error'] = str(exc)
            else:
                item['exact_human_validated'] = True
                reason = ('quarantined' if key in quarantine else
                    'inherited_500_frame_protected' if row['frame_id'] in previous_frames else
                    'gallery_history_protected' if key in gallery_keys else 'validated_reusable_human')
                if reason == 'validated_reusable_human':
                    candidates[key] = (v, metadata, record)
        item['reason'] = reason
        audit.append(item)
    return current, status, candidates, audit


def assign_frames(rows, pool_keys, human_keys, goal=TARGET_PER_CLASS):
    """Deterministic maximum quota matching, scarce classes first, one frame globally.

    Start with all possible saved-human frames. Augmenting paths can move a
    common-class source only when necessary to open a scarce-class quota slot.
    Earlier classes never lose coverage to later classes. Residual pool ownership
    does not count as a proposal or completed label.
    """
    if goal < 1:
        raise ValueError('Positive per-class quota required')
    by_frame = defaultdict(lambda: defaultdict(list))
    for key in sorted(pool_keys):
        row = rows[key]
        if row['final_class_id'] not in CLASSES:
            raise ValueError('Unexpected remaining class')
        by_frame[row['frame_id']][row['final_class_id']].append(key)
    availability = {c: sum(c in choices for choices in by_frame.values()) for c in CLASSES}
    order = sorted(CLASSES, key=lambda c: (availability[c], c))
    rank = {c: i for i, c in enumerate(order)}
    representative = {(f, c): min(keys, key=lambda k: (k not in human_keys, k))
        for f, choices in by_frame.items() for c, keys in choices.items()}
    preferences = {c: sorted((f for f, choices in by_frame.items() if c in choices),
        key=lambda f: (representative[f, c] not in human_keys, len(by_frame[f]), f)) for c in CLASSES}
    owners, held = {}, {c: set() for c in CLASSES}

    def give(frame, cls):
        old = owners.get(frame)
        if old is not None:
            held[old].remove(frame)
        owners[frame] = cls
        held[cls].add(frame)

    for frame, choices in sorted(by_frame.items()):
        manual_classes = [c for c in choices if representative[frame, c] in human_keys]
        if manual_classes:
            cls = min(manual_classes, key=lambda c: rank[c])
            if len(held[cls]) < goal:
                give(frame, cls)

    def augment(cls, visited):
        if cls in visited:
            return False
        visited.add(cls)
        for frame in preferences[cls]:
            if frame not in owners:
                give(frame, cls)
                return True
        for frame in preferences[cls]:
            old = owners[frame]
            if old != cls and augment(old, visited):
                give(frame, cls)
                return True
        return False

    for cls in order:
        while len(held[cls]) < goal and augment(cls, set()):
            pass
    quota_counts = {c: len(held[c]) for c in CLASSES}
    matched = set(owners)
    for frame, choices in sorted(by_frame.items()):
        if frame not in owners:
            give(frame, min(choices, key=lambda c: rank[c]))
    assignments = [dict(frame_id=f, class_id=c, target_key=representative[f, c],
        phase='quota_capacity_matching' if f in matched else 'remaining_pool_ownership',
        actual_saved_human=representative[f, c] in human_keys)
        for f, c in sorted(owners.items())]
    return assignments, quota_counts, order


def saved_manual_proposal(key, row, candidate, snapshot_sha, formal_sha, classification_sha):
    value, metadata, _ = candidate
    four = len(value['polygon']) == 4
    p = dict(schema=MANUAL_SCHEMA, generation_schema=VERSION, origin=prior.POSE_ORIGIN,
        target_key=key, class_id=row['final_class_id'], source=row, polygon=value['polygon'],
        vertex_count=len(value['polygon']), status='human_edited_draft_pending_confirmation',
        human_edited=True, source_label_kind='human_mask_source_manual_template_draft',
        manual_source_kind='existing_saved_human_reuse_remaining_classes',
        **dict.fromkeys(FALSE_FLAGS, False), pose_snapshot_saved=True, pose_store_written=False,
        gallery_store_written=False, machine_transfer_performed=False, new_human_edit_created=False,
        manual_four_point_source=four, propagation_seed_eligible=not four,
        mask_semantics='saved_human_fork_face_contour_pending_confirmation',
        rendering_id=GENERATION_NAMESPACE+(':manual_source:' if four else ':')+key,
        target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
        snapshot_sha256=snapshot_sha, formal_manifest_sha256=formal_sha,
        class_assignments_sha256=classification_sha, metrics={}, original_pose_url=None,
        flags=['manual_source_not_machine_transfer', 'saved_human_mask_requires_confirmation']+
            (['four_point_source_coverage_only_not_propagation_template'] if four else []),
        classification_is_candidate_not_truth=True,
        geometry_pose_policy='original_pose_unchanged; independent_gallery_review_not_imported')
    p['human_source' if four else 'template'] = metadata
    return p


def class_inventory(rows, previous_frames, pool_keys, assignments, manual, templates, audit, capacity):
    owners = {a['frame_id']: a['class_id'] for a in assignments}
    result = {}
    for cls in CLASSES:
        targets = {k for k, r in rows.items() if r['final_class_id'] == cls}
        frames = {rows[k]['frame_id'] for k in targets}
        pool = targets & set(pool_keys)
        pool_frames = {rows[k]['frame_id'] for k in pool}
        reused = [p for p in manual if p['class_id'] == cls]
        exact = [a for a in audit if a['class_id'] == cls and a['exact_human_validated']]
        blocked = {f for f in pool_frames if owners[f] != cls}
        result[cls] = dict(class_id=cls, proposed_target=TARGET_PER_CLASS,
            target_per_class=TARGET_PER_CLASS, total_targets=len(targets), distinct_frames=len(frames),
            inherited_500_blocked_frames=len(frames & previous_frames),
            distinct_frames_after_inherited_500=len(frames-previous_frames),
            unprotected_targets=len(pool), remaining_unique_unprotected=len(pool_frames),
            protected_or_noneligible_frames_after_inherited_500=len((frames-previous_frames)-pool_frames),
            assigned_unique_frames=len(pool_frames-blocked), crossclass_blocked_by_other_class=len(blocked),
            capacity_matching_distinct_frames=capacity[cls],
            capacity_shortfall=TARGET_PER_CLASS-capacity[cls],
            structural_shortfall_before_inherited_constraints=max(0, TARGET_PER_CLASS-len(frames)),
            inherited_image_crossclass_constraint_shortfall=max(0, min(TARGET_PER_CLASS, len(frames))-
                min(TARGET_PER_CLASS, len(frames-previous_frames))),
            actual_human_labels_validated=len(exact), actual_human_labels_reused=len(reused),
            manual_four_count=sum(p['manual_four_point_source'] for p in reused),
            manual_more_than_four_count=sum(not p['manual_four_point_source'] for p in reused),
            templates=sum(t['row']['final_class_id'] == cls for t in templates.values()),
            reused_unique_frames=len({p['source']['frame_id'] for p in reused}),
            total=len(reused), missing=TARGET_PER_CLASS-len(reused),
            capacity_remaining_after_human_reuse=capacity[cls]-len(reused),
            fresh_unprotected_targets=sum(k not in {p['target_key'] for p in manual} and
                k not in {a['target_key'] for a in audit if a['exact_human_validated']} for k in pool),
            human_mask_confirmed=0, ground_truth_eligible=0, new_machine_candidates=0,
            classification_is_candidate_not_truth=True)
    return result


def write_report(out, inventory, manual, templates):
    lines = ['# Remaining 17 classes — saved-human source audit', '',
        'Read-only freeze; **not publication 10**, not an independent verification seal. '
        'No transfers, new human requests, confirmations, training or robot eligibility.', '',
        '| Class | Targets | Original frames | Blocked by old 500 | Usable frames | '
        'Owned by other class | Quota capacity | Human reused | Four-point | >4 templates | Missing labels |',
        '|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|']
    fields = ('total_targets', 'distinct_frames', 'inherited_500_blocked_frames',
        'remaining_unique_unprotected', 'crossclass_blocked_by_other_class',
        'capacity_matching_distinct_frames', 'actual_human_labels_reused', 'manual_four_count',
        'templates', 'missing')
    for cls, item in inventory.items():
        lines.append('| '+str(cls)+' | '+' | '.join(str(item[f]) for f in fields)+' |')
    lines += ['', 'Capacity is not completed coverage. Original class assignments are candidates, '
        'not confirmed truth. Frame ownership uses deterministic scarce-class-first maximum quota '
        'matching; a frame is owned by only one class globally, including the inherited 500.', '',
        '## Direct >4-vertex templates (all 17 classes)', '']
    for cls in CLASSES:
        matching = [t for t in templates.values() if t['row']['final_class_id'] == cls]
        lines.append(f'- Class {cls}: '+(', '.join(f"`{t['row']['target_key']}` "
            f"({len(t['polygon'])} vertices, {t['row']['split']}, {t['row']['policy_id']})"
            for t in matching) or 'none; genuine human template still required.'))
    lines += ['', 'Public drawable records: `manual_proposals.jsonl`, `cases/`, `images/`, '
        '`masks/`, `labelme/`. Four-point sources retain exactly four vertices and never enter '
        '`remaining_classes_generation/templates/`.', '',
        'Private evidence: immutable SQLite snapshots/backups and raw '
        '`manual_source_records.jsonl` / `validated_manual_source_records.jsonl`. '
        'No public archive or service/configuration change was made.', '',
        f'Actual reused labels: {len(manual)}; propagation templates: {len(templates)}.']
    (out/'REPORT.md').write_text('\n'.join(lines)+'\n')


def generate(output=OUTPUT, parent=PARENT):
    out, parent = Path(output).resolve(), Path(parent).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory; never overwrite a frozen audit')
    manifest, parent_hashes = verified_parent(parent)
    config_hashes = file_inventory(PROJECT/'configs')
    code_hashes = dict(manifest['frozen_code_sha256'])
    code_hashes['pallet/mask_remaining_classes.py'] = digest(__file__)
    formal, rows, evidence = load_formal_evidence(manifest)
    if len(rows) != 12344 or {r['final_class_id'] for r in rows.values()} != set(range(1, 23)):
        raise ValueError('Original 22-class / 12344-target classification changed')
    previous = read_jsonl(parent/'proposals.jsonl')
    previous_frames = {p['source']['frame_id'] for p in previous}
    if (len(previous) != 500 or len(previous_frames) != 500 or
            Counter(p['class_id'] for p in previous) != {1: 100, 2: 100, 9: 100, 10: 100, 15: 100} or
            any(p['source'] != rows[p['target_key']] for p in previous)):
        raise ValueError('Inherited 500 canonical/source identity mismatch')
    protected = protected_snapshot(formal)
    if (len(protected) != 20768 or protected != read_json(parent/'protected_formal_before.json') or
            protected != read_json(parent/'protected_formal_after.json')):
        raise ValueError('Protected 20768 formal files differ from publication 9')
    quarantine = set(QUARANTINE) | set(manifest.get('preserved_quarantined_targets', []))
    out.mkdir(parents=True)
    for name, value in (('parent_file_inventory', parent_hashes), ('configuration_input_hashes', config_hashes),
            ('protected_formal_before', protected)):
        write_json(out/(name+'.json'), value)
    pose_live = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
    gallery_live = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
    pose_snapshot, gallery_snapshot = out/'reviews_snapshot.sqlite3', out/'gallery_reviews_snapshot.sqlite3'
    pose_sha = snapshot_database(pose_live, pose_snapshot)
    gallery_sha = snapshot_database(gallery_live, gallery_snapshot)
    with closing(readonly(gallery_snapshot, True)) as db:
        gallery_metadata = dict(db.execute('SELECT key,value FROM metadata'))
        if gallery_metadata.get('formal_manifest_sha256') != manifest['formal_manifest_sha256']:
            raise ValueError('Gallery formal binding mismatch')
        reviews = snapshot_rows(db, 'reviews', rows)
        snapshot_rows(db, 'events', rows)
        gallery_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        gallery_keys = {k for k, in db.execute('SELECT target_key FROM reviews UNION SELECT target_key FROM events')}
        gallery_classes = {c: dict(saved=sum(rows[k]['final_class_id'] == c for k in reviews),
            history_keys=sum(rows[k]['final_class_id'] == c for k in gallery_keys),
            reused_as_new_source=0, reason='gallery current/history protected; never supplement or resurrect')
            for c in CLASSES}
    with closing(readonly(pose_snapshot, True)) as db:
        pose_metadata = dict(db.execute('SELECT key,value FROM metadata'))
        binding = json.loads(pose_metadata['binding'])
        if (binding.get('formal_manifest_sha256') != manifest['formal_manifest_sha256'] or
                binding.get('formal_annotations_sha256') != digest(formal/'annotations/frames.jsonl') or
                binding.get('formal_frames_sha256') != digest(formal/'frames.jsonl')):
            raise ValueError('Pose formal/classification binding mismatch')
        snapshot_rows(db, 'annotations', rows)
        snapshot_rows(db, 'events', rows)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        current, status, candidates, audit = audit_sources(db, rows, pose_sha, sha(binding),
            previous_frames, quarantine, gallery_keys)
    pool_keys, target_audit = [], []
    for key, row in sorted(rows.items()):
        if row['final_class_id'] not in CLASSES:
            continue
        value = current.get(key)
        reason = ('quarantined' if key in quarantine else
            'inherited_500_frame_protected' if row['frame_id'] in previous_frames else
            'gallery_history_protected' if key in gallery_keys else
            'confirmed_protected' if confirmed(value) else
            'validated_reusable_human' if key in candidates else status[key])
        if reason in ('eligible', 'validated_reusable_human'):
            pool_keys.append(key)
        target_audit.append(dict(target_key=key, class_id=row['final_class_id'], source=row,
            reason=reason, fresh_eligible=reason == 'eligible',
            exact_saved_human=key in candidates, sidecar=None))
    assignments, capacity, scarcity_order = assign_frames(rows, pool_keys, set(candidates))
    assigned_by_frame = {a['frame_id']: a for a in assignments}
    selected = {a['target_key'] for a in assignments if a['actual_saved_human'] and
        a['phase'] == 'quota_capacity_matching'}
    for item in audit:
        key = item['target_key']
        item['reused'] = key in selected
        if key in candidates and key not in selected:
            owner = assigned_by_frame[rows[key]['frame_id']]
            item['reason'] = ('same_frame_other_target_selected' if owner['class_id'] == item['class_id'] else
                'crossclass_frame_owned_by_other_class')
            item['frame_owner_class'] = owner['class_id']
            item['frame_owner_target_key'] = owner['target_key']
    for item in target_audit:
        assignment = assigned_by_frame.get(item['source']['frame_id'])
        item.update(frame_owner_class=assignment['class_id'] if assignment else None,
            frame_owner_target_key=assignment['target_key'] if assignment else None,
            frame_owned_by_class=bool(assignment and assignment['class_id'] == item['class_id']),
            manual_reused=item['target_key'] in selected)
    manual = [saved_manual_proposal(k, rows[k], candidates[k], pose_sha,
        manifest['formal_manifest_sha256'], manifest['classification_sha256']) for k in sorted(selected)]
    templates = {}
    for key, (value, metadata, record) in sorted(candidates.items()):
        if len(value['polygon']) <= 4:
            continue
        templates[key] = dict(row=rows[key], polygon=value['polygon'], revision=value['revision'],
            human_polygon_revision=record['human_polygon_revision'], human_event_sha256=record['human_event_sha256'],
            current_payload_sha256=record['current_payload_sha256'], human_mask_confirmed=False,
            origin=prior.POSE_ORIGIN, pose_review=metadata['pose_review'],
            propagation_seed_eligible=True, rendering_id=GENERATION_NAMESPACE+':'+key,
            formal_manifest_sha256=manifest['formal_manifest_sha256'],
            class_assignments_sha256=manifest['classification_sha256'])
    if (len({p['source']['frame_id'] for p in manual}) != len(manual) or
            any(p['source']['frame_id'] in previous_frames for p in manual) or
            any(sum(p['class_id'] == c for p in manual) > TARGET_PER_CLASS for c in CLASSES) or
            any(len(t['polygon']) <= 4 for t in templates.values())):
        raise ValueError('Global frame/quota/template invariant failed')
    counts = class_inventory(rows, previous_frames, pool_keys, assignments, manual, templates, audit, capacity)
    for name, values in (('manual_proposals', manual),
            ('manual_source_records', [candidates[k][2] for k in sorted(selected)]),
            ('validated_manual_source_records', [candidates[k][2] for k in sorted(candidates)]),
            ('human_source_audit', audit), ('target_inventory', target_audit), ('frame_assignments', assignments)):
        write_jsonl(out/(name+'.jsonl'), values)
    for name in ('manual_source_records.jsonl', 'validated_manual_source_records.jsonl'):
        (out/name).chmod(0o600)
    for directory in ('cases', 'images', 'masks', 'labelme'):
        (out/directory).mkdir()
    prior.package_manual_preview(out, manual, evidence)
    (out/GENERATION_NAMESPACE/'templates').mkdir(parents=True)
    for key, template in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', template)
    inventory = dict(schema=VERSION, classes=counts, generation_template_namespace=GENERATION_NAMESPACE,
        templates=list(templates.values()), templates_by_class={c: sorted(k for k, t in templates.items()
            if t['row']['final_class_id'] == c) for c in CLASSES},
        manual_source_renderings=[dict(target_key=p['target_key'], polygon=p['polygon'],
            rendering_id=p['rendering_id'], human_source=p['human_source'], propagation_seed_eligible=False)
            for p in manual if p['manual_four_point_source']],
        rendering_by_target={p['target_key']: p['rendering_id'] for p in manual},
        sidecar=None, template_inventory_is_source_audit_only=True, machine_transfer_performed=False)
    write_json(out/'class_inventory.json', counts)
    write_json(out/'template_inventory.json', inventory)
    write_json(out/'gallery_source_audit.json', gallery_classes)
    write_json(out/'protected_targets.json', dict(quarantine=sorted(quarantine),
        inherited_500_frames=sorted(previous_frames), inherited_500_keys=sorted(p['target_key'] for p in previous),
        pose_mask_history_or_noneligible=sorted(k for k, v in status.items() if v != 'eligible'),
        gallery_saved_or_history=sorted(gallery_keys), confirmed_source_reuse=False,
        confirmed_targets=sorted(k for k, v in current.items() if confirmed(v))))
    write_json(out/'snapshot_manifest.json', dict(
        pose=dict(path=pose_snapshot.name, sha256=pose_sha, saved=len(current), events=pose_events,
            metadata=pose_metadata, immutable=True, backup='backups/'+pose_snapshot.name),
        gallery=dict(path=gallery_snapshot.name, sha256=gallery_sha, saved=len(reviews), events=gallery_events,
            metadata=gallery_metadata, immutable=True, backup='backups/'+gallery_snapshot.name)))
    write_json(out/'used_image_hashes.json', evidence.used)
    write_report(out, counts, manual, templates)
    after = protected_snapshot(formal)
    write_json(out/'protected_formal_after.json', after)
    if after != protected:
        raise ValueError('Protected formal files changed during audit')
    try:
        for live, frozen, table, metadata in ((pose_live, pose_snapshot, 'annotations', pose_metadata),
                (gallery_live, gallery_snapshot, 'reviews', gallery_metadata)):
            verify_history_preserved(live, frozen)
            prior.verify_sources_current(live, frozen, table)
            with closing(readonly(live)) as db:
                if dict(db.execute('SELECT key,value FROM metadata')) != metadata:
                    raise ValueError('Live source binding changed during audit')
        if (digest(pose_snapshot) != pose_sha or digest(gallery_snapshot) != gallery_sha or
                any(digest(out/'backups'/p.name) != digest(p) for p in (pose_snapshot, gallery_snapshot)) or
                file_inventory(parent) != parent_hashes or file_inventory(PROJECT/'configs') != config_hashes or
                any(digest(PROJECT/n) != h for n, h in code_hashes.items()) or
                any(digest(formal/n) != h for n, h in evidence.used.items())):
            raise ValueError('Snapshot/backup/baseline/config/code/source image changed')
    except ValueError as exc:
        write_json(out/'source_stability_check.json', dict(state='failed_requires_fresh_audit', reason=str(exc)))
        raise
    write_json(out/'source_stability_check.json', dict(state='stable', current_payloads_and_events_unchanged=True,
        metadata_unchanged=True, production_database_writes=0))
    result = dict(schema=VERSION, state='passed_awaiting_independent_validation', publication_version=None,
        published=False, independent_verification_performed=False, parent=str(parent),
        parent_manifest_sha256=parent_hashes['manifest.json'], parent_proposals_sha256=parent_hashes['proposals.jsonl'],
        parent_verification_sha256=parent_hashes['verification.json'],
        parent_archive_sha256=parent_hashes['pallet_mask_refined_review.zip'],
        parent_file_inventory_sha256=digest(out/'parent_file_inventory.json'),
        original_classification_targets=len(rows), original_classification_classes=22,
        formal_manifest_sha256=manifest['formal_manifest_sha256'], classification_sha256=manifest['classification_sha256'],
        proposed_target_per_class=100, proposed_target_total=1700, classes=counts,
        manual_proposals=len(manual), actual_human_labels_reused=len(manual),
        exact_validated_human_targets=sum(a['exact_human_validated'] for a in audit),
        validated_unprotected_human_targets=len(candidates),
        manual_four_point_sources=sum(p['manual_four_point_source'] for p in manual),
        manual_by_class=dict(Counter(p['class_id'] for p in manual)), generation_templates=len(templates),
        generation_template_namespace=GENERATION_NAMESPACE, scarcity_order=scarcity_order,
        capacity_total=sum(capacity.values()), capacity_shortfall=1700-sum(capacity.values()),
        missing_labels=1700-len(manual), inherited_canonical_preserved=500,
        inherited_resources_bytewise_unchanged=True, protected_formal_files=len(protected),
        snapshot_sha256=pose_sha, gallery_snapshot_sha256=gallery_sha,
        snapshot_saved=len(current), snapshot_events=pose_events,
        gallery_snapshot_saved=len(reviews), gallery_snapshot_events=gallery_events,
        code_sha256=digest(__file__), frozen_code_sha256=code_hashes, sidecar=None,
        original_splits_preserved=True, classification_is_candidate_not_truth=True,
        new_machine_proposals=0, machine_transfer_performed=False, registration_attempts=0,
        new_human_edits_created=0, human_requests_created=0, automatic_human_confirmation=False,
        automatic_training_eligible=False, ground_truth_eligible=False, robot_command_eligible=False,
        forced_fill=False, duplicate_fill=False, production_database_writes=0,
        configuration_writes=0, service_writes=0, public_archive_created=False,
        raw_source_records_private=True, source_current_stable=True)
    for name in ('manual_proposals', 'manual_source_records', 'validated_manual_source_records',
            'human_source_audit', 'target_inventory', 'frame_assignments'):
        result[name+'_sha256'] = digest(out/(name+'.jsonl'))
    for name in ('class_inventory', 'template_inventory', 'snapshot_manifest'):
        result[name+'_sha256'] = digest(out/(name+'.json'))
    write_json(out/'manifest.json', result)
    write_json(out/'generation_validation.json', dict(state='passed_awaiting_independent_validation',
        manifest_sha256=digest(out/'manifest.json'), independent_verification_performed=False,
        full_publication_seal=False, published=False, old_canonical_preserved=500,
        baseline_all_files_bytewise_unchanged=True, protected_formal_files=20768,
        no_production_writes=True, global_distinct_frames_verified=len(manual)+500))
    write_json(out/'artifact_hashes.json', file_inventory(out))
    return result


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=OUTPUT)
    args = parser.parse_args()
    result = generate(args.output)
    print(json.dumps({k: result[k] for k in ('state', 'manual_proposals', 'manual_by_class',
        'manual_four_point_sources', 'generation_templates', 'capacity_total', 'capacity_shortfall',
        'snapshot_saved', 'snapshot_events', 'gallery_snapshot_saved', 'gallery_snapshot_events')},
        ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
