Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* Copyright 2026 Marimo. All rights reserved. */

import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { appendCellLogs, LOG_CHUNK_SIZE } from "@/core/cells/cell-log-chunks";
import * as cellLogs from "@/core/cells/logs";
import type { CellLog } from "@/core/cells/logs";
import { LogViewer } from "../logs-panel";

Expand All @@ -16,10 +18,32 @@ describe("LogViewer", () => {
},
];

render(<LogViewer logs={logs} />);
render(<LogViewer logs={[logs]} />);

expect(screen.getByText("(lsp:pylsp)")).toBeInTheDocument();
expect(screen.getByText("pylsp failed to start")).toBeInTheDocument();
expect(screen.getByText("STDERR")).toBeInTheDocument();
});

it("continues row numbers without formatting a full chunk again", () => {
const log: CellLog = {
timestamp: 1,
level: "stdout",
message: "message",
source: { type: "lsp", name: "pylsp" },
};
const logs = appendCellLogs(
[],
Array.from({ length: LOG_CHUNK_SIZE }, () => log),
);
const { rerender } = render(<LogViewer logs={logs} />);
const format = vi.spyOn(cellLogs, "formatLogTimestamp");
try {
rerender(<LogViewer logs={appendCellLogs(logs, [log])} />);
expect(format).toHaveBeenCalledTimes(1);
expect(screen.getByText(String(LOG_CHUNK_SIZE + 1))).toBeInTheDocument();
} finally {
format.mockRestore();
}
});
});
38 changes: 27 additions & 11 deletions frontend/src/components/editor/chrome/panels/logs-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import { FileTextIcon } from "lucide-react";
import React from "react";
import { ClearButton } from "@/components/buttons/clear-button";
import {
type CellLogChunks,
LOG_CHUNK_SIZE,
} from "@/core/cells/cell-log-chunks";
import { useCellLogs } from "@/core/cells/cells";
import { useCellActions } from "@/core/cells/actions";
import {
Expand All @@ -16,7 +20,7 @@ import { PanelEmptyState } from "./empty-state";

interface Props {
className?: string;
logs: CellLog[];
logs: CellLogChunks;
}

const LogsPanel: React.FC = () => {
Expand Down Expand Up @@ -52,30 +56,42 @@ const LogsPanel: React.FC = () => {
export default LogsPanel;

export const LogViewer: React.FC<Props> = ({ logs, className }) => {
const hover = "opacity-70 group-hover:bg-(--gray-3) group-hover:opacity-100";
return (
<div className={cn("flex flex-col", className)}>
<pre className="grid text-xs font-mono gap-1 whitespace-break-spaces font-semibold align-left">
<div
className="grid grid-cols-[30px_1fr]"
style={{ whiteSpace: "pre-wrap" }}
>
{logs.map((log, index) => (
<div key={index} className="contents group">
<span className={cn(hover, "text-right col-span-1 py-1 pr-1")}>
{index + 1}
</span>
<span className={cn(hover, "px-2 flex gap-x-1.5 py-1 flex-wrap")}>
{formatLog(log)}
</span>
</div>
{logs.map((chunk, index) => (
<LogRows key={index} logs={chunk} offset={index * LOG_CHUNK_SIZE} />
Comment thread
kirangadhave marked this conversation as resolved.
Resolved
))}
</div>
</pre>
</div>
);
};

const LogRows = React.memo(function LogRows({
logs,
offset,
}: {
logs: readonly CellLog[];
offset: number;
}) {
const hover = "opacity-70 group-hover:bg-(--gray-3) group-hover:opacity-100";
return logs.map((log, index) => (
<div key={index} className="contents group">
Comment thread
kirangadhave marked this conversation as resolved.
Resolved
<span className={cn(hover, "text-right col-span-1 py-1 pr-1")}>
{offset + index + 1}
</span>
<span className={cn(hover, "px-2 flex gap-x-1.5 py-1 flex-wrap")}>
{formatLog(log)}
</span>
</div>
));
});

function formatLog(log: CellLog) {
const timestamp = formatLogTimestamp(log.timestamp);

Expand Down
54 changes: 34 additions & 20 deletions frontend/src/core/cells/__tests__/cells.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { MockExecutions } from "@/__mocks__/executions";
import { MockNotebook } from "@/__mocks__/notebook";
import { cellId } from "@/__tests__/branded";
import type { CellHandle } from "@/components/editor/notebook-cell";
import { LOG_CHUNK_SIZE } from "../cell-log-chunks";
import type { CellLog } from "../logs";
import { CellId, SCRATCH_CELL_ID } from "@/core/cells/ids";
import { foldAllBulk, unfoldAllBulk } from "@/core/codemirror/editing/commands";
import { adaptiveLanguageConfiguration } from "@/core/codemirror/language/extension";
Expand Down Expand Up @@ -235,23 +237,33 @@ describe("cell reducer", () => {
expect(unfoldAllBulk).toHaveBeenCalled();
});

it("can clear logs", () => {
state.cellLogs = [
{
level: "stderr",
message: "log1",
timestamp: 0,
it("retains ordered log batches across appends and runtime resets until cleared", () => {
const logs = Array.from(
{ length: 3 * LOG_CHUNK_SIZE + 2 },
(_, i): CellLog => ({
level: i % 2 === 0 ? "stdout" : "stderr",
message: String(i),
timestamp: i,
source: { type: "cell", cellId: firstCellId },
},
{
level: "stderr",
message: "log1",
timestamp: 0,
source: { type: "cell", cellId: firstCellId },
},
];
}),
);
actions.addLogs({ logs: logs.slice(0, LOG_CHUNK_SIZE + 1) });
const previous = state;
actions.addLogs({ logs: [] });
expect(state).toBe(previous);
actions.addLogs({ logs: logs.slice(LOG_CHUNK_SIZE + 1, -1) });
actions.addLogs({ logs: logs.slice(-1) });
const appended = state.cellLogs;
expect(appended[0]).toBe(previous.cellLogs[0]);
expect(previous.cellLogs.flat()).toEqual(logs.slice(0, LOG_CHUNK_SIZE + 1));
expect(appended.flat()).toEqual(logs);
actions.resetCellRuntimes(false);
expect(state.cellLogs).toBe(appended);
actions.clearLogs();
expect(state.cellLogs).toEqual([]);
actions.addLogs({ logs: logs.slice(-1) });
expect(state.cellLogs.flat()).toEqual(logs.slice(-1));
expect(appended.flat()).toEqual(logs);
});

describe("releaseCellAtoms", () => {
Expand Down Expand Up @@ -289,12 +301,14 @@ it("keeps layout cells stable when only a notebook log changes", () => {
store.set(notebookAtom, {
...notebook,
cellLogs: [
{
level: "stderr",
message: "log",
timestamp: 0,
source: { type: "cell", cellId: id },
},
[
{
level: "stderr",
message: "log",
timestamp: 0,
source: { type: "cell", cellId: id },
},
],
],
});
expect(store.get(renderedNotebookCellsAtom)).toBe(cells);
Expand Down
31 changes: 31 additions & 0 deletions frontend/src/core/cells/cell-log-chunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2026 Marimo. All rights reserved. */

import type { CellLog } from "./logs";

export type CellLogChunks = readonly (readonly CellLog[])[];
export const LOG_CHUNK_SIZE = 128;

/** Full chunks stay shared with previous notebook snapshots. */
export function appendCellLogs(
chunks: CellLogChunks,
logs: readonly CellLog[],
): CellLogChunks {
if (logs.length === 0) {
return chunks;
}
const next = [...chunks];
const last = next.at(-1);
let offset = 0;
if (last && last.length < LOG_CHUNK_SIZE) {
const tail = [...last];
while (offset < logs.length && tail.length < LOG_CHUNK_SIZE) {
tail.push(logs[offset]);
offset++;
}
next[next.length - 1] = tail;
}
for (; offset < logs.length; offset += LOG_CHUNK_SIZE) {
next.push(logs.slice(offset, offset + LOG_CHUNK_SIZE));
}
return next;
}
16 changes: 10 additions & 6 deletions frontend/src/core/cells/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { consoleExecution, consoleView } from "./execution-console";
import { applyDocument } from "./document";
import { isCodeHidden, shownHiddenCodeAtom } from "./editor";
import { type CellId, SCRATCH_CELL_ID } from "./ids";
import { appendCellLogs, type CellLogChunks } from "./cell-log-chunks";
import { type CellLog, getCellLogsForMessage } from "./logs";
import type { Outline } from "./outline";
import { focusAndScrollCellIntoView } from "./scrollCellIntoView";
Expand Down Expand Up @@ -119,7 +120,7 @@ export interface NotebookState {
/**
* Logs of all cell messages
*/
cellLogs: CellLog[];
cellLogs: CellLogChunks;
}

function withScratchCell(notebookState: NotebookState): NotebookState {
Expand Down Expand Up @@ -202,10 +203,10 @@ const {
? next
: {
...next,
cellLogs: [
...next.cellLogs,
...getCellLogsForMessage({ cell_id, console: entries }),
],
cellLogs: appendCellLogs(
next.cellLogs,
getCellLogsForMessage({ cell_id, console: entries }),
),
};
},
hideConsoleInputs: (
Expand Down Expand Up @@ -249,9 +250,12 @@ const {
return { ...state, cellRuntime };
},
addLogs: (state, action: { logs: CellLog[] }) => {
if (action.logs.length === 0) {
return state;
}
return {
...state,
cellLogs: [...state.cellLogs, ...action.logs],
cellLogs: appendCellLogs(state.cellLogs, action.logs),
};
},
/** Follow the host document's cells and order. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("handleLogMessage", () => {
handleLogMessage("pylsp", logMessage(2, "plugin not installed"));
handleLogMessage("pylsp", logMessage(3, "indexing"));

expect(store.get(notebookAtom).cellLogs).toMatchObject([
expect(store.get(notebookAtom).cellLogs.flat()).toMatchObject([
{ level: "stderr", message: "failed to start" },
{ level: "stderr", message: "plugin not installed" },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("createTransport", () => {
params: { uri: "file:///a", diagnostics: [] },
});

expect(store.get(notebookAtom).cellLogs).toMatchObject([
expect(store.get(notebookAtom).cellLogs.flat()).toMatchObject([
{
level: "stderr",
message: "pylsp failed to start",
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/core/islands/__tests__/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,8 @@ describe("IslandsPyodideBridge", () => {
expect(
store
.get(notebookAtom)
.cellLogs.map(({ level, message, source }) => [
level,
message,
source,
]),
.cellLogs.flat()
.map(({ level, message, source }) => [level, message, source]),
).toEqual([
["stdout", "hello", { type: "cell", cellId: "c2" }],
["stderr", "warning", { type: "cell", cellId: "c2" }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,12 @@ it.each(["live", "replay", "batched"])(
}
});
expect(rows()).toEqual(["ABC"]);
const messages = () =>
store
.get(notebookAtom)
.cellLogs.flat()
.map((log) => log.message);
expect(messages()).toEqual(mode === "replay" ? [] : ["B", "C"]);
act(() => {
conn.receive({ event: "console.removed", data: { id: "line-0" } });
conn.receive({ event: "console.removed", data: { id: "line-1" } });
Expand Down Expand Up @@ -1337,7 +1343,10 @@ it("clears the private debugger prompt and transcript when editing moves to anot
});
expect(rows()).toEqual([]);
expect(
store.get(notebookAtom).cellLogs.map((log) => log.message),
store
.get(notebookAtom)
.cellLogs.flat()
.map((log) => log.message),
).not.toContain("private debugger");
expect(store.get(pendingPdbInputAtom)).toBeNull();
});
Expand Down
Loading
Loading