Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(agent-panel): recover prompt submits failures (#10818)

Julio35411d7ca2b3
diff --git a/frontend/src/components/chat/acp/__tests__/agent-panel.test.tsx b/frontend/src/components/chat/acp/__tests__/agent-panel.test.tsx
new file mode 100644
index 0000000000..098d70ed89
--- /dev/null
+++ b/frontend/src/components/chat/acp/__tests__/agent-panel.test.tsx
@@ -0,0 +1,270 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import {
+ act,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from "@testing-library/react";
+import { Provider } from "jotai";
+import type { ComponentProps } from "react";
+import { JsonRpcError } from "use-acp";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { MockRequestClient } from "@/__mocks__/requests";
+import type { PromptInput } from "@/components/editor/ai/add-cell-with-ai";
+import { TooltipProvider } from "@/components/ui/tooltip";
+import { toast } from "@/components/ui/use-toast";
+import { pendingAiPromptAtom } from "@/core/ai/state";
+import { requestClientAtom } from "@/core/network/requests";
+import { cwdAtom, filenameAtom } from "@/core/saving/file-state";
+import { store } from "@/core/state/jotai";
+import AgentPanel from "../agent-panel";
+import { parseContextFromPrompt } from "../context-utils";
+import {
+ addSession,
+ agentSessionStateAtom,
+ selectedTabAtom,
+ updateSessionExternalAgentSessionId,
+} from "../state";
+import type { ExternalAgentSessionId } from "../types";
+
+const { agent, client } = vi.hoisted(() => {
+ const agent = {
+ initialize: vi.fn().mockResolvedValue({}),
+ prompt: vi.fn().mockResolvedValue({ stopReason: "end_turn" }),
+ cancel: vi.fn().mockResolvedValue({}),
+ };
+ return {
+ agent,
+ client: {
+ agent,
+ activeSessionId: "session-1",
+ connectionState: { status: "connected" },
+ notifications: [],
+ connect: vi.fn().mockResolvedValue(undefined),
+ disconnect: vi.fn(),
+ setActiveSessionId: vi.fn(),
+ },
+ };
+});
+
+vi.mock("use-acp", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("use-acp")>()),
+ useAcpClient: () => client,
+}));
+
+vi.mock("@/components/editor/ai/add-cell-with-ai", () => ({
+ PromptInput: ({
+ value,
+ onChange,
+ onAddFiles,
+ onSubmit,
+ }: ComponentProps<typeof PromptInput>) => (
+ <>
+ <textarea
+ aria-label="Prompt"
+ value={value}
+ onChange={(event) => onChange(event.target.value)}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ onSubmit(undefined, value);
+ }
+ }}
+ />
+ <input
+ type="file"
+ aria-label="Attachment"
+ onChange={(event) => onAddFiles?.([...(event.target.files ?? [])])}
+ />
+ </>
+ ),
+}));
+vi.mock("../session-tabs", () => ({ SessionTabs: () => null }));
+vi.mock("../blocks", () => ({ ReadyToChatBlock: () => null }));
+vi.mock("../context-utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../context-utils")>()),
+ parseContextFromPrompt: vi
+ .fn()
+ .mockResolvedValue({ contextBlocks: [], attachmentBlocks: [] }),
+}));
+vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));
+
+function renderPanel() {
+ return render(
+ <Provider store={store}>
+ <TooltipProvider>
+ <AgentPanel />
+ </TooltipProvider>
+ </Provider>,
+ );
+}
+
+function submitPrompt(prompt = "Explain this notebook") {
+ fireEvent.change(screen.getByRole("textbox", { name: "Prompt" }), {
+ target: { value: prompt },
+ });
+ fireEvent.keyDown(screen.getByRole("textbox", { name: "Prompt" }), {
+ key: "Enter",
+ });
+}
+
+describe("AgentPanel prompt submission", () => {
+ beforeEach(() => {
+ agent.prompt.mockReset().mockResolvedValue({ stopReason: "end_turn" });
+ vi.mocked(parseContextFromPrompt).mockReset().mockResolvedValue({
+ contextBlocks: [],
+ attachmentBlocks: [],
+ });
+ store.set(requestClientAtom, MockRequestClient.create());
+ store.set(filenameAtom, "notebook.py");
+ store.set(cwdAtom, "/notebooks");
+ store.set(pendingAiPromptAtom, null);
+ store.set(
+ agentSessionStateAtom,
+ updateSessionExternalAgentSessionId(
+ addSession({ sessions: [], activeTabId: null }, { agentId: "claude" }),
+ "session-1" as ExternalAgentSessionId,
+ ),
+ );
+ });
+
+ afterEach(() => {
+ store.set(pendingAiPromptAtom, null);
+ store.set(agentSessionStateAtom, { sessions: [], activeTabId: null });
+ store.set(filenameAtom, null);
+ store.set(cwdAtom, null);
+ });
+
+ it("preserves the draft, attachment and title when the notebook is unnamed", async () => {
+ store.set(filenameAtom, null);
+ renderPanel();
+ const title = store.get(selectedTabAtom)?.title;
+ fireEvent.change(screen.getByLabelText("Attachment"), {
+ target: {
+ files: [new File(["notes"], "notes.txt", { type: "text/plain" })],
+ },
+ });
+ submitPrompt();
+
+ expect(toast).toHaveBeenCalledWith({
+ title: "Notebook must be named",
+ description: "Please name the notebook to use the agent",
+ variant: "danger",
+ });
+ expect(agent.prompt).not.toHaveBeenCalled();
+ expect(screen.getByRole("textbox", { name: "Prompt" })).toHaveValue(
+ "Explain this notebook",
+ );
+ expect(screen.getByText("notes.txt")).toBeInTheDocument();
+ expect(store.get(selectedTabAtom)?.title).toBe(title);
+ expect(screen.queryByText("Agent is working...")).not.toBeInTheDocument();
+
+ act(() => store.set(filenameAtom, "notebook.py"));
+ fireEvent.keyDown(screen.getByRole("textbox", { name: "Prompt" }), {
+ key: "Enter",
+ });
+ await waitFor(() => expect(agent.prompt).toHaveBeenCalledOnce());
+ expect(agent.prompt).toHaveBeenCalledWith({
+ sessionId: "session-1",
+ prompt: expect.arrayContaining([
+ { type: "text", text: "Explain this notebook" },
+ expect.objectContaining({ type: "resource_link", name: "notes.txt" }),
+ ]),
+ });
+ await waitFor(() =>
+ expect(screen.queryByText("Agent is working...")).not.toBeInTheDocument(),
+ );
+ });
+
+ it.each([new Error("Agent unavailable"), "Agent unavailable"])(
+ "shows a rejected prompt and allows another submission after dismissal (%s)",
+ async (error) => {
+ agent.prompt.mockRejectedValueOnce(error);
+ renderPanel();
+ submitPrompt();
+
+ expect(await screen.findByText(/Agent unavailable/)).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "Dismiss" }));
+ expect(screen.queryByText(/Agent unavailable/)).not.toBeInTheDocument();
+ submitPrompt("Try again");
+ await waitFor(() => expect(agent.prompt).toHaveBeenCalledTimes(2));
+ await waitFor(() =>
+ expect(
+ screen.queryByText("Agent is working..."),
+ ).not.toBeInTheDocument(),
+ );
+ },
+ );
+
+ it("preserves the existing authentication error action and detail", async () => {
+ agent.prompt.mockRejectedValueOnce(
+ new JsonRpcError({
+ code: -32_000,
+ message: "Authentication required",
+ data: { message: "Sign in to the agent" },
+ }),
+ );
+ renderPanel();
+ submitPrompt();
+
+ expect(await screen.findByText("Sign in to the agent")).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Restart session" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Dismiss" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("clears busy state and reports failures while preparing prompt context", async () => {
+ vi.mocked(parseContextFromPrompt).mockRejectedValueOnce(
+ new Error("Context unavailable"),
+ );
+ renderPanel();
+ submitPrompt();
+
+ expect(await screen.findByText("Context unavailable")).toBeInTheDocument();
+ expect(agent.prompt).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole("button", { name: "Dismiss" }));
+ expect(screen.queryByText("Agent is working...")).not.toBeInTheDocument();
+ submitPrompt();
+ await waitFor(() => expect(agent.prompt).toHaveBeenCalledOnce());
+ });
+
+ it("reports failures from a queued prompt through the same handler", async () => {
+ agent.prompt.mockRejectedValueOnce(new Error("Queued prompt failed"));
+ renderPanel();
+ act(() =>
+ store.set(pendingAiPromptAtom, {
+ prompt: "Help with this cell",
+ submit: true,
+ }),
+ );
+
+ expect(await screen.findByText("Queued prompt failed")).toBeInTheDocument();
+ expect(store.get(pendingAiPromptAtom)).toBeNull();
+ expect(agent.prompt).toHaveBeenCalledWith({
+ sessionId: "session-1",
+ prompt: expect.arrayContaining([
+ { type: "text", text: "Help with this cell" },
+ ]),
+ });
+ });
+
+ it("clears busy state when a cancelled prompt settles", async () => {
+ const response = Promise.withResolvers<{ stopReason: string }>();
+ agent.prompt.mockReturnValueOnce(response.promise);
+ renderPanel();
+ submitPrompt();
+ await waitFor(() => expect(agent.prompt).toHaveBeenCalledOnce());
+ expect(screen.getByText("Agent is working...")).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "Stop" }));
+ await act(async () => response.resolve({ stopReason: "cancelled" }));
+ expect(agent.cancel).toHaveBeenCalledWith({ sessionId: "session-1" });
+ expect(screen.queryByText("Agent is working...")).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Dismiss" }),
+ ).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/components/chat/acp/agent-panel.tsx b/frontend/src/components/chat/acp/agent-panel.tsx
index ed0eab01cf..777af88e05 100644
--- a/frontend/src/components/chat/acp/agent-panel.tsx
+++ b/frontend/src/components/chat/acp/agent-panel.tsx
@@ -902,6 +902,18 @@ const AgentPanel: React.FC = () => {
return;
}
+ let absoluteFilename: string;
+ try {
+ absoluteFilename = getAbsoluteFilename();
+ } catch {
+ toast({
+ title: "Notebook must be named",
+ description: "Please name the notebook to use the agent",
+ variant: "danger",
+ });
+ return;
+ }
+
logger.debug("Submitting prompt to agent", {
sessionId: activeSessionId,
});
@@ -914,62 +926,51 @@ const AgentPanel: React.FC = () => {
setSessionState((prev) => updateSessionTitle(prev, prompt));
}
- let absoluteFilename: string;
try {
- absoluteFilename = getAbsoluteFilename();
- } catch {
- toast({
- title: "Notebook must be named",
- description: "Please name the notebook to use the agent",
- variant: "danger",
- });
- return;
- }
-
- const promptBlocks: ContentBlock[] = [{ type: "text", text: prompt }];
+ const promptBlocks: ContentBlock[] = [{ type: "text", text: prompt }];
- // Parse context from the prompt
- const { contextBlocks, attachmentBlocks } =
- await parseContextFromPrompt(prompt);
- promptBlocks.push(...contextBlocks, ...attachmentBlocks);
+ // Parse context from the prompt
+ const { contextBlocks, attachmentBlocks } =
+ await parseContextFromPrompt(prompt);
+ promptBlocks.push(...contextBlocks, ...attachmentBlocks);
- // Add manually uploaded files as resource links
- if (files && files.length > 0) {
- const fileResourceLinks = await convertFilesToResourceLinks(files);
- promptBlocks.push(...fileResourceLinks);
- }
+ // Add manually uploaded files as resource links
+ if (files && files.length > 0) {
+ const fileResourceLinks = await convertFilesToResourceLinks(files);
+ promptBlocks.push(...fileResourceLinks);
+ }
- const hasGivenRules = notifications.some(
- (notification) =>
- notification.type === "session_notification" &&
- notification.data.update.sessionUpdate === "user_message_chunk",
- );
- if (!hasGivenRules) {
- promptBlocks.push(
- {
- type: "resource_link",
- uri: absoluteFilename,
- mimeType: "text/x-python",
- name: absoluteFilename,
- },
- {
- type: "resource",
- resource: {
- uri: "marimo_rules.md",
- mimeType: "text/plain",
- text: getAgentPrompt(absoluteFilename),
- },
- },
+ const hasGivenRules = notifications.some(
+ (notification) =>
+ notification.type === "session_notification" &&
+ notification.data.update.sessionUpdate === "user_message_chunk",
);
- }
+ if (!hasGivenRules) {
+ promptBlocks.push(
+ {
+ type: "resource_link",
+ uri: absoluteFilename,
+ mimeType: "text/x-python",
+ name: absoluteFilename,
+ },
+ {
+ type: "resource",
+ resource: {
+ uri: "marimo_rules.md",
+ mimeType: "text/plain",
+ text: getAgentPrompt(absoluteFilename),
+ },
+ },
+ );
+ }
- try {
await agent.prompt({
sessionId: activeSessionId,
prompt: promptBlocks,
});
} catch (error) {
logger.error("Failed to send prompt", { error });
+ setError(error instanceof Error ? error : String(error));
} finally {
setIsLoading(false);
}