"""Synthetic fixtures only: never stage real candidates or access production stores."""
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_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_and_unchanged_pure_helpers_only():
    assert followup.CLASSES == (3, 4, 5, 7, 14, 22)
    assert followup.COMPLETED_CLASSES == (4, 5, 7, 14, 22)
    assert followup.PARENT.name == 'pallet_mask_remaining_continue_v1'
    assert followup.DEFAULT.name == 'pallet_mask_remaining_followup_requests_v1'
    assert followup.native_quality is direct.native_quality
    assert followup.render_case is direct.render_case
    assert followup.QUALITY_GATES == direct.QUALITY_GATES
    assert not hasattr(followup, 'snapshot_database') and not hasattr(followup, 'attempt_target')


def test_actual_gaps_not_assumed_fifty_or_class22_twenty_one():
    values = [dict(target_key=f'{c}-{i}', class_id=c, source=dict(frame_id=f'{c}-{i}'))
        for c, n in {3: 96, 4: 16, 5: 99, 7: 100, 14: 31, 22: 93}.items() for i in range(n)]
    assert followup.class_quotas(values) == {3: 4, 4: 10, 5: 1, 7: 0, 14: 10, 22: 7}
    values[-1]['source']['frame_id'] = values[0]['source']['frame_id']
    with pytest.raises(ValueError, match='globally distinct'):
        followup.class_quotas(values)


@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_special_history_blocks_entire_frame_across_classes(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, _ = followup.protection(rows, {'a': pose(r, 2)}, [(1, 'a', 1, json.dumps(value))], (), [], [])
    assert protected == {'a'} and frames == {rows['b']['frame_id']}


def test_original_pose_only_history_allowed_but_old_original_review_keys_still_locked():
    keys = list(followup.ORIGINAL_REVIEWS)
    rows = {k: row(k, followup.ORIGINAL_REVIEWS[k][0]) for k in keys}
    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 = followup.protection(rows, current, events, (), [],
        [dict(target_key=k) for k in keys])
    assert protected == set(keys) and len(frames) == 2
    assert history['fresh14'][0]['original_pose_only']


def test_all_namespace_request_discovery_includes_nested_non_mask_names(tmp_path, monkeypatch):
    monkeypatch.setattr(followup, 'PROJECT', tmp_path)
    out = tmp_path/'outputs/pallet_mask_remaining_followup_requests_v1'
    for root in (out, tmp_path/'outputs/unrelated_namespace/archive/request-copy',
            tmp_path/'outputs/pallet_mask_old_v1/parent_evidence'):
        root.mkdir(parents=True); (root/'requests.jsonl').write_text('{}\n')
    paths = followup.old_request_paths(out)
    assert out/'requests.jsonl' not in paths
    assert tmp_path/'outputs/unrelated_namespace/archive/request-copy/requests.jsonl' in paths
    assert tmp_path/'outputs/pallet_mask_old_v1/parent_evidence/requests.jsonl' in paths


def test_preferences_do_not_override_quality_pool_virgin_priority_or_distinct_frames():
    rows = {k: row(k, 14) for k in ('old-pose', 'virgin', 'not-usable')}
    pool = {'old-pose': usable(False), 'virgin': usable()}
    quotas = dict.fromkeys(followup.CLASSES, 0); quotas[14] = 1
    selected = followup.select_tasks(rows, pool, quotas,
        dict(preferred_keys=['not-usable', 'old-pose'], class_groups={'14': ['group-old-pose']}))
    assert selected == ['virgin']
    del pool['virgin']
    assert followup.select_tasks(rows, pool, quotas) == ['old-pose']
    assert followup.select_tasks(rows, pool, quotas, excluded_frames=['frame-old-pose']) == []


def test_group_preferences_and_clear_large_frame_rank_and_diversity():
    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(followup.CLASSES, 0); quotas[4] = 1
    assert followup.select_tasks(rows, pool, quotas) == ['b']
    assert followup.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(followup.select_tasks(rows, pool, quotas)) == {'b', 'c'}


@pytest.mark.parametrize('bad', [
    {'decisions': {'k': {'approved': True}}},
    {'decisions': {'k': dict(decision(), full_image_inspected=False)}},
    {'decisions': {'k': dict(decision(False), note_zh=' ')}},
    {'preferences': {'relax_quality': True}},
    {'preferences': {'class_groups': {'19': ['g']}}},
    {'preferences': {'preferred_keys': ['k', 'k']}},
    {'schema': 'unknown'},
])
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):
        followup.decision_input(path)


def make_parent(root, monkeypatch, new_counts=None):
    # Actual prior class4/5/7/14/22 bases; remaining classes fill the real1061.
    counts = {c: 100 for c in (1, 2, 9, 10, 15)}
    counts.update({4: 6, 5: 12, 7: 5, 14: 21, 22: 69})
    others = [c for c in range(1, 23) if c not in counts]
    counts.update({c: 37+(i < 4) for i, c in enumerate(others)})
    assert sum(counts.values()) == 1061
    def p(key, cls, **extra):
        return dict(target_key=key, class_id=cls, source=dict(frame_id='frame-'+key),
            polygon=[[1, 1], [60, 2], [61, 20], [2, 19]], **extra)
    previous = [p(f'old-{c}-{i}', c) for c, n in counts.items() for i in range(n)]
    completed = []
    for c in followup.COMPLETED_CLASSES:
        special = next((k for k, (cls, _) in followup.ORIGINAL_REVIEWS.items() if cls == c), None)
        for i in range(10):
            key = special if i == 0 and special else f'manual-{c}-{i}'
            is_review = key in followup.ORIGINAL_REVIEWS
            completed.append(p(key, c, human_edited=not is_review, propagation_seed_eligible=False,
                reviewed_original_rectangle=is_review, target_expected_revision=followup.ORIGINAL_REVIEWS[key][1] if is_review else 2))
    new = [p(f'new-{c}-{i}', c, machine_draft=True) for c, n in (new_counts or {3: 58, 5: 70, 22: 21}).items() for i in range(n)]
    all_values = previous+completed+new
    (root/'parent_evidence').mkdir(parents=True)
    for name, values in (('parent_evidence/proposals.jsonl', previous), ('completed_request_manual_proposals.jsonl', completed),
            ('proposals.jsonl', all_values), ('new_proposals.jsonl', new)):
        followup.private_json(root/name, values, True)
    pins = {'scripts/build_mask_template_requests.py': followup.digest(followup.PROJECT/'scripts/build_mask_template_requests.py')}
    total = followup.Counter(p['class_id'] for p in all_values)
    manifest = dict(publication_version=11, proposals=len(all_values), previous_proposals=1061, base_proposals=1111,
        new_proposals=len(new), modified_manual_proposals=48, reviewed_original_rectangle=2, saved_human=1022,
        proposals_sha256=followup.digest(root/'proposals.jsonl'), frozen_code_sha256=pins,
        classes={str(c): dict(total=total[c], distinct_frames=total[c], missing=100-total[c]) for c in range(1, 23)})
    followup.private_json(root/'manifest.json', manifest)
    followup.private_json(root/'generation_validation.json', dict(
        state='self_checks_passed_not_independent_verification', publication_version=11))
    followup.private_json(root/'artifact_hashes.json', followup.file_inventory(root))
    return manifest


def seal_parent(root, manifest, **extra):
    followup.private_json(root/'verification.json', dict(state='passed', publication_version=11,
        proposals_verified=manifest['proposals'], manifest_sha256=followup.digest(root/'manifest.json'),
        proposals_sha256=followup.digest(root/'proposals.jsonl'),
        verification_script_sha256=followup.digest(followup.PROJECT/'scripts/verify_mask_remaining_continue.py'), **extra))


def test_completed_parent_review_without_seal_build_requires_matching_independent_seal(tmp_path, monkeypatch):
    manifest = make_parent(tmp_path, monkeypatch)
    data = followup.load_parent(tmp_path)
    assert data['quotas'] == {3: 4, 4: 10, 5: 8, 7: 10, 14: 10, 22: 0}
    with pytest.raises(ValueError, match='independently verified'):
        followup.load_parent(tmp_path, True)
    seal_parent(tmp_path, manifest)
    assert followup.load_parent(tmp_path, True)['seals']
    seal_parent(tmp_path, manifest, artifact_hashes_sha256='wrong')
    with pytest.raises(ValueError, match='artifact binding'):
        followup.load_parent(tmp_path, True)


def test_concrete_pub11_1296_new185_uses_six_class_fifty_four_quota(tmp_path, monkeypatch):
    manifest = make_parent(tmp_path, monkeypatch, {3: 58, 6: 62, 8: 62, 11: 3})
    assert manifest['proposals'] == 1296 and manifest['new_proposals'] == 185
    parent = followup.load_parent(tmp_path)
    assert parent['quotas'] == {3: 4, 4: 10, 5: 10, 7: 10, 14: 10, 22: 10}
    assert sum(parent['quotas'].values()) == 54
    assert {c: manifest['classes'][str(c)]['total'] for c in followup.CLASSES} == {
        3: 96, 4: 16, 5: 22, 7: 15, 14: 31, 22: 79}
    rows, pool = {}, {}
    for c in followup.CLASSES:
        for i in range(12):
            key = f'{c}-{i}'; rows[key] = row(key, c)
            rows[key].update(split='train' if i % 2 else 'test', policy_id=f'sensor-{i % 3}',
                viewpoint_candidate='front' if i % 2 else 'back', group_id=f'group-{i % 4}')
            pool[key] = usable(c != 14)
    # Every class14 source can be pose-only; no fictitious virgin is required.
    selected = followup.select_tasks(rows, pool, parent['quotas'])
    assert len(selected) == 54 and len({rows[k]['frame_id'] for k in selected}) == 54
    assert followup.Counter(rows[k]['final_class_id'] for k in selected) == parent['quotas']


@pytest.mark.parametrize('attack', ['hash', 'count', 'quad_as_seed', 'box_as_edit', 'missing_box', 'wrong_helper', 'not_complete'])
def test_parent_actual_partition_original_box_policy_and_frozen_helpers_fail_closed(tmp_path, monkeypatch, attack):
    manifest = make_parent(tmp_path, monkeypatch)
    if attack == 'hash':
        (tmp_path/'proposals.jsonl').write_text('{}\n')
    elif attack == 'count':
        manifest['proposals'] += 1
    elif attack == 'wrong_helper':
        manifest['frozen_code_sha256']['scripts/build_mask_template_requests.py'] = 'wrong'
    elif attack == 'not_complete':
        followup.private_json(tmp_path/'generation_validation.json', dict(state='scanning', publication_version=11))
    else:
        completed = followup.read_jsonl(tmp_path/'completed_request_manual_proposals.jsonl')
        special = next(p for p in completed if p.get('reviewed_original_rectangle'))
        if attack == 'quad_as_seed':
            completed[-1]['propagation_seed_eligible'] = True
        elif attack == 'box_as_edit':
            special['human_edited'] = True
        else:
            special['reviewed_original_rectangle'] = False
        all_values = followup.read_jsonl(tmp_path/'parent_evidence/proposals.jsonl')+completed+followup.read_jsonl(tmp_path/'new_proposals.jsonl')
        followup.private_json(tmp_path/'completed_request_manual_proposals.jsonl', completed, True)
        followup.private_json(tmp_path/'proposals.jsonl', all_values, True)
        manifest['proposals_sha256'] = followup.digest(tmp_path/'proposals.jsonl')
    if attack != 'hash':
        followup.private_json(tmp_path/'manifest.json', manifest)
        followup.private_json(tmp_path/'artifact_hashes.json', {n: h for n, h in followup.file_inventory(tmp_path).items() if n != 'artifact_hashes.json'})
    with pytest.raises(ValueError):
        followup.load_parent(tmp_path)


@pytest.fixture
def staged(tmp_path, monkeypatch):
    formal = tmp_path/'formal'; formal.mkdir()
    rows = {}
    for c in followup.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 = {3: 0, 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=1204, new_proposals=93, saved_human=1022, 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': 'all-pose', 'gallery': 'all-gallery'},
        configuration_sha256={'cfg': 'hash'}, protected_formal_files={'formal': 'freeze'},
        snapshots={'pose': {'sha256': 'p'}, 'gallery': {'sha256': 'g'}}, code_sha256={'self': 'frozen-code'}, old_request_sha256={'old': 'o'})
    data = dict(out=None, parent=dict(manifest=pm, proposals=[{}]*1204, new_count=93,
        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=False):
        calls.append(require_seal); data['out'] = out
        return data
    monkeypatch.setattr(followup, 'prepare', prepare)
    monkeypatch.setattr(followup, 'finish_stability', lambda *args: None)
    monkeypatch.setattr(followup, 'protected_snapshot', lambda *args: {'formal': 'freeze'})
    monkeypatch.setattr(followup, '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(followup, '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(followup, 'build_font', font)
    out = tmp_path/'worklist'; followup.review(out, parent)
    return out, parent, data, calls


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


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


def test_private_review_immutable_native_montages_six_and_no_fabricated_inspection(staged):
    out, _, data, calls = staged
    assert calls == [False]
    root = followup.round_directories(out)[0]
    assert 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 followup.read_json(out/'protected_formal_before.json') == followup.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
    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'] == followup.raw_sha(data['current_raw'][old['target_key']])


def test_restage_replaces_rejection_and_preserves_all_prior_files_and_decisions(staged):
    out, parent, data, _ = staged
    initial = followup.round_directories(out)[0]; before = followup.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)
    replacement = next(k for k, r in data['rows'].items() if r['final_class_id'] == 5 and k not in decisions)
    followup.private_json(path, dict(decisions=decisions, preferences=dict(class_groups={'5': [data['rows'][replacement]['group_id']]})))
    result = followup.review(out, parent, decisions_path=path, restage=True)
    assert result['new_shortlist'] == 1 and result['active_shortlist'] == 8
    assert followup.file_inventory(initial) == before
    assert all_records(out)[-1]['target_key'] == replacement
    path, _ = write_decisions(out)
    incoming = followup.read_json(path); incoming['decisions'][rejected['target_key']] = decision(False)
    followup.private_json(path, incoming)
    assert followup.build(out, parent, path)['request_count'] == 8
    tasks = followup.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 followup.file_inventory(initial) == before
    assert len({r['source']['frame_id'] for r in all_records(out)}) == 9


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


def test_rejected_source_can_honestly_reduce_published_count_without_uninspected_refill(staged):
    out, parent, _, _ = staged
    path, decisions = write_decisions(out)
    decisions[next(iter(decisions))] = decision(False)
    followup.private_json(path, dict(decisions=decisions))
    assert followup.build(out, parent, path)['request_count'] == 7
    assert sum(followup.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(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['not-shortlisted'] = decision()
    elif attack == 'changed_crop':
        next((followup.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'):
        followup.private_json(path, dict(decisions=decisions))
    with pytest.raises((ValueError, FileNotFoundError)):
        followup.build(out, parent, path)
    assert calls[-1] is True and not (out/'requests.jsonl').exists()


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


def test_build_actual_counts_private_payload_exact_public_assets_dynamic_pending_page(staged):
    out, parent, data, calls = staged
    path, _ = write_decisions(out, classification_ambiguous=True)
    result = followup.build(out, parent, path)
    assert calls == [False, True] and result['request_count'] == 8 and result['public_assets'] == 19
    manifest = followup.read_json(out/'manifest.json'); tasks = followup.read_jsonl(out/'requests.jsonl')
    assert manifest['parent_cumulative_count'] == 1204 and manifest['parent_new_machine_drafts'] == 93
    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 ('1204', '93', '1022', '605', '369', '48', '269', '待独立验收', '原矩形', 'Class 5', '不是人工改绘', '全部本批类别'):
        assert token in text
    assert text.count('<tr>') == 23 and '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 'polygon' not in r and r['generated_polygons'] == 0
        assert r['classification_is_candidate_not_truth'] and r['classification_ambiguous']
        assert r['minimum_direct_manual_coverage_vertices'] == 4
        assert r['valid_four_point_manual_contour_allowed'] and not r['propagation_seed_eligible']
        assert r['extra_vertices_must_not_be_forced'] and not r['counts_as_completed_annotation']
        assert r['future_explicit_original_rectangle_no_change_review_requires_separate_metadata']
        assert not r['original_rectangle_review_only'] and not r['human_edited']
        assert all(r[f] is False for f in followup.FALSE_FLAGS)
        assert followup.digest(out/r['native_image']) == followup.digest(data['formal']/r['source']['image'])
    old = next(r for r in tasks if r['class_id'] == 14)
    assert old['existing_pose_payload_raw_sha256'] in text and old['existing_pose_current_payload_raw'] not in text
    assert old['existing_pose_revision'] == 8
    assert not list(out.rglob('*.sqlite*')) and not (out/'masks').exists() and not (out/'labelme').exists()
    with pytest.raises(ValueError, match='Never overwrite'):
        followup.review(out, parent, decisions_path=path, restage=True)


def test_self_verify_is_read_only_and_never_independent_seal(staged, monkeypatch):
    out, parent, _, _ = staged
    path, _ = write_decisions(out); followup.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 = followup.file_inventory(out)
    assert followup.verify(out, parent)['state'] == 'self_checks_passed_not_independent_verification'
    assert followup.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'):
        followup.verify(out, parent)


def test_valid_output_rejects_other_namespace_and_symlinks(tmp_path, monkeypatch):
    monkeypatch.setattr(followup, 'PROJECT', tmp_path)
    outputs = tmp_path/'outputs'; outputs.mkdir()
    parent = outputs/'parent'; parent.mkdir()
    assert followup.valid_output(outputs/'pallet_mask_remaining_followup_requests_v1', parent).parent == outputs
    for out in (outputs/'pallet_mask_remaining_direct_requests_v1', parent/'pallet_mask_remaining_followup_requests_v1'):
        with pytest.raises(ValueError):
            followup.valid_output(out, parent)
    link = outputs/'pallet_mask_remaining_followup_requests_v2'; link.symlink_to(parent)
    with pytest.raises(ValueError):
        followup.valid_output(link, parent)


@pytest.fixture
def source_records(tmp_path):
    current, raws, events, humans, originals = {}, {}, [], [], []
    for key in [f'human-{i}' for i in range(383)]+list(followup.ORIGINAL_REVIEWS):
        r = row(key, followup.ORIGINAL_REVIEWS.get(key, (4, 1))[0])
        value = pose(r, followup.ORIGINAL_REVIEWS.get(key, (4, 1))[1])
        if key not in followup.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=followup.sha(value), current_payload_raw_sha256=followup.raw_sha(raw),
            human_event_id=event_id, human_polygon_revision=value['revision'], human_event_payload_raw=raw,
            human_event_sha256=followup.raw_sha(raw))
        if key in followup.ORIGINAL_REVIEWS:
            record.update(human_edited=False, propagation_seed_eligible=False, original_rectangle_review_only=True)
            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(639):
            key = f'gallery-{i}'; value = dict(target_key=key, revision=1, human_edited=True)
            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=followup.raw_sha(raw), current_payload=value, current_payload_sha256=followup.sha(value),
                human_event_id=i+1, human_event_payload_raw=raw, human_event_sha256=followup.raw_sha(raw)))
    for name, values in (('all_manual_sources.jsonl', humans), ('reviewed_original_rectangle_source_records.jsonl', originals),
            ('gallery_source_records.jsonl', gallery_records)):
        followup.private_json(tmp_path/name, values, True)
    return tmp_path, current, raws, events, path


def test_all383_modified_two_original_and639_gallery_exact_raw_current_event_sources(source_records):
    parent, current, raws, events, gallery = source_records
    before = followup.file_inventory(parent)
    keys = followup.exact_source_records(parent, current, raws, events, gallery)
    assert len(keys) == 1022 and not keys & set(followup.ORIGINAL_REVIEWS)
    assert followup.file_inventory(parent) == before


@pytest.mark.parametrize('attack', ['pose_raw', 'pose_event', 'gallery_raw', 'gallery_event', 'box_seed', 'box_edit', 'gallery_scope'])
def test_counted_source_raw_event_or_original_box_inference_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"')
    else:
        path = parent/'reviewed_original_rectangle_source_records.jsonl'
        records = followup.read_jsonl(path)
        records[0]['propagation_seed_eligible' if attack == 'box_seed' else 'human_edited'] = True
        followup.private_json(path, records, True)
    with pytest.raises(ValueError):
        followup.exact_source_records(parent, current, raws, events, gallery)


@pytest.fixture
def snapshots(tmp_path, monkeypatch):
    monkeypatch.setattr(followup, 'PROJECT', tmp_path)
    parent = tmp_path/'parent'; parent.mkdir(); (parent/'backups').mkdir()
    specs, manifest, live_paths = {}, {}, {}
    for kind, table, saved, events in (('pose', 'annotations', 4882, 11553), ('gallery', 'reviews', 639, 689)):
        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'; snapshot = parent/name; backup = parent/'backups'/name
        shutil.copyfile(live, snapshot); shutil.copyfile(live, backup); snapshot.chmod(0o400); backup.chmod(0o400)
        specs[kind] = dict(path=name, backup='backups/'+name, sha256=followup.digest(snapshot),
            immutable=True, saved=saved, events=events)
        prefix = 'snapshot' if kind == 'pose' else 'gallery_snapshot'
        manifest.update({prefix+'_sha256': followup.digest(snapshot), prefix+'_saved': saved, prefix+'_events': events})
        live_paths[kind] = live
    monkeypatch.setattr(followup, 'POSE', live_paths['pose']); monkeypatch.setattr(followup, 'GALLERY', live_paths['gallery'])
    followup.private_json(parent/'snapshot_manifest.json', specs)
    return parent, manifest, live_paths


def test_parent_snapshots_and_immutable_backups_reused_without_copies(snapshots):
    parent, manifest, _ = snapshots
    before = followup.file_inventory(parent)
    specs, fingerprints = followup.frozen_snapshots(parent, manifest)
    assert fingerprints['pose']['table_counts']['events'] == 11553
    assert fingerprints['gallery']['table_counts']['reviews'] == 639
    assert specs['pose']['path'].startswith('parent/')
    assert followup.file_inventory(parent) == before


@pytest.mark.parametrize('attack', ['raw_event', 'metadata', 'schema', 'backup_bytes', 'backup_writable', 'count'])
def test_snapshot_logical_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):
        followup.frozen_snapshots(parent, manifest)


@pytest.mark.parametrize('name', ['reviews.sqlite3', 'backups/hidden.db', 'internal/snapshot.sqlite3.backup', 'internal/database-wal'])
def test_database_artifacts_forbidden_even_under_private_backups(tmp_path, name):
    path = tmp_path/name; path.parent.mkdir(parents=True, exist_ok=True); path.write_bytes(b'no-copy')
    with pytest.raises(ValueError, match='No private database copies'):
        followup.no_database_copies(tmp_path)
