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
fix: Fix execution hash regression (#4920)
Dylan Madisettid259549bc857
diff --git a/marimo/_save/hash.py b/marimo/_save/hash.pyindex 4e7d543c1d..e38dbc3f34 100644--- a/marimo/_save/hash.py+++ b/marimo/_save/hash.py@@ -330,10 +330,13 @@ class BlockHasher:these methods is that Nix sandboxes all execution, preventing external file access, andinternet. Sources of non-determinism are not accounted for in this implementation, and areleft to the user.+ NB. The ContextExecutionPath is an extended case of ExecutionPath hashing, just utilizing+ additional context.- In both cases, as long as the module is deterministic, the output will be deterministic. NB.- The ContextExecutionPath is an extended case of ExecutionPath hashing, just utilizing- additional context.+ In these cases, as long as the module is deterministic, the output will be deterministic.+ A side effects api is implemented to account for "uncontrollable external state" when+ utilizing builtin functions like `mo.watch.file`, but the onus is put on the user currently+ to manage external state.For optimization, the content hash is performed after the execution cache- however the contentreferences are collected first. This deferred content hash is useful in cases like repeated@@ -932,7 +935,13 @@ class BlockHasher:ref_cells = set().union(*[self.graph.definitions.get(ref, set()) for ref in refs])- return ancestors & ref_cells+ cell_basis = ancestors & ref_cells+ return (+ set().union(+ *[self.graph.ancestors(cell_id) for cell_id in cell_basis]+ )+ | cell_basis+ )def cache_attempt_from_hash(diff --git a/tests/_save/test_hash.py b/tests/_save/test_hash.pyindex 7215676d2b..da1c9e7acd 100644--- a/tests/_save/test_hash.py+++ b/tests/_save/test_hash.py@@ -7,6 +7,7 @@ from typing import Anyimport pytest+from marimo._ast.app import Appfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProvider@@ -217,22 +218,76 @@ class TestHash:return shared@staticmethod- def test_transitive_content_hash(app) -> None:- @app.cell- def load() -> tuple[int]:- from marimo._save.save import persistent_cache- from tests._save.loaders.mocks import MockLoader+ def test_transitive_content_hash() -> None:+ app1 = App()+ app1._anonymous_file = True+ app1._pytest_rewrite = True- shared = [None, object()]- return persistent_cache, MockLoader, shared+ @app1.cell+ def _():+ import marimo as mo- @app.cell- def one(persistent_cache, MockLoader, shared) -> tuple[int]:- _a = len(shared)- with persistent_cache(name="one", _loader=MockLoader()) as cache:- Y = 8 + _a- assert cache._cache.cache_type == "ContentAddressed"- return (Y,)+ return mo++ @app1.cell+ def _():+ value = False++ @app1.cell+ def cache_1(args, mo):+ with mo.persistent_cache("cache_bug") as cache:+ output = args.value+ assert cache.cache_type == "ExecutionPath"++ @app1.cell+ def _(value):+ class Unhashable:+ def __eq__(self, other):+ return isinstance(other, Unhashable)++ __hash__ = None # Makes instances unhashable++ args = Unhashable()+ args.value = value+ return (args,)++ app2 = App()+ app2._anonymous_file = True+ app2._pytest_rewrite = True++ @app2.cell+ def _():+ import marimo as mo++ return mo++ @app2.cell+ def _():+ value = True++ @app2.cell+ def cache_2(args, mo):+ with mo.persistent_cache("cache_bug") as cache:+ output = args.value+ assert cache.cache_type == "ExecutionPath"++ @app2.cell+ def _(value):+ class Unhashable:+ def __eq__(self, other):+ return isinstance(other, Unhashable)++ __hash__ = None # Makes instances unhashable++ args = Unhashable()+ args.value = value+ return (args,)++ _, defs1 = app1.run()+ _, defs2 = app2.run()++ assert defs1["cache"]._cache.hash != defs2["cache"]._cache.hash+ assert defs1["output"] != defs2["output"]@staticmethoddef test_function_ui_content_hash(app) -> None:@@ -839,6 +894,54 @@ class TestDataHash:return (two,)+# Skip for now, as the local branch is cache busting[email protected](True, reason="Cache busting")+class TestDynamicHash:+ @staticmethod+ async def test_transitive_state_hash(+ k: Kernel, exec_req: ExecReqProvider, tmp_path+ ) -> None:+ await k.run(+ [+ exec_req.get("import marimo as mo; from pathlib import Path"),+ exec_req.get("value, set_value = mo.state(False)"),+ exec_req.get("""+ class Unhashable:+ def __eq__(self, other):+ return isinstance(other, Unhashable)++ __hash__ = None # Makes instances unhashable++ args = Unhashable()+ args.value = value+ """),+ exec_req.get(f"""+ with mo.persistent_cache("cache", save_path=Path("{tmp_path.as_posix()}")) as cache:+ output = args.value+ """),+ ]+ )+ assert not k.errors+ assert not k.stdout.messages, k.stdout+ assert not k.stderr.messages, k.stderr+ assert not k.globals["cache"]._cache.hit++ hash_1 = k.globals["cache"]._cache.hash+ output_1 = k.globals["output"]++ await k.run([exec_req.get("set_value(True)")])+ assert not k.errors+ assert not k.stdout.messages, k.stdout+ assert not k.stderr.messages, k.stderr+ assert not k.globals["cache"]._cache.hit++ hash_2 = k.globals["cache"]._cache.hash+ output_2 = k.globals["output"]++ assert hash_1 != hash_2+ assert output_1 != output_2++class TestSideEffects:@staticmethodasync def test_side_effect_cache_different(