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.

102 KB
Raw Blame
# Copyright 2026 Marimo. All rights reserved.

from __future__ import annotations

import ast
import asyncio
import dataclasses
import os
import subprocess
import sys
import textwrap
from typing import TYPE_CHECKING, Any

import pytest

from marimo._ast.app import App
from marimo._dependencies.dependencies import DependencyManager
from marimo._runtime.commands import ExecuteStaleCellsCommand
from marimo._runtime.runtime import Kernel
from marimo._save.hash import BlockHasher
from tests.conftest import ExecReqProvider

if TYPE_CHECKING:
    from pathlib import Path

    from marimo._runtime.dataflow import DirectedGraph


class TestHash:
    @staticmethod
    def test_pure_hash(app) -> None:
        @app.cell
        def one() -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                Y = [1, 2, 3]
                Z = len(Y)
            assert cache._cache.cache_type == "Pure"
            return Y, Z

    @staticmethod
    def test_content_hash(app) -> None:
        @app.cell
        def one() -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            a = 1
            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                Y = a
            assert cache._cache.cache_type == "ContentAddressed"
            return (Y,)

    # Note: Hash may change based on byte code, so pin to particular version
    @staticmethod
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_content_reproducibility(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "RSccsMCC0dBqvdcnBN1mdxlvKUr4zzR_qupKBW_P_qE"

            return expected_hash, persistent_cache, MockLoader

        @app.cell
        def one(expected_hash, persistent_cache, MockLoader) -> None:
            _a = 1
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                _X = 10 + _a
            assert _X == 7
            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, _cache._cache.hash

        @app.cell
        def two(expected_hash, persistent_cache, MockLoader) -> tuple[int]:
            # The same as cell one, but with this comment
            _a = 2 - 1

            # Some white space
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                # More Comments
                _X = 10 + _a
            assert _X == 7
            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, _cache._cache.hash
            # and a post block difference
            Z = 11
            return (Z,)

    @staticmethod
    def test_execution_hash(app) -> None:
        @app.cell
        def one() -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            non_primitive = [object()]

            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                Y = 8 + len(non_primitive)
            assert cache._cache.cache_type == "ContextExecutionPath"
            return (Y,)

    @staticmethod
    def test_single_expression_execution_hash() -> None:
        # The final expression is treated a tiny bit differently than marimo,
        # which has led to bugs.
        app = App()
        app._anonymous_file = True
        app._pytest_rewrite = True

        @app.cell
        def _():
            non_primitive = [object()]
            return non_primitive

        @app.cell
        def _(non_primitive) -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            with persistent_cache(name="test", _loader=MockLoader()) as cache:
                Y = 8 + len(non_primitive)
            assert cache._cache.cache_type == "ExecutionPath"
            return (Y,)

        _, defs = app.run()

        app2 = App()
        app2._anonymous_file = True
        app2._pytest_rewrite = True

        @app2.cell
        def _():
            non_primitive = [object(), object()]
            return non_primitive

        @app2.cell
        def _(non_primitive) -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            with persistent_cache(name="test", _loader=MockLoader()) as cache:
                Y = 8 + len(non_primitive)
            assert cache._cache.cache_type == "ExecutionPath"
            return (Y,)

        _, defs2 = app2.run()

        assert defs["cache"]._cache.hash != defs2["cache"]._cache.hash

    @staticmethod
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_execution_reproducibility(app) -> None:
        # Rewrite changes the AST, breaking the hash
        app._pytest_rewrite = False

        @app.cell
        def imports() -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            return persistent_cache, MockLoader

        @app.cell
        def load() -> tuple[int]:
            shared = [None, object()]
            return shared

        @app.cell
        def one(persistent_cache, MockLoader, shared) -> tuple[int]:
            _a = [1, object()]
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                _X = 10 + _a[0] - len(shared)  # Comment
            assert _X == 7
            # Cannot be reused/ shared, because it will change the hash.
            assert (
                _cache._cache.hash
                == "r2_DqjuluzDmVs1wo1HZCNWz9wApoSSJlnXeYihOaNI"
            ), _cache._cache.hash
            assert _cache._cache.cache_type == "ContextExecutionPath"
            return

        @app.cell
        def two(persistent_cache, MockLoader, shared) -> tuple[int]:
            # The same as cell one, but with this comment
            _a = [
                1,  # Comment
                object(),
            ]
            # Some white space
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                # More Comments
                _X = 10 + _a[0] - len(shared)
            assert _X == 7
            assert (
                _cache._cache.hash
                == "r2_DqjuluzDmVs1wo1HZCNWz9wApoSSJlnXeYihOaNI"
            ), _cache._cache.hash
            assert _cache._cache.cache_type == "ContextExecutionPath"
            # and a post block difference
            Z = 11
            return (Z,)

    @staticmethod
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_execution_reproducibility_different_cell_order(app) -> None:
        # NB. load is last for cell order difference.
        # Rewrite changes the AST, breaking the hash
        app._pytest_rewrite = False

        @app.cell
        def one(persistent_cache, MockLoader, shared) -> tuple[int]:
            _a = [1, object()]
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                _X = 10 + _a[0] - len(shared)  # Comment
            assert _X == 7
            # Cannot be reused/ shared, because it will change the hash.
            assert (
                _cache._cache.hash
                == "r2_DqjuluzDmVs1wo1HZCNWz9wApoSSJlnXeYihOaNI"
            ), _cache._cache.hash
            assert _cache._cache.cache_type == "ContextExecutionPath"
            return

        @app.cell
        def two(persistent_cache, MockLoader, shared) -> tuple[int]:
            # The same as cell one, but with this comment
            _a = [
                1,  # Comment
                object(),
            ]
            # Some white space
            with persistent_cache(
                name="one", _loader=MockLoader(data={"_X": 7})
            ) as _cache:
                # More Comments
                _X = 10 + _a[0] - len(shared)
            assert _X == 7
            assert (
                _cache._cache.hash
                == "r2_DqjuluzDmVs1wo1HZCNWz9wApoSSJlnXeYihOaNI"
            ), _cache._cache.hash
            assert _cache._cache.cache_type == "ContextExecutionPath"
            # and a post block difference
            Z = 11
            return (Z,)

        @app.cell
        def imports() -> tuple[int]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            return persistent_cache, MockLoader

        @app.cell
        def load() -> tuple[int]:
            shared = [None, object()]
            return shared

    @staticmethod
    def test_transitive_content_hash() -> None:
        app1 = App()
        app1._anonymous_file = True
        app1._pytest_rewrite = True

        @app1.cell
        def _():
            import marimo as mo

            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"
            from marimo._runtime.context.types import get_context

            ctx = get_context()
            return ctx, output

        @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"
            from marimo._runtime.context.types import get_context

            ctx = get_context()
            return ctx, output

        @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 len(defs1["ctx"].cell_lifecycle_registry.registry) == 2
        assert len(defs2["ctx"].cell_lifecycle_registry.registry) == 2
        assert defs1["cache"]._cache.hash != defs2["cache"]._cache.hash
        assert defs1["output"] != defs2["output"]

    @staticmethod
    def test_function_ui_content_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import marimo as mo
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            slider = mo.ui.slider(1, 10)

            def shared() -> str:
                return "x" * slider.value

            return persistent_cache, MockLoader, shared, slider

        @app.cell
        def one(persistent_cache, MockLoader, shared, slider) -> tuple[Any]:
            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                _Y = len(shared())
            slider._update(7)
            assert cache._cache.cache_type == "ExecutionPath"
            return (cache,)

        @app.cell
        def two(persistent_cache, MockLoader, shared, cache) -> tuple[Any]:
            with persistent_cache(name="two", _loader=MockLoader()) as cache2:
                _Y = len(shared())
            assert cache2._cache.cache_type == "ExecutionPath"
            assert cache2._cache.hash != cache._cache.hash
            return (cache2,)

    @staticmethod
    def test_function_state_content_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import marimo as mo
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            state, set_state = mo.state(0)

            def shared(state) -> str:
                return "x" * state()

            return persistent_cache, MockLoader, shared, state, set_state

        @app.cell
        def one(
            persistent_cache, MockLoader, shared, state, set_state
        ) -> tuple[Any]:
            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                _Y = len(shared(state))
            set_state(1)
            assert cache._cache.cache_type == "ContentAddressed"
            return (cache,)

        @app.cell
        def two(
            persistent_cache, MockLoader, shared, state, cache
        ) -> tuple[Any]:
            with persistent_cache(name="two", _loader=MockLoader()) as cache2:
                _Y = len(shared(state))
            assert cache2._cache.cache_type == "ContentAddressed"
            assert cache2._cache.hash != cache._cache.hash
            return (cache2,)

    @staticmethod
    def test_function_state_content_hash_distinct(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import marimo as mo
            from marimo._save.save import cache

            state, _set_state = mo.state(0)

            @cache
            def check_type(v) -> str:
                return str(type(v))

            a = check_type(0)
            b = check_type(state)
            assert a != b, (a, "!=", b)
            assert a == "<class 'int'>", a
            assert "State" in b, b
            return a, b

    @staticmethod
    def test_transitive_execution_path_when_state_dependent(app) -> None:
        @app.cell
        def load() -> tuple[Any, ...]:
            import marimo as mo
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            state, set_state = mo.state(0)

            def shared() -> str:
                return "x" * state()

            return persistent_cache, MockLoader, shared, state, set_state

        @app.cell
        def one(
            persistent_cache, MockLoader, shared, set_state, state
        ) -> tuple[Any]:
            with persistent_cache(name="one", _loader=MockLoader()) as cache:
                _Y = len(shared())
            assert _Y == state()
            set_state(1)
            assert cache._cache.cache_type == "ExecutionPath"
            return (cache,)

        @app.cell
        def two(
            persistent_cache, MockLoader, shared, cache, state
        ) -> tuple[Any]:
            with persistent_cache(name="two", _loader=MockLoader()) as cache2:
                _Y = len(shared())
            assert _Y == state()
            assert cache2._cache.cache_type == "ExecutionPath"
            assert cache2._cache.hash != cache._cache.hash
            return (cache2,)

    @staticmethod
    def test_version_pinning(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import marimo as mo
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            mo.__version__ = "0.0.0"

            return mo, persistent_cache, MockLoader

        @app.cell
        def one(mo, persistent_cache, MockLoader) -> tuple[Any]:
            with persistent_cache(
                name="one", _loader=MockLoader(), pin_modules=True
            ) as cache:
                _Y = len(mo.__version__)
            mo.__version__ = "0.0.1"
            assert cache._cache.cache_type == "ContentAddressed"
            return (cache,)

        @app.cell
        def two(mo, persistent_cache, MockLoader, cache) -> tuple[Any]:
            with persistent_cache(
                name="two", _loader=MockLoader(), pin_modules=True
            ) as cache2:
                _Y = len(mo.__version__)
            mo.__version__ = "0.0.0"
            assert cache2._cache.cache_type == "ContentAddressed"
            assert cache2._cache.hash != cache._cache.hash
            return (cache2,)

    @staticmethod
    def test_builtins(app) -> None:
        @app.cell
        def _():
            import time
            from time import sleep

            import marimo as mo

            return mo, sleep, time

        @app.cell
        def _(mo, sleep):
            @mo.cache
            def direct():
                _ = sleep
                return 42

        @app.cell
        def _(mo, time):
            @mo.cache
            def module():
                _ = time.sleep
                return 42

        @app.cell
        def _(direct, module):
            assert direct() == module(), "direct() != module()"


class TestHashMemo:
    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_data_primitive_memoized_across_cells(app) -> None:
        """Data primitives should be memoized — same array used in two cells
        should only be serialized once."""

        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            arr = np.ones((64, 64))
            return MockLoader, persistent_cache, arr, np

        @app.cell
        def one(MockLoader, persistent_cache, arr, np) -> tuple[Any]:
            from marimo._runtime.context.types import get_context

            ctx = get_context()
            with persistent_cache(name="one", _loader=MockLoader()) as c1:
                _v = np.sum(arr)
            # After hashing, arr should be memoized
            assert len(ctx.cache.hash_memo) > 0
            return c1, ctx

        @app.cell
        def two(MockLoader, persistent_cache, arr, np, c1, ctx) -> None:
            memo_before = dict(ctx.cache.hash_memo)
            with persistent_cache(name="two", _loader=MockLoader()) as c2:
                _v = np.sum(arr)
            # Same arr, so memo should have been used (same entry)
            assert c1._cache.hash == c2._cache.hash
            assert memo_before == dict(ctx.cache.hash_memo)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_same_cell_def_not_memoized(app) -> None:
        """Variables defined by the current cell should not use memo
        (values can mutate within a cell)."""

        @app.cell
        def one() -> tuple[Any]:
            import numpy as np

            from marimo._runtime.context.types import get_context
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            arr = np.ones((4, 4))
            with persistent_cache(name="one", _loader=MockLoader()) as c:
                _v = np.sum(arr)
            ctx = get_context()
            # arr is defined in this cell, so should NOT be memoized
            assert len(ctx.cache.hash_memo) == 0
            return c, ctx

    @staticmethod
    def test_list_mutation_not_stale(app) -> None:
        """Lists are not memoized, so in-place mutation produces different
        hashes."""

        @app.cell
        def one() -> tuple[Any]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            data = [1, 2, 3]
            with persistent_cache(name="one", _loader=MockLoader()) as c1:
                _v = sum(data)
            data.append(4)
            with persistent_cache(name="two", _loader=MockLoader()) as c2:
                _v = sum(data)
            assert c1._cache.hash != c2._cache.hash

    @staticmethod
    def test_dict_mutation_not_stale(app) -> None:
        """Dicts are not memoized, so mutation produces different hashes."""

        @app.cell
        def one() -> tuple[Any]:
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            data = {"a": 1}
            with persistent_cache(name="one", _loader=MockLoader()) as c1:
                _v = sum(data.values())
            data["b"] = 2
            with persistent_cache(name="two", _loader=MockLoader()) as c2:
                _v = sum(data.values())
            assert c1._cache.hash != c2._cache.hash

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_memo_with_cached_function(app) -> None:
        """Memoization works with @mo.cache deferred hashing."""

        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            import marimo as mo

            arr = np.ones((32, 32))
            return mo, np, arr

        @app.cell
        def one(mo, np, arr) -> tuple[Any]:
            @mo.cache
            def compute(x):
                return np.sum(x)

            r1 = compute(arr)
            r2 = compute(arr)
            assert r1 == r2
            assert compute.hits == 1
            return (compute,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_cached_fn_distinct_ndarray_args(app) -> None:
        """Distinct ndarray args vs kwargs must not collide."""

        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            import marimo as mo

            return mo, np

        @app.cell
        def one(mo, np) -> tuple[Any]:
            @mo.cache
            def f(a):
                return float(a.sum())

            assert f(np.array([1.0, 2.0, 3.0])) == 6.0
            # Check alternative arg path to ensure no collision between args and kwargs.
            assert f(np.array([4.0, 5.0, 6.0])) == 15.0
            # Keyword form exercises the same arg path.
            assert f(a=np.array([7.0, 8.0, 9.0])) == 24.0
            assert f.hits == 0
            assert f.misses == 3
            return (f,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_lifecycle_cleanup(app) -> None:
        """HashMemoCleanup clears memo when defining cell is disposed."""

        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            from marimo._runtime.context.types import get_context
            from marimo._save.cache import HashMemoCleanup
            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            arr = np.ones((4, 4))
            return (
                MockLoader,
                persistent_cache,
                arr,
                np,
                get_context,
                HashMemoCleanup,
            )

        @app.cell
        def one(
            MockLoader, persistent_cache, arr, np, get_context, HashMemoCleanup
        ) -> tuple[Any]:
            with persistent_cache(name="one", _loader=MockLoader()) as c:
                _v = np.sum(arr)
            ctx = get_context()
            assert len(ctx.cache.hash_memo) > 0
            # Simulate lifecycle disposal — should clear memo
            cleanup = HashMemoCleanup()
            cleanup.dispose(ctx, deletion=False)
            assert len(ctx.cache.hash_memo) == 0

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    def test_globals_mutation_stale_memo(app) -> None:
        """Known limitation: mutating a data primitive via globals() bypasses
        lifecycle cleanup, producing a stale memo hit. This is expected (but
        highly discouraged) since globals() sidesteps marimo's reactivity — but
        we capture the behavior so it doesn't silently change."""

        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            arr = np.ones((4, 4))
            return MockLoader, persistent_cache, arr, np

        @app.cell
        def one(MockLoader, persistent_cache, arr, np) -> tuple[Any]:
            with persistent_cache(name="one", _loader=MockLoader()) as c1:
                _v = np.sum(arr)
            # Mutate via globals — no lifecycle disposal triggered
            globals()["arr"] = np.zeros((4, 4))
            with persistent_cache(name="two", _loader=MockLoader()) as c2:
                _v = np.sum(arr)
            # Stale memo: hashes match even though arr changed
            assert c1._cache.hash == c2._cache.hash


class TestDataHash:
    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.numpy.has(),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_numpy_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import numpy as np

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "zLpFb6ANG99kP-4yWoH4zdV_FfrUodnEom1tILpF55c"
            return MockLoader, persistent_cache, expected_hash, np

        @app.cell
        def one(MockLoader, persistent_cache, expected_hash, np) -> tuple[int]:
            _a = np.ones((16, 16)) * 2
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = np.sum(_a)
            one = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

        @app.cell
        def two(MockLoader, persistent_cache, expected_hash, np) -> tuple[int]:
            _a = np.ones((16, 16)) + np.ones((16, 16))

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = np.sum(_a)
            two = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (two,)

        @app.cell
        def three(one, two) -> None:
            assert one == two
            assert one == 512

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("jax"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_jax_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            from jax import numpy as np

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "IaLyzmZZ4nwXMveSQjMVAa682QAyd2O90iSeHJvlb44"
            return MockLoader, persistent_cache, expected_hash, np

        @app.cell
        def one(MockLoader, persistent_cache, expected_hash, np) -> tuple[int]:
            _a = np.ones((16, 16)) * 2
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = np.sum(_a)
            one = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

        @app.cell
        def two(MockLoader, persistent_cache, expected_hash, np) -> tuple[int]:
            _a = np.ones((16, 16)) + np.ones((16, 16))

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = np.sum(_a)
            two = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (two,)

        @app.cell
        def three(one, two) -> None:
            assert one == two
            assert one == 512

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("torch"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_torch_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import torch

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "6EJfKOu_iB6jpSTtCnUV1kjYy2u96m_w_3VzOUOn5Hg"
            return MockLoader, persistent_cache, expected_hash, torch

        @app.cell
        def one(
            MockLoader, persistent_cache, expected_hash, torch
        ) -> tuple[int]:
            _a = torch.ones((16, 16)) * 2
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = torch.sum(_a)
            one = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

        @app.cell
        def two(
            MockLoader, persistent_cache, expected_hash, torch
        ) -> tuple[int]:
            _a = torch.ones((16, 16)) + torch.ones((16, 16))

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = torch.sum(_a)
            two = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (two,)

        @app.cell
        def three(one, two) -> None:
            assert one == two
            assert one == 512

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("torch"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_torch_device_hash(app) -> None:
        # Utilizing the "meta" device should give similar cross device behavior
        # as gpu.

        @app.cell
        def load() -> tuple[Any]:
            import torch

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "QIjIEzceYIH7WIvdIasBLceU3Ad40kYqTCSBnUmJZV4"
            return MockLoader, persistent_cache, expected_hash, torch

        @app.cell
        def one(
            MockLoader, persistent_cache, expected_hash, torch
        ) -> tuple[int]:
            _a = torch.ones((16, 16), device="meta") * 2
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = torch.sum(_a)
            one = _A

            assert _cache._cache.cache_type == "ContextExecutionPath", (
                _cache._cache.cache_type
            )
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("skbio"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_skibio_hash(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            from copy import copy

            from skbio import DNA

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "1eEgTTthH-FyKbziqYse1ITogQUMat0JW1meFZtMWCI"
            return MockLoader, persistent_cache, expected_hash, DNA, copy

        @app.cell
        def one(
            MockLoader, persistent_cache, expected_hash, copy, DNA
        ) -> tuple[int]:
            strand = DNA("GATTACA")
            # Siblings not bound to be in-valid
            sibling = copy(strand)
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                assert strand == sibling

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("pandas"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_dataframe(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import numpy as np
            import pandas as pd

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "ycCqtVaQAODpfHyimtlbxj1TIQB3WtLnhDIGq59yiqw"
            return MockLoader, persistent_cache, expected_hash, np, pd

        @app.cell
        def one(
            MockLoader, persistent_cache, expected_hash, np, pd
        ) -> tuple[int]:
            _a = {
                "A": [1, 2, 3],
                "column names don't contribute to hash": [4, 5, 6],
                "the dict order does though": [7, 8, 9],
            }
            _a = pd.DataFrame(_a) ** 2
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = np.sum(_a["A"])
            one = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

        @app.cell
        def two(
            MockLoader, persistent_cache, expected_hash, np, pd
        ) -> tuple[int]:
            _a = {"A": [1, 4, 9], "B": [16, 25, 36], "C": [49, 64, 81]}
            _a = pd.DataFrame(_a)

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = np.sum(_a["A"])
            two = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (two,)

        @app.cell
        def three(one, two) -> None:
            assert one == two
            assert one == 14

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("pandas"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_dataframe_object(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import numpy as np
            import pandas as pd

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "C9MbH1ov4US2mrm_T_clm4VpI9WT97tg5BGEpOAbF1g"
            return MockLoader, persistent_cache, expected_hash, np, pd

        @app.cell
        def one(
            MockLoader, persistent_cache, expected_hash, np, pd
        ) -> tuple[int]:
            _a = {
                "A": [2, 8, 18],
                "B": ["a", "a", "a"],
                "C": [14, 16, 18],
            }
            _a = pd.DataFrame(_a)
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = np.sum(_a["A"])
            one = _A

            assert _cache._cache.cache_type == "ContextExecutionPath"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("polars"),
        reason="optional dependencies not installed",
    )
    # Pin to a particular python version for differences in underlying library
    # implementations / memory layout.
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_polars_dataframe(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import polars as pl

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "DtWHQ972QmRo2kBlzgoGQDi-bRnRknWZsSFk_rT4lRA"
            return MockLoader, persistent_cache, expected_hash, pl

        @app.cell
        def one(MockLoader, persistent_cache, expected_hash, pl) -> tuple[int]:
            _a = {
                "A": [1, 2, 3],
                "column names don't contribute to hash": [4, 5, 6],
                "the dict order does though": [7, 8, 9],
            }
            _a = pl.DataFrame(_a).select(pl.all() ** 2)
            with persistent_cache(name="one", _loader=MockLoader()) as _cache:
                _A = _a.select(pl.col("A").sum()).item()
            one = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (one,)

        @app.cell
        def two(MockLoader, persistent_cache, expected_hash, pl) -> tuple[int]:
            _a = {"A": [1, 4, 9], "B": [16, 25, 36], "C": [49, 64, 81]}
            _a = pl.DataFrame(_a)

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = _a.select(pl.col("A").sum()).item()
            two = _A

            assert _cache._cache.cache_type == "ContentAddressed"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            return (two,)

        @app.cell
        def three(one, two) -> None:
            assert one == two
            assert one == 14

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("polars"),
        reason="optional dependencies not installed",
    )
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    def test_polars_object(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import polars as pl

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            expected_hash = "1HlOXWU-oQ5MNB5B9YRNLnHcn3I8sngJvLuHV_ICLGY"
            return MockLoader, persistent_cache, expected_hash, pl

        @app.cell
        def two(MockLoader, persistent_cache, expected_hash, pl) -> tuple[int]:
            _a = {
                "A": [2, 8, 18],
                "B": ["a", "a", "a"],
                "C": [14, 16, 18],
            }
            _a = pl.DataFrame(_a)

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = _a.select(pl.col("A").sum()).item()

            assert _cache._cache.cache_type == "ContextExecutionPath"
            assert _cache._cache.hash == expected_hash, (
                f"expected_hash != {_cache._cache.hash}"
            )
            assert _A == 28
            return (two,)

    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("pyarrow"),
        reason="optional dependencies not installed",
    )
    def test_pyarrow_string_table(app) -> None:
        @app.cell
        def load() -> tuple[Any]:
            import pyarrow as pa

            from marimo._save.save import persistent_cache
            from tests._save.loaders.mocks import MockLoader

            return MockLoader, persistent_cache, pa

        @app.cell
        def two(MockLoader, persistent_cache, pa) -> tuple[int]:
            _a = pa.table({"A": [2, 8, 18], "B": ["a", "b", "c"]})

            with persistent_cache(name="two", _loader=MockLoader()) as _cache:
                _A = _a.column("A").to_pylist()

            assert _cache._cache.cache_type == "ContextExecutionPath"
            assert _A == [2, 8, 18]
            return (two,)


class TestCustomHash:
    @staticmethod
    @pytest.mark.skipif(
        not DependencyManager.has("pydantic"),
        reason="optional dependencies not installed",
    )
    @pytest.mark.skipif(
        "sys.version_info < (3, 12) or sys.version_info >= (3, 13)"
    )
    async def test_pydantic_model_hash(app: App) -> None:
        with app.setup:
            import pydantic

            import marimo as mo

        @app.class_definition
        class Model(pydantic.BaseModel):
            a: int
            b: str

        @app.function
        @mo.cache
        def use_model(model: Model) -> tuple[int, str]:
            return model.a, model.b

        @app.cell
        def _check_deterministic() -> None:
            assert use_model.hits == 0
            model = Model(a=1, b="test")
            a, b = use_model(model)
            initial_hash = use_model._last_hash
            a, b = use_model(model)  # Cache hit
            assert use_model.hits == 1
            model_copy = Model(a=1, b="test")
            A, B = use_model(model_copy)  # Cache hit, different instance
            assert use_model.hits == 2
            assert (a, b) == (A, B) == (1, "test")
            assert use_model._last_hash == initial_hash

        @app.cell
        def _check_different(a: int, b: str, initial_hash: str) -> None:
            diff_model = Model(a=2, b="test")
            c, d = use_model(diff_model)
            assert use_model.hits == 2
            assert (c, d) != (a, b)
            assert initial_hash != use_model._last_hash


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"]._value

        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"]._value

        assert hash_1 != hash_2
        assert output_1 != output_2


class TestSideEffects:
    @staticmethod
    async def test_side_effect_cache_different(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                from pathlib import Path

                state, set_state = mo.state(0)
                hashes = []
                """),
                exec_req.get("""
                with mo.cache("prim") as prim_cache:
                    non_primitive = [object(), len(hashes)]
                state
                """),
                exec_req.get("""
                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1]
                """),
            ]
        )
        await k.run(
            [
                exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                    set_state(1)
                hashes.append((v_cache.cache_type, v_cache._cache.hash))
                hashes.append((prim_cache.cache_type, prim_cache._cache.hash))
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        assert len(hashes) == 4
        assert hashes[1] != hashes[3]
        assert hashes[0] != hashes[2]
        assert non_primitive[1] == 2 == v

    @staticmethod
    async def test_side_effect_cache_context(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                from pathlib import Path

                state, set_state = mo.state(0)
                hashes = []
                """),
                exec_req.get("""
                with mo.cache("prim") as prim_cache:
                    non_primitive = [object(), len(hashes)]

                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1]
                state
                """),
            ]
        )
        await k.run(
            [
                exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                    set_state(1)
                hashes.append((v_cache.cache_type, v_cache._cache.hash))
                hashes.append((prim_cache.cache_type, prim_cache._cache.hash))
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        assert len(hashes) == 4
        assert hashes[1] != hashes[3]
        assert hashes[0] != hashes[2]
        assert non_primitive[1] == 2 == v

    @staticmethod
    async def test_side_effect_exception(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                from pathlib import Path

                state, set_state = mo.state(1)
                hashes = []
                """),
                exec_req.get("""
                non_primitive = [object(), 0]
                ref = 1
                1 / state() # Throw an exception when 0
                non_primitive = [object(), 1]
                """),
            ]
        )
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                req := exec_req.get("""
                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1] + ref
                """),
                exec_req.get("""
                if len(hashes) < 1:
                    set_state(0)
                hashes.append((v_cache.cache_type, v_cache._cache.hash, v))
                """),
            ]
        )
        await k.run([req])

        assert not k.stdout.messages, k.stdout
        assert "ZeroDivisionError" in str(k.stderr), k.stderr

        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]

        v = k.globals["v"]

        assert len(hashes) == 2
        assert non_primitive[1] == 0
        assert v == 1
        assert hashes[0] != hashes[1]

    @staticmethod
    async def test_side_effect_decorator_different(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        await k.run(
            [
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                import weakref

                state, set_state = mo.state(0)
                hashes = []
                """),
                exec_req.get("""
                @mo.cache
                def prim_cache():
                    return [weakref.ref(object), len(hashes)]
                non_primitive = prim_cache()
                state
                """),
                exec_req.get("""
                @mo.cache
                def v_cache():
                    return non_primitive[1]
                v = v_cache()
                """),
            ]
        )
        await k.run(
            [
                exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                    set_state(1)
                hashes.append(v_cache._last_hash)
                hashes.append(prim_cache._last_hash)
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        assert len(hashes) == 4
        assert hashes[1] != hashes[3]
        assert hashes[0] != hashes[2]
        assert non_primitive[1] == 2 == v

    @staticmethod
    async def test_side_effect_decorator_context(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        # Actually doesn't test side effects, because there's
        # no "context" level hash for functions. Placed here in case the
        # functionality does change in the future.
        await k.run(
            [
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                import weakref

                state, set_state = mo.state(0)
                hashes = []
                """),
                exec_req.get("""
                @mo.cache
                def prim_cache():
                    return [weakref.ref(object), len(hashes)]
                non_primitive = prim_cache()

                @mo.cache
                def v_cache():
                    return non_primitive[1]
                v = v_cache()
                state
                """),
            ]
        )
        await k.run(
            [
                exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                    set_state(1)
                hashes.append(v_cache._last_hash)
                hashes.append(prim_cache._last_hash)
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert k.stderr.messages, k.stderr
        assert "Content addressed hash could not be utilized" in str(
            k.stderr
        ), k.stderr

    @staticmethod
    async def test_side_effect_decorator_exception(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                from pathlib import Path

                state, set_state = mo.state(1)
                hashes = []
                """),
                exec_req.get("""
                non_primitive = [object(), 0]
                ref = 1
                1 / state() # Throw an exception when 0
                non_primitive = [object(), 1]
                """),
            ]
        )
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                req := exec_req.get("""
                @mo.cache
                def v_cache():
                    return non_primitive[1] + ref
                v = v_cache()
                """),
                exec_req.get("""
                if len(hashes) < 1:
                    set_state(0)
                hashes.append((v_cache._last_hash, v))
                """),
            ]
        )
        await k.run([req])

        assert not k.stdout.messages, k.stdout
        assert "ZeroDivisionError" in str(k.stderr), k.stderr

        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]

        v = k.globals["v"]

        assert len(hashes) == 2
        assert non_primitive[1] == 0
        assert v == 1
        assert hashes[0] != hashes[1]

    @staticmethod
    @pytest.mark.usefixtures("cleanup_watchers")
    async def test_side_effect_file(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        control_requests = []
        k.enqueue_control_request = lambda req: control_requests.append(req)  # type: ignore
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                mo.watch._file._TEST_SLEEP_INTERVAL = 0.01
                from pathlib import Path

                hashes = []
                """),
                r := exec_req.get("""
                f = mo.watch.file(tmp_path_fixture / "test.txt")
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                exec_req.get("""
                f
                non_primitive = [object(), len(hashes), f.exists()]
                """),
                exec_req.get("""
                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1]
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                r := exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                hashes.append((v_cache.cache_type, v_cache._cache.hash))
                """),
            ]
        )
        (tmp_path / "test.txt").touch()
        await asyncio.sleep(0.25)
        assert len(control_requests) == 1
        assert isinstance(control_requests[0], ExecuteStaleCellsCommand)
        assert k.graph.cells[r.cell_id].stale
        await k.run_stale_cells()

        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        assert len(hashes) == 2
        assert hashes[0] != hashes[1]
        assert non_primitive[1] == 1 == v

    @staticmethod
    @pytest.mark.usefixtures("cleanup_watchers")
    async def test_side_effect_directory(
        k: Kernel,
        exec_req: ExecReqProvider,
        tmp_path: Path,
        monkeypatch: pytest.MonkeyPatch,
    ) -> None:
        from marimo._runtime.commands import CommandMessage
        from marimo._runtime.watch import _directory

        loop = asyncio.get_running_loop()
        watcher_ready = asyncio.Event()
        request_received = asyncio.Event()
        control_requests: list[CommandMessage] = []
        original_hashable_walk = _directory.hashable_walk

        def hashable_walk(
            path: Path,
        ) -> set[tuple[Path, tuple[str], tuple[str]]]:
            structure = original_hashable_walk(path)
            # The watcher must capture its baseline before we change the directory.
            loop.call_soon_threadsafe(watcher_ready.set)
            return structure

        def enqueue_control_request(request: CommandMessage) -> None:
            control_requests.append(request)
            loop.call_soon_threadsafe(request_received.set)

        monkeypatch.setattr(_directory, "hashable_walk", hashable_walk)
        k.enqueue_control_request = enqueue_control_request
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                mo.watch._directory._TEST_SLEEP_INTERVAL = 0.01
                from pathlib import Path

                hashes = []
                """),
                exec_req.get("""
                (tmp_path_fixture / "test_dir").mkdir(parents=True, exist_ok=True)
                d = mo.watch.directory(tmp_path_fixture / "test_dir")
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                exec_req.get("""
                non_primitive = [object(), len(hashes), d.glob("*")]
                """),
                exec_req.get("""
                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1]
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                r := exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                hashes.append((v_cache.cache_type, v_cache._cache.hash))
                """),
            ]
        )
        await asyncio.wait_for(watcher_ready.wait(), timeout=5)
        (tmp_path / "test_dir" / "test.txt").write_text("test")
        await asyncio.wait_for(request_received.wait(), timeout=5)
        assert len(control_requests) == 1
        assert isinstance(control_requests[0], ExecuteStaleCellsCommand)
        assert k.graph.cells[r.cell_id].stale
        await k.run_stale_cells()

        await k.run([])
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        assert len(hashes) == 2
        assert hashes[0] != hashes[1]
        assert non_primitive[1] == 1 == v

    @staticmethod
    @pytest.mark.usefixtures("cleanup_watchers")
    async def test_side_effect_file_ref(
        k: Kernel, exec_req: ExecReqProvider, tmp_path
    ) -> None:
        await k.run(
            [
                exec_req.get(
                    f'tmp_path_fixture = Path("{tmp_path.as_posix()}")'
                ),
                exec_req.get("""
                from tests._save.loaders.mocks import MockLoader
                import marimo as mo
                from pathlib import Path

                hashes = []
                """),
                exec_req.get(
                    'u = mo.watch.file(tmp_path_fixture / "test.txt")'
                ),
                exec_req.get("""
                non_primitive = [object(), len(hashes)]
                with mo.cache("prim") as prim_cache:
                    # unused, but should trigger side effect
                    u
                """),
                exec_req.get("""
                with mo.persistent_cache("get_v", save_path=tmp_path_fixture) as v_cache:
                    v = non_primitive[1]
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        await k.run(
            [
                exec_req.get("""
                if len(hashes) < 1:
                    assert non_primitive[1] == 0
                    u.write_text("test")
                hashes.append((v_cache.cache_type, v_cache._cache.hash))
                hashes.append((prim_cache.cache_type, prim_cache._cache.hash))
                """),
            ]
        )
        assert not k.stdout.messages, k.stdout
        assert not k.stderr.messages, k.stderr
        v = k.globals["v"]
        hashes = k.globals["hashes"]
        non_primitive = k.globals["non_primitive"]
        # Docs warn not to use write directly since RC can occur, causing double
        # event.
        assert len(hashes) == 4
        assert hashes[1] != hashes[3]
        assert hashes[0] != hashes[2]
        assert non_primitive[1] == 2 == v


class TestWrappedFunctionCache:
    """Test cache behavior with wrapped functions (decorators) across kernel calls."""

    @staticmethod
    async def test_decorator_hash_same_name_different_kernels(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test that decorators with same function names in different kernel calls have different hashes."""

        # First kernel execution
        cell_id = "test_cell"
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            def my_decorator(func):
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    wrapper._call_count = getattr(wrapper, '_call_count', 0) + 1
                    return func(*args, **kwargs)
                wrapper._kernel_version = 1
                return wrapper

            @my_decorator
            def my_function():
                return "kernel_1"

            @mo.cache
            def cached_decorated_function():
                return my_function()

            result1 = cached_decorated_function()
            hash1 = cached_decorated_function._last_hash
            cache_type1 = cached_decorated_function.base_block.cache_type
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result1"] == "kernel_1"
        first_hash = k.globals["hash1"]
        first_function = k.globals["my_function"]
        first_cache_type = k.globals["cache_type1"]

        # Second kernel execution - update the same cell with different definitions
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            def my_decorator(func):
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    wrapper._call_count = getattr(wrapper, '_call_count', 0) + 1
                    return func(*args, **kwargs)
                wrapper._kernel_version = 2  # Different version
                return wrapper

            @my_decorator
            def my_function():
                return "kernel_2"  # Different return value

            @mo.cache
            def cached_decorated_function():
                return my_function()

            result2 = cached_decorated_function()
            hash2 = cached_decorated_function._last_hash
            cache_type2 = cached_decorated_function.base_block.cache_type
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result2"] == "kernel_2"
        second_hash = k.globals["hash2"]
        second_function = k.globals["my_function"]
        second_cache_type = k.globals["cache_type2"]

        # Functions should be different objects despite same name
        assert first_function is not second_function
        assert (
            first_function._kernel_version != second_function._kernel_version
        )

        # Should use ContentAddressed cache type since decorator is pure
        assert first_cache_type == "ContentAddressed", (
            f"Expected ContentAddressed, got {first_cache_type}"
        )
        assert second_cache_type == "ContentAddressed", (
            f"Expected ContentAddressed, got {second_cache_type}"
        )

        # Cache hashes should be different
        assert first_hash != second_hash, (
            f"Expected different hashes, got {first_hash} == {second_hash}"
        )

    @staticmethod
    async def test_impure_decorator_with_pure_function(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test impure decorator applied to pure function - decorator should dominate purity."""

        # First execution with impure decorator
        cell_id = "test_cell"
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            # This will be an impure decorator (contains non-primitive objects)
            impure_state = [object()]  # Non-primitive, makes decorator impure

            def my_impure_decorator(func):
                '''An impure decorator that depends on impure_state'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    # Decorator depends on impure_state
                    wrapper._call_count = len(impure_state)
                    return func(*args, **kwargs)
                return wrapper

            @my_impure_decorator
            def pure_function():
                # This function itself is pure (no external dependencies)
                return 42

            @mo.cache
            def cached_function():
                return pure_function()
            """,
                ),
                exec_req.get_with_id(
                    "call_cell",
                    """

            result1 = cached_function()
            hash1 = cached_function._last_hash
            cache_type1 = cached_function.base_block.cache_type
            """,
                ),
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result1"] == 42
        first_hash = k.globals["hash1"]
        first_cache_type = k.globals["cache_type1"]

        # Second execution - change the impure decorator state
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            # Different impure state (different length)
            impure_state = [object(), object()]  # Different length affects decorator

            def my_impure_decorator(func):
                '''Same impure decorator with different state'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    # Decorator depends on impure_state (now different)
                    wrapper._call_count = len(impure_state)
                    return func(*args, **kwargs)
                return wrapper

            @my_impure_decorator
            def pure_function():
                # Same pure function
                return 42

            @mo.cache
            def cached_function():
                return pure_function()
            """,
                ),
                exec_req.get_with_id(
                    "call_cell",
                    """

            result2 = cached_function()
            hash2 = cached_function._last_hash
            cache_type2 = cached_function.base_block.cache_type
            """,
                ),
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert (
            k.globals["result2"] == 42
        )  # Same result since pure_function unchanged
        second_hash = k.globals["hash2"]
        second_cache_type = k.globals["cache_type2"]

        # The decorated function should be treated as impure due to impure decorator
        # ContextExecutionPath, but since decorated ExecutionPath
        assert first_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {first_cache_type}"
        )
        assert second_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {second_cache_type}"
        )

        # Hashes should be different because the decorator's dependencies changed
        # (even though the underlying pure function is the same)
        assert first_hash != second_hash, (
            f"Expected different hashes for different decorator dependencies, "
            f"got {first_hash} == {second_hash}"
        )

    @staticmethod
    async def test_pure_decorator_with_impure_dependencies_different_cells(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test impure dependencies in different cells (should be execution refs)."""

        # First execution - setup dependencies in separate cells
        await k.run(
            [
                exec_req.get_with_id(
                    "dep_cell",
                    """
            import functools

            # This will be an impure dependency (contains non-primitive objects)
            impure_dependency = [object()]  # Non-primitive, makes it impure

            def my_pure_decorator(func):
                '''A pure decorator from external module perspective'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    return func(*args, **kwargs)
                return wrapper
            """,
                ),
                exec_req.get_with_id(
                    "func_cell",
                    """
            @my_pure_decorator
            def decorated_function():
                # This function depends on impure_dependency from another cell
                return len(impure_dependency)
            """,
                ),
                exec_req.get_with_id(
                    "cache_cell",
                    """
            import marimo as mo

            @mo.cache
            def cached_function():
                return decorated_function()

            result1 = cached_function()
            hash1 = cached_function._last_hash
            cache_type1 = cached_function.base_block.cache_type
            """,
                ),
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result1"] == 1
        first_hash = k.globals["hash1"]
        first_cache_type = k.globals["cache_type1"]

        # Second execution - change the impure dependency in separate cell
        await k.run(
            [
                exec_req.get_with_id(
                    "dep_cell",
                    """
            import functools

            # Different impure dependency (different object, different length)
            impure_dependency = [object(), object()]  # Different length

            def my_pure_decorator(func):
                '''Same pure decorator'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    return func(*args, **kwargs)
                return wrapper
            """,
                ),
                exec_req.get_with_id(
                    "func_cell",
                    """
            @my_pure_decorator
            def decorated_function():
                # Same function, but now depends on different impure_dependency
                return len(impure_dependency)
            """,
                ),
                exec_req.get_with_id(
                    "cache_cell",
                    """
            import marimo as mo

            @mo.cache
            def cached_function():
                return decorated_function()

            result2 = cached_function()
            hash2 = cached_function._last_hash
            cache_type2 = cached_function.base_block.cache_type
            """,
                ),
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert (
            k.globals["result2"] == 2
        )  # Different result due to different dependency
        second_hash = k.globals["hash2"]
        second_cache_type = k.globals["cache_type2"]

        # Should use ExecutionPath hashing since dependencies are in different cells
        assert first_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {first_cache_type}"
        )
        assert second_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {second_cache_type}"
        )

        # Hashes should be different because the execution path changed
        # (due to different cells being hashed)
        assert first_hash != second_hash, (
            f"Expected different hashes for different cell dependencies, "
            f"got {first_hash} == {second_hash}"
        )

    @staticmethod
    async def test_pure_decorator_with_impure_dependencies(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test the impure edge case: decorator is pure but dependent functions are not."""

        # First execution with impure dependency
        cell_id = "test_cell"
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            # This will be an impure dependency (contains non-primitive objects)
            impure_dependency = [object()]  # Non-primitive, makes it impure

            def my_pure_decorator(func):
                '''A pure decorator from external module perspective'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    return func(*args, **kwargs)
                return wrapper

            @my_pure_decorator
            def decorated_function():
                # This function depends on impure_dependency
                return len(impure_dependency)

            @mo.cache
            def cached_function():
                return decorated_function()

            result1 = cached_function()
            hash1 = cached_function._last_hash
            cache_type1 = cached_function.base_block.cache_type
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert not k.stdout.messages, k.stdout

        assert k.globals["result1"] == 1
        first_hash = k.globals["hash1"]
        first_cache_type = k.globals["cache_type1"]

        # Second execution - change the impure dependency
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import functools
            import marimo as mo

            # Different impure dependency (different object, different length)
            impure_dependency = [object(), object()]  # Different length

            def my_pure_decorator(func):
                '''Same pure decorator'''
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    return func(*args, **kwargs)
                return wrapper

            @my_pure_decorator
            def decorated_function():
                # Same function, but now depends on different impure_dependency
                return len(impure_dependency)

            @mo.cache
            def cached_function():
                return decorated_function()

            result2 = cached_function()
            hash2 = cached_function._last_hash
            cache_type2 = cached_function.base_block.cache_type
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert not k.stdout.messages, k.stdout

        assert (
            k.globals["result2"] == 2
        )  # Different result due to different dependency
        second_hash = k.globals["hash2"]
        second_cache_type = k.globals["cache_type2"]

        # The decorator itself is pure, but the function has impure dependencies
        # This should use ExecutionPath hashing, not ContentAddressed
        assert first_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {first_cache_type}"
        )
        assert second_cache_type == "ExecutionPath", (
            f"Expected ExecutionPath, got {second_cache_type}"
        )

        # Hashes should be different because the execution path changed
        # (due to different impure_dependency)
        assert first_hash != second_hash, (
            f"Expected different hashes for different impure dependencies, "
            f"got {first_hash} == {second_hash}"
        )

    @staticmethod
    async def test_default_argument_cache_invalidation(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test that changing default argument values invalidates the cache (issue #7977)."""

        cell_id = "test_cell"

        # First execution with config_version=1
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import marimo as mo

            @mo.cache
            def get_data_with_config(query: str, config_version=1):
                return query, config_version

            result1 = get_data_with_config("test")
            hash1 = get_data_with_config._last_hash
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result1"] == ("test", 1)
        first_hash = k.globals["hash1"]

        # Second execution with config_version=2 (same cell, different default)
        await k.run(
            [
                exec_req.get_with_id(
                    cell_id,
                    """
            import marimo as mo

            @mo.cache
            def get_data_with_config(query: str, config_version=2):
                return query, config_version

            result2 = get_data_with_config("test")
            hash2 = get_data_with_config._last_hash
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result2"] == ("test", 2)
        second_hash = k.globals["hash2"]

        # The hashes should be different because the default argument changed
        assert first_hash != second_hash, (
            f"Expected different hashes when default argument changes, "
            f"got {first_hash} == {second_hash}"
        )

    @staticmethod
    async def test_explicit_arg_matches_previous_default(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test that explicit args matching defaults produce the same hash.

        Calling fn() with default a=1 should produce the same hash as
        calling fn(1) with explicit arg, because the resolved value is the same.
        """

        # Test within a single cell to verify hash computation is correct
        await k.run(
            [
                exec_req.get(
                    """
            import marimo as mo

            @mo.cache
            def fn(a=1):
                return a + 1

            # Call with default
            result1 = fn()
            hash_default = fn._last_hash

            # Call with explicit arg matching the default
            result2 = fn(1)
            hash_explicit = fn._last_hash

            # Call with different value
            result3 = fn(2)
            hash_different = fn._last_hash
            """,
                )
            ]
        )

        assert not k.stderr.messages, k.stderr
        assert k.globals["result1"] == 2  # 1 + 1
        assert k.globals["result2"] == 2  # 1 + 1
        assert k.globals["result3"] == 3  # 2 + 1

        # fn() and fn(1) should have the same hash (same resolved value)
        assert k.globals["hash_default"] == k.globals["hash_explicit"], (
            f"Expected same hash for fn() and fn(1), "
            f"got {k.globals['hash_default']} != {k.globals['hash_explicit']}"
        )
        # fn(2) should have a different hash
        assert k.globals["hash_default"] != k.globals["hash_different"], (
            f"Expected different hash for fn() and fn(2), "
            f"got same hash {k.globals['hash_default']}"
        )

    @staticmethod
    async def test_various_function_signature_forms(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        """Test cache works with various function signature forms.

        Tests: positional-only (/), keyword-only (*), defaults, *args, **kwargs
        """

        await k.run(
            [
                exec_req.get(
                    """
import marimo as mo

# Test 1: Keyword-only parameters with defaults
@mo.cache
def kwonly(a, *, value=1):
    return a + value

kw_r1 = kwonly(10)           # Call with default
kw_h1 = kwonly._last_hash
kw_r2 = kwonly(10, value=1)  # Call with explicit same value
kw_h2 = kwonly._last_hash
kw_r3 = kwonly(10, value=2)  # Call with different value
kw_h3 = kwonly._last_hash
kw_hits = kwonly.hits

# Test 2: Positional-only parameters with defaults
@mo.cache
def posonly(a, b=5, /):
    return a + b

po_r1 = posonly(10)      # Call with default
po_h1 = posonly._last_hash
po_r2 = posonly(10, 5)   # Call with explicit same value
po_h2 = posonly._last_hash
po_r3 = posonly(10, 7)   # Call with different value
po_h3 = posonly._last_hash
po_hits = posonly.hits

# Test 3: Mixed positional-only, regular, and keyword-only
@mo.cache
def mixed(a, /, b, c=3, *, d=4):
    return a + b + c + d

mx_r1 = mixed(1, 2)          # Call with defaults for c and d
mx_h1 = mixed._last_hash
mx_r2 = mixed(1, 2, 3, d=4)  # Call with explicit same values
mx_h2 = mixed._last_hash
mx_r3 = mixed(1, 2, d=5)     # Call with different d
mx_h3 = mixed._last_hash
mx_hits = mixed.hits

# Test 4: *args and **kwargs
@mo.cache
def varargs(a, *args, b=10, **kwargs):
    return a + sum(args) + b + sum(kwargs.values())

va_r1 = varargs(1, 2, 3)         # Call with default b
va_h1 = varargs._last_hash
va_r2 = varargs(1, 2, 3, b=10)   # Call with explicit same b
va_h2 = varargs._last_hash
va_r3 = varargs(1, 2, 3, b=20)   # Call with different b
va_h3 = varargs._last_hash
va_r4 = varargs(1, 2, 3, x=100)  # Call with kwargs
va_h4 = varargs._last_hash
va_hits = varargs.hits
            """,
                )
            ]
        )
        assert not k.stderr.messages, k.stderr

        # Test 1: Keyword-only assertions
        assert k.globals["kw_r1"] == 11
        assert k.globals["kw_r2"] == 11
        assert k.globals["kw_r3"] == 12
        assert k.globals["kw_h1"] == k.globals["kw_h2"], (
            "kwonly: explicit same value should hit cache"
        )
        assert k.globals["kw_h1"] != k.globals["kw_h3"], (
            "kwonly: different value should miss cache"
        )
        assert k.globals["kw_hits"] == 1, (
            f"Expected 1 hit, got {k.globals['kw_hits']}"
        )

        # Test 2: Positional-only assertions
        assert k.globals["po_r1"] == 15
        assert k.globals["po_r2"] == 15
        assert k.globals["po_r3"] == 17
        assert k.globals["po_h1"] == k.globals["po_h2"], (
            "posonly: explicit same value should hit cache"
        )
        assert k.globals["po_h1"] != k.globals["po_h3"], (
            "posonly: different value should miss cache"
        )
        assert k.globals["po_hits"] == 1, (
            f"Expected 1 hit, got {k.globals['po_hits']}"
        )

        # Test 3: Mixed assertions
        assert k.globals["mx_r1"] == 10  # 1+2+3+4
        assert k.globals["mx_r2"] == 10
        assert k.globals["mx_r3"] == 11  # 1+2+3+5
        assert k.globals["mx_h1"] == k.globals["mx_h2"], (
            "mixed: explicit same values should hit cache"
        )
        assert k.globals["mx_h1"] != k.globals["mx_h3"], (
            "mixed: different d should miss cache"
        )
        assert k.globals["mx_hits"] == 1, (
            f"Expected 1 hit, got {k.globals['mx_hits']}"
        )

        # Test 4: Varargs assertions
        assert k.globals["va_r1"] == 16  # 1+2+3+10
        assert k.globals["va_r2"] == 16
        assert k.globals["va_r3"] == 26  # 1+2+3+20
        assert k.globals["va_r4"] == 116  # 1+2+3+10+100
        assert k.globals["va_h1"] == k.globals["va_h2"], (
            "varargs: explicit same b should hit cache"
        )
        assert k.globals["va_h1"] != k.globals["va_h3"], (
            "varargs: different b should miss cache"
        )
        assert k.globals["va_h1"] != k.globals["va_h4"], (
            "varargs: with kwargs should miss cache"
        )
        assert k.globals["va_hits"] == 1, (
            f"Expected 1 hit, got {k.globals['va_hits']}"
        )


def _hash_fn(fn: object) -> bytes:
    """Run the full get_hashable_ast -> compile -> hash pipeline."""
    from marimo._ast.transformers import get_hashable_ast
    from marimo._save.hash import hash_raw_module

    assert callable(fn)
    return hash_raw_module(get_hashable_ast(fn))


class TestRemoveReturnsBytecode:
    """Bytecode-level tests for RemoveReturns (regression #8364)."""

    def test_different_name_exprs_different_hash(self) -> None:
        """Changing the returned expression must change the hash."""

        def fn_add(x: int, y: int) -> int:
            return x + y

        def fn_sub(x: int, y: int) -> int:
            return x - y

        assert _hash_fn(fn_add) != _hash_fn(fn_sub)

    def test_different_constants_different_hash(self) -> None:
        """Different numeric returns must hash differently."""

        def fn_a() -> int:
            return 11 + 19

        def fn_b() -> int:
            return 11 + 29

        assert _hash_fn(fn_a) != _hash_fn(fn_b)

    def test_different_single_constants_different_hash(self) -> None:
        def fn_a() -> int:
            return 42

        def fn_b() -> int:
            return 99

        assert _hash_fn(fn_a) != _hash_fn(fn_b)

    def test_different_pure_lambdas_different_hash(self) -> None:
        """Pure lambdas with different bodies must hash differently."""

        def fn_a() -> object:
            return lambda: 1

        def fn_b() -> object:
            return lambda: 2

        assert _hash_fn(fn_a) != _hash_fn(fn_b)


class TestSetLiteralDeterminism:
    """Set literals are constant-folded into frozensets in co_consts, whose
    str()/iteration order is PYTHONHASHSEED-dependent (issue #9829)."""

    def test_set_literal_changes_hash(self) -> None:
        """Changing set members must still change the hash."""

        def fn_a(x: object) -> bool:
            return x in {"A", "B", "C"}

        def fn_b(x: object) -> bool:
            return x in {"A", "B", "D"}

        assert _hash_fn(fn_a) != _hash_fn(fn_b)

    def test_hash_deterministic_across_hashseed(self) -> None:
        """A set-literal function must hash identically across PYTHONHASHSEED.

        The set literal is constant-folded into a frozenset in the function's
        co_consts; `hash_module` is what previously serialized it with a
        seed-dependent `str()`.
        """
        program = textwrap.dedent(
            """
            from marimo._save.hash import hash_module

            def fn(x):
                return x in {"A", "B", "C", "D", "E", 1, 2.0, None}

            print(hash_module(fn.__code__).hex())
            """
        )
        digests = set()
        for seed in ("0", "1", "2", "42", "12345"):
            env = {**os.environ, "PYTHONHASHSEED": seed}
            result = subprocess.run(
                [sys.executable, "-c", program],
                capture_output=True,
                text=True,
                env=env,
            )
            assert result.returncode == 0, result.stderr
            digests.add(result.stdout.strip())
        assert len(digests) == 1, f"non-deterministic across seeds: {digests}"

    def test_singleton_set_unchanged(self) -> None:
        """A singleton set literal stays on the str() path (len > 1 guard).

        `str(frozenset({'A'}))` has only one possible order, so it was never
        broken; its hash must equal the plain str()-based serialization so we
        don't invalidate caches that already worked.
        """
        import hashlib

        from marimo._save.hash import hash_module

        def fn(x: object) -> bool:
            # Keep the singleton set: this test inspects its frozenset constant.
            return x in {"A"}  # noqa: FURB171

        code = fn.__code__
        singleton = next(c for c in code.co_consts if isinstance(c, frozenset))
        assert len(singleton) == 1

        expected = hashlib.new("sha256", usedforsecurity=False)
        for const in code.co_consts:
            expected.update(str(const).encode("utf8"))
        expected.update(bytes("|".join(code.co_names), "utf8"))
        expected.update(code.co_code)

        assert hash_module(code) == expected.digest()

    def test_exotic_const_types_dont_crash(self) -> None:
        """Set literals can hold complex/bytes/bool/tuple — none may crash.

        repr-based serialization handles these; the byte serializers
        (primitive_to_bytes / common_container_to_bytes) reject complex.
        """
        from marimo._save.hash import hash_module

        def fn(x: object) -> bool:
            return x in {1j, 2j, b"x", True, (1, 2), "s"}

        assert isinstance(hash_module(fn.__code__), bytes)


def test_decorator_params_affect_hash() -> None:
    """Verify that changing decorator parameters changes the function hash.

    When using @mo.cache with nested decorators like:
        @mo.cache
        @spl(schema={"a": "int"})
        def query(): ...

    Changing the schema should invalidate the cache.
    """
    from marimo._ast.transformers import get_hashable_ast
    from marimo._save.hash import hash_raw_module

    def cache(fn):
        return fn

    def decorator_with_param(param: str):
        del param

        def wrapper(fn):
            return fn

        return wrapper

    # Helper to hash with cache skipped (mimics mo.cache behavior)
    def hash_fn(fn):
        return hash_raw_module(get_hashable_ast(fn, skip_decorators={"cache"}))

    # Test 1: Decorator AFTER @cache - param change should affect hash
    @cache
    @decorator_with_param(param="value1")
    def fn():
        return 42

    hash1 = hash_fn(fn)

    @cache
    @decorator_with_param(param="value2")
    def fn():
        return 42

    hash2 = hash_fn(fn)

    assert hash1 != hash2, (
        "Decorator param change AFTER @cache should invalidate hash"
    )

    # Test 2: Decorator BEFORE @cache - param change should NOT affect hash
    @decorator_with_param(param="value1")
    @cache
    def fn():
        return 42

    hash3 = hash_fn(fn)

    @decorator_with_param(param="value2")
    @cache
    def fn():
        return 42

    hash4 = hash_fn(fn)

    assert hash3 == hash4, (
        "Decorator param change BEFORE @cache should NOT affect hash"
    )


async def test_decorator_ref_tracking_e2e(
    k: Kernel, exec_req: ExecReqProvider
) -> None:
    """End-to-end test that refs in decorator params affect cache behavior.

    When a decorator parameter references a variable:
        schema = {"a": "int"}
        @mo.cache
        @my_decorator(schema=schema)
        def query(): ...

    Changing the variable's value should produce different cache hashes.
    """

    cell_id = "test_cell"

    # First execution with schema_config = "v1"
    await k.run(
        [
            exec_req.get_with_id(
                cell_id,
                """
import marimo as mo

schema_config = "v1"

def my_decorator(schema):
    def wrapper(fn):
        fn._schema = schema
        return fn
    return wrapper

@mo.cache
@my_decorator(schema=schema_config)
def cached_query():
    return "result"

result = cached_query()
hash_val = cached_query._last_hash
            """,
            )
        ]
    )

    assert not k.stderr.messages, k.stderr
    assert k.globals["result"] == "result"
    first_hash = k.globals["hash_val"]

    # Second execution with schema_config = "v2"
    await k.run(
        [
            exec_req.get_with_id(
                cell_id,
                """
import marimo as mo

schema_config = "v2"

def my_decorator(schema):
    def wrapper(fn):
        fn._schema = schema
        return fn
    return wrapper

@mo.cache
@my_decorator(schema=schema_config)
def cached_query():
    return "result"

result = cached_query()
hash_val = cached_query._last_hash
            """,
            )
        ]
    )

    assert not k.stderr.messages, k.stderr
    assert k.globals["result"] == "result"
    second_hash = k.globals["hash_val"]

    # The hashes should be different because the decorator param changed
    assert first_hash != second_hash, (
        f"Expected different hashes when decorator ref changes, "
        f"got {first_hash} == {second_hash}"
    )


@dataclasses.dataclass(frozen=True)
class _Selection:
    """Stand-in for a UI value (e.g. mo.ui.matplotlib selection) that is
    picklable but neither a primitive nor a data-primitive."""

    vertices: tuple[tuple[float, float], ...]


def test_signed_stateful_bytes_content_varies() -> None:
    # A stateful value that is not content-addressable must still hash by its
    # content (pickle last-resort), so a consumer re-keys when it changes.
    hasher = BlockHasher.__new__(BlockHasher)
    a = hasher._signed_stateful_bytes(_Selection(((0.0, 0.0),)), "ui")
    b = hasher._signed_stateful_bytes(_Selection(((1.0, 2.0),)), "ui")
    assert isinstance(a, bytes)
    assert isinstance(b, bytes)
    assert a != b
    # Same value -> same bytes (deterministic).
    assert a == hasher._signed_stateful_bytes(_Selection(((0.0, 0.0),)), "ui")


def test_signed_stateful_bytes_uses_custom_stub() -> None:
    # A value with a registered custom stub hashes by the stub's canonical
    # bytes (what the cache stores it as), not a generic pickle.
    from marimo._save.encode import type_sign
    from marimo._save.stubs import CUSTOM_STUBS, CustomStub, register_stub

    @dataclasses.dataclass
    class _Model:
        payload: bytes

    class _ModelStub(CustomStub):
        __slots__ = ("payload",)

        def __init__(self, obj: Any) -> None:
            self.payload = obj.payload

        def load(self, glbls: dict[str, Any]) -> Any:
            del glbls
            return _Model(self.payload)

        @staticmethod
        def get_type() -> type:
            return _Model

        def to_bytes(self) -> bytes:
            return self.payload

    register_stub(_Model, _ModelStub)
    try:
        hasher = BlockHasher.__new__(BlockHasher)
        out = hasher._signed_stateful_bytes(_Model(b"abc"), "ui")
        assert out == type_sign(b"abc", "stub")
        # Distinct stub bytes -> distinct key.
        assert out != hasher._signed_stateful_bytes(_Model(b"xyz"), "ui")
    finally:
        CUSTOM_STUBS.pop(_Model, None)


def test_signed_stateful_bytes_bytearray_is_signed_bytes() -> None:
    # A bytearray value must hash to signed, immutable bytes (not a raw,
    # mutable bytearray), and vary by content.
    hasher = BlockHasher.__new__(BlockHasher)
    a = hasher._signed_stateful_bytes(bytearray(b"abc"), "ui")
    b = hasher._signed_stateful_bytes(bytearray(b"xyz"), "ui")
    assert type(a) is bytes
    assert type(b) is bytes
    assert a != b
    assert a == hasher._signed_stateful_bytes(bytearray(b"abc"), "ui")


def test_signed_stateful_bytes_unpicklable_raises() -> None:
    hasher = BlockHasher.__new__(BlockHasher)
    with pytest.raises(TypeError, match="neither"):
        hasher._signed_stateful_bytes(lambda: None, "ui")


NOTEBOOK_WITH_SETUP = """import marimo

app = marimo.App()

with app.setup:
    import marimo as mo


@app.cell
def _():
    _private = 3
    base = _private * 2
    return


@app.cell
def _(base):
    derived = base + len(mo.__name__)
    return
"""


def _static_graph(notebook: Path) -> DirectedGraph:
    """The graph a notebook compiles to, without running it."""
    from marimo._ast.app import InternalApp
    from marimo._ast.load import load_app

    app = load_app(str(notebook))
    assert app is not None
    return InternalApp(app).graph


def _path_hashes(graph: DirectedGraph) -> set[str]:
    from marimo._save.hash import hash_cell_closure

    return {hash_cell_closure(cell_id, graph).hex() for cell_id in graph.cells}


def _path_hash_of(graph: DirectedGraph, name: str) -> str:
    """The digest of the cell defining `name`."""
    from marimo._save.hash import hash_cell_closure

    (cell_id,) = [
        cell_id for cell_id, cell in graph.cells.items() if name in cell.defs
    ]
    return hash_cell_closure(cell_id, graph).hex()


def _write_notebook(path: Path, *cells: str) -> Path:
    path.write_text(
        "import marimo\n\napp = marimo.App()\n\n\n" + "\n\n".join(cells),
        encoding="utf-8",
    )
    return path


class TestPathHash:
    """The static digest a manifest records cache entries under."""

    @staticmethod
    def test_stable_across_loads(tmp_path: Path) -> None:
        notebook = tmp_path / "nb.py"
        notebook.write_text(NOTEBOOK_WITH_SETUP, encoding="utf-8")
        first = _path_hashes(_static_graph(notebook))
        second = _path_hashes(_static_graph(notebook))
        assert first == second
        assert len(first) == 3

    @staticmethod
    async def test_kernel_and_static_graphs_agree(
        k: Kernel, exec_req: ExecReqProvider, tmp_path: Path
    ) -> None:
        """A running kernel and a compiled file must produce one digest. If
        they differ, prune reads live code as dead. Private definitions are
        mangled by cell id, which differs between the two."""
        notebook = tmp_path / "nb.py"
        notebook.write_text(NOTEBOOK_WITH_SETUP, encoding="utf-8")

        await k.run(
            [
                exec_req.get("import marimo as mo"),
                exec_req.get("_private = 3\nbase = _private * 2"),
                exec_req.get("derived = base + len(mo.__name__)"),
            ]
        )
        assert not k.stderr.messages
        assert _path_hashes(k.graph) == _path_hashes(_static_graph(notebook))

    @staticmethod
    def test_only_an_ancestor_edit_moves_a_digest(tmp_path: Path) -> None:
        """A stable digest is how prune tells that the code which produced
        an entry is still there. Only the code the entry depends on moves
        it. A digest that followed the rest of the notebook lets any edit
        make entries produced by untouched code look dead."""

        def digests(name: str, *cells: str) -> tuple[str, str]:
            graph = _static_graph(_write_notebook(tmp_path / name, *cells))
            return _path_hash_of(graph, "a"), _path_hash_of(graph, "c")

        cells = [
            "@app.cell\ndef _():\n    a = 1\n    return\n",
            "@app.cell\ndef _():\n    b = 2\n    return\n",
            "@app.cell\ndef _(a):\n    c = a + 1\n    return\n",
        ]
        base_a, base_c = digests("base.py", *cells)

        unrelated = list(cells)
        unrelated[1] = "@app.cell\ndef _():\n    b = 3\n    return\n"
        assert digests("unrelated.py", *unrelated) == (base_a, base_c)

        # `c` reads `a`, so it is downstream of it and no part of its closure.
        downstream = list(cells)
        downstream[2] = "@app.cell\ndef _(a):\n    c = a + 2\n    return\n"
        assert digests("downstream.py", *downstream)[0] == base_a

        ancestor = list(cells)
        ancestor[0] = "@app.cell\ndef _():\n    a = 2\n    return\n"
        edited_a, edited_c = digests("ancestor.py", *ancestor)
        assert edited_a != base_a
        assert edited_c != base_c

    @staticmethod
    def test_ancestor_order_does_not_matter(tmp_path: Path) -> None:
        cells = [
            "@app.cell\ndef _():\n    a = 1\n    return\n",
            "@app.cell\ndef _():\n    b = 2\n    return\n",
            "@app.cell\ndef _(a, b):\n    c = a + b\n    return\n",
        ]
        forward = _write_notebook(tmp_path / "forward.py", *cells)
        swapped = _write_notebook(
            tmp_path / "swapped.py", cells[1], cells[0], cells[2]
        )
        assert _path_hashes(_static_graph(forward)) == _path_hashes(
            _static_graph(swapped)
        )

    @staticmethod
    async def test_property_tracks_the_defining_cell(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        from marimo._save.hash import hash_cell_closure

        base = exec_req.get("value = 1")
        await k.run([base, exec_req.get("doubled = value * 2")])
        assert not k.stderr.messages

        hasher = BlockHasher(
            module=ast.parse("held = value + 1"),
            graph=k.graph,
            cell_id=base.cell_id,
            scope={"value": 1},
        )
        assert (
            hasher.path_hash == hash_cell_closure(base.cell_id, k.graph).hex()
        )
        # A third digest: the block's own hashes fold in values and side
        # effects that no compile-time recomputation could reach.
        assert hasher.path_hash not in (hasher.hash, hasher.exe_hash)

    @staticmethod
    async def test_memo_is_dropped_when_a_cell_changes(
        k: Kernel, exec_req: ExecReqProvider
    ) -> None:
        from marimo._runtime.context import get_context
        from marimo._save.hash import cell_path_hash

        cell = exec_req.get("value = 1")
        await k.run([cell])
        before = cell_path_hash(cell.cell_id, k.graph)
        assert get_context().cache.node_memo

        await k.run([exec_req.get_with_id(cell.cell_id, "value = 2")])
        assert not k.stderr.messages
        assert cell_path_hash(cell.cell_id, k.graph) != before