import type { PrepareConnectionReconfigurationRequest, XgridsK1State } from "./api"; import { isProvenLocalReceiverInactive, isTerminalAcquisitionState } from "./lifecycle"; interface DeactivationController { pendingAction: string | null; getCurrentState: () => XgridsK1State | null; closeApplicationControlSession: () => Promise; stopLocalReceiver: () => Promise; prepareConnectionReconfiguration: (request: PrepareConnectionReconfigurationRequest) => Promise; } function hasUnfinishedPhysicalWork(state: XgridsK1State): boolean { const physical = state.application_control_session?.physical_command ?? state.physical_command; return physical?.status === "corrupt" || physical?.requires_reconciliation === true || physical?.resolved_active_recovery_required === true || ["scan_starting", "scanning", "scan_stopping"].includes(physical?.observed_session_state ?? "") || state.application_control_session?.outcome_unknown === true || ( state.source_mode !== "replay" && state.acquisition?.control_mode === "plugin-commanded" && !isTerminalAcquisitionState(state.acquisition.state) ); } /** Release local ownership for explicit model switching; never send START/STOP or Wi-Fi. */ export async function deactivateXgridsK1Runtime( controller: DeactivationController, ): Promise { if (controller.pendingAction !== null) return false; const initial = controller.getCurrentState(); if (!initial || hasUnfinishedPhysicalWork(initial)) return false; const control = initial.application_control_session; if (control?.control_socket_open) { // R16 intentionally retains the READY socket between scans. Close that // exact idle/pre-START checkpoint instead of treating it as an active scan. if (!["connection-ready", "workspace-ready", "project-ready", "completed"].includes(control.state)) { return false; } if (!(await controller.closeApplicationControlSession())) return false; } const stillCurrentAndIdle = (): XgridsK1State | null => { const current = controller.getCurrentState(); if ( !current || current.snapshot_runtime_id !== initial.snapshot_runtime_id || current.acquisition?.acquisition_id !== initial.acquisition?.acquisition_id || current.application_control_session?.control_socket_open || hasUnfinishedPhysicalWork(current) ) return null; return current; }; const current = stillCurrentAndIdle(); if (!current) return false; if (!isProvenLocalReceiverInactive(current)) { if (!(await controller.stopLocalReceiver())) return false; if (!isProvenLocalReceiverInactive(stillCurrentAndIdle())) return false; } const released = stillCurrentAndIdle(); if (!released) return false; if (released.desired_connection_mode !== "bridge" || !released.devices?.length) return true; // A remounted form cannot own the previous form's fresh BLE candidates. // Retire them through the existing safe Bridge handoff, then cancel its // pending search intent so the durable read-only target remains available. // Unlike a scenario reset, handoff rejects concurrent physical work on the // server and never retires unresolved command/network audit ownership. const reconfiguration = released.connection_reconfiguration; const lifecycle = released.connection_lifecycle; const modeRevision = released.desired_connection_mode_revision; if ( !reconfiguration || !lifecycle || typeof modeRevision !== "number" || !Number.isInteger(modeRevision) ) return false; if (!(await controller.prepareConnectionReconfiguration({ intent: "select-device", expected_reconfiguration_revision: reconfiguration.revision, expected_reconfiguration_intent_id: reconfiguration.intent_id, expected_desired_mode_revision: modeRevision, expected_active_binding_key: lifecycle.active_binding_key, }))) return false; const prepared = stillCurrentAndIdle(); const pending = prepared?.connection_reconfiguration; if ( !prepared || !isProvenLocalReceiverInactive(prepared) || pending?.intent !== "select-device" || !pending.intent_id || pending.revision !== reconfiguration.revision + 1 || !prepared.connection_lifecycle || prepared.desired_connection_mode_revision !== modeRevision ) return false; if (!(await controller.prepareConnectionReconfiguration({ intent: "cancel", expected_reconfiguration_revision: pending.revision, expected_reconfiguration_intent_id: pending.intent_id, expected_desired_mode_revision: modeRevision, expected_active_binding_key: prepared.connection_lifecycle.active_binding_key, }))) return false; const cleared = stillCurrentAndIdle(); return Boolean( cleared && isProvenLocalReceiverInactive(cleared) && cleared.devices?.length === 0 && cleared.connection_reconfiguration?.intent === null, ); }