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 creation during send loading
Louis Beaumontf6b62cf135f6
diff --git a/apps/screenpipe-app-tauri/app/(main)/home/page.tsx b/apps/screenpipe-app-tauri/app/(main)/home/page.tsxindex 7a59e14a3..2766a1cbf 100644--- a/apps/screenpipe-app-tauri/app/(main)/home/page.tsx+++ b/apps/screenpipe-app-tauri/app/(main)/home/page.tsx@@ -22,6 +22,7 @@ import {import { emit } from "@tauri-apps/api/event";import {getOrCreateEmptyChatId,+ isReusableBlankChatSession,applyChatSessionActivity,sessionRecordFromMeta,useChatStore,@@ -277,7 +278,9 @@ function HomeContent() {const { id, isNew } = getOrCreateEmptyChatId();// Clean up any *other* stray empty drafts, keeping the one we reuse.Object.values(store.sessions).forEach((s) => {- if (s.draft && s.id !== id) store.actions.drop(s.id);+ if (s.draft && s.id !== id && isReusableBlankChatSession(s)) {+ store.actions.drop(s.id);+ }});if (isNew) {store.actions.upsert({diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/send-immediate-feedback.test.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/send-immediate-feedback.test.tsindex 4eec16772..737b854f5 100644--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/send-immediate-feedback.test.ts+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/send-immediate-feedback.test.ts@@ -86,11 +86,23 @@ describe("chat send gives feedback in the same frame", () => {const preceding = window.slice(Math.max(0, i - 4), i).join("\n");expect(preceding,- `preflight exit "${line.trim()}" leaves the chat stranded — cards hidden, loader forever. Restore with setIsLoading(false).`,- ).toContain("setIsLoading(false)");+ `preflight exit "${line.trim()}" leaves the chat stranded — cards hidden, loader forever. Restore with finishAttempt().`,+ ).toContain("finishAttempt()");});});+ it("marks the shared session busy before the first await", () => {+ const lines = readFileSync(TRANSPORT, "utf8").split("\n");+ const fnStart = lines.findIndex((l) => l.includes("async function sendPiMessage"));+ const firstAwait = lines.findIndex(+ (l, i) => i > fnStart && l.includes("await awaitPendingPiPresetSwitch"),+ );+ const preAwait = lines.slice(fnStart, firstAwait).join("\n");++ expect(preAwait).toContain("attemptStore.actions.setStreaming(attemptSessionId");+ expect(preAwait).toContain("isLoading: true");+ });+it("has at least one guarded abort and one guarded throw", () => {// Guards the guard: if the window ever collapses to nothing (refactor,// renamed anchors) the test above would vacuously pass.diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.tsindex 56b34d9e7..1d5c18bd2 100644--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts@@ -523,11 +523,46 @@ export function useChatE2EGlobals({setIsStreaming(true);};+ // Reproduce the earlier send-preflight window: the UI has acknowledged+ // Send, but no durable user row exists yet. New chat must still allocate a+ // distinct id instead of reusing this apparently-empty session.+ (window as unknown as {+ __e2eLatchPreflightSend?: (sid: string) => void;+ }).__e2eLatchPreflightSend = (sid: string) => {+ const store = useChatStore.getState();+ if (!store.sessions[sid]) {+ store.actions.upsert({+ id: sid,+ title: "untitled",+ preview: "",+ status: "idle",+ messageCount: 0,+ createdAt: Date.now(),+ updatedAt: Date.now(),+ pinned: false,+ unread: false,+ draft: true,+ messages: [],+ });+ }+ store.actions.setCurrent(sid);+ store.actions.setPanelSession(sid);+ store.actions.setStreaming(sid, { isLoading: true, isStreaming: false });+ setMessages([]);+ setConversationId(sid);+ piSessionIdRef.current = sid;+ piSessionSyncedRef.current = true;+ setIsLoading(true);+ setIsStreaming(false);+ void emit("chat-current-session", { id: sid });+ };+return () => {delete (window as unknown as { __e2eSeedUserMessage?: unknown }).__e2eSeedUserMessage;delete (window as unknown as { __e2eSeedAssistantMessage?: unknown }).__e2eSeedAssistantMessage;delete (window as unknown as { __e2eReadActiveTurn?: unknown }).__e2eReadActiveTurn;delete (window as unknown as { __e2eLatchActiveSend?: unknown }).__e2eLatchActiveSend;+ delete (window as unknown as { __e2eLatchPreflightSend?: unknown }).__e2eLatchPreflightSend;};}, [forceQueueModeRef,diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.tsindex 138ac635f..d7fdec3bf 100644--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts@@ -97,6 +97,7 @@ export async function checkLivePiSession(* while a turn is in flight.*/export type PendingSend = {+ sessionId: string;text: string;/** The friendly label a Home card sends, so the optimistic bubble matches* the durable row's `displayContent` and nothing changes on the swap. */@@ -212,7 +213,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {}}- async function interruptActivePiTurn() {+ async function interruptActivePiTurn(sessionId = piSessionIdRef.current) {const hasActiveTurn = hasAuthoritativeActivePiTurn({isLoading,isStreaming,@@ -223,7 +224,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {let aborted = false;try {const abortResult = await Promise.race([- commands.piAbort(piSessionIdRef.current),+ commands.piAbort(sessionId),new Promise<{ status: "error"; error: string }>((resolve) => {window.setTimeout(() => resolve({ status: "error", error: "Abort timed out" }), 1_500);}),@@ -255,6 +256,29 @@ export function usePiSendTransport(options: PiSendTransportOptions) {sendOptions?: ChatSendOptions,) {clearPendingSteerTransportState();+ // This send belongs to the session that was foreground when the user+ // clicked Send. New chat is allowed during every asynchronous pause, so never+ // re-read piSessionIdRef for the attempt after this point: it may already+ // point at the newly-opened chat.+ const attemptSessionId = piSessionIdRef.current;+ const attemptPreset = getActivePreset();+ const isAttemptForeground = () => piSessionIdRef.current === attemptSessionId;+ const setAttemptPiInfo = (info: PiInfo | null) => {+ if (isAttemptForeground()) setPiInfo(info);+ };+ const finishAttempt = () => {+ const store = useChatStore.getState();+ if (store.sessions[attemptSessionId]) {+ store.actions.setStreaming(attemptSessionId, {+ isLoading: false,+ isStreaming: false,+ });+ }+ if (isAttemptForeground()) {+ setIsLoading(false);+ setIsStreaming(false);+ }+ };// Capture the attempt up front so it survives a failed ACP start — the// "switch to default" sign-in card resends this message on the new preset.lastUserMessageRef.current = userMessage;@@ -265,6 +289,14 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// its 5s ChatGPT token pre-check, two liveness checks, and the interrupt of// any active turn). Every abort and throw below restores this.setIsLoading(true);+ // Home chooses whether "+ new chat" can reuse the current draft from the+ // shared chat store, not this component's React state. Mirror the+ // optimistic loading edge before the first await so the current session+ // cannot be mistaken for a reusable blank chat during preflight.+ const attemptStore = useChatStore.getState();+ if (attemptStore.sessions[attemptSessionId]) {+ attemptStore.actions.setStreaming(attemptSessionId, { isLoading: true });+ }// Put the message on screen and empty the composer NOW, in the same commit.// Previously both waited for the preflight, so for its whole duration the@@ -274,7 +306,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// `pendingSend` renders a real user bubble until the durable row replaces// it, and the recovery effect at the end of this hook puts the text back if// the send aborts.- setPendingSend({ text: userMessage, displayLabel });+ setPendingSend({ sessionId: attemptSessionId, text: userMessage, displayLabel });setInput("");if (inputRef.current) inputRef.current.style.height = "auto";@@ -293,7 +325,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {variant: "destructive",});}- setIsLoading(false);+ finishAttempt();return;}@@ -303,9 +335,9 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// stale provider.let liveSession: Awaited<ReturnType<typeof checkLivePiSession>>;try {- liveSession = await checkLivePiSession(piSessionIdRef.current, setPiInfo);+ liveSession = await checkLivePiSession(attemptSessionId, setAttemptPiInfo);} catch (e) {- setIsLoading(false);+ finishAttempt();throw e;}@@ -314,7 +346,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {if (piStartInFlightRef.current) {if (!autoSendBypassRef.current) {toast({ title: "Pi starting", description: "Please wait a moment", variant: "destructive" });- setIsLoading(false);+ finishAttempt();return;}// Prefill auto-send: wait for in-flight start to complete@@ -323,7 +355,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {await new Promise(r => setTimeout(r, 300));}if (piStartInFlightRef.current) {- setIsLoading(false);+ finishAttempt();return; // timed out}} else {@@ -334,7 +366,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// Build a list of presets to try: active first, then other available// presets as fallbacks. This ensures that if the active preset fails// (e.g. ChatGPT OAuth expired), we try alternatives before giving up.- const activeP = getActivePreset();+ const activeP = attemptPreset;const allPresets = settings.aiPresets ?? [];const fallbackPresets = allPresets.filter((p) =>@@ -349,7 +381,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {let lastError = "";try {- const dir = await piProjectDirForSession(piSessionIdRef.current);+ const dir = await piProjectDirForSession(attemptSessionId);for (const preset of presetsToTry) {const providerConfig = buildProviderConfig(preset);@@ -382,17 +414,21 @@ export function usePiSendTransport(options: PiSendTransportOptions) {try {const result = await commands.piStart(- piSessionIdRef.current,+ attemptSessionId,dir,settings.user?.token ?? null,providerConfig,);if (result.status === "ok" && result.data.running) {- setPiInfo(result.data);- piSessionSyncedRef.current = false;+ if (isAttemptForeground()) {+ setPiInfo(result.data);+ piSessionSyncedRef.current = false;+ }piCrashCountRef.current = 0;- setRunningConfigFromProviderConfig(providerConfig);- syncThinkingLevelAfterStart(piSessionIdRef.current);+ if (isAttemptForeground()) {+ setRunningConfigFromProviderConfig(providerConfig);+ }+ syncThinkingLevelAfterStart(attemptSessionId);// Notify the user about the session-only fallback, but// do NOT persist it — the user's chosen preset stays.@@ -429,7 +465,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {}// The ACP sign-in / auth-cancelled card renders in-chat;// leave the loader up and it spins behind that card forever.- setIsLoading(false);+ finishAttempt();return;}}@@ -453,7 +489,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {}// Same as the branch above: the in-chat auth card owns the// outcome, so the optimistic loader must be handed back.- setIsLoading(false);+ finishAttempt();return;}}@@ -468,14 +504,14 @@ export function usePiSendTransport(options: PiSendTransportOptions) {: lastError,variant: "destructive",});- setIsLoading(false);+ finishAttempt();return;}} catch (e) {// A throw here (project dir, token check, piStart) aborts the send// before any turn exists. Without this the optimistic loading flag// set above would strand the chat: cards gone, loader forever.- setIsLoading(false);+ finishAttempt();throw e;} finally {setPiStarting(false);@@ -495,23 +531,23 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// transient IPC race), proceed with the send — the backend command surfaces// a real error if the process is genuinely gone, rather than dropping a send// that would have worked.- liveSession = await checkLivePiSession(piSessionIdRef.current, setPiInfo);+ liveSession = await checkLivePiSession(attemptSessionId, setAttemptPiInfo);if (!liveSession.running && !liveSession.indeterminate) {toast({title: "AI assistant is not ready",description: liveSession.error,variant: "destructive",});- setIsLoading(false);+ finishAttempt();return;}- await interruptActivePiTurn();+ await interruptActivePiTurn(attemptSessionId);} catch (e) {- setIsLoading(false);+ finishAttempt();throw e;}- forceQueueModeRef.current = true;+ if (isAttemptForeground()) forceQueueModeRef.current = true;const outgoingImages = imageDataUrls ?? pastedImages;const shouldClearPastedImages = imageDataUrls == null && pastedImages.length > 0;@@ -549,27 +585,28 @@ export function usePiSendTransport(options: PiSendTransportOptions) {role: "assistant",content: "Processing...",timestamp: Date.now(),- model: getActivePreset()?.model,- provider: getActivePreset()?.provider,+ model: attemptPreset?.model,+ provider: attemptPreset?.provider,};- piStreamingTextRef.current = "";- piMessageIdRef.current = assistantMessageId;- piContentBlocksRef.current = [];-piRateLimitRetries.current = 0;lastUserMessageRef.current = userMessage;// The id this turn is dispatched and streamed under — the single source of// truth for the whole turn's persistence (#4719).- const turnSessionId = piSessionIdRef.current;+ const turnSessionId = attemptSessionId;// Pull `conversationId` (React state, used by the edge/streaming auto-saves// in useChatConversations) into lockstep with the dispatched session id. If// it lags, those later saves write under a SECOND id and the sidebar// upserts a duplicate row (the summary/todo card twin). Setting it here// makes every save for this turn — immediate, streaming, and edge — land on// one id.- setConversationId(turnSessionId);+ if (isAttemptForeground()) {+ piStreamingTextRef.current = "";+ piMessageIdRef.current = assistantMessageId;+ piContentBlocksRef.current = [];+ setConversationId(turnSessionId);+ }// Persist from the current render snapshot instead of assigning inside the// state updater. React may defer updater callbacks; when that happened, a@@ -580,14 +617,56 @@ export function usePiSendTransport(options: PiSendTransportOptions) {newUserMessage,assistantPlaceholder,];- setMessages((prev) => [...prev, newUserMessage, assistantPlaceholder]);+ if (isAttemptForeground()) {+ setMessages((prev) => [...prev, newUserMessage, assistantPlaceholder]);+ }// The durable row is in the transcript now, so the optimistic one retires// in the same commit. Two bubbles must never paint at once.setPendingSend(null);- setInput("");- if (inputRef.current) inputRef.current.style.height = "auto";- setIsLoading(true);- setIsStreaming(true);+ if (isAttemptForeground()) {+ setInput("");+ if (inputRef.current) inputRef.current.style.height = "auto";+ setIsLoading(true);+ setIsStreaming(true);+ }++ // Mirror the user message + assistant placeholder before persistence.+ // saveConversation performs filesystem reads before its own store mirror;+ // during that await, New chat must already see this as a real active turn.+ // This also lets the background router own the response immediately when+ // the user switches chats while the initial save is still in flight.+ const storeState = useChatStore.getState();+ if (!storeState.sessions[turnSessionId]) {+ storeState.actions.upsert({+ id: turnSessionId,+ title: "untitled",+ preview: "",+ status: "streaming",+ messageCount: 0,+ createdAt: Date.now(),+ updatedAt: Date.now(),+ pinned: false,+ unread: false,+ });+ }+ storeState.actions.appendMessage(turnSessionId, newUserMessage as any);+ const currentTitle = useChatStore.getState().sessions[turnSessionId]?.title;+ if (displayLabel && isPlaceholderConversationTitle(currentTitle)) {+ storeState.actions.patch(turnSessionId, { title: displayLabel });+ }+ storeState.actions.appendMessage(turnSessionId, assistantPlaceholder as any);+ storeState.actions.setStreaming(turnSessionId, {+ streamingMessageId: assistantMessageId,+ streamingText: "",+ contentBlocks: [],+ isStreaming: true,+ isLoading: true,+ });+ storeState.actions.patch(turnSessionId, {+ lastUserMessageAt: Date.now(),+ draft: false,+ });+// conversationId state hasn't committed yet this tick, so force the// immediate save under the same id explicitly.// Do not dispatch to the provider until the user turn is durable. Besides@@ -598,62 +677,12 @@ export function usePiSendTransport(options: PiSendTransportOptions) {idOverride: turnSessionId,turnState: { isLoading: true, isStreaming: true },});-- // Mirror the user message + assistant placeholder DIRECTLY into the- // chat-store, synchronously. The snapshot-on-switch path reads- // `messages` from the React closure, which lags behind setMessages- // by one render cycle (React batches). If the user clicks "+ new- // chat" in that gap, the snapshot writes stale messages (without- // the freshly-sent user message) to the store. Then the router- // takes over for the now-backgrounded session and only knows about- // assistant deltas — the user comes back and sees the assistant- // reply with no preceding user message. By writing both messages- // here, the store is at least as fresh as the panel and survives- // any closure staleness.- const sidNow = piSessionIdRef.current;- if (sidNow) {- const storeState = useChatStore.getState();- if (!storeState.sessions[sidNow]) {- storeState.actions.upsert({- id: sidNow,- title: "untitled",- preview: "",- status: "streaming",- messageCount: 0,- createdAt: Date.now(),- updatedAt: Date.now(),- pinned: false,- unread: false,- });- }- storeState.actions.appendMessage(sidNow, newUserMessage as any);- const currentTitle = useChatStore.getState().sessions[sidNow]?.title;- if (displayLabel && isPlaceholderConversationTitle(currentTitle)) {- storeState.actions.patch(sidNow, { title: displayLabel });- }- storeState.actions.appendMessage(sidNow, assistantPlaceholder as any);- storeState.actions.setStreaming(sidNow, {- streamingMessageId: assistantMessageId,- streamingText: "",- contentBlocks: [],- isStreaming: true,- isLoading: true,- });- // Bump the sort key — sending a message is the one user action- // that should pull a chat to the top of the sidebar. Pi-driven- // activity (text_delta, agent_end) does NOT bump this; the- // sidebar order is otherwise stable.- storeState.actions.patch(sidNow, { lastUserMessageAt: Date.now() });- // Clear the draft flag so the chat appears in the sidebar immediately.- // Without this, navigating away before the assistant responds leaves- // the session hidden (draft:true) even though there's a user message.- storeState.actions.patch(sidNow, { draft: false });- }+ const sidNow = turnSessionId;const messageIndex = messages.filter((m) => m.role === "user").length;posthog.capture("chat_message_sent", {- provider: getActivePreset()?.provider,- model: getActivePreset()?.model,+ provider: attemptPreset?.provider,+ model: attemptPreset?.model,has_images: outgoingImages.length > 0 || !!prefillFrameId,has_context: !!prefillContext,message_index: messageIndex,@@ -676,27 +705,27 @@ export function usePiSendTransport(options: PiSendTransportOptions) {} catch (error) {console.error("Failed to fetch frame image for Pi:", error);}- setPrefillFrameId(null);+ if (isAttemptForeground()) setPrefillFrameId(null);}// The context is already part of `userMessage`; retire the composer// banner now that the durable turn owns it.if (prefillContext) {- setPrefillContext(null);+ if (isAttemptForeground()) setPrefillContext(null);}for (const img of outgoingImages) {const piImage = piImageFromDataUrl(img);if (piImage) piImages.push(piImage);}- if (shouldClearPastedImages) setPastedImages([]);+ if (shouldClearPastedImages && isAttemptForeground()) setPastedImages([]);// Always attach the bounded frontend snapshot used to recover issue// #3636. Rust knows the exact Pi subprocess state: a cold/new process// keeps this snapshot once, while a warm process strips it before RPC// so Pi's own threaded history is not duplicated.const promptMessage = promptWithConversationHistory(userMessage, messages);- piSessionSyncedRef.current = true;+ if (isAttemptForeground()) piSessionSyncedRef.current = true;// E2E test hook: capture the frontend recovery payload before Rust decides// whether the Pi subprocess still needs it.@@ -704,23 +733,25 @@ export function usePiSendTransport(options: PiSendTransportOptions) {const g = window as any;if (Array.isArray(g.__e2ePiPromptCaptures)) {g.__e2ePiPromptCaptures.push({- sessionId: piSessionIdRef.current,+ sessionId: turnSessionId,message: promptMessage,at: Date.now(),});}}- const providerPreflight = await preflightChatProvider(getActivePreset());+ const providerPreflight = await preflightChatProvider(attemptPreset);if (!providerPreflight.ok) {- piStreamingTextRef.current = "";- piMessageIdRef.current = null;- piContentBlocksRef.current = [];- setMessages((prev) =>- prev.map((m) => m.id === assistantMessageId- ? { ...m, content: providerPreflight.message, retryPrompt: userMessage }- : m)- );+ if (isAttemptForeground()) {+ piStreamingTextRef.current = "";+ piMessageIdRef.current = null;+ piContentBlocksRef.current = [];+ setMessages((prev) =>+ prev.map((m) => m.id === assistantMessageId+ ? { ...m, content: providerPreflight.message, retryPrompt: userMessage }+ : m)+ );+ }if (sidNow) {const storeState = useChatStore.getState();storeState.actions.patchMessage(sidNow, assistantMessageId, (m: any) => ({@@ -736,16 +767,15 @@ export function usePiSendTransport(options: PiSendTransportOptions) {isStreaming: false,});}- forceQueueModeRef.current = false;- setIsLoading(false);- setIsStreaming(false);+ if (isAttemptForeground()) forceQueueModeRef.current = false;+ finishAttempt();return;}// Send prompt — abort/new_session now await completion, so no retry neededconst displayPreview = queuedPreviewForText(displayLabel ?? displayUserMessage);let result = await commands.piPrompt(- piSessionIdRef.current,+ turnSessionId,promptMessage,piImages.length > 0 ? piImages : null,displayPreview,@@ -756,23 +786,25 @@ export function usePiSendTransport(options: PiSendTransportOptions) {if (result.status === "error" && result.error.includes("Pi not initialized")) {console.log("[Pi] session not registered yet — auto-spawning and retrying");try {- const dir = await piProjectDirForSession(piSessionIdRef.current);- const providerConfig = buildProviderConfig();+ const dir = await piProjectDirForSession(turnSessionId);+ const providerConfig = buildProviderConfig(attemptPreset);const startRes = await commands.piStart(- piSessionIdRef.current,+ turnSessionId,dir,settings.user?.token ?? null,providerConfig,);if (startRes.status === "ok" && startRes.data.running) {- setPiInfo(startRes.data);- piSessionSyncedRef.current = false;- if (providerConfig) {- setRunningConfigFromProviderConfig(providerConfig);+ if (isAttemptForeground()) {+ setPiInfo(startRes.data);+ piSessionSyncedRef.current = false;+ if (providerConfig) {+ setRunningConfigFromProviderConfig(providerConfig);+ }}- syncThinkingLevelAfterStart(piSessionIdRef.current);+ syncThinkingLevelAfterStart(turnSessionId);result = await commands.piPrompt(- piSessionIdRef.current,+ turnSessionId,promptMessage,piImages.length > 0 ? piImages : null,displayPreview,@@ -785,12 +817,12 @@ export function usePiSendTransport(options: PiSendTransportOptions) {if (result.status === "error") {if (timeoutId) clearTimeout(timeoutId);- piMessageIdRef.current = null;+ if (isAttemptForeground()) piMessageIdRef.current = null;// Provide helpful error messages for common failuresconst rawError = result.error;let errorMsg: string;let retryPrompt: string | undefined;- const currentPreset = getActivePreset();+ const currentPreset = attemptPreset;const providerError = buildProviderErrorPresentation(rawError, currentPreset);if (isPiPromptStartTimeout(rawError)) {@@ -814,13 +846,15 @@ export function usePiSendTransport(options: PiSendTransportOptions) {errorMsg = rawError;retryPrompt = userMessage;}- setMessages((prev) =>- prev.map((m) =>- m.id === assistantMessageId- ? { ...m, content: errorMsg, ...(retryPrompt ? { retryPrompt } : {}) }- : m- )- );+ if (isAttemptForeground()) {+ setMessages((prev) =>+ prev.map((m) =>+ m.id === assistantMessageId+ ? { ...m, content: errorMsg, ...(retryPrompt ? { retryPrompt } : {}) }+ : m+ )+ );+ }if (sidNow) {const storeState = useChatStore.getState();storeState.actions.patchMessage(sidNow, assistantMessageId, (message: any) => ({@@ -837,27 +871,28 @@ export function usePiSendTransport(options: PiSendTransportOptions) {});}posthog.capture("chat_response_error", {- provider: getActivePreset()?.provider,- model: getActivePreset()?.model,+ provider: attemptPreset?.provider,+ model: attemptPreset?.model,error_type: isPiPromptStartTimeout(rawError) ? "start_timeout" : "prompt_dispatch",...chatSendTelemetryContext(sendOptions, messageIndex, messages),});- forceQueueModeRef.current = false;- setIsLoading(false);- setIsStreaming(false);+ if (isAttemptForeground()) forceQueueModeRef.current = false;+ finishAttempt();}} catch (error) {if (timeoutId) clearTimeout(timeoutId);- piMessageIdRef.current = null;+ if (isAttemptForeground()) piMessageIdRef.current = null;const rawError = error instanceof Error ? error.message : "Unknown error";- const providerError = buildProviderErrorPresentation(rawError, getActivePreset());- setMessages((prev) =>- prev.map((m) =>- m.id === assistantMessageId- ? { ...m, content: providerError?.message || `Error: ${rawError}` }- : m- )- );+ const providerError = buildProviderErrorPresentation(rawError, attemptPreset);+ if (isAttemptForeground()) {+ setMessages((prev) =>+ prev.map((m) =>+ m.id === assistantMessageId+ ? { ...m, content: providerError?.message || `Error: ${rawError}` }+ : m+ )+ );+ }if (sidNow) {const content = providerError?.message || `Error: ${rawError}`;const storeState = useChatStore.getState();@@ -874,9 +909,8 @@ export function usePiSendTransport(options: PiSendTransportOptions) {isStreaming: false,});}- forceQueueModeRef.current = false;- setIsLoading(false);- setIsStreaming(false);+ if (isAttemptForeground()) forceQueueModeRef.current = false;+ finishAttempt();}}@@ -1002,6 +1036,10 @@ export function usePiSendTransport(options: PiSendTransportOptions) {// composer so the message is never lost.useEffect(() => {if (!pendingSend) return;+ // A pending send belongs to its original chat. Opening a new chat hides it+ // there without cancelling the background preflight, and an abort must not+ // restore the old text into the new chat's composer.+ if (pendingSend.sessionId !== piSessionIdRef.current) return;if (isLoading || isStreaming) return;const timer = window.setTimeout(() => {const stillPending = pendingSendRef.current;@@ -1013,7 +1051,7 @@ export function usePiSendTransport(options: PiSendTransportOptions) {inputRef.current?.focus();}, 0);return () => window.clearTimeout(timer);- }, [pendingSend, isLoading, isStreaming, setInput, inputRef]);+ }, [pendingSend, isLoading, isStreaming, setInput, inputRef, piSessionIdRef]);// Keep ref in sync so useEffect callbacks can call sendMessagesendMessageRef.current = sendMessage;@@ -1022,7 +1060,8 @@ export function usePiSendTransport(options: PiSendTransportOptions) {flushPendingSteerBatch,handleStop,openConnectionSetup,- pendingSend,+ pendingSend:+ pendingSend?.sessionId === piSessionIdRef.current ? pendingSend : null,sendMessage,steerMessage,steerQueuedPrompt,diff --git a/apps/screenpipe-app-tauri/e2e/specs/chat-newchat-fresh.spec.ts b/apps/screenpipe-app-tauri/e2e/specs/chat-newchat-fresh.spec.tsindex 9991b04fb..d8ab72622 100644--- a/apps/screenpipe-app-tauri/e2e/specs/chat-newchat-fresh.spec.ts+++ b/apps/screenpipe-app-tauri/e2e/specs/chat-newchat-fresh.spec.ts@@ -29,6 +29,7 @@ import { openHomeWindow, waitForAppReady, t } from "../helpers/test-utils.js";const MARKER = "E2E-NEWCHAT-FRESH-MARKER-9F2K7X";// A stable id for the "existing, non-empty" chat we foreground in Case 3.const EXISTING_CHAT = "77777777-cccc-4ccc-8ccc-cccccccccccc";+const PREFLIGHT_CHAT = "88888888-dddd-4ddd-8ddd-dddddddddddd";async function emitTauri(event: string, payload: unknown): Promise<void> {await browser.executeAsync(@@ -159,4 +160,22 @@ describe("New chat opens fresh + reuses blank (#4719)", function () {expect(second).toBe(first);expect(third).toBe(first);});++ it("Case 5: '+ new chat' works while a send is still in preflight", async () => {+ await browser.execute((sid: string) => {+ (window as any).__e2eLatchPreflightSend(sid);+ }, PREFLIGHT_CHAT);+ await waitForeground(+ (id) => id === PREFLIGHT_CHAT,+ "preflight chat never foregrounded",+ );++ await pressNewChat();+ const fresh = await waitForeground(+ (id) => id !== PREFLIGHT_CHAT,+ "'+ new chat' reused the loading preflight chat",+ );++ expect(fresh).not.toBe(PREFLIGHT_CHAT);+ });});diff --git a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.tsindex 68d33ae11..d01fdc02d 100644--- a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts+++ b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts@@ -14,6 +14,7 @@ import {selectOrderedSessions,selectRecentSwitcherSessions,getOrCreateEmptyChatId,+ isReusableBlankChatSession,dedupeSessionRecords,sessionRecordFromMeta,applyChatSessionActivity,@@ -241,6 +242,27 @@ describe("chat-store: getOrCreateEmptyChatId (no spam on +new)", () => {expect(isNew).toBe(true);});+ it("creates a new id while the panel chat is acknowledging a send", () => {+ useChatStore.setState({+ sessions: {+ panelChat: baseRecord({+ id: "panelChat",+ draft: true,+ messages: [],+ isLoading: true,+ }),+ },+ currentId: "panelChat",+ panelSessionId: "panelChat",+ });++ const { id, isNew } = getOrCreateEmptyChatId();++ expect(id).not.toBe("panelChat");+ expect(isNew).toBe(true);+ expect(isReusableBlankChatSession(useChatStore.getState().sessions.panelChat)).toBe(false);+ });+it("reuses any other empty chat (newest first) when the panel chat is full", () => {useChatStore.setState({sessions: {diff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.tsindex 8df037b2b..58f028a13 100644--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts@@ -805,30 +805,34 @@ export function sessionRecordFromMeta(m: ConversationMeta): SessionRecord {* SessionRecord doc), so a naive `messages.length === 0` check would* treat every real conversation on disk as "empty" and make "+ new* chat" hop through the sidebar instead of opening a fresh one (#4719).+ * - no loading/streaming turn — send preflight is mirrored here before its+ * first await, so New chat never reuses the session whose send is pending.* - no loaded `user` message — belt-and-suspenders for a foreground* draft that already has an unsent user turn before its count updates.** Returns `{ id, isNew }` so callers can decide whether to upsert.*/+export function isReusableBlankChatSession(s: SessionRecord): boolean {+ if (s.kind && s.kind !== "chat") return false;+ if (s.isLoading || s.isStreaming) return false;+ if (s.messageCount > 0) return false;+ const msgs = (s.messages as Array<{ role?: string }> | undefined) ?? [];+ return !msgs.some((m) => m?.role === "user");+}+export function getOrCreateEmptyChatId(): { id: string; isNew: boolean } {const state = useChatStore.getState();- const isReusableBlankChat = (s: SessionRecord) => {- if (s.kind && s.kind !== "chat") return false;- if (s.messageCount > 0) return false;- const msgs = (s.messages as Array<{ role?: string }> | undefined) ?? [];- return !msgs.some((m) => m?.role === "user");- };// Prefer the chat the user is already on.const panelId = state.panelSessionId;if (panelId) {const panel = state.sessions[panelId];- if (panel && isReusableBlankChat(panel)) return { id: panelId, isNew: false };+ if (panel && isReusableBlankChatSession(panel)) return { id: panelId, isNew: false };}// Otherwise any other blank chat, newest first by createdAt.const empties = Object.values(state.sessions)- .filter(isReusableBlankChat)+ .filter(isReusableBlankChatSession).sort((a, b) => b.createdAt - a.createdAt);if (empties.length > 0) return { id: empties[0].id, isNew: false };