"""Prepare direct-human coverage requests, never masks, transfers or activation.

Review accepts a completed publication-10 generation (an independent seal may
arrive afterwards). Build requires its matching passed independent seal AND
explicit per-source crop/full-image decisions. Verify is a read-only self-audit,
not independent verification. No live database/configuration/service is written.
"""
from __future__ import annotations

import argparse
from collections import Counter, defaultdict
from contextlib import closing
from datetime import datetime, timezone
import hashlib
import html
import json
import math
from pathlib import Path
import shutil
import sys
from urllib.parse import urlsplit

import cv2
from PIL import Image, ImageDraw, ImageFont

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest, protected_snapshot
from pallet.mask_quota_saved_extension import file_inventory, indexed, load_formal_evidence
from pallet.mask_remaining_classes import original_rectangle
from pallet.mask_template_refined import QUARANTINE, readonly, read_jsonl, write_jsonl
from pallet.mask_template_transfer import rectangle, write_json
from pallet.pose_review_geometry import canonical
from scripts.build_mask_template_requests import database_fingerprint, native_quality, render_case, signature

CLASSES = (4, 5, 7, 14, 22)
PARENT = PROJECT/'outputs/pallet_mask_remaining_expand_v1'
DEFAULT = PROJECT/'outputs/pallet_mask_remaining_direct_requests_v1'
POSE = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
GALLERY = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
ACCESS = PROJECT/'outputs/pallet_pose_review_managed_v1/public_access.json'
CLASSIFICATION = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
FONT = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
OLD_REQUEST_DIRS = (
    'pallet_mask_template_requests_v1', 'pallet_mask_class10_manual_supplement_v2',
    'pallet_mask_quota_100_requests_v2', 'pallet_mask_quota_100_final_requests_v2',
    'pallet_mask_remaining_requests_v1', 'pallet_mask_remaining_requests_v2',
)
# Only these ROOT audit files may be added after review. Existing ones remain bound.
PARENT_AUDIT_NAMES = frozenset((
    'verification.json', 'validation_report.json', 'independent_verification.json',
    'independent_validation_report.json',
))
FALSE_FLAGS = (
    'human_mask_confirmed', 'human_pose_confirmed', 'metric_pose_confirmed', 'human_confirmed',
    'ground_truth_eligible', 'automatic_training_eligible', 'robot_command_eligible',
)
GEOMETRY = ('pose', 'ruler', 'sensor', 'selected_faces', 'exclusions')
IDENTITY = ('target_key', 'target_id', 'frame_id', 'box_index', 'bbox', 'size', 'split',
    'group_id', 'policy_id', 'source_pcd_sha256')
QUALITY_GATES = dict(min_width=50, min_height=14, min_area=1000, min_image_margin=1,
    min_p95_p05=35, min_brightness_fraction_above_48=.12,
    max_saturated_fraction_above_250=.65, min_sobel_magnitude_above_60_fraction=.025)


def require(condition, message):
    if not condition:
        raise ValueError(message)


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


def sha(value):
    return hashlib.sha256(canonical(value)).hexdigest()


def raw_sha(raw):
    return hashlib.sha256(raw.encode()).hexdigest()


def private_json(path, value, lines=False):
    (write_jsonl if lines else write_json)(path, value)
    Path(path).chmod(0o600)


def member(root, name):
    path = Path(root)/name
    require(not Path(name).is_absolute() and '..' not in Path(name).parts
        and path.resolve().is_relative_to(Path(root).resolve()) and not path.is_symlink(),
        'Unsafe bound artifact path')
    return path


def valid_output(out, parent):
    out, parent = Path(out).resolve(), Path(parent).resolve()
    require(out.is_relative_to(PROJECT/'outputs') and
        out.name.startswith('pallet_mask_remaining_direct_requests_v') and
        not out.is_relative_to(parent) and not parent.is_relative_to(out),
        'Use a separate fresh outputs/pallet_mask_remaining_direct_requests_v* directory')
    return out


def class_quotas(proposals, per_class=10):
    require(type(per_class) is int and 1 <= per_class <= 10, 'Per-class limit must be 1–10')
    indexed(proposals)
    require(len({p['source']['frame_id'] for p in proposals}) == len(proposals),
        'Parent cumulative frames are not globally distinct')
    counts = Counter(p['class_id'] for p in proposals)
    require(all(type(c) is int and 1 <= c <= 22 and n <= 100 for c, n in counts.items()),
        'Parent class identity/quota differs')
    return {c: min(per_class, 100-counts[c]) for c in CLASSES}


def parent_seal(parent, manifest, required):
    """Self-check/generation reports cannot substitute for an external passed seal."""
    seals = {}
    for name in ('verification.json', 'independent_verification.json'):
        path = parent/name
        if not path.exists():
            continue
        seal = read_json(path)
        require(seal.get('state') == 'passed' and seal.get('publication_version') == 10
            and seal.get('manifest_sha256') == digest(parent/'manifest.json')
            and seal.get('proposals_sha256') == digest(parent/'proposals.jsonl')
            and seal.get('proposals_verified', seal.get('proposals')) == manifest['proposals'],
            'Publication 10 requires a matching passed independent seal')
        if 'artifact_hashes_sha256' in seal:
            require(seal['artifact_hashes_sha256'] == digest(parent/'artifact_hashes.json'),
                'Parent independent artifact binding changed')
        seals[name] = digest(path)
    require(bool(seals) or not required, 'Build requires verified publication 10; review is not a seal')
    return seals


def load_parent(parent, require_seal=False):
    manifest = read_json(parent/'manifest.json')
    require(manifest.get('publication_version') == 10 and manifest.get('previous_proposals') == 500
        and manifest.get('reused_old_manual_proposals') == 141
        and manifest.get('completed_request_manual_proposals') == 51
        and manifest.get('manual_proposals') == 192 and manifest.get('base_proposals') == 692
        and manifest.get('generation_templates') == 41,
        'Expected completed pub10 generation: protected 500 + actual 141 + 51 + machines')
    validation = read_json(parent/'generation_validation.json')
    require(validation.get('state') == 'self_checks_passed_not_independent_verification'
        and validation.get('publication_version') == 10, 'Parent generation is not complete')
    inventory = file_inventory(parent)
    for name, expected in read_json(parent/'artifact_hashes.json').items():
        require(inventory.get(name) == expected, 'Parent frozen artifact changed: '+name)
    for field, expected in manifest.items():
        if field.endswith('_sha256') and field[:-7]+'.jsonl' in inventory:
            require(inventory[field[:-7]+'.jsonl'] == expected, 'Parent source binding changed: '+field)
    proposals = read_jsonl(parent/'proposals.jsonl')
    quotas = class_quotas(proposals)
    require(len(proposals) == manifest['proposals'] and manifest['proposals_sha256'] == inventory['proposals.jsonl'],
        'Parent cumulative proposal count/hash changed')
    previous = read_jsonl(parent/'parent_evidence/proposals.jsonl')
    reused = read_jsonl(parent/'reuse_old_manual_proposals.jsonl')
    completed = read_jsonl(parent/'completed_request_manual_proposals.jsonl')
    new = read_jsonl(parent/'new_proposals.jsonl')
    combined = indexed(proposals)
    require(len(previous) == 500 and len(reused) == 141 and len(completed) == 51
        and len(previous+reused+completed+new) == len(proposals)
        and set(indexed(previous+reused+completed+new)) == set(combined)
        and all(canonical(combined[p['target_key']]) == canonical(p) for p in previous+reused+completed+new),
        'Parent must preserve all canonical 500 + 141 + 51 sources and actual new machines')
    requested_quads = [p for p in completed if p['class_id'] in CLASSES]
    require(Counter(p['class_id'] for p in requested_quads) == {c: 3 for c in CLASSES}
        and all(len(p['polygon']) == 4 and p.get('manual_four_point_source') is True
            and p.get('propagation_seed_eligible') is False for p in requested_quads),
        'Target classes require their actual three valid saved quads, not invented >4-point seeds')
    for c in range(1, 23):
        n = sum(p['class_id'] == c for p in proposals)
        counts = manifest['classes'][str(c)]
        require(counts['total'] == counts['distinct_frames'] == n and counts['missing'] == 100-n,
            'Parent cumulative class counts differ from actual proposals')
    for name, expected in manifest['frozen_code_sha256'].items():
        require(digest(member(PROJECT, name)) == expected, 'Frozen original helper changed: '+name)
    seals = parent_seal(parent, manifest, require_seal)
    return dict(manifest=manifest, proposals=proposals, quotas=quotas, inventory=inventory, seals=seals)


def original_pose_only(value, row):
    return (value.get('state') == 'draft' and original_rectangle(value.get('polygon'), row)
        and not any(value.get(f, False) is not False for f in FALSE_FLAGS)
        and value.get('event_kind') != 'mask_template_proposal'
        and value.get('origin') in (None, 'original_pose_human_revision')
        and not any(value.get(f) is True for f in
            ('machine_draft', 'machine_transfer_performed', 'automatic_generated', 'machine_generated')))


def protection(rows, current, events, gallery_keys, proposals, old_requests, quarantine=()):
    """Global frames, not only keys: restored masks and all special history stay out."""
    protected = set(quarantine) | set(gallery_keys) | {p['target_key'] for p in proposals}
    protected.update(r['target_key'] for r in old_requests)
    history = defaultdict(list)
    for event_id, key, revision, raw in events:
        require(key in rows, 'Unknown historical target')
        value = json.loads(raw)
        clean = original_pose_only(value, rows[key])
        if not clean:
            protected.add(key)
        history[key].append(dict(event_id=event_id, revision=revision,
            payload_raw_sha256=raw_sha(raw), original_rectangle=original_rectangle(value.get('polygon'), rows[key]),
            original_pose_only=clean, state=value.get('state'), event_kind=value.get('event_kind')))
    for key, value in current.items():
        require(key in rows, 'Unknown current target')
        if not original_pose_only(value, rows[key]):
            protected.add(key)
    # Orphaned histories are never interpreted as virgin sources.
    protected.update(set(history)-set(current))
    require(protected <= set(rows), 'Unknown protected target/frame identity')
    frames = {rows[k]['frame_id'] for k in protected}
    return protected, frames, history


def pose_rows(path, rows):
    with closing(readonly(path, True)) as db:
        raw_current = dict(db.execute('SELECT target_key,payload FROM annotations'))
        revisions = dict(db.execute('SELECT target_key,revision FROM annotations'))
        events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        metadata = dict(db.execute('SELECT key,value FROM metadata'))
    current = {k: json.loads(raw) for k, raw in raw_current.items()}
    latest = {}
    for _, key, revision, raw in events:
        value = json.loads(raw)
        require(key in rows and value.get('target_key') == key and value.get('revision') == revision
            and all(value.get('source', {}).get(f) == rows[key][f] for f in IDENTITY),
            'Pose event relational/source identity changed')
        latest[key] = (revision, raw)
    for key, value in current.items():
        require(key in rows and value.get('target_key') == key and value.get('revision') == revisions[key]
            and all(value.get('source', {}).get(f) == rows[key][f] for f in IDENTITY)
            and latest.get(key) == (revisions[key], raw_current[key]),
            'Pose current/latest raw event identity changed')
    return current, raw_current, events, metadata


def old_request_paths(out):
    return {PROJECT/'outputs'/d/'requests.jsonl' for d in OLD_REQUEST_DIRS} | {
        p for p in (PROJECT/'outputs').glob('pallet_mask*/requests.jsonl')
        if p.parent.resolve() != out.resolve()}


def old_request_inputs(out, rows):
    mandatory = [PROJECT/'outputs'/d/'requests.jsonl' for d in OLD_REQUEST_DIRS]
    paths = old_request_paths(out)
    require(all(p.is_file() for p in mandatory), 'Original 86/latest 51 request evidence is required')
    original = [r for p in mandatory[:4] for r in read_jsonl(p)]
    latest = read_jsonl(mandatory[-1])
    require(len(indexed(original)) == 86 and len(indexed(latest)) == 51,
        'Original 86/latest 51 request scope changed')
    requests, hashes = [], {}
    for path in sorted(paths):
        hashes[str(path.relative_to(PROJECT))] = digest(path)
        for r in read_jsonl(path):
            key = r['target_key']
            require(key in rows and r['source']['frame_id'] == rows[key]['frame_id']
                and r['class_id'] == rows[key]['final_class_id'], 'Historical request source/class changed')
            requests.append(r)
    return requests, hashes


def code_inventory():
    names = ('scripts/build_mask_remaining_direct_requests.py', 'scripts/build_mask_template_requests.py',
        'pallet/mask_remaining_classes.py', 'pallet/mask_quota_saved_extension.py',
        'pallet/mask_template_transfer.py', 'pallet/mask_template_refined.py',
        'pallet/cluster_targets.py', 'pallet/pose_review_geometry.py')
    return {n: digest(PROJECT/n) for n in names}


def prepare(out, parent, per_class=10, require_seal=False):
    parent_data = load_parent(parent, require_seal)
    manifest = parent_data['manifest']
    formal, rows, evidence = load_formal_evidence(manifest)
    original_rows = indexed(read_jsonl(CLASSIFICATION))
    require(len(rows) == 12344, 'Original canonical target inventory changed')
    for p in parent_data['proposals']:
        require(p['target_key'] in rows and p['source'] == rows[p['target_key']]
            and p['class_id'] == rows[p['target_key']]['final_class_id'], 'Parent canonical source identity changed')
    formal_hashes = protected_snapshot(formal)
    require(len(formal_hashes) == 20768 and formal_hashes == read_json(parent/'protected_formal_before.json')
        == read_json(parent/'protected_formal_after.json'), 'Protected formal 20768-file inventory changed')
    configs = file_inventory(PROJECT/'configs')
    require(configs == read_json(parent/'configuration_input_hashes.json'), 'Configurations differ from frozen pub10')
    snapshot_manifest = read_json(parent/'snapshot_manifest.json')
    fingerprints, snapshots = {}, {}
    for kind, live, field in (('pose', POSE, 'snapshot_sha256'), ('gallery', GALLERY, 'gallery_snapshot_sha256')):
        spec = snapshot_manifest[kind]
        frozen, backup = member(parent, spec['path']), member(parent, spec['backup'])
        require(spec.get('immutable') is True and digest(frozen) == digest(backup) == spec['sha256'] == manifest[field]
            and not (frozen.stat().st_mode & 0o222), 'Parent immutable snapshot/backup/hash differs')
        fingerprints[kind] = database_fingerprint(frozen, True)
        current_table = 'annotations' if kind == 'pose' else 'reviews'
        count_prefix = 'snapshot' if kind == 'pose' else 'gallery_snapshot'
        require(fingerprints[kind]['table_counts'][current_table] == spec['saved'] == manifest[count_prefix+'_saved']
            and fingerprints[kind]['table_counts']['events'] == spec['events'] == manifest[count_prefix+'_events'],
            'Parent complete snapshot/current/event counts differ')
        require(database_fingerprint(live) == fingerprints[kind],
            kind+' complete live schema/metadata/current/raw events differ from frozen pub10')
        snapshots[kind] = dict(path=str(frozen.relative_to(PROJECT)), sha256=digest(frozen),
            backup=str(backup.relative_to(PROJECT)), backup_sha256=digest(backup))
    current, current_raw, events, pose_metadata = pose_rows(PROJECT/snapshots['pose']['path'], rows)
    binding = json.loads(pose_metadata['binding'])
    require(binding.get('formal_manifest_sha256') == manifest['formal_manifest_sha256']
        and binding.get('formal_annotations_sha256') == digest(formal/'annotations/frames.jsonl')
        and binding.get('formal_frames_sha256') == digest(formal/'frames.jsonl'), 'Pose formal binding differs')
    with closing(readonly(PROJECT/snapshots['gallery']['path'], True)) as db:
        gallery_keys = {k for k, in db.execute('SELECT target_key FROM reviews')} | {
            k for k, in db.execute('SELECT target_key FROM events')}
        require(dict(db.execute('SELECT key,value FROM metadata')).get('formal_manifest_sha256')
            == manifest['formal_manifest_sha256'], 'Gallery formal binding differs')
    old_requests, old_hashes = old_request_inputs(out, rows)
    quarantine = set(QUARANTINE) | set(manifest.get('preserved_quarantined_targets', []))
    protected, frames, history = protection(rows, current, events, gallery_keys,
        parent_data['proposals'], old_requests, quarantine)
    quotas = class_quotas(parent_data['proposals'], per_class)
    humans = read_jsonl(parent/'all_manual_sources.jsonl')
    human_keys = {r['target_key'] for r in humans}
    # Both pose and gallery human coverage counts were independently audited in pub10.
    human_keys.update(p['target_key'] for p in parent_data['proposals']
        if p.get('schema') == 'saved_pose_mask_gallery_entry_v1')
    with closing(readonly(PROJECT/snapshots['gallery']['path'], True)) as db:
        human_keys.update(k for k, raw in db.execute('SELECT target_key,payload FROM reviews')
            if json.loads(raw).get('human_edited') is True
            and json.loads(raw).get('state') not in ('deleted', 'deferred'))
    human_signatures = Counter(signature(rows[k]) for k in human_keys)
    human_groups = Counter((rows[k]['final_class_id'], rows[k]['group_id']) for k in human_keys)
    usable, rejections = {}, Counter()
    for key, row in sorted(rows.items()):
        if (row['final_class_id'] not in CLASSES or not quotas[row['final_class_id']]
                or key in protected or row['frame_id'] in frames):
            continue
        quality, reason = native_quality(row, evidence)
        if reason:
            rejections[f'{row["final_class_id"]}:{reason}'] += 1
            continue
        usable[key] = dict(quality=quality, no_pose_current_or_history=key not in current and key not in history,
            existing_exact_signature_human_sources=human_signatures[signature(row)],
            existing_class_group_human_sources=human_groups[row['final_class_id'], row['group_id']])
    populations = {c: dict(original_targets=sum(r['final_class_id'] == c for r in rows.values()),
        original_distinct_frames=len({r['frame_id'] for r in rows.values() if r['final_class_id'] == c}),
        usable=sum(rows[k]['final_class_id'] == c for k in usable),
        usable_virgin=sum(rows[k]['final_class_id'] == c and v['no_pose_current_or_history'] for k, v in usable.items()),
        actual_existing_human_sources=sum(rows[k]['final_class_id'] == c for k in human_keys)) for c in CLASSES}
    return dict(out=out, parent=parent_data, formal=formal, rows=rows, original_rows=original_rows,
        evidence=evidence, protected=protected,
        protected_frames=frames, history=history, current=current, current_raw=current_raw,
        quotas=quotas, usable=usable, populations=populations, rejections=dict(rejections),
        frozen=dict(parent_directory=str(parent.relative_to(PROJECT)),
            parent_generation_inventory={n: h for n, h in parent_data['inventory'].items() if n not in PARENT_AUDIT_NAMES},
            parent_existing_audit_hashes={n: h for n, h in parent_data['inventory'].items() if n in PARENT_AUDIT_NAMES},
            configuration_sha256=configs, protected_formal_files=formal_hashes,
            snapshots=snapshots, database_logical_fingerprints=fingerprints, old_request_sha256=old_hashes,
            formal_manifest_sha256=manifest['formal_manifest_sha256'], classification_sha256=manifest['classification_sha256'],
            pose_public_url=public_pose_url(), code_sha256=code_inventory(), per_class=per_class))


def public_pose_url():
    url = read_json(ACCESS)['public_url'].rstrip('/')
    parsed = urlsplit(url)
    require(parsed.scheme == 'https' and bool(parsed.netloc) and not parsed.query and not parsed.fragment
        and parsed.username is None and parsed.password is None, 'Managed public URL is not credential-free HTTPS')
    return url


def select_tasks(rows, usable, quotas):
    selected, frames = [], set()
    order = sorted(CLASSES, key=lambda c: (
        len({rows[k]['frame_id'] for k in usable if rows[k]['final_class_id'] == c}), c))
    for cls in order:
        signatures, groups, splits, policies, views = (Counter() for _ in range(5))
        for _ in range(quotas[cls]):
            options = [k for k in usable if rows[k]['final_class_id'] == cls and rows[k]['frame_id'] not in frames]
            if not options:
                break
            def rank(key):
                row, value = rows[key], usable[key]
                quality = value['quality']
                score = .65*quality['clarity_score'] + .35*min(math.log1p(quality['bbox_area_native_pixels'])/math.log(10001), 1)
                return (not value['no_pose_current_or_history'], row.get('viewpoint_candidate') in (None, 'unknown'),
                    signatures[signature(row)], value['existing_exact_signature_human_sources'],
                    groups[row['group_id']], value['existing_class_group_human_sources'], splits[row['split']],
                    policies[row['policy_id']], views[row.get('viewpoint_candidate')], -score, key)
            key = min(options, key=rank)
            row = rows[key]
            selected.append(key); frames.add(row['frame_id'])
            signatures[signature(row)] += 1; groups[row['group_id']] += 1
            splits[row['split']] += 1; policies[row['policy_id']] += 1; views[row.get('viewpoint_candidate')] += 1
    return sorted(selected, key=lambda k: (rows[k]['final_class_id'], k))


def assert_frozen(expected, actual):
    old = dict(expected); new = dict(actual)
    old_audits = old.pop('parent_existing_audit_hashes')
    new_audits = new.pop('parent_existing_audit_hashes')
    require(old == new and all(new_audits.get(n) == h for n, h in old_audits.items()),
        'Staged parent/source/configuration/code/review inputs changed; use a fresh output')


def require_unsealed(out):
    require(not any((out/n).exists() for n in
        ('manifest.json', 'requests.jsonl', 'verification.json', 'independent_verification.json', 'artifact_hashes.json')),
        'Never overwrite a sealed or partially built worklist')


def contact(root, records, name, full=False):
    require(1 <= len(records) <= 6, 'Review montages contain at most six sources')
    width, height = 680, 520 if full else 280
    sheet = Image.new('RGB', (width*2, height*math.ceil(len(records)/2)), '#111923')
    draw = ImageDraw.Draw(sheet); font = ImageFont.truetype(str(FONT), 16)
    for i, record in enumerate(records):
        x, y = i % 2*width, i//2*height
        row = record['source']
        draw.text((x+8, y+4), f'{record["request_order"]:02d} Class {row["final_class_id"]} {record["target_key"]}', font=font, fill='white')
        draw.text((x+8, y+28), f'{row["split"]} | {row["policy_id"]} | virgin={record["no_pose_current_or_history"]}', font=font, fill='white')
        with Image.open(root/record['native_image' if full else 'contact_crop']) as image:
            image = image.convert('RGB')
            if full:
                b = row['bbox']
                ImageDraw.Draw(image).rectangle((b['x'], b['y'], b['x']+b['width'], b['y']+b['height']), outline='#ffb428', width=1)
            scale = min((width-20)/image.width, (height-60)/image.height)
            image = image.resize((max(1, round(image.width*scale)), max(1, round(image.height*scale))), Image.Resampling.NEAREST)
            sheet.paste(image, (x+8, y+55))
    sheet.save(root/name)


def pose_binding(key, data):
    old = data['current'].get(key)
    return dict(existing_pose_record=old is not None, existing_pose_revision=old['revision'] if old else 0,
        existing_pose_payload_sha256=sha(old) if old else None,
        existing_pose_payload_raw_sha256=raw_sha(data['current_raw'][key]) if old else None,
        existing_pose_geometry_sha256=sha({f: old.get(f) for f in GEOMETRY}) if old else None,
        pose_history_present=bool(data['history'].get(key)), pose_history_flags=data['history'].get(key, []),
        all_pose_history_masks_unmodified=True, preserve_existing_pose_until_intentional_human_edit=True,
        preserve_current_selected_ruler_sensor_faces_and_revision=True,
        existing_pose_revision_is_expected_not_overwrite_authorization=True)


def review(out, parent=PARENT, per_class=10):
    require(not out.exists(), 'Review requires a fresh output; never overwrite inspection evidence')
    data = prepare(out, parent, per_class, False)
    keys = select_tasks(data['rows'], data['usable'], data['quotas'])
    root = out/'internal/review'
    (root/'images').mkdir(parents=True); (root/'cases').mkdir()
    private_json(out/'input_manifest.json', data['frozen'])
    records = []
    for order, key in enumerate(keys, 1):
        row = data['rows'][key]
        bounds = render_case(row, data['evidence'], root/'cases'/f'{key}.png')
        shutil.copyfile(data['formal']/row['image'], root/'images'/f'{key}.png')
        records.append(dict(target_key=key, request_order=order, source=row,
            native_image=f'images/{key}.png', contact_crop=f'cases/{key}.png', crop_native_bounds=bounds,
            native_image_sha256=digest(root/'images'/f'{key}.png'), contact_crop_sha256=digest(root/'cases'/f'{key}.png'),
            **data['usable'][key], **pose_binding(key, data)))
    private_json(root/'shortlist.jsonl', records, True)
    for start in range(0, len(records), 6):
        for full in (False, True):
            contact(root, records[start:start+6], f'{"full" if full else "crop"}_{start+1:02d}.png', full)
    private_json(root/'diagnostic.json', dict(requested_class_counts=data['quotas'], populations=data['populations'],
        quality_rejections=data['rejections'], selected=len(records),
        class_counts=dict(Counter(r['source']['final_class_id'] for r in records)),
        parent_independently_verified=bool(data['parent']['seals']), source_visual_review_performed=False))
    # Bind exactly the files to inspect, without inventing any inspection decision.
    private_json(out/'internal/review_hashes.json', file_inventory(root))
    finish_stability(data, parent)
    return dict(state='awaiting_actual_crop_and_full_image_inspection', selected=len(records), quotas=data['quotas'])


def approved_sources(out, data):
    root = out/'internal/review'
    require(file_inventory(root) == read_json(out/'internal/review_hashes.json'), 'Review images/shortlist changed')
    records = read_jsonl(root/'shortlist.jsonl')
    require([r['target_key'] for r in records] == select_tasks(data['rows'], data['usable'], data['quotas']),
        'Staged selection no longer matches frozen sources')
    decisions = read_json(out/'internal/source_visual_decisions.json')
    keys = []
    for record in records:
        key = record['target_key']; decision = decisions.get(key, {})
        if decision.get('approved') is not True:
            continue
        require(decision.get('crop_inspected') is True and decision.get('full_image_inspected') is True
            and isinstance(decision.get('note_zh'), str) and bool(decision['note_zh'].strip()),
            'Every approval requires explicit crop/full-image inspection and a nonempty note')
        require(key in data['usable'] and record['source'] == data['rows'][key]
            and all(record.get(f) == value for f, value in pose_binding(key, data).items()),
            'Approved source/expected pose revision changed')
        keys.append(key)
    require(bool(keys) or not any(data['quotas'].values()), 'No approved sources; do not publish an empty pending batch')
    return keys, decisions


def request_record(key, order, data, decision):
    row = data['rows'][key]; old = data['current'].get(key)
    source = dict(row, image_sha256=digest(data['formal']/row['image']),
        formal_manifest_sha256=data['frozen']['formal_manifest_sha256'],
        classification_sha256=data['frozen']['classification_sha256'],
        canonical_source_row_sha256=sha(row), formal_source_record_sha256=sha(data['evidence'].sources[row['frame_id']]))
    original = data['original_rows'][key]
    source['classification_row_sha256'] = sha(original)
    return dict(schema='manual_mask_template_request_v1', task_kind='remaining_direct_human_coverage',
        request_order=order, target_key=key, class_id=row['final_class_id'], source=source,
        original_classification_row=original, classification_is_candidate_not_truth=True,
        class_identity=dict(candidate_class=row.get('final_class'), final_class_id=row['final_class_id'],
            human_class_confirmed=False, classification_row_sha256=sha(original)),
        source_label_kind='ORIGINAL_RECTANGLE', original_rectangle=rectangle(row), status='needs_manual_tracing',
        activation_state='prepared_for_independent_verification_not_activated',
        reason_zh='新增不同原图的直接人工覆盖；不承诺近邻匹配或后续传播。',
        source_visual_review_zh=decision['note_zh'], source_visual_review_kind='assistant_clarity_review_not_human_class_or_GT_confirmation',
        classification_review_flags=row.get('classification_review_flags', []), **pose_binding(key, data),
        # This JSONL is PRIVATE and never an allowed public route.
        existing_pose_current_payload=old, existing_pose_current_payload_raw=data['current_raw'].get(key),
        no_pose_current_or_history=data['usable'][key]['no_pose_current_or_history'],
        existing_exact_signature_human_sources=data['usable'][key]['existing_exact_signature_human_sources'],
        image_quality=data['usable'][key]['quality'], quality_metrics_are_not_probabilities=True,
        closest_human_reference_key=None, human_reference_not_needed_for_direct_task=True,
        requested_required_vertex_count=None, topology_must_be_manually_determined=True,
        minimum_manual_template_vertices=5, minimum_direct_manual_coverage_vertices=3,
        valid_four_point_manual_contour_allowed=True, valid_four_point_manual_contour_is_not_transfer_seed=True,
        extra_vertices_must_not_be_forced=True, original_rectangle_alone_is_not_completed_manual_coverage=True,
        direct_human_coverage_requires_valid_saved_changed_contour=True, neighbor_propagation_promised=False,
        preserve_closed_real_quad_topology=True, **dict.fromkeys(FALSE_FLAGS, False),
        accepted_candidate=False, production_saved=False, counts_as_completed_annotation=False,
        counts_toward_published_coverage=False, generated_polygons=0,
        native_image=f'images/{key}.png', contact_crop=f'cases/{key}.png',
        pose_annotation_url=data['frozen']['pose_public_url']+'/?target='+key)


def page(requests, data):
    e = lambda value: html.escape(str(value), quote=True)
    parent = data['parent']['manifest']
    rows = []
    for c in CLASSES:
        v = parent['classes'][str(c)]
        rows.append(f'<tr><td>{c}</td><td>{v["total"]}</td><td>{v["saved_human"]}</td><td>{v["new_machine_pending"]}</td>'
            f'<td>{v["missing"]}</td><td>{sum(r["class_id"] == c for r in requests)}</td></tr>')
    cards = []
    for r in requests:
        key, s = r['target_key'], r['source']
        protection_text = ('无位姿当前记录及历史事件；仅提供原图与原矩形。' if r['no_pose_current_or_history'] else
            f'已有仅原矩形位姿记录，预期修订 r{r["existing_pose_revision"]}。保留位姿、标尺、传感器、选面与排除设置；预期修订不是覆盖授权。')
        hashes = (f'<details><summary>既有记录保护指纹</summary><p>预期修订 r{r["existing_pose_revision"]}<br>'
            f'当前原始载荷哈希：<code>{r["existing_pose_payload_raw_sha256"]}</code><br>'
            f'位姿／标尺／传感器／选面／排除设置哈希：<code>{r["existing_pose_geometry_sha256"]}</code></p></details>'
            if r['existing_pose_record'] else '')
        cards.append(f'<article id="{key}" data-class="{r["class_id"]}"><h2>{r["request_order"]:02d} · Class {r["class_id"]} · 待人工描绘</h2>'
            f'<p><a class="action" target="_blank" rel="noopener" href="{e(r["pose_annotation_url"])}">打开原位姿标注</a> <code>{key}</code></p>'
            f'<p>{e(protection_text)}</p>{hashes}<p>划分：{e(s["split"])} · 传感器策略：{e(s["policy_id"])}<br>'
            f'观察面：{e(s.get("viewpoint_candidate"))} · 采集组：{e(s["group_id"])}<br>图像：{e(s["frame_id"])}</p>'
            f'<figure><img loading="lazy" src="{r["contact_crop"]}" alt="原生局部及原矩形"><figcaption>橙框仅为原始矩形，不是已完成轮廓。</figcaption></figure>'
            f'<p>助手可描绘性检查：{e(r["source_visual_review_zh"])}（不是人工类别或真值确认。）</p>'
            f'<details><summary>完整原生图像</summary><img loading="lazy" src="{r["native_image"]}" alt="完整原生图像"></details>'
            '<p>沿真实可见轮廓描绘；真实闭合四点轮廓保持四点，不强加第五点或凹槽拓扑。保存前不计完成；类别可疑或遮挡严重请暂缓。</p></article>')
    return '''<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>直接人工覆盖 · 新图待描绘任务</title><style>
@font-face{font-family:RequestSans;src:url('report-font.otf')}*{box-sizing:border-box}body{font:16px/1.65 RequestSans,sans-serif;background:#111923;color:#edf2f7;max-width:1200px;margin:auto;padding:20px;min-width:0}a{color:#8ed7ff}p,code,h1,h2,td,th,summary{overflow-wrap:anywhere}article,.notice{padding:16px;border:1px solid #536479;border-radius:10px;margin:18px 0;min-width:0}img{display:block;max-width:100%;height:auto}figure{margin:12px 0}figure img{width:100%;image-rendering:pixelated}.action{display:inline-block;padding:9px;background:#17496b;border-radius:6px}table{table-layout:fixed;width:100%;border-collapse:collapse}td,th{border:1px solid #536479;padding:6px}select{font:inherit;max-width:100%;padding:6px}[hidden]{display:none!important}@media(max-width:480px){body{padding:10px;font-size:15px}article,.notice{padding:10px}td,th{font-size:12px;padding:3px}h1{font-size:23px}h2{font-size:19px}}</style></head><body>
<h1>直接人工覆盖 · 新图待描绘任务</h1><div class="notice"><strong>未激活的待办草稿；本页任务尚未保存，不是新增标签。</strong>
''' + f'<p>第十版实际累计 {parent["proposals"]} 个不同帧，其中实际保存人工 {parent["saved_human"]}，本轮新增机器候选 {parent["new_proposals"]}。原五类500份及其历史受保护、不重复请求；其余类别已有141份人工复用＋51份本轮已保存人工＝192份。机器候选不是人工完成。</p>' + '''
<p>用户声明上一批51项均已完成；数据库精确来源由第十版核验。本次目标类别4、5、7、14、22的各3份新保存轮廓为有效四点，共15份；不据此判定Class 5描错，真实外轮廓不必强画凹槽。</p>
<p>每类最多10张，且不超过第十版距100张的实际缺额；已达100的类别跳过。仅不同新图的实际保存有效人工轮廓可计直接覆盖。真实闭合四点轮廓可以计覆盖，但不是超过四点传播种子；不得凑第五点。第十版仅41份超过四点的直接人工来源可作其生成模板，不使用机器草稿递归传播。</p>
<p>本任务仅提供原生图像与原矩形，不生成掩膜或机器候选，不承诺近邻匹配或传播增长。沿用严格原生质量门槛，不跨划分／传感器／观察面借用参考。分类哈希仅绑定候选分类，不代表语义真值。</p>
<p>所有记录仍是研究草稿，未确认GT，不自动用于训练或机器人控制。不改写位姿、标尺、传感器、选面、排除设置或历史；人工需要时再主动修改并核对保存。</p></div>
<table><tr><th>类别</th><th>第十版累计</th><th>实际人工</th><th>本轮新机器</th><th>距100缺额</th><th>本批待描绘</th></tr>''' + ''.join(rows) + '</table>' + f'<p>筛选类别 <select id="request-filter"><option value="">全部</option>{"".join(f"<option>{c}</option>" for c in CLASSES)}</select> <span id="request-count">{len(requests)}</span> 张</p>' + ''.join(cards) + '''
<script>const f=document.querySelector('#request-filter');f.addEventListener('change',()=>{let n=0;for(const a of document.querySelectorAll('article')){a.hidden=!!f.value&&a.dataset.class!==f.value;if(!a.hidden)n++;}document.querySelector('#request-count').textContent=n;});</script>
<footer><p>任务数量不计入已完成覆盖；需独立验收后再决定是否上线。<a href="FONT_LICENSE.txt">字体许可</a></p></footer></body></html>'''


def build_font(out, text):
    from fontTools import subset
    from fontTools.ttLib import TTFont
    required = {ord(c) for c in text if '\u3400' <= c <= '\u9fff'}
    with TTFont(FONT) as font:
        require(required <= set(font.getBestCmap()), 'Chinese source font lacks report glyphs')
        sub = subset.Subsetter(); sub.populate(text=text); sub.subset(font); font.save(out/'report-font.otf')
    with TTFont(out/'report-font.otf') as font:
        require(required <= set(font.getBestCmap()), 'Chinese subset font lacks report glyphs')
    shutil.copyfile(PROJECT/'pallet/assets/pose_review/FONT_LICENSE.txt', out/'FONT_LICENSE.txt')


def finish_stability(data, parent):
    # Recheck complete stores, not merely the selected revisions or record counts.
    for kind, live in (('pose', POSE), ('gallery', GALLERY)):
        spec = data['frozen']['snapshots'][kind]
        require(digest(PROJECT/spec['path']) == spec['sha256']
            and digest(PROJECT/spec['backup']) == spec['backup_sha256']
            and database_fingerprint(live) == data['frozen']['database_logical_fingerprints'][kind],
            'Source database changed during stage')
    require(file_inventory(parent) == data['parent']['inventory'], 'Parent changed during stage')
    require(protected_snapshot(data['formal']) == data['frozen']['protected_formal_files']
        and file_inventory(PROJECT/'configs') == data['frozen']['configuration_sha256']
        and code_inventory() == data['frozen']['code_sha256'] and public_pose_url() == data['frozen']['pose_public_url'],
        'Formal/configuration/code/managed URL changed during stage')
    require({str(p.relative_to(PROJECT)): digest(p) for p in old_request_paths(data['out'])}
        == data['frozen']['old_request_sha256']
        and all(digest(data['formal']/n) == h for n, h in data['evidence'].used.items()),
        'Historical requests/native source images changed during stage')


def build(out, parent=PARENT):
    require_unsealed(out)
    frozen = read_json(out/'input_manifest.json')
    data = prepare(out, parent, frozen['per_class'], True)
    assert_frozen(frozen, data['frozen'])
    keys, decisions = approved_sources(out, data)
    for folder in ('images', 'cases'):
        require(not (out/folder).exists(), 'Do not overwrite partially built public assets')
        (out/folder).mkdir()
    requests = []
    for order, key in enumerate(keys, 1):
        r = request_record(key, order, data, decisions[key])
        shutil.copyfile(data['formal']/data['rows'][key]['image'], out/r['native_image'])
        r['crop_native_bounds'] = render_case(data['rows'][key], data['evidence'], out/r['contact_crop'])
        require(digest(out/r['native_image']) == r['source']['image_sha256'], 'Native image copy changed')
        requests.append(r)
    private_json(out/'requests.jsonl', requests, True)
    text = page(requests, data); (out/'report_zh.html').write_text(text)
    build_font(out, text)
    private_json(out/'internal/protected_targets.json', dict(keys=sorted(data['protected']), frames=sorted(data['protected_frames'])))
    private_json(out/'internal/class_population.json', data['populations'])
    private_json(out/'internal/used_image_hashes.json', data['evidence'].used)
    private_json(out/'internal/protected_formal_after.json', protected_snapshot(data['formal']))
    require(len(requests) == len({r['source']['frame_id'] for r in requests}), 'Direct requests repeat frames')
    counts = Counter(r['class_id'] for r in requests)
    require(all(counts[c] <= data['quotas'][c] for c in CLASSES), 'Requests exceed actual remaining gaps')
    finish_stability(data, parent)
    public_names = ['report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt'] + [r[f] for r in requests for f in ('native_image', 'contact_crop')]
    manifest = dict(schema='remaining_direct_human_coverage_requests_v1', state='prepared_for_independent_verification',
        created_at=datetime.now(timezone.utc).isoformat(), classes=list(CLASSES), target_per_class=100,
        request_count=len(requests), class_counts={c: counts[c] for c in CLASSES}, requested_class_counts=data['quotas'],
        unfilled_requested_class_counts={c: data['quotas'][c]-counts[c] for c in CLASSES},
        parent_directory=str(parent.relative_to(PROJECT)), parent_publication_version=10,
        parent_manifest_sha256=digest(parent/'manifest.json'), parent_proposals_sha256=digest(parent/'proposals.jsonl'),
        parent_verification_binding=data['parent']['seals'], parent_file_inventory=data['parent']['inventory'],
        parent_cumulative_count=len(data['parent']['proposals']), parent_class_counts=data['parent']['manifest']['classes'],
        old500_protected=True, old_manual141_preserved=True, completed_manual51_preserved=True,
        completed_manual51_statement_authority='User says all saved; pub10 exact current/event source audit is authoritative',
        source_seed_policy='Direct valid real quads count only after actual saved human edits; never >4-point seeds. No forced extra vertices, topology or neighbor propagation.',
        source_generation_policy=dict(pub10_remaining_actual_humans=192, pub10_more_than_four_point_seeds=41,
            pub10_quad_seeds=0, target_classes_latest_three_each_real_quads=True),
        formal_manifest_sha256=data['frozen']['formal_manifest_sha256'], classification_sha256=data['frozen']['classification_sha256'],
        classification_is_candidate_not_truth=True, requests_sha256=digest(out/'requests.jsonl'),
        input_manifest_sha256=digest(out/'input_manifest.json'), source_visual_decisions_sha256=digest(out/'internal/source_visual_decisions.json'),
        review_inventory_sha256=digest(out/'internal/review_hashes.json'),
        configuration_sha256=data['frozen']['configuration_sha256'], database_logical_fingerprints=data['frozen']['database_logical_fingerprints'],
        reused_parent_frozen_snapshots=data['frozen']['snapshots'], snapshots_copied_into_worklist=False,
        live_stores_match_frozen_at_start_and_finish=True, protected_formal_files_verified=20768,
        quality_gates=QUALITY_GATES, quality_helper_sha256=digest(PROJECT/'scripts/build_mask_template_requests.py'),
        source_visual_inspections_required=True, public_assets={n: digest(out/n) for n in sorted(public_names)},
        private_request_payload_never_public=True, pose_public_url=data['frozen']['pose_public_url'],
        selected_group_counts=dict(Counter(r['source']['group_id'] for r in requests)),
        selected_split_counts=dict(Counter(r['source']['split'] for r in requests)),
        selected_policy_counts=dict(Counter(r['source']['policy_id'] for r in requests)),
        production_database_writes=0, configuration_writes=0, service_writes=0, generated_polygons=0,
        completed_new_manual_labels=0, counts_as_completed_annotation=False, activated=False,
        human_confirmation_inferred=False, **dict.fromkeys(FALSE_FLAGS, False), code_sha256=digest(__file__))
    private_json(out/'manifest.json', manifest)
    private_json(out/'artifact_hashes.json', file_inventory(out))
    return dict(state=manifest['state'], request_count=len(requests), class_counts=manifest['class_counts'],
        public_assets=len(manifest['public_assets']), independent_verification_still_required=True)


def verify(out, parent=PARENT):
    """Read-only builder self-audit; deliberately produces no successful seal."""
    manifest = read_json(out/'manifest.json')
    artifacts = read_json(out/'artifact_hashes.json')
    actual = {n: h for n, h in file_inventory(out).items()
        if n != 'artifact_hashes.json' and n not in PARENT_AUDIT_NAMES}
    require(actual == artifacts, 'Worklist frozen file inventory changed')
    for name, expected in artifacts.items():
        require(digest(member(out, name)) == expected, 'Worklist frozen artifact changed: '+name)
    require(not any(p.suffix in ('.sqlite', '.sqlite3', '.db') or '.sqlite' in p.name
        for p in out.rglob('*')), 'Private databases must not be copied into a worklist')
    require(not (out/'masks').exists() and not (out/'labelme').exists(), 'Task-only worklist cannot contain masks/proposals')
    data = prepare(out, parent, read_json(out/'input_manifest.json')['per_class'], True)
    assert_frozen(read_json(out/'input_manifest.json'), data['frozen'])
    require(manifest['parent_file_inventory'] == data['parent']['inventory']
        and manifest['parent_verification_binding'] == data['parent']['seals'], 'Sealed parent inventory changed')
    keys, decisions = approved_sources(out, data)
    requests = read_jsonl(out/'requests.jsonl')
    require([r['target_key'] for r in requests] == keys and manifest['request_count'] == len(keys)
        and manifest['requests_sha256'] == digest(out/'requests.jsonl'), 'Sealed request selection/count/hash changed')
    counts = Counter(r['class_id'] for r in requests)
    require(manifest['class_counts'] == {str(c): counts[c] for c in CLASSES}
        and manifest['requested_class_counts'] == {str(c): data['quotas'][c] for c in CLASSES}
        and manifest['unfilled_requested_class_counts'] == {str(c): data['quotas'][c]-counts[c] for c in CLASSES}
        and len({r['source']['frame_id'] for r in requests}) == len(requests),
        'Sealed class quotas/global distinct frames differ')
    expected_public = {'report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt'}
    for order, r in enumerate(requests, 1):
        expected = request_record(r['target_key'], order, data, decisions[r['target_key']])
        require({k: v for k, v in r.items() if k != 'crop_native_bounds'} == expected, 'Request payload/binding changed')
        key = r['target_key']
        for f in ('native_image', 'contact_crop'):
            expected_public.add(r[f])
        require(digest(out/r['native_image']) == r['source']['image_sha256'], 'Native source bytes changed')
        # Compare with the exact reviewed crop, itself frozen before inspection.
        reviewed = out/'internal/review'/r['contact_crop']
        require(digest(out/r['contact_crop']) == digest(reviewed), 'Published crop differs from inspected rectangle-only crop')
        require(key not in data['protected'] and r['source']['frame_id'] not in data['protected_frames'], 'Protected target/frame requested')
    require(set(manifest['public_assets']) == expected_public and len(expected_public) == 3+2*len(requests), 'Public allowlist differs')
    require(all(digest(out/n) == h for n, h in manifest['public_assets'].items()), 'Public asset changed')
    text = (out/'report_zh.html').read_text()
    require(text == page(requests, data), 'Public report content changed')
    for forbidden in ('existing_pose_current_payload', 'current_payload_raw', '.sqlite', 'access_code', 'password'):
        require(forbidden not in text, 'Private payload/credential in public report')
    from fontTools.ttLib import TTFont
    with TTFont(out/'report-font.otf') as font:
        require({ord(c) for c in text if '\u3400' <= c <= '\u9fff'} <= set(font.getBestCmap()), 'Chinese report glyph missing')
    finish_stability(data, parent)
    return dict(state='self_checks_passed_not_independent_verification', request_count=len(requests),
        independent_verification_still_required=True, activated=False)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--stage', choices=('review', 'build', 'verify'), required=True)
    parser.add_argument('--output', type=Path, default=DEFAULT)
    parser.add_argument('--parent', type=Path, default=PARENT)
    parser.add_argument('--per-class', type=int, default=10)
    args = parser.parse_args()
    parent = args.parent.resolve(); out = valid_output(args.output, parent)
    require(parent.is_relative_to(PROJECT/'outputs'), 'Parent must be a local outputs generation')
    cv2.setNumThreads(1)
    result = (review(out, parent, args.per_class) if args.stage == 'review' else
        build(out, parent) if args.stage == 'build' else verify(out, parent))
    print(json.dumps(result, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
