File size: 3,120 Bytes
2f382c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Apply human-reviewed accessibility category overrides by sample id."""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any, Mapping


PROJECT_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_CATEGORY_OVERRIDES_PATH = PROJECT_ROOT / "configs" / "accessibility_review_overrides.json"
GEOMETRY_REVIEW_REQUIRED_CATEGORIES = frozenset(
    {"stairs", "ramp", "curb_cut", "raised_curb"}
)


def load_reviewed_category_overrides(
    path: str | Path | None = None,
) -> dict[str, dict[str, Any]]:
    source = DEFAULT_CATEGORY_OVERRIDES_PATH if path is None else Path(path)
    if not source.is_absolute():
        source = PROJECT_ROOT / source
    if not source.is_file():
        return {}
    value = json.loads(source.read_text(encoding="utf-8"))
    if not isinstance(value, dict):
        raise ValueError(f"Category override config must contain an object: {source}")
    return {
        str(sample_id): entry
        for sample_id, entry in value.items()
        if isinstance(entry, dict)
    }


def resolve_reviewed_category(
    sample_id: str,
    fallback_category: str,
    overrides: Mapping[str, Mapping[str, Any]],
) -> str:
    fallback_category = str(fallback_category or "").strip().lower()
    entry = overrides.get(sample_id)
    if not entry or entry.get("category_reviewed") is not True:
        return fallback_category
    category = entry.get("category")
    if not isinstance(category, str) or not category.strip():
        raise ValueError(
            f"Reviewed category override for {sample_id!r} must contain a non-empty category"
        )
    return category.strip().lower()


def geometry_category_is_explicitly_reviewed(
    sample_id: str,
    metadata: Mapping[str, Any] | None,
    overrides: Mapping[str, Mapping[str, Any]],
) -> bool:
    """Return whether the geometry-driving category itself was reviewed."""
    if metadata and metadata.get("category_reviewed") is True:
        return True
    override = overrides.get(sample_id)
    return bool(override and override.get("category_reviewed") is True)


def resolve_geometry_category_for_modeling(
    sample_id: str,
    fallback_category: str,
    metadata: Mapping[str, Any] | None,
    overrides: Mapping[str, Mapping[str, Any]],
    *,
    allow_unreviewed_level_change: bool = False,
) -> str:
    """Resolve category and fail closed for unreviewed level-change geometry."""
    category = resolve_reviewed_category(sample_id, fallback_category, overrides)
    if not category:
        raise ValueError(f"Sample {sample_id!r} has no category for 3D geometry routing")
    if (
        not allow_unreviewed_level_change
        and category in GEOMETRY_REVIEW_REQUIRED_CATEGORIES
        and not geometry_category_is_explicitly_reviewed(sample_id, metadata, overrides)
    ):
        raise ValueError(
            f"Sample {sample_id!r} category {category!r} requires explicit category review "
            "before selecting a 3D geometry prior. Run "
            "tools/audit_accessibility_geometry_categories.py or add a reviewed override."
        )
    return category