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.

fix: only show previous session when in edit mode (#7737)

Myles Scolnick0b0875c0c3c0
diff --git a/marimo/_session/session.py b/marimo/_session/session.py
index de94994b6d..fb6c44404c 100644
--- a/marimo/_session/session.py
+++ b/marimo/_session/session.py
@@ -116,7 +116,9 @@ class SessionImpl(Session):
*(extensions or []),
LoggingExtension(),
HeartbeatExtension(),
- CachingExtension(enabled=not auto_instantiate),
+ CachingExtension(
+ enabled=not auto_instantiate and mode == SessionMode.EDIT
+ ),
NotificationListenerExtension(
kernel_manager=kernel_manager, queue_manager=queue_manager
),
diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.py
index 3cc6419d71..9fa98f4549 100644
--- a/tests/_server/test_sessions.py
+++ b/tests/_server/test_sessions.py
@@ -1066,3 +1066,53 @@ def test_session_with_script_config_overrides(
# Cleanup
session.close()
+
+
+@save_and_restore_main
+async def test_caching_enabled_only_in_edit_mode() -> None:
+ """Test that caching is only enabled in EDIT mode, not RUN mode."""
+ from marimo._session.extensions.extensions import CachingExtension
+
+ session_consumer = MagicMock()
+ session_consumer.connection_state.return_value = ConnectionState.OPEN
+
+ def create_session(mode: SessionMode, auto_instantiate: bool) -> Session:
+ return SessionImpl.create(
+ initialization_id="test_session",
+ session_consumer=session_consumer,
+ mode=mode,
+ app_metadata=app_metadata,
+ app_file_manager=AppFileManager.from_app(InternalApp(App())),
+ config_manager=get_default_config_manager(current_path=None),
+ virtual_files_supported=True,
+ redirect_console_to_browser=False,
+ ttl_seconds=None,
+ auto_instantiate=auto_instantiate,
+ )
+
+ def find_caching_extension(session: Session) -> CachingExtension:
+ extension = [
+ ext
+ for ext in session.extensions
+ if isinstance(ext, CachingExtension)
+ ]
+ assert len(extension) == 1
+ return extension[0]
+
+ # Test 1: EDIT mode with auto_instantiate=False -> caching enabled
+ session_edit = create_session(SessionMode.EDIT, False)
+
+ # Find the CachingExtension
+ caching_extension_edit = find_caching_extension(session_edit)
+ assert caching_extension_edit.enabled is True
+
+ # Test 2: RUN mode with auto_instantiate=True -> caching disabled
+ session_run = create_session(SessionMode.RUN, True)
+
+ # Find the CachingExtension
+ caching_extension_run = find_caching_extension(session_run)
+ assert caching_extension_run.enabled is False
+
+ # Cleanup
+ session_edit.close()
+ session_run.close()