"""Publication 6: direct human seed union and guarded, distinct-frame quotas.

Generation writes a fresh directory only. Run after diagnostic research approval;
the resulting draft still requires independent verification before publication.
Only the explicitly requested, saved manual pose masks join the gallery quota.
Other original pose polygons are seeds, never implicit quota additions.
"""
from __future__ import annotations

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

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

from .cluster_targets import digest, load_dataset, protected_snapshot, target_key
from .mask_gallery_review import VERSION as EDITOR_VERSION
from .mask_template_continue import attempt_target, sha
from .mask_template_refined import (CLASSES, QUARANTINE, readonly, read_jsonl,
    select_diverse, snapshot_database, verify_history_preserved, write_jsonl)
from .mask_template_transfer import (PROJECT, ImageEvidence, annotation_inventory,
    compatible, overlay, package_preview, write_json)
from .pose_review_geometry import canonical, polygon_value, raster_mask

VERSION = 'direct_human_union_quota_v1'
PUBLICATION_VERSION = 6
GENERATION_NAMESPACE = 'quota_generation'
PARENT = PROJECT/'outputs/pallet_mask_template_refined_v3'
OUTPUT = PROJECT/'outputs/pallet_mask_quota_completion_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_template_requests_v1'
MANUAL_SCHEMA = 'saved_pose_mask_gallery_entry_v1'
SOURCE_PUBLICATIONS = {
    3: PROJECT/'outputs/pallet_mask_template_bulk_v5',
    4: PROJECT/'outputs/pallet_mask_template_refined_v2',
    5: PARENT,
}
POSE_ORIGIN = 'original_pose_human_revision'
GALLERY_ORIGIN = 'mask_gallery_human_revision'
FALSE_FLAGS = ('human_mask_confirmed', 'human_pose_confirmed',
    'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible')


def publication_inventory(paths, formal_sha):
    """Resolve actual historical editor bindings, not the latest gallery binding."""
    result = {}
    for version, directory in sorted(paths.items()):
        directory = Path(directory)
        manifest = json.loads((directory/'manifest.json').read_text())
        verification = json.loads((directory/'verification.json').read_text())
        proposals_sha = digest(directory/'proposals.jsonl')
        if (manifest['publication_version'] != version or
                manifest['formal_manifest_sha256'] != formal_sha or
                manifest['proposals_sha256'] != proposals_sha or
                verification['state'] != 'passed' or
                verification['proposals_sha256'] != proposals_sha):
            raise ValueError('Historical publication identity/verification mismatch')
        proposals = read_jsonl(directory/'proposals.jsonl')
        by_key = {p['target_key']: p for p in proposals}
        if len(by_key) != len(proposals):
            raise ValueError('Historical publication has duplicate proposals')
        result[version] = dict(path=directory, proposals=by_key,
            binding=sha(dict(formal=formal_sha, proposals=proposals_sha,
                editor_version=EDITOR_VERSION)), proposals_sha256=proposals_sha,
            manifest_sha256=digest(directory/'manifest.json'),
            verification_sha256=digest(directory/'verification.json'))
    return result


def gallery_templates(previous, reviews, events, current, rows, parent, formal_sha,
                      snapshot_sha, publication_paths=None):
    """Validate every latest saved edit; only >4-vertex human edits become seeds.

    Saved reviews may be a strict subset of the parent. Event-only keys remain
    protected by the caller but cannot silently resurrect a deleted current edit.
    """
    publications = publication_inventory(
        SOURCE_PUBLICATIONS if publication_paths is None else publication_paths, formal_sha)
    previous_by_key = {p['target_key']: p for p in previous}
    if len(previous_by_key) != len(previous):
        raise ValueError('Duplicate parent proposal')
    histories = defaultdict(list)
    for event in events:
        if event['target_key'] not in rows:
            raise ValueError('Unknown gallery history target')
        histories[event['target_key']].append(event)
    for history in histories.values():
        if len({v['revision'] for v in history}) != len(history):
            raise ValueError('Duplicate gallery event revision')
    templates = {}
    for key, review in sorted(reviews.items()):
        if key not in rows or review['target_key'] != key:
            raise ValueError('Gallery target identity mismatch')
        history = histories[key]
        if not history or max(history, key=lambda v: v['revision']) != review:
            raise ValueError('Gallery current record is not its unique latest event')
        publication = publications.get(review.get('proposal_publication'))
        if publication is None:
            raise ValueError('Unknown gallery source publication')
        p = publication['proposals'].get(key)
        row = rows[key]
        if p is None:
            raise ValueError('Gallery source proposal absent from historical publication')
        identity = {k: row[k] for k in ('target_key', 'frame_id', 'split',
            'box_index', 'bbox', 'size', 'source_pcd_sha256')}
        image_sha = digest(publication['path']/'images'/f'{key}.png')
        identity.update(formal_manifest_sha256=formal_sha, image_sha256=image_sha)
        if (p['source'] != row or p['class_id'] != row['final_class_id'] or
                p['formal_manifest_sha256'] != formal_sha or
                review['source'] != identity or review['source_binding'] != sha(identity) or
                review['base_proposal_id'] != sha(p) or review['binding'] != publication['binding']):
            raise ValueError(f'Gallery source/proposal binding mismatch: {key}')
        if key in previous_by_key and (previous_by_key[key] != p or
                digest(parent/'images'/f'{key}.png') != image_sha):
            raise ValueError('Parent differs from historical gallery source')
        if review.get('event_kind') != 'human_mask_gallery_revision' or not review.get('human_edited'):
            raise ValueError('Template is not a saved human gallery edit')
        if any(review.get(flag) for flag in FALSE_FLAGS):
            raise ValueError('Unexpected gallery GT/training/robot claim')
        polygon_value(review['polygon'], *row['size'])
        if review['vertex_count'] != len(review['polygon']):
            raise ValueError('Gallery polygon vertex count mismatch')
        if len(review['polygon']) <= 4 or review.get('state') in ('deleted', 'deferred'):
            continue
        old = current.get(key)
        if old and old.get('state') in ('deleted', 'deferred'):
            continue
        templates[key] = dict(row=row, polygon=review['polygon'],
            revision=old['revision'] if old else 0,
            current_payload_sha256=sha(old) if old else None,
            human_polygon_revision=review['revision'], human_event_sha256=sha(review),
            human_mask_confirmed=False, origin=GALLERY_ORIGIN,
            gallery_review=dict(target_key=key, revision=review['revision'],
                payload_sha256=sha(review), source_binding=review['source_binding'],
                polygon_sha256=sha(review['polygon']), snapshot_sha256=snapshot_sha,
                proposal_publication=review['proposal_publication'],
                publication_proposals_sha256=publication['proposals_sha256']))
    return templates


def seed_union(pose_templates, gallery, pose_snapshot_sha, quarantine=()):
    """Only inventory-proven human polygons; latest saved gallery wins a tie."""
    templates = {}
    for key, t in sorted(pose_templates.items()):
        if len(t['polygon']) <= 4 or t.get('origin') not in (None, POSE_ORIGIN):
            raise ValueError('Pose seed is not a direct original human polygon')
        if not t.get('human_event_sha256') or not t.get('human_polygon_revision'):
            raise ValueError('Pose seed lacks human event provenance')
        templates[key] = dict(t, origin=POSE_ORIGIN,
            pose_review=dict(target_key=key, snapshot_sha256=pose_snapshot_sha,
                revision=t['revision'], payload_sha256=t['current_payload_sha256'],
                human_polygon_revision=t['human_polygon_revision'],
                human_event_sha256=t['human_event_sha256'], polygon_sha256=sha(t['polygon'])))
    for key, t in sorted(gallery.items()):
        if t.get('origin') != GALLERY_ORIGIN or len(t['polygon']) <= 4:
            raise ValueError('Gallery seed is not a direct human revision')
        templates[key] = t
    return {k: t for k, t in sorted(templates.items()) if k not in quarantine}


def target_inventory(rows, previous, status, gallery_keys, quarantine, manual_keys=()):
    previous_keys = {p['target_key'] for p in previous}
    work, outcomes = [], []
    for key, row in sorted(rows.items()):
        reason = ('previously_quarantined' if key in quarantine else
            'already_published' if key in previous_keys else
            'saved_manual_source_counted' if key in manual_keys else
            'gallery_history_protected' if key in gallery_keys else status[key])
        if reason == 'eligible' and row['final_class_id'] not in CLASSES:
            reason = 'class_not_requested'
        if reason == 'eligible':
            work.append((key, row))
        else:
            outcomes.append(dict(target_key=key, class_id=row['final_class_id'], reason=reason))
    return work, outcomes


def quota_counts(previous, new, saved_reviews, goal=50, manual=()):
    """Count explicit saved-human additions, never every human seed implicitly."""
    combined = list(previous)+list(manual)+list(new)
    if (len({p['target_key'] for p in combined}) != len(combined) or
            len({p['source']['frame_id'] for p in combined}) != len(combined)):
        raise ValueError('Quota repeats a target or frame')
    result = {}
    for cls in CLASSES:
        old = [p for p in previous if p['class_id'] == cls]
        human = [p for p in manual if p['class_id'] == cls]
        added = [p for p in new if p['class_id'] == cls]
        saved = sum(p['target_key'] in saved_reviews for p in old)
        total = len(old)+len(human)+len(added)
        frames = {p['source']['frame_id'] for p in old+human+added}
        if len(frames) != total or total > goal:
            raise ValueError('Quota exceeds goal or repeats a frame')
        result[cls] = dict(old_total=len(old), manual_added=len(human), added_saved_human=len(human),
            saved_gallery_human=saved, saved_pose_human=len(human), saved_human=saved+len(human),
            prior_machine_pending=len(old)-saved, new_candidates=len(added),
            total=total, missing=goal-total, goal=goal, distinct_frames=len(frames),
            saved_fine_edits=sum(p['target_key'] in saved_reviews and
                len(saved_reviews[p['target_key']].get('polygon', [])) > 4 for p in old)+len(human))
    return result


def template_metadata(t):
    row = t['row']
    provenance = 'gallery_review' if t['origin'] == GALLERY_ORIGIN else 'pose_review'
    return dict(target_key=row['target_key'], class_id=row['final_class_id'],
        split=row['split'], snapshot_revision=t['revision'],
        current_payload_sha256=t['current_payload_sha256'],
        human_polygon_revision=t['human_polygon_revision'], human_event_sha256=t['human_event_sha256'],
        vertex_count=len(t['polygon']), template_human_confirmed=t['human_mask_confirmed'],
        origin=t['origin'], **{provenance: t[provenance]})


def manual_source_proposals(requests, pose_templates, current, rows, previous, pose_sha,
                            formal_sha, classification_sha, quarantine=()):
    """Publish the requested completed human work as human-source drafts, not transfers."""
    keys = [r['target_key'] for r in requests]
    if len(keys) != len(set(keys)):
        raise ValueError('Duplicate manual request')
    previous_keys = {p['target_key'] for p in previous}
    frames = {p['source']['frame_id'] for p in previous}
    templates = seed_union(pose_templates, {}, pose_sha, quarantine)
    result = []
    for request in sorted(requests, key=lambda r: r['target_key']):
        key = request['target_key']; row = rows[key]
        t, value = templates.get(key), current.get(key)
        if (key in previous_keys or row['frame_id'] in frames or key in quarantine):
            raise ValueError('Manual source repeats a published frame or is quarantined')
        identity_fields = ('target_key', 'target_id', 'frame_id', 'split', 'group_id',
            'box_index', 'bbox', 'size', 'source_pcd_sha256', 'policy_id', 'viewpoint_candidate')
        if (request['class_id'] != row['final_class_id'] or
                request['source']['formal_manifest_sha256'] != formal_sha or
                any(request['source'].get(k) != row.get(k) for k in identity_fields)):
            raise ValueError('Manual request source identity changed')
        if (t is None or value is None or value.get('state') in ('deleted', 'deferred') or
                len(value.get('polygon', [])) <= 4 or t['polygon'] != value['polygon'] or
                t['revision'] != value['revision'] or t['current_payload_sha256'] != sha(value)):
            raise ValueError('Manual request lacks a current saved direct human polygon')
        if any(value.get(flag) for flag in FALSE_FLAGS):
            raise ValueError('Manual request unexpectedly confirmed or eligible')
        frames.add(row['frame_id'])
        result.append(dict(schema=MANUAL_SCHEMA, origin=POSE_ORIGIN,
            target_key=key, class_id=row['final_class_id'], source=row,
            polygon=value['polygon'], vertex_count=len(value['polygon']),
            status='human_edited_draft_pending_confirmation', human_edited=True,
            source_label_kind='human_mask_source_manual_template_draft',
            **dict.fromkeys(FALSE_FLAGS, False), pose_snapshot_saved=True,
            pose_store_written=False, gallery_store_written=False,
            mask_semantics='saved_human_fork_face_contour_pending_confirmation',
            template=template_metadata(t), rendering_id=GENERATION_NAMESPACE+':'+key,
            target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
            snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha,
            class_assignments_sha256=classification_sha, metrics={},
            flags=['manual_source_not_machine_transfer', 'saved_human_mask_requires_confirmation'],
            manual_request_sha256=sha(request), original_pose_url=request.get('pose_annotation_url'),
            geometry_pose_policy='original_pose_unchanged; independent_gallery_review_not_imported'))
    quota_counts(previous, [], {}, manual=result)
    return result


def freeze_manual_sources(db, manual):
    """Freeze exact source payloads and the event bytes hashed by annotation_inventory."""
    result = []
    for p in manual:
        key = p['target_key']; t = p['template']
        current_rows = db.execute('SELECT revision,payload FROM annotations WHERE target_key=?', (key,)).fetchall()
        event_rows = db.execute('SELECT id,revision,payload FROM events WHERE target_key=? AND revision=?',
            (key, t['human_polygon_revision'])).fetchall()
        if len(current_rows) != 1 or len(event_rows) != 1:
            raise ValueError('Manual source current/event identity is ambiguous')
        revision, raw = current_rows[0]; event_id, event_revision, event_raw = event_rows[0]
        value, event = json.loads(raw), json.loads(event_raw)
        if (revision != p['target_expected_revision'] or sha(value) != p['target_snapshot_payload_sha256'] or
                hashlib.sha256(event_raw.encode()).hexdigest() != t['human_event_sha256'] or
                value['polygon'] != p['polygon'] or event['polygon'] != p['polygon'] or
                event.get('event_kind') in ('unit_normalization', 'axes_normalization', 'mask_template_proposal')):
            raise ValueError('Manual source frozen payload/event mismatch')
        result.append(dict(schema='saved_pose_mask_source_freeze_v1', target_key=key,
            snapshot_sha256=p['snapshot_sha256'], current_revision=revision,
            current_payload_sha256=sha(value), current_payload_raw=raw, current_payload=value,
            current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(),
            human_event_id=event_id, human_polygon_revision=event_revision,
            human_event_sha256=t['human_event_sha256'], human_event_payload_raw=event_raw,
            human_event_payload=event, polygon_sha256=sha(p['polygon'])))
    return result


def proposal_from_attempt(key, row, accepted, templates, current, pose_sha,
                          formal_sha, classification_sha):
    t = templates[accepted['template_key']]
    if (t['origin'] not in (POSE_ORIGIN, GALLERY_ORIGIN) or
            len(accepted['polygon']) != len(t['polygon']) or not compatible(t['row'], row)):
        raise ValueError('Transfer violated direct-human/compatibility/vertex contract')
    polygon_value(accepted['polygon'], *row['size'])
    if accepted['metrics'].get('boundary_guard', {}).get('state') != 'passed':
        raise ValueError('Transfer lacks passed boundary gate')
    template = template_metadata(t)
    old = current.get(key)
    return dict(schema=VERSION, target_key=key, class_id=row['final_class_id'], source=row,
        polygon=accepted['polygon'], vertex_count=len(accepted['polygon']),
        status='machine_draft_pending_visual_review', **dict.fromkeys(FALSE_FLAGS, False),
        production_saved=False, mask_semantics='inherited_human_fork_face_contour_with_aperture_notches',
        template=template, target_expected_revision=old['revision'] if old else 0,
        target_snapshot_payload_sha256=sha(old) if old else None, snapshot_sha256=pose_sha,
        formal_manifest_sha256=formal_sha, class_assignments_sha256=classification_sha,
        metrics=accepted['metrics'], flags=['machine_mask_requires_review', 'direct_human_template']+
            (['class_assignment_requires_review'] if row.get('classification_review_required') else []),
        continuation=dict(batch=PUBLICATION_VERSION, template_rank=accepted['template_rank'],
            all_compatible_targets_scanned=True, recursive_candidate_propagation=False),
        generation_method=dict(name='original', version='attempt_target_v1',
            code_sha256=digest(PROJECT/'pallet/mask_template_continue.py')),
        geometry_pose_policy='explicit_adoption_only; rebuild_geometry; preserve_old_review_history')


def preserve_parent_assets(parent, out, previous):
    hashes = {}
    (out/GENERATION_NAMESPACE).mkdir()
    (out/'templates').rename(out/GENERATION_NAMESPACE/'templates')
    for p in previous:
        for folder, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            relative = f'{folder}/{p["target_key"]}.{extension}'
            if (out/relative).exists():
                raise ValueError('Historical target asset collision')
            shutil.copyfile(parent/relative, out/relative)
            hashes[relative] = digest(parent/relative)
    for directory in ('templates', 'refined', 'generation'):
        shutil.copytree(parent/directory, out/directory)
        for file in sorted((parent/directory).rglob('*')):
            if file.is_file():
                hashes[str(file.relative_to(parent))] = digest(file)
    if (parent/'vertex2_before_after.png').exists():
        shutil.copyfile(parent/'vertex2_before_after.png', out/'vertex2_before_after.png')
        hashes['vertex2_before_after.png'] = digest(parent/'vertex2_before_after.png')
    if any(digest(out/name) != value for name, value in hashes.items()):
        raise ValueError('Historical asset copy mismatch')
    return hashes


def rendering_inventory(parent_inventory, templates, combined, new_keys, manual_keys=()):
    inherited = parent_inventory['templates']
    generation = [dict(t, rendering_id=GENERATION_NAMESPACE+':'+key) for key, t in sorted(templates.items())]
    ids = [t['rendering_id'] for t in inherited+generation]
    if len(ids) != len(set(ids)):
        raise ValueError('Rendering template namespace collision')
    mapping = {p['target_key']: (GENERATION_NAMESPACE+':'+p['template']['target_key']
        if p['target_key'] in new_keys or p['target_key'] in manual_keys
        else parent_inventory['rendering_by_target'][p['target_key']]) for p in combined}
    if not set(mapping.values()) <= set(ids):
        raise ValueError('Rendering template missing')
    return dict(templates=inherited+generation, rendering_by_target=mapping,
        generation_template_namespace=GENERATION_NAMESPACE, generation_template_keys=sorted(templates),
        historical_templates=len(inherited),
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())))


def package_manual_preview(out, manual, evidence):
    """Human-source cards have no registration comparison or synthetic metrics."""
    for p in manual:
        key, row = p['target_key'], p['source']
        image = evidence.image(row['frame_id'])
        panel = Image.new('RGB', (1480, 305), '#121923'); draw = ImageDraw.Draw(panel)
        draw.text((15, 8), f'Class {p["class_id"]} | SAVED HUMAN POSE MASK | {p["vertex_count"]} vertices | NOT machine transfer; NOT confirmed GT', fill='white')
        panel.paste(overlay(image, row, p['polygon'], '#55ceff'), (0, 30))
        draw.text((765, 55), 'Original saved human polygon; no machine comparison or score.', fill='white')
        draw.text((765, 85), f'Pose snapshot revision: {p["target_expected_revision"]}', fill='white')
        draw.text((765, 115), f'Human polygon revision: {p["template"]["human_polygon_revision"]}', fill='white')
        draw.text((765, 145), 'Independent gallery review has NOT been imported or saved.', fill='white')
        draw.text((765, 175), 'Confirmation / automatic training / robot control: all disabled.', fill='white')
        draw.text((15, 284), f'original pose target {key} | {row["split"]} | human draft pending confirmation', fill='white')
        panel.save(out/'cases'/f'{key}.png')
        Image.fromarray(image).save(out/'images'/f'{key}.png')
        mask = raster_mask(p['polygon'], *row['size'])
        Image.fromarray(mask.astype(np.uint8)*255).save(out/'masks'/f'{key}.png')
        flags = dict(human_edited=True, human_confirmed=False, machine_draft=False,
            automatic_training_eligible=False, robot_command_eligible=False,
            ground_truth_eligible=False, pose_snapshot_saved=True, independent_gallery_saved=False)
        write_json(out/'labelme'/f'{key}.json', dict(version='5.0.0', imagePath=f'../images/{key}.png',
            imageData=None, imageWidth=row['size'][0], imageHeight=row['size'][1],
            annotation_scope='single_target_mask_review_not_full_frame', other_objects_unchanged=True,
            flags=flags, shapes=[dict(label='pallet', points=p['polygon'], shape_type='polygon',
                group_id=row['box_index'], flags=flags)], mask_template_provenance=p))


def build_report(out, combined, new_keys, counts, reviews, manual_keys=()):
    table = ''.join(f'<tr><td>{cls}</td><td>{c["old_total"]}</td><td>{c["manual_added"]}</td><td>{c["new_candidates"]}</td>'
        f'<td>{c["total"]}</td><td>{c["missing"]}</td></tr>' for cls, c in counts.items())
    notes = ''.join(f'<li>类别 {cls}：历史已保存图册人工修订 {c["saved_gallery_human"]} 份；'
        f'本轮计入原位姿人工描绘 {c["manual_added"]} 份；累计已保存人工轮廓 {c["saved_human"]} 份；'
        f'历史机器候选待复核 {c["prior_machine_pending"]} 份。</li>' for cls, c in counts.items())
    summary = (f'历史候选共 {sum(c["old_total"] for c in counts.values())} 份，'
        f'其中已保存人工修订 {sum(c["saved_gallery_human"] for c in counts.values())} 份，'
        f'机器候选待复核 {sum(c["prior_machine_pending"] for c in counts.values())} 份。'
        f'本轮计入已保存人工描绘 {sum(c["manual_added"] for c in counts.values())} 份；'
        f'本轮新增机器候选 {sum(c["new_candidates"] for c in counts.values())} 份，均待人工复核。')
    cards = []
    for p in sorted(combined, key=lambda p: (p['class_id'], p['target_key'])):
        key = p['target_key']
        manual = key in manual_keys
        label = (f'【本轮计入人工描绘 r{p["target_expected_revision"]}】原位姿已保存 · 未确认真值' if manual else
            '【本轮新增】机器草稿 · 待人工复核' if key in new_keys else
            f'【已保存人工修订 r{reviews[key]["revision"]}】原始候选对照'
            if key in reviews else '【历史机器候选】待人工复核')
        split = {'train': '训练划分', 'val': '验证划分', 'test': '测试划分'}.get(
            p['source']['split'], p['source']['split'])
        original_link = (f'<p><a href="{html.escape(p.get("original_pose_url") or "", quote=True)}">打开原位姿人工标注</a>；'
            '当前为原位姿人工轮廓快照，并未导入独立图册人工保存历史；不是机器迁移。</p>' if manual else '')
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" '
            f'data-batch="{"new" if key in new_keys or manual else "previous"}" data-kind="{"manual" if manual else "machine"}"><h2>类别 {p["class_id"]} · {label}</h2>'
            f'<img loading="lazy" src="cases/{key}.png?revision=6" alt="{"原位姿已保存人工轮廓" if manual else "左侧模板与右侧候选对照"}">'
            f'<p>目标：{html.escape(p["source"]["frame_id"])} ／ {html.escape(split)} ／ {key}</p>'
            f'{original_link}<a href="labelme/{key}.json?revision=6">{"人工快照" if manual else "候选"}多边形（LabelMe）</a> · '
            f'<a href="masks/{key}.png?revision=6">{"人工快照" if manual else "候选"}掩膜</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=6')}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1500px}a{color:#77ceff}img{width:100%}article{border:1px solid #425269;margin:20px 0;padding:12px}td,th{padding:7px;border:1px solid #425269}table{border-collapse:collapse}[hidden]{display:none!important}</style>
<h1>第六版 · 人工模板掩膜配额补充草稿</h1>
<p>直接模板来自最新已保存图册人工多边形与原始位姿人工多边形的并集，均须超过四个顶点。同一目标优先采用图册人工修订；明确请求且已完成人工描绘的24个不同帧计入配额，其他原始位姿模板不自动计入。机器候选不递归用作模板，已删除或暂缓的目标不作为种子。</p>
<p>只匹配同类、同划分、同传感器配置及兼容视角，保持顶点数与原质量门槛。历史候选、人工掩膜修改历史、图册历史及删除、暂缓、确认、隔离目标均受保护。新增结果均为待复核草稿，不自动确认为真值，不进入训练或机器人控制。</p>
<p>每类目标为累计五十个不同帧。缺额表示仍需补充的合格候选数量，不保证自动生成能够补齐。已保存人工修订与历史待复核机器候选分开统计，已保存不等于已确认为真值。</p>
<p>历史卡片与下载文件保留原始候选；最新人工修订请在在线编辑器查看或导出。本轮机器新增卡片左侧为直接人工模板，右侧为机器候选；本轮人工描绘卡片仅显示原位姿已保存轮廓，没有机器对照或匹配评分，不会自动导入独立图册人工保存历史。</p>
<p>'''+summary+'''</p><table><tr><th>类别</th><th>历史总数</th><th>新增已保存人工</th><th>新增机器候选</th><th>累计不同帧</th><th>缺额</th></tr>'''+table+'''</table><ul>'''+notes+'''</ul>
<label>筛选类别 <select id="filter"><option value="">全部</option>'''+''.join(f'<option>{c}</option>' for c in CLASSES)+'''</select></label>
<label><input type="checkbox" id="only-new" checked>只看本轮新增</label>'''+''.join(cards)+'''
<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 generate(output=OUTPUT, workers=4, method='original', parent=PARENT, requests_directory=REQUESTS):
    """Explicit invocation only; no live database/config/service writes."""
    if method != 'original':
        raise ValueError('Multiscale integration deferred; only original is implemented')
    if not 1 <= workers <= 8:
        raise ValueError('Workers must be 1–8')
    out, parent = Path(output).resolve(), Path(parent).resolve()
    request_dir = Path(requests_directory).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory')
    manifest = json.loads((parent/'manifest.json').read_text())
    formal = Path(json.loads((PROJECT/'configs/active_formal_dataset.json').read_text())['dataset'])
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    formal_sha, classification_sha = digest(formal/'manifest.json'), digest(classification)
    paths = dict(SOURCE_PUBLICATIONS)
    paths[5] = parent
    publications = publication_inventory(paths, formal_sha)
    if manifest['classification_sha256'] != classification_sha:
        raise ValueError('Classification changed since parent publication')
    request_manifest = json.loads((request_dir/'manifest.json').read_text())
    requests = read_jsonl(request_dir/'requests.jsonl')
    request_hashes = {str(request_dir/name): digest(request_dir/name)
        for name in ('requests.jsonl', 'manifest.json', 'verification.json')}
    request_verification = json.loads((request_dir/'verification.json').read_text())
    if (request_verification['state'] != 'passed' or len(requests) != 24 or
            Counter(r['class_id'] for r in requests) != {1: 2, 2: 8, 9: 3, 10: 8, 15: 3} or
            any(r['source']['formal_manifest_sha256'] != formal_sha for r in requests) or
            request_verification.get('manifest_sha256') != digest(request_dir/'manifest.json') or
            request_verification.get('requests_sha256') != digest(request_dir/'requests.jsonl') or
            request_manifest.get('requests_sha256') != digest(request_dir/'requests.jsonl')):
        raise ValueError('Expected verified, unchanged 24-item manual request queue')
    previous = read_jsonl(parent/'proposals.jsonl')
    previous_keys = {p['target_key'] for p in previous}
    if (len(previous) != 140 or len(previous_keys) != 140 or
            len({p['source']['frame_id'] for p in previous}) != 140 or
            Counter(p['class_id'] for p in previous) != {1: 48, 2: 5, 9: 38, 10: 10, 15: 39}):
        raise ValueError('Publication 5 parent frame/class inventory changed')
    frozen_code = {
        'pallet/mask_template_transfer.py': manifest['transfer_code_sha256'],
        'pallet/mask_boundary_review.py': manifest['boundary_code_sha256'],
        'pallet/mask_template_continue.py': manifest['attempt_code_sha256'],
    }
    for name in ('pallet/mask_template_refined.py', 'pallet/mask_quota_completion.py'):
        frozen_code[name] = digest(PROJECT/name)
    if any(digest(PROJECT/name) != value for name, value in frozen_code.items()):
        raise ValueError('Frozen transfer/threshold code changed')
    annotations, sources = load_dataset(formal)
    class_rows = read_jsonl(classification)
    rows = {r['target_key']: r for r in class_rows}
    identities = {target_key(a['frame_id'], i): (a, b) for a in annotations for i, b in enumerate(a['boxes'])}
    if len(rows) != len(class_rows) or set(rows) != set(identities):
        raise ValueError('Formal/classification target coverage mismatch')
    for key, row in rows.items():
        a, box = identities[key]; source = sources[a['frame_id']]
        if (row['bbox'] != box or any(row[k] != a[k] for k in ('frame_id', 'split', 'group_id')) or
                row['source_pcd_sha256'] != source['source_anchor_hash'] or
                row['policy_id'] != source['sensor_policy']['policy_id']):
            raise ValueError('Formal/classification source identity mismatch')
        row['size'] = source['size']
    if any(p['source'] != rows[p['target_key']] or p['formal_manifest_sha256'] != formal_sha or
            p['class_id'] != rows[p['target_key']]['final_class_id'] for p in previous):
        raise ValueError('Parent source identity changed')
    out.mkdir(parents=True)
    protected = protected_snapshot(formal)
    write_json(out/'protected_formal_before.json', protected)
    pose_live = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
    gallery_live = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
    pose_snapshot, gallery_snapshot = out/'reviews_snapshot.sqlite3', out/'gallery_reviews_snapshot.sqlite3'
    pose_sha = snapshot_database(pose_live, pose_snapshot)
    gallery_sha = snapshot_database(gallery_live, gallery_snapshot)
    with closing(readonly(pose_snapshot, True)) as db:
        current, pose_templates, status = annotation_inventory(db, rows)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        binding = json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])
        if binding['formal_manifest_sha256'] != formal_sha:
            raise ValueError('Pose snapshot formal binding mismatch')
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM annotations UNION ALL SELECT target_key,revision,payload FROM events'):
            value = json.loads(raw)
            if key not in rows or value['target_key'] != key or value['revision'] != revision:
                raise ValueError('Pose relational identity mismatch')
    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 snapshot formal binding mismatch')
        reviews = {k: json.loads(raw) for k, 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 key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews UNION ALL SELECT target_key,revision,payload FROM events'):
            value = json.loads(raw)
            if key not in rows or value['target_key'] != key or value['revision'] != revision:
                raise ValueError('Gallery relational identity mismatch')
    gallery = gallery_templates(previous, reviews, gallery_events, current, rows, parent,
        formal_sha, gallery_sha, paths)
    quarantine = QUARANTINE | set(manifest.get('preserved_quarantined_targets', [])) | set(manifest.get('quarantined_targets', []))
    templates = seed_union(pose_templates, gallery, pose_sha, quarantine)
    manual = manual_source_proposals(requests, pose_templates, current, rows, previous,
        pose_sha, formal_sha, classification_sha, quarantine)
    manual_keys = {p['target_key'] for p in manual}
    with closing(readonly(pose_snapshot, True)) as db:
        manual_sources = freeze_manual_sources(db, manual)
    base_counts = quota_counts(previous, [], reviews, manual=manual)
    if (len(manual) != 24 or {cls: c['missing'] for cls, c in base_counts.items()} !=
            {1: 0, 2: 37, 9: 9, 10: 32, 15: 8}):
        raise ValueError('Saved-manual base inventory changed')
    write_jsonl(out/'requests.jsonl', requests)
    write_jsonl(out/'manual_proposals.jsonl', manual)
    write_jsonl(out/'manual_source_records.jsonl', manual_sources)
    gallery_protected = set(reviews) | {v['target_key'] for v in gallery_events}
    pose_protected = {key for key, reason in status.items() if reason != 'eligible'}
    work, outcomes = target_inventory(rows, previous, status, gallery_protected, quarantine, manual_keys)
    write_json(out/'protected_targets.json', dict(previous=sorted(previous_keys),
        counted_manual_source_keys=sorted(manual_keys),
        pose_mask_history_or_noneligible=sorted(pose_protected), gallery_saved_or_history=sorted(gallery_protected),
        quarantine=sorted(quarantine), eligibility=status))
    write_jsonl(out/'refined_annotations.jsonl', [reviews[k] for k in sorted(reviews)])
    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()}))
    evidence = ImageEvidence(formal, rows, sources)
    for request in requests:
        row = rows[request['target_key']]
        evidence.image(row['frame_id'])
        if request['source']['image_sha256'] != digest(formal/sources[row['frame_id']]['image']):
            raise ValueError('Manual request image no longer matches 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('Parent gallery image differs from formal source pixels')
    attempts, accepted_proposals = [], []
    print(json.dumps(dict(state='scanning', templates=len(templates),
        templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        manual_added=len(manual), base_count=len(previous)+len(manual),
        base_gaps={cls: c['missing'] for cls, c in base_counts.items()},
        eligible_targets=len(work), workers=workers, method=method)), flush=True)
    def worker(item):
        return item, attempt_target(*item, templates, evidence)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (accepted, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            attempts.extend(trials); outcomes.append(outcome)
            if accepted:
                accepted_proposals.append(proposal_from_attempt(key, row, accepted, templates,
                    current, pose_sha, formal_sha, classification_sha))
            if number % 200 == 0:
                print(json.dumps(dict(scanned=number, accepted=len(accepted_proposals), attempts=len(attempts))), flush=True)
    new, dispositions = select_diverse(previous+manual, accepted_proposals)
    for outcome in outcomes:
        if outcome['target_key'] in dispositions:
            outcome['reason'] = dispositions[outcome['target_key']]
    combined = sorted(previous+manual+new, key=lambda p: p['target_key'])
    new_keys = {p['target_key'] for p in new}
    if (len({p['target_key'] for p in combined}) != len(combined) or
            len({p['source']['frame_id'] for p in combined}) != len(combined) or
            new_keys & (previous_keys | manual_keys | quarantine | gallery_protected | pose_protected)):
        raise ValueError('Protected or duplicated proposal target/frame')
    if len(outcomes) != len(rows) or {v['target_key'] for v in outcomes} != set(rows):
        raise ValueError('Incomplete outcome inventory')
    for name, values in (('proposals', combined), ('new_proposals', new),
            ('accepted_candidates', accepted_proposals), ('registration_attempts', attempts), ('outcomes', outcomes)):
        write_jsonl(out/f'{name}.jsonl', values)
    old_canonical = {p['target_key']: canonical(p) for p in previous}
    restored = {p['target_key']: canonical(p) for p in read_jsonl(out/'proposals.jsonl')}
    if any(restored.get(k) != value for k, value in old_canonical.items()):
        raise ValueError('Parent canonical proposal changed')
    counts = quota_counts(previous, new, reviews, manual=manual)
    for cls, c in counts.items():
        class_outcomes = [v for v in outcomes if v['class_id'] == cls]
        c.update(templates=sum(t['row']['final_class_id'] == cls for t in templates.values()),
            eligible=sum(row['final_class_id'] == cls for _, row in work),
            compatible_targets=sum(v['reason'] in ('new_proposal', 'image_similarity_below_gate',
                'registration_or_boundary_rejected', 'accepted_duplicate_frame_not_selected',
                'accepted_quota_full_not_selected') for v in class_outcomes),
            preview=c['total'], outcomes=dict(Counter(v['reason'] for v in class_outcomes)))
    inventory = rendering_inventory(json.loads((parent/'template_inventory.json').read_text()), templates, combined, new_keys, manual_keys)
    inventory.update(classes=counts, eligibility=status, 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), counted_manual_source_keys=sorted(manual_keys))
    write_json(out/'template_inventory.json', inventory)
    package_preview(out, new, templates, rows, evidence, counts)
    package_manual_preview(out, manual, evidence)
    copied = preserve_parent_assets(parent, out, previous)
    write_json(out/'preserved_parent_assets.json', copied)
    for key, t in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', t)
    build_report(out, combined, new_keys, counts, reviews, manual_keys)
    contact = Image.new('RGB', (1480, len(CLASSES)*305), '#121923')
    for i, cls in enumerate(CLASSES):
        p = next(p for p in manual+new+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 = json.loads((parent/'used_image_hashes.json').read_text())
    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 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('Protected formal files changed')
    verify_history_preserved(pose_live, pose_snapshot)
    verify_history_preserved(gallery_live, gallery_snapshot)
    if digest(pose_snapshot) != pose_sha or digest(gallery_snapshot) != gallery_sha:
        raise ValueError('Frozen review snapshot changed')
    if any(digest(PROJECT/name) != value for name, value in frozen_code.items()):
        raise ValueError('Frozen code changed during generation')
    if any(digest(name) != value for name, value in request_hashes.items()):
        raise ValueError('Manual request queue changed during generation')
    write_json(out/'shortfalls.json', {cls: dict(missing=c['missing'], outcomes=c['outcomes'],
        explanation='All eligible targets scanned with original gates and direct human seeds; distinct frames only. '
            'The 24 explicitly requested saved-human pose masks count separately; other pose seeds are not quota entries. '
            'Remaining quotas require further accepted candidates or explicitly counted human annotations.')
        for cls, c in counts.items()})
    result = dict(state='quota_pending_independent_verification', publication_version=PUBLICATION_VERSION,
        version=VERSION, parent=str(parent), parent_proposals_sha256=digest(parent/'proposals.jsonl'),
        parent_verification_sha256=digest(parent/'verification.json'), previous_proposals=len(previous),
        new_proposals=len(new), manual_proposals=len(manual), manual_added=len(manual),
        base_proposals=len(previous)+len(manual), base_classes=base_counts, proposals=len(combined), classes=counts,
        manual_by_class=dict(Counter(p['class_id'] for p in manual)),
        manual_source_schema=MANUAL_SCHEMA, counted_manual_source_keys=sorted(manual_keys),
        manual_source_snapshots_imported_into_gallery_store=False,
        new_by_class=dict(Counter(p['class_id'] for p in new)),
        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,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        method=method, scanned_eligible_targets=len(work), accepted_before_diversity=len(accepted_proposals),
        outcomes=dict(Counter(v['reason'] for v in outcomes)), preserved_quarantined_targets=sorted(quarantine),
        proposals_sha256=digest(out/'proposals.jsonl'), new_proposals_sha256=digest(out/'new_proposals.jsonl'),
        manual_proposals_sha256=digest(out/'manual_proposals.jsonl'),
        manual_source_records_sha256=digest(out/'manual_source_records.jsonl'),
        requests_sha256=digest(out/'requests.jsonl'), manual_request_input_sha256=request_hashes,
        template_inventory_sha256=digest(out/'template_inventory.json'),
        refined_annotations_sha256=digest(out/'refined_annotations.jsonl'),
        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'],
        production_database_writes=0, original_splits_preserved=True, cross_split_template_transfers=0,
        recursive_candidate_propagation=False, thresholds_relaxed=False, automatic_human_confirmation=False,
        automatic_training_eligible=False, robot_command_eligible=False, ground_truth_eligible=False)
    write_json(out/'manifest.json', result)
    write_json(out/'artifact_hashes.json', {str(p.relative_to(out)): digest(p)
        for p in sorted(out.rglob('*')) if p.is_file()})
    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)
    parser.add_argument('--method', choices=('original',), default='original',
        help='Original guarded attempt_target; multiscale integration is deferred')
    args = parser.parse_args()
    cv2.setNumThreads(1)
    print(json.dumps(generate(args.output, args.workers, args.method)), flush=True)


if __name__ == '__main__':
    main()
