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
internal: session extensions (#7542)
Myles Scolnick86e5e1866145
diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.pyindex c124bae25c..a4539f57a0 100644--- a/marimo/_pyodide/pyodide_session.py+++ b/marimo/_pyodide/pyodide_session.py@@ -8,19 +8,17 @@ import reimport signalimport typingfrom pathlib import Path-from typing import Any, Callable, TypeVar, cast+from typing import TYPE_CHECKING, Any, Callable, TypeVar, castimport msgspecfrom marimo import _loggers-from marimo._ast.cell import CellConfigfrom marimo._config.config import (MarimoConfig,PartialMarimoConfig,merge_default_config,)from marimo._messaging.msgspec_encoder import encode_json_str-from marimo._messaging.types import KernelMessagefrom marimo._pyodide.restartable_task import RestartableTaskfrom marimo._pyodide.streams import (PyodideStderr,@@ -73,14 +71,18 @@ from marimo._server.models.models import (SaveNotebookRequest,SaveUserConfigurationRequest,)-from marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._snippets.snippets import read_snippets-from marimo._types.ids import CellId_tfrom marimo._utils.formatter import DefaultFormatterfrom marimo._utils.inline_script_metadata import PyProjectReaderfrom marimo._utils.parse_dataclass import parse_raw+if TYPE_CHECKING:+ from marimo._ast.cell import CellConfig+ from marimo._messaging.types import KernelMessage+ from marimo._server.notebook.file_manager import AppFileManager+ from marimo._types.ids import CellId_t+LOGGER = _loggers.marimo_logger()diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.pyindex 4003975962..f3c8a8b1da 100644--- a/marimo/_server/api/endpoints/execution.py+++ b/marimo/_server/api/endpoints/execution.py@@ -377,7 +377,7 @@ async def takeover_endpoint()# Wait 100ms to ensure the client has received the messageawait asyncio.sleep(0.1)- existing_session.maybe_disconnect_consumer()+ existing_session.disconnect_main_consumer()else:LOGGER.warning("No existing session found for file key %s", file_key)diff --git a/marimo/_server/api/endpoints/files.py b/marimo/_server/api/endpoints/files.pyindex 6d252dfe1d..6498d5e193 100644--- a/marimo/_server/api/endpoints/files.py+++ b/marimo/_server/api/endpoints/files.py@@ -103,7 +103,7 @@ async def rename_file(if new_path:# Handle rename for watch- app_state.session_manager.handle_file_rename_for_watch(+ await app_state.session_manager.handle_file_rename_for_watch(app_state.require_current_session_id(), prev_path, new_path)diff --git a/marimo/_server/api/endpoints/ws.py b/marimo/_server/api/endpoints/ws.pyindex 9cf4d9f03c..a38be41a97 100644--- a/marimo/_server/api/endpoints/ws.py+++ b/marimo/_server/api/endpoints/ws.py@@ -391,9 +391,9 @@ class WebsocketHandler(SessionConsumer):self._write_kernel_ready(session=session,resumed=True,- ui_values=session.get_current_state().ui_values,- last_executed_code=session.get_current_state().last_executed_code,- last_execution_time=session.get_current_state().last_execution_time,+ ui_values=session.session_view.ui_values,+ last_executed_code=session.session_view.last_executed_code,+ last_execution_time=session.session_view.last_execution_time,kiosk=self.kiosk,)self.write_operation(@@ -425,9 +425,9 @@ class WebsocketHandler(SessionConsumer):self._write_kernel_ready(session=session,resumed=True,- ui_values=session.get_current_state().ui_values,- last_executed_code=session.get_current_state().last_executed_code,- last_execution_time=session.get_current_state().last_execution_time,+ ui_values=session.session_view.ui_values,+ last_executed_code=session.session_view.last_executed_code,+ last_execution_time=session.session_view.last_execution_time,kiosk=True,)self.status = ConnectionState.OPEN@@ -435,7 +435,7 @@ class WebsocketHandler(SessionConsumer):def _replay_previous_session(self, session: Session) -> None:"""Replay the previous session view."""- operations = session.get_current_state().operations+ operations = session.session_view.operationsif len(operations) == 0:LOGGER.debug("No operations to replay")return@@ -557,7 +557,7 @@ class WebsocketHandler(SessionConsumer):if existing_session is not None:LOGGER.debug("Reconnecting session %s", session_id)# In case there is a lingering connection, close it- existing_session.maybe_disconnect_consumer()+ existing_session.disconnect_main_consumer()self._reconnect_session(existing_session, replay=False)return existing_session@@ -600,6 +600,7 @@ class WebsocketHandler(SessionConsumer):session_id=session_id,session_consumer=self,file_key=self.file_key,+ auto_instantiate=self.auto_instantiate,)self.status = ConnectionState.CONNECTING# Let the frontend know it can instantiate the app.@@ -612,10 +613,7 @@ class WebsocketHandler(SessionConsumer):kiosk=False,)self.status = ConnectionState.OPEN- # if auto-instantiate if false, replay the previous session- if not self.auto_instantiate:- new_session.sync_session_view_from_cache()- self._replay_previous_session(new_session)+ self._replay_previous_session(new_session)return new_sessionget_session()@@ -773,9 +771,9 @@ class WebsocketHandler(SessionConsumer):self._write_kernel_ready(session=session,resumed=True,- ui_values=session.get_current_state().ui_values,- last_executed_code=session.get_current_state().last_executed_code,- last_execution_time=session.get_current_state().last_execution_time,+ ui_values=session.session_view.ui_values,+ last_executed_code=session.session_view.last_executed_code,+ last_execution_time=session.session_view.last_execution_time,kiosk=False,)self.status = ConnectionState.OPENdiff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 23d66ab4fb..5ef15b277f 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -338,6 +338,7 @@ async def run_app_until_completion(virtual_files_supported=False,redirect_console_to_browser=False,ttl_seconds=None,+ auto_instantiate=True,)# Run the notebook to completion oncediff --git a/marimo/_server/sessions/events.py b/marimo/_server/sessions/events.pyindex 57f2325f89..d4ad00af85 100644--- a/marimo/_server/sessions/events.py+++ b/marimo/_server/sessions/events.py@@ -6,28 +6,48 @@ Provides an event bus and listeners for session creation, closure, and resumptiofrom __future__ import annotations-from typing import Protocol+from typing import TYPE_CHECKING-from marimo._server.sessions.session import Session+from marimo import _loggersfrom marimo._types.ids import SessionId+if TYPE_CHECKING:+ from marimo._server.sessions.session import Session-class SessionEventListener(Protocol):- """Protocol for session event listeners."""+LOGGER = _loggers.marimo_logger()+++class SessionEventListener:+ """Base class for session event listeners.++ Not all methods need to be implemented.+ """async def on_session_created(self, session: Session) -> None:"""Called when a session is created."""- ...+ del session+ return Noneasync def on_session_closed(self, session: Session) -> None:"""Called when a session is closed."""- ...+ del session+ return Noneasync def on_session_resumed(self, session: Session, old_id: SessionId) -> None:"""Called when a session is resumed with a new ID."""- ...+ del session+ del old_id+ return None++ async def on_session_notebook_renamed(+ self, session: Session, new_path: str+ ) -> None:+ """Called when a session notebook is renamed."""+ del session+ del new_path+ return Noneclass SessionEventBus:@@ -51,16 +71,55 @@ class SessionEventBus:async def emit_session_created(self, session: Session) -> None:"""Emit a session created event."""for listener in self._listeners:- await listener.on_session_created(session)+ try:+ await listener.on_session_created(session)+ except Exception as e:+ LOGGER.error(+ "Error handling session created event for listener %s: %s",+ listener,+ e,+ )+ continueasync def emit_session_closed(self, session: Session) -> None:"""Emit a session closed event."""for listener in self._listeners:- await listener.on_session_closed(session)+ try:+ await listener.on_session_closed(session)+ except Exception as e:+ LOGGER.error(+ "Error handling session closed event for listener %s: %s",+ listener,+ e,+ )+ continueasync def emit_session_resumed(self, session: Session, old_id: SessionId) -> None:"""Emit a session resumed event."""for listener in self._listeners:- await listener.on_session_resumed(session, old_id)+ try:+ await listener.on_session_resumed(session, old_id)+ except Exception as e:+ LOGGER.error(+ "Error handling session resumed event for listener %s: %s",+ listener,+ e,+ )+ continue++ async def emit_session_notebook_renamed(+ self, session: Session, new_path: str+ ) -> None:+ """Emit a session renamed event."""+ for listener in self._listeners:+ try:+ await listener.on_session_notebook_renamed(session, new_path)+ except Exception as e:+ LOGGER.error(+ "Error handling session notebook renamed event for listener %s: %s",+ listener,+ e,+ )+ continuediff --git a/marimo/_server/sessions/extensions.py b/marimo/_server/sessions/extensions.pynew file mode 100644index 0000000000..43a0606df1--- /dev/null+++ b/marimo/_server/sessions/extensions.py@@ -0,0 +1,221 @@+# Copyright 2024 Marimo. All rights reserved.+"""Session extensions for composable session features.++Extensions provide a way to add cross-cutting concerns to sessions+(like heartbeat monitoring, caching, metrics) without modifying SessionImpl.+"""++from __future__ import annotations++import asyncio+from typing import TYPE_CHECKING, Optional, Protocol++from marimo import _loggers+from marimo._cli.print import red+from marimo._server.session.serialize import (+ SessionCacheKey,+ SessionCacheManager,+)+from marimo._server.sessions.events import (+ SessionEventBus,+ SessionEventListener,+)+from marimo._server.sessions.types import KernelState+from marimo._server.utils import print_, print_tabbed+from marimo._types.ids import SessionId++if TYPE_CHECKING:+ from marimo._server.sessions.types import Session++LOGGER = _loggers.marimo_logger()+++class SessionExtension(Protocol):+ """Base class for session extensions.++ Extensions can hook into session lifecycle events and add+ functionality without modifying the core Session class.+ """++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ """Called when extension is attached to a session.++ Args:+ session: The session this extension is attached to+ event_bus: Event bus for subscribing to session events+ """+ ...++ def on_detach(self) -> None:+ """Called when extension is detached from session (cleanup)."""+ ...+++class HeartbeatExtension(SessionExtension):+ """Extension for monitoring kernel health and handling kernel death.++ Periodically checks if the kernel is alive and triggers cleanup+ when the kernel dies unexpectedly.+ """++ def __init__(self) -> None:+ self.heartbeat_task: Optional[asyncio.Task[None]] = None++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ del event_bus+ self._start(session)++ def on_detach(self) -> None:+ self._stop()++ def _start(self, session: Session) -> None:+ """Start the heartbeat monitoring."""++ def _check_alive() -> None:+ if session.kernel_state() == KernelState.STOPPED:+ LOGGER.debug("Kernel died, invoking cleanup callback")+ session.close()+ print_()+ filename = session.app_file_manager.filename+ filename_str = filename or "unknown"+ print_tabbed(+ red(+ "The Python kernel for file "+ f"{filename_str} died unexpectedly."+ )+ )+ print_()++ async def _heartbeat() -> None:+ while True:+ await asyncio.sleep(1)+ _check_alive()++ try:+ loop = asyncio.get_event_loop()+ self.heartbeat_task = loop.create_task(_heartbeat())+ except RuntimeError:+ # This can happen if there is no event loop running+ self.heartbeat_task = None++ def _stop(self) -> None:+ """Stop the heartbeat monitoring."""+ if self.heartbeat_task:+ self.heartbeat_task.cancel()+++class CachingExtension(SessionExtension, SessionEventListener):+ """Extension for caching session state to disk.++ Periodically writes session state to disk so it can be restored+ on reconnection or browser refresh.+ """++ SESSION_CACHE_INTERVAL_SECONDS = 2++ def __init__(+ self,+ *,+ enabled: bool,+ interval: int = SESSION_CACHE_INTERVAL_SECONDS,+ ) -> None:+ """Initialize the caching extension.++ Args:+ enabled: Whether to enable caching+ interval: How often to write cache (in seconds)+ """+ self.interval = interval+ self.enabled = enabled+ self.session_cache_manager: Optional[SessionCacheManager] = None++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ """Initialize cache manager when attached to session."""+ if not self.enabled:+ return++ # Subscribe to events (like rename)+ event_bus.subscribe(self)++ from marimo._version import __version__++ LOGGER.debug("Syncing session view from cache")+ self.session_cache_manager = SessionCacheManager(+ session_view=session.session_view,+ path=session.app_file_manager.path,+ interval=self.interval,+ )++ # Serialize the session view based on the current app+ app = session.app_file_manager.app+ codes = tuple(+ cell_data.code for cell_data in app.cell_manager.cell_data()+ )+ cell_ids = tuple(app.cell_manager.cell_ids())+ key = SessionCacheKey(+ codes=codes, marimo_version=__version__, cell_ids=cell_ids+ )+ session.session_view = self.session_cache_manager.read_session_view(+ key+ )++ # Start the background task to write the session view to disk+ self.session_cache_manager.start()++ def on_detach(self) -> None:+ """Stop cache manager when detached."""+ self._stop()++ async def on_session_notebook_renamed(+ self, session: Session, new_path: str+ ) -> None:+ """Rename the path for the cache manager."""+ del session+ if self.session_cache_manager:+ self.session_cache_manager.rename_path(new_path)+ return None++ def _stop(self) -> None:+ """Stop the cache manager."""+ if self.session_cache_manager:+ self.session_cache_manager.stop()+ self.session_cache_manager = None+++class LoggingExtension(SessionExtension, SessionEventListener):+ """Extension for logging session events."""++ def __init__(self) -> None:+ self.logger = LOGGER++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ del session+ self.logger.debug("Attaching extensions")+ event_bus.subscribe(self)++ def on_detach(self) -> None:+ self.logger.debug("Detaching extensions")++ async def on_session_created(self, session: Session) -> None:+ self.logger.debug("Session created: %s", session.initialization_id)++ async def on_session_closed(self, session: Session) -> None:+ self.logger.debug("Session closed: %s", session.initialization_id)++ async def on_session_resumed(+ self, session: Session, old_id: SessionId+ ) -> None:+ self.logger.debug(+ "Session resumed: %s (old id: %s)",+ session.initialization_id,+ old_id,+ )++ async def on_session_notebook_renamed(+ self, session: Session, new_path: str+ ) -> None:+ self.logger.debug(+ "Session file renamed: %s (new path: %s)",+ session.initialization_id,+ new_path,+ )diff --git a/marimo/_server/sessions/managers.py b/marimo/_server/sessions/managers.pyindex acf0053b1d..5114b6a11a 100644--- a/marimo/_server/sessions/managers.py+++ b/marimo/_server/sessions/managers.py@@ -115,6 +115,22 @@ class QueueManagerImpl(QueueManager):self.win32_interrupt_queue.cancel_join_thread()self.win32_interrupt_queue.close()+ def put_control_request(self, request: requests.ControlRequest) -> None:+ """Put a control request in the control queue."""+ self.control_queue.put(request)+ if isinstance(request, requests.SetUIElementValueRequest):+ self.set_ui_element_queue.put(request)++ def put_completion_request(+ self, request: requests.CodeCompletionRequest+ ) -> None:+ """Put a code completion request in the completion queue."""+ self.completion_queue.put(request)++ def put_input(self, text: str) -> None:+ """Put an input request in the input queue."""+ self.input_queue.put(text)+class KernelManagerImpl(KernelManager):def __init__(@@ -286,11 +302,11 @@ class KernelManagerImpl(KernelManager):if self.kernel_task.is_alive():# We don't join the kernel thread because we don't want to server# to block on it finishing- self.queue_manager.control_queue.put(requests.StopRequest())+ self.queue_manager.put_control_request(requests.StopRequest())else:# otherwise we have something that is `ProcessLike`if self.profile_path is not None and self.kernel_task.is_alive():- self.queue_manager.control_queue.put(requests.StopRequest())+ self.queue_manager.put_control_request(requests.StopRequest())# Hack: Wait for kernel to exit and write out profile;# joining the process hangs, but not sure why.print_(diff --git a/marimo/_server/sessions/session.py b/marimo/_server/sessions/session.pyindex f8b69f3566..0ec3c6cbe8 100644--- a/marimo/_server/sessions/session.py+++ b/marimo/_server/sessions/session.py@@ -7,12 +7,10 @@ and websocket for bidirectional communication.from __future__ import annotations-import asyncio-from typing import TYPE_CHECKING, Any, Optional+from typing import TYPE_CHECKING, Optionalfrom uuid import uuid4from marimo import _loggers-from marimo._cli.print import redfrom marimo._config.manager import MarimoConfigManager, ScriptConfigManagerfrom marimo._messaging.ops import (FocusCell,@@ -33,11 +31,14 @@ from marimo._runtime.requests import (from marimo._server.model import ConnectionState, SessionConsumer, SessionModefrom marimo._server.models.models import InstantiateRequestfrom marimo._server.notebook import AppFileManager-from marimo._server.session.serialize import (- SessionCacheKey,- SessionCacheManager,-)from marimo._server.session.session_view import SessionView+from marimo._server.sessions.events import SessionEventBus+from marimo._server.sessions.extensions import (+ CachingExtension,+ HeartbeatExtension,+ LoggingExtension,+ SessionExtension,+)from marimo._server.sessions.managers import (KernelManagerImpl,QueueManagerImpl,@@ -49,7 +50,6 @@ from marimo._server.sessions.types import (QueueManager,Session,)-from marimo._server.utils import print_, print_tabbedfrom marimo._types.ids import ConsumerIdfrom marimo._utils.distributor import (ConnectionDistributor,@@ -59,7 +59,7 @@ from marimo._utils.distributor import (from marimo._utils.repr import format_reprif TYPE_CHECKING:- from collections.abc import Callable, Mapping+ from collections.abc import MappingLOGGER = _loggers.marimo_logger()@@ -68,62 +68,6 @@ _DEFAULT_TTL_SECONDS = 120__all__ = ["Session", "SessionImpl"]-class SessionHeartbeatMonitor:- """Monitors kernel health and handles cleanup when kernel dies."""-- def __init__(- self,- kernel_manager: KernelManager,- on_kernel_died: Callable[[], None],- app_filename: Optional[str],- ) -> None:- """Initialize the heartbeat monitor.-- Args:- kernel_manager: The kernel manager to monitor- on_kernel_died: Callback to invoke when kernel dies- app_filename: Filename for error messages (optional)- """- self.kernel_manager = kernel_manager- self.on_kernel_died = on_kernel_died- self.app_filename = app_filename- self.heartbeat_task: Optional[asyncio.Task[Any]] = None-- def start(self) -> None:- """Start the heartbeat monitoring."""-- def _check_alive() -> None:- if not self.kernel_manager.is_alive():- LOGGER.debug("Kernel died, invoking cleanup callback")- self.on_kernel_died()- print_()- filename_str = self.app_filename or "unknown"- print_tabbed(- red(- "The Python kernel for file "- f"{filename_str} died unexpectedly."- )- )- print_()-- async def _heartbeat() -> None:- while True:- await asyncio.sleep(1)- _check_alive()-- try:- loop = asyncio.get_event_loop()- self.heartbeat_task = loop.create_task(_heartbeat())- except RuntimeError:- # This can happen if there is no event loop running- self.heartbeat_task = None-- def stop(self) -> None:- """Stop the heartbeat monitoring."""- if self.heartbeat_task:- self.heartbeat_task.cancel()--def _create_message_distributor(kernel_manager: KernelManager,queue_manager: QueueManager,@@ -143,8 +87,6 @@ class SessionImpl(Session):and its own websocket, for sending messages to the client."""- SESSION_CACHE_INTERVAL_SECONDS = 2-@classmethoddef create(cls,@@ -157,6 +99,7 @@ class SessionImpl(Session):config_manager: MarimoConfigManager,virtual_files_supported: bool,redirect_console_to_browser: bool,+ auto_instantiate: bool,ttl_seconds: Optional[int],) -> Session:"""@@ -183,6 +126,16 @@ class SessionImpl(Session):redirect_console_to_browser=redirect_console_to_browser,)+ extensions = [+ LoggingExtension(),+ HeartbeatExtension(),+ CachingExtension(enabled=not auto_instantiate),+ # TODO: Refactor more into extensions+ # KernelExtension()+ # RoomBroadcastExtension()+ # SessionView()+ ]+return cls(initialization_id=initialization_id,session_consumer=session_consumer,@@ -191,6 +144,7 @@ class SessionImpl(Session):app_file_manager=app_file_manager,config_manager=config_manager,ttl_seconds=ttl_seconds,+ extensions=extensions,)def __init__(@@ -202,6 +156,7 @@ class SessionImpl(Session):app_file_manager: AppFileManager,config_manager: MarimoConfigManager,ttl_seconds: Optional[int],+ extensions: list[SessionExtension],) -> None:"""Initialize kernel and client connection to it."""# This is some unique ID that we can use to identify the session@@ -216,30 +171,52 @@ class SessionImpl(Session):ttl_seconds if ttl_seconds is not None else _DEFAULT_TTL_SECONDS)self.session_view = SessionView()- self.session_cache_manager: SessionCacheManager | None = Noneself.config_manager = config_manager- self.kernel_manager.start_kernel()+ self.extensions = extensions- # Create composed objects+ self.kernel_manager.start_kernel()self.message_distributor = _create_message_distributor(kernel_manager=self.kernel_manager,queue_manager=queue_manager,)- self.heartbeat_monitor = SessionHeartbeatMonitor(- kernel_manager=self.kernel_manager,- on_kernel_died=self._on_kernel_died,- app_filename=self.app_file_manager.filename,- )+ self._event_bus = SessionEventBus()self.message_distributor.add_consumer(lambda msg: self.session_view.add_raw_operation(msg))self.connect_consumer(session_consumer, main=True)self.message_distributor.start()-- self.heartbeat_monitor.start()self._closed = False+ # Attach all extensions+ self._attach_extensions()++ def _attach_extensions(self) -> None:+ """Attach all extensions to the session."""+ for extension in self.extensions:+ try:+ extension.on_attach(self, self._event_bus)+ except Exception as e:+ LOGGER.error(+ "Error attaching extension %s: %s",+ extension,+ e,+ )+ continue++ def _detach_extensions(self) -> None:+ """Detach all extensions from the session."""+ for extension in self.extensions:+ try:+ extension.on_detach()+ except Exception as e:+ LOGGER.error(+ "Error detaching extension %s: %s",+ extension,+ e,+ )+ continue+@propertydef consumers(self) -> Mapping[SessionConsumer, ConsumerId]:"""Get the consumers in the session."""@@ -249,18 +226,9 @@ class SessionImpl(Session):"""Flush any pending messages."""self.message_distributor.flush()- def rename_path(self, new_path: str) -> None:+ async def rename_path(self, new_path: str) -> None:"""Rename the path of the session."""- if self.session_cache_manager:- self.session_cache_manager.rename_path(new_path)-- def _on_kernel_died(self) -> None:- """Callback invoked when the kernel dies unexpectedly."""- LOGGER.debug(- "Closing session %s because kernel died",- self.initialization_id,- )- self.close()+ await self._event_bus.emit_session_notebook_renamed(self, new_path)def try_interrupt(self) -> None:"""Try to interrupt the kernel."""@@ -284,9 +252,8 @@ class SessionImpl(Session):from_consumer_id: Optional[ConsumerId],) -> None:"""Put a control request in the control queue."""- self._queue_manager.control_queue.put(request)- if isinstance(request, SetUIElementValueRequest):- self._queue_manager.set_ui_element_queue.put(request)+ self._queue_manager.put_control_request(request)+# Propagate the control request to the roomif isinstance(request, (ExecuteMultipleRequest, SyncGraphRequest)):if isinstance(request, ExecuteMultipleRequest):@@ -310,6 +277,7 @@ class SessionImpl(Session):FocusCell(cell_id=cell_ids[0]),except_consumer=from_consumer_id,)+self.session_view.add_control_request(request)def put_completion_request(@@ -320,7 +288,7 @@ class SessionImpl(Session):def put_input(self, text: str) -> None:"""Put an input() request in the input queue."""- self._queue_manager.input_queue.put(text)+ self._queue_manager.put_input(text)self.session_view.add_stdin(text)def disconnect_consumer(self, session_consumer: SessionConsumer) -> None:@@ -332,7 +300,7 @@ class SessionImpl(Session):"""self.room.remove_consumer(session_consumer)- def maybe_disconnect_consumer(self) -> None:+ def disconnect_main_consumer(self) -> None:"""Disconnect the main session consumer if it connected."""@@ -375,8 +343,8 @@ class SessionImpl(Session):from_consumer_id: Optional[ConsumerId],) -> None:"""Write an operation to the session consumer and the session view."""- self.session_view.add_operation(operation)self.room.broadcast(operation, except_consumer=from_consumer_id)+ self.session_view.add_operation(operation)def close(self) -> None:"""@@ -388,13 +356,13 @@ class SessionImpl(Session):returnself._closed = True++ # Close extensions+ self._detach_extensions()# Close the roomself.room.close()# Close the kernelself.message_distributor.stop()- self.heartbeat_monitor.stop()- if self.session_cache_manager:- self.session_cache_manager.stop()self.kernel_manager.close_kernel()def instantiate(@@ -428,32 +396,6 @@ class SessionImpl(Session):from_consumer_id=None,)- def sync_session_view_from_cache(self) -> None:- """Sync the session view from a file.-- Overwrites the existing session view.- Mutates the existing session.- """- from marimo._version import __version__-- LOGGER.debug("Syncing session view from cache")- self.session_cache_manager = SessionCacheManager(- session_view=self.session_view,- path=self.app_file_manager.path,- interval=self.SESSION_CACHE_INTERVAL_SECONDS,- )-- app = self.app_file_manager.app- codes = tuple(- cell_data.code for cell_data in app.cell_manager.cell_data()- )- cell_ids = tuple(app.cell_manager.cell_ids())- key = SessionCacheKey(- codes=codes, marimo_version=__version__, cell_ids=cell_ids- )- self.session_view = self.session_cache_manager.read_session_view(key)- self.session_cache_manager.start()-def __repr__(self) -> str:return format_repr(self,diff --git a/marimo/_server/sessions/session_manager.py b/marimo/_server/sessions/session_manager.pyindex 6b0a0e38f5..42e938eee0 100644--- a/marimo/_server/sessions/session_manager.py+++ b/marimo/_server/sessions/session_manager.py@@ -9,7 +9,6 @@ file watching, and LSP server management.from __future__ import annotationsimport asyncio-import osfrom pathlib import Pathfrom typing import TYPE_CHECKING, Optional, TypeVar@@ -41,6 +40,7 @@ from marimo._server.sessions.token_manager import TokenManagerfrom marimo._server.sessions.types import KernelStatefrom marimo._server.tokens import AuthToken, SkewProtectionTokenfrom marimo._types.ids import ConsumerId, SessionId+from marimo._utils.async_path import AsyncPathfrom marimo._utils.file_watcher import FileWatcherManagerif TYPE_CHECKING:@@ -175,6 +175,7 @@ class SessionManager:session_consumer: SessionConsumer,query_params: SerializedQueryParams,file_key: MarimoFileKey,+ auto_instantiate: bool,) -> Session:"""Create a new session."""LOGGER.debug("Creating new session for id %s", session_id)@@ -211,6 +212,7 @@ class SessionManager:virtual_files_supported=True,redirect_console_to_browser=self.redirect_console_to_browser,ttl_seconds=self.ttl_seconds,+ auto_instantiate=auto_instantiate,)# Add to repository@@ -221,7 +223,7 @@ class SessionManager:return session- def handle_file_rename_for_watch(+ async def handle_file_rename_for_watch(self, session_id: SessionId, prev_path: Optional[str], new_path: str) -> tuple[bool, Optional[str]]:"""Handle renaming a file for a session.@@ -233,13 +235,13 @@ class SessionManager:if not session:return False, "Session not found"- if not os.path.exists(new_path):+ if not await AsyncPath(new_path).exists():return False, f"File {new_path} does not exist"if not session.app_file_manager.path:return False, "Session has no associated file"- session.rename_path(new_path)+ await session.rename_path(new_path)try:if self.watch and self._file_watcher_lifecycle:diff --git a/marimo/_server/sessions/types.py b/marimo/_server/sessions/types.pyindex fbc7490b9d..75d98df664 100644--- a/marimo/_server/sessions/types.py+++ b/marimo/_server/sessions/types.py@@ -42,6 +42,20 @@ class QueueManager(Protocol):"""Close all queues."""...+ def put_control_request(self, request: requests.ControlRequest) -> None:+ """Put a control request in the control queue."""+ ...++ def put_completion_request(+ self, request: requests.CodeCompletionRequest+ ) -> None:+ """Put a code completion request in the completion queue."""+ ...++ def put_input(self, text: str) -> None:+ """Put an input() request in the input queue."""+ ...+class KernelManager(Protocol):"""Protocol for kernel management."""@@ -93,9 +107,9 @@ class Session(Protocol):"""Protocol for session management."""initialization_id: str- session_view: SessionViewapp_file_manager: AppFileManagerconfig_manager: MarimoConfigManager+ session_view: SessionViewttl_seconds: int@property@@ -119,7 +133,7 @@ class Session(Protocol):"""Flush any pending messages."""...- def rename_path(self, new_path: str) -> None:+ async def rename_path(self, new_path: str) -> None:"""Rename the path of the session."""...@@ -145,7 +159,7 @@ class Session(Protocol):"""Stop the session consumer but keep the kernel running."""...- def maybe_disconnect_consumer(self) -> None:+ def disconnect_main_consumer(self) -> None:"""Disconnect the main session consumer if it connected."""...@@ -155,10 +169,6 @@ class Session(Protocol):"""Connect or resume the session with a new consumer."""...- def get_current_state(self) -> SessionView:- """Return the current state of the session."""- ...-def connection_state(self) -> ConnectionState:"""Return the connection state of the session."""...@@ -180,10 +190,6 @@ class Session(Protocol):"""Instantiate the app."""...- def sync_session_view_from_cache(self) -> None:- """Sync the session view from cache."""- ...-def close(self) -> None:"""Close the session."""...diff --git a/tests/_ai/tools/test_base.py b/tests/_ai/tools/test_base.pyindex 3afd748534..081b26fbe8 100644--- a/tests/_ai/tools/test_base.py+++ b/tests/_ai/tools/test_base.py@@ -148,11 +148,13 @@ def test_get_notebook_errors_orders_by_cell_manager():# Mock session with cells c1, c2, c3session = Mock()- session.session_view.cell_operations = {+ session_view = Mock()+ session_view.cell_operations = {CellId_t("c1"): error_op,CellId_t("c2"): error_op,CellId_t("c3"): error_op,}+ session.session_view = session_view# Cell manager returns in order: c3, c2, c1 (not alphabetical)cell_data = [diff --git a/tests/_ai/tools/test_utils.py b/tests/_ai/tools/test_utils.pynew file mode 100644index 0000000000..07f1eaee34--- /dev/null+++ b/tests/_ai/tools/test_utils.py@@ -0,0 +1,36 @@+# Copyright 2025 Marimo. All rights reserved.+"""Shared fixtures and mocks for AI tools tests."""++from __future__ import annotations++from dataclasses import dataclass+from typing import Any+++@dataclass+class MockSessionView:+ """Mock session view for testing."""++ cell_operations: dict | None = None+ last_execution_time: dict | None = None+ variable_values: dict | None = None+ data_connectors: Any = None++ def __post_init__(self) -> None:+ if self.cell_operations is None:+ self.cell_operations = {}+ if self.last_execution_time is None:+ self.last_execution_time = {}+ if self.variable_values is None:+ self.variable_values = {}+++@dataclass+class MockSession:+ """Mock session for testing."""++ _session_view: MockSessionView++ @property+ def session_view(self) -> MockSessionView:+ return self._session_viewdiff --git a/tests/_ai/tools/tools/test_cells.py b/tests/_ai/tools/tools/test_cells.pyindex 02137bc3a6..7e925b4a54 100644--- a/tests/_ai/tools/tools/test_cells.py+++ b/tests/_ai/tools/tools/test_cells.py@@ -14,8 +14,8 @@ from marimo._ai._tools.tools.cells import (GetLightweightCellMap,)from marimo._messaging.ops import VariableValue-from marimo._server.sessions import Sessionfrom marimo._types.ids import CellId_t, SessionId+from tests._ai.tools.test_utils import MockSession, MockSessionView@dataclass@@ -48,26 +48,6 @@ class MockError:return self._message-@dataclass-class MockSessionView:- cell_operations: dict | None = None- last_execution_time: dict | None = None- variable_values: dict | None = None-- def __post_init__(self) -> None:- if self.cell_operations is None:- self.cell_operations = {}- if self.last_execution_time is None:- self.last_execution_time = {}- if self.variable_values is None:- self.variable_values = {}---@dataclass-class MockSession(Session):- session_view: MockSessionView--def test_is_markdown_cell():tool = GetLightweightCellMap(ToolContext())assert tool._is_markdown_cell('mo.md("hi")') is True@@ -78,7 +58,7 @@ def test_get_cell_metadata_basic():tool = GetCellRuntimeData(ToolContext())cell_op = MockCellOp(status="idle")session = MockSession(- MockSessionView(+ _session_view=MockSessionView(cell_operations={"c1": cell_op}, last_execution_time={"c1": 42.5}))@@ -91,7 +71,7 @@ def test_get_cell_metadata_basic():def test_get_cell_metadata_no_cell_op():tool = GetCellRuntimeData(ToolContext())- session = MockSession(MockSessionView())+ session = MockSession(_session_view=MockSessionView())result = tool._get_cell_metadata(session, CellId_t("missing"))assert result == CellRuntimeMetadata(@@ -112,7 +92,9 @@ def test_get_cell_variables():var_z = VariableValue("z", "[1]", "list")session = MockSession(- MockSessionView(variable_values={"x": var_x, "y": var_y, "z": var_z})+ _session_view=MockSessionView(+ variable_values={"x": var_x, "y": var_y, "z": var_z}+ ))result = tool._get_cell_variables(session, cell_data)diff --git a/tests/_ai/tools/tools/test_datasource_tool.py b/tests/_ai/tools/tools/test_datasource_tool.pyindex 73357dcffd..c265a25d81 100644--- a/tests/_ai/tools/tools/test_datasource_tool.py+++ b/tests/_ai/tools/tools/test_datasource_tool.py@@ -17,6 +17,7 @@ from marimo._ai._tools.utils.exceptions import ToolExecutionErrorfrom marimo._data.models import Database, DataTable, DataTableColumn, Schemafrom marimo._messaging.ops import DataSourceConnectionsfrom marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINE+from tests._ai.tools.test_utils import MockSession, MockSessionView@dataclass@@ -28,16 +29,6 @@ class MockDataSourceConnection:default_schema: Optional[str] = None-@dataclass-class MockSessionView:- data_connectors: DataSourceConnections---@dataclass-class MockSession:- session_view: MockSessionView--@pytest.fixturedef tool() -> GetDatabaseTables:"""Create a GetDatabaseTables tool instance."""@@ -97,7 +88,7 @@ def sample_connection(sample_database: Database) -> MockDataSourceConnection:def sample_session(sample_connection: MockDataSourceConnection) -> MockSession:"""Sample session with data connectors."""return MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[sample_connection])@@ -154,7 +145,7 @@ def multi_table_session() -> MockSession:)return MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[connection])))@@ -271,7 +262,7 @@ def test_get_tables_with_schema_match(def test_get_tables_empty_connections(tool: GetDatabaseTables):"""Test getting tables when no connections exist."""empty_session = MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[])))@@ -378,7 +369,7 @@ def test_multiple_connections(tool: GetDatabaseTables):)multi_conn_session = MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[conn1, conn2])))@@ -444,7 +435,7 @@ def test_query_matches_multiple_levels(tool: GetDatabaseTables):)session = MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[connection])))@@ -528,7 +519,7 @@ def test_query_no_duplicates(tool: GetDatabaseTables):)session = MockSession(- session_view=MockSessionView(+ _session_view=MockSessionView(data_connectors=DataSourceConnections(connections=[connection])))diff --git a/tests/_server/test_utils.py b/tests/_server/test_server_utils.pysimilarity index 96%rename from tests/_server/test_utils.pyrename to tests/_server/test_server_utils.pyindex 93ebfd380a..8a823a2641 100644--- a/tests/_server/test_utils.py+++ b/tests/_server/test_server_utils.py@@ -4,7 +4,6 @@ from __future__ import annotationsfrom marimo._server.api.utils import parse_title-# The function to be testeddef test_parse_title() -> None:assert parse_title(None) == "marimo"assert parse_title("example_file.txt") == "example file"diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.pyindex cdeef70fc8..93a79305bd 100644--- a/tests/_server/test_session_manager.py+++ b/tests/_server/test_session_manager.py@@ -81,6 +81,7 @@ async def test_create_session_new(mock_session_consumer,query_params={},file_key=AppFileRouter.NEW_FILE,+ auto_instantiate=False,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is session@@ -98,6 +99,7 @@ async def test_create_session_absolute_url(mock_session_consumer,query_params={},file_key=temp_marimo_file,+ auto_instantiate=False,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is session@@ -241,6 +243,7 @@ async def test_create_session_with_script_config_overrides(mock_session_consumer,query_params={},file_key=str(tmp_path / "test.py"),+ auto_instantiate=False,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is sessiondiff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.pyindex 4f569cdee0..834557ae96 100644--- a/tests/_server/test_sessions.py+++ b/tests/_server/test_sessions.py@@ -294,6 +294,7 @@ async def test_session() -> None:AppFileManager.from_app(InternalApp(App())),get_default_config_manager(current_path=None),ttl_seconds=None,+ extensions=[],)# Assert startup@@ -341,6 +342,7 @@ def test_session_disconnect_reconnect() -> None:app_file_manager=AppFileManager.from_app(InternalApp(App())),config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,+ extensions=[],)# Assert startup@@ -399,6 +401,7 @@ def test_session_with_kiosk_consumers() -> None:app_file_manager=AppFileManager.from_app(InternalApp(App())),config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,+ extensions=[],)# Assert startup@@ -497,6 +500,7 @@ def __():session_consumer=session_consumer,query_params={},file_key=file_key,+ auto_instantiate=False,)# Wait a loop to ensure the session is created@@ -539,6 +543,7 @@ def __():session_consumer=session_consumer2,query_params={},file_key=file_key,+ auto_instantiate=False,)# Modify the file again@@ -717,6 +722,7 @@ def __():session_consumer=session_consumer,query_params={},file_key=str(tmp_path),+ auto_instantiate=False,)session.session_view = MagicMock(SessionView)@@ -862,10 +868,11 @@ def __():session_consumer=session_consumer,query_params={},file_key=str(tmp_path1),+ auto_instantiate=False,)# Try to rename to a non-existent file- success, error = session_manager.handle_file_rename_for_watch(+ success, error = await session_manager.handle_file_rename_for_watch(session_id, str(tmp_path1), "/nonexistent/file.py")assert not success@@ -873,7 +880,7 @@ def __():assert "does not exist" in error# Try to rename with an invalid session- success, error = session_manager.handle_file_rename_for_watch(+ success, error = await session_manager.handle_file_rename_for_watch("nonexistent", str(tmp_path1), str(new_path))assert not success@@ -885,7 +892,7 @@ def __():assert session is not Nonesession.app_file_manager.rename(str(new_path))assert new_path.exists()- success, error = session_manager.handle_file_rename_for_watch(+ success, error = await session_manager.handle_file_rename_for_watch(session_id, str(tmp_path1), str(new_path))assert success@@ -923,6 +930,7 @@ def __():@save_and_restore_main[email protected](reason="Caching functionality moved to CachingExtension")async def test_sync_session_view_from_cache(tmp_path: Path) -> None:"""Test syncing session view from cache."""# Create a temporary file@@ -965,6 +973,7 @@ def __():app_file_manager=app_file_manager,config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,+ extensions=[],)# Test syncing when no cache exists@@ -997,6 +1006,7 @@ def __():app_file_manager=app_file_manager,config_manager=get_default_config_manager(current_path=None),ttl_seconds=None,+ extensions=[],)session2.sync_session_view_from_cache()@@ -1067,6 +1077,7 @@ def test_session_with_script_config_overrides(virtual_files_supported=True,redirect_console_to_browser=False,ttl_seconds=None,+ auto_instantiate=True,)# Verify that the session's config is affected by the script config