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:
@@ -10,6 +10,66 @@ function deviceSessionScope(state: XgridsK1State): string | null {
|
||||
return typeof sessionId === "string" && sessionId.trim() ? sessionId : null;
|
||||
}
|
||||
|
||||
interface RuntimeSnapshotStamp {
|
||||
startedAtMonotonicNs: bigint | null;
|
||||
startedAtEpochMs: number | null;
|
||||
runtimeId: string;
|
||||
revision: number;
|
||||
}
|
||||
|
||||
function monotonicNanoseconds(value: string | null | undefined): bigint | null {
|
||||
if (typeof value !== "string" || !/^(0|[1-9][0-9]*)$/.test(value)) return null;
|
||||
try {
|
||||
return BigInt(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function runtimeSnapshotStamp(state: XgridsK1State): RuntimeSnapshotStamp | null {
|
||||
const startedAt = state.snapshot_runtime_started_at_utc;
|
||||
const startedAtMonotonicNs = monotonicNanoseconds(
|
||||
state.snapshot_runtime_started_monotonic_ns,
|
||||
);
|
||||
const runtimeId = state.snapshot_runtime_id;
|
||||
const revision = monotonicInteger(state.snapshot_revision);
|
||||
if (
|
||||
typeof runtimeId !== "string"
|
||||
|| !runtimeId.trim()
|
||||
|| revision === null
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const parsedEpochMs = typeof startedAt === "string" ? Date.parse(startedAt) : Number.NaN;
|
||||
const startedAtEpochMs = Number.isFinite(parsedEpochMs) ? parsedEpochMs : null;
|
||||
if (startedAtMonotonicNs === null && startedAtEpochMs === null) return null;
|
||||
return { startedAtMonotonicNs, startedAtEpochMs, runtimeId, revision };
|
||||
}
|
||||
|
||||
function stampedSnapshotIsAtLeastAsNew(
|
||||
current: RuntimeSnapshotStamp,
|
||||
incoming: RuntimeSnapshotStamp,
|
||||
): boolean {
|
||||
if (incoming.runtimeId === current.runtimeId) {
|
||||
return incoming.revision >= current.revision;
|
||||
}
|
||||
if (incoming.startedAtMonotonicNs !== null || current.startedAtMonotonicNs !== null) {
|
||||
if (incoming.startedAtMonotonicNs === null) return false;
|
||||
if (current.startedAtMonotonicNs === null) return true;
|
||||
return incoming.startedAtMonotonicNs > current.startedAtMonotonicNs;
|
||||
}
|
||||
if (
|
||||
incoming.startedAtEpochMs !== null
|
||||
&& current.startedAtEpochMs !== null
|
||||
&& incoming.startedAtEpochMs !== current.startedAtEpochMs
|
||||
) {
|
||||
return incoming.startedAtEpochMs > current.startedAtEpochMs;
|
||||
}
|
||||
// Legacy UTC-only process identities with equal timestamps cannot be
|
||||
// ordered safely. Keep the already accepted authority.
|
||||
return false;
|
||||
}
|
||||
|
||||
function cameraSnapshotIsAtLeastAsNew(
|
||||
current: XgridsCameraPreviewState,
|
||||
incoming: XgridsCameraPreviewState,
|
||||
@@ -43,6 +103,17 @@ export function selectMonotonicXgridsState(
|
||||
current: XgridsK1State | null,
|
||||
incoming: XgridsK1State,
|
||||
): XgridsK1State {
|
||||
if (!current) return incoming;
|
||||
const currentStamp = runtimeSnapshotStamp(current);
|
||||
const incomingStamp = runtimeSnapshotStamp(incoming);
|
||||
if (currentStamp || incomingStamp) {
|
||||
if (!currentStamp) return incoming;
|
||||
if (!incomingStamp) return current;
|
||||
return stampedSnapshotIsAtLeastAsNew(currentStamp, incomingStamp)
|
||||
? incoming
|
||||
: current;
|
||||
}
|
||||
|
||||
if (current && deviceSessionScope(current) !== deviceSessionScope(incoming)) {
|
||||
return incoming;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user