Restore complete LAB recordings from verified Worker publications

This commit is contained in:
DCCONSTRUCTIONS
2026-11-10 03:23:20 +03:00
parent a724a9796a
commit 1317f78672
11 changed files with 635 additions and 12 deletions
+10 -1
View File
@@ -18,6 +18,7 @@ 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,
@@ -997,7 +998,13 @@ async def device_plugin_events(websocket: WebSocket, plugin_id: str) -> None:
await websocket.close(code=1011, reason="Device plugin state stream failed")
local_presentation_copies = LocalPresentations(session_store.data_dir)
_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,
@@ -1017,6 +1024,8 @@ if session_artifact_gateway is not None and _ffmpeg is not None:
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,
+3
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import logging
from typing import Annotated
import httpx
from fastapi import APIRouter, HTTPException, Path, Query, Response
from fastapi.responses import FileResponse
@@ -81,6 +82,7 @@ def build_portable_replay_router(
try:
artifact = service.prepare(result_id, base_sha)
except (
httpx.HTTPError,
ValueError,
OSError,
KeyError,
@@ -139,6 +141,7 @@ def build_portable_replay_router(
try:
artifact = service.prepare_composition(run_id, composition_members(run_id), base_sha)
except (
httpx.HTTPError,
ValueError,
OSError,
KeyError,
+65
View File
@@ -0,0 +1,65 @@
"""Authenticated Worker API for complete immutable LAB recordings."""
from typing import Annotated
from fastapi import APIRouter, HTTPException, Path, Query
from fastapi.responses import FileResponse
from k1link.laboratory.data_deletion import LOCK, LabDeletionError
from k1link.laboratory.replay_publication import REPLAY_ID, SHA, ReplayPublications
def build_ready_replay_router(store: ReplayPublications) -> APIRouter:
router = APIRouter(prefix="/api/v1/laboratory/portable-replays")
identity = "/{result_id}/{base_sha}"
def read(result_id, base_sha):
try:
return store.read(result_id, base_sha)
except FileNotFoundError as exc:
raise HTTPException(404, "Ready LAB recording is not published") from exc
except (ValueError, OSError, LabDeletionError) as exc:
raise HTTPException(409, "Ready LAB recording failed verification") from exc
@router.get(identity)
def manifest(
result_id: Annotated[str, Path(pattern=REPLAY_ID)],
base_sha: Annotated[str, Path(pattern=SHA)],
):
with LOCK:
doc, _ = read(result_id, base_sha)
return doc.model_dump()
class LeasedResponse(FileResponse):
async def __call__(self, scope, receive, send):
try:
await super().__call__(scope, receive, send)
finally:
with LOCK:
store.active[self.owner] -= 1
@router.get(identity + "/recording.rrd")
def recording(
result_id: Annotated[str, Path(pattern=REPLAY_ID)],
base_sha: Annotated[str, Path(pattern=SHA)],
generation: Annotated[str, Query(pattern=SHA)],
):
with LOCK:
doc, path = read(result_id, base_sha)
if doc.sha256 != generation:
raise HTTPException(412, "Ready LAB generation changed")
response = LeasedResponse(
path,
media_type="application/vnd.rerun.rrd",
headers={
"ETag": f'"{doc.sha256}"',
"X-Rerun-Format": "RRF2",
"Cache-Control": "private, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
)
response.owner = result_id
store.active[result_id] += 1
return response
return router