"""Publication 11: read-only saved-review continuation, never a live-store writer.

Original-box reviews are a separate, statement-backed coverage category. They
are neither modified human masks nor transfer seeds. Frozen engines are imported
unchanged; only a previously absent output directory is written.
"""
from __future__ import annotations

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

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

from . import mask_remaining_expand as expand
from . import mask_remaining_classes as remaining
from . import mask_quota_100_continue as prior
from . import mask_quota_completion as original
from .cluster_targets import digest, protected_snapshot
from .mask_quota_saved_extension import file_inventory, indexed, load_formal_evidence
from .mask_template_continue import attempt_target, 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, overlay, write_json
from .pose_review_geometry import canonical, polygon_value, raster_mask

VERSION = 'remaining_saved_review_continue_v1'
PUBLICATION_VERSION = 11
GENERATION_NAMESPACE = 'remaining_continue_generation'
CLASSES, ALL_CLASSES = remaining.CLASSES, tuple(range(1, 23))
TARGET_PER_CLASS = 100
PARENT = PROJECT/'outputs/pallet_mask_remaining_expand_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_remaining_direct_requests_v2'
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_continue_v1'
MANUAL_SCHEMA, FALSE_FLAGS = prior.MANUAL_SCHEMA, prior.FALSE_FLAGS
REVIEW_SCHEMA = REVIEW_ORIGIN = 'reviewed_original_rectangle_entry_v1'
USER_STATEMENT = '已经完成269张候选复核和50张新图的标注，其中没有修改记录的是检验后无需修改的。继续'
ORIGINAL_REVIEWS = {'c64837a264b02755b52495c6': (4, 4), '3756ced3791a9669463f6fb0': (7, 1)}
HISTORICAL_NAMESPACES = expand.HISTORICAL_NAMESPACES + (
    remaining.GENERATION_NAMESPACE, expand.GENERATION_NAMESPACE)
PARENT_PINS = {
    'manifest.json': '237613bab6ab8274babc6ac0f33ef66973dbf45b9f25381924431d9644413649',
    'verification.json': '03a69478b67d48d3573480c27a1842840bd1457ffb9fcf6f6cb57255aad9291b',
    'proposals.jsonl': '024deaf1d2ca5ce9b6e9d12735cc2a7e24acdec36404c0a8cbd96aa6809888d2',
    'artifact_hashes.json': '4d611c41444bb92c8461075d2e0320061d22df54068058dd60242ecddd90ca7f',
    'template_inventory.json': 'd86620d2e5da9d253bee79816ca22bafe874854dd17fa500ad5603dbcf6bfb41',
    'all_manual_sources.jsonl': 'bd383587ec362b256ed98b8f55967e7b0cf4c6b235f0ad005fb83df6118beb62',
    'manual_proposals.jsonl': '47988f36d51bcfde3f3b9bde02b15204dc22b1f22a3e2b65554f180e29589890',
    'new_proposals.jsonl': 'ebc61908621aafc313acb2dfce46ab5cd333b81ace23e488762fb6ca8e84ec11',
    'review_dispositions.jsonl': 'f25170db6171ab170fd45df68343fa45168a4099d9c369431ee64bef5ea50072',
    'review_disposition_statement.json': '9f67c853dd7b5f0e5473653056e3ce45d5fdbb7f3c4a323dfe6400fee3fd1535',
    'pallet_mask_refined_review.zip': 'c0201a07d578db72a48151ff14195a3574c3c65234daffcfceb2be44691a06fd',
}
REQUEST_PINS = {
    'manifest.json': '9f7e9a6327ac652fa78057e3d256a10a80cae1ec6524e9dd5fb365ed8ca88c8b',
    'verification.json': '6f11c91117f8ac552cd759e4816c7292ca9de33a5862fc56c202fd07bdb34609',
    'independent_verification.json': '819bf24bf9881804fc1f2a7e12e3e347bfe33ad491a8f939ba1ede4f0ee302ce',
    'requests.jsonl': 'd44050d6ceea723c223e7219625778c77c6bd917932d297d2277efdb50f98cdb',
}
read_json = remaining.read_json


def verified_inputs(parent, requests):
    parent_hashes, request_hashes = file_inventory(parent), file_inventory(requests)
    for hashes, pins in ((parent_hashes, PARENT_PINS), (request_hashes, REQUEST_PINS)):
        if any(hashes.get(n) != h for n, h in pins.items()):
            raise ValueError('Pinned independently verified source changed')
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 10 or manifest.get('proposals') != 1061 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 10 or
            seal.get('manifest_sha256') != parent_hashes['manifest.json'] or
            seal.get('proposals_sha256') != parent_hashes['proposals.jsonl'] or
            seal.get('artifact_hashes_sha256') != parent_hashes['artifact_hashes.json']):
        raise ValueError('Matching independently verified publication 10 required')
    for n, h in read_json(parent/'artifact_hashes.json').items():
        if parent_hashes.get(n) != h:
            raise ValueError('Frozen parent resource changed: '+n)
    for n, h in manifest['frozen_code_sha256'].items():
        if digest(PROJECT/n) != h:
            raise ValueError('Frozen original engine changed: '+n)
    seal = read_json(requests/'independent_verification.json')
    if (seal.get('state') != 'passed' or seal.get('request_count') != 50 or
            seal.get('manifest_sha256') != request_hashes['manifest.json'] or
            seal.get('requests_sha256') != request_hashes['requests.jsonl'] or
            seal.get('parent_verification_sha256') != parent_hashes['verification.json']):
        raise ValueError('Matching independent 50-request seal required')
    for n, h in seal['public_assets'].items():
        if request_hashes.get(n) != h:
            raise ValueError('Verified request asset changed: '+n)
    return manifest, parent_hashes, request_hashes


def review_statement(requests):
    return dict(schema='explicit_user_review_no_change_statement_v1', date='2026-10-25',
        statement_zh=USER_STATEMENT, scoped_request_keys=sorted(indexed(requests)),
        reviewed_original_rectangle_keys=sorted(ORIGINAL_REVIEWS),
        scope='Current 50 direct requests only; unchanged means inspected without contour modification',
        candidate_count_in_statement=269, actual_parent_batch_saved_count=369,
        candidate_count_authority='actual immutable current/event evidence, not the conversational number',
        old_42_unsaved_not_inferred_reviewed=True, creates_human_event=False,
        confirms_ground_truth=False, authorizes_propagation=False)


def exact_original_rectangle_review(db, key, row, snapshot_sha, binding, statement):
    """Bind explicit no-change review to real raw current/event history, never fake a save."""
    if (statement.get('statement_zh') != USER_STATEMENT or
            key not in statement.get('scoped_request_keys', []) or
            key not in statement.get('reviewed_original_rectangle_keys', [])):
        raise ValueError('Original rectangle lacks an explicit scoped user review statement')
    found = list(db.execute('SELECT revision,payload FROM annotations WHERE target_key=?', (key,)))
    history = list(db.execute('SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,)))
    if len(found) != 1 or not history:
        raise ValueError('Original rectangle lacks unique current and real event history')
    revision, raw = found[0]; value = json.loads(raw)
    if len({rev for _, rev, _ in history}) != len(history):
        raise ValueError('Duplicate original rectangle event revision')
    flags = remaining.CONFIRMATION_FLAGS + ('full_six_dof',)

    def validate(v, rev):
        if (v.get('target_key') != key or v.get('revision') != rev or v.get('binding') != binding or
                v.get('state') != 'draft' or not remaining.original_rectangle(v.get('polygon'), row) or
                v.get('event_kind') in AUTO_EVENTS or remaining.machine_marked(v) or
                any(v.get(f, False) is not False for f in flags) or
                any(v.get('pose', {}).get(f, False) is not False for f in flags) or
                any(v.get('source', {}).get(f) != row[f] for f in prior.IDENTITY_FIELDS) or
                any(f in v.get('source', {}) and v['source'][f] != row.get(f)
                    for f in ('final_class_id', 'viewpoint_candidate'))):
            raise ValueError('Original rectangle identity/binding/history/state/flags mismatch')
        polygon_value(v['polygon'], *row['size'])

    validate(value, revision)
    for _, rev, oldraw in history:
        validate(json.loads(oldraw), rev)
    event_id, event_revision, event_raw = history[-1]
    if (event_revision != revision or max(v[1] for v in history) != revision or event_raw != raw):
        raise ValueError('Original rectangle current is not its exact latest real human event')
    event_sha = hashlib.sha256(event_raw.encode()).hexdigest()
    pose_review = dict(target_key=key, snapshot_sha256=snapshot_sha, revision=revision,
        payload_sha256=sha(value), saved_review_revision=event_revision,
        saved_review_event_id=event_id, saved_review_event_sha256=event_sha,
        polygon_sha256=sha(value['polygon']), user_statement_sha256=sha(statement))
    metadata = dict(target_key=key, class_id=row['final_class_id'], split=row['split'],
        snapshot_revision=revision, current_payload_sha256=sha(value), origin=REVIEW_ORIGIN,
        vertex_count=4, human_edited=False, saved_pose_review_source=True,
        user_reviewed_no_change=True, original_rectangle_review_only=True,
        propagation_seed_eligible=False, pose_review=pose_review)
    record = dict(schema='reviewed_original_rectangle_source_freeze_v1', target_key=key,
        source=row, source_identity_sha256=sha(row), snapshot_sha256=snapshot_sha,
        current_revision=revision, current_payload_raw=raw, current_payload=value,
        current_payload_sha256=sha(value), current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(),
        saved_review_event_id=event_id, saved_review_revision=event_revision,
        saved_review_event_payload_raw=event_raw, saved_review_event_payload=json.loads(event_raw),
        saved_review_event_sha256=event_sha, polygon_sha256=sha(value['polygon']),
        history_event_ids=[i for i, _, _ in history], human_edited=False,
        saved_pose_review_source=True, original_rectangle_review_only=True, user_reviewed_no_change=True,
        propagation_seed_eligible=False, user_statement_sha256=sha(statement),
        classification_is_candidate_not_truth=True, **dict.fromkeys(FALSE_FLAGS, False))
    return value, metadata, record


def completed_manual_proposals(requests, candidates, reviewed, rows, previous,
                               pose_sha, formal_sha, classification_sha, quarantine=(), gallery_keys=()):
    indexed(requests)
    frames = {p['source']['frame_id'] for p in previous}; keys = set(indexed(previous))
    result = []
    for request in sorted(requests, key=lambda r: r['target_key']):
        key = request['target_key']; row = rows[key]
        if (key in keys or row['frame_id'] in frames or key in quarantine or key in gallery_keys or
                row['final_class_id'] not in CLASSES or request['class_id'] != row['final_class_id'] or
                request['source'].get('formal_manifest_sha256') != formal_sha or
                any(request['source'].get(f) != row[f] for f in prior.IDENTITY_FIELDS+('viewpoint_candidate',))):
            raise ValueError('Completed request repeats protected frame or has changed source identity')
        if key in reviewed:
            if key in candidates:
                raise ValueError('Original review cannot be a modified manual source')
            value, metadata, _ = reviewed[key]
            p = dict(schema=REVIEW_SCHEMA, generation_schema=VERSION, origin=REVIEW_ORIGIN,
                target_key=key, class_id=row['final_class_id'], source=row, polygon=value['polygon'],
                vertex_count=4, status='user_reviewed_original_rectangle_pending_confirmation',
                human_edited=False, saved_pose_review_source=True, user_reviewed_no_change=True,
                original_rectangle_review_only=True, propagation_seed_eligible=False,
                source_label_kind='REVIEWED_ORIGINAL_RECTANGLE',
                coverage_basis='explicit_user_inspection_no_change_plus_real_saved_pose_review',
                mask_semantics='original_box_accepted_by_explicit_user_review_not_modified_mask_or_GT',
                original_box_review_basis='review_completion_statement.json', human_source=metadata,
                **dict.fromkeys(FALSE_FLAGS, False), metrics={}, pose_snapshot_saved=True,
                target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
                snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha,
                class_assignments_sha256=classification_sha, machine_draft=False,
                flags=['reviewed_original_rectangle_coverage_only_not_modified_human_mask_not_GT'])
        elif key in candidates:
            p = remaining.saved_manual_proposal(key, row, candidates[key], pose_sha, formal_sha, classification_sha)
            p.update(generation_schema=VERSION, machine_draft=False)
        else:
            raise ValueError('Completed request lacks exact saved human or explicit original-box review')
        p.update(manual_source_kind='completed_direct_manual_request',
            rendering_id=GENERATION_NAMESPACE+':'+('review_source:' if key in reviewed else 'manual_source:')+key,
            manual_request_sha256=sha(request), original_pose_url=request.get('pose_annotation_url'),
            pose_store_written=False, gallery_store_written=False, machine_transfer_performed=False,
            new_human_edit_created=False)
        result.append(p); frames.add(row['frame_id']); keys.add(key)
    return result


def generation_templates(candidates, remaining_keys, gallery, parent_new_keys, formal_sha, classification_sha):
    templates = {}
    for key in sorted(remaining_keys):
        value, metadata, record = candidates[key]
        if len(value['polygon']) <= 4:
            continue
        row = record['source']
        if row['final_class_id'] not in CLASSES:
            raise ValueError('Old five-class human cannot seed remaining continuation')
        templates[key] = dict(row=row, 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'])
    for key in sorted(parent_new_keys):
        if key not in gallery or key in templates:
            raise ValueError('Previous new candidate lacks validated >4 saved gallery review')
        t = gallery[key]
        if t['row']['final_class_id'] not in CLASSES or len(t['polygon']) <= 4:
            raise ValueError('Invalid remaining gallery propagation source')
        templates[key] = t
    return {key: dict(t, propagation_seed_eligible=True, rendering_id=GENERATION_NAMESPACE+':'+key,
        formal_manifest_sha256=formal_sha, class_assignments_sha256=classification_sha)
        for key, t in sorted(templates.items())}


def target_inventory(rows, locked, status, pose_protected, gallery_keys, quarantine):
    work, outcomes = expand.target_inventory(rows, locked, status, pose_protected, gallery_keys, quarantine)
    counts = Counter(p['class_id'] for p in locked)
    active = []
    for key, row in work:
        if counts[row['final_class_id']] >= TARGET_PER_CLASS:
            outcomes.append(dict(target_key=key, class_id=row['final_class_id'], reason='locked_class_quota_full',
                registration_attempts=0))
        else:
            active.append((key, row))
    return active, outcomes


def proposal_from_attempt(key, row, accepted, templates, current, pose_sha, formal_sha, classification_sha):
    p = original.proposal_from_attempt(key, row, accepted, templates, current, pose_sha, formal_sha, classification_sha)
    return dict(p, schema=VERSION, machine_draft=True, human_edited=False, machine_transfer_performed=True,
        classification_is_candidate_not_truth=True, target_per_class=100,
        rendering_id=GENERATION_NAMESPACE+':'+accepted['template_key'],
        continuation=dict(p['continuation'], batch=PUBLICATION_VERSION, all_compatible_templates_considered=True))


def class_counts(previous, manual, new, reviews, source_records, reviewed_records, dispositions,
                 templates, rows, work, outcomes, capacity):
    combined = previous+manual+new; indexed(combined)
    if len({p['source']['frame_id'] for p in combined}) != len(combined):
        raise ValueError('Cumulative frames are not globally unique')
    pose, boxes, acknowledged = map(lambda v: set(indexed(v)), (source_records, reviewed_records, dispositions))
    gallery = {k for k, v in reviews.items() if v.get('event_kind') == 'human_mask_gallery_revision' and
        v.get('human_edited') is True and v.get('state') not in ('deleted', 'deferred') and len(v.get('polygon', [])) >= 4}
    if pose & gallery or boxes & (pose | gallery | acknowledged) or acknowledged & (pose | gallery):
        raise ValueError('Modified sources, real saved gallery reviews and no-change coverage overlap')
    saved = pose | gallery
    four = {r['target_key'] for r in source_records if r['manual_four_point_source']}
    result = {}
    for cls in ALL_CLASSES:
        keys = {p['target_key'] for p in combined if p['class_id'] == cls}
        old = {p['target_key'] for p in previous if p['class_id'] == cls}
        n = len(keys)
        if n > TARGET_PER_CLASS:
            raise ValueError('Cumulative class exceeds quota')
        result[cls] = dict(class_id=cls, old_total=len(old),
            manual_requested=sum(p['class_id'] == cls for p in manual),
            manual_modified_added=sum(p['class_id'] == cls and p['schema'] == MANUAL_SCHEMA for p in manual),
            reviewed_original_rectangle=len(keys & boxes),
            saved_pose_human=len(keys & pose), saved_gallery_human=len(keys & gallery),
            saved_human=len(keys & saved), saved_human_contour_source=len(keys & saved),
            db_stored_human_review=len(keys & (saved | boxes)),
            modified_pose_source_records=len(keys & pose), manual_four_point_sources=len(keys & four),
            user_reviewed_no_change=len(keys & acknowledged),
            pending_machine_raw=len(keys-(saved | boxes)), old_pending_machine=len(old-(saved | boxes)),
            no_new_server_review=len(keys-(saved | boxes | acknowledged)),
            new_candidates=sum(p['class_id'] == cls for p in new),
            new_machine_pending=sum(p['class_id'] == cls for p in new),
            total=n, preview=n, distinct_frames=n, missing=100-n, goal=100, target_per_class=100,
            templates=sum(t['row']['final_class_id'] == cls for t in templates.values()),
            eligible=sum(r['final_class_id'] == cls for _, r in work),
            original_targets=sum(r['final_class_id'] == cls for r in rows.values()),
            original_distinct_frames=len({r['frame_id'] for r in rows.values() if r['final_class_id'] == cls}),
            capacity_matching_distinct_frames=capacity[cls], capacity_shortfall=100-capacity[cls],
            outcomes=dict(Counter(o['reason'] for o in outcomes if o['class_id'] == cls)),
            classification_is_candidate_not_truth=True, human_mask_confirmed=0, ground_truth_eligible=0)
    return result


def preserve_assets(parent, out, previous):
    """Full private parent clone plus byte-exact public historical assets/namespaces."""
    shutil.copytree(parent, out/'parent_evidence')
    hashes = {'parent_evidence/'+n: h for n, h in file_inventory(parent).items()}
    for p in previous:
        for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            n = f'{directory}/{p["target_key"]}.{extension}'
            shutil.copyfile(parent/n, out/n); hashes[n] = digest(parent/n)
    for directory in HISTORICAL_NAMESPACES:
        shutil.copytree(parent/directory, out/directory)
        hashes.update({directory+'/'+n: h for n, h in file_inventory(parent/directory).items()})
    for n in ('vertex2_before_after.png', 'FONT_LICENSE.txt', 'review_dispositions.jsonl',
            'review_disposition_statement.json'):
        shutil.copyfile(parent/n, out/n); hashes[n] = digest(parent/n)
    if any(digest(out/n) != h for n, h in hashes.items()):
        raise ValueError('Historical asset/private evidence copy changed bytes')
    return hashes


def package_manual_preview(out, manual, evidence):
    prior.package_manual_preview(out, [p for p in manual if p['schema'] == MANUAL_SCHEMA], evidence)
    for p in manual:
        if p['schema'] != REVIEW_SCHEMA:
            continue
        key, row = p['target_key'], p['source']; image = evidence.image(row['frame_id'])
        panel = Image.new('RGB', (1480, 305), '#121923'); draw = ImageDraw.Draw(panel)
        draw.text((15, 8), f'Class {p["class_id"]} | USER REVIEWED ORIGINAL RECTANGLE | NOT modified mask; NOT GT', fill='white')
        panel.paste(overlay(image, row, p['polygon'], '#55ceff'), (0, 30))
        for y, text in ((55, 'Explicit user statement: inspected; no contour change needed.'),
                (85, f'Real saved pose review revision {p["target_expected_revision"]}; no fabricated event.'),
                (115, 'Original box accepted for REVIEW COVERAGE ONLY.'),
                (145, 'NOT a human-modified mask; NOT a propagation template.'),
                (175, 'GT / automatic training / robot control: all disabled.')):
            draw.text((765, y), text, fill='white')
        draw.text((15, 284), f'original pose target {key} | {row["split"]} | original-box review only', fill='white')
        panel.save(out/'cases'/f'{key}.png'); Image.fromarray(image).save(out/'images'/f'{key}.png')
        Image.fromarray(raster_mask(p['polygon'], *row['size']).astype(np.uint8)*255).save(out/'masks'/f'{key}.png')
        flags = dict(human_edited=False, human_confirmed=False, machine_draft=False,
            saved_pose_review_source=True, original_rectangle_review_only=True, user_reviewed_no_change=True,
            propagation_seed_eligible=False, **dict.fromkeys(FALSE_FLAGS, False))
        write_json(out/'labelme'/f'{key}.json', dict(version='5.0.0', imagePath=f'../images/{key}.png',
            imageData=None, imageWidth=row['size'][0], imageHeight=row['size'][1],
            annotation_scope='single_target_mask_review_not_full_frame', other_objects_unchanged=True,
            flags=flags, original_box_review_basis=p['original_box_review_basis'],
            shapes=[dict(label='pallet', points=p['polygon'], shape_type='polygon', group_id=row['box_index'], flags=flags)],
            mask_template_provenance=p))


def package_new_preview(out, new, templates, evidence):
    # The pinned preview writer has no generation policy; labels below distinguish
    # saved gallery reviews (including unchanged saves) from original pose edits.
    expand.package_new_preview(out, new, templates, evidence)
    for p in new:
        t = templates[p['template']['target_key']]
        if t['origin'] != original.GALLERY_ORIGIN:
            continue
        key, row = p['target_key'], p['source']
        panel = Image.new('RGB', (1480, 305), '#121923'); draw = ImageDraw.Draw(panel)
        draw.text((15, 8), f'Class {p["class_id"]} | {p["vertex_count"]} vertices | SAVED GALLERY HUMAN REVIEW -> machine draft', fill='white')
        panel.paste(overlay(evidence.image(t['row']['frame_id']), t['row'], t['polygon'], '#55ceff'), (0, 30))
        panel.paste(overlay(evidence.image(row['frame_id']), row, p['polygon'], '#ffc247'), (740, 30))
        draw.text((15, 284), f'saved review {t["row"]["target_key"]} r{t["human_polygon_revision"]} | target {key} | {row["split"]} | NOT GT', fill='white')
        panel.save(out/'cases'/f'{key}.png')


def rendering_inventory(parent_inventory, templates, manual, combined, new_keys):
    historical = parent_inventory['templates']
    sources = parent_inventory.get('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, manual_four_point_source=p.get('manual_four_point_source', False),
        original_rectangle_review_only=p.get('original_rectangle_review_only', False), human_edited=p['human_edited'])
        for p in manual]
    all_templates = historical+list(templates.values())
    ids = [t['rendering_id'] for t in all_templates+sources]
    if len(set(ids)) != len(ids):
        raise ValueError('Historical/current rendering namespace collision')
    mapping = dict(parent_inventory['rendering_by_target'])
    mapping.update({p['target_key']: p['rendering_id'] for p in manual})
    mapping.update({p['target_key']: p['rendering_id'] for p in combined if p['target_key'] in new_keys})
    if set(mapping) != set(indexed(combined)) or not set(mapping.values()) <= set(ids):
        raise ValueError('Missing cumulative drawable source')
    return dict(schema=VERSION, publication_version=11, templates=all_templates,
        manual_source_renderings=sources, rendering_by_target=mapping,
        generation_template_keys=sorted(templates), generation_templates=len(templates),
        generation_template_namespace=GENERATION_NAMESPACE,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        historical_templates=len(historical), historical_templates_not_generation_authorization=True,
        historical_snapshot_provenance=dict(parent='parent_evidence/snapshot_manifest.json'))


def build_report(out, combined, counts, new_keys, manual_keys, pose_keys, reviews,
                 disposition_keys, status, templates):
    total = lambda f: sum(v[f] for v in counts.values())
    fields = ('old_total', 'manual_modified_added', 'reviewed_original_rectangle', 'saved_human',
        'new_candidates', 'total', 'missing')
    table = ''.join(f'<tr><td>{c}</td>'+''.join(f'<td>{counts[c][f]}</td>' for f in fields)+'</tr>' for c in ALL_CLASSES)
    cards = []
    for p in sorted(combined, key=lambda v: (v['class_id'], v['target_key'])):
        key = p['target_key']
        if key in new_keys:
            label = '【本轮新增机器候选】待人工复核，不是人工标签或真值'
        elif p.get('original_rectangle_review_only'):
            label = '【原框已复核无需修改】用户明确说明＋真实位姿保存；仅计复核覆盖，不是人工改绘或模板'
        elif key in manual_keys:
            label = '【本轮完成的人工四点改绘】真实保存，不补造顶点，不作传播模板'
        elif key in reviews:
            label = '【已有真实图册保存复核】历史图像原样保留；最新轮廓及原始事件单独冻结'
            if key in status['unchanged_keys']:
                label += '；与原候选相同，表示复核后无需改动，不声称新增人工改绘'
        elif key in pose_keys:
            label = '【既有人工改绘来源】历史快照原样保留，最新保存另行审计'
        elif key in disposition_keys:
            label = '【旧45份用户已复核无需修改】原侧记保留，不伪造数据库保存，不作模板'
        else:
            label = '【旧候选未见新服务器保存】不据此推断用户未查看；不扩大无需修改声明范围'
        hidden = ' hidden' if new_keys and key not in new_keys else ''
        kind = 'reviewed_original_rectangle' if p.get('original_rectangle_review_only') else 'manual' if key in manual_keys else 'machine'
        metrics = (f'<p>边界相关度 {p["metrics"]["boundary_ncc"]:.3f}；ECC {p["metrics"]["ecc"]:.3f}，仅为配准诊断，不是正确率。</p>' if key in new_keys else '')
        link = (f'<p><a href="{html.escape(p["original_pose_url"], quote=True)}">打开原位姿人工标注</a></p>' if p.get('original_pose_url') else '')
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" data-kind="{kind}" '
            f'data-batch="{"new" if key in new_keys else "previous"}"{hidden}><h2>类别 {p["class_id"]} · {label}</h2>'
            f'<img loading="lazy" src="cases/{key}.png?revision=11" alt="单目标历史快照或本轮来源对照">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>{metrics}{link}'
            f'<a href="labelme/{key}.json?revision=11">多边形（LabelMe）</a> · <a href="masks/{key}.png?revision=11">掩膜</a></article>')
    page = '''<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>第十一版 · 剩余类别严格续批</title><style>@font-face{font-family:PreviewSans;src:url('report-font.otf?revision=11')}*{box-sizing:border-box}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1090px}a{color:#77ceff}img{display:block;width:100%;max-width:100%;height:auto}article{border:1px solid #425269;margin:20px 0;padding:12px;min-width:0}p,h2{overflow-wrap:anywhere}.table-wrap{max-width:100%;overflow-x:auto}td,th{padding:9px;border:1px solid #425269;white-space:nowrap}table{border-collapse:collapse;min-width:1090px;width:100%}select{font:inherit;padding:6px}[hidden]{display:none!important}@media(max-width:700px){body{padding:10px}article{padding:7px}h2{font-size:18px}}</style>
<h1>第十一版 · 剩余十七类严格续批（待独立核验，尚未发布）</h1>
'''
    page += (f'<section id="new-batch"><h2>本轮新增机器候选 {len(new_keys)} 份</h2>'
        f'<p>上轮369份候选均有真实保存：260份轮廓变化、109份复核后保持原候选；109份不称为新增人工改绘。'
        f'本轮50项请求：48份实际修改的四点轮廓，另2份原框经用户明确复核无需修改。'
        f'人工保存轮廓来源 {total("saved_human")} = 位姿改绘 {total("saved_pose_human")} + 图册保存复核 {total("saved_gallery_human")}；'
        f'另列原框复核 {total("reviewed_original_rectangle")}，数据库已存复核合计 {total("db_stored_human_review")}。'
        f'全22类累计覆盖 {total("total")}/2200 个不同帧，缺 {total("missing")}；覆盖不是人工完成率或真值率。</p></section>')
    page += (f'<p>传播仅使用原剩余192份来源中的41份超过四点人工改绘，以及上轮369份真实保存的超过四点图册复核，合计 {len(templates)} 份。'
        '新48份四点及2份原框不作模板；旧45份对话侧记和其余42份旧未保存候选不作模板。'
        '类别6、13及旧五类已经达到100，禁止继续生成；类别4、7、22无超过四点模板。'
        f'类别19锁定现有覆盖后的原图容量为 {counts[19]["capacity_matching_distinct_frames"]}，不强行补满。</p>')
    page += (f'<p>本次用户原话：{html.escape(USER_STATEMENT)}。其中269是对话数字；实际冻结证据为369个上轮候选全部保存，不据此臆测其余42份旧候选。'
        '原框2份仅表示用户接受原框用于复核覆盖，不改称叉面人工掩膜，也不确认为GT。</p>')
    page += '''<p>只用冻结的原始 attempt_target：同类、同划分、同传感器与兼容视角；初始相关度至少0.55、ECC至少0.84、边界相关度至少0.78、逐顶点往返误差不超过1.5并通过横梁边界检查。保持顶点数及顺序，不递归传播、不放宽门槛、不跨类或冲突视角、不强行填满。1111个已计入帧全部锁定；最终新增使用全局不同帧最大匹配和组别多样性选择。</p>
<p>旧1061条规范记录、图像、掩膜、LabelMe及全部历史命名空间逐字节保留，完整私有证据在parent_evidence冻结。历史卡片不冒充最新保存图。两库最新快照与备份、全部历史及20768个正式文件均受保护。不写生产库、配置或服务；不创建新人工请求或ZIP。所有结果未确认真值，不自动训练或控制机器人。</p>
'''
    page += '<div class="table-wrap"><table><tr><th>类别</th><th>旧覆盖</th><th>新人工改绘</th><th>另列原框复核</th><th>人工保存轮廓</th><th>新增机器</th><th>累计不同帧</th><th>缺额</th></tr>'+table+'</table></div>'
    page += '<p>筛选类别 <select id="filter"><option value="">全部22类</option>'+''.join(f'<option value="{c}">{c}</option>' for c in ALL_CLASSES)+'</select> '
    page += '<label><input type="checkbox" id="only-new"'+(' checked' if new_keys else '')+'>只看本轮新增机器候选</label></p>'+''.join(cards)
    page += """<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();</script></html>"""
    (out/'report_zh.html').write_text(page)
    from fontTools import subset
    from fontTools.ttLib import TTFont
    source = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    with TTFont(source) as font:
        if not required <= set(font.getBestCmap()):
            raise ValueError('Report source font missing Chinese glyph')
        sub = subset.Subsetter(); sub.populate(text=page); sub.subset(font); font.save(out/'report-font.otf')
    with TTFont(out/'report-font.otf') as font:
        if not required <= set(font.getBestCmap()):
            raise ValueError('Report subset missing Chinese glyph')
    write_json(out/'font_coverage.json', dict(chinese_glyphs=len(required), all_report_Chinese_present=True,
        font_source_sha256=digest(source), font_sha256=digest(out/'report-font.otf')))


def generate(output=OUTPUT, workers=4, parent=PARENT, requests_directory=REQUESTS):
    out, parent, request_dir = (Path(p).resolve() for p in (output, parent, requests_directory))
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory; never overwrite frozen publications')
    if not 1 <= workers <= 8:
        raise ValueError('Workers must be 1–8')
    manifest, parent_hashes, request_hashes = verified_inputs(parent, request_dir)
    config_hashes = file_inventory(PROJECT/'configs')
    frozen_code = dict(manifest['frozen_code_sha256'])
    frozen_code['pallet/mask_remaining_continue.py'] = digest(__file__)
    formal, rows, evidence = load_formal_evidence(manifest)
    formal_sha, classification_sha = manifest['formal_manifest_sha256'], manifest['classification_sha256']
    previous, parent_new, old_manual, requests = (read_jsonl(p) for p in (
        parent/'proposals.jsonl', parent/'new_proposals.jsonl', parent/'manual_proposals.jsonl', request_dir/'requests.jsonl'))
    old_pose_keys = set(indexed(read_jsonl(parent/'all_manual_sources.jsonl')))
    remaining_keys, parent_new_keys = set(indexed(old_manual)), set(indexed(parent_new))
    if (len(rows) != 12344 or {r['final_class_id'] for r in rows.values()} != set(ALL_CLASSES) or
            len(previous) != 1061 or len(parent_new) != 369 or len(old_manual) != 192 or len(old_pose_keys) != 335 or
            len({p['source']['frame_id'] for p in previous}) != 1061 or
            any(p['source'] != rows[p['target_key']] for p in previous) or
            Counter(r['class_id'] for r in requests) != {c: 10 for c in (4, 5, 7, 14, 22)}):
        raise ValueError('Original formal / canonical1061 / prior369 / direct50 inventory changed')
    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 formal 20768-file inventory changed')
    out.mkdir(parents=True)
    for n, value in (('parent_file_inventory', parent_hashes), ('request_file_inventory', request_hashes),
            ('configuration_input_hashes', config_hashes), ('protected_formal_before', protected)):
        write_json(out/(n+'.json'), value)
    statement = review_statement(requests); write_json(out/'review_completion_statement.json', statement)
    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)
    candidates, reviewed, source_audit = {}, {}, []
    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') != formal_sha 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 binding changed')
        current = remaining.snapshot_rows(db, 'annotations', rows)
        remaining.snapshot_rows(db, 'events', rows)
        _, _, eligibility = annotation_inventory(db, rows)
        pose_protected = expand.protected_history(db, rows, current)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        requested = set(indexed(requests))
        for key in sorted(old_pose_keys | requested):
            if key in ORIGINAL_REVIEWS:
                reviewed[key] = exact_original_rectangle_review(db, key, rows[key], pose_sha, sha(binding), statement)
                if (rows[key]['final_class_id'], reviewed[key][0]['revision']) != ORIGINAL_REVIEWS[key]:
                    raise ValueError('Explicit original-box review class/revision changed')
                candidate = reviewed[key]
            else:
                candidates[key] = remaining.validated_manual_source(db, key, rows[key], pose_sha, sha(binding))
                candidate = candidates[key]
            value, metadata, record = candidate
            source_audit.append(dict(target_key=key, class_id=rows[key]['final_class_id'],
                current_revision=value['revision'], current_payload_sha256=sha(value),
                current_payload_raw_sha256=record['current_payload_raw_sha256'],
                exact_current_and_event_validated=True, modified_human_contour=key not in reviewed,
                original_rectangle_review_only=key in reviewed, vertex_count=len(value['polygon']),
                historical_canonical_preserved=key in old_pose_keys,
                current_template_selected=key in remaining_keys and len(value['polygon']) > 4,
                snapshot_sha256=pose_sha))
    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') != formal_sha:
            raise ValueError('Gallery formal binding changed')
        reviews = remaining.snapshot_rows(db, 'reviews', rows)
        remaining.snapshot_rows(db, 'events', rows)
        events = [json.loads(raw) for raw, in db.execute('SELECT payload FROM events ORDER BY id')]
        gallery_keys = set(reviews) | {v['target_key'] for v in events}
        gallery_sources = []
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews ORDER BY target_key'):
            history = list(db.execute('SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,)))
            if not history or history[-1][1:] != (revision, raw):
                raise ValueError('Gallery current is not exact raw latest event')
            event_id, _, event_raw = history[-1]
            gallery_sources.append(dict(target_key=key, revision=revision, current_payload_raw=raw,
                current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), current_payload=json.loads(raw),
                current_payload_sha256=sha(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))
    paths = dict(expand.complete.SOURCE_PUBLICATIONS)
    paths[9] = remaining.PARENT; paths[10] = parent
    publication_hashes = {v: file_inventory(p) for v, p in paths.items() if p != parent}
    # Generic arbitrary-class historical-publication validator, not a five-class filter.
    gallery_templates = original.gallery_templates(previous, reviews, events, current, rows, parent,
        formal_sha, gallery_sha, publication_paths=paths)
    previous_by_key, gallery_source_by_key = indexed(previous), indexed(gallery_sources)
    for key, template in gallery_templates.items():
        if key not in parent_new_keys:
            continue
        source = gallery_source_by_key[key]
        template.update(saved_human_review=True, new_human_edit_created=False,
            saved_review_unchanged=reviews[key]['polygon'] == previous_by_key[key]['polygon'],
            polygon_modified_from_proposal=reviews[key]['polygon'] != previous_by_key[key]['polygon'],
            current_gallery_payload_raw_sha256=source['current_payload_raw_sha256'],
            saved_review_event_id=source['human_event_id'], saved_review_event_raw_sha256=source['human_event_sha256'])
    quarantine = set(QUARANTINE) | set(manifest.get('preserved_quarantined_targets', []))
    manual = completed_manual_proposals(requests, candidates, reviewed, rows, previous,
        pose_sha, formal_sha, classification_sha, quarantine, gallery_keys)
    locked = previous+manual
    templates = generation_templates(candidates, remaining_keys, gallery_templates, parent_new_keys, formal_sha, classification_sha)
    all_sources = [candidates[k][2] for k in sorted(candidates)]
    review_sources = [reviewed[k][2] for k in sorted(reviewed)]
    if (len(locked) != 1111 or len({p['source']['frame_id'] for p in locked}) != 1111 or
            len(all_sources) != 383 or len(review_sources) != 2 or
            len(templates) != 410 or Counter(t['origin'] for t in templates.values()) != {
                prior.POSE_ORIGIN: 41, original.GALLERY_ORIGIN: 369} or
            any(len(p['polygon']) != 4 for p in manual)):
        raise ValueError('Exact locked1111 / modified383 / review2 / seed410 contract changed')
    saved_status_records = []
    for p in parent_new:
        key = p['target_key']; review = reviews[key]; source = gallery_source_by_key[key]
        saved_status_records.append(dict(target_key=key, class_id=p['class_id'],
            actual_saved=True, polygon_changed_from_proposal=review['polygon'] != p['polygon'],
            saved_review_unchanged=review['polygon'] == p['polygon'],
            human_new_edit_claim=False, new_human_edit_created=False,
            proposal_sha256=sha(p), proposal_polygon_sha256=sha(p['polygon']),
            current_revision=review['revision'], current_polygon_sha256=sha(review['polygon']),
            current_payload_sha256=sha(review), current_payload_raw_sha256=source['current_payload_raw_sha256'],
            human_event_id=source['human_event_id'], human_event_sha256=source['human_event_sha256'],
            gallery_snapshot_sha256=gallery_sha, source_binding=review['source_binding'],
            proposal_publication=review['proposal_publication'], binding=review['binding'], base_proposal_id=review['base_proposal_id']))
    unchanged = sorted(r['target_key'] for r in saved_status_records if r['saved_review_unchanged'])
    status = dict(parent_new_machine_candidates=369, actually_saved=len(saved_status_records), pending=0,
        polygon_changed_from_proposal=369-len(unchanged), saved_unchanged=len(unchanged),
        saved_keys=sorted(parent_new_keys), pending_keys=[], unchanged_keys=unchanged,
        saved_evidence_authority='real frozen current/raw latest events with historical publication/image/source bindings',
        unchanged_review_interpretation_statement_sha256=sha(statement),
        new_dispositions_created=0, new_human_edits_created=0, unsaved_candidates_are_seeds=False)
    if status['polygon_changed_from_proposal'] != 260 or status['saved_unchanged'] != 109:
        raise ValueError('Actual saved260-changed /109-unchanged review evidence changed')
    work, outcomes = target_inventory(rows, locked, eligibility, pose_protected, gallery_keys, quarantine)
    capacity_candidates = [dict(target_key=k, class_id=r['final_class_id'], source=r, metrics={}) for k, r in work]
    potential, _, capacity_assignments, capacity_order = expand.select_locked_diverse(locked, capacity_candidates)
    capacity = Counter(p['class_id'] for p in locked+potential)
    for cls in ALL_CLASSES:
        capacity.setdefault(cls, 0)
    if capacity[19] > 90:
        raise ValueError('Protected original class19 capacity cannot exceed90')
    write_json(out/'protected_targets.json', dict(locked_keys=sorted(indexed(locked)),
        locked_frames=sorted(p['source']['frame_id'] for p in locked), quarantine=sorted(quarantine),
        pose_mask_or_state_history_protected=sorted(pose_protected), gallery_saved_or_history=sorted(gallery_keys),
        eligibility=eligibility, counted_manual_source_keys=sorted(candidates),
        reviewed_original_rectangle_keys=sorted(reviewed), conversation_dispositions_not_seed_authorization=True))
    write_json(out/'snapshot_manifest.json', dict(formal_manifest_sha256=formal_sha,
        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=len(events),
            metadata=gallery_metadata, immutable=True, backup='backups/'+gallery_snapshot.name),
        historical_snapshots_retained_under=['parent_evidence']))
    for n, values in (('manual_proposals', manual), ('completed_request_manual_proposals', manual),
            ('manual_source_records', [candidates[k][2] for k in sorted(set(indexed(requests))-set(reviewed))]),
            ('all_manual_sources', all_sources), ('reviewed_original_rectangle_source_records', review_sources),
            ('human_source_audit', source_audit), ('remaining_current_manual_source_records', [candidates[k][2] for k in sorted(remaining_keys)]),
            ('gallery_source_records', gallery_sources), ('saved_review_source_audit', saved_status_records),
            ('refined_annotations', [reviews[k] for k in sorted(reviews)]), ('capacity_frame_assignments', capacity_assignments)):
        write_jsonl(out/(n+'.jsonl'), values)
    for n in ('manual_source_records', 'all_manual_sources', 'reviewed_original_rectangle_source_records',
            'remaining_current_manual_source_records', 'gallery_source_records'):
        (out/(n+'.jsonl')).chmod(0o600)
    write_json(out/'saved_review_status.json', status)
    shutil.copyfile(request_dir/'requests.jsonl', out/'requests.jsonl')
    for request in requests:
        row = rows[request['target_key']]; native = evidence.image(row['frame_id'])
        if request['source']['image_sha256'] != evidence.used[row['image']]:
            raise ValueError('Completed request native source image changed')
        with Image.open(request_dir/'images'/f'{row["target_key"]}.png') as image:
            if not np.array_equal(np.asarray(image), native):
                raise ValueError('Request full native pixels differ from formal source')
    for p in previous:
        with Image.open(parent/'images'/f'{p["target_key"]}.png') as image:
            if not np.array_equal(np.asarray(image), evidence.image(p['source']['frame_id'])):
                raise ValueError('Inherited full image differs from formal native pixels')
    print(json.dumps(dict(state='scanning', workers=workers, locked=len(locked), templates=len(templates),
        eligible_targets=len(work), saved_human_contours=1022, reviewed_original_rectangle=2,
        pose_saved=len(current), pose_events=pose_events, gallery_saved=len(reviews), gallery_events=len(events))), flush=True)
    attempts, accepted = [], []
    def worker(item):
        return item, attempt_target(*item, templates, evidence)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (candidate, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            attempts.extend(trials); outcomes.append(dict(outcome, registration_attempts=len(trials)))
            if candidate:
                accepted.append(proposal_from_attempt(key, row, candidate, templates, current, pose_sha, formal_sha, classification_sha))
            if number % 200 == 0:
                print(json.dumps(dict(scanned=number, accepted=len(accepted), attempts=len(attempts))), flush=True)
    new, selections, assignments, scarcity_order = expand.select_locked_diverse(locked, accepted)
    for outcome in outcomes:
        if outcome['target_key'] in selections:
            outcome['reason'] = selections[outcome['target_key']]
    if len(outcomes) != len(rows) or set(indexed(outcomes)) != set(rows):
        raise ValueError('Every formal target requires exactly one outcome')
    combined = sorted(locked+new, key=lambda p: p['target_key'])
    restored = indexed(combined)
    if any(canonical(restored[p['target_key']]) != canonical(p) for p in previous):
        raise ValueError('Inherited canonical proposal changed')
    dispositions = read_jsonl(parent/'review_dispositions.jsonl')
    counts = class_counts(previous, manual, new, reviews, all_sources, review_sources, dispositions,
        templates, rows, work, outcomes, capacity)
    total = lambda f: sum(v[f] for v in counts.values())
    if (total('saved_human') != 1022 or total('db_stored_human_review') != 1024 or
            total('user_reviewed_no_change') != 45 or total('no_new_server_review') != 42+len(new)):
        raise ValueError('Saved contour1022 / original review2 / old45 / other42 contract changed')
    for n, values in (('proposals', combined), ('new_proposals', new), ('registration_attempts', attempts),
            ('accepted_candidates', accepted), ('outcomes', sorted(outcomes, key=lambda v: v['target_key'])),
            ('frame_assignments', [dict(frame_id=p['source']['frame_id'], class_id=p['class_id'],
                target_key=p['target_key'], locked=True, phase='previously_counted_source') for p in locked]+assignments)):
        write_jsonl(out/(n+'.jsonl'), values)
    for d in ('cases', 'images', 'masks', 'labelme'):
        (out/d).mkdir()
    copied = preserve_assets(parent, out, previous)
    package_manual_preview(out, manual, evidence); package_new_preview(out, new, templates, 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 = rendering_inventory(read_json(parent/'template_inventory.json'), templates, manual, combined, set(indexed(new)))
    inventory.update(classes=counts, previous_proposals=1061, manual_proposals=50, modified_manual_proposals=48,
        reviewed_original_rectangle=2, all_manual_sources=383, snapshot_saved=len(current), snapshot_events=pose_events,
        gallery_snapshot_saved=len(reviews), gallery_snapshot_events=len(events), eligibility=eligibility,
        target_per_class=100, counted_manual_source_keys=sorted(candidates))
    write_json(out/'template_inventory.json', inventory); write_json(out/'preserved_parent_assets.json', copied)
    build_report(out, combined, counts, set(indexed(new)), set(indexed(manual)), set(candidates), reviews,
        set(indexed(dispositions)), status, templates)
    contact = Image.new('RGB', (1480, 305*len(ALL_CLASSES)), '#121923')
    for i, cls in enumerate(ALL_CLASSES):
        p = next(p for p in new+manual+previous if p['class_id'] == cls)
        with Image.open(out/'cases'/f'{p["target_key"]}.png') as image:
            contact.paste(image, (0, i*305))
    contact.save(out/'preview_all_classes.png')
    used = read_json(parent/'used_image_hashes.json')
    for n, h in evidence.used.items():
        if n in used and used[n] != h:
            raise ValueError('Formal native image hash conflict')
        used[n] = h
    if any(digest(formal/n) != h for n, h in used.items()):
        raise ValueError('Used source image changed')
    write_json(out/'used_image_hashes.json', used)
    after = protected_snapshot(formal); write_json(out/'protected_formal_after.json', after)
    if after != protected:
        raise ValueError('Formal files changed')
    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 metadata changed during generation')
    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(request_dir) != request_hashes or
            file_inventory(PROJECT/'configs') != config_hashes or
            any(file_inventory(paths[v]) != hashes for v, hashes in publication_hashes.items()) or
            any(digest(PROJECT/n) != h for n, h in frozen_code.items()) or
            any(digest(out/n) != h for n, h in copied.items())):
        raise ValueError('Frozen inputs / immutable evidence / code / config / assets changed')
    write_json(out/'source_stability_check.json', dict(state='stable', source_current_stable=True,
        current_payloads_and_events_unchanged=True, metadata_unchanged=True, production_database_writes=0))
    write_json(out/'source_publication_file_inventories.json', publication_hashes)
    write_json(out/'shortfalls.json', {c: dict(missing=counts[c]['missing'], capacity=capacity[c],
        capacity_shortfall=100-capacity[c], templates=counts[c]['templates'], outcomes=counts[c]['outcomes'],
        manual_template_required=counts[c]['templates'] == 0,
        explanation='Strict original gates; 1111 locked frames; no four-point/original-box propagation; no fake fill.') for c in CLASSES})
    result = dict(state='remaining_continue_pending_independent_verification', schema=VERSION, version=VERSION,
        publication_version=11, 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'],
        previous_proposals=1061, manual_proposals=50, completed_request_manual_proposals=50,
        modified_manual_proposals=48, reviewed_original_rectangle=2, reviewed_original_rectangle_schema=REVIEW_SCHEMA,
        manual_source_schema=MANUAL_SCHEMA, inherited_manual_sources=335, all_manual_sources=383,
        base_proposals=1111, proposals=len(combined), new_proposals=len(new), new_machine_pending=len(new),
        new_by_class=dict(Counter(p['class_id'] for p in new)), manual_by_class=dict(Counter(p['class_id'] for p in manual)),
        classes=counts, remaining_classes=list(CLASSES), all_classes=list(ALL_CLASSES), target_per_class=100,
        target_total=2200, remaining_target_total=1700, missing=total('missing'), saved_human=total('saved_human'),
        saved_human_contour_source=total('saved_human'), saved_pose_human=total('saved_pose_human'),
        saved_gallery_human=total('saved_gallery_human'), modified_pose_source_records=383,
        db_stored_human_review=total('db_stored_human_review'), pending_machine_raw=total('pending_machine_raw'),
        old_pending_machine=total('old_pending_machine'), user_reviewed_no_change=45,
        no_new_server_review=total('no_new_server_review'), saved_review_status=status,
        counted_manual_source_keys=sorted(candidates), requested_manual_keys=sorted(indexed(manual)),
        reviewed_original_rectangle_keys=sorted(reviewed), manual_four_point_sources=48,
        all_pose_four_point_sources=sum(r['manual_four_point_source'] for r in all_sources),
        generation_templates=len(templates), generation_template_namespace=GENERATION_NAMESPACE,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        generation_templates_by_class=dict(Counter(t['row']['final_class_id'] for t in templates.values())),
        generation_template_keys=sorted(templates), method='original', workers=workers,
        scanned_eligible_targets=len(work), registration_attempts=len(attempts), accepted_before_diversity=len(accepted),
        outcomes=dict(Counter(v['reason'] for v in outcomes)), scarcity_order=scarcity_order,
        capacity_matching_order=capacity_order, capacity_matching_total=sum(capacity.values()),
        formal_manifest_sha256=formal_sha, classification_sha256=classification_sha,
        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=len(events), protected_formal_files=len(protected),
        preserved_quarantined_targets=sorted(quarantine), original_splits_preserved=True,
        cross_split_template_transfers=0, recursive_candidate_propagation=False, thresholds_relaxed=False,
        forced_fill=False, duplicate_fill=False, research_candidates_added=0, new_human_edits_created=0,
        human_requests_created=0, production_database_writes=0, configuration_writes=0, service_writes=0,
        automatic_human_confirmation=False, automatic_training_eligible=False, robot_command_eligible=False,
        ground_truth_eligible=False, classification_is_candidate_not_truth=True, source_current_stable=True,
        public_archive_created=False, historical_canonical_preserved=1061, inherited_resources_bytewise_preserved=True,
        manual_source_snapshots_imported_into_gallery_store=False, code_sha256=digest(__file__), frozen_code_sha256=frozen_code,
        transfer_code_sha256=frozen_code['pallet/mask_template_transfer.py'], boundary_code_sha256=frozen_code['pallet/mask_boundary_review.py'],
        attempt_code_sha256=frozen_code['pallet/mask_template_continue.py'])
    for path in sorted(out.glob('*.jsonl')):
        result[path.stem+'_sha256'] = digest(path)
    for n in ('template_inventory', 'snapshot_manifest', 'font_coverage', 'saved_review_status', 'review_completion_statement'):
        result[n+'_sha256'] = digest(out/(n+'.json'))
    write_json(out/'manifest.json', result)
    write_json(out/'generation_validation.json', dict(state='self_checks_passed_not_independent_verification',
        publication_version=11, published=False, independent_verification_performed=False,
        historical_canonical_preserved=1061, locked_before_machines=1111, global_distinct_frames=len(combined),
        preserved_source_files=len(copied), protected_formal_files=20768, no_production_writes=True))
    (out/'GENERATION_HANDOFF.md').write_text(
        '# Publication 11 draft — pending independent verification\n\n'
        f'{len(combined)} distinct frames = immutable parent1061 + completed50 + new machine{len(new)}. '
        '48 completed requests are genuinely modified four-point pose contours; 2 are explicitly reviewed original boxes. '
        'The latter have separate schema/origin and raw real pose-review provenance, no fabricated human-mask event.\n\n'
        '`all_manual_sources.jsonl`: latest383 modified pose sources (old335+48). '
        '`reviewed_original_rectangle_source_records.jsonl`: separate2. `manual_proposals.jsonl`: completed50. '
        '`gallery_source_records.jsonl`: all639 latest raw current/event pairs. '
        '`saved_review_source_audit.jsonl`: exactly369 prior candidates,260 changed and109 saved unchanged; '
        'the109 are real saved reviews, not newly invented human edits. `review_completion_statement.json` freezes the user statement.\n\n'
        '410 direct >4-point remaining seeds = pose41 + saved gallery369. New quads/original boxes, old45 conversation '
        'dispositions and old42 unsaved candidates never seed. All1061 canonical proposals and public assets/namespaces '
        'are byte-exact; full private parent under parent_evidence, with hash proof in preserved_parent_assets.json. '
        'Saved contour sources1022 and original-box reviews2 are separate; stored review total1024. '
        'Independent verifier must replay every eligible attempt and audit the frozen raw evidence before publishing. '
        'No requests, archive, live database, configuration or service writes performed.\n')
    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)
    parser.add_argument('--workers', type=int, default=4)
    args = parser.parse_args(); cv2.setNumThreads(1)
    result = generate(args.output, args.workers)
    print(json.dumps({k: result[k] for k in ('state', 'proposals', 'new_proposals', 'new_by_class',
        'saved_human', 'reviewed_original_rectangle', 'missing', 'registration_attempts', 'accepted_before_diversity', 'code_sha256')}, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
