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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(chat): keep stale saves from restoring old sessions
Louis Beaumontb0833a95c793
diff --git a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.tsindex c4667ac94..2d784ce72 100644--- a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts+++ b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts@@ -859,9 +859,13 @@ export function useChatConversations(opts: UseChatConversationsOpts) {}}- if (!conversationId || conversationId !== convId) {- setConversationId(convId);- }+ // Persistence must not claim foreground ownership. A save can outlive the+ // chat that started it (for example, the user clicks an automation card+ // and then opens a new chat while the first-turn write is still pending).+ // Re-applying convId here would navigate the panel back to that obsolete+ // session when the write completes. Session creation/loading and the send+ // transport already publish the active id before calling this function;+ // saveConversation only makes that session durable.};// ---- Auto-save conversation when a response completes (isLoading transitions from true to false) ----diff --git a/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx b/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsxindex 53dffdba4..44461ad21 100644--- a/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx+++ b/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx@@ -58,6 +58,8 @@ const saveCalls: Array<{lastViewedAt?: number;presetId?: string;}> = [];+let pendingSaveGate: Promise<void> | null = null;+let notifySaveStarted: (() => void) | null = null;vi.mock("@/lib/chat-storage", () => ({saveConversationFile: vi.fn(async (conv: any) => {@@ -69,6 +71,9 @@ vi.mock("@/lib/chat-storage", () => ({lastViewedAt: conv.lastViewedAt,presetId: conv.presetId,});+ notifySaveStarted?.();+ notifySaveStarted = null;+ await pendingSaveGate;}),loadConversationFile: vi.fn(async () => null),deleteConversationFile: vi.fn(async () => undefined),@@ -154,6 +159,8 @@ function useHarness(args: {beforeEach(() => {saveCalls.length = 0;+ pendingSaveGate = null;+ notifySaveStarted = null;deleteCachedBrowserState("chat-A");deleteCachedBrowserState("fresh-sid");useChatStore.setState({ sessions: {}, currentId: null, panelSessionId: null });@@ -418,6 +425,49 @@ describe("saveConversation race (PR #3600 / issue #3636 candidate)", () => {expect(saveCalls[0].id).not.toBe("stale-conversation-id");});+ it("a late first-turn save cannot restore an obsolete foreground conversation", async () => {+ // The automation-card send owns chat A and starts by making its first user+ // turn durable. While that disk write is pending, the user can switch to a+ // fresh chat B. Persistence must finish writing A without navigating the+ // panel back to A — saveConversation is storage, not foreground routing.+ let releaseSave!: () => void;+ pendingSaveGate = new Promise<void>((resolve) => {+ releaseSave = resolve;+ });+ const saveStarted = new Promise<void>((resolve) => {+ notifySaveStarted = resolve;+ });+ const firstTurn = [+ { id: "u1", role: "user" as const, content: "summarize my day", timestamp: 1 },+ ];++ const { result } = renderHook(() =>+ useHarness({+ initialMessages: [],+ // Matches a first-send render whose setConversationId(A) has not+ // committed yet; idOverride still binds the durable write to A.+ initialConversationId: null,+ initialPiSessionId: "chat-A",+ }),+ );++ const pendingSave = result.current.hook.saveConversation(firstTurn, {+ idOverride: "chat-A",+ });+ await saveStarted;++ // A new-chat action wins foreground ownership while A is still saving.+ result.current.conversationIdRef.current = "chat-B";+ releaseSave();+ await act(async () => {+ await pendingSave;+ });++ expect(saveCalls).toHaveLength(1);+ expect(saveCalls[0].id).toBe("chat-A");+ expect(result.current.conversationIdRef.current).toBe("chat-B");+ });+it("writes exactly ONE file for a single first turn — no twin (#4719)", async () => {// A single first user turn, with the panel id in lockstep across// conversationId / piSessionIdRef / store.currentId (the single-source-of-