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(vault): delegate CLI lock to running daemon, fall back to direct lock

floML7aadba0a4408
diff --git a/crates/screenpipe-engine/src/cli/mod.rs b/crates/screenpipe-engine/src/cli/mod.rs
index 08a8d5e36..773c783b1 100644
--- a/crates/screenpipe-engine/src/cli/mod.rs
+++ b/crates/screenpipe-engine/src/cli/mod.rs
@@ -1338,7 +1338,12 @@ pub enum VaultCommand {
/// Set up vault encryption (first-time password setup)
Setup {},
/// Lock the vault (encrypt all data, stop recording)
- Lock {},
+ Lock {
+ /// Port of the running screenpipe daemon (used to delegate lock if
+ /// reachable). Falls back to direct file-level lock if unreachable.
+ #[arg(short = 'p', long, default_value_t = 3030)]
+ port: u16,
+ },
/// Unlock the vault (decrypt data, resume recording)
Unlock {},
}
diff --git a/crates/screenpipe-engine/src/cli/vault.rs b/crates/screenpipe-engine/src/cli/vault.rs
index 4df5c5825..6c2dff596 100644
--- a/crates/screenpipe-engine/src/cli/vault.rs
+++ b/crates/screenpipe-engine/src/cli/vault.rs
@@ -40,10 +40,34 @@ pub async fn handle_vault_command(command: &VaultCommand) -> anyhow::Result<()>
println!("vault set up successfully");
}
- VaultCommand::Lock { .. } => {
+ VaultCommand::Lock { port } => {
let password = read_password("vault password: ")?;
+
+ // If the daemon is running, ask IT to lock — that way the
+ // daemon (which owns the DB pool and recording loop) handles
+ // shutdown cleanly. Fall back to direct file-level lock if
+ // the daemon is unreachable so locking remains guaranteed.
+ if daemon_running(*port).await {
+ match try_lock_via_daemon(*port, &password).await {
+ Ok(()) => {
+ println!("vault locked — data encrypted (via running daemon)");
+ return Ok(());
+ }
+ Err(LockViaDaemonError::WrongPassword) => {
+ anyhow::bail!("wrong password");
+ }
+ Err(e) => {
+ eprintln!(
+ "warning: daemon on port {} unreachable for lock ({}). \
+ falling back to direct lock — stop screenpipe first \
+ if you want a clean shutdown.",
+ port, e
+ );
+ }
+ }
+ }
+
let _progress_rx = vault.lock(&password).await?;
- // Wait for encryption to complete
loop {
let state = vault.state().await;
match state {
@@ -88,3 +112,50 @@ fn read_password(prompt: &str) -> anyhow::Result<String> {
let password = rpassword::read_password()?;
Ok(password)
}
+
+async fn daemon_running(port: u16) -> bool {
+ tokio::net::TcpStream::connect(format!("127.0.0.1:{}", port))
+ .await
+ .is_ok()
+}
+
+enum LockViaDaemonError {
+ WrongPassword,
+ NoApiKey,
+ Http(String),
+}
+
+impl std::fmt::Display for LockViaDaemonError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ LockViaDaemonError::WrongPassword => write!(f, "wrong password"),
+ LockViaDaemonError::NoApiKey => write!(f, "no API key available"),
+ LockViaDaemonError::Http(e) => write!(f, "{}", e),
+ }
+ }
+}
+
+async fn try_lock_via_daemon(port: u16, password: &str) -> Result<(), LockViaDaemonError> {
+ let api_key = crate::auth_key::find_api_auth_key()
+ .await
+ .ok_or(LockViaDaemonError::NoApiKey)?;
+
+ let client = reqwest::Client::builder()
+ .timeout(std::time::Duration::from_secs(10))
+ .build()
+ .map_err(|e| LockViaDaemonError::Http(e.to_string()))?;
+
+ let resp = client
+ .post(format!("http://127.0.0.1:{}/vault/lock", port))
+ .header("Authorization", format!("Bearer {}", api_key))
+ .json(&json!({ "password": password }))
+ .send()
+ .await
+ .map_err(|e| LockViaDaemonError::Http(e.to_string()))?;
+
+ match resp.status().as_u16() {
+ 200 => Ok(()),
+ 403 => Err(LockViaDaemonError::WrongPassword),
+ other => Err(LockViaDaemonError::Http(format!("HTTP {}", other))),
+ }
+}