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(scheduler): skip pipes in setup mode (missing connections)

Louis Beaumont3d1582a27ab5
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 36be93be0..2c4c61522 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -1062,6 +1062,19 @@ pub type OnPipeRunComplete = Arc<dyn Fn(&str, bool, f64, Option<&str>) + Send +
/// Args: (pipe_name, execution_id, line)
pub type OnPipeOutputLine = Arc<dyn Fn(&str, i64, &str) + Send + Sync>;
+/// Async predicate: given a pipe's required connections, return the
+/// subset that is NOT yet configured (`enabled && credentials present`).
+/// Returning an empty vec means "all connections are ready, pipe may run".
+///
+/// Injected from the engine layer (which owns the SecretStore + screenpipe
+/// dir) to keep the scheduler in `screenpipe-core` free of a
+/// `screenpipe-connect` dep — that crate already depends on us.
+pub type ConnectionCheck = Arc<
+ dyn Fn(Vec<String>) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<String>> + Send>>
+ + Send
+ + Sync,
+>;
+
/// Default execution timeout: 10 minutes.
const DEFAULT_TIMEOUT_SECS: u64 = 600;
@@ -1192,6 +1205,10 @@ pub struct PipeManager {
on_run_complete: Option<OnPipeRunComplete>,
/// Optional callback fired for each stdout line from a running pipe.
on_output_line: Option<OnPipeOutputLine>,
+ /// Optional async predicate that returns the missing connections for a
+ /// pipe. If set, the scheduler skips any enabled pipe whose required
+ /// connections aren't all configured ("setup mode").
+ connection_check: Option<ConnectionCheck>,
/// Optional persistence store (None in CLI mode).
store: Option<Arc<dyn PipeStore>>,
/// API port for prompt rendering (default 3030).
@@ -1233,6 +1250,7 @@ impl PipeManager {
scheduler_generation: Arc::new(std::sync::atomic::AtomicU64::new(0)),
on_run_complete: None,
on_output_line: None,
+ connection_check: None,
store,
api_port,
last_reload: Arc::new(Mutex::new(
@@ -1296,6 +1314,14 @@ impl PipeManager {
self.on_run_complete = Some(cb);
}
+ /// Set the async predicate used to gate scheduled runs on connection
+ /// readiness. Without this, pipes "in setup mode" (enabled but missing
+ /// required integrations) would still tick on their cron / event
+ /// trigger and run with broken credentials.
+ pub fn set_connection_check(&mut self, cb: ConnectionCheck) {
+ self.connection_check = Some(cb);
+ }
+
/// Set a callback to be invoked for each stdout line from a running pipe.
pub fn set_on_output_line(&mut self, cb: OnPipeOutputLine) {
self.on_output_line = Some(cb);
@@ -3037,6 +3063,7 @@ impl PipeManager {
let pipes_dir = self.pipes_dir.clone();
let on_run_complete = self.on_run_complete.clone();
let on_output_line = self.on_output_line.clone();
+ let connection_check = self.connection_check.clone();
let store = self.store.clone();
let api_port = self.api_port;
let token_registry = self.token_registry.clone();
@@ -3231,6 +3258,25 @@ impl PipeManager {
}
}
+ // Setup-mode gate: pipes whose declared `connections` aren't
+ // all configured (`enabled && credentials present`) must not
+ // run on schedule or event. Mirrors the manual-run gate in
+ // pipes_api::run_pipe_now. Placed after the schedule/queue
+ // checks so we only hit the SecretStore when the pipe would
+ // otherwise be about to start.
+ if !config.connections.is_empty() {
+ if let Some(check) = &connection_check {
+ let missing = check(config.connections.clone()).await;
+ if !missing.is_empty() {
+ debug!(
+ "scheduler: pipe '{}' in setup mode (missing connections: {:?}), skipping",
+ name, missing
+ );
+ continue;
+ }
+ }
+ }
+
// Defense-in-depth: check PID file (survives crashes)
if let Some(existing_pid) = read_pid_file(&pipes_dir, name) {
if is_process_alive(existing_pid) {
diff --git a/crates/screenpipe-engine/src/bin/screenpipe-engine.rs b/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
index 1698d0538..fcd5ab76d 100644
--- a/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
+++ b/crates/screenpipe-engine/src/bin/screenpipe-engine.rs
@@ -1030,6 +1030,34 @@ async fn main() -> anyhow::Result<()> {
analytics::capture_event_nonblocking("pipe_scheduled_run", props);
},
));
+ // Gate scheduled pipe runs on connection readiness — same predicate the
+ // manual /pipes/:id/run endpoint uses (pipes_api.rs). Avoids running
+ // pipes that are still in "setup mode" (declared connections not paired).
+ {
+ let secret_store_for_check = server.secret_store.clone();
+ let screenpipe_dir_for_check = local_data_dir.clone();
+ pipe_manager.set_connection_check(std::sync::Arc::new(move |required| {
+ let ss = secret_store_for_check.clone();
+ let dir = screenpipe_dir_for_check.clone();
+ Box::pin(async move {
+ let mut missing = Vec::new();
+ for conn_id in required {
+ let configured = screenpipe_connect::connections::load_connection(
+ ss.as_deref(),
+ &dir,
+ &conn_id,
+ )
+ .await
+ .map(|c| c.enabled && !c.credentials.is_empty())
+ .unwrap_or(false);
+ if !configured {
+ missing.push(conn_id);
+ }
+ }
+ missing
+ })
+ }));
+ }
// Inject local API key so pipe subprocesses can authenticate to localhost
if config.api_auth {
pipe_manager.set_local_api_key(config.api_auth_key.clone());