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): reap idle ACP agent sessions

Louis Beaumonte271f4c9eeda
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/acp-warmup.test.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/acp-warmup.test.ts
index 5cc38e1aa..56844a558 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/acp-warmup.test.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/acp-warmup.test.ts
@@ -12,10 +12,24 @@
* prevent.
*/
-import { describe, expect, it } from "vitest";
+import { act, renderHook, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+const warmupMocks = vi.hoisted(() => ({
+ piStart: vi.fn(),
+}));
+
+vi.mock("@/lib/utils/tauri", () => ({
+ commands: {
+ piStart: warmupMocks.piStart,
+ },
+}));
+
+import { useAcpWarmup } from "../use-acp-warmup";
type WarmupInput = {
enabled: boolean;
+ sessionId: string;
provider?: string;
presetId?: string;
agentId?: string;
@@ -29,11 +43,12 @@ function shouldWarm(input: WarmupInput): boolean {
if (!input.enabled) return false;
if (input.provider !== "acp" || !input.presetId || !input.agentId) return false;
if (input.running || input.startInFlight) return false;
- return input.alreadyAttempted !== `${input.presetId}:${input.agentId}`;
+ return input.alreadyAttempted !== `${input.sessionId}:${input.presetId}:${input.agentId}`;
}
const base: WarmupInput = {
enabled: true,
+ sessionId: "chat-a",
provider: "acp",
presetId: "p1",
agentId: "codex-acp",
@@ -70,20 +85,103 @@ describe("ACP warmup fires exactly when it should", () => {
it("does not retry the same agent after one attempt", () => {
// The send path is the retry, and it owns the error reporting.
- expect(shouldWarm({ ...base, alreadyAttempted: "p1:codex-acp" })).toBe(false);
+ expect(shouldWarm({ ...base, alreadyAttempted: "chat-a:p1:codex-acp" })).toBe(false);
});
it("warms again when the user switches to a different agent", () => {
- expect(shouldWarm({ ...base, agentId: "claude-acp", alreadyAttempted: "p1:codex-acp" })).toBe(true);
+ expect(shouldWarm({ ...base, agentId: "claude-acp", alreadyAttempted: "chat-a:p1:codex-acp" })).toBe(true);
});
it("warms again for the same agent under a different preset", () => {
// A second preset can carry different args/env, so it is a distinct launch.
- expect(shouldWarm({ ...base, presetId: "p2", alreadyAttempted: "p1:codex-acp" })).toBe(true);
+ expect(shouldWarm({ ...base, presetId: "p2", alreadyAttempted: "chat-a:p1:codex-acp" })).toBe(true);
+ });
+
+ it("warms the same preset separately for a different conversation", () => {
+ expect(
+ shouldWarm({
+ ...base,
+ sessionId: "chat-b",
+ alreadyAttempted: "chat-a:p1:codex-acp",
+ }),
+ ).toBe(true);
});
it("needs a resolved agent id before spawning anything", () => {
expect(shouldWarm({ ...base, agentId: undefined })).toBe(false);
expect(shouldWarm({ ...base, presetId: undefined })).toBe(false);
});
+
+ it("keeps the start guard until the ACP handshake call resolves", async () => {
+ warmupMocks.piStart.mockReset();
+ let resolveStart: ((value: any) => void) | undefined;
+ warmupMocks.piStart.mockReturnValue(
+ new Promise((resolve) => {
+ resolveStart = resolve;
+ }),
+ );
+
+ const piStartInFlightRef = { current: false };
+ const piSessionIdRef = { current: "chat-a" };
+ const setPiStarting = vi.fn();
+ const activePreset = {
+ id: "p1",
+ provider: "acp",
+ acpAgent: { id: "github-copilot-cli" },
+ } as any;
+
+ const { rerender } = renderHook(
+ ({ piInfo }) =>
+ useAcpWarmup({
+ enabled: true,
+ activePreset,
+ piInfo: piInfo as any,
+ piStartInFlightRef,
+ piSessionIdRef,
+ piProjectDirForSession: vi.fn(async () => "C:/tmp/chat-a"),
+ buildProviderConfig: vi.fn(() => ({
+ backend: "acp",
+ provider: "acp",
+ model: "github-copilot-cli",
+ url: "",
+ apiKey: null,
+ maxTokens: 4096,
+ maxContextChars: null,
+ systemPrompt: null,
+ } as any)),
+ userToken: null,
+ setPiInfo: vi.fn(),
+ setPiStarting,
+ setRunningConfigFromProviderConfig: vi.fn(),
+ syncThinkingLevelAfterStart: vi.fn(),
+ }),
+ { initialProps: { piInfo: null as any } },
+ );
+
+ await waitFor(() => expect(warmupMocks.piStart).toHaveBeenCalledTimes(1));
+ expect(piStartInFlightRef.current).toBe(true);
+ expect(setPiStarting).toHaveBeenLastCalledWith(true);
+
+ // pi_info reports the process as alive before the ACP handshake finishes.
+ // That liveness update must not clean up the still-pending warmup effect.
+ rerender({
+ piInfo: {
+ running: true,
+ busy: false,
+ sessionId: "chat-a",
+ },
+ });
+ expect(piStartInFlightRef.current).toBe(true);
+ expect(setPiStarting).not.toHaveBeenCalledWith(false);
+
+ await act(async () => {
+ resolveStart?.({
+ status: "ok",
+ data: { running: true, busy: false, sessionId: "chat-a" },
+ });
+ });
+
+ await waitFor(() => expect(piStartInFlightRef.current).toBe(false));
+ expect(setPiStarting).toHaveBeenLastCalledWith(false);
+ });
});
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-acp-warmup.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-acp-warmup.ts
index 889770857..54d3dc435 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-acp-warmup.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-acp-warmup.ts
@@ -66,15 +66,19 @@ export function useAcpWarmup({
const provider = activePreset?.provider;
const presetId = activePreset?.id;
const agentId = activePreset?.acpAgent?.id;
- const running = Boolean(piInfo?.running);
+ const sessionId = piSessionIdRef.current;
+ const runningForSessionRef = useRef(false);
+ runningForSessionRef.current = Boolean(
+ piInfo?.running && (!piInfo.sessionId || piInfo.sessionId === sessionId),
+ );
useEffect(() => {
if (!enabled) return;
if (provider !== "acp" || !presetId || !agentId) return;
// Already up, or the send path is mid-start — either way, leave it alone.
- if (running || piStartInFlightRef.current) return;
+ if (runningForSessionRef.current || piStartInFlightRef.current) return;
- const key = `${presetId}:${agentId}`;
+ const key = `${sessionId}:${presetId}:${agentId}`;
if (attemptedRef.current === key) return;
attemptedRef.current = key;
@@ -89,9 +93,9 @@ export function useAcpWarmup({
try {
const providerConfig = buildProviderConfig(preset);
if (!providerConfig) return;
- const dir = await piProjectDirForSession(piSessionIdRef.current);
+ const dir = await piProjectDirForSession(sessionId);
const result = await commands.piStart(
- piSessionIdRef.current,
+ sessionId,
dir,
userToken ?? null,
providerConfig,
@@ -100,7 +104,7 @@ export function useAcpWarmup({
if (result.status === "ok" && result.data.running) {
setPiInfo(result.data);
setRunningConfigFromProviderConfig(providerConfig);
- syncThinkingLevelAfterStart(piSessionIdRef.current);
+ syncThinkingLevelAfterStart(sessionId);
}
// A non-running result is not reported here. It is usually the
// auth case, which has already surfaced its own card.
@@ -121,9 +125,12 @@ export function useAcpWarmup({
piStartInFlightRef.current = false;
setPiStarting(false);
};
- // `activePreset` identity churns; the id/provider/agent triple is what
- // actually decides whether a different agent needs warming.
- }, [enabled, provider, presetId, agentId, running]);
+ // `activePreset` identity churns; the session/id/provider/agent tuple is
+ // what actually decides whether a different runtime needs warming. Do not
+ // depend on `piInfo.running`: the process becomes alive before ACP finishes
+ // its handshake, and treating that poll result as effect cleanup releases
+ // the start guard while the original `piStart` call is still pending.
+ }, [enabled, provider, presetId, agentId, sessionId]);
// Selecting a different agent makes a previous failure irrelevant.
useEffect(() => {
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.test.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.test.tsx
index a2737cd7b..d56e42f5d 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.test.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.test.tsx
@@ -11,12 +11,20 @@ const runtimeMocks = vi.hoisted(() => ({
foregroundHandler: null as ((envelope: any) => void) | null,
startPipeExecution: vi.fn(),
clearPipeExecution: vi.fn(),
+ piStopIfIdle: vi.fn(async () => ({
+ status: "ok" as const,
+ data: { running: false, busy: false },
+ })),
}));
vi.mock("@tauri-apps/api/event", () => ({
emit: vi.fn(async () => undefined),
}));
+vi.mock("@/lib/utils/tauri", () => ({
+ commands: { piStopIfIdle: runtimeMocks.piStopIfIdle },
+}));
+
vi.mock("@/lib/events/bus", () => ({
mountAgentEventBus: vi.fn(async () => undefined),
onEvicted: vi.fn(() => () => undefined),
@@ -96,6 +104,7 @@ describe("useChatSessionRuntime", () => {
runtimeMocks.foregroundHandler = null;
runtimeMocks.startPipeExecution.mockClear();
runtimeMocks.clearPipeExecution.mockClear();
+ runtimeMocks.piStopIfIdle.mockClear();
useChatStore.setState({
sessions: {},
currentId: null,
@@ -177,6 +186,17 @@ describe("useChatSessionRuntime", () => {
expect(runtimeMocks.clearPipeExecution).toHaveBeenCalled();
});
+ it("releases the idle agent after the chat panel unmounts", async () => {
+ const { unmount } = renderHook(() => useRuntimeHarness());
+
+ unmount();
+
+ await waitFor(() => {
+ expect(runtimeMocks.piStopIfIdle).toHaveBeenCalledTimes(1);
+ expect(runtimeMocks.piStopIfIdle).toHaveBeenCalledWith(SESSION_ID);
+ });
+ });
+
// Regression: a turn that ends without this panel's foreground handler
// observing `agent_end` (duplicate registration, the gap before the handler
// is live, or a completion that lands while the user is on another chat)
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
index 95842f001..4ba92526c 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
@@ -15,6 +15,7 @@ import { useChatStore } from "@/lib/stores/chat-store";
import { handlePiEvent } from "@/lib/stores/pi-event-router";
import { parsePipeSessionId } from "@/lib/events/types";
import type { ContentBlock, Message } from "@/lib/chat/types";
+import { commands } from "@/lib/utils/tauri";
interface UseChatSessionRuntimeOptions {
conversationId: string | null;
@@ -43,6 +44,27 @@ interface UseChatSessionRuntimeOptions {
* the length of one disk write. This is comfortably longer than that window
* and short enough that a stuck composer recovers on its own. */
const TURN_GUARD_HEAL_GRACE_MS = 750;
+const IDLE_AGENT_RELEASE_GRACE_MS = 250;
+const pendingIdleAgentReleases = new Map<string, ReturnType<typeof setTimeout>>();
+
+function cancelIdleAgentRelease(sessionId: string): void {
+ const pending = pendingIdleAgentReleases.get(sessionId);
+ if (!pending) return;
+ clearTimeout(pending);
+ pendingIdleAgentReleases.delete(sessionId);
+}
+
+function scheduleIdleAgentRelease(sessionId: string): void {
+ cancelIdleAgentRelease(sessionId);
+ const timer = setTimeout(() => {
+ if (pendingIdleAgentReleases.get(sessionId) !== timer) return;
+ pendingIdleAgentReleases.delete(sessionId);
+ void commands.piStopIfIdle(sessionId).catch((error) => {
+ console.warn("[Pi] Failed to release idle background session:", error);
+ });
+ }, IDLE_AGENT_RELEASE_GRACE_MS);
+ pendingIdleAgentReleases.set(sessionId, timer);
+}
export function useChatSessionRuntime({
conversationId,
@@ -72,6 +94,15 @@ export function useChatSessionRuntime({
};
}, []);
+ useEffect(() => {
+ if (!conversationId) return;
+ // React Strict Mode and fast route transitions can unmount/remount the same
+ // chat back-to-back. Cancel its delayed release when foreground ownership
+ // returns; otherwise release only after the panel is genuinely gone.
+ cancelIdleAgentRelease(conversationId);
+ return () => scheduleIdleAgentRelease(conversationId);
+ }, [conversationId]);
+
const currentSessionKind = useChatStore((state) =>
state.currentId ? state.sessions[state.currentId]?.kind : undefined,
);
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/pi-event-router.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/pi-event-router.test.ts
index da51cb302..fea0b0457 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/pi-event-router.test.ts
+++ b/apps/screenpipe-app-tauri/lib/__tests__/pi-event-router.test.ts
@@ -20,6 +20,17 @@ import {
setCachedBrowserState,
} from "../browser-state-cache";
+const routerCommandMocks = vi.hoisted(() => ({
+ piStopIfIdle: vi.fn(async () => ({
+ status: "ok" as const,
+ data: { running: false, busy: false },
+ })),
+}));
+
+vi.mock("@/lib/utils/tauri", () => ({
+ commands: { piStopIfIdle: routerCommandMocks.piStopIfIdle },
+}));
+
vi.mock("@/lib/chat-storage", () => ({
listConversations: vi.fn(async () => []),
loadConversationFile: vi.fn(async () => null),
@@ -172,12 +183,17 @@ describe("pi-event-router: status mirroring for backgrounded sessions", () => {
useChatStore.setState({ currentId: "B" });
await handlePiEvent(piEvt("A", { type: "agent_end" }));
expect(useChatStore.getState().sessions.A.status).toBe("idle");
+ await vi.waitFor(() => {
+ expect(routerCommandMocks.piStopIfIdle).toHaveBeenCalledWith("A");
+ });
});
it("stays streaming while agent_end is followed by an automatic retry", async () => {
seed("A", { status: "streaming" });
useChatStore.setState({ currentId: "B" });
await handlePiEvent(piEvt("A", { type: "agent_end", willRetry: true }));
+ await Promise.resolve();
+ expect(routerCommandMocks.piStopIfIdle).not.toHaveBeenCalled();
expect(useChatStore.getState().sessions.A.status).toBe("streaming");
await handlePiEvent(piEvt("A", {
diff --git a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
index a5ecab2a5..81d2ce708 100644
--- a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
+++ b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
@@ -43,6 +43,7 @@
import {
mountAgentEventBus,
registerDefault,
+ hasForegroundHandler,
onTerminated,
onEvicted,
type Unregister,
@@ -90,6 +91,7 @@ import {
import { connectionActionFromToolResult } from "@/components/chat/standalone/hooks/pi-event-handlers";
import { normalizePlanEntries, upsertPlanBlock } from "@/lib/chat/acp-plan";
import type { ContentBlock } from "@/lib/chat/types";
+import { commands } from "@/lib/utils/tauri";
// Module-level state — the router is a singleton process-wide.
let mounted = false;
@@ -893,7 +895,18 @@ function applyEventToSessionContent(sid: string, payload: PiInnerEvent) {
// ends up on disk and survives a restart.
if (t === "agent_end") {
store.actions.endTurn(sid);
- void persistBackgroundSession(sid);
+ const willRetry = (payload as { willRetry?: boolean }).willRetry === true;
+ void persistBackgroundSession(sid).finally(() => {
+ // A hidden conversation may finish after its panel's unmount cleanup saw
+ // it as busy. Re-check atomically once its transcript is durable, then
+ // reap the heavyweight agent tree. Foreground chats stay warm for the
+ // next prompt, and automatic retries keep their live process.
+ if (!willRetry && !hasForegroundHandler(sid)) {
+ void commands.piStopIfIdle(sid).catch((error) => {
+ console.warn("[router] failed to release idle background session:", error);
+ });
+ }
+ });
return;
}
}
diff --git a/apps/screenpipe-app-tauri/lib/utils/tauri.ts b/apps/screenpipe-app-tauri/lib/utils/tauri.ts
index 22d770d5e..b84dde3aa 100644
--- a/apps/screenpipe-app-tauri/lib/utils/tauri.ts
+++ b/apps/screenpipe-app-tauri/lib/utils/tauri.ts
@@ -1782,6 +1782,22 @@ async piStop(sessionId: string | null) : Promise<Result<PiInfo, string>> {
else return { status: "error", error: e as any };
}
},
+/**
+ * Stop and forget an idle Pi session without interrupting in-flight work.
+ *
+ * Chat panels call this when they give up foreground ownership. Keeping every
+ * completed ACP conversation resident leaves a full Bun/Node/agent process
+ * tree behind for each chat. The busy check and removal happen under the pool
+ * lock, so a prompt cannot race between the check and teardown.
+ */
+async piStopIfIdle(sessionId: string | null) : Promise<Result<PiInfo, string>> {
+ try {
+ return { status: "ok", data: await TAURI_INVOKE("pi_stop_if_idle", { sessionId }) };
+} catch (e) {
+ if(e instanceof Error) throw e;
+ else return { status: "error", error: e as any };
+}
+},
/**
* Update Pi config and restart the chat session so the new model takes effect.
* Without restart, Pi keeps using the provider/model from its original CLI args.
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 2e36ef51a..7f6ee0582 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -2304,6 +2304,40 @@ pub async fn pi_stop(
}
}
+/// Stop and forget an idle Pi session without interrupting in-flight work.
+///
+/// Chat panels call this when they give up foreground ownership. Keeping every
+/// completed ACP conversation resident leaves a full Bun/Node/agent process
+/// tree behind for each chat. The busy check and removal happen under the pool
+/// lock, so a prompt cannot race between the check and teardown.
+#[tauri::command]
+#[specta::specta]
+pub async fn pi_stop_if_idle(
+ state: State<'_, PiState>,
+ session_id: Option<String>,
+) -> Result<PiInfo, String> {
+ let sid = session_id.unwrap_or_else(|| "chat".to_string());
+
+ let manager = {
+ let mut pool = state.0.lock().await;
+ let Some(manager) = pool.sessions.get_mut(&sid) else {
+ return Ok(PiInfo::default());
+ };
+ if manager.has_in_flight_work() {
+ debug!("Keeping busy Pi session '{}' alive in background", sid);
+ return Ok(manager.snapshot(&sid));
+ }
+ pool.sessions.remove(&sid)
+ };
+
+ let Some(mut manager) = manager else {
+ return Ok(PiInfo::default());
+ };
+ info!("Stopping idle Pi sidecar for released session: {}", sid);
+ manager.stop().await;
+ Ok(manager.snapshot(&sid))
+}
+
/// Start the Pi sidecar in RPC mode (Tauri command wrapper)
#[tauri::command]
#[specta::specta]