refactor(lab): move execution to Worker and manage replay and transfer lifecycles

This commit is contained in:
DCCONSTRUCTIONS
2026-11-10 01:09:39 +03:00
parent cd34b6ce1a
commit fe5900ff3b
101 changed files with 9305 additions and 2256 deletions
+19 -11
View File
@@ -6,6 +6,7 @@ import struct
import subprocess
import threading
from collections import OrderedDict
from collections.abc import Callable
from contextlib import suppress
from dataclasses import dataclass
from pathlib import Path
@@ -70,18 +71,20 @@ class RecordedCameraFrameService:
def __init__(
self,
store: SessionStore,
store: SessionStore | None,
inspector: RecordedMediaInspector,
*,
ffmpeg_path: Path,
cache_root: Path,
max_decode_lanes: int = _DEFAULT_MAX_DECODE_LANES,
max_source_manifests: int = _DEFAULT_MAX_SOURCE_MANIFESTS,
manifest_provider: Callable[[str, str], RecordedMediaManifest] | None = None,
) -> None:
resolved_ffmpeg = ffmpeg_path.expanduser().resolve(strict=True)
if not resolved_ffmpeg.is_file() or not os.access(resolved_ffmpeg, os.X_OK):
raise SessionIntegrityError("ffmpeg is unavailable for camera frame review")
self._store = store
self._manifest_provider = manifest_provider
self._inspector = inspector
self._ffmpeg_path = resolved_ffmpeg
self._cache_root = cache_root.expanduser().absolute()
@@ -217,16 +220,21 @@ class RecordedCameraFrameService:
self._source_manifests.move_to_end(source_key)
if cached is not None:
return cached
replay = self._store.prepare_replay(session_id, speed=1.0, loop=False)
matches = tuple(
artifact
for artifact in self._store.list_recorded_media(session_id)
if artifact.source_path.name == expected_source_name
)
if len(matches) != 1:
raise SessionIntegrityError("recorded camera source is unavailable")
artifact = matches[0]
manifest = self._inspector.inspect(artifact, replay)
if self._manifest_provider is not None:
manifest = self._manifest_provider(session_id, expected_source_name)
else:
if self._store is None:
raise SessionIntegrityError("Recorded camera source provider is unavailable")
replay = self._store.prepare_replay(session_id, speed=1.0, loop=False)
matches = tuple(
artifact
for artifact in self._store.list_recorded_media(session_id)
if artifact.source_path.name == expected_source_name
)
if len(matches) != 1:
raise SessionIntegrityError("recorded camera source is unavailable")
artifact = matches[0]
manifest = self._inspector.inspect(artifact, replay)
with self._coordination:
bound = self._source_manifests.get(source_key)
if bound is None:
+22 -3
View File
@@ -502,6 +502,8 @@ class SessionStore:
raise ValueError("LAB id must use the form 'LAB E21'")
_validate_identifier(result_kind, "LAB result kind")
_validate_identifier(result_id, "LAB result id")
if (self.data_dir / "lab-deletions" / "blocked" / (result_id + ".json")).exists():
raise SessionIntegrityError("LAB was explicitly deleted; republication is blocked")
if source_result_id is not None:
_validate_identifier(source_result_id, "LAB source result id")
if config_sha256 is not None and SHA256_PATTERN.fullmatch(config_sha256) is None:
@@ -763,6 +765,12 @@ class SessionStore:
connection.commit()
return normalized_name
def validate_lab_projection(self, session_id: str) -> None:
"""Validate catalog ownership before any physical storage mutation."""
_validate_identifier(session_id, "LAB session id")
with self._lock, self._connect() as connection:
_require_capability_owned_lab_projection(connection, session_id)
def delete_capability_lab_projection(self, session_id: str) -> None:
"""Delete only one typed LAB catalog projection and its copied rows.
@@ -1868,10 +1876,10 @@ def _require_capability_owned_lab_projection(
if (
summary["archive_id"] != LAB_ARCHIVE_ID
or summary["origin"] != LAB_ORIGIN
or binding.replay_capability is None
or binding.session_id != session_id
or binding.source_session_id == session_id
or lab["include_recorded_media"] not in {0, 1}
or (lab["include_recorded_media"] not in {0, 1}
and binding.replay_capability is not None)
or connection.execute(
"SELECT 1 FROM observation_lab_instances WHERE session_id = ?",
(binding.source_session_id,),
@@ -1882,6 +1890,16 @@ def _require_capability_owned_lab_projection(
"observation session is not a capability-owned LAB projection"
)
_operator_display_name_from_row(lab["operator_display_name"])
if binding.replay_capability is None:
# Old publications predate the media-copy flag. Catalog mutations do not
# grant replay and never traverse or remove evidence paths.
source = connection.execute(
"SELECT origin FROM observation_sessions WHERE session_id = ?",
(binding.source_session_id,),
).fetchone()
if source is None or source["origin"] == LAB_ORIGIN:
raise SessionIntegrityError("LAB projection source ownership is invalid")
return summary, lab
_validate_existing_lab_projection(
connection,
session_id=session_id,
@@ -1891,7 +1909,8 @@ def _require_capability_owned_lab_projection(
duration_seconds=summary["duration_seconds"],
include_recorded_media=bool(lab["include_recorded_media"]),
require_recorded_replay=(
binding.replay_capability.viewer_profile == "recorded-session"
binding.replay_capability is not None
and binding.replay_capability.viewer_profile == "recorded-session"
),
)
return summary, lab