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(chat): compact context between tool calls (#7083)

Ezra Ellette7e25eb09e2df
diff --git a/.github/workflows/test-frontend.yml b/.github/workflows/test-frontend.yml
index 692e65114..d88868894 100644
--- a/.github/workflows/test-frontend.yml
+++ b/.github/workflows/test-frontend.yml
@@ -8,6 +8,7 @@ on:
branches: [main]
paths:
- 'crates/screenpipe-core/assets/extensions/skill-learning*'
+ - 'crates/screenpipe-core/assets/pi-context-compaction*'
- 'crates/screenpipe-core/assets/skills/**'
- 'crates/screenpipe-core/src/starter_skills.rs'
- 'apps/screenpipe-app-tauri/**/*.ts'
@@ -21,6 +22,7 @@ on:
pull_request:
paths:
- 'crates/screenpipe-core/assets/extensions/skill-learning*'
+ - 'crates/screenpipe-core/assets/pi-context-compaction*'
- 'crates/screenpipe-core/assets/skills/**'
- 'crates/screenpipe-core/src/starter_skills.rs'
- 'apps/screenpipe-app-tauri/**/*.ts'
@@ -76,6 +78,9 @@ jobs:
# must pass for CI green.
run: bun run test:bun
+ - name: Pi context compaction runtime eval
+ run: bun scripts/eval-pi-compaction.ts
+
- name: Biome lint (root config)
# Biome auto-discovers ../../biome.json from the repo root. Running
# from there avoids the bun-script `cd` quoting headache.
diff --git a/Cargo.lock b/Cargo.lock
index a84f896e0..6f5ea5be4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2671,6 +2671,15 @@ dependencies = [
"thousands",
]
+[[package]]
+name = "diffy"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b545b8c50194bdd008283985ab0b31dba153cfd5b3066a92770634fbc0d7d291"
+dependencies = [
+ "nu-ansi-term",
+]
+
[[package]]
name = "digest"
version = "0.10.7"
@@ -8803,6 +8812,7 @@ dependencies = [
"core-foundation 0.10.1",
"core-graphics",
"cron",
+ "diffy",
"dirs 6.0.0",
"ffmpeg-sidecar",
"futures",
diff --git a/apps/screenpipe-app-tauri/knip.json b/apps/screenpipe-app-tauri/knip.json
index 01ebfab65..0e92e0468 100644
--- a/apps/screenpipe-app-tauri/knip.json
+++ b/apps/screenpipe-app-tauri/knip.json
@@ -11,6 +11,7 @@
"e2e/mock-updates/windows-persistence-server.ts",
"e2e/scripts/verify-spotlight-exclusion.ts",
"scripts/timeline-parity-export.ts",
+ "scripts/eval-pi-compaction.ts",
"scripts/eval-workflow-assistant.ts",
"scripts/eval-workflow-discovery.ts",
"scripts/eval-workflow-guide.ts",
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/context-pruning.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/context-pruning.test.ts
index de7960e18..7f4e58f0e 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/context-pruning.test.ts
+++ b/apps/screenpipe-app-tauri/lib/__tests__/context-pruning.test.ts
@@ -31,7 +31,6 @@ import extension, {
maxMessageChars,
normalizeContextOverflowError,
resolveContextWindowTokens,
- shouldProactivelyCompact,
} from "@screenpipe-ext/context-pruning";
// ── helpers ────────────────────────────────────────────────────────────
@@ -46,7 +45,6 @@ function registerExtension() {
};
extension(pi as any);
return {
- agent_settled: handlers["agent_settled"][0],
context: handlers["context"][0],
message_end: handlers["message_end"][0],
tool_result: handlers["tool_result"][0],
@@ -104,64 +102,6 @@ describe("provider overflow normalization", () => {
});
});
-describe("proactive compaction", () => {
- it("uses 70% as the inclusive quality threshold", () => {
- expect(shouldProactivelyCompact({ percent: 69.9 })).toBe(false);
- expect(shouldProactivelyCompact({ percent: 70 })).toBe(true);
- expect(shouldProactivelyCompact({ percent: 85 })).toBe(true);
- expect(shouldProactivelyCompact({ percent: null })).toBe(false);
- expect(shouldProactivelyCompact(undefined)).toBe(false);
- });
-
- it("compacts after a successful settled turn reaches 70%", async () => {
- const handlers = registerExtension();
- await handlers.message_end({
- type: "message_end",
- message: { role: "assistant", content: [], stopReason: "stop" },
- });
- const compactCalls: any[] = [];
-
- await handlers.agent_settled(
- { type: "agent_settled" },
- {
- getContextUsage: () => ({ tokens: 140_000, contextWindow: 200_000, percent: 70 }),
- hasPendingMessages: () => false,
- compact: (options: any) => compactCalls.push(options),
- },
- );
-
- expect(compactCalls).toHaveLength(1);
- expect(compactCalls[0].onComplete).toBeTypeOf("function");
- expect(compactCalls[0].onError).toBeTypeOf("function");
- });
-
- it("does not compact below 70%, after an error, or with queued work", async () => {
- for (const testCase of [
- { stopReason: "stop", percent: 69.9, pending: false },
- { stopReason: "error", percent: 90, pending: false },
- { stopReason: "stop", percent: 90, pending: true },
- ]) {
- const handlers = registerExtension();
- await handlers.message_end({
- type: "message_end",
- message: { role: "assistant", content: [], stopReason: testCase.stopReason },
- });
- const compactCalls: any[] = [];
-
- await handlers.agent_settled(
- { type: "agent_settled" },
- {
- getContextUsage: () => ({ percent: testCase.percent }),
- hasPendingMessages: () => testCase.pending,
- compact: (options: any) => compactCalls.push(options),
- },
- );
-
- expect(compactCalls).toHaveLength(0);
- }
- });
-});
-
function bigHistoryMessage(turnChars: number, turns: number, question: string) {
const body = Array.from({ length: turns }, () => "z".repeat(turnChars)).join("\n");
return `<conversation_history>\n${body}\n</conversation_history>\n\n${question}`;
diff --git a/apps/screenpipe-app-tauri/scripts/eval-pi-compaction.ts b/apps/screenpipe-app-tauri/scripts/eval-pi-compaction.ts
new file mode 100644
index 000000000..81ba72695
--- /dev/null
+++ b/apps/screenpipe-app-tauri/scripts/eval-pi-compaction.ts
@@ -0,0 +1,30 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+import { mkdtemp, readFile, writeFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+
+const repo = resolve(import.meta.dir, "../../..");
+const root = await mkdtemp(join(tmpdir(), "screenpipe-pi-compaction-"));
+async function run(args: string[], cwd = root, env = process.env) {
+ const child = Bun.spawn(args, { cwd, env, stdout: "inherit", stderr: "inherit" });
+ const code = await child.exited;
+ if (code !== 0) throw new Error(`${args.join(" ")} exited ${code}`);
+}
+try {
+ const source = await readFile(join(repo, "crates/screenpipe-core/src/agents/pi.rs"), "utf8");
+ const version = source.match(/PI_PACKAGE: &str = "@earendil-works\/pi-coding-agent@([^"]+)"/)![1];
+ await writeFile(join(root, "package.json"), JSON.stringify({ type: "module", dependencies: {
+ "@earendil-works/pi-coding-agent": version, "@earendil-works/pi-ai": version,
+ } }));
+ await run([process.execPath, "install", "--ignore-scripts"]);
+ if (!process.argv.includes("--baseline")) {
+ await run(["git", "apply", join(repo, "crates/screenpipe-core/assets/pi-context-compaction.patch")],
+ join(root, "node_modules/@earendil-works/pi-coding-agent"));
+ }
+ await run([process.execPath, "test", join(repo, "crates/screenpipe-core/assets/pi-context-compaction.test.ts")], root,
+ { ...process.env, SCREENPIPE_TEST_PI_DIR: root });
+} finally {
+ await rm(root, { recursive: true, force: true });
+}
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index e258e6c14..bc71d43ff 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -3377,6 +3377,15 @@ dependencies = [
"cipher 0.5.2",
]
+[[package]]
+name = "diffy"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b545b8c50194bdd008283985ab0b31dba153cfd5b3066a92770634fbc0d7d291"
+dependencies = [
+ "nu-ansi-term",
+]
+
[[package]]
name = "digest"
version = "0.10.7"
@@ -11494,6 +11503,7 @@ dependencies = [
"core-foundation 0.10.1",
"core-graphics 0.24.0",
"cron",
+ "diffy",
"dirs 6.0.0",
"ffmpeg-sidecar",
"futures",
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 7000c421b..a539dcd08 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -1542,7 +1542,9 @@ fn verify_pi_package_install(install_dir: &Path) -> Result<(), String> {
"Pi install completed but dependency verification failed: {}",
error
)),
- None => Ok(()),
+ None => screenpipe_core::agents::pi_compaction::ensure(install_dir).map_err(|error| {
+ format!("Pi install completed but dependency verification failed: compaction: {error}")
+ }),
}
}
@@ -3193,6 +3195,8 @@ pub async fn pi_start_inner(
}
};
+ screenpipe_core::agents::pi_compaction::ensure_for_entrypoint(Path::new(&pi_path))
+ .map_err(|error| format!("Failed to prepare Pi compaction: {error}"))?;
let bun_path = find_bun_executable().unwrap_or_else(|| "NOT FOUND".to_string());
info!(
"Starting {} from {} in dir: {} with provider: {} model: {} bun: {}",
@@ -6057,6 +6061,14 @@ pub fn ensure_pi_installed_background() {
!pkg_contents.is_empty() && !pkg_contents.contains("@anthropic-ai/sdk");
let needs_upgrade = !is_local_pi_version_current(&install_dir);
+ if !needs_upgrade {
+ if let Err(error) = screenpipe_core::agents::pi_compaction::ensure(&install_dir) {
+ set_pi_install_error(format!("Failed to prepare Pi compaction: {error}"));
+ PI_INSTALL_DONE.store(true, Ordering::SeqCst);
+ return;
+ }
+ }
+
if needs_lru_fix || needs_anthropic_sdk || needs_upgrade {
if needs_lru_fix {
info!("Pi installed but missing lru-cache overrides — patching");
@@ -8396,6 +8408,6 @@ error: InstallFailed extracting tarball"#;
let pruning = std::fs::read_to_string(ext_dir.join("context-pruning.ts"))
.expect("read seeded context-pruning extension");
assert!(pruning.contains("normalizeContextOverflowError"));
- assert!(pruning.contains("PROACTIVE_COMPACTION_PERCENT = 70"));
+ assert!(!pruning.contains("ctx.compact("));
}
}
diff --git a/crates/screenpipe-core/Cargo.toml b/crates/screenpipe-core/Cargo.toml
index f2fe8731b..c3d44acc1 100644
--- a/crates/screenpipe-core/Cargo.toml
+++ b/crates/screenpipe-core/Cargo.toml
@@ -22,6 +22,7 @@ which = { workspace = true }
ffmpeg-sidecar = { git = "https://github.com/nathanbabcock/ffmpeg-sidecar", rev = "b0f48a514235d1278d860f4a0af66aa9d6e1618d" }
log = { workspace = true }
anyhow = { workspace = true }
+diffy = "0.4"
arc-swap = { workspace = true }
agent-client-protocol = { version = "=1.2.0", features = ["unstable_auth_methods"], optional = true }
diff --git a/crates/screenpipe-core/assets/extensions/context-pruning.ts b/crates/screenpipe-core/assets/extensions/context-pruning.ts
index 5755c8210..e53c01dfb 100644
--- a/crates/screenpipe-core/assets/extensions/context-pruning.ts
+++ b/crates/screenpipe-core/assets/extensions/context-pruning.ts
@@ -7,7 +7,7 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
/**
* Context management extension for screenpipe pipes and chat.
*
- * Five mechanisms that keep pi's existing compaction path effective. The
+ * Four mechanisms that keep pi's existing compaction path effective. The
* `context` hook is pi's `transformContext` slot, which runs before every LLM
* call and whose returned messages are what actually gets sent:
*
@@ -38,9 +38,9 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
* that wording, so normalize it to pi's standard overflow marker before
* pi decides between ordinary retry and compaction.
*
- * 5. `agent_settled` (proactive compaction) — Compact after a successful turn
- * reaches 70% of the configured context window. This protects answer
- * quality before pi's near-limit threshold or provider overflow fallback.
+ * Proactive compaction and retained-history budgeting live in the managed
+ * Pi runtime (pi-context-compaction.patch), at its safe between-tool-step
+ * boundary. Calling ctx.compact here would abort an active agent run.
*/
// A single tool result above this threshold triggers the "too large" feedback.
@@ -81,17 +81,6 @@ const CHARS_PER_TOKEN = 4;
const HISTORY_OPEN = "<conversation_history>";
const HISTORY_CLOSE = "</conversation_history>";
const CONTEXT_SIZE_EXCEEDED = /context size has been exceeded/i;
-const PROACTIVE_COMPACTION_PERCENT = 70;
-
-/** Whether the reported context usage has reached the quality guardrail. */
-export function shouldProactivelyCompact(usage: any): boolean {
- return (
- typeof usage?.percent === "number" &&
- Number.isFinite(usage.percent) &&
- usage.percent >= PROACTIVE_COMPACTION_PERCENT
- );
-}
-
/**
* Mark the provider's generic HTTP 500 overflow wording for pi's existing
* overflow classifier. The original error remains intact for diagnostics and
@@ -215,47 +204,12 @@ export function boundOversizedMessages(messages: any[], contextWindowTokens: num
}
export default function (pi: ExtensionAPI) {
- let lastAssistantSucceeded = false;
- let proactiveCompactionInFlight = false;
-
- // ── 4. Route provider overflow into pi's compact-and-retry path ─────
+ // Route provider overflow into pi's compact-and-retry path.
pi.on("message_end", async (event) => {
- if (event.message?.role === "assistant") {
- lastAssistantSucceeded = event.message.stopReason === "stop";
- }
const message = normalizeContextOverflowError(event.message);
if (message) return { message };
});
- // ── 5. Compact proactively at 70% after a successful settled turn ──
- pi.on("agent_settled", async (_event, ctx) => {
- if (
- !lastAssistantSucceeded ||
- proactiveCompactionInFlight ||
- ctx.hasPendingMessages()
- ) {
- return;
- }
-
- let usage;
- try {
- usage = ctx.getContextUsage();
- } catch {
- return;
- }
- if (!shouldProactivelyCompact(usage)) return;
-
- proactiveCompactionInFlight = true;
- ctx.compact({
- onComplete: () => {
- proactiveCompactionInFlight = false;
- },
- onError: () => {
- proactiveCompactionInFlight = false;
- },
- });
- });
-
// ── 1. Feedback on oversized tool results ──────────────────────────
// Instead of silently truncating, tell the model the result was too
// large so it can retry with better filters (smaller limit, narrower
diff --git a/crates/screenpipe-core/assets/pi-context-compaction.patch b/crates/screenpipe-core/assets/pi-context-compaction.patch
new file mode 100644
index 000000000..3d1624e02
--- /dev/null
+++ b/crates/screenpipe-core/assets/pi-context-compaction.patch
@@ -0,0 +1,123 @@
+--- a/dist/core/agent-session.js
++++ b/dist/core/agent-session.js
+@@ -1,3 +1,7 @@
++// screenpipe — AI that knows everything you've seen, said, or heard
++// https://screenpipe.com
++// screenpipe-context-compaction-v1
++const PROACTIVE_COMPACTION_PERCENT = 70;
+ /**
+ * AgentSession - Core abstraction for agent lifecycle and session management.
+ *
+@@ -279,10 +283,18 @@
+ this.agent.prepareNextTurnWithContext = async (turn, signal) => {
+ const previousSnapshot = await previousPrepareNextTurnWithContext?.(turn, signal);
+ const previousContext = previousSnapshot?.context ?? turn.context;
++ const messagesBefore = this.agent.state.messages;
++ // All tools in this step have finished. Compact here without aborting
++ // the agent, then hand the rebuilt context to the next model call.
++ if (!signal?.aborted && turn.message.stopReason === "toolUse") {
++ await this._checkCompaction(turn.message);
++ }
+ return {
+ ...previousSnapshot,
+ context: {
+ ...previousContext,
++ messages: this.agent.state.messages !== messagesBefore
++ ? this.agent.state.messages : previousContext.messages,
+ systemPrompt: this._systemPromptOverride ?? this._baseSystemPrompt,
+ tools: this.agent.state.tools.slice(),
+ },
+@@ -1374,7 +1386,7 @@
+ }
+ const { model: requestModel, apiKey, headers, env } = await this._getSummarizationRequestAuth(this.model);
+ const pathEntries = this.sessionManager.getBranch();
+- const settings = this.settingsManager.getCompactionSettings();
++ const settings = this._getContextCompactionSettings();
+ const preparation = prepareCompaction(pathEntries, settings);
+ if (!preparation) {
+ // Check why we can't compact
+@@ -1495,6 +1507,31 @@
+ abortBranchSummary() {
+ this._branchSummaryAbortController?.abort();
+ }
++ _getContextCompactionSettings() {
++ const settings = this.settingsManager.getCompactionSettings();
++ const window = this.model?.contextWindow;
++ if (!window || window <= 0) return settings;
++ const messages = this.agent.state.messages;
++ const estimatedHistory = estimateMessagesTokens(messages);
++ const usage = estimateContextTokens(messages);
++ // Provider usage includes the prompt, tool schemas and tokenizer error.
++ // Keep the larger of that observed overhead and the current prompt estimate.
++ const promptTokens = Math.ceil(((this.systemPrompt?.length ?? 0)
++ + JSON.stringify(this.agent.state.tools.map(({ name, description, parameters }) =>
++ ({ name, description, parameters }))).length) / 4);
++ const overhead = Math.max(promptTokens, usage.tokens - estimatedHistory);
++ const outputTokens = Math.min(this.model.maxTokens ?? 0,
++ Math.floor(window * (1 - PROACTIVE_COMPACTION_PERCENT / 100)));
++ const available = Math.max(1, window - overhead - outputTokens);
++ // reserveTokens also sizes Pi's summary request. Allocate a quarter of
++ // the remaining room to that summary, and keep recent history in the rest.
++ const reserveTokens = Math.max(1, Math.min(settings.reserveTokens, Math.floor(available / 4)));
++ // A split turn can need both a history summary (0.8 * reserveTokens)
++ // and a turn-prefix summary (0.5 * reserveTokens).
++ const summaryTokens = Math.ceil(1.3 * reserveTokens);
++ return { ...settings, reserveTokens,
++ keepRecentTokens: Math.max(1, Math.min(settings.keepRecentTokens, available - summaryTokens)) };
++ }
+ /**
+ * Check if compaction is needed and run it.
+ * Called after agent_end and before prompt submission.
+@@ -1508,7 +1545,7 @@
+ * @param skipAbortedCheck If false, include aborted messages (for pre-prompt check). Default: true
+ */
+ async _checkCompaction(assistantMessage, skipAbortedCheck = true) {
+- const settings = this.settingsManager.getCompactionSettings();
++ const settings = this._getContextCompactionSettings();
+ if (!settings.enabled)
+ return false;
+ // Skip if message was aborted (user cancelled) - unless skipAbortedCheck is false
+@@ -1584,7 +1621,10 @@
+ else {
+ contextTokens = directContextTokens;
+ }
+- if (shouldCompact(contextTokens, contextWindow, settings)) {
++ // Include tool results produced since the last model response.
++ contextTokens = Math.max(contextTokens, estimateContextTokens(this.agent.state.messages).tokens);
++ if (contextTokens >= contextWindow * PROACTIVE_COMPACTION_PERCENT / 100 ||
++ shouldCompact(contextTokens, contextWindow, settings)) {
+ return await this._runAutoCompaction("threshold", false);
+ }
+ return false;
+@@ -1593,10 +1633,12 @@
+ * Internal: Run auto-compaction with events.
+ */
+ async _runAutoCompaction(reason, willRetry) {
+- const settings = this.settingsManager.getCompactionSettings();
++ const settings = this._getContextCompactionSettings();
+ let started = false;
++ const parentSignal = this.agent.signal;
++ const abortCompaction = () => this._autoCompactionAbortController?.abort();
+ try {
+- if (!this.model) {
++ if (!this.model || parentSignal?.aborted) {
+ return false;
+ }
+ const { model: requestModel, apiKey, headers, env } = await this._getSummarizationRequestAuth(this.model);
+@@ -1607,6 +1649,8 @@
+ }
+ this._emit({ type: "compaction_start", reason });
+ this._autoCompactionAbortController = new AbortController();
++ parentSignal?.addEventListener("abort", abortCompaction, { once: true });
++ if (parentSignal?.aborted) this._autoCompactionAbortController.abort();
+ started = true;
+ let extensionCompaction;
+ let fromExtension = false;
+@@ -1725,6 +1769,7 @@
+ return false;
+ }
+ finally {
++ parentSignal?.removeEventListener("abort", abortCompaction);
+ this._autoCompactionAbortController = undefined;
+ }
+ }
diff --git a/crates/screenpipe-core/assets/pi-context-compaction.test.ts b/crates/screenpipe-core/assets/pi-context-compaction.test.ts
new file mode 100644
index 000000000..fe2522d05
--- /dev/null
+++ b/crates/screenpipe-core/assets/pi-context-compaction.test.ts
@@ -0,0 +1,157 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+// Run via apps/screenpipe-app-tauri/scripts/eval-pi-compaction.ts. The real
+// pinned SDK runs in a disposable install; only the model and tool are synthetic.
+import { afterEach, expect, test } from "bun:test";
+import { mkdtemp, rm } from "node:fs/promises";
+import { join } from "node:path";
+import { pathToFileURL } from "node:url";
+import { tmpdir } from "node:os";
+
+const install = process.env.SCREENPIPE_TEST_PI_DIR;
+if (!install) throw new Error("Run scripts/eval-pi-compaction.ts to use an isolated Pi runtime");
+const sdk = await import(pathToFileURL(join(install, "node_modules/@earendil-works/pi-coding-agent/dist/index.js")).href);
+const { createAssistantMessageEventStream } = await import(pathToFileURL(join(install, "node_modules/@earendil-works/pi-ai/dist/index.js")).href);
+const roots: string[] = [];
+const sessions: any[] = [];
+afterEach(async () => {
+ for (const session of sessions.splice(0)) session.dispose();
+ for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true });
+});
+
+async function harness(options: { steps?: number; enabled?: boolean; failSummary?: boolean; stopOnSummary?: boolean; steerOnSummary?: boolean } = {}) {
+ const root = await mkdtemp(join(tmpdir(), "screenpipe-compaction-session-"));
+ roots.push(root);
+ const modelDefinition = { id: "test-32k", name: "test-32k", reasoning: false, input: ["text"],
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 32768, maxTokens: 8192 };
+ const runtime = await sdk.ModelRuntime.create({ authPath: join(root, "auth.json"), modelsPath: null,
+ modelsStorePath: join(root, "models-store.json"), refreshOnCreate: false, allowModelNetwork: false });
+ runtime.registerProvider("test", { api: "openai-completions", baseUrl: "http://unused.invalid/v1", apiKey: "synthetic-key", models: [modelDefinition] });
+ const settings = sdk.SettingsManager.inMemory({ compaction: { enabled: options.enabled ?? true }, retry: { enabled: false } });
+ const loader = new sdk.DefaultResourceLoader({ cwd: root, agentDir: root, settingsManager: settings,
+ noExtensions: true, noSkills: true, noPromptTemplates: true, noThemes: true, noContextFiles: true,
+ systemPrompt: "System context. ".repeat(4000) });
+ await loader.reload();
+ const toolCalls: number[] = [];
+ const { session } = await sdk.createAgentSession({ cwd: root, agentDir: root, modelRuntime: runtime,
+ model: runtime.getModel("test", "test-32k"), settingsManager: settings, resourceLoader: loader,
+ thinkingLevel: "off", sessionManager: sdk.SessionManager.create(root, join(root, "sessions")), tools: ["lookup"],
+ customTools: [{ name: "lookup", label: "Lookup", description: "Retrieve the next research result",
+ parameters: { type: "object", properties: { step: { type: "number" } }, required: ["step"] },
+ execute: async (_id: string, args: { step: number }) => {
+ toolCalls.push(args.step);
+ return { content: [{ type: "text", text: `Research result ${args.step}: ` + "evidence ".repeat(670) }], details: {} };
+ } }] });
+ sessions.push(session);
+ const events: any[] = [];
+ const requests: { tokens: number; summary: boolean; corrected: boolean }[] = [];
+ let summaries = 0;
+ let modelCalls = 0;
+ let steered = false;
+ session.subscribe((event: any) => {
+ events.push(event);
+ if (event.type === "compaction_start" && options.steerOnSummary && !steered) {
+ steered = true;
+ void session.steer("USER CORRECTION: preserve my newest instruction");
+ }
+ });
+ session.agent.streamFunction = (_model: any, context: any, requestOptions: any) => {
+ const stream = createAssistantMessageEventStream();
+ const summary = !context.tools?.length;
+ const tokens = Math.ceil((context.systemPrompt?.length ?? 0) / 4)
+ + context.messages.reduce((n: number, message: any) => n + sdk.estimateTokens(message), 0) + (summary ? 0 : 100);
+ requests.push({ tokens, summary, corrected: JSON.stringify(context.messages).includes("USER CORRECTION") });
+ let message: any = { role: "assistant", provider: "test", model: "test-32k", api: "openai-completions",
+ content: [], stopReason: "stop", timestamp: Date.now(),
+ usage: { input: tokens, output: 32, totalTokens: tokens + 32, cacheRead: 0, cacheWrite: 0,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } } };
+ const finish = () => {
+ stream.push(message.stopReason === "error" || message.stopReason === "aborted"
+ ? { type: "error", reason: message.stopReason, error: message }
+ : { type: "done", reason: message.stopReason, message });
+ stream.end(message);
+ };
+ if (requestOptions.signal?.aborted) {
+ message = { ...message, stopReason: "aborted", errorMessage: "Aborted" };
+ finish();
+ return stream;
+ }
+ if (summary) {
+ summaries++;
+ if (options.stopOnSummary) {
+ requestOptions.signal.addEventListener("abort", () => {
+ message = { ...message, stopReason: "aborted", errorMessage: "Aborted" };
+ finish();
+ }, { once: true });
+ void session.abort();
+ return stream;
+ }
+ message.content = [{ type: "text", text: "The user asked to finish the research. Prior lookup results are summarized. Continue with the remaining steps and preserve the user's latest instruction." }];
+ } else {
+ modelCalls++;
+ message.content = toolCalls.length < (options.steps ?? 14)
+ ? [{ type: "toolCall", id: `lookup-${toolCalls.length + 1}`, name: "lookup", arguments: { step: toolCalls.length + 1 } }]
+ : [{ type: "text", text: "Research complete." }];
+ message.stopReason = message.content[0].type === "toolCall" ? "toolUse" : "stop";
+ }
+ if (tokens > 32768 || (summary && options.failSummary)) {
+ message = { ...message, content: [], stopReason: "error", usage: { ...message.usage, input: 0, output: 0, totalTokens: 0 },
+ errorMessage: summary && options.failSummary ? "summary provider failed" : `request (${tokens} tokens) exceeds the available context size (32768 tokens)` };
+ }
+ finish();
+ return stream;
+ };
+ return { session, toolCalls, events, requests, get summaries() { return summaries; }, get modelCalls() { return modelCalls; } };
+}
+
+test("compacts inside one tool sequence before overflow, then persists resumable context", async () => {
+ const h = await harness();
+ await h.session.prompt("Finish all research steps; preserve the original request.");
+ expect(h.toolCalls).toEqual(Array.from({ length: 14 }, (_, i) => i + 1));
+ expect(h.summaries).toBeGreaterThan(0);
+ expect(h.requests.every(request => request.tokens <= 32768)).toBe(true);
+ expect(h.events.filter(event => event.type === "agent_start")).toHaveLength(1);
+ expect(h.events.filter(event => event.type === "agent_end")).toHaveLength(1);
+ expect(h.events.findIndex(event => event.type === "compaction_start")).toBeLessThan(h.events.findIndex(event => event.type === "agent_end"));
+ expect(h.session.messages.at(-1).content[0].text).toBe("Research complete.");
+ const restored = sdk.SessionManager.open(h.session.sessionManager.getSessionFile());
+ expect(restored.getBranch().some((entry: any) => entry.type === "compaction")).toBe(true);
+ expect(restored.buildSessionContext().messages.some((message: any) => message.role === "compactionSummary")).toBe(true);
+}, 20000);
+
+test("delivers a user correction queued during compaction exactly once", async () => {
+ const h = await harness({ steerOnSummary: true });
+ await h.session.prompt("Finish the research.");
+ expect(h.toolCalls).toHaveLength(14);
+ expect(h.requests.some(request => !request.summary && request.corrected)).toBe(true);
+ expect(h.events.filter(event => event.type === "message_end" && event.message.role === "user"
+ && JSON.stringify(event.message.content).includes("USER CORRECTION"))).toHaveLength(1);
+}, 20000);
+
+test("user stop cancels the in-flight summary without continuing tools", async () => {
+ const h = await harness({ stopOnSummary: true });
+ await h.session.prompt("Finish the research.");
+ expect(h.summaries).toBe(1);
+ expect(h.toolCalls.length).toBeLessThan(14);
+ expect(h.session.sessionManager.getBranch().filter((entry: any) => entry.type === "compaction")).toHaveLength(0);
+ expect(h.events.at(-1).type).toBe("agent_settled");
+}, 20000);
+
+test("failed summaries retain the original history", async () => {
+ const h = await harness({ failSummary: true, steps: 6 });
+ await h.session.prompt("Finish the research.");
+ expect(h.summaries).toBeGreaterThan(0);
+ expect(h.session.sessionManager.getBranch().filter((entry: any) => entry.type === "compaction")).toHaveLength(0);
+ expect(h.session.messages.filter((message: any) => message.role === "toolResult")).toHaveLength(h.toolCalls.length);
+ expect(h.events.some(event => event.type === "compaction_end" && event.errorMessage)).toBe(true);
+}, 20000);
+
+test("short chats and explicitly disabled auto-compaction do not compact", async () => {
+ for (const options of [{ steps: 2 }, { steps: 6, enabled: false }]) {
+ const h = await harness(options);
+ await h.session.prompt("Finish the research.");
+ expect(h.summaries).toBe(0);
+ expect(h.toolCalls).toHaveLength(options.steps);
+ }
+}, 20000);
diff --git a/crates/screenpipe-core/src/agents/mod.rs b/crates/screenpipe-core/src/agents/mod.rs
index 6dfc2b565..7dbc83253 100644
--- a/crates/screenpipe-core/src/agents/mod.rs
+++ b/crates/screenpipe-core/src/agents/mod.rs
@@ -18,6 +18,7 @@ pub mod cli_runtime;
pub mod cloud;
mod cloud_context;
pub mod pi;
+pub mod pi_compaction;
pub mod worktree;
use anyhow::Result;
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rs
index 785d88cb6..b7f1b1024 100644
--- a/crates/screenpipe-core/src/agents/pi.rs
+++ b/crates/screenpipe-core/src/agents/pi.rs
@@ -1792,6 +1792,7 @@ impl PiExecutor {
continue_session: bool,
pipe_system_prompt: Option<&str>,
) -> Result<AgentOutput> {
+ super::pi_compaction::ensure_for_entrypoint(Path::new(pi_path))?;
let mut cmd = build_async_command(pi_path);
cmd.current_dir(working_dir);
apply_pi_isolation_env(&mut |k, v| {
@@ -1934,6 +1935,7 @@ impl PiExecutor {
mcp_server_allowlist: Option<&[String]>,
session_owner: Option<&str>,
) -> Result<AgentOutput> {
+ super::pi_compaction::ensure_for_entrypoint(Path::new(pi_path))?;
let mut cmd = build_async_command(pi_path);
cmd.current_dir(working_dir);
apply_pi_isolation_env(&mut |k, v| {
diff --git a/crates/screenpipe-core/src/agents/pi_compaction.rs b/crates/screenpipe-core/src/agents/pi_compaction.rs
new file mode 100644
index 000000000..ed4daba64
--- /dev/null
+++ b/crates/screenpipe-core/src/agents/pi_compaction.rs
@@ -0,0 +1,77 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+//! Backport context compaction fixes to Screenpipe's pinned Pi runtime.
+//! Apply atomically before launch; never patch a user's global Pi installation.
+
+use anyhow::{anyhow, Context, Result};
+use std::io::Write;
+use std::path::Path;
+use std::sync::Mutex;
+
+const PATCH: &str = include_str!("../../assets/pi-context-compaction.patch");
+const MARKER: &str = "// screenpipe-context-compaction-v1\n";
+static PATCH_LOCK: Mutex<()> = Mutex::new(());
+
+pub fn ensure_for_entrypoint(entrypoint: &Path) -> Result<()> {
+ let install_dir = crate::paths::default_screenpipe_data_dir().join("pi-agent");
+ if entrypoint.starts_with(&install_dir) {
+ ensure(&install_dir)?;
+ }
+ Ok(())
+}
+
+fn patched_source(source: &str) -> Result<Option<String>> {
+ if source.contains(MARKER) {
+ return Ok(None);
+ }
+ let patch = diffy::Patch::from_str(PATCH).context("invalid bundled Pi compaction patch")?;
+ diffy::apply(source, &patch)
+ .map(Some)
+ .map_err(|error| anyhow!("Pi runtime does not match the pinned compaction patch: {error}"))
+}
+
+/// `install_dir` is the app-owned `pi-agent` directory, never a global package.
+pub fn ensure(install_dir: &Path) -> Result<()> {
+ let _guard = PATCH_LOCK
+ .lock()
+ .map_err(|_| anyhow!("Pi patch lock poisoned"))?;
+ let runtime =
+ install_dir.join("node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.js");
+ let source = std::fs::read_to_string(&runtime).context("cannot read managed Pi runtime")?;
+ let Some(patched) = patched_source(&source)? else {
+ return Ok(());
+ };
+ let mut output = tempfile::NamedTempFile::new_in(runtime.parent().unwrap())?;
+ output.write_all(patched.as_bytes())?;
+ output.as_file().sync_all()?;
+ output
+ .persist(&runtime)
+ .context("cannot save managed Pi compaction fix")?;
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn already_patched_runtime_is_unchanged() {
+ assert!(patched_source(MARKER).unwrap().is_none());
+ }
+
+ #[test]
+ fn unknown_runtime_is_rejected_without_writing() {
+ let dir = tempfile::tempdir().unwrap();
+ let runtime = dir
+ .path()
+ .join("node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.js");
+ std::fs::create_dir_all(runtime.parent().unwrap()).unwrap();
+ std::fs::write(&runtime, "unrecognized runtime").unwrap();
+ assert!(ensure(dir.path()).is_err());
+ assert_eq!(
+ std::fs::read_to_string(runtime).unwrap(),
+ "unrecognized runtime"
+ );
+ }
+}
diff --git a/packages/sdk/Cargo.lock b/packages/sdk/Cargo.lock
index 99211aea1..9afa64564 100644
--- a/packages/sdk/Cargo.lock
+++ b/packages/sdk/Cargo.lock
@@ -2114,6 +2114,15 @@ dependencies = [
"cipher 0.5.2",
]
+[[package]]
+name = "diffy"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b545b8c50194bdd008283985ab0b31dba153cfd5b3066a92770634fbc0d7d291"
+dependencies = [
+ "nu-ansi-term",
+]
+
[[package]]
name = "digest"
version = "0.10.7"
@@ -6950,6 +6959,7 @@ dependencies = [
"core-foundation",
"core-graphics",
"cron",
+ "diffy",
"dirs 6.0.0",
"ffmpeg-sidecar",
"futures",
diff --git a/packages/sdk/examples/tauri-app/src-tauri/Cargo.lock b/packages/sdk/examples/tauri-app/src-tauri/Cargo.lock
index b927ba958..5b7837662 100644
--- a/packages/sdk/examples/tauri-app/src-tauri/Cargo.lock
+++ b/packages/sdk/examples/tauri-app/src-tauri/Cargo.lock
@@ -2138,6 +2138,15 @@ dependencies = [
"cipher 0.5.2",
]
+[[package]]
+name = "diffy"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b545b8c50194bdd008283985ab0b31dba153cfd5b3066a92770634fbc0d7d291"
+dependencies = [
+ "nu-ansi-term",
+]
+
[[package]]
name = "digest"
version = "0.10.7"
@@ -7076,6 +7085,7 @@ dependencies = [
"core-foundation",
"core-graphics 0.24.0",
"cron",
+ "diffy",
"dirs 6.0.0",
"ffmpeg-sidecar",
"futures",