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(pi): pin and repair the required subagents extension (#7633)

Louis Beaumont91f1a17fea35
diff --git a/apps/screenpipe-app-tauri/scripts/eval-pi-extensions.ts b/apps/screenpipe-app-tauri/scripts/eval-pi-extensions.ts
index 70e2f194b..9bb114c78 100644
--- a/apps/screenpipe-app-tauri/scripts/eval-pi-extensions.ts
+++ b/apps/screenpipe-app-tauri/scripts/eval-pi-extensions.ts
@@ -1,7 +1,7 @@
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com
-// Real pinned Pi + the published extension that reproduced the support error.
+// Real pinned Pi repairing a previously installed incompatible extension.
// Only the model response is synthetic; all installs/config live in a temp dir.
import assert from "node:assert/strict";
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
@@ -13,6 +13,11 @@ const config = join(root, "pi-config");
const runtime = join(root, "pi-agent");
const project = join(root, "project");
const source = await readFile(resolve(import.meta.dir, "../../../crates/screenpipe-core/src/agents/pi.rs"), "utf8");
+const packageSource = source.match(/REQUIRED_PI_EXTENSION_PACKAGE: &str = "([^"]+)"/)![1];
+const extensionVersion = packageSource.split("@").at(-1)!;
+const brokenVersion = "0.78.0"; // Uses toToolDeclaration, absent from Pi 0.84.1.
+const configuredPackage = process.argv.includes("--incompatible-package")
+ ? `npm:pi-subagents@${brokenVersion}` : packageSource;
const version = source.match(/PI_PACKAGE: &str = "@earendil-works\/pi-coding-agent@([^"]+)"/)![1];
// Read the production spawn policy so removing it also fails the runtime eval.
const isolation = source.slice(source.indexOf("pub fn apply_pi_isolation_env("), source.indexOf("/// Marker file recording"));
@@ -62,13 +67,13 @@ try {
for (const dir of [runtime, join(config, "npm"), project]) await mkdir(dir, { recursive: true });
for (const [dir, dependencies] of [
[runtime, { "@earendil-works/pi-coding-agent": version }],
- [join(config, "npm"), { "pi-subagents": "0.70.0" }],
+ [join(config, "npm"), { "pi-subagents": brokenVersion }],
] as const) {
await writeFile(join(dir, "package.json"), JSON.stringify({ type: "module", dependencies }));
const install = await run([process.execPath, "install", "--ignore-scripts"], dir);
assert.equal(install.code, 0, install.stderr);
}
- await writeFile(join(config, "settings.json"), JSON.stringify({ packages: ["npm:[email protected]"], retry: { enabled: false } }));
+ await writeFile(join(config, "settings.json"), JSON.stringify({ packages: [configuredPackage], npmCommand: [process.execPath], retry: { enabled: false } }));
await writeFile(join(config, "models.json"), JSON.stringify({ providers: { eval: {
baseUrl: `http://127.0.0.1:${server.port}/v1`, api: "openai-completions", apiKey: "fictional-eval-key",
models: [{ id: "eval", name: "Eval", reasoning: false, input: ["text"], contextWindow: 128000, maxTokens: 256,
@@ -76,7 +81,8 @@ try {
} } }));
const cli = join(runtime, "node_modules/@earendil-works/pi-coding-agent/dist/cli.js");
const args = [process.execPath, cli, "--provider", "eval", "--model", "eval", "--no-session", "--no-skills", "--no-prompt-templates", "--no-context-files"];
- for (const mode of ["rpc", "json"]) {
+ // A scheduled run must recover before anyone opens interactive chat.
+ for (const mode of ["json", "rpc"]) {
const result = await run([...args, "--mode", mode, ...(mode === "json" ? ["--print", "Say extension startup works."] : [])], project, mode === "rpc");
assert.doesNotMatch(result.stderr, /Failed to load extension/, `${mode}: ${result.stderr}`);
const events = result.stdout.split("\n").filter(line => line.startsWith("{")).map(line => JSON.parse(line));
@@ -86,7 +92,9 @@ try {
if (mode === "json") assert.equal(result.code, 0, result.stderr);
assert(requests.at(-1)?.tools?.some(tool => tool.function.name === "subagent"), `${mode}: subagent tool missing`);
assert(requests.at(-1)?.tools?.some(tool => tool.function.name === "bg_wait"), `${mode}: bg_wait tool missing`);
- console.log(`PASS ${mode}: published pi-subagents loaded, tools available, assistant response completed`);
+ const installed = JSON.parse(await readFile(join(config, "npm/node_modules/pi-subagents/package.json"), "utf8"));
+ assert.equal(installed.version, extensionVersion, `${mode}: incompatible package was not replaced`);
+ console.log(`PASS ${mode}: pi-subagents ${brokenVersion} repaired to ${extensionVersion}, tools available, assistant response completed`);
}
// A real extension error must still report its originating cause.
const broken = join(project, "broken.ts");
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 859f53ebd..38b92316e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -8,7 +8,7 @@
use screenpipe_core::agents::pi::{
apply_custom_provider_compat, screenpipe_cloud_models, PI_AI_PACKAGE, PI_NAMESPACE_DIR,
- PI_PACKAGE, SCREENPIPE_API_URL, TINFOIL_SDK_VERSION,
+ PI_PACKAGE, REQUIRED_PI_EXTENSION_PACKAGE, SCREENPIPE_API_URL, TINFOIL_SDK_VERSION,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
@@ -83,7 +83,6 @@ const TITLE_SESSION_PREFIX: &str = "__title:";
/// Reserved namespace for temporary side chats. Keep in sync with
/// `lib/chat/ephemeral-side-conversation.ts`.
const EPHEMERAL_SIDE_CONVERSATION_PREFIX: &str = "temporary-side-chat-";
-const REQUIRED_PI_EXTENSION_PACKAGE: &str = "npm:pi-subagents";
const CONVERSATION_HISTORY_OPEN: &str = "<conversation_history>";
const CONVERSATION_HISTORY_CLOSE: &str = "</conversation_history>";
const PI_INSTALL_ARGS: [&str; 2] = ["install", "--ignore-scripts"];
@@ -5338,45 +5337,9 @@ fn is_required_pi_extension_package_source(source: &str) -> bool {
.is_some_and(|name| name.eq_ignore_ascii_case("pi-subagents"))
}
-fn normalize_required_pi_extension_setting(
- settings: &mut serde_json::Value,
-) -> Result<bool, String> {
- if !settings.is_object() {
- *settings = json!({});
- }
- let obj = settings
- .as_object_mut()
- .ok_or_else(|| "Pi settings must be a JSON object".to_string())?;
- let packages = obj
- .entry("packages".to_string())
- .or_insert_with(|| json!([]));
- if !packages.is_array() {
- *packages = json!([]);
- }
- let entries = packages
- .as_array_mut()
- .ok_or_else(|| "Pi packages must be a JSON array".to_string())?;
- let already_canonical = entries
- .iter()
- .filter(|package| {
- package_source_string(package).is_some_and(is_required_pi_extension_package_source)
- })
- .count()
- == 1
- && entries
- .iter()
- .any(|package| package.as_str() == Some(REQUIRED_PI_EXTENSION_PACKAGE));
- if already_canonical {
- return Ok(false);
- }
- // Replace version-pinned or filtered object entries with the canonical,
- // unrestricted package. A filtered `{ source, extensions: [...] }` entry
- // can otherwise list pi-subagents while silently hiding its tool.
- entries.retain(|package| {
- !package_source_string(package).is_some_and(is_required_pi_extension_package_source)
- });
- entries.push(json!(REQUIRED_PI_EXTENSION_PACKAGE));
- Ok(true)
+fn normalize_required_pi_extension_setting(settings: &mut serde_json::Value) -> Result<bool, String> {
+ screenpipe_core::agents::pi::normalize_required_pi_extension_setting(settings)
+ .map_err(|error| error.to_string())
}
fn ensure_required_pi_extension_setting() -> Result<(), String> {
@@ -5524,6 +5487,9 @@ fn valid_github_path_part(part: &str) -> bool {
}
fn pi_package_source_looks_installed(source: &str) -> bool {
+ if is_required_pi_extension_package_source(source) {
+ return get_pi_config_dir().is_ok_and(|dir| required_pi_extension_is_installed(&dir));
+ }
if let Some(package_name) = npm_package_name_from_source(source) {
if let Ok(config_dir) = get_pi_config_dir() {
return config_dir
@@ -5642,18 +5608,34 @@ async fn run_pi_package_command(args: Vec<String>) -> Result<(), String> {
.map_err(|e| format!("Pi package command panicked: {}", e))?
}
+fn required_pi_extension_is_installed(config_dir: &Path) -> bool {
+ let manifest = config_dir.join("npm/node_modules/pi-subagents/package.json");
+ let installed = std::fs::read(manifest)
+ .ok()
+ .and_then(|bytes| serde_json::from_slice::<serde_json::Value>(&bytes).ok());
+ installed
+ .as_ref()
+ .and_then(|value| value.get("version"))
+ .and_then(|v| v.as_str())
+ == REQUIRED_PI_EXTENSION_PACKAGE.strip_prefix("npm:pi-subagents@")
+}
+
async fn ensure_required_pi_extension_package() -> Result<(), String> {
let lock = REQUIRED_PI_PACKAGE_INSTALL_LOCK.get_or_init(|| Mutex::new(()));
let _guard = lock.lock().await;
ensure_required_pi_extension_setting()?;
- if pi_package_source_looks_installed(REQUIRED_PI_EXTENSION_PACKAGE) {
+ if required_pi_extension_is_installed(&get_pi_config_dir()?) {
return Ok(());
}
run_pi_package_command(vec![
"install".to_string(),
REQUIRED_PI_EXTENSION_PACKAGE.to_string(),
])
- .await
+ .await?;
+ if !required_pi_extension_is_installed(&get_pi_config_dir()?) {
+ return Err(format!("Required Pi extension was not installed: {}", REQUIRED_PI_EXTENSION_PACKAGE));
+ }
+ Ok(())
}
async fn stop_idle_pi_sessions_for_package_change(state: &PiState) -> Result<(), String> {
@@ -5696,6 +5678,11 @@ pub async fn pi_install_extension_package(
source: String,
) -> Result<Vec<PiExtensionPackage>, String> {
let source = validate_pi_extension_package_source(&source)?;
+ let source = if is_required_pi_extension_package_source(&source) {
+ REQUIRED_PI_EXTENSION_PACKAGE.to_string()
+ } else {
+ source
+ };
stop_idle_pi_sessions_for_package_change(&state).await?;
run_pi_package_command(vec!["install".to_string(), source]).await?;
stop_idle_pi_sessions_for_package_change(&state).await?;
@@ -6634,11 +6621,26 @@ mod tests {
assert!(super::normalize_required_pi_extension_setting(&mut settings).unwrap());
assert_eq!(
settings["packages"],
- json!(["npm:other-tool", "npm:pi-subagents"])
+ json!(["npm:other-tool", super::REQUIRED_PI_EXTENSION_PACKAGE])
);
assert!(!super::normalize_required_pi_extension_setting(&mut settings).unwrap());
}
+ #[test]
+ fn required_subagents_install_check_rejects_incompatible_or_broken_manifests() {
+ let dir = tempfile::tempdir().unwrap();
+ let package_dir = dir.path().join("npm/node_modules/pi-subagents");
+ std::fs::create_dir_all(&package_dir).unwrap();
+ assert!(!super::required_pi_extension_is_installed(dir.path()));
+ for manifest in [r#"{"version":"0.78.0"}"#, "invalid", "{}"] {
+ std::fs::write(package_dir.join("package.json"), manifest).unwrap();
+ assert!(!super::required_pi_extension_is_installed(dir.path()));
+ }
+ let version = super::REQUIRED_PI_EXTENSION_PACKAGE.rsplit('@').next().unwrap();
+ std::fs::write(package_dir.join("package.json"), json!({"version":version}).to_string()).unwrap();
+ assert!(super::required_pi_extension_is_installed(dir.path()));
+ }
+
#[test]
fn lists_string_and_filtered_object_pi_packages() {
let settings = json!({
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rs
index f7cbe49bd..4d946c0c5 100644
--- a/crates/screenpipe-core/src/agents/pi.rs
+++ b/crates/screenpipe-core/src/agents/pi.rs
@@ -95,6 +95,9 @@ fn user_skill_fingerprint(root: &Path) -> std::io::Result<String> {
pub const PI_PACKAGE: &str = "@earendil-works/[email protected]";
pub const PI_AI_PACKAGE: &str = "@earendil-works/[email protected]";
+// Keep the required extension compatible with the pinned Pi host. Newer
+// pi-subagents releases use host APIs that 0.84.1 does not export.
+pub const REQUIRED_PI_EXTENSION_PACKAGE: &str = "npm:[email protected]";
pub const TINFOIL_SDK_VERSION: &str = "1.2.1";
pub const PI_NAMESPACE_DIR: &str = "@earendil-works";
pub const SCREENPIPE_API_URL: &str = "https://api.screenpipe.com/v1";
@@ -2739,15 +2742,16 @@ pub fn pi_config_dir() -> Result<PathBuf> {
_ => crate::paths::default_screenpipe_data_dir().join("pi-config"),
};
seed_pi_config_from_global(&dir);
- ensure_pi_compaction_default(&dir)?;
+ ensure_pi_settings_defaults(&dir)?;
Ok(dir)
}
+/// Maintain compatible managed packages and conservative compaction defaults.
/// Pi's 20k recent-history default leaves too little room for the system prompt,
/// skill instructions and a summary on 32k models. Use its standard setting for
/// both chat and Pipes. Explicit user settings (including disabled compaction)
/// remain authoritative; no model selection or summarization logic lives here.
-fn ensure_pi_compaction_default(config_dir: &Path) -> Result<()> {
+fn ensure_pi_settings_defaults(config_dir: &Path) -> Result<()> {
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = LOCK
.lock()
@@ -2765,14 +2769,37 @@ fn ensure_pi_compaction_default(config_dir: &Path) -> Result<()> {
let Some(object) = settings.as_object_mut() else {
return Ok(());
};
+ let mut changed = false;
let compaction = object.entry("compaction").or_insert_with(|| json!({}));
- let Some(compaction) = compaction.as_object_mut() else {
- return Ok(());
- };
- if compaction.contains_key("keepRecentTokens") {
+ if let Some(compaction) = compaction.as_object_mut() {
+ if !compaction.contains_key("keepRecentTokens") {
+ compaction.insert("keepRecentTokens".into(), json!(8192));
+ changed = true;
+ }
+ }
+ // Scheduled tasks use this path without opening desktop chat. Pi's package
+ // resolver replaces a mismatched installed version before loading extensions.
+ // Do not add optional packages to profiles that never enabled subagents.
+ if settings
+ .get("packages")
+ .and_then(|v| v.as_array())
+ .is_some_and(|packages| {
+ packages.iter().any(|package| {
+ pi_extension_package_source(package).is_some_and(is_required_pi_extension_source)
+ })
+ })
+ {
+ changed |= normalize_required_pi_extension_setting(&mut settings)?;
+ if settings.get("npmCommand").is_none() {
+ if let Some(bun) = find_bun_executable() {
+ settings["npmCommand"] = json!([bun]);
+ changed = true;
+ }
+ }
+ }
+ if !changed {
return Ok(());
}
- compaction.insert("keepRecentTokens".into(), json!(8192));
std::fs::create_dir_all(config_dir)?;
let mut temporary = tempfile::NamedTempFile::new_in(config_dir)?;
serde_json::to_writer_pretty(temporary.as_file_mut(), &settings)?;
@@ -2780,6 +2807,55 @@ fn ensure_pi_compaction_default(config_dir: &Path) -> Result<()> {
Ok(())
}
+fn pi_extension_package_source(package: &serde_json::Value) -> Option<&str> {
+ package
+ .as_str()
+ .or_else(|| package.get("source").and_then(|v| v.as_str()))
+}
+
+fn is_required_pi_extension_source(source: &str) -> bool {
+ pi_package_source_matches(&source.to_ascii_lowercase(), "pi-subagents")
+}
+
+/// Register the tested subagent package without dropping unrelated settings.
+pub fn normalize_required_pi_extension_setting(settings: &mut serde_json::Value) -> Result<bool> {
+ if !settings.is_object() {
+ *settings = json!({});
+ }
+ let obj = settings
+ .as_object_mut()
+ .ok_or_else(|| anyhow!("Pi settings must be a JSON object"))?;
+ let packages = obj
+ .entry("packages".to_string())
+ .or_insert_with(|| json!([]));
+ if !packages.is_array() {
+ *packages = json!([]);
+ }
+ let entries = packages
+ .as_array_mut()
+ .ok_or_else(|| anyhow!("Pi packages must be a JSON array"))?;
+ let already_canonical = entries
+ .iter()
+ .filter(|package| {
+ pi_extension_package_source(package).is_some_and(is_required_pi_extension_source)
+ })
+ .count()
+ == 1
+ && entries
+ .iter()
+ .any(|package| package.as_str() == Some(REQUIRED_PI_EXTENSION_PACKAGE));
+ if already_canonical {
+ return Ok(false);
+ }
+ // Keep the tested version and all of its tools enabled. A filtered object
+ // can otherwise list pi-subagents while silently hiding its tool.
+ entries.retain(|package| {
+ !pi_extension_package_source(package).is_some_and(is_required_pi_extension_source)
+ });
+ entries.push(json!(REQUIRED_PI_EXTENSION_PACKAGE));
+ Ok(true)
+}
+
fn pi_package_source_matches(source: &str, package_name: &str) -> bool {
let Some(spec) = source.trim().strip_prefix("npm:") else {
return false;
@@ -5656,7 +5732,7 @@ mod tests {
r#"{"theme":"dark","compaction":{"enabled":false,"reserveTokens":12000}}"#,
)
.unwrap();
- ensure_pi_compaction_default(dir.path()).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
let settings: serde_json::Value =
serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
assert_eq!(settings["theme"], "dark");
@@ -5665,7 +5741,7 @@ mod tests {
json!({"enabled":false,"reserveTokens":12000,"keepRecentTokens":8192})
);
let original = std::fs::read(&path).unwrap();
- ensure_pi_compaction_default(dir.path()).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), original);
}
@@ -5680,15 +5756,53 @@ mod tests {
r#"{"compaction":null}"#,
] {
std::fs::write(&path, original).unwrap();
- ensure_pi_compaction_default(dir.path()).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
}
}
+ #[test]
+ fn pi_subagents_pin_migrates_existing_settings_before_scheduled_runs() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("settings.json");
+ for package in [
+ json!("npm:pi-subagents"),
+ json!("npm:[email protected]"),
+ json!({"source":"npm:[email protected]", "extensions":[]}),
+ ] {
+ let mut settings = json!({
+ "theme":"dark", "npmCommand":["custom-bun"],
+ "compaction":{"keepRecentTokens":24000},
+ "packages":["npm:other-extension", package, "npm:[email protected]"]
+ });
+ std::fs::write(&path, serde_json::to_vec(&settings).unwrap()).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
+ settings["packages"] = json!(["npm:other-extension", REQUIRED_PI_EXTENSION_PACKAGE]);
+ let migrated: serde_json::Value =
+ serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
+ assert_eq!(migrated, settings);
+ let original = std::fs::read(&path).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
+ assert_eq!(std::fs::read(&path).unwrap(), original);
+ }
+ }
+
+ #[test]
+ fn pi_subagents_pin_registers_fresh_desktop_profiles() {
+ let mut settings = json!({"theme":"dark", "packages":["npm:other-extension"]});
+ assert!(normalize_required_pi_extension_setting(&mut settings).unwrap());
+ assert_eq!(
+ settings["packages"],
+ json!(["npm:other-extension", REQUIRED_PI_EXTENSION_PACKAGE])
+ );
+ assert_eq!(settings["theme"], "dark");
+ assert!(!normalize_required_pi_extension_setting(&mut settings).unwrap());
+ }
+
#[test]
fn pi_compaction_default_initializes_fresh_config() {
let dir = tempfile::tempdir().unwrap();
- ensure_pi_compaction_default(dir.path()).unwrap();
+ ensure_pi_settings_defaults(dir.path()).unwrap();
let settings: serde_json::Value =
serde_json::from_slice(&std::fs::read(dir.path().join("settings.json")).unwrap())
.unwrap();