#[cfg(coverage)]
#[tonic::async_trait]
impl Relay for Handler {
    type StreamKeyboardStream = ReceiverStream<Result<KeyboardReport, Status>>;
    type StreamMouseStream = ReceiverStream<Result<MouseReport, Status>>;
    type CaptureVideoStream = VideoStream;
    type CaptureAudioStream = AudioStream;
    type StreamMicrophoneStream = ReceiverStream<Result<Empty, Status>>;
    type StreamCameraStream = ReceiverStream<Result<Empty, Status>>;
    type StreamWebcamMediaStream = ReceiverStream<Result<Empty, Status>>;
    type RunOutputDelayProbeStream = ReceiverStream<Result<OutputDelayProbeReply, Status>>;

    /// Keeps `stream_keyboard` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn stream_keyboard(
        &self,
        req: Request<tonic::Streaming<KeyboardReport>>,
    ) -> Result<Response<Self::StreamKeyboardStream>, Status> {
        let (tx, rx) = tokio::sync::mpsc::channel(32);
        let kb = self.kb.clone();
        let report_delay = live_keyboard_report_delay();

        tokio::spawn(async move {
            let mut s = req.into_inner();
            while let Some(pkt) = s.next().await.transpose()? {
                let _ = runtime_support::write_hid_report(&kb, &hid_endpoint(0), &pkt.data).await;
                tx.send(Ok(pkt)).await.ok();
                if !report_delay.is_zero() {
                    #[cfg(not(coverage))]
                    tokio::time::sleep(report_delay).await;
                }
            }
            Ok::<(), Status>(())
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    /// Keeps `stream_mouse` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn stream_mouse(
        &self,
        req: Request<tonic::Streaming<MouseReport>>,
    ) -> Result<Response<Self::StreamMouseStream>, Status> {
        let (tx, rx) = tokio::sync::mpsc::channel(32);
        let ms = self.ms.clone();

        tokio::spawn(async move {
            let mut s = req.into_inner();
            while let Some(pkt) = s.next().await.transpose()? {
                let _ = runtime_support::write_hid_report(&ms, &hid_endpoint(1), &pkt.data).await;
                tx.send(Ok(pkt)).await.ok();
            }
            Ok::<(), Status>(())
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    /// Keeps `stream_microphone` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn stream_microphone(
        &self,
        req: Request<tonic::Streaming<AudioPacket>>,
    ) -> Result<Response<Self::StreamMicrophoneStream>, Status> {
        let lease = self.upstream_media_rt.activate_microphone();
        let Some(microphone_sink_permit) = self
            .upstream_media_rt
            .reserve_microphone_sink(lease.generation)
            .await
        else {
            return Err(Status::aborted(
                "microphone stream superseded before sink became available",
            ));
        };
        let uac_dev = std::env::var("LESAVKA_UAC_DEV").unwrap_or_else(|_| "hw:UAC2Gadget,0".into());
        let mut sink = runtime_support::open_voice_with_retry(&uac_dev)
            .await
            .map_err(|e| {
                self.upstream_media_rt.close_microphone(lease.generation);
                Status::internal(format!("{e:#}"))
            })?;
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        let upstream_media_rt = self.upstream_media_rt.clone();

        tokio::spawn(async move {
            let _microphone_sink_permit = microphone_sink_permit;
            let mut inbound = req.into_inner();
            let mut pending = std::collections::VecDeque::new();
            let mut inbound_closed = false;
            let stale_drop_budget = upstream_stale_drop_budget();
            loop {
                if !upstream_media_rt.is_microphone_active(lease.generation) {
                    break;
                }
                if !inbound_closed {
                    let next_packet = tokio::select! {
                        packet = inbound.next() => Some(packet),
                        _ = tokio::time::sleep(Duration::from_millis(25)) => None,
                    };
                    if let Some(next_packet) = next_packet {
                        match next_packet.transpose()? {
                            Some(pkt) => {
                                pending.push_back(pkt);
                                let _ = retain_freshest_audio_packet(&mut pending);
                            }
                            None => inbound_closed = true,
                        }
                    }
                }
                let Some(mut pkt) = pending.pop_front() else {
                    if inbound_closed {
                        break;
                    }
                    continue;
                };
                let Some(plan) = coverage_audio_plan_from_decision(
                    upstream_media_rt.plan_audio_pts(pkt.pts),
                    &mut pending,
                    pkt.clone(),
                    inbound_closed,
                    stale_drop_budget,
                ) else {
                    continue;
                };
                tokio::time::sleep_until(plan.due_at).await;
                let actual_late_by = tokio::time::Instant::now()
                    .checked_duration_since(plan.due_at)
                    .unwrap_or_default();
                if actual_late_by > stale_drop_budget {
                    continue;
                }
                pkt.pts = plan.local_pts_us;
                sink.push(&pkt);
                upstream_media_rt.mark_audio_presented(pkt.pts, plan.due_at);
            }
            sink.finish();
            upstream_media_rt.close_microphone(lease.generation);
            let _ = tx.send(Ok(Empty {})).await;
            Ok::<(), Status>(())
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    /// Keeps `stream_webcam_media` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn stream_webcam_media(
        &self,
        req: Request<tonic::Streaming<UpstreamMediaBundle>>,
    ) -> Result<Response<Self::StreamWebcamMediaStream>, Status> {
        let bundled_leases = self.upstream_media_rt.activate_bundled_session();
        let microphone_lease = bundled_leases.microphone;
        let camera_lease = bundled_leases.camera;
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        let upstream_media_rt = self.upstream_media_rt.clone();

        tokio::spawn(async move {
            let mut inbound = req.into_inner();
            while let Some(bundle) = inbound.next().await.transpose()? {
                if let Some(video) = bundle.video {
                    upstream_media_rt.record_client_timing(
                        UpstreamMediaKind::Camera,
                        video_client_timing(&video),
                    );
                }
                for audio in bundle.audio {
                    upstream_media_rt.record_client_timing(
                        UpstreamMediaKind::Microphone,
                        audio_client_timing(&audio),
                    );
                }
            }
            upstream_media_rt.close_camera(camera_lease.generation);
            upstream_media_rt.close_microphone(microphone_lease.generation);
            let _ = tx.send(Ok(Empty {})).await;
            Ok::<(), Status>(())
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    /// Keeps `stream_camera` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn stream_camera(
        &self,
        req: Request<tonic::Streaming<VideoPacket>>,
    ) -> Result<Response<Self::StreamCameraStream>, Status> {
        let cfg = camera::current_camera_config();
        let upstream_lease = self.upstream_media_rt.activate_camera();
        let (session_id, relay, _relay_reused) = self.camera_rt.activate(&cfg).await?;
        let camera_rt = self.camera_rt.clone();
        let upstream_media_rt = self.upstream_media_rt.clone();
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        let frame_step_us = (1_000_000u64 / u64::from(cfg.fps.max(1))).max(1);

        tokio::spawn(async move {
            let mut s = req.into_inner();
            let mut pending = std::collections::VecDeque::new();
            let mut inbound_closed = false;
            let stale_drop_budget = upstream_stale_drop_budget();
            loop {
                if !camera_rt.is_active(session_id)
                    || !upstream_media_rt.is_camera_active(upstream_lease.generation)
                {
                    break;
                }
                if !inbound_closed {
                    let next_packet = tokio::select! {
                        packet = s.next() => Some(packet),
                        _ = tokio::time::sleep(Duration::from_millis(25)) => None,
                    };
                    if let Some(next_packet) = next_packet {
                        match next_packet.transpose()? {
                            Some(pkt) => {
                                pending.push_back(pkt);
                                let _ = retain_freshest_video_packet(&mut pending);
                            }
                            None => inbound_closed = true,
                        }
                    }
                }
                let Some(mut pkt) = pending.pop_front() else {
                    if inbound_closed {
                        break;
                    }
                    continue;
                };
                let Some(plan) = coverage_video_plan_from_decision(
                    upstream_media_rt.plan_video_pts(pkt.pts, frame_step_us),
                    &mut pending,
                    pkt.clone(),
                    inbound_closed,
                    stale_drop_budget,
                ) else {
                    continue;
                };
                if !coverage_audio_master_ready(
                    &upstream_media_rt,
                    upstream_media_rt
                        .wait_for_audio_master(plan.local_pts_us, plan.due_at)
                        .await,
                ) {
                    continue;
                }
                tokio::time::sleep_until(plan.due_at).await;
                pkt.pts = plan.local_pts_us;
                let presented_pts = pkt.pts;
                relay.feed(pkt);
                upstream_media_rt.mark_video_presented(presented_pts, plan.due_at);
            }
            upstream_media_rt.close_camera(upstream_lease.generation);
            tx.send(Ok(Empty {})).await.ok();
            Ok::<(), Status>(())
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    /// Keeps `run_output_delay_probe` explicit because it sits on relay RPC orchestration, where hardware failures must surface without stopping the server.
    /// Inputs are the typed parameters; output is the return value or side effect.
    async fn run_output_delay_probe(
        &self,
        req: Request<OutputDelayProbeRequest>,
    ) -> Result<Response<Self::RunOutputDelayProbeStream>, Status> {
        let cfg = camera::current_camera_config();
        let (_session_id, relay, _relay_reused) = self.camera_rt.activate(&cfg).await?;
        let mut sink = lesavka_server::audio::Voice::new("coverage-uac")
            .await
            .map_err(|e| Status::internal(format!("{e:#}")))?;
        let summary = lesavka_server::output_delay_probe::run_server_output_delay_probe(
            relay,
            &mut sink,
            &cfg,
            &req.into_inner(),
        )
        .await
        .map_err(|e| Status::internal(format!("{e:#}")))?;
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        let detail = format!(
            "server-generated UVC/UAC output-delay probe complete: video_frames={} audio_packets={} events={}",
            summary.video_frames, summary.audio_packets, summary.event_count
        );
        tx.send(Ok(OutputDelayProbeReply {
            ok: true,
            detail,
            server_timeline_json: summary.timeline_json,
        }))
        .await
        .ok();
        Ok(Response::new(ReceiverStream::new(rx)))
    }

    async fn capture_video(
        &self,
        req: Request<MonitorRequest>,
    ) -> Result<Response<Self::CaptureVideoStream>, Status> {
        self.capture_video_reply(req.into_inner()).await
    }

    async fn capture_audio(
        &self,
        _req: Request<MonitorRequest>,
    ) -> Result<Response<Self::CaptureAudioStream>, Status> {
        Err(Status::internal(
            "audio capture unavailable in coverage harness",
        ))
    }

    async fn paste_text(&self, req: Request<PasteRequest>) -> Result<Response<PasteReply>, Status> {
        self.paste_text_reply(req).await
    }

    async fn reset_usb(&self, _req: Request<Empty>) -> Result<Response<ResetUsbReply>, Status> {
        self.reset_usb_reply().await
    }

    async fn recover_usb(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<ResetUsbReply>, Status> {
        self.recover_usb_reply().await
    }

    async fn recover_uac(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<ResetUsbReply>, Status> {
        self.recover_uac_reply().await
    }

    async fn recover_uvc(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<ResetUsbReply>, Status> {
        self.recover_uvc_reply().await
    }

    async fn get_capture_power(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<CapturePowerState>, Status> {
        self.get_capture_power_reply().await
    }

    async fn set_capture_power(
        &self,
        req: Request<SetCapturePowerRequest>,
    ) -> Result<Response<CapturePowerState>, Status> {
        self.set_capture_power_reply(req).await
    }

    async fn get_calibration(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<CalibrationState>, Status> {
        self.get_calibration_reply().await
    }

    async fn calibrate(
        &self,
        req: Request<CalibrationRequest>,
    ) -> Result<Response<CalibrationState>, Status> {
        self.calibrate_reply(req).await
    }

    async fn get_upstream_sync(
        &self,
        _req: Request<Empty>,
    ) -> Result<Response<UpstreamSyncState>, Status> {
        self.get_upstream_sync_reply().await
    }
}
