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

fix: use PTY for Pi stdin to work around bun readline pipe bug

Louis Beaumont404a7562fdf8
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 37d4e07f6..1358a64a1 100755
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -8097,7 +8097,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.0.466"
+version = "2.0.468"
dependencies = [
"anyhow",
"arboard",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index 5bef9fe42..a956629e5 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.0.467"
+version = "2.0.468"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 923c7bca5..c39c5d5dd 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -125,6 +125,11 @@ struct RpcResponse {
pub struct PiManager {
child: Option<Child>,
stdin: Option<ChildStdin>,
+ /// PTY master for writing to Pi's stdin (macOS only).
+ /// Bun's readline doesn't emit line events on piped stdin,
+ /// so we use a PTY which makes readline work correctly.
+ #[cfg(target_os = "macos")]
+ pty_master: Option<std::fs::File>,
project_dir: Option<String>,
request_id: u64,
app_handle: AppHandle,
@@ -135,6 +140,8 @@ impl PiManager {
Self {
child: None,
stdin: None,
+ #[cfg(target_os = "macos")]
+ pty_master: None,
project_dir: None,
request_id: 0,
app_handle,
@@ -152,6 +159,8 @@ impl PiManager {
info!("Pi process (pid {}) has exited with status: {}", pid, status);
self.child = None;
self.stdin = None;
+ #[cfg(target_os = "macos")]
+ { self.pty_master = None; }
// Emit pi_terminated so frontend can auto-restart
let _ = self.app_handle.emit("pi_terminated", pid);
false
@@ -181,9 +190,17 @@ impl PiManager {
pub fn stop(&mut self) {
if let Some(mut child) = self.child.take() {
- // Send abort command first
- if let Some(ref mut stdin) = self.stdin {
- let _ = writeln!(stdin, r#"{{"type":"abort"}}"#);
+ // Send abort command first (prefer PTY on Unix)
+ let mut sent = false;
+ #[cfg(target_os = "macos")]
+ if let Some(ref mut pty) = self.pty_master {
+ let _ = writeln!(pty, r#"{{"type":"abort"}}"#);
+ sent = true;
+ }
+ if !sent {
+ if let Some(ref mut stdin) = self.stdin {
+ let _ = writeln!(stdin, r#"{{"type":"abort"}}"#);
+ }
}
// Kill the process
@@ -193,6 +210,8 @@ impl PiManager {
let _ = child.wait();
}
self.stdin = None;
+ #[cfg(target_os = "macos")]
+ { self.pty_master = None; }
self.project_dir = None;
}
@@ -200,15 +219,13 @@ impl PiManager {
self.check_alive()
}
- /// Send a command to Pi via stdin and return response
+ /// Send a command to Pi via stdin (PTY on Unix) and return response
pub fn send_command(&mut self, command: Value) -> Result<(), String> {
// Verify process is actually alive before writing
if !self.check_alive() {
return Err("Pi process has died".to_string());
}
- let stdin = self.stdin.as_mut().ok_or("Pi not running")?;
-
self.request_id += 1;
let mut cmd = command;
if let Some(obj) = cmd.as_object_mut() {
@@ -216,20 +233,35 @@ impl PiManager {
}
let cmd_str = serde_json::to_string(&cmd).map_err(|e| e.to_string())?;
+ let child_pid = self.child.as_ref().map(|c| c.id());
+ let cmd_type = cmd.get("type").and_then(|t| t.as_str()).unwrap_or("?").to_string();
+
+ // Use PTY master on macOS if available (bun readline workaround)
+ #[cfg(target_os = "macos")]
+ {
+ if let Some(ref mut pty) = self.pty_master {
+ use std::os::unix::io::AsRawFd;
+ info!("Sending to Pi (req_{}): type={}, pty_fd={}, child_pid={:?}, bytes={}",
+ self.request_id, cmd_type, pty.as_raw_fd(), child_pid, cmd_str.len() + 1);
+
+ writeln!(pty, "{}", cmd_str).map_err(|e| format!("Failed to write to Pi pty: {}", e))?;
+ pty.flush().map_err(|e| format!("Failed to flush Pi pty: {}", e))?;
+ info!("Sent to Pi (req_{}): flushed ok", self.request_id);
+ return Ok(());
+ }
+ }
+
+ // Fall back to pipe stdin (Windows, or if PTY creation failed)
+ let stdin = self.stdin.as_mut().ok_or("Pi not running")?;
- // Log raw FD and child PID for pipe debugging
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
- let fd = stdin.as_raw_fd();
- let child_pid = self.child.as_ref().map(|c| c.id());
info!("Sending to Pi (req_{}): type={}, stdin_fd={}, child_pid={:?}, bytes={}",
- self.request_id,
- cmd.get("type").and_then(|t| t.as_str()).unwrap_or("?"),
- fd, child_pid, cmd_str.len() + 1);
+ self.request_id, cmd_type, stdin.as_raw_fd(), child_pid, cmd_str.len() + 1);
}
#[cfg(not(unix))]
- info!("Sending to Pi (req_{}): type={}", self.request_id, cmd.get("type").and_then(|t| t.as_str()).unwrap_or("?"));
+ info!("Sending to Pi (req_{}): type={}", self.request_id, cmd_type);
writeln!(stdin, "{}", cmd_str).map_err(|e| format!("Failed to write to Pi stdin: {}", e))?;
stdin.flush().map_err(|e| format!("Failed to flush Pi stdin: {}", e))?;
@@ -633,8 +665,37 @@ pub async fn pi_start_inner(
cmd.args(["--append-system-prompt", api_hint]);
}
- cmd.stdin(Stdio::piped())
- .stdout(Stdio::piped())
+ // On macOS, use a PTY for stdin so bun's readline emits line events.
+ // Bun 1.2's readline.createInterface doesn't fire "line" events on piped
+ // stdin — it buffers until EOF. A PTY makes it work line-by-line.
+ #[cfg(target_os = "macos")]
+ let pty_master: Option<std::fs::File> = {
+ use std::os::unix::io::FromRawFd;
+ let mut master_fd: libc::c_int = 0;
+ let mut slave_fd: libc::c_int = 0;
+ let ret = unsafe {
+ libc::openpty(
+ &mut master_fd,
+ &mut slave_fd,
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ )
+ };
+ if ret == 0 {
+ cmd.stdin(unsafe { Stdio::from_raw_fd(slave_fd) });
+ info!("Using PTY for Pi stdin (master_fd={}, slave_fd={})", master_fd, slave_fd);
+ Some(unsafe { std::fs::File::from_raw_fd(master_fd) })
+ } else {
+ warn!("openpty failed (ret={}), falling back to pipe for Pi stdin", ret);
+ cmd.stdin(Stdio::piped());
+ None
+ }
+ };
+ #[cfg(not(target_os = "macos"))]
+ cmd.stdin(Stdio::piped());
+
+ cmd.stdout(Stdio::piped())
.stderr(Stdio::piped());
// On Windows, prevent console window from appearing
@@ -663,26 +724,6 @@ pub async fn pi_start_inner(
}
}
- // On macOS, isolate the child process from the parent Tauri app's Metal/GPU
- // context. Without this, Pi's native modules (clipboard-darwin-universal) try
- // to initialize Metal and deadlock on inherited Mach ports from the webview.
- // setsid() creates a new session; closing FDs 3+ drops inherited Mach ports.
- #[cfg(target_os = "macos")]
- {
- use std::os::unix::process::CommandExt;
- unsafe {
- cmd.pre_exec(|| {
- libc::setsid();
- let max_fd = libc::sysconf(libc::_SC_OPEN_MAX) as i32;
- let max_fd = if max_fd > 0 { max_fd.min(4096) } else { 1024 };
- for fd in 3..max_fd {
- libc::close(fd);
- }
- Ok(())
- });
- }
- }
-
// Spawn process
let mut child = cmd.spawn()
.map_err(|e| format!("Failed to spawn pi: {}", e))?;
@@ -690,9 +731,17 @@ pub async fn pi_start_inner(
let pid = child.id();
info!("Pi started with PID: {}", pid);
- // Take stdin for writing commands
- let stdin = child.stdin.take()
- .ok_or_else(|| "Failed to get pi stdin".to_string())?;
+ // Take stdin for writing commands (only when not using PTY)
+ #[cfg(target_os = "macos")]
+ let stdin = if pty_master.is_some() {
+ None // Writing via PTY master
+ } else {
+ Some(child.stdin.take()
+ .ok_or_else(|| "Failed to get pi stdin".to_string())?)
+ };
+ #[cfg(not(target_os = "macos"))]
+ let stdin = Some(child.stdin.take()
+ .ok_or_else(|| "Failed to get pi stdin".to_string())?);
// Take stdout for reading events
let stdout = child.stdout.take()
@@ -704,7 +753,9 @@ pub async fn pi_start_inner(
// Update manager
if let Some(m) = manager_guard.as_mut() {
m.child = Some(child);
- m.stdin = Some(stdin);
+ m.stdin = stdin;
+ #[cfg(target_os = "macos")]
+ { m.pty_master = pty_master; }
m.project_dir = Some(project_dir.clone());
}