"""Publication-12 audit attacks; no generator import, old-policy patch or live write."""
import ast
from collections import Counter
import copy
import hashlib
import json
import sqlite3

import pytest

from scripts import verify_mask_remaining_round2 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=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 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 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, obj):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(audit.canonical(obj)+b'\n')


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


def snapshot(path, kind, events, metadata=None):
    table = 'annotations' if kind == 'pose' else 'reviews'
    latest = {}
    for _, key, revision, raw in events:
        latest[key] = revision, raw
    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 honest_manifest():
    return dict(publication_version=12, 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_publication12_owns_new_policy_without_mutating_previous_fixed50_policy():
    assert audit.PUBLICATION == 12 and audit.NAMESPACE == 'remaining_round2_generation'
    assert audit.CLASSES == tuple(range(1, 23))
    assert set(audit.COMPLETED) == set(audit.OLD_CLASSES) | {3, 6, 13}
    assert sum(audit.BASE_COUNTS.values()) == 1350
    assert 2200-sum(audit.BASE_COUNTS.values()) == 850
    assert audit.previous.PUBLICATION == 11 and 3 not in audit.previous.COMPLETED
    assert audit.previous.ORIGINAL_REVIEWS == audit.OLD_ORIGINAL_REVIEWS


@pytest.mark.parametrize('field,bad', [('publication_version', 11), ('method', 'research'),
    ('target_per_class', 99), ('generation_template_namespace', 'remaining_continue_generation'),
    ('production_database_writes', False), ('configuration_writes', 1), ('service_writes', '0'),
    ('new_human_edits_created', 1), ('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),
    ('original_splits_preserved', False), ('classification_is_candidate_not_truth', False),
    ('duplicate_fill', True), ('human_confirmation_inferred', True), ('activated', True), ('published', True),
    ('all_classes_complete', True), ('review_dispositions_are_saved_human_masks', True),
    ('review_dispositions_seed_authorization', True), ('human_requests_created', 54)])
def test_manifest_denies_writes_relaxed_gates_automatic_confirmation_and_truthy_zero(field, bad):
    manifest = honest_manifest()
    audit.check_claims(manifest)
    manifest[field] = bad
    with pytest.raises(ValueError):
        audit.check_claims(manifest)


@pytest.fixture
def partition():
    requests = {f'request-{c}-{i}': dict(target_key=f'request-{c}-{i}', class_id=c)
        for c, n in {3: 4, 4: 10, 5: 10, 7: 10, 14: 10, 22: 10}.items() for i in range(n)}
    for i, key in enumerate(audit.NEW_ORIGINAL_REVIEWS):
        requests.pop(f'request-4-{i}')
        requests[key] = dict(target_key=key, class_id=4)
    manual = {k: proposal(row(k, r['class_id']), FINE if r['class_id'] == 3 else FOUR) for k, r in requests.items()}
    for key in audit.NEW_ORIGINAL_REVIEWS:
        manual[key]['schema'] = audit.REVIEW_SCHEMA
    manual_counts = 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-manual_counts[c])}
    for key, (cls, _) in audit.OLD_ORIGINAL_REVIEWS.items():
        old.pop(f'old-{cls}-0')
        old[key] = dict(proposal(row(key, cls)), schema=audit.REVIEW_SCHEMA)
    new = {'machine': proposal(row('machine', 4), FINE)}
    combined = copy.deepcopy({**old, **manual, **new})
    return old, requests, manual, new, combined, copy.deepcopy(new)


def test_exact1296_plus54_locks_newly_completed_class3_and_all_five_review_only_entries(partition):
    result = audit.check_partition(*partition)
    assert len(partition[4]) == 1351
    assert result['3']['proposals'] == 100
    assert sum(v['proposals'] for v in result.values()) == 1351


@pytest.mark.parametrize('attack', ['lost_old', 'rewrite_old', 'missing_task', 'extra_manual', 'lost_old_review',
    'new_review_as_edit', 'extra_review', 'machine_class3', 'machine_class6', 'machine_class13', 'machine_old5',
    'machine_as_manual', 'accepted_rewrite', 'frame_reuse', 'crossclass_frame', 'overquota', 'changed_locked_class'])
def test_partition_fails_on_substitution_laundering_or_crossclass_frame_reuse(partition, attack):
    old, requests, manual, new, combined, accepted = partition
    if attack == 'lost_old':
        combined.pop(next(iter(old)))
    elif attack == 'rewrite_old':
        combined[next(iter(old))]['comment'] = 'not preserved'
    elif attack == 'missing_task':
        manual.pop(next(iter(requests)))
    elif attack == 'extra_manual':
        manual['extra'] = proposal(row('extra'))
    elif attack == 'lost_old_review':
        key = next(iter(audit.OLD_ORIGINAL_REVIEWS)); old[key]['schema'] = 'saved_pose_mask_gallery_entry_v1'
        combined[key] = copy.deepcopy(old[key])
    elif attack == 'new_review_as_edit':
        key = next(iter(audit.NEW_ORIGINAL_REVIEWS)); manual[key]['schema'] = 'saved_pose_mask_gallery_entry_v1'
        combined[key] = copy.deepcopy(manual[key])
    elif attack == 'extra_review':
        manual['request-5-1']['schema'] = audit.REVIEW_SCHEMA
        combined['request-5-1'] = copy.deepcopy(manual['request-5-1'])
    elif attack.startswith('machine_class') or attack == 'machine_old5':
        cls = {'machine_class3': 3, 'machine_class6': 6, 'machine_class13': 13, 'machine_old5': 1}[attack]
        for v in (new, combined, accepted):
            v['machine']['class_id'] = cls
    elif attack == 'machine_as_manual':
        key = next(iter(manual)); accepted[key] = copy.deepcopy(manual[key])
    elif attack == 'accepted_rewrite':
        accepted['machine']['polygon'][0][0] += .1
    elif attack == 'frame_reuse':
        for v in (new, combined, accepted):
            v['machine']['source']['frame_id'] = next(iter(old.values()))['source']['frame_id']
    elif attack == 'crossclass_frame':
        manual['request-5-1']['source']['frame_id'] = manual['request-7-1']['source']['frame_id']
        combined['request-5-1'] = copy.deepcopy(manual['request-5-1'])
    elif attack == 'changed_locked_class':
        key = 'request-3-1'; manual[key]['class_id'] = 4; combined[key] = copy.deepcopy(manual[key])
    else:
        for i in range(80):
            key = 'extra-'+str(i); p = proposal(row(key, 4), FINE)
            new[key] = combined[key] = accepted[key] = p
    with pytest.raises(ValueError):
        audit.check_partition(*partition)


@pytest.fixture(params=[4, 8])
def completed(request):
    fine = request.param == 8
    r = row('human', 3 if fine else 4)
    v = value(r, FINE if fine else FOUR)
    recovered = audit.sources.recover_pose_sources(store({'human': v}, [event(v)]), {'human': r})
    source = recovered['human']
    task = dict(target_key='human', class_id=r['final_class_id'], source=copy.deepcopy(r), pose_annotation_url='https://pose.example/?target=human')
    p = dict(proposal(r, v['polygon']), schema='saved_pose_mask_gallery_entry_v1', origin=audit.POSE_ORIGIN,
        status='human_edited_draft_pending_confirmation', human_edited=True, manual_four_point_source=not fine,
        propagation_seed_eligible=fine, 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={},
        rendering_id=audit.NAMESPACE+(':' if fine else ':manual_source:')+'human', manual_request_sha256=audit.sha(task),
        original_pose_url=task['pose_annotation_url'])
    p['template' if fine else 'human_source'] = audit.template_binding(audit.matching.pose_template(source, 'snapshot'))
    return p, task, r, recovered, 'snapshot', 'formal', 'classes'


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


@pytest.mark.parametrize('attack', ['vertices', 'seed', 'event_hash', 'revision', 'binding', 'request',
    'split', 'policy', 'view', 'confirmed', 'write', 'metric', 'synthetic_source'])
def test_new_modified_sources_cannot_invent_geometry_or_provenance(completed, attack):
    p, task, *_ = completed
    field = 'human_source' if p['vertex_count'] == 4 else 'template'
    if attack == 'vertices':
        p['polygon'].append([30, 30])
    elif attack == 'seed':
        p['propagation_seed_eligible'] = not p['propagation_seed_eligible']
    elif attack == 'event_hash':
        p[field]['human_event_sha256'] = 'canonical-not-raw'
    elif attack == 'revision':
        p['target_expected_revision'] = 0
    elif attack == 'binding':
        p['target_snapshot_payload_sha256'] = 'stale'
    elif attack == 'request':
        p['manual_request_sha256'] = 'fake'
    elif attack in ('split', 'policy', 'view'):
        task['source'][dict(split='split', policy='policy_id', view='viewpoint_candidate')[attack]] = 'other'
    elif attack == 'confirmed':
        p['ground_truth_eligible'] = True
    elif attack == 'write':
        p['pose_store_written'] = True
    elif attack == 'metric':
        p['metrics']['ecc'] = 1
    else:
        p['human_edited'] = False
    with pytest.raises(ValueError):
        audit.check_manual(*completed)


@pytest.fixture
def reviews(tmp_path, monkeypatch):
    parent, out = tmp_path/'parent', tmp_path/'out'
    parent.mkdir(); out.mkdir()
    monkeypatch.setattr(audit, 'PARENT', parent)
    tasks = {k: dict(target_key=k, class_id=4) for k in audit.NEW_ORIGINAL_REVIEWS}
    statement = dict(schema='explicit_user_review_no_change_statement_v1', statement_zh=audit.USER_STATEMENT,
        scoped_request_keys=sorted(tasks), reviewed_original_rectangle_keys=sorted(audit.NEW_ORIGINAL_REVIEWS),
        candidate_count_in_statement=185, actual_parent_batch_saved_count=185, old_42_unsaved_not_inferred_reviewed=True,
        creates_human_event=False, confirms_ground_truth=False, authorizes_propagation=False)
    historic = dict(statement_zh=audit.previous.USER_STATEMENT,
        scoped_request_keys=sorted(audit.OLD_ORIGINAL_REVIEWS), reviewed_original_rectangle_keys=sorted(audit.OLD_ORIGINAL_REVIEWS))
    write_json(parent/'review_completion_statement.json', historic)
    write_json(out/'parent_evidence/review_completion_statement.json', historic)
    write_json(out/'review_completion_statement.json', statement)
    binding = {'formal_manifest_sha256': 'formal'}
    events, rows = [], {}
    for i, (key, (cls, revision)) in enumerate(audit.ORIGINAL_REVIEWS.items(), 1):
        rows[key] = row(key, cls)
        events.append(event(value(rows[key], audit.sources.rectangle(rows[key]), revision, binding=audit.sha(binding)), i))
    path = tmp_path/'pose.sqlite3'
    pose = snapshot(path, 'pose', events, {'binding': json.dumps(binding)})
    (parent/'reviews_snapshot.sqlite3').write_bytes(path.read_bytes())
    originals = {k: audit.original_review_source(k, pose, r, path) for k, r in rows.items()}
    records = []
    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')
    for key, source in originals.items():
        authority = historic if key in audit.OLD_ORIGINAL_REVIEWS else statement
        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=audit.digest(parent/'reviews_snapshot.sqlite3') if key in audit.OLD_ORIGINAL_REVIEWS else 'snapshot',
            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(authority),
            classification_is_candidate_not_truth=True, **dict.fromkeys(audit.baseline.previous.FALSE_FLAGS, False))
        records.append(record)
    record_path = out/'reviewed_original_rectangle_source_records.jsonl'
    write_jsonl(record_path, records); record_path.chmod(0o600)
    write_jsonl(parent/'reviewed_original_rectangle_source_records.jsonl', [r for r in records if r['target_key'] in audit.OLD_ORIGINAL_REVIEWS])
    return out, originals, tasks, records, statement, historic, pose, rows, path


def test_all_five_reviewonly_records_bind_real_utf8_events_and_two_distinct_statement_scopes(reviews):
    out, originals, tasks, *_ = reviews
    records, statement = audit.check_review_records(out, originals, 'snapshot', tasks)
    for key in audit.OLD_ORIGINAL_REVIEWS:
        assert records[key]['user_statement_sha256'] == audit.sha(reviews[5])
        assert records[key]['user_statement_sha256'] != audit.sha(statement)
    for source in originals.values():
        assert source['saved_review_event_sha256'] == hashlib.sha256(source['saved_review_event_payload_raw'].encode()).hexdigest()
        assert 'human_polygon_revision' not in source and source['human_edited'] is False
    assert audit.sources.recover_pose_sources(reviews[6], reviews[7]) == {}


@pytest.mark.parametrize('attack', ['old_latest_scope', 'new_old_scope', 'extra_key_scope', 'lost_old',
    'human_edited', 'seed', 'fake_human_event', 'fake_human_revision', 'event_whitespace', 'current_whitespace',
    'event_hash', 'statement', 'count', 'infer_old42', 'propagation', 'private_permissions'])
def test_original_review_records_cannot_infer_old42_edits_or_broaden_historical_statement(reviews, attack):
    out, originals, tasks, records, statement, historic, *_ = reviews
    bykey = {r['target_key']: r for r in records}
    key = next(iter(audit.NEW_ORIGINAL_REVIEWS)); record = bykey[key]
    if attack == 'old_latest_scope':
        bykey[next(iter(audit.OLD_ORIGINAL_REVIEWS))]['user_statement_sha256'] = audit.sha(statement)
    elif attack == 'new_old_scope':
        record['user_statement_sha256'] = audit.sha(historic)
    elif attack == 'extra_key_scope':
        statement['scoped_request_keys'].append('old42-never-reviewed')
    elif attack == 'lost_old':
        records.remove(bykey[next(iter(audit.OLD_ORIGINAL_REVIEWS))])
    elif attack in ('human_edited', 'seed'):
        record['human_edited' if attack == 'human_edited' else 'propagation_seed_eligible'] = True
    elif attack == 'fake_human_event':
        record['human_event_id'] = record['saved_review_event_id']
    elif attack == 'fake_human_revision':
        record['human_polygon_revision'] = 1
    elif attack == 'event_whitespace':
        record['saved_review_event_payload_raw'] += '\n'
    elif attack == 'current_whitespace':
        record['current_payload_raw'] += '\n'
    elif attack == 'event_hash':
        record['saved_review_event_sha256'] = audit.sha(record['saved_review_event_payload'])
    elif attack == 'statement':
        statement['statement_zh'] = audit.previous.USER_STATEMENT
    elif attack == 'count':
        statement['actual_parent_batch_saved_count'] = 269
    elif attack == 'infer_old42':
        statement['old_42_unsaved_not_inferred_reviewed'] = False
    elif attack == 'propagation':
        statement['authorizes_propagation'] = True
    write_jsonl(out/'reviewed_original_rectangle_source_records.jsonl', records)
    write_json(out/'review_completion_statement.json', statement)
    if attack == 'private_permissions':
        (out/'reviewed_original_rectangle_source_records.jsonl').chmod(0o644)
    with pytest.raises(ValueError):
        audit.check_review_records(out, originals, 'snapshot', tasks)


@pytest.mark.parametrize('state', ['confirmed', 'deferred', 'deleted'])
def test_even_one_protected_old_original_rectangle_event_forbids_reviewonly_coverage(reviews, state):
    _, _, _, _, _, _, pose, rows, path = reviews
    key = next(iter(audit.NEW_ORIGINAL_REVIEWS)); r = rows[key]
    old = dict(pose['current'][key], state=state)
    pose['events'].insert(0, event(old, 1000))
    with pytest.raises(ValueError, match='history'):
        audit.original_review_source(key, pose, r, path)


@pytest.fixture
def delta(tmp_path):
    parent_paths, paths, parents, stores = {}, {}, {}, {}
    tasks = {f'task-{i}': {} for i in range(54)}
    reviews = {f'review-{i}': {} for i in range(185)}
    for kind, authorized, old_task_count, total in (('pose', tasks, 20, 145), ('gallery', reviews, 0, 201)):
        old = [event(value(row('unrelated')), 1)]
        for i, key in enumerate(list(authorized)[:old_task_count], 2):
            old.append(event(value(row(key)), i))
        events = list(old)
        revisions = Counter({e[1]: e[2] for e in old})
        keys = list(authorized)
        for i in range(total):
            key = keys[i % len(keys)]; revisions[key] += 1
            events.append(event(value(row(key), revision=revisions[key]), len(events)+1))
        parent_paths[kind], paths[kind] = tmp_path/(kind+'-parent.sqlite3'), tmp_path/(kind+'-new.sqlite3')
        parents[kind] = snapshot(parent_paths[kind], kind, old)
        stores[kind] = snapshot(paths[kind], kind, events)
    return parent_paths, paths, parents, stores, tasks, reviews


def test_exact_new145_pose_events_only54_tasks_and201_gallery_events_only185_reviewed_candidates(delta):
    audit.check_store_delta(*delta)


@pytest.mark.parametrize('attack', ['unrelated_raw', 'unrelated_event', 'missing_event', 'rewritten_old_event',
    'lost_old_current', 'new_unrequested_current', 'latest_raw', 'metadata', 'stale_revision'])
def test_exact_batch_delta_cannot_hide_unrelated_writes_or_unchanged_old_source_rebindings(delta, attack):
    parent_paths, paths, parents, stores, tasks, reviews = delta
    if attack == 'unrelated_raw':
        with sqlite3.connect(paths['pose']) as db:
            raw = db.execute('SELECT payload FROM annotations WHERE target_key="unrelated"').fetchone()[0]
            db.execute('UPDATE annotations SET payload=? WHERE target_key="unrelated"', (raw+'\n',))
    elif attack == 'unrelated_event':
        stores['pose']['events'][-1] = event(value(row('unrelated'), revision=2), 9999)
    elif attack == 'missing_event':
        stores['gallery']['events'].pop()
    elif attack == 'rewritten_old_event':
        e = stores['pose']['events'][0]
        stores['pose']['events'][0] = (*e[:3], e[3]+'\n')
    elif attack == 'lost_old_current':
        with sqlite3.connect(paths['pose']) as db:
            db.execute('DELETE FROM annotations WHERE target_key="unrelated"')
    elif attack == 'new_unrequested_current':
        with sqlite3.connect(paths['pose']) as db:
            v = value(row('not-requested'))
            db.execute('INSERT INTO annotations VALUES (?,?,?)', ('not-requested', 1, json.dumps(v)))
    elif attack == 'latest_raw':
        with sqlite3.connect(paths['pose']) as db:
            raw = db.execute('SELECT payload FROM annotations WHERE target_key="task-0"').fetchone()[0]
            db.execute('UPDATE annotations SET payload=? WHERE target_key="task-0"', (raw+'\n',))
    elif attack == 'metadata':
        stores['pose']['metadata']['fake'] = 'new'
    else:
        with sqlite3.connect(paths['pose']) as db:
            db.execute('UPDATE annotations SET revision=1 WHERE target_key="task-0"')
    with pytest.raises(ValueError):
        audit.check_store_delta(*delta)


def test_seed_inventory_is_exact_old410_plus185_gallery_plus_four_new_class3_never_original5_or_quads():
    recovered = {}
    manual = {}
    for key, cls, polygon in [('oldpose', 4, FINE), ('newclass3', 3, FINE), ('quad', 4, FOUR), ('oldfive', 1, FINE)]:
        r = row(key, cls); v = value(r, polygon)
        recovered.update(audit.sources.recover_pose_sources(store({key: v}, [event(v)]), {key: r}))
        if key == 'newclass3':
            manual[key] = proposal(r, polygon)
    gallery = {k: dict(row=row(k, c), polygon=FINE, origin=audit.GALLERY_ORIGIN)
        for k, c in [('oldgallery', 4), ('newgallery', 5), ('old5gallery', 1)]}
    seeds = audit.independent_seeds(recovered, recovered, gallery, {'oldpose', 'oldgallery'}, {'newgallery': {}}, manual, 'snapshot')
    assert set(seeds) == {'oldpose', 'oldgallery', 'newgallery', 'newclass3'}
    gallery['unsaved-machine'] = dict(row=row('unsaved-machine'), polygon=FINE, origin=audit.GALLERY_ORIGIN)
    with pytest.raises(ValueError, match='exact parent410'):
        audit.independent_seeds(recovered, recovered, gallery, {'oldpose', 'oldgallery'}, {'newgallery': {}}, manual, 'snapshot')


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


@pytest.fixture
def attempts(monkeypatch):
    rows = {k: row(k) for k in ('selected', 'unselected', 'rejected', 'low')}
    templates = {k: dict(row=row(k), 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=4, 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=12, 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=4, 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_every_accepted_rejected_and_diversity_unselected_attempt_is_replayed_with_all_rank_scores(attempts):
    assert audit.audit_attempts(*attempts[:6]) == set(attempts[1])
    assert Counter(attempts[6]) == {(source, key): 1 for key in ('selected', 'unselected', 'rejected') for source in ('best', 'second')}


@pytest.mark.parametrize('attack', ['missing_rejection', 'omit_unselected', 'extra_below_gate', 'wrong_order',
    'wrong_rank', 'rejected_metric', 'unselected_metric', 'accepted_polygon', 'relaxed_ecc', 'relaxed_boundary',
    'relaxed_fb', 'beam', 'protected_key', 'protected_frame', 'filled3', 'filled6', 'filled13', 'missing_outcome', 'incompatible'])
def test_full_replay_catches_hidden_failures_relaxed_thresholds_and_completed_class3(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 == 'extra_below_gate':
        trials.append(dict(target_key='low', class_id=4, template_key='below', metrics={}))
    elif attack == 'wrong_order':
        trials[0], trials[1] = trials[1], trials[0]
    elif attack == 'wrong_rank':
        accepted['selected']['continuation']['template_rank'] = 1
    elif attack == 'rejected_metric':
        trials[4]['metrics']['initial_ncc'] += .01
    elif attack == 'unselected_metric':
        accepted['unselected']['metrics']['ecc'] += .01
    elif attack == 'accepted_polygon':
        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.startswith('filled'):
        cls = int(attack[6:]); rows['selected']['final_class_id'] = outcomes['selected']['class_id'] = cls
        for trial in trials[:2]:
            trial['class_id'] = cls
    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_calls_full_beam_guard_and_preserves_detailed_rejection_metrics(monkeypatch):
    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(dict(row=row('seed'), polygon=FINE), row('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_reviewonly_five_are_distinct_from1258_saved_contours_and45_conversation_only():
    old = {str(i): proposal(row(str(i))) for i in range(10)}
    manual = {'edit': proposal(row('edit')), 'review': proposal(row('review'))}
    new = {'new': proposal(row('new'), FINE)}
    counts = audit.source_counts(old, manual, new, {'0': {}, 'edit': {}}, {'1': {}, '2': {}}, {'3': {}}, {'review': {}})
    assert counts['4']['saved_human'] == 4 and counts['4']['original_rectangle_review_only'] == 1
    assert counts['4']['pending_machine_raw'] == 8 and counts['4']['no_new_server_review'] == 7
    assert counts['4']['manual_modified_added'] == 1


def candidate(key, cls=4, 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_diversity_locks_every_historical_or_manual_frame_and_reassigns_only_new_machines():
    base = {f'locked-{c}-{i}': candidate(f'locked-{c}-{i}', c) for c in (4, 5) for i in range(99)}
    base['review-only'] = dict(candidate('review-only', 7), schema=audit.REVIEW_SCHEMA, human_edited=False)
    accepted = {p['target_key']: p for p in [candidate('a', 4, 'shared'), candidate('b', 4, 'only4', 'rare'),
        candidate('c', 5, 'shared'), candidate('d', 5, 'frame-review-only')]}
    selected, owners, _ = audit.matching.diverse_matching(base, accepted)
    assert set(selected) == {'b', 'c'} and owners == {'only4': 4, 'shared': 5}
    assert audit.matching.residual_maximum_size(base, accepted) == 2


def test_class19_capacity_is_dynamic_not_frozen_to_previous82():
    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(23)}
    old['locked-other'] = proposal(row('locked-other', 4, 'frame30'))
    protected_frames = {'frame'+str(i) for i in range(23)} | {'frame30', 'frame31', 'frame32', 'frame33', 'frame34', 'frame35', 'frame36', 'frame37'}
    capacity = audit.matching.capacity_audit(rows, old, old, set(map(str, range(23))), protected_frames)['19']
    assert capacity['protected_aware_capacity'] == 88 and capacity['capacity_shortfall'] == 12


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


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


def test_exclusive_success_seal_binds_exact_report_hash_and_cannot_be_overwritten(seal_output):
    result = audit.seal(seal_output, lambda _: dict(state='passed', publication_version=12, proposals_verified=1350, new_verified=0))
    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_sealing_rejects_symlink_output_symlink_report_and_missing_manifest(seal_output):
    link = seal_output.parent/'pallet_mask_remaining_round2_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('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'
    (seal_output/'manifest.json').unlink()
    with pytest.raises(ValueError, match='not complete'):
        audit.seal(seal_output, lambda _: dict(state='passed'))


def test_standalone_audit_has_no_generator_import_assert_old_module_mutation_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_round2'
        elif isinstance(node, ast.Import):
            assert 'pallet.mask_remaining_round2' 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', 'setattr')
        if isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
            targets = node.targets if isinstance(node, ast.Assign) else [node.target]
            for target in targets:
                assert not (isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name)
                    and target.value.id in ('previous', 'matching', 'sources', 'helpers', 'baseline'))


def test_cli_defaults_new_output_and_forwards_explicit_readonly_paths_without_polling(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)]
