1199 lines
50 KiB
Python
1199 lines
50 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import os
|
|
import shutil
|
|
from collections.abc import AsyncIterator, Iterable
|
|
from contextlib import asynccontextmanager, suppress
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import ValidationError
|
|
|
|
from k1link import __version__
|
|
from k1link.artifact_gateway import configured_artifact_gateway
|
|
from k1link.laboratory.deletion_coordinator import LabDeletionCoordinator
|
|
from k1link.laboratory.local_presentations import LocalPresentations
|
|
from k1link.laboratory.replay_restore import WorkerReplayRestore
|
|
from k1link.laboratory.worker_gateway import (
|
|
LaboratoryGatewayConfiguration,
|
|
LaboratoryWorkerGateway,
|
|
build_laboratory_worker_gateway_router,
|
|
)
|
|
from k1link.missions.default_sources import DefaultPlanningSources
|
|
from k1link.missions.drafts import MissionDrafts
|
|
from k1link.missions.registration_runs import RegistrationRuns
|
|
from k1link.missions.sources import PlanningSources
|
|
from k1link.observatory import (
|
|
LaboratorySetupRegistry,
|
|
LaboratorySetupRegistryError,
|
|
ObservatoryRunPreparationLedger,
|
|
load_observatory_run_preparation_ledger,
|
|
)
|
|
from k1link.observatory.composition_runs import CompositionRunError, CompositionRunStore
|
|
from k1link.observatory.domain_ontology import (
|
|
ObservatoryDomainOntology,
|
|
ObservatoryOntologyError,
|
|
)
|
|
from k1link.observatory.lab_view_profiles import LabViewProfileError, LabViewProfileStore
|
|
from k1link.observatory.m49_queue_binding import (
|
|
M49QueueBindingConfig,
|
|
M49QueueBindingError,
|
|
M49RecordedQueueBindingService,
|
|
)
|
|
from k1link.observatory.modular_composition import CompositionError, ModuleRegistry
|
|
from k1link.observatory.modular_composition_store import ModularCompositionStore
|
|
from k1link.observatory.portable_publication_reconciler import (
|
|
PortablePublicationReconciler,
|
|
)
|
|
from k1link.observatory.portable_queue_binding import (
|
|
PortableQueueBindingError,
|
|
PortableRecordedQueueBindingService,
|
|
)
|
|
from k1link.observatory.portable_replay import PortableReplayService
|
|
from k1link.observatory.portable_result_cache import PortableResultCache
|
|
from k1link.observatory.portable_result_contract import (
|
|
PortableCalculationProfileRegistry,
|
|
PortableResultContractValidatorRegistry,
|
|
)
|
|
from k1link.observatory.portable_result_publisher import (
|
|
resolve_published_portable_calculation_profile,
|
|
)
|
|
from k1link.observatory.portable_result_view import PortableResultViewService
|
|
from k1link.observatory.portable_run_definitions import (
|
|
PortableRunDefinitionRegistry,
|
|
PortableRunDefinitionRegistryError,
|
|
)
|
|
from k1link.observatory.portable_setup_projection import (
|
|
PortableSetupProjectionError,
|
|
PortableSetupProjector,
|
|
portable_calculation_profile_registry,
|
|
)
|
|
from k1link.observatory.portable_worker_integration import (
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED_ENV,
|
|
PortableObservatoryWorkerIntegration,
|
|
PortableWorkerIntegrationError,
|
|
PortableWorkerStorageRoots,
|
|
build_portable_observatory_worker_integration,
|
|
observatory_worker_local_enabled,
|
|
portable_result_validator_registry,
|
|
)
|
|
from k1link.observatory.recorded_jobs import (
|
|
ObservatoryRecordedJobQueue,
|
|
ObservatoryRecordedQueueError,
|
|
RecordedRunDefinitionRegistry,
|
|
)
|
|
from k1link.reconstruction.session_versions import SessionMapVersions
|
|
from k1link.sessions import (
|
|
MaterializedRecording,
|
|
RecordedCameraFrameService,
|
|
RecordedCameraPlaybackSource,
|
|
RecordedMediaInspector,
|
|
RecordedMediaManifest,
|
|
RecordingPreparationQueueFull,
|
|
ReplayCommand,
|
|
SessionRecordingMaterializer,
|
|
SessionRecordingPreparationManager,
|
|
SessionStore,
|
|
)
|
|
from k1link.sessions.map_recording import MapReplaySessionStore
|
|
from k1link.sessions.models import SessionSummary
|
|
from k1link.sessions.overview import SessionOverviewService
|
|
from k1link.simulation.projects import SimulationProjectService, SimulationProjectStore
|
|
from k1link.web.ai_polygon_api import build_ai_polygon_router
|
|
from k1link.web.artifact_health_api import build_artifact_health_router
|
|
from k1link.web.compute_contour_api import build_compute_contour_router
|
|
from k1link.web.compute_connection_api import build_compute_connection_router
|
|
from k1link.web.device_plugin_composition import load_installed_device_plugins
|
|
from k1link.web.environment_api import build_environment_router
|
|
from k1link.web.equipment_firmware_api import router as equipment_firmware_router
|
|
from k1link.web.fleet_api import router as fleet_router
|
|
from k1link.web.frontend_assets import ControlStationStaticFiles, frontend_build_id
|
|
from k1link.web.independent_equipment_api import router as equipment_router
|
|
from k1link.web.installer_api import router as installer_router
|
|
from k1link.web.local_storage_api import build_local_storage_router
|
|
from k1link.web.map_api import (
|
|
MapGatewayConfiguration,
|
|
MapGatewayProxy,
|
|
build_map_router,
|
|
)
|
|
from k1link.web.map_view_api import build_map_view_router
|
|
from k1link.web.mission_planner_api import build_mission_planner_router
|
|
from k1link.web.mission_registration_api import build_mission_registration_router
|
|
from k1link.web.modular_observatory_api import build_modular_observatory_router
|
|
from k1link.web.observatory_api import build_observatory_router
|
|
from k1link.web.observatory_worker_api import (
|
|
ObservatoryWorkerAuthentication,
|
|
build_observatory_worker_router,
|
|
load_observatory_worker_authentication,
|
|
)
|
|
from k1link.web.plugin_catalog import DevicePluginCatalog, PluginCatalogError
|
|
from k1link.web.plugin_runtime import (
|
|
STATE_READ_ACTION_ID,
|
|
DevicePluginActionRequest,
|
|
DevicePluginDispatcher,
|
|
PluginActionNotFoundError,
|
|
PluginExecutionError,
|
|
PluginNotFoundError,
|
|
PluginRuntimeUnavailableError,
|
|
)
|
|
from k1link.web.polygon_api import build_polygon_router, configured_polygon_runs_root
|
|
from k1link.web.portable_replay_api import build_portable_replay_router
|
|
from k1link.web.response_compression import ResponseCompressionMiddleware
|
|
from k1link.web.rover_api import router as rover_router
|
|
from k1link.web.runtime_diagnostics import configure_scanner_diagnostics
|
|
from k1link.web.runtime_http_timing import RuntimeHttpTimingMiddleware
|
|
from k1link.web.runtime_readiness import (
|
|
BackgroundReconcilerReadiness,
|
|
build_runtime_readiness,
|
|
)
|
|
from k1link.web.scene_profiles_api import build_scene_profiles_router
|
|
from k1link.web.session_api import build_session_router
|
|
from k1link.web.session_overview_api import build_session_overview_router
|
|
from k1link.web.simulation_projects_api import build_simulation_projects_router
|
|
from k1link.web.simulation_world_provider_api import build_simulation_world_provider_router
|
|
from k1link.web.system_telemetry_api import build_system_telemetry_router
|
|
from k1link.web.viewer_diagnostics_api import build_viewer_diagnostics_router
|
|
from k1link.web.vps_host_api import router as vps_host_router
|
|
from k1link.web.server_catalog_api import router as server_catalog_router
|
|
from k1link.web.vps_host_api import start as start_vps_hosts
|
|
|
|
REPOSITORY_ROOT = Path(__file__).resolve().parents[3]
|
|
INVALID_REQUEST_DETAIL = "Некорректные параметры запроса."
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY: LaboratorySetupRegistry | None
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY_ERROR: str | None
|
|
try:
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY = LaboratorySetupRegistry.from_file(
|
|
REPOSITORY_ROOT / "config" / "observatory-laboratory-setups.json",
|
|
repository_root=REPOSITORY_ROOT,
|
|
)
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY_ERROR = None
|
|
except (LaboratorySetupRegistryError, OSError) as exc:
|
|
# Observatory is an optional observation-only slice. Its configuration must
|
|
# fail closed locally without preventing K1, Simulation or legacy LAB from
|
|
# starting.
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY = None
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY_ERROR = str(exc)
|
|
|
|
|
|
def _resolve_media_tool(name: str) -> Path | None:
|
|
"""Resolve media tools under interactive shells and minimal launchd PATHs."""
|
|
|
|
discovered = shutil.which(name)
|
|
candidates = (
|
|
Path(discovered) if discovered is not None else None,
|
|
Path("/opt/homebrew/bin") / name,
|
|
Path("/usr/local/bin") / name,
|
|
Path("/usr/bin") / name,
|
|
)
|
|
for candidate in candidates:
|
|
if candidate is not None and candidate.is_file() and os.access(candidate, os.X_OK):
|
|
return candidate
|
|
return None
|
|
|
|
|
|
plugin_environment = load_installed_device_plugins(REPOSITORY_ROOT)
|
|
plugin_catalog: DevicePluginCatalog = plugin_environment.catalog
|
|
plugin_dispatcher: DevicePluginDispatcher = plugin_environment.dispatcher
|
|
session_store = SessionStore(REPOSITORY_ROOT)
|
|
OBSERVATORY_AI_COMPOSITIONS: ModularCompositionStore | None
|
|
OBSERVATORY_AI_COMPOSITION_RUNS: CompositionRunStore | None
|
|
OBSERVATORY_DOMAIN_ONTOLOGY: ObservatoryDomainOntology | None
|
|
OBSERVATORY_LAB_VIEW_PROFILES: LabViewProfileStore | None
|
|
try:
|
|
OBSERVATORY_DOMAIN_ONTOLOGY = ObservatoryDomainOntology.from_file(
|
|
REPOSITORY_ROOT / "config" / "observatory-domain-ontology.json"
|
|
)
|
|
OBSERVATORY_AI_COMPOSITIONS = ModularCompositionStore(
|
|
session_store.data_dir / "observatory-ai-compositions",
|
|
ModuleRegistry.from_file(REPOSITORY_ROOT / "config" / "observatory-ai-modules.json"),
|
|
)
|
|
OBSERVATORY_AI_COMPOSITION_RUNS = CompositionRunStore(
|
|
session_store.data_dir / "observatory-ai-composition-runs"
|
|
)
|
|
OBSERVATORY_LAB_VIEW_PROFILES = LabViewProfileStore(
|
|
session_store.data_dir / "observatory-lab-view-profiles"
|
|
)
|
|
except (
|
|
CompositionError,
|
|
CompositionRunError,
|
|
LabViewProfileError,
|
|
ObservatoryOntologyError,
|
|
OSError,
|
|
ValueError,
|
|
):
|
|
# A modular catalog failure cannot disable recordings, existing LABs or Legacy.
|
|
OBSERVATORY_AI_COMPOSITIONS = None
|
|
OBSERVATORY_AI_COMPOSITION_RUNS = None
|
|
OBSERVATORY_DOMAIN_ONTOLOGY = None
|
|
OBSERVATORY_LAB_VIEW_PROFILES = None
|
|
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY: PortableRunDefinitionRegistry | None
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY_ERROR: str | None
|
|
OBSERVATORY_PORTABLE_CALCULATION_PROFILES: PortableCalculationProfileRegistry | None
|
|
OBSERVATORY_PORTABLE_RESULT_VALIDATORS: PortableResultContractValidatorRegistry | None
|
|
try:
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY = PortableRunDefinitionRegistry.from_file(
|
|
REPOSITORY_ROOT / "config" / "observatory-portable-run-definitions.json"
|
|
)
|
|
OBSERVATORY_PORTABLE_CALCULATION_PROFILES = portable_calculation_profile_registry(
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY
|
|
)
|
|
OBSERVATORY_PORTABLE_RESULT_VALIDATORS = portable_result_validator_registry(
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY
|
|
)
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY_ERROR = None
|
|
except (
|
|
PortableRunDefinitionRegistryError,
|
|
PortableWorkerIntegrationError,
|
|
OSError,
|
|
ValueError,
|
|
) as exc:
|
|
# Portable definitions are an optional observation-only slice. Registry
|
|
# drift cannot affect K1, Simulation, legacy LAB, or the exact M49 binding.
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY = None
|
|
OBSERVATORY_PORTABLE_CALCULATION_PROFILES = None
|
|
OBSERVATORY_PORTABLE_RESULT_VALIDATORS = None
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY_ERROR = str(exc)
|
|
|
|
|
|
def _resolve_observatory_calculation_profile(
|
|
summary: SessionSummary,
|
|
) -> dict[str, object] | None:
|
|
if OBSERVATORY_LABORATORY_SETUP_REGISTRY is not None:
|
|
legacy = OBSERVATORY_LABORATORY_SETUP_REGISTRY.observatory_calculation_profile(summary)
|
|
if legacy is not None:
|
|
return legacy
|
|
if (
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY is None
|
|
or OBSERVATORY_PORTABLE_CALCULATION_PROFILES is None
|
|
):
|
|
return None
|
|
return resolve_published_portable_calculation_profile(
|
|
summary,
|
|
definitions=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
calculation_profiles=OBSERVATORY_PORTABLE_CALCULATION_PROFILES,
|
|
)
|
|
|
|
|
|
def _load_optional_observatory_worker_authentication(
|
|
recorded_job_queue: ObservatoryRecordedJobQueue | None,
|
|
*,
|
|
token_path: Path,
|
|
) -> tuple[ObservatoryWorkerAuthentication | None, str | None]:
|
|
"""Load the optional Worker credential without widening app startup risk."""
|
|
|
|
if recorded_job_queue is None:
|
|
return None, "Observatory recorded-job queue is unavailable"
|
|
try:
|
|
return load_observatory_worker_authentication(token_path), None
|
|
except ValueError as exc:
|
|
# Worker pull transport is optional. A missing or unsafe credential
|
|
# disables only this router; K1, Simulation and legacy LAB still start.
|
|
return None, str(exc)
|
|
|
|
|
|
OBSERVATORY_RUN_PREPARATION_LEDGER: ObservatoryRunPreparationLedger | None
|
|
OBSERVATORY_RUN_PREPARATION_LEDGER_ERROR: str | None
|
|
(
|
|
OBSERVATORY_RUN_PREPARATION_LEDGER,
|
|
OBSERVATORY_RUN_PREPARATION_LEDGER_ERROR,
|
|
) = load_observatory_run_preparation_ledger(session_store.data_dir)
|
|
OBSERVATORY_RECORDED_BINDING_SERVICE: M49RecordedQueueBindingService | None
|
|
OBSERVATORY_RECORDED_JOB_QUEUE: ObservatoryRecordedJobQueue | None
|
|
OBSERVATORY_RECORDED_JOB_QUEUE_ERROR: str | None
|
|
try:
|
|
if OBSERVATORY_LABORATORY_SETUP_REGISTRY is None:
|
|
raise M49QueueBindingError("observatory setup registry is unavailable")
|
|
OBSERVATORY_RECORDED_BINDING_SERVICE = M49RecordedQueueBindingService(
|
|
data_dir=session_store.data_dir,
|
|
session_store=session_store,
|
|
setup_registry=OBSERVATORY_LABORATORY_SETUP_REGISTRY,
|
|
config=M49QueueBindingConfig.from_file(
|
|
REPOSITORY_ROOT / "config" / "observatory-m49-recorded-queue-binding.json"
|
|
),
|
|
)
|
|
recorded_definitions = list(OBSERVATORY_RECORDED_BINDING_SERVICE.definitions.definitions)
|
|
if OBSERVATORY_PORTABLE_DEFINITION_REGISTRY is not None:
|
|
recorded_definitions.extend(
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY.ready_recorded_definitions()
|
|
)
|
|
OBSERVATORY_RECORDED_JOB_QUEUE = ObservatoryRecordedJobQueue(
|
|
session_store.data_dir,
|
|
definitions=RecordedRunDefinitionRegistry(tuple(recorded_definitions)),
|
|
)
|
|
OBSERVATORY_RECORDED_JOB_QUEUE_ERROR = None
|
|
except (M49QueueBindingError, ObservatoryRecordedQueueError, OSError, ValueError) as exc:
|
|
# Recorded execution remains an optional observation-only slice. A drifted
|
|
# seal or queue must fail closed without preventing K1, Simulation or legacy
|
|
# LAB from starting.
|
|
OBSERVATORY_RECORDED_BINDING_SERVICE = None
|
|
OBSERVATORY_RECORDED_JOB_QUEUE = None
|
|
OBSERVATORY_RECORDED_JOB_QUEUE_ERROR = str(exc)
|
|
OBSERVATORY_WORKER_TOKEN_PATH = session_store.data_dir / "worker-auth" / "observatory-worker.token"
|
|
OBSERVATORY_WORKER_PRODUCTION_API_ENABLED = False
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED: bool
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED_ERROR: str | None
|
|
try:
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED = observatory_worker_local_enabled()
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED_ERROR = None
|
|
except PortableWorkerIntegrationError as exc:
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED = False
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED_ERROR = str(exc)
|
|
OBSERVATORY_WORKER_AUTHENTICATION: ObservatoryWorkerAuthentication | None
|
|
OBSERVATORY_WORKER_AUTHENTICATION_ERROR: str | None
|
|
OBSERVATORY_WORKER_API_ERROR: str | None
|
|
(
|
|
OBSERVATORY_WORKER_AUTHENTICATION,
|
|
OBSERVATORY_WORKER_AUTHENTICATION_ERROR,
|
|
) = _load_optional_observatory_worker_authentication(
|
|
OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
token_path=OBSERVATORY_WORKER_TOKEN_PATH,
|
|
)
|
|
simulation_project_store = SimulationProjectStore(session_store.data_dir)
|
|
simulation_project_service = SimulationProjectService(simulation_project_store)
|
|
session_artifact_gateway = configured_artifact_gateway(session_store.data_dir)
|
|
|
|
session_recording_materializer = SessionRecordingMaterializer(
|
|
session_store.data_dir,
|
|
exporters=plugin_environment.recording_exporters,
|
|
artifact_gateway=session_artifact_gateway,
|
|
)
|
|
|
|
session_map_versions = SessionMapVersions(session_store.data_dir)
|
|
operator_session_store = MapReplaySessionStore(session_store, session_map_versions)
|
|
session_overview_service = SessionOverviewService(session_store, plugin_environment.overview_exporters,
|
|
map_versions=session_map_versions)
|
|
original_planning_sources = PlanningSources(
|
|
session_store, plugin_environment.planning_exporters, plugin_environment.submap_extractors,
|
|
plugin_environment.scene_submap_extractors)
|
|
mission_drafts = MissionDrafts(session_store.data_dir / 'missions',
|
|
DefaultPlanningSources(original_planning_sources, session_map_versions))
|
|
mission_registration_runs = RegistrationRuns(mission_drafts)
|
|
from k1link.missions.live_tests import PlanningLiveTests
|
|
from k1link.web.planning_live_api import build_planning_live_router
|
|
|
|
planning_live_tests = PlanningLiveTests(mission_drafts, plugin_environment.live_planning_sources,
|
|
mission_registration_runs.lock,
|
|
capture_recorder=session_store.record_planning_capture)
|
|
session_recorded_media_inspector = RecordedMediaInspector(
|
|
session_store.data_dir / "recorded-media-preparations"
|
|
)
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION: PortableObservatoryWorkerIntegration | None
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION_ERROR: str | None
|
|
OBSERVATORY_PORTABLE_WORKER_STORAGE_ROOTS: PortableWorkerStorageRoots | None = None
|
|
try:
|
|
if OBSERVATORY_PORTABLE_DEFINITION_REGISTRY is None:
|
|
raise PortableWorkerIntegrationError(
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY_ERROR
|
|
or "portable definition registry is unavailable"
|
|
)
|
|
if OBSERVATORY_PORTABLE_CALCULATION_PROFILES is None:
|
|
raise PortableWorkerIntegrationError("portable calculation profile registry is unavailable")
|
|
if OBSERVATORY_PORTABLE_RESULT_VALIDATORS is None:
|
|
raise PortableWorkerIntegrationError("portable result validator registry is unavailable")
|
|
if OBSERVATORY_RECORDED_JOB_QUEUE is None:
|
|
raise PortableWorkerIntegrationError(
|
|
OBSERVATORY_RECORDED_JOB_QUEUE_ERROR or "Observatory recorded-job queue is unavailable"
|
|
)
|
|
if session_artifact_gateway is None:
|
|
raise PortableWorkerIntegrationError("central artifact store is not configured")
|
|
if session_artifact_gateway.status().central_status != "ready":
|
|
raise PortableWorkerIntegrationError("central artifact store is unavailable")
|
|
OBSERVATORY_PORTABLE_WORKER_STORAGE_ROOTS = PortableWorkerStorageRoots.from_environment(
|
|
artifact_store_root=session_artifact_gateway.store.root,
|
|
)
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION = build_portable_observatory_worker_integration(
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
session_store=session_store,
|
|
media_inspector=session_recorded_media_inspector,
|
|
definitions=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
artifact_store=session_artifact_gateway.store,
|
|
calculation_profiles=OBSERVATORY_PORTABLE_CALCULATION_PROFILES,
|
|
validators=OBSERVATORY_PORTABLE_RESULT_VALIDATORS,
|
|
source_cas_root=(OBSERVATORY_PORTABLE_WORKER_STORAGE_ROOTS.source_cas_root),
|
|
result_staging_root=(OBSERVATORY_PORTABLE_WORKER_STORAGE_ROOTS.result_staging_root),
|
|
)
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION_ERROR = None
|
|
except (PortableWorkerIntegrationError, OSError, ValueError) as exc:
|
|
# Constructing this dormant foundation does not enable the Worker router.
|
|
# Failure remains isolated from K1, Simulation and legacy LAB.
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION = None
|
|
OBSERVATORY_PORTABLE_WORKER_INTEGRATION_ERROR = str(exc)
|
|
OBSERVATORY_PUBLICATION_RECONCILER = (
|
|
None
|
|
if (OBSERVATORY_RECORDED_JOB_QUEUE is None or OBSERVATORY_PORTABLE_WORKER_INTEGRATION is None)
|
|
else PortablePublicationReconciler(
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
artifact_transport=OBSERVATORY_PORTABLE_WORKER_INTEGRATION.artifact_transport,
|
|
result_publisher=OBSERVATORY_PORTABLE_WORKER_INTEGRATION.result_publisher,
|
|
)
|
|
)
|
|
OBSERVATORY_WORKER_API_GATE_ENABLED = OBSERVATORY_WORKER_LOCAL_ENABLED
|
|
OBSERVATORY_WORKER_CLAIM_LEASE_READY = (
|
|
OBSERVATORY_WORKER_API_GATE_ENABLED and OBSERVATORY_RECORDED_JOB_QUEUE is not None
|
|
)
|
|
OBSERVATORY_WORKER_VERIFIED_RESULT_PUBLISHER_READY = (
|
|
OBSERVATORY_WORKER_API_GATE_ENABLED and OBSERVATORY_PORTABLE_WORKER_INTEGRATION is not None
|
|
)
|
|
OBSERVATORY_WORKER_DISPATCH_READY = (
|
|
OBSERVATORY_WORKER_CLAIM_LEASE_READY
|
|
and OBSERVATORY_WORKER_VERIFIED_RESULT_PUBLISHER_READY
|
|
and OBSERVATORY_RECORDED_JOB_QUEUE is not None
|
|
and OBSERVATORY_WORKER_AUTHENTICATION is not None
|
|
and OBSERVATORY_PORTABLE_WORKER_INTEGRATION is not None
|
|
)
|
|
if OBSERVATORY_WORKER_DISPATCH_READY:
|
|
OBSERVATORY_WORKER_API_ERROR = None
|
|
else:
|
|
worker_api_errors: list[str] = []
|
|
if not OBSERVATORY_WORKER_API_GATE_ENABLED:
|
|
worker_api_errors.append(
|
|
OBSERVATORY_WORKER_LOCAL_ENABLED_ERROR
|
|
or (
|
|
"local-only Worker API gate is disabled; set "
|
|
f"{OBSERVATORY_WORKER_LOCAL_ENABLED_ENV}=1 to enable it"
|
|
)
|
|
)
|
|
if OBSERVATORY_WORKER_AUTHENTICATION_ERROR is not None:
|
|
worker_api_errors.append(
|
|
f"authentication unavailable: {OBSERVATORY_WORKER_AUTHENTICATION_ERROR}"
|
|
)
|
|
if OBSERVATORY_PORTABLE_WORKER_INTEGRATION_ERROR is not None:
|
|
worker_api_errors.append(
|
|
f"integration unavailable: {OBSERVATORY_PORTABLE_WORKER_INTEGRATION_ERROR}"
|
|
)
|
|
OBSERVATORY_WORKER_API_ERROR = "Worker pull API is disabled; " + "; ".join(worker_api_errors)
|
|
OBSERVATORY_PORTABLE_BINDING_SERVICE: PortableRecordedQueueBindingService | None
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR: PortableSetupProjector | None
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR_ERROR: str | None
|
|
OBSERVATORY_PORTABLE_RESULT_CACHE: PortableResultCache | None = None
|
|
try:
|
|
if OBSERVATORY_PORTABLE_DEFINITION_REGISTRY is None:
|
|
raise PortableSetupProjectionError(
|
|
OBSERVATORY_PORTABLE_DEFINITION_REGISTRY_ERROR
|
|
or "portable definition registry is unavailable"
|
|
)
|
|
if (
|
|
session_artifact_gateway is not None
|
|
and OBSERVATORY_RECORDED_JOB_QUEUE is not None
|
|
and OBSERVATORY_PORTABLE_CALCULATION_PROFILES is not None
|
|
):
|
|
OBSERVATORY_PORTABLE_RESULT_CACHE = PortableResultCache(
|
|
sessions=session_store,
|
|
artifacts=session_artifact_gateway.store,
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
definitions=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
calculation_profiles=OBSERVATORY_PORTABLE_CALCULATION_PROFILES,
|
|
)
|
|
OBSERVATORY_PORTABLE_BINDING_SERVICE = PortableRecordedQueueBindingService(
|
|
data_dir=session_store.data_dir,
|
|
session_store=session_store,
|
|
media_inspector=session_recorded_media_inspector,
|
|
definitions=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
published_result_available=(
|
|
None
|
|
if OBSERVATORY_PORTABLE_RESULT_CACHE is None
|
|
else OBSERVATORY_PORTABLE_RESULT_CACHE.available
|
|
),
|
|
)
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR = PortableSetupProjector(
|
|
registry=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
capability_probe=OBSERVATORY_PORTABLE_BINDING_SERVICE,
|
|
dispatch_available=OBSERVATORY_WORKER_DISPATCH_READY,
|
|
equipment_capture_registry=session_store.equipment_capture_registry,
|
|
result_cache=OBSERVATORY_PORTABLE_RESULT_CACHE,
|
|
)
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR_ERROR = None
|
|
except (
|
|
PortableQueueBindingError,
|
|
PortableRunDefinitionRegistryError,
|
|
PortableSetupProjectionError,
|
|
OSError,
|
|
ValueError,
|
|
) as exc:
|
|
# Portable setup execution is an optional observation-only slice. A drifted
|
|
# registry cannot affect K1, Simulation, legacy LAB, or the exact M49 queue.
|
|
OBSERVATORY_PORTABLE_BINDING_SERVICE = None
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR = None
|
|
OBSERVATORY_PORTABLE_SETUP_PROJECTOR_ERROR = str(exc)
|
|
_ffmpeg = _resolve_media_tool("ffmpeg")
|
|
_ffprobe = _resolve_media_tool("ffprobe")
|
|
session_recorded_camera_frame_service = (
|
|
RecordedCameraFrameService(
|
|
session_store,
|
|
session_recorded_media_inspector,
|
|
ffmpeg_path=_ffmpeg,
|
|
cache_root=session_store.data_dir / "camera-frame-cache",
|
|
)
|
|
if _ffmpeg is not None
|
|
else None
|
|
)
|
|
try:
|
|
lab_gateway_configuration = LaboratoryGatewayConfiguration.from_file(
|
|
session_store.data_dir / "laboratory-worker-gateway.json")
|
|
except (OSError, ValueError):
|
|
lab_gateway_configuration = None
|
|
laboratory_worker_gateway = LaboratoryWorkerGateway(lab_gateway_configuration)
|
|
map_gateway_proxy = MapGatewayProxy(MapGatewayConfiguration.from_environment())
|
|
recording_reconciler_readiness = BackgroundReconcilerReadiness()
|
|
|
|
|
|
def _prepare_recorded_media_for_launch(
|
|
command: ReplayCommand,
|
|
_: MaterializedRecording,
|
|
) -> tuple[RecordedMediaManifest, ...]:
|
|
"""Prepare all camera descriptors inside the background job."""
|
|
|
|
return tuple(
|
|
session_recorded_media_inspector.inspect(artifact, command)
|
|
for artifact in session_store.list_recorded_media(command.session_id)
|
|
)
|
|
|
|
|
|
def _restore_recorded_media_for_launch(
|
|
command: ReplayCommand,
|
|
_: MaterializedRecording,
|
|
) -> tuple[RecordedMediaManifest, ...] | None:
|
|
"""Restore only previously published media descriptors."""
|
|
|
|
restored: list[RecordedMediaManifest] = []
|
|
for artifact in session_store.list_recorded_media(command.session_id):
|
|
manifest = session_recorded_media_inspector.restore_prepared(artifact, command)
|
|
if manifest is None:
|
|
return None
|
|
restored.append(manifest)
|
|
return tuple(restored)
|
|
|
|
|
|
session_recording_preparation_manager = SessionRecordingPreparationManager(
|
|
session_recording_materializer,
|
|
ready_preparer=_prepare_recorded_media_for_launch,
|
|
ready_restorer=_restore_recorded_media_for_launch,
|
|
)
|
|
|
|
|
|
def _m48_recorded_camera_playback_source(
|
|
session_id: str,
|
|
) -> RecordedCameraPlaybackSource:
|
|
"""Publish the durable replay package before exposing its manifest URL."""
|
|
|
|
if session_recorded_camera_frame_service is None:
|
|
raise RuntimeError("recorded camera playback is unavailable")
|
|
command = operator_session_store.prepare_replay(session_id, speed=1.0, loop=False)
|
|
snapshot = session_recording_preparation_manager.restore_published(command)
|
|
if snapshot is None or snapshot.state != "ready" or snapshot.recorded_media is None:
|
|
raise RuntimeError("recorded camera playback package is not published")
|
|
return session_recorded_camera_frame_service.playback_source(session_id)
|
|
|
|
|
|
|
|
|
|
def _canonical_lab_recording_source(session_id: str) -> tuple[Path, str] | None:
|
|
"""Read a ready operator recording for the existing portable result viewer."""
|
|
snapshot = session_recording_preparation_manager.status(session_id)
|
|
if snapshot is None or snapshot.state != "ready" or snapshot.recording is None:
|
|
return None
|
|
if snapshot.command.map_version is not None:
|
|
return None
|
|
return snapshot.recording.path, snapshot.recording.sha256
|
|
|
|
|
|
def refresh_observation_catalog() -> tuple[str, ...]:
|
|
"""Discover completed or recoverable local evidence without copying payloads."""
|
|
|
|
imported = [
|
|
session_id
|
|
for archive in plugin_environment.observation_archives
|
|
for session_id in session_store.reconcile_archive(archive)
|
|
]
|
|
return tuple(dict.fromkeys(imported))
|
|
|
|
|
|
def finalized_replayable_recording_ids() -> tuple[str, ...]:
|
|
"""List finalized replayable catalog identities without scheduling work."""
|
|
|
|
finalized: list[str] = []
|
|
cursor: str | None = None
|
|
while True:
|
|
page = session_store.list_recent(
|
|
limit=100,
|
|
cursor=cursor,
|
|
include_capability_projections=False,
|
|
)
|
|
finalized.extend(
|
|
summary.session_id
|
|
for summary in page.items
|
|
if summary.replayable and summary.status in {"ready", "interrupted", "failed"}
|
|
)
|
|
cursor = page.next_cursor
|
|
if cursor is None:
|
|
return tuple(finalized)
|
|
|
|
|
|
def enqueue_replayable_recordings(session_ids: Iterable[str]) -> tuple[str, ...]:
|
|
"""Schedule only explicitly selected newly finalized sessions."""
|
|
|
|
enqueued: list[str] = []
|
|
for session_id in dict.fromkeys(session_ids):
|
|
try:
|
|
command = operator_session_store.prepare_replay(session_id)
|
|
session_recording_preparation_manager.enqueue(
|
|
command,
|
|
retry_interrupted=True,
|
|
)
|
|
except RecordingPreparationQueueFull:
|
|
return tuple(enqueued)
|
|
except Exception:
|
|
# One stale/corrupt row must not starve a later newly completed
|
|
# session. Historical cold caches remain operator-triggered.
|
|
continue
|
|
enqueued.append(session_id)
|
|
return tuple(enqueued)
|
|
|
|
|
|
def newly_finalized_recording_ids(
|
|
known_finalized: set[str] | None,
|
|
current_finalized: Iterable[str],
|
|
) -> tuple[str, ...]:
|
|
"""Return only post-startup completions; the first scan is a baseline."""
|
|
|
|
if known_finalized is None:
|
|
return ()
|
|
return tuple(sorted(set(current_finalized) - known_finalized))
|
|
|
|
|
|
def observation_archive_revision(roots: Iterable[Path]) -> tuple[tuple[object, ...], ...]:
|
|
"""Return a cheap change fence for direct-child observation archives.
|
|
|
|
The K1 writer creates and retires ``.current_session`` in the archive root,
|
|
and every session directory is a direct child of that same root. Those
|
|
operations advance the directory metadata, while growing capture/media
|
|
files do not. This lets the reconciler notice session start, completion and
|
|
removal without recursively reopening tens of thousands of immutable
|
|
evidence files every two seconds.
|
|
"""
|
|
|
|
revisions: list[tuple[object, ...]] = []
|
|
for configured_root in roots:
|
|
root = configured_root.expanduser().resolve(strict=False)
|
|
try:
|
|
metadata = root.lstat()
|
|
except OSError:
|
|
revisions.append((str(root), None))
|
|
continue
|
|
revisions.append(
|
|
(
|
|
str(root),
|
|
metadata.st_dev,
|
|
metadata.st_ino,
|
|
metadata.st_mtime_ns,
|
|
metadata.st_ctime_ns,
|
|
)
|
|
)
|
|
revisions.sort(key=lambda item: str(item[0]))
|
|
return tuple(revisions)
|
|
|
|
|
|
async def _recording_preparation_reconciler() -> None:
|
|
"""Prepare sessions finalized during this process, never historical rows."""
|
|
|
|
known_finalized: set[str] | None = None
|
|
reconciled_archive_revision: tuple[tuple[object, ...], ...] | None = None
|
|
while True:
|
|
try:
|
|
archive_revision = await asyncio.to_thread(
|
|
observation_archive_revision,
|
|
(archive.root for archive in plugin_environment.observation_archives),
|
|
)
|
|
if archive_revision != reconciled_archive_revision:
|
|
await asyncio.to_thread(refresh_observation_catalog)
|
|
finalized = set(await asyncio.to_thread(finalized_replayable_recording_ids))
|
|
newly_finalized = newly_finalized_recording_ids(
|
|
known_finalized,
|
|
finalized,
|
|
)
|
|
if newly_finalized:
|
|
await asyncio.to_thread(
|
|
enqueue_replayable_recordings,
|
|
newly_finalized,
|
|
)
|
|
known_finalized = finalized
|
|
# Keep the pre-scan revision. If a writer changed the archive
|
|
# during the expensive discovery, the next two-second check
|
|
# observes the newer revision and performs one follow-up pass.
|
|
reconciled_archive_revision = archive_revision
|
|
recording_reconciler_readiness.record_success()
|
|
except Exception as exc:
|
|
# A transient filesystem/catalog failure must not permanently
|
|
# disable preparation of sessions completed later in the run.
|
|
recording_reconciler_readiness.record_failure(exc)
|
|
await asyncio.sleep(2.0)
|
|
|
|
|
|
async def _portable_result_publication_reconciler() -> None:
|
|
service = OBSERVATORY_PUBLICATION_RECONCILER
|
|
if service is None:
|
|
return
|
|
while True:
|
|
# Durable state remains pending/failed and is retried on the next
|
|
# bounded pass or through the explicit operator action.
|
|
with suppress(OSError, ValueError):
|
|
await asyncio.to_thread(service.run_once)
|
|
with suppress(OSError, ValueError):
|
|
await asyncio.to_thread(service.artifact_transport.collect_source_copies)
|
|
await asyncio.sleep(15.0)
|
|
|
|
|
|
async def _recorded_blueprint_resource_reaper() -> None:
|
|
from k1link.viewer.recorded import recorded_blueprint_sessions
|
|
|
|
while True:
|
|
await asyncio.sleep(30.0)
|
|
await asyncio.to_thread(recorded_blueprint_sessions.expire)
|
|
|
|
|
|
async def _local_copy_reaper() -> None:
|
|
while True:
|
|
try:
|
|
await asyncio.to_thread(local_presentation_copies.prune)
|
|
except (OSError, ValueError):
|
|
logging.getLogger(__name__).exception(
|
|
"Local presentation retention failed; files preserved"
|
|
)
|
|
await asyncio.sleep(3600)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def app_lifespan(application: FastAPI) -> AsyncIterator[None]:
|
|
import logging
|
|
import sqlite3
|
|
|
|
from k1link.fleet.registry import FleetRegistry
|
|
|
|
fleet = None
|
|
application.state.fleet = None
|
|
try:
|
|
fleet = FleetRegistry(session_store.data_dir / "fleet")
|
|
fleet.start()
|
|
start_vps_hosts(fleet)
|
|
application.state.fleet = fleet
|
|
except (OSError, ValueError, sqlite3.Error):
|
|
# Fail closed for pairing without taking down unrelated operator work.
|
|
if fleet is not None:
|
|
fleet.close()
|
|
fleet = None
|
|
logging.getLogger(__name__).error("Fleet trust storage unavailable; pairing disabled")
|
|
reconciler: asyncio.Task[None] | None = None
|
|
publication_reconciler: asyncio.Task[None] | None = None
|
|
blueprint_reaper: asyncio.Task[None] | None = None
|
|
local_copy_reaper: asyncio.Task[None] | None = None
|
|
try:
|
|
configure_scanner_diagnostics(session_store.data_dir / "logs")
|
|
session_recording_preparation_manager.start()
|
|
simulation_project_service.recover_pending()
|
|
# Recovery is intentionally a one-shot startup phase. The archive
|
|
# helper owns a cross-process lease, while ordinary catalog requests
|
|
# only perform discovery and therefore never touch a live writer.
|
|
for archive in plugin_environment.observation_archives:
|
|
await asyncio.to_thread(archive.recover, archive.root)
|
|
# Full evidence discovery, hashing and RRD queue reconciliation can be
|
|
# expensive on field captures. Start it immediately in the background
|
|
# instead of holding the ASGI startup gate.
|
|
reconciler = asyncio.create_task(_recording_preparation_reconciler())
|
|
publication_reconciler = asyncio.create_task(_portable_result_publication_reconciler())
|
|
blueprint_reaper = asyncio.create_task(_recorded_blueprint_resource_reaper())
|
|
local_copy_reaper = asyncio.create_task(_local_copy_reaper())
|
|
yield
|
|
finally:
|
|
from k1link.viewer.recorded import recorded_blueprint_sessions
|
|
|
|
application.state.fleet = None
|
|
if fleet is not None:
|
|
await asyncio.to_thread(fleet.close)
|
|
if local_copy_reaper is not None:
|
|
local_copy_reaper.cancel()
|
|
with suppress(asyncio.CancelledError):
|
|
await local_copy_reaper
|
|
if blueprint_reaper is not None:
|
|
blueprint_reaper.cancel()
|
|
with suppress(asyncio.CancelledError):
|
|
await blueprint_reaper
|
|
await asyncio.to_thread(recorded_blueprint_sessions.close)
|
|
await map_gateway_proxy.close()
|
|
if reconciler is not None:
|
|
reconciler.cancel()
|
|
with suppress(asyncio.CancelledError):
|
|
await reconciler
|
|
if publication_reconciler is not None:
|
|
publication_reconciler.cancel()
|
|
with suppress(asyncio.CancelledError):
|
|
await publication_reconciler
|
|
await asyncio.to_thread(session_recording_preparation_manager.close)
|
|
await asyncio.to_thread(session_overview_service.close)
|
|
await asyncio.to_thread(planning_live_tests.close)
|
|
await asyncio.to_thread(mission_registration_runs.close)
|
|
plugin_environment.close()
|
|
|
|
|
|
app = FastAPI(
|
|
title="NODEDC MISSION CORE API",
|
|
version=__version__,
|
|
docs_url="/api/docs",
|
|
redoc_url=None,
|
|
openapi_url="/api/openapi.json",
|
|
lifespan=app_lifespan,
|
|
)
|
|
app.add_middleware(ResponseCompressionMiddleware, minimum_size=1_024, compresslevel=5)
|
|
|
|
app.add_middleware(RuntimeHttpTimingMiddleware)
|
|
|
|
|
|
app.include_router(fleet_router)
|
|
app.include_router(equipment_router)
|
|
app.include_router(equipment_firmware_router)
|
|
app.include_router(vps_host_router)
|
|
app.state.system_connections_root = REPOSITORY_ROOT / ".runtime" / "system"
|
|
app.include_router(server_catalog_router)
|
|
app.include_router(installer_router)
|
|
app.include_router(rover_router)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def request_validation_error_handler(
|
|
_: Request,
|
|
__: RequestValidationError,
|
|
) -> JSONResponse:
|
|
"""Return validation failures without reflecting request values or credentials."""
|
|
|
|
return JSONResponse(status_code=422, content={"detail": INVALID_REQUEST_DETAIL})
|
|
|
|
|
|
@app.get("/api/liveness")
|
|
async def liveness() -> dict[str, object]:
|
|
"""Prove only that the canonical event loop is responsive."""
|
|
|
|
return {
|
|
"ok": True,
|
|
"status": "alive",
|
|
"service": "mission-core-control-plane",
|
|
}
|
|
|
|
|
|
@app.get("/api/health")
|
|
def health() -> dict[str, Any]:
|
|
return build_runtime_readiness(
|
|
version=__version__,
|
|
plugin_runtime_health=plugin_environment.runtime_health,
|
|
recording_materializer=session_recording_materializer,
|
|
artifact_gateway=session_artifact_gateway,
|
|
map_gateway_configured=map_gateway_proxy.configured,
|
|
ffmpeg_available=_ffmpeg is not None,
|
|
ffprobe_available=_ffprobe is not None,
|
|
reconciler=recording_reconciler_readiness,
|
|
)
|
|
|
|
|
|
@app.get("/api/v1/device-plugins")
|
|
def get_device_plugins() -> dict[str, Any]:
|
|
try:
|
|
return {"items": plugin_catalog.plugin_documents()}
|
|
except PluginCatalogError as exc:
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
|
|
|
|
@app.get("/api/v1/device-models")
|
|
def get_device_models() -> dict[str, Any]:
|
|
try:
|
|
return {"items": plugin_catalog.model_documents()}
|
|
except PluginCatalogError as exc:
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
|
|
|
|
@app.get("/api/v1/device-plugin-runtimes")
|
|
def get_device_plugin_runtimes() -> dict[str, Any]:
|
|
return {"items": list(plugin_environment.runtime_health)}
|
|
|
|
|
|
@app.post("/api/v1/device-plugins/{plugin_id}/actions/{action_id}")
|
|
async def invoke_device_plugin_action(
|
|
plugin_id: str,
|
|
action_id: str,
|
|
request: DevicePluginActionRequest,
|
|
) -> dict[str, Any]:
|
|
try:
|
|
state = await plugin_dispatcher.invoke(plugin_id, action_id, request.input)
|
|
return {"state": state}
|
|
except (PluginNotFoundError, PluginActionNotFoundError) as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
except ValidationError as exc:
|
|
raise HTTPException(status_code=422, detail=INVALID_REQUEST_DETAIL) from exc
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
except PluginExecutionError as exc:
|
|
raise HTTPException(status_code=exc.http_status_code, detail=str(exc)) from exc
|
|
except PluginRuntimeUnavailableError as exc:
|
|
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
|
|
|
|
|
_CLOSED_WEBSOCKET_SEND_ERRORS = frozenset(
|
|
{
|
|
"handler is closed",
|
|
'Cannot call "send" once a close message has been sent.',
|
|
(
|
|
"Unexpected ASGI message 'websocket.send', after sending "
|
|
"'websocket.close' or response already completed."
|
|
),
|
|
"Unexpected ASGI message 'websocket.send', after sending 'websocket.close'.",
|
|
}
|
|
)
|
|
|
|
# This stream carries the comparatively heavy control-state snapshot, not
|
|
# camera or point-cloud media. Action requests return their own result, and the
|
|
# frontend also keeps a four-second REST fallback, so a two-second passive
|
|
# cadence preserves status recovery without starving the live media sockets.
|
|
DEVICE_PLUGIN_EVENT_POLL_INTERVAL_SECONDS = 2.0
|
|
|
|
|
|
def _is_closed_websocket_send_error(exc: RuntimeError) -> bool:
|
|
message = " ".join(str(exc).split())
|
|
if message in _CLOSED_WEBSOCKET_SEND_ERRORS:
|
|
return True
|
|
# uvloop raises this after the browser has already torn down the TCP
|
|
# transport, before Starlette can translate the failed send into a
|
|
# WebSocketDisconnect. Match both stable parts so an unrelated RuntimeError
|
|
# containing only "closed" is never swallowed.
|
|
return message.startswith("unable to perform operation on ") and message.endswith(
|
|
"; the handler is closed"
|
|
)
|
|
|
|
|
|
@app.websocket("/api/v1/device-plugins/{plugin_id}/events")
|
|
async def device_plugin_events(websocket: WebSocket, plugin_id: str) -> None:
|
|
await websocket.accept()
|
|
sequence = 0
|
|
try:
|
|
while True:
|
|
state = await plugin_dispatcher.invoke(plugin_id, STATE_READ_ACTION_ID, {})
|
|
sequence += 1
|
|
try:
|
|
await websocket.send_json(
|
|
{"pluginId": plugin_id, "sequence": sequence, "state": state}
|
|
)
|
|
except WebSocketDisconnect:
|
|
return
|
|
except RuntimeError as exc:
|
|
if _is_closed_websocket_send_error(exc):
|
|
return
|
|
raise
|
|
await asyncio.sleep(DEVICE_PLUGIN_EVENT_POLL_INTERVAL_SECONDS)
|
|
except (PluginNotFoundError, PluginActionNotFoundError):
|
|
await websocket.close(code=1008, reason="Device plugin is not available")
|
|
except (PluginExecutionError, PluginRuntimeUnavailableError):
|
|
await websocket.close(code=1011, reason="Device plugin state stream failed")
|
|
|
|
|
|
_replay_cache_binding = os.environ.get("MISSIONCORE_LAB_VIEW_CACHE_DIR", "").strip()
|
|
local_presentation_copies = LocalPresentations(
|
|
session_store.data_dir,
|
|
cache_data_dir=(
|
|
Path(_replay_cache_binding).expanduser().absolute() if _replay_cache_binding else None
|
|
),
|
|
)
|
|
lab_deletion_service = LabDeletionCoordinator(
|
|
store=session_store, queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
compositions=OBSERVATORY_AI_COMPOSITION_RUNS, copies=local_presentation_copies,
|
|
gateway=laboratory_worker_gateway.configuration,
|
|
)
|
|
app.include_router(build_local_storage_router(local_presentation_copies))
|
|
|
|
if session_artifact_gateway is not None and _ffmpeg is not None:
|
|
app.include_router(
|
|
build_portable_replay_router(
|
|
PortableReplayService(
|
|
view=PortableResultViewService(
|
|
sessions=session_store, artifacts=session_artifact_gateway.store
|
|
),
|
|
data_dir=session_store.data_dir,
|
|
media=session_recorded_media_inspector,
|
|
recording_source=_canonical_lab_recording_source,
|
|
ffmpeg_path=_ffmpeg,
|
|
local_copies=local_presentation_copies,
|
|
remote_replays=WorkerReplayRestore(laboratory_worker_gateway.configuration)
|
|
if laboratory_worker_gateway.configuration is not None else None,
|
|
),
|
|
composition_runs=OBSERVATORY_AI_COMPOSITION_RUNS,
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
)
|
|
)
|
|
|
|
for legacy_router in plugin_environment.legacy_routers:
|
|
app.include_router(legacy_router)
|
|
|
|
app.include_router(build_session_overview_router(session_overview_service))
|
|
app.include_router(build_mission_planner_router(mission_drafts))
|
|
app.include_router(build_planning_live_router(planning_live_tests))
|
|
app.include_router(build_mission_registration_router(mission_registration_runs, planning_live_tests))
|
|
|
|
app.include_router(
|
|
build_session_router(
|
|
operator_session_store,
|
|
# Production discovery belongs to the startup/background reconciler.
|
|
# HTTP list/replay paths must never rescan evidence roots inline.
|
|
catalog_refresher=None,
|
|
recording_materializer=session_recording_materializer,
|
|
recording_preparation_manager=session_recording_preparation_manager,
|
|
media_inspector=session_recorded_media_inspector,
|
|
laboratory_gateway=laboratory_worker_gateway,
|
|
point_color_renderers=plugin_environment.point_color_renderers,
|
|
point_display_renderers=plugin_environment.point_display_renderers,
|
|
lab_calculation_profile_resolver=(
|
|
None
|
|
if (
|
|
OBSERVATORY_LABORATORY_SETUP_REGISTRY is None
|
|
and OBSERVATORY_PORTABLE_DEFINITION_REGISTRY is None
|
|
)
|
|
else _resolve_observatory_calculation_profile
|
|
),
|
|
)
|
|
)
|
|
app.include_router(
|
|
build_observatory_router(
|
|
session_store,
|
|
deletion_service=lab_deletion_service,
|
|
setup_registry=OBSERVATORY_LABORATORY_SETUP_REGISTRY,
|
|
setup_registry_error=OBSERVATORY_LABORATORY_SETUP_REGISTRY_ERROR,
|
|
run_preparation_ledger=OBSERVATORY_RUN_PREPARATION_LEDGER,
|
|
run_preparation_ledger_error=OBSERVATORY_RUN_PREPARATION_LEDGER_ERROR,
|
|
recorded_binding_service=OBSERVATORY_RECORDED_BINDING_SERVICE,
|
|
recorded_job_queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
recorded_job_queue_error=OBSERVATORY_RECORDED_JOB_QUEUE_ERROR,
|
|
portable_setup_projector=OBSERVATORY_PORTABLE_SETUP_PROJECTOR,
|
|
portable_setup_projector_error=OBSERVATORY_PORTABLE_SETUP_PROJECTOR_ERROR,
|
|
portable_binding_service=OBSERVATORY_PORTABLE_BINDING_SERVICE,
|
|
portable_result_view=(
|
|
None
|
|
if session_artifact_gateway is None
|
|
else PortableResultViewService(
|
|
sessions=session_store,
|
|
artifacts=session_artifact_gateway.store,
|
|
)
|
|
),
|
|
portable_artifact_transport=(
|
|
None
|
|
if OBSERVATORY_PORTABLE_WORKER_INTEGRATION is None
|
|
else OBSERVATORY_PORTABLE_WORKER_INTEGRATION.artifact_transport
|
|
),
|
|
portable_result_publisher=(
|
|
None
|
|
if OBSERVATORY_PORTABLE_WORKER_INTEGRATION is None
|
|
else OBSERVATORY_PORTABLE_WORKER_INTEGRATION.result_publisher
|
|
),
|
|
)
|
|
)
|
|
if (
|
|
OBSERVATORY_AI_COMPOSITIONS is not None
|
|
and OBSERVATORY_AI_COMPOSITION_RUNS is not None
|
|
and OBSERVATORY_DOMAIN_ONTOLOGY is not None
|
|
):
|
|
app.include_router(
|
|
build_modular_observatory_router(
|
|
store=session_store,
|
|
compositions=OBSERVATORY_AI_COMPOSITIONS,
|
|
composition_runs=OBSERVATORY_AI_COMPOSITION_RUNS,
|
|
ontology=OBSERVATORY_DOMAIN_ONTOLOGY,
|
|
definitions=OBSERVATORY_PORTABLE_DEFINITION_REGISTRY,
|
|
binding=OBSERVATORY_PORTABLE_BINDING_SERVICE,
|
|
queue=OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
view_profiles=OBSERVATORY_LAB_VIEW_PROFILES,
|
|
deletion_service=lab_deletion_service,
|
|
)
|
|
)
|
|
if OBSERVATORY_WORKER_DISPATCH_READY:
|
|
assert OBSERVATORY_RECORDED_JOB_QUEUE is not None
|
|
assert OBSERVATORY_WORKER_AUTHENTICATION is not None
|
|
assert OBSERVATORY_PORTABLE_WORKER_INTEGRATION is not None
|
|
app.include_router(
|
|
build_observatory_worker_router(
|
|
OBSERVATORY_RECORDED_JOB_QUEUE,
|
|
authentication=OBSERVATORY_WORKER_AUTHENTICATION,
|
|
artifact_transport=(OBSERVATORY_PORTABLE_WORKER_INTEGRATION.artifact_transport),
|
|
result_publisher=(OBSERVATORY_PORTABLE_WORKER_INTEGRATION.result_publisher),
|
|
)
|
|
)
|
|
app.include_router(
|
|
build_environment_router(root_provider=lambda: session_store.data_dir / "ui-environment")
|
|
)
|
|
app.include_router(build_scene_profiles_router(lambda: session_store.data_dir / "scene-profiles"))
|
|
app.include_router(build_map_router(map_gateway_proxy))
|
|
app.include_router(
|
|
build_map_view_router(
|
|
root_provider=lambda: session_store.data_dir / "map-view",
|
|
)
|
|
)
|
|
app.include_router(
|
|
build_polygon_router(
|
|
root_provider=lambda: (
|
|
configured_polygon_runs_root() or REPOSITORY_ROOT / ".runtime" / "polygon-runs"
|
|
)
|
|
)
|
|
)
|
|
app.include_router(build_laboratory_worker_gateway_router(laboratory_worker_gateway))
|
|
app.include_router(
|
|
build_laboratory_worker_gateway_router(laboratory_worker_gateway, namespace="lidar")
|
|
)
|
|
app.include_router(
|
|
build_system_telemetry_router(
|
|
root_provider=lambda: REPOSITORY_ROOT / ".runtime" / "system",
|
|
)
|
|
)
|
|
app.include_router(
|
|
build_artifact_health_router(
|
|
runtime_root_provider=lambda: REPOSITORY_ROOT / ".runtime",
|
|
e44_results_root_provider=lambda: (
|
|
REPOSITORY_ROOT / ".runtime" / "compute-experiments" / "e44" / "results"
|
|
),
|
|
e50_results_root_provider=lambda: (
|
|
REPOSITORY_ROOT / ".runtime" / "compute-experiments" / "e50" / "results"
|
|
),
|
|
)
|
|
)
|
|
app.include_router(
|
|
build_compute_contour_router(
|
|
root_provider=lambda: REPOSITORY_ROOT / ".runtime" / "system",
|
|
)
|
|
)
|
|
app.include_router(build_simulation_world_provider_router())
|
|
app.include_router(build_compute_connection_router(
|
|
root_provider=lambda: REPOSITORY_ROOT / ".runtime" / "system",
|
|
))
|
|
app.include_router(
|
|
build_simulation_projects_router(
|
|
store=simulation_project_store,
|
|
service=simulation_project_service,
|
|
)
|
|
)
|
|
frontend_dist = REPOSITORY_ROOT / "apps" / "control-station" / "dist"
|
|
try:
|
|
app.include_router(build_ai_polygon_router(session_store.data_dir, OBSERVATORY_RECORDED_JOB_QUEUE))
|
|
except (OSError, ValueError):
|
|
logging.getLogger(__name__).exception("AI polygon could not load its private runtime state")
|
|
app.include_router(
|
|
build_viewer_diagnostics_router(
|
|
expected_ui_build_id=lambda: frontend_build_id(frontend_dist),
|
|
)
|
|
)
|
|
|
|
|
|
if frontend_dist.is_dir():
|
|
app.mount(
|
|
"/",
|
|
ControlStationStaticFiles(directory=frontend_dist, html=True),
|
|
name="frontend",
|
|
)
|