"""Independent publication-11 audit attacks, without generation or live writes."""
import ast
from collections import Counter
import copy
import hashlib
import json
import sqlite3

import pytest

from scripts import verify_mask_remaining_continue as audit


FOUR = [[20, 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=3, 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 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 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 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 proposal(r, polygon=FOUR):
    return dict(target_key=r['target_key'], source=r, class_id=r['final_class_id'], polygon=copy.deepcopy(polygon), vertex_count=len(polygon))


def write_json(path, value):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(audit.canonical(value)+b'\n')


def write_jsonl(path, values):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b''.join(audit.canonical(v)+b'\n' for v in values))


def honest_manifest():
    return dict(publication_version=11, 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_new_publication_keeps22_classes_and_does_not_regenerate_old5_or_completed6_and13():
    assert audit.CLASSES == tuple(range(1, 23))
    assert set(audit.REMAINING) | set(audit.OLD_CLASSES) == set(audit.CLASSES)
    assert set(audit.COMPLETED) == set(audit.OLD_CLASSES) | {6, 13}
    assert audit.NAMESPACE == 'remaining_continue_generation'


@pytest.mark.parametrize('field,bad', [('publication_version', 10), ('method', 'research'),
    ('target_per_class', 90), ('generation_template_namespace', 'remaining_expand_generation'),
    ('production_database_writes', False), ('configuration_writes', 1), ('service_writes', '0'),
    ('new_human_edits_created', 1), ('thresholds_relaxed', True), ('forced_fill', True), ('duplicate_fill', True),
    ('recursive_candidate_propagation', True), ('automatic_human_confirmation', True),
    ('automatic_training_eligible', True), ('robot_command_eligible', True), ('ground_truth_eligible', True),
    ('research_candidates_added', 1), ('cross_split_template_transfers', 1), ('human_requests_created', 50),
    ('original_splits_preserved', False), ('classification_is_candidate_not_truth', False),
    ('review_dispositions_are_saved_human_masks', True), ('review_dispositions_seed_authorization', True),
    ('human_confirmation_inferred', True), ('activated', True), ('published', True), ('all_classes_complete', True)])
def test_claims_fail_closed_without_truthy_zero_or_inferred_confirmation(field, bad):
    manifest = honest_manifest()
    audit.check_claims(manifest)
    manifest[field] = bad
    with pytest.raises(ValueError):
        audit.check_claims(manifest)


def test_aliases_accept_actual_schema_but_not_conflicting_or_boolean_counts():
    audit.claim({'review_only_original_rectangle': 2}, ('original_rectangle_review_only', 'review_only_original_rectangle'), 2)
    audit.claim({}, 'not_in_actual_schema', 0, required=False)
    with pytest.raises(ValueError, match='Conflicting'):
        audit.claim({'a': 2, 'b': 50}, ('a', 'b'), 2)
    with pytest.raises(ValueError):
        audit.claim({'n': False}, 'n', 0)


@pytest.fixture
def partition():
    counts = dict.fromkeys(audit.COMPLETED, 100)
    counts.update({3: 90, 4: 60, 5: 60, 7: 60, 8: 91})
    old = {f'old-{c}-{i}': proposal(row(f'old-{c}-{i}', c)) for c, n in counts.items() for i in range(n)}
    requests = {f'request-{c}-{i}': dict(target_key=f'request-{c}-{i}', class_id=c)
        for c in (4, 5, 7, 14, 22) for i in range(10)}
    for key, (cls, _) in audit.ORIGINAL_REVIEWS.items():
        requests.pop(f'request-{cls}-0')
        requests[key] = dict(target_key=key, class_id=cls)
    manual = {k: proposal(row(k, r['class_id'])) for k, r in requests.items()}
    for key in audit.ORIGINAL_REVIEWS:
        manual[key]['schema'] = audit.REVIEW_SCHEMA
    new = {'machine': proposal(row('machine', 3), FINE)}
    combined = {**old, **manual, **new}
    return old, requests, manual, new, combined, copy.deepcopy(new)


def test_exact1061_plus50_including_two_separate_reviews_plus_machine_partition(partition):
    counts = audit.check_partition(*partition)
    assert len(partition[4]) == 1112
    assert sum(c['proposals'] for c in counts.values()) == 1112


@pytest.mark.parametrize('attack', ['lost_old', 'rewrite_old', 'missing_request', 'extra_manual',
    'original_as_human', 'extra_original', 'machine_in_old_class', 'machine_in_filled6', 'machine_in_filled13',
    'machine_as_manual', 'accepted_rewrite', 'crossclass_frame', 'new_reuses_locked_frame', 'overquota'])
def test_partition_cannot_substitute_doublecount_or_launder_original_rectangles(partition, attack):
    old, requests, manual, new, combined, accepted = partition
    if attack == 'lost_old':
        combined.pop(next(iter(old)))
    elif attack == 'rewrite_old':
        key = next(iter(old)); combined[key] = dict(old[key], new_field='rebind')
    elif attack == 'missing_request':
        manual.pop(next(iter(requests)))
    elif attack == 'extra_manual':
        manual['extra'] = proposal(row('extra'))
    elif attack == 'original_as_human':
        manual[next(iter(audit.ORIGINAL_REVIEWS))]['schema'] = 'saved_pose_mask_gallery_entry_v1'
    elif attack == 'extra_original':
        manual['request-4-1']['schema'] = audit.REVIEW_SCHEMA
    elif attack.startswith('machine_in_'):
        cls = dict(machine_in_old_class=1, machine_in_filled6=6, machine_in_filled13=13)[attack]
        for inventory in (new, combined, accepted):
            inventory['machine']['class_id'] = cls
    elif attack == 'machine_as_manual':
        accepted[next(iter(manual))] = next(iter(manual.values()))
    elif attack == 'accepted_rewrite':
        accepted['machine']['polygon'][0][0] += 1
    elif attack == 'crossclass_frame':
        manual['request-4-1']['source']['frame_id'] = manual['request-5-1']['source']['frame_id']
    elif attack == 'new_reuses_locked_frame':
        new['machine']['source']['frame_id'] = next(iter(old.values()))['source']['frame_id']
        accepted['machine'] = copy.deepcopy(new['machine'])
    else:
        for i in range(11):
            key = 'extra-'+str(i); p = proposal(row(key, 3), FINE)
            new[key] = combined[key] = accepted[key] = p
    with pytest.raises(ValueError):
        audit.check_partition(*partition)


@pytest.fixture
def human():
    r = row()
    v = value(r)
    pose = store({'human': v}, [event(v)])
    recovered = audit.sources.recover_pose_sources(pose, {'human': r})
    return r, pose, recovered


def test_seed_inventory_counts_old_remaining_pose_and_saved_gallery_only_not_new_quads_or_old5(human):
    _, _, recovered = human
    fine = dict(recovered['human'], row=row('old-fine'), polygon=copy.deepcopy(FINE), vertex_count=8)
    recovered['old-fine'] = fine
    recovered['new-quad'] = dict(recovered['human'], row=row('new-quad', 4))
    gallery = {'reviewed': dict(row=row('reviewed'), polygon=FINE, origin=audit.GALLERY_ORIGIN)}
    seeds = audit.independent_seeds({'old-fine': {}, 'human': {}}, recovered, gallery, {'reviewed': {}}, 'snapshot')
    assert set(seeds) == {'old-fine', 'reviewed'}
    assert not {'human', 'new-quad'} & set(seeds)
    gallery['reviewed']['row']['final_class_id'] = 1
    with pytest.raises(ValueError, match='old-five'):
        audit.independent_seeds({'old-fine': {}}, recovered, gallery, {'reviewed': {}}, 'snapshot')


def test_raw_event_sha_is_exact_utf8_and_normalization_is_not_a_new_human_source(human):
    r, pose, _ = human
    raw = pose['events'][0][3]
    current = dict(pose['current']['human'], revision=2, event_kind='unit_normalization')
    pose['current']['human'] = current
    pose['events'].append(event(current))
    recovered = audit.sources.recover_pose_sources(pose, {'human': r})['human']
    assert recovered['human_polygon_revision'] == 1 and recovered['current_revision'] == 2
    assert recovered['human_event_sha256'] == hashlib.sha256(raw.encode()).hexdigest()
    assert recovered['human_event_sha256'] != audit.sha(json.loads(raw))


@pytest.fixture
def completed(human):
    r, _, recovered = human
    source = recovered['human']
    request = dict(target_key='human', class_id=3, source=copy.deepcopy(r), pose_annotation_url='https://pose.example/?target=human')
    p = dict(proposal(r), schema='saved_pose_mask_gallery_entry_v1', origin=audit.POSE_ORIGIN,
        status='human_edited_draft_pending_confirmation', human_edited=True, manual_four_point_source=True,
        propagation_seed_eligible=False, pose_store_written=False, gallery_store_written=False,
        machine_transfer_performed=False, new_human_edit_created=False, snapshot_sha256='snapshot',
        formal_manifest_sha256='formal', class_assignments_sha256='classes', target_expected_revision=1,
        target_snapshot_payload_sha256=source['current_payload_sha256'], metrics={},
        human_source=audit.template_binding(audit.previous.pose_template(source, 'snapshot')),
        rendering_id=audit.NAMESPACE+':manual_source:human', manual_request_sha256=audit.sha(request), original_pose_url=request['pose_annotation_url'])
    return p, request, r, recovered, 'snapshot', 'formal', 'classes'


def test_changed_quad_counts_as_human_coverage_but_never_a_propagation_template(completed):
    audit.check_manual(*completed)


@pytest.mark.parametrize('attack', ['vertex', 'seed', 'human_event_hash', 'current_hash', 'revision',
    'template', 'request_hash', 'split', 'policy', 'view', 'class', 'confirmed', 'write', 'metric'])
def test_real_quads_cannot_gain_vertices_seed_rights_or_false_source_binding(completed, attack):
    p, request, _, _, *_ = completed
    if attack == 'vertex':
        p['polygon'].append([30, 30])
    elif attack == 'seed':
        p['propagation_seed_eligible'] = True
    elif attack == 'human_event_hash':
        p['human_source']['human_event_sha256'] = 'canonical-not-raw'
    elif attack == 'current_hash':
        p['target_snapshot_payload_sha256'] = 'stale'
    elif attack == 'revision':
        p['target_expected_revision'] = 0
    elif attack == 'template':
        p['template'] = p['human_source']
    elif attack == 'request_hash':
        p['manual_request_sha256'] = 'fake'
    elif attack in ('split', 'policy', 'view'):
        request['source'][dict(split='split', policy='policy_id', view='viewpoint_candidate')[attack]] = 'other'
    elif attack == 'class':
        p['class_id'] = 1
    elif attack == 'confirmed':
        p['ground_truth_eligible'] = True
    elif attack == 'write':
        p['pose_store_written'] = True
    else:
        p['metrics']['ecc'] = 1
    with pytest.raises(ValueError):
        audit.check_manual(*completed)


@pytest.fixture
def original(tmp_path):
    key = next(iter(audit.ORIGINAL_REVIEWS))
    cls, revision = audit.ORIGINAL_REVIEWS[key]
    r = row(key, cls)
    v = value(r, audit.sources.rectangle(r), revision, binding=audit.sha({'formal_manifest_sha256': 'formal'}))
    pose = store({key: v}, [event(v, 7)], metadata={'binding': json.dumps({'formal_manifest_sha256': 'formal'})})
    path = tmp_path/'pose.sqlite3'
    with sqlite3.connect(path) as db:
        db.executescript('CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        raw = json.dumps(v, indent=2, ensure_ascii=False)
        db.execute('INSERT INTO annotations VALUES (?,?,?)', (key, revision, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (7, key, revision, raw))
    source = audit.original_review_source(key, pose, r, path)
    source['user_statement_sha256'] = 'validated-scoped-statement'
    request = dict(target_key=key, class_id=cls, source=copy.deepcopy(r), pose_annotation_url='https://pose.example/?target='+key)
    provenance = dict(target_key=key, snapshot_sha256='snapshot', revision=revision,
        payload_sha256=source['current_payload_sha256'], saved_review_revision=revision,
        saved_review_event_id=7, saved_review_event_sha256=source['saved_review_event_sha256'],
        polygon_sha256=audit.sha(source['polygon']), user_statement_sha256=source['user_statement_sha256'])
    binding = dict(target_key=key, class_id=cls, split=r['split'], snapshot_revision=revision,
        current_payload_sha256=source['current_payload_sha256'], origin=audit.REVIEW_SCHEMA,
        vertex_count=4, human_edited=False, saved_pose_review_source=True, user_reviewed_no_change=True,
        original_rectangle_review_only=True, propagation_seed_eligible=False, pose_review=provenance)
    p = dict(proposal(r, source['polygon']), schema=audit.REVIEW_SCHEMA, origin=audit.REVIEW_SCHEMA,
        human_edited=False, propagation_seed_eligible=False, original_rectangle_review_only=True,
        source_label_kind='REVIEWED_ORIGINAL_RECTANGLE', status='user_reviewed_original_rectangle_pending_confirmation',
        saved_pose_review_source=True, user_reviewed_no_change=True,
        pose_store_written=False, gallery_store_written=False, machine_transfer_performed=False,
        new_human_edit_created=False, snapshot_sha256='snapshot', target_expected_revision=revision,
        target_snapshot_payload_sha256=source['current_payload_sha256'], formal_manifest_sha256='formal',
        class_assignments_sha256='classes', manual_request_sha256=audit.sha(request), original_pose_url=request['pose_annotation_url'],
        metrics={}, human_source=binding, rendering_id=audit.NAMESPACE+':review_only:'+key)
    return p, request, source, 'snapshot', 'formal', 'classes', pose, r, path


def test_original_rectangle_real_save_is_audited_but_not_claimed_as_an_edit(original):
    audit.check_original_review(*original[:6])
    source = original[2]
    assert source['saved_review_event_id'] == 7 and source['human_edited'] is False
    assert source['saved_review_event_sha256'] == hashlib.sha256(source['saved_review_event_payload_raw'].encode()).hexdigest()
    assert 'human_polygon_revision' not in source
    assert audit.sources.recover_pose_sources(original[6], {original[7]['target_key']: original[7]}) == {}


@pytest.mark.parametrize('attack', ['schema', 'origin', 'edited', 'seed', 'template', 'rectangle', 'event', 'current', 'confirmed', 'metric'])
def test_original_rectangle_cannot_be_laundered_into_a_human_edit_or_seed(original, attack):
    p = original[0]
    if attack == 'schema':
        p['schema'] = 'saved_pose_mask_gallery_entry_v1'
    elif attack == 'origin':
        p['origin'] = audit.POSE_ORIGIN
    elif attack == 'edited':
        p['human_edited'] = True
    elif attack == 'seed':
        p['propagation_seed_eligible'] = True
    elif attack == 'template':
        p['template'] = p['human_source']
    elif attack == 'rectangle':
        p['polygon'][0][0] += .1
    elif attack == 'event':
        p['human_source']['pose_review']['saved_review_event_sha256'] = 'fake'
    elif attack == 'current':
        p['target_snapshot_payload_sha256'] = 'fake'
    elif attack == 'confirmed':
        p['human_mask_confirmed'] = True
    else:
        p['metrics']['ecc'] = 1
    with pytest.raises(ValueError):
        audit.check_original_review(*original[:6])


@pytest.mark.parametrize('state', ['confirmed', 'deferred', 'deleted'])
def test_original_review_cannot_hide_a_protected_historical_state(original, state):
    p, _, _, _, _, _, pose, r, path = original
    old = value(r, audit.sources.rectangle(r), 1, binding=pose['current'][r['target_key']]['binding'])
    old['state'] = state
    pose['events'].insert(0, event(old))
    with pytest.raises(ValueError, match='history'):
        audit.original_review_source(r['target_key'], pose, r, path)


def test_user_statement_is_verbatim_and_does_not_authorize_other_targets():
    audit.check_statement({'user_statement': audit.USER_STATEMENT})
    with pytest.raises(ValueError):
        audit.check_statement({'user_statement': audit.USER_STATEMENT.replace('269', '369')})
    with pytest.raises(ValueError):
        audit.check_statement({'user_statement': audit.USER_STATEMENT, 'human_confirmation_inferred': True})


def test_original_rectangle_assets_render_their_own_source_and_deny_human_edited_flag(original, tmp_path):
    p, _, _, _, _, _, _, r, _ = original
    image = audit.np.zeros((r['size'][1], r['size'][0]), dtype=audit.np.uint8)
    for name in ('images', 'cases', 'masks', 'labelme'):
        (tmp_path/name).mkdir()
    key = p['target_key']
    audit.Image.fromarray(image).save(tmp_path/'images'/f'{key}.png')
    audit.Image.fromarray(image).save(tmp_path/'cases'/f'{key}.png')
    audit.Image.fromarray(audit.raster_mask(p['polygon'], *r['size']).astype(audit.np.uint8)*255).save(tmp_path/'masks'/f'{key}.png')
    flags = dict(human_edited=False, machine_draft=False, original_rectangle_review_only=True)
    label = dict(imageWidth=r['size'][0], imageHeight=r['size'][1], imagePath=f'../images/{key}.png',
        imageData=None, mask_template_provenance=p, flags=flags,
        shapes=[dict(points=p['polygon'], shape_type='polygon', group_id=r['box_index'], label='pallet', flags=copy.deepcopy(flags))])
    path = tmp_path/'labelme'/f'{key}.json'; write_json(path, label)
    class NativeEvidence:
        def image(self, frame):
            return image
    audit.check_original_assets(tmp_path, p, r, NativeEvidence())
    label['flags']['human_edited'] = True; write_json(path, label)
    with pytest.raises(ValueError, match='human_edited'):
        audit.check_original_assets(tmp_path, p, r, NativeEvidence())


@pytest.fixture
def review_bundle(original, tmp_path, monkeypatch):
    p, request, source, pose_sha, *_ = original
    key = p['target_key']
    monkeypatch.setattr(audit, 'ORIGINAL_REVIEWS', {key: audit.ORIGINAL_REVIEWS[key]})
    statement = dict(schema='explicit_user_review_no_change_statement_v1', statement_zh=audit.USER_STATEMENT,
        scoped_request_keys=[key], reviewed_original_rectangle_keys=[key], candidate_count_in_statement=269,
        actual_parent_batch_saved_count=369, old_42_unsaved_not_inferred_reviewed=True,
        creates_human_event=False, confirms_ground_truth=False, authorizes_propagation=False)
    p['original_box_review_basis'] = 'review_completion_statement.json'
    p['human_source']['pose_review']['user_statement_sha256'] = audit.sha(statement)
    write_json(tmp_path/'review_completion_statement.json', statement)
    write_jsonl(tmp_path/'requests.jsonl', [request])
    fields = ('target_key', 'current_revision', 'current_payload_sha256', 'current_payload_raw',
        'current_payload', 'current_payload_raw_sha256', 'saved_review_event_id', 'saved_review_revision',
        'saved_review_event_sha256', 'saved_review_event_payload_raw', 'saved_review_event_payload', 'history_event_ids')
    record = {f: source[f] for f in fields}
    record.update(schema='reviewed_original_rectangle_source_freeze_v1', source=source['row'],
        source_identity_sha256=audit.sha(source['row']), snapshot_sha256=pose_sha,
        polygon_sha256=audit.sha(source['polygon']), propagation_seed_eligible=False, human_edited=False,
        original_rectangle_review_only=True, saved_pose_review_source=True, user_reviewed_no_change=True,
        user_statement_sha256=audit.sha(statement), classification_is_candidate_not_truth=True)
    record.update(dict.fromkeys(audit.baseline.previous.FALSE_FLAGS, False))
    path = tmp_path/'reviewed_original_rectangle_source_records.jsonl'
    write_jsonl(path, [record]); path.chmod(0o600)
    return original, tmp_path, path, record, statement


def test_actual_reviewed_original_rectangle_raw_record_filename_and_saved_review_metadata(review_bundle):
    original, out, _, _, statement = review_bundle
    p, _, source, pose_sha, _, _, _, _, snapshot = original
    records = audit.check_review_records(out, {p['target_key']: source}, snapshot, pose_sha)
    audit.check_original_review(*original[:6])
    assert audit.check_review_statement(out, {p['target_key']: p}, records) == statement
    assert not any(name.startswith('human_event') or name == 'human_polygon_revision' for name in records[p['target_key']])


@pytest.mark.parametrize('attack', ['event_hash', 'raw_whitespace', 'current_raw_whitespace', 'source',
    'history_ids', 'statement_hash', 'seed', 'edited', 'fake_human_revision', 'fake_human_event'])
def test_actual_original_review_raw_record_still_fails_on_any_byte_binding_or_fake_human_event(review_bundle, attack):
    original, out, path, record, _ = review_bundle
    if attack == 'event_hash':
        record['saved_review_event_sha256'] = 'fake'
    elif attack == 'raw_whitespace':
        record['saved_review_event_payload_raw'] += '\n'
    elif attack == 'current_raw_whitespace':
        record['current_payload_raw'] += '\n'
    elif attack == 'source':
        record['source_identity_sha256'] = 'other'
    elif attack == 'history_ids':
        record['history_event_ids'] = [8]
    elif attack == 'statement_hash':
        record['user_statement_sha256'] = 'hash-of-unscoped-text-not-statement'
    elif attack == 'seed':
        record['propagation_seed_eligible'] = True
    elif attack == 'edited':
        record['human_edited'] = True
    elif attack == 'fake_human_revision':
        record['human_polygon_revision'] = record['saved_review_revision']
    else:
        record['human_event_id'] = record['saved_review_event_id']
    write_jsonl(path, [record])
    p, _, source, pose_sha, _, _, _, _, snapshot = original
    with pytest.raises(ValueError, match='Exact raw SQLite'):
        audit.check_review_records(out, {p['target_key']: source}, snapshot, pose_sha)


@pytest.mark.parametrize('field,bad', [('statement_zh', '全部都已确认真值'),
    ('scoped_request_keys', ['other']), ('reviewed_original_rectangle_keys', ['other']),
    ('candidate_count_in_statement', 369), ('actual_parent_batch_saved_count', 269),
    ('old_42_unsaved_not_inferred_reviewed', False), ('creates_human_event', True),
    ('confirms_ground_truth', True), ('authorizes_propagation', True)])
def test_actual_statement_object_is_verbatim_exactly_scoped_and_never_creates_events_or_seed_rights(review_bundle, field, bad):
    original, out, _, record, statement = review_bundle
    statement[field] = bad
    write_json(out/'review_completion_statement.json', statement)
    p = original[0]
    p['human_source']['pose_review']['user_statement_sha256'] = audit.sha(statement)
    record['user_statement_sha256'] = audit.sha(statement)
    with pytest.raises(ValueError):
        audit.check_review_statement(out, {p['target_key']: p}, {p['target_key']: record})


@pytest.fixture
def gallery(tmp_path):
    from pallet.mask_gallery_review import VERSION as EDITOR_VERSION
    r = row('gallery', 3)
    p = dict(proposal(r, FINE), template={'target_key': 'old-human'})
    root = tmp_path/'publication'; (root/'images').mkdir(parents=True)
    (root/'images/gallery.png').write_bytes(b'image bytes')
    write_jsonl(root/'proposals.jsonl', [p])
    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(root/'images/gallery.png'))
    binding = audit.sha(dict(formal='formal', proposals=audit.digest(root/'proposals.jsonl'), editor_version=EDITOR_VERSION))
    v = dict(target_key='gallery', revision=1, source=identity, source_binding=audit.sha(identity),
        proposal_publication=10, base_proposal_id=audit.sha(p), proposal_polygon=copy.deepcopy(FINE),
        template_provenance=p['template'], binding=binding, event_kind='human_mask_gallery_revision',
        human_edited=True, pose=None, polygon=copy.deepcopy(FINE), vertex_count=8)
    return store({'gallery': v}, [event(v)]), store(), {'gallery': r}, {'gallery': p}, {10: dict(path=root, proposals={'gallery': p})}, 'formal', 'gallery-snapshot'


def test_saved_gallery_polygon_equal_to_machine_proposal_is_valid_reviewed_seed_not_a_new_edit(gallery):
    templates = audit.independent_gallery(*gallery)
    assert templates['gallery']['polygon'] == gallery[3]['gallery']['polygon']
    assert templates['gallery']['origin'] == audit.GALLERY_ORIGIN
    assert templates['gallery']['human_polygon_revision'] == 1


@pytest.mark.parametrize('attack', ['unknown_pub', 'proposal_hash', 'proposal_polygon', 'source_binding', 'binding',
    'template', 'nonhuman', 'gt', 'pose_write', 'duplicate_revision', 'stale_current', 'bad_old_event'])
def test_every_historical_gallery_event_must_bind_actual_publication_and_direct_save(gallery, attack):
    current = gallery[0]['current']['gallery']
    fields = {'unknown_pub': ('proposal_publication', 11), 'proposal_hash': ('base_proposal_id', 'fake'),
        'proposal_polygon': ('proposal_polygon', FOUR), 'source_binding': ('source_binding', 'fake'),
        'binding': ('binding', 'fake'), 'template': ('template_provenance', {}), 'nonhuman': ('human_edited', False),
        'gt': ('ground_truth_eligible', True), 'pose_write': ('pose_store_written', True)}
    if attack in fields:
        field, bad = fields[attack]; current[field] = bad
        gallery[0]['events'] = [event(current)]
    elif attack == 'duplicate_revision':
        gallery[0]['events'].append(event(current, 2))
    elif attack == 'stale_current':
        gallery[0]['events'].append(event(dict(current, revision=2), 2))
    else:
        gallery[0]['events'].insert(0, event(dict(current, revision=0, binding='forged-history'), 2))
    with pytest.raises(ValueError):
        audit.independent_gallery(*gallery)


def test_actual_gallery_raw_record_can_include_canonical_current_hash_but_must_match_every_raw_byte(gallery, tmp_path):
    frozen = tmp_path/'gallery.sqlite3'
    value = gallery[0]['current']['gallery']; raw = json.dumps(value, ensure_ascii=False, indent=2)
    with sqlite3.connect(frozen) as db:
        db.executescript('CREATE TABLE reviews(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        db.execute('INSERT INTO reviews VALUES (?,?,?)', ('gallery', 1, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (1, 'gallery', 1, raw))
    record = dict(target_key='gallery', revision=1, current_payload_raw=raw,
        current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), current_payload=value,
        current_payload_sha256=audit.sha(value), human_event_id=1, human_event_payload_raw=raw,
        human_event_sha256=hashlib.sha256(raw.encode()).hexdigest(), snapshot_sha256='snapshot')
    path = tmp_path/'gallery_source_records.jsonl'; write_jsonl(path, [record])
    assert audit.check_gallery_records(tmp_path, frozen, gallery[0], 'snapshot') == {'gallery': record}
    record['human_event_payload_raw'] += '\n'; write_jsonl(path, [record])
    with pytest.raises(ValueError, match='Exact raw gallery'):
        audit.check_gallery_records(tmp_path, frozen, gallery[0], 'snapshot')


@pytest.fixture
def saved_review(gallery, tmp_path):
    value, p = gallery[0]['current']['gallery'], gallery[3]['gallery']
    raw = json.dumps(value, indent=2, ensure_ascii=False)
    record = dict(current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest(), human_event_id=1,
        human_event_sha256=hashlib.sha256(raw.encode()).hexdigest())
    statement = {'statement_zh': audit.USER_STATEMENT}
    item = dict(target_key='gallery', class_id=3, actual_saved=True,
        polygon_changed_from_proposal=False, saved_review_unchanged=True,
        human_new_edit_claim=False, new_human_edit_created=False, proposal_sha256=audit.sha(p),
        proposal_polygon_sha256=audit.sha(p['polygon']), current_revision=1,
        current_polygon_sha256=audit.sha(value['polygon']), current_payload_sha256=audit.sha(value),
        current_payload_raw_sha256=record['current_payload_raw_sha256'], human_event_id=1,
        human_event_sha256=record['human_event_sha256'], gallery_snapshot_sha256='snapshot',
        source_binding=value['source_binding'], proposal_publication=10,
        binding=value['binding'], base_proposal_id=value['base_proposal_id'])
    status = dict(parent_new_machine_candidates=1, actually_saved=1, pending=0,
        polygon_changed_from_proposal=0, saved_unchanged=1, saved_keys=['gallery'], pending_keys=[],
        unchanged_keys=['gallery'], saved_evidence_authority='real frozen current/raw latest events with historical publication/image/source bindings',
        unchanged_review_interpretation_statement_sha256=audit.sha(statement), new_dispositions_created=0,
        new_human_edits_created=0, unsaved_candidates_are_seeds=False)
    write_jsonl(tmp_path/'saved_review_source_audit.jsonl', [item])
    write_json(tmp_path/'saved_review_status.json', status)
    templates = audit.independent_gallery(*gallery)
    args = (tmp_path, gallery[3], gallery[0], {'gallery': record}, templates, 'snapshot', statement, {'saved_review_status': status})
    return args, item, status


def test_saved_unchanged_review_retains_actual_raw_binding_and_is_not_a_new_human_edit(saved_review):
    args, _, _ = saved_review
    assert audit.check_saved_reviews(*args) == {'gallery'}
    template = args[4]['gallery']
    assert template['saved_human_review'] is True and template['saved_review_unchanged'] is True
    assert template['new_human_edit_created'] is False and template['polygon_modified_from_proposal'] is False
    assert template['saved_review_event_raw_sha256'] == args[3]['gallery']['human_event_sha256']


@pytest.mark.parametrize('field,bad', [('human_new_edit_claim', True), ('new_human_edit_created', True),
    ('actual_saved', False), ('saved_review_unchanged', False), ('polygon_changed_from_proposal', True),
    ('proposal_sha256', 'other'), ('current_payload_raw_sha256', 'other'),
    ('human_event_id', 2), ('human_event_sha256', 'canonical-not-raw'), ('binding', 'other')])
def test_saved_unchanged_source_cannot_be_reclassified_edited_or_lose_exact_publication_event_binding(saved_review, field, bad):
    args, item, _ = saved_review
    item[field] = bad
    write_jsonl(args[0]/'saved_review_source_audit.jsonl', [item])
    with pytest.raises(ValueError, match='current/raw-event audit'):
        audit.check_saved_reviews(*args)


@pytest.mark.parametrize('field,bad', [('saved_unchanged', 0), ('pending', 1), ('new_dispositions_created', 1),
    ('new_human_edits_created', 1), ('unsaved_candidates_are_seeds', True),
    ('unchanged_review_interpretation_statement_sha256', 'unscoped-statement')])
def test_saved_review_status_cannot_invent_dispositions_or_seed_eligibility(saved_review, field, bad):
    args, _, status = saved_review
    status[field] = bad
    write_json(args[0]/'saved_review_status.json', status)
    with pytest.raises(ValueError, match='Saved369'):
        audit.check_saved_reviews(*args)


def test_readonly_no_template_sibling_diagnostic_does_not_authorize_a_protected_frame_output():
    rows = {'saved': row('saved'), 'sibling': row('sibling', 4, 'frame-saved')}
    saved = value(rows['saved'], FINE)
    pose = store({'saved': saved}, [event(saved)])
    outcomes = {'saved': dict(reason='pose_mask_or_state_history_protected', registration_attempts=0),
        'sibling': dict(reason='no_compatible_template', registration_attempts=0)}
    excluded, keys, frames, _ = audit.scan_inventory(rows, pose, store(), {}, (), outcomes)
    assert excluded == {'saved'} and 'frame-saved' in frames
    machine = proposal(rows['sibling'], FINE)
    with pytest.raises(ValueError, match='protected key/frame'):
        audit.check_machine(machine, pose, {}, rows, keys, frames, 'snapshot', 'formal', 'classes')
    outcomes['sibling']['registration_attempts'] = 1
    with pytest.raises(ValueError, match='untouched no-template'):
        audit.scan_inventory(rows, pose, store(), {}, (), outcomes)


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


@pytest.fixture
def attempts(monkeypatch):
    rows = {k: row(k) for k in ('target', 'rejected', 'low')}
    templates = {k: dict(row=row(k), polygon=copy.deepcopy(FINE)) for k in ('best', 'second', 'below')}
    similarity = {'best': .8, 'second': .7, 'below': .549999}
    monkeypatch.setattr(audit, 'correlate', lambda source, target: .549999 if target == 'low' else similarity[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 == 'target':
            return copy.deepcopy(FINE), copy.deepcopy(metrics)
        return None, dict(rejection='image_registration_failed', initial_ncc=similarity[source])
    monkeypatch.setattr(audit, 'replay', replay)
    trials = [dict(target_key=key, class_id=3, template_key=source,
        metrics=replay(templates[source], rows[key], None)[1]) for key in ('target', 'rejected') for source in ('best', 'second')]
    calls.clear()
    accepted = {'target': dict(proposal(rows['target'], FINE), template=dict(target_key='second'), metrics=metrics,
        continuation=dict(batch=11, template_rank=2, all_compatible_targets_scanned=True, recursive_candidate_propagation=False))}
    outcomes = {key: dict(target_key=key, class_id=3, reason=reason) for key, reason in
        [('target', 'new_proposal'), ('rejected', 'registration_or_boundary_rejected'), ('low', 'image_similarity_below_gate')]}
    return trials, rows, templates, accepted, outcomes, Evidence(), calls


def test_all_accepted_and_rejected_attempts_are_replayed_in_exact_rank_order(attempts):
    assert audit.audit_attempts(*attempts[:6]) == {'target', 'rejected', 'low'}
    assert Counter(attempts[6]) == {('best', 'target'): 1, ('second', 'target'): 1,
        ('best', 'rejected'): 1, ('second', 'rejected'): 1}


@pytest.mark.parametrize('attack', ['missing_rejection', 'extra_below_gate', 'wrong_order', 'wrong_rank',
    'rejected_metric', 'accepted_metric', 'accepted_polygon', 'relaxed_ecc', 'relaxed_boundary', 'relaxed_fb',
    'beam', 'protected_key', 'protected_frame', 'filled6', 'filled13', 'missing_outcome', 'incompatible_seed'])
def test_full_replay_rejects_hidden_attempts_relaxed_gates_or_incompatible_sources(attempts, monkeypatch, attack):
    trials, rows, templates, accepted, outcomes, evidence, _ = attempts
    extra = {}
    if attack == 'missing_rejection':
        trials.pop(2)
    elif attack == 'extra_below_gate':
        trials.append(dict(target_key='target', class_id=3, template_key='below', metrics={}))
    elif attack == 'wrong_order':
        trials[0], trials[1] = trials[1], trials[0]
    elif attack == 'wrong_rank':
        accepted['target']['continuation']['template_rank'] = 1
    elif attack == 'rejected_metric':
        trials[2]['metrics']['initial_ncc'] += .001
    elif attack == 'accepted_metric':
        accepted['target']['metrics']['ecc'] += .001
    elif attack == 'accepted_polygon':
        accepted['target']['polygon'][0][0] += .01
    elif attack.startswith('relaxed_') or attack == 'beam':
        field = {'relaxed_ecc': 'ecc', 'relaxed_boundary': 'boundary_ncc',
            'relaxed_fb': 'forward_backward_error_patch_pixels', 'beam': 'boundary_guard'}[attack]
        bad = {'relaxed_ecc': .83999, 'relaxed_boundary': .77999, 'relaxed_fb': [1.50001]*8,
            'beam': dict(state='review_required')}[attack]
        accepted['target']['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:
                metrics[field] = bad
            return polygon, metrics
        monkeypatch.setattr(audit, 'replay', bad_replay)
    elif attack == 'protected_key':
        extra['protected_keys'] = {'target'}
    elif attack == 'protected_frame':
        extra['protected_frames'] = {'frame-target'}
    elif attack in ('filled6', 'filled13'):
        rows['target']['final_class_id'] = 6 if attack == 'filled6' else 13
        outcomes['target']['class_id'] = rows['target']['final_class_id']
        for trial in trials[:2]:
            trial['class_id'] = rows['target']['final_class_id']
    elif attack == 'missing_outcome':
        outcomes.pop('low')
    else:
        templates['second']['row']['split'] = 'test'
    with pytest.raises((ValueError, AssertionError)):
        audit.audit_attempts(trials, rows, templates, accepted, outcomes, evidence, **extra)


def test_replay_runs_the_full_beam_guard_not_only_registration_gates(monkeypatch):
    template, target = dict(row=row('seed'), polygon=FINE), row('target')
    monkeypatch.setattr(audit, 'transfer', lambda *args: (copy.deepcopy(FINE), dict(ecc=.9)))
    monkeypatch.setattr(audit, 'affine_reference', lambda p, t: 'reference')
    calls = []
    monkeypatch.setattr(audit, 'beam_boundary_audit', lambda image, polygon, reference:
        calls.append((image, reference)) or dict(state='review_required', all_beams=[1, 2, 3]))
    class FullEvidence(Evidence):
        def image(self, frame):
            return frame
    polygon, metrics = audit.replay(template, target, FullEvidence())
    assert polygon is None and metrics['rejection'] == 'local_beam_boundary_review_required'
    assert metrics['boundary_guard']['all_beams'] == [1, 2, 3] and calls == [('frame-target', 'reference')]


def test_saved_counts_do_not_conflate_original_reviews_with1022_actual_human_contours():
    old = {str(i): proposal(row(str(i), 3)) for i in range(10)}
    manual = {'edit': proposal(row('edit', 4)), 'review': proposal(row('review', 4))}
    new = {'new': proposal(row('new', 4), FINE)}
    counts = audit.source_counts(old, manual, new, {'0': {}, 'edit': {}}, {'1': {}, '2': {}}, {'3': {}}, {'review': {}})
    assert counts['3']['saved_human'] == 3 and counts['3']['no_new_server_review'] == 6
    assert counts['4']['saved_human'] == 1 and counts['4']['original_rectangle_review_only'] == 1
    assert counts['4']['pending_machine_raw'] == 1 and counts['4']['new_candidates'] == 1


def candidate(key, cls=3, frame=None, group='common', score=.9):
    r = row(key, cls, frame); r['group_id'] = group
    return dict(proposal(r, FINE), metrics=dict(boundary_ncc=score, ecc=.9))


def test_global_matching_locks_review_only_frames_and_reassigns_only_new_machines():
    base = {f'locked-{c}-{i}': candidate(f'locked-{c}-{i}', c) for c in (3, 4) for i in range(99)}
    base['rectangle-review'] = dict(candidate('rectangle-review', 7), schema=audit.REVIEW_SCHEMA,
        human_edited=False, propagation_seed_eligible=False)
    accepted = {p['target_key']: p for p in [candidate('a', 3, 'shared'), candidate('b', 3, 'only3', 'rare'),
        candidate('c', 4, 'shared'), candidate('d', 4, 'frame-rectangle-review')]}
    selected, owners, _ = audit.previous.diverse_matching(base, accepted)
    assert set(selected) == {'b', 'c'} and owners == {'only3': 3, 'shared': 4}
    assert audit.previous.residual_maximum_size(base, accepted) == 2
    outcomes = {k: dict(reason='new_proposal' if k in selected else 'accepted_duplicate_frame_not_selected') for k in accepted}
    audit.previous.check_diversity(base, accepted, selected, outcomes)
    outcomes['d']['reason'] = 'new_proposal'
    with pytest.raises(ValueError, match='Diversity'):
        audit.previous.check_diversity(base, accepted, {**selected, 'd': accepted['d']}, outcomes)


def test_class19_capacity_is_recomputed_below90_after1111_locked_other_class_frames():
    rows = {str(i): row(str(i), 19, 'frame'+str(i)) for i in range(96)}
    old = {str(i): proposal(rows[str(i)]) for i in range(4)}
    # Another newly locked source removes a previously usable class19 frame.
    old['locked-other-class'] = proposal(row('locked-other-class', 4, 'frame6'))
    protected_frames = {'frame'+str(i) for i in range(7)}
    capacity = audit.previous.capacity_audit(rows, old, {}, set(map(str, range(7))), protected_frames)['19']
    assert capacity['original_distinct_frames'] == 96
    assert capacity['protected_aware_capacity'] == 89 and capacity['capacity_shortfall'] == 11


@pytest.mark.parametrize('archived', [False, True])
def test_private_old45_scope_resolves_at_exact_preserved_parent_evidence_path(tmp_path, monkeypatch, archived):
    parent, out, baseline = (tmp_path/name for name in ('parent', 'out', 'baseline'))
    for p in (parent, out, baseline):
        p.mkdir()
    monkeypatch.setattr(audit, 'PARENT', parent)
    monkeypatch.setattr(audit.sources, 'BASELINE', baseline)
    for p in (parent/'proposals.jsonl', out/'proposals.jsonl'):
        p.write_bytes(b'')
    for name in ('review_dispositions.jsonl', 'review_disposition_statement.json', 'disposition_scope_source.jsonl'):
        raw = b'exact old45 '+name.encode()
        (baseline/name).write_bytes(raw)
        destination = parent/('parent_evidence/'+name if archived and name != 'review_dispositions.jsonl' else name)
        destination.parent.mkdir(exist_ok=True); destination.write_bytes(raw)
        destination = out/('historic/'+name if name != 'review_dispositions.jsonl' else name)
        destination.parent.mkdir(exist_ok=True); destination.write_bytes(raw)
    audit.check_preservation(out, {})
    (out/'historic/disposition_scope_source.jsonl').write_bytes(b'broadened scope')
    with pytest.raises(ValueError, match='scope/statement'):
        audit.check_preservation(out, {})


def test_readonly_live_guard_detects_exact_raw_whitespace_changes(tmp_path):
    frozen, live = tmp_path/'frozen.sqlite3', tmp_path/'live.sqlite3'
    with sqlite3.connect(frozen) as db:
        db.executescript('CREATE TABLE metadata(key TEXT,value TEXT); CREATE TABLE annotations(target_key TEXT,revision INTEGER,payload TEXT);'
            'CREATE TABLE reviews(target_key TEXT,revision INTEGER,payload TEXT); CREATE TABLE events(id INTEGER,target_key TEXT,revision INTEGER,payload TEXT);')
        raw = '{"target_key": "a", "revision": 1}'
        for table in ('annotations', 'reviews'):
            db.execute(f'INSERT INTO {table} VALUES (?,?,?)', ('a', 1, raw))
        db.execute('INSERT INTO events VALUES (?,?,?,?)', (1, 'a', 1, raw))
    live.write_bytes(frozen.read_bytes()); before = live.read_bytes()
    stores = {kind: audit.helpers.read_store(frozen, kind, frozen=True) for kind in ('pose', 'gallery')}
    snapshots = dict.fromkeys(stores, frozen)
    audit.check_live(stores, snapshots, live, live)
    assert live.read_bytes() == before
    with sqlite3.connect(live) as db:
        db.execute('UPDATE annotations SET payload=?', ('{"target_key":"a","revision":1}',))
    with pytest.raises(ValueError, match='logical database'):
        audit.check_live(stores, snapshots, live, live)


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


def test_failure_only_writes_failed_report_never_success_seal(seal_output):
    def fail(_):
        raise ValueError('invalid raw source')
    with pytest.raises(ValueError, match='invalid raw'):
        audit.seal(seal_output, fail)
    assert not (seal_output/audit.SEAL).exists()
    assert audit.read_json(seal_output/audit.REPORT)['state'] == 'failed'


def test_success_seal_is_exclusive_and_binds_the_complete_validation_report(seal_output):
    result = audit.seal(seal_output, lambda _: dict(state='passed', publication_version=11, proposals_verified=1111))
    before = (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() == before


def test_symlinks_and_incomplete_outputs_cannot_write_a_seal(seal_output):
    link = seal_output.parent/'pallet_mask_remaining_continue_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/'historical.json'; protected.write_text('immutable')
    (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() == 'immutable'


def test_standalone_verifier_has_no_generator_import_assert_or_live_writer():
    tree = ast.parse(audit.Path(audit.__file__).read_text())
    for node in ast.walk(tree):
        assert not isinstance(node, ast.Assert)
        if isinstance(node, ast.ImportFrom):
            assert node.module != 'pallet.mask_remaining_continue'
        elif isinstance(node, ast.Import):
            assert 'pallet.mask_remaining_continue' not in {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')


def test_cli_forwards_explicit_paths_without_polling_or_generation(monkeypatch, tmp_path):
    calls = []
    monkeypatch.setattr(audit, 'audit', lambda out, pose, gallery: calls.append((out, pose, gallery)) or dict(state='passed'))
    monkeypatch.setattr(audit, 'seal', lambda out, operation: operation(out))
    out, pose, gallery = tmp_path/'output', tmp_path/'pose', tmp_path/'gallery'
    assert audit.main(['--output', str(out), '--pose-live', str(pose), '--gallery-live', str(gallery)]) == dict(state='passed')
    assert calls == [(out, pose, gallery)]
