YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(health): treat clamshell mic as unavailable (#6712)
Ezra Ellette141752d2ab5a
diff --git a/crates/screenpipe-core/src/display_topology.rs b/crates/screenpipe-core/src/display_topology.rsindex 192e9e837..1b71359f0 100644--- a/crates/screenpipe-core/src/display_topology.rs+++ b/crates/screenpipe-core/src/display_topology.rs@@ -166,6 +166,86 @@ pub fn capture_ready_display_ids() -> Option<BTreeSet<u32>> {None}+/// Whether this Mac is currently running with its built-in display inactive+/// (clamshell mode).+///+/// CoreGraphics omits the built-in panel from both its active and online lists+/// on some MacBooks after the lid closes, so display enumeration cannot answer+/// this reliably. The IOPM root domain's `AppleClamshellState` is the system's+/// direct lid-state signal. `false` on read failure and non-macOS platforms:+/// unknown state must never suppress a real capture failure.+#[cfg(target_os = "macos")]+pub fn is_clamshell_mode() -> bool {+ use std::ffi::{c_char, c_void};++ type IoObject = u32;+ type MachPort = u32;++ #[link(name = "IOKit", kind = "framework")]+ extern "C" {+ fn IOServiceMatching(name: *const c_char) -> *mut c_void;+ fn IOServiceGetMatchingService(main_port: MachPort, matching: *mut c_void) -> IoObject;+ fn IORegistryEntryCreateCFProperty(+ entry: IoObject,+ key: *const c_void,+ allocator: *const c_void,+ options: u32,+ ) -> *const c_void;+ fn IOObjectRelease(object: IoObject) -> i32;+ }+ #[link(name = "CoreFoundation", kind = "framework")]+ extern "C" {+ fn CFStringCreateWithCString(+ allocator: *const c_void,+ value: *const c_char,+ encoding: u32,+ ) -> *const c_void;+ fn CFGetTypeID(value: *const c_void) -> usize;+ fn CFBooleanGetTypeID() -> usize;+ fn CFBooleanGetValue(value: *const c_void) -> u8;+ fn CFRelease(value: *const c_void);+ }++ const UTF8: u32 = 0x0800_0100;+ unsafe {+ let matching = IOServiceMatching(b"IOPMrootDomain\0".as_ptr().cast());+ if matching.is_null() {+ return false;+ }+ // kIOMainPortDefault is MACH_PORT_NULL (0). The matching dictionary is+ // consumed by IOServiceGetMatchingService.+ let root_domain = IOServiceGetMatchingService(0, matching);+ if root_domain == 0 {+ return false;+ }++ let key = CFStringCreateWithCString(+ std::ptr::null(),+ b"AppleClamshellState\0".as_ptr().cast(),+ UTF8,+ );+ if key.is_null() {+ let _ = IOObjectRelease(root_domain);+ return false;+ }+ let value = IORegistryEntryCreateCFProperty(root_domain, key, std::ptr::null(), 0);+ CFRelease(key);+ let _ = IOObjectRelease(root_domain);+ if value.is_null() {+ return false;+ }++ let is_closed = CFGetTypeID(value) == CFBooleanGetTypeID() && CFBooleanGetValue(value) != 0;+ CFRelease(value);+ is_closed+ }+}++#[cfg(not(target_os = "macos"))]+pub fn is_clamshell_mode() -> bool {+ false+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/screenpipe-engine/src/routes/health.rs b/crates/screenpipe-engine/src/routes/health.rsindex 6dba21ddb..bfea650cd 100644--- a/crates/screenpipe-engine/src/routes/health.rs+++ b/crates/screenpipe-engine/src/routes/health.rs@@ -448,6 +448,25 @@ fn meetings_only_audio_idle_state(}}+/// macOS continues to enumerate the built-in MacBook microphone after the lid+/// closes even though it can only deliver zero-filled buffers. For health+/// purposes that device is unavailable in clamshell mode. External microphones+/// remain real inputs and must still surface capture failures normally.+fn input_device_is_available(device_name: &str, clamshell_mode: bool) -> bool {+ if !device_name.contains("(input)") {+ return false;+ }+ if !clamshell_mode {+ return true;+ }++ let lower = device_name.to_ascii_lowercase();+ let built_in_laptop_mic = (lower.contains("macbook") && lower.contains("microphone"))+ || lower.contains("built-in microphone")+ || lower.contains("internal microphone");+ !built_in_laptop_mic+}+/// Classify the raw audio capture status from health signals. Pure so it can be/// unit-tested in isolation. `stream_timeout_recent` must mean a recent timeout/// that the same current device has not recovered from, NOT a cumulative count@@ -478,11 +497,12 @@ fn classify_audio_status("meeting_detector_unavailable"} else if audio_waiting_for_meeting {"waiting_for_meeting"- } else if audio_never_captured && !has_input_device {+ } else if !has_input_device {// Audio is on but there is no microphone to capture from — expected// idle, not a failure. Distinct from "not_started" so /health stays 200- // and the desktop stall notification (which keys off "not_started")- // does not false-fire on machines without a mic.+ // and the desktop stall notification does not false-fire on machines+ // without a mic. This also covers a previously-used built-in MacBook+ // mic becoming unavailable when the lid closes in clamshell mode."no_input_device"} else if audio_never_captured {"not_started"@@ -1493,9 +1513,10 @@ async fn health_check_inner(state: &Arc<AppState>) -> HealthCheckResponse {// 503 degraded, nor trip the desktop "mic not capturing" stall notification.// Input devices are tagged "(input)" in the device list (output-only devices// like "Remote Audio (output)" are not microphones).+ let clamshell_mode = screenpipe_core::display_topology::is_clamshell_mode();let has_input_device = audio_devices.iter()- .any(|device| device.to_string().contains("(input)"));+ .any(|device| input_device_is_available(&device.to_string(), clamshell_mode));// Detect "active_no_data": the device appears active (selected and in the// device list) but the zero-fill watchdog has fired *recently*, indicating@@ -3106,6 +3127,14 @@ mod tests {),"no_input_device");+ // previously captured + mic later unavailable (for example clamshell)+ // remains the same benign state instead of degrading to stale.+ assert_eq!(+ classify_audio_status(+ false, false, false, false, false, false, true, false, 1000, 2000, 60+ ),+ "no_input_device"+ );// never captured but a mic exists -> not_startedassert_eq!(classify_audio_status(@@ -3190,4 +3219,23 @@ mod tests {"a present-but-silent mic must still surface as degraded");}++ #[test]+ fn clamshell_ignores_only_the_builtin_laptop_microphone() {+ assert!(input_device_is_available(+ "MacBook Pro Microphone (input)",+ false+ ));+ assert!(!input_device_is_available(+ "MacBook Pro Microphone (input)",+ true+ ));+ assert!(!input_device_is_available(+ "Built-in Microphone (input)",+ true+ ));+ assert!(input_device_is_available("USB Microphone (input)", true));+ assert!(input_device_is_available("AirPods Pro (input)", true));+ assert!(!input_device_is_available("System Audio (output)", true));+ }}