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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: Implement ANSI escape sequence handling and console output merging (#7171)
Myles Scolnick11530768a1b0
diff --git a/frontend/.eslintrc.cjs b/frontend/.eslintrc.cjsindex 489cece0ee..5abf70585e 100644--- a/frontend/.eslintrc.cjs+++ b/frontend/.eslintrc.cjs@@ -56,6 +56,7 @@ module.exports = {"no-empty": "off", // → noEmptyBlockStatements in Biome"no-inner-declarations": "off", // → noInnerDeclarations in Biome"no-useless-constructor": "off", // → noUselessConstructor in Biome+ "no-control-regex": "off", // → noControlRegex in Biome"no-var": "off", // → noVar in Biome"prefer-const": "off", // → useConst in Biome"prefer-template": "off", // → useTemplate in Biomediff --git a/frontend/src/__mocks__/notebook.ts b/frontend/src/__mocks__/notebook.tsindex 42219a7535..723f780dda 100644--- a/frontend/src/__mocks__/notebook.ts+++ b/frontend/src/__mocks__/notebook.ts@@ -46,7 +46,7 @@ export const MockNotebook = {})),cellIds: MultiColumn.from([Object.keys(cellData) as CellId[]]),cellRuntime: Objects.mapValues(cellData, (_data, cellId) =>- createCellRuntimeState({ ...(cellRuntime[cellId] || {}) }),+ createCellRuntimeState({ ...cellRuntime[cellId] }),),cellHandles: Objects.mapValues(cellData, (_data) => createRef()),cellLogs: [],diff --git a/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.tsnew file mode 100644index 0000000000..4c42c14c87--- /dev/null+++ b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts@@ -0,0 +1,560 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, expect, test } from "vitest";+import {+ AnsiParser,+ AnsiReducer,+ StatefulOutputMessage,+ TerminalBuffer,+} from "../ansi-reduce";++describe("TerminalBuffer", () => {+ test("writeChar writes single character", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ expect(buffer.render()).toMatchInlineSnapshot(`"a"`);+ });++ test("writeChar writes multiple characters", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("H");+ buffer.writeChar("e");+ buffer.writeChar("l");+ buffer.writeChar("l");+ buffer.writeChar("o");+ expect(buffer.render()).toMatchInlineSnapshot(`"Hello"`);+ });++ test("writeChar overwrites at cursor position", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ // Move cursor back and overwrite+ buffer.handleEscape("\u001B[2D"); // Move left 2+ buffer.writeChar("X");+ buffer.writeChar("Y");+ expect(buffer.render()).toMatchInlineSnapshot(`"aXY"`);+ });++ test("control handles newline", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.control("\n");+ buffer.writeChar("b");+ expect(buffer.render()).toMatchInlineSnapshot(`+ "a+ b"+ `);+ });++ test("control handles carriage return", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ buffer.control("\r");+ buffer.writeChar("X");+ buffer.writeChar("Y");+ expect(buffer.render()).toMatchInlineSnapshot(`"XYc"`);+ });++ test("handleEscape cursor up", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.control("\n");+ buffer.writeChar("b");+ buffer.handleEscape("\u001B[1A"); // Move up 1+ buffer.writeChar("X");+ expect(buffer.render()).toMatchInlineSnapshot(`"aX"`);+ });++ test("handleEscape cursor down", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.handleEscape("\u001B[1B"); // Move down 1+ buffer.writeChar("b");+ expect(buffer.render()).toMatchInlineSnapshot(`+ "a+ b"+ `);+ });++ test("handleEscape cursor forward", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.handleEscape("\u001B[3C"); // Move right 3+ buffer.writeChar("b");+ expect(buffer.render()).toMatchInlineSnapshot(`"a b"`);+ });++ test("handleEscape cursor back", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ buffer.handleEscape("\u001B[2D"); // Move left 2+ buffer.writeChar("X");+ expect(buffer.render()).toMatchInlineSnapshot(`"aXc"`);+ });++ test("handleEscape cursor home with params", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.control("\n");+ buffer.writeChar("b");+ buffer.handleEscape("\u001B[1;1H"); // Move to (1,1) which is (0,0) in 0-indexed+ buffer.writeChar("X");+ expect(buffer.render()).toMatchInlineSnapshot(`+ "X+ b"+ `);+ });++ test("handleEscape cursor home without params", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.handleEscape("\u001B[H"); // Move to home+ buffer.writeChar("X");+ expect(buffer.render()).toMatchInlineSnapshot(`"Xb"`);+ });++ test("handleEscape erase display (2J)", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.control("\n");+ buffer.writeChar("b");+ buffer.handleEscape("\u001B[2J"); // Clear screen+ buffer.writeChar("X");+ expect(buffer.render()).toMatchInlineSnapshot(`"X"`);+ });++ test("handleEscape erase line to end (0K)", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ buffer.handleEscape("\u001B[2D"); // Move left 2+ buffer.handleEscape("\u001B[0K"); // Clear to end+ expect(buffer.render()).toMatchInlineSnapshot(`"a"`);+ });++ test("handleEscape erase line to start (1K)", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ buffer.handleEscape("\u001B[2D"); // Move left 2+ buffer.handleEscape("\u001B[1K"); // Clear to start+ expect(buffer.render()).toMatchInlineSnapshot(`" bc"`);+ });++ test("handleEscape erase entire line (2K)", () => {+ const buffer = new TerminalBuffer();+ buffer.writeChar("a");+ buffer.writeChar("b");+ buffer.writeChar("c");+ buffer.handleEscape("\u001B[2K"); // Clear entire line+ expect(buffer.render()).toMatchInlineSnapshot(`""`);+ });+});++describe("AnsiParser", () => {+ test("parse plain text", () => {+ const parser = new AnsiParser();+ const tokens = parser.parse("hello");+ expect(tokens).toMatchInlineSnapshot(`+ [+ {+ "type": "text",+ "value": "hello",+ },+ ]+ `);+ });++ test("parse text with escape sequence", () => {+ const parser = new AnsiParser();+ const tokens = parser.parse("hello\u001B[1Aworld");+ expect(tokens).toMatchInlineSnapshot(`+ [+ {+ "type": "text",+ "value": "hello",+ },+ {+ "type": "escape",+ "value": "[1A",+ },+ {+ "type": "text",+ "value": "world",+ },+ ]+ `);+ });++ test("parse multiple escape sequences", () => {+ const parser = new AnsiParser();+ const tokens = parser.parse("\u001B[1A\u001B[2C\u001B[0K");+ expect(tokens).toMatchInlineSnapshot(`+ [+ {+ "type": "escape",+ "value": "[1A",+ },+ {+ "type": "escape",+ "value": "[2C",+ },+ {+ "type": "escape",+ "value": "[0K",+ },+ ]+ `);+ });++ test("parse escape with multiple parameters", () => {+ const parser = new AnsiParser();+ const tokens = parser.parse("\u001B[10;20H");+ expect(tokens).toMatchInlineSnapshot(`+ [+ {+ "type": "escape",+ "value": "[10;20H",+ },+ ]+ `);+ });++ test("parse empty string", () => {+ const parser = new AnsiParser();+ const tokens = parser.parse("");+ expect(tokens).toMatchInlineSnapshot("[]");+ });+});++describe("AnsiReducer", () => {+ test("reduce plain text", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("hello world");+ expect(result).toMatchInlineSnapshot(`"hello world"`);+ });++ test("reduce text with newlines", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("line1\nline2\nline3");+ expect(result).toMatchInlineSnapshot(`+ "line1+ line2+ line3"+ `);+ });++ test("reduce progress bar simulation", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce(+ "Progress: 10%\rProgress: 50%\rProgress: 100%",+ );+ expect(result).toMatchInlineSnapshot(`"Progress: 100%"`);+ });++ test("reduce spinner simulation", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce(+ "Loading |\rLoading /\rLoading -\rLoading \\",+ );+ expect(result).toMatchInlineSnapshot(`"Loading \\"`);+ });++ test("reduce cursor movement", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("Hello\u001B[5DWorld");+ expect(result).toMatchInlineSnapshot(`"World"`);+ });++ test("reduce clear line", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("Hello World\u001B[2K");+ expect(result).toMatchInlineSnapshot(`""`);+ });++ test("reduce clear screen", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("Line 1\nLine 2\n\u001B[2JNew Start");+ expect(result).toMatchInlineSnapshot(`"New Start"`);+ });++ test("reduce complex cursor positioning", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("abc\u001B[1;2Hxy");+ expect(result).toMatchInlineSnapshot(`"axy"`);+ });++ test("reduce ignores control characters below space", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("hello\u0000\u0001\u0007world");+ expect(result).toMatchInlineSnapshot(`"helloworld"`);+ });++ test("reduce handles carriage return without newline", () => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce("AAAA\rBB");+ expect(result).toMatchInlineSnapshot(`"BBAA"`);+ });+});++function ansiReduce(input: string): string {+ const reducer = new AnsiReducer();+ return reducer.reduce(input);+}++describe("ansiReduce", () => {+ test("basic usage", () => {+ const result = ansiReduce("Hello World");+ expect(result).toMatchInlineSnapshot(`"Hello World"`);+ });++ test("progress bar example", () => {+ const result = ansiReduce(+ "[ ] 0%\r[== ] 20%\r[==== ] 40%\r[====== ] 60%\r[======== ] 80%\r[==========] 100%",+ );+ expect(result).toMatchInlineSnapshot(`"[==========] 100%"`);+ });++ test("multi-line with cursor movement", () => {+ const result = ansiReduce("Line 1\nLine 2\nLine 3\u001B[2AModified");+ expect(result).toMatchInlineSnapshot(`"Line 1Modified"`);+ });++ test("erase and rewrite", () => {+ const result = ansiReduce("Old text\u001B[2KNew text");+ expect(result).toMatchInlineSnapshot(`" New text"`);+ });++ test("empty input", () => {+ const result = ansiReduce("");+ expect(result).toMatchInlineSnapshot(`""`);+ });++ test("newlines only", () => {+ const result = ansiReduce("\n\n\n");+ expect(result).toMatchInlineSnapshot(`+ "+++ "+ `);+ });++ test("real-world tqdm-like progress", () => {+ const result = ansiReduce(+ "Processing: | | 0/100\r" ++ "Processing: |█ | 10/100\r" ++ "Processing: |██ | 20/100\r" ++ "Processing: |██████████| 100/100",+ );+ expect(result).toMatchInlineSnapshot(`"Processing: |██████████| 100/100"`);+ });++ test("cursor positioning with absolute coordinates", () => {+ const result = ansiReduce("\u001B[1;1Ha\u001B[2;2Hb\u001B[3;3Hc");+ expect(result).toMatchInlineSnapshot(`+ "a+ b+ c"+ `);+ });++ test("partial line erase from cursor to end", () => {+ const result = ansiReduce("Hello World\u001B[6D\u001B[0K!");+ expect(result).toMatchInlineSnapshot(`"Hello!"`);+ });++ test("partial line erase from start to cursor", () => {+ const result = ansiReduce("Hello World\u001B[6D\u001B[1K!");+ expect(result).toMatchInlineSnapshot(`" !World"`);+ });++ test("complex progress simulation", () => {+ const text =+ "\r 0%| | 0/101000 [00:00<?, ?it/s]\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 0%| | 0/101000 [00:00<?, ?it/s]\n\rRunning chain 1: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\rRunning chain 2: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 15%|████▎ | 15150/101000 [00:00<00:00, 129479.68it/s]\u001B[A\u001B[A\n\rRunning chain 1: 15%|████▎ | 15150/101000 [00:00<00:00, 124469.08it/s]\u001B[A\rRunning chain 0: 15%|████▎ | 15150/101000 [00:00<00:00, 122865.03it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 30%|████████▋ | 30300/101000 [00:00<00:00, 130679.02it/s]\u001B[A\u001B[A\n\rRunning chain 1: 30%|████████▋ | 30300/101000 [00:00<00:00, 126103.71it/s]\u001B[A\rRunning chain 0: 30%|████████▋ | 30300/101000 [00:00<00:00, 124748.00it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 45%|█████████████ | 45450/101000 [00:00<00:00, 127078.72it/s]\u001B[A\u001B[A\n\rRunning chain 1: 45%|█████████████ | 45450/101000 [00:00<00:00, 123708.59it/s]\u001B[A\rRunning chain 0: 45%|█████████████ | 45450/101000 [00:00<00:00, 121550.69it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 126319.28it/s]\u001B[A\u001B[A\n\rRunning chain 1: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 123669.56it/s]\u001B[A\rRunning chain 0: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 122438.92it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 126984.84it/s]\u001B[A\u001B[A\n\rRunning chain 1: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123764.98it/s]\u001B[A\rRunning chain 0: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123796.97it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 128241.91it/s]\u001B[A\u001B[A\n\rRunning chain 1: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 125149.55it/s]\u001B[A\rRunning chain 0: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 124582.89it/s]\rRunning chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 88054.93it/s]\n\rRunning chain 2: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 86530.93it/s]\n\rRunning chain 1: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 85142.19it/s]\n\rRunning chain 0: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 84719.76it/s]\n";+ expect(ansiReduce(text)).toMatchInlineSnapshot(`+ "Running chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 88054.93it/s]+ Running chain 2: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 86530.93it/s]+ Running chain 1: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 85142.19it/s]+ Running chain 0: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 84719.76it/s]+ "+ `);+ });++ test("more complex cursor movements", () => {+ const text =+ "\r 0%| | 0/101000 [00:00<?, ?it/s]\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 0%| | 0/101000 [00:00<?, ?it/s]\n\rRunning chain 1: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\rRunning chain 2: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 5%|█▌ | 5050/101000 [00:00<00:08, 11945.37it/s]\n\rRunning chain 1: 5%|█▌ | 5050/101000 [00:00<00:08, 11535.69it/s]\u001B[A\n\n\rRunning chain 2: 5%|█▌ | 5050/101000 [00:00<00:08, 11491.43it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 10%|███ | 10100/101000 [00:01<00:07, 12010.48it/s]\rRunning chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 83919.80it/s]\n\n\rRunning chain 1: 10%|███ | 10100/101000 [00:01<00:07, 11604.98it/s]\u001B[A\n\n\rRunning chain 2: 10%|███ | 10100/101000 [00:01<00:07, 11575.75it/s]\u001B[A\u001B[A\rRunning chain 0: 15%|████▌ | 15150/101000 [00:01<00:06, 12551.29it/s]\n\rRunning chain 1: 15%|████▌ | 15150/101000 [00:01<00:07, 12082.69it/s]\u001B[A\n\n\rRunning chain 2: 15%|████▌ | 15150/101000 [00:01<00:07, 12024.20it/s]\u001B[A\u001B[A\rRunning chain 0: 20%|██████ | 20200/101000 [00:01<00:06, 12896.22it/s]\n\rRunning chain 1: 20%|██████ | 20200/101000 [00:02<00:06, 12358.86it/s]\u001B[A\n\n\rRunning chain 2: 20%|██████ | 20200/101000 [00:02<00:06, 12331.67it/s]\u001B[A\u001B[A\rRunning chain 0: 25%|███████▌ | 25250/101000 [00:02<00:05, 12987.27it/s]\n\rRunning chain 1: 25%|███████▌ | 25250/101000 [00:02<00:06, 12486.00it/s]\u001B[A\n\n\rRunning chain 2: 25%|███████▌ | 25250/101000 [00:02<00:06, 12446.08it/s]\u001B[A\u001B[A\rRunning chain 0: 30%|█████████ | 30300/101000 [00:02<00:05, 13286.93it/s]\n\rRunning chain 1: 30%|█████████ | 30300/101000 [00:02<00:05, 12504.21it/s]\u001B[A\n\n\rRunning chain 2: 30%|█████████ | 30300/101000 [00:02<00:05, 12362.40it/s]\u001B[A\u001B[A\rRunning chain 0: 35%|██████████▌ | 35350/101000 [00:03<00:04, 13319.74it/s]\n\rRunning chain 1: 35%|██████████▌ | 35350/101000 [00:03<00:05, 12636.28it/s]\u001B[A\n\n\rRunning chain 2: 35%|██████████▌ | 35350/101000 [00:03<00:05, 12506.56it/s]\u001B[A\u001B[A\rRunning chain 0: 40%|████████████ | 40400/101000 [00:03<00:04, 13473.33it/s]\n\rRunning chain 1: 40%|████████████ | 40400/101000 [00:03<00:04, 12846.16it/s]\u001B[A\n\n\rRunning chain 2: 40%|████████████ | 40400/101000 [00:03<00:04, 12683.84it/s]\u001B[A\u001B[A\rRunning chain 0: 45%|█████████████▌ | 45450/101000 [00:03<00:04, 13461.17it/s]\n\rRunning chain 1: 45%|█████████████▌ | 45450/101000 [00:03<00:04, 12862.53it/s]\u001B[A\n\n\rRunning chain 2: 45%|█████████████▌ | 45450/101000 [00:04<00:04, 12725.95it/s]\u001B[A\u001B[A\rRunning chain 0: 50%|███████████████ | 50500/101000 [00:04<00:03, 13487.23it/s]\n\rRunning chain 1: 50%|███████████████ | 50500/101000 [00:04<00:03, 12913.94it/s]\u001B[A\n\n\rRunning chain 2: 50%|███████████████ | 50500/101000 [00:04<00:03, 12775.79it/s]\u001B[A\u001B[A\rRunning chain 0: 55%|████████████████▌ | 55550/101000 [00:04<00:03, 13532.41it/s]\n\rRunning chain 1: 55%|████████████████▌ | 55550/101000 [00:04<00:03, 12942.85it/s]\u001B[A\n\n\rRunning chain 2: 55%|████████████████▌ | 55550/101000 [00:04<00:03, 12817.21it/s]\u001B[A\u001B[A\rRunning chain 0: 60%|██████████████████ | 60600/101000 [00:04<00:02, 13553.39it/s]\n\rRunning chain 1: 60%|██████████████████ | 60600/101000 [00:05<00:03, 12997.63it/s]\u001B[A\n\n\rRunning chain 2: 60%|██████████████████ | 60600/101000 [00:05<00:03, 12847.11it/s]\u001B[A\u001B[A\rRunning chain 0: 65%|███████████████████▌ | 65650/101000 [00:05<00:02, 13525.88it/s]\n\rRunning chain 1: 65%|███████████████████▌ | 65650/101000 [00:05<00:02, 12983.99it/s]\u001B[A\n\n\rRunning chain 2: 65%|███████████████████▌ | 65650/101000 [00:05<00:02, 12824.49it/s]\u001B[A\u001B[A\rRunning chain 0: 70%|█████████████████████ | 70700/101000 [00:05<00:02, 13491.28it/s]\n\rRunning chain 1: 70%|█████████████████████ | 70700/101000 [00:05<00:02, 13011.52it/s]\u001B[A\n\n\rRunning chain 2: 70%|█████████████████████ | 70700/101000 [00:05<00:02, 12779.17it/s]\u001B[A\u001B[A\rRunning chain 0: 75%|██████████████████████▌ | 75750/101000 [00:06<00:01, 13469.35it/s]\n\rRunning chain 1: 75%|██████████████████████▌ | 75750/101000 [00:06<00:01, 12989.82it/s]\u001B[A\n\n\rRunning chain 2: 75%|██████████████████████▌ | 75750/101000 [00:06<00:01, 12747.26it/s]\u001B[A\u001B[A\rRunning chain 0: 80%|████████████████████████ | 80800/101000 [00:06<00:01, 13449.38it/s]\n\rRunning chain 1: 80%|████████████████████████ | 80800/101000 [00:06<00:01, 12991.59it/s]\u001B[A\n\n\rRunning chain 2: 80%|████████████████████████ | 80800/101000 [00:06<00:01, 12750.35it/s]\u001B[A\u001B[A\rRunning chain 0: 85%|█████████████████████████▌ | 85850/101000 [00:06<00:01, 13411.69it/s]\n\rRunning chain 1: 85%|█████████████████████████▌ | 85850/101000 [00:07<00:01, 13116.35it/s]\u001B[A\n\n\rRunning chain 2: 85%|█████████████████████████▌ | 85850/101000 [00:07<00:01, 12779.54it/s]\u001B[A\u001B[A\rRunning chain 0: 90%|███████████████████████████ | 90900/101000 [00:07<00:00, 13610.86it/s]\n\rRunning chain 1: 90%|███████████████████████████ | 90900/101000 [00:07<00:00, 13109.18it/s]\u001B[A\rRunning chain 0: 95%|████████████████████████████▌ | 95950/101000 [00:07<00:00, 13590.91it/s]\n\n\rRunning chain 2: 90%|███████████████████████████ | 90900/101000 [00:07<00:00, 12786.82it/s]\u001B[A\u001B[A\n\rRunning chain 1: 95%|████████████████████████████▌ | 95950/101000 [00:07<00:00, 13082.02it/s]\u001B[A\rRunning chain 0: 100%|█████████████████████████████| 101000/101000 [00:07<00:00, 13573.97it/s]\rRunning chain 0: 100%|█████████████████████████████| 101000/101000 [00:07<00:00, 12749.04it/s]\n\n\n\rRunning chain 2: 95%|████████████████████████████▌ | 95950/101000 [00:07<00:00, 12922.54it/s]\u001B[A\u001B[A\n\rRunning chain 1: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 14371.12it/s]\u001B[A\rRunning chain 1: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 12442.67it/s]\n\n\n\rRunning chain 2: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 14869.01it/s]\u001B[A\u001B[A\rRunning chain 2: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 12373.90it/s]\n";+ const result = ansiReduce(text);+ expect(result).toMatchInlineSnapshot(`+ "Running chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 83919.80it/s]+ Running chain 0: 100%|█████████████████████████████| 101000/101000 [00:07<00:00, 12749.04it/s]+ Running chain 1: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 12442.67it/s]+ Running chain 2: 100%|█████████████████████████████| 101000/101000 [00:08<00:00, 12373.90it/s]+ "+ `);+ });+});++describe("AnsiReducer streaming with append()", () => {+ test("append simple text incrementally", () => {+ const reducer = new AnsiReducer();+ reducer.append("Hello ");+ reducer.append("World");+ expect(reducer.render()).toMatchInlineSnapshot(`"Hello World"`);+ });++ test("append with progress bar simulation", () => {+ const reducer = new AnsiReducer();+ reducer.append("Progress: 0%");+ reducer.append("\rProgress: 50%");+ reducer.append("\rProgress: 100%");+ expect(reducer.render()).toMatchInlineSnapshot(`"Progress: 100%"`);+ });++ test("append with newlines", () => {+ const reducer = new AnsiReducer();+ reducer.append("Line 1\n");+ reducer.append("Line 2\n");+ reducer.append("Line 3");+ expect(reducer.render()).toMatchInlineSnapshot(`+ "Line 1+ Line 2+ Line 3"+ `);+ });++ test("append with cursor movements", () => {+ const reducer = new AnsiReducer();+ reducer.append("abc\n");+ reducer.append("def\n");+ reducer.append("\u001B[1A"); // Move up one line+ reducer.append("XYZ");+ expect(reducer.render()).toMatchInlineSnapshot(`+ "abc+ XYZ"+ `);+ });++ test("reset clears state", () => {+ const reducer = new AnsiReducer();+ reducer.append("Old content");+ expect(reducer.render()).toMatchInlineSnapshot(`"Old content"`);++ reducer.reset();+ reducer.append("New content");+ expect(reducer.render()).toMatchInlineSnapshot(`"New content"`);+ });++ test("reduce() resets before processing", () => {+ const reducer = new AnsiReducer();+ reducer.append("Old content");+ const result = reducer.reduce("New content");+ expect(result).toMatchInlineSnapshot(`"New content"`);+ });++ test("streaming tqdm-like progress", () => {+ const reducer = new AnsiReducer();++ // Simulate streaming progress updates+ reducer.append("Processing: | | 0/100\r");+ reducer.append("Processing: |██ | 20/100\r");+ reducer.append("Processing: |████ | 40/100\r");+ reducer.append("Processing: |██████ | 60/100\r");+ reducer.append("Processing: |████████ | 80/100\r");+ reducer.append("Processing: |██████████| 100/100");++ expect(reducer.render()).toMatchInlineSnapshot(+ `"Processing: |██████████| 100/100"`,+ );+ });++ test("append empty string does nothing", () => {+ const reducer = new AnsiReducer();+ reducer.append("Hello");+ reducer.append("");+ expect(reducer.render()).toMatchInlineSnapshot(`"Hello"`);+ });++ test("append with mixed content and escapes", () => {+ const reducer = new AnsiReducer();+ reducer.append("Loading");+ reducer.append(" |");+ reducer.append("\rLoading /");+ reducer.append("\rLoading -");+ reducer.append("\rLoading \\");+ reducer.append("\rDone! ");+ expect(reducer.render()).toMatchInlineSnapshot(`"Done! "`);+ });+});++describe("StatefulOutputMessage", () => {+ test("initializes and processes text", () => {+ const message = {+ mimetype: "text/plain",+ channel: "stdout",+ timestamp: 123,+ data: "Hello",+ } as const;+ const stateful = StatefulOutputMessage.create(message);++ expect(stateful.mimetype).toBe("text/plain");+ expect(stateful.channel).toBe("stdout");+ expect(stateful.data).toBe("Hello");+ });++ test("appendData appends text", () => {+ const message = {+ mimetype: "text/plain",+ channel: "stdout",+ timestamp: 0,+ data: "Hello",+ } as const;+ let stateful = StatefulOutputMessage.create(message);++ stateful = stateful.appendData(" World");++ expect(stateful.data).toBe("Hello World");+ });++ test("appendData handles progress bars", () => {+ const message = {+ mimetype: "text/plain",+ channel: "stdout",+ timestamp: 0,+ data: "Progress: 0%",+ } as const;+ let stateful = StatefulOutputMessage.create(message);++ stateful = stateful.appendData("\rProgress: 50%");+ stateful = stateful.appendData("\rProgress: 100%");++ expect(stateful.data).toBe("Progress: 100%");+ });++ test("appendData maintains ANSI state", () => {+ const message = {+ mimetype: "text/plain",+ channel: "stdout",+ timestamp: 0,+ data: "Line 1\n",+ } as const;+ let stateful = StatefulOutputMessage.create(message);++ stateful = stateful.appendData("Line 2\n");+ stateful = stateful.appendData("\u001B[1A"); // Move up+ stateful = stateful.appendData("X");++ expect(stateful.data).toMatchInlineSnapshot(`+ "Line 1+ Xine 2"+ `);+ });+});diff --git a/frontend/src/components/editor/output/ansi-reduce.ts b/frontend/src/components/editor/output/ansi-reduce.tsnew file mode 100644index 0000000000..c0019547f8--- /dev/null+++ b/frontend/src/components/editor/output/ansi-reduce.ts@@ -0,0 +1,392 @@+/* Copyright 2024 Marimo. All rights reserved. */+import type { OutputMessage } from "@/core/kernel/messages";++export interface Cursor {+ row: number;+ col: number;+}++/**+ * A simplified terminal buffer.+ * It maintains an array of lines and grows as needed.+ */+export class TerminalBuffer {+ private lines: string[] = [""];+ private cursor: Cursor = { row: 0, col: 0 };+ // biome-ignore lint/suspicious/noControlCharactersInRegex: Needed for ANSI parsing+ private static readonly ESCAPE_REGEX = /\u001B\[([0-9;]*)([A-DJKH])/u;++ /** Ensure the internal lines array is large enough. */+ private ensureLine(row: number) {+ while (this.lines.length <= row) {+ this.lines.push("");+ }+ }++ /** Move cursor by relative offsets. */+ private moveCursor(rowDelta: number, colDelta: number) {+ const oldRow = this.cursor.row;+ this.cursor.row = Math.max(0, this.cursor.row + rowDelta);+ this.cursor.col = Math.max(0, this.cursor.col + colDelta);+ this.ensureLine(this.cursor.row);++ // When moving up, discard lines below the new cursor position+ // This simulates a fixed terminal window where tqdm overwrites content+ if (rowDelta < 0 && this.cursor.row < oldRow) {+ this.lines.splice(this.cursor.row + 1);+ }+ }++ /** Move cursor to a specific absolute position. */+ private setCursor(row: number, col: number) {+ this.cursor.row = Math.max(0, row);+ this.cursor.col = Math.max(0, col);+ this.ensureLine(this.cursor.row);+ }++ /** Write a visible character at the current cursor position. */+ writeChar(ch: string) {+ this.ensureLine(this.cursor.row);+ const line = this.lines[this.cursor.row];+ const padded = line.padEnd(this.cursor.col, " ");+ this.lines[this.cursor.row] =+ padded.slice(0, this.cursor.col) + ch + padded.slice(this.cursor.col + 1);+ this.cursor.col++;+ }++ /** Write a string of visible characters at the current cursor position (optimized batch write). */+ writeString(str: string) {+ if (str.length === 0) {+ return;+ }+ this.ensureLine(this.cursor.row);+ const line = this.lines[this.cursor.row];+ const padded = line.padEnd(this.cursor.col, " ");+ this.lines[this.cursor.row] =+ padded.slice(0, this.cursor.col) ++ str ++ padded.slice(this.cursor.col + str.length);+ this.cursor.col += str.length;+ }++ /** Handle simple control characters (\n, \r). */+ control(ch: string) {+ switch (ch) {+ case "\n":+ this.cursor.row++;+ this.cursor.col = 0;+ this.ensureLine(this.cursor.row);+ break;+ case "\r":+ this.cursor.col = 0;+ break;+ }+ }++ /**+ * Handle a basic ANSI escape sequence.+ * Supports cursor movement and line erasing.+ */+ handleEscape(seq: string) {+ const match = TerminalBuffer.ESCAPE_REGEX.exec(seq);+ if (!match) {+ return;+ }++ const params = match[1]+ .split(";")+ .map((p) => (p === "" ? 1 : Number.parseInt(p, 10)));+ const code = match[2];++ switch (code) {+ case "A": // Cursor Up+ this.moveCursor(-params[0], 0);+ break;+ case "B": // Cursor Down+ this.moveCursor(params[0], 0);+ break;+ case "C": // Cursor Forward (Right)+ this.moveCursor(0, params[0]);+ break;+ case "D": // Cursor Back (Left)+ this.moveCursor(0, -params[0]);+ break;+ case "H": // Cursor Home+ this.setCursor(params[0] - 1 || 0, params[1] - 1 || 0);+ break;+ case "J": // Erase in display+ if (params[0] === 2) {+ this.lines = [""];+ this.setCursor(0, 0);+ }+ break;+ case "K": // Erase line+ this.ensureLine(this.cursor.row);+ switch (params[0]) {+ case 0:+ // Clear to end of line+ this.lines[this.cursor.row] = this.lines[this.cursor.row].slice(+ 0,+ this.cursor.col,+ );++ break;++ case 1: {+ // Clear to start of line+ const len = this.lines[this.cursor.row].length;+ this.lines[this.cursor.row] =+ " ".repeat(this.cursor.col) ++ this.lines[this.cursor.row].slice(this.cursor.col, len);++ break;+ }+ case 2:+ this.lines[this.cursor.row] = "";++ break;++ // No default+ }+ break;+ }+ }++ /** Return the final rendered buffer as a single string. */+ render(): string {+ return this.lines.join("\n");+ }+}++/**+ * Parses ANSI escape sequences into tokens for processing.+ */+export class AnsiParser {+ // biome-ignore lint/suspicious/noControlCharactersInRegex: Needed for ANSI parsing+ private ESC_REGEX = /\u001B\[[0-9;]*[A-Za-z]/gu;++ parse(input: string): { type: "text" | "escape"; value: string }[] {+ const tokens: { type: "text" | "escape"; value: string }[] = [];+ let lastIndex = 0;++ for (const match of input.matchAll(this.ESC_REGEX)) {+ const index = match.index ?? 0;+ if (index > lastIndex) {+ tokens.push({ type: "text", value: input.slice(lastIndex, index) });+ }+ tokens.push({ type: "escape", value: match[0] });+ lastIndex = index + match[0].length;+ }++ if (lastIndex < input.length) {+ tokens.push({ type: "text", value: input.slice(lastIndex) });+ }++ return tokens;+ }+}++/**+ * High-level reducer that processes ANSI sequences and returns final visible output.+ * This class is stateful - it maintains the terminal buffer and cursor position across calls.+ * Use append() for streaming/incremental updates.+ */+export class AnsiReducer {+ private parser = new AnsiParser();+ private buffer = new TerminalBuffer();++ /**+ * Process the entire input string (replaces any previous state).+ * Use this for one-time processing or when starting fresh.+ */+ reduce(input: string): string {+ this.reset();+ this.append(input);+ return this.render();+ }++ /**+ * Append new input to the existing buffer (for streaming/incremental updates).+ * This is efficient for streaming scenarios - only processes the new chunk.+ */+ append(input: string): void {+ const tokens = this.parser.parse(input);++ // Fast path: if only one text token (no ANSI codes), handle directly+ if (tokens.length === 1 && tokens[0].type === "text") {+ const text = tokens[0].value;+ // Check if there are any control characters that need special handling+ if (!this.hasControlChars(text)) {+ // Simple text with no newlines or control chars - fastest path+ this.buffer.writeString(text);+ return;+ }+ }++ for (const token of tokens) {+ if (token.type === "text") {+ this.processText(token.value);+ } else {+ this.buffer.handleEscape(token.value);+ }+ }++ return;+ }++ /**+ * Reset the buffer and cursor to initial state.+ * Use this when you want to start processing fresh input.+ */+ reset(): void {+ this.buffer = new TerminalBuffer();+ }++ /**+ * Get the current rendered output without processing new input.+ */+ render(): string {+ return this.buffer.render();+ }++ /** Check if text contains control characters that need special handling. */+ private hasControlChars(text: string): boolean {+ for (const element of text) {+ if (element < " ") {+ return true;+ }+ }+ return false;+ }++ /** Process text token efficiently by batching writes when possible. */+ private processText(text: string) {+ let start = 0;+ const len = text.length;++ for (let i = 0; i < len; i++) {+ const ch = text[i];++ // Handle control characters+ if (ch === "\n" || ch === "\r") {+ // Write accumulated text before the control character+ if (i > start) {+ const segment = text.slice(start, i);+ // Filter out characters below space (but we already have \n and \r handled)+ const filtered = this.filterControlChars(segment);+ if (filtered.length > 0) {+ this.buffer.writeString(filtered);+ }+ }+ this.buffer.control(ch);+ start = i + 1;+ } else if (ch < " ") {+ // Skip other control characters (below space)+ if (i > start) {+ const segment = text.slice(start, i);+ if (segment.length > 0) {+ this.buffer.writeString(segment);+ }+ }+ start = i + 1;+ }+ }++ // Write any remaining text+ if (start < len) {+ const segment = text.slice(start);+ const filtered = this.filterControlChars(segment);+ if (filtered.length > 0) {+ this.buffer.writeString(filtered);+ }+ }+ }++ /** Filter out control characters below space (except \n and \r which are handled separately). */+ private filterControlChars(text: string): string {+ // Fast path: if no control chars, return as-is+ let hasControlChars = false;+ for (const element of text) {+ if (element < " ") {+ hasControlChars = true;+ break;+ }+ }++ if (!hasControlChars) {+ return text;+ }++ // Slow path: filter out control chars+ let result = "";+ for (const element of text) {+ if (element >= " ") {+ result += element;+ }+ }+ return result;+ }+}++export type StringOutputMessage = Omit<OutputMessage, "data"> & {+ data: string;+};++/**+ * Immutable output message that maintains ANSI state across appends.+ * This is used with react so must be immutable.+ */+export class StatefulOutputMessage implements OutputMessage {+ public readonly mimetype: OutputMessage["mimetype"];+ public readonly channel: OutputMessage["channel"];+ public readonly timestamp: OutputMessage["timestamp"];+ private ansiReducer = new AnsiReducer();+ private _data: string;++ public get data(): string {+ return this._data;+ }++ static create(message: StringOutputMessage): StatefulOutputMessage {+ const ansiReducer = new AnsiReducer();+ ansiReducer.append(message.data);+ return new StatefulOutputMessage(+ message.mimetype,+ message.channel,+ message.timestamp,+ ansiReducer,+ );+ }++ private constructor(+ mimetype: OutputMessage["mimetype"],+ channel: OutputMessage["channel"],+ timestamp: OutputMessage["timestamp"],+ ansiReducer: AnsiReducer,+ ) {+ this.mimetype = mimetype;+ this.channel = channel;+ this.timestamp = timestamp;+ this.ansiReducer = ansiReducer;+ this._data = this.ansiReducer.render();+ }++ appendData(chunk: string): StatefulOutputMessage {+ this.ansiReducer.append(chunk);+ return new StatefulOutputMessage(+ this.mimetype,+ this.channel,+ this.timestamp,+ this.ansiReducer,+ );+ }++ toJSON(): StringOutputMessage {+ return {+ mimetype: this.mimetype,+ channel: this.channel,+ timestamp: this.timestamp,+ data: this.data,+ };+ }+}diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.tsindex 94f43d029e..9786fd1535 100644--- a/frontend/src/core/cells/__tests__/cells.test.ts+++ b/frontend/src/core/cells/__tests__/cells.test.ts@@ -1092,7 +1092,7 @@ describe("cell reducer", () => {expect(cell).toMatchSnapshot(); // snapshot everything as a catch allcell = cells[0];- expect(cell.consoleOutputs).toEqual([STDOUT]);+ expect(cell.consoleOutputs[0]).toMatchObject(STDOUT);expect(cell.status).toBe("running");expect(cell).toMatchSnapshot(); // snapshot everything as a catch all@@ -1106,7 +1106,8 @@ describe("cell reducer", () => {timestamp: new Date(22).getTime() as Seconds,});cell = cells[0];- expect(cell.consoleOutputs).toEqual([STDOUT, STD_IN_1]);+ expect(cell.consoleOutputs[0]).toMatchObject(STDOUT);+ expect(cell.consoleOutputs[1]).toMatchObject(STD_IN_1);expect(cell.status).toBe("running");expect(cell).toMatchSnapshot(); // snapshot everything as a catch all@@ -1117,13 +1118,10 @@ describe("cell reducer", () => {outputIndex: 1,});cell = cells[0];- expect(cell.consoleOutputs).toEqual([- STDOUT,- {- ...STD_IN_1,- response: "Marimo!",- },- ]);+ expect(cell.consoleOutputs[1]).toMatchObject({+ ...STD_IN_1,+ response: "Marimo!",+ });expect(cell.status).toBe("running");expect(cell).toMatchSnapshot(); // snapshot everything as a catch all@@ -1137,7 +1135,7 @@ describe("cell reducer", () => {timestamp: new Date(22).getTime() as Seconds,});cell = cells[0];- expect(cell.consoleOutputs).toEqual([+ expect(cell.consoleOutputs).toMatchObject([STDOUT,{ ...STD_IN_1, response: "Marimo!" },STD_IN_2,@@ -1161,7 +1159,7 @@ describe("cell reducer", () => {});cell = cells[0];expect(cell.status).toBe("idle");- expect(cell.consoleOutputs).toEqual([+ expect(cell.consoleOutputs).toMatchObject([STDOUT,{ ...STD_IN_1, response: "Marimo!" },{ ...STD_IN_2, response: "" },@@ -1204,7 +1202,7 @@ describe("cell reducer", () => {});cell = cells[0];expect(cell.status).toBe("queued");- expect(cell.consoleOutputs).toEqual([OLD_STDOUT]); // Old stays there until it starts running+ expect(cell.consoleOutputs).toMatchObject([OLD_STDOUT]); // Old stays there until it starts runningexpect(cell).toMatchSnapshot(); // snapshot everything as a catch all// Receive queued messages@@ -1218,7 +1216,7 @@ describe("cell reducer", () => {});cell = cells[0];expect(cell.status).toBe("queued");- expect(cell.consoleOutputs).toEqual([OLD_STDOUT]); // Old stays there until it starts running+ expect(cell.consoleOutputs).toMatchObject([OLD_STDOUT]); // Old stays there until it starts runningexpect(cell).toMatchSnapshot(); // snapshot everything as a catch all// Receive running messages@@ -1232,7 +1230,7 @@ describe("cell reducer", () => {});cell = cells[0];expect(cell.status).toBe("running");- expect(cell.consoleOutputs).toEqual([]);+ expect(cell.consoleOutputs).toMatchObject([]);expect(cell).toMatchSnapshot(); // snapshot everything as a catch all// Add console@@ -1245,7 +1243,7 @@ describe("cell reducer", () => {timestamp: new Date(22).getTime() as Seconds,});cell = cells[0];- expect(cell.consoleOutputs).toEqual([STDOUT]);+ expect(cell.consoleOutputs).toMatchObject([STDOUT]);expect(cell.status).toBe("idle");expect(cell).toMatchSnapshot(); // snapshot everything as a catch all});@@ -1640,7 +1638,16 @@ describe("cell reducer", () => {});const cell = cells[0];- expect(cell.consoleOutputs).toEqual([STDOUT1, STDOUT2]);+ expect(cell.consoleOutputs).toMatchInlineSnapshot(`+ [+ {+ "channel": "stdout",+ "data": "output1output2",+ "mimetype": "text/plain",+ "timestamp": 1,+ },+ ]+ `);});it("can add a column breakpoint", () => {diff --git a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.tsindex 6198b1df03..eeb838475a 100644--- a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts+++ b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts@@ -1,8 +1,12 @@/* Copyright 2024 Marimo. All rights reserved. */import { describe, expect, it } from "vitest";+import { StatefulOutputMessage } from "@/components/editor/output/ansi-reduce";import type { OutputMessage } from "@/core/kernel/messages";-import { collapseConsoleOutputs } from "../collapseConsoleOutputs";+import {+ collapseConsoleOutputs,+ maybeMakeOutputStateful,+} from "../collapseConsoleOutputs";describe("collapseConsoleOutputs", () => {it("should collapse last two text/plain outputs on the same channel", () => {@@ -236,4 +240,397 @@ describe("collapseConsoleOutputs", () => {// heuristic: non-text counts as 1 line ...expect(result[2].data).toBe("<pre>E\nF\nG\nH\n</pre>");});++ describe("ANSI escape sequences", () => {+ it("should handle cursor movement with collapse", () => {+ const consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "output",+ data: "Hello",+ timestamp: 0,+ },+ {+ mimetype: "text/plain",+ channel: "output",+ data: "\u001B[5DWorld", // Move cursor back 5, write World+ timestamp: 0,+ },+ ];+ const result = collapseConsoleOutputs(consoleOutputs);+ expect(result[0].data).toBe("World");+ });++ it("should handle progress bar simulation", () => {+ // Simulate streaming: collapse is called after each new message+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Progress: 0%\r",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Progress: 50%\r",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Progress: 100%",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe("Progress: 100%");+ });++ it("should handle tqdm-like progress bars", () => {+ // Simulate streaming: collapse is called after each new message+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Processing: | | 0/100\r",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Processing: |█████ | 50/100\r",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Processing: |██████████| 100/100",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe("Processing: |██████████| 100/100");+ });++ it("should handle cursor up/down movements", () => {+ // Test that cursor movements within a single message are handled+ const consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Line 1\nLine 2\n\u001B[1AModified",+ timestamp: 0,+ },+ ];++ const result = collapseConsoleOutputs(consoleOutputs);+ // After moving up 1 line and writing "Modified", Line 2 gets overwritten+ expect(result[0].data).toBe("Line 1\nModified");+ });++ it("should handle clear screen sequence", () => {+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Old content\nMore old content\n",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\u001B[2J", // Clear screen+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "New content",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe("New content");+ });++ it("should handle erase line sequences", () => {+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Hello World",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\u001B[2K", // Erase entire line+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Goodbye",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe(" Goodbye");+ });++ it("should handle cursor positioning", () => {+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "abc",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\u001B[1;2H", // Move to position (1,2)+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "XY",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe("aXY");+ });++ it("should handle complex multi-line progress updates", () => {+ // Test multi-line updates with ANSI sequences all in one message+ const consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Task 1: 0%\nTask 2: 0%\n\u001B[2A\rTask 1: 100%\n\rTask 2: 100%\n",+ timestamp: 0,+ },+ ];+ const result = collapseConsoleOutputs(consoleOutputs);+ expect(result[0].data).toBe("Task 1: 100%\nTask 2: 100%\n");+ });++ it("should preserve ANSI state across multiple collapsed messages", () => {+ // Simulate streaming collapses+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Loading",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\rLoading.",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\rLoading..",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\rLoading...",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\rComplete! ",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs[0].data).toBe("Complete! ");+ });+ });++ describe("StatefulOutputMessage", () => {+ it("should preserve StatefulOutputMessage through collapse", () => {+ const statefulMessage = StatefulOutputMessage.create({+ mimetype: "text/plain",+ channel: "stdout",+ data: "Hello\rWorld",+ timestamp: 0,+ });++ const consoleOutputs: OutputMessage[] = [+ statefulMessage,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: " Complete",+ timestamp: 0,+ },+ ];++ const result = collapseConsoleOutputs(consoleOutputs);+ expect(result.length).toBe(1);+ expect(result[0]).toBeInstanceOf(StatefulOutputMessage);+ expect(result[0].data).toBe("World Complete");+ });++ it("should handle mixing StatefulOutputMessage and regular messages", () => {+ let consoleOutputs: OutputMessage[] = [+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Line 1\n",+ timestamp: 0,+ },+ ];++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Progress: 0%",+ timestamp: 0,+ },+ ]);++ consoleOutputs = collapseConsoleOutputs([+ ...consoleOutputs,+ {+ mimetype: "text/plain",+ channel: "stdout",+ data: "\rProgress: 100%",+ timestamp: 0,+ },+ ]);++ expect(consoleOutputs.length).toBe(1);+ expect(consoleOutputs[0]).toBeInstanceOf(StatefulOutputMessage);+ expect(consoleOutputs[0].data).toBe("Line 1\nProgress: 100%");+ });++ it("should handle StatefulOutputMessage with empty array input", () => {+ const consoleOutputs: OutputMessage[] = [];+ const result = collapseConsoleOutputs(consoleOutputs);+ expect(result).toEqual([]);+ });++ it("should handle StatefulOutputMessage with single message", () => {+ const statefulMessage = StatefulOutputMessage.create({+ mimetype: "text/plain",+ channel: "stdout",+ data: "Hello\rWorld",+ timestamp: 0,+ });++ const result = collapseConsoleOutputs([statefulMessage]);+ expect(result.length).toBe(1);+ expect(result[0]).toBeInstanceOf(StatefulOutputMessage);+ expect(result[0].data).toBe("World");+ });+ });++ describe("maybeMakeOutputStateful", () => {+ it("should convert regular string output to StatefulOutputMessage", () => {+ const output: OutputMessage = {+ mimetype: "text/plain",+ channel: "stdout",+ data: "Hello",+ timestamp: 0,+ };++ const result = maybeMakeOutputStateful(output);+ expect(result).toBeInstanceOf(StatefulOutputMessage);+ expect(result.data).toBe("Hello");+ });++ it("should preserve StatefulOutputMessage as-is", () => {+ const statefulMessage = StatefulOutputMessage.create({+ mimetype: "text/plain",+ channel: "stdout",+ data: "Hello",+ timestamp: 0,+ });++ const result = maybeMakeOutputStateful(statefulMessage);+ expect(result).toBe(statefulMessage);+ });++ it("should return non-string output as-is", () => {+ const output: OutputMessage = {+ mimetype: "application/json",+ channel: "output",+ data: { key: "value" },+ timestamp: 0,+ };++ const result = maybeMakeOutputStateful(output);+ expect(result).toBe(output);+ expect(result).not.toBeInstanceOf(StatefulOutputMessage);+ });+ });});diff --git a/frontend/src/core/cells/cell.ts b/frontend/src/core/cells/cell.tsindex a8e70b7242..246f0b59aa 100644--- a/frontend/src/core/cells/cell.ts+++ b/frontend/src/core/cells/cell.ts@@ -126,15 +126,14 @@ export function transitionCell(});}- if (message.console !== null) {+ const messageConsole = message.console;+ if (messageConsole != null) {// The kernel sends an empty array to clear the console; otherwise,- // message.console is an output that needs to be appended to the+ // messageConsole is an output that needs to be appended to the// existing console outputs.- consoleOutputs = Array.isArray(message.console)- ? message.console- : collapseConsoleOutputs(- [...consoleOutputs, message.console].filter(Boolean),- );+ consoleOutputs = Array.isArray(messageConsole)+ ? collapseConsoleOutputs(messageConsole)+ : collapseConsoleOutputs([...consoleOutputs, messageConsole]);}nextCell.consoleOutputs = consoleOutputs;// Derive outline from outputdiff --git a/frontend/src/core/cells/collapseConsoleOutputs.tsx b/frontend/src/core/cells/collapseConsoleOutputs.tsxindex 6f7fc498c7..a46b539251 100644--- a/frontend/src/core/cells/collapseConsoleOutputs.tsx+++ b/frontend/src/core/cells/collapseConsoleOutputs.tsx@@ -1,4 +1,8 @@/* Copyright 2024 Marimo. All rights reserved. */+import {+ StatefulOutputMessage,+ type StringOutputMessage,+} from "@/components/editor/output/ansi-reduce";import type { OutputMessage } from "@/core/kernel/messages";import { invariant } from "@/utils/invariant";@@ -13,21 +17,27 @@ export function collapseConsoleOutputs(const newConsoleOutputs = [...consoleOutputs];if (newConsoleOutputs.length < 2) {- return truncateHead(handleCarriageReturns(newConsoleOutputs), maxLines);+ return truncateHead(+ newConsoleOutputs.map(maybeMakeOutputStateful),+ maxLines,+ );}const lastOutput = newConsoleOutputs[newConsoleOutputs.length - 1];const secondLastOutput = newConsoleOutputs[newConsoleOutputs.length - 2];if (shouldCollapse(lastOutput, secondLastOutput)) {- invariant(typeof lastOutput.data === "string", "expected string");- invariant(typeof secondLastOutput.data === "string", "expected string");+ assertStringOutputMessage(lastOutput);+ assertStringOutputMessage(secondLastOutput);- secondLastOutput.data += lastOutput.data;+ newConsoleOutputs[newConsoleOutputs.length - 2] = mergeLeft(+ secondLastOutput,+ lastOutput,+ );newConsoleOutputs.pop();}- return truncateHead(handleCarriageReturns(newConsoleOutputs), maxLines);+ return truncateHead(newConsoleOutputs, maxLines);}function shouldCollapse(@@ -46,51 +56,31 @@ function shouldCollapse(return isTextPlain && isSameChannel && isNotStdin;}-function handleCarriageReturns(- consoleOutputs: OutputMessage[],-): OutputMessage[] {- const newConsoleOutputs = [...consoleOutputs];- if (newConsoleOutputs.length === 0) {- return newConsoleOutputs;+export function maybeMakeOutputStateful(output: OutputMessage): OutputMessage {+ if (output instanceof StatefulOutputMessage) {+ return output;}-- const lastOutput = newConsoleOutputs[newConsoleOutputs.length - 1];- if (lastOutput.mimetype !== "text/plain") {- return newConsoleOutputs;+ if (typeof output.data === "string") {+ return StatefulOutputMessage.create(output as StringOutputMessage);}+ return output;+}- // eslint-disable-next-line no-control-regex- const carriagePattern = /\r[^\n]/g;- // collapse carriage returns in the final output's data- let text = lastOutput.data;- invariant(typeof text === "string", "expected string");- let carriageIdx = text.search(carriagePattern);- while (carriageIdx > -1) {- // find the newline character preceding the carriage return, if any- let newlineIdx = -1;- for (let i = carriageIdx - 1; i >= 0; i--) {- if (text.at(i) === "\n") {- newlineIdx = i;- break;- }- }- const postCarriageText: string = text.slice(carriageIdx + 1);- const prefix = text.slice(0, newlineIdx + 1);- const intermediateText: string = text.slice(newlineIdx + 1, carriageIdx);- text =- intermediateText.length <= postCarriageText.length- ? prefix + postCarriageText- : prefix +- postCarriageText +- intermediateText.slice(postCarriageText.length);- carriageIdx = text.search(carriagePattern);+function mergeLeft(+ first: StringOutputMessage | StatefulOutputMessage,+ second: StringOutputMessage,+): StatefulOutputMessage {+ if (first instanceof StatefulOutputMessage) {+ return first.appendData(second.data);}- newConsoleOutputs[newConsoleOutputs.length - 1] = {- ...lastOutput,- data: text,- };- return newConsoleOutputs;+ return StatefulOutputMessage.create(first).appendData(second.data);+}++function assertStringOutputMessage(+ output: OutputMessage,+): asserts output is StringOutputMessage {+ invariant(typeof output.data === "string", "expected string output");}function truncateHead(consoleOutputs: OutputMessage[], limit: number) {diff --git a/frontend/src/core/static/virtual-file-tracker.ts b/frontend/src/core/static/virtual-file-tracker.tsindex 8b53ed0b9a..6223d263a7 100644--- a/frontend/src/core/static/virtual-file-tracker.ts+++ b/frontend/src/core/static/virtual-file-tracker.ts@@ -4,7 +4,7 @@ import type { CellId } from "../cells/ids";import type { CellMessage } from "../kernel/messages";// Virtual files are of the form /@file/<file-name>.<extension>-const VIRTUAL_FILE_REGEX = /\/@file\/([^\s/"'&]+)\.([\dA-Za-z]+)/g;+const VIRTUAL_FILE_REGEX = /\/@file\/([^\s"&'/]+)\.([\dA-Za-z]+)/g;/*** Tracks virtual files that are present on the page.diff --git a/frontend/src/utils/__tests__/json-parser.test.ts b/frontend/src/utils/__tests__/json-parser.test.tsindex 5cd097198c..fe255bfc78 100644--- a/frontend/src/utils/__tests__/json-parser.test.ts+++ b/frontend/src/utils/__tests__/json-parser.test.ts@@ -113,7 +113,7 @@ it("can convert json to tsv with fr-FR locale", () => {const locale = "fr-FR";// Handles floats with fr-FR locale (uses , as decimal separator)- expect(jsonToTSV([{ a: 3.14, b: 2.71828 }], locale)).toEqual(+ expect(jsonToTSV([{ a: 3.14, b: 2.718_28 }], locale)).toEqual("a\tb\n3,14\t2,71828",);});diff --git a/marimo/_server/session/session_view.py b/marimo/_server/session/session_view.pyindex 4b8deb57b5..fd8e86b4ef 100644--- a/marimo/_server/session/session_view.py+++ b/marimo/_server/session/session_view.py@@ -411,6 +411,38 @@ class SessionView:self.auto_export_state.mark_all_stale()+def _merge_consecutive_console_outputs(+ console: list[CellOutput],+) -> list[CellOutput]:+ """Merge consecutive text/plain outputs with the same channel."""+ if not console:+ return console++ merged: list[CellOutput] = []++ for output in console:+ # Check if we can merge with the last output+ if (+ merged+ and merged[-1].mimetype == "text/plain"+ and output.mimetype == "text/plain"+ and merged[-1].channel == output.channel+ and isinstance(merged[-1].data, str)+ and isinstance(output.data, str)+ ):+ # Merge by concatenating the data+ merged[-1] = CellOutput(+ channel=merged[-1].channel,+ mimetype=merged[-1].mimetype,+ data=merged[-1].data + output.data,+ timestamp=merged[-1].timestamp,+ )+ else:+ merged.append(output)++ return merged++def merge_cell_operation(previous: Optional[CellOp],next_: CellOp,@@ -430,7 +462,7 @@ def merge_cell_operation(else:combined_console: list[CellOutput] = as_list(previous.console)combined_console.extend(as_list(next_.console))- next_.console = combined_console+ next_.console = _merge_consecutive_console_outputs(combined_console)# If we went from running to running, use the previous timestamp.if next_.status == "running" and previous.status == "running":diff --git a/marimo/_smoke_tests/third_party/pymc_example.py b/marimo/_smoke_tests/third_party/pymc_example.pynew file mode 100644index 0000000000..9161cc17d5--- /dev/null+++ b/marimo/_smoke_tests/third_party/pymc_example.py@@ -0,0 +1,36 @@+import marimo++__generated_with = "0.17.7"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return+++def _():+ import pymc as pm+ import numpy as np+ import time++ # Define a simple model+ with pm.Model() as model:+ # Define a normal distribution+ mu = pm.Normal("mu", mu=0, sigma=1)+ # Sample from the distribution+ obs = pm.Normal("obs", mu=mu, sigma=1, observed=np.random.randn(100))+++ # Sample from the posterior using NUTS with NumPyro+ trace = pm.sample(draws=100000, nuts_sampler="numpyro")++ # Returning the trace object for further analysis+ trace+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_dependencies/test_dependencies.py b/tests/_dependencies/test_dependencies.pyindex 775d510a60..2db4e39908 100644--- a/tests/_dependencies/test_dependencies.py+++ b/tests/_dependencies/test_dependencies.py@@ -90,34 +90,34 @@ def test_regular_package_no_cache_invalidation() -> None:def test_versions():assert (DependencyManager.altair.require_version(- min_version="0.0.0", max_version="6.0.0"+ min_version="0.0.0", max_version="10.0.0")is None)with pytest.raises(RuntimeError) as excinfo:- DependencyManager.altair.require_version(min_version="6.0.0")+ DependencyManager.altair.require_version(min_version="10.0.0")version = DependencyManager.altair.get_version()assert (str(excinfo.value)- == f"Mismatched version of altair: expected >=6.0.0, got {version}"+ == f"Mismatched version of altair: expected >=10.0.0, got {version}")# Override versionassert (DependencyManager.altair.has_at_version(- min_version="0.0.0", max_version="6.0.0"+ min_version="0.0.0", max_version="10.0.0")is True)assert (- DependencyManager.altair.has_at_version(min_version="7.0.0") is False+ DependencyManager.altair.has_at_version(min_version="10.0.0") is False)with pytest.raises(RuntimeError) as excinfo:DependencyManager.altair.require_at_version(- why="for testing", min_version="6.0.0"+ why="for testing", min_version="10.0.0")assert "Mismatched version of test: expected >=2.0.0, got 1.0.0"diff --git a/tests/_server/session/test_session_view.py b/tests/_server/session/test_session_view.pyindex e36dba5e0e..20e68e456f 100644--- a/tests/_server/session/test_session_view.py+++ b/tests/_server/session/test_session_view.py@@ -714,9 +714,9 @@ def test_combine_console_outputs())+ # Consecutive text/plain stdout outputs are mergedassert session_view.cell_operations[cell_id].console == [- CellOutput.stdout("one"),- CellOutput.stdout("two"),+ CellOutput.stdout("onetwo"),]# Moves to queued@@ -729,8 +729,7 @@ def test_combine_console_outputs()assert session_view.cell_operations[cell_id].console == [- CellOutput.stdout("one"),- CellOutput.stdout("two"),+ CellOutput.stdout("onetwo"),]# Moves to running clears console@@ -787,6 +786,145 @@ def test_stdin(time_mock: Any, session_view: SessionView) -> None:]+@patch("time.time", return_value=123)+def test_merge_consecutive_text_plain_outputs(+ time_mock: Any, session_view: SessionView+) -> None:+ """Test that consecutive text/plain outputs with same channel are merged."""+ del time_mock++ # Add multiple consecutive stdout outputs+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("Hello "),+ status="running",+ )+ )+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("World"),+ status="running",+ )+ )+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("!"),+ status="running",+ )+ )++ # Should be merged into a single output+ assert len(session_view.cell_operations[cell_id].console) == 1+ assert (+ session_view.cell_operations[cell_id].console[0].data == "Hello World!"+ )+ assert (+ session_view.cell_operations[cell_id].console[0].channel+ == CellChannel.STDOUT+ )+++@patch("time.time", return_value=123)+def test_merge_different_channels_not_merged(+ time_mock: Any, session_view: SessionView+) -> None:+ """Test that outputs with different channels are not merged."""+ del time_mock++ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("stdout message"),+ status="running",+ )+ )+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stderr("stderr message"),+ status="running",+ )+ )++ # Should remain separate+ assert len(session_view.cell_operations[cell_id].console) == 2+ assert (+ session_view.cell_operations[cell_id].console[0].channel+ == CellChannel.STDOUT+ )+ assert (+ session_view.cell_operations[cell_id].console[1].channel+ == CellChannel.STDERR+ )+++@patch("time.time", return_value=123)+def test_merge_different_mimetypes_not_merged(+ time_mock: Any, session_view: SessionView+) -> None:+ """Test that outputs with different mimetypes are not merged."""+ del time_mock++ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("plain text", mimetype="text/plain"),+ status="running",+ )+ )+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("html content", mimetype="text/html"),+ status="running",+ )+ )++ # Should remain separate+ assert len(session_view.cell_operations[cell_id].console) == 2+ assert (+ session_view.cell_operations[cell_id].console[0].mimetype+ == "text/plain"+ )+ assert (+ session_view.cell_operations[cell_id].console[1].mimetype+ == "text/html"+ )+++@patch("time.time", return_value=123)+def test_merge_with_non_string_data_not_merged(+ time_mock: Any, session_view: SessionView+) -> None:+ """Test that outputs with non-string data are not merged."""+ del time_mock++ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput.stdout("text"),+ status="running",+ )+ )+ session_view.add_operation(+ CellOp(+ cell_id=cell_id,+ console=CellOutput(+ channel=CellChannel.STDOUT,+ mimetype="text/plain",+ data={"key": "value"}, # dict, not string+ ),+ status="running",+ )+ )++ # Should remain separate+ assert len(session_view.cell_operations[cell_id].console) == 2++@patch("time.time", return_value=123)def test_get_cell_outputs(time_mock: Any, session_view: SessionView) -> None:del time_mock@@ -877,8 +1015,9 @@ def test_get_cell_console_outputs())+ # Consecutive text/plain stdout outputs are mergedassert session_view.get_cell_console_outputs([cell_id, cell_2_id]) == {- cell_id: [CellOutput.stdout("one"), CellOutput.stdout("two")],+ cell_id: [CellOutput.stdout("onetwo")],cell_2_id: [CellOutput.stdout("two")],}