"""Isolated bulk-cap, raw-history and preservation attacks; no live editor writes."""
import ast
from collections import Counter
import copy
import json
from pathlib import Path

import pytest

from scripts import verify_mask_under50_requests_v6 as audit
from test_verify_mask_under50_requests import staged, draft, export, write, row, KEY, POLYGON
from test_verify_mask_under50_requests_v2 import catalog_case, gallery, event
from test_verify_mask_under50_requests_v5 import (
    completed_batches as fifth_completed, fresh_catalog as fifth_catalog,
    fourth_completed, fourth_catalog, previous_completed, previous_catalog,
    count_case as fifth_count_case, source_claims_case as fifth_source_claims_case,
)


def test_no_producer_builder_import_old_global_mutation_real_save_or_new_training_access():
    tree = ast.parse(Path(audit.__file__).read_text())
    for node in ast.walk(tree):
        if isinstance(node, (ast.Import, ast.ImportFrom)):
            names = [value.name for value in node.names] + [getattr(node, 'module', '') or '']
            assert not any('build_mask' in name or 'mask_review_continuation' in name for name in names)
        if isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
            targets = node.targets if isinstance(node, ast.Assign) else [node.target]
            assert not any(isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name)
                and target.value.id in ('geometry', 'continuation', 'previous', 'sources') for target in targets)
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            assert node.func.attr not in ('save_review', 'save_annotation', 'backup', 'setup')
            if isinstance(node.func.value, ast.Name) and node.func.value.id in ('geometry', 'continuation', 'previous'):
                assert node.func.attr not in ('audit', 'check_sources', 'check_count_basis', 'check_issued_inventory', 'check_staged')
    assert 'mask_model_improvement' not in Path(audit.__file__).read_text()


def test_direct_audit_refuses_unready_worklist_before_read(tmp_path, monkeypatch):
    monkeypatch.setattr(audit, 'inventory', lambda _: pytest.fail('unfinished worklist read'))
    with pytest.raises(ValueError, match='READY'):
        audit.audit(tmp_path/'not-built')


@pytest.fixture
def sealed_roots(tmp_path, monkeypatch):
    outputs = tmp_path/'outputs'; pins = {}
    for number in range(1, 6):
        namespace = f'pallet_mask_under50_requests_v{number}'; root = outputs/namespace
        write(root/'manifest.json', dict(number=number))
        for name in audit.SEALS:
            write(root/name, dict(state='passed', number=number))
        pins[namespace] = dict(manifest_sha256=audit.digest(root/'manifest.json'), verification_sha256=audit.digest(root/'verification.json'))
    monkeypatch.setattr(audit, 'SEALED_PACK_PINS', pins)
    return outputs, outputs/'pallet_mask_under50_requests_v6', pins


def test_all_five_sealed_prior_packages_registered_not_own_partial_pack(sealed_roots):
    outputs, own, pins = sealed_roots; write(own/'verification.json', {})
    assert set(audit.discover_sealed_packs(outputs, own)) == set(pins)


@pytest.mark.parametrize('attack', ['missing', 'half', 'manifest', 'twins', 'unknown', 'symlink'])
def test_old_v5_and_all_seals_require_exact_pins_not_just_passed_claim(sealed_roots, attack):
    outputs, own, _ = sealed_roots; root = outputs/'pallet_mask_under50_requests_v5'
    if attack == 'missing':
        for name in audit.SEALS:
            (root/name).unlink()
    elif attack == 'half':
        (root/'independent_verification.json').unlink()
    elif attack == 'manifest':
        write(root/'manifest.json', dict(altered=True))
    elif attack == 'twins':
        write(root/'independent_verification.json', dict(altered=True))
    elif attack == 'unknown':
        for name in audit.SEALS:
            write(outputs/'pallet_mask_under50_requests_v7'/name, dict(state='passed'))
    else:
        (root/'manifest.json').rename(root/'real_manifest.json'); (root/'manifest.json').symlink_to(root/'real_manifest.json')
    with pytest.raises(ValueError):
        audit.discover_sealed_packs(outputs, own)


@pytest.fixture
def completed_batches(fifth_completed, tmp_path):
    old_fresh, baseline, task_keys, registry, refs = fifth_completed
    namespace = 'pallet_mask_under50_requests_v5'; task_keys[namespace] = {f'{n+501:024x}' for n in range(4)}
    values = [audit.sources.loads(raw) for _, _, _, raw in old_fresh['events']]
    for key in sorted(task_keys[namespace]):
        proposal = dict(target_key=key, polygon=[[x+1, y] for x, y in POLYGON])
        value = event(key, marker='真实第五批保存')
        value.update(base_proposal_id=audit.sha(proposal), proposal_publication=15,
            human_edited=True, event_kind='human_mask_gallery_revision')
        values.append(value); registry[15, audit.sha(proposal), key] = dict(kind='independent_preannotation', namespace=namespace, proposal=proposal)
    # One legal repeated human revision: five events are still four distinct saves.
    again = copy.deepcopy(values[-1]); again['revision'] = 2
    again['polygon'] = [[x+.25, y] for x, y in POLYGON]; again['marker'] = '真实再次修改，不增distinct计数'; values.append(again)
    fresh = gallery(tmp_path/'current-v6.sqlite3', values)
    for event_id, key, _, raw in fresh['events']:
        if key in task_keys[namespace]:
            value = audit.sources.loads(raw); refs[event_id] = registry[15, value['base_proposal_id'], key]
    return fresh, baseline, task_keys, registry, refs


def test36_completed_images_are39_save_events_plus17_old_revisions_and_v5_can_repeat(completed_batches):
    extension, completed = audit.check_saved_batches(*completed_batches)
    assert list(completed.values()) == [10, 9, 7, 6, 4]
    assert extension['appended_gallery_events'] == 56 and len(extension['new_distinct_saved_keys']) == 36
    assert len(extension['changed_baseline_current_keys']) == 14 and extension['baseline_gallery_current_unchanged'] is False


@pytest.mark.parametrize('attack', ['raw_prefix', 'missing_v5', 'namespace', 'hash', 'draft_copy', 'assistant_event',
    'revision', 'route', 'old_raw_revision', 'missing_pack', 'publication', 'duplicate_key', 'decoded_raw_mismatch', 'stale_current'])
def test_repeated_saves_still_require_all_correct_triples_changed_latest_and_recoverable_raw_history(completed_batches, attack):
    fresh, baseline, tasks, registry, refs = copy.deepcopy(completed_batches)
    namespace = 'pallet_mask_under50_requests_v5'; key = sorted(tasks[namespace])[-1]
    history = fresh['histories'][key]; event_id = history[-1][0]
    if attack == 'raw_prefix':
        entry = list(fresh['events'][0]); entry[3] = json.dumps(json.loads(entry[3])); fresh['events'][0] = tuple(entry)
    elif attack == 'missing_v5':
        fresh['current'].pop(key); fresh['histories'].pop(key)
    elif attack == 'namespace':
        refs[event_id]['namespace'] = 'pallet_mask_under50_requests_v4'
    elif attack == 'hash':
        history[-1][3]['base_proposal_id'] = 'bad'
    elif attack == 'draft_copy':
        history[-1][3]['polygon'] = refs[event_id]['proposal']['polygon']; fresh['current'][key][2]['polygon'] = refs[event_id]['proposal']['polygon']
    elif attack == 'assistant_event':
        history[-1][3]['human_edited'] = False
    elif attack == 'revision':
        first = list(history[0]); first[1] = 2; history[0] = tuple(first)
    elif attack == 'route':
        refs[fresh['events'][len(baseline['events'])][0]]['namespace'] = namespace
    elif attack == 'old_raw_revision':
        old_key = next(iter(baseline['current'])); first = list(fresh['histories'][old_key][0])
        first[2] = json.dumps(json.loads(first[2])); fresh['histories'][old_key][0] = tuple(first)
    elif attack == 'missing_pack':
        tasks.pop('pallet_mask_under50_requests_v1')
    elif attack == 'publication':
        proposal_sha = history[-1][3]['base_proposal_id']; registry[14, proposal_sha, key] = registry.pop((15, proposal_sha, key))
    elif attack == 'duplicate_key':
        tasks['pallet_mask_under50_requests_v4'].add(key)
    elif attack == 'decoded_raw_mismatch':
        history[-1][3]['marker'] = 'not really in raw event'
    else:
        fresh['current'][key] = history[0][1:]
    with pytest.raises(ValueError):
        audit.check_saved_batches(fresh, baseline, tasks, registry, refs)


def test_prior_current_may_change_by_legal_append_without_losing_earlier_raw_payload(tmp_path):
    old = gallery(tmp_path/'earlier.sqlite3', [event(marker='原始当前行')])
    fresh = gallery(tmp_path/'latest.sqlite3', [event(marker='原始当前行'), event(revision=2, marker='合法后续修订')])
    result = audit.continuation.check_revision_extension(old, fresh)
    assert result['changed_baseline_current_keys'] == [KEY] and result['baseline_current_revisions_preserved_in_history']
    assert not result['baseline_gallery_current_unchanged']


@pytest.fixture
def fresh_catalog(fifth_catalog):
    case = fifth_catalog
    for n in range(1516, 1520):
        key = f'{n+1:024x}'; source = row(key); proposal = dict(target_key=key, polygon=copy.deepcopy(POLYGON))
        value = event(key, marker='第五批新来源')
        value.update(proposal_publication=15, base_proposal_id=audit.sha(proposal), proposal_polygon=copy.deepcopy(POLYGON))
        raw = json.dumps(value, ensure_ascii=False, indent=2); event_id = len(case['gallery']['events'])+1
        case['rows'][key] = source; case['gallery']['current'][key] = (1, raw, value)
        case['gallery']['histories'][key] = [(event_id, 1, raw, value)]; case['gallery']['events'].append((event_id, key, 1, raw))
        case['registry'][15, audit.sha(proposal), key] = dict(kind='independent_preannotation', namespace='pallet_mask_under50_requests_v5',
            proposal=proposal, path=Path('synthetic-v5'))
        case['catalog'].append(dict(target_key=key, row=source, polygon=copy.deepcopy(POLYGON), origin='saved_gallery', revision=1,
            payload_sha256=audit.sha(value), event_raw_sha256=audit.sources.raw_sha(raw), initial_polygon=copy.deepcopy(POLYGON),
            batch='pallet_mask_under50_requests_v5', history_revisions=1, actual_saved_research_reference_not_GT=True))
        case['event_audit'].append(dict(event_id=event_id, target_key=key, revision=1, payload_raw_sha256=audit.sources.raw_sha(raw),
            proposal_publication=15, base_proposal_id=value['base_proposal_id'], namespace='pallet_mask_under50_requests_v5', event_specific_source_verified=True))
    key = case['catalog'][-1]['target_key']; record = case['catalog'][-1]
    value = copy.deepcopy(case['gallery']['current'][key][2]); value['revision'] = 2; value['polygon'][0][0] += .25
    raw = json.dumps(value, ensure_ascii=False, indent=2); event_id = 1144
    case['gallery']['current'][key] = (2, raw, value); case['gallery']['histories'][key].append((event_id, 2, raw, value))
    case['gallery']['events'].append((event_id, key, 2, raw))
    record.update(polygon=copy.deepcopy(value['polygon']), revision=2, payload_sha256=audit.sha(value), event_raw_sha256=audit.sources.raw_sha(raw), history_revisions=2)
    case['event_audit'].append(dict(event_id=event_id, target_key=key, revision=2, payload_raw_sha256=audit.sources.raw_sha(raw),
        proposal_publication=15, base_proposal_id=value['base_proposal_id'], namespace='pallet_mask_under50_requests_v5', event_specific_source_verified=True))
    write(case['snapshot']/'private/source_catalog.jsonl', case['catalog'], True); write(case['snapshot']/'private/event_binding_audit.jsonl', case['event_audit'], True)
    return case


def check_catalog(case):
    return audit.check_fresh_catalog(case['snapshot'], case['baseline'], case['pose'], case['gallery'], case['rows'],
        case['publications'], case['registry'], 'f'*64, None)


def test1520_current_sources_and_every1144_raw_event_are_recomputed_including_repeat(fresh_catalog):
    catalog, refs = check_catalog(fresh_catalog)
    assert len(catalog) == 1520 and len(fresh_catalog['gallery']['current']) == 1038
    assert len(refs) == len(fresh_catalog['checked']) == 1144


@pytest.mark.parametrize('attack', ['polygon', 'canonical_raw_hash', 'namespace', 'duplicate_frame', 'original_box',
    'event_raw_hash', 'event_target', 'event_revision', 'event_publication', 'event_hash', 'duplicate_event_id', 'stale_current'])
def test_latest_catalog_does_not_forge_current_or_canonicalize_raw_utf8(fresh_catalog, attack):
    case = fresh_catalog; record = case['catalog'][-1]; key = record['target_key']
    if attack == 'polygon':
        record['polygon'][0][0] += 1
    elif attack == 'canonical_raw_hash':
        record['event_raw_sha256'] = audit.sha(case['gallery']['current'][key][2])
    elif attack == 'namespace':
        record['batch'] = 'pallet_mask_under50_requests_v4'
    elif attack == 'duplicate_frame':
        record['row']['frame_id'] = case['catalog'][-2]['row']['frame_id']
    elif attack == 'original_box':
        case['gallery']['current'][key][2]['polygon'] = [[20, 30], [80, 30], [80, 50], [20, 50]]
    elif attack == 'event_raw_hash':
        case['event_audit'][-1]['payload_raw_sha256'] = audit.sha(case['gallery']['current'][key][2])
    elif attack.startswith('event_'):
        entry = list(case['gallery']['events'][-1]); value = audit.sources.loads(entry[3])
        if attack == 'event_target':
            entry[1] = case['catalog'][-2]['target_key']
        elif attack == 'event_revision':
            entry[2] = 999
        else:
            value['proposal_publication' if attack == 'event_publication' else 'base_proposal_id'] = 14 if attack == 'event_publication' else 'bad'
            entry[3] = json.dumps(value)
        case['gallery']['events'][-1] = tuple(entry)
    elif attack == 'duplicate_event_id':
        entry = list(case['gallery']['events'][-1]); entry[0] = 1; case['gallery']['events'][-1] = tuple(entry)
    else:
        case['gallery']['current'][key] = case['gallery']['histories'][key][0][1:]
    write(case['snapshot']/'private/source_catalog.jsonl', case['catalog'], True); write(case['snapshot']/'private/event_binding_audit.jsonl', case['event_audit'], True)
    with pytest.raises((ValueError, AssertionError)):
        check_catalog(case)


def count_case():
    catalog, inputs, manifest = fifth_count_case()
    for cls in (11, 16):
        for n in range(2):
            key = f'{cls+600:022x}{n:02x}'; catalog.append(dict(target_key=key, row=row(key, cls)))
    counts = Counter(r['row']['final_class_id'] for r in catalog)
    inputs.update(actual_counted_saved_basis=1520, actual_class_counts={str(c): n for c, n in counts.items()})
    manifest['actual_human_saved_counts_before'] = copy.deepcopy(inputs['actual_class_counts'])
    for value in (inputs, manifest):
        value.pop('maximum_issued_per_class'); value.update(maximum_issued_by_class={str(c): n for c, n in audit.REMAINING.items()},
            consolidated_remaining_gap_batch=True)
    return catalog, inputs, manifest


def test1520_count_basis_means_bulk_remaining23_3_6_7_not_pilot2():
    counts = audit.check_count_basis(*count_case())
    assert sum(counts.values()) == 1520 and {c: 50-counts[c] for c in audit.CLASSES} == audit.REMAINING
    assert sum(audit.REMAINING.values()) == 39 and counts[17] == counts[20] == 50
    assert audit.previous.EXPECTED_COUNTS[11] == 25


@pytest.mark.parametrize('attack', ['stale1516', 'machine1550', 'scope', 'class17', 'class20', 'duplicate_frame', 'duplicate_key', 'bool_count', 'old_cap2', 'wrong_gap'])
def test_bulk_current_count_scope_and_remaining_cap_claims_fail_closed(attack):
    catalog, inputs, manifest = count_case()
    if attack == 'stale1516':
        inputs['actual_counted_saved_basis'] = 1516
    elif attack == 'machine1550':
        inputs['actual_counted_saved_basis'] = 1550
    elif attack == 'scope':
        manifest['filter_classes'].append(17)
    elif attack in ('class17', 'class20'):
        inputs['actual_class_counts']['17' if attack == 'class17' else '20'] = 49
    elif attack == 'duplicate_frame':
        catalog[-1]['row']['frame_id'] = catalog[-2]['row']['frame_id']
    elif attack == 'duplicate_key':
        catalog[-1]['target_key'] = catalog[-2]['target_key']
    elif attack == 'bool_count':
        inputs['actual_class_counts']['11'] = True
    elif attack == 'old_cap2':
        inputs['maximum_issued_per_class'] = 2
    else:
        manifest['maximum_issued_by_class']['11'] = 24
    with pytest.raises(ValueError):
        audit.check_count_basis(catalog, inputs, manifest)


def issued_case(issued=None, extras=4):
    issued = audit.REMAINING if issued is None else issued
    staged_rows, proposals, decisions = [], [], {}
    for cls, count in issued.items():
        for n in range(count+extras):
            key = f'{cls:022x}{n:02x}'; record = dict(target_key=key, class_id=cls); staged_rows.append(record)
            decisions[key] = dict(approved=n < count)
            if n < count:
                proposals.append(copy.deepcopy(record))
    counts = Counter(p['class_id'] for p in proposals)
    manifest = dict(request_count=len(proposals), class_counts={str(c): n for c, n in counts.items()}, classes=sorted(counts))
    return proposals, copy.deepcopy(proposals), staged_rows, decisions, manifest, audit.EXPECTED_COUNTS.copy()


def test_full_bulk39_and_more_than_gap_alternatives_are_legal():
    counts, withheld = audit.check_issued_inventory(*issued_case())
    assert counts == audit.REMAINING and sum(counts.values()) == 39 and len(withheld) == 16
    assert all(r['reason'] == 'explicitly_withheld_after_inspection' for r in withheld)


def test_honest_partial_delivery_need_not_fill39_or_issue_every_class():
    counts, withheld = audit.check_issued_inventory(*issued_case({11: 20, 16: 6, 19: 1}))
    assert sum(counts.values()) == 27 and 12 not in counts and len(withheld) == 12


@pytest.mark.parametrize('cls', audit.CLASSES)
def test_each_individual_class_gap_not_old_max2_is_enforced(cls):
    counts, _ = audit.check_issued_inventory(*issued_case({cls: audit.REMAINING[cls]}))
    assert counts[cls] == audit.REMAINING[cls] > 2
    with pytest.raises(ValueError, match='gap exceeded|over39'):
        audit.check_issued_inventory(*issued_case({cls: audit.REMAINING[cls]+1}))


@pytest.mark.parametrize('attack', ['class17', 'class20', 'unpaired', 'order', 'bool_approval', 'bool_count', 'foreign_key', 'approved_missing', 'duplicate', 'gap'])
def test_bulk_does_not_waive_pairing_scope_decisions_and_real_remaining_gaps(attack):
    args = list(issued_case({11: 4, 16: 3})); proposals, tasks, staged_rows, decisions, manifest, actual_counts = args
    if attack in ('class17', 'class20'):
        cls = 17 if attack == 'class17' else 20
        for record in (proposals[0], tasks[0], staged_rows[0]):
            record['class_id'] = cls
        counts = Counter(p['class_id'] for p in proposals); manifest.update(class_counts={str(c): n for c, n in counts.items()}, classes=sorted(counts))
    elif attack == 'unpaired':
        tasks.pop()
    elif attack == 'order':
        tasks.reverse()
    elif attack == 'bool_approval':
        decisions[proposals[0]['target_key']]['approved'] = 1
    elif attack == 'bool_count':
        manifest['request_count'] = True
    elif attack == 'foreign_key':
        proposals[0]['target_key'] = tasks[0]['target_key'] = 'f'*24
    elif attack == 'approved_missing':
        decisions[staged_rows[4]['target_key']]['approved'] = True
    elif attack == 'duplicate':
        proposals[1] = proposals[0]
    else:
        actual_counts[11] = 47
    with pytest.raises(ValueError):
        audit.check_issued_inventory(*args)


@pytest.fixture
def bulk_staged(staged):
    base = staged; records, decisions, rows = [], {}, {}
    image_bytes = (base['formal']/base['row']['image']).read_bytes()
    crop_bytes = (base['root']/'internal/cases'/f'{KEY}.png').read_bytes()
    image = base['evidence'].image(base['row']['frame_id'])
    class Evidence:
        def image(self, frame):
            assert frame in {r['frame_id'] for r in rows.values()}
            return image
    for n in range(40):
        key = f'{n+701:024x}'; source = row(key); record = copy.deepcopy(base['record'])
        record.update(target_key=key, source=source, request_order=n+1, native_image=f'images/{key}.png', contact_crop=f'cases/{key}.png')
        for path in (base['formal']/source['image'], base['root']/'internal'/record['native_image']):
            path.write_bytes(image_bytes+key.encode())
        (base['root']/'internal'/record['contact_crop']).write_bytes(crop_bytes)
        records.append(record); rows[key] = source; decisions[key] = dict(approved=False, full_image_inspected=True, crop_inspected=True, note_zh='真实合成拒绝理由')
    return base['root'], records, decisions, rows, base['formal'], Evidence(), dict(current={}, histories={}), set()


def test_bulk_staging40_sameclass_exceeds_old3_and_remaining23_but_keeps_all_decisions(bulk_staged):
    records, hashes = audit.check_staged(*bulk_staged)
    assert len(records) == len(hashes) == 40


@pytest.mark.parametrize('attack', ['missing_decision', 'withheld_no_crop', 'withheld_no_full', 'withheld_no_reason', 'class17', 'class20',
    'native_bytes', 'quality', 'crop', 'pose_payload', 'history_sha', 'order', 'formal_split', 'duplicate_key', 'duplicate_frame', 'duplicate_bytes', 'protected_key'])
def test_bulk_staging_does_not_waive_any_native_gate_or_withheld_inspection(bulk_staged, attack):
    args = list(bulk_staged); root, records, decisions, rows, formal, _, _, forbidden = args
    first = records[0]; key = first['target_key']
    if attack == 'missing_decision':
        decisions.pop(key)
    elif attack == 'withheld_no_crop':
        decisions[key]['crop_inspected'] = False
    elif attack == 'withheld_no_full':
        decisions[key]['full_image_inspected'] = False
    elif attack == 'withheld_no_reason':
        decisions[key]['note_zh'] = ''
    elif attack in ('class17', 'class20'):
        first['class_id'] = 17 if attack == 'class17' else 20
    elif attack == 'native_bytes':
        (root/'internal'/first['native_image']).write_bytes(b'wrong')
    elif attack == 'quality':
        first['image_quality']['clarity_score'] += .1
    elif attack == 'crop':
        from PIL import Image
        Image.new('RGB', (2, 2)).save(root/'internal'/first['contact_crop'])
    elif attack == 'pose_payload':
        first['existing_pose_current_payload'] = {'pose': 'not really preserved'}
    elif attack == 'history_sha':
        first['existing_pose_history_sha256'] = 'bad'
    elif attack == 'order':
        first['request_order'] = 2
    elif attack == 'formal_split':
        first['source'] = dict(first['source'], split='test')
    elif attack == 'duplicate_key':
        records.append(first)
    elif attack == 'duplicate_frame':
        rows[records[1]['target_key']]['frame_id'] = rows[key]['frame_id']
    elif attack == 'duplicate_bytes':
        other = records[1]
        (formal/other['source']['image']).write_bytes((formal/first['source']['image']).read_bytes())
    else:
        forbidden.add(key)
    with pytest.raises(ValueError):
        audit.check_staged(*args)


@pytest.fixture
def registry_case(tmp_path, monkeypatch):
    rows, pack_roots = {}, {}; formal = tmp_path/'formal'; formal.mkdir()
    publication = dict(manifest_sha256='parent-manifest', proposals_sha256='parent-proposals', verification_sha256='parent-seal')
    for version, (namespace, pins) in enumerate(audit.SEALED_PACK_PINS.items(), 1):
        root = tmp_path/'outputs'/namespace; pack_roots[namespace] = root; records, proposals, decisions = [], [], {}
        for n in range(pins['staged_count']):
            key = f'{version+800:022x}{n:02x}'; source = row(key); rows[key] = source
            path = formal/source['image']; path.parent.mkdir(exist_ok=True); path.write_bytes(key.encode())
            records.append(dict(target_key=key, source=source, existing_pose_current_payload=None,
                existing_pose_revision=0, existing_pose_payload_sha256=None))
            decisions[key] = dict(approved=n < pins['request_count'])
            if n < pins['request_count']:
                proposals.append(dict(target_key=key, source=source, polygon=copy.deepcopy(POLYGON)))
        write(root/'internal/shortlist.jsonl', records, True); write(root/'internal/build_decisions.json', dict(decisions=decisions))
        write(root/'proposals.jsonl', proposals, True); write(root/'requests.jsonl', [dict(target_key=p['target_key']) for p in proposals], True)
        inputs = dict(shortlist_sha256=audit.digest(root/'internal/shortlist.jsonl'), shortlisted_classes=list(pins['classes']),
            actual_class_counts={str(c): 10 for c in pins['classes']}); write(root/'input_manifest.json', inputs)
        manifest = dict(request_count=len(proposals), filter_classes=list(pins['classes']), public_assets={}, formal_manifest_sha256='formal',
            **{'parent_'+field: value for field, value in publication.items()})
        for field, name in (('proposals_sha256', 'proposals.jsonl'), ('requests_sha256', 'requests.jsonl'),
                ('input_manifest_sha256', 'input_manifest.json'), ('decisions_sha256', 'internal/build_decisions.json')):
            manifest[field] = audit.digest(root/name)
        write(root/'manifest.json', manifest)
        seal = {field: manifest[field] for field in ('proposals_sha256', 'requests_sha256', 'formal_manifest_sha256', 'public_assets')}
        seal.update(state='passed', preannotations_verified=True, manifest_sha256=pins['manifest_sha256'], request_count=len(proposals), shortlist_sha256=inputs['shortlist_sha256'])
        write(root/'verification.json', seal)
    monkeypatch.setattr(audit.previous, 'check_public', lambda *args: None)
    monkeypatch.setattr(audit.geometry, 'check_draft', lambda *args: None); monkeypatch.setattr(audit.geometry, 'check_exports', lambda *args: None)
    class Evidence:
        def __init__(self):
            self.formal = formal
        def image(self, frame):
            return None
    return pack_roots, rows, 'formal', dict(current={}), publication, {}, Evidence()


def test_exact77_prior_inspections_and36_task_triples_are_registered(registry_case):
    registry, inspected, tasks = audit.extend_registry(*registry_case)
    assert len(registry) == 36 and len(inspected) == 77 and list(map(len, tasks.values())) == [10, 9, 7, 6, 4]


@pytest.mark.parametrize('attack', ['repeat_frame', 'repeat_bytes', 'wrong_scope', 'lost_reject', 'changed_shortlist'])
def test_all77_including41_old_withheld_and_pack_local_scopes_are_protected(registry_case, attack):
    packs, rows, *_ = registry_case; root = packs['pallet_mask_under50_requests_v5']; records = audit.read_jsonl(root/'internal/shortlist.jsonl')
    if attack in ('repeat_frame', 'repeat_bytes'):
        keys = list(rows); old, latest = rows[keys[0]], rows[keys[-1]]
        if attack == 'repeat_frame':
            latest['frame_id'] = old['frame_id']
        else:
            formal = registry_case[-1].formal; (formal/latest['image']).write_bytes((formal/old['image']).read_bytes())
    elif attack == 'wrong_scope':
        manifest = audit.read(root/'manifest.json'); manifest['filter_classes'].append(17); write(root/'manifest.json', manifest)
    elif attack == 'lost_reject':
        records.pop(); write(root/'internal/shortlist.jsonl', records, True)
    else:
        records[-1]['new_field'] = 'tampered'; write(root/'internal/shortlist.jsonl', records, True)
    with pytest.raises(ValueError):
        audit.extend_registry(*registry_case)


def test_all_historical_requests_and_shortlists_exclude77_prior_staged_not_own(tmp_path):
    own = tmp_path/'outputs/pallet_mask_under50_requests_v6'; rows = {}
    for version, pins in enumerate(audit.SEALED_PACK_PINS.values(), 1):
        records = []
        for n in range(pins['staged_count']):
            key = f'{version:022x}{n:02x}'; rows[key] = row(key); records.append(dict(target_key=key, approved=False))
        root = tmp_path/f'outputs/pallet_mask_under50_requests_v{version}'
        write(root/'internal/shortlist.jsonl', records, True); write(root/'requests.jsonl', records[:pins['request_count']], True)
    write(own/'internal/shortlist.jsonl', [dict(target_key='f'*24)], True)
    hashes, keys = audit.geometry.prior_inspections(tmp_path, own, rows)
    assert len(keys) == 77 and len(hashes) == 10


def test_capacity_claim_is_numeric_upper_bound_not_semantic_or_issued_truth(bulk_staged):
    _, records, _, rows, formal, evidence, _, forbidden = bulk_staged
    capacity = audit.numerical_capacity(rows, formal, evidence, forbidden)
    assert capacity == {11: 40, 12: 0, 16: 0, 19: 0}
    # Byte/frame global dedup and known protected images reduce capacity.
    forbidden.add(records[0]['target_key'])
    assert audit.numerical_capacity(rows, formal, evidence, forbidden)[11] == 39


def test_shared_crossclass_frame_capacity_is_sorted_key_global_not_input_iteration_order(bulk_staged):
    _, records, _, rows, formal, evidence, _, forbidden = bulk_staged
    first, second = records[:2]; first_row, second_row = rows[first['target_key']], rows[second['target_key']]
    first_row['final_class_id'] = 12; second_row.update(final_class_id=19, frame_id=first_row['frame_id'], image=first_row['image'])
    subset = {second['target_key']: second_row, first['target_key']: first_row}
    assert audit.numerical_capacity(subset, formal, evidence, forbidden) == {11: 0, 12: 1, 16: 0, 19: 0}
    assert audit.numerical_capacity(dict(reversed(list(subset.items()))), formal, evidence, forbidden) == {11: 0, 12: 1, 16: 0, 19: 0}


def bulk_claims_case():
    capacity = {11: 117, 12: 786, 16: 108, 19: 3}; issued = Counter({11: 20, 16: 6, 19: 1})
    staged_rows = [dict(class_id=c) for c, n in {11: 29, 12: 36, 16: 9, 19: 3}.items() for _ in range(n)]
    inputs = dict(stage_budget_by_class={'11': 29, '12': 36, '16': 9, '19': 7},
        unprotected_native_quality_capacity_by_class={str(c): n for c, n in capacity.items()})
    manifest = dict(unprotected_native_quality_capacity_by_class=copy.deepcopy(inputs['unprotected_native_quality_capacity_by_class']),
        remaining_gap_after_all_drafts_saved={str(c): audit.REMAINING[c]-issued[c] for c in audit.CLASSES})
    return inputs, manifest, staged_rows, issued, capacity


def test_bulk_stage_budgets_and_conditional_gap_are_not_false_current_completion_counts():
    args = bulk_claims_case(); audit.check_bulk_claims(*args)
    assert sum(len([r for r in args[2] if r['class_id'] == c]) for c in audit.CLASSES) == 77
    assert audit.REMAINING[19]-args[-1][19] == 4  # at least four lack fresh native sources


def test_final_staging_budget_can_extend_before_sealing_without_old_pilot_or_initial_budget_cap():
    capacity, issued = {11: 117, 12: 785, 16: 108, 19: 4}, Counter({11: 23, 12: 3, 16: 6})
    staged_rows = [dict(class_id=c) for c, n in {11: 29, 12: 45, 16: 18, 19: 4}.items() for _ in range(n)]
    inputs = dict(stage_budget_by_class={'11': 29, '12': 45, '16': 18, '19': 7},
        unprotected_native_quality_capacity_by_class={str(c): n for c, n in capacity.items()})
    manifest = dict(unprotected_native_quality_capacity_by_class=dict(inputs['unprotected_native_quality_capacity_by_class']),
        remaining_gap_after_all_drafts_saved={str(c): audit.REMAINING[c]-issued[c] for c in audit.CLASSES})
    audit.check_bulk_claims(inputs, manifest, staged_rows, issued, capacity)
    assert len(staged_rows) == 96 and manifest['remaining_gap_after_all_drafts_saved'] == {'11': 0, '12': 0, '16': 0, '19': 7}


@pytest.fixture
def staging_chain(tmp_path):
    root = tmp_path/'pack'; records = [dict(target_key=f'{n:024x}', class_id=11) for n in range(2)]
    inputs = dict(source_audit_sha256='source-pin', actual_counted_saved_basis=1520, configuration_sha256={'config': 'frozen'},
        stage_budget_by_class={'11': 2, '12': 1, '16': 1, '19': 1}, continuation_builder_sha256='earliest-unsealed-code')
    write(root/'internal/shortlist.jsonl', records, True); inputs['shortlist_sha256'] = audit.digest(root/'internal/shortlist.jsonl')
    write(root/'input_manifest.json', inputs)
    for cls in (16, 12):
        backup = root/f'internal/staging_before_class{cls}_extension'
        for name, source in (('input_manifest.json', root/'input_manifest.json'), ('shortlist.jsonl', root/'internal/shortlist.jsonl')):
            (backup/name).parent.mkdir(parents=True, exist_ok=True); (backup/name).write_bytes(source.read_bytes())
        hashes = audit.inventory(backup); records += [dict(target_key=f'{cls:022x}{n:02x}', class_id=cls) for n in range(2)]
        write(root/'internal/shortlist.jsonl', records, True); inputs['shortlist_sha256'] = audit.digest(root/'internal/shortlist.jsonl')
        inputs['stage_budget_by_class'][str(cls)] += 2
        inputs.update(continuation_builder_sha256=f'newer-unsealed-code{cls}', earlier_staging_preserved_sha256=hashes, extension_reason='More alternatives; no gate relaxation')
        if cls == 12:  # Earlier interface lacked a list, but its hash-chain remains.
            inputs['staging_extensions'] = [dict(class_id=12, count=2, earlier_receipt_directory=str(backup.relative_to(root)), earlier_receipt_sha256=hashes)]
        write(root/'input_manifest.json', inputs)
    return root, inputs, records


def test_staging_extensions_preserve_recursive_earlier_receipts_without_requiring_new_interface_on_old_input(staging_chain):
    chain = audit.check_staging_extensions(*staging_chain)
    assert [(r['class_id'], r['added_sources']) for r in chain] == [(16, 2), (12, 2)]
    assert chain[0]['historical_unsealed_builder_sha256'] == 'earliest-unsealed-code'


@pytest.mark.parametrize('attack', ['missing_receipt', 'changed_early_bytes', 'lost_old_prefix', 'changed_source_basis', 'wrong_budget',
    'wrong_explicit_count', 'wrong_explicit_hash', 'missing_early_link', 'wrong_added_class', 'extra_receipt'])
def test_staging_extension_chain_rejects_lost_or_rewritten_evidence_and_false_append_claims(staging_chain, attack):
    root, inputs, records = staging_chain
    if attack == 'missing_receipt':
        (root/'internal/staging_before_class16_extension/shortlist.jsonl').unlink()
    elif attack == 'changed_early_bytes':
        path = root/'internal/staging_before_class16_extension/input_manifest.json'; path.write_bytes(path.read_bytes()+b'\n')
    elif attack == 'lost_old_prefix':
        records[0]['target_key'] = 'f'*24; write(root/'internal/shortlist.jsonl', records, True)
    elif attack == 'changed_source_basis':
        inputs['actual_counted_saved_basis'] = 1550
    elif attack == 'wrong_budget':
        inputs['stage_budget_by_class']['12'] += 1
    elif attack == 'wrong_explicit_count':
        inputs['staging_extensions'][0]['count'] = 100
    elif attack == 'wrong_explicit_hash':
        inputs['staging_extensions'][0]['earlier_receipt_sha256']['shortlist.jsonl'] = 'bad'
    elif attack == 'missing_early_link':
        inputs.pop('earlier_staging_preserved_sha256')
    elif attack == 'wrong_added_class':
        records[-1]['class_id'] = 19; write(root/'internal/shortlist.jsonl', records, True)
    else:
        backup = root/'internal/staging_before_class19_extension'
        for name in ('input_manifest.json', 'shortlist.jsonl'):
            (backup/name).parent.mkdir(parents=True, exist_ok=True)
            (backup/name).write_bytes((root/'internal/staging_before_class16_extension'/name).read_bytes())
    with pytest.raises(ValueError):
        audit.check_staging_extensions(root, inputs, records)


@pytest.mark.parametrize('attack', ['fake_capacity', 'fake_conditional_gap', 'over_budget', 'bool_budget', 'missing_class'])
def test_bulk_capacity_shortfalls_must_be_honest_without_claiming_exhaustive_visual_search(attack):
    args = bulk_claims_case(); inputs, manifest, *_ = args
    if attack == 'fake_capacity':
        manifest['unprotected_native_quality_capacity_by_class']['19'] = 7
    elif attack == 'fake_conditional_gap':
        manifest['remaining_gap_after_all_drafts_saved']['19'] = 0
    elif attack == 'over_budget':
        inputs['stage_budget_by_class']['11'] = 20
    elif attack == 'bool_budget':
        inputs['stage_budget_by_class']['11'] = True
    else:
        inputs['stage_budget_by_class'].pop('19')
    with pytest.raises(ValueError):
        audit.check_bulk_claims(*args)


@pytest.fixture
def public_pack(staged, monkeypatch):
    import fontTools.ttLib
    root = staged['root']; proposal = export(staged)
    (root/'FONT_LICENSE.txt').write_bytes((audit.PROJECT/'pallet/assets/pose_review/FONT_LICENSE.txt').read_bytes()); (root/'report-font.otf').write_bytes(b'synthetic font')
    page = ('少于50 · 真实人工轮廓 · 剩余补样合并复核 · 已预标注 · 未保存不计完成 · '
        '不自动获得GT、传播、训练或机器人资格 · 有效四点保持四点 · 不强迫增点 · val/test保持原划分 · '
        '不是严格配准通过或真实人工保存 · 不凑数\n'
        + '、'.join(f'Class {c}：{audit.EXPECTED_COUNTS[c]}份' for c in audit.CLASSES)
        + '<select id="request-filter"><option value="">全部</option>'
        + ''.join(f'<option value="{c}">{c}</option>' for c in audit.CLASSES) + '</select>'
        + f'<article id="target-{KEY}" data-class="11"><img src="cases/{KEY}.png"><a href="images/{KEY}.png">原图</a>'
        + f'<a href="masks/{KEY}.png">掩膜</a><a href="labelme/{KEY}.json">LabelMe</a></article>')
    (root/'report_zh.html').write_text(page); glyphs = {ord(c) for c in page}
    class Font:
        def __init__(self, path):
            assert path == root/'report-font.otf'
        def __enter__(self):
            return self
        def __exit__(self, *args):
            return False
        def getBestCmap(self):
            return {value: str(value) for value in glyphs}
    monkeypatch.setattr(fontTools.ttLib, 'TTFont', Font)
    manifest = dict(public_assets={name: audit.digest(root/name) for name in ('report_zh.html', 'report-font.otf', 'FONT_LICENSE.txt',
        *(f'{folder}/{KEY}.{ext}' for folder, ext in (('images', 'png'), ('cases', 'png'), ('masks', 'png'), ('labelme', 'json'))))})
    return root, manifest, [proposal], glyphs


def test_bulk_public_scope_and_font_allowlist_are_current_not_pilot(public_pack):
    root, manifest, proposals, _ = public_pack; assert audit.check_public(root, manifest, proposals) == set(manifest['public_assets'])


@pytest.mark.parametrize('attack', ['class17count', 'class20filter', 'private_link', 'wrong_count', 'old_max2', 'accuracy', 'eligibility', 'font'])
def test_public_false_scope_private_link_or_missing_limitations_rejected(public_pack, attack):
    root, manifest, proposals, glyphs = public_pack; page = (root/'report_zh.html').read_text()
    if attack == 'class17count':
        page += 'Class 17：50份'
    elif attack == 'class20filter':
        page = page.replace('</select>', '<option value="20">20</option></select>')
    elif attack == 'private_link':
        page += '<a href="internal/shortlist.jsonl">private</a>'
    elif attack == 'wrong_count':
        page = page.replace('Class 11：27份', 'Class 11：25份')
    elif attack == 'old_max2':
        page += '每类最多2张'
    elif attack == 'accuracy':
        page = page.replace('不是严格配准通过或真实人工保存', '准确率已通过')
    elif attack == 'eligibility':
        page = page.replace('不自动获得GT、传播、训练或机器人资格', '自动获得GT')
    else:
        glyphs.remove(ord('少'))
    (root/'report_zh.html').write_text(page); manifest['public_assets']['report_zh.html'] = audit.digest(root/'report_zh.html')
    with pytest.raises(ValueError):
        audit.check_public(root, manifest, proposals)


def source_binding_case(tmp_path, monkeypatch):
    monkeypatch.setattr(audit, 'PROJECT', tmp_path); snapshot = tmp_path/'outputs/pallet_mask_continuation_sources_v10'
    write(snapshot/'audit.json', dict(source='ready synthetic v10')); write(snapshot/'private/source_catalog.jsonl', [], True)
    value = audit.digest(snapshot/'audit.json'); monkeypatch.setattr(audit, 'SOURCE_AUDIT_SHA256', value)
    inputs = dict(source_audit_sha256=value, continuation_source_audit_sha256=value,
        continuation_source_directory='outputs/pallet_mask_continuation_sources_v10', continuation_source_catalog_sha256=audit.digest(snapshot/'private/source_catalog.jsonl'),
        excluded_classes_already_at_50=[17, 20], previous_batch_directory='outputs/pallet_mask_under50_requests_v5', previous_batch_actual_completed=4,
        previous_batch_manifest_sha256=audit.SEALED_PACK_PINS['pallet_mask_under50_requests_v5']['manifest_sha256'], source_snapshot_never_added_to_frozen_training=True)
    manifest = dict(source_audit_sha256=value, continuation_source_audit_sha256=value, excluded_classes_already_at_50=[17, 20], previous_batch_actual_completed=4)
    return inputs, manifest, snapshot


def test_explicit_v10_catalog_audit_and_v5_manifest_bindings(tmp_path, monkeypatch):
    audit.check_source_binding(*source_binding_case(tmp_path, monkeypatch))


@pytest.mark.parametrize('field', ['source_audit_sha256', 'continuation_source_audit_sha256', 'continuation_source_directory',
    'continuation_source_catalog_sha256', 'excluded_classes_already_at_50', 'previous_batch_directory',
    'previous_batch_actual_completed', 'previous_batch_manifest_sha256', 'source_snapshot_never_added_to_frozen_training'])
def test_stale_snapshot_or_four_v5_saved_claims_fail(tmp_path, monkeypatch, field):
    inputs, manifest, snapshot = source_binding_case(tmp_path, monkeypatch); inputs[field] = audit.geometry.QUALITY_SEAL_SHA256
    with pytest.raises(ValueError):
        audit.check_source_binding(inputs, manifest, snapshot)


def source_claims_case():
    summary, catalog, pose, gallery_store, extension, manifests, trained = fifth_source_claims_case()
    catalog.extend(dict(row=dict(final_class_id=11)) for _ in range(4)); extension['new_distinct_saved_keys'] = [f'{n:024x}' for n in range(36)]
    manifests['pallet_mask_under50_requests_v5'] = 'h'
    summary.update(total_contours=1520, gallery_contours=1038, gallery_events=1144, class_counts={'11': 1520},
        new_distinct_saved_keys=copy.deepcopy(extension['new_distinct_saved_keys']), source_registry_task_manifests=copy.deepcopy(manifests))
    return summary, catalog, pose, gallery_store, extension, manifests, trained


def test_source_summary_is_recomputed_not_producer_truth():
    audit.check_source_claims(*source_claims_case())


@pytest.mark.parametrize('attack', ['old1516', 'old1139', 'not36', 'training_changed', 'no_v5', 'accuracy', 'bool_writes'])
def test_false_source_summary_or_training_claims_are_rejected(attack):
    args = source_claims_case(); summary = args[0]
    if attack == 'old1516':
        summary['total_contours'] = 1516
    elif attack == 'old1139':
        summary['gallery_events'] = 1139
    elif attack == 'not36':
        summary['new_distinct_saved_keys'].pop()
    elif attack == 'training_changed':
        summary['frozen_training_dataset_modified'] = True
    elif attack == 'no_v5':
        summary['source_registry_task_manifests'].pop('pallet_mask_under50_requests_v5')
    elif attack == 'accuracy':
        summary['state'] = 'passed_accuracy'
    else:
        summary['production_annotation_writes'] = False
    with pytest.raises(ValueError):
        audit.check_source_claims(*args)


def test_four_point_contours_and_falseflags_still_use_independent_schema_and_raster(staged):
    proposal, task = draft(staged); image_sha = audit.digest(staged['formal']/staged['row']['image'])
    audit.geometry.check_draft(proposal, task, staged['record'], staged['decision'], 'e'*64, 'f'*64, image_sha)
    audit.geometry.check_exports(staged['root'], export(staged), staged['record'], image_sha)
    assert len(proposal['polygon']) == 4 and all(proposal[f] is False for f in audit.geometry.FALSE_FLAGS)


@pytest.mark.parametrize('split', ['val', 'test'])
def test_class19_split_cannot_be_promoted_to_train(staged, split):
    staged['row'].update(final_class_id=19, split=split); proposal, task = draft(staged); proposal['class_id'] = task['class_id'] = 19
    image_sha = audit.digest(staged['formal']/staged['row']['image'])
    audit.geometry.check_draft(proposal, task, staged['record'], staged['decision'], 'e'*64, 'f'*64, image_sha)
    proposal['source']['split'] = 'train'
    with pytest.raises(ValueError):
        audit.geometry.check_draft(proposal, task, staged['record'], staged['decision'], 'e'*64, 'f'*64, image_sha)


def test_old_model_pins_read_only_and_ignore_authorized_new_output(tmp_path, monkeypatch):
    old = tmp_path/'old-model'; path = old/'m1/best.pt'; path.parent.mkdir(parents=True); path.write_bytes(b'frozen')
    monkeypatch.setattr(audit, 'OLD_MODEL_PINS', {'m1/best.pt': audit.digest(path)})
    new = tmp_path/'separate-new-research-version'; new.mkdir(); (new/'checkpoint.pt').write_bytes(b'new training in progress')
    assert audit.check_old_models(old) == audit.OLD_MODEL_PINS
    (new/'checkpoint.pt').write_bytes(b'new epoch does not change frozen model')
    assert audit.check_old_models(old) == audit.OLD_MODEL_PINS
    path.write_bytes(b'illegally rewritten')
    with pytest.raises(ValueError):
        audit.check_old_models(old)


@pytest.fixture
def seal_root(tmp_path, monkeypatch):
    monkeypatch.setattr(audit, 'PROJECT', tmp_path); root = tmp_path/'outputs/pallet_mask_under50_requests_v6'; write(root/'manifest.json', {}); return root


def test_no_ready_means_no_audit_no_seals(seal_root):
    with pytest.raises(ValueError, match='READY'):
        audit.seal(seal_root, auditor=lambda _: pytest.fail('unready staging audited'))
    assert set(audit.inventory(seal_root)) == {'manifest.json'}


def test_passing_ready_audit_creates_identical_private_exclusive_twins(seal_root):
    result = dict(state='passed', request_count=30, semantic_contour_accuracy_established=False)
    audit.seal(seal_root, producer_complete=True, auditor=lambda _: result)
    assert (seal_root/audit.SEALS[0]).read_bytes() == (seal_root/audit.SEALS[1]).read_bytes() == audit.canonical(result)+b'\n'
    assert all((seal_root/name).stat().st_mode & 0o777 == 0o600 for name in audit.SEALS)
    with pytest.raises(ValueError, match='Never overwrite'):
        audit.seal(seal_root, producer_complete=True, auditor=lambda _: result)


@pytest.mark.parametrize('state', ['failed', 'pending', 'passed_provenance_only'])
def test_nonpassing_audit_never_creates_seals(seal_root, state):
    with pytest.raises(ValueError, match='did not pass'):
        audit.seal(seal_root, producer_complete=True, auditor=lambda _: dict(state=state))
    assert set(audit.inventory(seal_root)) == {'manifest.json'}


def test_second_seal_race_preserves_other_writer_and_only_rolls_back_own_file(seal_root):
    def raced(root):
        (root/'independent_verification.json').write_bytes(b'other writer'); return dict(state='passed')
    with pytest.raises(FileExistsError):
        audit.seal(seal_root, producer_complete=True, auditor=raced)
    assert not (seal_root/'verification.json').exists() and (seal_root/'independent_verification.json').read_bytes() == b'other writer'


def test_old_pack_and_symlink_alias_cannot_receive_v6_seals(seal_root):
    old = seal_root.parent/'pallet_mask_under50_requests_v5'; write(old/'manifest.json', {})
    alias = seal_root.parent/'alias'; alias.symlink_to(seal_root, target_is_directory=True)
    for root in (old, alias):
        with pytest.raises(ValueError, match='exact new v6'):
            audit.seal(root, producer_complete=True, auditor=lambda _: dict(state='passed'))
