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.

chore: consolidate hash_code, set usedforsecurity=false (#7074)

Myles Scolnick7973ce70d2ba
diff --git a/frontend/src/core/cells/__tests__/session.test.ts b/frontend/src/core/cells/__tests__/session.test.ts
index 5a49178826..c154ad37f0 100644
--- a/frontend/src/core/cells/__tests__/session.test.ts
+++ b/frontend/src/core/cells/__tests__/session.test.ts
@@ -764,5 +764,125 @@ describe("notebookStateFromSession", () => {
"Session and notebook have different cells, attempted merge.",
);
});
+
+ it("merges session and notebook with mismatched cells and swapped code hashes", () => {
+ // This test simulates a real-world scenario where:
+ // - Session has cells cell-999, cell-1, cell-2 with certain outputs
+ // - Notebook has cells cell-1, cell-2, cell-3 with different code
+ // - The code hashes are mismatched, showing code has changed
+ //
+ // The merge algorithm uses edit distance on code hashes to match cells:
+ // - Session cell-1 (hash=moMd) matches Notebook cell-2 (hash=moMd)
+ // - Session cell-2 (hash=slider) matches Notebook cell-3 (hash=slider)
+ // - Session cell-999 (hash=null) doesn't match anything (null hashes never match)
+ // - Notebook cell-1 (hash=null) gets a stub session cell
+ const hashes = {
+ moMd: "2260380a88f9b8759c0ccb1230f1498e", // hash for mo.md("Hello")
+ slider: "34938d98b7a6c88431d6cad23ea0a574", // hash for x = mo.ui.slider(0, 10)
+ };
+
+ // Session has cells: cell-999 (error, no hash), cell-1 (markdown, hash=moMd), cell-2 (empty, hash=slider)
+ const sessionCells = [
+ createSessionCell(
+ "cell-999",
+ [
+ {
+ type: "error",
+ ename: "exception",
+ evalue: "division by zero",
+ traceback: [],
+ },
+ ],
+ [
+ {
+ type: "stream",
+ name: "stderr",
+ text: "Traceback (most recent call last): File <stdin>, line 1, in <module ZeroDivisionError: division by zero",
+ mimetype: "application/vnd.marimo+traceback",
+ },
+ ],
+ null, // no hash - won't match anything
+ ),
+ createSessionCell(
+ "cell-1",
+ [
+ {
+ type: "data",
+ data: { "text/markdown": "Welcome to marimo!" },
+ },
+ ],
+ [],
+ hashes.moMd, // matches notebook cell-2
+ ),
+ createSessionCell(
+ "cell-2",
+ [
+ {
+ type: "data",
+ data: { "text/plain": "" },
+ },
+ ],
+ [],
+ hashes.slider, // matches notebook cell-3
+ ),
+ ];
+
+ // Notebook has cells: cell-1 (1 / 0, no hash), cell-2 (mo.md("Hello"), hash=moMd), cell-3 (slider, hash=slider)
+ const notebookCells = [
+ createNotebookCell("cell-1", "1 / 0", null, null, null), // no hash
+ createNotebookCell("cell-2", 'mo.md("Hello")', null, null, hashes.moMd),
+ createNotebookCell(
+ "cell-3",
+ "x = mo.ui.slider(0, 10)",
+ null,
+ null,
+ hashes.slider,
+ ),
+ ];
+
+ const session = createSession(sessionCells);
+ const notebook = createNotebook(notebookCells);
+ const result = notebookStateFromSession(session, notebook);
+
+ expect(result).not.toBeNull();
+ invariant(result, "result is null");
+
+ // Should have notebook cell IDs in order (notebook is canonical)
+ expect(result.cellIds.inOrderIds).toEqual(
+ MultiColumn.from([["cell-1", "cell-2", "cell-3"]]).inOrderIds,
+ );
+
+ // Should have correct code from notebook
+ expect(result.cellData["cell-1" as CellId].code).toBe("1 / 0");
+ expect(result.cellData["cell-2" as CellId].code).toBe('mo.md("Hello")');
+ expect(result.cellData["cell-3" as CellId].code).toBe(
+ "x = mo.ui.slider(0, 10)",
+ );
+
+ // cell-1: No matching session cell (hash is null), gets stub session cell
+ expect(result.cellRuntime["cell-1" as CellId].output).toBeNull();
+ expect(result.cellRuntime["cell-1" as CellId].consoleOutputs).toEqual([]);
+
+ // cell-2: Matches session cell-1 by hash (moMd), gets its output
+ expect(result.cellRuntime["cell-2" as CellId].output).toEqual({
+ channel: "output",
+ data: "Welcome to marimo!",
+ mimetype: "text/markdown",
+ timestamp: 0,
+ });
+
+ // cell-3: Matches session cell-2 by hash (slider), gets its output
+ expect(result.cellRuntime["cell-3" as CellId].output).toEqual({
+ channel: "output",
+ data: "",
+ mimetype: "text/plain",
+ timestamp: 0,
+ });
+
+ // Should log warning about different cells (merge had edit distance > 0)
+ expect(Logger.warn).toHaveBeenCalledWith(
+ "Session and notebook have different cells, attempted merge.",
+ );
+ });
});
});
diff --git a/marimo/_server/session/serialize.py b/marimo/_server/session/serialize.py
index fd65546fd9..7b11579e60 100644
--- a/marimo/_server/session/serialize.py
+++ b/marimo/_server/session/serialize.py
@@ -2,7 +2,6 @@
from __future__ import annotations
import asyncio
-import hashlib
import json
import os
from dataclasses import dataclass
@@ -42,6 +41,7 @@ from marimo._server.session.session_view import SessionView
from marimo._types.ids import CellId_t
from marimo._utils.async_path import AsyncPath
from marimo._utils.background_task import AsyncBackgroundTask
+from marimo._utils.code import hash_code
from marimo._utils.lists import as_list
from marimo._version import __version__
@@ -361,7 +361,7 @@ def get_session_cache_file(path: Path) -> Path:
def _hash_code(code: Optional[str]) -> Optional[str]:
if code is None or code == "":
return None
- return hashlib.md5(code.encode("utf-8"), usedforsecurity=False).hexdigest()
+ return hash_code(code)
class SessionCacheWriter(AsyncBackgroundTask):
diff --git a/marimo/_utils/code.py b/marimo/_utils/code.py
index f2aa664108..aff66e573a 100644
--- a/marimo/_utils/code.py
+++ b/marimo/_utils/code.py
@@ -5,4 +5,4 @@ from __future__ import annotations
def hash_code(code: str) -> str:
import hashlib
- return hashlib.sha256(code.encode("utf-8")).hexdigest()
+ return hashlib.md5(code.encode("utf-8"), usedforsecurity=False).hexdigest()
diff --git a/tests/_server/templates/snapshots/export1.txt b/tests/_server/templates/snapshots/export1.txt
index 717c326399..bb5c5cc6be 100644
--- a/tests/_server/templates/snapshots/export1.txt
+++ b/tests/_server/templates/snapshots/export1.txt
@@ -63,7 +63,7 @@
"configOverrides": {"formatting": {"line_length": 100}},
"appConfig": {"sql_output": "auto", "width": "compact"},
"view": {"showAppCode": true},
- "notebook": {"cells": [{"code": "print('Hello, Cell 1')", "code_hash": "dd79c4410b5ac8e7ed32b54c7ebd1cda575d9a41770f739b9b8ed19a2a50ff87", "config": {}, "id": "cell1", "name": "Cell 1"}, {"code": "print('Hello, Cell 2')", "code_hash": "9f00c511e0ed7fe6413b8f4d19876bf064117d56109d22d4f8d7f7b1d426c1f9", "config": {}, "id": "cell2", "name": "Cell 2"}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
+ "notebook": {"cells": [{"code": "print('Hello, Cell 1')", "code_hash": "6eea77c855e5bfd73f3d851f474ed789", "config": {}, "id": "cell1", "name": "Cell 1"}, {"code": "print('Hello, Cell 2')", "code_hash": "0dce0226aecd5db222f6a3aa3223214b", "config": {}, "id": "cell2", "name": "Cell 2"}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
"session": {"cells": [{"code_hash": "abc123", "console": [{"name": "stdout", "text": "Hello, Cell 1", "type": "stream"}, {"name": "stderr", "text": "Error in Cell 1", "type": "stream"}], "id": "cell1", "outputs": [{"data": {"text/plain": "Hello, Cell 1"}, "type": "data"}]}, {"code_hash": "def456", "console": [], "id": "cell2", "outputs": []}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
"runtimeConfig": null,
};
@@ -84,6 +84,6 @@
print('Hello%2C%20World!')
</marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>
diff --git a/tests/_server/templates/snapshots/export2.txt b/tests/_server/templates/snapshots/export2.txt
index d25deb5f71..c7856051a4 100644
--- a/tests/_server/templates/snapshots/export2.txt
+++ b/tests/_server/templates/snapshots/export2.txt
@@ -63,7 +63,7 @@
"configOverrides": {"formatting": {"line_length": 100}},
"appConfig": {"sql_output": "auto", "width": "compact"},
"view": {"showAppCode": true},
- "notebook": {"cells": [{"code": "print('Hello, Cell 1')", "code_hash": "dd79c4410b5ac8e7ed32b54c7ebd1cda575d9a41770f739b9b8ed19a2a50ff87", "config": {}, "id": "cell1", "name": "Cell 1"}, {"code": "print('Hello, Cell 2')", "code_hash": "9f00c511e0ed7fe6413b8f4d19876bf064117d56109d22d4f8d7f7b1d426c1f9", "config": {}, "id": "cell2", "name": "Cell 2"}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
+ "notebook": {"cells": [{"code": "print('Hello, Cell 1')", "code_hash": "6eea77c855e5bfd73f3d851f474ed789", "config": {}, "id": "cell1", "name": "Cell 1"}, {"code": "print('Hello, Cell 2')", "code_hash": "0dce0226aecd5db222f6a3aa3223214b", "config": {}, "id": "cell2", "name": "Cell 2"}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
"session": {"cells": [{"code_hash": "abc123", "console": [{"name": "stdout", "text": "Hello, Cell 1", "type": "stream"}, {"name": "stderr", "text": "Error in Cell 1", "type": "stream"}], "id": "cell1", "outputs": [{"data": {"text/plain": "Hello, Cell 1"}, "type": "data"}]}, {"code_hash": "def456", "console": [], "id": "cell2", "outputs": []}], "metadata": {"marimo_version": "0.1.0"}, "version": "1"},
"runtimeConfig": null,
};
@@ -84,6 +84,6 @@
print('Hello%2C%20World!')
</marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>
diff --git a/tests/_server/templates/snapshots/export3.txt b/tests/_server/templates/snapshots/export3.txt
index 841f4adee5..46888e7f97 100644
--- a/tests/_server/templates/snapshots/export3.txt
+++ b/tests/_server/templates/snapshots/export3.txt
@@ -80,6 +80,6 @@
<body>
<div id="root"></div>
<marimo-code hidden=""></marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>
diff --git a/tests/_server/templates/snapshots/export4.txt b/tests/_server/templates/snapshots/export4.txt
index e24ac0ced8..f47124c5ae 100644
--- a/tests/_server/templates/snapshots/export4.txt
+++ b/tests/_server/templates/snapshots/export4.txt
@@ -80,6 +80,6 @@
<body>
<div id="root"></div>
<marimo-code hidden=""></marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>
diff --git a/tests/_server/templates/snapshots/export5.txt b/tests/_server/templates/snapshots/export5.txt
index 2277fae3f9..2ce953b5d5 100644
--- a/tests/_server/templates/snapshots/export5.txt
+++ b/tests/_server/templates/snapshots/export5.txt
@@ -92,6 +92,6 @@
<body>
<div id="root"></div>
<marimo-code hidden=""></marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>
diff --git a/tests/_server/templates/snapshots/export6.txt b/tests/_server/templates/snapshots/export6.txt
index ea7e907fad..640c7fc850 100644
--- a/tests/_server/templates/snapshots/export6.txt
+++ b/tests/_server/templates/snapshots/export6.txt
@@ -81,6 +81,6 @@
<body>
<div id="root"></div>
<marimo-code hidden=""></marimo-code>
-<marimo-code-hash hidden="">2b33215fadf3c54926d5c4100348afc158dbff4c94b15044e3a7fe804f80ed2d</marimo-code-hash>
+<marimo-code-hash hidden="">07da107bb575cc4e02b0e1d6d99cc204</marimo-code-hash>
</body>
</html>