diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6d4b25a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +/ptz_tracker_modular/alarm_boundary.py text eol=lf +/ptz_tracker_modular/alarm_eligibility.py text eol=lf +/ptz_tracker_modular/frame_time.py text eol=lf +/ptz_tracker_modular/inspection_bridge.py text eol=lf +/ptz_tracker_modular/inspection_handoff.py text eol=lf +/ptz_tracker_modular/inspection_input.py text eol=lf +/ptz_tracker_modular/inspection_runtime.py text eol=lf +/ptz_tracker_modular/model_class_contract.py text eol=lf +/ptz_tracker_modular/unknown_inspection.py text eol=lf +/ptz_tracker_modular/inspection_startup.py text eol=lf +/integration/unknown_r3/server.patch text eol=lf -whitespace diff --git a/integration/unknown_r3/README.md b/integration/unknown_r3/README.md new file mode 100644 index 0000000..245ffba --- /dev/null +++ b/integration/unknown_r3/README.md @@ -0,0 +1,24 @@ +# UNKNOWN R3 + +Этот пакет предназначен для офлайн-проверки на снимке кода объекта. Ветка `main` содержит другую версию контроллера. Патч привязан к шести файлам снимка от 9 ноября 2026 года и проверяет SHA-256 входных и выходных файлов. Входной `streaming.py` должен быть снят с фактического Docker bind. Исправление prezoom retention в этот пакет не входит. + +## Проверка копии + +```sh +python -B integration/unknown_r3/build_review_copy.py /path/to/server-source /path/to/new-review-copy +cd /path/to/new-review-copy +python -B test_handoff.py +python -B test_alarm_transport.py +python -B test_composition.py +python -B test_server_path.py +``` + +Нужны Python 3.11 или новее, Git, NumPy и requests для тестового окружения. Тесты подменяют WebSocket, HTTP, инференс и управление. Последний тест использует локальный цикл событий asyncio и проходит через реальные функции декодера и очереди. + +## Поведение + +Детектор класса `airborne_object` даёт геометрию для центровки и ограниченного зума до определения класса. Кадр сохраняет время локального декодирования, последовательность, источник и владельца при проходе через очередь и OSTrack. Классификатор обрабатывает кропы отдельно. Решение о тревоге требует свежего независимого подтверждения БПЛА на двух кадрах и повторной проверки при отправке. Птица и пилотируемый самолёт не дают тревогу. + +`UNKNOWN_INSPECTION_ENABLE` по умолчанию выключен. Для запуска нужны проверенные локальные `.pt` веса двух моделей, `UNKNOWN_INSPECTION_SETTINGS`, список PTZ `UNKNOWN_INSPECTION_CAMERAS`, пути `UNKNOWN_INSPECTION_PROPOSAL_PATH` и `UNKNOWN_INSPECTION_CLASSIFIER_PATH`, а также доверенные функции допуска `UNKNOWN_INSPECTION_AUTHORIZE(camera, owner)` и оценки перекрытия `UNKNOWN_INSPECTION_OCCLUDED(camera, stamp, frame, bbox)`. Функция оценки возвращает `False` только при подтверждённой видимости. Отсутствующая или неопределённая оценка не даёт семантического подтверждения. + +Режим ограничен прямым обнаружением в разрешённых PTZ-потоках. Обзорные потоки сохраняют предпросмотр. Для передачи цели с обзорной камеры нужен отдельный проверенный путь. Физическая скорость, устойчивое удержание и итоговая классификация на объекте требуют испытаний с готовыми весами и измерениями на камерах. diff --git a/integration/unknown_r3/build_review_copy.py b/integration/unknown_r3/build_review_copy.py new file mode 100644 index 0000000..a9cabf9 --- /dev/null +++ b/integration/unknown_r3/build_review_copy.py @@ -0,0 +1,52 @@ +"""Build an offline review copy from an exact server source snapshot.""" +import argparse +import hashlib +import json +from pathlib import Path +import shutil +import subprocess + + +BUNDLE = Path(__file__).resolve().parent +REPO = BUNDLE.parent.parent + + +def sha(path): + with path.open("rb") as file: + return hashlib.file_digest(file, "sha256").hexdigest() + + +def check(directory, expected): + for name, digest in expected.items(): + path = directory / name + if not path.is_file() or sha(path) != digest: + raise ValueError(f"SHA256 mismatch or missing file: {name}") + + +def build(source, destination): + manifest = json.loads((BUNDLE / "pins.json").read_text(encoding="utf-8")) + check(source, manifest["source"]) + check(REPO / "ptz_tracker_modular", manifest["modules"]) + check(BUNDLE, {"server.patch": manifest["patch_sha256"]}) + destination.mkdir() + package = destination / "ptz_tracker_modular" + package.mkdir() + for name in manifest["source"]: + shutil.copyfile(source / name, package / name) + for name in manifest["modules"]: + shutil.copyfile(REPO / "ptz_tracker_modular" / name, package / name) + patch = str(BUNDLE / "server.patch") + subprocess.run(["git", "-c", "core.autocrlf=false", "apply", "--check", patch], cwd=destination, check=True) + subprocess.run(["git", "-c", "core.autocrlf=false", "apply", patch], cwd=destination, check=True) + check(package, manifest["result"]) + for path in (BUNDLE / "tests").glob("test_*.py"): + shutil.copyfile(path, destination / path.name) + print(f"Verified offline copy: {destination}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("source", type=Path, help="directory containing exact server source files") + parser.add_argument("destination", type=Path, help="new local output directory") + args = parser.parse_args() + build(args.source.resolve(), args.destination.resolve()) diff --git a/integration/unknown_r3/pins.json b/integration/unknown_r3/pins.json new file mode 100644 index 0000000..28c255b --- /dev/null +++ b/integration/unknown_r3/pins.json @@ -0,0 +1,31 @@ +{ + "source": { + "alarms.py": "f71d75f34664f48ffc230d54f3ee10168ad1d7f7942fbe815bc319d2764701ca", + "events.py": "c6606f18fdb17df59fbe74f38a500c0a0c0a9e7c8989bab9cf13685a726c8c53", + "main.py": "d1da4c165a89ff86713be8e940fd86cef84276250bb9f08ef42cfb8c8038618a", + "postprocess.py": "5326c9d3093b7c863310b9c0a7a85010a1a83ff014631a054ac9a16cc31c8fc5", + "streaming.py": "313828be8be66a5ebbf2365ec91de965128f06da16c54ff2332733a0e2fc701c", + "visual_control_probe.py": "a5d6608c0fefa316a48384685ddf9b35bcd7dc1a5f7b1f8f1b348490d448832f" + }, + "result": { + "alarms.py": "6faf903aa6d538cb7edabbc6a065ed9190d45f8b59f4e9efb9c9b3cd8ae349e5", + "events.py": "f002025c393025b7ebec63ef2af94326322d5153b70ecc896961d48dba961cdb", + "main.py": "b703db6509979f8ec4f736a994dbded6726ea590249e5f4ad381bdf79e38c5aa", + "postprocess.py": "d88802a44cfbf2658ea8d6fc85c689a8e72369815205db4df68a13690b850495", + "streaming.py": "f4debcda1e995eb5e6c56e30b8da027c0020fe9e5ea80b150fde12290fda4ec9", + "visual_control_probe.py": "6d81759060646a4af08d5d0de49ebad7ac417e1a789e37eff2403fff6d2162c9" + }, + "modules": { + "alarm_boundary.py": "c7ca0150f284375cef1129ecbbb5bffa91bb84e0a09e25483a3f90a579c28713", + "alarm_eligibility.py": "b0de51051bff969f590b0eee91a62a2456c4cf66e733231cdd545525249d3f48", + "frame_time.py": "c9d64814c54383b94299f78b1255e5f50715b25ef6c3aff7197bc8fa78a0884b", + "inspection_bridge.py": "bb8663293b36002902dbfef1f63b40ff7a9e7dea7147088305f1332a91ad37f9", + "inspection_handoff.py": "c714d69b24fde86653ab8613e9518fd689fa3691f129421068f3bb975f44e246", + "inspection_input.py": "324562a7deacb3abdb5c536263dfa104f149c6157306bb792beea318d7226db1", + "inspection_runtime.py": "1eaee1021d43d1814d2c48881df90eab227e5d2bae4744c379eccfa5b169694d", + "model_class_contract.py": "93ebdc13602e757ac24bf35e10698c4a19fd9fa4ca8bd4f0fef850b099edef2f", + "unknown_inspection.py": "ff799622ae45d91b745cc345c7a3ed3495cae88a29df9ca49ac7dbc5fe8933bd", + "inspection_startup.py": "9d9f901f4ee953e5efdbbf975a0c14413cdf17e9a1e6cd22b6d6f2135a81d74a" + }, + "patch_sha256": "fbb76f3fce454702016fa5416e00e09cefff9e59afa3fc6eca57d81aa7cbf4da" +} diff --git a/integration/unknown_r3/server.patch b/integration/unknown_r3/server.patch new file mode 100644 index 0000000..9120b0a --- /dev/null +++ b/integration/unknown_r3/server.patch @@ -0,0 +1,896 @@ +--- a/ptz_tracker_modular/alarms.py ++++ b/ptz_tracker_modular/alarms.py +@@ -10,2 +10,3 @@ + import threading ++from itertools import count + from datetime import datetime, timezone +@@ -29,2 +30,7 @@ + cameras_io = None # type: ignore ++ ++try: ++ from . import alarm_boundary as _boundary ++except ImportError: ++ import alarm_boundary as _boundary + +@@ -81,2 +87,3 @@ + _http_queue: Optional[asyncio.Queue] = None ++_http_revisions = count(1) + def _ensure_http_queue() -> asyncio.Queue: +@@ -266,3 +273,3 @@ + online = bool(cam_id in detected) +- armed = bool((_H.get(cam_id) or {}).get("armed", False)) ++ armed = is_armed(cam_id) + cameras_payload.append(_build_swagger_camera(mac_n, alarm=armed, status=online)) +@@ -277,3 +284,16 @@ + ++def _camera_for_mac(mac): ++ try: ++ normalized = _norm_mac(mac) ++ found = [int(c) for c in config.CAMERA_CONFIG if _get_cam_mac(int(c)) == normalized] ++ return found[0] if len(found) == 1 else None ++ except Exception: ++ return None ++ ++ + def _dispatch_ws(minimal: Dict[str, Any]) -> None: ++ if _boundary.enabled(): ++ minimal = {**minimal, "_cc10_contexts": _boundary.capture_contexts(config.CAMERA_CONFIG)} ++ token = minimal["_cc10_contexts"] ++ minimal = {**_boundary.filter_ws(minimal), "_cc10_contexts": token} + q = _ensure_ws_queue() +@@ -291,3 +311,6 @@ + loop = _state.MAIN_LOOP +- item = {"url": url, "json": body} ++ # Keep the original ordering when a failed snapshot is retried later. ++ contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) ++ item = {"url": url, "json": _boundary.filter_http(body, contexts, _camera_for_mac), ++ "_created_at": next(_http_revisions), "_cc10_contexts": contexts} + if loop is not None: +@@ -316,3 +339,3 @@ + """Публичный геттор текущего состояния тревоги (armed) по камере.""" +- return bool((_H.get(int(cam_idx)) or {}).get("armed", False)) ++ return bool((_H.get(int(cam_idx)) or {}).get("armed", False)) and _boundary.arm_allowed(int(cam_idx)) + +@@ -332,2 +355,3 @@ + """ ++ detected = bool(detected) and _boundary.arm_allowed(cam_idx) + changed = False +@@ -335,2 +359,4 @@ + st = _hyst_state(cam_idx) ++ if _boundary.enabled() and not detected: ++ st["off"] = ALARM_COUNT_OFF + if detected: +@@ -347,3 +373,3 @@ + changed = True +- aggregate_alarm = any(bool(v.get("armed", False)) for v in _H.values()) ++ aggregate_alarm = any(is_armed(c) for c in _H) + +@@ -383,2 +409,4 @@ + return ++ ++ alarm = bool(alarm) and _boundary.arm_allowed(_camera_for_mac(mac_n)) + +@@ -465,3 +493,4 @@ + +- data_bit = bool(last.get("data", False)) if isinstance(last, dict) else bool(last) ++ guarded_last = _boundary.filter_ws(last) ++ data_bit = bool(guarded_last.get("data", False)) if isinstance(guarded_last, dict) else bool(guarded_last) + to_send = {"type": "video_alarm", "data": data_bit} +@@ -489,2 +518,9 @@ + ++def _http_error_detail(response: Any) -> str: ++ detail = str(response.text or "") ++ if ALARM_HTTP_API_KEY: ++ detail = detail.replace(ALARM_HTTP_API_KEY, "[redacted]") ++ return " ".join(detail.split())[:500] ++ ++ + async def alarm_http_sender() -> None: +@@ -499,22 +535,6 @@ + from requests.adapters import HTTPAdapter +- try: +- from urllib3.util.retry import Retry # type: ignore +- except Exception: +- Retry = None # type: ignore +- + sess = requests.Session() + sess.trust_env = False +- if Retry is not None: +- adapter = HTTPAdapter( +- pool_connections=16, +- pool_maxsize=16, +- max_retries=Retry( +- total=2, +- backoff_factor=0.3, +- status_forcelist=[500, 502, 503, 504], +- raise_on_status=False, +- ), +- ) +- else: +- adapter = HTTPAdapter(pool_connections=16, pool_maxsize=16, max_retries=0) ++ # One retry policy owns POST attempts; do not multiply transport retries. ++ adapter = HTTPAdapter(pool_connections=16, pool_maxsize=16, max_retries=0) + sess.mount("http://", adapter) +@@ -527,7 +547,20 @@ + +- def _merge_http_bucket(bucket: List[Dict[str, Any]]) -> Tuple[str, Dict[str, Any]]: +- # Все элементы в bucket — с одинаковым URL. Берём ПОСЛЕДНИЙ body. +- url = bucket[0].get("url", "") +- last_body = bucket[-1].get("json") or {} +- return url, last_body ++ latest_by_url: Dict[str, int] = {} ++ max_retries = max(0, min(5, int(getattr(config, "ALARM_HTTP_MAX_RETRIES", 2)))) ++ retry_base = max(0.0, float(getattr(config, "ALARM_HTTP_RETRY_BASE_SEC", 1.0))) ++ ++ async def retry_failed(failed: Dict[str, Any], response: Any = None) -> None: ++ attempts = int(failed.get("_retries", 0)) ++ if attempts >= max_retries: ++ logger.error("[ALARM][HTTP-BULK][RETRY-EXHAUSTED] url=%s attempts=%d; waiting for fresh state", ++ failed.get("url", ""), attempts + 1) ++ return ++ delay = min(8.0, retry_base * (2 ** attempts)) ++ if response is not None: ++ try: ++ delay = max(delay, min(30.0, float(response.headers.get("Retry-After", 0)))) ++ except (ValueError, TypeError, AttributeError): ++ pass ++ await asyncio.sleep(delay) ++ q.put_nowait({**failed, "_retries": attempts + 1}) + +@@ -535,4 +568,6 @@ + item = await q.get() +- try: +- bucket = [item] ++ item.setdefault("_created_at", next(_http_revisions)) ++ selected = item ++ bucket = [item] ++ try: + t0 = asyncio.get_running_loop().time() +@@ -541,2 +576,3 @@ + nxt = q.get_nowait() ++ nxt.setdefault("_created_at", next(_http_revisions)) + if nxt.get("url") != bucket[0].get("url"): +@@ -550,3 +586,7 @@ + +- url_raw, body = _merge_http_bucket(bucket) ++ # A retry can be queued behind a newer alarm OFF. Queue position ++ # must never make an old ON overwrite that newer observation. ++ _, selected = max(enumerate(bucket), key=lambda pair: (pair[1]["_created_at"], pair[0])) ++ url_raw = selected.get("url", "") ++ body = selected.get("json") or {} + base = _sanitize_http_base(ALARM_HTTP_BASE) +@@ -559,2 +599,7 @@ + ++ revision = selected["_created_at"] ++ if revision < latest_by_url.get(url, 0): ++ continue ++ latest_by_url[url] = revision ++ + cams_cnt = len(body.get("cameras") or []) +@@ -563,3 +608,3 @@ + def _post(): +- return sess.post(url, json=body, headers=headers, timeout=3.0, verify=verify_arg) ++ return sess.post(url, json=_boundary.filter_http(body, selected.get("_cc10_contexts"), _camera_for_mac), headers=headers, timeout=3.0, verify=verify_arg) + +@@ -569,15 +614,11 @@ + else: +- logger.warning("[ALARM][HTTP-BULK][FAIL] %s HTTP %s", url, resp.status_code) +- logger.debug("[ALARM][HTTP-BULK][RESP] %s", (resp.text or "")[:200]) +- # вернуть последний элемент на ретрай +- try: +- q.put_nowait(bucket[-1]) +- except Exception: +- pass ++ logger.warning("[ALARM][HTTP-BULK][FAIL] %s HTTP %s response=%s", ++ url, resp.status_code, _http_error_detail(resp)) ++ if resp.status_code in (408, 429) or 500 <= resp.status_code < 600: ++ await retry_failed(selected, resp) ++ # Repeating an unchanged 400/401/403/404/422 cannot repair it. ++ # A fresh event or periodic heartbeat may still be delivered. + except Exception as exc: + logger.error("[ALARM][HTTP-BULK] error: %s", exc) +- try: +- q.put_nowait(item) +- except Exception: +- pass ++ await retry_failed(selected) + finally: +@@ -627,3 +668,3 @@ + sess.trust_env = False +- if Retry is not None: ++ if Retry is not None and not _boundary.enabled(): + adapter = HTTPAdapter( +@@ -688,2 +729,3 @@ + body = _build_swagger_payload(cameras) ++ contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) + cams_cnt = len(cameras) +@@ -692,3 +734,3 @@ + def _post(): +- return sess.post(url, json=body, headers=headers, timeout=3.0, verify=verify_arg) ++ return sess.post(url, json=_boundary.filter_http(body, contexts, _camera_for_mac), headers=headers, timeout=3.0, verify=verify_arg) + +@@ -698,4 +740,4 @@ + else: +- logger.warning("[ALARM][HEARTBEAT][FAIL] %s HTTP %s", url, resp.status_code) +- logger.debug("[ALARM][HEARTBEAT][RESP] %s", (resp.text or "")[:200]) ++ logger.warning("[ALARM][HEARTBEAT][FAIL] %s HTTP %s response=%s", ++ url, resp.status_code, _http_error_detail(resp)) + else: +--- a/ptz_tracker_modular/events.py ++++ b/ptz_tracker_modular/events.py +@@ -11,3 +11,3 @@ + +-from . import config, state ++from . import config, state, alarm_boundary as _boundary + from .event_logging import start_record_event, stop_record_event +@@ -100,2 +100,5 @@ + """ ++ if _boundary.enabled(): ++ contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) ++ payload = {**_boundary.filter_ws({**payload, "_cc10_contexts": contexts}), "_cc10_contexts": contexts} + if state.alarm_queue is not None and state.MAIN_LOOP is not None: +@@ -132,2 +135,3 @@ + try: ++ guarded_payload = _boundary.filter_ws(payload) + # Строго минимальный словарь только с нужными ключами +@@ -135,4 +139,4 @@ + minimal = { +- "type": payload.get("type"), +- "data": bool(payload.get("data")), ++ "type": guarded_payload.get("type"), ++ "data": bool(guarded_payload.get("data")), + } +@@ -140,3 +144,3 @@ + # на случай если кто-то положил уже готовую строку/другую структуру +- minimal = payload ++ minimal = guarded_payload + +@@ -169,3 +173,3 @@ + """ +- _dispatch_alarm({"type": "freq_alarm", "data": bool(state_on)}) ++ _dispatch_alarm({"type": "freq_alarm", "data": bool(state_on) and _boundary.arm_allowed(cam_idx)}) + +--- a/ptz_tracker_modular/main.py ++++ b/ptz_tracker_modular/main.py +@@ -217,2 +217,8 @@ + async def main() -> None: ++ from ptz_tracker_modular import inspection_handoff ++ if inspection_handoff.enabled(): ++ from ptz_tracker_modular.inspection_startup import configure_runtime ++ inspection = configure_runtime() ++ else: ++ inspection = None + # --- Инициализация общего состояния --- +@@ -479,3 +485,3 @@ + # ---- Опциональный smoke-тест тревоги (по первому MAC) ---- +- if os.getenv("ALARM_SMOKE", "0") in ("1", "true", "TRUE", "yes", "YES"): ++ if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is not True and os.getenv("ALARM_SMOKE", "0") in ("1", "true", "TRUE", "yes", "YES"): + async def _smoke(): +@@ -528,2 +534,5 @@ + ++ if inspection is not None: ++ tasks.append(watch(inspection.recognition_loop(), "unknown_recognition")) ++ + try: +--- a/ptz_tracker_modular/postprocess.py ++++ b/ptz_tracker_modular/postprocess.py +@@ -11,2 +11,3 @@ + from . import config, state, visual_control ++from . import unknown_inspection as inspection + from . import motion_detector +@@ -47,2 +48,3 @@ + logger = logging.getLogger("PTZTracker") ++_inspection_move_emit = move_ptz_bounded + +@@ -52,3 +54,4 @@ + """Keep class filtering on the detector result's actual device.""" +- return torch.as_tensor(config.TARGET_CLASSES, device=classes.device, dtype=classes.dtype) ++ return torch.as_tensor((0,) if _inspection_enabled() else config.TARGET_CLASSES, ++ device=classes.device, dtype=classes.dtype) + +@@ -57,2 +60,4 @@ + """Return the detector class name retained by candidate/visual tracking.""" ++ if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True: ++ return "unknown airborne object" + try: +@@ -540,3 +545,10 @@ + st["ptz_move_seq"] = int(st.get("ptz_move_seq", 0) or 0) + 1 ++ inspection_on = getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True ++ old_owner = visual_control.owner(st) if inspection_on else None + st["zoom_rearm_epoch"] = int(st.get("zoom_rearm_epoch", 0) or 0) + 1 ++ session = st.get("inspection_session") ++ if inspection_on and isinstance(session, inspection.Session) and session.owner == old_owner: ++ session.owner = visual_control.owner(st) ++ session.recent.clear() ++ session.revoke_semantics("zoom_revalidation") + st["zoom_rearm_required"] = True +@@ -650,2 +662,9 @@ + """Require strong detector evidence after any optical narrowing.""" ++ if _inspection_enabled(): ++ cfg = getattr(config, "UNKNOWN_INSPECTION_SETTINGS", None) ++ session = st.get("inspection_session") ++ return bool(isinstance(cfg, inspection.Settings) and isinstance(session, inspection.Session) ++ and session.live(time.monotonic()) and inspection.finite(confidence) ++ and confidence >= cfg.proposal_min ++ and 0 <= time.time()-session.last_at <= .65) + followup = bool( +@@ -3972,2 +3991,3 @@ + frame_h: int, ++ *, inspection_state: dict | None = None, + ) -> tuple[bool, float]: +@@ -3990,2 +4010,8 @@ + detector_conf = float(matched[1]) if matched is not None else 0.0 ++ if _inspection_enabled(): ++ st = inspection_state or {} ++ session = st.get("inspection_session") ++ allowed = st.get("inspection_authorized") is True and isinstance(session, inspection.Session) and matched is not None and session.contract == getattr(config, "UNKNOWN_INSPECTION_SETTINGS", None) and session.alarm_allowed( ++ time.time(), time.monotonic(), visual_control.owner(st), tuple(target_bbox), (frame_h, frame_w)) ++ return bool(allowed), session.semantic_score if allowed else 0. + threshold = float(getattr(config, "ALARM_CONF", 0.50)) +@@ -4016,2 +4042,9 @@ + """Keep localization, class identity and protection as separate states.""" ++ if _inspection_enabled(): ++ session = st.get("inspection_session") ++ st["tracking_state"] = "inspecting" ++ st["identity_state"] = session.decision if isinstance(session, inspection.Session) else "unresolved" ++ st["identity_class"] = -1 ++ st["protection_state"] = "pending" if alarm_eligible else "safe" ++ return + source = str(target_source or "none") +@@ -4093,2 +4126,7 @@ + def _format_visual_tracking_label(st: dict, *, tracker_score: float) -> str: ++ if _inspection_enabled(): ++ session = st.get("inspection_session") ++ if isinstance(session, inspection.Session): ++ return f"{session.decision}: {session.semantic_name or 'unknown object'}; inspecting" ++ return "unknown object; inspection unavailable" + identity_state = str(st.get("identity_state", "unknown")) +@@ -4569,2 +4607,4 @@ + """Break continuous zoom-in into short feedback-controlled steps.""" ++ if _inspection_enabled(): ++ zoom_cmd = min(float(zoom_cmd), .10) + pulse_until = float(st.get("zoom_step_until", 0.0) or 0.0) +@@ -5454,3 +5494,3 @@ + +-def _enter_acquire_locked(cam_idx: int, now: float) -> bool: ++def _legacy_enter_acquire_locked(cam_idx: int, now: float) -> bool: + st = state.ensure_cam_state(cam_idx, is_ptz=True) +@@ -5978,3 +6018,3 @@ + +-def _enter_track_locked(cam_idx: int) -> None: ++def _legacy_enter_track_locked(cam_idx: int) -> None: + st = state.ensure_cam_state(cam_idx, is_ptz=cam_idx in config.PTZ_CAM_IDS) +@@ -6541,3 +6581,3 @@ + alarm_eligible, alarm_conf = _alarm_eligible_for_target( +- bbox, target_class, detector_targets, width, height) ++ bbox, target_class, detector_targets, width, height, inspection_state=st) + identity_conf = float(matched[1]) if matched is not None and int(matched[2]) == target_class else 0. +@@ -6548,3 +6588,3 @@ + alarm_eligible=alarm_eligible, cam_idx=cam_idx) +- alarms.ingest_detection(cam_idx, alarm_eligible) ++ alarms.ingest_detection(cam_idx, bool(alarm_eligible) and (not _inspection_enabled() or _inspection_alarm_ready(st))) + +@@ -6596,2 +6636,4 @@ + if not visual_only: ++ if _inspection_enabled(): ++ eligible = bool(eligible) and _inspection_alarm_ready(st) + alarms.ingest_detection(cam_idx, eligible) +@@ -6805,2 +6847,8 @@ + ++ if _inspection_enabled() and not _inspection_before_frame( ++ cam_idx, st, res, frame_bgr, source_seq, is_ptz, visual_only, visual_sample): ++ return ++ if _inspection_enabled(): ++ boxes_t = getattr(res, "boxes", None) ++ + _cfg = config.CAMERA_CONFIG.get(int(cam_idx), {}) if hasattr(config, "CAMERA_CONFIG") else {} +@@ -6874,3 +6922,3 @@ + mask = torch.isin(clss, _targets_tensor_like(clss)) +- if getattr(config, "DRONE_ONLY", False): ++ if not _inspection_enabled() and getattr(config, "DRONE_ONLY", False): + drone_id = int(getattr(config, "DRONE_CLASS_ID", 1)) +@@ -6887,3 +6935,3 @@ + conf_by_cls = getattr(config, "DETECT_CONF_BY_CLASS", None) +- if isinstance(conf_by_cls, dict) and conf_by_cls: ++ if not _inspection_enabled() and isinstance(conf_by_cls, dict) and conf_by_cls: + for k, v in conf_by_cls.items(): +@@ -6896,2 +6944,5 @@ + ++ if _inspection_enabled(): ++ conf_thr = torch.full_like(confs, config.UNKNOWN_INSPECTION_SETTINGS.proposal_min) ++ + # === Motion-aided detection: boost conf для боксов внутри motion ROI === +@@ -6902,2 +6953,3 @@ + is_ptz ++ and not _inspection_enabled() + and bool(getattr(config, "MOTION_DETECT_ENABLE", False)) +@@ -6924,3 +6976,3 @@ + # optional drone size filter (fractions of frame) +- if getattr(config, "DRONE_SIZE_FILTER", False): ++ if not _inspection_enabled() and getattr(config, "DRONE_SIZE_FILTER", False): + drone_ids_cfg = getattr(config, "DRONE_CLASS_ID", 1) +@@ -7943,3 +7995,3 @@ + (int(x1), int(y1), int(x2), int(y2)), +- int(best_cls), detector_targets, w, h, ++ int(best_cls), detector_targets, w, h, inspection_state=st, + ) +@@ -10272 +10324,231 @@ + return response ++ ++ ++def _enter_acquire_locked(cam_idx: int, now: float): ++ st = state.ensure_cam_state(cam_idx, is_ptz=True) ++ old_owner = visual_control.owner(st) ++ count = int(st.get("zoom_no_effect_count", 0) or 0) ++ blocked = bool(st.get("zoom_no_effect_blocked", False)) ++ result = _legacy_enter_acquire_locked(cam_idx, now) ++ if _inspection_enabled(): ++ st["zoom_no_effect_count"] = max(count, int(st.get("zoom_no_effect_count", 0) or 0)) ++ st["zoom_no_effect_blocked"] = blocked or bool(st.get("zoom_no_effect_blocked", False)) ++ session = st.get("inspection_session") ++ if isinstance(session, inspection.Session) and session.owner == old_owner: ++ new_owner = visual_control.owner(st) ++ if new_owner != old_owner: ++ session.owner = new_owner ++ session.recent.clear() ++ session.revoke_semantics("known_owner_transition") ++ return result ++ ++ ++def _enter_track_locked(cam_idx: int): ++ st = state.ensure_cam_state(cam_idx, is_ptz=True) ++ old_owner = visual_control.owner(st) ++ count = int(st.get("zoom_no_effect_count", 0) or 0) ++ blocked = bool(st.get("zoom_no_effect_blocked", False)) ++ result = _legacy_enter_track_locked(cam_idx) ++ if _inspection_enabled(): ++ st["zoom_no_effect_count"] = max(count, int(st.get("zoom_no_effect_count", 0) or 0)) ++ st["zoom_no_effect_blocked"] = blocked or bool(st.get("zoom_no_effect_blocked", False)) ++ session = st.get("inspection_session") ++ if isinstance(session, inspection.Session) and session.owner == old_owner: ++ new_owner = visual_control.owner(st) ++ if new_owner != old_owner: ++ session.owner = new_owner ++ session.recent.clear() ++ session.revoke_semantics("known_owner_transition") ++ return result ++ ++ ++def _inspection_enabled(): ++ return getattr(config, 'UNKNOWN_INSPECTION_ENABLE', False) is True ++ ++ ++def _inspection_alarm_ready(st): ++ from .inspection_runtime import current_detector ++ session = st.get('inspection_session') ++ cfg = getattr(config, 'UNKNOWN_INSPECTION_SETTINGS', None) ++ return bool(isinstance(cfg, inspection.Settings) and cfg.enabled ++ and isinstance(session, inspection.Session) and session.contract == cfg ++ and st.get('inspection_authorized') is True ++ and st.get('mode') not in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'} ++ and current_detector(st, time.monotonic()) ++ and _inspection_common_ready(st) ++ and session.alarm_allowed(time.time(), time.monotonic(), visual_control.owner(st), ++ tuple(st.get('last_bbox') or session.bbox or ()), session.shape)) ++ ++ ++def _inspection_common_ready(st): ++ """Independent monotonic two-frame veto; no binding is no alarm.""" ++ guard = st.get('inspection_common_alarm_guard') ++ try: ++ return callable(guard) and guard() is True ++ except Exception: ++ return False ++ ++ ++def _inspection_before_frame(cam, st, result, frame, seq, is_ptz, visual_only, sample): ++ """No inference here: only bounded metadata and current-frame admission.""" ++ cfg = getattr(config, 'UNKNOWN_INSPECTION_SETTINGS', None) ++ session = st.get('inspection_session') ++ now, mono = time.time(), time.monotonic() ++ ++ def deny(reason): ++ if isinstance(session, inspection.Session) and session.owner != visual_control.owner(st): ++ return False # an old callback cannot STOP or clear a newer owner ++ st['alarm_gate_eligible'] = False ++ st['alarm_detector_conf'] = 0. ++ st['inspection_reason'] = reason ++ if isinstance(session, inspection.Session): ++ session.revoke_semantics(reason) ++ if st.get('inspection_stop_reason') != reason: ++ stop_ptz(cam, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) ++ st['inspection_stop_reason'] = reason ++ if not visual_only: ++ alarms.ingest_detection(cam, False) ++ return False ++ ++ if not is_ptz: ++ return deny('inspection_ptz_only') ++ if not isinstance(cfg, inspection.Settings) or not cfg.enabled: ++ return deny('missing_validated_contract') ++ if isinstance(session, inspection.Session) and session.contract is not None and session.contract != cfg: ++ session.phase = 'closed' ++ return deny('model_contract_changed') ++ if st.get('inspection_authorized') is not True: ++ return deny('inspection_not_authorized') ++ if st.get('mode') in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'}: ++ return deny('camera_not_authorized_for_inspection') ++ incoming = getattr(result, 'inspection_observation', None) ++ if isinstance(incoming, inspection.Observation) and incoming.owner != visual_control.owner(st): ++ return False ++ from .inspection_runtime import provenance, check_stamp ++ reason = provenance(st, result, sample, camera=cam, seq=seq, ++ owner=visual_control.owner(st), mono=mono, visual_only=visual_only) ++ if reason: ++ return deny(reason) ++ if isinstance(session, inspection.Session) and session.phase == 'closed': ++ # A new SEARCH owner may inspect another object. A closed callback itself ++ # never renews its session/budget. Same-object retry is deliberately not ++ # automatic in this first candidate; the discovery scheduler must retire ++ # its bounded suppression entry explicitly. ++ another = (isinstance(incoming, inspection.Observation) ++ and any(inspection.valid_box(b, incoming.shape) ++ and not inspection.same_object(session.bbox, b, incoming.shape) ++ for b, _, _ in incoming.detections)) ++ if (another and not st.get('tracking_active') ++ and st.get('mode') in {'IDLE', 'SWEEP', 'auto'}): ++ session = None ++ st.pop('inspection_session', None) ++ else: ++ return deny('inspection_closed') ++ if isinstance(session, inspection.Session): ++ if session.decision == 'target' and not 0 <= now-session.semantic_at <= .65: ++ session.revoke_semantics('semantic_evidence_expired') ++ if session.owner != visual_control.owner(st): ++ session.phase = 'closed' ++ return deny('owner_changed') ++ if not session.live(mono): ++ session.phase = 'closed' ++ return deny('inspection_timeout') ++ if session.decision == 'non_target': ++ session.phase = 'closed' ++ deny('confirmed_non_target') ++ _end_track_and_park(cam, reason='inspection confirmed non-target') ++ return False ++ if session.phase == 'closed': ++ return deny('inspection_closed') ++ if visual_only: ++ if not isinstance(session, inspection.Session) or session.phase != 'inspection': ++ return deny('no_inspection_owner') ++ if (sample is None or not sample.result.ok or sample.result.bbox is None ++ or not 0 <= now-session.last_at <= .65 ++ or tuple(frame.shape[:2]) != session.shape ++ or not inspection.same_object(session.bbox, sample.result.bbox, session.shape)): ++ return deny('unverified_visual_continuity') ++ st['inspection_control_deadline'] = min( ++ mono + max(0., .25-(now-sample.captured_at)), st['inspection_input_mono']+.25) ++ return True ++ observation = getattr(result, 'inspection_observation', None) ++ if not isinstance(observation, inspection.Observation) or observation.source_seq != seq: ++ return deny('missing_original_frame_metadata') ++ if observation.shape != tuple(frame.shape[:2]) or observation.owner != visual_control.owner(st): ++ return deny('foreign_observation') ++ if session is None: ++ st['inspection_serial'] = int(st.get('inspection_serial', 0) or 0)+1 ++ session = inspection.Session(cam, st['inspection_serial'], visual_control.owner(st), ++ mono, mono+cfg.budget_sec) ++ st['inspection_session'] = session ++ st['purpose'] = 'inspection' ++ admitted = session.observe(observation, cfg, now, mono) ++ recognition = getattr(result, 'inspection_recognition', None) ++ if admitted and isinstance(recognition, inspection.Recognition): ++ rec_stamp = getattr(result, 'inspection_recognition_stamp', None) ++ if check_stamp(rec_stamp, camera=cam, seq=recognition.source_seq, ++ owner=recognition.owner, expected_owner=visual_control.owner(st), ++ binding=st.get('inspection_source_binding'), now=mono, age=.65): ++ session.recognize(recognition, cfg, now, mono) ++ if session.semantic_seq == recognition.source_seq and session.semantic_votes: ++ st['inspection_semantic_stamp'] = rec_stamp ++ else: ++ session.revoke_semantics('stale_or_foreign_recognition_stamp') ++ if not admitted: ++ return deny(session.reason) ++ if session.decision == 'non_target': ++ session.phase = 'closed' ++ deny('confirmed_non_target') ++ _end_track_and_park(cam, reason='inspection confirmed non-target') ++ return False ++ # Old-frame evidence may validate identity, but must not replay a MOVE. ++ if sample is None and (not 0 <= now-observation.observed_at <= .25 ++ or not 0 <= mono-st['inspection_input_mono'] <= .25): ++ return deny('too_old_for_motion') ++ st['inspection_stop_reason'] = '' ++ st['inspection_reason'] = session.reason ++ st['purpose'] = 'target' if session.decision == 'target' else 'inspection' ++ st['inspection_control_deadline'] = min( ++ mono + max(0., .25-(now-observation.observed_at)), st['inspection_input_mono']+.25) ++ # The legacy ranker must never select a stronger neighboring proposal over ++ # the object whose temporal admission we just checked. ++ if getattr(result, 'boxes', None) is not None: ++ raw_boxes = result.boxes.xyxy.detach().cpu().tolist() ++ indices = [i for i, b in enumerate(raw_boxes) if tuple(b) == session.bbox] ++ if not indices: ++ return deny('proposal_result_mismatch') ++ result.boxes = result.boxes[indices[:1]] ++ return True ++ ++ ++def move_ptz_bounded(cam_idx, pan_speed, tilt_speed, zoom=0., **kwargs): ++ """Last-boundary recheck; the existing physical worker is still authoritative.""" ++ if _inspection_enabled(): ++ from .inspection_runtime import current_detector ++ st = state.ptz_states.get(cam_idx, {}) ++ session = st.get('inspection_session') ++ now, mono = time.time(), time.monotonic() ++ if isinstance(session, inspection.Session) and session.decision == 'target': ++ stamp = st.get('inspection_semantic_stamp') ++ from .inspection_runtime import check_stamp ++ if (not 0 <= now-session.semantic_at <= .65 ++ or not check_stamp(stamp, camera=cam_idx, seq=session.semantic_seq, ++ owner=session.owner, expected_owner=visual_control.owner(st), ++ binding=st.get('inspection_source_binding'), now=mono, age=.65)): ++ session.revoke_semantics('semantic_evidence_expired_at_emit') ++ elif mono >= session.deadline_mono and not _inspection_common_ready(st): ++ session.revoke_semantics('independent_semantic_evidence_expired_at_emit') ++ valid = (isinstance(session, inspection.Session) ++ and st.get('inspection_authorized') is True ++ and st.get('mode') not in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'} ++ and session.live(mono) and session.phase == 'inspection' ++ and session.decision != 'non_target' ++ and session.owner == visual_control.owner(st) ++ and current_detector(st, mono) ++ and 0 <= now-session.last_at <= .65 ++ and mono < float(st.get('inspection_control_deadline', 0.) or 0.)) ++ if not valid: ++ stop_ptz(cam_idx, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) ++ return ++ zoom = min(float(zoom), .10) ++ return _inspection_move_emit(cam_idx, pan_speed, tilt_speed, zoom, **kwargs) +--- a/ptz_tracker_modular/streaming.py ++++ b/ptz_tracker_modular/streaming.py +@@ -25,2 +25,4 @@ + from .postprocess import postprocess_control ++from . import inspection_handoff ++from uuid import uuid4 + from .visual_tracker import visual_tracker_manager +@@ -60,3 +62,3 @@ + _cpp_stream_map_missing_logged: str = "" +-_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float]] = {} ++_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float, Any]] = {} + _visual_feed_last_served: Dict[int, float] = {} +@@ -228,3 +230,3 @@ + waiting = [] +- for cam_id, (_, _, queued_at) in _visual_feed_pending.items(): ++ for cam_id, (_, _, queued_at, _) in _visual_feed_pending.items(): + st = state.ptz_states.get(int(cam_id), {}) +@@ -256,3 +258,3 @@ + break +- frame_bgr, source_seq, queued_at = _visual_feed_pending.pop(cam_id) ++ frame_bgr, source_seq, queued_at, ticket = _visual_feed_pending.pop(cam_id) + captured_at = _visual_feed_captured_at.pop(cam_id, time.time()) +@@ -274,2 +276,3 @@ + captured_at=captured_at, ++ inspection_ticket=ticket, + ), +@@ -299,2 +302,3 @@ + source_seq: int, ++ *, inspection_ticket=None, + ) -> None: +@@ -315,4 +319,6 @@ + queued_at = float(previous[2]) if previous is not None else time.perf_counter() +- _visual_feed_pending[int(cam_id)] = (frame_bgr, int(source_seq), queued_at) +- _visual_feed_captured_at[int(cam_id)] = time.time() ++ _visual_feed_pending[int(cam_id)] = (frame_bgr, int(source_seq), queued_at, inspection_ticket) ++ _visual_feed_captured_at[int(cam_id)] = ( ++ inspection_ticket.stamp.decode_utc_ns/1e9 if inspection_ticket else time.time() ++ ) + if _visual_feed_worker_task is None or _visual_feed_worker_task.done(): +@@ -776,2 +782,4 @@ + st = state.ptz_states.get(cam_id, {}) ++ if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True: ++ continue # UNKNOWN publishes from semantic eligibility, never recording. + has_target_now = bool(st.get("rec_active", False)) +@@ -1910,2 +1918,4 @@ + """Replace a queued stale frame with the newest frame from the same camera.""" ++ if inspection_handoff.enabled(): ++ return inspection_handoff.require_configured().refresh(item) + cam_idx, frame_bgr, is_ptz, source_seq, fut, queued_at = item +@@ -1967,2 +1977,3 @@ + source_seq: int = 0, ++ *, inspection_ticket=None, + ) -> None: +@@ -1971,5 +1982,7 @@ + async with self._cond: +- self._buf.append( +- (cam_idx, frame_bgr, is_ptz, int(source_seq), fut, time.perf_counter()) +- ) ++ item = cam_idx, frame_bgr, is_ptz, int(source_seq), fut, time.perf_counter() ++ if inspection_handoff.enabled(): ++ if not inspection_handoff.require_configured().bind_item(item, inspection_ticket): ++ return ++ self._buf.append(item) + self._cond.notify() +@@ -2034,2 +2047,6 @@ + ]) ++ if inspection_handoff.enabled(): ++ batch = inspection_handoff.require_configured().prune(batch) ++ if not batch: ++ continue + frames = [b[1] for b in batch] +@@ -2037,3 +2054,6 @@ + infer_t0 = time.perf_counter() +- results = await loop.run_in_executor(None, run, frames) ++ detect = run ++ if inspection_handoff.enabled(): ++ detect = inspection_handoff.require_configured().detect ++ results = await loop.run_in_executor(None, detect, frames) + forward_ms = (time.perf_counter() - infer_t0) * 1000.0 +@@ -2044,3 +2064,3 @@ + ) +- if bool(getattr(config, "SAHI_ENABLE", False)): ++ if bool(getattr(config, "SAHI_ENABLE", False)) and not inspection_handoff.enabled(): + results = await loop.run_in_executor( +@@ -2108,3 +2128,6 @@ + ++ handoff = inspection_handoff.require_configured() if inspection_handoff.enabled() else None + while True: ++ source_cameras = set() ++ stream_epoch = uuid4().hex if handoff else None + try: +@@ -2196,2 +2219,10 @@ + ++ inspection_ptz = handoff is not None and idx in handoff.bridge.crops.cameras ++ epoch = handoff.camera_epoch(idx) if inspection_ptz else None ++ if inspection_ptz: ++ if not handoff.begin_source(idx, f"cpp_{idx_raw}", stream_epoch, epoch): ++ continue ++ source_cameras.add(idx) ++ ++ ticket = None + now_pc = time.perf_counter() +@@ -2212,2 +2243,4 @@ + if img is not None: ++ if inspection_ptz: ++ ticket = handoff.accepted(idx, img, source_seq, epoch) + _publish_frontend_raw_preview(idx, img, pst, source_seq, priority=4) +@@ -2223,8 +2256,15 @@ + +- if getattr(config, "DROP_DECODE_WHEN_BUSY", False) and pst["proc_busy"]: ++ if getattr(config, "DROP_DECODE_WHEN_BUSY", False) and pst["proc_busy"] and ( ++ handoff is None or inspection_ptz ++ ): + img_busy = img if img is not None else _decode_image(encoded) + if img_busy is not None: +- _feed_visual_tracker_frame(idx, img_busy, pst, source_seq) +- pst["pending_frame"] = img_busy +- pst["pending_source_seq"] = int(source_seq) ++ if handoff is None: ++ pst["pending_frame"] = img_busy ++ pst["pending_source_seq"] = int(source_seq) ++ elif inspection_ptz: ++ if img is None: ++ ticket = handoff.accepted(idx, img_busy, source_seq, epoch) ++ handoff.pending(ticket) ++ _feed_visual_tracker_frame(idx, img_busy, pst, source_seq, inspection_ticket=ticket) + if can_publish and _frontend_raw_preview_due(idx, pst): +@@ -2236,2 +2276,4 @@ + img = _decode_image(encoded) ++ if img is not None and inspection_ptz: ++ ticket = handoff.accepted(idx, img, source_seq, epoch) + if img is None: +@@ -2242,3 +2284,3 @@ + +- if skip_infer_for_non_ptz: ++ if skip_infer_for_non_ptz or (handoff is not None and not inspection_ptz): + # Still keep the UI alive for non-PTZ/panorama streams. Detection +@@ -2250,2 +2292,4 @@ + ++ if inspection_ptz and ticket is None: ++ continue + now = time.time() +@@ -2284,3 +2328,3 @@ + if pst["proc_busy"]: +- _feed_visual_tracker_frame(idx, img, pst, source_seq) ++ _feed_visual_tracker_frame(idx, img, pst, source_seq, inspection_ticket=ticket) + skipped_busy_total += 1 +@@ -2294,3 +2338,3 @@ + if (now_pc - last_submit) < infer_min_dt: +- _feed_visual_tracker_frame(idx, img, pst, source_seq) ++ _feed_visual_tracker_frame(idx, img, pst, source_seq, inspection_ticket=ticket) + if publish_raw_allowed: +@@ -2306,3 +2350,3 @@ + +- async def process_frame(cam_idx: int, frame, frame_seq: int) -> None: ++ async def process_frame(cam_idx: int, frame, frame_seq: int, frame_ticket=None) -> None: + try: +@@ -2313,2 +2357,3 @@ + source_seq=frame_seq, ++ inspection_ticket=frame_ticket, + ) +@@ -2318,2 +2363,3 @@ + pst_done = state.ptz_states[cam_idx] ++ next_ticket = pst_done.pop("pending_inspection_ticket", None) + next_frame = pst_done.pop("pending_frame", None) +@@ -2331,3 +2377,3 @@ + pst_done["proc_busy"] = True +- asyncio.create_task(process_frame(cam_idx, next_frame, next_seq)) ++ asyncio.create_task(process_frame(cam_idx, next_frame, next_seq, next_ticket)) + else: +@@ -2341,3 +2387,3 @@ + +- asyncio.create_task(process_frame(idx, img, source_seq)) ++ asyncio.create_task(process_frame(idx, img, source_seq, ticket)) + +@@ -2345,3 +2391,7 @@ + logger.error("[CPP WS] error: %s", exc) +- await asyncio.sleep(0.5) ++ finally: ++ if handoff is not None: ++ for camera in source_cameras: ++ handoff.end_source(camera, stream_epoch) ++ await asyncio.sleep(0.5) + +@@ -2475,2 +2525,8 @@ + with ExitStack() as locks: ++ if inspection_handoff.enabled(): ++ handoff = inspection_handoff.require_configured() ++ for cid in sorted(ids): ++ locks.enter_context(visual_control.control_lock(cid)) ++ for cid in ids: ++ handoff.end_source(cid, reason='camera_address_changed') + for cid in ids: +@@ -2565,3 +2621,3 @@ + def _update_visual_feed(cam_id: int, frame_bgr: np.ndarray, source_seq: int, +- captured_at: Optional[float] = None) -> None: ++ captured_at: Optional[float] = None, *, inspection_ticket=None) -> None: + with visual_control.control_lock(cam_id): +@@ -2572,2 +2628,10 @@ + token = visual_control.owner(st) ++ handoff = inspection_handoff.require_configured() if inspection_handoff.enabled() else None ++ if handoff is not None and ( ++ not handoff.current(inspection_ticket, age=.25) ++ or inspection_ticket.pixels is not frame_bgr ++ or inspection_ticket.stamp.camera_id != cam_id ++ or inspection_ticket.stamp.source_seq != source_seq ++ ): ++ return + result = visual_control_probe.call_tracker_update(visual_tracker_manager.update, cam_id, frame_bgr, source_seq=source_seq) +@@ -2582,3 +2646,8 @@ + return +- sample = visual_control.VisualSample(token, int(source_seq), captured_at, result) ++ if handoff is not None: ++ sample = handoff.visual_sample(inspection_ticket, result) ++ if sample is None: ++ return ++ else: ++ sample = visual_control.VisualSample(token, int(source_seq), captured_at, result) + postprocess_control(cam_id, frame_bgr, None, True, source_seq, +@@ -2592,2 +2661,4 @@ + """Pair YOLO with an exact visual observation, independently of GPU latency.""" ++ if inspection_handoff.enabled(): ++ return inspection_handoff.require_configured().prepare(item) + cam_id, frame, is_ptz, source_seq, future, queued_at = item +@@ -2609,2 +2680,4 @@ + def _postprocess_inference_item(cam_id, frame, res, is_ptz, source_seq, future): ++ if inspection_handoff.enabled(): ++ return inspection_handoff.require_configured().process_item(cam_id, frame, res, is_ptz, source_seq, future) + sample = getattr(future, 'visual_sample', None) +--- a/ptz_tracker_modular/visual_control_probe.py ++++ b/ptz_tracker_modular/visual_control_probe.py +@@ -87,4 +87,4 @@ + @wraps(function) +- def wrapped(camera, frame, st, source_seq): +- result = function(camera, frame, st, source_seq) ++ def wrapped(camera, frame, st, source_seq, **kwargs): ++ result = function(camera, frame, st, source_seq, **kwargs) + try: +@@ -103,3 +103,3 @@ + @wraps(function) +- def wrapped(camera, frame, source_seq, captured_at=None): ++ def wrapped(camera, frame, source_seq, captured_at=None, **kwargs): + start = _perf() +@@ -117,3 +117,3 @@ + try: +- return function(camera, frame, source_seq, captured_at=captured_at) ++ return function(camera, frame, source_seq, captured_at=captured_at, **kwargs) + finally: diff --git a/integration/unknown_r3/tests/test_alarm_transport.py b/integration/unknown_r3/tests/test_alarm_transport.py new file mode 100644 index 0000000..e1ab355 --- /dev/null +++ b/integration/unknown_r3/tests/test_alarm_transport.py @@ -0,0 +1,272 @@ +"""Offline checks: real candidate alarm functions, fake transports; no model imports.""" +import ast, asyncio, dataclasses as dc, importlib, json, logging, math +from pathlib import Path +import sys, types, unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parent +PKG = 'cc10_offline' +pkg = types.ModuleType(PKG); pkg.__path__ = [str(ROOT/'ptz_tracker_modular')]; sys.modules[PKG]=pkg +cfg = types.ModuleType(PKG+'.config') +cfg.UNKNOWN_INSPECTION_ENABLE=True +cfg.CAMERA_CONFIG={10: {'mac':'00:11:22:33:44:55'},8: {'mac':'00:11:22:33:44:66'}} +cfg.ALARM_COUNT_ON=1;cfg.ALARM_COUNT_OFF=3;cfg.ALARM_HTTP_ENABLE=True +cfg.ALARM_HTTP_BASE='https://offline.invalid';cfg.ALARM_HTTP_PATH='/videodata' +sys.modules[PKG+'.config']=cfg +state=types.ModuleType(PKG+'.state');state.MAIN_LOOP=None;state.alarm_queue=None;state.detected_cameras={10,8} +sys.modules[PKG+'.state']=state +cams=types.ModuleType(PKG+'.cameras_io');cams.get_all=lambda:cfg.CAMERA_CONFIG +sys.modules[PKG+'.cameras_io']=cams +policy=importlib.import_module(PKG+'.alarm_eligibility') +contracts=importlib.import_module(PKG+'.model_class_contract') +boundary=importlib.import_module(PKG+'.alarm_boundary') +alarms=importlib.import_module(PKG+'.alarms') +Context,Frame,Semantic,AlarmEligibility=policy.Context,policy.Frame,policy.Semantic,policy.AlarmEligibility +A='a'*64;B='b'*64 +NAMES=('multirotor_uav','fixed_wing_uav','bird','manned_aircraft') + + +def model(role='semantic_classifier', **kw): + data=dict(path='fake.pt',actual_sha=B if role=='semantic_classifier' else A, + expected_sha=B if role=='semantic_classifier' else A, + raw_names=NAMES if role=='semantic_classifier' else ['airborne_object'], + role=role,metadata_source='loaded_model.names',actual_task='classify' if role=='semantic_classifier' else 'detect') + data.update(kw) + return contracts.validate_contract(**data) + + +def scores(name='multirotor_uav'): + return tuple((n,.91 if n==name else .03) for n in NAMES) + + +class Base(unittest.TestCase): + def setUp(self): + cfg.UNKNOWN_INSPECTION_ENABLE=True;cfg.CAMERA_CONFIG={10:{'mac':'00:11:22:33:44:55'},8:{'mac':'00:11:22:33:44:66'}} + self.clock=10.2 + self.ctx=Context(10,1,('auto',7,12),'camera21:stream_epoch=1',A,B) + self.gate=AlarmEligibility() + self.gate.begin(self.ctx,proposal_contract=model('generic_detector'),classifier_contract=model()) + self.provider={10:self.ctx} + boundary.bind_guard(self.gate,lambda c:self.provider.get(c)) + self.tm=patch.object(boundary,'time',types.SimpleNamespace(monotonic=lambda:self.clock));self.tm.start();self.addCleanup(self.tm.stop) + alarms._H.clear();alarms._http_queue=asyncio.Queue();state.alarm_queue=asyncio.Queue();state.MAIN_LOOP=None + self.f1=Frame(1,10.,(100,100,200,200),(720,1280)) + self.f2=Frame(2,10.1,(101,100,201,200),(720,1280)) + + def put(self,frame,name='multirotor_uav',**kw): + self.assertTrue(self.gate.observe(self.ctx,frame,self.clock)) + return self.gate.submit(self.ctx,Semantic(frame,B,scores(name),100,**kw),self.clock) + + def confirm(self): + self.assertFalse(self.put(self.f1));self.assertTrue(self.put(self.f2)) + + +class EligibilityTests(Base): + def test_unknown_record_and_tracker_score_are_not_evidence(self): + state.rec_active=True;state.tracker_score=.999 + self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + self.assertFalse(boundary.arm_allowed(10)) + def test_one_frame_denied_two_frames_allowed(self): self.confirm() + def test_duplicate_cached_frame_never_second_vote(self): + self.put(self.f1) + self.assertFalse(self.gate.submit(self.ctx,Semantic(self.f1,B,scores(),100),self.clock)) + def test_reordered_frame_cannot_vote(self): + self.put(self.f2) + self.assertFalse(self.gate.observe(self.ctx,self.f1,self.clock)) + self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + def test_result_must_match_original_frame_exactly(self): + self.gate.observe(self.ctx,self.f1,self.clock) + forged=dc.replace(self.f1,bbox=(100,100,201,200)) + self.assertFalse(self.gate.submit(self.ctx,Semantic(forged,B,scores(),100),self.clock)) + def test_inference_completion_cannot_replace_acquisition(self): + self.gate.observe(self.ctx,self.f1,self.clock) + forged=dc.replace(self.f1,acquired_mono=self.clock) + self.assertFalse(self.gate.submit(self.ctx,Semantic(forged,B,scores(),100),self.clock)) + def test_wrong_context_dimensions(self): + self.confirm() + for field,value in [('camera',8),('inspection_id',2),('owner',('auto',8,12)),('source_id','camera21:stream_epoch=2'),('classifier_sha','c'*64),('proposal_sha','d'*64)]: + with self.subTest(field=field):self.assertFalse(self.gate.eligible(dc.replace(self.ctx,**{field:value}),self.clock)) + def test_unpinned_classifier_does_not_vote(self): + self.put(self.f1);self.gate.observe(self.ctx,self.f2,self.clock) + self.assertFalse(self.gate.submit(self.ctx,Semantic(self.f2,'c'*64,scores(),100),self.clock)) + def test_both_semantic_frames_must_remain_fresh(self): + self.confirm();self.assertFalse(self.gate.eligible(self.ctx,10.66)) + def test_future_and_nan_time_rejected(self): + for value in (11.,float('nan'),float('inf')): + with self.subTest(value=value):self.assertFalse(self.gate.observe(self.ctx,dc.replace(self.f1,acquired_mono=value),self.clock)) + def test_duplicate_timestamp_not_independent(self): + self.put(self.f1);self.assertFalse(self.gate.observe(self.ctx,dc.replace(self.f2,acquired_mono=10.),self.clock)) + def test_semantic_class_must_be_consistent(self): + self.put(self.f1);self.assertFalse(self.put(self.f2,'fixed_wing_uav')) + def test_bird_or_manned_aircraft_never_alarm(self): + for name in ('bird','manned_aircraft'): + with self.subTest(name=name): + self.setUp();self.assertFalse(self.put(self.f1,name));self.assertFalse(self.put(self.f2,name)) + def test_bad_details_revoke_previously_confirmed_target(self): + for kwargs in ({'clipped':True},{'occluded':True},{'sharpness':0},{'scores':(('multirotor_uav',1.),)}, {'scores':scores('bird')}): + with self.subTest(kwargs=kwargs): + self.setUp();self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2) + self.gate.observe(self.ctx,f,self.clock) + result=dc.replace(Semantic(f,B,scores(),100),**kwargs) + self.assertFalse(self.gate.submit(self.ctx,result,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + def test_small_original_box_cannot_be_enlarged_crop_proof(self): + f=dc.replace(self.f1,bbox=(100,100,110,110));self.gate.observe(self.ctx,f,self.clock) + self.assertFalse(self.gate.submit(self.ctx,Semantic(f,B,scores(),100),self.clock)) + def test_geometry_discontinuity_revokes(self): + self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2,bbox=(600,100,700,200)) + self.assertFalse(self.gate.observe(self.ctx,f,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + def test_shape_change_revokes(self): + self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2,shape=(1080,1920)) + self.assertFalse(self.gate.observe(self.ctx,f,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + def test_revoke_lost_owner_fullstop_blocks_queued_proof(self): + self.confirm();self.gate.revoke(10);self.assertFalse(self.gate.eligible(self.ctx,self.clock)) + def test_actual_visual_control_owner_tuple_accepts_finite_float(self): + context=dc.replace(self.ctx,owner=(147852,7,1793000000.125,2)) + self.gate.begin(context,proposal_contract=model('generic_detector'),classifier_contract=model()) + self.assertTrue(context.valid()) + for value in (True,float('nan'),float('inf')): + with self.subTest(value=value):self.assertFalse(dc.replace(context,owner=(147852,7,value,2)).valid()) + + def test_begin_requires_verified_two_roles(self): + with self.assertRaises(ValueError):self.gate.begin(self.ctx,proposal_contract=model(),classifier_contract=model()) + def test_frame_ledger_is_bounded(self): + for seq in range(1,101):self.gate.observe(self.ctx,Frame(seq,10+seq/1000,(100,100,200,200),(720,1280)),self.clock) + self.assertEqual(len(self.gate._states[10].frames),16) + + +class ModelTests(unittest.TestCase): + def test_preserves_legacy_numeric_names_no_fixed_wing_alias(self): + c=model('legacy_display',raw_names={0:'Fighter',1:'drone'},actual_sha=A,expected_sha=A) + self.assertEqual(c.names,('Fighter','drone'));self.assertEqual(c.role,'legacy_display') + with self.assertRaises(ValueError):model(raw_names=c.names) + def test_task_cannot_be_inferred_from_label_names(self): + for task in (None,'detect','segment'): + with self.subTest(task=task),self.assertRaises(ValueError):model(actual_task=task) + with self.assertRaises(ValueError):model('generic_detector',actual_task='classify') + + def test_generic_class_zero_is_not_uav_class_zero(self): + self.assertEqual(model('generic_detector').names,('airborne_object',)) + with self.assertRaises(ValueError):model('generic_detector',raw_names=['drone']) + def test_four_semantic_labels_exact_no_silent_alias(self): + for names in [('multirotor_uav','fixedwing_uav','bird','manned_aircraft'),('multirotor_uav','fixed_wing_uav','bird','civil_aircraft')]: + with self.subTest(names=names),self.assertRaises(ValueError):model(raw_names=names) + def test_format_metadata_sources_follow_checkout(self): + for path,source in [('fake.pt','loaded_model.names'),('fake.pth','result.names'),('fake.onnx','onnx.metadata.names'),('fake.onnx','result.names'),('fake.engine','result.names')]: + with self.subTest(path=path,source=source):self.assertEqual(model(path=path,metadata_source=source).names,NAMES) + def test_unsupported_format_or_metadata_fails(self): + for path,source in [('fake.tflite','result.names'),('fake.engine','loaded_model.names'),('fake.pt','config.CLASS_NAMES'),('fake.onnx','sidecar-unpinned')]: + with self.subTest(path=path,source=source),self.assertRaises(ValueError):model(path=path,metadata_source=source) + def test_duplicate_literal_keys_are_not_silently_overwritten(self): + with self.assertRaises(ValueError):contracts.normalize_names("{0: 'Fighter', 0: 'drone'}") + + def test_model_hash_must_match_pin(self): + with self.assertRaises(ValueError):model(actual_sha=A) + def test_no_fallback_when_names_missing_or_ambiguous(self): + for raw in (None,{},[],{True:'x'},{0:'x','0':'y'},{'01':'x'},['bird','bird'],{0:'x',2:'y'}, {0:' x '}): + with self.subTest(raw=raw),self.assertRaises(ValueError):contracts.normalize_names(raw) + def test_onnx_literal_names_are_data_only(self): + self.assertEqual(contracts.normalize_names("{0: 'Fighter', 1: 'drone'}"),('Fighter','drone')) + with self.assertRaises(ValueError):contracts.normalize_names("__import__('os').system('false')") + + +class BoundaryTests(Base): + def test_unknown_false_allows_recording_but_blocks_alarm_ingress(self): + alarms.ingest_detection(10,True) + self.assertFalse(alarms.is_armed(10));self.assertTrue(state.alarm_queue.empty()) + def test_verified_alarm_ingress_and_snapshot(self): + self.confirm();alarms.ingest_detection(10,True) + self.assertTrue(alarms.is_armed(10));self.assertTrue(state.alarm_queue.get_nowait()['data']) + self.assertTrue(alarms._http_queue.get_nowait()['json']['cameras'][1]['alarm']) + def test_raw_queued_boolean_or_untokened_on_denied(self): + self.confirm() + for payload in (True,'true',{'type':'video_alarm','data':True}):self.assertFalse(boundary.filter_ws(payload)['data']) + def test_smoke_cannot_arm_unknown_camera(self): + alarms.queue_alarm_mac('00:11:22:33:44:55',True) + self.assertFalse(state.alarm_queue.get_nowait()['data']) + def test_unmapped_smoke_mac_denied_even_with_other_target(self): + self.confirm();alarms.queue_alarm_mac('00:11:22:33:44:77',True) + self.assertFalse(state.alarm_queue.get_nowait()['data']) + def test_rec_active_alone_and_legacy_events_api_denied(self): + tree=ast.parse((ROOT/'ptz_tracker_modular/events.py').read_text(encoding='utf8')) + fn=next(n for n in tree.body if isinstance(n,ast.FunctionDef) and n.name=='queue_alarm') + queued=[];env={'_dispatch_alarm':queued.append,'_boundary':boundary} + exec(compile(ast.Module(body=[fn],type_ignores=[]),'events-queue','exec'),env) + env['queue_alarm'](10,True);self.assertFalse(queued[0]['data']) + def test_buffered_on_rechecked_after_expiry(self): + self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait();self.clock=10.9 + self.assertFalse(boundary.filter_ws(queued)['data']);self.assertFalse(alarms.is_armed(10)) + def test_owner_or_stream_changed_before_send_denied(self): + self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait() + self.provider[10]=dc.replace(self.ctx,owner=('manual',8,12));self.assertFalse(boundary.filter_ws(queued)['data']) + def test_old_token_cannot_use_new_confirmed_session(self): + self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait() + self.ctx=dc.replace(self.ctx,inspection_id=2);self.provider[10]=self.ctx + self.gate.begin(self.ctx,proposal_contract=model('generic_detector'),classifier_contract=model());self.confirm() + self.assertFalse(boundary.filter_ws(queued)['data']) + def test_http_retry_rechecks_expiry_and_preserves_body(self): + self.confirm();alarms.ingest_detection(10,True);item=alarms._http_queue.get_nowait();self.clock=10.9 + clean=boundary.filter_http(item['json'],item['_cc10_contexts'],alarms._camera_for_mac) + self.assertFalse(any(c['alarm'] for c in clean['cameras']));self.assertTrue(any(c['alarm'] for c in item['json']['cameras'])) + def test_heartbeat_drops_stale_armed_state(self): + self.confirm();alarms.ingest_detection(10,True);self.clock=10.9 + _,body=alarms._bulk_snapshot_for_all_cams();self.assertFalse(any(c['alarm'] for c in body['cameras'])) + def test_provider_failure_and_missing_binding_fail_closed(self): + self.confirm();boundary._context_provider=lambda c:1/0 + self.assertFalse(boundary.arm_allowed(10));boundary._gate=None;self.assertFalse(boundary.arm_allowed(10)) + def test_off_always_passes_as_off(self): + alarms.queue_alarm_mac('00:11:22:33:44:55',False) + self.assertFalse(state.alarm_queue.get_nowait()['data']) + def test_disabled_mode_preserves_legacy(self): + cfg.UNKNOWN_INSPECTION_ENABLE=False;alarms.ingest_detection(10,True);self.assertTrue(alarms.is_armed(10)) + def test_smoke_startup_is_disabled_under_unknown_mode(self): + tree=ast.parse((ROOT/'ptz_tracker_modular/main.py').read_text(encoding='utf8')) + node=next(n for n in ast.walk(tree) if isinstance(n,ast.If) and 'ALARM_SMOKE' in ast.unparse(n.test)) + fake_os=types.SimpleNamespace(getenv=lambda *a:'1') + self.assertFalse(eval(compile(ast.Expression(node.test),'smoke-test','eval'),{'config':cfg,'os':fake_os})) + def run_sender(self, coroutine): + # Windows Proactor creates a TCP socketpair even for fake transports. + # Step this coroutine with a deterministic fake loop instead; no sockets. + class Loop: + tick=0. + def time(self):self.tick+=.02;return self.tick + async def run_in_executor(self, executor, fn):return fn() + async def sleep(*a):return None + loop=Loop();state.MAIN_LOOP=loop + with patch.object(asyncio,'get_running_loop',return_value=loop),patch.object(asyncio,'sleep',sleep): + with self.assertRaises(asyncio.CancelledError):coroutine.send(None) + + def test_heartbeat_internal_transport_retries_disabled_in_unknown_mode(self): + tree=ast.parse((ROOT/'ptz_tracker_modular/alarms.py').read_text(encoding='utf8')) + fn=next(n for n in tree.body if isinstance(n,ast.AsyncFunctionDef) and n.name=='periodic_status_sender') + condition=next(n.test for n in ast.walk(fn) if isinstance(n,ast.If) and 'Retry is not None' in ast.unparse(n.test)) + self.assertFalse(eval(compile(ast.Expression(condition),'retry-policy','eval'),{'Retry':object(),'_boundary':boundary})) + + def test_actual_ws_sender_filters_queued_on(self): + self.confirm();alarms.ingest_detection(10,True);self.clock=10.9 + sent=[] + class WS: + async def __aenter__(self):return self + async def __aexit__(self,*a):return False + async def send(self,payload):sent.append(json.loads(payload));raise asyncio.CancelledError + fake=types.ModuleType('websockets');fake.connect=lambda *a,**k:WS() + with patch.dict(sys.modules,{'websockets':fake}):self.run_sender(alarms.alarm_sender()) + self.assertEqual(sent,[{'type':'video_alarm','data':False}]) + def test_actual_http_sender_filters_queued_on(self): + self.confirm();alarms.ingest_detection(10,True);self.clock=10.9;sent=[] + class Session: + def mount(self,*a):pass + def post(self,url,**kw):sent.append(kw['json']);raise asyncio.CancelledError + req=types.ModuleType('requests');req.Session=Session + adapters=types.ModuleType('requests.adapters');adapters.HTTPAdapter=lambda **kw:object() + with patch.dict(sys.modules,{'requests':req,'requests.adapters':adapters}):self.run_sender(alarms.alarm_http_sender()) + self.assertEqual(len(sent),1);self.assertFalse(any(c['alarm'] for c in sent[0]['cameras'])) + + +if __name__=='__main__': + import socket + # Any accidental real socket attempt fails this run. Fake senders above do not + # instantiate model.py/main.py/streaming.py or contact actual services. + with patch.object(socket,'create_connection',side_effect=AssertionError('network forbidden')): + suite=unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__]) + result=unittest.TextTestRunner(verbosity=2).run(suite) + raise SystemExit(not result.wasSuccessful()) diff --git a/integration/unknown_r3/tests/test_composition.py b/integration/unknown_r3/tests/test_composition.py new file mode 100644 index 0000000..2ca8cb8 --- /dev/null +++ b/integration/unknown_r3/tests/test_composition.py @@ -0,0 +1,67 @@ +import ast +import asyncio +from pathlib import Path +import sys +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +import test_alarm_transport as transport +from cc10_offline import inspection_handoff + +def forbid_network(event, args): + if event.startswith("socket."): + raise RuntimeError("network forbidden") +sys.addaudithook(forbid_network) + + +class CompositionTests(transport.Base): + def test_configured_handoff_guard_controls_real_queued_egress(self): + self.confirm() + bridge = SimpleNamespace(gate=self.gate, active_context=lambda cam: self.provider.get(cam)) + adapter = inspection_handoff.Handoff(bridge, camera_epoch=lambda cam: 1, + latest_visual=lambda cam, now: None) + self.addCleanup(setattr, inspection_handoff, "_configured", None) + inspection_handoff.configure(adapter) + transport.alarms.ingest_detection(10, True) + ws = transport.state.alarm_queue.get_nowait() + http = transport.alarms._http_queue.get_nowait() + self.assertTrue(transport.boundary.filter_ws(ws)["data"]) + self.provider.clear() + self.assertFalse(transport.boundary.filter_ws(ws)["data"]) + result = transport.boundary.filter_http( + http["json"], http["_cc10_contexts"], transport.alarms._camera_for_mac) + self.assertFalse(any(cam["alarm"] for cam in result["cameras"])) + + def test_recording_loop_is_excluded_only_in_unknown_mode(self): + tree = ast.parse((Path(__file__).parent / "ptz_tracker_modular/streaming.py") + .read_text(encoding="utf-8")) + fn = next(node for node in tree.body if isinstance(node, ast.AsyncFunctionDef) + and node.name == "alarm_gate_loop") + transport.cfg.CAMERA_CONFIG = {10: {}} + sent = [] + decisions = [] + def legacy_gate(cam, recording): + decisions.append((cam, recording)) + return recording + async def end_iteration(*args): + raise asyncio.CancelledError + env = dict(config=transport.cfg, + state=SimpleNamespace(ptz_states={10: {"rec_active": True}}), + alarms=SimpleNamespace(queue_alarm=lambda *args: sent.append(args)), + alarm_boundary=transport.boundary, _alarm_gate=legacy_gate, + log_main=transport.logging.getLogger("test"), asyncio=asyncio) + exec(compile(ast.Module(body=[fn], type_ignores=[]), "alarm_gate_loop", "exec"), env) + for enabled, expected in ((True, []), (False, [(10, True)])): + transport.cfg.UNKNOWN_INSPECTION_ENABLE = enabled + sent.clear() + decisions.clear() + with patch.object(asyncio, "sleep", end_iteration): + with self.assertRaises(asyncio.CancelledError): + env["alarm_gate_loop"]().send(None) + self.assertEqual(sent, expected) + self.assertEqual(decisions, expected) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/integration/unknown_r3/tests/test_handoff.py b/integration/unknown_r3/tests/test_handoff.py new file mode 100644 index 0000000..22306ce --- /dev/null +++ b/integration/unknown_r3/tests/test_handoff.py @@ -0,0 +1,278 @@ +"""New exact adapter + checkout handoff checks; no old suites or model imports.""" +import ast +from contextlib import nullcontext, contextmanager +import hashlib +import importlib.util +import io +import json +from pathlib import Path +import sys +import threading +from types import ModuleType, SimpleNamespace +import unittest + +import numpy as np + +sys.dont_write_bytecode = True +HERE = Path(__file__).resolve().parent +package = ModuleType('ptz_tracker_modular'); package.__path__=[str(HERE/'ptz_tracker_modular')] +sys.modules[package.__name__] = package +config = ModuleType('ptz_tracker_modular.config'); config.UNKNOWN_INSPECTION_ENABLE=True +sys.modules[config.__name__] = config +from ptz_tracker_modular import inspection_handoff as wiring +from ptz_tracker_modular.inspection_bridge import InspectionBridge +from ptz_tracker_modular.unknown_inspection import Settings, Session +from ptz_tracker_modular.model_class_contract import ModelContract +from ptz_tracker_modular.inspection_runtime import CropContext, source_key +from ptz_tracker_modular import alarm_boundary + + +class Future: + def __init__(self): self.finished=False; self.value='unset' + def done(self): return self.finished + def set_result(self,value): self.finished=True; self.value=value + + +def checkout_functions(path, namespace): + tree=ast.parse(path.read_text(encoding='utf8')) + names={'_refresh_inference_item','_prepare_visual_inference_item','_postprocess_inference_item'} + nodes=[n for n in tree.body if isinstance(n,ast.FunctionDef) and n.name in names] + assert {n.name for n in nodes}==names + tree=ast.Module(body=[ast.ImportFrom(module='__future__',names=[ast.alias(name='annotations')],level=0)]+nodes,type_ignores=[]) + exec(compile(ast.fix_missing_locations(tree),str(path),'exec'),namespace) + return namespace + + +class HandoffTests(unittest.TestCase): + def setUp(self): + self.st=dict(generation=4,track_started_at=99.,mode='TRACK',tracking_active=True) + self.states={0:self.st};self.epoch=1;self.now=50.;self.utc=100.;self.saved=None + self.lock=threading.RLock();self.calls=[] + self.owner=lambda st:(id(st),st['generation'],st['track_started_at'],0) + cfg=Settings('a'*64,'b'*64,('airborne_object',),.25,.8,.2,16,10.,5.,enabled=True) + proposal=ModelContract('a'*64,'.pt',('airborne_object',),'generic_detector','loaded_model.names','detect') + semantic=ModelContract('b'*64,'.pt',('multirotor_uav','fixed_wing_uav','bird','manned_aircraft'),'semantic_classifier','loaded_model.names','classify') + self.bridge=InspectionBridge(settings=cfg,cameras=(0,),proposal_contract=proposal, + classifier_contract=semantic,owner=self.owner,control_lock=lambda c:self.lock, + controller=lambda *a,**k:self.calls.append((a,k)),alarm_ready=lambda st:False, + state_provider=lambda c:self.states[c],clock=lambda:self.now) + self.adapter=wiring.Handoff(self.bridge,camera_epoch=lambda c:self.epoch, + latest_visual=lambda c,now:self.saved,mono_ns=lambda:round(self.now*1e9),utc_ns=lambda:round(self.utc*1e9)) + wiring.configure(self.adapter) + self.addCleanup(setattr,wiring,'_configured',None) + self.addCleanup(setattr,config,'UNKNOWN_INSPECTION_ENABLE',True) + self.assertTrue(self.adapter.begin_source(0,'cpp_image','conn1',self.epoch)) + self.hooks=checkout_functions(HERE/'ptz_tracker_modular/streaming.py',dict( + inspection_handoff=wiring,state=SimpleNamespace(ptz_states=self.states), + visual_control=SimpleNamespace(enabled=lambda:False), + postprocess_control=lambda *a,**k:self.calls.append((a,k)))) + + def accepted(self,seq=1): + image=np.random.default_rng(seq).integers(0,256,(64,64,3),dtype=np.uint8) + return self.adapter.accepted(0,image,seq,self.epoch) + + def item(self,ticket): + item=(0,ticket.pixels,True,ticket.stamp.source_seq,Future(),7.) + self.assertTrue(self.adapter.bind_item(item,ticket));return item + + def advance(self,seconds): self.now+=seconds;self.utc+=seconds + + def test_accepted_keeps_decode_stamp_and_never_invents_capture_utc(self): + t=self.accepted();self.assertEqual(t.stamp.decode_mono_ns,50_000_000_000) + self.assertEqual(t.stamp.decode_utc_ns,100_000_000_000) + self.assertIsNone(t.stamp.capture_utc_ns);self.assertIsNone(t.stamp.raw_pts) + self.assertFalse(self.st['inspection_authorized']) + self.assertEqual(t.acquired_owner,self.owner(self.st)) + + def test_exact_checkout_refresh_atomically_replaces_pixels_seq_stamp_owner(self): + old=self.accepted();item=self.item(old);self.advance(.05);new=self.accepted(2) + self.assertTrue(self.adapter.pending(new)) + updated,changed=self.hooks['_refresh_inference_item'](item) + self.assertTrue(changed);self.assertIs(updated[1],new.pixels) + self.assertEqual(updated[3],2);self.assertIs(updated[4].inspection_ticket,new) + self.assertIsNone(updated[4].visual_sample);self.assertNotIn('pending_frame',self.st) + self.assertEqual(new.stamp.decode_mono_ns,50_050_000_000) + + def test_mismatched_pending_pixel_ticket_is_dropped_without_restamping(self): + old=self.accepted();item=self.item(old);self.advance(.05);new=self.accepted(2) + self.adapter.pending(new);self.st['pending_frame']=new.pixels.copy() + updated,changed=self.hooks['_refresh_inference_item'](item) + self.assertFalse(changed);self.assertIs(updated[4].inspection_ticket,old) + self.assertNotIn('pending_frame',self.st) + + def test_stale_pending_and_expired_batch_are_dropped_before_inference(self): + old=self.accepted();item=self.item(old);self.adapter.pending(old);self.advance(.651) + self.hooks['_refresh_inference_item'](item) + self.assertNotIn('pending_inspection_ticket',self.st) + self.assertEqual(self.adapter.prune([item]),[]) + self.assertTrue(item[4].done());self.assertIsNone(item[4].value) + + def test_pair_replaces_whole_ticket_with_exact_retained_image(self): + old=self.accepted();item=self.item(old);self.advance(.05);new=self.accepted(2) + sample=self.adapter.visual_sample(new,SimpleNamespace(source_seq=2,ok=True)) + self.saved=(new.pixels,sample) + paired=self.hooks['_prepare_visual_inference_item'](item) + self.assertIs(paired[1],new.pixels);self.assertEqual(paired[3],2) + self.assertIs(paired[4].inspection_ticket,new);self.assertIs(paired[4].visual_sample,sample) + + def test_pair_wrong_pixels_or_expired_visual_cannot_replace_packet(self): + old=self.accepted();item=self.item(old) + sample=self.adapter.visual_sample(old,SimpleNamespace(source_seq=1,ok=True)) + self.saved=(old.pixels.copy(),sample) + self.assertIs(self.hooks['_prepare_visual_inference_item'](item)[1],old.pixels) + self.advance(.251);self.saved=(old.pixels,sample) + self.assertIsNone(self.adapter.visual_sample(old,SimpleNamespace(source_seq=1))) + self.assertIs(self.hooks['_prepare_visual_inference_item'](item)[4].inspection_ticket,old) + + def test_postprocess_over_650ms_or_old_owner_never_calls_controller(self): + item=self.item(self.accepted());self.advance(.651) + self.assertFalse(self.hooks['_postprocess_inference_item'](*item[:2],None,*item[2:5])) + self.assertFalse(self.calls) + self.st['generation']+=1 + self.assertFalse(self.adapter.current(item[4].inspection_ticket)) + + def test_fullstop_and_manual_revoke_before_existing_transition_without_regrant(self): + for reason in ('FULLSTOP','manual'): + t=self.accepted(1 if reason=='FULLSTOP' else 2) + self.st['inspection_authorized']=True + self.st['zoom_no_effect_count']=3 + session=Session(0,9,self.owner(self.st),49.,55.,phase='inspection') + self.st['inspection_session']=session;self.adapter.pending(t) + called=[] + def existing(): + called.append(True);self.assertFalse(self.st['inspection_authorized']) + self.assertEqual(session.phase,'closed');self.st['mode']='MANUAL' + self.assertTrue(self.adapter.transition(0,self.st,t.acquired_owner,reason,existing)) + self.assertTrue(called);self.assertFalse(self.adapter.current(t)) + self.assertEqual(session.deadline_mono,55.);self.assertEqual(self.st['zoom_no_effect_count'],3) + self.assertNotIn('pending_frame',self.st) + self.advance(.01) + + def test_same_owner_revoke_rebind_aba_cannot_revive_old_future(self): + t=self.accepted();item=self.item(t);owner=t.acquired_owner + self.assertTrue(self.adapter.transition(0,self.st,owner,'FULLSTOP',lambda:None)) + self.st['inspection_authorized']=True # explicit external fresh grant for test + self.assertEqual(self.owner(self.st),owner) + self.advance(.01);new=self.accepted(2) + self.assertTrue(self.adapter.current(new));self.assertFalse(self.adapter.current(t)) + self.assertEqual(self.adapter.prune([item]),[]) + + def test_foreign_retired_state_transition_cannot_revoke_current_state(self): + old=self.st.copy();self.st['inspection_authorized']=True + self.assertFalse(self.adapter.transition(0,old,self.owner(old),'FULLSTOP',lambda:self.fail('transition called'))) + self.assertTrue(self.st['inspection_authorized']) + + def test_reconnect_and_camera_lifecycle_epoch_drop_old_ticket(self): + t=self.accepted();self.adapter.begin_source(0,'cpp_image','conn2',self.epoch) + self.assertFalse(self.adapter.current(t));self.assertFalse(self.st['inspection_authorized']) + new=self.accepted(1);self.epoch+=1 + self.assertFalse(self.adapter.current(new));self.assertIsNone(self.adapter.accepted(0,new.pixels,2,self.epoch)) + + def test_source_utc_clock_jump_revokes_original_binding_without_retimestamp(self): + old=self.accepted();self.advance(.1);self.utc+=2.;new=self.accepted(2) + self.assertEqual(new.stamp.clock_epoch,1);self.assertFalse(self.adapter.current(old)) + self.assertFalse(self.st['inspection_authorized']) + + def test_slow_recognition_does_not_block_exact_checkout_control_handoff(self): + t=self.accepted();item=self.item(t);ctx=CropContext(0,1,t.acquired_owner,source_key(t.stamp),'b'*64) + self.bridge.crops.bind(ctx) + self.assertTrue(self.bridge.crops.submit(ctx,t.stamp,t.pixels,(8.,8.,48.,48.),now=self.now,occluded=False)) + entered,release=threading.Event(),threading.Event() + def classify(pixels): + entered.set();release.wait(2) + return (('multirotor_uav',.94),('fixed_wing_uav',.02),('bird',.02),('manned_aircraft',.02)) + thread=threading.Thread(target=lambda:self.bridge.crops.run_one(classify,clock=lambda:self.now)) + thread.start() + try: + self.assertTrue(entered.wait(1)) + # This is the real copied checkout wrapper and actual bridge, not + # classifier completion. No observation is supplied, so no grant. + self.hooks['_postprocess_inference_item'](0,t.pixels,SimpleNamespace(names={0:'airborne_object'},boxes=None),True,1,item[4]) + self.assertEqual(len(self.calls),1) + finally: + release.set();thread.join(2) + self.assertFalse(thread.is_alive()) + + def test_disabled_checkout_handoff_calls_legacy_controller_unchanged(self): + config.UNKNOWN_INSPECTION_ENABLE=False + item=(0,np.zeros((4,4,3),dtype=np.uint8),True,8,Future(),7.) + self.assertIs(self.hooks['_prepare_visual_inference_item'](item),item) + self.assertEqual(self.hooks['_refresh_inference_item'](item),(item,False)) + self.hooks['_postprocess_inference_item'](0,item[1],'legacy',True,8,item[4]) + self.assertEqual(self.calls[-1][0][2],'legacy') + + def test_unknown_startup_and_unbound_alarm_fail_closed(self): + wiring._configured=None + with self.assertRaises(RuntimeError): wiring.require_configured() + self.assertFalse(alarm_boundary.arm_allowed(0)) + + def test_recognition_diagnostics_are_original_decode_age_and_revoke_clears(self): + t=self.accepted();ctx=CropContext(0,1,t.acquired_owner,source_key(t.stamp),'b'*64) + self.bridge.crops.bind(ctx) + self.bridge.crops.submit(ctx,t.stamp,t.pixels,(8.,8.,48.,48.),now=self.now,occluded=False) + self.advance(.1) + def classify(pixels): + self.advance(.2) + return (('multirotor_uav',.94),('fixed_wing_uav',.02),('bird',.02),('manned_aircraft',.02)) + self.assertTrue(self.bridge.crops.run_one(classify,clock=lambda:self.now)) + ages=self.bridge.crops.decode_ages()[0] + self.assertAlmostEqual(ages['recognition_start'],.1) + self.assertAlmostEqual(ages['recognition_complete'],.3) + self.bridge.crops.revoke(0) + self.assertEqual(self.bridge.crops.decode_ages(),{}) + + def test_transition_between_outer_check_and_bridge_admission_rejects_old_future(self): + t=self.accepted();item=self.item(t);interposed=[False] + @contextmanager + def locked(camera): + with self.lock: + if not interposed[0]: + interposed[0]=True + def new_discovery(): + self.st['inspection_authorized']=True + self.st['inspection_session']=Session(0,2,self.owner(self.st),50.,55., + phase='inspection',bbox=(8.,8.,48.,48.),shape=(64,64),contract=self.bridge.settings) + self.adapter.transition(0,self.st,t.acquired_owner,'FULLSTOP',new_discovery) + yield + self.bridge.control_lock=locked + result=SimpleNamespace(names={0:'airborne_object'},boxes=None) + self.assertFalse(self.hooks['_postprocess_inference_item'](0,t.pixels,result,True,1,item[4])) + self.assertTrue(interposed[0]);self.assertFalse(self.calls) + self.assertTrue(self.st['inspection_authorized']) # old callback does not revoke new discovery + + def test_prepare_foreign_camera_future_cannot_consult_or_pair_other_camera(self): + t=self.accepted();item=self.item(t) + self.adapter.latest_visual=lambda *a:self.fail('foreign camera consulted retained image') + foreign=(2,item[1],True,item[3],item[4],item[5]) + self.assertIs(self.adapter.prepare(foreign),foreign) + + def test_decode_receipt_does_not_hide_800ms_control_lock_wait(self): + @contextmanager + def delayed(camera): + self.advance(.8) + with self.lock: yield + self.bridge.control_lock=delayed + self.assertIsNone(self.accepted()) + + def test_decode_receipt_preserves_original_clock_across_short_lock_wait(self): + @contextmanager + def delayed(camera): + self.advance(.2) + with self.lock: yield + self.bridge.control_lock=delayed + ticket=self.accepted() + self.assertEqual(ticket.stamp.decode_mono_ns,50_000_000_000) + self.assertEqual(ticket.stamp.decode_utc_ns,100_000_000_000) + self.assertAlmostEqual(self.now-ticket.stamp.decode_mono_ns/1e9,.2) + + def test_decode_receipt_cannot_bless_owner_changed_while_waiting(self): + @contextmanager + def changed(camera): + self.advance(.1);self.st['generation']+=1 + with self.lock: yield + self.bridge.control_lock=changed + self.assertIsNone(self.accepted()) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/integration/unknown_r3/tests/test_server_path.py b/integration/unknown_r3/tests/test_server_path.py new file mode 100644 index 0000000..e77ff9e --- /dev/null +++ b/integration/unknown_r3/tests/test_server_path.py @@ -0,0 +1,122 @@ +"""Exercise production decoder, MicroBatcher and provenance with fake I/O.""" +import asyncio +import logging +from pathlib import Path +import time +from types import SimpleNamespace +import unittest +from typing import Any, List, Optional, Tuple +from uuid import uuid4 + +import numpy as np + +import test_handoff as fixture +from ptz_tracker_modular import inspection_handoff as wiring + + +class ServerPathTests(unittest.IsolatedAsyncioTestCase): + def setUp(self): + self.fx = fixture.HandoffTests() + self.fx.setUp() + self.addCleanup(self.fx.doCleanups) + self.state = self.fx.st + self.adapter = self.fx.adapter + self.adapter.detect = lambda frames: [None] * len(frames) + self.config = fixture.config + self.config.UNKNOWN_INSPECTION_ENABLE = True + self.config.CAMERA_CONFIG = {0: {}} + self.config.PTZ_CAM_IDS = (0,) + self.config.CPP_WS_URI = 'offline' + self.config.ADAPTIVE_BATCH_ENABLE = False + self.config.BATCH_MIN = self.config.BATCH_MAX = 1 + self.config.BATCH_TARGET_MS = 1 + self.config.SAHI_ENABLE = True # Forbidden for UNKNOWN provenance. + self.server_state = SimpleNamespace(ptz_states={0: self.state}, detected_cameras={0}, + video_clients=set()) + self.env = dict( + asyncio=asyncio, time=time, np=np, cv2=SimpleNamespace(IMREAD_COLOR=1, + imdecode=lambda data, mode: np.full((16, 16, 3), int(data[0]), dtype=np.uint8)), + logger=logging.getLogger('server-path'), config=self.config, state=self.server_state, + uuid4=uuid4, inspection_handoff=wiring, Optional=Optional, + Any=Any, Tuple=Tuple, List=List, InferenceItem=tuple, + _resolve_cpp_cam_id=int, _cpp_stream_map_logged=set(), ensure_runtime_state_for_camera=lambda cam: None, + _has_recent_video_client=lambda now: False, _frontend_raw_preview_due=lambda *args: False, + _target_predecode_fps=lambda *args: 0, _target_infer_fps=lambda *args: 0, + _select_inference_batch=lambda items, size, **kw: (items[:size], items[size:]), + visual_control=SimpleNamespace(enabled=lambda: True), + postprocess_control=lambda *args, **kw: self.fail('Legacy controller used in UNKNOWN'), + sahi_augment_batch=lambda *args: self.fail('SAHI used in UNKNOWN'), + yolo_forward=lambda *args: self.fail('Legacy detector used in UNKNOWN'), + ) + self.state['proc_busy'] = False + self.load('cpp_ws_loop', 'MicroBatcher', '_refresh_inference_item', + '_prepare_visual_inference_item', '_postprocess_inference_item') + + def load(self, *names): + import ast + path = Path(__file__).parent / 'ptz_tracker_modular/streaming.py' + tree = ast.parse(path.read_text(encoding='utf-8')) + definitions = [node for node in tree.body if getattr(node, 'name', None) in names] + exec(compile(ast.Module(body=definitions, type_ignores=[]), str(path), 'exec'), self.env) + + async def test_decoder_batch_passes_exact_pixels_ticket_and_owner(self): + seen = asyncio.Event() + called = [] + original = self.adapter.process_item + + def capture(cam, pixels, result, is_ptz, seq, future): + ticket = future.inspection_ticket + called.append((cam, pixels, is_ptz, seq, ticket, self.adapter.current(ticket))) + loop.call_soon_threadsafe(seen.set) + return True + + self.adapter.process_item = capture + loop = asyncio.get_running_loop() + batcher = self.env['MicroBatcher'](max_batch=1) + self.server_state.batcher = batcher + + class Socket: + async def __aenter__(self): + return self + + async def __aexit__(self, *args): + pass + + async def __aiter__(self): + yield b'\x01\x00\x00\x17' + await asyncio.Future() + + self.env['websockets'] = SimpleNamespace(connect=lambda *args, **kwargs: Socket()) + ws = asyncio.create_task(self.env['cpp_ws_loop']()) + try: + try: + await asyncio.wait_for(seen.wait(), 2) + except TimeoutError: + self.fail((self.state.get('last_source_seq'), self.state.get('inspection_decode_age_s'), self.state.get('proc_busy'), ws.done(), batcher._task.exception() if batcher._task.done() else batcher._task.get_stack())) + cam, pixels, ptz, seq, ticket, current = called.pop() + self.assertEqual((cam, ptz, seq, int(pixels[0, 0, 0])), (0, True, 1, 23)) + self.assertTrue(current) + self.assertIs(ticket.pixels, pixels) + self.assertEqual(ticket.acquired_owner, self.adapter.bridge.owner(self.state)) + finally: + ws.cancel() + batcher._task.cancel() + await asyncio.gather(ws, batcher._task, return_exceptions=True) + self.adapter.process_item = original + self.assertFalse(self.adapter.current(ticket)) + self.assertFalse(self.state['inspection_authorized']) + + async def test_mismatched_ticket_does_not_enter_batch(self): + batcher = self.env['MicroBatcher'](max_batch=1) + try: + ticket = self.fx.accepted() + await batcher.submit(0, ticket.pixels.copy(), True, + source_seq=ticket.stamp.source_seq, inspection_ticket=ticket) + self.assertEqual(batcher._buf, []) + finally: + batcher._task.cancel() + await asyncio.gather(batcher._task, return_exceptions=True) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/ptz_tracker_modular/inspection_bridge.py b/ptz_tracker_modular/inspection_bridge.py index ffa6bf2..b272b86 100644 --- a/ptz_tracker_modular/inspection_bridge.py +++ b/ptz_tracker_modular/inspection_bridge.py @@ -21,12 +21,13 @@ def source_token(key): class InspectionBridge: def __init__(self, *, settings, cameras, proposal_contract, classifier_contract, - owner, control_lock, controller, alarm_ready, state_provider, clock): + owner, control_lock, controller, alarm_ready, state_provider, clock, quality=None): if not isinstance(settings, Settings): raise ValueError('Calibrated settings required') self.settings, self.proposal, self.classifier = settings, proposal_contract, classifier_contract self.owner, self.control_lock, self.controller = owner, control_lock, controller self.alarm_ready, self.state_provider, self.clock = alarm_ready, state_provider, clock + self.quality = quality self.crops = CropWorker(tuple(cameras)) self.gate = AlarmEligibility(semantic_min=settings.semantic_min, margin=settings.semantic_margin, min_side=settings.min_side_px, sharpness_min=settings.min_sharpness) @@ -169,4 +170,9 @@ class InspectionBridge: crop_ctx = CropContext(camera,session.inspection_id,self.owner(st),key,self.settings.classifier_sha) self.crops.bind(crop_ctx) pending = crop_ctx, session.bbox + if self.quality is not None: + try: + occluded = self.quality(camera, stamp, frame, pending[1]) + except Exception: + occluded = None return self.crops.submit(pending[0],stamp,frame,pending[1],now=self.clock(),occluded=occluded) diff --git a/ptz_tracker_modular/inspection_handoff.py b/ptz_tracker_modular/inspection_handoff.py index 9ebcc8c..9412625 100644 --- a/ptz_tracker_modular/inspection_handoff.py +++ b/ptz_tracker_modular/inspection_handoff.py @@ -48,10 +48,11 @@ class FrameTicket: class Handoff: def __init__(self, bridge, *, camera_epoch, latest_visual, - mono_ns=time.monotonic_ns, utc_ns=time.time_ns): + mono_ns=time.monotonic_ns, utc_ns=time.time_ns, authorize=None): self.bridge, self.camera_epoch, self.latest_visual = bridge, camera_epoch, latest_visual self.mono_ns, self.utc_ns = mono_ns, utc_ns self.sources, self.epochs = {}, {} + self.authorize = authorize def begin_source(self, camera, source_id, stream_epoch, epoch): with self.bridge.control_lock(camera): @@ -69,6 +70,19 @@ class Handoff: self._clear_pending(st) return True # NEVER grants inspection_authorized + def end_source(self, camera, stream_epoch=None, *, reason='decoder_disconnected'): + with self.bridge.control_lock(camera): + source = self.sources.get(camera) + if source is None or (stream_epoch is not None and source[0][1] != stream_epoch): + return + self.sources.pop(camera) + self.epochs[camera] += 1 + st = self.bridge.state_provider(camera) + self._clear_pending(st) + self.bridge.revoke(camera, st, reason) + st.pop('inspection_source_binding', None) + st['inspection_control_deadline'] = 0. + @staticmethod def _clear_pending(st): for key in ('pending_frame', 'pending_source_seq', 'pending_inspection_ticket'): @@ -209,7 +223,24 @@ class Handoff: self.note_age(ticket, 'after_inference') return self.bridge.process(camera, ticket.state_ref, ticket.stamp, pixels, result, acquired_owner=ticket.acquired_owner, visual_sample=getattr(future,'visual_sample',None), - handoff_check=lambda: self.current(ticket)) + handoff_check=lambda: self._authorize_current(ticket)) + + def _authorize_current(self, ticket): + if not self.current(ticket): + return False + if self.authorize is None: + return True # Explicit external grant used by an embedded dispatcher. + st = ticket.state_ref + try: + allowed = self.authorize(ticket.stamp.camera_id, ticket.acquired_owner) is True + except Exception: + allowed = False + if not self.current(ticket): + return False + if not allowed and st.get('inspection_authorized') is True: + self.bridge.revoke(ticket.stamp.camera_id, st, 'inspection_authorization_revoked') + st['inspection_authorized'] = allowed + return True # The controller handles STOP for denied active sessions. def visual_sample(self, ticket, result): if not self.current(ticket, age=.25) or result.source_seq != ticket.stamp.source_seq: diff --git a/ptz_tracker_modular/inspection_startup.py b/ptz_tracker_modular/inspection_startup.py new file mode 100644 index 0000000..71f8f26 --- /dev/null +++ b/ptz_tracker_modular/inspection_startup.py @@ -0,0 +1,84 @@ +"""Opt-in server entry point for UNKNOWN using locally pinned PyTorch weights.""" +import asyncio +import hashlib +from pathlib import Path +import time + +from . import config, state, visual_control +from .inspection_bridge import InspectionBridge +from .inspection_handoff import Handoff, configure +from .model_class_contract import normalize_names, validate_contract +from .unknown_inspection import Settings + + +def _file_sha(path): + with path.open('rb') as file: + return hashlib.file_digest(file, 'sha256').hexdigest() + + +def _load_model(yolo, path, expected, role): + path = Path(path) + if not path.is_absolute() or path.suffix.lower() != '.pt' or not path.is_file(): + raise ValueError(f'{role} requires an absolute local .pt checkpoint') + digest = _file_sha(path) + if digest != expected: + raise ValueError(f'{role} checkpoint SHA mismatch') + model = yolo(str(path)) # Read task from the checkpoint, never force it. + if _file_sha(path) != digest: + raise ValueError(f'{role} checkpoint changed while loading') + return model, validate_contract(path, digest, expected, model.names, role=role, + metadata_source='loaded_model.names', actual_task=model.task) + + +class ServerInspection(Handoff): + def __init__(self, bridge, detector, classifier, *, authorize): + super().__init__(bridge, camera_epoch=lambda camera: id(config.CAMERA_CONFIG.get(camera)), + latest_visual=visual_control.latest, authorize=authorize) + self.detector = detector + self.classifier = classifier + + def detect(self, frames): + return self.detector(frames, verbose=False, conf=self.bridge.settings.proposal_min, + imgsz=int(config.INFER_IMGSZ), max_det=64) + + def classify(self, crop): + result, = self.classifier(crop, verbose=False) + names = normalize_names(result.names) + if names != self.bridge.classifier.names: + raise ValueError('Classifier output labels differ from the loaded checkpoint') + scores = result.probs.data.detach().cpu().tolist() + if len(scores) != len(names): + raise ValueError('Classifier output count differs from the loaded checkpoint') + return tuple(zip(names, scores)) + + async def recognition_loop(self): + while True: + if not await asyncio.to_thread(self.bridge.crops.run_one, self.classify, clock=time.monotonic): + await asyncio.sleep(.01) + + +def configure_runtime(): + from ultralytics import YOLO + from .postprocess import _inspection_alarm_ready, postprocess_control + + settings = getattr(config, 'UNKNOWN_INSPECTION_SETTINGS', None) + cameras = tuple(getattr(config, 'UNKNOWN_INSPECTION_CAMERAS', ())) + authorize = getattr(config, 'UNKNOWN_INSPECTION_AUTHORIZE', None) + quality = getattr(config, 'UNKNOWN_INSPECTION_OCCLUDED', None) + if (not isinstance(settings, Settings) or not settings.enabled or not visual_control.enabled() + or not cameras or len(set(cameras)) != len(cameras) + or not set(cameras) <= set(config.PTZ_CAM_IDS) + or not callable(authorize) or not callable(quality)): + raise RuntimeError('UNKNOWN requires calibrated settings, PTZ allowlist, authorization and quality providers') + detector, proposal = _load_model(YOLO, config.UNKNOWN_INSPECTION_PROPOSAL_PATH, + settings.proposal_sha, 'generic_detector') + classifier, semantics = _load_model(YOLO, config.UNKNOWN_INSPECTION_CLASSIFIER_PATH, + settings.classifier_sha, 'semantic_classifier') + bridge = InspectionBridge(settings=settings, cameras=cameras, proposal_contract=proposal, + classifier_contract=semantics, owner=visual_control.owner, control_lock=visual_control.control_lock, + controller=postprocess_control, alarm_ready=_inspection_alarm_ready, + state_provider=lambda camera: state.ptz_states.get(camera, {}), clock=time.monotonic, + quality=quality) + handoff = ServerInspection(bridge, detector, classifier, authorize=authorize) + configure(handoff) + return handoff