"""Round-5 independent task/canonical routing, raw-byte, replay and seal attacks."""
import ast
from collections import Counter
import copy
import json
import sqlite3

import numpy as np
from PIL import Image
import pytest

from scripts import verify_mask_remaining_round5 as audit

FOUR = [[20, 20], [85, 21], [83, 43], [19, 42]]
CHANGED = [[21, 20], [85, 21], [83, 43], [19, 42]]
FINE = [[20, 20], [85, 20], [85, 44], [65, 44], [65, 35], [40, 35], [40, 44], [20, 44]]


def row(key='human', cls=4, frame=None):
    return dict(target_key=key, target_id=key+'::box:0000', frame_id=frame or 'frame-'+key,
        final_class_id=cls, split='train', group_id='group', box_index=0,
        bbox=dict(x=18, y=18, width=70, height=28), size=[120, 80], policy_id='camera',
        source_pcd_sha256='pcd', image='images/'+key+'.png', viewpoint_candidate='front')


def proposal(r, polygon=FOUR):
    return dict(target_key=r['target_key'], source=copy.deepcopy(r), class_id=r['final_class_id'],
        polygon=copy.deepcopy(polygon), vertex_count=len(polygon))


def value(r, polygon=FOUR, revision=1, **extra):
    return dict(target_key=r['target_key'], revision=revision, source=copy.deepcopy(r), state='draft',
        polygon=copy.deepcopy(polygon), **extra)


def event(v, event_id=None):
    return event_id or v['revision'], v['target_key'], v['revision'], json.dumps(v, indent=2, ensure_ascii=False)


def store(current=None, events=(), metadata=None):
    return dict(path=audit.Path('readonly.sqlite3'), metadata=metadata or {}, current=current or {},
        events=list(events), history_keys={e[1] for e in events})


def snapshot(path, kind, events, metadata=None):
    table = 'annotations' if kind == 'pose' else 'reviews'
    latest = {key: (revision, raw) for _, key, revision, raw in events}
    with sqlite3.connect(path) as db:
        db.executescript(f'CREATE TABLE {table}(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE metadata(key TEXT,value TEXT);')
        db.executemany(f'INSERT INTO {table} VALUES (?,?,?)', ((k, *v) for k, v in latest.items()))
        db.executemany('INSERT INTO events VALUES (?,?,?,?)', events)
        db.executemany('INSERT INTO metadata VALUES (?,?)', (metadata or {}).items())
    return audit.helpers.read_store(path, kind, frozen=True)


def write_rows(path, rows):
    path.write_text(''.join(json.dumps(v, ensure_ascii=False)+'\n' for v in rows), encoding='utf-8')


def honest_manifest():
    return dict(publication_version=15, method='original', target_per_class=100,
        generation_template_namespace=audit.NAMESPACE, production_database_writes=0,
        configuration_writes=0, service_writes=0, new_human_edits_created=0,
        thresholds_relaxed=False, forced_fill=False, recursive_candidate_propagation=False,
        automatic_human_confirmation=False, automatic_training_eligible=False,
        robot_command_eligible=False, ground_truth_eligible=False,
        original_splits_preserved=True, classification_is_candidate_not_truth=True)


def test_pinned_policy_1550_base_parent_zero_new_and_two_unreviewed_old_rectangles():
    assert audit.PUBLICATION == 15 and audit.NAMESPACE == 'remaining_round5_generation'
    assert sum(audit.BASE_COUNTS.values()) == 1550
    assert {c: audit.BASE_COUNTS[c] for c in (4, 5, 7, 14)} == {4: 55, 5: 54, 7: 54, 14: 71}
    assert len(audit.ORIGINAL_REVIEWS) == 5 and audit.PENDING_RECTANGLES == {
        '966203cd4e5ebd979e98ab2a': (4, 13), '0cb41390d16d3bcee50315e5': (7, 1)}
    assert audit.PARENT_PINS['verification.json'].startswith('a60e6c7d')
    assert audit.PARENT_PINS['new_proposals.jsonl'] == audit.hashlib.sha256(b'').hexdigest()
    assert audit.REQUEST_PINS['verification.json'].startswith('4730c01b')


@pytest.mark.parametrize('field,bad', [('publication_version', 14), ('method', 'research'), ('target_per_class', 101),
    ('generation_template_namespace', 'remaining_round4_generation'), ('production_database_writes', False),
    ('configuration_writes', 1), ('service_writes', '0'), ('new_human_edits_created', 35), ('thresholds_relaxed', True),
    ('forced_fill', True), ('recursive_candidate_propagation', True), ('automatic_human_confirmation', True),
    ('automatic_training_eligible', True), ('robot_command_eligible', True), ('ground_truth_eligible', True),
    ('human_confirmation_inferred', True), ('published', True), ('human_requests_created', 35), ('assistant_prelabels_created', 1),
    ('human_events_created', 38)])
def test_strict_manifest_rejects_writes_relaxed_gates_and_new_task_invention(field, bad):
    manifest = honest_manifest(); audit.check_claims(manifest); manifest[field] = bad
    with pytest.raises(ValueError): audit.check_claims(manifest)


@pytest.fixture
def partition():
    tasks = {f'task-{c}-{i}': dict(target_key=f'task-{c}-{i}', class_id=c, source=row(f'task-{c}-{i}', c))
        for c, n in ((4, 10), (5, 5), (7, 10), (14, 10)) for i in range(n)}
    manual = {k: proposal(v['source']) for k, v in tasks.items()}
    added = Counter(p['class_id'] for p in manual.values())
    old = {f'old-{c}-{i}': proposal(row(f'old-{c}-{i}', c)) for c, n in audit.BASE_COUNTS.items() for i in range(n-added[c])}
    for i, (key, (cls, _)) in enumerate(audit.ORIGINAL_REVIEWS.items()):
        old.pop(f'old-{cls}-{i}'); old[key] = dict(proposal(row(key, cls)), schema=audit.previous.previous.previous.REVIEW_SCHEMA)
    new = {'machine': proposal(row('machine', 19), FINE)}
    return old, tasks, manual, new, copy.deepcopy({**old, **manual, **new}), copy.deepcopy(new)


def test_partition_does_not_blindly_assert_new_zero_and_supports_empty_machine_partition(partition):
    assert audit.check_partition(*partition)['19']['proposals'] == 38
    old, tasks, manual, *_ = partition
    assert len({**old, **manual}) == 1550
    assert audit.check_partition(old, tasks, manual, {}, {**old, **manual}, {})['19']['proposals'] == 37


@pytest.mark.parametrize('attack', ['pending', 'lost_old', 'rewrite_old', 'missing_manual', 'fake_manual', 'machine_completed8',
    'accepted_manual', 'accepted_rewrite', 'frame_reuse', 'crossclass_frame', 'overquota', 'assistant_draft', 'lost_old_review'])
def test_partition_rejects_laundering_frame_reuse_quota_and_historical_mutation(partition, attack):
    old, tasks, manual, new, combined, accepted = partition
    if attack == 'pending':
        key = next(iter(audit.PENDING_RECTANGLES)); combined[key] = proposal(row(key))
    elif attack == 'lost_old': combined.pop(next(iter(old)))
    elif attack == 'rewrite_old': combined[next(iter(old))]['comment'] = 'changed'
    elif attack == 'missing_manual': manual.pop(next(iter(manual)))
    elif attack == 'fake_manual': manual['extra'] = proposal(row('extra'))
    elif attack == 'machine_completed8':
        for d in (new, combined, accepted): d['machine']['class_id'] = 8
    elif attack == 'accepted_manual':
        key = next(iter(manual)); accepted[key] = copy.deepcopy(manual[key])
    elif attack == 'accepted_rewrite': accepted['machine']['polygon'].reverse()
    elif attack == 'frame_reuse':
        for d in (new, combined, accepted): d['machine']['source']['frame_id'] = next(iter(old.values()))['source']['frame_id']
    elif attack == 'crossclass_frame':
        manual['task-5-1']['source']['frame_id'] = manual['task-7-1']['source']['frame_id']; combined['task-5-1'] = copy.deepcopy(manual['task-5-1'])
    elif attack == 'overquota':
        for i in range(100):
            key = 'extra'+str(i); new[key] = combined[key] = accepted[key] = proposal(row(key, 19), FINE)
    elif attack == 'lost_old_review':
        key = next(iter(audit.ORIGINAL_REVIEWS)); old[key].pop('schema'); combined[key] = copy.deepcopy(old[key])
    else: combined['assistant'] = proposal(row('assistant'))
    with pytest.raises(ValueError): audit.check_partition(*partition)


@pytest.fixture
def gallery(tmp_path, monkeypatch):
    pubs = {}
    roots = {n: tmp_path/n for n in ('pub13', 'pub14', 'task1', 'task2')}
    for name, path in roots.items():
        path.mkdir(); (path/'images').mkdir(); (path/'manifest.json').write_text(json.dumps(dict(name=name)))
        (path/'verification.json').write_text(json.dumps(dict(state='passed', name=name)))
        (path/'proposals.jsonl').write_text(json.dumps(dict(inventory=name))+'\n')
    monkeypatch.setattr(audit, 'PARENT', roots['pub14']); monkeypatch.setattr(audit, 'REQUESTS', roots['task2'])
    groups = []
    for key, name, version in [('oldquad', 'task1', 13), ('newquad', 'task2', 14)]:
        r = row(key); path = roots[name]; (path/'images'/f'{key}.png').write_bytes(('native-'+key).encode())
        proof = dict(author_role='AI_assistant', human_saved=False, crop_inspected=True, full_image_inspected=True,
            method='assistant_direct_native_image_contour_not_template_transfer', image_sha256=audit.digest(path/'images'/f'{key}.png'),
            polygon_sha256=audit.sha(FOUR), note_zh='逐图原生预标注')
        p = dict(proposal(r), schema='assistant_visual_preannotation_v1', origin='assistant_visual_preannotation',
            status='assistant_preannotated_draft_pending_human_review', formal_manifest_sha256='formal',
            target_expected_revision=0, target_snapshot_payload_sha256=None, preannotation=proof,
            draft_source=dict(target_key=key, origin='assistant_visual_preannotation', snapshot_revision=0, current_payload_sha256=None),
            **dict.fromkeys(('human_edited', 'human_mask_confirmed', 'human_pose_confirmed', 'ground_truth_eligible',
                'automatic_training_eligible', 'robot_command_eligible', 'production_saved', 'counts_as_completed_annotation',
                'accepted_candidate', 'propagation_seed_eligible'), False))
        task = dict(target_key=key, class_id=4, source=copy.deepcopy(r), assistant_polygon=copy.deepcopy(FOUR), preannotation=copy.deepcopy(proof),
            status='preannotated_awaiting_human_review', counts_as_completed_annotation=False,
            preserve_existing_pose_until_intentional_human_edit=True, existing_pose_revision=0, existing_pose_payload_sha256=None)
        groups.append(dict(path=path, publication=version, namespace='v1' if version == 13 else 'v2', drafts={key: p}, tasks={key: task}))
    normal = dict(proposal(row('normal', 19), FINE), template=dict(target_key='seed'))
    oldquad = dict(proposal(row('oldquad'), CHANGED), schema='saved_gallery_mask_entry_v1', human_source=dict(target_key='oldquad', gallery_review={}))
    old = {'normal': normal, 'oldquad': oldquad}
    pubs = {13: dict(path=roots['pub13'], proposals={'normal': normal}), 14: dict(path=roots['pub14'], proposals=old)}
    for p in old.values():
        for pub in pubs.values():
            if p['target_key'] in pub['proposals']: (pub['path']/'images'/f'{p["target_key"]}.png').write_bytes(('native-'+p['target_key']).encode())
    from pallet.mask_gallery_review import VERSION
    def saved(p, path, version, revision=1):
        key = p['target_key']; r = p['source']
        identity = {f: r[f] for f in ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
        identity.update(formal_manifest_sha256='formal', image_sha256=audit.digest(path/'images'/f'{key}.png'))
        v = dict(target_key=key, revision=revision, event_kind='human_mask_gallery_revision', human_edited=True, state='draft',
            pose=None, vertex_count=len(p['polygon']), polygon=copy.deepcopy(CHANGED if len(p['polygon']) == 4 else FINE),
            source=identity, source_binding=audit.sha(identity), base_proposal_id=audit.sha(p), proposal_polygon=copy.deepcopy(p['polygon']),
            proposal_publication=version, binding=audit.sha(dict(formal='formal', proposals=audit.digest(pubs[version]['path']/'proposals.jsonl'), editor_version=VERSION)),
            pose_store_written=False, original_proposal_modified=False, template_provenance=p.get('template'), human_source_provenance=p.get('human_source'),
            **dict.fromkeys(('human_mask_confirmed', 'human_pose_confirmed', 'automatic_training_eligible', 'robot_command_eligible', 'ground_truth_eligible'), False))
        if p.get('preannotation'):
            v['preannotation_provenance'] = dict(assistant_draft=copy.deepcopy(p['preannotation']), assistant_draft_not_human_edit=True,
                proposal_sha256=audit.sha(p), worklist_manifest_sha256=audit.digest(path/'manifest.json'))
        return v
    current = {'normal': saved(normal, roots['pub13'], 13)}
    for group in groups:
        key, p = next(iter(group['drafts'].items())); current[key] = saved(p, group['path'], group['publication'])
    events = [event(v, i) for i, v in enumerate(current.values(), 1)]
    rows = {k: row(k, 19 if k == 'normal' else 4) for k in current}
    return [store(current, events), store(), rows, old, pubs, groups, 'formal', 'gallery-sha'], saved


def test_both_independent_task_sources_keep_mainpub_binding_and_independent_base_hash(gallery):
    args, _ = gallery; result = audit.independent_gallery(*args)
    assert set(result) == {'oldquad', 'newquad', 'normal'}
    assert result['oldquad']['gallery_review']['proposal_publication'] == 13
    assert result['newquad']['gallery_review']['publication_proposals_sha256'] == audit.digest(args[4][14]['path']/'proposals.jsonl')
    assert args[0]['current']['newquad']['base_proposal_id'] != audit.sha(args[4][14]['proposals'])


def test_later_normal_pub14_edit_of_old37_resolves_by_publication_base_hash_key_not_key_alone(gallery):
    args, saved = gallery
    later = saved(args[3]['oldquad'], args[4][14]['path'], 14, 2)
    args[0]['events'].append(event(later, 4)); args[0]['current']['oldquad'] = later
    result = audit.independent_gallery(*args)
    assert result['oldquad']['human_polygon_revision'] == 2
    registry = audit.independent_registry(args[5])
    p, root, ref = audit.resolve_event(later, args[4], registry)
    assert p == args[3]['oldquad'] and root == args[4][14]['path'] and ref is None
    assert audit.event_identity(json.loads(args[0]['events'][1][3])) in registry
    assert audit.event_identity(later) not in registry


def test_source_inventory_v2_preserves_both_namespaces_and_sorts_full_triple(gallery, tmp_path):
    args, _ = gallery
    from pallet.mask_gallery_review import VERSION
    publications = {}
    for version, pub in args[4].items():
        proposal_sha = audit.digest(pub['path']/'proposals.jsonl')
        publications[str(version)] = dict(binding=audit.sha(dict(formal='formal', proposals=proposal_sha, editor_version=VERSION)),
            manifest_sha256=audit.digest(pub['path']/'manifest.json'), proposals_sha256=proposal_sha,
            verification_sha256=audit.digest(pub['path']/'verification.json'))
    labels = []
    for (version, base, key), reference in sorted(audit.independent_registry(args[5]).items()):
        value = args[0]['current'][key]
        labels.append(dict(target_key=key, namespace=reference['namespace'], base_proposal_id=base,
            main_gallery_binding=publications[str(version)]['binding'], main_gallery_proposals_sha256=publications[str(version)]['proposals_sha256'],
            preannotation_provenance=value['preannotation_provenance'], proposal_publication=version,
            source=value['source'], source_binding=value['source_binding'], task_manifest_sha256=audit.digest(reference['path']/'manifest.json'),
            task_verification_sha256=audit.digest(reference['path']/'verification.json')))
    output = tmp_path/'registry-check'; output.mkdir()
    inventory = dict(schema='canonical_and_independent_prelabel_event_registry_v2', current_task_namespace='v2',
        canonical_gallery_saved=1, independent_prelabel_gallery_saved=2, independent_task_registry_entries=2,
        registry_key_fields=['proposal_publication', 'base_proposal_id', 'target_key'], publications=publications, prelabels=labels)
    path = output/'source_inventory.json'; path.write_text(json.dumps(inventory))
    audit.check_source_registry(output, args[4], args[0], args[5], 'formal')
    for field, bad in [('namespace', 'v2'), ('task_verification_sha256', 'fake'), ('main_gallery_proposals_sha256', 'task-not-main')]:
        attacked = copy.deepcopy(inventory); attacked['prelabels'][0][field] = bad; path.write_text(json.dumps(attacked))
        with pytest.raises(ValueError): audit.check_source_registry(output, args[4], args[0], args[5], 'formal')


@pytest.mark.parametrize('attack', ['base_main', 'task_editor_binding', 'wrong_pub', 'bool_pub', 'foreign_image', 'foreign_source', 'wrong_split',
    'proposal_polygon', 'task_manifest', 'task_proof', 'fake_template', 'fake_pose_source', 'not_human', 'pose_event', 'fake_confirmed',
    'stale_current', 'historical_bad_base', 'duplicate_revision', 'pose_deleted', 'draft_human', 'newtask_pub13', 'oldtask_pub14', 'otherkey_hash'])
def test_all_history_events_reject_task_and_canonical_source_laundering(gallery, attack):
    args, _ = gallery; saved, pose, rows, old, pubs, groups, *_ = args
    v = saved['current']['newquad']; oldv = saved['current']['oldquad']
    if attack == 'base_main': v['base_proposal_id'] = audit.sha(old['oldquad'])
    elif attack == 'task_editor_binding': v['binding'] = audit.digest(groups[1]['path']/'proposals.jsonl')
    elif attack in ('wrong_pub', 'newtask_pub13'): v['proposal_publication'] = 13
    elif attack == 'bool_pub': v['proposal_publication'] = True
    elif attack == 'foreign_image': v['source']['image_sha256'] = 'foreign'
    elif attack == 'foreign_source': v['source']['frame_id'] = 'foreign'
    elif attack == 'wrong_split': groups[1]['drafts']['newquad']['source']['split'] = 'test'
    elif attack == 'proposal_polygon': v['proposal_polygon'] = copy.deepcopy(CHANGED)
    elif attack == 'task_manifest': v['preannotation_provenance']['worklist_manifest_sha256'] = 'other'
    elif attack == 'task_proof': v['preannotation_provenance']['assistant_draft']['note_zh'] = 'changed'
    elif attack == 'fake_template': v['template_provenance'] = dict(target_key='fake')
    elif attack == 'fake_pose_source': v['human_source_provenance'] = dict(pose_review={})
    elif attack == 'not_human': v['human_edited'] = False
    elif attack == 'pose_event': v['event_kind'] = 'human_pose_revision'
    elif attack == 'fake_confirmed': v['ground_truth_eligible'] = True
    elif attack == 'stale_current':
        later = copy.deepcopy(v); later['revision'] = 2; saved['events'].append(event(later, 4))
    elif attack == 'historical_bad_base':
        bad = json.loads(saved['events'][0][3]); bad['base_proposal_id'] = 'changed'; saved['events'][0] = event(bad, 1)
    elif attack == 'duplicate_revision': saved['events'].append(event(v, 4))
    elif attack == 'pose_deleted': pose['current']['newquad'] = value(rows['newquad'], state_override='deleted'); pose['current']['newquad']['state'] = 'deleted'
    elif attack == 'draft_human': groups[1]['drafts']['newquad']['human_edited'] = True
    elif attack == 'oldtask_pub14':
        oldv['proposal_publication'] = 14; saved['events'][1] = event(oldv, 2)
    else: v['base_proposal_id'] = oldv['base_proposal_id']
    if attack not in ('stale_current', 'historical_bad_base', 'duplicate_revision'):
        saved['events'][2] = event(v, 3)
    with pytest.raises(ValueError): audit.independent_gallery(*args)


def test_direct_human_save_keeps_actual_native_vertex_count_in_general_history(gallery):
    args, _ = gallery; v = args[0]['current']['newquad']
    v['polygon'] = [[20, 20], [85, 21], [90, 30], [83, 43], [19, 42]]; v['vertex_count'] = 5
    args[0]['events'][2] = event(v, 3)
    assert len(audit.independent_gallery(*args)['newquad']['polygon']) == 5
    # The observed round-5 source set separately proves its real 35 quads; the
    # event resolver itself does not coerce future manual edits to four points.


@pytest.fixture
def manual(gallery):
    args, _ = gallery; source = audit.independent_gallery(*args)['newquad']
    v = args[0]['current']['newquad']; raw = args[0]['events'][2][3]
    record = dict(revision=v['revision'], current_payload=v, current_payload_raw_sha256=audit.raw_sha(raw),
        human_event_id=3, human_event_sha256=audit.raw_sha(raw))
    draft, task = args[5][1]['drafts']['newquad'], args[5][1]['tasks']['newquad']
    p = dict(proposal(args[2]['newquad'], v['polygon']), schema='saved_gallery_mask_entry_v1', origin=audit.GALLERY_ORIGIN,
        status='saved_gallery_human_contour_draft_pending_confirmation', human_edited=True, saved_gallery_manual_source=True,
        human_source=audit.previous.manual_gallery_binding(args[2]['newquad'], record, 'gallery-sha',
            audit.digest(audit.PARENT/'proposals.jsonl'), audit.digest(audit.REQUESTS/'manifest.json')),
        manual_four_point_source=True, propagation_seed_eligible=False, formal_manifest_sha256='formal', class_assignments_sha256='classes',
        manual_request_sha256=audit.sha(task), metrics={}, pose_store_written=False, gallery_store_written=False,
        machine_transfer_performed=False, new_human_edit_created=False, gallery_snapshot_sha256='gallery-sha',
        rendering_id=audit.NAMESPACE+':manual_source:newquad', assistant_draft_proposal_sha256=audit.sha(draft),
        preannotation_provenance=v['preannotation_provenance'], polygon_changed_from_assistant_draft=True,
        target_expected_revision=0, target_snapshot_payload_sha256=None)
    return p, task, draft, args[2]['newquad'], source, record, 'gallery-sha', 'formal', 'classes'


def test_manual_gallery_self_source_uses_real_raw_save_never_fake_pose_event(manual):
    audit.check_manual(*manual)
    review = manual[0]['human_source']['gallery_review']
    assert review['human_event_raw_sha256'] == manual[5]['human_event_sha256']
    assert review['human_event_raw_sha256'] != audit.sha(manual[5]['current_payload'])


@pytest.mark.parametrize('field,bad', [('origin', audit.POSE_ORIGIN), ('schema', 'saved_pose_mask_gallery_entry_v1'),
    ('propagation_seed_eligible', True), ('template', {}), ('human_source', dict(pose_review={})), ('pose_store_written', True),
    ('gallery_store_written', True), ('machine_transfer_performed', True), ('new_human_edit_created', True),
    ('polygon', FOUR), ('metrics', {'ecc': 1}), ('ground_truth_eligible', True), ('vertex_count', 8),
    ('assistant_draft_proposal_sha256', 'mainpub-hash'), ('polygon_changed_from_assistant_draft', False),
    ('rendering_id', 'remaining_round4_generation:manual_source:newquad')])
def test_manual_gallery_entry_cannot_invent_pose_template_truth_or_reuse_ai_polygon(manual, field, bad):
    p = copy.deepcopy(manual[0]); p[field] = bad
    with pytest.raises(ValueError): audit.check_manual(p, *manual[1:])


@pytest.fixture
def gallery_assets(tmp_path):
    key = '0123456789abcdef01234567'; r = row(key)
    p = dict(proposal(r, CHANGED), origin=audit.GALLERY_ORIGIN,
        human_source=dict(target_key=key, origin=audit.GALLERY_ORIGIN, gallery_review=dict(target_key=key)))
    for folder, _ in audit.ASSETS: (tmp_path/folder).mkdir()
    native = np.arange(120*80, dtype=np.uint8).reshape(80, 120)
    Image.fromarray(native).save(tmp_path/'images'/f'{key}.png')
    Image.fromarray(audit.previous.raster_mask(p['polygon'], *r['size']).astype(np.uint8)*255).save(tmp_path/'masks'/f'{key}.png')
    panel = Image.new('RGB', (1480, 305), '#121923'); panel.paste(audit.previous.overlay(native, r, p['polygon'], '#55ceff'), (0, 30))
    panel.save(tmp_path/'cases'/f'{key}.png')
    flags = dict(human_confirmed=False, human_edited=True, machine_draft=False, automatic_training_eligible=False,
        robot_command_eligible=False, ground_truth_eligible=False, propagation_seed_eligible=False, pose_snapshot_saved=False)
    label = dict(imageWidth=120, imageHeight=80, imagePath=f'../images/{key}.png', imageData=None,
        mask_gallery_provenance=p, saved_gallery_source=p['human_source'], annotation_scope='single_target_mask_review_not_full_frame',
        other_objects_unchanged=True, flags=flags, shapes=[dict(points=p['polygon'], shape_type='polygon', group_id=0, label='pallet', flags=flags)])
    (tmp_path/'labelme'/f'{key}.json').write_text(json.dumps(label))
    class Native:
        def image(self, frame): return native
    return tmp_path, p, r, Native()


def test_native_mask_numbered_png_and_labelme_all_show_latest_human_not_assistant_draft(gallery_assets):
    audit.previous.check_gallery_assets(*gallery_assets)


@pytest.mark.parametrize('attack', ['ai_mask', 'ai_preview', 'ai_labelme', 'image_pixels', 'pose_claim', 'seed_claim', 'truth_claim', 'symlink'])
def test_gallery_assets_reject_stale_ai_geometry_or_fake_pose_template_truth(gallery_assets, attack):
    root, p, r, evidence = gallery_assets; key = p['target_key']
    if attack == 'ai_mask':
        Image.fromarray(audit.previous.raster_mask(FOUR, *r['size']).astype(np.uint8)*255).save(root/'masks'/f'{key}.png')
    elif attack == 'ai_preview':
        panel = Image.new('RGB', (1480, 305)); panel.paste(audit.previous.overlay(evidence.image(''), r, FOUR, '#55ceff'), (0, 30))
        panel.save(root/'cases'/f'{key}.png')
    elif attack == 'image_pixels': Image.new('L', tuple(r['size'])).save(root/'images'/f'{key}.png')
    elif attack == 'symlink':
        image = root/'images'/f'{key}.png'; target = root/'other.png'; image.rename(target); image.symlink_to(target)
    else:
        path = root/'labelme'/f'{key}.json'; label = json.loads(path.read_text())
        if attack == 'ai_labelme': label['shapes'][0]['points'] = FOUR
        else: label['flags'][{'pose_claim': 'pose_snapshot_saved', 'seed_claim': 'propagation_seed_eligible', 'truth_claim': 'human_confirmed'}[attack]] = True
        path.write_text(json.dumps(label))
    with pytest.raises((ValueError, AssertionError)): audit.previous.check_gallery_assets(*gallery_assets)


def test_traceability_64hex_raw_hashes_are_legitimate_public_metadata_not_private_raw_data():
    audit.check_public_payload(dict(human_source=dict(gallery_review=dict(current_payload_raw_sha256='a'*64,
        human_event_raw_sha256='b'*64, saved_event_payload_raw_sha256='c'*64))))


@pytest.mark.parametrize('key,bad', [('current_payload_raw', '{}'), ('human_event_payload_raw', '{}'), ('saved_event_payload_raw', '{}'),
    ('current_payload', {}), ('human_event_payload', {}), ('saved_event_payload', {}), ('human_event_raw', '{}'), ('saved_event_raw', '{}'),
    ('access_key', 'secret'), ('current_payload_raw_sha256', 'not-hex'), ('human_event_raw_sha256', 64), ('current_payload_raw_encoding', 'utf8')])
def test_public_export_rejects_raw_event_data_and_malformed_traceability_hashes(key, bad):
    with pytest.raises(ValueError): audit.check_public_payload([dict(nested={key: bad})])


@pytest.fixture
def deltas(tmp_path):
    old_pose = [event(value(row('oldpose')), 1)]
    old_gallery = [event(value(row('oldgallery'), FINE), 1)]
    tasks = {f'task-{i}': {} for i in range(35)}
    added = [event(value(row(key), FOUR), i+2) for i, key in enumerate(tasks)]
    added.extend(event(value(row(f'task-{i}'), CHANGED, 2), i+37) for i in range(3))
    before_paths = {kind: tmp_path/('before-'+kind+'.sqlite3') for kind in ('pose', 'gallery')}
    after_paths = {kind: tmp_path/('after-'+kind+'.sqlite3') for kind in ('pose', 'gallery')}
    before = {kind: snapshot(before_paths[kind], kind, events) for kind, events in (('pose', old_pose), ('gallery', old_gallery))}
    after = {kind: snapshot(after_paths[kind], kind, events) for kind, events in (('pose', old_pose), ('gallery', old_gallery+added))}
    return before_paths, after_paths, before, after, tasks, {}


def test_exact_delta35_new_keys38_events_no_parent_candidates_or_pose_saves(deltas):
    result = audit.check_store_delta(*deltas)
    assert result['pose']['new_events'] == 0 and not result['pose']['current_changed_keys']
    assert result['gallery']['new_events'] == 38 and len(result['gallery']['current_changed_keys']) == 35
    assert len(result['gallery']['new_event_raw_records']) == 38
    assert result['gallery']['new_event_counts_by_target']['task-0'] == 2
    assert result['gallery']['new_event_raw_records'][-1]['payload_raw_sha256'] == audit.raw_sha(deltas[3]['gallery']['events'][-1][3])


@pytest.mark.parametrize('attack', ['fake_parent_completed', 'pose_new_event', 'gallery_missing_event', 'gallery_foreign_event',
    'rewritten_prefix', 'equivalent_pose_bytes', 'equivalent_gallery_bytes', 'removed_current', 'latest_not_raw_equal'])
def test_exact_delta_rejects_raw_rewrites_and_unrelated_or_fabricated_completions(deltas, attack):
    before_paths, after_paths, before, after, tasks, old_new = deltas
    if attack == 'fake_parent_completed': old_new['fake'] = {}
    elif attack == 'pose_new_event': after['pose']['events'].append(event(value(row('oldpose'), FOUR, 2), 2))
    elif attack == 'gallery_missing_event': after['gallery']['events'].pop()
    elif attack == 'gallery_foreign_event': after['gallery']['events'][-1] = event(value(row('foreign'), CHANGED, 2), 39)
    elif attack == 'rewritten_prefix':
        i, k, r, raw = after['gallery']['events'][0]; after['gallery']['events'][0] = i, k, r, json.dumps(json.loads(raw))
    else:
        kind, table = ('pose', 'annotations') if attack == 'equivalent_pose_bytes' else ('gallery', 'reviews')
        with sqlite3.connect(after_paths[kind]) as db:
            if attack == 'removed_current': db.execute("DELETE FROM reviews WHERE target_key='task-0'")
            else:
                key = 'task-0' if attack == 'latest_not_raw_equal' else ('oldpose' if kind == 'pose' else 'oldgallery')
                raw = db.execute(f'SELECT payload FROM {table} WHERE target_key=?', (key,)).fetchone()[0]
                db.execute(f'UPDATE {table} SET payload=? WHERE target_key=?', (json.dumps(json.loads(raw)), key))
    with pytest.raises(ValueError): audit.check_store_delta(*deltas)


def test_raw_gallery_records_include_both_task_registries_but_only_new35_manual_subset(tmp_path):
    r = row('newquad'); v = value(r, CHANGED, 2, proposal_publication=14, base_proposal_id='actual-independent-base', note='真实保存原字节')
    path = tmp_path/'gallery.sqlite3'; current = snapshot(path, 'gallery', [event(value(r), 1), event(v, 2)])
    raw = current['events'][-1][3]; registry = {audit.event_identity(v): dict(namespace='v2')}
    record = dict(target_key='newquad', revision=2, current_payload_raw=raw, current_payload_raw_sha256=audit.raw_sha(raw),
        current_payload=v, current_payload_sha256=audit.sha(v), human_event_id=2, human_event_payload_raw=raw,
        human_event_sha256=audit.raw_sha(raw), snapshot_sha256='snapshot', history_event_ids=[1, 2],
        proposal_registry='independent_preannotation', preannotation_namespace='v2')
    output = tmp_path/'records'; output.mkdir()
    for name in ('gallery_source_records.jsonl', 'manual_source_records.jsonl'): write_rows(output/name, [record])
    assert audit.check_gallery_records(output, path, current, 'snapshot', {'newquad'}, registry) == {'newquad': record}
    for field, bad in [('human_event_sha256', audit.sha(v)), ('current_payload_raw', json.dumps(v)), ('human_event_id', 1),
            ('revision', 1), ('proposal_registry', 'canonical_publication')]:
        changed = copy.deepcopy(record); changed[field] = bad; write_rows(output/'gallery_source_records.jsonl', [changed])
        with pytest.raises(ValueError): audit.check_gallery_records(output, path, current, 'snapshot', {'newquad'}, registry)
    write_rows(output/'gallery_source_records.jsonl', [record]); write_rows(output/'manual_source_records.jsonl', [])
    with pytest.raises(ValueError): audit.check_gallery_records(output, path, current, 'snapshot', {'newquad'}, registry)


def test_only_parent679_real_human_seed_domain_no72_task_quads_even_if_saved_unchanged():
    r = row('pose', 19); v = value(r, FINE)
    recovered = audit.sources.recover_pose_sources(store({'pose': v}, [event(v)]), {'pose': r})
    galleries = {k: dict(row=row(k, c), polygon=copy.deepcopy(poly), origin=audit.GALLERY_ORIGIN)
        for k, c, poly in [('oldgallery', 19, FINE), ('old37', 4, FOUR), ('new35', 4, FOUR), ('oldfive', 1, FINE)]}
    result = audit.independent_seeds(recovered, recovered, galleries, {'pose', 'oldgallery'}, 'snapshot', {'old37', 'new35'})
    assert set(result) == {'pose', 'oldgallery'}
    galleries['unreviewed'] = dict(row=row('unreviewed', 19), polygon=FINE, origin=audit.GALLERY_ORIGIN)
    with pytest.raises(ValueError, match='unchanged parent679'):
        audit.independent_seeds(recovered, recovered, galleries, {'pose', 'oldgallery'}, 'snapshot', {'old37', 'new35'})


class Evidence:
    def patch(self, key): return key


@pytest.fixture
def attempts(monkeypatch):
    rows = {k: row(k, 19) for k in ('selected', 'unselected', 'rejected', 'low')}
    templates = {k: dict(row=row(k, 19), polygon=copy.deepcopy(FINE)) for k in ('best', 'second', 'below')}
    scores = {'best': .8, 'second': .7, 'below': .549999}
    monkeypatch.setattr(audit, 'correlate', lambda source, target: .549999 if target == 'low' else scores[source])
    metrics = dict(initial_ncc=.7, ecc=.84, boundary_ncc=.78, forward_backward_error_patch_pixels=[1.5]*8,
        boundary_guard=dict(state='passed', all_beams=[True, 2, .1]))
    calls = []
    def replay(template, target, evidence):
        source, key = template['row']['target_key'], target['target_key']; calls.append((source, key))
        if source == 'second' and key in ('selected', 'unselected'): return copy.deepcopy(FINE), copy.deepcopy(metrics)
        return None, dict(rejection='image_registration_failed', initial_ncc=scores[source])
    monkeypatch.setattr(audit, 'replay', replay)
    trials = [dict(target_key=k, class_id=19, template_key=s, metrics=replay(templates[s], rows[k], None)[1])
        for k in ('selected', 'unselected', 'rejected') for s in ('best', 'second')]
    calls.clear()
    accepted = {k: dict(proposal(rows[k], FINE), template=dict(target_key='second'), metrics=copy.deepcopy(metrics),
        continuation=dict(batch=15, template_rank=2, all_compatible_templates_considered=True, recursive_candidate_propagation=False))
        for k in ('selected', 'unselected')}
    outcomes = {k: dict(target_key=k, class_id=19, reason=reason) for k, reason in [
        ('selected', 'new_proposal'), ('unselected', 'accepted_duplicate_frame_not_selected'),
        ('rejected', 'registration_or_boundary_rejected'), ('low', 'image_similarity_below_gate')]}
    return trials, rows, templates, accepted, outcomes, Evidence(), calls


def test_replay_all_rejections_plus_accepted_unselected_not_just_selected(attempts):
    assert audit.audit_attempts(*attempts[:6]) == set(attempts[1])
    assert len(attempts[6]) == 6


@pytest.mark.parametrize('attack', ['missing_rejection', 'omit_unselected', 'wrong_order', 'wrong_rank', 'wrong_batch', 'rejected_metric',
    'unselected_metric', 'vertex_order', 'relaxed_ecc', 'relaxed_boundary', 'relaxed_fb', 'beam', 'protected_key', 'protected_frame',
    'completed8', 'missing_outcome', 'cross_split', 'policy', 'view', 'class'])
def test_replay_rejects_omissions_gate_relaxations_and_unsafe_domain(attempts, monkeypatch, attack):
    trials, rows, templates, accepted, outcomes, evidence, _ = attempts; extra = {}
    if attack == 'missing_rejection': trials.pop(4)
    elif attack == 'omit_unselected': trials[:] = [t for t in trials if t['target_key'] != 'unselected']
    elif attack == 'wrong_order': trials[0], trials[1] = trials[1], trials[0]
    elif attack == 'wrong_rank': accepted['selected']['continuation']['template_rank'] = 1
    elif attack == 'wrong_batch': accepted['selected']['continuation']['batch'] = 14
    elif attack == 'rejected_metric': trials[4]['metrics']['initial_ncc'] += .01
    elif attack == 'unselected_metric': accepted['unselected']['metrics']['ecc'] += .01
    elif attack == 'vertex_order': accepted['selected']['polygon'].reverse()
    elif attack.startswith('relaxed_') or attack == 'beam':
        field, bad = {'relaxed_ecc': ('ecc', .83999), 'relaxed_boundary': ('boundary_ncc', .77999),
            'relaxed_fb': ('forward_backward_error_patch_pixels', [1.50001]*8), 'beam': ('boundary_guard', dict(state='review_required'))}[attack]
        accepted['selected']['metrics'][field] = bad; trials[1]['metrics'][field] = bad; saved = audit.replay
        def bad_replay(template, target, evidence):
            polygon, metrics = saved(template, target, evidence)
            if polygon is not None and target['target_key'] == 'selected': metrics[field] = bad
            return polygon, metrics
        monkeypatch.setattr(audit, 'replay', bad_replay)
    elif attack == 'protected_key': extra['protected_keys'] = {'selected'}
    elif attack == 'protected_frame': extra['protected_frames'] = {'frame-selected'}
    elif attack == 'completed8':
        rows['selected']['final_class_id'] = outcomes['selected']['class_id'] = 8
        for trial in trials[:2]: trial['class_id'] = 8
    elif attack == 'missing_outcome': outcomes.pop('low')
    else: templates['second']['row'][{'cross_split': 'split', 'policy': 'policy_id', 'view': 'viewpoint_candidate', 'class': 'final_class_id'}[attack]] = 'other'
    with pytest.raises((ValueError, AssertionError)): audit.audit_attempts(trials, rows, templates, accepted, outcomes, evidence, **extra)


def test_empty_actual_attempts_still_prove_eligible_domain_not_a_blind_parent_negative(monkeypatch):
    rows = {'target': row('target', 19)}; outcomes = {'target': dict(class_id=19, reason='image_similarity_below_gate')}
    templates = {'seed': dict(row=row('seed', 19), polygon=FINE)}
    monkeypatch.setattr(audit, 'correlate', lambda *_: .549999)
    assert audit.audit_attempts([], rows, templates, {}, outcomes, Evidence()) == {'target'}
    monkeypatch.setattr(audit, 'correlate', lambda *_: .55)
    with pytest.raises(ValueError): audit.audit_attempts([], rows, templates, {}, outcomes, Evidence())


def test_original_replay_applies_beam_guard_without_reordering_or_forcing_vertices(monkeypatch):
    t, r = dict(row=row('source', 19), polygon=copy.deepcopy(FINE)), row('target', 19)
    monkeypatch.setattr(audit, 'transfer', lambda *_: (copy.deepcopy(FINE), dict(initial_ncc=.8)))
    monkeypatch.setattr(audit, 'affine_reference', lambda *_: 'reference')
    monkeypatch.setattr(audit, 'beam_boundary_audit', lambda *_: dict(state='review_required', reason='beam-order'))
    class Native(Evidence):
        def image(self, _): return 'native'
    polygon, metrics = audit.replay(t, r, Native())
    assert polygon is None and metrics['rejection'] == 'local_beam_boundary_review_required'
    monkeypatch.setattr(audit, 'beam_boundary_audit', lambda *_: dict(state='passed'))
    polygon, metrics = audit.replay(t, r, Native())
    assert polygon == FINE and metrics['boundary_guard']['state'] == 'passed'


def test_capacity_is_recomputed_from_current_global_frame_domain_not_hardcoded_class19_61(partition):
    old, _, manual, *_ = partition; base = {**old, **manual}
    candidates = {key: proposal(row(key, cls, frame), FINE) for key, cls, frame in
        [('a', 19, 'shared'), ('b', 4, 'shared'), ('c', 19, 'only19')]}
    selected, _, _ = audit.matching.diverse_matching(base, candidates)
    assert len(selected) == audit.matching.residual_maximum_size(base, candidates) == 2
    counts = Counter(p['class_id'] for p in list(base.values())+list(selected.values()))
    assert counts[19] in (38, 39) and counts[19] != 61
    assert len({p['source']['frame_id'] for p in list(base.values())+list(selected.values())}) == 1552


def test_old_task_rejections_protect_whole_frames_but_upcoming_v3_private_draft_is_not_history(tmp_path, monkeypatch):
    project = tmp_path/'project'; outputs = project/'outputs'; outputs.mkdir(parents=True)
    rows = {k: row(k) for k in ('oldtask', 'rejected1', 'rejected2', 'upcoming', *audit.PENDING_RECTANGLES)}
    roots = []
    for name, key in [('pallet_mask_preannotated_requests_v1', 'rejected1'), ('pallet_mask_preannotated_requests_v2', 'rejected2')]:
        root = outputs/name; (root/'internal').mkdir(parents=True); roots.append(root)
        (root/'internal/build_decisions.json').write_text(json.dumps(dict(decisions={key: {}})))
        (root/'internal/assistant_decisions.json').write_text(json.dumps(dict(decisions={key: {}})))
        (root/'internal/deferred_sources.json').write_text(json.dumps([dict(target_key=key)]))
        write_rows(root/'internal/shortlist.jsonl', [dict(target_key=key)])
        write_rows(root/'requests.jsonl', [dict(target_key='oldtask')])
    upcoming = outputs/'pallet_mask_preannotated_requests_v3'; upcoming.mkdir(); write_rows(upcoming/'requests.jsonl', [dict(target_key='upcoming')])
    monkeypatch.setattr(audit, 'PROJECT', project); monkeypatch.setattr(audit, 'OLD_REQUESTS', roots[0]); monkeypatch.setattr(audit, 'REQUESTS', roots[1])
    keys, frames, files = audit.historical_task_protection(rows)
    assert keys == {'oldtask', 'rejected1', 'rejected2', *audit.PENDING_RECTANGLES}
    assert rows['rejected1']['frame_id'] in frames and rows['upcoming']['frame_id'] not in frames
    assert upcoming/'requests.jsonl' not in files


@pytest.fixture
def seal_output(tmp_path, monkeypatch):
    project = tmp_path/'project'; output = project/'outputs/pallet_mask_remaining_round5_v1'
    output.mkdir(parents=True); (output/'manifest.json').write_text('{}')
    monkeypatch.setattr(audit, 'PROJECT', project)
    return output


def test_failed_reports_preserve_original_bytes_and_never_write_passed_seal(seal_output):
    def fail(_): raise ValueError('invalid independent source')
    for _ in range(2):
        prior = (seal_output/audit.REPORT).read_text() if (seal_output/audit.REPORT).exists() else None
        with pytest.raises(ValueError, match='invalid independent'): audit.seal(seal_output, fail)
        result = audit.read_json(seal_output/audit.REPORT)
        if prior is not None:
            assert result['previous_failed_validation_report_raw'] == prior
            assert result['previous_failed_validation_report_sha256'] == audit.raw_sha(prior)
    assert not (seal_output/audit.SEAL).exists()
    assert {p.name for p in seal_output.iterdir()} == {'manifest.json', audit.REPORT}


def test_success_seal_is_exclusive_and_binds_validation_report_bytes(seal_output):
    result = audit.seal(seal_output, lambda _: dict(state='passed', publication_version=15, proposals_verified=1550))
    raw = (seal_output/audit.SEAL).read_bytes()
    assert result['validation_report_sha256'] == audit.digest(seal_output/audit.REPORT)
    with pytest.raises(ValueError, match='overwrite'): audit.seal(seal_output, lambda _: dict(state='passed'))
    assert (seal_output/audit.SEAL).read_bytes() == raw


def test_unsafe_symlink_output_or_report_and_unfinished_generation_rejected(seal_output):
    link = seal_output.parent/'pallet_mask_remaining_round5_v2'; link.symlink_to(seal_output, target_is_directory=True)
    with pytest.raises(ValueError, match='Only a new'): audit.seal(link, lambda _: dict(state='passed'))
    protected = seal_output.parent/'private.json'; protected.write_text('untouched'); (seal_output/audit.REPORT).symlink_to(protected)
    with pytest.raises(ValueError, match='Unsafe'): audit.seal(seal_output, lambda _: dict(state='passed'))
    assert protected.read_text() == 'untouched'
    (seal_output/'manifest.json').unlink()
    with pytest.raises(ValueError, match='not complete'): audit.seal(seal_output, lambda _: dict(state='passed'))


def test_seal_cannot_write_outside_authorized_exact_v1_or_through_hardlinked_report(seal_output):
    other = seal_output.parent/'pallet_mask_remaining_round5_v2'; other.mkdir(); (other/'manifest.json').write_text('{}')
    with pytest.raises(ValueError, match='Only a new'): audit.seal(other, lambda _: dict(state='passed'))
    frozen = seal_output.parent/'old.json'; frozen.write_bytes(b'{"state":"failed"}\n')
    (seal_output/audit.REPORT).hardlink_to(frozen)
    with pytest.raises(ValueError, match='Unsafe'): audit.seal(seal_output, lambda _: dict(state='passed'))
    assert frozen.read_bytes() == b'{"state":"failed"}\n'


def test_preserved_failure_retains_utf8_and_crlf_bytes_exactly(seal_output):
    raw = '{"state":"failed", "error":"旧失败，不得丢弃"}\r\n'
    (seal_output/audit.REPORT).write_bytes(raw.encode('utf-8'))
    result = audit.seal(seal_output, lambda _: dict(state='passed'))
    assert result['previous_failed_validation_report_raw'] == raw
    assert result['previous_failed_validation_report_sha256'] == audit.raw_sha(raw)


def test_no_generator_helper_import_assert_polling_writer_or_old_module_mutation():
    tree = ast.parse(audit.Path(audit.__file__).read_text())
    forbidden = {'pallet.mask_remaining_round5', 'pallet.mask_gallery_saved_source', 'pallet.mask_saved_preannotations'}
    for node in ast.walk(tree):
        assert not isinstance(node, ast.Assert)
        if isinstance(node, ast.ImportFrom): assert node.module not in forbidden
        if isinstance(node, ast.Import): assert not forbidden & {n.name for n in node.names}
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            assert node.func.attr not in ('backup', 'generate', 'save_review', 'save_annotation', 'setattr', 'sleep')
        if isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
            for target in node.targets if isinstance(node, ast.Assign) else [node.target]:
                assert not (isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name)
                    and target.value.id in ('previous', 'history', 'matching', 'sources', 'helpers', 'baseline'))
