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 pdb arrow navigation (#8543)

Shahmir Varqha30ebab05c5bc
diff --git a/frontend/src/components/editor/output/console/ConsoleOutput.tsx b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
index bece78ff80..61c07b6138 100644
--- a/frontend/src/components/editor/output/console/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
@@ -18,7 +18,10 @@ import { isInternalCellName } from "@/core/cells/names";
import { useExpandedConsoleOutput } from "@/core/cells/outputs";
import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
-import { useInputHistory } from "@/hooks/useInputHistory";
+import {
+ type UseInputHistoryReturn,
+ useInputHistory,
+} from "@/hooks/useInputHistory";
import { useOverflowDetection } from "@/hooks/useOverflowDetection";
import { useSelectAllContent } from "@/hooks/useSelectAllContent";
import { cn } from "@/utils/cn";
@@ -54,6 +57,11 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
const ref = React.useRef<HTMLDivElement>(null);
const { wrapText, setWrapText } = useWrapText();
const [isExpanded, setIsExpanded] = useExpandedConsoleOutput(props.cellId);
+ const [stdinValue, setStdinValue] = React.useState("");
+ const inputHistory = useInputHistory({
+ value: stdinValue,
+ setValue: setStdinValue,
+ });
const {
consoleOutputs,
stale,
@@ -210,6 +218,9 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
isPassword={isPassword}
onSubmit={(text) => onSubmitDebugger(text, originalIdx)}
onClear={onClear}
+ value={stdinValue}
+ setValue={setStdinValue}
+ inputHistory={inputHistory}
/>
);
}
@@ -249,25 +260,32 @@ const StdInput = (props: {
onSubmit: (text: string) => void;
onClear?: () => void;
output: string;
- response?: string;
isPdb: boolean;
isPassword?: boolean;
+ value: string;
+ setValue: (value: string) => void;
+ inputHistory: UseInputHistoryReturn;
}) => {
- const [value, setValue] = React.useState("");
-
- const { navigateUp, navigateDown, addToHistory } = useInputHistory({
+ const {
value,
setValue,
- });
+ inputHistory,
+ output,
+ isPassword,
+ isPdb,
+ onSubmit,
+ onClear,
+ } = props;
+ const { navigateUp, navigateDown, addToHistory } = inputHistory;
return (
<div className="flex gap-2 items-center pt-2">
- {renderText(props.output)}
+ {renderText(output)}
<Input
data-testid="console-input"
// This is used in <StdinBlockingAlert> to find the input
data-stdin-blocking={true}
- type={props.isPassword ? "password" : "text"}
+ type={isPassword ? "password" : "text"}
autoComplete="off"
autoFocus={true}
value={value}
@@ -292,7 +310,7 @@ const StdInput = (props: {
if (e.key === "Enter" && !e.shiftKey) {
if (value) {
addToHistory(value);
- props.onSubmit(value);
+ onSubmit(value);
setValue("");
}
e.preventDefault();
@@ -306,9 +324,7 @@ const StdInput = (props: {
}
}}
/>
- {props.isPdb && (
- <DebuggerControls onSubmit={props.onSubmit} onClear={props.onClear} />
- )}
+ {isPdb && <DebuggerControls onSubmit={onSubmit} onClear={onClear} />}
</div>
);
};
diff --git a/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx b/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
index c1a5b9e782..cfef1f3c52 100644
--- a/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
+++ b/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
@@ -1,9 +1,10 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { render, screen } from "@testing-library/react";
-import { describe, expect, it } from "vitest";
+import { fireEvent, render, screen } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
import { TooltipProvider } from "@/components/ui/tooltip";
import type { CellId } from "@/core/cells/ids";
+import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
import { ConsoleOutput } from "../ConsoleOutput";
@@ -20,6 +21,10 @@ global.ResizeObserver = class ResizeObserver {
}
};
+const renderWithProvider = (ui: React.ReactElement) => {
+ return render(<TooltipProvider>{ui}</TooltipProvider>);
+};
+
describe("ConsoleOutput integration", () => {
const createOutput = (data: string, channel = "stdout"): OutputMessage => ({
channel: channel as "stdout" | "stderr",
@@ -31,7 +36,7 @@ describe("ConsoleOutput integration", () => {
const defaultProps = {
cellId: "cell-1" as CellId,
cellName: "test_cell",
- consoleOutputs: [],
+ consoleOutputs: [] as WithResponse<OutputMessage>[],
stale: false,
debuggerActive: false,
onSubmitDebugger: () => {
@@ -50,14 +55,151 @@ describe("ConsoleOutput integration", () => {
],
};
- render(
- <TooltipProvider>
- <ConsoleOutput {...props} />
- </TooltipProvider>,
- );
+ renderWithProvider(<ConsoleOutput {...props} />);
const link = screen.getByRole("link", { name: "https://marimo.io" });
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute("href", "https://marimo.io");
});
});
+
+describe("ConsoleOutput pdb history", () => {
+ const defaultProps = {
+ cellId: "cell-1" as CellId,
+ cellName: "test_cell",
+ consoleOutputs: [] as WithResponse<OutputMessage>[],
+ stale: false,
+ debuggerActive: false,
+ onSubmitDebugger: vi.fn(),
+ };
+
+ const stdinPrompt = (
+ data: string,
+ response?: string,
+ ): WithResponse<OutputMessage> => ({
+ channel: "stdin" as const,
+ mimetype: "text/plain",
+ data,
+ timestamp: 0,
+ response,
+ });
+
+ it("should persist command history across StdInput remounts", () => {
+ // Initial state: pdb prompt waiting for input
+ const outputs1: WithResponse<OutputMessage>[] = [stdinPrompt("(Pdb) ")];
+
+ const onSubmitDebugger = vi.fn();
+ const { rerender } = renderWithProvider(
+ <ConsoleOutput
+ {...defaultProps}
+ consoleOutputs={outputs1}
+ onSubmitDebugger={onSubmitDebugger}
+ />,
+ );
+
+ const input = screen.getByTestId("console-input");
+
+ // Type "next" and submit
+ fireEvent.change(input, { target: { value: "next" } });
+ fireEvent.keyDown(input, { key: "Enter" });
+
+ expect(onSubmitDebugger).toHaveBeenCalledWith("next", 0);
+
+ // Simulate server response: old stdin gets a response, new stdin prompt appears
+ const outputs2: WithResponse<OutputMessage>[] = [
+ stdinPrompt("(Pdb) ", "next"),
+ stdinPrompt("(Pdb) "),
+ ];
+
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput
+ {...defaultProps}
+ consoleOutputs={outputs2}
+ onSubmitDebugger={onSubmitDebugger}
+ />
+ </TooltipProvider>,
+ );
+
+ // New StdInput mounted — press ArrowUp to recall previous command
+ const newInput = screen.getByTestId("console-input");
+ fireEvent.keyDown(newInput, { key: "ArrowUp" });
+
+ expect(newInput).toHaveValue("next");
+ });
+
+ it("should navigate through multiple history entries across remounts", () => {
+ const onSubmitDebugger = vi.fn();
+
+ // First prompt
+ const outputs1: WithResponse<OutputMessage>[] = [stdinPrompt("(Pdb) ")];
+
+ const { rerender } = renderWithProvider(
+ <ConsoleOutput
+ {...defaultProps}
+ consoleOutputs={outputs1}
+ onSubmitDebugger={onSubmitDebugger}
+ />,
+ );
+
+ // Submit "step"
+ let input = screen.getByTestId("console-input");
+ fireEvent.change(input, { target: { value: "step" } });
+ fireEvent.keyDown(input, { key: "Enter" });
+
+ // Second prompt
+ const outputs2: WithResponse<OutputMessage>[] = [
+ stdinPrompt("(Pdb) ", "step"),
+ stdinPrompt("(Pdb) "),
+ ];
+
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput
+ {...defaultProps}
+ consoleOutputs={outputs2}
+ onSubmitDebugger={onSubmitDebugger}
+ />
+ </TooltipProvider>,
+ );
+
+ // Submit "print(x)"
+ input = screen.getByTestId("console-input");
+ fireEvent.change(input, { target: { value: "print(x)" } });
+ fireEvent.keyDown(input, { key: "Enter" });
+
+ // Third prompt
+ const outputs3: WithResponse<OutputMessage>[] = [
+ stdinPrompt("(Pdb) ", "step"),
+ stdinPrompt("(Pdb) ", "print(x)"),
+ stdinPrompt("(Pdb) "),
+ ];
+
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput
+ {...defaultProps}
+ consoleOutputs={outputs3}
+ onSubmitDebugger={onSubmitDebugger}
+ />
+ </TooltipProvider>,
+ );
+
+ // ArrowUp should show most recent command first
+ input = screen.getByTestId("console-input");
+ fireEvent.keyDown(input, { key: "ArrowUp" });
+ expect(input).toHaveValue("print(x)");
+
+ // ArrowUp again should show older command
+ fireEvent.keyDown(input, { key: "ArrowUp" });
+ expect(input).toHaveValue("step");
+
+ // ArrowDown should go back to "print(x)"
+ fireEvent.keyDown(input, { key: "ArrowDown" });
+ expect(input).toHaveValue("print(x)");
+
+ // ArrowDown again should return to empty input
+ fireEvent.keyDown(input, { key: "ArrowDown" });
+ expect(input).toHaveValue("");
+ });
+});
diff --git a/frontend/src/hooks/useInputHistory.ts b/frontend/src/hooks/useInputHistory.ts
index c9feeb08b1..1bffb27d39 100644
--- a/frontend/src/hooks/useInputHistory.ts
+++ b/frontend/src/hooks/useInputHistory.ts
@@ -8,7 +8,7 @@ interface UseInputHistoryOptions {
setValue: (value: string) => void;
}
-interface UseInputHistoryReturn {
+export interface UseInputHistoryReturn {
/** Command history array */
history: string[];
/** Navigate to previous command (ArrowUp) */