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
perf: CPU and memory optimization pass (tracking) (#6302)
Louis Beaumont2402c7c6567d
diff --git a/crates/screenpipe-a11y/src/platform/macos.rs b/crates/screenpipe-a11y/src/platform/macos.rsindex 77ee27ea2..2edf7cff0 100644--- a/crates/screenpipe-a11y/src/platform/macos.rs+++ b/crates/screenpipe-a11y/src/platform/macos.rs@@ -54,6 +54,26 @@ fn record_input_monitoring_truth(granted: bool) {}// Keycodes for clipboard operations (macOS)+/// Did `run_in_mode` come back without having waited at all?+///+/// `Finished` (no sources or timers left in this mode) and `Stopped`+/// (`CFRunLoopStop`) both return immediately, so a caller that loops straight+/// back in spins a core. `TimedOut` already waited out the slice, and+/// `HandledSource` is the healthy busy path that must stay hot.+fn ax_run_loop_returned_without_waiting(result: cf::RunLoopRunResult) -> bool {+ matches!(+ result,+ cf::RunLoopRunResult::Finished | cf::RunLoopRunResult::Stopped+ )+}++/// How long one AX run-loop slice waits when the mode is idle but healthy.+const AX_RUN_LOOP_SLICE_SECS: f64 = 0.1;+/// Yield for a full slice when `run_in_mode` returned without waiting at all.+/// Matching the slice keeps a sourceless observer thread at roughly the same+/// wake rate as a healthy idle one instead of at 100% of a core.+const AX_RUN_LOOP_IDLE_BACKOFF: Duration = Duration::from_millis(100);+const KEY_C: u16 = 8;const KEY_X: u16 = 7;const KEY_V: u16 = 9;@@ -1613,7 +1633,27 @@ fn run_app_observer(};while !stop.load(Ordering::Acquire) {- cf::RunLoop::run_in_mode(run_loop_mode, 0.1, true);+ let result = cf::RunLoop::run_in_mode(run_loop_mode, AX_RUN_LOOP_SLICE_SECS, true);++ // `Finished` means this mode has no sources or timers left — the+ // observed app exited, the observer was invalidated, or every+ // `add_notification` above failed — and `Stopped` means someone called+ // CFRunLoopStop. Both return *immediately* instead of waiting out the+ // interval, so looping straight back into `run_in_mode` turns this+ // thread into a spin at 100% of one core. Nothing re-adds a source+ // unless `refresh_requested` fires, so it never recovers on its own.+ //+ // This is the shape behind the macOS pinned-core cohort: p98 CPU sits+ // at ~112% (one saturated core) in every uptime bucket, and the pinned+ // share climbs with uptime because a latched process never comes back+ // — 99 users at a 55.8h median while their median peer sits near 8%.+ //+ // `HandledSource` must keep looping immediately; returning promptly+ // after one source is the entire point of `return_after_source_handled`+ // and is what keeps focus tracking responsive.+ if ax_run_loop_returned_without_waiting(result) {+ std::thread::sleep(AX_RUN_LOOP_IDLE_BACKOFF);+ }if refresh_requested.swap(false, Ordering::SeqCst) {reattach_observer();@@ -2755,6 +2795,49 @@ extern "C" fn activity_only_callback(mod tests {use super::*;+ /// The AX observer thread loops on `run_in_mode`. Two of the four results+ /// come back instantly: `Finished` when the mode has no sources or timers+ /// left, and `Stopped` after `CFRunLoopStop`. Looping straight back in on+ /// either pins the thread at 100% of one core, and nothing re-adds a source+ /// unless a refresh is requested, so it never recovers — the shape behind+ /// ~99 macOS users sitting at one saturated core with a 55.8h median+ /// uptime while their median peer used ~8%.+ #[test]+ fn instant_run_loop_returns_back_off_and_waiting_ones_do_not() {+ assert!(+ ax_run_loop_returned_without_waiting(cf::RunLoopRunResult::Finished),+ "no sources left returns immediately — looping on it is the spin"+ );+ assert!(+ ax_run_loop_returned_without_waiting(cf::RunLoopRunResult::Stopped),+ "CFRunLoopStop returns immediately too"+ );++ // These two must never back off. `TimedOut` already waited out the+ // slice, and delaying `HandledSource` would add latency to every focus+ // change — returning promptly after one source is the entire point of+ // `return_after_source_handled`.+ assert!(+ !ax_run_loop_returned_without_waiting(cf::RunLoopRunResult::TimedOut),+ "TimedOut already waited the full slice"+ );+ assert!(+ !ax_run_loop_returned_without_waiting(cf::RunLoopRunResult::HandledSource),+ "HandledSource is the healthy busy path and must stay hot"+ );+ }++ /// A sourceless observer thread should wake at roughly the same rate as a+ /// healthy idle one, not faster.+ #[test]+ fn idle_backoff_matches_one_healthy_slice() {+ assert_eq!(+ AX_RUN_LOOP_IDLE_BACKOFF.as_secs_f64(),+ AX_RUN_LOOP_SLICE_SECS,+ "backoff drifting from the slice changes the sourceless wake rate"+ );+ }+#[test]fn test_permission_check() {// This will fail without permissions, but should not panicdiff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rsindex 01f69322c..880184a29 100644--- a/crates/screenpipe-core/src/agents/pi.rs+++ b/crates/screenpipe-core/src/agents/pi.rs@@ -1859,7 +1859,7 @@ impl PiExecutor {// BufReader::lines() which crashes on invalid UTF-8 bytes.// See: toggl-sync crash "stream did not contain valid UTF-8".let mut reader = tokio::io::BufReader::new(child_stdout);- let mut stdout_buf = String::new();+ let mut stdout_buf = BoundedOutput::default();let mut llm_error: Option<String> = None;let mut line_bytes = Vec::new();@@ -1905,8 +1905,7 @@ impl PiExecutor {}}- stdout_buf.push_str(&line);- stdout_buf.push('\n');+ stdout_buf.push_line(&line);}let status = child.wait().await?;@@ -1938,7 +1937,7 @@ impl PiExecutor {};Ok(AgentOutput {- stdout: stdout_buf,+ stdout: stdout_buf.into_string(),stderr,success,pid,@@ -2828,6 +2827,67 @@ pub fn describe_exit_status_code(code: i32) -> String {format!("exit code {code}")}+/// Head bytes kept verbatim by [`BoundedOutput`] — enough for the run's setup.+const BOUNDED_OUTPUT_HEAD: usize = 64 * 1024;+/// Trailing bytes kept by [`BoundedOutput`] — where the result or error lands.+const BOUNDED_OUTPUT_TAIL: usize = 192 * 1024;++/// A run's captured stdout, bounded in memory while keeping both ends.+///+/// The agent's stdout was accumulated into an unbounded `String` for the whole+/// run, so a long agent turn with large tool results held all of it resident.+/// Nothing parses this buffer — the JSON events are decoded per line as they+/// arrive and this is only the stored record — so eliding the middle costs no+/// behavior.+///+/// Both ends are kept deliberately: the head carries the run's setup and the+/// tail carries the result or the error, which are the two things anyone+/// reading a failed run actually needs.+#[derive(Default)]+pub struct BoundedOutput {+ head: String,+ tail: String,+ dropped: usize,+}++impl BoundedOutput {+ pub fn push_line(&mut self, line: &str) {+ // `self.tail.is_empty()` closes the head for good once anything has+ // spilled. Without it a short line still fits the head's leftover+ // capacity after longer lines have already gone to the tail, and the+ // record silently reorders itself.+ if self.tail.is_empty() && self.head.len() + line.len() + 1 <= BOUNDED_OUTPUT_HEAD {+ self.head.push_str(line);+ self.head.push('\n');+ return;+ }+ self.tail.push_str(line);+ self.tail.push('\n');+ // Trim whole lines off the front so the tail stays a run of complete+ // lines rather than resuming mid-token.+ while self.tail.len() > BOUNDED_OUTPUT_TAIL {+ let Some(cut) = self.tail.find('\n').map(|i| i + 1) else {+ break;+ };+ self.dropped += cut;+ self.tail.drain(..cut);+ }+ }++ pub fn into_string(self) -> String {+ if self.tail.is_empty() {+ return self.head;+ }+ if self.dropped == 0 {+ return self.head + &self.tail;+ }+ format!(+ "{}\n...[{} bytes elided to bound memory]...\n{}",+ self.head, self.dropped, self.tail+ )+ }+}+/// Last `max` bytes of a captured process stream, lossy-decoded and/// char-boundary safe, with an "(empty)" placeholder so a silent subprocess/// can never reduce an error message to nothing.@@ -5315,6 +5375,72 @@ mod tests {);}+ /// A normal run must round-trip byte-for-byte — this replaced an unbounded+ /// `String`, so anything short has to look exactly as it did before.+ #[test]+ fn short_output_is_unchanged() {+ let mut out = BoundedOutput::default();+ out.push_line("{\"type\":\"start\"}");+ out.push_line("{\"type\":\"agent_end\"}");+ assert_eq!(+ out.into_string(),+ "{\"type\":\"start\"}\n{\"type\":\"agent_end\"}\n"+ );+ }++ /// A long agent turn is bounded, and both ends survive: the head carries+ /// the setup, the tail carries the result or error. Losing the tail would+ /// be worse than the memory it saves — that is where failures land.+ #[test]+ fn long_output_is_bounded_and_keeps_both_ends() {+ let mut out = BoundedOutput::default();+ out.push_line("FIRST_LINE_MARKER");+ for i in 0..40_000 {+ out.push_line(&format!(+ "{{\"tool_result\":{},\"padding\":\"{}\"}}",+ i,+ "x".repeat(64)+ ));+ }+ out.push_line("LAST_LINE_MARKER");++ let s = out.into_string();+ assert!(+ s.starts_with("FIRST_LINE_MARKER\n"),+ "head must survive so the run's setup is still readable"+ );+ assert!(+ s.trim_end().ends_with("LAST_LINE_MARKER"),+ "tail must survive — the result and any error land there"+ );+ assert!(+ s.contains("bytes elided"),+ "elision must be visible, not silent"+ );+ assert!(+ s.len() <= BOUNDED_OUTPUT_HEAD + BOUNDED_OUTPUT_TAIL + 128,+ "bounded output grew to {} bytes",+ s.len()+ );+ }++ /// The tail is trimmed by whole lines, so it never resumes mid-JSON.+ #[test]+ fn elided_tail_starts_on_a_line_boundary() {+ let mut out = BoundedOutput::default();+ for i in 0..40_000 {+ out.push_line(&format!("{{\"n\":{},\"pad\":\"{}\"}}", i, "y".repeat(64)));+ }+ let s = out.into_string();+ let tail = s.rsplit("]...\n").next().unwrap();+ for line in tail.lines().take(5) {+ assert!(+ line.starts_with('{') && line.ends_with('}'),+ "tail line resumed mid-record: {line}"+ );+ }+ }+#[test]fn output_tail_truncates_to_last_bytes() {assert_eq!(output_tail(b"", 100), "(empty)");