{
            {
                let window = window.clone();
                let state = Rc::clone(&state);
                let child_proc = Rc::clone(&child_proc);
                let widgets = widgets.clone();
                let focus_signal_path = Rc::clone(&focus_signal_path);
                let input_state_path = Rc::clone(&input_state_path);
                let tests = Rc::clone(&tests);
                let server_entry = server_entry.clone();
                let server_addr_fallback = Rc::clone(&server_addr);
                let last_focus_marker =
                    Rc::new(RefCell::new(path_marker(focus_signal_path.as_path())));
                let power_request_in_flight = Rc::clone(&power_request_in_flight);
                let calibration_request_in_flight = Rc::clone(&calibration_request_in_flight);
                let upstream_sync_request_in_flight = Rc::clone(&upstream_sync_request_in_flight);
                let relay_request_in_flight = Rc::clone(&relay_request_in_flight);
                let preview = preview.clone();
                let power_tx = power_tx.clone();
                let calibration_tx = calibration_tx.clone();
                let upstream_sync_tx = upstream_sync_tx.clone();
                let caps_tx = caps_tx.clone();
                let caps_request_in_flight = Rc::clone(&caps_request_in_flight);
                let diagnostics_network = Rc::clone(&diagnostics_network);
                let diagnostics_process = Rc::clone(&diagnostics_process);
                let next_upstream_sync_probe = Rc::clone(&next_upstream_sync_probe);
                let next_diagnostics_probe = Rc::clone(&next_diagnostics_probe);
                let next_diagnostics_sample = Rc::clone(&next_diagnostics_sample);
                let preview_session_active = Rc::clone(&preview_session_active);
                let disconnect_cooldown_until = Rc::clone(&disconnect_cooldown_until);
                let log_tx = log_tx.clone();
                let camera_preview_path = uplink_camera_preview_path();
                let mic_level_path = uplink_mic_level_path();
                let uplink_telemetry_path = uplink_telemetry_path();
                glib::timeout_add_local(Duration::from_millis(180), move || {
                    let child_running = reap_exited_child(&child_proc);
                    if let Some(preview) = preview.as_ref() {
                        let desired_preview_active = {
                            let state_snapshot = state.borrow();
                            session_preview_active(&state_snapshot, child_running)
                        };
                        if preview_session_active.get() != desired_preview_active {
                            preview.set_session_active(desired_preview_active);
                            preview_session_active.set(desired_preview_active);
                        }
                    }
                    if !child_running && state.borrow().remote_active {
                        let power_mode = {
                            let mut state = state.borrow_mut();
                            let _ = state.stop_remote();
                            state.capture_power.mode.clone()
                        };
                        dock_all_displays_to_preview(&state, &child_proc, &popouts, &widgets);
                        window.present();
                        if let Some(preview) = preview.as_ref() {
                            preview.set_session_active(false);
                        }
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        if power_mode != "auto" {
                            widgets.status_label.set_text(
                                "Relay disconnected. Returning capture to automatic mode so it can fall back after the disconnect grace.",
                            );
                            request_capture_power_command(
                                power_tx.clone(),
                                server_addr.clone(),
                                CapturePowerCommand::Auto,
                            );
                        } else {
                            widgets
                                .status_label
                                .set_text(disconnected_capture_note(&power_mode));
                        }
                        request_capture_power_refresh(
                            power_tx.clone(),
                            server_addr.clone(),
                            Duration::from_millis(250),
                        );
                        request_calibration_refresh(
                            calibration_tx.clone(),
                            server_addr.clone(),
                            Duration::from_millis(350),
                        );
                        request_upstream_sync_refresh(
                            upstream_sync_tx.clone(),
                            server_addr.clone(),
                            Duration::from_millis(450),
                        );
                        request_capture_power_refresh(
                            power_tx.clone(),
                            server_addr,
                            Duration::from_secs(31),
                        );
                    }

                    if child_running
                        && let Some(routing) = read_input_routing_state(input_state_path.as_path())
                        && routing != state.borrow().routing
                    {
                        state.borrow_mut().set_routing(routing);
                        refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                        if matches!(routing, InputRouting::Remote) {
                            present_popout_windows(&popouts);
                        } else {
                            window.present();
                        }
                    }

                    let next_focus_marker = path_marker(focus_signal_path.as_path());
                    let mut last_focus = last_focus_marker.borrow_mut();
                    if next_focus_marker > *last_focus {
                        *last_focus = next_focus_marker;
                        state.borrow_mut().set_routing(InputRouting::Local);
                        refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                        widgets
                            .status_label
                            .set_text("Local control restored and the launcher is focused.");
                        window.present();
                    }

                    while let Ok(message) = relay_rx.try_recv() {
                        relay_request_in_flight.set(false);
                        match message {
                            RelayMessage::Spawned(Ok(mut child)) => {
                                attach_child_log_streams(&mut child, log_tx.clone());
                                *child_proc.borrow_mut() = Some(child);
                                disconnect_cooldown_until.set(Some(
                                    Instant::now() + UPSTREAM_EPOCH_HEAL_DISCONNECT_COOLDOWN,
                                ));
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    let _ = state.start_remote();
                                }
                                let server_addr =
                                    selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                                if let Some(preview) = preview.as_ref() {
                                    preview.set_server_addr(server_addr.clone());
                                    preview.set_session_active(session_preview_active(
                                        &state.borrow(),
                                        child_proc.borrow().is_some(),
                                    ));
                                }
                                let routing = routing_name(state.borrow().routing);
                                let power_mode = state.borrow().capture_power.mode.clone();
                                let message = match power_mode.as_str() {
                                    "forced-off" => format!(
                                        "Relay connected with inputs routed to {routing}, but capture is forced off. Return capture to Auto or Force On when you want remote video."
                                    ),
                                    "forced-on" => format!(
                                        "Relay connected with inputs routed to {routing}. Capture is being held awake and the eye previews are coming online."
                                    ),
                                    _ => format!(
                                        "Relay connected with inputs routed to {routing}. The eye previews will come up with the live session."
                                    ),
                                };
                                widgets.status_label.set_text(&message);
                                if matches!(state.borrow().routing, InputRouting::Remote) {
                                    present_popout_windows(&popouts);
                                }
                                request_capture_power_refresh(
                                    power_tx.clone(),
                                    server_addr.clone(),
                                    Duration::from_millis(250),
                                );
                                request_calibration_refresh(
                                    calibration_tx.clone(),
                                    server_addr.clone(),
                                    Duration::from_millis(300),
                                );
                                request_upstream_sync_refresh(
                                    upstream_sync_tx.clone(),
                                    server_addr.clone(),
                                    Duration::from_millis(400),
                                );
                                request_capture_power_refresh(
                                    power_tx.clone(),
                                    server_addr,
                                    Duration::from_millis(1250),
                                );
                            }
                            RelayMessage::Spawned(Err(err)) => {
                                state.borrow_mut().set_server_available(false);
                                if let Some(preview) = preview.as_ref() {
                                    preview.set_session_active(false);
                                }
                                widgets
                                    .status_label
                                    .set_text(&format!("Relay start failed: {err}"));
                            }
                        }
                    }

                    while let Ok(line) = log_rx.try_recv() {
                        let level = *widgets.session_log_level.borrow();
                        if append_session_log_for_level(&widgets.session_log_buffer, &line, level)
                        {
                            let mut end = widgets.session_log_buffer.end_iter();
                            widgets
                                .session_log_view
                                .scroll_to_iter(&mut end, 0.0, false, 0.0, 1.0);
                        }
                    }

                    while let Ok(message) = power_rx.try_recv() {
                        power_request_in_flight.set(false);
                        match message {
                            PowerMessage::Refresh(Ok(power)) => {
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    state.set_capture_power(power);
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                            }
                            PowerMessage::Refresh(Err(err)) => {
                                let relay_live = child_proc.borrow().is_some()
                                    || state.borrow().remote_active;
                                {
                                    let mut state = state.borrow_mut();
                                    if relay_live {
                                        state.set_server_available(true);
                                        if !state.capture_power.available {
                                            state.set_capture_power(unavailable_capture_power(err));
                                        }
                                    } else {
                                        state.set_server_available(false);
                                        state.set_capture_power(unavailable_capture_power(err));
                                    }
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                            }
                            PowerMessage::Command(Ok(power)) => {
                                let mode = power.mode.clone();
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    state.set_capture_power(power);
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                                widgets.status_label.set_text(match mode.as_str() {
                                    "forced-on" => "Capture feeds forced on. Remote eyes stay awake even if previews or the relay stop.",
                                    "forced-off" => "Capture feeds forced off. Remote eye previews and session video stay dark until you switch back.",
                                    _ => "Capture feeds returned to automatic mode. Live previews and relay demand will wake them as needed.",
                                });
                            }
                            PowerMessage::Command(Err(err)) => {
                                let mut state = state.borrow_mut();
                                state.set_server_available(false);
                                state.set_capture_power(unavailable_capture_power(err.clone()));
                                widgets
                                    .status_label
                                    .set_text(&format!("Capture power update failed: {err}"));
                            }
                        }
                    }

                    while let Ok(message) = calibration_rx.try_recv() {
                        calibration_request_in_flight.set(false);
                        let is_command = matches!(message, CalibrationMessage::Command(_));
                        match message {
                            CalibrationMessage::Refresh(Ok(calibration))
                            | CalibrationMessage::Command(Ok(calibration)) => {
                                let summary = calibration_summary(&calibration);
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    state.set_calibration(calibration);
                                }
                                if is_command {
                                    widgets.status_label.set_text(&summary);
                                }
                            }
                            CalibrationMessage::Refresh(Err(err)) => {
                                let relay_live = child_proc.borrow().is_some()
                                    || state.borrow().remote_active;
                                let mut state = state.borrow_mut();
                                if relay_live {
                                    state.set_server_available(true);
                                }
                                state.set_calibration(unavailable_calibration(err));
                            }
                            CalibrationMessage::Command(Err(err)) => {
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_calibration(unavailable_calibration(err.clone()));
                                }
                                widgets
                                    .status_label
                                    .set_text(&format!("Calibration update failed: {err}"));
                            }
                        }
                    }

                    while let Ok(message) = upstream_sync_rx.try_recv() {
                        upstream_sync_request_in_flight.set(false);
                        match message {
                            UpstreamSyncMessage::Refresh(Ok(upstream_sync)) => {
                                let mut state = state.borrow_mut();
                                state.set_server_available(true);
                                state.set_upstream_sync(upstream_sync);
                            }
                            UpstreamSyncMessage::Refresh(Err(err)) => {
                                let relay_live = child_proc.borrow().is_some()
                                    || state.borrow().remote_active;
                                let mut state = state.borrow_mut();
                                if relay_live {
                                    state.set_server_available(true);
                                }
                                state.set_upstream_sync(unavailable_upstream_sync(err));
                            }
                        }
                    }

                    while let Ok(message) = caps_rx.try_recv() {
                        caps_request_in_flight.set(false);
                        match message {
                            CapsMessage::Refresh(probe_result) => {
                                diagnostics_network.borrow_mut().record(&probe_result);
                                let caps = probe_result.caps;
                                let rebind_preview = eye_caps_changed(&state.borrow(), &caps);
                                {
                                    let mut state = state.borrow_mut();
                                    if probe_result.reachable {
                                        state.set_server_available(true);
                                    } else if child_proc.borrow().is_none() {
                                        state.set_server_available(false);
                                    }
                                    state.set_server_version(caps.server_version.clone());
                                    if probe_result.reachable {
                                        state.set_server_media_caps(
                                            Some(caps.camera),
                                            Some(caps.microphone),
                                            caps.camera_output.clone(),
                                            caps.camera_codec.clone(),
                                        );
                                    } else {
                                        state.set_server_media_caps(None, None, None, None);
                                    }
                                }
                                if let (Some(width), Some(height)) =
                                    (caps.eye_width, caps.eye_height)
                                {
                                    let fps = caps
                                        .eye_fps
                                        .unwrap_or(crate::launcher::state::PreviewSourceSize::default().fps);
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_preview_source_profile(width, height, fps);
                                }
                                    if rebind_preview && let Some(preview) = preview.as_ref() {
                                        sync_preview_profiles(preview, &widgets, &popouts, &state.borrow());
                                    }
                                    refresh_eye_feed_controls(&widgets, &state.borrow());
                                } else {
                                    refresh_eye_feed_controls(&widgets, &state.borrow());
                                }
                            }
                        }
                    }

                    while let Ok(message) = clipboard_rx.try_recv() {
                        match message {
                            ClipboardMessage::Finished(Ok(detail)) => {
                                widgets.status_label.set_text(&format!("✨ {detail}"));
                            }
                            ClipboardMessage::Finished(Err(err)) => {
                                widgets
                                    .status_label
                                    .set_text(&format!("Clipboard send failed: {err}"));
                            }
                        }
                    }

                    let now = Instant::now();
                    let child_running = child_proc.borrow().is_some();
                    let disconnect_cooldown =
                        disconnect_cooldown_remaining(now, disconnect_cooldown_until.get());
                    if child_running && disconnect_cooldown.is_none() {
                        disconnect_cooldown_until.set(None);
                    }

                    if now >= next_power_probe.get()
                        && !power_request_in_flight.get()
                        && (child_running
                            || state.borrow().capture_power.enabled
                            || state.borrow().remote_active)
                    {
                        power_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_capture_power_refresh(power_tx.clone(), server_addr, Duration::ZERO);
                        next_power_probe.set(now + Duration::from_secs(2));
                    }

                    if now >= next_calibration_probe.get()
                        && !calibration_request_in_flight.get()
                        && (child_running || state.borrow().server_available)
                    {
                        calibration_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_calibration_refresh(
                            calibration_tx.clone(),
                            server_addr,
                            Duration::ZERO,
                        );
                        next_calibration_probe.set(now + Duration::from_secs(2));
                    }

                    if now >= next_upstream_sync_probe.get()
                        && !upstream_sync_request_in_flight.get()
                        && (child_running || state.borrow().server_available)
                    {
                        upstream_sync_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_upstream_sync_refresh(
                            upstream_sync_tx.clone(),
                            server_addr,
                            Duration::ZERO,
                        );
                        next_upstream_sync_probe.set(now + Duration::from_secs(2));
                    }

                    if now >= next_diagnostics_probe.get() && !caps_request_in_flight.get() {
                        caps_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_handshake_caps(caps_tx.clone(), server_addr, Duration::ZERO);
                        next_diagnostics_probe.set(now + Duration::from_secs(2));
                    }

                    include!("runtime_poll/runtime_monitor_tick.rs");

                    refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                    if child_running
                        && let Some(remaining) = disconnect_cooldown
                    {
                        widgets.start_button.set_sensitive(false);
                        widgets
                            .start_button
                            .set_tooltip_text(Some(&disconnect_cooldown_status(remaining)));
                    }
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                    glib::ControlFlow::Continue
                });
            }
}
