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(sidebar): refresh completed Brain pipe runs

Louis Beaumontef6f551e8a7e
diff --git a/apps/screenpipe-app-tauri/components/__tests__/chat-sidebar-grouping.test.ts b/apps/screenpipe-app-tauri/components/__tests__/chat-sidebar-grouping.test.ts
index b37aa7be5..2a8bc0a83 100644
--- a/apps/screenpipe-app-tauri/components/__tests__/chat-sidebar-grouping.test.ts
+++ b/apps/screenpipe-app-tauri/components/__tests__/chat-sidebar-grouping.test.ts
@@ -18,6 +18,7 @@ import {
import {
buildPipeExecutionHistoryPage,
isTerminalPipeExecutionStatus,
+ shouldReloadPipeExecutionHistory,
} from "@/lib/pipe-execution-status";
import type { SessionRecord } from "@/lib/stores/chat-store";
@@ -63,6 +64,16 @@ describe("buildPipeExecutionHistoryPage", () => {
});
});
+describe("shouldReloadPipeExecutionHistory", () => {
+ it("invalidates an expanded history when inventory advances", () => {
+ expect(shouldReloadPipeExecutionHistory(41, 42)).toBe(true);
+ expect(shouldReloadPipeExecutionHistory(undefined, 42)).toBe(true);
+ expect(shouldReloadPipeExecutionHistory(42, 42)).toBe(false);
+ expect(shouldReloadPipeExecutionHistory(43, 42)).toBe(false);
+ expect(shouldReloadPipeExecutionHistory(42, undefined)).toBe(false);
+ });
+});
+
describe("latestSidebarPipeRunTimes", () => {
it("updates only when a terminal pipe run enters history", () => {
const oldRun = "2026-07-24T08:00:00.000Z";
diff --git a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
index 9f9b9a924..9bf2eca2a 100644
--- a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
+++ b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx
@@ -109,6 +109,7 @@ import { pipeExecutionToConversation } from "@/lib/pipe-ndjson-to-chat";
import {
buildPipeExecutionHistoryPage,
isTerminalPipeExecutionStatus,
+ shouldReloadPipeExecutionHistory,
} from "@/lib/pipe-execution-status";
import { parsePipeSessionId, pipeSessionId } from "@/lib/events/types";
import type { ChatConversation } from "@/lib/hooks/use-settings";
@@ -526,6 +527,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
const executionConversationsRef = useRef<Map<string, ChatConversation>>(new Map());
const executionMetadataRef = useRef<Map<string, SidebarPipeExecution>>(new Map());
const syntheticExecutionIdsRef = useRef<Set<string>>(new Set());
+ const loadedPipeLatestExecutionIdsRef = useRef<Record<string, number>>({});
const pipeDataGenerationRef = useRef(0);
const releasePipeData = useCallback(() => {
@@ -541,6 +543,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
: new Set();
executionConversationsRef.current.clear();
executionMetadataRef.current.clear();
+ loadedPipeLatestExecutionIdsRef.current = {};
setLoadedPipeRuns({});
setPipeRunCursors({});
setPipeRunsHaveMore({});
@@ -562,7 +565,10 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
(event) => updatePipesCollapsed(event.payload.collapsed),
);
- const fetchPipeInventory = useCallback(async (append = false) => {
+ const fetchPipeInventory = useCallback(async (
+ append = false,
+ preserveExisting = false,
+ ) => {
const generation = pipeDataGenerationRef.current;
if (append) setPipeInventoryLoadingMore(true);
try {
@@ -591,16 +597,20 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
});
}
setPipeInventory((previous) => {
- if (!append) return page;
+ if (!append && !preserveExisting) return page;
const merged = new Map(previous.map((pipe) => [pipe.name, pipe]));
for (const pipe of page) merged.set(pipe.name, pipe);
return Array.from(merged.values()).sort(
(a, b) => b.latestExecutionId - a.latestExecutionId,
);
});
- setPipeInventoryHasMore(payload.has_more === true);
- pipeInventoryCursorRef.current =
- typeof payload.next_before_id === "number" ? payload.next_before_id : null;
+ // A heartbeat refreshes only the newest page. Preserve the pagination
+ // cursor and older inventory rows the user explicitly loaded.
+ if (!preserveExisting) {
+ setPipeInventoryHasMore(payload.has_more === true);
+ pipeInventoryCursorRef.current =
+ typeof payload.next_before_id === "number" ? payload.next_before_id : null;
+ }
setPipeInventoryAuthoritative(true);
} catch {
// Keep recent in-memory pipe groups available if the engine is still
@@ -619,12 +629,25 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
if (!pipesCollapsed) void fetchPipeInventory(false);
}, [pipesCollapsed, fetchPipeInventory]);
useInterval(
- () => void fetchPipeInventory(false),
- pipesCollapsed || pipeInventory.length > PIPE_INVENTORY_PAGE_SIZE ? null : 15_000,
+ () => void fetchPipeInventory(false, true),
+ pipesCollapsed ? null : 15_000,
);
- const loadPipeRuns = useCallback(async (pipeName: string, append = false) => {
- if (loadingPipeRuns.has(pipeName) || (!append && loadedPipeRuns[pipeName])) return;
+ const loadPipeRuns = useCallback(async (
+ pipeName: string,
+ append = false,
+ latestExecutionId?: number,
+ ) => {
+ const loadedLatestExecutionId =
+ loadedPipeLatestExecutionIdsRef.current[pipeName];
+ const loadedPageIsCurrent = !shouldReloadPipeExecutionHistory(
+ loadedLatestExecutionId,
+ latestExecutionId,
+ );
+ if (
+ loadingPipeRuns.has(pipeName) ||
+ (!append && loadedPipeRuns[pipeName] && loadedPageIsCurrent)
+ ) return;
const generation = pipeDataGenerationRef.current;
setLoadingPipeRuns((prev) => new Set(prev).add(pipeName));
try {
@@ -674,6 +697,10 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
const records = candidates.flatMap((candidate) =>
candidate?.belongsInPipeGroup ? [candidate.record] : [],
);
+ if (!append) {
+ loadedPipeLatestExecutionIdsRef.current[pipeName] =
+ terminalExecutions[0]?.id ?? 0;
+ }
setLoadedPipeRuns((prev) => ({
...prev,
[pipeName]: append
@@ -718,6 +745,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
const orderedNames = visibleSidebarPipeNames(pipeInventory, pipes);
return orderedNames.map((name) => {
+ const inventoryItem = pipeInventory.find((pipe) => pipe.name === name);
// Keep a newly completed run visible before the activity inventory
// refreshes, while deduping the same saved row returned by both sources.
// Filter out sessions deleted from the store but still in the lazy cache.
@@ -745,6 +773,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
kind: "group" as const,
key: `pipe:${name}`,
title: name,
+ latestExecutionId: inventoryItem?.latestExecutionId,
sessions,
};
});
@@ -833,7 +862,11 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
return next;
});
if (!wasExpanded && key.startsWith("pipe:")) {
- void loadPipeRuns(key.slice("pipe:".length));
+ const pipeName = key.slice("pipe:".length);
+ const latestExecutionId = pipeInventory.find(
+ (pipe) => pipe.name === pipeName,
+ )?.latestExecutionId;
+ void loadPipeRuns(pipeName, false, latestExecutionId);
}
};
@@ -844,7 +877,7 @@ export function ChatSidebar({ className, onViewAll }: ChatSidebarProps) {
if (pipesCollapsed) return;
for (const item of pipeItems) {
if (expandedGroups.has(item.key)) {
- void loadPipeRuns(item.title);
+ void loadPipeRuns(item.title, false, item.latestExecutionId);
}
}
}, [expandedGroups, loadPipeRuns, pipeItems, pipesCollapsed]);
diff --git a/apps/screenpipe-app-tauri/e2e/specs/chat-sidebar-pipe-inventory.spec.ts b/apps/screenpipe-app-tauri/e2e/specs/chat-sidebar-pipe-inventory.spec.ts
index efddd72db..3ac1549fc 100644
--- a/apps/screenpipe-app-tauri/e2e/specs/chat-sidebar-pipe-inventory.spec.ts
+++ b/apps/screenpipe-app-tauri/e2e/specs/chat-sidebar-pipe-inventory.spec.ts
@@ -11,7 +11,8 @@
* 2. opening Pipes loads the compact activity inventory;
* 3. expanding one pipe omits the running execution;
* 4. the first page contains exactly 10 completed executions; and
- * 5. older executions paginate only when requested.
+ * 5. older executions paginate only when requested; and
+ * 6. a newly completed Brain/background run invalidates an expanded group.
*/
import { randomUUID } from "node:crypto";
@@ -151,22 +152,25 @@ async function mockPipeActivityApi(): Promise<void> {
__pipeActivityFetches?: number;
__pipeExecutionFetches?: number;
__pipeFullOutputFetches?: number;
+ __pipeLatestCompletedId?: number;
__pipeSidebarOriginalFetch?: typeof window.fetch;
};
testWindow.__pipeActivityFetches = 0;
testWindow.__pipeExecutionFetches = 0;
testWindow.__pipeFullOutputFetches = 0;
+ testWindow.__pipeLatestCompletedId = 12;
const originalFetch = testWindow.__pipeSidebarOriginalFetch ?? window.fetch.bind(window);
testWindow.__pipeSidebarOriginalFetch = originalFetch;
window.fetch = (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : (input as Request)?.url ?? String(input);
if (url.includes("/pipes/activity?")) {
testWindow.__pipeActivityFetches = (testWindow.__pipeActivityFetches ?? 0) + 1;
+ const latestCompletedId = testWindow.__pipeLatestCompletedId ?? 12;
return Promise.resolve(new Response(JSON.stringify({
data: [{
pipe_name: pipeName,
- execution_count: 12,
- latest_execution_id: 12,
+ execution_count: latestCompletedId,
+ latest_execution_id: latestCompletedId,
last_run_at: new Date().toISOString(),
status: "completed",
}],
@@ -180,17 +184,21 @@ async function mockPipeActivityApi(): Promise<void> {
const beforeId = Number(parsed.searchParams.get("before_id") ?? Number.POSITIVE_INFINITY);
const limit = Number(parsed.searchParams.get("limit") ?? 11);
const includeOutput = parsed.searchParams.get("include_output") !== "false";
+ const latestCompletedId = testWindow.__pipeLatestCompletedId ?? 12;
if (includeOutput) {
testWindow.__pipeFullOutputFetches =
(testWindow.__pipeFullOutputFetches ?? 0) + 1;
}
- const data = Array.from({ length: 13 }, (_, index) => 13 - index)
+ const data = Array.from(
+ { length: latestCompletedId + 1 },
+ (_, index) => latestCompletedId + 1 - index,
+ )
.filter((id) => id < beforeId)
.slice(0, limit)
.map((id) => ({
id,
pipe_name: pipeName,
- status: id === 13 ? "running" : "completed",
+ status: id === latestCompletedId + 1 ? "running" : "completed",
trigger_type: "scheduled",
started_at: new Date(Date.now() - id * 60_000).toISOString(),
finished_at: new Date(Date.now() - id * 60_000 + 1_000).toISOString(),
@@ -359,5 +367,25 @@ describe("chat sidebar pipe inventory", function () {
timeoutMsg: "the selected pipe execution did not open in chat",
},
);
+
+ // Model a Brain-triggered background completion after this group has
+ // already loaded. The activity heartbeat must notice the newer terminal
+ // execution and replace the cached first page without a collapse/reopen.
+ await browser.execute(() => {
+ const testWindow = window as typeof window & {
+ __pipeLatestCompletedId?: number;
+ };
+ testWindow.__pipeLatestCompletedId = 14;
+ });
+ await browser.waitUntil(
+ async () => await browser.execute((id: string) =>
+ Boolean(document.querySelector(`[data-testid="chat-row-${id}"]`)),
+ `pipe:${PIPE_NAME}:14`),
+ {
+ timeout: t(25_000),
+ interval: 250,
+ timeoutMsg: "new terminal execution did not refresh the expanded pipe history",
+ },
+ );
});
});
diff --git a/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-run-recorder.test.ts b/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-run-recorder.test.ts
index de16ad418..70c199add 100644
--- a/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-run-recorder.test.ts
+++ b/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-run-recorder.test.ts
@@ -4,9 +4,24 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
-const saveSpy = vi.fn(async () => undefined);
+const { saveSpy } = vi.hoisted(() => ({
+ saveSpy: vi.fn(async () => undefined),
+}));
vi.mock("@/lib/chat-storage", () => ({
saveConversationFile: saveSpy,
+ conversationMetaFromJson: (conversation: any) => ({
+ id: conversation.id,
+ title: conversation.title,
+ titleSource: conversation.titleSource,
+ messageCount: conversation.messages.length,
+ createdAt: conversation.createdAt,
+ updatedAt: conversation.updatedAt,
+ lastContentAt: conversation.lastContentAt,
+ lastViewedAt: conversation.lastViewedAt,
+ pinned: false,
+ kind: conversation.kind,
+ pipeContext: conversation.pipeContext,
+ }),
}));
vi.mock("@/lib/hooks/use-settings", async () => {
@@ -19,6 +34,7 @@ vi.mock("@/lib/hooks/use-settings", async () => {
import { __testing } from "../pipe-run-recorder";
import type { AgentEventEnvelope } from "../types";
+import { useChatStore } from "@/lib/stores/chat-store";
const SID = "pipe:my-pipe:42";
const env = (event: any): AgentEventEnvelope => ({
@@ -30,6 +46,7 @@ const env = (event: any): AgentEventEnvelope => ({
beforeEach(() => {
saveSpy.mockClear();
__testing.reset();
+ useChatStore.getState().actions.drop(SID);
});
describe("pipe-run-recorder: buffering", () => {
@@ -91,6 +108,62 @@ describe("pipe-run-recorder: finalize on terminal event", () => {
expect(arg.messages.some((m: any) => m.role === "assistant" && m.content?.includes("result"))).toBe(true);
});
+ it("saves when the sidebar poll raced in a metadata-only pipe row", async () => {
+ useChatStore.getState().actions.upsert({
+ id: SID,
+ title: "my-pipe #42",
+ titleSource: "user",
+ preview: "",
+ status: "idle",
+ messageCount: 1,
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ pinned: false,
+ unread: false,
+ kind: "pipe-run",
+ pipeContext: { pipeName: "my-pipe", executionId: 42 },
+ });
+
+ await __testing.inject(env({ type: "message_start", message: { role: "assistant" } }));
+ await __testing.inject(
+ env({
+ type: "message_update",
+ assistantMessageEvent: { type: "text_delta", delta: "result" },
+ }),
+ );
+ await __testing.inject(env({ type: "agent_end" }));
+
+ expect(saveSpy).toHaveBeenCalledTimes(1);
+ });
+
+ it("leaves a live pipe-watch session to its authoritative writer", async () => {
+ useChatStore.getState().actions.upsert({
+ id: SID,
+ title: "my-pipe #42",
+ titleSource: "user",
+ preview: "",
+ status: "streaming",
+ messageCount: 0,
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ pinned: false,
+ unread: false,
+ kind: "pipe-watch",
+ pipeContext: { pipeName: "my-pipe", executionId: 42 },
+ });
+
+ await __testing.inject(env({ type: "message_start", message: { role: "assistant" } }));
+ await __testing.inject(
+ env({
+ type: "message_update",
+ assistantMessageEvent: { type: "text_delta", delta: "result" },
+ }),
+ );
+ await __testing.inject(env({ type: "agent_end" }));
+
+ expect(saveSpy).not.toHaveBeenCalled();
+ });
+
it("clears the buffer after finalize (idempotent)", async () => {
await __testing.inject(env({ type: "message_start", message: { role: "assistant" } }));
await __testing.inject(
diff --git a/apps/screenpipe-app-tauri/lib/events/pipe-run-recorder.ts b/apps/screenpipe-app-tauri/lib/events/pipe-run-recorder.ts
index 33d8d5b2a..01e9350b1 100644
--- a/apps/screenpipe-app-tauri/lib/events/pipe-run-recorder.ts
+++ b/apps/screenpipe-app-tauri/lib/events/pipe-run-recorder.ts
@@ -161,15 +161,19 @@ async function finalizeBuffer(sid: string, buf: PipeRunBuffer): Promise<void> {
// no-op rather than re-saving.
buffers.delete(sid);
- // If the chat-store already has a session record for this sid, the
- // panel was the foreground owner at some point during the run — its
- // persistence pipeline (chat-store → saveConversationFile via the
- // background-router) is the authoritative writer. Skipping here
- // avoids clobbering the panel's view with the recorder's
- // recorder-only NDJSON replay.
+ // A live pipe-watch (or a session with messages already hydrated) has an
+ // authoritative panel/writer, so avoid clobbering it with recorder-only
+ // NDJSON replay. Do not treat every store row as ownership: the sidebar's
+ // execution poll can upsert a metadata-only `pipe-run` row just before the
+ // terminal event reaches us. Returning for that row loses the completed
+ // transcript permanently and leaves an expanded history stale.
try {
const { useChatStore } = await import("@/lib/stores/chat-store");
- if (useChatStore.getState().sessions[sid]) {
+ const existing = useChatStore.getState().sessions[sid];
+ if (
+ existing?.kind === "pipe-watch" ||
+ (existing?.messages?.length ?? 0) > 0
+ ) {
return;
}
} catch {
diff --git a/apps/screenpipe-app-tauri/lib/pipe-execution-status.ts b/apps/screenpipe-app-tauri/lib/pipe-execution-status.ts
index b87ee0a41..9c72d1d81 100644
--- a/apps/screenpipe-app-tauri/lib/pipe-execution-status.ts
+++ b/apps/screenpipe-app-tauri/lib/pipe-execution-status.ts
@@ -8,6 +8,17 @@ export function isTerminalPipeExecutionStatus(status: string): boolean {
return !ACTIVE_PIPE_EXECUTION_STATUSES.has(status.trim().toLowerCase());
}
+export function shouldReloadPipeExecutionHistory(
+ loadedLatestExecutionId: number | undefined,
+ inventoryLatestExecutionId: number | undefined,
+): boolean {
+ return (
+ inventoryLatestExecutionId != null &&
+ (loadedLatestExecutionId == null ||
+ inventoryLatestExecutionId > loadedLatestExecutionId)
+ );
+}
+
export function buildPipeExecutionHistoryPage<
T extends { id: number; status: string },
>(executions: T[], pageSize: number): {
diff --git a/apps/screenpipe-app-tauri/vitest.config.ts b/apps/screenpipe-app-tauri/vitest.config.ts
index 6b8666547..9dd8e3bbb 100644
--- a/apps/screenpipe-app-tauri/vitest.config.ts
+++ b/apps/screenpipe-app-tauri/vitest.config.ts
@@ -35,9 +35,7 @@ export default defineConfig({
// fix in follow-ups. Don't add new entries without an owner.
// - text-overlay.test.tsx: container.firstChild null + missing
// "solid" underline on link blocks
- // - pipe-run-recorder.test.ts: vi.mock factory hoist issue
"components/__tests__/text-overlay.test.tsx",
- "lib/events/__tests__/pipe-run-recorder.test.ts",
],
},
resolve: {