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): stop branched chats vanishing from the sidebar (#5500)
Harsh Kumarfc1b42fd4e37
diff --git a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx b/apps/screenpipe-app-tauri/components/chat-sidebar.tsxindex a9d8e24e9..9c46a466a 100644--- a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx+++ b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx@@ -410,6 +410,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {kind: meta.kind,pipeContext: meta.pipeContext,dedupKey: meta.dedupKey,+ branchedFrom: meta.branchedFrom,draft: false,});return;diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/chat-message-list.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/chat-message-list.tsxindex b5b4b3b67..ddae5e167 100644--- a/apps/screenpipe-app-tauri/components/chat/standalone/chat-message-list.tsx+++ b/apps/screenpipe-app-tauri/components/chat/standalone/chat-message-list.tsx@@ -501,7 +501,9 @@ export function ChatMessageList({<buttononClick={() => {onCloseMessageMenu();- branchConversation(message.id);+ Promise.resolve(branchConversation(message.id)).catch((e) =>+ console.error("[chat] branch failed:", e),+ );}}className="w-full flex items-center gap-2 px-2 py-1.5 text-sm rounded-md hover:bg-muted text-left">diff --git a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.tsindex 2d784ce72..91f2c0eb6 100644--- a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts+++ b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts@@ -743,6 +743,9 @@ export function useChatConversations(opts: UseChatConversationsOpts) {...(existing?.kind ? { kind: existing.kind } : {}),...(existing?.pipeContext ? { pipeContext: existing.pipeContext } : {}),...(existing?.sidebarGroup ? { sidebarGroup: existing.sidebarGroup } : {}),+ // Same for the branch marker: dropping it on the first typed reply would+ // let the sidebar dedup collapse the fork back into its parent.+ ...(existing?.branchedFrom ? { branchedFrom: existing.branchedFrom } : {}),...(browserState ? { browserState } : {}),...(existing?.pinned ? { pinned: existing.pinned } : {}),...(existing?.hidden ? { hidden: existing.hidden } : {}),@@ -1373,6 +1376,7 @@ export function useChatConversations(opts: UseChatConversationsOpts) {const newId = crypto.randomUUID();const currentTitle = store.sessions[outgoingSid]?.title?.trim();const title = currentTitle || "Branched Chat";+ const branchedFrom = outgoingSid || conversationId || undefined;const createdAt = Date.now();const lastUserMessageAt = [...branchedMessages].reverse()@@ -1419,6 +1423,9 @@ export function useChatConversations(opts: UseChatConversationsOpts) {createdAt,updatedAt: createdAt,...(lastUserMessageAt ? { lastUserMessageAt } : {}),+ // Exempts the fork from the first-user-message dedup, which would+ // otherwise collapse it into the parent it was branched from.+ ...(branchedFrom ? { branchedFrom } : {}),};await saveConversationFile(conversation);@@ -1440,6 +1447,9 @@ export function useChatConversations(opts: UseChatConversationsOpts) {...(conversation.lastUserMessageAt? { lastUserMessageAt: conversation.lastUserMessageAt }: {}),+ ...(conversation.branchedFrom+ ? { branchedFrom: conversation.branchedFrom }+ : {}),});store.actions.setMessages(newId, conversation.messages as any);store.actions.setCurrent(newId);diff --git a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.tsindex 837ac6e4f..e985a137e 100644--- a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts+++ b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts@@ -725,6 +725,24 @@ describe("chat-store: cross-window duplicate row collapsing", () => {expect(rows[0].id).toBe("visibleTwin");});+ it("keeps a branch that shares its parent's first user message", () => {+ // A fork repeats its parent's opening message and is created inside the+ // dedup window, so without the exemption it merged into the parent and+ // vanished from the sidebar the moment it was created.+ useChatStore.getState().actions.upsert(+ withMessages("parent", "shared opener", "long answer", { createdAt: 1_000 }),+ );+ useChatStore.getState().actions.upsert(+ withMessages("branch", "shared opener", "long answer", {+ createdAt: 1_100,+ branchedFrom: "parent",+ }),+ );+ const rows = dedupeSessionRecords(Object.values(useChatStore.getState().sessions));+ expect(rows).toHaveLength(2);+ expect(rows.map((r) => r.id).sort()).toEqual(["branch", "parent"]);+ });+it("leaves distinct conversations untouched", () => {useChatStore.getState().actions.upsert(withMessages("a", "first chat", "x", { createdAt: 1_000 }),diff --git a/apps/screenpipe-app-tauri/lib/chat-dedup.ts b/apps/screenpipe-app-tauri/lib/chat-dedup.tsindex 0c2848ae0..6283c3a95 100644--- a/apps/screenpipe-app-tauri/lib/chat-dedup.ts+++ b/apps/screenpipe-app-tauri/lib/chat-dedup.ts@@ -49,12 +49,15 @@ interface DedupMessageLike {interface DedupConvLike {kind?: string | null;messages?: unknown;+ branchedFrom?: string | null;}/** Dedup key for a conversation: its first user message, normalized to the* user's SEMANTIC text. Returns null for non-chat (pipe) conversations —* repeated pipe runs share a templated first message and must never be- * collapsed — and for chats with no user message.+ * collapsed — for branches, which repeat their parent's first message by+ * construction and would otherwise collapse into it — and for chats with no+ * user message.** Why semantic, not raw `content`: a cross-window save race persists one* logical chat under two ids, and the two copies frequently disagree on the@@ -67,6 +70,7 @@ interface DedupConvLike {export function conversationDedupKey(conv: DedupConvLike | null | undefined): string | null {const kind = conv?.kind ?? "chat";if (kind !== "chat") return null;+ if (conv?.branchedFrom) return null;const messages = Array.isArray(conv?.messages) ? (conv!.messages as DedupMessageLike[]) : [];const firstUser = messages.find((m) => m?.role === "user");const display = typeof firstUser?.displayContent === "string" ? firstUser.displayContent : "";diff --git a/apps/screenpipe-app-tauri/lib/chat-storage.ts b/apps/screenpipe-app-tauri/lib/chat-storage.tsindex 2c8fb816c..451d33a5f 100644--- a/apps/screenpipe-app-tauri/lib/chat-storage.ts+++ b/apps/screenpipe-app-tauri/lib/chat-storage.ts@@ -256,6 +256,10 @@ export interface ConversationMeta {* `chat-conversation-saved`) the same way `dedupeConversationMetas` does* on disk. Undefined for pipe runs / chats with no user message yet. */dedupKey?: string;+ /** Id of the conversation this was branched from. Exempts the row from+ * first-user-message dedup — a branch shares its parent's opening+ * message on purpose. */+ branchedFrom?: string;/** The AI preset ID last used in this conversation. Used to restore* the model selection when switching between chats. */presetId?: string;@@ -400,6 +404,7 @@ export function conversationMetaFromJson(conv: any): ConversationMeta | null {sidebarGroup: typeof conv.sidebarGroup === "string" ? conv.sidebarGroup : undefined,titleSource: conv.titleSource,dedupKey: conversationDedupKey(conv) ?? undefined,+ branchedFrom: typeof conv.branchedFrom === "string" ? conv.branchedFrom : undefined,presetId: typeof conv.presetId === "string" ? conv.presetId : undefined,};}diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsxindex e5da6ecca..129a1eafc 100644--- a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx+++ b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx@@ -195,6 +195,10 @@ export interface ChatConversation {/** Optional user-assigned sidebar group label (lowercase, trimmed).* Drives the manual-grouping sections in the chat sidebar. */sidebarGroup?: string;+ /** Id of the conversation this one was branched from. Set only by+ * "branch in new chat". Exempts the branch from first-user-message+ * dedup — it deliberately shares its parent's opening message. */+ branchedFrom?: string;/** Last URL the agent navigated the embedded browser sidebar to.* Drives the right-side `<BrowserSidebar />` panel: when the user* re-opens this conversation the panel restores to this URL.diff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.tsindex cfdf556d7..e10bfb63f 100644--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts@@ -75,6 +75,9 @@ export interface SessionRecord {* dedup can still match them; foreground rows that DO hold `messages` derive* the key live instead. Never persisted. */dedupKey?: string;+ /** Id of the conversation this was branched from. Exempts the row from+ * dedup: a branch shares its parent's first user message by design. */+ branchedFrom?: string;/** Partial AI title being streamed — displayed in sidebar/header while* generation is in progress. Cleared on completion or failure.* Never persisted to disk. */@@ -450,6 +453,7 @@ export const useChatStore = create<ChatStore>((set) => ({pipeContext: existing.pipeContext ?? r.pipeContext,sidebarGroup: existing.sidebarGroup ?? r.sidebarGroup,dedupKey: existing.dedupKey ?? r.dedupKey,+ branchedFrom: existing.branchedFrom ?? r.branchedFrom,};merged.unread = restoreUnread(existing, merged);next[r.id] = merged;@@ -777,6 +781,7 @@ export function sessionRecordFromMeta(m: ConversationMeta): SessionRecord {pipeContext: m.pipeContext,sidebarGroup: m.sidebarGroup,dedupKey: m.dedupKey,+ branchedFrom: m.branchedFrom,presetId: m.presetId,};if (typeof m.lastViewedAt === "number") {@@ -886,10 +891,13 @@ function compareForSidebar(a: SessionRecord, b: SessionRecord): number {/** First-user-message dedup key for a store session. Prefer the key derived* from in-store `messages` (foreground / hydrated rows); fall back to the* `dedupKey` carried from disk meta (metadata-only rows — a boot-hydrated row- * or a cross-window twin). Null exempts the row (pipe runs, or a chat with no- * user message yet). */+ * or a cross-window twin). Null exempts the row (pipe runs, branches, or a+ * chat with no user message yet). */function sessionDedupKey(s: SessionRecord): string | null {if (s.kind === "pipe-watch" || s.kind === "pipe-run") return null;+ // Checked here as well as in conversationDedupKey: this path falls back to+ // the persisted `dedupKey`, which would otherwise reinstate a key.+ if (s.branchedFrom) return null;return conversationDedupKey({ kind: s.kind, messages: s.messages }) ?? s.dedupKey ?? null;}diff --git a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.tsindex bb1a6eaf1..95d3617c2 100644--- a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts+++ b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts@@ -844,6 +844,7 @@ async function persistBackgroundSession(sid: string): Promise<void> {...(session.kind ? { kind: session.kind } : existing?.kind ? { kind: existing.kind } : {}),...(session.pipeContext ? { pipeContext: session.pipeContext } : existing?.pipeContext ? { pipeContext: existing.pipeContext } : {}),...(existing?.sidebarGroup ? { sidebarGroup: existing.sidebarGroup } : {}),+ ...(session.branchedFrom ? { branchedFrom: session.branchedFrom } : existing?.branchedFrom ? { branchedFrom: existing.branchedFrom } : {}),...(browserState ? { browserState } : {}),};