"""Independent publication-10 audit attacks; never run the expansion generator."""
import ast
from collections import Counter
import copy
import hashlib
import json
import sqlite3

import pytest

from scripts import verify_mask_remaining_expand 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=()):
    return dict(path=audit.Path('readonly.sqlite3'), metadata={}, 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=10, method='original', target_per_class=100,
        generation_template_namespace=audit.NAMESPACE, production_database_writes=0,
        configuration_writes=0, service_writes=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)


def test_new_class_groups_are_independent_and_cover22_without_generating_old5():
    assert audit.CLASSES == tuple(range(1, 23))
    assert audit.REMAINING == (3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22)
    assert not set(audit.REMAINING) & set(audit.OLD_CLASSES)
    assert set(audit.REMAINING) | set(audit.OLD_CLASSES) == set(audit.CLASSES)


@pytest.mark.parametrize('field,bad', [('publication_version', 9), ('method', 'research'),
    ('target_per_class', 90), ('generation_template_namespace', 'remaining_classes_generation'),
    ('production_database_writes', False), ('configuration_writes', 1), ('service_writes', '0'),
    ('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),
    ('new_human_edits_created', 1), ('research_candidates_added', 1), ('cross_split_template_transfers', 1),
    ('original_splits_preserved', False), ('review_dispositions_are_saved_human_masks', True),
    ('review_dispositions_seed_authorization', True), ('human_confirmation_inferred', True),
    ('activated', True), ('all_classes_complete', True)])
def test_manifest_claims_fail_closed_without_truthy_zero_or_invented_completion(field, bad):
    manifest = honest_manifest()
    audit.check_claims(manifest)
    manifest[field] = bad
    with pytest.raises(ValueError):
        audit.check_claims(manifest)


def test_aliases_are_strict_and_do_not_require_invented_metadata():
    audit.claim({'manual_added': 192}, ('manual_added', 'manual_proposals'), 192)
    audit.claim({}, 'not_in_actual_schema', 0, required=False)
    with pytest.raises(ValueError, match='Conflicting'):
        audit.claim({'manual_added': 192, 'manual_proposals': 51}, ('manual_added', 'manual_proposals'), 192)
    with pytest.raises(ValueError):
        audit.claim({'n': False}, 'n', 0)
    with pytest.raises(ValueError, match='Missing'):
        audit.claim({}, 'production_database_writes', 0)


@pytest.fixture
def partition():
    old = {f'old-{c}-{i}': proposal(row(f'old-{c}-{i}', c)) for c in audit.OLD_CLASSES for i in range(100)}
    previous = {f'human-{i}': proposal(row(f'human-{i}', 22 if i < 66 else 12 if i < 101 else 14 if i < 119 else 5))
        for i in range(141)}
    # Keep each class below100; the exact old141 distribution is separately pinned.
    requests = {f'request-{c}-{i}': dict(target_key=f'request-{c}-{i}', class_id=c)
        for c in audit.REMAINING for i in range(3)}
    manual = dict(previous)
    manual.update({k: proposal(row(k, r['class_id']), FINE) for k, r in requests.items()})
    new = {'machine': proposal(row('machine', 3), FINE)}
    combined = {**old, **manual, **new}
    return old, previous, requests, manual, new, combined, copy.deepcopy(new)


def test_exact500_plus141_plus51_plus_machine_partition(partition):
    counts = audit.check_partition(*partition)
    assert len(partition[5]) == 693
    assert all(counts[str(c)]['proposals'] == 100 for c in audit.OLD_CLASSES)
    assert sum(c['proposals'] for c in counts.values()) == 693


@pytest.mark.parametrize('attack', ['lost_old500', 'rewrite_old', 'lost_old141', 'rewrite_manual',
    'missing_request', 'extra_human', 'old_class_machine', 'machine_as_manual', 'accepted_rewrite',
    'crossclass_frame', 'new_reuses_old_frame', 'class_overquota'])
def test_partition_cannot_substitute_lose_or_doublecount_old_work(partition, attack):
    old, previous, requests, manual, new, combined, accepted = partition
    if attack == 'lost_old500':
        combined.pop(next(iter(old)))
    elif attack == 'rewrite_old':
        key = next(iter(old)); combined[key] = dict(old[key], human_mask_confirmed=False)
    elif attack == 'lost_old141':
        manual.pop(next(iter(previous)))
    elif attack == 'rewrite_manual':
        key = next(iter(previous)); manual[key] = dict(manual[key], new_field='rebinding')
    elif attack == 'missing_request':
        manual.pop(next(iter(requests)))
    elif attack == 'extra_human':
        manual['extra'] = proposal(row('extra'))
    elif attack == 'old_class_machine':
        for values in (new, accepted, combined):
            values['machine'] = dict(values['machine'], class_id=1)
    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':
        key = 'request-3-0'
        manual[key]['source']['frame_id'] = manual['request-4-0']['source']['frame_id']
    elif attack == 'new_reuses_old_frame':
        new['machine']['source']['frame_id'] = next(iter(old.values()))['source']['frame_id']
        accepted['machine'] = copy.deepcopy(new['machine'])
    else:
        for i in range(100):
            key = f'extra-machine-{i}'; p = proposal(row(key, 3), FINE)
            new[key] = combined[key] = accepted[key] = p
    with pytest.raises(ValueError):
        audit.check_partition(*partition)


@pytest.fixture(params=[FOUR, FINE], ids=['four-point', 'fine'])
def human(request):
    r = row()
    v = value(r, request.param)
    pose = store({'human': v}, [event(v)])
    recovered = audit.sources.recover_pose_sources(pose, {'human': r})
    return r, pose, recovered


def test_seed_inventory_only_uses_counted_remaining_human_gt_not_other_eligible_sources(human):
    r, pose, recovered = human
    extra_row = row('extra', 7)
    extra = value(extra_row, FINE)
    pose['current']['extra'] = extra
    pose['events'].append(event(extra, 2))
    recovered = audit.sources.recover_pose_sources(pose, {'human': r, 'extra': extra_row})
    seeds = audit.independent_seeds({'human': {}}, recovered, 'snapshot')
    assert set(seeds) == ({'human'} if len(recovered['human']['polygon']) > 4 else set())
    assert 'extra' not in seeds  # An extra sixth old seed is not counted human192.


@pytest.mark.parametrize('kind', ['unit_normalization', 'axes_normalization'])
def test_human_template_event_hash_uses_exact_raw_utf8_and_normalization_is_not_human(human, kind):
    r, pose, _ = human
    first_raw = pose['events'][0][3]
    current = dict(pose['current']['human'], revision=2, event_kind=kind)
    pose['current']['human'] = current
    pose['events'].append(event(current))
    recovered = audit.sources.recover_pose_sources(pose, {'human': r})['human']
    template = audit.pose_template(recovered, 'snapshot')
    assert template['human_polygon_revision'] == 1 and template['revision'] == 2
    assert template['human_event_sha256'] == hashlib.sha256(first_raw.encode('utf-8')).hexdigest()
    assert template['human_event_sha256'] != audit.sha(json.loads(first_raw))


@pytest.mark.parametrize('state', ['confirmed', 'deferred', 'deleted'])
def test_any_protected_pose_history_stays_frame_locked_after_return_to_rectangle(state):
    r, other = row('a'), row('b', 4, frame='frame-a')
    historical = value(r, audit.sources.rectangle(r), state_marker=state)
    historical['state'] = state
    current = value(r, audit.sources.rectangle(r), revision=2)
    pose = store({'a': current}, [event(historical), event(current)])
    keys, frames = audit.target_protection({'a': r, 'b': other}, pose, store(), {}, {})
    assert 'a' in keys and other['frame_id'] in frames


def test_gallery_any_history_and_old500_and_fourpoint_manual_protect_whole_frames():
    rows = {k: row(k, 3, frame=f) for k, f in [('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd')]}
    gallery = store({}, [(1, 'a', 1, '{}')])
    keys, frames = audit.target_protection(rows, store(), gallery,
        {'b': proposal(rows['b'])}, {'c': proposal(rows['c'])}, {'d'})
    assert keys == set(rows) and frames == {'a', 'b', 'c', 'd'}


def test_class19_96original_minus4old500_minus2history_capacity90_not_completed90():
    rows = {str(i): row(str(i), 19, frame='f'+str(i)) for i in range(96)}
    old = {str(i): proposal(rows[str(i)]) for i in range(4)}
    protected_frames = {'f'+str(i) for i in range(6)}
    result = audit.capacity_audit(rows, old, {}, set(map(str, range(6))), protected_frames)['19']
    assert result == dict(original_distinct_frames=96, baseline_disjoint_frames=92,
        protected_aware_capacity=90, capacity_shortfall=10, saved_human=0)


@pytest.fixture
def completed(human):
    r, pose, recovered = human
    source = recovered['human']
    four = len(source['polygon']) == 4
    request = dict(target_key='human', class_id=3, source=copy.deepcopy(r), pose_annotation_url='https://pose.example/?target=human')
    p = dict(proposal(r, source['polygon']), schema='saved_pose_mask_gallery_entry_v1',
        origin='original_pose_human_revision', status='human_edited_draft_pending_confirmation', human_edited=True,
        manual_source_kind='completed_manual_request', manual_four_point_source=four, propagation_seed_eligible=not four,
        pose_snapshot_saved=True, 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+(':manual_source:' if four else ':')+'human',
        manual_request_sha256=audit.sha(request), original_pose_url=request['pose_annotation_url'])
    p['human_source' if four else 'template'] = audit.template_binding(audit.pose_template(source, 'snapshot'))
    return p, request, r, recovered, 'snapshot', 'formal', 'classes'


def test_completed51_sources_are_real_saved_manual_work_not_machine_self_transfers(completed):
    audit.check_new_manual(*completed)


@pytest.mark.parametrize('attack', ['vertex', 'seed', 'human_event_hash', 'current_hash', 'target_revision',
    'template_field', 'request_hash', 'request_url', 'split', 'policy', 'view', 'class', 'confirmed', 'write', 'machine_metric'])
def test_manual_source_cannot_launder_fourpoint_topology_or_source_provenance(completed, attack):
    p, request, r, recovered, *_ = completed
    field = 'human_source' if len(p['polygon']) == 4 else 'template'
    if attack == 'vertex':
        p['polygon'].append([30, 30])
    elif attack == 'seed':
        p['propagation_seed_eligible'] = not p['propagation_seed_eligible']
    elif attack == 'human_event_hash':
        p[field]['human_event_sha256'] = 'canonical-json-not-raw'
    elif attack == 'current_hash':
        p['target_snapshot_payload_sha256'] = 'stale'
    elif attack == 'target_revision':
        p['target_expected_revision'] = 0
    elif attack == 'template_field':
        p['template' if field == 'human_source' else 'human_source'] = p[field]
    elif attack == 'request_hash':
        p['manual_request_sha256'] = 'fake'
    elif attack == 'request_url':
        p['original_pose_url'] += '-other'
    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['human_mask_confirmed'] = True
    elif attack == 'write':
        p['pose_store_written'] = True
    else:
        p['metrics']['ecc'] = 1
    with pytest.raises(ValueError):
        audit.check_new_manual(*completed)


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', nested=[True, 2, .1]))
    calls = []
    def replay(template, target, _):
        source = template['row']['target_key']; 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=10, 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_replay_covers_accepted_and_all_rejections_and_stops_only_at_first_success(attempts):
    args, calls = attempts[:6], attempts[6]
    assert audit.audit_attempts(*args) == {'target', 'rejected', 'low'}
    assert Counter(calls) == {('best', 'target'): 1, ('second', 'target'): 1,
        ('best', 'rejected'): 1, ('second', 'rejected'): 1}


@pytest.mark.parametrize('attack', ['missing_rejected_attempt', 'extra_below_gate', 'wrong_order', 'bad_rank',
    'rejected_metric', 'accepted_metric', 'accepted_polygon', 'relaxed_ecc', 'relaxed_boundary', 'relaxed_fb',
    'protected_key', 'protected_crossclass_frame', 'rejected_as_no_template', 'missing_outcome', 'incompatible_seed'])
def test_all_attempt_ranks_metrics_compatibility_and_protection_are_independently_checked(attempts, monkeypatch, attack):
    trials, rows, templates, accepted, outcomes, evidence, _ = attempts
    extra = {}
    if attack == 'missing_rejected_attempt':
        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 == 'bad_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_'):
        field = dict(relaxed_ecc='ecc', relaxed_boundary='boundary_ncc', relaxed_fb='forward_backward_error_patch_pixels')[attack]
        accepted['target']['metrics'][field] = [1.50001]*8 if attack == 'relaxed_fb' else .83999 if attack == 'relaxed_ecc' else .77999
        # Tamper the declared metrics AND a purportedly passing replay; explicit
        # gates still reject this rather than trusting a success return alone.
        saved = audit.replay
        def bad_replay(template, target, evidence):
            polygon, metrics = saved(template, target, evidence)
            if polygon is not None:
                metrics[field] = accepted['target']['metrics'][field]
            return polygon, metrics
        monkeypatch.setattr(audit, 'replay', bad_replay)
        trials[1]['metrics'][field] = accepted['target']['metrics'][field]
    elif attack == 'protected_key':
        extra['protected_keys'] = {'target'}
    elif attack == 'protected_crossclass_frame':
        extra['protected_frames'] = {'frame-target'}
    elif attack == 'rejected_as_no_template':
        outcomes['rejected']['reason'] = 'no_compatible_template'
    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_and_propagates_its_rejection(monkeypatch):
    template = dict(row=row('seed'), polygon=FINE)
    target = 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]
    assert calls == [('frame-target', 'reference')]


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


def test_diversity_honors_manual_and_other5_frame_locks_and_group_balance():
    base = {'old': candidate('old', 'common', cls=1), 'human': candidate('human', 'common')}
    accepted = {k: p for k, p in [('a', candidate('a', 'common', .99)),
        ('b', candidate('b', 'rare', .8)), ('c', candidate('c', 'other', frame='frame-old'))]}
    outcomes = {k: dict(reason='accepted_duplicate_frame_not_selected' if k == 'c' else 'new_proposal') for k in accepted}
    new = {k: p for k, p in accepted.items() if k != 'c'}
    audit.check_diversity(base, accepted, new, outcomes)
    outcomes['c']['reason'] = 'new_proposal'
    with pytest.raises(ValueError, match='Diversity'):
        audit.check_diversity(base, accepted, {**new, 'c': accepted['c']}, outcomes)


def test_diversity_may_honestly_stop_short_but_cannot_hide_a_passing_selected_candidate():
    accepted = {'a': candidate('a', 'rare')}
    with pytest.raises(ValueError, match='Selected machine'):
        audit.check_diversity({}, accepted, {}, {'a': dict(reason='new_proposal')})
    base = {str(i): candidate(str(i), 'common') for i in range(100)}
    audit.check_diversity(base, accepted, {}, {'a': dict(reason='accepted_quota_full_not_selected')})


def test_maximum_matching_reallocates_only_new_machine_frames_to_protect_scarce_class():
    base = {f'locked-{c}-{i}': candidate(f'locked-{c}-{i}', 'g', cls=c) for c in (3, 4) for i in range(99)}
    accepted = {p['target_key']: p for p in [candidate('a', 'common', frame='shared', cls=3),
        candidate('b', 'rare', frame='only3', cls=3), candidate('c', 'rare', frame='shared', cls=4)]}
    selected, owners, order = audit.diverse_matching(base, accepted)
    assert set(selected) == {'b', 'c'} and owners == {'only3': 3, 'shared': 4}
    assert audit.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.check_diversity(base, accepted, selected, outcomes)


def test_frame_first_maximum_proof_does_not_leave_ghost_owner_when_moving_a_sibling_frame():
    base = {f'locked-{c}-{i}': candidate(f'locked-{c}-{i}', 'g', cls=c) for c, n in ((3, 98), (4, 99)) for i in range(n)}
    accepted = {p['target_key']: p for p in [candidate('a', 'g', frame='a', cls=3),
        candidate('b3', 'g', frame='b', cls=3), candidate('b4', 'g', frame='b', cls=4),
        candidate('d', 'g', frame='d', cls=3), candidate('e', 'g', frame='e', cls=3)]}
    assert audit.residual_maximum_size(base, accepted) == 3
    assert len(audit.diverse_matching(base, accepted)[0]) == 3


@pytest.mark.parametrize('archived', [False, True])
def test_private_scope_allows_exact_frozen_parent_evidence_but_public_dispositions_stay_root(tmp_path, monkeypatch, archived):
    parent, sources, out = (tmp_path/name for name in ('parent', 'sources', 'out'))
    for p in (parent, sources, out):
        p.mkdir()
    monkeypatch.setattr(audit, 'PARENT', parent)
    monkeypatch.setattr(audit, 'SOURCES', sources)
    for p in (parent/'proposals.jsonl', sources/'manual_proposals.jsonl', out/'proposals.jsonl', out/'manual_proposals.jsonl'):
        p.write_bytes(b'')
    for name in ('review_dispositions.jsonl', 'review_disposition_statement.json', 'disposition_scope_source.jsonl'):
        (parent/name).write_bytes(b'original scope '+name.encode())
        destination = out/'parent_evidence'/name if archived and name != 'review_dispositions.jsonl' else out/name
        destination.parent.mkdir(exist_ok=True)
        destination.write_bytes((parent/name).read_bytes())
    audit.check_preservation(out, {}, {})
    scope = out/('parent_evidence/disposition_scope_source.jsonl' if archived else 'disposition_scope_source.jsonl')
    scope.write_bytes(b'changed scope')
    with pytest.raises(ValueError, match='scope/statement'):
        audit.check_preservation(out, {}, {})


def test_actual_all335_audit_schema_validates_current_and_historical_sources_without_legacy_overlays(tmp_path, human):
    r, _, recovered = human
    old = dict(proposal(r), target_expected_revision=1, target_snapshot_payload_sha256='old')
    source = recovered['human']
    record = {'target_key': 'human'}  # Raw byte fields are independently checked by check_source_records.
    item = dict(target_key='human', class_id=3, exact_current_direct_human_validated=True,
        current_revision=1, current_payload_sha256=source['current_payload_sha256'], vertex_count=source['vertex_count'],
        historical_canonical_preserved=True, canonical_proposal_sha256=audit.sha(old),
        historical_polygon_changed=old['polygon'] != source['polygon'],
        current_template_selected=source['vertex_count'] > 4, current_snapshot_sha256='snapshot',
        human_event_sha256=source['human_event_sha256'])
    write_jsonl(tmp_path/'human_source_audit.jsonl', [item])
    audit.check_inherited_audits(tmp_path, {'human': old}, {'human': old}, recovered, {'human': record}, 'snapshot')
    item['human_event_sha256'] = 'canonical-hash-not-raw'
    write_jsonl(tmp_path/'human_source_audit.jsonl', [item])
    with pytest.raises(ValueError, match='current/historical'):
        audit.check_inherited_audits(tmp_path, {'human': old}, {'human': old}, recovered, {'human': record}, 'snapshot')


def test_raw_pending_includes_new_machines_but_old_unsaved42_stays_separate():
    old = {str(i): candidate(str(i), 'g', cls=1) for i in range(500)}
    inherited = {str(i): old[str(i)] for i in range(143)}
    gallery = store({str(i): {} for i in range(143, 413)})
    dispositions = {str(i): {} for i in range(413, 458)}
    new = {'new': candidate('new', 'new', cls=3)}
    counts = audit.source_counts(old, {}, new, inherited, gallery, dispositions)
    assert counts['1']['old_pending_machine'] == 87
    assert counts['1']['no_new_server_review'] == 42
    assert counts['3']['pending_machine_raw'] == counts['3']['no_new_server_review'] == 1


def test_dynamic_gallery_save_reduces_old_pending_without_rewriting_old45():
    old = {str(i): candidate(str(i), 'g', cls=1) for i in range(100)}
    inherited = {str(i): old[str(i)] for i in range(20)}
    gallery = store({str(i): {} for i in range(20, 55)})
    dispositions = {str(i): {} for i in range(55, 100)}
    result = audit.source_counts(old, {}, {}, inherited, gallery, dispositions)['1']
    assert result['saved_human'] == 55 and result['user_reviewed_no_change'] == 45
    assert result['pending_machine_raw'] == 45 and result['no_new_server_review'] == 0
    gallery['current']['55'] = {}
    result = audit.source_counts(old, {}, {}, inherited, gallery, dispositions)['1']
    assert result['saved_human'] == 56  # Current saved evidence is dynamic.
    assert result['user_reviewed_no_change'] == 45  # Historical statement never rewritten.


def test_raw_jsonl_preservation_is_stricter_than_equivalent_canonical_objects(tmp_path):
    path = tmp_path/'objects.jsonl'
    path.write_text('{"target_key": "a", "x": 1}\n')
    before = audit.raw_lines(path)
    path.write_text('{"target_key":"a","x":1}\n')
    after = audit.raw_lines(path)
    assert json.loads(before['a']) == json.loads(after['a']) and before != after
    path.write_bytes(after['a']*2)
    with pytest.raises(ValueError, match='Duplicate'):
        audit.raw_lines(path)


def test_readonly_live_guard_detects_whitespace_changes_and_never_writes(tmp_path):
    def database(path):
        with sqlite3.connect(path) 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))
    frozen, live = tmp_path/'frozen.sqlite3', tmp_path/'live.sqlite3'
    database(frozen); live.write_bytes(frozen.read_bytes())
    stores = {kind: audit.helpers.read_store(frozen, kind, frozen=True) for kind in ('pose', 'gallery')}
    snapshots = dict.fromkeys(stores, frozen)
    original = live.read_bytes()
    audit.check_live(stores, snapshots, live, live)
    assert live.read_bytes() == original
    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_expand_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 actual source')
    with pytest.raises(ValueError, match='invalid actual'):
        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_hash_binds_the_full_report(seal_output):
    result = audit.seal(seal_output, lambda _: dict(state='passed', publication_version=10, proposals_verified=692))
    before = (seal_output/audit.SEAL).read_bytes()
    assert result['validation_report_sha256'] == audit.digest(seal_output/audit.REPORT)
    assert audit.read_json(seal_output/audit.REPORT)['proposals_verified'] == 692
    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_expand_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_no_expansion_generator_import_no_asserts_no_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_expand'
        elif isinstance(node, ast.Import):
            assert 'pallet.mask_remaining_expand' 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_explicit_paths_are_forwarded_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)]
