wip(k1): checkpoint connection recovery rewrite

Capture the current unreleased K1 connection, recovery, lifecycle, viewer, and test work as a single known-bad baseline for subsequent fixes.
This commit is contained in:
DCCONSTRUCTIONS
2026-08-14 14:57:50 +03:00
parent aff331082f
commit 0ca7316a24
157 changed files with 152962 additions and 4036 deletions
@@ -8,11 +8,19 @@ import {
type MissionRuntimeState,
} from "@mission-core/plugin-sdk";
import {
activeConnectionEndpointLabel,
canonicalDeviceConnectivity,
confirmedRuntimeSourceMode,
effectiveAcquisition,
hasAuthoritativeData,
hasControlAuthority,
normalizeRuntimePhase,
spatialSourceId,
} from "./lifecycle";
import {
activeStreamRecoveryOwnsPresentationDecision,
activeStreamRecoveryPresentationAuthority,
} from "./activeStreamRecovery";
import { localizeRuntimeMessage } from "./messages";
import { xgridsK1Manifest } from "./manifest";
import { deviceTelemetry, finiteMetric, pipelineLatency } from "./presentation";
@@ -23,13 +31,21 @@ export type XgridsK1Controller = ReturnType<typeof useXgridsK1Runtime>;
const XgridsK1RuntimeContext = createContext<XgridsK1Controller | null>(null);
function normalizeState(
controller: XgridsK1Controller,
export function normalizeXgridsK1MissionState(
controller: Pick<XgridsK1Controller, "state">,
activeModel: DeviceModelDefinition,
): MissionRuntimeState | null {
const state = controller.state;
if (!state) return null;
const metrics = state.metrics;
const controlAuthoritative = hasControlAuthority(state);
const recoveryPresentationAuthority = activeStreamRecoveryPresentationAuthority(state);
const recoveryOwnsPresentation = activeStreamRecoveryOwnsPresentationDecision(state);
const dataAuthoritative = hasAuthoritativeData(state) && !recoveryOwnsPresentation;
const recoveryPresentationAuthoritative = recoveryPresentationAuthority !== null;
const replayAuthoritative = state.source_mode === "replay";
const metrics = replayAuthoritative || dataAuthoritative
? state.metrics
: undefined;
const telemetry = deviceTelemetry(metrics);
const deviceRef = state.device_ref;
const deviceSession = state.device_session;
@@ -43,13 +59,13 @@ function normalizeState(
return {
phase: normalizeRuntimePhase(state),
message: localizeRuntimeMessage(state.message),
activeDevice: deviceRef
activeDevice: deviceRef && controlAuthoritative
? {
pluginId: xgridsK1Manifest.metadata.id,
modelId: deviceRef.model_id || activeModel.id,
displayName: activeModel.displayName,
instanceId: deviceRef.device_id,
endpointLabel: state.k1_ip,
endpointLabel: activeConnectionEndpointLabel(state),
}
: null,
deviceSession: deviceSession
@@ -57,7 +73,7 @@ function normalizeState(
sessionId: deviceSession.device_session_id,
deviceId: deviceSession.device_id,
compatibilityProfileId: deviceSession.compatibility_profile_id,
connectivity: deviceSession.connectivity,
connectivity: canonicalDeviceConnectivity(state),
}
: null,
acquisition: acquisition
@@ -80,7 +96,9 @@ function normalizeState(
stageCode: operation.stage_code,
messageCode: operation.message_code,
})),
spatialSource: sourceUrl && resolvedSpatialSourceId
spatialSource: sourceUrl
&& resolvedSpatialSourceId
&& (replayAuthoritative || dataAuthoritative || recoveryPresentationAuthoritative)
? {
id: resolvedSpatialSourceId,
url: sourceUrl,
@@ -102,7 +120,9 @@ function normalizeState(
range: null,
},
viewerSettings: state.viewer_settings,
sourceMode: confirmedRuntimeSourceMode(state),
sourceMode: recoveryPresentationAuthoritative
? "live"
: confirmedRuntimeSourceMode(state),
metrics: {
publishedFrameCount: (
Number.isSafeInteger(metrics?.pcl_frames) &&
@@ -140,10 +160,13 @@ export function XgridsK1RuntimeProvider({
const inheritedRuntime = useMissionRuntime();
const controller = useXgridsK1Runtime(active);
const missionRuntime: MissionRuntimeController = {
state: activeModel ? normalizeState(controller, activeModel) : null,
state: activeModel
? normalizeXgridsK1MissionState(controller, activeModel)
: null,
backendStatus: controller.backendStatus,
pendingAction: controller.pendingAction,
refresh: controller.refresh,
refresh: () => controller.refresh().then(() => undefined),
resetConnectionScenario: controller.resetConnectionScenario,
updateViewerSettings: controller.updateViewerSettings,
setObservationSourceActive: controller.setObservationSourceActive,
};