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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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""Pydantic data model + JSON-backed store for the rare-event miner.

Follows the record/store pattern of sensorflow.evaluation.records (thread-safe
in-memory collections with JSON persistence under runs/raremine/), and the
lineage/report shapes of sensorflow.megaeval.
"""

from __future__ import annotations

import json
import threading
import uuid
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional

from pydantic import BaseModel, Field

STORE_DIR = Path("runs/raremine")

# ------------------------------------------------------------------ vocabularies

COSTUME_TYPES = [
    "mascot",
    "inflatable",
    "animal",
    "character",
    "robot_armor",
    "oversized",
    "large_prop",
]

CONFOUNDER_TYPES = [
    "mascot_statue",
    "inflatable_decoration",
    "roadside_advertisement",
    "mannequin",
    "sign",
    "construction_object",
]

MODALITIES = [
    "rgb",
    "multi_camera",
    "lidar_projection",
    "lidar_intensity",
    "point_cloud",
    "fusion_view",
    "temporal_sequence",
    "baseline_predictions",
    "gt_annotations",
]

# Modalities that may carry visual/sensor evidence (predictions & GT are data
# availability flags, not evidence sources for the proposer).
EVIDENCE_MODALITIES = [
    "rgb",
    "multi_camera",
    "lidar_projection",
    "lidar_intensity",
    "point_cloud",
    "fusion_view",
    "temporal_sequence",
]

SilhouetteDeviation = Literal["LOW", "MODERATE", "HIGH", "EXTREME"]
OcclusionLevel = Literal["NONE", "PARTIAL", "HEAVY", "EXTREME"]
OcclusionSource = Literal["NONE", "COSTUME_INDUCED", "ENVIRONMENTAL", "COMBINED"]
Difficulty = Literal["EASY", "MODERATE", "HARD", "EXTREME"]
Priority = Literal["LOW", "MEDIUM", "HIGH", "CRITICAL"]
EvidenceQuality = Literal["LOW", "MEDIUM", "HIGH"]

LIGHTING = ["day", "night"]
WEATHER = ["clear", "rain", "fog"]
CONTEXTS = ["sidewalk", "crosswalk", "road_edge", "event_area"]
SAFETY_CONTEXTS = {"crosswalk", "road_edge"}

FAILURE_MODES = [
    "FALSE_NEGATIVE",
    "MISCLASSIFICATION_VEHICLE",
    "MISCLASSIFICATION_ANIMAL",
    "MISCLASSIFICATION_BACKGROUND",
    "LOCALIZATION_ERROR",
    "LOW_CONFIDENCE",
    "TRACKING_FAILURE",
]

DESTINATIONS = [
    "NO_ACTION",
    "REVIEW_QUEUE",
    "RARE_EVENT_DATASET",
    "HARD_EXAMPLE_DATASET",
    "REGRESSION_EVALUATION_SET",
    "SAFETY_CRITICAL_EVALUATION_SET",
    "TRAINING_CANDIDATE",
]

# Anything landing in these sets is protected: leakage into training requires
# an explicit, audited governance override.
PROTECTED_EVAL_DESTINATIONS = {
    "REGRESSION_EVALUATION_SET",
    "SAFETY_CRITICAL_EVALUATION_SET",
}

STAGES = [
    "RAW",
    "MINED",
    "DEDUPLICATED",
    "AUTO_VALIDATED",
    "HUMAN_VALIDATED",
    "CURATED",
    "ARCHIVED",
]


def now_iso() -> str:
    return datetime.now(timezone.utc).isoformat()


def new_id(prefix: str) -> str:
    return f"{prefix}-{uuid.uuid4().hex[:10]}"


# ------------------------------------------------------------------ scene bank


class SceneObject(BaseModel):
    """One object in a scene. `truth_*` fields are the planted ground truth
    used ONLY by validation/statistics — the miner consumes `observables`."""

    object_id: str
    track_id: str = ""
    # planted truth (hidden from the miner)
    truth_kind: Literal["normal_pedestrian", "costumed_pedestrian", "confounder"]
    truth_costume_type: Optional[str] = None  # COSTUME_TYPES or CONFOUNDER_TYPES
    truth_silhouette_deviation: SilhouetteDeviation = "LOW"
    truth_occlusion_env: OcclusionLevel = "NONE"
    truth_occlusion_costume: OcclusionLevel = "NONE"
    truth_is_moving: bool = True
    # shared scene-truth attributes (also observable)
    distance_m: float = 15.0
    context: str = "sidewalk"
    # schematic geometry for the UI (BEV metres + normalized 2D bbox)
    position: List[float] = Field(default_factory=lambda: [10.0, 0.0])
    bbox_2d: List[float] = Field(default_factory=lambda: [0.4, 0.4, 0.1, 0.25])
    # per-modality observable features; keys MUST be available scene modalities
    observables: Dict[str, Dict[str, Any]] = Field(default_factory=dict)


class BaselinePrediction(BaseModel):
    prediction_id: str
    object_id: Optional[str] = None  # None => spurious detection
    predicted_class: str = "pedestrian"  # vehicle | animal | background | none
    confidence: float = 0.9
    # offset between predicted box and the sensed object cluster (observable)
    localization_offset_m: float = 0.1
    track_stable: bool = True
    # truth-only fields, used by quantitative validation — never by the miner
    iou_with_gt: Optional[float] = None
    planted_failure: Optional[str] = None


class GtBox(BaseModel):
    object_id: str
    class_name: str = "pedestrian"
    is_costumed: bool = False
    costume_type: Optional[str] = None


class Scene(BaseModel):
    scene_id: str
    bank_id: str
    sequence_id: str
    frame_index: int = 0
    lighting: str = "day"
    weather: str = "clear"
    modalities: Dict[str, bool] = Field(default_factory=dict)
    objects: List[SceneObject] = Field(default_factory=list)
    baseline_predictions: List[BaselinePrediction] = Field(default_factory=list)
    gt_boxes: List[GtBox] = Field(default_factory=list)
    near_duplicate_of: Optional[str] = None  # scene_id of the original frame


class SceneBank(BaseModel):
    bank_id: str
    seed: int = 7
    created_at: str = Field(default_factory=now_iso)
    num_scenes: int = 0
    num_sequences: int = 0
    num_planted_rare: int = 0  # track-level planted costumed pedestrians
    num_confounders: int = 0
    generation_params: Dict[str, Any] = Field(default_factory=dict)


# ------------------------------------------------------------------ candidates


class Evidence(BaseModel):
    modality: str  # one of EVIDENCE_MODALITIES
    description: str


class AlternativeHypothesis(BaseModel):
    hypothesis: str
    confidence: float
    status: Literal["RETAINED", "REJECTED"]
    reason: str


class TemporalValidation(BaseModel):
    available: bool = False
    status: Literal["VALIDATED", "NOT_AVAILABLE", "INCONCLUSIVE"] = "NOT_AVAILABLE"
    evidence: List[str] = Field(default_factory=list)


class ObservedModelBehavior(BaseModel):
    """ONLY populated when baseline predictions were supplied for the scene.
    Strictly separated from `predicted_failure_mode` (the miner's forecast)."""

    failure_observed: bool = False
    failure_modes: List[str] = Field(default_factory=list)
    details: List[str] = Field(default_factory=list)


class Candidate(BaseModel):
    candidate_id: str
    bank_id: str
    scene_id: str
    sequence_id: str
    object_id: str
    track_id: str = ""
    frame_index: int = 0
    run_id: str = ""

    edge_case_detected: bool = False
    insufficient_visual_evidence: bool = False
    event_type: str = "none"  # costumed_pedestrian | none
    costume_type: List[str] = Field(default_factory=list)

    # THREE separate confidences — never collapsed into one number.
    confidence_human_identity: float = 0.0
    confidence_costume: float = 0.0
    confidence_rare_event: float = 0.0

    visual_evidence: List[Evidence] = Field(default_factory=list)
    human_identity_evidence: List[Evidence] = Field(default_factory=list)
    alternative_hypotheses: List[AlternativeHypothesis] = Field(default_factory=list)

    silhouette_deviation: SilhouetteDeviation = "LOW"
    occlusion_level: OcclusionLevel = "NONE"
    occlusion_source: OcclusionSource = "NONE"
    visible_human_features: List[str] = Field(default_factory=list)
    occluded_human_features: List[str] = Field(default_factory=list)
    unusual_geometry: List[str] = Field(default_factory=list)

    temporal_validation: TemporalValidation = Field(default_factory=TemporalValidation)
    location: Dict[str, Any] = Field(default_factory=dict)

    perception_difficulty: Difficulty = "EASY"
    difficulty_evidence: List[str] = Field(default_factory=list)

    observed_model_behavior: Optional[ObservedModelBehavior] = None
    predicted_failure_mode: Optional[str] = None

    curation_priority: Priority = "LOW"
    priority_reason: str = ""
    recommended_dataset_destination: str = "NO_ACTION"
    requires_human_validation: bool = False
    human_validation_reason: str = ""
    evidence_quality: EvidenceQuality = "LOW"
    rank_in_scene: int = 1


class RepresentativeFrames(BaseModel):
    best_evidence: Optional[str] = None  # scene_id
    worst_case: Optional[str] = None
    model_failure: Optional[str] = None
    minimal_set: List[str] = Field(default_factory=list)


class TrackCandidate(BaseModel):
    """One consolidated candidate per (sequence, track): the unit that flows
    through the validation pipeline."""

    track_candidate_id: str
    bank_id: str
    run_id: str = ""
    sequence_id: str
    track_id: str
    object_id: str
    frame_count: int = 1
    duration_frames: int = 1
    frame_candidate_ids: List[str] = Field(default_factory=list)
    representative: Candidate  # highest-evidence frame-level proposal
    representative_frames: RepresentativeFrames = Field(default_factory=RepresentativeFrames)
    max_visibility: EvidenceQuality = "LOW"
    max_difficulty: Difficulty = "EASY"

    # lifecycle
    stage: str = "MINED"
    duplicate_of: Optional[str] = None
    diversity_selected: bool = False
    auto_validation: Optional[Dict[str, Any]] = None
    human_validation: Optional[Dict[str, Any]] = None
    destination: str = "REVIEW_QUEUE"


class LineageRecord(BaseModel):
    lineage_id: str
    track_candidate_id: str
    source_frame_id: str
    source_sequence_id: str
    dataset_version: str = "scenebank-v1"
    curation_timestamp: str = Field(default_factory=now_iso)
    curator: str = "raremine-pipeline"
    validation_status: str = "PENDING"  # PENDING | AUTO_COHERENT | AUTO_INCOHERENT | APPROVED | REJECTED
    training_eligible: bool = False
    evaluation_eligible: bool = False
    protected_evaluation: bool = False
    destination: str = "REVIEW_QUEUE"
    governance_overrides: List[Dict[str, str]] = Field(default_factory=list)


class MiningRun(BaseModel):
    run_id: str
    bank_id: str
    created_at: str = Field(default_factory=now_iso)
    config: Dict[str, Any] = Field(default_factory=dict)
    num_frame_candidates: int = 0
    num_track_candidates: int = 0
    num_duplicates: int = 0
    num_diversity_selected: int = 0


class AuditEvent(BaseModel):
    event_id: str
    timestamp: str = Field(default_factory=now_iso)
    actor: str = "system"
    action: str = ""
    entity_type: str = ""
    entity_id: str = ""
    detail: str = ""


# ------------------------------------------------------------------ store

_COLLECTIONS = {
    "banks": SceneBank,
    "scenes": Scene,
    "runs": MiningRun,
    "candidates": Candidate,
    "track_candidates": TrackCandidate,
    "lineage": LineageRecord,
    "audit_events": AuditEvent,
}

_KEY_FIELDS = {
    "banks": "bank_id",
    "scenes": "scene_id",
    "runs": "run_id",
    "candidates": "candidate_id",
    "track_candidates": "track_candidate_id",
    "lineage": "lineage_id",
    "audit_events": "event_id",
}


class RareMineStore:
    """Thread-safe in-memory store with JSON persistence (EvalStore pattern)."""

    def __init__(self, base_dir: Path = STORE_DIR):
        self.base_dir = Path(base_dir)
        self.lock = threading.RLock()
        self.collections: Dict[str, Dict[str, BaseModel]] = {c: {} for c in _COLLECTIONS}
        self.meta: Dict[str, Any] = {}
        self._load()

    def _path(self) -> Path:
        return self.base_dir / "store.json"

    def _load(self) -> None:
        path = self._path()
        if not path.exists():
            return
        try:
            with open(path) as f:
                raw = json.load(f)
        except Exception:
            return
        with self.lock:
            for name, model_cls in _COLLECTIONS.items():
                parsed: Dict[str, BaseModel] = {}
                for key, payload in raw.get(name, {}).items():
                    try:
                        parsed[key] = model_cls.model_validate(payload)
                    except Exception:
                        continue
                self.collections[name] = parsed
            self.meta = raw.get("_meta", {})

    def save(self) -> None:
        with self.lock:
            payload = {
                name: {k: v.model_dump() for k, v in items.items()}
                for name, items in self.collections.items()
            }
            payload["_meta"] = self.meta
        self.base_dir.mkdir(parents=True, exist_ok=True)
        tmp = self._path().with_suffix(".tmp")
        with open(tmp, "w") as f:
            json.dump(payload, f)
        tmp.replace(self._path())

    def put(self, collection: str, item: BaseModel) -> None:
        key = getattr(item, _KEY_FIELDS[collection])
        with self.lock:
            self.collections[collection][key] = item

    def get(self, collection: str, key: str) -> Optional[Any]:
        with self.lock:
            return self.collections[collection].get(key)

    def all(self, collection: str) -> List[Any]:
        with self.lock:
            return list(self.collections[collection].values())

    def where(self, collection: str, **filters: Any) -> List[Any]:
        with self.lock:
            items = list(self.collections[collection].values())
        return [it for it in items
                if all(getattr(it, k, None) == v for k, v in filters.items())]

    def clear(self, collection: str) -> None:
        with self.lock:
            self.collections[collection] = {}

    def audit(self, action: str, entity_type: str, entity_id: str,
              detail: str = "", actor: str = "system") -> None:
        self.put("audit_events", AuditEvent(
            event_id=new_id("rmaudit"), actor=actor, action=action,
            entity_type=entity_type, entity_id=entity_id, detail=detail))


_STORE: Optional[RareMineStore] = None
_STORE_LOCK = threading.Lock()


def get_store() -> RareMineStore:
    global _STORE
    with _STORE_LOCK:
        if _STORE is None:
            _STORE = RareMineStore()
        return _STORE


def reset_store(base_dir: Optional[Path] = None) -> RareMineStore:
    """Used by tests to get a fresh isolated store."""
    global _STORE
    with _STORE_LOCK:
        _STORE = RareMineStore(base_dir or STORE_DIR)
        return _STORE