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: use Pi's native auto-compaction for pipe session history

Louis Beaumont8f49e2cf02e3
diff --git a/Cargo.lock b/Cargo.lock
index 77b10ab27..99816955e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5512,7 +5512,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"audiopipe",
@@ -5569,7 +5569,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -5611,7 +5611,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -5636,7 +5636,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -5654,7 +5654,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -5674,7 +5674,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"async-trait",
@@ -5735,7 +5735,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 1ab52fef9..d3a599021 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -8223,7 +8223,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.2.11"
+version = "2.2.12"
dependencies = [
"anyhow",
"arboard",
@@ -8322,7 +8322,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"audiopipe",
@@ -8374,7 +8374,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -8415,7 +8415,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -8440,7 +8440,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -8456,7 +8456,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"chrono",
@@ -8476,7 +8476,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"anyhow",
"async-trait",
@@ -8532,7 +8532,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.160"
+version = "0.3.161"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 350638ef8..49e964b6b 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -840,29 +840,7 @@ impl PipeManager {
let pipe_dir = self.pipes_dir.clone().join(name);
- // Session compaction: if history is enabled and sessions are too large,
- // compact them into a summary and prepend to the prompt
- let compaction_summary = if history_enabled && config.agent == "pi" {
- maybe_compact_session(
- &pipe_dir,
- executor.as_ref(),
- &run_model,
- run_provider.as_deref(),
- run_provider_url.as_deref(),
- run_api_key.as_deref(),
- )
- .await
- } else {
- None
- };
-
- let mut prompt = self.render_prompt(&config, &body, preset_prompt.as_deref());
- if let Some(ref summary) = compaction_summary {
- prompt = format!(
- "<conversation_summary>\n{}\n</conversation_summary>\n\n{}",
- summary, prompt
- );
- }
+ let prompt = self.render_prompt(&config, &body, preset_prompt.as_deref());
let pipe_name = name.to_string();
// Mark running in DB
@@ -1203,29 +1181,8 @@ impl PipeManager {
.and_then(|v| v.as_bool())
.unwrap_or(false);
- // Session compaction check
- let compaction_summary = if history_enabled && config.agent == "pi" {
- maybe_compact_session(
- &pipe_dir,
- executor.as_ref(),
- &run_model,
- run_provider.as_deref(),
- run_provider_url.as_deref(),
- run_api_key.as_deref(),
- )
- .await
- } else {
- None
- };
-
// Build prompt with context header
- let mut prompt = self.render_prompt(&config, &body, preset_prompt.as_deref());
- if let Some(ref summary) = compaction_summary {
- prompt = format!(
- "<conversation_summary>\n{}\n</conversation_summary>\n\n{}",
- summary, prompt
- );
- }
+ let prompt = self.render_prompt(&config, &body, preset_prompt.as_deref());
// Create a channel so the executor can report PID immediately
let (pid_tx, pid_rx) = tokio::sync::oneshot::channel::<u32>();
@@ -1867,29 +1824,8 @@ impl PipeManager {
let pipe_dir = pipes_dir.join(name);
- // Session compaction check (before spawning the task)
- let compaction_summary = if history_enabled && config.agent == "pi" {
- maybe_compact_session(
- &pipe_dir,
- executor.as_ref(),
- &model,
- provider.as_deref(),
- provider_url.as_deref(),
- api_key.as_deref(),
- )
- .await
- } else {
- None
- };
-
- let mut prompt =
+ let prompt =
render_prompt_with_port(config, body, api_port, preset_prompt.as_deref());
- if let Some(ref summary) = compaction_summary {
- prompt = format!(
- "<conversation_summary>\n{}\n</conversation_summary>\n\n{}",
- summary, prompt
- );
- }
let pipe_name = name.clone();
let logs_ref = logs.clone();
let running_ref = running.clone();
@@ -2526,10 +2462,6 @@ fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
// Pi session helpers (for pipe history / --continue)
// ---------------------------------------------------------------------------
-/// Maximum session file size (in bytes) before compaction kicks in.
-/// ~300K chars ≈ 75K tokens — well under Pi's 200K token context window.
-const SESSION_COMPACTION_THRESHOLD: u64 = 300_000;
-
/// Encode a working-directory path the same way Pi does for session storage.
/// Pi uses the CWD as a key: `~/.pi/agent/sessions/<encoded-cwd>/`.
/// The encoding wraps the path with `--` and replaces `/` (or `\`) with `-`.
@@ -2545,24 +2477,6 @@ fn encode_pi_session_dir(working_dir: &Path) -> Option<PathBuf> {
Some(sessions_base.join(encoded))
}
-/// Get the total size of all session JSONL files for a pipe's working directory.
-fn pi_session_size(session_dir: &Path) -> u64 {
- let Ok(entries) = std::fs::read_dir(session_dir) else {
- return 0;
- };
- entries
- .filter_map(|e| e.ok())
- .filter(|e| {
- e.path()
- .extension()
- .map(|ext| ext == "jsonl")
- .unwrap_or(false)
- })
- .filter_map(|e| e.metadata().ok())
- .map(|m| m.len())
- .sum()
-}
-
/// Delete all Pi session files for a pipe's working directory.
pub fn delete_pi_sessions(pipe_dir: &Path) -> Result<()> {
let session_dir = encode_pi_session_dir(pipe_dir)
@@ -2574,162 +2488,6 @@ pub fn delete_pi_sessions(pipe_dir: &Path) -> Result<()> {
Ok(())
}
-/// Check if a pipe's Pi session needs compaction and handle it.
-///
-/// If sessions exceed `SESSION_COMPACTION_THRESHOLD`:
-/// 1. Read the session JSONL files and extract assistant message content
-/// 2. Run a one-shot Pi call to summarize the conversation history
-/// 3. Delete old session files
-/// 4. Return the summary to be prepended to the next prompt
-///
-/// Returns `None` if no compaction needed or if compaction fails (fallback to normal --continue).
-async fn maybe_compact_session(
- pipe_dir: &Path,
- executor: &dyn AgentExecutor,
- model: &str,
- provider: Option<&str>,
- provider_url: Option<&str>,
- provider_api_key: Option<&str>,
-) -> Option<String> {
- let session_dir = encode_pi_session_dir(pipe_dir)?;
- if !session_dir.exists() {
- return None;
- }
-
- let total_size = pi_session_size(&session_dir);
- if total_size < SESSION_COMPACTION_THRESHOLD {
- debug!(
- "session size {}B < threshold {}B, no compaction needed",
- total_size, SESSION_COMPACTION_THRESHOLD
- );
- return None;
- }
-
- info!(
- "session size {}B exceeds threshold, compacting for {:?}",
- total_size, pipe_dir
- );
-
- // Read session files and extract assistant messages
- let mut conversation_text = String::new();
- let mut entries: Vec<_> = std::fs::read_dir(&session_dir)
- .ok()?
- .filter_map(|e| e.ok())
- .filter(|e| {
- e.path()
- .extension()
- .map(|ext| ext == "jsonl")
- .unwrap_or(false)
- })
- .collect();
- entries.sort_by_key(|e| e.path());
-
- for entry in &entries {
- if let Ok(content) = std::fs::read_to_string(entry.path()) {
- for line in content.lines() {
- if let Ok(evt) = serde_json::from_str::<serde_json::Value>(line) {
- let is_message = evt.get("type").and_then(|t| t.as_str()) == Some("message");
- let role = evt
- .get("message")
- .and_then(|m| m.get("role"))
- .and_then(|r| r.as_str());
- if is_message && (role == Some("assistant") || role == Some("user")) {
- // Pi stores content as an array: [{type: "text", text: "..."}, ...]
- // Extract text from all "text" content blocks
- let text = evt
- .get("message")
- .and_then(|m| m.get("content"))
- .and_then(|c| {
- if let Some(arr) = c.as_array() {
- let parts: Vec<&str> = arr
- .iter()
- .filter(|item| {
- item.get("type").and_then(|t| t.as_str())
- == Some("text")
- })
- .filter_map(|item| {
- item.get("text").and_then(|t| t.as_str())
- })
- .collect();
- if parts.is_empty() {
- None
- } else {
- Some(parts.join("\n"))
- }
- } else {
- // Fallback: plain string content
- c.as_str().map(|s| s.to_string())
- }
- });
- if let Some(text) = text {
- let prefix = if role == Some("user") {
- "User"
- } else {
- "Assistant"
- };
- let truncated = &text[..text.len().min(2000)];
- conversation_text
- .push_str(&format!("{}: {}\n\n", prefix, truncated));
- }
- }
- }
- }
- }
- }
-
- if conversation_text.is_empty() {
- return None;
- }
-
- // Truncate to avoid sending too much to the summarizer
- let max_input = 50_000;
- if conversation_text.len() > max_input {
- conversation_text.truncate(max_input);
- }
-
- let summary_prompt = format!(
- "Summarize this conversation history in under 5000 characters. \
- Preserve key findings, patterns, decisions, and any important context. \
- Be concise but complete.\n\n---\n{}",
- conversation_text
- );
-
- // Run a one-shot Pi call (--no-session) to generate the summary
- match executor
- .run(
- &summary_prompt,
- model,
- pipe_dir,
- provider,
- provider_url,
- provider_api_key,
- None,
- false, // no-session for the summary call
- )
- .await
- {
- Ok(output) if output.success && !output.stdout.trim().is_empty() => {
- // Delete old session files
- if let Err(e) = std::fs::remove_dir_all(&session_dir) {
- warn!("failed to remove old sessions after compaction: {}", e);
- }
- info!("session compacted for {:?}", pipe_dir);
- Some(output.stdout.trim().to_string())
- }
- Ok(output) => {
- warn!(
- "session compaction summary call failed: {}",
- output.stderr
- );
- None
- }
- Err(e) => {
- warn!("session compaction summary call error: {}", e);
- None
- }
- }
-}
-
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
@@ -3144,75 +2902,4 @@ mod tests {
assert!(!history, "history should default to false");
}
- // -- Pi session JSONL content extraction ---------------------------------
-
- #[test]
- fn test_pi_session_content_extraction_array_format() {
- // Pi stores message content as an array of blocks
- let jsonl = r#"{"type":"message","message":{"role":"user","content":[{"type":"text","text":"hello world"}]}}"#;
- let evt: serde_json::Value = serde_json::from_str(jsonl).unwrap();
-
- let is_message = evt.get("type").and_then(|t| t.as_str()) == Some("message");
- assert!(is_message);
-
- let role = evt
- .get("message")
- .and_then(|m| m.get("role"))
- .and_then(|r| r.as_str());
- assert_eq!(role, Some("user"));
-
- let content = evt
- .get("message")
- .and_then(|m| m.get("content"))
- .and_then(|c| {
- if let Some(arr) = c.as_array() {
- let parts: Vec<&str> = arr
- .iter()
- .filter(|item| {
- item.get("type").and_then(|t| t.as_str()) == Some("text")
- })
- .filter_map(|item| item.get("text").and_then(|t| t.as_str()))
- .collect();
- Some(parts.join("\n"))
- } else {
- c.as_str().map(|s| s.to_string())
- }
- });
- assert_eq!(content, Some("hello world".to_string()));
- }
-
- #[test]
- fn test_pi_session_content_skips_thinking_blocks() {
- // Assistant messages may have thinking + text blocks; only text should be extracted
- let jsonl = r#"{"type":"message","message":{"role":"assistant","content":[{"type":"thinking","thinking":"let me think..."},{"type":"text","text":"the answer is 42"}]}}"#;
- let evt: serde_json::Value = serde_json::from_str(jsonl).unwrap();
-
- let content = evt
- .get("message")
- .and_then(|m| m.get("content"))
- .and_then(|c| {
- if let Some(arr) = c.as_array() {
- let parts: Vec<&str> = arr
- .iter()
- .filter(|item| {
- item.get("type").and_then(|t| t.as_str()) == Some("text")
- })
- .filter_map(|item| item.get("text").and_then(|t| t.as_str()))
- .collect();
- Some(parts.join("\n"))
- } else {
- c.as_str().map(|s| s.to_string())
- }
- });
- assert_eq!(content, Some("the answer is 42".to_string()));
- }
-
- #[test]
- fn test_pi_session_ignores_non_message_events() {
- // session, model_change, tool_use etc. should be ignored
- let jsonl = r#"{"type":"session","version":3,"id":"abc","timestamp":"2026-01-01T00:00:00Z","cwd":"/tmp"}"#;
- let evt: serde_json::Value = serde_json::from_str(jsonl).unwrap();
- let is_message = evt.get("type").and_then(|t| t.as_str()) == Some("message");
- assert!(!is_message);
- }
}