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-sidebar): tier user-touched chats above pipe completions

Louis Beaumont75e61c1d7ae6
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
index 1bf0f8e7c..88aee4fe3 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
+++ b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts
@@ -158,6 +158,24 @@ describe("chat-store: stable sort by createdAt", () => {
expect(ordered.map((s) => s.id)).toEqual(["older", "newer"]);
});
+ it("user-touched chats outrank pipe completions even with older lastUserMessageAt", () => {
+ // Bug: pipe-watch / pipe-run sessions are upserted with
+ // createdAt: Date.now() when they spawn. After they finish they
+ // appear in Recents. Their `createdAt` is more recent than a
+ // user's last user-send timestamp from a few minutes earlier, so
+ // pipes were burying the chat the user just typed in.
+ // Tiered sort fixes this: rows with `lastUserMessageAt` set
+ // always rank above rows without one.
+ useChatStore.getState().actions.upsert(
+ baseRecord({ id: "user-chat", createdAt: 100, lastUserMessageAt: 1_000 }),
+ );
+ useChatStore.getState().actions.upsert(
+ baseRecord({ id: "pipe-completion", kind: "pipe-watch", createdAt: 9_000 }),
+ );
+ const ordered = selectOrderedSessions(useChatStore.getState());
+ expect(ordered.map((s) => s.id)).toEqual(["user-chat", "pipe-completion"]);
+ });
+
it("pinned rows float above unpinned, both sorted by createdAt within group", () => {
useChatStore.getState().actions.upsert(baseRecord({ id: "p1", createdAt: 100, pinned: true }));
useChatStore.getState().actions.upsert(baseRecord({ id: "r1", createdAt: 200 }));
diff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
index a56755915..5cb2e70fb 100644
--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts
@@ -582,14 +582,23 @@ function sortKey(s: SessionRecord): number {
return s.lastUserMessageAt ?? s.createdAt;
}
+/** Tier: user-touched chats (any lastUserMessageAt set) sit above
+ * auto-generated rows (pipe-watch / pipe-run completions). Without
+ * this, a pipe that finished 30 s ago would outrank a chat the user
+ * typed in 2 min ago — `createdAt` of a fresh pipe session is more
+ * recent than the user's last bump. Lower tier = higher in list. */
+function tier(s: SessionRecord): number {
+ return s.lastUserMessageAt ? 0 : 1;
+}
+
+function compareForSidebar(a: SessionRecord, b: SessionRecord): number {
+ return tier(a) - tier(b) || sortKey(b) - sortKey(a);
+}
+
export function selectOrderedSessions(state: ChatStore): SessionRecord[] {
const all = Object.values(state.sessions);
- const pinned = all
- .filter((s) => s.pinned)
- .sort((a, b) => sortKey(b) - sortKey(a));
- const recents = all
- .filter((s) => !s.pinned)
- .sort((a, b) => sortKey(b) - sortKey(a));
+ const pinned = all.filter((s) => s.pinned).sort(compareForSidebar);
+ const recents = all.filter((s) => !s.pinned).sort(compareForSidebar);
return [...pinned, ...recents];
}
@@ -604,12 +613,8 @@ export function useOrderedSessions(): SessionRecord[] {
const sessionsMap = useChatStore((s) => s.sessions);
return useMemo(() => {
const all = Object.values(sessionsMap);
- const pinned = all
- .filter((s) => s.pinned)
- .sort((a, b) => sortKey(b) - sortKey(a));
- const recents = all
- .filter((s) => !s.pinned)
- .sort((a, b) => sortKey(b) - sortKey(a));
+ const pinned = all.filter((s) => s.pinned).sort(compareForSidebar);
+ const recents = all.filter((s) => !s.pinned).sort(compareForSidebar);
return [...pinned, ...recents];
}, [sessionsMap]);
}