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
refactor: SessionConsumer as a SessionExtension (#7570)
Myles Scolnick5b14c1875363
diff --git a/marimo/_ai/_tools/base.py b/marimo/_ai/_tools/base.pyindex e222086196..975fff0fa6 100644--- a/marimo/_ai/_tools/base.py+++ b/marimo/_ai/_tools/base.py@@ -36,7 +36,6 @@ from marimo._server.ai.tools.types import ()from marimo._server.api.deps import AppStateBasefrom marimo._server.model import ConnectionState-from marimo._server.sessions import Session, SessionManagerfrom marimo._types.ids import CellId_t, SessionIdfrom marimo._utils.case import to_snake_casefrom marimo._utils.dataclass_to_openapi import PythonTypeToOpenAPI@@ -55,6 +54,8 @@ if TYPE_CHECKING:from starlette.applications import Starlette+ from marimo._server.sessions import Session, SessionManager+@dataclassclass ToolContext:diff --git a/marimo/_ai/_tools/tools/datasource.py b/marimo/_ai/_tools/tools/datasource.pyindex ed8f47da91..e36f3921c9 100644--- a/marimo/_ai/_tools/tools/datasource.py+++ b/marimo/_ai/_tools/tools/datasource.py@@ -3,20 +3,22 @@from __future__ import annotationsfrom dataclasses import dataclass, field-from typing import Optional+from typing import TYPE_CHECKING, Optionalfrom marimo import _loggersfrom marimo._ai._tools.base import ToolBasefrom marimo._ai._tools.types import SuccessResult, ToolGuidelinesfrom marimo._ai._tools.utils.exceptions import ToolExecutionErrorfrom marimo._data.models import DataTable-from marimo._server.sessions import Sessionfrom marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINEfrom marimo._types.ids import SessionIdfrom marimo._utils.fuzzy_match import compile_regex, is_fuzzy_matchLOGGER = _loggers.marimo_logger()+if TYPE_CHECKING:+ from marimo._server.sessions import Session+@dataclassclass GetDatabaseTablesArgs:diff --git a/marimo/_server/api/deps.py b/marimo/_server/api/deps.pyindex 3547467ae2..a899a8b1b7 100644--- a/marimo/_server/api/deps.py+++ b/marimo/_server/api/deps.py@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Any, Optional, Union, castfrom marimo import _loggers as loggersfrom marimo._config.manager import MarimoConfigManager, ScriptConfigManagerfrom marimo._server.model import SessionMode-from marimo._server.sessions import Session, SessionManager+from marimo._server.sessions import SessionManagerfrom marimo._server.tokens import SkewProtectionTokenfrom marimo._types.ids import SessionId@@ -18,6 +18,8 @@ if TYPE_CHECKING:from starlette.websockets import WebSocketfrom uvicorn import Server+ from marimo._server.sessions import Session+LOGGER = loggers.marimo_logger()diff --git a/marimo/_server/api/endpoints/editing.py b/marimo/_server/api/endpoints/editing.pyindex e400f24fe6..cb4d03ddea 100644--- a/marimo/_server/api/endpoints/editing.py+++ b/marimo/_server/api/endpoints/editing.py@@ -96,7 +96,7 @@ async def sync_cell_ids(request: Request) -> BaseResponse:app_state = AppState(request)body = await parse_request(request, cls=UpdateCellIdsRequest)session_id = app_state.require_current_session_id()- app_state.require_current_session().write_operation(+ app_state.require_current_session().notify(body, from_consumer_id=ConsumerId(session_id))return SuccessResponse()diff --git a/marimo/_server/api/endpoints/execution.py b/marimo/_server/api/endpoints/execution.pyindex f3c8a8b1da..ae7e21fcb9 100644--- a/marimo/_server/api/endpoints/execution.py+++ b/marimo/_server/api/endpoints/execution.py@@ -367,7 +367,7 @@ async def takeover_endpoint()if existing_session is not None:# Send a disconnect message to the client- existing_session.write_operation(+ existing_session.notify(Alert(title="Session taken over",description="Another user has taken over this session.",diff --git a/marimo/_server/api/endpoints/file_explorer.py b/marimo/_server/api/endpoints/file_explorer.pyindex 07a9ed5543..51696ba238 100644--- a/marimo/_server/api/endpoints/file_explorer.py+++ b/marimo/_server/api/endpoints/file_explorer.py@@ -222,7 +222,7 @@ async def update_file(# Handle marimo notebook reload if there's an active sessionsession_manager = app_state.session_manager- await session_manager.handle_file_change(body.path)+ await session_manager.trigger_file_change(body.path)return FileUpdateResponse(success=True, info=info)except Exception as e:diff --git a/marimo/_server/api/endpoints/files.py b/marimo/_server/api/endpoints/files.pyindex 6498d5e193..1057d370c2 100644--- a/marimo/_server/api/endpoints/files.py+++ b/marimo/_server/api/endpoints/files.py@@ -85,27 +85,15 @@ async def rename_file("""body = await parse_request(request, cls=RenameFileRequest)app_state = AppState(request)- session = app_state.require_current_session()- prev_path = session.app_file_manager.path-- session.app_file_manager.rename(body.filename)- new_path = session.app_file_manager.path-- if prev_path and new_path:- app_state.session_manager.recents.rename(prev_path, new_path)- elif new_path:- app_state.session_manager.recents.touch(new_path)app_state.require_current_session().put_control_request(body.as_execution_request(),from_consumer_id=ConsumerId(app_state.require_current_session_id()),)- if new_path:- # Handle rename for watch- await app_state.session_manager.handle_file_rename_for_watch(- app_state.require_current_session_id(), prev_path, new_path- )+ await app_state.session_manager.rename_session(+ app_state.require_current_session_id(), body.filename+ )return SuccessResponse()diff --git a/marimo/_server/api/endpoints/ws.py b/marimo/_server/api/endpoints/ws.pyindex a38be41a97..ebf094349d 100644--- a/marimo/_server/api/endpoints/ws.py+++ b/marimo/_server/api/endpoints/ws.py@@ -31,15 +31,16 @@ from marimo._runtime.params import QueryParamsfrom marimo._server.api.auth import validate_authfrom marimo._server.api.deps import AppStatefrom marimo._server.codes import WebSocketCodes+from marimo._server.consumer import SessionConsumerfrom marimo._server.file_router import MarimoFileKeyfrom marimo._server.model import (ConnectionState,- SessionConsumer,SessionMode,)from marimo._server.router import APIRouterfrom marimo._server.rtc.doc import LoroDocManagerfrom marimo._server.sessions import Session, SessionManager+from marimo._server.sessions.events import SessionEventBusfrom marimo._types.ids import CellId_t, ConsumerId, SessionIdif TYPE_CHECKING:@@ -279,6 +280,12 @@ class WebsocketHandler(SessionConsumer):def consumer_id(self) -> ConsumerId:return self._consumer_id+ def notify(self, notification: KernelMessage) -> None:+ self.message_queue.put_nowait(notification)++ def _serialize_and_notify(self, notification: MessageOperation) -> None:+ self.notify(serialize_kernel_message(notification))+def _write_kernel_ready(self,session: Session,@@ -341,25 +348,21 @@ class WebsocketHandler(SessionConsumer):last_executed_code = {}last_execution_time = {}- self.message_queue.put_nowait(- (- serialize_kernel_message(- KernelReady(- codes=codes,- names=names,- configs=configs,- layout=file_manager.read_layout_config(),- cell_ids=cell_ids,- resumed=resumed,- ui_values=ui_values,- last_executed_code=last_executed_code,- last_execution_time=last_execution_time,- app_config=app.config,- kiosk=kiosk,- capabilities=KernelCapabilities(),- )- )- ),+ self._serialize_and_notify(+ KernelReady(+ codes=codes,+ names=names,+ configs=configs,+ layout=file_manager.read_layout_config(),+ cell_ids=cell_ids,+ resumed=resumed,+ ui_values=ui_values,+ last_executed_code=last_executed_code,+ last_execution_time=last_execution_time,+ app_config=app.config,+ kiosk=kiosk,+ capabilities=KernelCapabilities(),+ ))def _reconnect_session(self, session: Session, replay: bool) -> None:@@ -376,11 +379,11 @@ class WebsocketHandler(SessionConsumer):session.connect_consumer(self, main=True)# Write reconnected message- self.write_operation(Reconnected())+ self._serialize_and_notify(Reconnected())# If not replaying, just send a toastif not replay:- self.write_operation(+ self._serialize_and_notify(Alert(title="Reconnected",description="You have reconnected to an existing session.",@@ -396,7 +399,7 @@ class WebsocketHandler(SessionConsumer):last_execution_time=session.session_view.last_execution_time,kiosk=self.kiosk,)- self.write_operation(+ self._serialize_and_notify(Banner(title="Reconnected",description="You have reconnected to an existing session.",@@ -442,7 +445,7 @@ class WebsocketHandler(SessionConsumer):LOGGER.debug(f"Replaying {len(operations)} operations")for op in operations:LOGGER.debug("Replaying operation %s", op)- self.write_operation(op)+ self._serialize_and_notify(op)def _on_disconnect(self,@@ -691,27 +694,17 @@ class WebsocketHandler(SessionConsumer):LOGGER.debug("Websocket terminated with CancelledError")pass- def on_start(- self,- ) -> Callable[[KernelMessage], None]:- def listener(response: KernelMessage) -> None:- self.message_queue.put_nowait(response)-- return listener-- def write_operation(self, op: MessageOperation) -> None:- self.message_queue.put_nowait(serialize_kernel_message(op))-- def on_stop(self) -> None:- # Cancel the heartbeat task, reader- if self.heartbeat_task and not self.heartbeat_task.cancelled():- self.heartbeat_task.cancel()+ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ del session+ del event_bus+ def on_detach(self) -> None:# If the websocket is open, send a close message- if (+ is_connected = (self.status == ConnectionState.OPENor self.status == ConnectionState.CONNECTING- ) and self.websocket.application_state is WebSocketState.CONNECTED:+ ) and self.websocket.application_state is WebSocketState.CONNECTED+ if is_connected:asyncio.create_task(self.websocket.close(WebSocketCodes.NORMAL_CLOSE, "MARIMO_SHUTDOWN"@@ -758,7 +751,9 @@ class WebsocketHandler(SessionConsumer):)description += notices_text- self.write_operation(Alert(title=title, description=description))+ self._serialize_and_notify(+ Alert(title=title, description=description)+ )check_for_updates(on_update)diff --git a/marimo/_server/consumer.py b/marimo/_server/consumer.pynew file mode 100644index 0000000000..f71ab53717--- /dev/null+++ b/marimo/_server/consumer.py@@ -0,0 +1,30 @@+from __future__ import annotations++from abc import ABC, abstractmethod+from typing import TYPE_CHECKING++from marimo._server.sessions.extensions.types import SessionExtension+from marimo._types.ids import ConsumerId++if TYPE_CHECKING:+ from marimo._messaging.types import KernelMessage+ from marimo._server.model import ConnectionState+++class SessionConsumer(ABC, SessionExtension):+ """+ Consumer for a session. This extends the SessionExtension interface.++ This allows us to communicate with a session via different+ connection types.+ """++ @property+ @abstractmethod+ def consumer_id(self) -> ConsumerId: ...++ @abstractmethod+ def notify(self, notification: KernelMessage) -> None: ...++ @abstractmethod+ def connection_state(self) -> ConnectionState: ...diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 5ef15b277f..fc2b61c212 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -5,7 +5,7 @@ import asyncioimport osimport sysfrom dataclasses import dataclass-from typing import TYPE_CHECKING, Callable, Literal, Optional, cast+from typing import TYPE_CHECKING, Literal, Optional, castfrom marimo import _loggersfrom marimo._cli.print import echo@@ -18,7 +18,6 @@ from marimo._messaging.errors import Error, is_unexpected_errorfrom marimo._messaging.ops import (CellOp,CompletedRun,- MessageOperation,deserialize_kernel_message,)from marimo._messaging.types import KernelMessage@@ -26,11 +25,10 @@ from marimo._output.hypertext import patch_html_for_non_interactive_outputfrom marimo._runtime.requests import AppMetadata, SerializedCLIArgsfrom marimo._server.export.exporter import Exporterfrom marimo._server.file_router import AppFileRouter-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.model import ConnectionState, SessionModefrom marimo._server.models.export import ExportAsHTMLRequestfrom marimo._server.models.models import InstantiateRequestfrom marimo._server.notebook import AppFileManager-from marimo._server.session.session_view import SessionViewfrom marimo._types.ids import ConsumerIdfrom marimo._utils.marimo_path import MarimoPath@@ -39,6 +37,9 @@ LOGGER = _loggers.marimo_logger()if TYPE_CHECKING:from pathlib import Path+ from marimo._server.session.session_view import SessionView+ from marimo._server.sessions.types import Session+@dataclassclass ExportResult:@@ -237,11 +238,13 @@ async def run_app_until_completion(cli_args: SerializedCLIArgs,argv: list[str] | None,) -> tuple[SessionView, bool]:+ from marimo._server.consumer import SessionConsumer+ from marimo._server.sessions.events import SessionEventBusfrom marimo._server.sessions.session import SessionImplinstantiated_event = asyncio.Event()- class DefaultSessionConsumer(SessionConsumer):+ class RunUntilCompletionSessionConsumer(SessionConsumer):def __init__(self) -> None:self.did_error = False@@ -249,53 +252,50 @@ async def run_app_until_completion(def consumer_id(self) -> ConsumerId:return ConsumerId("default")- def on_start(- self,- ) -> Callable[[KernelMessage], None]:- def listener(message: KernelMessage) -> None:- data = deserialize_kernel_message(message)- # Print errors to stderr- if isinstance(data, CellOp):- output = data.output- console_output = data.console- if output and output.channel == CellChannel.MARIMO_ERROR:- errors = cast(list[Error], output.data)- for err in errors:- # Not all errors are fatal- if is_unexpected_error(err):- echo(- f"{err.__class__.__name__}: {err.describe()}",- file=sys.stderr,- )- self.did_error = True-- if console_output:- console_as_list: list[CellOutput] = (- console_output- if isinstance(console_output, list)- else [console_output]- )- try:- for line in console_as_list:- # We print to stderr to not interfere with the- # piped output- mimetype = line.mimetype- if mimetype == "text/plain":- echo(line.data, file=sys.stderr, nl=False)- except Exception:- LOGGER.warning("Error printing console output")- pass-- if isinstance(data, CompletedRun):- instantiated_event.set()-- return listener-- def on_stop(self) -> None:- pass-- def write_operation(self, op: MessageOperation) -> None:- pass+ def notify(self, notification: KernelMessage) -> None:+ data = deserialize_kernel_message(notification)+ # Print errors to stderr+ if isinstance(data, CellOp):+ output = data.output+ console_output = data.console+ if output and output.channel == CellChannel.MARIMO_ERROR:+ errors = cast(list[Error], output.data)+ for err in errors:+ # Not all errors are fatal+ if is_unexpected_error(err):+ echo(+ f"{err.__class__.__name__}: {err.describe()}",+ file=sys.stderr,+ )+ self.did_error = True++ if console_output:+ console_as_list: list[CellOutput] = (+ console_output+ if isinstance(console_output, list)+ else [console_output]+ )+ try:+ for line in console_as_list:+ # We print to stderr to not interfere with the+ # piped output+ mimetype = line.mimetype+ if mimetype == "text/plain":+ echo(line.data, file=sys.stderr, nl=False)+ except Exception:+ LOGGER.warning("Error printing console output")++ if isinstance(data, CompletedRun):+ instantiated_event.set()++ def on_attach(+ self, session: Session, event_bus: SessionEventBus+ ) -> None:+ del session+ del event_bus++ def on_detach(self) -> None:+ return Nonedef connection_state(self) -> ConnectionState:return ConnectionState.OPEN@@ -319,7 +319,7 @@ async def run_app_until_completion()# Create a session- session_consumer = DefaultSessionConsumer()+ session_consumer = RunUntilCompletionSessionConsumer()session = SessionImpl.create(# Any initialization ID will doinitialization_id="_any_",diff --git a/marimo/_server/model.py b/marimo/_server/model.pyindex 0fea99e85f..49e62fa7e3 100644--- a/marimo/_server/model.py+++ b/marimo/_server/model.py@@ -2,13 +2,6 @@from __future__ import annotationsfrom enum import Enum-from typing import TYPE_CHECKING, Callable, Protocol--from marimo._types.ids import ConsumerId--if TYPE_CHECKING:- from marimo._messaging.ops import MessageOperation- from marimo._messaging.types import KernelMessageclass ConnectionState(Enum):@@ -27,30 +20,3 @@ class SessionMode(str, Enum):EDIT = "edit"# read-onlyRUN = "run"---class SessionConsumer(Protocol):- """- Consumer for a session-- This allows use to communicate with a session via different- connection types. Currently we consume a session via WebSocket- """-- @property- def consumer_id(self) -> ConsumerId: ...-- def on_start(- self,- ) -> Callable[[KernelMessage], None]:- """- Start the session consumer- and return a subscription function for the session consumer- """- ...-- def on_stop(self) -> None: ...-- def write_operation(self, op: MessageOperation) -> None: ...-- def connection_state(self) -> ConnectionState: ...diff --git a/marimo/_server/notebook/file_manager.py b/marimo/_server/notebook/file_manager.pyindex 9954a55df0..8eb713333f 100644--- a/marimo/_server/notebook/file_manager.py+++ b/marimo/_server/notebook/file_manager.py@@ -228,19 +228,22 @@ class AppFileManager:result.cell_manager.ensure_one_cell()return result- def rename(self, new_filename: str | Path) -> None:+ def rename(self, new_filename: str | Path) -> str:"""Rename the notebook file.Args:new_filename: New filename (will be canonicalized)+ Returns:+ The filename of the new file.+Raises:HTTPException: If rename fails or target exists"""new_path = Path(canonicalize_filename(str(new_filename)))if self._is_same_path(new_path):- return+ return new_path.nameself._assert_path_does_not_exist(new_path)@@ -265,6 +268,8 @@ class AppFileManager:previous_path=previous_filename,)+ return new_path.name+def read_layout_config(self) -> Optional[LayoutConfig]:"""Read layout configuration file.diff --git a/marimo/_server/sessions/events.py b/marimo/_server/sessions/events.pyindex d4ad00af85..51fa343c7b 100644--- a/marimo/_server/sessions/events.py+++ b/marimo/_server/sessions/events.py@@ -9,6 +9,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKINGfrom marimo import _loggers+from marimo._messaging.types import KernelMessagefrom marimo._types.ids import SessionIdif TYPE_CHECKING:@@ -42,11 +43,19 @@ class SessionEventListener:return Noneasync def on_session_notebook_renamed(- self, session: Session, new_path: str+ self, session: Session, old_path: str | None) -> None:"""Called when a session notebook is renamed."""del session- del new_path+ del old_path+ return None++ def on_notification_sent(+ self, session: Session, notification: KernelMessage+ ) -> None:+ """Called when a notification is emitted by a session."""+ del session+ del notificationreturn None@@ -110,12 +119,12 @@ class SessionEventBus:continueasync def emit_session_notebook_renamed(- self, session: Session, new_path: str+ self, session: Session, old_path: str | None) -> None:"""Emit a session renamed event."""for listener in self._listeners:try:- await listener.on_session_notebook_renamed(session, new_path)+ await listener.on_session_notebook_renamed(session, old_path)except Exception as e:LOGGER.error("Error handling session notebook renamed event for listener %s: %s",@@ -123,3 +132,18 @@ class SessionEventBus:e,)continue++ def emit_notification_sent(+ self, session: Session, notification: KernelMessage+ ) -> None:+ """Emit a notification sent event."""+ for listener in self._listeners:+ try:+ listener.on_notification_sent(session, notification)+ except Exception as e:+ LOGGER.error(+ "Error handling notification sent event for listener %s: %s",+ listener,+ e,+ )+ continuediff --git a/marimo/_server/sessions/extensions/__init__.py b/marimo/_server/sessions/extensions/__init__.pynew file mode 100644index 0000000000..be52407b50--- /dev/null+++ b/marimo/_server/sessions/extensions/__init__.py@@ -0,0 +1 @@+# Copyright 2024 Marimo. All rights reserved.diff --git a/marimo/_server/sessions/extensions.py b/marimo/_server/sessions/extensions/extensions.pysimilarity index 68%rename from marimo/_server/sessions/extensions.pyrename to marimo/_server/sessions/extensions/extensions.pyindex 43a0606df1..fa4f498f86 100644--- a/marimo/_server/sessions/extensions.py+++ b/marimo/_server/sessions/extensions/extensions.py@@ -8,49 +8,41 @@ Extensions provide a way to add cross-cutting concerns to sessionsfrom __future__ import annotationsimport asyncio-from typing import TYPE_CHECKING, Optional, Protocol+from typing import TYPE_CHECKING, Optionalfrom marimo import _loggersfrom marimo._cli.print import red+from marimo._messaging.types import KernelMessagefrom marimo._server.session.serialize import (SessionCacheKey,SessionCacheManager,)-from marimo._server.sessions.events import (- SessionEventBus,- SessionEventListener,+from marimo._server.sessions.events import SessionEventListener+from marimo._server.sessions.extensions.types import SessionExtension+from marimo._server.sessions.types import (+ KernelManager,+ KernelState,+ QueueManager,)-from marimo._server.sessions.types import KernelStatefrom marimo._server.utils import print_, print_tabbed-from marimo._types.ids import SessionId+from marimo._utils.distributor import (+ ConnectionDistributor,+ Distributor,+ QueueDistributor,+)if TYPE_CHECKING:+ from logging import Logger++ from marimo._server.sessions.events import (+ SessionEventBus,+ )from marimo._server.sessions.types import Session+ from marimo._types.ids import SessionIdLOGGER = _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.@@ -128,6 +120,7 @@ class CachingExtension(SessionExtension, SessionEventListener):self.interval = intervalself.enabled = enabledself.session_cache_manager: Optional[SessionCacheManager] = None+ self.event_bus: Optional[SessionEventBus] = Nonedef on_attach(self, session: Session, event_bus: SessionEventBus) -> None:"""Initialize cache manager when attached to session."""@@ -136,6 +129,7 @@ class CachingExtension(SessionExtension, SessionEventListener):# Subscribe to events (like rename)event_bus.subscribe(self)+ self.event_bus = event_busfrom marimo._version import __version__@@ -165,14 +159,18 @@ class CachingExtension(SessionExtension, SessionEventListener):def on_detach(self) -> None:"""Stop cache manager when detached."""self._stop()+ if self.event_bus:+ self.event_bus.unsubscribe(self)+ self.event_bus = Noneasync def on_session_notebook_renamed(- self, session: Session, new_path: str+ self, session: Session, old_path: str | None) -> None:"""Rename the path for the cache manager."""- del session- if self.session_cache_manager:- self.session_cache_manager.rename_path(new_path)+ del old_path+ path = session.app_file_manager.path+ if self.session_cache_manager and path:+ self.session_cache_manager.rename_path(path)return Nonedef _stop(self) -> None:@@ -182,19 +180,65 @@ class CachingExtension(SessionExtension, SessionEventListener):self.session_cache_manager = None+class NotificationListenerExtension(SessionExtension):+ """Extension for listening to notifications from the kernel and forwarding them to the session."""++ def __init__(+ self, kernel_manager: KernelManager, queue_manager: QueueManager+ ) -> None:+ self.kernel_manager = kernel_manager+ self.queue_manager = queue_manager+ self.distributor: Optional[Distributor[KernelMessage]] = None++ def _create_distributor(+ self,+ kernel_manager: KernelManager,+ queue_manager: QueueManager,+ ) -> Distributor[KernelMessage]:+ from marimo._server.model import SessionMode++ if kernel_manager.mode == SessionMode.EDIT:+ return ConnectionDistributor(kernel_manager.kernel_connection)+ else:+ q = queue_manager.stream_queue+ assert q is not None+ return QueueDistributor(queue=q)++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ del event_bus+ self.distributor = self._create_distributor(+ kernel_manager=self.kernel_manager,+ queue_manager=self.queue_manager,+ )+ self.distributor.add_consumer(+ lambda msg: session.notify(msg, from_consumer_id=None)+ )+ self.distributor.start()++ def on_detach(self) -> None:+ if self.distributor is not None:+ self.distributor.stop()+ self.distributor = None++class LoggingExtension(SessionExtension, SessionEventListener):"""Extension for logging session events."""- def __init__(self) -> None:- self.logger = LOGGER+ def __init__(self, logger: Logger = LOGGER) -> None:+ self.logger = logger+ self.event_bus: Optional[SessionEventBus] = Nonedef on_attach(self, session: Session, event_bus: SessionEventBus) -> None:del sessionself.logger.debug("Attaching extensions")event_bus.subscribe(self)+ self.event_bus = event_busdef on_detach(self) -> None:self.logger.debug("Detaching extensions")+ if self.event_bus:+ self.event_bus.unsubscribe(self)+ self.event_bus = Noneasync def on_session_created(self, session: Session) -> None:self.logger.debug("Session created: %s", session.initialization_id)@@ -212,10 +256,10 @@ class LoggingExtension(SessionExtension, SessionEventListener):)async def on_session_notebook_renamed(- self, session: Session, new_path: str+ self, session: Session, old_path: str | None) -> None:self.logger.debug("Session file renamed: %s (new path: %s)",session.initialization_id,- new_path,+ old_path,)diff --git a/marimo/_server/sessions/extensions/types.py b/marimo/_server/sessions/extensions/types.pynew file mode 100644index 0000000000..9f203b8b82--- /dev/null+++ b/marimo/_server/sessions/extensions/types.py@@ -0,0 +1,28 @@+from __future__ import annotations++from typing import TYPE_CHECKING, Protocol++if TYPE_CHECKING:+ from marimo._server.sessions.events import SessionEventBus+ from marimo._server.sessions.session import Session+++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)."""+ ...diff --git a/marimo/_server/sessions/file_change_handler.py b/marimo/_server/sessions/file_change_handler.pyindex 272510303b..97e760c5f9 100644--- a/marimo/_server/sessions/file_change_handler.py+++ b/marimo/_server/sessions/file_change_handler.py@@ -15,7 +15,6 @@ from marimo._config.manager import MarimoConfigManagerfrom marimo._messaging.ops import Reload, UpdateCellCodes, UpdateCellIdsRequestfrom marimo._runtime.requests import DeleteCellRequest, SyncGraphRequestfrom marimo._server.model import SessionMode-from marimo._server.sessions.session import Sessionfrom marimo._types.ids import CellId_tfrom marimo._utils import async_path@@ -24,6 +23,8 @@ LOGGER = _loggers.marimo_logger()if TYPE_CHECKING:from pathlib import Path+ from marimo._server.sessions.types import Session+@dataclassclass FileChangeResult:@@ -74,7 +75,7 @@ class EditModeReloadStrategy(ReloadStrategy):)# Send the updated cell IDs to the frontend- session.write_operation(+ session.notify(UpdateCellIdsRequest(cell_ids=cell_ids),from_consumer_id=None,)@@ -111,7 +112,7 @@ class EditModeReloadStrategy(ReloadStrategy):from_consumer_id=None,)if cell_ids:- session.write_operation(+ session.notify(UpdateCellCodes(cell_ids=cell_ids,codes=codes,@@ -132,7 +133,7 @@ class RunModeReloadStrategy:) -> None:"""Handle reload in run mode by sending Reload operation."""del changed_cell_ids- session.write_operation(Reload(), from_consumer_id=None)+ session.notify(Reload(), from_consumer_id=None)class FileChangeCoordinator:@@ -221,7 +222,6 @@ class FileChangeCoordinator:self._reload_strategy.handle_reload(session, changed_cell_ids=changed_cell_ids)-return FileChangeResult(handled=True, changed_cell_ids=changed_cell_ids)diff --git a/marimo/_server/sessions/file_watcher_integration.py b/marimo/_server/sessions/file_watcher_integration.pyindex de4edac57f..c0de03ca89 100644--- a/marimo/_server/sessions/file_watcher_integration.py+++ b/marimo/_server/sessions/file_watcher_integration.py@@ -11,11 +11,12 @@ from pathlib import Pathfrom typing import TYPE_CHECKING, Callablefrom marimo import _loggers-from marimo._server.sessions.events import SessionEventListener+from marimo._server.sessions.events import (+ SessionEventBus,+ SessionEventListener,+)+from marimo._server.sessions.extensions.types import SessionExtensionfrom marimo._server.sessions.session import Session-from marimo._server.sessions.session_repository import SessionRepository-from marimo._types.ids import SessionId-from marimo._utils import async_pathfrom marimo._utils.file_watcher import FileWatcherManagerLOGGER = _loggers.marimo_logger()@@ -24,159 +25,101 @@ if TYPE_CHECKING:from collections.abc import Awaitable-class SessionFileWatcherLifecycle:+class SessionFileWatcherExtension(SessionExtension, SessionEventListener):"""Manages file watcher lifecycle for sessions."""def __init__(self,watcher_manager: FileWatcherManager,- file_change_callback: Callable[[Path, Session], Awaitable[None]],- repository: SessionRepository,+ on_change_callback: Callable[[Path, Session], Awaitable[None]],) -> None:- """Initialize the file watcher lifecycle manager.+ """Initialize the file watcher extension.Args:watcher_manager: The underlying file watcher manager- file_change_callback: Callback to invoke when files change- repository: Session repository for ID lookups+ on_change_callback: The callback to invoke when a file changes"""self._watcher_manager = watcher_manager- self._file_change_callback = file_change_callback- self._repository = repository- # Store callbacks by session object instead of ID- # since ID may change- self._session_callbacks: dict[- Session, Callable[[Path], Awaitable[None]]- ] = {}+ self._event_bus: SessionEventBus | None = None+ self._session: Session | None = None+ self._on_change_callback = on_change_callback- def attach(self, session: Session) -> None:- """Attach a file watcher to a session.+ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ """Attach the file watcher extension to a session."""+ self._event_bus = event_bus+ self._event_bus.subscribe(self)+ self._session = session- Args:- session: The session to attach a file watcher to- """- session_id = self._repository.get_session_id(session)if not session.app_file_manager.path:- LOGGER.debug(- "Session %s has no file path, skipping file watcher",- session_id,- )return- # Create callback for this specific session- async def on_file_changed(path: Path) -> None:- # Skip if the session does not relate to the file- if session.app_file_manager.path != await async_path.abspath(path):- return-- # Use the centralized file change handler- await self._file_change_callback(path, session)-- # Store the callback- self._session_callbacks[session] = on_file_changed-# Register with the watcher managerself._watcher_manager.add_callback(- Path(session.app_file_manager.path), on_file_changed+ Path(session.app_file_manager.path), self._handle_file_change)- LOGGER.debug(+ LOGGER.info("Attached file watcher for session %s at path %s",- session_id,+ session.initialization_id,session.app_file_manager.path,)- def detach(self, session: Session) -> None:- """Detach a file watcher from a session.+ async def _handle_file_change(self, path: Path) -> None:+ """Handle a file change."""+ if not self._session:+ return- Args:- session: The session to detach the file watcher from- """- session_id = self._repository.get_session_id(session)- if not session.app_file_manager.path:+ await self._on_change_callback(path, self._session)++ def on_detach(self) -> None:+ """Detach the file watcher extension from a session."""+ if not self._session:return- callback = self._session_callbacks.get(session)- if callback is None:- LOGGER.debug(- "No file watcher callback found for session %s",- session_id,- )+ if not self._session.app_file_manager.path:return# Remove from watcher managerself._watcher_manager.remove_callback(- Path(session.app_file_manager.path), callback+ self._canonicalize_path(self._session.app_file_manager.path),+ self._handle_file_change,)- # Remove from our registry- del self._session_callbacks[session]-- LOGGER.debug(+ LOGGER.info("Detached file watcher for session %s from path %s",- session_id,- session.app_file_manager.path,+ self._session.initialization_id,+ self._session.app_file_manager.path,)- def update(self, session: Session, old_path: Path, new_path: Path) -> None:- """Update file watcher when a session's file path changes.+ def _canonicalize_path(self, path: str) -> Path:+ """Canonicalize a path."""+ return Path(path).absolute()- Args:- session: The session whose path changed- old_path: The old file path- new_path: The new file path- """- session_id = self._repository.get_session_id(session)- callback = self._session_callbacks.get(session)- if callback:+ async def on_session_notebook_renamed(+ self, session: Session, old_path: str | None+ ) -> None:+ """Update file watcher when a session's file path changes."""+ if not self._session:+ return++ if old_path:# Remove old watcher- self._watcher_manager.remove_callback(old_path, callback)+ self._watcher_manager.remove_callback(+ self._canonicalize_path(old_path), self._handle_file_change+ )++ if not session.app_file_manager.path:+ return# Attach new watcher- self.attach(session)+ self._watcher_manager.add_callback(+ self._canonicalize_path(session.app_file_manager.path),+ self._handle_file_change,+ )- LOGGER.debug(+ LOGGER.info("Updated file watcher for session %s from %s to %s",- session_id,+ session.initialization_id,old_path,- new_path,+ session.app_file_manager.path,)-- def stop_all(self) -> None:- """Stop all file watchers."""- self._watcher_manager.stop_all()- self._session_callbacks.clear()---class FileWatcherAttachmentListener(SessionEventListener):- """Event listener that attaches/detaches file watchers on session lifecycle events."""-- def __init__(- self,- file_watcher_lifecycle: SessionFileWatcherLifecycle,- enabled: bool,- ) -> None:- """Initialize the file watcher attachment listener.-- Args:- file_watcher_lifecycle: The file watcher lifecycle manager- enabled: Whether file watching is enabled- """- self._lifecycle = file_watcher_lifecycle- self._enabled = enabled-- async def on_session_created(self, session: Session) -> None:- """Attach file watcher when a session is created."""- if self._enabled:- self._lifecycle.attach(session)-- async def on_session_closed(self, session: Session) -> None:- """Detach file watcher when a session is closed."""- if self._enabled:- self._lifecycle.detach(session)-- async def on_session_resumed(- self, session: Session, old_id: SessionId- ) -> None:- """No action needed on resume - watcher remains attached."""- passdiff --git a/marimo/_server/sessions/listeners.py b/marimo/_server/sessions/listeners.pyindex 43f986b3fd..710b836405 100644--- a/marimo/_server/sessions/listeners.py+++ b/marimo/_server/sessions/listeners.py@@ -8,12 +8,16 @@ tracking recent files, managing caches, etc.from __future__ import annotationsfrom marimo._server.recents import RecentFilesManager-from marimo._server.sessions.events import SessionEventListener+from marimo._server.sessions.events import (+ SessionEventBus,+ SessionEventListener,+)+from marimo._server.sessions.extensions.types import SessionExtensionfrom marimo._server.sessions.session import Sessionfrom marimo._types.ids import SessionId-class RecentsTrackerListener(SessionEventListener):+class RecentsTrackerListener(SessionExtension, SessionEventListener):"""Event listener that tracks recently accessed files."""def __init__(self, recents_manager: RecentFilesManager) -> None:@@ -23,6 +27,19 @@ class RecentsTrackerListener(SessionEventListener):recents_manager: Manager for recent files"""self._recents = recents_manager+ self._event_bus: SessionEventBus | None = None++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ """Attach the recents tracker listener to a session."""+ del session+ self._event_bus = event_bus+ self._event_bus.subscribe(self)++ def on_detach(self) -> None:+ """Detach the recents tracker listener from a session."""+ if self._event_bus:+ self._event_bus.unsubscribe(self)+ self._event_bus = Noneasync def on_session_created(self, session: Session) -> None:"""Update recent files when a session is created."""@@ -40,3 +57,15 @@ class RecentsTrackerListener(SessionEventListener):del old_idif session.app_file_manager.path:self._recents.touch(session.app_file_manager.path)++ async def on_session_notebook_renamed(+ self, session: Session, old_path: str | None+ ) -> None:+ """Update recent files when a session notebook is renamed."""+ path = session.app_file_manager.path+ if not path:+ return+ if old_path:+ self._recents.rename(path, old_path)+ else:+ self._recents.touch(path)diff --git a/marimo/_server/sessions/room.py b/marimo/_server/sessions/room.pyindex 5aa13fda48..29223e469c 100644--- a/marimo/_server/sessions/room.py+++ b/marimo/_server/sessions/room.py@@ -8,16 +8,18 @@ session) and can have multiple kiosk consumers.from __future__ import annotations-from typing import Optional+from typing import TYPE_CHECKING, Optionalfrom marimo import _loggers-from marimo._messaging.ops import MessageOperation-from marimo._server.model import ConnectionState, SessionConsumer+from marimo._messaging.types import KernelMessage+from marimo._server.model import ConnectionStatefrom marimo._types.ids import ConsumerId-from marimo._utils.disposable import DisposableLOGGER = _loggers.marimo_logger()+if TYPE_CHECKING:+ from marimo._server.consumer import SessionConsumer+class Room:"""@@ -29,7 +31,6 @@ class Room:def __init__(self) -> None:self.main_consumer: Optional[SessionConsumer] = Noneself.consumers: dict[SessionConsumer, ConsumerId] = {}- self.disposables: dict[SessionConsumer, Disposable] = {}@propertydef size(self) -> int:@@ -38,14 +39,13 @@ class Room:def add_consumer(self,consumer: SessionConsumer,- dispose: Disposable,+ *,consumer_id: ConsumerId,# Whether the consumer is the main session consumer# We only allow one main consumer, the rest are kiosk consumersmain: bool,) -> None:self.consumers[consumer] = consumer_id- self.disposables[consumer] = disposeif main:assert self.main_consumer is None, ("Main session consumer already exists"@@ -62,27 +62,23 @@ class Room:if consumer == self.main_consumer:self.main_consumer = Noneself.consumers.pop(consumer)- disposable = self.disposables.pop(consumer)- try:- consumer.on_stop()- finally:- disposable.dispose()+ consumer.on_detach()def broadcast(self,- operation: MessageOperation,+ notification: KernelMessage,+ *,except_consumer: Optional[ConsumerId],) -> None:+ """Broadcast a notification to all consumers except the one specified."""for consumer in self.consumers:if consumer.consumer_id == except_consumer:continueif consumer.connection_state() == ConnectionState.OPEN:- consumer.write_operation(operation)+ consumer.notify(notification)def close(self) -> None:- for consumer in self.consumers:- disposable = self.disposables.pop(consumer)- consumer.on_stop()- disposable.dispose()+ # We don't need to detach consumers here because+ # they will be detached when the session is closed.self.consumers = {}self.main_consumer = Nonediff --git a/marimo/_server/sessions/session.py b/marimo/_server/sessions/session.pyindex 0ec3c6cbe8..c216571984 100644--- a/marimo/_server/sessions/session.py+++ b/marimo/_server/sessions/session.py@@ -16,6 +16,7 @@ from marimo._messaging.ops import (FocusCell,MessageOperation,UpdateCellCodes,+ serialize_kernel_message,)from marimo._messaging.types import KernelMessagefrom marimo._runtime import requests@@ -28,17 +29,19 @@ from marimo._runtime.requests import (SetUIElementValueRequest,SyncGraphRequest,)-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.consumer import SessionConsumer+from marimo._server.model import ConnectionState, SessionModefrom marimo._server.models.models import InstantiateRequestfrom marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._server.sessions.events import SessionEventBus-from marimo._server.sessions.extensions import (+from marimo._server.sessions.extensions.extensions import (CachingExtension,HeartbeatExtension,LoggingExtension,- SessionExtension,+ NotificationListenerExtension,)+from marimo._server.sessions.extensions.types import SessionExtensionfrom marimo._server.sessions.managers import (KernelManagerImpl,QueueManagerImpl,@@ -51,11 +54,6 @@ from marimo._server.sessions.types import (Session,)from marimo._types.ids import ConsumerId-from marimo._utils.distributor import (- ConnectionDistributor,- Distributor,- QueueDistributor,-)from marimo._utils.repr import format_reprif TYPE_CHECKING:@@ -68,18 +66,6 @@ _DEFAULT_TTL_SECONDS = 120__all__ = ["Session", "SessionImpl"]-def _create_message_distributor(- kernel_manager: KernelManager,- queue_manager: QueueManager,-) -> Distributor[KernelMessage]:- if kernel_manager.mode == SessionMode.EDIT:- return ConnectionDistributor(kernel_manager.kernel_connection)- else:- q = queue_manager.stream_queue- assert q is not None- return QueueDistributor(queue=q)--class SessionImpl(Session):"""A client session.@@ -101,6 +87,7 @@ class SessionImpl(Session):redirect_console_to_browser: bool,auto_instantiate: bool,ttl_seconds: Optional[int],+ extensions: list[SessionExtension] | None = None,) -> Session:"""Create a new session.@@ -127,9 +114,13 @@ class SessionImpl(Session):)extensions = [+ *(extensions or []),LoggingExtension(),HeartbeatExtension(),CachingExtension(enabled=not auto_instantiate),+ NotificationListenerExtension(+ kernel_manager=kernel_manager, queue_manager=queue_manager+ ),# TODO: Refactor more into extensions# KernelExtension()# RoomBroadcastExtension()@@ -175,21 +166,15 @@ class SessionImpl(Session):self.extensions = extensionsself.kernel_manager.start_kernel()- self.message_distributor = _create_message_distributor(- kernel_manager=self.kernel_manager,- queue_manager=queue_manager,- )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._closed = False# Attach all extensionsself._attach_extensions()+ # Connect the main consumer after attaching extensions,+ # to avoid calling on_attach on the main consumer twice.+ self.connect_consumer(session_consumer, main=True)def _attach_extensions(self) -> None:"""Attach all extensions to the session."""@@ -224,11 +209,18 @@ class SessionImpl(Session):def flush_messages(self) -> None:"""Flush any pending messages."""- self.message_distributor.flush()+ # HACK: Ideally we don't need to reach into this extension directly+ for extension in self.extensions:+ if isinstance(extension, NotificationListenerExtension):+ if extension.distributor is not None:+ extension.distributor.flush()+ returnasync def rename_path(self, new_path: str) -> None:"""Rename the path of the session."""- await self._event_bus.emit_session_notebook_renamed(self, new_path)+ old_path = self.app_file_manager.path+ self.app_file_manager.rename(new_path)+ await self._event_bus.emit_session_notebook_renamed(self, old_path)def try_interrupt(self) -> None:"""Try to interrupt the kernel."""@@ -263,19 +255,19 @@ class SessionImpl(Session):cell_ids = request.run_idscodes = [request.cells[cell_id] for cell_id in cell_ids]if cell_ids:- self.room.broadcast(+ self.notify(UpdateCellCodes(cell_ids=cell_ids,codes=codes,# Not stale because we just ran the codecode_is_stale=False,),- except_consumer=from_consumer_id,+ from_consumer_id=from_consumer_id,)if len(cell_ids) == 1:- self.room.broadcast(+ self.notify(FocusCell(cell_id=cell_ids[0]),- except_consumer=from_consumer_id,+ from_consumer_id=from_consumer_id,)self.session_view.add_control_request(request)@@ -299,6 +291,7 @@ class SessionImpl(Session):or a kiosk consumer."""self.room.remove_consumer(session_consumer)+ self.extensions.remove(session_consumer)def disconnect_main_consumer(self) -> None:"""@@ -316,12 +309,12 @@ class SessionImpl(Session):If its the main consumer and one already exists,an exception is raised."""- subscribe = session_consumer.on_start()- unsubscribe_consumer = self.message_distributor.add_consumer(subscribe)+ # Consumers are also extensions, so we want to attach them to the session+ self.extensions.append(session_consumer)+ session_consumer.on_attach(self, self._event_bus)self.room.add_consumer(session_consumer,- unsubscribe_consumer,- session_consumer.consumer_id,+ consumer_id=session_consumer.consumer_id,main=main,)@@ -337,14 +330,19 @@ class SessionImpl(Session):return ConnectionState.ORPHANEDreturn self.room.main_consumer.connection_state()- def write_operation(+ def notify(self,- operation: MessageOperation,+ operation: MessageOperation | KernelMessage,from_consumer_id: Optional[ConsumerId],) -> None:"""Write an operation to the session consumer and the session view."""- self.room.broadcast(operation, except_consumer=from_consumer_id)- self.session_view.add_operation(operation)+ if isinstance(operation, bytes):+ notification = operation+ else:+ notification = serialize_kernel_message(operation)+ self.room.broadcast(notification, except_consumer=from_consumer_id)+ self.session_view.add_raw_operation(notification)+ self._event_bus.emit_notification_sent(self, notification)def close(self) -> None:"""@@ -361,8 +359,6 @@ class SessionImpl(Session):self._detach_extensions()# Close the roomself.room.close()- # Close the kernel- self.message_distributor.stop()self.kernel_manager.close_kernel()def instantiate(diff --git a/marimo/_server/sessions/session_manager.py b/marimo/_server/sessions/session_manager.pyindex 42e938eee0..3f98506245 100644--- a/marimo/_server/sessions/session_manager.py+++ b/marimo/_server/sessions/session_manager.py@@ -18,19 +18,19 @@ from marimo._runtime.requests import (SerializedCLIArgs,SerializedQueryParams,)+from marimo._server.consumer import SessionConsumerfrom marimo._server.file_router import AppFileRouter, MarimoFileKeyfrom marimo._server.lsp import LspServer-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode-from marimo._server.notebook import AppFileManager+from marimo._server.model import ConnectionState, SessionModefrom marimo._server.recents import RecentFilesManagerfrom marimo._server.sessions.events import SessionEventBus+from marimo._server.sessions.extensions.types import SessionExtensionfrom marimo._server.sessions.file_change_handler import (FileChangeCoordinator,create_reload_strategy,)from marimo._server.sessions.file_watcher_integration import (- FileWatcherAttachmentListener,- SessionFileWatcherLifecycle,+ SessionFileWatcherExtension,)from marimo._server.sessions.listeners import RecentsTrackerListenerfrom marimo._server.sessions.resume_strategies import create_resume_strategy@@ -40,12 +40,13 @@ 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:from collections.abc import Awaitable, Coroutine, Mapping+ from marimo._server.notebook import AppFileManager+LOGGER = _loggers.marimo_logger()@@ -107,17 +108,14 @@ class SessionManager:# Initialize resume strategyself._resume_strategy = create_resume_strategy(mode, self._repository)- # Initialize event bus and listeners- self._event_bus = SessionEventBus()-# Add recents tracking listenerself.recents = RecentFilesManager()- self._event_bus.subscribe(RecentsTrackerListener(self.recents))+ self._event_bus = SessionEventBus()- # Initialize file watching components if enabled+ # Initialize file watching components+ self._watcher_manager = FileWatcherManager()self.watch = watch- if watch:- self._setup_file_watching()+ self._file_change_coordinator = self._create_file_change_coordinator()@propertydef auth_token(self) -> AuthToken:@@ -134,32 +132,6 @@ class SessionManager:"""Get all sessions as a dict."""return self._repository.sessions- def _setup_file_watching(self) -> None:- """Set up file watching components."""- # Create file watcher manager- self.watcher_manager = FileWatcherManager()-- # Create file change coordinator with appropriate reload strategy- reload_strategy = create_reload_strategy(- self.mode, self._config_manager- )- self._file_change_coordinator = FileChangeCoordinator(reload_strategy)-- # Create file watcher lifecycle manager- async def on_file_change(path: Path, session: Session) -> None:- await self._file_change_coordinator.handle_change(path, session)-- self._file_watcher_lifecycle = SessionFileWatcherLifecycle(- self.watcher_manager, on_file_change, self._repository- )-- # Register file watcher attachment listener- self._event_bus.subscribe(- FileWatcherAttachmentListener(- self._file_watcher_lifecycle, enabled=True- )- )-def app_manager(self, key: MarimoFileKey) -> AppFileManager:"""Get the app manager for the given key."""return self.file_router.get_file_manager(@@ -196,6 +168,17 @@ class SessionManager:# Create the sessionfrom marimo._runtime.requests import AppMetadata+ extensions: list[SessionExtension] = [+ RecentsTrackerListener(self.recents)+ ]+ if self.watch:+ extensions.append(+ SessionFileWatcherExtension(+ self._watcher_manager,+ self._handle_file_change,+ )+ )+session = SessionImpl.create(initialization_id=file_key,session_consumer=session_consumer,@@ -213,6 +196,7 @@ class SessionManager:redirect_console_to_browser=self.redirect_console_to_browser,ttl_seconds=self.ttl_seconds,auto_instantiate=auto_instantiate,+ extensions=extensions,)# Add to repository@@ -223,46 +207,48 @@ class SessionManager:return session- async def handle_file_rename_for_watch(- self, session_id: SessionId, prev_path: Optional[str], new_path: str+ def _create_file_change_coordinator(self) -> FileChangeCoordinator:+ """Create a file change coordinator."""+ reload_strategy = create_reload_strategy(+ self.mode, self._config_manager+ )+ return FileChangeCoordinator(reload_strategy)++ async def _handle_file_change(+ self, file_path: Path, session: Session+ ) -> None:+ await self._file_change_coordinator.handle_change(file_path, session)++ async def rename_session(+ self, session_id: SessionId, new_path: str) -> tuple[bool, Optional[str]]:"""Handle renaming a file for a session.Returns:tuple[bool, Optional[str]]: (success, error_message)"""+ from marimo._server.api.status import HTTPException+session = self.get_session(session_id)if not session:return False, "Session not found"- 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"-- await session.rename_path(new_path)+ old_path = session.app_file_manager.pathtry:- if self.watch and self._file_watcher_lifecycle:- # Update the file watcher- if prev_path:- self._file_watcher_lifecycle.update(- session, Path(prev_path), Path(new_path)- )- else:- self._file_watcher_lifecycle.attach(session)-- return True, None-+ await session.rename_path(new_path)+ except HTTPException as e:+ # HTTPException stores the message in detail, not in __str__+ return False, e.detail or str(e)except Exception as e:- LOGGER.error(f"Error handling file rename: {e}")-- if self.watch and hasattr(self, "_file_watcher_lifecycle"):- self._file_watcher_lifecycle.attach(session)return False, str(e)- async def handle_file_change(self, path: str) -> None:+ # Emit the session notebook renamed event+ await self._event_bus.emit_session_notebook_renamed(session, old_path)++ return True, None++ async def trigger_file_change(self, path: str) -> None:"""Handle a file change for all relevant sessions."""# Find all sessions associated with this filesessions_for_file = self._repository.get_by_file_path(path)@@ -271,18 +257,10 @@ class SessionManager:return# Handle file change for each session- if self.watch and hasattr(self, "_file_watcher_lifecycle"):- from marimo._server.sessions.file_change_handler import (- FileChangeCoordinator,- )-- reload_strategy = create_reload_strategy(- self.mode, self._config_manager+ for session in sessions_for_file:+ await self._file_change_coordinator.handle_change(+ Path(path), session)- coordinator = FileChangeCoordinator(reload_strategy)-- for session in sessions_for_file:- await coordinator.handle_change(Path(path), session)def get_session(self, session_id: SessionId) -> Optional[Session]:"""Get a session by ID, checking both direct and consumer IDs."""@@ -361,7 +339,7 @@ class SessionManager:f"LSP server startup failed: {alert.title} - {alert.description}")for session in self._repository.get_all():- session.write_operation(alert, from_consumer_id=None)+ session.notify(alert, from_consumer_id=None)returnelse:LOGGER.info("LSP server started successfully")@@ -391,8 +369,7 @@ class SessionManager:LOGGER.debug("Shutting down")self.close_all_sessions()self.lsp_server.stop()- if self.watch and hasattr(self, "watcher_manager"):- self.watcher_manager.stop_all()+ self._watcher_manager.stop_all()def should_send_code_to_frontend(self) -> bool:"""Returns True if the server can send messages to the frontend."""diff --git a/marimo/_server/sessions/types.py b/marimo/_server/sessions/types.pyindex 75d98df664..c748caef4f 100644--- a/marimo/_server/sessions/types.py+++ b/marimo/_server/sessions/types.py@@ -11,21 +11,24 @@ from __future__ import annotationsfrom enum import Enumfrom typing import TYPE_CHECKING, Any, Optional, Protocol, Union-from marimo._messaging.ops import MessageOperation-from marimo._messaging.types import KernelMessage-from marimo._runtime import requests-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode-from marimo._server.notebook.file_manager import AppFileManager-from marimo._server.session.session_view import SessionView-from marimo._server.types import ProcessLike, QueueType-from marimo._types.ids import ConsumerId-from marimo._utils.typed_connection import TypedConnection-if TYPE_CHECKING:import threadingfrom collections.abc import Mappingfrom marimo._config.manager import MarimoConfigManager+ from marimo._messaging.ops import MessageOperation+ from marimo._messaging.types import KernelMessage+ from marimo._runtime import requests+ from marimo._server.consumer import SessionConsumer+ from marimo._server.model import (+ ConnectionState,+ SessionMode,+ )+ from marimo._server.notebook.file_manager import AppFileManager+ from marimo._server.session.session_view import SessionView+ from marimo._server.types import ProcessLike, QueueType+ from marimo._types.ids import ConsumerId+ from marimo._utils.typed_connection import TypedConnectionclass QueueManager(Protocol):@@ -173,9 +176,9 @@ class Session(Protocol):"""Return the connection state of the session."""...- def write_operation(+ def notify(self,- operation: MessageOperation,+ operation: MessageOperation | KernelMessage,from_consumer_id: Optional[ConsumerId],) -> None:"""Write an operation to the session consumer and the session view."""diff --git a/marimo/_server/sessions/utils.py b/marimo/_server/sessions/utils.pyindex 0bc3e2b5f1..da9d1addd7 100644--- a/marimo/_server/sessions/utils.py+++ b/marimo/_server/sessions/utils.py@@ -5,6 +5,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING, Optional+from marimo._messaging.ops import serialize_kernel_messagefrom marimo._server.model import ConnectionStatefrom marimo._types.ids import ConsumerId@@ -19,7 +20,8 @@ def send_message_to_consumer(consumer_id: Optional[ConsumerId],) -> None:"""Send a message operation to a specific consumer in a session."""+ notification = serialize_kernel_message(operation)if session.connection_state() == ConnectionState.OPEN:for consumer, c_id in session.consumers.items():if c_id == consumer_id:- consumer.write_operation(operation)+ consumer.notify(notification)diff --git a/marimo/_server/start.py b/marimo/_server/start.pyindex 71c0271344..4d4bb8b197 100644--- a/marimo/_server/start.py+++ b/marimo/_server/start.py@@ -53,9 +53,9 @@ def _execute_startup_command(for session in session_manager.sessions.values():# Clear buffer if it has contentif buffer.content != "":- session.write_operation(buffer, from_consumer_id=None)+ session.notify(buffer, from_consumer_id=None)buffer = StartupLogs(content="", status="start")- session.write_operation(content, from_consumer_id=None)+ session.notify(content, from_consumer_id=None)else:buffer.content += content.contentbuffer.status = content.statusdiff --git a/marimo/_utils/file_watcher.py b/marimo/_utils/file_watcher.pyindex 4d8902f064..71b8690fe4 100644--- a/marimo/_utils/file_watcher.py+++ b/marimo/_utils/file_watcher.py@@ -176,7 +176,9 @@ class FileWatcherManager:async def shared_callback(changed_path: Path) -> None:callbacks = self._callbacks.get(str(changed_path), set())- for cb in callbacks:+ # Iterate over a copy to avoid "Set changed size during iteration"+ # if a callback modifies the callbacks set+ for cb in list(callbacks):await cb(changed_path)watcher = FileWatcher.create(path, shared_callback)@@ -188,7 +190,7 @@ class FileWatcherManager:"""Remove a callback for a file path. Removes watcher if no more callbacks."""path_str = str(path)if path_str not in self._callbacks:- LOGGER.warning(f"Callback for {path_str} not found")+ # May already be removed from stop_all()returnself._callbacks[path_str].discard(callback)diff --git a/tests/_server/api/endpoints/test_file_explorer.py b/tests/_server/api/endpoints/test_file_explorer.pyindex ffe6b007b1..8b29c4215d 100644--- a/tests/_server/api/endpoints/test_file_explorer.py+++ b/tests/_server/api/endpoints/test_file_explorer.py@@ -98,9 +98,8 @@ def test_update_file(client: TestClient) -> None:@pytest.mark.skipif(is_windows(), reason="not supported on Windows")def test_update_file_with_session(client: TestClient) -> None:sm = get_session_manager(client)- # Enable watch mode to set up file watcher lifecycle+ # Enable watch mode (file watcher is set up automatically)sm.watch = True- sm._setup_file_watching()file_path = sm.file_router.get_unique_file_key()assert file_path@@ -116,7 +115,7 @@ def test_update_file_with_session(client: TestClient) -> None:assert data["op"] == "kernel-ready"# Verify file watcher was attached when session was created- assert len(sm._file_watcher_lifecycle._session_callbacks) == 1+ assert len(sm._watcher_manager._callbacks) == 1# Update the fileresponse = client.post(@@ -139,7 +138,7 @@ def test_update_file_with_session(client: TestClient) -> None:# Clean upsm.watch = False- sm.watcher_manager.stop_all()+ sm._watcher_manager.stop_all()def test_move_file_or_directory(client: TestClient) -> None:diff --git a/tests/_server/api/endpoints/test_resume_session.py b/tests/_server/api/endpoints/test_resume_session.pyindex 0005241435..30083f627b 100644--- a/tests/_server/api/endpoints/test_resume_session.py+++ b/tests/_server/api/endpoints/test_resume_session.py@@ -303,7 +303,6 @@ def test_restart_session(client: TestClient) -> None:def test_resume_session_with_watch(client: TestClient) -> None:session_manager = get_session_manager(client)session_manager.watch = True- session_manager._setup_file_watching()with client.websocket_connect(_create_ws_url("123")) as websocket:data = websocket.receive_json()@@ -373,7 +372,7 @@ def test_resume_session_with_watch(client: TestClient) -> None:assert session_view.last_executed_code == {"MJUe": "x=10; x"}session_manager.watch = False- session_manager.watcher_manager.stop_all()+ session_manager._watcher_manager.stop_all()client.post("/api/kernel/shutdown", headers=HEADERS)diff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.pyindex f3059f4b3a..59b219552e 100644--- a/tests/_server/api/endpoints/test_ws.py+++ b/tests/_server/api/endpoints/test_ws.py@@ -212,8 +212,11 @@ def test_fails_on_multiple_connections_with_same_file(async def test_file_watcher_calls_reload(client: TestClient) -> None:session_manager: SessionManager = get_session_manager(client)session_manager.mode = SessionMode.RUN+ # Recreate the file change coordinator with the new mode's strategy+ session_manager._file_change_coordinator = (+ session_manager._create_file_change_coordinator()+ )session_manager.watch = True- session_manager._setup_file_watching()with client.websocket_connect(WS_URL) as websocket:data = websocket.receive_json()assert_kernel_ready_response(data)@@ -222,8 +225,8 @@ async def test_file_watcher_calls_reload(client: TestClient) -> None:with open(filename, "a") as f: # noqa: ASYNC230f.write("\n# test")f.close()- assert session_manager.watcher_manager._watchers- watcher = list(session_manager.watcher_manager._watchers.values())[0]+ assert session_manager._watcher_manager._watchers+ watcher = list(session_manager._watcher_manager._watchers.values())[0]await watcher.callback(Path(filename))data = websocket.receive_json()assert data == {"op": "reload", "data": {"op": "reload"}}diff --git a/tests/_server/sessions/test_file_change_handler.py b/tests/_server/sessions/test_file_change_handler.pyindex 7a6ad3c323..92a7b6a591 100644--- a/tests/_server/sessions/test_file_change_handler.py+++ b/tests/_server/sessions/test_file_change_handler.py@@ -39,7 +39,7 @@ def create_test_app_file_manager(def mock_session():"""Create a mock session for testing."""session = MagicMock()- session.write_operation = MagicMock()+ session.notify = MagicMock()session.put_control_request = MagicMock()return session@@ -86,10 +86,10 @@ def test_edit_mode_reload_strategy_lazy(strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)# Should send UpdateCellIdsRequest- assert mock_session.write_operation.call_count >= 1+ assert mock_session.notify.call_count >= 1update_ids_calls = [call- for call in mock_session.write_operation.call_args_list+ for call in mock_session.notify.call_args_listif isinstance(call[0][0], UpdateCellIdsRequest)]assert len(update_ids_calls) == 1@@ -97,7 +97,7 @@ def test_edit_mode_reload_strategy_lazy(# Should send UpdateCellCodes with code_is_stale=Trueupdate_codes_calls = [call- for call in mock_session.write_operation.call_args_list+ for call in mock_session.notify.call_args_listif isinstance(call[0][0], UpdateCellCodes)]assert len(update_codes_calls) == 1@@ -131,10 +131,10 @@ def test_edit_mode_reload_strategy_autorun(strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)# Should send UpdateCellIdsRequest- assert mock_session.write_operation.call_count >= 1+ assert mock_session.notify.call_count >= 1update_ids_calls = [call- for call in mock_session.write_operation.call_args_list+ for call in mock_session.notify.call_args_listif isinstance(call[0][0], UpdateCellIdsRequest)]assert len(update_ids_calls) == 1@@ -193,8 +193,8 @@ def test_run_mode_reload_strategy(mock_session: MagicMock) -> None:strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)# Should send Reload operation- mock_session.write_operation.assert_called_once()- operation = mock_session.write_operation.call_args[0][0]+ mock_session.notify.assert_called_once()+ operation = mock_session.notify.call_args[0][0]assert isinstance(operation, Reload)diff --git a/tests/_server/sessions/test_file_watcher_integration.py b/tests/_server/sessions/test_file_watcher_integration.pyindex 7ca1370b9f..35dc3f8e53 100644--- a/tests/_server/sessions/test_file_watcher_integration.py+++ b/tests/_server/sessions/test_file_watcher_integration.py@@ -8,9 +8,9 @@ from unittest.mock import AsyncMock, MagicMockimport pytest+from marimo._server.sessions.events import SessionEventBusfrom marimo._server.sessions.file_watcher_integration import (- FileWatcherAttachmentListener,- SessionFileWatcherLifecycle,+ SessionFileWatcherExtension,)from marimo._server.sessions.session_repository import SessionRepositoryfrom marimo._types.ids import SessionId@@ -44,24 +44,18 @@ def file_change_callback():@pytest.fixture-def lifecycle(watcher_manager, file_change_callback, repository):+def lifecycle(watcher_manager, file_change_callback):"""Create a SessionFileWatcherLifecycle instance."""- return SessionFileWatcherLifecycle(- watcher_manager, file_change_callback, repository- )+ return SessionFileWatcherExtension(watcher_manager, file_change_callback)def test_attach_session_with_path(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test attaching a file watcher to a session with a file path."""session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- lifecycle.attach(session)+ lifecycle.on_attach(session, SessionEventBus())# Verify watcher was addedassert watcher_manager.add_callback.call_count == 1@@ -72,141 +66,106 @@ def test_attach_session_with_path(def test_attach_session_without_path(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test attaching a file watcher to a session without a file path."""session = create_mock_session(None)session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- lifecycle.attach(session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)# Verify watcher was not addedwatcher_manager.add_callback.assert_not_called()def test_detach_session_with_path(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test detaching a file watcher from a session with a file path."""session = create_mock_session("/path/to/file.py")session_id = SessionId("test-session")- repository.add_sync(session_id, session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)- # First attach- lifecycle.attach(session)callback = watcher_manager.add_callback.call_args[0][1]# Then detach- lifecycle.detach(session)+ lifecycle.on_detach()# Verify watcher was removed with the same callbackwatcher_manager.remove_callback.assert_called_once_with(- Path("/path/to/file.py"), callback+ Path("/path/to/file.py").absolute(), callback)def test_detach_session_without_path(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test detaching a file watcher from a session without a file path."""session = create_mock_session(None)session_id = SessionId("test-session")- repository.add_sync(session_id, session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)- lifecycle.detach(session)+ lifecycle.on_detach()# Verify watcher was not removedwatcher_manager.remove_callback.assert_not_called()def test_detach_session_not_attached(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test detaching a file watcher from a session that was never attached."""- session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-# Detach without attaching first- lifecycle.detach(session)+ lifecycle.on_detach()- # Should not crash, just skip removalwatcher_manager.remove_callback.assert_not_called()-def test_update_session_path(- lifecycle: SessionFileWatcherLifecycle,+async def test_update_session_path(+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test updating a session's file path."""session = create_mock_session("/path/to/old.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)# Attach to old path- lifecycle.attach(session)old_callback = watcher_manager.add_callback.call_args[0][1]# Update pathsession.app_file_manager.path = "/path/to/new.py"- lifecycle.update(session, Path("/path/to/old.py"), Path("/path/to/new.py"))+ await lifecycle.on_session_notebook_renamed(session, "/path/to/old.py")# Verify old watcher was removedwatcher_manager.remove_callback.assert_called_once_with(- Path("/path/to/old.py"), old_callback+ Path("/path/to/old.py").absolute(), # noqa: ASYNC240+ old_callback,)# Verify new watcher was addedassert watcher_manager.add_callback.call_count == 2new_call = watcher_manager.add_callback.call_args_list[1]- assert new_call[0][0] == Path("/path/to/new.py")---def test_stop_all(- lifecycle: SessionFileWatcherLifecycle,- watcher_manager: MagicMock,- repository: SessionRepository,-) -> None:- """Test stopping all file watchers."""- # Attach multiple sessions- session1 = create_mock_session("/path/to/file1.py")- session2 = create_mock_session("/path/to/file2.py")- repository.add_sync(SessionId("session1"), session1)- repository.add_sync(SessionId("session2"), session2)-- lifecycle.attach(session1)- lifecycle.attach(session2)-- # Stop all- lifecycle.stop_all()-- # Verify watcher manager was stopped- watcher_manager.stop_all.assert_called_once()+ assert new_call[0][0] == Path("/path/to/new.py").absolute() # noqa: ASYNC240async def test_file_change_callback_invoked(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,file_change_callback: AsyncMock,- repository: SessionRepository,) -> None:"""Test that file change callback is invoked correctly."""session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)# Attach session- lifecycle.attach(session)# Get the callback that was registered with the watcherregistered_callback = watcher_manager.add_callback.call_args[0][1]@@ -220,104 +179,29 @@ async def test_file_change_callback_invoked()-async def test_file_change_callback_wrong_path_skipped(- lifecycle: SessionFileWatcherLifecycle,- watcher_manager: MagicMock,- file_change_callback: AsyncMock,- repository: SessionRepository,-) -> None:- """Test that file change callback is skipped for wrong path."""- session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- # Attach session- lifecycle.attach(session)-- # Get the callback that was registered with the watcher- registered_callback = watcher_manager.add_callback.call_args[0][1]-- # Simulate file change with different path- await registered_callback(Path("/path/to/different.py"))-- # Verify our file change callback was not invoked- file_change_callback.assert_not_called()--async def test_listener_on_session_created_enabled(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test listener attaches watcher when session is created (enabled)."""- listener = FileWatcherAttachmentListener(lifecycle, enabled=True)session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- await listener.on_session_created(session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)# Verify watcher was attachedwatcher_manager.add_callback.assert_called_once()-async def test_listener_on_session_created_disabled(- lifecycle: SessionFileWatcherLifecycle,- watcher_manager: MagicMock,- repository: SessionRepository,-) -> None:- """Test listener does not attach watcher when disabled."""- listener = FileWatcherAttachmentListener(lifecycle, enabled=False)-- session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- await listener.on_session_created(session)-- # Verify watcher was not attached- watcher_manager.add_callback.assert_not_called()---async def test_listener_on_session_closed_enabled(- lifecycle: SessionFileWatcherLifecycle,- watcher_manager: MagicMock,- repository: SessionRepository,-) -> None:- """Test listener detaches watcher when session is closed (enabled)."""- listener = FileWatcherAttachmentListener(lifecycle, enabled=True)-- session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- # First attach- await listener.on_session_created(session)- callback = watcher_manager.add_callback.call_args[0][1]-- # Then close- await listener.on_session_closed(session)-- # Verify watcher was detached- watcher_manager.remove_callback.assert_called_once_with(- Path("/path/to/file.py"), callback- )--async def test_listener_on_session_closed_disabled(- lifecycle: SessionFileWatcherLifecycle,+ lifecycle: SessionFileWatcherExtension,watcher_manager: MagicMock,- repository: SessionRepository,) -> None:"""Test listener does not detach watcher when disabled."""- listener = FileWatcherAttachmentListener(lifecycle, enabled=False)session = create_mock_session("/path/to/file.py")- session_id = SessionId("test-session")- repository.add_sync(session_id, session)-- await listener.on_session_closed(session)+ event_bus = SessionEventBus()+ lifecycle.on_attach(session, event_bus)# Verify watcher was not detachedwatcher_manager.remove_callback.assert_not_called()diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.pyindex 93a79305bd..28c1501d2b 100644--- a/tests/_server/test_session_manager.py+++ b/tests/_server/test_session_manager.py@@ -7,9 +7,10 @@ from unittest.mock import MagicMock, Mockimport pytestfrom marimo._config.manager import get_default_config_manager+from marimo._server.consumer import SessionConsumerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.lsp import LspServer-from marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.model import ConnectionState, SessionModefrom marimo._server.notebook import AppFileManagerfrom marimo._server.sessions import (KernelManager,diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.pyindex 834557ae96..07725d2b39 100644--- a/tests/_server/test_sessions.py+++ b/tests/_server/test_sessions.py@@ -23,7 +23,12 @@ from marimo._ast.app_config import _AppConfigfrom marimo._config.manager import (get_default_config_manager,)-from marimo._messaging.ops import UpdateCellCodes+from marimo._messaging.ops import (+ MessageOperation,+ UpdateCellCodes,+ deserialize_kernel_message,+)+from marimo._messaging.types import KernelMessagefrom marimo._runtime.requests import (AppMetadata,CreationRequest,@@ -31,10 +36,13 @@ from marimo._runtime.requests import (SetUIElementValueRequest,SyncGraphRequest,)+from marimo._server.consumer import SessionConsumerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.model import ConnectionState, SessionModefrom marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionView+from marimo._server.sessions import Session+from marimo._server.sessions.events import SessionEventBusfrom marimo._server.sessions.managers import (KernelManagerImpl,QueueManagerImpl,@@ -44,7 +52,7 @@ from marimo._server.sessions.session import ()from marimo._server.sessions.session_manager import SessionManagerfrom marimo._server.utils import initialize_asyncio-from marimo._types.ids import SessionId+from marimo._types.ids import ConsumerId, SessionIdfrom marimo._utils.marimo_path import MarimoPathinitialize_asyncio()@@ -60,6 +68,27 @@ app_metadata = AppMetadata()+class MockSessionConsumer(SessionConsumer):+ def __init__(self) -> None:+ self.notify_calls: list[MessageOperation] = []++ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:+ pass++ def on_detach(self) -> None:+ pass++ @property+ def consumer_id(self) -> ConsumerId:+ return ConsumerId("test_consumer_id")++ def connection_state(self) -> ConnectionState:+ return ConnectionState.OPEN++ def notify(self, notification: KernelMessage) -> None:+ self.notify_calls.append(deserialize_kernel_message(notification))++# TODO(akshayka): automatically do this for every test in our test suitedef save_and_restore_main(f: F) -> F:"""Kernels swap out the main module; restore it after running tests"""@@ -301,8 +330,8 @@ async def test_session() -> None:assert session.room.main_consumer == session_consumerassert session._queue_manager == queue_managerassert session.kernel_manager == kernel_manager- session_consumer.on_start.assert_called_once()- assert session_consumer.on_stop.call_count == 0+ session_consumer.on_attach.assert_called_once()+ assert session_consumer.on_detach.call_count == 0assert session.connection_state() == ConnectionState.OPENsession.close()@@ -313,8 +342,8 @@ async def test_session() -> None:assert not kernel_manager.is_alive()assert queue_manager.input_queue.empty()assert queue_manager.control_queue.empty()- session_consumer.on_start.assert_called_once()- session_consumer.on_stop.assert_called_once()+ session_consumer.on_attach.assert_called_once()+ session_consumer.on_detach.assert_called_once()assert session.connection_state() == ConnectionState.CLOSED@@ -347,23 +376,23 @@ def test_session_disconnect_reconnect() -> None:# Assert startupassert session.room.main_consumer == session_consumer- session_consumer.on_start.assert_called_once()- assert session_consumer.on_stop.call_count == 0+ session_consumer.on_attach.assert_called_once()+ assert session_consumer.on_detach.call_count == 0session.disconnect_consumer(session_consumer)# Assert shutdown of consumerassert session.room.main_consumer is None- session_consumer.on_start.assert_called_once()- session_consumer.on_stop.assert_called_once()+ session_consumer.on_attach.assert_called_once()+ session_consumer.on_detach.assert_called_once()assert session.connection_state() == ConnectionState.ORPHANED# Reconnectnew_session_consumer = MagicMock()session.connect_consumer(new_session_consumer, main=True)assert session.room.main_consumer == new_session_consumer- new_session_consumer.on_start.assert_called_once()- assert new_session_consumer.on_stop.call_count == 0+ new_session_consumer.on_attach.assert_called_once()+ assert new_session_consumer.on_detach.call_count == 0session.close()new_session_consumer.connection_state.return_value = ConnectionState.CLOSED@@ -372,8 +401,8 @@ def test_session_disconnect_reconnect() -> None:assert kernel_manager.kernel_task is not Nonekernel_manager.kernel_task.join()assert not kernel_manager.is_alive()- new_session_consumer.on_start.assert_called_once()- new_session_consumer.on_stop.assert_called_once()+ new_session_consumer.on_attach.assert_called_once()+ new_session_consumer.on_detach.assert_called_once()assert session.connection_state() == ConnectionState.CLOSED@@ -408,8 +437,8 @@ def test_session_with_kiosk_consumers() -> None:assert session.room.main_consumer == session_consumerassert session._queue_manager == queue_managerassert session.kernel_manager == kernel_manager- session_consumer.on_start.assert_called_once()- assert session_consumer.on_stop.call_count == 0+ session_consumer.on_attach.assert_called_once()+ assert session_consumer.on_detach.call_count == 0assert session.connection_state() == ConnectionState.OPEN# Create a kiosk consumer@@ -420,8 +449,8 @@ def test_session_with_kiosk_consumers() -> None:# Assert startup of kiosk consumerassert session.room.main_consumer != kiosk_consumerassert kiosk_consumer in session.room.consumers- kiosk_consumer.on_start.assert_called_once()- assert kiosk_consumer.on_stop.call_count == 0+ kiosk_consumer.on_attach.assert_called_once()+ assert kiosk_consumer.on_detach.call_count == 0assert session.connection_state() == ConnectionState.OPENsession.close()@@ -434,10 +463,10 @@ def test_session_with_kiosk_consumers() -> None:assert not kernel_manager.is_alive()assert queue_manager.input_queue.empty()assert queue_manager.control_queue.empty()- session_consumer.on_start.assert_called_once()- session_consumer.on_stop.assert_called_once()- kiosk_consumer.on_start.assert_called_once()- kiosk_consumer.on_stop.assert_called_once()+ session_consumer.on_attach.assert_called_once()+ session_consumer.on_detach.assert_called_once()+ kiosk_consumer.on_attach.assert_called_once()+ kiosk_consumer.on_detach.assert_called_once()assert session.connection_state() == ConnectionState.CLOSEDassert not session.room.consumersassert session.room.main_consumer is None@@ -486,13 +515,7 @@ def __():watch=True,)- # Create a mock session consumer- session_consumer = MagicMock()- session_consumer.connection_state.return_value = ConnectionState.OPEN- operations: list[Any] = []- session_consumer.write_operation = (- lambda op, *_args: operations.append(op)- )+ session_consumer = MockSessionConsumer()# Create a sessionsession_manager.create_session(@@ -504,7 +527,7 @@ def __():)# Wait a loop to ensure the session is created- await asyncio.sleep(0.01)+ await asyncio.sleep(0.2)tmp_file.write_text("""import marimo@@ -519,25 +542,21 @@ def __():# Wait for the watcher to detect the changefor _ in range(16): # noqa: B007await asyncio.sleep(0.1)- if len(operations) > 0:+ if len(session_consumer.notify_calls) > 0:break# Check that UpdateCellCodes was sent with the new codeupdate_ops = [- op for op in operations if isinstance(op, UpdateCellCodes)+ op+ for op in session_consumer.notify_calls+ if isinstance(op, UpdateCellCodes)]assert len(update_ops) == 1assert "2" == update_ops[0].codes[0]assert update_ops[0].code_is_stale is True# Create another session for the same file- session_consumer2 = MagicMock()- session_consumer2.connection_state.return_value = ConnectionState.OPEN- operations2: list[Any] = []- session_consumer2.write_operation = (- lambda op, *_args: operations2.append(op)- )-+ session_consumer2 = MockSessionConsumer()session_manager.create_session(session_id=SessionId("test2"),session_consumer=session_consumer2,@@ -547,8 +566,8 @@ def __():)# Modify the file again- operations.clear()- operations2.clear()+ session_consumer.notify_calls.clear()+ session_consumer2.notify_calls.clear()tmp_file.write_text("""import marimoapp = marimo.App()@@ -562,15 +581,19 @@ def __():# Wait for the watcher to detect the changefor _ in range(16): # noqa: B007await asyncio.sleep(0.1)- if len(operations) > 0:+ if len(session_consumer.notify_calls) > 0:break# Both sessions should receive the updateupdate_ops = [- op for op in operations if isinstance(op, UpdateCellCodes)+ op+ for op in session_consumer.notify_calls+ if isinstance(op, UpdateCellCodes)]update_ops2 = [- op for op in operations2 if isinstance(op, UpdateCellCodes)+ op+ for op in session_consumer2.notify_calls+ if isinstance(op, UpdateCellCodes)]assert len(update_ops) == 1assert len(update_ops2) == 1@@ -579,8 +602,8 @@ def __():# Close one session and verify the other still receives updatessession_manager.close_session(session_id)- operations.clear()- operations2.clear()+ session_consumer.notify_calls.clear()+ session_consumer2.notify_calls.clear()tmp_file.write_text("""import marimo@@ -595,12 +618,14 @@ def __():# Wait for the watcher to detect the changefor _ in range(16): # noqa: B007await asyncio.sleep(0.1)- if len(operations2) > 0:+ if len(session_consumer2.notify_calls) > 0:break# Only one session should receive the updateupdate_ops2 = [- op for op in operations2 if isinstance(op, UpdateCellCodes)+ op+ for op in session_consumer2.notify_calls+ if isinstance(op, UpdateCellCodes)]assert len(update_ops2) == 1assert "4" == update_ops2[0].codes[0]@@ -663,22 +688,24 @@ def test_watch_mode_does_not_override_config(tmp_path: Path) -> None:session_manager_no_watch.shutdown()[email protected](reruns=3)@save_and_restore_main-async def test_watch_mode_with_watcher_on_save_config() -> None:- """Test that watch mode works correctly with watcher_on_save config."""- # Create a temporary file- with NamedTemporaryFile(delete=False, suffix=".py") as tmp_file:- tmp_path = Path(tmp_file.name)- # Write initial notebook content- tmp_file.write(- b"""import marimo-app = marimo.App()+async def test_watch_mode_with_watcher_on_save_autorun(tmp_path: Path) -> None:+ """Test that watch mode with autorun config auto-executes changed cells."""+ tmp_file = tmp_path / "test.py"+ tmp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()-def __():- 1-"""+ @app.cell+ def __():+ 1+ """)+ )+ session_manager: SessionManager | None = Nonetry:# Create a config with watcher_on_save set to autorun@@ -692,7 +719,7 @@ def __():)# Create a session manager with file watching enabled- file_router = AppFileRouter.from_filename(MarimoPath(str(tmp_path)))+ file_router = AppFileRouter.from_filename(MarimoPath(str(tmp_file)))session_manager = SessionManager(file_router=file_router,mode=SessionMode.EDIT,@@ -709,52 +736,55 @@ def __():)# Create a mock session consumer- session_consumer = MagicMock()- session_consumer.connection_state.return_value = ConnectionState.OPEN- operations: list[Any] = []- session_consumer.write_operation = (- lambda op, *_args: operations.append(op)- )+ session_consumer = MockSessionConsumer()# Create a sessionsession = session_manager.create_session(session_id=session_id,session_consumer=session_consumer,query_params={},- file_key=str(tmp_path),+ file_key=str(tmp_file),auto_instantiate=False,)session.session_view = MagicMock(SessionView)- # Wait a bit and then modify the file- for _ in range(16): # noqa: B007- await asyncio.sleep(0.1)- if len(operations) > 0:+ # Wait for file watcher to be initialized by checking it exists+ for _ in range(20): # noqa: B007+ await asyncio.sleep(0.05)+ if (+ hasattr(session_manager, "_file_watcher")+ and session_manager._file_watcher is not None+ ):break# Modify the file- operations.clear()- with open(tmp_path, "w") as f: # noqa: ASYNC230- f.write(- """import marimo-app = marimo.App()--def __():- 2-"""+ session_consumer.notify_calls.clear()+ tmp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def __():+ 2+ """)+ )- # Wait for the watcher to detect the change- for _ in range(16): # noqa: B007+ # Wait for the watcher to detect the change and send UpdateCellCodes+ update_ops: list[UpdateCellCodes] = []+ for _ in range(20): # noqa: B007await asyncio.sleep(0.1)- if len(operations) > 0:+ update_ops = [+ op+ for op in session_consumer.notify_calls+ if isinstance(op, UpdateCellCodes)+ ]+ if update_ops:break# Check that UpdateCellCodes was sent with code_is_stale=False (autorun)- update_ops = [- op for op in operations if isinstance(op, UpdateCellCodes)- ]assert len(update_ops) == 1assert "2" in update_ops[0].codes[0]assert update_ops[0].code_is_stale is False@@ -764,7 +794,33 @@ def __():last_call = session.session_view.add_control_request.call_args[0][0]assert isinstance(last_call, SyncGraphRequest)- # Now change config to lazy mode+ finally:+ # Cleanup+ if session_manager:+ session_manager.shutdown()+++@save_and_restore_main+async def test_watch_mode_with_watcher_on_save_lazy(tmp_path: Path) -> None:+ """Test that watch mode with lazy config marks cells as stale without executing."""+ tmp_file = tmp_path / "test.py"+ tmp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def __():+ 1+ """+ )+ )+ session_manager: SessionManager | None = None++ try:+ # Create a config with watcher_on_save set to lazy+ config_reader = get_default_config_manager(current_path=None)config_reader_lazy = config_reader.with_overrides({"runtime": {@@ -772,47 +828,77 @@ def __():}})- session_manager._config_manager.get_config = (- lambda: config_reader_lazy.get_config()++ # Create a session manager with file watching enabled+ file_router = AppFileRouter.from_filename(MarimoPath(str(tmp_file)))+ session_manager = SessionManager(+ file_router=file_router,+ mode=SessionMode.EDIT,+ quiet=True,+ include_code=True,+ lsp_server=MagicMock(),+ config_manager=config_reader_lazy,+ cli_args={},+ argv=None,+ auth_token=None,+ redirect_console_to_browser=False,+ ttl_seconds=None,+ watch=True,)- # Reset the mock- session_consumer.put_control_request.reset_mock()+ # Create a mock session consumer+ session_consumer = MockSessionConsumer()- # Modify the file again- operations.clear()- with open(tmp_path, "w") as f: # noqa: ASYNC230- f.write(- """import marimo-app = marimo.App()+ # Create a session+ session = session_manager.create_session(+ session_id=session_id,+ session_consumer=session_consumer,+ query_params={},+ file_key=str(tmp_file),+ auto_instantiate=False,+ )-def __():- 3-"""+ # Wait a bit for session to be ready+ for _ in range(16): # noqa: B007+ await asyncio.sleep(0.1)+ if len(session_consumer.notify_calls) > 0:+ break++ # Modify the file+ session_consumer.notify_calls.clear()+ tmp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def __():+ 2+ """)+ )# Wait for the watcher to detect the changefor _ in range(16): # noqa: B007await asyncio.sleep(0.1)- if len(operations) > 0:+ if len(session_consumer.notify_calls) > 0:break# Check that UpdateCellCodes was sent with code_is_stale=True (lazy)update_ops = [- op for op in operations if isinstance(op, UpdateCellCodes)+ op+ for op in session_consumer.notify_calls+ if isinstance(op, UpdateCellCodes)]assert len(update_ops) == 1- assert "3" in update_ops[0].codes[0]+ assert "2" in update_ops[0].codes[0]assert update_ops[0].code_is_stale is True- # Verify that no execution was queued- assert not session_consumer.put_control_request.called-finally:# Cleanup- session_manager.shutdown()- os.remove(tmp_path)+ if session_manager:+ session_manager.shutdown()@save_and_restore_main@@ -858,8 +944,8 @@ def __():session_consumer = MagicMock()session_consumer.connection_state.return_value = ConnectionState.OPENoperations: list[Any] = []- session_consumer.write_operation = (- lambda op, *_args: operations.append(op)+ session_consumer.notify = lambda op, *_args: operations.append(+ deserialize_kernel_message(op))# Create a session@@ -872,16 +958,16 @@ def __():)# Try to rename to a non-existent file- success, error = await session_manager.handle_file_rename_for_watch(- session_id, str(tmp_path1), "/nonexistent/file.py"+ success, error = await session_manager.rename_session(+ session_id, "/nonexistent/file.py")assert not successassert error is not None- assert "does not exist" in error+ assert "Failed to rename" in error# Try to rename with an invalid session- success, error = await session_manager.handle_file_rename_for_watch(- "nonexistent", str(tmp_path1), str(new_path)+ success, error = await session_manager.rename_session(+ "nonexistent", str(new_path))assert not successassert error is not None@@ -892,8 +978,8 @@ def __():assert session is not Nonesession.app_file_manager.rename(str(new_path))assert new_path.exists()- success, error = await session_manager.handle_file_rename_for_watch(- session_id, str(tmp_path1), str(new_path)+ success, error = await session_manager.rename_session(+ session_id, str(new_path))assert successassert error is None@@ -926,121 +1012,10 @@ def __():finally:# Cleanupsession_manager.shutdown()- os.remove(new_path)---@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- notebook_path = tmp_path / "test.py"- notebook_path.write_text(- """import marimo--app = marimo.App()--def __():- 1-"""- )-- # Create session with the temporary file- session_consumer = MagicMock()- session_consumer.connection_state.return_value = ConnectionState.OPEN- queue_manager = QueueManagerImpl(use_multiprocessing=False)- kernel_manager = KernelManagerImpl(- queue_manager=queue_manager,- mode=SessionMode.RUN,- configs={},- app_metadata=app_metadata,- config_manager=get_default_config_manager(current_path=None),- virtual_files_supported=True,- redirect_console_to_browser=False,- )-- app_file_manager = AppFileManager(filename=str(notebook_path))- codes = tuple(- cell_data.code- for cell_data in app_file_manager.app.cell_manager.cell_data()- )- session = SessionImpl(- initialization_id=session_id,- session_consumer=session_consumer,- queue_manager=queue_manager,- kernel_manager=kernel_manager,- 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- session.sync_session_view_from_cache()- # Should create a new empty session view since no cache exists- assert session.session_view is not None- assert session.session_cache_manager is not None- assert session.session_cache_manager.path == str(notebook_path)- assert (- session.session_cache_manager.interval- == SessionImpl.SESSION_CACHE_INTERVAL_SECONDS- )-- # Add some operations to the session view- session.write_operation(- UpdateCellCodes(- cell_ids=["1"],- codes=["print('hello')"],- code_is_stale=False,- ),- from_consumer_id=None,- )-- # Create a new session and sync from cache- session2 = SessionImpl(- initialization_id="test2",- session_consumer=session_consumer,- queue_manager=queue_manager,- kernel_manager=kernel_manager,- 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()-- # Verify the session views match- assert session2.session_view is not None- assert session2.session_cache_manager is not None- assert session2.session_cache_manager.path == str(notebook_path)- assert len(session2.session_view.operations) == len(- session.session_view.operations- )-- # Test syncing with no file path- app_file_manager_no_path = AppFileManager.from_app(InternalApp(App()))- session3 = SessionImpl(- initialization_id="test3",- session_consumer=session_consumer,- queue_manager=queue_manager,- kernel_manager=kernel_manager,- app_file_manager=app_file_manager_no_path,- config_manager=get_default_config_manager(current_path=None),- ttl_seconds=None,- )- session3.sync_session_view_from_cache()- # Should create a new empty session view since no path exists- assert session3.session_view is not None- assert session3.session_cache_manager is not None- assert session3.session_cache_manager.path is None-- # Cleanup- session.close()- session2.close()- session3.close()- if kernel_manager.kernel_task:- kernel_manager.kernel_task.join()+ if new_path.exists(): # noqa: ASYNC240+ os.remove(new_path)+ if tmp_path1.exists(): # noqa: ASYNC240+ os.remove(tmp_path1)@save_and_restore_main