A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat(cache): default persistent caching to the lazy loader (#11403)
Dylan Madisetticb68a52f4a1e
diff --git a/marimo/_config/config.py b/marimo/_config/config.pyindex 17939c81ee..d8b0d03992 100644--- a/marimo/_config/config.py+++ b/marimo/_config/config.py@@ -8,6 +8,7 @@ from dataclasses import dataclassfrom marimo._config.config_schema import MarimoConfig as MarimoConfigSchemafrom marimo._config.types import (+ CacheLoader as CacheLoader,CacheVerification as CacheVerification,CopilotMode as CopilotMode,ExecutionType as ExecutionType,@@ -631,11 +632,13 @@ class CacheConfig(TypedDict, total=False):"""Configuration for caching.`verification` is the signature-checking posture; `store` is the backing- store, or a list of stores composed into a `TieredStore`.+ store, or a list of stores composed into a `TieredStore`; `loader` is the+ default `method` for `mo.persistent_cache`."""verification: CacheVerificationstore: CacheStoreConfig+ loader: CacheLoaderclass SigningConfig(TypedDict, total=False):diff --git a/marimo/_config/config_schema.py b/marimo/_config/config_schema.pyindex afd5d20a38..fa26dc7fb7 100644--- a/marimo/_config/config_schema.py+++ b/marimo/_config/config_schema.py@@ -14,6 +14,7 @@ from msgspec import UNSET, Meta, UnsetType, fieldfrom marimo._config.packages import infer_package_managerfrom marimo._config.schema_utils import Configfrom marimo._config.types import (+ CacheLoader,CacheVerification,CopilotMode,ExecutionType,@@ -788,6 +789,10 @@ class CacheConfig(Config, kw_only=True):description="Backing cache store, or a list of stores composed into a tiered store."),] = UNSET+ loader: Annotated[+ CacheLoader | UnsetType,+ Meta(description="Default loader for mo.persistent_cache."),+ ] = UNSETclass SigningConfig(Config, kw_only=True):diff --git a/marimo/_config/reader.py b/marimo/_config/reader.pyindex 5d9c06206b..5b25f24bd0 100644--- a/marimo/_config/reader.py+++ b/marimo/_config/reader.py@@ -149,6 +149,7 @@ def allowlist_script_config(_UNTRUSTED_MARIMO_KEYS: tuple[tuple[str, ...], ...] = (("signing",),("cache", "verification"),+ ("cache", "loader"),)# `cache.store` is not a trust anchor on its own: the verifying loaders checkdiff --git a/marimo/_config/types.py b/marimo/_config/types.pyindex 1745bb5dfd..61e1687f5b 100644--- a/marimo/_config/types.py+++ b/marimo/_config/types.py@@ -14,3 +14,5 @@ SqlOutputType = Literal["polars", "lazy-polars", "pandas", "native", "auto"]StoreKey = Literal["file", "redis", "rest", "tiered"]CopilotMode = Literal["ask", "manual", "agent", "code_mode"]CacheVerification = Literal["off", "on", "strict"]+# Built-in loader or installed marimo.cache.loader entry-point name.+CacheLoader = Literal["pickle", "json", "lazy"] | strdiff --git a/marimo/_entrypoints/ids.py b/marimo/_entrypoints/ids.pyindex 00b86b191c..d5f70e4adf 100644--- a/marimo/_entrypoints/ids.py+++ b/marimo/_entrypoints/ids.py@@ -7,6 +7,7 @@ from typing import LiteralKnownEntryPoint = Literal["marimo.agent.capability","marimo.cell.executor",+ "marimo.cache.loader","marimo.cache.store","marimo.kernel.lifespan","marimo.server.asgi.lifespan",diff --git a/marimo/_runtime/context/kernel_context.py b/marimo/_runtime/context/kernel_context.pyindex 8b8338fef1..948573beae 100644--- a/marimo/_runtime/context/kernel_context.py+++ b/marimo/_runtime/context/kernel_context.py@@ -164,6 +164,7 @@ def create_kernel_context(VirtualFileStorage,)from marimo._save.cache import CacheState+ from marimo._save.loaders import get_loader_keyfrom marimo._save.signing_policy import get_signing_policyfrom marimo._save.stores import cache_store_is_untrusted, get_store@@ -201,6 +202,13 @@ def create_kernel_context(kernel.app_metadata.filename, config=kernel.user_config)),+ loader=(+ parent.cache.loader+ if parent is not None+ else get_loader_key(+ kernel.app_metadata.filename, config=kernel.user_config+ )+ ),),cell_lifecycle_registry=CellLifecycleRegistry(),app_kernel_runner_registry=(diff --git a/marimo/_runtime/context/script_context.py b/marimo/_runtime/context/script_context.pyindex 5771f3de3d..be331cd024 100644--- a/marimo/_runtime/context/script_context.py+++ b/marimo/_runtime/context/script_context.py@@ -149,6 +149,7 @@ def initialize_script_context(VirtualFileRegistry,)from marimo._save.cache import CacheState+ from marimo._save.loaders import get_loader_keyfrom marimo._save.signing_policy import get_signing_policyfrom marimo._save.stores import cache_store_is_untrusted, get_store@@ -161,6 +162,7 @@ def initialize_script_context(store=get_store(filename),store_from_untrusted_origin=cache_store_is_untrusted(filename),signing_policy=get_signing_policy(filename),+ loader=get_loader_key(filename),),cell_lifecycle_registry=CellLifecycleRegistry(),app_kernel_runner_registry=AppKernelRunnerRegistry(),diff --git a/marimo/_save/cache.py b/marimo/_save/cache.pyindex 776f63df46..a1dda7037c 100644--- a/marimo/_save/cache.py+++ b/marimo/_save/cache.py@@ -37,6 +37,7 @@ UNEXPECTED_FAILURE_BOILERPLATE = (if TYPE_CHECKING:from marimo._ast.visitor import Name+ from marimo._config.config import CacheLoaderfrom marimo._runtime.context.types import RuntimeContextfrom marimo._runtime.state import Statefrom marimo._save.hash import HashKey@@ -97,6 +98,8 @@ class CacheState:manifest_dirty: set[Store] = field(default_factory=set)# Path hash per cell, cleared with `hash_memo` when a cell re-executes.node_memo: dict[CellId_t, str] = field(default_factory=dict)+ # Default loader from the session's effective cache.loader configuration.+ loader: CacheLoader = "lazy"# Lazy-store session state; see `loaders/lazy.py:_cache_state`.active_lazy_loaders: dict[str, LazyLoader] = field(default_factory=dict)poisoned_keys: set[str] = field(default_factory=set)diff --git a/marimo/_save/loaders/__init__.py b/marimo/_save/loaders/__init__.pyindex 445bc0eeb3..19c7339833 100644--- a/marimo/_save/loaders/__init__.py+++ b/marimo/_save/loaders/__init__.py@@ -2,8 +2,10 @@from __future__ import annotationsfrom dataclasses import dataclass-from typing import Literal+from typing import TYPE_CHECKING, Literal, cast+from marimo import _loggers+from marimo._entrypoints.registry import EntryPointRegistryfrom marimo._save.loaders.json import JsonLoaderfrom marimo._save.loaders.lazy import (LazyLoader,@@ -21,8 +23,21 @@ from marimo._save.loaders.memory import MemoryLoaderfrom marimo._save.loaders.pickle import PickleLoaderfrom marimo._utils.platform import is_pyodide+if TYPE_CHECKING:+ from marimo._config.config import CacheLoader, MarimoConfig++LOGGER = _loggers.marimo_logger()+LoaderKey = Literal["memory", "pickle", "json", "lazy"]+# What `mo.persistent_cache` uses when neither `method` nor `cache.loader`+# is set.+DEFAULT_LOADER: CacheLoader = "lazy"++# Packages register Loader subclasses in marimo.cache.loader for selection+# through cache.loader.+_LOADER_REGISTRY = EntryPointRegistry[LoaderType]("marimo.cache.loader")+@dataclass(frozen=True)class DualLoader:@@ -51,7 +66,92 @@ PERSISTENT_LOADERS: dict[LoaderKey, LoaderType | DualLoader] = {"lazy": DualLoader(native=LazyLoader, wasm=WasmLazyLoader),}++def persistent_loader_names(+ registry: EntryPointRegistry[LoaderType] | None = None,+) -> list[str]:+ """Built-in persistent loader names plus allowed entry points."""+ registry = registry or _LOADER_REGISTRY+ return sorted(set(PERSISTENT_LOADERS) | set(registry.names()))+++def get_persistent_loader(+ key: str,+ registry: EntryPointRegistry[LoaderType] | None = None,+) -> LoaderType:+ """Resolve a persistent loader by name.++ Built-in names win over entry points. Raises `ValueError` for an unknown+ or disallowed name, an entry point that fails to load, or one that is not+ a `Loader` subclass.+ """+ registry = registry or _LOADER_REGISTRY+ if key in PERSISTENT_LOADERS:+ return resolve_loader(PERSISTENT_LOADERS[cast("LoaderKey", key)])+ try:+ loader = registry.get(key)+ except (KeyError, ValueError) as e:+ raise ValueError(+ f"Invalid cache loader {key!r}, expected one of "+ f"{persistent_loader_names(registry)}"+ ) from e+ except Exception as e:+ raise ValueError(f"Failed to load cache loader {key!r}: {e}") from e+ if not (isinstance(loader, type) and issubclass(loader, Loader)):+ raise ValueError(+ f"Cache loader {key!r} must be a Loader subclass, got {loader!r}."+ )+ return loader+++def normalize_loader_key(+ raw: object,+ registry: EntryPointRegistry[LoaderType] | None = None,+) -> CacheLoader:+ """Validate a `cache.loader` value, falling back to `DEFAULT_LOADER`.++ A built-in name or an allowed `marimo.cache.loader` entry point is valid.+ The entry point is not imported here; `get_persistent_loader` loads it+ when a cache first needs it.+ """+ registry = registry or _LOADER_REGISTRY+ if raw is None:+ return DEFAULT_LOADER+ if isinstance(raw, str) and (+ raw in PERSISTENT_LOADERS or raw in registry.names()+ ):+ return raw+ LOGGER.warning(+ "Invalid cache.loader %r; expected one of %s. Using %r.",+ raw,+ persistent_loader_names(registry),+ DEFAULT_LOADER,+ )+ return DEFAULT_LOADER+++def get_loader_key(+ current_path: str | None = None,+ *,+ config: MarimoConfig | None = None,+) -> CacheLoader:+ """Resolve `cache.loader` for the session, else `DEFAULT_LOADER`.++ Pass `config`, the kernel's effective merged config, to honor a value the+ session or a WebAssembly export provides. Otherwise the on-disk user and+ project config is read.+ """+ if config is None:+ from marimo._config.manager import get_default_config_manager++ config = get_default_config_manager(+ current_path=current_path+ ).get_config()+ return normalize_loader_key((config.get("cache") or {}).get("loader"))++__all__ = [+ "DEFAULT_LOADER","PERSISTENT_LOADERS","BasePersistenceLoader","DualLoader",@@ -66,5 +166,9 @@ __all__ = ["WasmLazyLoader","dump_cache_manifests","flush_active_caches",+ "get_loader_key",+ "get_persistent_loader",+ "normalize_loader_key",+ "persistent_loader_names","resolve_loader",]diff --git a/marimo/_save/loaders/lazy.py b/marimo/_save/loaders/lazy.pyindex 155876c19f..e964e8e778 100644--- a/marimo/_save/loaders/lazy.py+++ b/marimo/_save/loaders/lazy.py@@ -8,6 +8,7 @@ import mathimport pickleimport queueimport threading+from contextlib import contextmanagerfrom enum import Enum, autofrom pathlib import Path, PurePosixPathfrom typing import TYPE_CHECKING, Any@@ -41,10 +42,16 @@ from marimo._save.signing import (normalize_fingerprints,normalize_verification,)-from marimo._save.stores import DEFAULT_STORE, FileStore, Store+from marimo._save.stores import (+ DEFAULT_STORE,+ FileStore,+ Store,+ TieredStore,+)if TYPE_CHECKING:from collections.abc import Callable, Iterable, Iterator+ from contextlib import AbstractContextManagerfrom marimo._save.stubs import (ClassStub,FunctionStub,@@ -124,30 +131,16 @@ def _is_local_file_store(store: Store) -> bool:`FileStore`, so unwrap to the inner store. Shared/remote backends(Redis, REST) and the WASM HTTP store (`DictStore` inner) return`False` — their entries can't be verified by a key only this machine- holds.+ holds. A `TieredStore` is local when every tier is, since its writes+ land only on this machine."""if isinstance(store, LazyStore):- return isinstance(store._inner, FileStore)+ return _is_local_file_store(store._inner)+ if isinstance(store, TieredStore):+ return all(_is_local_file_store(tier) for tier in store.stores)return isinstance(store, FileStore)-def _is_wasm_same_origin_store(store: Store) -> bool:- """True when a store's blobs share the notebook's own origin."""- # NB. WebAssembly blobs are fetched from the notebook location, so swapping- # them requires control of the notebook code served from that origin.- # Verification adds nothing there, which is why it is the only store- # exempted while signing is available.- return isinstance(store, WasmLazyStore)---def _is_trusted_origin_store(store: Store) -> bool:- """True when a store's bytes are reachable only by controlling the code."""- # NB. a local cache directory sits behind the same filesystem access as the- # notebook file. Shared and remote stores are what signing protects, so- # they never qualify.- return _is_wasm_same_origin_store(store) or _is_local_file_store(store)--def _verify_signed_blob(key: str,data: bytes,@@ -433,15 +426,11 @@ def dump_cache_manifests(manifest_name: str) -> None:keys = LazyLoader.export_keys()seen: set[Path] = set()for loader in loaders:- store = loader.store- inner = store._inner if isinstance(store, LazyStore) else None- if not isinstance(inner, FileStore):- continue- cache_dir = inner.save_path- if cache_dir in seen:- continue- seen.add(cache_dir)- _write_export_manifest(cache_dir, manifest_name, keys)+ for cache_dir in loader.store.local_dirs():+ if cache_dir in seen:+ continue+ seen.add(cache_dir)+ _write_export_manifest(cache_dir, manifest_name, keys)def _write_export_manifest(@@ -612,7 +601,7 @@ class WasmLazyStore(LazyStore):class LazyLoader(BasePersistenceLoader):- _store_cls: type[Store] = LazyStore+ _store_cls: type[LazyStore] = LazyStoredef __init__(self,@@ -644,15 +633,16 @@ class LazyLoader(BasePersistenceLoader):machine-local key in `marimo_state_dir()/cache_signing_key.pem`is loaded or generated for local file stores only — shared/remotestores use only an explicitly configured key, since an auto key- is unverifiable elsewhere. Resolves to `None` (unsigned) when- the `cryptography` package is not installed.+ is unverifiable elsewhere. Resolves to `None` when the+ `cryptography` package is not installed, so verified caching+ remains unavailable.trusted_signers: Fingerprint strings (`"SHA256:<base64>"` from:func:`~marimo._save.signing.fingerprint`) that this loadertrusts, in addition to its own signer (always trusted for itsown writes). An entry verifies when it is signed directly by atrusted fingerprint's key. Padded or urlsafe fingerprints arenormalized to canonical form.- verification: Posture — `"off"`, `"on"` (default), or `"strict"`.+ verification: Posture — `"off"` (default), `"on"`, or `"strict"`.`off` neither signs nor verifies (legacy opt-out). `on` signson write and, on read, serves only entries that verify againsta trusted key; an unverifiable entry misses and is recomputed@@ -663,9 +653,8 @@ class LazyLoader(BasePersistenceLoader):capability exists (no `cryptography`, or neither a signer nor`trusted_signers`), `on` keeps verifying — every read missesand every write is skipped, with a one-time warning — rather- than serving unsigned data. Two stores are exempt: WebAssembly- blobs served from the notebook's own origin, and, when- `cryptography` is not installed at all, a local file store.+ than serving unsigned data. This also applies to local and+ same-origin WebAssembly stores."""state = _cache_state()# An unset arg falls back to the session's config-derived policy (trust@@ -700,12 +689,24 @@ class LazyLoader(BasePersistenceLoader):)loaders = state.active_lazy_loadersif store is None:- # Reuse the store across recreations of a named loader (State GC,- # partial reconstruction) so cached data survives.- prev = loaders.get(name)- store = prev.store if prev is not None else self._store_cls()+ ctx = safe_get_context()+ if (+ verification == "off"+ and ctx is not None+ and ctx.cache.store_from_untrusted_origin+ ):+ store = self._store_cls()+ else:+ # Reuse the store across recreations of a named loader (State GC,+ # partial reconstruction) so cached data survives.+ prev = loaders.get(name)+ store = (+ prev.store if prev is not None else self._default_store()+ )super().__init__(name, "jsonl", store)self._pending: list[threading.Thread] = []+ self._pending_writes: dict[str, threading.Event] = {}+ self._pending_write_lock = threading.Lock()self._trusted_fingerprints = normalize_fingerprints(trusted_signers)self._verification = verificationself._degrade_warned = False@@ -725,6 +726,31 @@ class LazyLoader(BasePersistenceLoader):self._effective_verification()loaders[name] = self+ def _default_store(self) -> Store:+ """The session's configured `cache.store`, wrapped for key tracking.++ Every other persistent loader reads `get_context().cache.store`, so a+ configured tiered or remote store must reach the default lazy loader+ too. Outside a kernel, fall back to the plain file-backed store.+ """+ ctx = safe_get_context()+ if ctx is None:+ return self._store_cls()+ configured = ctx.cache.store+ if isinstance(configured, LazyStore):+ return configured+ return self._store_cls(configured)++ def _clearable_root(self) -> Path | None:+ store = self.store+ while isinstance(store, LazyStore):+ store = store._inner+ return store.save_path if isinstance(store, FileStore) else None++ def clear(self) -> None:+ self.flush()+ super().clear()+def _resolve_unset_signer(self) -> CacheSigner | None:"""Auto-resolve the signer for an unset value, matching __init__.@@ -750,15 +776,17 @@ class LazyLoader(BasePersistenceLoader):# window exists, so full resolution including env is safe.return _get_default_signer(auto_generate=auto_generate)- def _effective_verification(self) -> str:- """Resolve the verification posture after capability checks.+ def _can_verify(self) -> bool:+ from marimo._dependencies.dependencies import DependencyManager- With nothing to verify with, `strict` raises. `on` keeps verifying —- every read misses, every write is skipped — instead of serving unsigned- bytes, except for the stores named at each branch below. Recomputed each- call (not cached) so a `setattr` reconfigure that applies kwargs in- caller order is always honored.- """+ return DependencyManager.cryptography.has() and bool(+ self.signer is not None or self._trusted_fingerprints+ )++ def _effective_verification(self) -> str:+ """Check verification capability without weakening the requested policy."""+ # Recompute on each call so reconfiguration through setattr honors+ # the current signer and verification settings in any caller order.if self._verification == "off":return "off"@@ -768,17 +796,12 @@ class LazyLoader(BasePersistenceLoader):if self._verification == "strict":raise ValueError("verification='strict' cache signing requires the "- "'cryptography' "- "package, which is not installed."+ "'cryptography' package, which is not installed.")- # NB. nothing in this install can verify, so keeping `on` turns- # off the cache outright. Degrade for a store whose bytes are- # already gated on control of the notebook code.- return self._degrade_or_keep_on(- "the 'cryptography' package is not installed",- degradable=_is_trusted_origin_store(self.store),+ self._warn_no_trust_anchor(+ "the 'cryptography' package is not installed")- if self.signer is None and not self._trusted_fingerprints:+ elif self.signer is None and not self._trusted_fingerprints:if self._verification == "strict":raise ValueError("verification='strict' requires a signer or trusted_signers to "@@ -786,33 +809,11 @@ class LazyLoader(BasePersistenceLoader):"signer=CacheSigner.from_public_key_pem(...) or ""trusted_signers={fingerprint, ...}.")- # NB. signing works here, and a local file store auto-mints its- # own key, so reaching this branch means the caller asked for- # verification and passed `signer=None`. A local cache directory- # travels with a cloned repository, so it keeps verifying.- return self._degrade_or_keep_on(- "no signer or trusted_signers is set",- degradable=_is_wasm_same_origin_store(self.store),- )+ # A local directory can arrive with a cloned repository, and a+ # same-origin store does not establish trust in its signing key.+ self._warn_no_trust_anchor("no signer or trusted_signers is set")return self._verification- def _degrade_or_keep_on(self, reason: str, *, degradable: bool) -> str:- if degradable:- self._warn_degraded(reason)- return "off"- self._warn_no_trust_anchor(reason)- return "on"-- def _warn_degraded(self, reason: str) -> None:- if not self._degrade_warned:- self._degrade_warned = True- LOGGER.warning(- "LazyLoader verification=%r degraded to 'off' because %s; cache "- "entries are neither signed nor verified.",- self._verification,- reason,- )-def _warn_no_trust_anchor(self, reason: str) -> None:"""One-time warning: the loader currently cannot verify anything.@@ -891,6 +892,8 @@ class LazyLoader(BasePersistenceLoader):for t in self._pending:t.join()self._pending.clear()+ with self._pending_write_lock:+ self._pending_writes.clear()@classmethoddef flush_all(cls) -> None:@@ -917,6 +920,41 @@ class LazyLoader(BasePersistenceLoader):"""Loaders the current session has created (for flush/export)."""return list(_cache_state().active_lazy_loaders.values())+ def _write_context(+ self, manifest_key: str+ ) -> AbstractContextManager[None]:+ """Register a write and order it after earlier writes to the same key."""+ done = threading.Event()+ with self._pending_write_lock:+ previous = self._pending_writes.get(manifest_key)+ self._pending_writes[manifest_key] = done++ @contextmanager+ def write() -> Iterator[None]:+ try:+ if previous is not None:+ previous.wait()+ yield+ finally:+ done.set()++ return write()++ def _await_write(self, manifest_key: str) -> None:+ """Wait for this loader's pending write of the manifest."""+ with self._pending_write_lock:+ done = self._pending_writes.get(manifest_key)+ if done is None:+ return+ done.wait()+ with self._pending_write_lock:+ if self._pending_writes.get(manifest_key) is done:+ del self._pending_writes[manifest_key]++ def cache_hit(self, key: HashKey) -> bool:+ self._await_write(str(self.build_path(key)))+ return super().cache_hit(key)+def mark_stale(self, manifest_key: str) -> None:"""Force this manifest to miss for the rest of the session."""_cache_state().stale_keys.add(manifest_key)@@ -931,10 +969,13 @@ class LazyLoader(BasePersistenceLoader):# configuration surfaces as a ValueError rather than being swallowed# as a generic miss by the except clause below.verification = self._effective_verification()+ if verification != "off" and not self._can_verify():+ return Nonemanifest_key = str(self.build_path(key))# Invalidated for re-execution this session.if manifest_key in _cache_state().stale_keys:return None+ self._await_write(manifest_key)blob: bytes | None = Nonetry:blob = self.store.get(manifest_key)@@ -1024,6 +1065,10 @@ class LazyLoader(BasePersistenceLoader):"""if verification == "off":return None+ if not self._can_verify():+ raise CacheSignatureError(+ "Cache verification capability is unavailable."+ )sig = cache_data.meta.signatureif sig is None:@@ -1403,7 +1448,10 @@ class LazyLoader(BasePersistenceLoader):# Sign only when verifying: 'off' writes unsigned (legacy), and a# signer without a private key can't sign at all.signing = (- verification != "off" and signer is not None and signer.can_sign+ verification != "off"+ and self._can_verify()+ and signer is not None+ and signer.can_sign)if verification != "off" and not signing:@@ -1532,55 +1580,60 @@ class LazyLoader(BasePersistenceLoader):return Falsereturn True+ write_context = self._write_context(manifest_key)+def _serialize_and_write() -> None:- """Serialize and write all blobs + manifest in background."""- try:- if return_ref:- _put_or_mark_unserializable(- return_ref,- return_value,- return_loader,- [return_item],- "return",- )- if ui_vars:- ui_key = (path / "ui.pickle").as_posix()- ui_ok = _put_or_mark_unserializable(- ui_key,- ui_vars,- "pickle",- [defs_dict[v] for v in ui_defs_list],- "ui",- )- if not ui_ok:- # UI defs restore via `ui_defs` → `ui.pickle`,- # bypassing the per-Item marks. Drop them from- # `ui_defs` so restore routes through the now-marked- # Items instead, and clear any stale blob lingering- # from a prior run at this same hash path (which would- # otherwise load as a phantom hit).- ui_defs_list.clear()- try:- store.clear(ui_key)- except Exception:- LOGGER.warning(- "Failed to clear stale ui blob %s", ui_key- )- for loader, vars_dict in format_vars.items():- for var, obj in vars_dict.items():+ """Serialize and write blobs, then the manifest."""+ with write_context:+ try:+ if return_ref:_put_or_mark_unserializable(- (path / f"{var}.{loader}").as_posix(),- obj,- loader,- [defs_dict[var]],- var,+ return_ref,+ return_value,+ return_loader,+ [return_item],+ "return",)- # Manifest last — readers check for it to detect complete- # writes, and it now carries any unserializable marks set above- # plus (when signing) the signed blob hashes and signature.- store.put(manifest_key, _encode_manifest())- except Exception:- LOGGER.exception("Failed to write cache blobs for %s", path)+ if ui_vars:+ ui_key = (path / "ui.pickle").as_posix()+ ui_ok = _put_or_mark_unserializable(+ ui_key,+ ui_vars,+ "pickle",+ [defs_dict[v] for v in ui_defs_list],+ "ui",+ )+ if not ui_ok:+ # UI defs restore via `ui_defs` → `ui.pickle`,+ # bypassing the per-Item marks. Drop them from+ # `ui_defs` so restore routes through the now-marked+ # Items instead, and clear any stale blob lingering+ # from a prior run at this same hash path (which would+ # otherwise load as a phantom hit).+ ui_defs_list.clear()+ try:+ store.clear(ui_key)+ except Exception:+ LOGGER.warning(+ "Failed to clear stale ui blob %s", ui_key+ )+ for loader, vars_dict in format_vars.items():+ for var, obj in vars_dict.items():+ _put_or_mark_unserializable(+ (path / f"{var}.{loader}").as_posix(),+ obj,+ loader,+ [defs_dict[var]],+ var,+ )+ # Manifest last — readers check for it to detect complete+ # writes, and it now carries any unserializable marks set above+ # plus (when signing) the signed blob hashes and signature.+ store.put(manifest_key, _encode_manifest())+ except Exception:+ LOGGER.exception(+ "Failed to write cache blobs for %s", path+ )self._dispatch_write(_serialize_and_write)return True@@ -1603,6 +1656,11 @@ class WasmLazyLoader(LazyLoader):_store_cls = WasmLazyStore+ def _default_store(self) -> Store:+ # The browser session's shared dict store, not a configured+ # `cache.store`: WASM blobs are served from the notebook's own origin.+ return self._store_cls()+def _read_blobs(self,unique_keys: set[str],diff --git a/marimo/_save/loaders/pickle.py b/marimo/_save/loaders/pickle.pyindex 654f4b297e..5503e08ced 100644--- a/marimo/_save/loaders/pickle.py+++ b/marimo/_save/loaders/pickle.py@@ -29,8 +29,10 @@ class PickleLoader(BasePersistenceLoader):LOGGER.warning("Ignoring cache.store from an untrusted config layer for the ""unsigned pickle loader (it would feed pickle.loads); using the "- "default store. Use method='lazy' to verify a shared store, or "- "configure the store in user config."+ "default store. Use method='lazy' with cache.verification set "+ "to 'on' or 'strict' and a signing key or "+ "signing.trusted_signers to verify a shared store, or configure "+ "the store in user config.")@staticmethoddiff --git a/marimo/_save/save.py b/marimo/_save/save.pyindex 1e4348502f..2120b98b51 100644--- a/marimo/_save/save.py+++ b/marimo/_save/save.py@@ -23,6 +23,7 @@ from typing import (Any,Concatenate,Generic,+ Literal,ParamSpec,TypeVar,cast,@@ -57,13 +58,13 @@ from marimo._save.hash import (content_cache_attempt_from_base,)from marimo._save.loaders import (+ DEFAULT_LOADER,PERSISTENT_LOADERS,Loader,- LoaderKey,LoaderPartial,LoaderType,MemoryLoader,- resolve_loader,+ get_persistent_loader,)from marimo._save.stores.file import FileStorefrom marimo._save.toplevel import get_cell_id_from_scope, graph_from_scope@@ -386,6 +387,7 @@ class _cache_call(CacheContext, Generic[P, R]):scope = {k: v for k, v in scope.items() if k in attempt.stateful_refs}attempt.update(scope, meta={"return": response, "runtime": runtime})if self.loader.save_cache(attempt):+ self.loader.flush()self._record(attempt)def _record(self, attempt: Cache) -> None:@@ -880,7 +882,10 @@ class _cache_context(SkipContext, CacheContext):saved = Falsetry:saved = self.loader.save_cache(self._cache)+ if saved:+ self.loader.flush()except Exception as e:+ saved = Falsesys.stderr.write("An exception was raised when attempting to cache this code ""block with the following message:\n"@@ -1249,11 +1254,17 @@ def lru_cache( # type: ignore[misc])+def _default_loader_key() -> str:+ """`cache.loader` from the session config, else `DEFAULT_LOADER`."""+ ctx = safe_get_context()+ return ctx.cache.loader if ctx is not None else DEFAULT_LOADER++@overloaddef persistent_cache(fn: Callable[P, Coroutine[Any, Any, R]],save_path: str | None = None,- method: LoaderKey = "pickle",+ method: Literal["pickle", "json", "lazy"] | None = None,pin_modules: bool = False,) -> _cache_call_async[P, R]: ...@@ -1262,7 +1273,7 @@ def persistent_cache(def persistent_cache(fn: Callable[P, R],save_path: str | None = None,- method: LoaderKey = "pickle",+ method: Literal["pickle", "json", "lazy"] | None = None,pin_modules: bool = False,) -> _cache_call[P, R]: ...@@ -1271,7 +1282,7 @@ def persistent_cache(def persistent_cache(fn: None = None,save_path: str | None = None,- method: LoaderKey = "pickle",+ method: Literal["pickle", "json", "lazy"] | None = None,pin_modules: bool = False,) -> _cache_call[Any, Any]: ...@@ -1280,7 +1291,7 @@ def persistent_cache(def persistent_cache(name: str,save_path: str | None = None,- method: LoaderKey = "pickle",+ method: Literal["pickle", "json", "lazy"] | None = None,pin_modules: bool = False,) -> _cache_context: ...@@ -1288,7 +1299,7 @@ def persistent_cache(def persistent_cache( # type: ignore[misc]name: str | Callable[..., Any] | None = None,save_path: str | None = None,- method: LoaderKey = "pickle",+ method: Literal["pickle", "json", "lazy"] | None = None,store: Store | None = None,fn: Callable[..., Any] | None = None,*args: Any,@@ -1358,8 +1369,8 @@ def persistent_cache( # type: ignore[misc]invalidate the cache, change the name.save_path: the folder in which to save the cache, defaults to`__marimo__/cache` in the directory of the notebook file- method: the serialization method to use, current options are "json",- and "pickle" (default).+ method: "json", "pickle", or "lazy", or `None` to use the configured+ loader ("lazy" by default).store: optional store.fn: the wrapped function if no settings are passed.*args: positional arguments passed to `cache()`@@ -1376,11 +1387,15 @@ def persistent_cache( # type: ignore[misc]"loader is not a valid argument ""for persistent_cache, use mo.cache instead.")- if method not in PERSISTENT_LOADERS:+ if method is not None and method not in PERSISTENT_LOADERS:raise ValueError(- f"Invalid method {method}, expected one of "- f"{PERSISTENT_LOADERS.keys()}"+ f"Invalid method {method!r}, expected one of "+ f"{list(PERSISTENT_LOADERS)}. "+ "Select custom loaders through cache.loader in configuration.")+ loader_type = get_persistent_loader(+ _default_loader_key() if method is None else method+ )if save_path is not None and store is not None:raise ValueError("save_path and store cannot both be provided, "@@ -1396,7 +1411,7 @@ def persistent_cache( # type: ignore[misc]if store is not None:partial_args["store"] = store- loader = resolve_loader(PERSISTENT_LOADERS[method]).partial(**partial_args)+ loader = loader_type.partial(**partial_args)# Injection hook for testingif "_loader" in kwargs:loader = kwargs.pop("_loader")diff --git a/marimo/_save/signing.py b/marimo/_save/signing.pyindex cf80853367..5353563445 100644--- a/marimo/_save/signing.py+++ b/marimo/_save/signing.py@@ -16,13 +16,16 @@ if TYPE_CHECKING:from collections.abc import Iterable# Verification posture, shared by the config resolver (`signing_policy`) and-# the loader (`loaders.lazy`). `off`: no signing or verification (legacy-# opt-out). `on` (default): sign on write, and on read serve only entries that-# verify against a trusted key — unverifiable entries miss and recompute-# (fail-safe). `strict`: like `on`, but an unverifiable entry raises-# (fail-closed).+# the loader (`loaders.lazy`). `off` (default): no signing or verification.+# `on`: sign on write, and on read serve only entries that verify against a+# trusted key — unverifiable entries miss and recompute (fail-safe). `strict`:+# like `on`, but an unverifiable entry raises (fail-closed).VALID_VERIFICATIONS = ("off", "on", "strict")-DEFAULT_VERIFICATION = "on"+DEFAULT_VERIFICATION = "off"+# NB. a misspelled config value falls back to `on`, not to the default: a user+# who wrote a verification setting meant to verify, and a typo must not turn+# verification off silently.+INVALID_VERIFICATION_FALLBACK = "on"class CacheSignatureError(Exception):@@ -42,11 +45,10 @@ def _sha256hex(data: bytes) -> str:def normalize_verification(raw: Any, *, strict: bool = False) -> str:"""Validate a verification posture.- With `strict=False` (config files) an unrecognized value warns and degrades- to the default rather than breaking the session; with `strict=True` (a- caller-supplied kwarg) it raises. The default is fail-safe — unverifiable- entries recompute — so the degrade path never widens what gets- deserialized.+ With `strict=False` (config files) an unrecognized value warns and falls+ back to `on` rather than breaking the session; with `strict=True` (a+ caller-supplied kwarg) it raises. `on` is fail-safe — unverifiable entries+ recompute — so the fallback never widens what gets deserialized."""if isinstance(raw, str) and raw in VALID_VERIFICATIONS:return raw@@ -61,9 +63,9 @@ def normalize_verification(raw: Any, *, strict: bool = False) -> str:"Invalid cache verification %r; expected one of %s. Falling back to %r.",raw,expected,- DEFAULT_VERIFICATION,+ INVALID_VERIFICATION_FALLBACK,)- return DEFAULT_VERIFICATION+ return INVALID_VERIFICATION_FALLBACKclass CacheSigner:@@ -467,16 +469,15 @@ def _get_machine_signer(return signerexcept Exception:logging.getLogger("marimo").warning(- "Failed to load cache signing key from %s; "- "generating a new one.",+ "Failed to load cache signing key from %s. "+ "Restore the key or remove the file to generate a new identity.",key_file,)+ return None- # Auto-generate and persist atomically (write to temp, rename).- # os.replace is atomic on POSIX; if two processes race, the last rename- # wins. We then load the key that actually landed on disk (rather than the- # one we generated), so a process that lost the race still uses the winning- # key this session and can verify caches written by the winner.+ # Publish a complete key without replacing an identity another process+ # already uses. Atomic replacement alone lets concurrent first-time+ # writers adopt different keys before the last replacement wins.import tempfiletry:@@ -485,34 +486,30 @@ def _get_machine_signer(fd, tmp = tempfile.mkstemp(dir=key_file.parent, prefix=".cache_key_", suffix=".tmp")- closed = Falsetry:- os.write(fd, private_pem.encode())- os.close(fd)- closed = True+ with os.fdopen(fd, "w") as file:+ file.write(private_pem)os.chmod(tmp, stat.S_IRUSR | stat.S_IWUSR)- os.replace(tmp, str(key_file))- except BaseException:- if not closed:- os.close(fd)+ try:+ os.link(tmp, key_file)+ except FileExistsError:+ # The winner published its complete key before creating the+ # link, so every caller can now adopt the same identity.+ pass+ else:+ logging.getLogger("marimo").info(+ "Generated cache signing key: %s", key_file+ )+ finally:try:os.unlink(tmp)except OSError:pass- raise- logging.getLogger("marimo").info(- "Generated cache signing key: %s", key_file- )- # Adopt whatever key won the rename (see note above); fall back to the- # in-memory key if the file is unreadable for any reason.- try:- return CacheSigner.from_private_key_pem(key_file.read_text())- except Exception:- return CacheSigner.from_private_key_pem(private_pem)+ return CacheSigner.from_private_key_pem(key_file.read_text())except Exception:logging.getLogger("marimo").warning("Could not persist cache signing key to %s; "- "cache entries will be unsigned for this session.",+ "verified cache writes will be skipped for this session.",key_file,)return Nonediff --git a/packages/openapi/api.yaml b/packages/openapi/api.yamlindex 297460da96..4bae5cfb49 100644--- a/packages/openapi/api.yaml+++ b/packages/openapi/api.yaml@@ -370,8 +370,15 @@ components:CacheConfig:description: "Configuration for caching.\n\n `verification` is the signature-checking\\ posture; `store` is the backing\n store, or a list of stores composed\- \ into a `TieredStore`."+ \ into a `TieredStore`; `loader` is the\n default `method` for `mo.persistent_cache`."properties:+ loader:+ anyOf:+ - enum:+ - json+ - lazy+ - pickle+ - type: stringstore:anyOf:- items:diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.tsindex 0e618b1c42..dde8bea74a 100644--- a/packages/openapi/src/api.ts+++ b/packages/openapi/src/api.ts@@ -4062,9 +4062,11 @@ export interface components {* @description Configuration for caching.** `verification` is the signature-checking posture; `store` is the backing- * store, or a list of stores composed into a `TieredStore`.+ * store, or a list of stores composed into a `TieredStore`; `loader` is the+ * default `method` for `mo.persistent_cache`.*/CacheConfig: {+ loader?: ("json" | "lazy" | "pickle") | string;store?:| components["schemas"]["StoreConfig"][]| components["schemas"]["StoreConfig"];diff --git a/tests/_config/test_config_schema.py b/tests/_config/test_config_schema.pyindex b63a11e84f..a9a9bfc106 100644--- a/tests/_config/test_config_schema.py+++ b/tests/_config/test_config_schema.py@@ -356,6 +356,13 @@ def test_metadata_describes_and_hides_secrets() -> None:)[email protected]("loader", ["pickle", "json", "lazy", "custom"])+def test_cache_loader_accepts_builtin_and_custom_names(loader: str) -> None:+ config = config_schema.CacheConfig.from_dict({"loader": loader})+ assert config.loader == loader+ assert config.to_dict() == {"loader": loader}++def test_cache_store_union_uses_struct_conversion() -> None:for store in ({"type": "file", "args": {"path": "/cache"}},diff --git a/tests/_config/test_manager.py b/tests/_config/test_manager.pyindex 79e6c68b84..9d7b6236ae 100644--- a/tests/_config/test_manager.py+++ b/tests/_config/test_manager.py@@ -1432,8 +1432,9 @@ def test_script_config_manager_strips_signing_and_verification(assert "cache" not in config[email protected]("loader", ["pickle", "custom"])def test_project_config_manager_strips_signing_and_verification(- tmp_path: Path,+ tmp_path: Path, loader: str) -> None:"""A cloned repo's pyproject.toml is untrusted origin for signing config."""pyproject_path = tmp_path / "pyproject.toml"@@ -1446,6 +1447,7 @@ def test_project_config_manager_strips_signing_and_verification([tool.marimo.cache]verification = "off"+ loader = "{loader}"[tool.marimo.cache.store]type = "file"@@ -1455,8 +1457,6 @@ def test_project_config_manager_strips_signing_and_verification(config = PyprojectConfigReader(str(pyproject_path)).read()assert "signing" not in config- # `cache.store` is not a trust anchor, so a project may still choose one;- # the verifying loaders check its bytes before unpickling.assert config.get("cache") == {"store": {"type": "file"}}@@ -1487,8 +1487,9 @@ def test_effective_config_anchors_trust_in_user_layer_only(assert user_fp in trusted[email protected]("loader", ["pickle", "custom"])def test_workspace_marimo_toml_strips_signing_and_verification(- tmp_path: Path, monkeypatch: pytest.MonkeyPatch+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, loader: str) -> None:"""A `.marimo.toml` found by walking up from the cwd is project-origin.@@ -1507,6 +1508,7 @@ def test_workspace_marimo_toml_strips_signing_and_verification([cache]verification = "off"+ loader = "{loader}"[cache.store]type = "file"@@ -1528,6 +1530,7 @@ def test_workspace_marimo_toml_strips_signing_and_verification(assert "signing" not in configassert config.get("cache", {}).get("verification") is None+ assert config.get("cache", {}).get("loader") is None# The store goes too, unlike the project and script layers. A store set# here would load as the user layer, so `cache_store_is_untrusted` could# not tell it apart from one the operator chose, and the unsigned pickle@@ -1548,6 +1551,7 @@ def test_trusted_user_config_location_anchors_trust([cache]verification = "strict"+ loader = "pickle""""))@@ -1566,3 +1570,4 @@ def test_trusted_user_config_location_anchors_trust(assert config["signing"]["trusted_signers"] == {_FAKE_FP: "me"}assert config["cache"]["verification"] == "strict"+ assert config["cache"]["loader"] == "pickle"diff --git a/tests/_runtime/test_cache_callbacks.py b/tests/_runtime/test_cache_callbacks.pyindex bdfa30b973..ae77a37d98 100644--- a/tests/_runtime/test_cache_callbacks.py+++ b/tests/_runtime/test_cache_callbacks.py@@ -167,11 +167,9 @@ async def test_get_cache_info_promises_a_shared_block_once(assert sent[0].disk_to_free == 100-async def test_get_cache_info_promises_nothing_a_lazy_cache_keeps(+async def test_get_cache_info_reports_lazy_manifest_bytes(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- # A lazy cache stores through a wrapper its loader cannot clear by path,- # so its bytes count as used but none of them as freeable.cache_dir = tmp_path / "__marimo__" / "cache"write_entry(cache_dir, "cell_cache", "P_ab12.jsonl", 100)write_entry(cache_dir, "cell_cache", "ab12/return.npy", 500)@@ -191,8 +189,9 @@ async def test_get_cache_info_promises_nothing_a_lazy_cache_keeps(await callbacks.get_cache_info(GetCacheInfoCommand())assert sent[0].disk_total == 600- assert sent[0].disk_to_free == 0- assert sent[-1].disk_total == 600+ assert sent[0].disk_to_free == 100+ assert sent[-1].disk_total == 500+ assert sent[-1].disk_to_free == 0async def test_get_cache_info_ignores_a_cache_without_a_loader(diff --git a/tests/_save/loaders/test_cache_export.py b/tests/_save/loaders/test_cache_export.pyindex 3fc6ddc9ef..fb90522536 100644--- a/tests/_save/loaders/test_cache_export.py+++ b/tests/_save/loaders/test_cache_export.py@@ -12,6 +12,8 @@ import jsonfrom typing import TYPE_CHECKINGfrom unittest.mock import MagicMock+import pytest+from marimo._runtime.callbacks.cache import (CacheCallbacks,cache_cells_enabled,@@ -23,8 +25,6 @@ from marimo._save.stores.file import FileStore, export_manifest_nameif TYPE_CHECKING:from pathlib import Path- import pytest-def _clear_loaders() -> None:_cache_state().active_lazy_loaders.clear()@@ -39,9 +39,23 @@ def test_export_manifest_name_is_per_notebook() -> None:assert name.endswith("-export.json")-def test_dump_cache_manifests_writes_keys(tmp_path: Path) -> None:[email protected]("tiered", [False, True])+def test_dump_cache_manifests_writes_keys(+ tmp_path: Path, tiered: bool+) -> None:+ from marimo._save.stores.dict_store import DictStore+ from marimo._save.stores.tiered import TieredStore+_clear_loaders()- store = LazyStore(FileStore(save_path=str(tmp_path)))+ directories = (+ [tmp_path / "first", tmp_path / "second"] if tiered else [tmp_path]+ )+ stores = [FileStore(save_path=str(directory)) for directory in directories]+ store = LazyStore(+ TieredStore([DictStore(), TieredStore(stores)])+ if tiered+ else stores[0]+ )LazyLoader("blk", store=store)store.put("blk/hash/a.pickle", b"x")store.put("blk/hash/return.pickle", b"y")@@ -50,12 +64,12 @@ def test_dump_cache_manifests_writes_keys(tmp_path: Path) -> None:finally:_clear_loaders()- manifest = tmp_path / export_manifest_name("nb.py")- assert manifest.exists()- assert set(json.loads(manifest.read_text())) == {- "blk/hash/a.pickle",- "blk/hash/return.pickle",- }+ for directory in directories:+ manifest = directory / export_manifest_name("nb.py")+ assert set(json.loads(manifest.read_text())) == {+ "blk/hash/a.pickle",+ "blk/hash/return.pickle",+ }def test_dump_cache_manifests_skips_non_file_stores(tmp_path: Path) -> None:diff --git a/tests/_save/loaders/test_lazy_signing.py b/tests/_save/loaders/test_lazy_signing.pyindex 0bc726cce9..92c68373aa 100644--- a/tests/_save/loaders/test_lazy_signing.py+++ b/tests/_save/loaders/test_lazy_signing.py@@ -3,10 +3,13 @@from __future__ import annotations+import loggingimport pickleimport tempfile+import threading+from concurrent.futures import ThreadPoolExecutor, TimeoutErrorfrom pathlib import Path-from typing import Any+from typing import TYPE_CHECKING, Anyimport msgspecimport pytest@@ -32,6 +35,9 @@ from marimo._save.stubs.lazy_stub import ()from tests._save.store.mocks import MockStore+if TYPE_CHECKING:+ from collections.abc import Iterator+# Module-level class so pickle can resolve it.class _Point:@@ -86,6 +92,9 @@ class _FileStoreLoaderTest:self.temp_dir.cleanup()def _loader(self, **kwargs: Any) -> LazyLoader:+ # These tests exercise the verifying postures; `on` unless a test+ # says otherwise.+ kwargs.setdefault("verification", "on")return LazyLoader("test", store=self.store, **kwargs)@@ -110,7 +119,7 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):assert loader.save_cache(cache)loader.flush()- reader = self._loader(signer=verifier)+ reader = self._loader(signer=verifier, verification="on")loaded = reader.load_cache(key("reconfig_hash"))assert loaded is not Noneassert loaded.defs["v"] == 99@@ -160,12 +169,12 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):trusts its own key's fingerprint, so no trusted_signers needed)."""signer, verifier = _keypair()- writer = self._loader(signer=signer)+ writer = self._loader(signer=signer, verification="on")cache = _simple_cache(hash_val="signed_hash", x=99, label="signed")assert writer.save_cache(cache)writer.flush()- reader = self._loader(signer=verifier)+ reader = self._loader(signer=verifier, verification="on")loaded = reader.load_cache(key("signed_hash"))assert loaded is not Noneassert loaded.defs["x"] == 99@@ -176,12 +185,12 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):signer, verifier = _keypair()pt = _Point(3, 4)- writer = self._loader(signer=signer)+ writer = self._loader(signer=signer, verification="on")cache = _simple_cache(hash_val="pt_hash", pt=pt)assert writer.save_cache(cache)writer.flush()- reader = self._loader(signer=verifier)+ reader = self._loader(signer=verifier, verification="on")loaded = reader.load_cache(key("pt_hash"))assert loaded is not Noneresult = loaded.defs["pt"]@@ -191,7 +200,7 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):def test_self_verify_with_private_key(self) -> None:"""A loader with a private-key signer can also verify its own entries."""signer, _ = _keypair()- loader = self._loader(signer=signer)+ loader = self._loader(signer=signer, verification="on")cache = _simple_cache(hash_val="self_verify", val=7)assert loader.save_cache(cache)@@ -205,12 +214,14 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):"""A reader trusts a third party's direct signature by adding thewriter's fingerprint to trusted_signers (reader has no own key)."""signer, verifier = _keypair()- writer = self._loader(signer=signer)+ writer = self._loader(signer=signer, verification="on")writer.save_cache(_simple_cache(hash_val="third_party", n=5))writer.flush()reader = self._loader(- signer=None, trusted_signers={verifier.fingerprint()}+ signer=None,+ trusted_signers={verifier.fingerprint()},+ verification="on",)loaded = reader.load_cache(key("third_party"))assert loaded is not None@@ -221,12 +232,16 @@ class TestSignedRoundTrip(_FileStoreLoaderTest):signer, verifier = _keypair()store = MockStore()- writer = LazyLoader("ns", store=store, signer=signer)+ writer = LazyLoader(+ "ns", store=store, signer=signer, verification="on"+ )cache = _simple_cache(hash_val="mock_hash", n=123)assert writer.save_cache(cache)writer.flush()- reader = LazyLoader("ns", store=store, signer=verifier)+ reader = LazyLoader(+ "ns", store=store, signer=verifier, verification="on"+ )loaded = reader.load_cache(key("mock_hash"))assert loaded is not Noneassert loaded.defs["n"] == 123@@ -255,11 +270,11 @@ class TestUnsignedEntries(_FileStoreLoaderTest):assert loaded.defs["z"] == 55def test_unsigned_misses_in_verify_mode(self) -> None:- """verify mode (default): an unsigned entry is unverifiable, so it+ """verify mode: an unsigned entry is unverifiable, so itmisses (fail-safe) rather than being served."""self._write_unsigned()_, verifier = _keypair()- loader = self._loader(signer=verifier) # verification="on" default+ loader = self._loader(signer=verifier, verification="on")assert loader.load_cache(key("unsigned_hash")) is Nonedef test_unsigned_rejected_in_strict_mode(self) -> None:@@ -276,7 +291,9 @@ class TestUnsignedEntries(_FileStoreLoaderTest):from unittest.mock import patch_, verifier = _keypair()- loader = self._loader(signer=verifier) # verify mode, no private key+ loader = self._loader(+ signer=verifier, verification="on"+ ) # verify mode, no private keycache = _simple_cache(hash_val="warn_hash", z=1)with patch("marimo._save.loaders.lazy.LOGGER") as mock_log:assert loader.save_cache(cache) is False@@ -299,7 +316,9 @@ class TestTampering(_FileStoreLoaderTest):if not defs:defs = {"v": 1}signer, _ = _keypair()- loader = LazyLoader("test", store=self.store, signer=signer)+ loader = LazyLoader(+ "test", store=self.store, signer=signer, verification="on"+ )cache = _simple_cache(hash_val=hash_val, **defs)loader.save_cache(cache)loader.flush()@@ -308,6 +327,7 @@ class TestTampering(_FileStoreLoaderTest):def _read_loader(self, signer: CacheSigner, **kwargs: Any) -> LazyLoader:# Build a verifier from the same public keyverifier = CacheSigner.from_public_key_pem(signer.public_key_pem())+ kwargs.setdefault("verification", "on")return LazyLoader("test", store=self.store, signer=verifier, **kwargs)def test_tampered_manifest_raises(self) -> None:@@ -393,7 +413,7 @@ class TestTampering(_FileStoreLoaderTest):)self.store.put(manifest_key, tampered)- # verify mode (default) — degrade to cache miss+ # verify mode — degrade to cache missreader = self._read_loader(signer)assert reader.load_cache(key("verify_tamper")) is None@@ -402,7 +422,9 @@ class TestTampering(_FileStoreLoaderTest):signer_a, _ = _keypair()_, verifier_b = _keypair()- writer = LazyLoader("test", store=self.store, signer=signer_a)+ writer = LazyLoader(+ "test", store=self.store, signer=signer_a, verification="on"+ )cache = _simple_cache(hash_val="wrong_key", val=1)writer.save_cache(cache)writer.flush()@@ -421,13 +443,17 @@ class TestTampering(_FileStoreLoaderTest):signer_a, _ = _keypair()_, verifier_b = _keypair()- writer = LazyLoader("test", store=self.store, signer=signer_a)+ writer = LazyLoader(+ "test", store=self.store, signer=signer_a, verification="on"+ )writer.save_cache(_simple_cache(hash_val="foreign_ok", val=7))writer.flush()- # verify mode (default) with an unrelated key B: the foreign signature+ # verify mode with an unrelated key B: the foreign signature# fails verification -> fail-safe cache miss.- reader = LazyLoader("test", store=self.store, signer=verifier_b)+ reader = LazyLoader(+ "test", store=self.store, signer=verifier_b, verification="on"+ )assert reader.load_cache(key("foreign_ok")) is Nonedef test_signed_manifest_missing_blob_hash_rejected(self) -> None:@@ -523,7 +549,9 @@ class TestBlobHashesInManifest(_FileStoreLoaderTest):def test_signed_manifest_has_blob_hashes_and_signature(self) -> None:signer, _ = _keypair()- loader = LazyLoader("test", store=self.store, signer=signer)+ loader = LazyLoader(+ "test", store=self.store, signer=signer, verification="on"+ )cache = _simple_cache(hash_val="with_hashes", obj={"a": 1})loader.save_cache(cache)loader.flush()@@ -543,7 +571,9 @@ class TestBlobHashesInManifest(_FileStoreLoaderTest):def test_blob_hashes_match_actual_blobs(self) -> None:signer, _ = _keypair()- loader = LazyLoader("test", store=self.store, signer=signer)+ loader = LazyLoader(+ "test", store=self.store, signer=signer, verification="on"+ )cache = _simple_cache(hash_val="verify_hash_content", n=42)loader.save_cache(cache)loader.flush()@@ -655,7 +685,9 @@ class TestVerificationAndCapabilityValidation(_FileStoreLoaderTest):w.flush()# Reader with verify but no signer and no trusted_signers must NOT# degrade to off on a local file store — that was the fail-open hole.- reader = LazyLoader("test", store=self.store, signer=None)+ reader = LazyLoader(+ "test", store=self.store, signer=None, verification="on"+ )assert reader._effective_verification() == "on"assert reader.load_cache(key("degrade")) is None@@ -663,6 +695,23 @@ class TestVerificationAndCapabilityValidation(_FileStoreLoaderTest):with pytest.raises(ValueError, match="Invalid cache verification"):LazyLoader("test", store=self.store, verification="paranoid")+ def test_invalid_config_verification_warns_and_falls_back_to_on(+ self, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """A misspelled config value warns and verifies rather than raising+ (a config file must not break the session) or silently going off."""+ from marimo import _loggers+ from marimo._save.signing_policy import SigningPolicy++ # The marimo logger does not propagate, so caplog needs it to.+ monkeypatch.setattr(_loggers.marimo_logger(), "propagate", True)+ with caplog.at_level(logging.WARNING):+ policy = SigningPolicy.from_config(+ {"cache": {"verification": "typo"}}+ )+ assert policy.verification == "on"+ assert "Invalid cache verification 'typo'" in caplog.text+def test_bare_string_trusted_signers_raises(self) -> None:"""A bare str would iterate into single characters — rejected."""with pytest.raises(TypeError, match="iterable of fingerprint"):@@ -670,15 +719,23 @@ class TestVerificationAndCapabilityValidation(_FileStoreLoaderTest):"test",store=self.store,trusted_signers="SHA256:abc", # type: ignore[arg-type]+ verification="on",)def test_malformed_fingerprint_raises(self) -> None:with pytest.raises(ValueError, match="Invalid trusted_signers"):- LazyLoader("test", store=self.store, trusted_signers={"not-a-fp"})+ LazyLoader(+ "test",+ store=self.store,+ trusted_signers={"not-a-fp"},+ verification="on",+ )def test_bad_signer_type_raises(self) -> None:with pytest.raises(TypeError, match="signer must be a CacheSigner"):- LazyLoader("test", store=self.store, signer="nope") # type: ignore[arg-type]+ LazyLoader(+ "test", store=self.store, signer="nope", verification="on"+ ) # type: ignore[arg-type]def test_no_cryptography_strict_raises(self) -> None:"""strict must fail closed when cryptography is unavailable."""@@ -698,13 +755,8 @@ class TestVerificationAndCapabilityValidation(_FileStoreLoaderTest):verification="strict",)- def test_no_cryptography_local_store_degrades_to_off(self) -> None:- """Without cryptography, a local file store degrades to `off`.-- Nothing in the install can verify, so keeping `on` turns the cache off- for every plain `pip install marimo`. A local cache directory is already- gated on filesystem access to the notebook, so it is served.- """+ def test_no_cryptography_local_store_misses(self) -> None:+ """Missing cryptography must not permit unsigned local entries."""from unittest import mock# Write an unsigned entry (off mode).@@ -720,11 +772,12 @@ class TestVerificationAndCapabilityValidation(_FileStoreLoaderTest):"cryptography.has",return_value=False,):- reader = LazyLoader("test", store=self.store, signer=signer)- assert reader._effective_verification() == "off"- cache = reader.load_cache(key("nocrypto"))- assert cache is not None- assert cache.defs["z"] == 8+ reader = LazyLoader(+ "test", store=self.store, signer=signer, verification="on"+ )+ assert reader._effective_verification() == "on"+ assert reader.load_cache(key("nocrypto")) is None+ assert not reader.save_cache(_simple_cache(hash_val="new", z=9))# ---------------------------------------------------------------------------@@ -746,6 +799,7 @@ class TestReviewFixes(_FileStoreLoaderTest):"test",store=self.store,trusted_signers={fingerprint(other_pub)},+ verification="on",)# Own signing key auto-resolved despite trusted_signers being set.assert loader.signer is not None@@ -765,7 +819,7 @@ class TestReviewFixes(_FileStoreLoaderTest):w.save_cache(_simple_cache(hash_val="remote_unsigned", z=3))w.flush()- reader = LazyLoader("ns", store=store, signer=None)+ reader = LazyLoader("ns", store=store, signer=None, verification="on")assert reader._effective_verification() == "on"assert reader.load_cache(key("remote_unsigned")) is None@@ -779,7 +833,9 @@ class TestReviewFixes(_FileStoreLoaderTest):)w.save_cache(_simple_cache(hash_val="local_unsigned", z=4))w.flush()- reader = LazyLoader("test", store=self.store, signer=None)+ reader = LazyLoader(+ "test", store=self.store, signer=None, verification="on"+ )assert reader._effective_verification() == "on"assert reader.load_cache(key("local_unsigned")) is None@@ -810,7 +866,9 @@ class TestReviewFixes(_FileStoreLoaderTest):matches fingerprint() output (rather than validating but nevermatching → permanent silent miss)."""signer, _ = _keypair()- writer = LazyLoader("test", store=self.store, signer=signer)+ writer = LazyLoader(+ "test", store=self.store, signer=signer, verification="on"+ )writer.save_cache(_simple_cache(hash_val="norm", n=5))writer.flush()@@ -825,6 +883,7 @@ class TestReviewFixes(_FileStoreLoaderTest):store=self.store,signer=None,trusted_signers={variant},+ verification="on",)assert reader.trusted_signers == {canonical}loaded = reader.load_cache(key("norm"))@@ -862,7 +921,9 @@ class TestReviewFixes(_FileStoreLoaderTest):"cryptography.has",return_value=False,):- reader = LazyLoader("ns", store=store, signer=None)+ reader = LazyLoader(+ "ns", store=store, signer=None, verification="on"+ )assert reader._effective_verification() == "on"assert reader.load_cache(key("nocrypto_remote")) is None@@ -992,11 +1053,8 @@ class TestReviewFixes(_FileStoreLoaderTest):# Still unresolved, so a later reconfigure to verify can resolve.assert isinstance(loader._signer, _Unset)- def test_wasm_store_on_degrades_to_off(self) -> None:- """The WASM HTTP store is same-origin as the notebook code, so a verify- loader with no key/anchor degrades to off (serves) rather than missing- every read — otherwise the bundled-cache restore feature this stack- ships is silently disabled in the browser."""+ def test_wasm_store_without_trust_misses(self) -> None:+ """Same-origin storage does not establish signer trust."""from marimo._save.loaders.lazy import WasmLazyStorefrom marimo._save.stores.dict_store import DictStore@@ -1006,14 +1064,12 @@ class TestReviewFixes(_FileStoreLoaderTest):w.flush()reader = LazyLoader("ns", store=store, signer=None, verification="on")- assert reader._effective_verification() == "off"- loaded = reader.load_cache(key("wasm_unsigned"))- assert loaded is not None- assert loaded.defs["z"] == 7+ assert reader._effective_verification() == "on"+ assert reader.load_cache(key("wasm_unsigned")) is None+ assert not reader.save_cache(_simple_cache(hash_val="new", z=9))- def test_wasm_store_no_crypto_degrades_to_off(self) -> None:- """Same same-origin rationale under the no-cryptography branch: the- WASM store degrades to off rather than missing every read."""+ def test_wasm_store_without_crypto_misses(self) -> None:+ """Missing cryptography must not permit unsigned WASM entries."""from unittest import mockfrom marimo._save.loaders.lazy import WasmLazyStore@@ -1032,10 +1088,9 @@ class TestReviewFixes(_FileStoreLoaderTest):reader = LazyLoader("ns", store=store, signer=None, verification="on")- assert reader._effective_verification() == "off"- loaded = reader.load_cache(key("wasm_nocrypto"))- assert loaded is not None- assert loaded.defs["z"] == 8+ assert reader._effective_verification() == "on"+ assert reader.load_cache(key("wasm_nocrypto")) is None+ assert not reader.save_cache(_simple_cache(hash_val="new", z=9))def test_strict_raises_on_undecodable_manifest(self) -> None:"""A manifest that fails to decode (malformed JSON, or a tampered@@ -1079,7 +1134,7 @@ class TestReviewFixes(_FileStoreLoaderTest):strict raises (fail-closed), verify misses (recompute). Otherwise astrict loader would silently miss an incomplete signed entry."""signer, verifier = _keypair()- writer = self._loader(signer=signer)+ writer = self._loader(signer=signer, verification="on")writer.save_cache(_simple_cache(hash_val="missing_blob", pt=_Point(1, 2)))@@ -1102,7 +1157,7 @@ class TestReviewFixes(_FileStoreLoaderTest):from unittest import mocksigner, verifier = _keypair()- writer = self._loader(signer=signer)+ writer = self._loader(signer=signer, verification="on")writer.save_cache(_simple_cache(hash_val="deser_fail", pt=_Point(1, 2)))@@ -1177,7 +1232,9 @@ class TestWasmSignatureEviction:signer, verifier = _keypair()store = WasmLazyStore(inner=DictStore())- writer = WasmLazyLoader("wasm_sig", store=store, signer=signer)+ writer = WasmLazyLoader(+ "wasm_sig", store=store, signer=signer, verification="on"+ )writer.save_cache(_simple_cache(hash_val="wasm_tamper", secret={"k": "v"}))@@ -1189,7 +1246,9 @@ class TestWasmSignatureEviction:)store._inner.put(blob_key, pickle.dumps({"injected": True}))- reader = WasmLazyLoader("wasm_sig", store=store, signer=verifier)+ reader = WasmLazyLoader(+ "wasm_sig", store=store, signer=verifier, verification="on"+ )# No network: the tampered blob is already resident in the inner store,# and evicted keys must not trigger a real fetch.with mock.patch.object(@@ -1226,7 +1285,7 @@ class TestUntrustedLayerCannotAnchorTrust(_FileStoreLoaderTest):Returns the fingerprint of the key that signed it."""attacker, _ = _keypair()- writer = self._loader(signer=attacker)+ writer = self._loader(signer=attacker, verification="on")assert writer.save_cache(_simple_cache(hash_val=hash_val, payload=1))writer.flush()return attacker.fingerprint()@@ -1238,7 +1297,7 @@ class TestUntrustedLayerCannotAnchorTrust(_FileStoreLoaderTest):"[tool.marimo.signing.trusted_signers]\n"f'"{attacker_fp}" = "totally legit CI key"\n'"[tool.marimo.cache]\n"- 'verification = "off"\n'+ 'verification = "strict"\n')notebook = tmp_path / "notebook.py"notebook.write_text("import marimo as mo")@@ -1251,8 +1310,8 @@ class TestUntrustedLayerCannotAnchorTrust(_FileStoreLoaderTest):policy = self._policy_for_project(tmp_path, attacker_fp)assert attacker_fp not in policy.trusted_signers- # The same layer also tried to switch verification off entirely.- assert policy.verification != "off"+ # The same layer also tried to set the verification posture.+ assert policy.verification != "strict"def test_committed_signed_cache_misses_under_on(self, tmp_path: Path@@ -1263,7 +1322,7 @@ class TestUntrustedLayerCannotAnchorTrust(_FileStoreLoaderTest):reader = self._loader(signer=None,trusted_signers=policy.trusted_signers,- verification=policy.verification,+ verification="on",)assert reader.load_cache(key("committed_on")) is None@@ -1297,3 +1356,160 @@ class TestUntrustedLayerCannotAnchorTrust(_FileStoreLoaderTest):loaded = reader.load_cache(key("committed_control"))assert loaded is not Noneassert loaded.defs["payload"] == 1+++class TestReadYourWrites(_FileStoreLoaderTest):+ @pytest.mark.parametrize(+ ("untrusted", "explicit"),+ [(True, False), (True, True), (False, False)],+ )+ @pytest.mark.parametrize("verification", ["off", "on"])+ def test_configured_store_provenance(+ self, untrusted, explicit, verification+ ) -> None:+ from marimo._save.loaders.lazy import LazyStore+ from tests._runtime._helpers.session import mocked_kernel_session++ signer, _ = _keypair()+ with mocked_kernel_session() as tk:+ tk.ctx.cache.store = self.store+ tk.ctx.cache.store_from_untrusted_origin = untrusted+ writer = LazyLoader(+ "source",+ store=LazyStore(self.store),+ signer=signer,+ verification=verification,+ )+ assert writer.save_cache(_simple_cache(v=42))+ writer.flush()+ reader = LazyLoader(+ "source",+ store=writer.store if explicit else None,+ signer=signer,+ verification=verification,+ )+ loaded = reader.load_cache(key("abc123"))+ if untrusted and not explicit and verification == "off":+ assert loaded is None+ else:+ assert loaded is not None+ assert loaded.defs == {"v": 42}++ def test_writes_to_one_key_finish_in_order(self, monkeypatch) -> None:+ first_started = threading.Event()+ read_started = threading.Event()+ release = threading.Event()+ original_put = self.store.put++ def blocked_first_put(key: str, value: bytes) -> bool:+ if not first_started.is_set():+ first_started.set()+ release.wait()+ return original_put(key, value)++ monkeypatch.setattr(self.store, "put", blocked_first_put)+ loader = self._loader(verification="off")++ def read():+ read_started.set()+ return loader.load_cache(key("same"))++ with ThreadPoolExecutor(max_workers=1) as executor:+ try:+ assert loader.save_cache(_simple_cache(hash_val="same", v=7))+ assert first_started.wait(timeout=5)+ assert loader.save_cache(_simple_cache(hash_val="same", v=9))+ result = executor.submit(read)+ assert read_started.wait(timeout=5)+ with pytest.raises(TimeoutError):+ result.result(timeout=0.1)+ finally:+ release.set()+ loader.flush()+ loaded = result.result(timeout=5)+ assert loaded is not None+ assert loaded.defs == {"v": 9}+ persisted = loader.load_cache(key("same"))+ assert persisted is not None+ assert persisted.defs == {"v": 9}++ @pytest.fixture+ def pending_write(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> Iterator[tuple[LazyLoader, threading.Event]]:+ started = threading.Event()+ release = threading.Event()+ original_put = self.store.put++ def blocked_put(key: str, value: bytes) -> bool:+ started.set()+ release.wait()+ return original_put(key, value)++ monkeypatch.setattr(self.store, "put", blocked_put)+ loader = self._loader(verification="off")+ try:+ assert loader.save_cache(_simple_cache(hash_val="pending", v=7))+ assert started.wait(timeout=5)+ yield loader, release+ finally:+ release.set()+ loader.flush()++ @pytest.mark.parametrize("operation", ["cache_hit", "load_cache"])+ def test_reads_wait_for_own_write(self, pending_write, operation) -> None:+ loader, release = pending_write+ started = threading.Event()++ def read():+ started.set()+ return getattr(loader, operation)(key("pending"))++ with ThreadPoolExecutor(max_workers=1) as executor:+ try:+ result = executor.submit(read)+ assert started.wait(timeout=5)+ with pytest.raises(TimeoutError):+ result.result(timeout=0.1)+ finally:+ release.set()+ loaded = result.result(timeout=5)++ if operation == "cache_hit":+ assert loaded is True+ else:+ assert loaded is not None+ assert loaded.defs == {"v": 7}++ @pytest.mark.parametrize("operation", ["cache_hit", "load_cache"])+ @pytest.mark.parametrize(+ "target", ["other_key", "other_loader", "other_store"]+ )+ def test_reads_do_not_wait_for_unrelated_write(+ self, pending_write, operation, target+ ) -> None:+ writer, release = pending_write+ if target == "other_key":+ reader = writer+ cache_key = key("missing")+ elif target == "other_loader":+ reader = self._loader(verification="off")+ cache_key = key("pending")+ else:+ reader = LazyLoader("test", store=MockStore(), verification="off")+ cache_key = key("pending")++ with ThreadPoolExecutor(max_workers=1) as executor:+ try:+ result = executor.submit(getattr(reader, operation), cache_key)+ assert result.result(timeout=5) is (+ False if operation == "cache_hit" else None+ )+ finally:+ release.set()++ if target == "other_loader":+ writer.flush()+ loaded = reader.load_cache(cache_key)+ assert loaded is not None+ assert loaded.defs == {"v": 7}diff --git a/tests/_save/loaders/test_lazy_wasm.py b/tests/_save/loaders/test_lazy_wasm.pyindex 7d0da5e220..d7d77efd85 100644--- a/tests/_save/loaders/test_lazy_wasm.py+++ b/tests/_save/loaders/test_lazy_wasm.py@@ -804,6 +804,7 @@ class TestOnRestoreFailure:# End-to-end: a manifest referencing a missing blob makes restore# raise; load_cache swallows it and invokes _on_restore_failure.store, loader, key = self._loader_and_key()+ loader.verification = "off" # The fixture contains unsigned data.manifest_path = str(loader.build_path(key))missing_ref = "h/missing.pickle"store._inner.put(manifest_path, self._manifest([missing_ref]))diff --git a/tests/_save/store/test_store.py b/tests/_save/store/test_store.pyindex 6f73631fdf..571490d6a2 100644--- a/tests/_save/store/test_store.py+++ b/tests/_save/store/test_store.py@@ -18,7 +18,9 @@ class TestStore:@app.celldef _(mo, store):assert store._cache == {}- with mo.persistent_cache("mock", store=store) as cache:+ with mo.persistent_cache(+ "mock", store=store, method="pickle"+ ) as cache:a = 1b = 2diff --git a/tests/_save/stubs/test_lazy_asset_codec.py b/tests/_save/stubs/test_lazy_asset_codec.pyindex d40a64e957..37beda7d00 100644--- a/tests/_save/stubs/test_lazy_asset_codec.py+++ b/tests/_save/stubs/test_lazy_asset_codec.py@@ -59,9 +59,6 @@ def test_lazy_loader_round_trips_blob_asset() -> None:assert loader.save_cache(cache)loader.flush()- assert (- Path("asset-test") / "asset_hash" / "preview.bin"- ).as_posix() in store._cacheassert (Path("asset-test") / "asset_hash" / "return.bin").as_posix() in store._cache@@ -69,4 +66,4 @@ def test_lazy_loader_round_trips_blob_asset() -> None:assert loaded is not Noneassert loaded.defs == {"preview": asset}- assert loaded.meta["return"] == asset+ assert loaded.meta["return"] is loaded.defs["preview"]diff --git a/tests/_save/test_cache_loader_config.py b/tests/_save/test_cache_loader_config.pynew file mode 100644index 0000000000..7f3c368b0f--- /dev/null+++ b/tests/_save/test_cache_loader_config.py@@ -0,0 +1,309 @@+# Copyright 2026 Marimo. All rights reserved.+"""`cache.loader` selects the default `mo.persistent_cache` loader."""++from __future__ import annotations++import logging+from types import SimpleNamespace+from typing import TYPE_CHECKING, Any++import pytest++from marimo import App, _loggers+from marimo._config.config import merge_default_config+from marimo._entrypoints.registry import EntryPointRegistry+from marimo._runtime.commands import ExecuteCellCommand+from marimo._runtime.context import get_context+from marimo._save.cache import CacheState+from marimo._save.loaders import (+ DEFAULT_LOADER,+ JsonLoader,+ LazyLoader,+ LoaderType,+ PickleLoader,+ get_loader_key,+ get_persistent_loader,+ normalize_loader_key,+)+from marimo._save.save import persistent_cache+from marimo._save.stores.file import FileStore+from marimo._types.ids import CellId_t+from tests._runtime._helpers.session import mocked_kernel_session++if TYPE_CHECKING:+ from marimo._save.cache import Cache+++def _patch_ctx(monkeypatch, cache: CacheState | None) -> None:+ ctx = None if cache is None else SimpleNamespace(cache=cache)+ monkeypatch.setattr("marimo._save.save.safe_get_context", lambda: ctx)+++def _loader_type(cache_context) -> type:+ return type(cache_context._loader())+++class FakeLoader(PickleLoader):+ """A third-party loader as a `marimo.cache.loader` entry point would be."""+++class BufferedLoader(FakeLoader):+ def __init__(self, name: str, **kwargs: Any) -> None:+ super().__init__(name, **kwargs)+ self._pending: list[Cache] = []++ def save_cache(self, cache: Cache) -> bool:+ self._pending.append(cache)+ return True++ def flush(self) -> None:+ for cache in self._pending:+ super().save_cache(cache)+ self._pending.clear()+++def _registry(**loaders: object) -> EntryPointRegistry[LoaderType]:+ registry = EntryPointRegistry[LoaderType]("marimo.cache.loader")+ for name, loader in loaders.items():+ registry.register(name, loader) # type: ignore[arg-type]+ return registry+++class TestNormalizeLoaderKey:+ def test_unset_is_lazy(self) -> None:+ assert DEFAULT_LOADER == "lazy"+ assert normalize_loader_key(None) == "lazy"++ @pytest.mark.parametrize("key", ["pickle", "json", "lazy"])+ def test_persistent_keys_pass_through(self, key: str) -> None:+ assert normalize_loader_key(key) == key++ @pytest.mark.parametrize("raw", ["memory", {"type": "lazy"}, 3])+ def test_invalid_value_warns_and_falls_back(+ self, raw: object, caplog: pytest.LogCaptureFixture, monkeypatch+ ) -> None:+ # The marimo logger does not propagate, so caplog needs it to.+ monkeypatch.setattr(_loggers.marimo_logger(), "propagate", True)+ with caplog.at_level(logging.WARNING):+ assert normalize_loader_key(raw) == "lazy"+ assert f"Invalid cache.loader {raw!r}" in caplog.text+++class TestEntryPointLoaders:+ def test_registered_name_is_a_valid_key(self) -> None:+ registry = _registry(fake=FakeLoader)+ assert normalize_loader_key("fake", registry) == "fake"+ assert get_persistent_loader("fake", registry) is FakeLoader++ def test_builtin_name_wins_over_entry_point(self) -> None:+ registry = _registry(pickle=FakeLoader)+ assert get_persistent_loader("pickle", registry) is PickleLoader++ def test_unknown_name_raises(self) -> None:+ registry = _registry(fake=FakeLoader)+ with pytest.raises(ValueError, match="Invalid cache loader 'nope'"):+ get_persistent_loader("nope", registry)++ def test_non_loader_entry_point_raises(self) -> None:+ registry = _registry(bad=object)+ with pytest.raises(ValueError, match="must be a Loader subclass"):+ get_persistent_loader("bad", registry)++ def test_allowlist_env_hides_entry_point(+ self, monkeypatch, caplog: pytest.LogCaptureFixture+ ) -> None:+ monkeypatch.setenv("MARIMO_CACHE_LOADER_ALLOWLIST", "")+ registry = _registry(fake=FakeLoader)+ monkeypatch.setattr(_loggers.marimo_logger(), "propagate", True)+ with caplog.at_level(logging.WARNING):+ assert normalize_loader_key("fake", registry) == "lazy"+ assert "Invalid cache.loader 'fake'" in caplog.text+ with pytest.raises(ValueError, match="Invalid cache loader 'fake'"):+ get_persistent_loader("fake", registry)++ def test_persistent_cache_resolves_configured_entry_point(+ self, monkeypatch, tmp_path+ ) -> None:+ monkeypatch.setattr(+ "marimo._save.loaders._LOADER_REGISTRY", _registry(fake=FakeLoader)+ )+ _patch_ctx(+ monkeypatch,+ CacheState(store=FileStore(str(tmp_path)), loader="fake"),+ )+ assert _loader_type(persistent_cache("ns")) is FakeLoader++ @pytest.mark.parametrize("method", ["fake", "memory", "missing"])+ def test_persistent_cache_rejects_custom_method(+ self, method: str, monkeypatch, tmp_path+ ) -> None:+ monkeypatch.setattr(+ "marimo._save.loaders._LOADER_REGISTRY", _registry(fake=FakeLoader)+ )+ _patch_ctx(+ monkeypatch,+ CacheState(store=FileStore(str(tmp_path)), loader="fake"),+ )+ with pytest.raises(+ ValueError, match=rf"Invalid method {method!r}.*cache\.loader"+ ):+ persistent_cache("ns", method=method)++ @pytest.mark.parametrize(+ ("loader", "expected_loader"),+ [+ ("pickle", PickleLoader),+ ("json", JsonLoader),+ ("lazy", LazyLoader),+ ("fake", FakeLoader),+ ("buffered", BufferedLoader),+ ],+ )+ @pytest.mark.parametrize("form", ["context", "function", "async"])+ async def test_configured_loaders_restore_across_sessions(+ self,+ loader: str,+ expected_loader: LoaderType,+ form: str,+ monkeypatch,+ tmp_path,+ ) -> None:+ monkeypatch.setattr(+ "marimo._save.loaders._LOADER_REGISTRY",+ _registry(fake=FakeLoader, buffered=BufferedLoader),+ )+ user_config = merge_default_config({"cache": {"loader": loader}})+ if form == "context":+ code = (+ f"with mo.persistent_cache('configured', save_path={str(tmp_path)!r}) as cache:\n"+ " result = {'answer': [42]}\n"+ "was_hit = cache.hit"+ )+ else:+ prefix = "async " if form == "async" else ""+ await_call = "await " if form == "async" else ""+ code = (+ f"@mo.persistent_cache(save_path={str(tmp_path)!r})\n"+ f"{prefix}def cache():\n return {{'answer': [42]}}\n"+ f"result = {await_call}cache()\n"+ "was_hit = cache.cache_info().hits > 0"+ )+ commands = [+ ExecuteCellCommand(+ cell_id=CellId_t("imports"), code="import marimo as mo"+ ),+ ExecuteCellCommand(+ cell_id=CellId_t("cache"),+ code=code,+ ),+ ]+ for expected_hit in (False, True):+ with mocked_kernel_session(user_config=user_config) as tk:+ await tk.kernel.run(commands)+ assert (+ type(tk.kernel.globals["cache"].loader) is expected_loader+ )+ assert (+ tk.kernel.globals["result"],+ tk.kernel.globals["was_hit"],+ ) == ({"answer": [42]}, expected_hit)++ async def test_embedded_app_inherits_configured_loader(+ self, monkeypatch+ ) -> None:+ monkeypatch.setattr(+ "marimo._save.loaders._LOADER_REGISTRY", _registry(fake=FakeLoader)+ )+ child = App()++ @child.cell+ def _():+ import marimo as mo++ selected = mo.persistent_cache("child").loader+ return (selected,)++ user_config = merge_default_config({"cache": {"loader": "fake"}})+ with mocked_kernel_session(user_config=user_config):+ result = await child.embed()+ assert type(result.defs["selected"]) is FakeLoader+++class TestGetLoaderKey:+ def test_reads_cache_loader(self) -> None:+ assert (+ get_loader_key(config={"cache": {"loader": "pickle"}}) == "pickle"+ )++ def test_missing_section_is_default(self) -> None:+ assert get_loader_key(config={}) == "lazy"+ assert get_loader_key(config={"cache": {}}) == "lazy"+++class TestPersistentCacheDefaultMethod:+ def test_configured_loader_is_used(self, monkeypatch, tmp_path) -> None:+ _patch_ctx(+ monkeypatch,+ CacheState(store=FileStore(str(tmp_path)), loader="pickle"),+ )+ assert _loader_type(persistent_cache("ns")) is PickleLoader++ @pytest.mark.parametrize(+ ("method", "expected_loader"),+ [("pickle", PickleLoader), ("json", JsonLoader), ("lazy", LazyLoader)],+ )+ def test_explicit_method_wins(+ self, method: str, expected_loader: LoaderType, monkeypatch, tmp_path+ ) -> None:+ monkeypatch.setattr(+ "marimo._save.loaders._LOADER_REGISTRY", _registry(fake=FakeLoader)+ )+ _patch_ctx(+ monkeypatch,+ CacheState(store=FileStore(str(tmp_path)), loader="fake"),+ )+ assert (+ _loader_type(persistent_cache("ns", method=method))+ is expected_loader+ )++ def test_no_context_is_lazy(self, monkeypatch) -> None:+ _patch_ctx(monkeypatch, None)+ assert _loader_type(persistent_cache("ns")) is LazyLoader++ def test_cache_state_default_is_lazy(self, tmp_path) -> None:+ assert CacheState(store=FileStore(str(tmp_path))).loader == "lazy"+++def test_kernel_reads_cache_loader_from_user_config() -> None:+ user_config = merge_default_config({"cache": {"loader": "pickle"}})+ with mocked_kernel_session(user_config=user_config):+ assert get_context().cache.loader == "pickle"+++async def test_default_cache_clear_recomputes(tmp_path) -> None:+ with mocked_kernel_session() as tk:+ tk.ctx.cache.store = FileStore(str(tmp_path))+ await tk.kernel.run(+ [+ ExecuteCellCommand(+ cell_id=CellId_t("imports"), code="import marimo as mo"+ ),+ ExecuteCellCommand(+ cell_id=CellId_t("cache"),+ code=(+ "@mo.persistent_cache\n"+ "def compute():\n return 42\n"+ "compute()\ncompute()\n"+ "before = compute.cache_info()\n"+ "compute.cache_clear()\n"+ "result = compute()\n"+ "after = compute.cache_info()"+ ),+ ),+ ]+ )+ assert tk.kernel.globals["result"] == 42+ assert tk.kernel.globals["before"].hits == 1+ after = tk.kernel.globals["after"]+ assert (after.hits, after.misses) == (1, 2)diff --git a/tests/_save/test_cache_versions.py b/tests/_save/test_cache_versions.pyindex 24db9a425d..8fc8329b36 100644--- a/tests/_save/test_cache_versions.py+++ b/tests/_save/test_cache_versions.py@@ -21,7 +21,9 @@ class TestVersionCache:ref = 1with persistent_cache(- name="pickle-dump-v4", save_path="tests/_save/cache-dumps"+ name="pickle-dump-v4",+ save_path="tests/_save/cache-dumps",+ method="pickle",) as cache:value = 1 + len(unhashable) + refassert cache.hitdiff --git a/tests/_save/test_manifest.py b/tests/_save/test_manifest.pyindex 4862353857..228da429a2 100644--- a/tests/_save/test_manifest.py+++ b/tests/_save/test_manifest.py@@ -254,6 +254,32 @@ def _recorded_puts(monkeypatch: pytest.MonkeyPatch) -> Iterator[list[str]]:class TestRecording:+ @staticmethod+ async def test_failed_cache_flush_does_not_record_entry(+ tmp_path: Path,+ exec_req: ExecReqProvider,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ from marimo._save.loaders import PickleLoader++ def fail_flush(_self: PickleLoader) -> None:+ raise OSError("Cache write failed")++ monkeypatch.setattr(PickleLoader, "save_cache", lambda *_: True)+ monkeypatch.setattr(PickleLoader, "flush", fail_flush)+ with _notebook_kernel(tmp_path) as (k, notebook, cache_dir):+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get(+ "with mo.persistent_cache('train', method='pickle'):\n x = 42\n"+ ),+ ]+ )+ assert k.globals["x"] == 42+ assert k.stderr.messages+ assert not (cache_dir / manifest_name(notebook)).exists()+@staticmethodasync def test_with_block_records_its_entry(tmp_path: Path, exec_req: ExecReqProvider@@ -277,7 +303,7 @@ class TestRecording:assert len(entries) == 1block, key = entries.pop()assert block == "train"- assert (cache_dir / block / f"{key}.pickle").exists()+ assert (cache_dir / block / f"{key}.jsonl").exists()@staticmethodasync def test_records_accumulate_across_edits(@@ -706,7 +732,7 @@ class TestRecording:assert {block for block, _ in there} == {"there"}for block, key in here | there:directory = cache_dir if block == "here" else elsewhere- assert (directory / block / f"{key}.pickle").exists()+ assert (directory / block / f"{key}.jsonl").exists()@staticmethodasync def test_every_copy_of_a_manifest_finds_the_notebook(diff --git a/tests/_save/test_signed_defaults.py b/tests/_save/test_signed_defaults.pynew file mode 100644index 0000000000..2b4a8df971--- /dev/null+++ b/tests/_save/test_signed_defaults.py@@ -0,0 +1,208 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import asyncio+import os+import subprocess+import sys+import threading+from typing import TYPE_CHECKING, Any+from unittest.mock import patch++import msgspec+import pytest++from marimo._config.config import merge_default_config+from marimo._runtime.commands import ExecuteCellCommand+from marimo._save import signing+from marimo._save.loaders import lazy+from marimo._save.stubs.lazy_stub import Cache as CacheSchema+from marimo._types.ids import CellId_t+from tests._runtime._helpers.session import mocked_kernel_session++if TYPE_CHECKING:+ from pathlib import Path+++def _read_manifest(path: Path) -> CacheSchema:+ manifests = list(path.rglob("*.jsonl"))+ assert len(manifests) == 1+ return msgspec.json.decode(manifests[0].read_bytes(), type=CacheSchema)++[email protected]("cryptography")[email protected]("form", ["function", "async", "context"])[email protected]("verification", [None, "on"])+async def test_persistent_cache_defaults_to_lazy(+ form: str,+ verification: str | None,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The lazy format is the default; signing needs `verification = "on"`."""+ signer = signing.CacheSigner.from_private_key_pem(+ signing.generate_keypair()[0]+ )+ monkeypatch.setattr(+ lazy, "_get_default_signer", lambda *_args, **_kwargs: signer+ )+ monkeypatch.setattr(+ lazy, "_get_machine_signer", lambda *_args, **_kwargs: signer+ )+ arguments = f"save_path={str(tmp_path)!r}"+ if form == "context":+ code = f'with mo.persistent_cache("default", {arguments}):\n result = 42'+ else:+ prefix = "async " if form == "async" else ""+ await_call = "await " if form == "async" else ""+ code = (+ f"@mo.persistent_cache({arguments})\n"+ f"{prefix}def compute():\n return 42\n"+ f"result = {await_call}compute()"+ )+ user_config = merge_default_config(+ {}+ if verification is None+ else {"cache": {"verification": verification}}+ )+ with mocked_kernel_session(user_config=user_config) as tk:+ await tk.kernel.run(+ [+ ExecuteCellCommand(+ cell_id=CellId_t("imports"), code="import marimo as mo"+ ),+ ExecuteCellCommand(cell_id=CellId_t("cache"), code=code),+ ]+ )+ assert tk.kernel.globals["result"] == 42+ lazy.LazyLoader.flush_all()+ schema = await asyncio.to_thread(_read_manifest, tmp_path)+ if verification is None:+ assert schema.meta.signature is None+ else:+ assert schema.meta.signature is not None+ signer.verify(lazy._signable_bytes(schema), schema.meta.signature)++[email protected]("cryptography")+def test_concurrent_key_creation_keeps_one_identity(tmp_path: Path) -> None:+ key_path = tmp_path / "key.pem"+ both_generating = threading.Barrier(2)+ first_returned = threading.Event()+ original_generate = signing.generate_keypair+ original_replace, original_link = os.replace, os.link+ signers: dict[str, signing.CacheSigner | None] = {}+ failures: list[Exception] = []++ def generate() -> tuple[str, str]:+ pair = original_generate()+ both_generating.wait(timeout=10)+ return pair++ def publish(operation: Any, *args: Any, **kwargs: Any) -> None:+ # Both callers observed a missing key, but the first finishes before+ # the second publishes its candidate.+ if threading.current_thread().name == "second":+ assert first_returned.wait(timeout=10)+ operation(*args, **kwargs)++ def resolve() -> None:+ name = threading.current_thread().name+ try:+ signers[name] = signing._get_machine_signer(str(key_path))+ except Exception as exc:+ failures.append(exc)+ finally:+ if name == "first":+ first_returned.set()++ with (+ patch.object(signing, "generate_keypair", generate),+ patch.object(+ os, "replace", lambda *a, **kw: publish(original_replace, *a, **kw)+ ),+ patch.object(+ os, "link", lambda *a, **kw: publish(original_link, *a, **kw)+ ),+ ):+ threads = [+ threading.Thread(target=resolve, name=name)+ for name in ("first", "second")+ ]+ for thread in threads:+ thread.start()+ for thread in threads:+ thread.join(timeout=15)+ assert not any(thread.is_alive() for thread in threads)+ assert not failures+ persisted = signing.CacheSigner.from_private_key_pem(key_path.read_text())+ for signer in signers.values():+ assert signer is not None+ persisted.verify(b"cached result", signer.sign(b"cached result"))++[email protected]("cryptography")+def test_key_cleanup_failure_keeps_published_identity(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ key_path = tmp_path / "key.pem"++ def fail_cleanup(_path: str) -> None:+ raise OSError("Temporary file could not be removed")++ monkeypatch.setattr(os, "unlink", fail_cleanup)+ signer = signing._get_machine_signer(str(key_path))+ assert signer is not None+ persisted = signing.CacheSigner.from_private_key_pem(key_path.read_text())+ persisted.verify(b"cached result", signer.sign(b"cached result"))++[email protected]("cryptography")+def test_persistent_cache_verifies_across_restarts(tmp_path: Path) -> None:+ """With `verification = "on"` in user config, a tampered entry recomputes."""+ private_pem, _ = signing.generate_keypair()+ xdg_config = tmp_path / "xdg-config"+ (xdg_config / "marimo").mkdir(parents=True)+ (xdg_config / "marimo" / "marimo.toml").write_text(+ '[cache]\nverification = "on"\n'+ )+ notebook = tmp_path / "notebook.py"+ notebook.write_text(+ "import marimo\napp = marimo.App()\n"+ "with app.setup:\n import marimo as mo\n"+ "@app.cell\ndef _():\n"+ f" with mo.persistent_cache('restart', save_path={str(tmp_path / 'cache')!r}) as cache:\n"+ " print('computed')\n value = 42\n"+ " print(f'value={value};hit={cache.hit}')\n return\n"+ "if __name__ == '__main__':\n app.run()\n"+ )+ # A `~/.marimo.toml` wins over the XDG path, so give the child its own home.+ home = tmp_path / "home"+ home.mkdir()+ env = dict(+ os.environ,+ MARIMO_CACHE_SIGNING_PRIVATE_KEY=private_pem,+ HOME=str(home),+ USERPROFILE=str(home),+ XDG_CONFIG_HOME=str(xdg_config),+ XDG_STATE_HOME=str(tmp_path / "xdg-state"),+ )++ def run() -> str:+ return subprocess.run(+ [sys.executable, str(notebook)],+ cwd=tmp_path,+ env=env,+ check=True,+ capture_output=True,+ text=True,+ timeout=30,+ ).stdout++ assert run().splitlines() == ["computed", "value=42;hit=False"]+ assert run().splitlines() == ["value=42;hit=True"]+ manifest = next((tmp_path / "cache").rglob("*.jsonl"))+ data = msgspec.json.decode(manifest.read_bytes())+ data["meta"]["signature"] = "A" * 88+ manifest.write_bytes(msgspec.json.encode(data))+ assert run().splitlines() == ["computed", "value=42;hit=False"]diff --git a/tests/_save/test_signing.py b/tests/_save/test_signing.pyindex 4859dbb8a0..32092848b9 100644--- a/tests/_save/test_signing.py+++ b/tests/_save/test_signing.py@@ -386,7 +386,7 @@ class TestGetDefaultSigner:).public_key_pem())- def test_corrupt_key_file_regenerates(self, tmp_path: Any) -> None:+ def test_corrupt_key_file_preserves_identity(self, tmp_path: Any) -> None:from marimo._save.signing import _get_default_signerkey_path = tmp_path / "cache_signing_key.pem"@@ -409,8 +409,5 @@ class TestGetDefaultSigner:),):signer = _get_default_signer()- assert signer is not None- assert signer.can_sign- # Should have overwritten the corrupt file- assert key_path.exists()- assert "PRIVATE KEY" in key_path.read_text()+ assert signer is None+ assert key_path.read_text() == "not a valid PEM"diff --git a/tests/_save/test_signing_policy.py b/tests/_save/test_signing_policy.pyindex 0642f9f55d..acdde45221 100644--- a/tests/_save/test_signing_policy.py+++ b/tests/_save/test_signing_policy.py@@ -51,7 +51,7 @@ def _alternate_spelling(fp: str) -> str:class TestSigningPolicyFromConfig:def test_empty_config_defaults(self) -> None:policy = SigningPolicy.from_config({})- assert policy.verification == "on"+ assert policy.verification == "off"assert policy.trusted_signers == frozenset()assert policy.signer is None# The machine-key path is frozen at construction, not read live later.