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: always kill Pi on +NEW to prevent context leak race condition

Louis Beaumont8264b5348d68
diff --git a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
index d7cd070ef..b0ab57b19 100644
--- a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
+++ b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
@@ -294,16 +294,11 @@ export function useChatConversations(opts: UseChatConversationsOpts) {
};
// ---- startNewConversation ----
+ // Always kills and restarts Pi to guarantee a clean session.
+ // piNewSession() has a race condition: if a prompt is sent before it
+ // completes, the old context leaks through. Kill+restart is safe.
const startNewConversation = async () => {
- // If Pi is running and idle (not busy), just reset the session
- if (piInfo?.running && !isLoading && !isStreaming) {
- try {
- await commands.piNewSession(PI_CHAT_SESSION);
- } catch (e) {
- console.warn("[Pi] Failed to reset session:", e);
- }
- }
- // Clear frontend state (Pi kill+restart is handled by the caller when busy)
+ // Clear frontend state first
piStreamingTextRef.current = "";
piMessageIdRef.current = null;
piContentBlocksRef.current = [];
@@ -315,6 +310,13 @@ export function useChatConversations(opts: UseChatConversationsOpts) {
if (inputRef.current) inputRef.current.style.height = "auto";
setShowHistory(false);
setPastedImages([]);
+
+ // Kill Pi and restart fresh — the only reliable way to clear context
+ if (piInfo?.running) {
+ try {
+ await commands.piStop(PI_CHAT_SESSION);
+ } catch {}
+ }
piSessionSyncedRef.current = true;
};
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 5249a314f..2dbb2a520 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -2883,30 +2883,9 @@ export function StandaloneChat({ className }: { className?: string } = {}) {
size="sm"
onClick={async (e) => {
e.stopPropagation();
- // Kill Pi process if busy, then restart fresh
- if (piInfo?.running && (isLoading || isStreaming)) {
- piStoppedIntentionallyRef.current = true;
- try { await commands.piStop(PI_CHAT_SESSION); } catch {}
- piStreamingTextRef.current = "";
- piMessageIdRef.current = null;
- piContentBlocksRef.current = [];
- setIsLoading(false);
- setIsStreaming(false);
- // Restart Pi fresh
- try {
- const providerConfig = buildProviderConfig();
- const home = await homeDir();
- const dir = await join(home, ".screenpipe", "pi-chat");
- const result = await commands.piStart(PI_CHAT_SESSION, dir, settings.user?.token ?? null, providerConfig);
- if (result.status === "ok") {
- setPiInfo(result.data);
- piSessionSyncedRef.current = true;
- }
- } catch (err) {
- console.warn("[Pi] Failed to restart after new chat:", err);
- }
- }
- startNewConversation();
+ piStoppedIntentionallyRef.current = true;
+ await startNewConversation();
+ // Pi will auto-restart on the next message via the sendPiMessage flow
}}
className="h-7 px-3 gap-1.5 text-xs bg-foreground text-background hover:bg-background hover:text-foreground transition-colors duration-150"
title="New chat"