feat(observatory): ship modular AI inference labs

This commit is contained in:
DCCONSTRUCTIONS
2026-09-04 17:59:05 +03:00
parent eff60e490a
commit cada687173
145 changed files with 17651 additions and 1667 deletions
+96 -70
View File
@@ -2,17 +2,20 @@
from __future__ import annotations
from collections import OrderedDict
from collections.abc import Callable
from contextlib import suppress
from threading import Lock
from typing import Any, Literal
from uuid import UUID
from uuid import UUID, uuid5
import rerun as rr
import rerun_bindings as bindings
from rerun import blueprint as rrb
from k1link.viewer.recorded_blueprint_lifecycle import (
BlueprintSessionReleased,
RecordedBlueprintSessions,
)
from k1link.viewer.rerun_bridge import RerunSceneSettings
APPLICATION_ID = "nodedc_mission_core_recorded"
@@ -48,8 +51,9 @@ class _RecordedBlueprintStream:
"""Keep one bounded SDK blueprint source for each browser viewport.
Upstream 0.36.3 activates a clone, not this source store. Explicit refresh
makes layer changes visible but cannot retain the clone's operator eye.
It is opt-in for portable replay pending native camera-state support.
makes layer changes visible. Ordinary layer updates deliberately omit eye
components so the active clone retains the operator's camera. Only an
explicit 3D/plan/follow transition may write a new spatial eye preset.
"""
def __init__(
@@ -57,8 +61,10 @@ class _RecordedBlueprintStream:
application_id: str,
recording_id: str,
*,
blueprint_session_id: str,
view_reset_generation: Literal[0, 1],
) -> None:
self.blueprint_session_id = blueprint_session_id
self.view_reset_generation = view_reset_generation
self._lock = Lock()
self._sequence = 0
@@ -81,7 +87,7 @@ class _RecordedBlueprintStream:
def render(
self,
blueprint_factory: Callable[[bool, bool], rrb.Blueprint],
blueprint_factory: Callable[[bool, bool, str], rrb.Blueprint],
*,
follow_trajectory: bool,
plan_view: bool,
@@ -94,10 +100,20 @@ class _RecordedBlueprintStream:
update_eye_controls = self._eye_contract != eye_contract
# Initial admission/reset uses native framing. Explicit presets
# apply to mode transitions, after the viewer has a source cursor.
blueprint = blueprint_factory(update_eye_controls, self._eye_contract is not None)
# Keep one view identity for the lifetime of this browser owner.
# Replacing the UUID on every layer toggle rebuilt both native
# viewports, delayed a simple visibility change, and discarded the
# operator's active interaction state. The explicit reset
# generation already owns the one intentional identity change.
view_instance_token = self.blueprint_session_id
blueprint = blueprint_factory(
update_eye_controls,
self._eye_contract is not None,
view_instance_token,
)
# Appending rows alone does not refresh upstream's active clone.
# Keep legacy admission unchanged; portable replay opts into
# working layer updates with an explicitly documented eye reset.
# Reactivation makes layer changes visible; eye components remain
# absent unless the explicit view-mode contract changed above.
make_active = self._sequence == 0 or reactivate_updates
self._blueprint_recording.set_time(
"blueprint",
@@ -127,40 +143,7 @@ class _RecordedBlueprintStream:
self._transport_recording.disconnect()
_MAX_RECORDED_BLUEPRINT_STREAMS = 32
_recorded_blueprint_streams_lock = Lock()
_recorded_blueprint_streams: OrderedDict[tuple[str, str, str], _RecordedBlueprintStream] = (
OrderedDict()
)
def _stable_recorded_blueprint_stream(
*,
application_id: str,
recording_id: str,
blueprint_session_id: str,
view_reset_generation: Literal[0, 1],
) -> _RecordedBlueprintStream:
key = (application_id, recording_id, blueprint_session_id)
with _recorded_blueprint_streams_lock:
stream = _recorded_blueprint_streams.get(key)
if stream is not None and stream.view_reset_generation != view_reset_generation:
stream.close()
del _recorded_blueprint_streams[key]
stream = None
if stream is None:
stream = _RecordedBlueprintStream(
application_id,
recording_id,
view_reset_generation=view_reset_generation,
)
_recorded_blueprint_streams[key] = stream
else:
_recorded_blueprint_streams.move_to_end(key)
while len(_recorded_blueprint_streams) > _MAX_RECORDED_BLUEPRINT_STREAMS:
_, stale = _recorded_blueprint_streams.popitem(last=False)
stale.close()
return stream
recorded_blueprint_sessions = RecordedBlueprintSessions[_RecordedBlueprintStream]()
def recorded_blueprint(
@@ -170,15 +153,21 @@ def recorded_blueprint(
active_view: RecordedView = "spatial",
view_reset_generation: Literal[0, 1] = 0,
unified_perception: bool = False,
unified_camera_share: float = 0.46,
semantic_layer: Literal["city", "vegetation"] | None = None,
plan_view: bool = False,
show_detections_2d: bool = False,
show_camera_image: bool = True,
show_segmentation: bool = False,
show_cuboids_3d: bool = False,
show_costmap: bool = False,
follow_trajectory: bool = False,
update_eye_controls: bool = True,
explicit_spatial_preset: bool = False,
eye_position: tuple[float, float, float] | None = None,
eye_look_target: tuple[float, float, float] | None = None,
eye_up: tuple[float, float, float] | None = None,
view_instance_token: str | None = None,
) -> rrb.Blueprint:
accumulation = max(0.0, settings.accumulation_seconds)
time_ranges: list[rr.VisibleTimeRange] | None = None
@@ -218,6 +207,14 @@ def recorded_blueprint(
)
spatial_eye_controls = (
rrb.EyeControls3D.from_fields(
kind=rrb.Eye3DKind.Orbital,
position=eye_position,
look_target=eye_look_target,
eye_up=eye_up,
tracking_entity="/world/sensor_pose" if follow_trajectory else "",
)
if eye_position is not None and eye_look_target is not None and eye_up is not None
else rrb.EyeControls3D.from_fields(
kind=rrb.Eye3DKind.Orbital,
position=[0.0, 0.0, 30.0],
look_target=[0.0, 0.0, 0.0],
@@ -267,15 +264,21 @@ def recorded_blueprint(
# alone does not preserve edits in upstream's activated blueprint clone.
eye_controls=spatial_eye_controls,
)
spatial_view.id = (
RECORDED_SPATIAL_RESET_VIEW_ID if view_reset_generation else RECORDED_SPATIAL_VIEW_ID
def instance_id(primary: UUID, reset: UUID) -> UUID:
base = reset if view_reset_generation else primary
return uuid5(base, view_instance_token) if view_instance_token is not None else base
spatial_view.id = instance_id(
RECORDED_SPATIAL_VIEW_ID,
RECORDED_SPATIAL_RESET_VIEW_ID,
)
camera_view = rrb.Spatial2DView(
origin="/perception/camera",
name="Оригинальное видео · слои AI",
background=[7, 8, 10, 255],
overrides={
"/perception/camera/image": rrb.EntityBehavior(visible=True),
"/perception/camera/image": rrb.EntityBehavior(visible=show_camera_image),
"/perception/camera/detections": rrb.EntityBehavior(
visible=show_detections_2d,
),
@@ -290,8 +293,9 @@ def recorded_blueprint(
),
},
)
camera_view.id = (
RECORDED_CAMERA_RESET_VIEW_ID if view_reset_generation else RECORDED_CAMERA_VIEW_ID
camera_view.id = instance_id(
RECORDED_CAMERA_VIEW_ID,
RECORDED_CAMERA_RESET_VIEW_ID,
)
perception_3d_view = rrb.Spatial3DView(
# Cuboids are expressed in the same calibrated world frame as the
@@ -325,10 +329,9 @@ def recorded_blueprint(
},
eye_controls=spatial_eye_controls,
)
perception_3d_view.id = (
RECORDED_PERCEPTION_3D_RESET_VIEW_ID
if view_reset_generation
else RECORDED_PERCEPTION_3D_VIEW_ID
perception_3d_view.id = instance_id(
RECORDED_PERCEPTION_3D_VIEW_ID,
RECORDED_PERCEPTION_3D_RESET_VIEW_ID,
)
metrics_view = rrb.TimeSeriesView(
origin="/metrics/device",
@@ -357,16 +360,16 @@ def recorded_blueprint(
spatial_view.visualizer_overrides["/world/perception/boxes3d"] = [
rrb.EntityBehavior(visible=show_cuboids_3d),
]
camera_share = min(0.9, max(0.1, unified_camera_share))
root_container = rrb.Horizontal(
camera_view,
spatial_view,
column_shares=[0.46, 0.54],
column_shares=[camera_share, 1.0 - camera_share],
name="Единая сцена восприятия",
)
root_container.id = (
RECORDED_UNIFIED_RESET_ROOT_CONTAINER_ID
if view_reset_generation
else RECORDED_UNIFIED_ROOT_CONTAINER_ID
root_container.id = instance_id(
RECORDED_UNIFIED_ROOT_CONTAINER_ID,
RECORDED_UNIFIED_RESET_ROOT_CONTAINER_ID,
)
else:
# Keep operator video and 3D cuboids as direct root views. Rerun's nested
@@ -384,7 +387,7 @@ def recorded_blueprint(
# replace it from a later blueprint message. Give every operator mode (and
# its explicit reset generation) a stable root identity so the requested
# child is authoritative instead of inheriting a previously visited tab.
root_container.id = {
base_root_id = {
("spatial", 0): RECORDED_ROOT_CONTAINER_ID,
("spatial", 1): RECORDED_SPATIAL_RESET_ROOT_CONTAINER_ID,
("perception", 0): RECORDED_PERCEPTION_ROOT_CONTAINER_ID,
@@ -394,6 +397,11 @@ def recorded_blueprint(
("metrics", 0): RECORDED_METRICS_ROOT_CONTAINER_ID,
("metrics", 1): RECORDED_METRICS_RESET_ROOT_CONTAINER_ID,
}[(active_view, view_reset_generation)]
root_container.id = (
uuid5(base_root_id, view_instance_token)
if view_instance_token is not None
else base_root_id
)
if include_initial_playback_state:
return rrb.Blueprint(
@@ -424,19 +432,26 @@ def recorded_blueprint_rrd(
active_view: RecordedView = "spatial",
view_reset_generation: Literal[0, 1] = 0,
unified_perception: bool = False,
unified_camera_share: float = 0.46,
semantic_layer: Literal["city", "vegetation"] | None = None,
plan_view: bool = False,
show_detections_2d: bool = False,
show_camera_image: bool = True,
show_segmentation: bool = False,
show_cuboids_3d: bool = False,
show_costmap: bool = False,
follow_trajectory: bool = False,
reactivate_updates: bool = False,
eye_position: tuple[float, float, float] | None = None,
eye_look_target: tuple[float, float, float] | None = None,
eye_up: tuple[float, float, float] | None = None,
) -> bytes:
"""Serialize a bounded active blueprint update without recorded data."""
def build_blueprint(
update_eye_controls: bool, use_spatial_preset: bool = False
update_eye_controls: bool,
use_spatial_preset: bool = False,
view_instance_token: str | None = None,
) -> rrb.Blueprint:
return recorded_blueprint(
settings,
@@ -444,32 +459,43 @@ def recorded_blueprint_rrd(
active_view=active_view,
view_reset_generation=view_reset_generation,
unified_perception=unified_perception,
unified_camera_share=unified_camera_share,
semantic_layer=semantic_layer,
plan_view=plan_view,
show_detections_2d=show_detections_2d,
show_camera_image=show_camera_image,
show_segmentation=show_segmentation,
show_cuboids_3d=show_cuboids_3d,
show_costmap=show_costmap,
follow_trajectory=follow_trajectory,
update_eye_controls=update_eye_controls,
explicit_spatial_preset=reactivate_updates and use_spatial_preset,
explicit_spatial_preset=use_spatial_preset and update_eye_controls,
eye_position=eye_position,
eye_look_target=eye_look_target,
eye_up=eye_up,
view_instance_token=view_instance_token,
)
payload: bytes | None
if blueprint_session_id is not None:
try:
payload = _stable_recorded_blueprint_stream(
application_id=application_id,
recording_id=recording_id,
blueprint_session_id=blueprint_session_id,
view_reset_generation=view_reset_generation,
).render(
build_blueprint,
follow_trajectory=follow_trajectory,
plan_view=plan_view,
reactivate_updates=reactivate_updates,
payload = recorded_blueprint_sessions.use(
(application_id, recording_id, blueprint_session_id),
view_reset_generation,
lambda: _RecordedBlueprintStream(
application_id,
recording_id,
blueprint_session_id=blueprint_session_id,
view_reset_generation=view_reset_generation,
),
lambda stream: stream.render(
build_blueprint,
follow_trajectory=follow_trajectory,
plan_view=plan_view,
reactivate_updates=reactivate_updates,
),
)
except RecordedBlueprintError:
except (RecordedBlueprintError, BlueprintSessionReleased):
raise
except Exception as exc:
raise RecordedBlueprintError("failed to serialize stable recorded blueprint") from exc