"""Compose publication 9 from 475 frozen entries and 25 saved human masks.

This is a read-only source audit, not a transfer/generation engine. Only a new
output directory is written; independent verification and deployment are separate.
Conversation dispositions never create SQLite events, human masks or seed rights.
"""
from __future__ import annotations

import argparse
from collections import Counter
from contextlib import closing
import hashlib
import html
import json
from pathlib import Path
import shutil
import zipfile

import numpy as np
from PIL import Image

from . import mask_quota_100 as legacy
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, load_formal_evidence
from .mask_template_continue import sha
from .mask_template_refined import (CLASSES, QUARANTINE, readonly, read_jsonl,
    snapshot_database, verify_history_preserved, write_jsonl)
from .mask_template_transfer import PROJECT, annotation_inventory, write_json
from .pose_review_geometry import canonical

VERSION = 'saved_human_quota100_complete_v1'
PUBLICATION_VERSION = 9
TARGET_PER_CLASS = 100
GENERATION_NAMESPACE = 'quota100_complete_generation'
PARENT = PROJECT/'outputs/pallet_mask_quota_100_continue_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_quota_100_complete_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_quota_100_final_requests_v2'
DISPOSITION_SOURCE = PROJECT/'outputs/pallet_mask_quota_100_continue_deployment_v1/missing_previous_batch_reviews.jsonl'
SOURCE_PUBLICATIONS = dict(prior.SOURCE_PUBLICATIONS)
SOURCE_PUBLICATIONS[8] = PARENT
FALSE_FLAGS = original.FALSE_FLAGS
MANUAL_SCHEMA = original.MANUAL_SCHEMA
read_json, indexed = legacy.read_json, legacy.indexed
DISPOSITION_SOURCE_SHA256 = '934a5bc89c53a878fc47725d05faee580c7b7fffd44e71acee992b3aed10636a'
PARENT_PINS = {
    'manifest.json': 'e59a6656529832e1ab0f246922b5c4bcca80b73895cc59434d2c41808c4aa70a',
    'verification.json': 'd9e04d2f9ea9627d1f7abfcf5215c9d5cb1cc25c5d790fe57d60708513281915',
    'proposals.jsonl': '90885a9979ee092e81b5472b8c388f504b012861e4d5b341d3a01d99f29b13bd',
    'artifact_hashes.json': 'a60fdfea33f52152edefa04688afcd696a0d57eca44c1b3dff31eda0d9d11cba',
    'template_inventory.json': '174807db103b776b3ecd9cd92c32fd44b65fffff5d1a0a6dcad0d6e5b47d911f',
    'all_manual_sources.jsonl': '85abc9f9f8ac53ab57053edb70b8eff96d7b9092b9d98b7b01f74ae60029b703',
    'manual_proposals.jsonl': '35ce84323a630dbd218888759fdf93d1d60a8bb16a3dbe7b8a4a39df9ac98a24',
    'manual_source_records.jsonl': 'af43e05568432384ef28b50f6a18e9773f9093577652d7dab7cde90b5f199967',
    'new_proposals.jsonl': 'dbf94e49f3a12fa6de77155e48890a1721072e2aead268cbb7afa2ca8ec95c29',
}
REQUEST_PINS = {
    'manifest.json': 'ea0e60ccba84fea14ea86a1c4abbe6aea60cfedcfb36335d6f159f5ac4e01529',
    'verification.json': 'ca571f8dfad145e3a368538587669a2f0eec2ad3dedc226594522945f75eb6fc',
    'requests.jsonl': '06b3fc715c70c879378960fe288b3406fa0a3fd093acb33f8268bc471d92629e',
}
HISTORICAL_NAMESPACES = ('templates', 'refined', 'generation', 'quota_generation',
    'quota100_generation', 'quota100_continue_generation')


def verified_parent(parent):
    for name, expected in PARENT_PINS.items():
        if digest(parent/name) != expected:
            raise ValueError('Pinned publication 8 changed: '+name)
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 8 or manifest.get('proposals') != 475 or
            manifest.get('all_manual_sources') != 118 or seal.get('state') != 'passed' or
            seal.get('publication_version') != 8 or seal.get('proposals_verified') != 475 or
            seal.get('all_manual_sources_verified') != 118 or
            seal.get('manifest_sha256') != digest(parent/'manifest.json') or
            seal.get('proposals_sha256') != digest(parent/'proposals.jsonl') or
            seal.get('artifact_hashes_sha256') != digest(parent/'artifact_hashes.json')):
        raise ValueError('Matching independent publication-8 verification required')
    hashes = file_inventory(parent)
    for name, expected in read_json(parent/'artifact_hashes.json').items():
        if hashes.get(name) != expected:
            raise ValueError('Frozen parent artifact changed: '+name)
    frozen = dict(manifest['frozen_code_sha256'])
    if frozen.get('pallet/mask_quota_100_continue.py') != manifest['code_sha256']:
        raise ValueError('Parent generator provenance mismatch')
    for name, expected in frozen.items():
        if digest(PROJECT/name) != expected:
            raise ValueError('Frozen source code changed: '+name)
    frozen['pallet/mask_quota_100_complete.py'] = digest(__file__)
    return manifest, hashes, frozen


def manual_source_proposals(db, requests, pose_templates, current, rows, previous,
                            pose_sha, formal_sha, classification_sha, quarantine=()):
    """Reuse the frozen, exact-event four-point branch without changing its code."""
    proposals, records = prior.manual_source_proposals(db, requests, pose_templates, current,
        rows, previous, pose_sha, formal_sha, classification_sha, quarantine)
    return [dict(p, generation_schema=VERSION,
        rendering_id=GENERATION_NAMESPACE+(':manual_source:' if p['manual_four_point_source'] else ':')+
            p['target_key']) for p in proposals], records


def audit_inherited_manual(db, proposals, pose_sha):
    """Freeze latest raw human evidence, including four points; never rewrite p."""
    indexed(proposals)
    sources, audit = [], []
    for p in sorted(proposals, key=lambda v: v['target_key']):
        value, metadata, record = prior.exact_manual_source(db, p['target_key'], p['source'], pose_sha)
        item = dict(target_key=p['target_key'], original_proposal_sha256=sha(p),
            historical_revision=p['target_expected_revision'], current_revision=value['revision'],
            current_payload_sha256=sha(value), source_changed=sha(value) != p['target_snapshot_payload_sha256'],
            polygon_changed=value['polygon'] != p['polygon'], latest_active_direct_human=True,
            vertex_count=len(value['polygon']), propagation_seed_eligible=len(value['polygon']) > 4,
            historical_assets_preserved=True, canonical_proposal_overwritten=False)
        sources.append(dict(record, inherited_manual_source=True, latest_human_source=metadata,
            legacy_source_audit=item))
        audit.append(item)
    return sources, audit


def review_dispositions(source_rows, publication7_new, previous, saved_reviews, user_statement):
    """Bind only the exact old-45 set; user acknowledgment is not a saved edit."""
    source = indexed(source_rows); old_new = indexed(publication7_new); previous = indexed(previous)
    if not user_statement or not user_statement.strip():
        raise ValueError('Explicit user statement text is required; never invent a quotation')
    if (len(source) != 45 or not set(source) <= set(old_new) or
            not set(source) <= set(previous) or set(source) & set(saved_reviews)):
        raise ValueError('Disposition scope must be exactly the old unsaved 45, not any other candidates')
    result = []
    for key, item in sorted(source.items()):
        p = old_new[key]
        if (p != previous[key] or item['class_id'] != p['class_id'] or
                item['frame_id'] != p['source']['frame_id'] or item['status'] != 'no_server_review_saved'):
            raise ValueError('Old disposition source identity changed')
        result.append(dict(schema='mask_review_disposition_v1', target_key=key,
            class_id=p['class_id'], frame_id=p['source']['frame_id'], source_publication=7,
            source_proposal_id=sha(p), source_proposal_sha256=sha(p), polygon_sha256=sha(p['polygon']),
            disposition='user_reviewed_no_change', human_edited=False, creates_gallery_revision=False,
            human_mask_confirmed=False, seed_authorization=False, automatic_training_eligible=False,
            robot_command_eligible=False, ground_truth_eligible=False,
            origin='explicit_user_statement_in_conversation', user_statement_sha256=hashlib.sha256(
                user_statement.encode()).hexdigest(), source_missing_review_record_sha256=sha(item)))
    return result


def quota_counts(previous, manual, reviews, source_records, dispositions, goal=TARGET_PER_CLASS):
    combined = list(previous)+list(manual); by_key = indexed(combined)
    if (len({p['source']['frame_id'] for p in combined}) != len(combined) or
            any(p['class_id'] not in CLASSES for p in combined)):
        raise ValueError('Quota repeats a frame or has an unexpected class')
    sources, acknowledged = indexed(source_records), indexed(dispositions)
    if not set(sources) <= set(by_key) or not set(acknowledged) <= set(by_key):
        raise ValueError('Evidence outside quota')
    pose_keys = set(sources)
    gallery_keys = {k for k, v in reviews.items() if k in by_key and
        v.get('human_edited') is True and v.get('event_kind') == 'human_mask_gallery_revision' and
        v.get('state') not in ('deleted', 'deferred') and len(v.get('polygon', [])) >= 4}
    saved = pose_keys | gallery_keys
    if set(acknowledged) & saved:
        raise ValueError('Conversation acknowledgment must not duplicate saved-human evidence')
    four = {k for k, v in sources.items() if v.get('manual_four_point_source') is True}
    result = {}
    for cls in CLASSES:
        old = {p['target_key'] for p in previous if p['class_id'] == cls}
        added = {p['target_key'] for p in manual if p['class_id'] == cls}; keys = old | added
        if len(keys) > goal:
            raise ValueError('Quota exceeds goal')
        raw_pending = keys-saved
        result[cls] = dict(old_total=len(old), manual_added=len(added), manual_requested=len(added),
            inherited_manual_sources=len(old & pose_keys), saved_gallery_human=len(keys & gallery_keys),
            saved_pose_human=len(keys & pose_keys), saved_human=len(keys & saved),
            saved_fine_edits=len((keys & saved)-four), manual_four_point_sources=len(keys & four),
            pending_machine_raw=len(raw_pending), old_pending_machine=len(raw_pending),
            user_reviewed_no_change=len(keys & set(acknowledged)),
            no_new_server_review=len(raw_pending-set(acknowledged)), new_candidates=0,
            new_machine_pending=0, total=len(keys), preview=len(keys), distinct_frames=len(keys),
            missing=goal-len(keys), goal=goal, target_per_class=goal)
    return result


def saved_review_status(parent_new, gallery, dispositions, all_saved, previous):
    keys = set(indexed(parent_new)); saved = keys & set(gallery); pending = keys-saved
    acknowledged = set(indexed(dispositions)); raw_pending = set(indexed(previous))-set(all_saved)
    if pending & acknowledged:
        raise ValueError('Old45 statement must not authorize the current missing save')
    return dict(parent_publication=8, parent_new_machine_candidates=len(keys), actually_saved=len(saved),
        pending=len(pending), saved_keys=sorted(saved), pending_keys=sorted(pending),
        current_batch_user_reported_reviewed=True, current_batch_claim_is_not_save_evidence=True,
        older_pending_excluding_acknowledged=sorted(raw_pending-acknowledged-pending),
        no_new_server_review_keys=sorted(raw_pending-acknowledged),
        pending_machine_raw=len(raw_pending), user_reviewed_no_change=len(acknowledged),
        no_new_server_review=len(raw_pending-acknowledged),
        unsaved_candidates_are_seeds=False, user_completion_claim_used_as_saved_mask_evidence=False,
        no_inference_user_did_not_view=True)


def rendering_inventory(parent_inventory, templates, combined, manual):
    inherited = parent_inventory['templates']
    generation = [dict(t, rendering_id=GENERATION_NAMESPACE+':'+key) for key, t in sorted(templates.items())]
    old_sources = parent_inventory.get('manual_source_renderings', [])
    new_sources = [dict(target_key=p['target_key'], rendering_id=p['rendering_id'], polygon=p['polygon'],
        human_source=p['human_source'], manual_four_point_source=True, propagation_seed_eligible=False)
        for p in manual if p.get('manual_four_point_source')]
    sources = old_sources+new_sources
    ids = [t['rendering_id'] for t in inherited+generation]+[s['rendering_id'] for s in sources]
    if len(ids) != len(set(ids)):
        raise ValueError('Rendering namespace collision')
    added = indexed(manual)
    mapping = {p['target_key']: (added[p['target_key']]['rendering_id'] if p['target_key'] in added else
        parent_inventory['rendering_by_target'][p['target_key']]) for p in combined}
    if not set(mapping.values()) <= set(ids):
        raise ValueError('Rendering source missing')
    return dict(templates=inherited+generation, rendering_by_target=mapping,
        manual_source_renderings=sources, generation_template_namespace=GENERATION_NAMESPACE,
        generation_template_keys=sorted(templates), historical_templates=len(inherited),
        deferred_manual_template_keys=sorted(s['target_key'] for s in sources),
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())))


def preserve_parent_assets(parent, out, previous):
    hashes = {}
    for p in previous:
        for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            name = f'{directory}/{p["target_key"]}.{extension}'
            if (out/name).exists():
                raise ValueError('Historical asset collision')
            shutil.copyfile(parent/name, out/name); hashes[name] = digest(parent/name)
    for directory in HISTORICAL_NAMESPACES:
        shutil.copytree(parent/directory, out/directory)
        for path in sorted((parent/directory).rglob('*')):
            if path.is_file():
                hashes[str(path.relative_to(parent))] = digest(path)
    for name in ('vertex2_before_after.png', 'FONT_LICENSE.txt'):
        if (parent/name).exists():
            shutil.copyfile(parent/name, out/name); hashes[name] = digest(parent/name)
    if any(digest(out/name) != expected for name, expected in hashes.items()):
        raise ValueError('Historical asset copy mismatch')
    return hashes


def build_report(out, combined, counts, reviews, current, inherited_manual, manual, dispositions, review_status):
    added = set(indexed(manual)); all_manual = added | set(indexed(inherited_manual))
    acknowledged = set(indexed(dispositions)); missing_save = set(review_status['pending_keys'])
    total = lambda field: sum(c[field] for c in counts.values())
    fields = ('old_total', 'manual_added', 'saved_gallery_human', 'saved_pose_human', 'saved_human',
        'user_reviewed_no_change', 'no_new_server_review', 'total', 'missing')
    table = ''.join(f'<tr><td>{cls}</td>'+''.join(f'<td>{c[f]}</td>' for f in fields)+'</tr>'
        for cls, c in counts.items())
    cards = []
    for p in sorted(combined, key=lambda v: (v['class_id'], v['target_key'])):
        key = p['target_key']; is_manual = key in all_manual
        if key in added:
            label = f'【本轮计入已保存人工轮廓 r{p["target_expected_revision"]}】不是机器候选，未确认真值'
            if p.get('manual_four_point_source'):
                label += '；真实四点，仅计覆盖、不作传播模板'
        elif key in reviews:
            label = f'【已保存图册人工修订 r{reviews[key]["revision"]}】下图保留历史候选，不是最新修订渲染'
        elif is_manual:
            label = f'【既有原位姿人工来源 r{current[key]["revision"]}】下图保留历史快照，未确认真值'
        elif key in acknowledged:
            label = '【用户已复核，无需修改】旧45份无需重复复核；对话侧记，不是数据库保存'
        elif key in missing_save:
            label = '【本轮66份中缺少保存的1份】用户报告已查看，服务器无新修订；保留候选原样'
        else:
            label = '【较早机器候选，未见服务器新复核修订】不据此断言用户没有查看'
        link = (f'<p><a href="{html.escape(p.get("original_pose_url") or "", quote=True)}">打开原位姿人工标注</a>；'
            '图册修订与原位姿人工来源独立保留。</p>' if is_manual else '')
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" '
            f'data-batch="{"new" if key in added else "previous"}" data-kind="{"manual" if is_manual else "machine"}">'
            f'<h2>类别 {p["class_id"]} · {label}</h2><img loading="lazy" src="cases/{key}.png?revision=9" '
            f'alt="{"真实人工来源快照" if is_manual else "历史模板候选对照"}">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>{link}'
            f'<a href="labelme/{key}.json?revision=9">多边形（LabelMe）</a> · '
            f'<a href="masks/{key}.png?revision=9">掩膜</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=9')}*{box-sizing:border-box}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1800px}a{color:#77ceff}img{width:100%;height:auto}article{border:1px solid #425269;margin:20px 0;padding:12px;min-width:0}p{overflow-wrap:anywhere}.table-wrap{overflow-x:auto}td,th{padding:9px;border:1px solid #425269;white-space:nowrap}table{border-collapse:collapse;min-width:1100px;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>
<p>覆盖达到500帧，不等于500份已保存人工轮廓，更不等于500份已确认真值。只整理475份历史记录和25份实际已保存人工来源；本轮新增机器候选为0，不运行迁移或配准，不补造顶点。</p>
<p>旧45份已逐项绑定此前84份中的原始候选，用户明确表示无需修改：无需重复复核。侧记仅表示用户已复核且无需修改，不创建图册修订，不伪造人工编辑，不确认真值、不授权为传播种子。其余没有服务器新修订的候选不据此断言用户没有查看。</p>
<p>历史475份对象及六个历史资源命名空间逐字节保留；历史图片不是最新人工修订渲染。最新实际人工多边形、原始事件和修订另行冻结，绝不覆盖历史候选。两个真实四点人工来源保持四点，计入人工覆盖，不作传播或模板渲染种子。</p>
<p>全部轮廓仍为草稿，未确认真值，不自动进入训练或机器人控制。原位姿、独立图册、正式数据与原划分均不改写。</p>
'''
    page += (f'<p>累计 {total("total")} 帧，缺额 {total("missing")}；已保存人工轮廓 {total("saved_human")} 份 '
        f'= 图册 {total("saved_gallery_human")} + 原位姿 {total("saved_pose_human")}（按目标去重）。'
        f'本轮计入人工 {total("manual_added")} 份；四点人工来源合计 {total("manual_four_point_sources")} 份。'
        f'未具备已保存人工轮廓的历史机器候选原始数 {total("pending_machine_raw")} 份，其中 '
        f'{total("user_reviewed_no_change")} 份为用户已复核无需修改，另 {total("no_new_server_review")} 份未见新服务器复核修订。</p>'
        f'<p>上一版人工覆盖321份 + 本轮图册实际保存 {review_status["actually_saved"]}/'
        f'{review_status["parent_new_machine_candidates"]} 份 + 本轮人工描绘25份 = {total("saved_human")} 份。'
        f'本轮66份虽报告均已查看，仍有 {review_status["pending"]} 份没有服务器保存；不自动改写或补记事件。</p>')
    if review_status['pending_keys']:
        page += '<p>本轮未保存：'+', '.join(f'<a href="#target-{key}">{key}</a>'
            for key in review_status['pending_keys'])+'。</p>'
    page += '<div class="table-wrap"><table><tr><th>类别</th><th>历史总数</th><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="">全部</option>'+''.join(
        f'<option>{cls}</option>' for cls in CLASSES)+'</select> '
    page += '<label><input type="checkbox" id="only-new">只看本轮计入人工标签</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 coverage failure')
        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 font coverage failure')
    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 package_public_zip(out, combined):
    """Explicit public allowlist: no SQLite, backups, raw events, config or keys."""
    names = ['report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt', 'preview_all_classes.png',
        'proposals.jsonl', 'new_proposals.jsonl', 'manual_proposals.jsonl', 'review_dispositions.jsonl']
    names += [f'{directory}/{p["target_key"]}.{extension}' for p in combined
        for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json'))]
    if (out/'vertex2_before_after.png').exists():
        names.append('vertex2_before_after.png')
    expected = {name: digest(out/name) for name in sorted(names)}
    path = out/'pallet_mask_refined_review.zip'
    with zipfile.ZipFile(path, 'x', compression=zipfile.ZIP_DEFLATED) as archive:
        for name in expected:
            archive.write(out/name, name)
    with zipfile.ZipFile(path) as archive:
        if set(archive.namelist()) != set(expected) or archive.testzip() is not None or any(
                hashlib.sha256(archive.read(name)).hexdigest() != value for name, value in expected.items()):
            raise ValueError('Public archive differs from allowlist')
    write_json(out/'public_archive_inventory.json', dict(files=expected, archive=path.name,
        archive_sha256=digest(path), private_files_included=False, sqlite_included=False))
    return expected


def compose(output=OUTPUT, parent=PARENT, requests_directory=REQUESTS, *, user_statement,
            statement_is_verbatim=True, statement_source=None):
    out, parent, request_dir = Path(output).resolve(), Path(parent).resolve(), Path(requests_directory).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory')
    if not user_statement or not user_statement.strip():
        raise ValueError('Explicit user statement text is required')
    statement_path = Path(statement_source).resolve() if statement_source is not None else None
    statement_input_sha = digest(statement_path) if statement_path else None
    manifest, parent_hashes, frozen_code = verified_parent(parent)
    configuration_hashes, request_hashes = file_inventory(PROJECT/'configs'), file_inventory(request_dir)
    if any(request_hashes.get(name) != expected for name, expected in REQUEST_PINS.items()):
        raise ValueError('Pinned 25-item manual request queue changed')
    request_manifest, request_seal = read_json(request_dir/'manifest.json'), read_json(request_dir/'verification.json')
    requests = read_jsonl(request_dir/'requests.jsonl'); indexed(requests)
    if (len(requests) != 25 or Counter(r['class_id'] for r in requests) != {9: 10, 10: 15} or
            request_manifest.get('request_count') != 25 or request_seal.get('state') != 'passed' or
            request_seal.get('requests_sha256') != request_hashes['requests.jsonl']):
        raise ValueError('Expected verified 25-item request queue')
    active = read_json(PROJECT/'configs/active_mask_template_requests.json')
    if (PROJECT/active['directory']).resolve() != request_dir:
        raise ValueError('Manual request queue is no longer active')
    formal, rows, evidence = load_formal_evidence(manifest)
    formal_sha, classification_sha = manifest['formal_manifest_sha256'], manifest['classification_sha256']
    paths = dict(SOURCE_PUBLICATIONS); paths[8] = parent
    publications = original.publication_inventory(paths, formal_sha)
    source_hashes = {v: file_inventory(path) for v, path in paths.items() if path != parent}
    protected = protected_snapshot(formal)
    if (len(protected) != 20768 or protected != read_json(parent/'protected_formal_before.json') or
            protected != read_json(parent/'protected_formal_after.json')):
        raise ValueError('Protected 20768 formal files differ from parent')
    previous = read_jsonl(parent/'proposals.jsonl'); previous_keys = set(indexed(previous))
    inherited_manual = [p for p in previous if p.get('schema') == MANUAL_SCHEMA]
    inherited_keys = set(indexed(inherited_manual))
    if (len(previous) != 475 or len(inherited_manual) != 118 or
            Counter(p['class_id'] for p in previous) != {1: 100, 2: 100, 9: 90, 10: 85, 15: 100} or
            len({p['source']['frame_id'] for p in previous}) != 475 or
            inherited_keys != set(indexed(read_jsonl(parent/'all_manual_sources.jsonl'))) or
            any(p['source'] != rows[p['target_key']] or p['formal_manifest_sha256'] != formal_sha for p in previous)):
        raise ValueError('Parent canonical/source inventory changed')
    disposition_input_hash = digest(DISPOSITION_SOURCE)
    disposition_input = read_jsonl(DISPOSITION_SOURCE)
    if (disposition_input_hash != DISPOSITION_SOURCE_SHA256 or
            set(indexed(disposition_input)) != set(manifest['saved_review_status']['pending_keys'])):
        raise ValueError('Exact previous saved-report old45 disposition scope changed')
    out.mkdir(parents=True)
    for directory in ('cases', 'images', 'masks', 'labelme', 'parent_evidence'):
        (out/directory).mkdir()
    (out/GENERATION_NAMESPACE/'templates').mkdir(parents=True)
    for name, value in (('protected_formal_before', protected), ('parent_file_inventory', parent_hashes),
            ('configuration_input_hashes', configuration_hashes), ('request_file_inventory', request_hashes),
            ('source_publication_file_inventories', source_hashes)):
        write_json(out/(name+'.json'), value)
    for name in ('manifest.json', 'verification.json', 'proposals.jsonl', 'manual_proposals.jsonl',
            'manual_source_records.jsonl', 'all_manual_sources.jsonl', 'new_proposals.jsonl', 'template_inventory.json'):
        shutil.copyfile(parent/name, out/'parent_evidence'/name)
    for name in frozen_code:
        destination = out/'frozen_code'/name; destination.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(PROJECT/name, destination)
    shutil.copyfile(request_dir/'requests.jsonl', out/'requests.jsonl')
    shutil.copyfile(DISPOSITION_SOURCE, out/'disposition_scope_source.jsonl')
    if statement_path:
        shutil.copyfile(statement_path, out/'conversation_statement_input.txt')
    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, gallery_sha = snapshot_database(pose_live, pose_snapshot), snapshot_database(gallery_live, gallery_snapshot)
    with closing(readonly(pose_snapshot, True)) as db:
        current, pose_templates, eligibility = annotation_inventory(db, rows)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        if json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])['formal_manifest_sha256'] != formal_sha:
            raise ValueError('Pose formal binding mismatch')
        for table in ('annotations', 'events'):
            legacy.validate_snapshot_rows(db, rows, table)
        inherited_sources, inherited_audit = audit_inherited_manual(db, inherited_manual, pose_sha)
        quarantine = QUARANTINE | set(manifest.get('preserved_quarantined_targets', []))
        manual, manual_sources = manual_source_proposals(db, requests, pose_templates, current, rows,
            previous, pose_sha, formal_sha, classification_sha, quarantine)
    with closing(readonly(gallery_snapshot, True)) as db:
        if db.execute("SELECT value FROM metadata WHERE key='formal_manifest_sha256'").fetchone()[0] != formal_sha:
            raise ValueError('Gallery formal binding mismatch')
        reviews = {key: json.loads(raw) for key, raw in db.execute('SELECT target_key,payload FROM reviews')}
        gallery_events = [json.loads(raw) for raw, in db.execute('SELECT payload FROM events ORDER BY id')]
        for table in ('reviews', 'events'):
            legacy.validate_snapshot_rows(db, rows, table)
        gallery_sources = []
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews ORDER BY target_key'):
            event_id, event_raw = db.execute('SELECT id,payload FROM events WHERE target_key=? AND revision=?',
                (key, revision)).fetchone()
            if json.loads(event_raw) != json.loads(raw):
                raise ValueError('Gallery current source/event mismatch')
            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), human_event_id=event_id, human_event_payload_raw=event_raw,
                human_event_sha256=hashlib.sha256(event_raw.encode()).hexdigest(), snapshot_sha256=gallery_sha))
    gallery = original.gallery_templates(previous, reviews, gallery_events, current, rows,
        parent, formal_sha, gallery_sha, publication_paths=paths)
    templates = original.seed_union(pose_templates, gallery, pose_sha, quarantine)
    dispositions = review_dispositions(disposition_input, read_jsonl(paths[7]/'new_proposals.jsonl'),
        previous, reviews, user_statement)
    all_sources = sorted(inherited_sources+manual_sources, key=lambda p: p['target_key'])
    all_keys = set(indexed(all_sources)); manual_keys = set(indexed(manual))
    combined = sorted(previous+manual, key=lambda p: p['target_key'])
    four_keys = sorted(p['target_key'] for p in all_sources if p['manual_four_point_source'])
    if (len(manual) != 25 or len(all_sources) != 143 or all_keys != inherited_keys | manual_keys or
            set(four_keys) & set(templates)):
        raise ValueError('Exact human-source inventory mismatch or four-point source entered seed pool')
    counts = quota_counts(previous, manual, reviews, all_sources, dispositions)
    if len(combined) != 500 or any(c['total'] != 100 or c['missing'] != 0 for c in counts.values()):
        raise ValueError('Human additions did not exactly complete the 100-per-class quota')
    saved_keys = all_keys | (set(reviews) & set(indexed(combined)))
    review_status = saved_review_status(read_jsonl(parent/'new_proposals.jsonl'), reviews,
        dispositions, saved_keys, combined)
    if (set(indexed(dispositions)) | set(review_status['pending_keys'])) & set(templates):
        raise ValueError('Conversation-only acknowledgment or current missing save became a seed')
    for name, values in (('proposals', combined), ('manual_proposals', manual), ('manual_source_records', manual_sources),
            ('all_manual_sources', all_sources), ('inherited_manual_source_audit', inherited_audit),
            ('refined_annotations', [reviews[key] for key in sorted(reviews)]),
            ('gallery_source_records', gallery_sources), ('review_dispositions', dispositions),
            ('new_proposals', []), ('registration_attempts', []), ('accepted_candidates', [])):
        write_jsonl(out/(name+'.jsonl'), values)
    restored = indexed(read_jsonl(out/'proposals.jsonl'))
    if any(canonical(restored[key]) != canonical(p) for key, p in indexed(previous).items()):
        raise ValueError('Historical canonical proposal changed')
    statement_metadata = dict(schema='explicit_conversation_review_statement_v1',
        user_statement=user_statement, user_statement_is_verbatim=statement_is_verbatim,
        user_statement_sha256=hashlib.sha256(user_statement.encode()).hexdigest(),
        statement_source=str(statement_path) if statement_path else None,
        statement_input_sha256=statement_input_sha,
        origin='explicit_user_statement_in_conversation', scope_count=45,
        scope_source=str(DISPOSITION_SOURCE.relative_to(PROJECT)), scope_source_sha256=disposition_input_hash,
        scope_target_keys=sorted(indexed(dispositions)), source_publication=7, source_batch_size=84,
        statement_summary_zh='用户说明：旧45份若属于此前84份，则均无需修改。只读核验确认这45份全部属于此前84份。',
        summary_is_not_verbatim_quotation=True, no_other_targets_authorized=True,
        creates_gallery_revision=False, human_edited=False, human_mask_confirmed=False, seed_authorization=False,
        production_database_writes=0)
    write_json(out/'review_disposition_statement.json', statement_metadata)
    write_json(out/'saved_review_status.json', review_status)
    inventory = rendering_inventory(read_json(parent/'template_inventory.json'), templates, combined, manual)
    inventory.update(schema=VERSION, publication_version=PUBLICATION_VERSION, target_per_class=TARGET_PER_CLASS,
        classes=counts, eligibility=eligibility, snapshot_saved=len(current), snapshot_events=pose_events,
        generation_templates=len(templates), gallery_snapshot_saved=len(reviews), previous_proposals=len(previous),
        manual_proposals=len(manual), inherited_manual_sources=len(inherited_manual), all_manual_sources=len(all_sources),
        counted_manual_source_keys=sorted(all_keys), saved_review_status=review_status,
        template_inventory_is_source_audit_only=True, machine_transfer_performed=False)
    write_json(out/'template_inventory.json', inventory)
    write_json(out/'snapshot_manifest.json', dict(formal_manifest_sha256=formal_sha,
        pose=dict(path=pose_snapshot.name, backup='backups/'+pose_snapshot.name, sha256=pose_sha,
            saved=len(current), events=pose_events),
        gallery=dict(path=gallery_snapshot.name, backup='backups/'+gallery_snapshot.name, sha256=gallery_sha,
            saved=len(reviews), events=len(gallery_events)),
        manual_requests=dict(directory=str(request_dir), count=len(requests), frozen_input_sha256=request_hashes),
        source_publications={v: {k: str(value) if k == 'path' else value for k, value in pub.items()
            if k != 'proposals'} for v, pub in publications.items()}))
    write_json(out/'protected_targets.json', dict(previous=sorted(previous_keys),
        counted_manual_source_keys=sorted(all_keys), newly_counted_manual_source_keys=sorted(manual_keys),
        requested_manual_keys=sorted(manual_keys), deferred_manual_template_keys=four_keys,
        quarantine=sorted(quarantine), gallery_saved_or_history=sorted(set(reviews) | {e['target_key'] for e in gallery_events}),
        pose_mask_history_or_noneligible=sorted(k for k, reason in eligibility.items() if reason != 'eligible'),
        eligibility=eligibility, conversation_dispositions_not_seed_authorization=True))
    for request in requests:
        evidence.image(rows[request['target_key']]['frame_id'])
        if request['source']['image_sha256'] != evidence.used[request['source']['image']]:
            raise ValueError('Manual request formal image changed')
    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('Parent image differs from formal pixels')
    prior.package_manual_preview(out, manual, evidence)
    copied = preserve_parent_assets(parent, out, previous)
    write_json(out/'preserved_parent_assets.json', copied)
    for key, template in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', template)
    build_report(out, combined, counts, reviews, current, inherited_manual, manual, dispositions, review_status)
    contact = Image.new('RGB', (1480, len(CLASSES)*305), '#121923')
    for i, cls in enumerate(CLASSES):
        p = next(p for p in 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 name, value in evidence.used.items():
        if name in used and used[name] != value:
            raise ValueError('Parent source image hash conflict')
        used[name] = value
    if any(digest(formal/name) != value for name, value in used.items()):
        raise ValueError('Used formal image changed')
    write_json(out/'used_image_hashes.json', used)
    archive_files = package_public_zip(out, combined)
    after = protected_snapshot(formal); write_json(out/'protected_formal_after.json', after)
    if after != protected:
        raise ValueError('Protected formal files changed')
    verify_history_preserved(pose_live, pose_snapshot); verify_history_preserved(gallery_live, gallery_snapshot)
    try:
        prior.verify_sources_current(pose_live, pose_snapshot, 'annotations')
        prior.verify_sources_current(gallery_live, gallery_snapshot, 'reviews')
    except ValueError as exc:
        write_json(out/'source_stability_check.json', dict(state='changed_requires_fresh_composition',
            reason=str(exc), published=False, production_database_writes=0))
        raise
    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))):
        raise ValueError('Frozen snapshot/backup changed')
    if (file_inventory(parent) != parent_hashes or file_inventory(PROJECT/'configs') != configuration_hashes or
            file_inventory(request_dir) != request_hashes or digest(DISPOSITION_SOURCE) != disposition_input_hash or
            (statement_path is not None and digest(statement_path) != statement_input_sha) or
            any(file_inventory(paths[v]) != values for v, values in source_hashes.items()) or
            any(digest(PROJECT/name) != value for name, value in frozen_code.items()) or
            any(digest(out/name) != value for name, value in copied.items())):
        raise ValueError('Frozen input/code/config/publication/assets changed')
    write_json(out/'source_stability_check.json', dict(state='stable', source_current_stable=True,
        current_payloads_and_events_unchanged=True, production_database_writes=0))
    total = lambda field: sum(c[field] for c in counts.values())
    write_json(out/'shortfalls.json', {cls: dict(missing=0,
        explanation='Exactly 100 distinct frames from unchanged parent entries plus actual saved manual sources. '
            'No machine generation, transfer, synthetic points, confirmation or conversation-as-save evidence.')
        for cls in CLASSES})
    result = dict(state='quota_complete_pending_independent_verification', schema=VERSION, version=VERSION,
        publication_version=PUBLICATION_VERSION, target_per_class=100, target_total=500, 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=len(previous),
        proposals=len(combined), new_proposals=0, new_by_class={}, manual_proposals=len(manual),
        manual_added=len(manual), manual_requested=len(manual), manual_existing_reused=0,
        inherited_manual_sources=len(inherited_manual), all_manual_sources=len(all_sources),
        manual_source_schema=MANUAL_SCHEMA, manual_by_class=dict(Counter(p['class_id'] for p in manual)),
        counted_manual_source_keys=sorted(all_keys), newly_counted_manual_source_keys=sorted(manual_keys),
        requested_manual_keys=sorted(manual_keys), classes=counts, missing=0,
        saved_gallery_human=total('saved_gallery_human'), saved_pose_human=total('saved_pose_human'),
        saved_human=total('saved_human'), pending_machine_raw=total('pending_machine_raw'),
        old_pending_machine=total('pending_machine_raw'), user_reviewed_no_change=len(dispositions),
        no_new_server_review=total('no_new_server_review'),
        older_no_new_server_review=len(review_status['older_pending_excluding_acknowledged']),
        current_batch_missing_gallery_saves=review_status['pending'], saved_review_status=review_status,
        review_dispositions_are_saved_human_masks=False, review_dispositions_seed_authorization=False,
        review_disposition_statement_sha256=digest(out/'review_disposition_statement.json'),
        manual_four_point_sources=len(four_keys), deferred_manual_template_keys=four_keys,
        inherited_manual_source_revisions_changed=[v['target_key'] for v in inherited_audit if v['source_changed']],
        manual_source_snapshots_imported_into_gallery_store=False, 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(gallery_events), generation_templates=len(templates),
        generation_template_namespace=GENERATION_NAMESPACE, template_inventory_is_source_audit_only=True,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        method='saved_human_composition_only', scanned_eligible_targets=0, registration_attempts=0,
        accepted_before_diversity=0, machine_transfer_performed=False, new_machine_pending=0,
        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'], source_current_stable=True,
        preserved_quarantined_targets=sorted(quarantine), protected_formal_files=len(protected),
        original_splits_preserved=True, cross_split_template_transfers=0, recursive_candidate_propagation=False,
        thresholds_relaxed=False, forced_fill=False, research_candidates_added=0, new_human_edits_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, archive_sha256=digest(out/'pallet_mask_refined_review.zip'),
        archive_public_files=len(archive_files), archive_private_files=0)
    for name in ('proposals', 'new_proposals', 'manual_proposals', 'manual_source_records', 'all_manual_sources',
            'inherited_manual_source_audit', 'requests', 'refined_annotations', 'gallery_source_records',
            'review_dispositions', 'registration_attempts', 'accepted_candidates'):
        result[name+'_sha256'] = digest(out/(name+'.jsonl'))
    result['template_inventory_sha256'] = digest(out/'template_inventory.json')
    write_json(out/'manifest.json', result)
    write_json(out/'generation_validation.json', dict(state='self_checks_passed_not_independent_verification',
        publication_version=9, old_canonical_preserved=len(previous), old_assets_bytewise_preserved=len(copied),
        inherited_manual_sources=118, newly_counted_manual_sources=25, all_manual_sources=143,
        independent_verification_performed=False, published=False, live_database_writes=0,
        configuration_writes=0, service_writes=0, protected_formal_files=len(protected)))
    (out/'GENERATION_HANDOFF.md').write_text(
        '# Publication 9 — pending independent verification\n\n'
        '500 distinct frames, exactly 100 per class = byte-preserved parent 475 + actual saved human 25. '
        'No new machine proposals or registration attempts. No source/config/service writes.\n\n'
        f'Saved human coverage {result["saved_human"]} = gallery {result["saved_gallery_human"]} + '
        f'original pose {result["saved_pose_human"]}, key-deduplicated. Inherited 118 canonical manual '
        'proposals selected from the complete parent inventory; all 143 exact current and actual human '
        'event raw payloads are frozen separately. Canonical historical proposals/assets are never refreshed.\n\n'
        f'Four-point actual sources {four_keys} stay four points, `human_source` not `template`; '
        'both are excluded from all new propagation templates. Six historical namespaces preserved.\n\n'
        f'Current 66 machine entries: actual gallery saves {review_status["actually_saved"]}/66; '
        f'missing {review_status["pending_keys"]}. User review claim is not converted into a saved edit. '
        f'Raw machine-without-saved-human count {result["pending_machine_raw"]}; exact old45 user '
        'no-change dispositions are separate, require no repeat review, and create no SQLite events, '
        f'GT confirmation or seed authorization. Other no-new-server-review count {result["no_new_server_review"]} '
        f'= older {result["older_no_new_server_review"]} + current {review_status["pending"]}; '
        'this does not imply the user did not look.\n\n'
        'Snapshots and backups are immutable, all 20768 formal files unchanged. Every latest gallery '
        'binding validated against publications 3–8. No transfers, relaxed gates, recursive seeds, '
        'training, robot control or GT eligibility. Public ZIP uses an explicit allowlist, no raw '
        'SQLite/events/backups/config/keys. Verification and publication remain separate.\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('--user-statement-file', type=Path, required=True,
        help='Exact user statement; no fabricated Chinese quotation')
    parser.add_argument('--statement-is-delegated-summary', action='store_true',
        help='Explicitly mark non-verbatim delegated context when original quotation is unavailable')
    args = parser.parse_args()
    text = args.user_statement_file.read_text()
    try:
        value = json.loads(text)
    except json.JSONDecodeError:
        value = None
    statement = value['user_statement'] if isinstance(value, dict) else text
    result = compose(args.output, user_statement=statement,
        statement_is_verbatim=not args.statement_is_delegated_summary,
        statement_source=args.user_statement_file)
    print(json.dumps(result, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
