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): show terminal errors and correct hosted GLM limits (#7080)

Ezra Ellette217b37714d2e
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/pi-terminal-error.test.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/pi-terminal-error.test.tsx
new file mode 100644
index 000000000..29c449d94
--- /dev/null
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/__tests__/pi-terminal-error.test.tsx
@@ -0,0 +1,129 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+import { useRef, useState } from "react";
+import { act, renderHook } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { Message } from "@/lib/chat/types";
+import { buildContextOverflowMessage } from "@/lib/chat/provider-errors";
+import { usePiChatState } from "../use-pi-chat-state";
+import { usePiForegroundEvents } from "../use-pi-foreground-events";
+
+const storeMessages = vi.hoisted(() => vi.fn());
+vi.mock("@tauri-apps/api/event", () => ({ emit: vi.fn(async () => {}) }));
+vi.mock("@/lib/stores/chat-store", () => ({
+ useChatStore: { getState: () => ({
+ sessions: {},
+ actions: { patch: vi.fn(), setStreaming: vi.fn(), setMessages: storeMessages },
+ }) },
+}));
+vi.mock("@/lib/events/bus", () => ({
+ mountAgentEventBus: vi.fn(async () => {}),
+ onTerminated: vi.fn(() => () => {}),
+}));
+vi.mock("../pi-log-listener", () => ({ registerPiLogListener: vi.fn(async () => () => {}) }));
+vi.mock("../pi-reauth-listener", () => ({ registerPiReauthListener: vi.fn(async () => () => {}) }));
+
+const progress = "I'll look at the repo to see what it actually contains.";
+const overflow = "400 request (33252 tokens) exceeds the available context size (32768 tokens), try increasing it";
+const initial: Message = {
+ id: "assistant-1", role: "assistant", content: progress, timestamp: 0,
+ contentBlocks: [
+ { type: "text", text: progress, phase: "commentary" },
+ { type: "tool", toolCall: { id: "search-1", toolName: "sp_web_search", args: {}, isRunning: false } },
+ ],
+};
+
+function useErrorHarness() {
+ const pi = usePiChatState();
+ const [messages, setMessages] = useState<Message[]>([initial]);
+ const messagesRef = useRef(messages);
+ messagesRef.current = messages;
+ const handler = useRef<((event: unknown) => void) | null>(null);
+ usePiForegroundEvents({
+ ...pi,
+ activePreset: undefined,
+ buildProviderConfig: () => null,
+ cancelStreamingMessageRender: vi.fn(),
+ clearPipeExecution: vi.fn(),
+ consumeQueuedDisplayForStartedMessage: () => undefined,
+ findTurnIntentForUserStart: () => undefined,
+ flushPendingSteerBatch: async () => {},
+ flushStreamingMessageRender: vi.fn(),
+ handleAgentEventDataRef: handler,
+ handleAgentActionEvent: () => false,
+ clearAgentActionsForSession: vi.fn(),
+ handleInvalidatedAuthToken: vi.fn(),
+ lastUserMessageRef: useRef("Explain this repo"),
+ markTurnIntentConsumed: vi.fn(),
+ messages, messagesRef, setMessages,
+ mountedRef: useRef(true),
+ optimisticSteerRef: useRef(null),
+ pendingNextPiUserDisplayRef: useRef(null),
+ pendingNextPiUserIntentRef: useRef(null),
+ pendingSteerBatchRef: useRef([]),
+ saveConversation: async () => {},
+ scheduleStreamingMessageRender: vi.fn(),
+ setIsLoading: vi.fn(), setIsStreaming: vi.fn(),
+ settings: {} as never,
+ syncThinkingLevelAfterStart: vi.fn(),
+ turnIntentLedgerRef: useRef([]),
+ turnIntentTextValuesMatch: () => false,
+ noteTurnLivenessEvent: vi.fn(),
+ });
+ return { pi, messages, setMessages, handler };
+}
+
+describe("Pi terminal provider errors", () => {
+ beforeEach(() => vi.useFakeTimers());
+ afterEach(() => {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ });
+ it.each([false, true])("retains an error after tool progress (earlier error events: %s)", (emitEarlierError) => {
+ const { result } = renderHook(useErrorHarness);
+ const errorMessage = { role: "assistant", stopReason: "error", errorMessage: overflow, content: [] };
+ const sessionId = result.current.pi.piSessionIdRef.current;
+
+ act(() => {
+ const { pi, handler } = result.current;
+ pi.piMessageIdRef.current = initial.id;
+ pi.piStreamingTextRef.current = progress;
+ pi.piContentBlocksRef.current = [...initial.contentBlocks!];
+ if (emitEarlierError) {
+ handler.current!({ type: "message_start", message: errorMessage });
+ handler.current!({ type: "message_end", message: errorMessage });
+ }
+ handler.current!({ type: "agent_end", messages: [
+ { role: "assistant", stopReason: "toolUse", content: [{ type: "text", text: progress }] },
+ errorMessage,
+ ] });
+ });
+
+ expect(result.current.messages[0]).toMatchObject({
+ content: `Error: ${buildContextOverflowMessage(overflow)}`,
+ retryPrompt: "Explain this repo",
+ contentBlocks: initial.contentBlocks,
+ });
+ expect(storeMessages).toHaveBeenLastCalledWith(sessionId, result.current.messages);
+ expect(result.current.pi.piMessageIdRef.current).toBeNull();
+ });
+
+ it("clears the retry action when the provider recovers and answers", () => {
+ const { result } = renderHook(useErrorHarness);
+ const answer = "Here is how to run the repository.";
+ act(() => {
+ result.current.setMessages([{ ...initial, content: buildContextOverflowMessage(overflow), retryPrompt: "Explain this repo" }]);
+ result.current.pi.piMessageIdRef.current = initial.id;
+ result.current.pi.piStreamingTextRef.current = answer;
+ result.current.pi.piContentBlocksRef.current = [
+ ...initial.contentBlocks!, { type: "text", text: answer },
+ ];
+ result.current.handler.current!({ type: "agent_end", messages: [
+ { role: "assistant", stopReason: "stop", content: [{ type: "text", text: answer }] },
+ ] });
+ });
+ expect(result.current.messages[0].content).toBe(answer);
+ expect(result.current.messages[0].retryPrompt).toBeUndefined();
+ });
+});
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-foreground-events.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-foreground-events.ts
index 1b1fec1f6..3e719803f 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-foreground-events.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-foreground-events.ts
@@ -1183,13 +1183,9 @@ export function usePiForegroundEvents({
const agentEndProviderError = agentEndError
? buildProviderErrorPresentation(agentEndError, getActivePreset())
: null;
- if (agentEndProviderError?.kind === "safety_refusal") {
- // A provider can emit partial text before its terminal refusal.
- // Keep the refusal note visible instead of finalizing that partial
- // text as though the turn completed successfully.
- content = agentEndProviderError.message;
- // Surface credits_exhausted / rate limit / connection errors from agent_end
- } else if (agentEndError && !content) {
+ // Progress text is not a successful answer. Preserve terminal
+ // failures even when the model narrated its work before using tools.
+ if (agentEndError) {
const errStr = agentEndError;
const quotaErrorType = classifyQuotaError(errStr);
if (isInvalidatedAuthTokenError(errStr)) {
@@ -1204,8 +1200,9 @@ export function usePiForegroundEvents({
} else if (errStr.includes("model_not_allowed")) {
content = buildModelNotAllowedMessage(errStr);
} else {
- content = buildProviderErrorPresentation(errStr, getActivePreset())?.message || errStr;
+ content = agentEndProviderError?.message || errStr;
}
+ content = content.startsWith("Error:") ? content : `Error: ${content}`;
}
// Snapshot refs BEFORE setMessages — React's batching may defer the
@@ -1223,6 +1220,7 @@ export function usePiForegroundEvents({
);
const isQualifiedChatResult =
!wasStoppedByUser &&
+ !agentEndError &&
!piLastErrorRef.current &&
hasNonEmptyChatResult;
@@ -1241,7 +1239,7 @@ export function usePiForegroundEvents({
existing?.content?.includes("safety policy") ||
existing?.content?.includes("chat is too long") ||
existing?.content?.startsWith("Error:");
- if (isErrorMessage) {
+ if (isErrorMessage && !agentEndError) {
return prev;
}
// Don't overwrite if we have no new content and existing isn't "Processing..."
@@ -1287,7 +1285,7 @@ export function usePiForegroundEvents({
}
// Add text as a content block if no text block exists yet
const hasTextBlock = contentBlocks.some((b) => b.type === "text");
- if (!streamedText && content && !hasTextBlock) {
+ if (!streamedText && content && !hasTextBlock && !agentEndError) {
contentBlocks.push({ type: "text", text: content });
}
const nextMessages = prev.map((m) => m.id === msgId
@@ -1295,6 +1293,9 @@ export function usePiForegroundEvents({
...m,
content,
contentBlocks,
+ retryPrompt: agentEndProviderError?.retryable
+ ? lastUserMessageRef.current || undefined
+ : undefined,
...(wasStoppedByUser
? {
workDurationMs: Math.max(1, Date.now() - m.timestamp),
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/message-content.answer-recovery.test.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/message-content.answer-recovery.test.tsx
index 7e9eb9197..080e27c1f 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/message-content.answer-recovery.test.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/message-content.answer-recovery.test.tsx
@@ -7,6 +7,7 @@ import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import type { ContentBlock, Message } from "@/lib/chat/types";
import { MessageContent } from "./message-content";
+import { buildContextOverflowMessage } from "@/lib/chat/provider-errors";
function assistantMessage(id: string, contentBlocks: ContentBlock[]): Message {
return {
@@ -29,6 +30,41 @@ const completedTool = (id: string): ContentBlock => ({
});
describe("MessageContent assistant answer recovery", () => {
+ it("shows the provider error after progress and completed tools", () => {
+ const error = buildContextOverflowMessage(
+ "400 request (33252 tokens) exceeds the available context size (32768 tokens), try increasing it",
+ );
+ const message = {
+ ...assistantMessage("m-context-overflow", [
+ { type: "text", text: "I'll look at the repo." },
+ completedTool("tool-search"),
+ ]),
+ content: error,
+ retryPrompt: "Explain this repo",
+ };
+
+ const { rerender } = render(<MessageContent message={message} />);
+ expect(screen.getByRole("alert")).toHaveTextContent(error);
+ expect(screen.getByText("I'll look at the repo.")).toBeInTheDocument();
+ expect(screen.getByText("Failed")).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: "Try again" })).toBeInTheDocument();
+ expect(screen.queryByText(/^Done in /)).not.toBeInTheDocument();
+
+ // Non-retryable terminal failures use the existing Error: marker too.
+ rerender(<MessageContent message={{ ...message, content: `Error: ${error}`, retryPrompt: undefined }} />);
+ expect(screen.getByRole("alert")).toHaveTextContent(error);
+ expect(screen.queryByRole("button", { name: "Try again" })).not.toBeInTheDocument();
+ });
+
+ it("does not repeat an error already rendered as a text block", () => {
+ const error = "The provider request failed.";
+ render(<MessageContent message={{
+ ...assistantMessage("m-visible-error", [completedTool("tool-1"), { type: "text", text: error }]),
+ content: `Error: ${error}`,
+ }} />);
+ expect(screen.getAllByText(error)).toHaveLength(1);
+ });
+
it("keeps every progress update visible when a completed turn ends on a tool", () => {
const message = assistantMessage("m-recovered-answer", [
{ type: "text", text: "First I will inspect the files." },
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/message-content.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/message-content.tsx
index 7dccced3f..367d68bf6 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/message-content.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/message-content.tsx
@@ -1846,8 +1846,13 @@ export function MessageContent({
const interruptedSummary = message.interruptedByQuit && hasToolWorkGroup
? "interrupted — app closed mid-task"
: undefined;
- const workSummaryOverride = stoppedSummary || interruptedSummary;
- const recoveredWithAnswer = !isGenerating && displayGroups.some(
+ const failedSummary = !isGenerating && isErrorMessage ? "Failed" : undefined;
+ const workSummaryOverride = stoppedSummary || interruptedSummary || failedSummary;
+ const errorText = isErrorMessage ? message.content.replace(/^Error:\s*/, "") : "";
+ const hasVisibleError = displayGroups.some(
+ (group) => group.type === "text" && group.text.replace(/^Error:\s*/, "") === errorText,
+ );
+ const recoveredWithAnswer = !isGenerating && !isErrorMessage && displayGroups.some(
(group) => group.type === "text" && group.phase === "final_answer",
);
return (
@@ -1977,6 +1982,11 @@ export function MessageContent({
})}
<RichResultCards results={richResults} onOpen={onOpenRichResult} />
{sourceFooter}
+ {errorText && !hasVisibleError && (
+ <div role="alert">
+ <MarkdownBlock {...markdownOptions} text={errorText} isUser={false} />
+ </div>
+ )}
{retryCta}
</div>
);
diff --git a/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts b/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
index 1e6331805..a1033047b 100644
--- a/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/__tests__/provider-errors.test.ts
@@ -403,6 +403,13 @@ describe("provider error copy", () => {
);
});
+ it("shows hosted GLM's limit without directing users to a hidden manual setting", () => {
+ expect(buildProviderErrorMessage(
+ "400 request (33252 tokens) exceeds the available context size (32768 tokens), try increasing it",
+ { provider: "screenpipe-cloud", model: "glm-5.3-flash-reap50-iq3m" },
+ )).toBe("This model accepts 32,768 context tokens, but the request used 33,252. Start a new chat or choose a model with a larger context window, then retry.");
+ });
+
it("points larger-context providers at the matching preset value", () => {
const raw = "request (70000 tokens) exceeds the available context size (65536 tokens)";
expect(
diff --git a/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts b/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
index d2ffa035f..b8eab7ce3 100644
--- a/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
+++ b/apps/screenpipe-app-tauri/lib/chat/provider-errors.ts
@@ -154,8 +154,14 @@ function parseContextTokenCounts(errorStr: string): {
return { requested: parse(requested), available: parse(available) };
}
-export function buildContextOverflowMessage(errorStr = ""): string {
+export function buildContextOverflowMessage(errorStr = "", preset?: ProviderLike | null): string {
const { requested, available } = parseContextTokenCounts(errorStr);
+ if (isHostedScreenpipeProvider(preset?.provider)) {
+ const detail = requested && available
+ ? `This model accepts ${tokenFormatter.format(available)} context tokens, but the request used ${tokenFormatter.format(requested)}.`
+ : "This chat is too long for the selected model.";
+ return `${detail} Start a new chat or choose a model with a larger context window, then retry.`;
+ }
if (requested && available) {
if (available < MIN_AGENT_CONTEXT_TOKENS) {
return `This provider exposes ${tokenFormatter.format(available)} context tokens, but Screenpipe's agent request already needs ${tokenFormatter.format(requested)}. Increase the provider's context window (for example, Ollama num_ctx) to at least ${tokenFormatter.format(MIN_AGENT_CONTEXT_TOKENS)}, or choose a larger-context model, then retry in a new chat.`;
@@ -265,7 +271,7 @@ function buildGenericProviderErrorMessage(
const normalized = errorStr.toLowerCase();
if (isContextOverflowError(errorStr)) {
- return buildContextOverflowMessage(errorStr);
+ return buildContextOverflowMessage(errorStr, preset);
}
// ChatGPT OAuth tokens from Enterprise/Business workspaces can lack the
diff --git a/apps/screenpipe-app-tauri/lib/model-metadata.test.ts b/apps/screenpipe-app-tauri/lib/model-metadata.test.ts
index b6118ddc5..aa5c603b0 100644
--- a/apps/screenpipe-app-tauri/lib/model-metadata.test.ts
+++ b/apps/screenpipe-app-tauri/lib/model-metadata.test.ts
@@ -35,6 +35,25 @@ describe("model metadata resolution", () => {
expect(resolveModelLimits("custom", "gpt-5.6-terra")).toBeNull();
});
+ it("corrects saved hosted GLM limits without applying them to other deployments", () => {
+ const preset = {
+ provider: "screenpipe-cloud",
+ model: "glm-5.3-flash-reap50-iq3m",
+ maxContextChars: 512_000,
+ maxTokens: 16_384,
+ };
+ expect(applyResolvedModelLimits(preset)).toMatchObject({
+ maxContextChars: 131_072,
+ maxTokens: 8_192,
+ });
+ expect(resolveModelLimits("custom", preset.model)).toBeNull();
+ expect(resolveModelLimits("screenpipe-cloud", "glm-5")).toBeNull();
+ expect(resolveModelLimits(preset.provider, preset.model, {
+ context_window: 65_536,
+ max_output_tokens: 16_384,
+ })).toEqual({ contextWindow: 65_536, maxOutputTokens: 16_384 });
+ });
+
it("uses provider discovery metadata when it is available", () => {
expect(
metadataFromDiscovery({
diff --git a/apps/screenpipe-app-tauri/lib/model-metadata.ts b/apps/screenpipe-app-tauri/lib/model-metadata.ts
index 51894ddbb..ba785294d 100644
--- a/apps/screenpipe-app-tauri/lib/model-metadata.ts
+++ b/apps/screenpipe-app-tauri/lib/model-metadata.ts
@@ -122,6 +122,10 @@ export function resolveModelLimits(
if (!provider || !model) return null;
const normalized = model.toLowerCase();
+ if (provider === "screenpipe-cloud" && normalized === "glm-5.3-flash-reap50-iq3m") {
+ // The hosted deployment has a smaller window than the GLM model family.
+ return { contextWindow: 32_768, maxOutputTokens: 8_192 };
+ }
const table =
provider === "openai"
? OPENAI_MODEL_LIMITS