"""Publication 13: freeze real saves, reserve unreviewed boxes, strict transfer.

Writes only a previously absent output directory. Original engines are imported
unchanged; saved original rectangles without a scoped review remain uncounted.
The output is a draft requiring independent verification, not a publication.
"""
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

from . import mask_remaining_round2 as previous_generation
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, write_json
from .pose_review_geometry import canonical, polygon_value

VERSION = 'remaining_saved_review_round3_v1'
PUBLICATION_VERSION = 13
GENERATION_NAMESPACE = 'remaining_round3_generation'
CLASSES, ALL_CLASSES = remaining.CLASSES, tuple(range(1, 23))
TARGET_PER_CLASS = 100
PARENT = PROJECT/'outputs/pallet_mask_remaining_round2_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_remaining_round2_requests_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_round3_v1'
MANUAL_SCHEMA, FALSE_FLAGS = prior.MANUAL_SCHEMA, prior.FALSE_FLAGS
REVIEW_SCHEMA = previous_generation.REVIEW_SCHEMA
USER_STATEMENT = '已经完成185张候选复核和54张新图的标注。新图的人工任务应当在标注后再提供，继续'
PENDING_ORIGINAL_RECTANGLES = {
    '966203cd4e5ebd979e98ab2a': (4, 13), '0cb41390d16d3bcee50315e5': (7, 1)}
HISTORICAL_NAMESPACES = previous_generation.HISTORICAL_NAMESPACES + (previous_generation.GENERATION_NAMESPACE,)
PARENT_PINS = {
    'manifest.json': 'c76c845420a35b69ed860e8efed3d15b9dc3083629013c3a5dd5ab6b3b2d747e',
    'verification.json': '724cd94da93f9c3ff5a103ca8becf014a9b90857b4f8efda0b3cce318c820a65',
    'proposals.jsonl': 'e3f1fdf32f6672238481d2cf69e08ae1cf0c12fd3f817851e33131367649f26e',
    'artifact_hashes.json': 'e92c1f3f8aeec83b3ad61132f4d86aa23b655ea59a2d25c3b91eb207997cead0',
    'all_manual_sources.jsonl': '2ff51607050be6a9f759e03cd79c1ccb320a0a1aa54d7bd8211bae05faf74586',
    'new_proposals.jsonl': '8e23bdbb5e641b510399ddee5366b3445966e80bff0f7b5f33777a527d83f395',
    'reviewed_original_rectangle_source_records.jsonl': 'e0370bfb8ccd384e14078433d3920ee2e17aa054d39e012cf72905c0c4d2867f',
    'review_completion_statement.json': '5173db46a432eb9a5708781b150ac7dea957b3202f0088369d223017a475bf68',
}
REQUEST_PINS = {
    'manifest.json': 'addbe5516f031a0ea2f792d286be94cbbfa3cda54c2856ea6c3a2d002f0b1e15',
    'verification.json': '1b804a2d8f8f662243050a6d563e7e0799de31410b268176cb5486999e16786d',
    'independent_verification.json': '1b804a2d8f8f662243050a6d563e7e0799de31410b268176cb5486999e16786d',
    'artifact_hashes.json': 'cfbe61f09aa1ff952814686e4c487636aa0d15782cece695169aee3b5939786f',
    'requests.jsonl': '0b10ba72c4cd6b02b9776c7bf5f9d3343a331a24be06d8139d05854006274a4a',
}
read_json = remaining.read_json
class_counts = previous_generation.class_counts
validate_source_delta = previous_generation.validate_source_delta
package_manual_preview = previous_generation.package_manual_preview
package_new_preview = previous_generation.package_new_preview


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 input changed')
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 12 or manifest.get('proposals') != 1401 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 12 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 12 required')
    for directory, hashes in ((parent, parent_hashes), (requests, request_hashes)):
        for n, h in read_json(directory/'artifact_hashes.json').items():
            if hashes.get(n) != h:
                raise ValueError('Frozen input 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 user_instruction(requests, parent_new):
    """Record the instruction without manufacturing an original-box review scope."""
    return dict(schema='continuation_user_instruction_v1', date='2026-10-25', statement_zh=USER_STATEMENT,
        mentioned_candidate_count=185, mentioned_manual_count=54,
        mentioned_batches_already_accounted_in_parent_publication=12,
        actual_parent_new_candidate_count=len(parent_new), actual_current_request_count=len(requests),
        observed_current_request_keys=sorted(indexed(requests)),
        observed_parent_candidate_keys=sorted(indexed(parent_new)),
        counts_authority='frozen real current and raw events, not repeated conversational counts',
        future_new_manual_tasks_require_assistant_preannotation=True,
        current_original_rectangle_no_change_authorized=False,
        historical_original_rectangle_statement_bindings_unchanged=True,
        creates_human_event=False, confirms_ground_truth=False, authorizes_propagation=False)


def pending_original_rectangle_save(db, key, row, snapshot_sha, binding, request):
    """Freeze a real unchanged-box save, with no invented review or edit authority."""
    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 or len({r for _, r, _ in history}) != len(history):
        raise ValueError('Pending original box lacks unique current and real events')
    revision, raw = found[0]; value = json.loads(raw)
    flags = remaining.CONFIRMATION_FLAGS + ('full_six_dof',)
    for v, rev in [(value, revision)]+[(json.loads(s), r) for _, r, s in history]:
        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('Pending original-box identity/history/state/flags mismatch')
        polygon_value(v['polygon'], *row['size'])
    event_id, event_revision, event_raw = history[-1]
    if (event_revision != revision or max(r for _, r, _ in history) != revision or event_raw != raw or
            revision <= request['existing_pose_revision']):
        raise ValueError('Pending original box lacks a newer exact raw latest save')
    return dict(schema='pending_original_rectangle_save_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('utf-8')).hexdigest(),
        saved_event_id=event_id, saved_event_revision=event_revision,
        saved_event_payload_raw=event_raw, saved_event_payload=json.loads(event_raw),
        saved_event_raw_sha256=hashlib.sha256(event_raw.encode('utf-8')).hexdigest(),
        history_event_ids=[i for i, _, _ in history], polygon_sha256=sha(value['polygon']),
        manual_request_sha256=sha(request), human_edited=False, pending_excluded_from_coverage=True,
        pending_specific_original_rectangle_review=True, counts_toward_published_coverage=False,
        propagation_seed_eligible=False, source_frame_reserved=True,
        classification_is_candidate_not_truth=True, **dict.fromkeys(FALSE_FLAGS, False))


def completed_manual_proposals(requests, candidates, pending, 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 = []
    if set(pending) & set(candidates):
        raise ValueError('Pending original box cannot be a modified source')
    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 pending:
            value = pending[key]['current_payload']
        elif key in candidates:
            value = candidates[key][0]
            p = remaining.saved_manual_proposal(key, row, candidates[key], pose_sha, formal_sha, classification_sha)
            branch = 'manual_source:' if len(value['polygon']) == 4 else ''
            p.update(generation_schema=VERSION, machine_draft=False,
                manual_source_kind='completed_direct_manual_request',
                rendering_id=GENERATION_NAMESPACE+':'+branch+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)
        else:
            raise ValueError('Request lacks a validated human edit or separately frozen pending box')
        if value['revision'] <= request['existing_pose_revision']:
            raise ValueError('Request has no newer saved revision')
        frames.add(row['frame_id']); keys.add(key)
    return result


def generation_templates(candidates, counted_remaining_keys, gallery, formal_sha, classification_sha):
    templates = {}
    for key in sorted(counted_remaining_keys):
        value, metadata, record = candidates[key]; row = record['source']
        if row['final_class_id'] not in CLASSES:
            raise ValueError('Old five-class human cannot seed remaining continuation')
        if len(value['polygon']) <= 4:
            continue
        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, template in sorted(gallery.items()):
        if template['row']['final_class_id'] not in CLASSES:
            continue
        if key in templates or len(template['polygon']) <= 4 or template['origin'] != original.GALLERY_ORIGIN:
            raise ValueError('Invalid or overlapping direct remaining gallery seed')
        templates[key] = template
    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, reserved_frames=()):
    """Reserve pending original-box frames independently of counted frame locks."""
    work, outcomes = previous_generation.target_inventory(rows, locked, status, pose_protected, gallery_keys, quarantine)
    reserved = set(reserved_frames); active = []
    for key, row in work:
        if row['frame_id'] in reserved:
            outcomes.append(dict(target_key=key, class_id=row['final_class_id'],
                reason='pending_original_rectangle_frame_reserved', 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 audit_pose_snapshot(db, rows, binding):
    """Check all raw relational/formal identities; retain all historical risk states."""
    current = remaining.snapshot_rows(db, 'annotations', rows)
    remaining.snapshot_rows(db, 'events', rows)
    histories = {}; records = []
    for table in ('annotations', 'events'):
        for key, revision, raw in db.execute(f'SELECT target_key,revision,payload FROM {table}'):
            value = json.loads(raw); row = rows[key]
            if (value.get('binding') != binding or
                    any(value.get('source', {}).get(f) != row[f] for f in prior.IDENTITY_FIELDS)):
                raise ValueError('Pose snapshot formal/source binding mismatch: '+key)
            if table == 'events':
                histories.setdefault(key, []).append(revision)
    if any(len(revs) != len(set(revs)) for revs in histories.values()):
        raise ValueError('Duplicate pose historical event revision')
    for key, value in sorted(current.items()):
        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,))]
        # Legacy current rows may have no event; never treat those as new human sources.
        changed_history = any(v.get('polygon') and not remaining.original_rectangle(v['polygon'], rows[key])
            for _, _, _, v in history)
        records.append(dict(target_key=key, current_revision=value['revision'], history_events=len(history),
            current_state=value.get('state'), historical_modified_polygon=changed_history,
            deleted_history=any(v.get('state') == 'deleted' for _, _, _, v in history),
            deferred_history=any(v.get('state') == 'deferred' for _, _, _, v in history),
            confirmed_history=any(remaining.confirmed(v) for _, _, _, v in history),
            returned_to_original_rectangle=bool(changed_history and remaining.original_rectangle(value.get('polygon'), rows[key])),
            current_payload_sha256=sha(value), formal_source_identity_validated=True))
    return current, records


def gallery_source_records(db, rows, previous, parent, formal_sha, snapshot_sha, paths):
    """Validate every actual event binding, not just the selected/latest templates."""
    reviews = remaining.snapshot_rows(db, 'reviews', rows)
    remaining.snapshot_rows(db, 'events', rows)
    publications = original.publication_inventory(paths, formal_sha)
    histories = {}; events = []; audit = []
    for event_id, key, revision, raw in db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'):
        value = json.loads(raw); row = rows[key]; publication = publications.get(value.get('proposal_publication'))
        if publication is None or key not in publication['proposals']:
            raise ValueError('Unknown gallery historical publication/source')
        proposal = publication['proposals'][key]
        image_sha = digest(publication['path']/'images'/f'{key}.png')
        identity = {f: row[f] for f in ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
        identity.update(formal_manifest_sha256=formal_sha, image_sha256=image_sha)
        if (proposal['source'] != row or proposal['class_id'] != row['final_class_id'] or
                proposal['formal_manifest_sha256'] != formal_sha or value.get('source') != identity or
                value.get('source_binding') != sha(identity) or value.get('base_proposal_id') != sha(proposal) or
                value.get('binding') != publication['binding'] or
                value.get('event_kind') != 'human_mask_gallery_revision' or value.get('human_edited') is not True or
                any(value.get(f, False) is not False for f in remaining.CONFIRMATION_FLAGS) or
                value.get('vertex_count') != len(value.get('polygon', []))):
            raise ValueError('Gallery historical source/image/publication/flags mismatch')
        polygon_value(value['polygon'], *row['size'])
        histories.setdefault(key, []).append((event_id, revision, raw)); events.append(value)
        audit.append(dict(target_key=key, event_id=event_id, revision=revision,
            payload_raw_sha256=hashlib.sha256(raw.encode('utf-8')).hexdigest(),
            proposal_publication=value['proposal_publication'], source_binding=value['source_binding'],
            base_proposal_id=value['base_proposal_id'], historical_binding_validated=True))
    sources = []
    for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews ORDER BY target_key'):
        history = histories.get(key, [])
        if (not history or len({r for _, r, _ in history}) != len(history) or
                history[-1][1:] != (revision, raw) or max(r for _, r, _ in history) != revision):
            raise ValueError('Gallery current is not exact raw unique latest event')
        event_id, _, event_raw = history[-1]; value = json.loads(raw)
        sources.append(dict(target_key=key, revision=revision, current_payload_raw=raw,
            current_payload_raw_sha256=hashlib.sha256(raw.encode('utf-8')).hexdigest(), current_payload=value,
            current_payload_sha256=sha(value), human_event_id=event_id, human_event_payload_raw=event_raw,
            human_event_sha256=hashlib.sha256(event_raw.encode('utf-8')).hexdigest(), snapshot_sha256=snapshot_sha))
    templates = original.gallery_templates(previous, reviews, events, {}, rows, parent,
        formal_sha, snapshot_sha, publication_paths=paths)
    return reviews, events, sources, templates, audit


def historical_review_bindings(parent, records):
    """Resolve each old statement by its original binding, without rebinding it."""
    old = read_json(parent/'review_statement_bindings.json'); result = []
    for group in old.values():
        statement_path = parent/group['path']; statement = read_json(statement_path)
        if sha(statement) != group['statement_sha256']:
            raise ValueError('Historical original-box statement hash changed')
        keys = set(group['target_keys'])
        if any(r['user_statement_sha256'] != sha(statement) for r in records if r['target_key'] in keys):
            raise ValueError('Historical original-box source was rebound')
        result.append(dict(path='parent_evidence/'+group['path'], file_sha256=digest(statement_path),
            statement_sha256=sha(statement), target_keys=sorted(keys), statement=statement))
    if set().union(*(set(g['target_keys']) for g in result)) != set(indexed(records)):
        raise ValueError('Historical five-box statement coverage changed')
    return result


def preserve_assets(parent, out, previous):
    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', 'reviewed_original_rectangle_source_records.jsonl',
            'review_completion_statement.json', 'new_review_completion_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 rendering_inventory(parent_inventory, templates, manual, combined, new_keys):
    result = previous_generation.rendering_inventory(parent_inventory, templates, manual, combined, new_keys)
    return dict(result, schema=VERSION, publication_version=PUBLICATION_VERSION,
        generation_template_namespace=GENERATION_NAMESPACE)


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 key in manual_keys:
            label = '【本轮真实人工改绘】已保存四点轮廓，不补造顶点，不作传播模板'
        elif p.get('original_rectangle_review_only'):
            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 key not in new_keys else ''
        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-batch="{"new" if key in new_keys else "previous"}"{hidden}>'
            f'<h2>类别 {p["class_id"]} · {label}</h2><img loading="lazy" src="cases/{key}.png?revision=13" alt="历史快照或本轮来源对照">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>{metrics}{link}'
            f'<a href="labelme/{key}.json?revision=13">多边形（LabelMe）</a> · <a href="masks/{key}.png?revision=13">掩膜</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=13')}*{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>第12版51份新候选均有真实图册保存：{status["polygon_changed_from_proposal"]}份轮廓变化、{status["saved_unchanged"]}份保存后保持原候选；后者不是新增人工改绘。'
        '当前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>')
    origins = Counter(t['origin'] for t in templates.values())
    page += (f'<p>本轮传播模板 {len(templates)} 份 = 剩余类别位姿来源 {origins[prior.POSE_ORIGIN]} + 真实保存图册复核 {origins[original.GALLERY_ORIGIN]}。'
        '旧五类270份图册来源全部核验但不参与剩余类别传播。四点、原框、旧45份对话侧记和42份旧未保存候选不作模板。'
        '类别1、2、3、6、9、10、13、15已达100，禁止继续生成。'
        f'类别19按当前1449个已计入帧和2个保留原图重新匹配，原图容量 {counts[19]["capacity_matching_distinct_frames"]}，'
        f'至少还需新增不同原图 {100-counts[19]["capacity_matching_distinct_frames"]} 份才可能达100，不强行补满。</p>')
    page += (f'<p>本次用户原话：{html.escape(USER_STATEMENT)}。提到的185份与54项已在第12版计入，不重复计数。'
        '本次说明不等于当前两份原框已明确复核无需修改；旧五份原框复核继续绑定原来的两份声明，旧45份侧记原声明也不变。'
        '新图人工任务应先提供助手预标注，再交人工修订；该独立流程不属于严格传播，不计覆盖或模板，直到真实人工保存并经校验。</p>')
    page += '''<p>只用冻结原始attempt_target：同类、同划分、同传感器及兼容已知视角；初始相关度至少0.55、ECC至少0.84、边界相关度至少0.78、逐顶点往返误差不超过1.5并通过横梁边界检查。保持顶点数及顺序，不递归传播、不放宽门槛、不强行填满。1449个已计入帧全部锁定，2个待专项复核原图另行保留；新增使用全局不同帧最大匹配和组别多样性选择。</p>
<p>旧1401条规范记录、图片、掩膜、LabelMe及全部历史命名空间逐字节保留，完整私有父级证据链冻结在parent_evidence，不要求旧私有文件出现在父级根目录。历史卡片不是最新保存图。两库快照、备份、全部历史和20768个正式文件受保护；原始分类分母12344个目标。所有类别均为候选，不确认为GT，不自动训练或控制机器人。不写生产库、配置或服务，不创建新请求或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>只看本轮新增机器候选</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_round3.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_sources, requests = (read_jsonl(p) for p in (
        parent/'proposals.jsonl', parent/'new_proposals.jsonl', parent/'all_manual_sources.jsonl', request_dir/'requests.jsonl'))
    old_pose_keys, parent_new_keys, requested = map(lambda v: set(indexed(v)), (old_sources, parent_new, requests))
    review_sources = read_jsonl(parent/'reviewed_original_rectangle_source_records.jsonl')
    bindings = historical_review_bindings(parent, review_sources)
    if (len(rows) != 12344 or {r['final_class_id'] for r in rows.values()} != set(ALL_CLASSES) or
            len(previous) != 1401 or len(parent_new) != 51 or len(old_pose_keys) != 434 or len(review_sources) != 5 or
            len({p['source']['frame_id'] for p in previous}) != 1401 or
            any(p['source'] != rows[p['target_key']] for p in previous) or
            Counter(r['class_id'] for r in requests) != {4: 10, 5: 10, 7: 10, 14: 10, 22: 10} or
            not set(PENDING_ORIGINAL_RECTANGLES) <= requested):
        raise ValueError('Formal / canonical1401 / prior51 / current50 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),
            ('current_user_instruction', user_instruction(requests, parent_new))):
        write_json(out/(n+'.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)
    delta = dict(pose=validate_source_delta(parent/'reviews_snapshot.sqlite3', pose_snapshot, 'annotations', requested),
        gallery=validate_source_delta(parent/'gallery_reviews_snapshot.sqlite3', gallery_snapshot, 'reviews', parent_new_keys))
    if delta['pose']['new_events'] != 228 or delta['gallery']['new_events'] != 62:
        raise ValueError('Exact real completed-task event delta changed')
    write_json(out/'source_delta_audit.json', delta)
    candidates, pending, 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, pose_history_audit = audit_pose_snapshot(db, rows, sha(binding))
        _, _, eligibility = annotation_inventory(db, rows)
        pose_protected = expand.protected_history(db, rows, current)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        old_source_by_key, request_by_key = indexed(old_sources), indexed(requests)
        for key in sorted(old_pose_keys | requested):
            if key in PENDING_ORIGINAL_RECTANGLES:
                record = pending_original_rectangle_save(db, key, rows[key], pose_sha, sha(binding), request_by_key[key])
                value = record['current_payload']; pending[key] = record
                if (rows[key]['final_class_id'], value['revision']) != PENDING_ORIGINAL_RECTANGLES[key]:
                    raise ValueError('Pending original-box class/revision changed')
            else:
                candidates[key] = remaining.validated_manual_source(db, key, rows[key], pose_sha, sha(binding))
                value, _, record = candidates[key]
            latest = db.execute('SELECT revision,payload FROM events WHERE target_key=? ORDER BY id DESC LIMIT 1', (key,)).fetchone()
            if latest != (value['revision'], record['current_payload_raw']):
                raise ValueError('Manual source current is not exact raw latest event')
            if key in old_pose_keys and record['current_payload_raw'] != old_source_by_key[key]['current_payload_raw']:
                raise ValueError('Inherited modified pose source changed')
            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 pending,
                pending_original_rectangle=key in pending, vertex_count=len(value['polygon']),
                historical_canonical_preserved=key in old_pose_keys,
                current_template_selected=key not in pending and rows[key]['final_class_id'] in CLASSES and len(value['polygon']) > 4,
                snapshot_sha256=pose_sha))
        for record in review_sources:
            key = record['target_key']; group = next(g for g in bindings if key in g['target_keys'])
            statement = group['statement']
            value, _, fresh = previous_generation.exact_original_rectangle_review(db, key, rows[key], pose_sha,
                sha(binding), statement, expected_statement=statement['statement_zh'])
            if (fresh['current_payload_raw'] != record['current_payload_raw'] or
                    fresh['saved_review_event_payload_raw'] != record['saved_review_event_payload_raw'] or
                    fresh['saved_review_event_id'] != record['saved_review_event_id']):
                raise ValueError('Historical original-box current/event evidence changed')
            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=fresh['current_payload_raw_sha256'],
                exact_current_and_event_validated=True, modified_human_contour=False, original_rectangle_review_only=True,
                vertex_count=4, historical_canonical_preserved=True, current_template_selected=False,
                snapshot_sha256=pose_sha, original_statement_path=group['path'],
                original_statement_sha256=record['user_statement_sha256']))
    paths = dict(expand.complete.SOURCE_PUBLICATIONS)
    paths[9] = remaining.PARENT; paths[10] = expand.OUTPUT; paths[11] = previous_generation.PARENT; paths[12] = parent
    publication_hashes = {v: file_inventory(p) for v, p in paths.items() if p != parent}
    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, events, gallery_sources, gallery_templates, gallery_event_audit = gallery_source_records(
            db, rows, previous, parent, formal_sha, gallery_sha, paths)
    gallery_keys = set(reviews) | {v['target_key'] for v in events}
    previous_by_key, gallery_source_by_key = indexed(previous), indexed(gallery_sources)
    for key, template in gallery_templates.items():
        source = gallery_source_by_key[key]; old = current.get(key)
        if old and old.get('state') in ('deleted', 'deferred'):
            raise ValueError('Saved gallery template has a protected deleted/deferred pose')
        template.update(revision=old['revision'] if old else 0, current_payload_sha256=sha(old) if old else None,
            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, pending, rows, previous,
        pose_sha, formal_sha, classification_sha, quarantine, gallery_keys)
    locked = previous+manual; reserved_frames = {rows[k]['frame_id'] for k in pending}
    remaining_keys = {k for k in candidates if rows[k]['final_class_id'] in CLASSES}
    templates = generation_templates(candidates, remaining_keys, gallery_templates, formal_sha, classification_sha)
    all_sources = [candidates[k][2] for k in sorted(candidates)]
    if (len(locked) != 1449 or len({p['source']['frame_id'] for p in locked}) != 1449 or
            len(reserved_frames) != 2 or reserved_frames & {p['source']['frame_id'] for p in locked} or
            len(all_sources) != 482 or len(gallery_templates) != 875 or len(templates) != 650 or
            Counter(t['origin'] for t in templates.values()) != {prior.POSE_ORIGIN: 45, original.GALLERY_ORIGIN: 605} or
            len(manual) != 48 or Counter(p['vertex_count'] for p in manual) != {4: 48} or
            len(current) != 4946 or pose_events != 11926 or len(reviews) != 875 or len(events) != 952):
        raise ValueError('Actual locked1449 / modified482 / pending2 / gallery875 / seed650 contract changed')
    saved_status_records = []
    for p in parent_new:
        key = p['target_key']; review = reviews[key]; source = gallery_source_by_key[key]
        if key not in gallery_templates:
            raise ValueError('Parent candidate lacks validated real >4 saved gallery review')
        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=len(parent_new), actually_saved=len(saved_status_records), pending=0,
        polygon_changed_from_proposal=len(parent_new)-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_save_is_real_review_not_new_human_modification=True,
        new_dispositions_created=0, new_human_edits_created=0, unsaved_candidates_are_seeds=False)
    if status['polygon_changed_from_proposal'] != 36 or status['saved_unchanged'] != 15:
        raise ValueError('Actual saved36-changed /15-unchanged review evidence changed')
    work, outcomes = target_inventory(rows, locked, eligibility, pose_protected, gallery_keys, quarantine, reserved_frames)
    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)
    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),
        reserved_pending_original_rectangle_keys=sorted(pending), reserved_pending_original_rectangle_frames=sorted(reserved_frames),
        pending_original_rectangles_excluded_from_coverage=True, all_current_task_keys_protected=sorted(requested),
        previous_machine_task_keys_protected=sorted(parent_new_keys),
        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(indexed(review_sources)), 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(requested-set(pending))]),
            ('all_manual_sources', all_sources), ('pending_original_rectangle_saves', [pending[k] for k in sorted(pending)]),
            ('human_source_audit', source_audit), ('pose_history_audit', pose_history_audit),
            ('remaining_current_manual_source_records', [candidates[k][2] for k in sorted(remaining_keys)]),
            ('gallery_source_records', gallery_sources), ('gallery_event_binding_audit', gallery_event_audit),
            ('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', 'pending_original_rectangle_saves',
            'remaining_current_manual_source_records', 'gallery_source_records'):
        (out/(n+'.jsonl')).chmod(0o600)
    write_json(out/'review_statement_bindings.json', dict(
        historical_groups=[{k: v for k, v in g.items() if k != 'statement'} for g in bindings],
        no_current_original_rectangle_review_statement_created=True,
        pending_original_rectangle_keys=sorted(pending), current_instruction_path='current_user_instruction.json'))
    write_json(out/'saved_review_status.json', status)
    shutil.copyfile(request_dir/'requests.jsonl', out/'requests.jsonl'); (out/'requests.jsonl').chmod(0o600)
    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 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 image differs from formal native pixels')
    print(json.dumps(dict(state='scanning', workers=workers, locked=len(locked), reserved_frames=len(reserved_frames),
        templates=len(templates), eligible_targets=len(work), saved_human_contours=1357, reviewed_original_rectangle=5,
        pending_original_rectangle=2, class19_capacity=capacity[19], 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')
    if reserved_frames & {p['source']['frame_id'] for p in combined}:
        raise ValueError('Pending original rectangle reserved frame was counted')
    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') != 1357 or total('db_stored_human_review') != 1362 or
            total('user_reviewed_no_change') != 45 or total('no_new_server_review') != 42+len(new)):
        raise ValueError('Saved contour1357 / old original review5 / 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)
    (out/'reviewed_original_rectangle_source_records.jsonl').chmod(0o600)
    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=1401, manual_proposals=48, modified_manual_proposals=48,
        reviewed_original_rectangle=5, new_reviewed_original_rectangle=0, pending_original_rectangle=2, all_manual_sources=482,
        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'],
        additional_new_distinct_images_required_at_least=100-capacity[c],
        manual_template_required=counts[c]['templates'] == 0,
        explanation='Strict original gates; 1449 locked frames plus2 reserved; no quad/original-box propagation; no fake fill.') for c in CLASSES})
    result = dict(state='remaining_round3_pending_independent_verification', schema=VERSION, version=VERSION,
        publication_version=PUBLICATION_VERSION, 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=1401, manual_proposals=48, completed_request_manual_proposals=48, actual_saved_current_requests=50,
        modified_manual_proposals=48, reviewed_original_rectangle=5, new_reviewed_original_rectangle=0,
        inherited_reviewed_original_rectangle=5, reviewed_original_rectangle_schema=REVIEW_SCHEMA,
        pending_original_rectangle=2, pending_original_rectangle_keys=sorted(pending),
        pending_original_rectangles_excluded_from_coverage=True, reserved_pending_original_rectangle_frames=sorted(reserved_frames),
        manual_source_schema=MANUAL_SCHEMA, inherited_manual_sources=434, all_manual_sources=482,
        base_proposals=1449, 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=482,
        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(requested),
        reviewed_original_rectangle_keys=sorted(indexed(review_sources)), new_reviewed_original_rectangle_keys=[],
        manual_four_point_sources=48, manual_more_than_four_sources=0,
        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()),
        class19_additional_new_distinct_images_required_at_least=100-capacity[19],
        formal_manifest_sha256=formal_sha, classification_sha256=classification_sha,
        original_classification_targets=len(rows), original_classification_classes=22,
        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),
        gallery_all_raw_historical_events_source_binding_validated=len(gallery_event_audit),
        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, assistant_prelabels_created=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=1401, 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',
            'current_user_instruction', 'review_statement_bindings', 'source_delta_audit'):
        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=PUBLICATION_VERSION, published=False, independent_verification_performed=False,
        historical_canonical_preserved=1401, locked_before_machines=1449, reserved_uncounted_frames=2,
        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 13 draft — pending independent verification\n\n'
        f'{len(combined)} globally distinct frames = immutable parent1401 + genuine manual48 + strict machine{len(new)}.\n\n'
        'All current50 requests have newer real saves:48 changed quads count;2 original rectangles remain '
        'pending specific review and reserve their frames, never coverage, modified sources or seeds. '
        'pending_original_rectangle_saves.jsonl contains exact raw evidence, without a fabricated no-change statement.\n\n'
        'all_manual_sources.jsonl:482 validated modified pose sources. '
        'reviewed_original_rectangle_source_records.jsonl:five literal inherited lines, unchanged statement bindings. '
        'manual_proposals.jsonl and completed_request_manual_proposals.jsonl:48 genuine changes only. '
        'gallery_source_records.jsonl:875 current/raw latest event pairs; gallery_event_binding_audit.jsonl:all952 events. '
        'saved_review_source_audit.jsonl:actual prior51 candidates,36 changed and15 saved unchanged.\n\n'
        '650 direct >4 remaining seeds = pose45 + gallery605. Old270 five-class gallery sources '
        'are validated but excluded. Filled classes1,2,3,6,9,10,13,15 are not scanned. '
        'Quads, boxes, old45 conversation dispositions and42 old unsaved candidates never seed. '
        'Saved contours1357 and old original-box reviews5 remain separate; stored coverage1362.\n\n'
        'Parent canonical1401, all public assets/namespaces and the complete private parent chain are byte-preserved. '
        'Independent verifier must replay attempts, audit exact evidence and maximum matching before publication. '
        'New manual tasks must be separately assistant-preannotated; this generator creates none. '
        'No ZIP, raw requests, live DB, configuration or service writes.\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', 'pending_original_rectangle', 'missing',
        'registration_attempts', 'accepted_before_diversity', 'code_sha256')}, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
