Skip to content

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

feat(telemetry): surface lagged audio loss + fix cpu_count, cheaper process refresh (#4209)

Louis Beaumont2023c74abade
diff --git a/crates/screenpipe-audio/src/core/run_record_and_transcribe.rs b/crates/screenpipe-audio/src/core/run_record_and_transcribe.rs
index 386edafc2..b8edf24cc 100644
--- a/crates/screenpipe-audio/src/core/run_record_and_transcribe.rs
+++ b/crates/screenpipe-audio/src/core/run_record_and_transcribe.rs
@@ -386,6 +386,10 @@ async fn recv_audio_chunk(
Ok(Some(chunk))
}
Ok(Err(broadcast::error::RecvError::Lagged(n))) => {
+ // The recorder fell behind the capture broadcast and the OS frames
+ // in between are gone — record the count so this silent loss is
+ // visible in /health and analytics instead of only a debug log.
+ metrics.record_chunks_lagged(n);
debug!(
"audio channel lagged by {} messages for {}, continuing",
n, device_name
diff --git a/crates/screenpipe-audio/src/metrics.rs b/crates/screenpipe-audio/src/metrics.rs
index c8058534c..9826858c7 100644
--- a/crates/screenpipe-audio/src/metrics.rs
+++ b/crates/screenpipe-audio/src/metrics.rs
@@ -18,6 +18,10 @@ pub struct AudioPipelineMetrics {
pub chunks_channel_full: AtomicU64,
/// Device stream timeouts (no audio data received for >30s)
pub stream_timeouts: AtomicU64,
+ /// Audio buffers skipped because the recorder consumer fell behind the
+ /// capture broadcast channel. This is otherwise-silent audio loss under
+ /// CPU contention — previously invisible to telemetry.
+ pub chunks_lagged: AtomicU64,
// --- VAD stage ---
/// Chunks that passed VAD (speech_ratio > threshold)
@@ -83,6 +87,7 @@ impl AudioPipelineMetrics {
chunks_sent: AtomicU64::new(0),
chunks_channel_full: AtomicU64::new(0),
stream_timeouts: AtomicU64::new(0),
+ chunks_lagged: AtomicU64::new(0),
vad_passed: AtomicU64::new(0),
vad_rejected: AtomicU64::new(0),
speech_ratio_sum_x1000: AtomicU64::new(0),
@@ -121,6 +126,14 @@ impl AudioPipelineMetrics {
self.stream_timeouts.fetch_add(1, Ordering::Relaxed);
}
+ /// Record `n` audio buffers skipped because the recorder consumer fell
+ /// behind the capture broadcast channel (broadcast `Lagged(n)`). This is
+ /// otherwise-silent audio loss — surfaced so the health endpoint and
+ /// analytics can see contention-induced gaps.
+ pub fn record_chunks_lagged(&self, n: u64) {
+ self.chunks_lagged.fetch_add(n, Ordering::Relaxed);
+ }
+
// --- Consumer stage ---
pub fn record_chunk_received(&self) {
@@ -281,6 +294,7 @@ impl AudioPipelineMetrics {
chunks_sent,
chunks_channel_full: self.chunks_channel_full.load(Ordering::Relaxed),
stream_timeouts: self.stream_timeouts.load(Ordering::Relaxed),
+ chunks_lagged: self.chunks_lagged.load(Ordering::Relaxed),
// Consumer
chunks_received: self.chunks_received.load(Ordering::Relaxed),
process_errors: self.process_errors.load(Ordering::Relaxed),
@@ -342,6 +356,9 @@ pub struct AudioMetricsSnapshot {
pub chunks_sent: u64,
pub chunks_channel_full: u64,
pub stream_timeouts: u64,
+ /// Audio buffers skipped because the recorder lagged the capture channel
+ /// (silent loss under CPU contention).
+ pub chunks_lagged: u64,
// Consumer stage
pub chunks_received: u64,
diff --git a/crates/screenpipe-engine/src/resource_monitor.rs b/crates/screenpipe-engine/src/resource_monitor.rs
index e9638234e..028f140be 100644
--- a/crates/screenpipe-engine/src/resource_monitor.rs
+++ b/crates/screenpipe-engine/src/resource_monitor.rs
@@ -7,7 +7,7 @@ use std::fs::OpenOptions;
use std::io::Write;
use std::sync::Arc;
use std::time::{Duration, Instant};
-use sysinfo::{CpuExt, PidExt, ProcessExt, System, SystemExt};
+use sysinfo::{CpuExt, PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
use tracing::debug;
use tracing::trace;
use tracing::{error, info, warn};
@@ -49,7 +49,14 @@ impl HardwareInfo {
.unwrap_or_default();
let cpu_arch = std::env::consts::ARCH.to_string();
- let cpu_count = sys.cpus().len();
+ // `available_parallelism()` is the deterministic source for logical
+ // core count. sysinfo's `cpus()` can still be empty right after a
+ // single `refresh_cpu()` — that flake reported cpu_count=0 on ~99% of
+ // hosts, which made per-core CPU normalization impossible downstream.
+ // Fall back to `cpus().len()` only if the std call fails.
+ let cpu_count = std::thread::available_parallelism()
+ .map(|n| n.get())
+ .unwrap_or_else(|_| sys.cpus().len());
let os_name = sys.name().unwrap_or_default();
let os_version = sys.os_version().unwrap_or_default();
let kernel_version = sys.kernel_version().unwrap_or_default();
@@ -509,7 +516,10 @@ impl ResourceMonitor {
// Only load process + CPU info — skip disks, networks, components.
let mut sys = System::new();
sys.refresh_cpu();
- sys.refresh_processes();
+ // Refresh per-process CPU only (memory/parent are always collected).
+ // Skipping per-process disk-usage and user lookups avoids the extra
+ // per-PID syscalls that make a full refresh costly on Windows.
+ sys.refresh_processes_specifics(ProcessRefreshKind::new().with_cpu());
sys.refresh_memory();
loop {
@@ -519,7 +529,9 @@ impl ResourceMonitor {
// CPU + process list + system memory totals.
// Skips disks, networks, components — saves allocations.
sys.refresh_cpu();
- sys.refresh_processes();
+ // CPU-only process refresh: skip per-PID disk/user
+ // syscalls (the expensive part on Windows).
+ sys.refresh_processes_specifics(ProcessRefreshKind::new().with_cpu());
sys.refresh_memory();
// Tell the system allocator to return freed pages to the OS.
diff --git a/crates/screenpipe-engine/src/routes/health.rs b/crates/screenpipe-engine/src/routes/health.rs
index dd67d39f0..0ebb91f54 100644
--- a/crates/screenpipe-engine/src/routes/health.rs
+++ b/crates/screenpipe-engine/src/routes/health.rs
@@ -295,6 +295,10 @@ pub struct AudioPipelineHealthInfo {
pub chunks_received: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub process_errors: Option<u64>,
+ /// Audio buffers skipped because the recorder lagged the capture channel
+ /// (silent loss). Omitted when zero.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub chunks_lagged: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub audio_level_rms: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -1089,6 +1093,11 @@ async fn health_check_inner(state: &Arc<AppState>) -> HealthCheckResponse {
// Consumer stage diagnostics
chunks_received: Some(audio_snap.chunks_received),
process_errors: Some(audio_snap.process_errors),
+ chunks_lagged: if audio_snap.chunks_lagged > 0 {
+ Some(audio_snap.chunks_lagged)
+ } else {
+ None
+ },
audio_level_rms: Some(audio_snap.audio_level_rms),
per_device_audio_level_rms: if per_device_levels.is_empty() {
None
diff --git a/crates/screenpipe-engine/src/server.rs b/crates/screenpipe-engine/src/server.rs
index 4800c2b87..446fd2829 100644
--- a/crates/screenpipe-engine/src/server.rs
+++ b/crates/screenpipe-engine/src/server.rs
@@ -492,6 +492,7 @@ impl SCServer {
"chunks_received": snap.chunks_received,
"chunks_channel_full": snap.chunks_channel_full,
"stream_timeouts": snap.stream_timeouts,
+ "chunks_lagged": snap.chunks_lagged,
"process_errors": snap.process_errors,
"vad_passed": snap.vad_passed,
"vad_rejected": snap.vad_rejected,