"""Synthetic fixtures only; never stage real sources or open production stores."""
import ast
import copy
import json
from pathlib import Path
import shutil
import sqlite3

import numpy as np
from PIL import Image
import pytest

from scripts import build_mask_remaining_round2_requests as round2
from scripts import build_mask_remaining_followup_requests as followup
from scripts import build_mask_remaining_direct_requests as direct


def row(key, cls=4, frame=None):
    return dict(target_key=key, target_id='target-'+key, frame_id=frame or 'frame-'+key,
        final_class_id=cls, final_class=f'class-{cls}', split='train', group_id='group-'+key,
        box_index=0, policy_id='sensor', viewpoint_candidate='front', source_pcd_sha256='pcd',
        bbox=dict(x=4, y=4, width=60, height=20), size=[100, 40], image=key+'.png', appearance_status='ok')


def pose(r, revision=1):
    return dict(target_key=r['target_key'], source=r, revision=revision, state='draft',
        polygon=direct.rectangle(r), pose={'x': 1}, ruler={'length': 2}, sensor={'id': 3},
        selected_faces=['front'], exclusions=['back'])


def usable(virgin=True, area=2000, clarity=.8):
    return dict(no_pose_current_or_history=virgin, existing_exact_signature_human_sources=0,
        existing_class_group_human_sources=0, quality=dict(clarity_score=clarity, bbox_area_native_pixels=area))


def decision(approved=True, **extra):
    return dict(approved=approved, crop_inspected=True, full_image_inspected=True,
        note_zh='完整原图与局部均已检查，目标完整、结构清楚，沿真实可见轮廓人工判断。', **extra)


def test_new_scope_only_and_frozen_helpers_never_patched_or_orchestrated():
    assert round2.CLASSES == (4, 5, 7, 14, 22)
    assert followup.CLASSES == (3, 4, 5, 7, 14, 22)
    assert round2.PARENT.name == 'pallet_mask_remaining_round2_v1'
    assert round2.DEFAULT.name == 'pallet_mask_remaining_round2_requests_v1'
    assert round2.native_quality is direct.native_quality
    assert round2.render_case is direct.render_case
    assert round2.pose_binding is direct.pose_binding
    assert round2.QUALITY_GATES == direct.QUALITY_GATES
    assert len(round2.ORIGINAL_REVIEWS) == 5 and len(round2.NEW_ORIGINAL_REVIEWS) == 3
    assert not hasattr(round2, 'snapshot_database') and not hasattr(round2, 'attempt_target')
    tree = ast.parse(Path(round2.__file__).read_text())
    forbidden = {'prepare', 'review', 'build', 'verify', 'select_tasks', 'review_history', 'load_parent', 'parent_seal'}
    for node in ast.walk(tree):
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            assert not (isinstance(node.func.value, ast.Name) and node.func.value.id in {'direct', 'followup'}
                and node.func.attr in forbidden)
        if isinstance(node, (ast.Assign, ast.AnnAssign)):
            targets = node.targets if isinstance(node, ast.Assign) else [node.target]
            assert not any(isinstance(t, ast.Attribute) and isinstance(t.value, ast.Name)
                and t.value.id in {'direct', 'followup'} for t in targets)


def proposals(counts):
    return [dict(target_key=f'{c}-{i}', class_id=c, source=dict(frame_id=f'{c}-{i}'))
        for c, n in counts.items() for i in range(n)]


def test_actual_post_generation_gaps_and_completed_class3_never_requested():
    values = proposals({3: 100, 4: 26, 5: 32, 7: 25, 14: 41, 22: 89})
    assert round2.class_quotas(values) == {4: 10, 5: 10, 7: 10, 14: 10, 22: 10}
    values += proposals({22: 0})
    for i in range(9):
        values.append(dict(target_key=f'new22-{i}', class_id=22, source=dict(frame_id=f'new22-{i}')))
    assert round2.class_quotas(values)[22] == 2
    assert sum(round2.class_quotas(values).values()) == 42
    assert round2.class_quotas(values, 3) == {4: 3, 5: 3, 7: 3, 14: 3, 22: 2}
    values[-1]['source']['frame_id'] = values[0]['source']['frame_id']
    with pytest.raises(ValueError, match='globally distinct'):
        round2.class_quotas(values)


@pytest.mark.parametrize('limit', [0, 11, True, 2.5])
def test_per_class_limit_cannot_relax_ten(limit):
    with pytest.raises(ValueError):
        round2.class_quotas([], limit)


@pytest.mark.parametrize('kind', ['restored_edit', 'confirmed', 'deleted', 'deferred', 'nested_confirmation',
    'human_edited', 'original_rectangle_review_only', 'user_reviewed_no_change', 'machine_draft', 'metric_pose_confirmed'])
def test_any_non_original_history_blocks_entire_frame_even_if_current_restored(kind):
    r = row('a'); value = pose(r)
    if kind == 'restored_edit':
        value['polygon'][0][0] += .1
    elif kind in ('confirmed', 'deleted', 'deferred'):
        value['state'] = kind
    elif kind == 'nested_confirmation':
        value['pose']['human_pose_confirmed'] = True
    else:
        value[kind] = True
    rows = {'a': r, 'b': row('b', 5, frame=r['frame_id'])}
    protected, frames, _ = round2.protection(rows, {'a': pose(r, 2)}, [(1, 'a', 1, json.dumps(value))], (), [], [])
    assert protected == {'a'} and frames == {rows['b']['frame_id']}


def test_five_explicit_original_reviews_always_locked_but_clean_pose_only_can_be_new_task():
    rows = {k: row(k, cls) for k, (cls, _) in round2.ORIGINAL_REVIEWS.items()}
    rows['fresh14'] = row('fresh14', 14)
    current = {k: pose(r) for k, r in rows.items()}
    events = [(i, k, 1, json.dumps(current[k])) for i, k in enumerate(rows)]
    protected, frames, history = round2.protection(rows, current, events, (), [], [])
    assert protected == set(round2.ORIGINAL_REVIEWS) and len(frames) == 5
    assert history['fresh14'][0]['original_pose_only']
    protected, _, _ = round2.protection(rows, {}, events, (), [], [])
    assert 'fresh14' in protected  # Orphan history is never virgin.


def test_all_namespace_request_discovery_includes_failed_roots_and_nested_archives(tmp_path, monkeypatch):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    out = tmp_path/'outputs/pallet_mask_remaining_round2_requests_v1'
    others = [tmp_path/'outputs/unrelated_namespace/archive/request-copy',
        tmp_path/'outputs/pallet_mask_remaining_followup_requests_v1',
        tmp_path/'outputs/pallet_mask_remaining_followup_requests_v2/parent_evidence']
    for root in [out]+others:
        root.mkdir(parents=True); (root/'requests.jsonl').write_text('{}\n')
    paths = round2.old_request_paths(out)
    assert out/'requests.jsonl' not in paths
    assert all(root/'requests.jsonl' in paths for root in others)
    assert tmp_path/'outputs/pallet_mask_remaining_followup_requests_v2/requests.jsonl' in paths


def test_group_preferences_never_override_usable_pool_virgin_priority_or_distinct_frames():
    rows = {k: row(k, 14) for k in ('old-pose', 'virgin', 'not-usable')}
    rows['class3'] = row('class3', 3)
    pool = {'old-pose': usable(False), 'virgin': usable(), 'class3': usable()}
    quotas = dict.fromkeys(round2.CLASSES, 0); quotas[14] = 1
    prefs = dict(preferred_keys=['class3', 'not-usable', 'old-pose'], class_groups={'14': ['group-old-pose']})
    assert round2.select_tasks(rows, pool, quotas, prefs) == ['virgin']
    del pool['virgin']
    assert round2.select_tasks(rows, pool, quotas) == ['old-pose']
    assert round2.select_tasks(rows, pool, quotas, excluded_frames=['frame-old-pose']) == []
    with pytest.raises(ValueError):
        round2.select_tasks(rows, pool, quotas | {3: 1})


def test_quality_rank_and_explicit_group_preferences_carry_into_selection():
    rows = {k: row(k) for k in ('a', 'b', 'c')}
    pool = {'a': usable(area=1000, clarity=.4), 'b': usable(area=7000, clarity=1), 'c': usable()}
    quotas = dict.fromkeys(round2.CLASSES, 0); quotas[4] = 1
    assert round2.select_tasks(rows, pool, quotas) == ['b']
    assert round2.select_tasks(rows, pool, quotas, dict(class_groups={'4': ['group-c']})) == ['c']
    quotas[4] = 2; rows['a']['frame_id'] = rows['b']['frame_id']
    assert set(round2.select_tasks(rows, pool, quotas)) == {'b', 'c'}


@pytest.mark.parametrize('risk', round2.RISK_FIELDS)
def test_ambiguous_multishelf_multi3hole_and_clipped_sources_must_be_rejected(tmp_path, risk):
    path = tmp_path/'decisions.json'; value = dict(decisions={'k': decision(**{risk: True})})
    path.write_text(json.dumps(value))
    with pytest.raises(ValueError, match='must be rejected'):
        round2.decision_input(path)
    value['decisions']['k']['approved'] = False; path.write_text(json.dumps(value))
    assert round2.decision_input(path)['decisions']['k'][risk] is True


@pytest.mark.parametrize('bad', [
    {'decisions': {'k': {'approved': True}}},
    {'decisions': {'k': dict(decision(), full_image_inspected=False)}},
    {'decisions': {'k': dict(decision(False), note_zh=' ')}},
    {'decisions': {'k': dict(decision(), classification_ambiguous='false')}},
    {'preferences': {'relax_quality': True}},
    {'preferences': {'class_groups': {'3': ['g']}}},
    {'preferences': {'class_groups': {'19': ['g']}}},
    {'preferences': {'preferred_keys': ['k', 'k']}}, {'schema': followup.DECISION_SCHEMA},
])
def test_decisions_require_real_inspection_and_cannot_relax_scope(tmp_path, bad):
    path = tmp_path/'decisions.json'; path.write_text(json.dumps(bad))
    with pytest.raises(ValueError):
        round2.decision_input(path)


def make_parent(root, project, new_counts=None):
    counts = {3: 96, 4: 16, 5: 22, 7: 15, 14: 31, 22: 79}
    counts.update({c: 100 for c in (1, 2, 9, 10, 15)})
    others = [c for c in range(1, 23) if c not in counts]
    counts.update({c: 48+(i < 9) for i, c in enumerate(others)})
    assert sum(counts.values()) == 1296
    def p(key, cls, **extra):
        n = 8 if cls == 3 and key.startswith('manual') else 4
        return dict(target_key=key, class_id=cls, source=dict(frame_id='frame-'+key),
            polygon=[[i+1, i+2] for i in range(n)], **extra)
    previous = []
    for c, n in counts.items():
        old_box = next((k for k, (cls, _) in round2.OLD_ORIGINAL_REVIEWS.items() if cls == c), None)
        for i in range(n):
            key = old_box if i == 0 and old_box else f'old-{c}-{i}'
            extra = dict(human_edited=False, propagation_seed_eligible=False, reviewed_original_rectangle=True,
                target_expected_revision=round2.OLD_ORIGINAL_REVIEWS[key][1]) if key in round2.OLD_ORIGINAL_REVIEWS else {}
            previous.append(p(key, c, **extra))
    completed = []
    for c in round2.COMPLETED_CLASSES:
        for i in range(4 if c == 3 else 10):
            key = list(round2.NEW_ORIGINAL_REVIEWS)[i] if c == 4 and i < 3 else f'manual-{c}-{i}'
            is_box = key in round2.NEW_ORIGINAL_REVIEWS
            completed.append(p(key, c, human_edited=not is_box, propagation_seed_eligible=c == 3,
                original_rectangle_review_only=is_box, target_expected_revision=1 if is_box else 2))
    new = [p(f'new-{c}-{i}', c, machine_draft=True) for c, n in (new_counts or {}).items() for i in range(n)]
    values = previous+completed+new; (root/'parent_evidence').mkdir(parents=True)
    for name, records in (('parent_evidence/proposals.jsonl', previous),
            ('completed_request_manual_proposals.jsonl', completed), ('new_proposals.jsonl', new), ('proposals.jsonl', values)):
        round2.private_json(root/name, records, True)
    (project/'scripts').mkdir(exist_ok=True)
    helper = project/'scripts/build_mask_template_requests.py'; helper.write_text('# frozen fixture')
    (project/round2.PARENT_VERIFIER).write_text('# independent fixture verifier')
    totals = round2.Counter(p['class_id'] for p in values)
    manifest = dict(publication_version=12, proposals=len(values), previous_proposals=1296, base_proposals=1350,
        new_proposals=len(new), modified_manual_proposals=51, reviewed_original_rectangle=5, saved_human=1258,
        proposals_sha256=round2.digest(root/'proposals.jsonl'),
        frozen_code_sha256={'scripts/build_mask_template_requests.py': round2.digest(helper)},
        classes={str(c): dict(total=totals[c], distinct_frames=totals[c], missing=100-totals[c]) for c in range(1, 23)})
    round2.private_json(root/'manifest.json', manifest)
    round2.private_json(root/'generation_validation.json', dict(
        state='self_checks_passed_not_independent_verification', publication_version=12))
    freeze_parent(root)
    return manifest


def freeze_parent(root):
    round2.private_json(root/'artifact_hashes.json', {n: h for n, h in round2.file_inventory(root).items()
        if n != 'artifact_hashes.json' and n not in round2.PARENT_AUDIT_NAMES})


def seal_parent(root, manifest, project, **extra):
    value = dict(state='passed', publication_version=12, proposals_verified=manifest['proposals'],
        manifest_sha256=round2.digest(root/'manifest.json'), proposals_sha256=round2.digest(root/'proposals.jsonl'),
        verification_script_sha256=round2.digest(project/round2.PARENT_VERIFIER))
    value.update(extra)
    for name in ('verification.json', 'independent_verification.json'):
        round2.private_json(root/name, value)


def test_concrete_pub12_1350_plus_actual_new_and_matching_seal_required_even_for_review(tmp_path, monkeypatch):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    root = tmp_path/'parent'; manifest = make_parent(root, tmp_path, {22: 9})
    assert manifest['proposals'] == 1359
    assert round2.load_parent(root, False)['quotas'] == {4: 10, 5: 10, 7: 10, 14: 10, 22: 2}
    with pytest.raises(ValueError, match='independently verified publication 12'):
        round2.load_parent(root)
    seal_parent(root, manifest, tmp_path)
    data = round2.load_parent(root)
    assert data['new_count'] == 9 and len(data['seals']) == 2
    assert {c: manifest['classes'][str(c)]['total'] for c in round2.COMPLETED_CLASSES} == {
        3: 100, 4: 26, 5: 32, 7: 25, 14: 41, 22: 98}
    rows, pool = {}, {}
    for c in round2.COMPLETED_CLASSES:
        for i in range(12):
            key = f'{c}-{i}'; rows[key] = row(key, c); pool[key] = usable(c != 14)
    keys = round2.select_tasks(rows, pool, data['quotas'])
    assert len(keys) == 42 and len({rows[k]['frame_id'] for k in keys}) == 42
    assert round2.Counter(rows[k]['final_class_id'] for k in keys) == data['quotas']


@pytest.mark.parametrize('attack', ['failed', 'version', 'manifest', 'count', 'verifier', 'artifact', 'conflict'])
def test_parent_seal_provenance_binding_and_conflicting_aliases_fail_closed(tmp_path, monkeypatch, attack):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    root = tmp_path/'parent'; manifest = make_parent(root, tmp_path)
    extra = {'state': 'failed'} if attack == 'failed' else {'publication_version': 11} if attack == 'version' else {}
    extra.update({'manifest_sha256': 'wrong'} if attack == 'manifest' else {})
    extra.update({'proposals_verified': 1296} if attack == 'count' else {})
    extra.update({'verification_script_sha256': 'wrong'} if attack == 'verifier' else {})
    extra.update({'artifact_hashes_sha256': 'wrong'} if attack == 'artifact' else {})
    seal_parent(root, manifest, tmp_path, **extra)
    if attack == 'conflict':
        value = round2.read_json(root/'independent_verification.json'); value['extra'] = True
        round2.private_json(root/'independent_verification.json', value)
    with pytest.raises(ValueError):
        round2.load_parent(root)


@pytest.mark.parametrize('attack', ['hash', 'count', 'quad_seed', 'box_edit', 'missing_box', 'vertex', 'not_complete', 'helper'])
def test_actual_parent_partition_four_eight_points_and_original_review_policy(tmp_path, monkeypatch, attack):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    root = tmp_path/'parent'; manifest = make_parent(root, tmp_path)
    if attack == 'hash':
        (root/'proposals.jsonl').write_text('{}\n')
    elif attack == 'count':
        manifest['proposals'] += 1
    elif attack == 'helper':
        manifest['frozen_code_sha256']['scripts/build_mask_template_requests.py'] = 'wrong'
    elif attack == 'not_complete':
        round2.private_json(root/'generation_validation.json', dict(state='scanning', publication_version=12))
    else:
        completed = round2.read_jsonl(root/'completed_request_manual_proposals.jsonl')
        box = next(p for p in completed if p.get('original_rectangle_review_only'))
        if attack == 'quad_seed':
            completed[-1]['propagation_seed_eligible'] = True
        elif attack == 'box_edit':
            box['human_edited'] = True
        elif attack == 'missing_box':
            box['original_rectangle_review_only'] = False
        else:
            completed[0]['polygon'].pop()
        values = round2.read_jsonl(root/'parent_evidence/proposals.jsonl')+completed
        round2.private_json(root/'completed_request_manual_proposals.jsonl', completed, True)
        round2.private_json(root/'proposals.jsonl', values, True)
        manifest['proposals_sha256'] = round2.digest(root/'proposals.jsonl')
    if attack != 'hash':
        round2.private_json(root/'manifest.json', manifest); freeze_parent(root)
    with pytest.raises(ValueError):
        round2.load_parent(root, False)


@pytest.fixture
def staged(tmp_path, monkeypatch):
    formal = tmp_path/'formal'; formal.mkdir(); rows = {}
    for c in round2.COMPLETED_CLASSES:
        for i in range(4):
            key = f'{c:02x}{i:022x}'; r = row(key, c); rows[key] = r
            Image.fromarray(np.random.default_rng(c+i).integers(0, 255, (40, 100), dtype=np.uint8)).save(formal/r['image'])
    class Evidence:
        used = {}
        sources = {r['frame_id']: dict(image=r['image'], native=True) for r in rows.values()}
        def image(self, frame):
            r = next(r for r in rows.values() if r['frame_id'] == frame)
            return np.asarray(Image.open(formal/r['image']))
    current, raws, history = {}, {}, {}
    old = next(k for k, r in rows.items() if r['final_class_id'] == 14)
    current[old] = pose(rows[old], 8); raws[old] = json.dumps(current[old], indent=2)
    history[old] = [dict(event_id=12, revision=8, original_rectangle=True, original_pose_only=True)]
    pool = {k: usable(k != old) for k in rows if rows[k]['final_class_id'] != 14 or k == old}
    quotas = {4: 3, 5: 2, 7: 1, 14: 1, 22: 1}
    classes = {str(c): dict(total=100-quotas.get(c, 0), distinct_frames=100-quotas.get(c, 0),
        missing=quotas.get(c, 0)) for c in range(1, 23)}
    pm = dict(proposals=1359, new_proposals=9, saved_human=1258, classes=classes)
    parent = tmp_path/'parent'; parent.mkdir()
    for name in ('manifest.json', 'proposals.jsonl'):
        (parent/name).write_text('{}')
    frozen = dict(parent_directory='parent', parent_generation_inventory={'manifest.json': 'm'},
        parent_existing_audit_hashes={}, per_class=10, formal_manifest_sha256='formal', classification_sha256='classification',
        pose_public_url='https://example.test/pose', database_logical_fingerprints={'pose': 'pose', 'gallery': 'gallery'},
        configuration_sha256={'cfg': 'hash'}, protected_formal_files={'formal': 'freeze'},
        snapshots={'pose': {'sha256': 'p'}, 'gallery': {'sha256': 'g'}}, code_sha256={'self': 'frozen'}, old_request_sha256={'old': 'o'})
    data = dict(out=None, parent=dict(manifest=pm, proposals=[{}]*1359, new_count=9,
        seals={'verification.json': 'independent'}, inventory={}), formal=formal, rows=rows,
        original_rows=copy.deepcopy(rows), evidence=Evidence(), current=current, current_raw=raws,
        history=history, protected=set(), protected_frames=set(), usable=pool, quotas=quotas,
        populations={}, rejections={}, frozen=frozen)
    calls = []
    def prepare(out, parent, per_class=10, require_seal=True):
        calls.append(require_seal); data['out'] = out
        return data
    monkeypatch.setattr(round2, 'prepare', prepare)
    monkeypatch.setattr(round2, 'finish_stability', lambda *args: None)
    monkeypatch.setattr(round2, 'protected_snapshot', lambda *args: {'formal': 'freeze'})
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    (tmp_path/'scripts').mkdir(); (tmp_path/'scripts/build_mask_template_requests.py').write_text('# fixture')
    def contact(root, records, name, full=False):
        assert 1 <= len(records) <= 6
        Image.new('RGB', (100, 50)).save(root/name)
    monkeypatch.setattr(round2, 'contact', contact)
    def font(out, text):
        (out/'report-font.otf').write_bytes(b'fixture-font'); (out/'FONT_LICENSE.txt').write_text('fixture-license')
    monkeypatch.setattr(round2, 'build_font', font)
    out = tmp_path/'worklist'; round2.review(out, parent)
    return out, parent, data, calls


def all_records(out):
    return [r for root in round2.round_directories(out) for r in round2.read_jsonl(root/'shortlist.jsonl')]


def write_decisions(out, records=None):
    decisions = {r['target_key']: decision() for r in (records or all_records(out))}
    path = out/'internal/source_visual_decisions.json'
    round2.private_json(path, dict(schema=round2.DECISION_SCHEMA, decisions=decisions))
    return path, decisions


def test_private_review_only_native_montages_at_most_six_no_fabricated_decisions(staged):
    out, _, data, calls = staged; root = round2.round_directories(out)[0]
    assert calls == [True] and len(all_records(out)) == 8
    assert {p.name for p in root.glob('full_*.png')} == {'full_01.png', 'full_07.png'}
    assert not (out/'internal/source_visual_decisions.json').exists()
    assert not (out/'requests.jsonl').exists() and not (out/'manifest.json').exists()
    assert not list(out.rglob('*.sqlite*')) and not (out/'images').exists()
    assert round2.read_json(out/'protected_formal_before.json') == round2.read_json(out/'protected_formal_after.json')
    assert (out/'internal').stat().st_mode & 0o077 == 0
    assert (out/'input_manifest.json').stat().st_mode & 0o077 == 0
    assert all(r['source']['final_class_id'] != 3 for r in all_records(out))
    old = next(r for r in all_records(out) if r['source']['final_class_id'] == 14)
    assert old['existing_pose_revision'] == 8 and not old['no_pose_current_or_history']
    assert old['existing_pose_payload_raw_sha256'] == round2.raw_sha(data['current_raw'][old['target_key']])


def test_restage_replaces_rejected_ambiguous_source_using_explicit_group_preferences(staged):
    out, parent, data, _ = staged; initial = round2.round_directories(out)[0]
    before = round2.file_inventory(initial); path, decisions = write_decisions(out)
    rejected = next(r for r in all_records(out) if r['source']['final_class_id'] == 5)
    decisions[rejected['target_key']] = decision(False, multi3hole_ambiguous=True)
    replacement = next(k for k, r in data['rows'].items() if r['final_class_id'] == 5 and k not in decisions)
    round2.private_json(path, dict(decisions=decisions, preferences=dict(class_groups={'5': [data['rows'][replacement]['group_id']]})))
    result = round2.review(out, parent, decisions_path=path, restage=True)
    assert result['new_shortlist'] == 1 and result['active_shortlist'] == 8
    assert round2.file_inventory(initial) == before and all_records(out)[-1]['target_key'] == replacement
    root2 = round2.round_directories(out)[1]
    assert round2.read_json(root2/'review_input.json')['preferences']['class_groups']['5'] == [data['rows'][replacement]['group_id']]
    decisions[replacement] = decision(); round2.private_json(path, dict(decisions=decisions))
    assert round2.build(out, parent, path)['request_count'] == 8
    tasks = round2.read_jsonl(out/'requests.jsonl')
    assert rejected['target_key'] not in {r['target_key'] for r in tasks}
    assert replacement in {r['target_key'] for r in tasks}
    assert round2.file_inventory(initial) == before
    assert len({r['source']['frame_id'] for r in all_records(out)}) == 9
    r = next(r for r in tasks if r['target_key'] == replacement)
    assert r['source_visual_review_binding']['review_round'] == 'round_0002'


def test_review_rerun_records_decisions_without_refilling_or_overwriting(staged):
    out, parent, _, _ = staged; path, _ = write_decisions(out)
    result = round2.review(out, parent, decisions_path=path)
    assert result['new_shortlist'] == 0 and len(round2.round_directories(out)) == 2
    assert round2.build(out, parent)['request_count'] == 8


def test_rejected_source_reduces_count_instead_of_uninspected_refill(staged):
    out, parent, _, _ = staged; path, decisions = write_decisions(out)
    decisions[next(iter(decisions))] = decision(False, clipped_source=True)
    round2.private_json(path, dict(decisions=decisions))
    assert round2.build(out, parent, path)['request_count'] == 7
    assert sum(round2.read_json(out/'manifest.json')['unfilled_requested_class_counts'].values()) == 1


@pytest.mark.parametrize('attack', ['missing', 'uninspected', 'unknown', 'changed_crop', 'source_changed', 'code_changed'])
def test_build_blocks_unreviewed_changed_or_unknown_inputs_before_public_writes(staged, attack):
    out, parent, data, calls = staged; path, decisions = write_decisions(out); key = next(iter(decisions))
    if attack == 'missing':
        path.unlink()
    elif attack == 'uninspected':
        decisions[key]['full_image_inspected'] = False
    elif attack == 'unknown':
        decisions['unknown'] = decision()
    elif attack == 'changed_crop':
        next((round2.round_directories(out)[0]/'cases').iterdir()).write_bytes(b'changed')
    elif attack == 'source_changed':
        data['frozen']['database_logical_fingerprints']['pose'] = 'same-count-different-raw-event'
    else:
        data['frozen']['code_sha256']['self'] = 'edited-after-review'
    if attack in ('uninspected', 'unknown'):
        round2.private_json(path, dict(decisions=decisions))
    with pytest.raises((ValueError, FileNotFoundError)):
        round2.build(out, parent, path)
    assert calls[-1] is True and not (out/'requests.jsonl').exists() and not (out/'images').exists()


def test_frozen_rejections_cannot_be_reversed_and_round_chain_cannot_disappear(staged):
    out, parent, data, _ = staged; path, decisions = write_decisions(out)
    key = next(iter(decisions)); decisions[key] = decision(False)
    round2.private_json(path, dict(decisions=decisions)); round2.review(out, parent, decisions_path=path, restage=True)
    decisions[key] = decision(); round2.private_json(path, dict(decisions=decisions))
    with pytest.raises(ValueError, match='cannot be rewritten'):
        round2.review(out, parent, decisions_path=path, restage=True)
    first = round2.round_directories(out)[0]; first.rename(first.with_name('removed_round'))
    with pytest.raises(ValueError, match='contiguous'):
        round2.review_history(out, data)


def test_build_actual_counts_full_pose_preservation_exact_public_allowlist_and_pending_marker(staged):
    out, parent, data, calls = staged; path, _ = write_decisions(out)
    result = round2.build(out, parent, path)
    assert calls == [True, True] and result['request_count'] == 8 and result['public_assets'] == 19
    manifest = round2.read_json(out/'manifest.json'); tasks = round2.read_jsonl(out/'requests.jsonl')
    assert manifest['parent_cumulative_count'] == 1359 and manifest['parent_new_machine_drafts'] == 9
    assert manifest['parent_publication_version'] == 12
    assert len(manifest['public_assets']) == 3+2*len(tasks)
    assert not {'requests.jsonl', 'input_manifest.json', 'manifest.json'} & set(manifest['public_assets'])
    assert (out/'requests.jsonl').stat().st_mode & 0o077 == 0
    text = (out/'report_zh.html').read_text()
    for token in ('1359', '1258', '434', '824', '1263', '185', '132', '53', '54', '51', '47',
            '145', '201', '4916', '11698', '890', '639', '383', '599', '554', '45', '42',
            '待独立验收，尚未激活', '原矩形', 'Class 3已满100张', '不是人工改绘', '全部本批类别'):
        assert token in text
    assert text.count('<tr>') == 23 and '<option>3</option>' not in text
    assert 'table-layout:fixed' in text and 'overflow-wrap:anywhere' in text
    for token in ('existing_pose_current_payload', 'current_payload_raw', '.sqlite', 'access_code', 'password'):
        assert token not in text
    for r in tasks:
        assert r['class_id'] in round2.CLASSES and 'polygon' not in r and r['generated_polygons'] == 0
        assert r['classification_is_candidate_not_truth'] and not r['classification_ambiguous']
        assert r['minimum_direct_manual_coverage_vertices'] == 4 and not r['propagation_seed_eligible']
        assert r['valid_four_point_manual_contour_allowed'] and r['extra_vertices_must_not_be_forced']
        assert not r['counts_as_completed_annotation'] and not r['original_rectangle_review_only'] and not r['human_edited']
        assert all(r[f] is False for f in round2.FALSE_FLAGS)
        assert round2.digest(out/r['native_image']) == round2.digest(data['formal']/r['source']['image'])
        binding = r['source_visual_review_binding']; assert binding['decision_sha256'] == round2.sha(r['review_decision'])
        assert binding['native_image_sha256'] == round2.digest(out/r['native_image'])
    old = next(r for r in tasks if r['class_id'] == 14); key = old['target_key']
    assert old['existing_pose_current_payload_raw'] == data['current_raw'][key]
    assert old['existing_pose_current_payload'] == data['current'][key] and old['existing_pose_revision'] == 8
    assert old['existing_pose_payload_raw_sha256'] in text and old['existing_pose_current_payload_raw'] not in text
    assert not list(out.rglob('*.sqlite*')) and not (out/'masks').exists() and not (out/'labelme').exists()
    with pytest.raises(ValueError, match='Never overwrite'):
        round2.review(out, parent, decisions_path=path, restage=True)


def test_self_verify_read_only_never_seal_and_does_not_rewrite_pending_html(staged, monkeypatch):
    out, parent, _, _ = staged; path, _ = write_decisions(out); round2.build(out, parent, path)
    from fontTools import ttLib
    class Font:
        def __init__(self, *args): pass
        def __enter__(self): return self
        def __exit__(self, *args): pass
        def getBestCmap(self): return set(range(0x3400, 0xa000))
    monkeypatch.setattr(ttLib, 'TTFont', Font)
    before = round2.file_inventory(out)
    assert round2.verify(out, parent)['state'] == 'self_checks_passed_not_independent_verification'
    assert round2.file_inventory(out) == before
    assert not (out/'verification.json').exists() and not (out/'independent_verification.json').exists()
    (out/'cases/unlisted.png').write_bytes(b'not-allowlisted')
    with pytest.raises(ValueError, match='file inventory'):
        round2.verify(out, parent)


def test_valid_output_strict_positive_round_namespace_and_no_symlinks(tmp_path, monkeypatch):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path); outputs = tmp_path/'outputs'; outputs.mkdir()
    parent = outputs/'parent'; parent.mkdir()
    assert round2.valid_output(outputs/'pallet_mask_remaining_round2_requests_v1', parent).parent == outputs
    for name in ('pallet_mask_remaining_followup_requests_v1', 'pallet_mask_remaining_round2_requests_v1-extra',
            'pallet_mask_remaining_round2_requests_v0', 'pallet_mask_remaining_round2_requests_v01'):
        with pytest.raises(ValueError):
            round2.valid_output(outputs/name, parent)
    link = outputs/'pallet_mask_remaining_round2_requests_v2'; link.symlink_to(parent)
    with pytest.raises(ValueError):
        round2.valid_output(link, parent)


@pytest.fixture
def source_records(tmp_path):
    current, raws, events, humans, originals = {}, {}, [], [], []
    statements = {name: dict(scoped_request_keys=sorted(keys), reviewed_original_rectangle_keys=sorted(keys),
        statement_zh='明确检查无需修改，原框单列，不是人工改绘或GT。', creates_human_event=False,
        confirms_ground_truth=False, authorizes_propagation=False)
        for name, keys in (('old', round2.OLD_ORIGINAL_REVIEWS), ('new', round2.NEW_ORIGINAL_REVIEWS))}
    (tmp_path/'parent_evidence').mkdir()
    for name, path in (('old', tmp_path/'parent_evidence/review_completion_statement.json'),
            ('new', tmp_path/'review_completion_statement.json')):
        round2.private_json(path, statements[name])
    for key in [f'human-{i}' for i in range(434)]+list(round2.ORIGINAL_REVIEWS):
        r = row(key, round2.ORIGINAL_REVIEWS.get(key, (4, 1))[0])
        value = pose(r, round2.ORIGINAL_REVIEWS.get(key, (4, 1))[1])
        if key not in round2.ORIGINAL_REVIEWS:
            value['polygon'][0][0] += 1
        raw = json.dumps(value, indent=2); current[key] = value; raws[key] = raw
        event_id = len(events)+1; events.append((event_id, key, value['revision'], raw))
        record = dict(target_key=key, source=r, current_revision=value['revision'], current_payload=value,
            current_payload_raw=raw, current_payload_sha256=round2.sha(value), current_payload_raw_sha256=round2.raw_sha(raw),
            human_event_id=event_id, human_polygon_revision=value['revision'], human_event_payload_raw=raw,
            human_event_sha256=round2.raw_sha(raw))
        if key in round2.ORIGINAL_REVIEWS:
            statement = statements['old' if key in round2.OLD_ORIGINAL_REVIEWS else 'new']
            record.update(human_edited=False, propagation_seed_eligible=False, original_rectangle_review_only=True,
                user_statement_sha256=round2.sha(statement))
            originals.append(record)
        else:
            humans.append(record)
    path = tmp_path/'gallery.sqlite3'; gallery_records = []
    with sqlite3.connect(path) 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);')
        for i in range(824):
            key = f'gallery-{i}'; value = dict(target_key=key, revision=1, human_edited=True,
                polygon=[[1, 2], [3, 4], [5, 6], [7, 8]]); raw = json.dumps(value, indent=2)
            db.execute('INSERT INTO reviews VALUES (?,?,?)', (key, 1, raw))
            db.execute('INSERT INTO events VALUES (?,?,?,?)', (i+1, key, 1, raw))
            gallery_records.append(dict(target_key=key, revision=1, current_payload_raw=raw,
                current_payload_raw_sha256=round2.raw_sha(raw), current_payload=value, current_payload_sha256=round2.sha(value),
                human_event_id=i+1, human_event_payload_raw=raw, human_event_sha256=round2.raw_sha(raw)))
    for name, values in (('all_manual_sources.jsonl', humans), ('review_only_source_records.jsonl', originals),
            ('gallery_source_records.jsonl', gallery_records),
            ('parent_evidence/all_manual_sources.jsonl', humans[:383]),
            ('parent_evidence/gallery_source_records.jsonl', gallery_records[:639]),
            ('parent_evidence/reviewed_original_rectangle_source_records.jsonl',
                [r for r in originals if r['target_key'] in round2.OLD_ORIGINAL_REVIEWS])):
        round2.private_json(tmp_path/name, values, True)
    previous_new = [dict(target_key=r['target_key'], polygon=copy.deepcopy(r['current_payload']['polygon']))
        for r in gallery_records[639:]]
    for p in previous_new[:132]:
        p['polygon'][0][0] += 1
    round2.private_json(tmp_path/'parent_evidence/new_proposals.jsonl', previous_new, True)
    return tmp_path, current, raws, events, path


def test_all434_modified_five_separate_originals_and824_gallery_exact_bytes_events(source_records):
    parent, current, raws, events, gallery = source_records; before = round2.file_inventory(parent)
    keys = round2.exact_source_records(parent, current, raws, events, gallery)
    assert len(keys) == 1258 and not keys & set(round2.ORIGINAL_REVIEWS)
    assert round2.file_inventory(parent) == before


@pytest.mark.parametrize('attack', ['pose_raw', 'pose_event', 'gallery_raw', 'gallery_event', 'gallery_scope',
    'box_seed', 'box_edit', 'box_statement', 'old_pins', 'new_uses_old_statement', 'hidden_edited_history',
    'inherited_pose_pin', 'inherited_gallery_pin', 'new_manual_nonlatest_event', 'fake_changed_count'])
def test_source_raw_latest_events_original_statement_and_old_pins_cannot_be_faked(source_records, attack):
    parent, current, raws, events, gallery = source_records
    if attack == 'pose_raw':
        raws['human-0'] = json.dumps(current['human-0'])
    elif attack == 'pose_event':
        i, key, rev, raw = events[0]; events[0] = (i, key, rev, json.dumps(json.loads(raw)))
    elif attack.startswith('gallery_'):
        with sqlite3.connect(gallery) as db:
            if attack == 'gallery_raw':
                db.execute('UPDATE reviews SET payload=? WHERE target_key="gallery-0"', ('{"target_key":"gallery-0","revision":1,"human_edited":true}',))
            elif attack == 'gallery_event':
                db.execute('UPDATE events SET payload="changed" WHERE id=1')
            else:
                db.execute('DELETE FROM reviews WHERE target_key="gallery-0"')
    elif attack == 'hidden_edited_history':
        key = next(iter(round2.NEW_ORIGINAL_REVIEWS)); value = copy.deepcopy(current[key]); value['polygon'][0][0] += 1
        events.insert(0, (9999, key, 0, json.dumps(value)))
    elif attack.startswith('inherited_'):
        name = 'all_manual_sources' if attack == 'inherited_pose_pin' else 'gallery_source_records'
        path = parent/'parent_evidence'/(name+'.jsonl'); records = round2.read_jsonl(path)
        records[0]['human_event_sha256'] = 'wrong'; round2.private_json(path, records, True)
    elif attack == 'new_manual_nonlatest_event':
        key = 'human-433'; value = copy.deepcopy(current[key]); value['revision'] += 1
        raw = json.dumps(value); current[key] = value; raws[key] = raw; events.append((9999, key, value['revision'], raw))
        path = parent/'all_manual_sources.jsonl'; records = round2.read_jsonl(path); r = records[-1]
        r.update(current_revision=value['revision'], current_payload=value, current_payload_raw=raw,
            current_payload_sha256=round2.sha(value), current_payload_raw_sha256=round2.raw_sha(raw))
        round2.private_json(path, records, True)
    elif attack == 'fake_changed_count':
        path = parent/'parent_evidence/new_proposals.jsonl'; records = round2.read_jsonl(path)
        records[0]['polygon'][0][0] -= 1; round2.private_json(path, records, True)
    else:
        path = parent/'review_only_source_records.jsonl'; records = round2.read_jsonl(path)
        new = next(r for r in records if r['target_key'] in round2.NEW_ORIGINAL_REVIEWS)
        if attack == 'box_seed':
            records[0]['propagation_seed_eligible'] = True
        elif attack == 'box_edit':
            records[0]['human_edited'] = True
        elif attack == 'box_statement':
            new['user_statement_sha256'] = 'wrong'
        elif attack == 'new_uses_old_statement':
            new['user_statement_sha256'] = round2.sha(round2.read_json(parent/'parent_evidence/review_completion_statement.json'))
        else:
            records[0]['invented_pin'] = 'must not silently refresh old evidence'
        round2.private_json(path, records, True)
    with pytest.raises(ValueError):
        round2.exact_source_records(parent, current, raws, events, gallery)


@pytest.fixture
def snapshots(tmp_path, monkeypatch):
    monkeypatch.setattr(round2, 'PROJECT', tmp_path)
    parent = tmp_path/'parent'; parent.mkdir(); (parent/'backups').mkdir(); specs, manifest, lives = {}, {}, {}
    for kind, table, saved, events in (('pose', 'annotations', 4916, 11698), ('gallery', 'reviews', 824, 890)):
        live = tmp_path/(kind+'-live.sqlite3')
        with sqlite3.connect(live) as db:
            db.executescript(f'CREATE TABLE {table}(id INTEGER,payload TEXT); CREATE TABLE events(id INTEGER,payload TEXT);'
                'CREATE TABLE metadata(key TEXT,value TEXT);')
            db.executemany(f'INSERT INTO {table} VALUES (?,?)', ((i, '{ "x": 1 }') for i in range(saved)))
            db.executemany('INSERT INTO events VALUES (?,?)', ((i, '{ "x": 1 }') for i in range(events)))
            db.execute('INSERT INTO metadata VALUES ("binding","exact-formal")')
        name = kind+'-snapshot.sqlite3'; frozen = parent/name; backup = parent/'backups'/name
        shutil.copyfile(live, frozen); shutil.copyfile(live, backup); frozen.chmod(0o400); backup.chmod(0o400)
        specs[kind] = dict(path=name, backup='backups/'+name, sha256=round2.digest(frozen), immutable=True, saved=saved, events=events)
        prefix = 'snapshot' if kind == 'pose' else 'gallery_snapshot'
        manifest.update({prefix+'_sha256': round2.digest(frozen), prefix+'_saved': saved, prefix+'_events': events}); lives[kind] = live
    monkeypatch.setattr(round2, 'POSE', lives['pose']); monkeypatch.setattr(round2, 'GALLERY', lives['gallery'])
    round2.private_json(parent/'snapshot_manifest.json', specs)
    return parent, manifest, lives


def test_parent_snapshots_and_immutable_backups_referenced_without_worklist_copies(snapshots):
    parent, manifest, _ = snapshots; before = round2.file_inventory(parent)
    specs, fingerprints = round2.frozen_snapshots(parent, manifest)
    assert fingerprints['pose']['table_counts']['events'] == 11698
    assert fingerprints['gallery']['table_counts']['reviews'] == 824
    assert specs['pose']['path'].startswith('parent/') and round2.file_inventory(parent) == before


@pytest.mark.parametrize('attack', ['raw_event', 'metadata', 'schema', 'backup_bytes', 'backup_writable', 'count'])
def test_snapshot_identity_includes_all_raw_events_metadata_schema_and_backup(snapshots, attack):
    parent, manifest, lives = snapshots
    if attack in ('raw_event', 'metadata', 'schema'):
        with sqlite3.connect(lives['pose']) as db:
            if attack == 'raw_event':
                db.execute('UPDATE events SET payload=? WHERE id=0', ('{"x":1}',))
            elif attack == 'metadata':
                db.execute('UPDATE metadata SET value="wrong"')
            else:
                db.execute('CREATE TABLE extra(value TEXT)')
    elif attack.startswith('backup'):
        path = parent/'backups/pose-snapshot.sqlite3'; path.chmod(0o600)
        if attack == 'backup_bytes':
            path.write_bytes(b'wrong'); path.chmod(0o400)
    else:
        manifest['snapshot_events'] -= 1
    with pytest.raises(ValueError):
        round2.frozen_snapshots(parent, manifest)


@pytest.mark.parametrize('name', ['reviews.sqlite3', 'backups/hidden.db', 'internal/snapshot.sqlite3.backup',
    'internal/database-wal', 'internal/hidden.db.backup', 'internal/disguised.bin'])
def test_database_copies_forbidden_anywhere_even_with_disguised_names(tmp_path, name):
    path = tmp_path/name; path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(b'SQLite format 3\x00' if name.endswith('.bin') else b'no-copy')
    with pytest.raises(ValueError, match='No private database copies'):
        round2.no_database_copies(tmp_path)


def test_code_inventory_freezes_only_relevant_sources_and_parent_verifier(monkeypatch):
    monkeypatch.setattr(round2, 'digest', lambda path: str(path))
    names = set(round2.code_inventory())
    assert {round2.PARENT_VERIFIER, 'scripts/build_mask_remaining_round2_requests.py',
        'scripts/build_mask_remaining_followup_requests.py'} <= names
    assert not any('publish' in n or 'assets/' in n or 'mask_template_requests.py' == n.split('/')[-1]
        and n.startswith('pallet/') for n in names)


def test_no_review_directory_is_created_when_pub12_is_not_independently_sealed(tmp_path, monkeypatch):
    out = tmp_path/'worklist'
    def not_sealed(out, parent, per_class=10, require_seal=True):
        assert require_seal is True
        raise ValueError('Review/build requires independently verified publication 12')
    monkeypatch.setattr(round2, 'prepare', not_sealed)
    with pytest.raises(ValueError, match='independently verified publication 12'):
        round2.review(out, tmp_path/'unsealed-parent')
    assert not out.exists()
