Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

refactor: split up SessionManager (#7534)

Myles Scolnickae1e00428569
diff --git a/marimo/_ai/_tools/base.py b/marimo/_ai/_tools/base.py
index 8491412cac..e222086196 100644
--- a/marimo/_ai/_tools/base.py
+++ b/marimo/_ai/_tools/base.py
@@ -79,8 +79,8 @@ class ToolContext:
return app
def get_session(self, session_id: SessionId) -> Session:
- session_manager = self.session_manager
- if session_id not in session_manager.sessions:
+ session = self.session_manager.get_session(session_id)
+ if session is None:
raise ToolExecutionError(
f"Session {session_id} not found",
code="SESSION_NOT_FOUND",
@@ -88,7 +88,7 @@ class ToolContext:
suggested_fix="Use get_active_notebooks to find valid session IDs",
meta={"session_id": session_id},
)
- return session_manager.sessions[session_id]
+ return session
def get_cell_ops(self, session_id: SessionId, cell_id: CellId_t) -> CellOp:
session_view = self.get_session(session_id).session_view
diff --git a/marimo/_server/notebook/file_manager.py b/marimo/_server/notebook/file_manager.py
index 00defd2049..9954a55df0 100644
--- a/marimo/_server/notebook/file_manager.py
+++ b/marimo/_server/notebook/file_manager.py
@@ -99,8 +99,10 @@ class AppFileManager:
Set of cell IDs that were added, deleted, or modified
"""
prev_cell_manager = self.app.cell_manager
- self.app = self._load_app(self.path)
- self.app.cell_manager.sort_cell_ids_by_similarity(prev_cell_manager)
+ new_app = self._load_app(self.path)
+ new_app.cell_manager.sort_cell_ids_by_similarity(prev_cell_manager)
+ # Only update self.app after successful reload
+ self.app = new_app
# Return the changed cell IDs
prev_cell_ids = set(prev_cell_manager.cell_ids())
diff --git a/marimo/_server/sessions/events.py b/marimo/_server/sessions/events.py
new file mode 100644
index 0000000000..57f2325f89
--- /dev/null
+++ b/marimo/_server/sessions/events.py
@@ -0,0 +1,66 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Event system for session lifecycle events.
+
+Provides an event bus and listeners for session creation, closure, and resumption.
+"""
+
+from __future__ import annotations
+
+from typing import Protocol
+
+from marimo._server.sessions.session import Session
+from marimo._types.ids import SessionId
+
+
+class SessionEventListener(Protocol):
+ """Protocol for session event listeners."""
+
+ async def on_session_created(self, session: Session) -> None:
+ """Called when a session is created."""
+ ...
+
+ async def on_session_closed(self, session: Session) -> None:
+ """Called when a session is closed."""
+ ...
+
+ async def on_session_resumed(
+ self, session: Session, old_id: SessionId
+ ) -> None:
+ """Called when a session is resumed with a new ID."""
+ ...
+
+
+class SessionEventBus:
+ """Event bus for coordinating session lifecycle events.
+
+ Allows components to react to session events without tight coupling.
+ """
+
+ def __init__(self) -> None:
+ self._listeners: list[SessionEventListener] = []
+
+ def subscribe(self, listener: SessionEventListener) -> None:
+ """Subscribe a listener to session events."""
+ self._listeners.append(listener)
+
+ def unsubscribe(self, listener: SessionEventListener) -> None:
+ """Unsubscribe a listener from session events."""
+ if listener in self._listeners:
+ self._listeners.remove(listener)
+
+ async def emit_session_created(self, session: Session) -> None:
+ """Emit a session created event."""
+ for listener in self._listeners:
+ await listener.on_session_created(session)
+
+ async def emit_session_closed(self, session: Session) -> None:
+ """Emit a session closed event."""
+ for listener in self._listeners:
+ await listener.on_session_closed(session)
+
+ async def emit_session_resumed(
+ self, session: Session, old_id: SessionId
+ ) -> None:
+ """Emit a session resumed event."""
+ for listener in self._listeners:
+ await listener.on_session_resumed(session, old_id)
diff --git a/marimo/_server/sessions/file_change_handler.py b/marimo/_server/sessions/file_change_handler.py
new file mode 100644
index 0000000000..272510303b
--- /dev/null
+++ b/marimo/_server/sessions/file_change_handler.py
@@ -0,0 +1,245 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""File change handling for marimo notebooks.
+
+Provides strategies for handling file changes in different session modes.
+"""
+
+from __future__ import annotations
+
+import asyncio
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Optional, Protocol
+
+from marimo import _loggers
+from marimo._config.manager import MarimoConfigManager
+from marimo._messaging.ops import Reload, UpdateCellCodes, UpdateCellIdsRequest
+from marimo._runtime.requests import DeleteCellRequest, SyncGraphRequest
+from marimo._server.model import SessionMode
+from marimo._server.sessions.session import Session
+from marimo._types.ids import CellId_t
+from marimo._utils import async_path
+
+LOGGER = _loggers.marimo_logger()
+
+if TYPE_CHECKING:
+ from pathlib import Path
+
+
+@dataclass
+class FileChangeResult:
+ """Result of handling a file change."""
+
+ handled: bool
+ error: Optional[str] = None
+ changed_cell_ids: Optional[set[CellId_t]] = None
+
+
+class ReloadStrategy(Protocol):
+ """Protocol for file reload strategies."""
+
+ def handle_reload(
+ self, session: Session, *, changed_cell_ids: set[CellId_t]
+ ) -> None:
+ """Handle reloading after file change.
+
+ Args:
+ session: The session to reload
+ changed_cell_ids: Set of cell IDs that changed
+ """
+ ...
+
+
+class EditModeReloadStrategy(ReloadStrategy):
+ """Reload strategy for edit mode.
+
+ In edit mode, we update cell IDs and codes, and optionally auto-run
+ changed cells based on configuration.
+ """
+
+ def __init__(self, config_manager: MarimoConfigManager) -> None:
+ self._config_manager = config_manager
+
+ def handle_reload(
+ self, session: Session, *, changed_cell_ids: set[CellId_t]
+ ) -> None:
+ """Handle reload in edit mode with optional auto-run."""
+ # Get the latest codes and cell IDs
+ codes = list(session.app_file_manager.app.cell_manager.codes())
+ cell_ids = list(session.app_file_manager.app.cell_manager.cell_ids())
+
+ LOGGER.info(
+ f"File changed: {session.app_file_manager.path}. "
+ f"num_cell_ids: {len(cell_ids)}, num_codes: {len(codes)}, "
+ f"changed_cell_ids: {changed_cell_ids}"
+ )
+
+ # Send the updated cell IDs to the frontend
+ session.write_operation(
+ UpdateCellIdsRequest(cell_ids=cell_ids),
+ from_consumer_id=None,
+ )
+
+ # Check if we should auto-run cells based on config
+ watcher_on_save = self._config_manager.get_config()["runtime"][
+ "watcher_on_save"
+ ]
+ should_autorun = watcher_on_save == "autorun"
+
+ # Determine deleted cells
+ deleted = {
+ cell_id for cell_id in changed_cell_ids if cell_id not in cell_ids
+ }
+
+ # Auto-run cells if configured
+ if should_autorun:
+ changed_cell_ids_list = list(changed_cell_ids - deleted)
+ cells = dict(zip(cell_ids, codes))
+
+ session.put_control_request(
+ SyncGraphRequest(
+ cells=cells,
+ run_ids=changed_cell_ids_list,
+ delete_ids=list(deleted),
+ ),
+ from_consumer_id=None,
+ )
+ else:
+ # Just send deletes and code updates
+ for to_delete in deleted:
+ session.put_control_request(
+ DeleteCellRequest(cell_id=to_delete),
+ from_consumer_id=None,
+ )
+ if cell_ids:
+ session.write_operation(
+ UpdateCellCodes(
+ cell_ids=cell_ids,
+ codes=codes,
+ code_is_stale=True,
+ ),
+ from_consumer_id=None,
+ )
+
+
+class RunModeReloadStrategy:
+ """Reload strategy for run mode.
+
+ In run mode, we simply send a reload operation to the frontend.
+ """
+
+ def handle_reload(
+ self, session: Session, *, changed_cell_ids: set[CellId_t]
+ ) -> None:
+ """Handle reload in run mode by sending Reload operation."""
+ del changed_cell_ids
+ session.write_operation(Reload(), from_consumer_id=None)
+
+
+class FileChangeCoordinator:
+ """Coordinates file change handling with proper locking and strategies.
+
+ This class handles the complexities of file watching, including
+ preventing duplicate reloads and delegating to mode-specific strategies.
+ """
+
+ def __init__(
+ self,
+ reload_strategy: ReloadStrategy,
+ ) -> None:
+ """Initialize the file change coordinator.
+
+ Args:
+ reload_strategy: Strategy for handling reloads
+ """
+ self._reload_strategy = reload_strategy
+ # Track ongoing file change operations to prevent duplicates
+ self._file_change_locks: dict[str, asyncio.Lock] = {}
+
+ async def handle_change(
+ self, file_path: Path, session: Session
+ ) -> FileChangeResult:
+ """Handle a file change for a session.
+
+ This method reloads the notebook and sends appropriate operations
+ to the frontend when a marimo file is modified.
+
+ Args:
+ file_path: The path to the file that changed
+ session: The session associated with the file
+
+ Returns:
+ FileChangeResult indicating success or failure
+ """
+ abs_file_path = await async_path.abspath(file_path)
+
+ # Use a lock to prevent concurrent processing of the same file
+ if str(abs_file_path) not in self._file_change_locks:
+ self._file_change_locks[str(abs_file_path)] = asyncio.Lock()
+
+ async with self._file_change_locks[str(abs_file_path)]:
+ return self._handle_file_change_locked(abs_file_path, session)
+
+ def _handle_file_change_locked(
+ self, file_path: str, session: Session
+ ) -> FileChangeResult:
+ """Handle file change with lock already acquired.
+
+ Args:
+ file_path: Absolute path to the file that changed
+ session: The session associated with the file
+
+ Returns:
+ FileChangeResult indicating success or failure
+ """
+ LOGGER.debug(f"{file_path} was modified, handling {session}")
+
+ # Verify the session is for this file
+ if session.app_file_manager.path != file_path:
+ return FileChangeResult(
+ handled=False,
+ error=f"Session path mismatch: {session.app_file_manager.path} != {file_path}",
+ )
+
+ # Check if the file content matches the last save
+ # to avoid reloading our own writes
+ if session.app_file_manager.file_content_matches_last_save():
+ LOGGER.debug(
+ f"File {file_path} content matches last save, skipping reload"
+ )
+ return FileChangeResult(handled=False)
+
+ # Reload the file manager to get the latest code
+ try:
+ changed_cell_ids = session.app_file_manager.reload()
+ except Exception as e:
+ # If there are syntax errors, we just skip
+ # and don't send the changes
+ LOGGER.error(f"Error loading file: {e}")
+ return FileChangeResult(handled=False, error=str(e))
+
+ # Delegate to the reload strategy
+ self._reload_strategy.handle_reload(
+ session, changed_cell_ids=changed_cell_ids
+ )
+
+ return FileChangeResult(
+ handled=True, changed_cell_ids=changed_cell_ids
+ )
+
+
+def create_reload_strategy(
+ mode: SessionMode, config_manager: MarimoConfigManager
+) -> ReloadStrategy:
+ """Factory function to create the appropriate reload strategy.
+
+ Args:
+ mode: The session mode
+ config_manager: Configuration manager
+
+ Returns:
+ The appropriate reload strategy for the mode
+ """
+ if mode == SessionMode.EDIT:
+ return EditModeReloadStrategy(config_manager)
+ else:
+ return RunModeReloadStrategy()
diff --git a/marimo/_server/sessions/file_watcher_integration.py b/marimo/_server/sessions/file_watcher_integration.py
new file mode 100644
index 0000000000..de4edac57f
--- /dev/null
+++ b/marimo/_server/sessions/file_watcher_integration.py
@@ -0,0 +1,182 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""File watcher integration for sessions.
+
+Provides clean abstraction for attaching/detaching file watchers to sessions
+without leaking implementation details or monkey-patching.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+from typing import TYPE_CHECKING, Callable
+
+from marimo import _loggers
+from marimo._server.sessions.events import SessionEventListener
+from 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_path
+from marimo._utils.file_watcher import FileWatcherManager
+
+LOGGER = _loggers.marimo_logger()
+
+if TYPE_CHECKING:
+ from collections.abc import Awaitable
+
+
+class SessionFileWatcherLifecycle:
+ """Manages file watcher lifecycle for sessions."""
+
+ def __init__(
+ self,
+ watcher_manager: FileWatcherManager,
+ file_change_callback: Callable[[Path, Session], Awaitable[None]],
+ repository: SessionRepository,
+ ) -> None:
+ """Initialize the file watcher lifecycle manager.
+
+ Args:
+ watcher_manager: The underlying file watcher manager
+ file_change_callback: Callback to invoke when files change
+ repository: Session repository for ID lookups
+ """
+ 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]]
+ ] = {}
+
+ def attach(self, session: Session) -> None:
+ """Attach a file watcher to a 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 manager
+ self._watcher_manager.add_callback(
+ Path(session.app_file_manager.path), on_file_changed
+ )
+
+ LOGGER.debug(
+ "Attached file watcher for session %s at path %s",
+ session_id,
+ session.app_file_manager.path,
+ )
+
+ def detach(self, session: Session) -> None:
+ """Detach a file watcher from a session.
+
+ 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:
+ return
+
+ callback = self._session_callbacks.get(session)
+ if callback is None:
+ LOGGER.debug(
+ "No file watcher callback found for session %s",
+ session_id,
+ )
+ return
+
+ # Remove from watcher manager
+ self._watcher_manager.remove_callback(
+ Path(session.app_file_manager.path), callback
+ )
+
+ # Remove from our registry
+ del self._session_callbacks[session]
+
+ LOGGER.debug(
+ "Detached file watcher for session %s from path %s",
+ session_id,
+ 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.
+
+ 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:
+ # Remove old watcher
+ self._watcher_manager.remove_callback(old_path, callback)
+
+ # Attach new watcher
+ self.attach(session)
+
+ LOGGER.debug(
+ "Updated file watcher for session %s from %s to %s",
+ session_id,
+ old_path,
+ new_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."""
+ pass
diff --git a/marimo/_server/sessions/listeners.py b/marimo/_server/sessions/listeners.py
new file mode 100644
index 0000000000..43f986b3fd
--- /dev/null
+++ b/marimo/_server/sessions/listeners.py
@@ -0,0 +1,42 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Common session event listeners.
+
+Provides reusable listeners for common session lifecycle concerns like
+tracking recent files, managing caches, etc.
+"""
+
+from __future__ import annotations
+
+from marimo._server.recents import RecentFilesManager
+from marimo._server.sessions.events import SessionEventListener
+from marimo._server.sessions.session import Session
+from marimo._types.ids import SessionId
+
+
+class RecentsTrackerListener(SessionEventListener):
+ """Event listener that tracks recently accessed files."""
+
+ def __init__(self, recents_manager: RecentFilesManager) -> None:
+ """Initialize the recents tracker listener.
+
+ Args:
+ recents_manager: Manager for recent files
+ """
+ self._recents = recents_manager
+
+ async def on_session_created(self, session: Session) -> None:
+ """Update recent files when a session is created."""
+ if session.app_file_manager.path:
+ self._recents.touch(session.app_file_manager.path)
+
+ async def on_session_closed(self, session: Session) -> None:
+ """No action needed on session close."""
+ pass
+
+ async def on_session_resumed(
+ self, session: Session, old_id: SessionId
+ ) -> None:
+ """Update recent files when a session is resumed."""
+ del old_id
+ if session.app_file_manager.path:
+ self._recents.touch(session.app_file_manager.path)
diff --git a/marimo/_server/sessions/resume_strategies.py b/marimo/_server/sessions/resume_strategies.py
new file mode 100644
index 0000000000..01ea87bc80
--- /dev/null
+++ b/marimo/_server/sessions/resume_strategies.py
@@ -0,0 +1,139 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Session resume strategies for different modes.
+
+Provides mode-specific logic for determining whether and how to resume sessions.
+"""
+
+from __future__ import annotations
+
+from typing import Optional, Protocol
+
+from marimo import _loggers
+from marimo._server.file_router import MarimoFileKey
+from marimo._server.model import ConnectionState, SessionMode
+from marimo._server.sessions.session import Session
+from marimo._server.sessions.session_repository import SessionRepository
+from marimo._types.ids import SessionId
+
+LOGGER = _loggers.marimo_logger()
+
+
+class SessionResumeStrategy(Protocol):
+ """Protocol for session resume strategies."""
+
+ def try_resume(
+ self,
+ new_session_id: SessionId,
+ file_key: MarimoFileKey,
+ ) -> Optional[Session]:
+ """Try to resume a session.
+
+ Args:
+ new_session_id: The new session ID to assign
+ file_key: The file key for the session
+
+ Returns:
+ The resumed session if successful, None otherwise
+ """
+ ...
+
+
+class EditModeResumeStrategy(SessionResumeStrategy):
+ """Resume strategy for edit mode.
+
+ In edit mode, we can resume orphaned sessions for the same file.
+ Only one session per file is allowed in edit mode.
+ """
+
+ def __init__(self, repository: SessionRepository) -> None:
+ self._repository = repository
+
+ def try_resume(
+ self,
+ new_session_id: SessionId,
+ file_key: MarimoFileKey,
+ ) -> Optional[Session]:
+ """Try to resume an orphaned session for the same file."""
+ import os
+
+ # Find sessions with the same file
+ sessions_with_file = []
+ for session in self._repository.get_all():
+ session_id = self._repository.get_session_id(session)
+ if session_id and session.app_file_manager.path == os.path.abspath(
+ file_key
+ ):
+ sessions_with_file.append((session_id, session))
+
+ if len(sessions_with_file) == 0:
+ return None
+
+ if len(sessions_with_file) > 1:
+ from marimo._server.exceptions import InvalidSessionException
+
+ raise InvalidSessionException(
+ "Only one session should exist while editing"
+ )
+
+ session_id, session = sessions_with_file[0]
+ connection_state = session.connection_state()
+
+ if connection_state == ConnectionState.ORPHANED:
+ LOGGER.debug(
+ f"Found a resumable EDIT session: prev_id={session_id}"
+ )
+ # Update session ID
+ self._repository.update_session_id_sync(session_id, new_session_id)
+ return session
+
+ LOGGER.debug(
+ "Session is not resumable, current state: %s",
+ connection_state,
+ )
+ return None
+
+
+class RunModeResumeStrategy(SessionResumeStrategy):
+ """Resume strategy for run mode.
+
+ In run mode, we can only resume if the session ID matches and it's orphaned.
+ Multiple sessions can exist for the same file in run mode.
+ """
+
+ def __init__(self, repository: SessionRepository) -> None:
+ self._repository = repository
+
+ def try_resume(
+ self,
+ new_session_id: SessionId,
+ file_key: MarimoFileKey, # noqa: ARG002
+ ) -> Optional[Session]:
+ """Try to resume a session with matching ID if orphaned."""
+ session = self._repository.get_sync(new_session_id)
+
+ if session and session.connection_state() == ConnectionState.ORPHANED:
+ LOGGER.debug(
+ "Found a resumable RUN session: prev_id=%s",
+ new_session_id,
+ )
+ return session
+
+ return None
+
+
+def create_resume_strategy(
+ mode: SessionMode, repository: SessionRepository
+) -> SessionResumeStrategy:
+ """Factory function to create the appropriate resume strategy.
+
+ Args:
+ mode: The session mode
+ repository: The session repository
+
+ Returns:
+ The appropriate resume strategy for the mode
+ """
+ if mode == SessionMode.EDIT:
+ return EditModeResumeStrategy(repository)
+ else:
+ return RunModeResumeStrategy(repository)
diff --git a/marimo/_server/sessions/session.py b/marimo/_server/sessions/session.py
index 83ee8b8603..cbffcd912a 100644
--- a/marimo/_server/sessions/session.py
+++ b/marimo/_server/sessions/session.py
@@ -229,15 +229,16 @@ class SessionImpl(Session):
else:
cell_ids = request.run_ids
codes = [request.cells[cell_id] for cell_id in cell_ids]
- self.room.broadcast(
- UpdateCellCodes(
- cell_ids=cell_ids,
- codes=codes,
- # Not stale because we just ran the code
- code_is_stale=False,
- ),
- except_consumer=from_consumer_id,
- )
+ if cell_ids:
+ self.room.broadcast(
+ UpdateCellCodes(
+ cell_ids=cell_ids,
+ codes=codes,
+ # Not stale because we just ran the code
+ code_is_stale=False,
+ ),
+ except_consumer=from_consumer_id,
+ )
if len(cell_ids) == 1:
self.room.broadcast(
FocusCell(cell_id=cell_ids[0]),
diff --git a/marimo/_server/sessions/session_manager.py b/marimo/_server/sessions/session_manager.py
index bba5bd37e6..38729b3254 100644
--- a/marimo/_server/sessions/session_manager.py
+++ b/marimo/_server/sessions/session_manager.py
@@ -11,47 +11,51 @@ from __future__ import annotations
import asyncio
import os
from pathlib import Path
-from typing import Optional
+from typing import TYPE_CHECKING, Optional, TypeVar
from marimo import _loggers
from marimo._config.manager import MarimoConfigManager
-from marimo._messaging.ops import (
- Reload,
- UpdateCellCodes,
- UpdateCellIdsRequest,
-)
from marimo._runtime.requests import (
- DeleteCellRequest,
SerializedCLIArgs,
SerializedQueryParams,
- SyncGraphRequest,
)
-from marimo._server.exceptions import InvalidSessionException
from marimo._server.file_router import AppFileRouter, MarimoFileKey
from marimo._server.lsp import LspServer
from marimo._server.model import ConnectionState, SessionConsumer, SessionMode
from marimo._server.notebook import AppFileManager
from marimo._server.recents import RecentFilesManager
+from marimo._server.sessions.events import SessionEventBus
+from marimo._server.sessions.file_change_handler import (
+ FileChangeCoordinator,
+ create_reload_strategy,
+)
+from marimo._server.sessions.file_watcher_integration import (
+ FileWatcherAttachmentListener,
+ SessionFileWatcherLifecycle,
+)
+from marimo._server.sessions.listeners import RecentsTrackerListener
+from marimo._server.sessions.resume_strategies import create_resume_strategy
from marimo._server.sessions.session import Session, SessionImpl
+from marimo._server.sessions.session_repository import SessionRepository
+from marimo._server.sessions.token_manager import TokenManager
from marimo._server.tokens import AuthToken, SkewProtectionToken
from marimo._types.ids import ConsumerId, SessionId
-from marimo._utils import async_path
from marimo._utils.file_watcher import FileWatcherManager
+if TYPE_CHECKING:
+ from collections.abc import Awaitable, Coroutine, Mapping
+
LOGGER = _loggers.marimo_logger()
class SessionManager:
- """Mapping from client session IDs to sessions.
-
- Maintains a mapping from client session IDs to client sessions;
- there is exactly one session per client.
+ """Orchestrates session management
- The SessionManager also encapsulates state common to all sessions:
- - the app filename
- - the app mode (edit or run)
- - the auth token
- - the skew-protection token
+ - SessionRepository: stores sessions
+ - TokenManager: manages auth tokens
+ - SessionEventBus: coordinates lifecycle events
+ - ResumeStrategy: handles session resumption logic
+ - FileWatcherLifecycle: manages file watching
"""
def __init__(
@@ -70,61 +74,93 @@ class SessionManager:
ttl_seconds: Optional[int],
watch: bool = False,
) -> None:
+ # Core configuration
self.file_router = file_router
self.mode = mode
self.quiet = quiet
- self.sessions: dict[SessionId, Session] = {}
self.include_code = include_code
self.ttl_seconds = ttl_seconds
self.lsp_server = lsp_server
- self.file_change_handler = SessionFileChangeHandler(
- self, config_manager
- )
- self.watcher_manager = FileWatcherManager()
- self.watch = watch
- self.recents = RecentFilesManager()
self.cli_args = cli_args
self.argv = argv
self.redirect_console_to_browser = redirect_console_to_browser
-
- # We should access the config_manager from the session if possible
- # since this will contain config-level overrides
self._config_manager = config_manager
+ self._repository = SessionRepository()
+
def _get_code() -> str:
app = file_router.get_single_app_file_manager(
- default_width=self._config_manager.default_width,
- default_auto_download=self._config_manager.default_auto_download,
- default_sql_output=self._config_manager.default_sql_output,
+ default_width=config_manager.default_width,
+ default_auto_download=config_manager.default_auto_download,
+ default_sql_output=config_manager.default_sql_output,
).app
return "".join(code for code in app.cell_manager.codes())
- # Auth token and Skew-protection token
- if mode == SessionMode.EDIT:
- # In edit mode, if no auth token is provided,
- # generate a random token
- self.auth_token = (
- AuthToken.random() if auth_token is None else auth_token
- )
- self.skew_protection_token = SkewProtectionToken.random()
- else:
- source_code = _get_code()
- # Because run-mode is read-only and we could have multiple
- # servers for the same app (going to sleep or autoscaling),
- # we default to a token based on the app's code
- self.auth_token = (
- AuthToken.from_code(source_code)
- if auth_token is None
- else auth_token
- )
- self.skew_protection_token = SkewProtectionToken.from_code(
- source_code
+ source_code = None if mode == SessionMode.EDIT else _get_code()
+ self._token_manager = TokenManager(
+ mode=mode,
+ auth_token=auth_token,
+ source_code=source_code,
+ )
+
+ # Initialize resume strategy
+ self._resume_strategy = create_resume_strategy(mode, self._repository)
+
+ # Initialize event bus and listeners
+ self._event_bus = SessionEventBus()
+
+ # Add recents tracking listener
+ self.recents = RecentFilesManager()
+ self._event_bus.subscribe(RecentsTrackerListener(self.recents))
+
+ # Initialize file watching components if enabled
+ self.watch = watch
+ if watch:
+ self._setup_file_watching()
+
+ @property
+ def auth_token(self) -> AuthToken:
+ """Get the auth token."""
+ return self._token_manager.auth_token
+
+ @property
+ def skew_protection_token(self) -> SkewProtectionToken:
+ """Get the skew protection token."""
+ return self._token_manager.skew_protection_token
+
+ @property
+ def sessions(self) -> Mapping[SessionId, Session]:
+ """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.
- """
+ """Get the app manager for the given key."""
return self.file_router.get_file_manager(
key,
default_width=self._config_manager.default_width,
@@ -139,66 +175,50 @@ class SessionManager:
query_params: SerializedQueryParams,
file_key: MarimoFileKey,
) -> Session:
- """Create a new session"""
+ """Create a new session."""
LOGGER.debug("Creating new session for id %s", session_id)
- if session_id not in self.sessions:
- app_file_manager = self.file_router.get_file_manager(
- file_key,
- default_width=self._config_manager.default_width,
- default_auto_download=self._config_manager.default_auto_download,
- default_sql_output=self._config_manager.default_sql_output,
- )
-
- if app_file_manager.path:
- self.recents.touch(app_file_manager.path)
-
- from marimo._runtime.requests import AppMetadata
-
- session = SessionImpl.create(
- initialization_id=file_key,
- session_consumer=session_consumer,
- mode=self.mode,
- app_metadata=AppMetadata(
- query_params=query_params,
- filename=app_file_manager.path,
- cli_args=self.cli_args,
- argv=self.argv,
- app_config=app_file_manager.app.config,
- ),
- app_file_manager=app_file_manager,
- config_manager=self._config_manager,
- virtual_files_supported=True,
- redirect_console_to_browser=self.redirect_console_to_browser,
- ttl_seconds=self.ttl_seconds,
- )
- self.sessions[session_id] = session
-
- # Start file watcher if enabled
- if self.watch and app_file_manager.path:
- self._start_file_watcher_for_session(session)
- return self.sessions[session_id]
+ # Check if session already exists
+ existing = self._repository.get_sync(session_id)
+ if existing:
+ return existing
- def _start_file_watcher_for_session(self, session: Session) -> None:
- """Start a file watcher for a session."""
- if not session.app_file_manager.path:
- return
+ # Get app file manager
+ app_file_manager = self.file_router.get_file_manager(
+ file_key,
+ default_width=self._config_manager.default_width,
+ default_auto_download=self._config_manager.default_auto_download,
+ default_sql_output=self._config_manager.default_sql_output,
+ )
- 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
+ # Create the session
+ from marimo._runtime.requests import AppMetadata
+
+ session = SessionImpl.create(
+ initialization_id=file_key,
+ session_consumer=session_consumer,
+ mode=self.mode,
+ app_metadata=AppMetadata(
+ query_params=query_params,
+ filename=app_file_manager.path,
+ cli_args=self.cli_args,
+ argv=self.argv,
+ app_config=app_file_manager.app.config,
+ ),
+ app_file_manager=app_file_manager,
+ config_manager=self._config_manager,
+ virtual_files_supported=True,
+ redirect_console_to_browser=self.redirect_console_to_browser,
+ ttl_seconds=self.ttl_seconds,
+ )
- # Use the centralized file change handler
- await self.file_change_handler.handle_file_change(
- str(path), [session]
- )
+ # Add to repository
+ self._repository.add_sync(session_id, session)
- session._unsubscribe_file_watcher_ = on_file_changed # type: ignore
+ # Emit session created event (triggers file watcher attachment, recents, etc.)
+ run_async(self._event_bus.emit_session_created(session))
- self.watcher_manager.add_callback(
- Path(session.app_file_manager.path), on_file_changed
- )
+ return session
def handle_file_rename_for_watch(
self, session_id: SessionId, prev_path: Optional[str], new_path: str
@@ -223,130 +243,106 @@ class SessionManager:
session.session_cache_manager.rename_path(new_path)
try:
- if self.watch:
- # Remove the old file watcher if it exists
+ if self.watch and self._file_watcher_lifecycle:
+ # Update the file watcher
if prev_path:
- self.watcher_manager.remove_callback(
- Path(prev_path),
- session._unsubscribe_file_watcher_, # type: ignore
+ self._file_watcher_lifecycle.update(
+ session, Path(prev_path), Path(new_path)
)
-
- # Add a watcher for the new path if needed
- self._start_file_watcher_for_session(session)
+ else:
+ self._file_watcher_lifecycle.attach(session)
return True, None
except Exception as e:
LOGGER.error(f"Error handling file rename: {e}")
- if self.watch:
- self._start_file_watcher_for_session(session)
+ 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:
- """Handle a file change."""
- await self.file_change_handler.handle_file_change(
- path, list(self.sessions.values())
- )
+ """Handle a file change for all relevant sessions."""
+ # Find all sessions associated with this file
+ sessions_for_file = self._repository.get_by_file_path(path)
+
+ if not sessions_for_file:
+ 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
+ )
+ 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]:
- session = self.sessions.get(session_id)
+ """Get a session by ID, checking both direct and consumer IDs."""
+ session = self._repository.get_sync(session_id)
if session:
return session
- # Search for kiosk sessions
- for session in self.sessions.values():
- if ConsumerId(session_id) in session.consumers.values():
- return session
-
- return None
+ # Search for kiosk sessions by consumer ID
+ return self._repository.get_by_consumer_id(ConsumerId(session_id))
def get_session_by_file_key(
self, file_key: MarimoFileKey
) -> Optional[Session]:
- for session in self.sessions.values():
- if (
- session.initialization_id == file_key
- or session.app_file_manager.path == os.path.abspath(file_key)
- ):
- return session
- return None
+ """Get a session by file key."""
+ return self._repository.get_by_file_key(file_key)
def maybe_resume_session(
self, new_session_id: SessionId, file_key: MarimoFileKey
) -> Optional[Session]:
- """
- Try to resume a session if one is resumable.
+ """Try to resume a session if one is resumable.
+
If it is resumable, return the session and update the session id.
"""
+ # Cleanup sessions with dead kernels first
+ self._cleanup_dead_sessions()
- # If in run mode, only resume the session if it is orphaned and has
- # the same session id, otherwise we want to create a new session
- if self.mode == SessionMode.RUN:
- maybe_session = self.get_session(new_session_id)
- if (
- maybe_session
- and maybe_session.connection_state()
- == ConnectionState.ORPHANED
- ):
- LOGGER.debug(
- "Found a resumable RUN session: prev_id=%s",
- new_session_id,
+ # Try to resume using the strategy
+ resumed_session = self._resume_strategy.try_resume(
+ new_session_id, file_key
+ )
+
+ if resumed_session:
+ # Emit resume event (use new_session_id as both old and new since
+ # the strategy already updated it)
+ run_async(
+ self._event_bus.emit_session_resumed(
+ resumed_session, new_session_id
)
- return maybe_session
- return None
-
- # Cleanup sessions with dead kernels; materializing as a list because
- # close_sessions mutates self.sessions
- for session_id, session in list(self.sessions.items()):
- task = session.kernel_manager.kernel_task
- if task is not None and not task.is_alive():
- self.close_session(session_id)
-
- # Should only return an orphaned session
- sessions_with_the_same_file: dict[SessionId, Session] = {
- session_id: session
- for session_id, session in self.sessions.items()
- if session.app_file_manager.path == os.path.abspath(file_key)
- }
-
- if len(sessions_with_the_same_file) == 0:
- return None
- if len(sessions_with_the_same_file) > 1:
- raise InvalidSessionException(
- "Only one session should exist while editing"
)
- (session_id, session) = next(iter(sessions_with_the_same_file.items()))
- connection_state = session.connection_state()
- if connection_state == ConnectionState.ORPHANED:
- LOGGER.debug(
- f"Found a resumable EDIT session: prev_id={session_id}"
- )
- # Set new session and remove old session
- self.sessions[new_session_id] = session
- # If the ID is the same, we don't need to delete the old session
- if new_session_id != session_id and session_id in self.sessions:
- del self.sessions[session_id]
- return session
+ return resumed_session
- LOGGER.debug(
- "Session is not resumable, current state: %s",
- connection_state,
- )
- return None
+ def _cleanup_dead_sessions(self) -> None:
+ """Remove sessions with dead kernels."""
+ for session_id in list(self._repository.get_all_session_ids()):
+ session = self._repository.get_sync(session_id)
+ if session:
+ task = session.kernel_manager.kernel_task
+ if task is not None and not task.is_alive():
+ self.close_session(session_id)
def any_clients_connected(self, key: MarimoFileKey) -> bool:
"""Returns True if at least one client has an open socket."""
if key.startswith(AppFileRouter.NEW_FILE):
return False
- for session in self.sessions.values():
- if session.connection_state() == ConnectionState.OPEN and (
- session.app_file_manager.path == os.path.abspath(key)
- ):
- return True
- return False
+ sessions_for_file = self._repository.get_by_file_path(key)
+ return any(
+ session.connection_state() == ConnectionState.OPEN
+ for session in sessions_for_file
+ )
async def start_lsp_server(self) -> None:
"""Starts the lsp server if it is not already started.
@@ -364,181 +360,69 @@ class SessionManager:
LOGGER.error(
f"LSP server startup failed: {alert.title} - {alert.description}"
)
- for session in self.sessions.values():
+ for session in self._repository.get_all():
session.write_operation(alert, from_consumer_id=None)
return
else:
LOGGER.info("LSP server started successfully")
def close_session(self, session_id: SessionId) -> bool:
- """Close a session and remove its file watcher if it has one."""
+ """Close a session."""
LOGGER.debug("Closing session %s", session_id)
- session = self.get_session(session_id)
+ session = self._repository.remove_sync(session_id)
if session is None:
return False
- # Remove the file watcher callback for this session
- if session.app_file_manager.path and self.watch:
- self.watcher_manager.remove_callback(
- Path(session.app_file_manager.path),
- session._unsubscribe_file_watcher_, # type: ignore
- )
+ run_async(self._event_bus.emit_session_closed(session))
session.close()
- if session_id in self.sessions:
- del self.sessions[session_id]
return True
def close_all_sessions(self) -> None:
- LOGGER.debug("Closing all sessions (sessions: %s)", self.sessions)
- for session in self.sessions.values():
- session.close()
+ """Close all sessions."""
+ session_ids = self._repository.get_all_session_ids()
+ LOGGER.debug("Closing all sessions (count: %s)", len(session_ids))
+ for session_id in session_ids:
+ self.close_session(session_id)
LOGGER.debug("Closed all sessions.")
- self.sessions = {}
def shutdown(self) -> None:
"""Shutdown the session manager and stop all file watchers."""
LOGGER.debug("Shutting down")
self.close_all_sessions()
self.lsp_server.stop()
- self.watcher_manager.stop_all()
+ if self.watch and hasattr(self, "watcher_manager"):
+ self.watcher_manager.stop_all()
def should_send_code_to_frontend(self) -> bool:
"""Returns True if the server can send messages to the frontend."""
return self.mode == SessionMode.EDIT or self.include_code
def get_active_connection_count(self) -> int:
- return len(
- [
- session
- for session in self.sessions.values()
- if session.connection_state() == ConnectionState.OPEN
- ]
- )
-
-
-class SessionFileChangeHandler:
- def __init__(
- self,
- session_manager: SessionManager,
- config_manager: MarimoConfigManager,
- ) -> None:
- self.session_manager = session_manager
- self.config_manager = config_manager
- # Track ongoing file change operations to prevent duplicates
- self._file_change_locks: dict[str, asyncio.Lock] = {}
+ """Get the number of sessions with active connections."""
+ return len(self._repository.get_active_sessions())
- async def handle_file_change(
- self, file_path: str, sessions: list[Session]
- ) -> None:
- """Handle file changes for marimo notebooks.
-
- This method reloads the notebook and sends appropriate operations
- to the frontend when a marimo file is modified.
- """
- abs_file_path = await async_path.abspath(file_path)
-
- # Use a lock to prevent concurrent processing of the same file
- if abs_file_path not in self._file_change_locks:
- self._file_change_locks[abs_file_path] = asyncio.Lock()
-
- async with self._file_change_locks[abs_file_path]:
- # Find all sessions associated with this file
- sessions_for_file: list[Session] = []
- for s in sessions:
- if s.app_file_manager.path == abs_file_path:
- sessions_for_file.append(s)
-
- if not sessions_for_file:
- # No active session for this file
- return
-
- if len(sessions_for_file) > 1:
- LOGGER.error(
- f"Only one session should exist for a file: {abs_file_path}"
- )
-
- self._handle_file_change(
- abs_file_path,
- sessions_for_file[0],
- )
-
- def _handle_file_change(
- self,
- file_path: str,
- session: Session,
- ) -> None:
- LOGGER.debug(f"{file_path} was modified, handling {session}")
-
- # Check if the file content matches the last save
- # to avoid reloading our own writes
- if session.app_file_manager.file_content_matches_last_save():
- LOGGER.debug(
- f"File {file_path} content matches last save, skipping reload"
- )
- return
- # Reload the file manager to get the latest code
- try:
- changed_cell_ids = session.app_file_manager.reload()
- except Exception as e:
- # If there are syntax errors, we just skip
- # and don't send the changes
- LOGGER.error(f"Error loading file: {e}")
- return
-
- # In run mode, we just call Reload()
- if self.session_manager.mode == SessionMode.RUN:
- session.write_operation(Reload(), from_consumer_id=None)
- return
-
- # Get the latest codes
- codes = list(session.app_file_manager.app.cell_manager.codes())
- cell_ids = list(session.app_file_manager.app.cell_manager.cell_ids())
+T = TypeVar("T")
- LOGGER.info(
- f"File changed: {file_path}. num_cell_ids: {len(cell_ids)}, num_codes: {len(codes)}, changed_cell_ids: {changed_cell_ids}"
- )
- # Send the updated cell ids and codes to the frontend
- session.write_operation(
- UpdateCellIdsRequest(cell_ids=cell_ids),
- from_consumer_id=None,
- )
+def run_async(coro: Coroutine[None, None, T] | Awaitable[T]) -> T:
+ """Run an async coroutine, handling various event loop states.
- # Check if we should auto-run cells based on config
- watcher_on_save = self.config_manager.get_config()["runtime"][
- "watcher_on_save"
- ]
- should_autorun = watcher_on_save == "autorun"
- deleted = {
- cell_id for cell_id in changed_cell_ids if cell_id not in cell_ids
- }
-
- # Auto-run cells if configured
- if should_autorun:
- changed_cell_ids_list = list(changed_cell_ids - deleted)
- cells = dict(zip(cell_ids, codes))
-
- session.put_control_request(
- SyncGraphRequest(
- cells=cells,
- run_ids=changed_cell_ids_list,
- delete_ids=list(deleted),
- ),
- from_consumer_id=None,
- )
+ 1. Event loop is running: create a task
+ 2. Event loop exists but not running: run_until_complete
+ 3. No event loop: create one with asyncio.run
+ """
+ try:
+ loop = asyncio.get_event_loop()
+ if loop.is_running():
+ # Create a task and return it (fire and forget)
+ # Note: This doesn't wait for completion
+ task = asyncio.create_task(coro) # type: ignore
+ return task # type: ignore
else:
- for to_delete in deleted:
- session.put_control_request(
- DeleteCellRequest(cell_id=to_delete),
- from_consumer_id=None,
- )
- session.write_operation(
- UpdateCellCodes(
- cell_ids=cell_ids,
- codes=codes,
- code_is_stale=True,
- ),
- from_consumer_id=None,
- )
+ # Run to completion
+ return loop.run_until_complete(coro) # type: ignore
+ except RuntimeError:
+ # No event loop exists, create one
+ return asyncio.run(coro) # type: ignore
diff --git a/marimo/_server/sessions/session_repository.py b/marimo/_server/sessions/session_repository.py
new file mode 100644
index 0000000000..19c882be74
--- /dev/null
+++ b/marimo/_server/sessions/session_repository.py
@@ -0,0 +1,165 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Repository for session storage and retrieval."""
+
+from __future__ import annotations
+
+import asyncio
+from typing import TYPE_CHECKING, Optional
+
+from marimo._server.file_router import MarimoFileKey
+from marimo._server.model import ConnectionState
+from marimo._server.sessions.session import Session
+from marimo._types.ids import ConsumerId, SessionId
+
+if TYPE_CHECKING:
+ from collections.abc import Mapping
+
+
+class SessionRepository:
+ """In-memory storage for sessions with thread-safe operations."""
+
+ def __init__(self) -> None:
+ self._sessions: dict[SessionId, Session] = {}
+ # Reverse mapping from session to session ID
+ self._session_to_id: dict[Session, SessionId] = {}
+ self._lock = asyncio.Lock()
+
+ async def add(self, session_id: SessionId, session: Session) -> None:
+ """Add a session to the repository."""
+ async with self._lock:
+ self._sessions[session_id] = session
+ self._session_to_id[session] = session_id
+
+ def add_sync(self, session_id: SessionId, session: Session) -> None:
+ """Add a session synchronously (use when not in async context)."""
+ self._sessions[session_id] = session
+ self._session_to_id[session] = session_id
+
+ async def get(self, session_id: SessionId) -> Optional[Session]:
+ """Get a session by its ID."""
+ async with self._lock:
+ return self._sessions.get(session_id)
+
+ def get_sync(self, session_id: SessionId) -> Optional[Session]:
+ """Get a session synchronously."""
+ return self._sessions.get(session_id)
+
+ def get_by_consumer_id(self, consumer_id: ConsumerId) -> Optional[Session]:
+ """Find a session by consumer ID (for kiosk mode)."""
+ for session in self._sessions.values():
+ if consumer_id in session.consumers.values():
+ return session
+ return None
+
+ def get_by_file_key(self, file_key: MarimoFileKey) -> Optional[Session]:
+ """Get a session by file key."""
+ import os
+
+ for session in self._sessions.values():
+ if (
+ session.initialization_id == file_key
+ or session.app_file_manager.path == os.path.abspath(file_key)
+ ):
+ return session
+ return None
+
+ def get_by_file_path(self, file_path: str) -> list[Session]:
+ """Get all sessions associated with a file path."""
+ import os
+
+ abs_path = os.path.abspath(file_path)
+ return [
+ session
+ for session in self._sessions.values()
+ if session.app_file_manager.path == abs_path
+ ]
+
+ @property
+ def sessions(self) -> Mapping[SessionId, Session]:
+ """Get all sessions as a dict."""
+ return dict(self._sessions)
+
+ def get_all(self) -> list[Session]:
+ """Get all sessions."""
+ return list(self._sessions.values())
+
+ def get_all_session_ids(self) -> list[SessionId]:
+ """Get all session IDs."""
+ return list(self._sessions.keys())
+
+ def get_active_sessions(self) -> list[Session]:
+ """Get all sessions with open connections."""
+ return [
+ session
+ for session in self._sessions.values()
+ if session.connection_state() == ConnectionState.OPEN
+ ]
+
+ async def remove(self, session_id: SessionId) -> Optional[Session]:
+ """Remove and return a session."""
+ async with self._lock:
+ session = self._sessions.pop(session_id, None)
+ if session and session in self._session_to_id:
+ del self._session_to_id[session]
+ return session
+
+ def remove_sync(self, session_id: SessionId) -> Optional[Session]:
+ """Remove a session synchronously."""
+ session = self._sessions.pop(session_id, None)
+ if session and session in self._session_to_id:
+ del self._session_to_id[session]
+ return session
+
+ def get_session_id(self, session: Session) -> Optional[SessionId]:
+ """Get the session ID for a session object."""
+ return self._session_to_id.get(session)
+
+ async def update_session_id(
+ self, old_id: SessionId, new_id: SessionId
+ ) -> bool:
+ """Update a session's ID (for resume operations)."""
+ async with self._lock:
+ if old_id not in self._sessions:
+ return False
+
+ session = self._sessions[old_id]
+ self._sessions[new_id] = session
+ self._session_to_id[session] = new_id
+
+ # Only delete if IDs are different
+ if new_id != old_id:
+ del self._sessions[old_id]
+
+ return True
+
+ def update_session_id_sync(
+ self, old_id: SessionId, new_id: SessionId
+ ) -> bool:
+ """Update a session's ID synchronously."""
+ if old_id not in self._sessions:
+ return False
+
+ session = self._sessions[old_id]
+ self._sessions[new_id] = session
+ self._session_to_id[session] = new_id
+
+ # Only delete if IDs are different
+ if new_id != old_id:
+ del self._sessions[old_id]
+
+ return True
+
+ async def clear(self) -> None:
+ """Clear all sessions."""
+ async with self._lock:
+ self._sessions.clear()
+ self._session_to_id.clear()
+
+ def clear_sync(self) -> None:
+ """Clear all sessions synchronously."""
+ self._sessions.clear()
+ self._session_to_id.clear()
+
+ def __len__(self) -> int:
+ """Get the number of sessions."""
+ return len(self._sessions)
diff --git a/marimo/_server/sessions/token_manager.py b/marimo/_server/sessions/token_manager.py
new file mode 100644
index 0000000000..1540ae588a
--- /dev/null
+++ b/marimo/_server/sessions/token_manager.py
@@ -0,0 +1,62 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Token management for authentication and skew protection.
+
+Manages creation and validation of auth tokens and skew protection tokens
+based on session mode.
+"""
+
+from __future__ import annotations
+
+from typing import Optional
+
+from marimo._server.model import SessionMode
+from marimo._server.tokens import AuthToken, SkewProtectionToken
+
+
+class TokenManager:
+ """Manages authentication and skew protection tokens."""
+
+ def __init__(
+ self,
+ mode: SessionMode,
+ auth_token: Optional[AuthToken] = None,
+ source_code: Optional[str] = None,
+ ) -> None:
+ """Initialize token manager.
+
+ Args:
+ mode: The session mode (edit or run)
+ auth_token: Optional pre-configured auth token
+ source_code: Source code for generating code-based tokens in run mode
+ """
+ self.mode = mode
+
+ # Create tokens based on mode
+ if mode == SessionMode.EDIT:
+ # In edit mode, use random tokens or provided token
+ self.auth_token = (
+ AuthToken.random() if auth_token is None else auth_token
+ )
+ self.skew_protection_token = SkewProtectionToken.random()
+ else:
+ # In run mode, use code-based tokens for consistency across instances
+ if source_code is None:
+ raise ValueError(
+ "source_code is required for run mode token generation"
+ )
+ self.auth_token = (
+ AuthToken.from_code(source_code)
+ if auth_token is None
+ else auth_token
+ )
+ self.skew_protection_token = SkewProtectionToken.from_code(
+ source_code
+ )
+
+ def validate_auth(self, token: AuthToken) -> bool:
+ """Validate an auth token."""
+ return token == self.auth_token
+
+ def validate_skew(self, token: SkewProtectionToken) -> bool:
+ """Validate a skew protection token."""
+ return token == self.skew_protection_token
diff --git a/marimo/_utils/file_watcher.py b/marimo/_utils/file_watcher.py
index dc9e96e369..4d8902f064 100644
--- a/marimo/_utils/file_watcher.py
+++ b/marimo/_utils/file_watcher.py
@@ -5,7 +5,7 @@ import asyncio
import os
from abc import ABC, abstractmethod
from collections import defaultdict
-from collections.abc import Coroutine
+from collections.abc import Awaitable, Coroutine
from pathlib import Path
from typing import Callable, Optional
@@ -155,6 +155,9 @@ def _create_watchdog(
return WatchdogFileWatcher(path, callback, loop)
+FileCallback = Callable[[Path], Awaitable[None]]
+
+
class FileWatcherManager:
"""Manages multiple file watchers, sharing watchers for the same file."""
@@ -162,9 +165,9 @@ class FileWatcherManager:
# Map of file paths to their watchers
self._watchers: dict[str, FileWatcher] = {}
# Map of file paths to their callbacks
- self._callbacks: dict[str, set[Callback]] = defaultdict(set)
+ self._callbacks: dict[str, set[FileCallback]] = defaultdict(set)
- def add_callback(self, path: Path, callback: Callback) -> None:
+ def add_callback(self, path: Path, callback: FileCallback) -> None:
"""Add a callback for a file path. Creates watcher if needed."""
path_str = str(path)
self._callbacks[path_str].add(callback)
@@ -181,7 +184,7 @@ class FileWatcherManager:
self._watchers[path_str] = watcher
LOGGER.debug(f"Created new watcher for {path_str}")
- def remove_callback(self, path: Path, callback: Callback) -> None:
+ def remove_callback(self, path: Path, callback: FileCallback) -> None:
"""Remove a callback for a file path. Removes watcher if no more callbacks."""
path_str = str(path)
if path_str not in self._callbacks:
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index 082873188e..017fa3cf04 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -32,7 +32,7 @@ from marimo._utils.toml import read_toml
from tests.utils import try_assert_n_times
if TYPE_CHECKING:
- from collections.abc import Generator, Iterator
+ from collections.abc import Iterator
HAS_UV = DependencyManager.which("uv")
@@ -157,23 +157,19 @@ def _read_toml(filepath: Path) -> Optional[dict[str, Any]]:
return read_toml(filepath)
-def _write_temp_notebook(notebook: str) -> Generator[str, None, None]:
- tmp_dir = tempfile.TemporaryDirectory()
- tmp_file = os.path.join(tmp_dir.name, "notebook.py")
+def _write_temp_notebook(notebook: str, tmp_dir: Path) -> Path:
+ tmp_file = tmp_dir / "notebook.py"
content = inspect.cleandoc(notebook)
-
- try:
- with open(tmp_file, "w") as f:
- f.write(content)
- yield tmp_file
- finally:
- tmp_dir.cleanup()
+ with open(tmp_file, "w") as f:
+ f.write(content)
+ return tmp_file
@pytest.fixture
-def temp_marimo_file_with_inline_metadata() -> Generator[str, None, None]:
- with _write_temp_notebook(
+def temp_marimo_file_with_inline_metadata(
+ tmp_path: Path,
+) -> str:
+ tmp_file = _write_temp_notebook(
"""
# /// script
# requires-python = ">=3.11"
@@ -195,14 +191,15 @@ def temp_marimo_file_with_inline_metadata() -> Generator[str, None, None]:
if __name__ == "__main__":
app.run()
- """
- ) as temp_file:
- yield temp_file
+ """,
+ tmp_path,
+ )
+ return str(tmp_file)
@pytest.fixture
-def temp_non_marimo_file() -> Generator[str, None, None]:
- with _write_temp_notebook(
+def temp_non_marimo_file(tmp_path: Path) -> str:
+ tmp_file = _write_temp_notebook(
"""
import numpy as np
@@ -210,33 +207,36 @@ def temp_non_marimo_file() -> Generator[str, None, None]:
if __name__ == "__main__":
print("This is a non-marimo file.")
- """
- ) as temp_file:
- yield temp_file
+ """,
+ tmp_path,
+ )
+ return str(tmp_file)
@pytest.fixture
-def temp_non_marimo_file_with_marimo() -> Generator[str, None, None]:
- with _write_temp_notebook(
+def temp_non_marimo_file_with_marimo(tmp_path: Path) -> str:
+ tmp_file = _write_temp_notebook(
"""
import numpy as np
import marimo as mo
if __name__ == "__main__":
print("This is a non-marimo file with a marimo import.")
- """
- ) as temp_file:
- yield temp_file
+ """,
+ tmp_path,
+ )
+ return str(tmp_file)
@pytest.fixture
-def temp_text_file() -> Generator[str, None, None]:
- with _write_temp_notebook(
+def temp_text_file(tmp_path: Path) -> str:
+ tmp_file = _write_temp_notebook(
"""
This is a syntax invalid file.
- """
- ) as temp_file:
- yield temp_file
+ """,
+ tmp_path,
+ )
+ return str(tmp_file)
@pytest.fixture(
@@ -361,35 +361,36 @@ def test_cli_edit_token_password_mutual_exclusivity() -> None:
)
-def test_cli_run_token_password_file_stdin() -> None:
+def test_cli_run_token_password_file_stdin(tmp_path: Path) -> None:
# Test stdin with run command using --token-password-file -
- with _write_temp_notebook(
+ tmp_file = _write_temp_notebook(
"""
import marimo
app = marimo.App()
- """
- ) as tmp_file:
- port = _get_port()
- p = subprocess.Popen(
- [
- "marimo",
- "run",
- tmp_file,
- "-p",
- str(port),
- "--headless",
- "--token",
- "--token-password-file",
- "-",
- ],
- stdin=subprocess.PIPE,
- )
- if p.stdin:
- p.stdin.write(b"run_secret")
- p.stdin.close()
+ """,
+ tmp_path,
+ )
+ port = _get_port()
+ p = subprocess.Popen(
+ [
+ "marimo",
+ "run",
+ tmp_file,
+ "-p",
+ str(port),
+ "--headless",
+ "--token",
+ "--token-password-file",
+ "-",
+ ],
+ stdin=subprocess.PIPE,
+ )
+ if p.stdin:
+ p.stdin.write(b"run_secret")
+ p.stdin.close()
- contents = _try_fetch(port, "localhost", "run_secret")
- _check_contents(p, b'"appConfig":', contents)
+ contents = _try_fetch(port, "localhost", "run_secret")
+ _check_contents(p, b'"appConfig":', contents)
def test_cli_edit_token_password_file_path() -> None:
diff --git a/tests/_server/api/endpoints/test_file_explorer.py b/tests/_server/api/endpoints/test_file_explorer.py
index c740724126..3b45201aa8 100644
--- a/tests/_server/api/endpoints/test_file_explorer.py
+++ b/tests/_server/api/endpoints/test_file_explorer.py
@@ -6,6 +6,8 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
+import pytest
+
from tests._server.mocks import get_session_manager, token_header
if TYPE_CHECKING:
@@ -91,27 +93,51 @@ def test_update_file(client: TestClient) -> None:
f.write(test_content)
def test_update_file_with_session(client: TestClient) -> None:
sm = get_session_manager(client)
+ # Enable watch mode to set up file watcher lifecycle
+ sm.watch = True
+ sm._setup_file_watching()
+
file_path = sm.file_router.get_unique_file_key()
assert file_path
file_path = Path(file_path)
assert file_path.exists()
- response = client.post(
- "/api/files/update",
- headers=HEADERS,
- json={
- "path": str(file_path),
- "contents": "@app.cell\ndef _(): x=10; x\n",
- },
- )
- assert response.status_code == 200, response.text
- assert response.headers["content-type"] == "application/json"
- assert response.json()["success"] is True
- file_contents = file_path.read_text()
- assert "@app.cell" in file_contents
- assert "x=10; x" in file_contents
- assert len(sm.file_change_handler._file_change_locks) == 1
+
+ # Create a session by connecting via websocket
+ with client.websocket_connect(
+ "/ws?session_id=test-session&access_token=fake-token"
+ ) as websocket:
+ # Receive kernel ready message
+ data = websocket.receive_json()
+ assert data["op"] == "kernel-ready"
+
+ # Verify file watcher was attached when session was created
+ assert len(sm._file_watcher_lifecycle._session_callbacks) == 1
+
+ # Update the file
+ response = client.post(
+ "/api/files/update",
+ headers=HEADERS,
+ json={
+ "path": str(file_path),
+ "contents": "@app.cell\ndef _(): x=10; x\n",
+ },
+ )
+ assert response.status_code == 200, response.text
+ assert response.headers["content-type"] == "application/json"
+ assert response.json()["success"] is True
+ file_contents = file_path.read_text()
+ assert "@app.cell" in file_contents
+ assert "x=10; x" in file_contents
+
+ # Shutdown
+ client.post("/api/kernel/shutdown", headers=HEADERS)
+
+ # Clean up
+ sm.watch = False
+ sm.watcher_manager.stop_all()
def test_move_file_or_directory(client: TestClient) -> None:
diff --git a/tests/_server/api/endpoints/test_login.py b/tests/_server/api/endpoints/test_login.py
index 8977ff56e4..1fe0ab1740 100644
--- a/tests/_server/api/endpoints/test_login.py
+++ b/tests/_server/api/endpoints/test_login.py
@@ -41,7 +41,7 @@ def create_app(base_url: str = "") -> Starlette:
],
)
app.state.session_manager = get_mock_session_manager()
- app.state.session_manager.auth_token = AUTH_TOKEN
+ app.state.session_manager._token_manager.auth_token = AUTH_TOKEN
app.state.base_url = base_url
return app
diff --git a/tests/_server/api/endpoints/test_resume_session.py b/tests/_server/api/endpoints/test_resume_session.py
index 32f4c1c73f..0005241435 100644
--- a/tests/_server/api/endpoints/test_resume_session.py
+++ b/tests/_server/api/endpoints/test_resume_session.py
@@ -303,6 +303,7 @@ 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()
@@ -372,6 +373,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()
client.post("/api/kernel/shutdown", headers=HEADERS)
diff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.py
index f5966ec28e..f3059f4b3a 100644
--- a/tests/_server/api/endpoints/test_ws.py
+++ b/tests/_server/api/endpoints/test_ws.py
@@ -211,11 +211,12 @@ def test_fails_on_multiple_connections_with_same_file(
@pytest.mark.flaky(reruns=3)
async def test_file_watcher_calls_reload(client: TestClient) -> None:
session_manager: SessionManager = get_session_manager(client)
+ session_manager.mode = SessionMode.RUN
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)
- session_manager.mode = SessionMode.RUN
filename = session_manager.file_router.get_unique_file_key()
assert filename
with open(filename, "a") as f: # noqa: ASYNC230
diff --git a/tests/_server/api/test_middleware.py b/tests/_server/api/test_middleware.py
index 0a78909209..6281aa9520 100644
--- a/tests/_server/api/test_middleware.py
+++ b/tests/_server/api/test_middleware.py
@@ -177,7 +177,8 @@ def no_auth_edit_app() -> Starlette:
app = create_starlette_app(base_url="", enable_auth=False)
app.state.session_manager = get_mock_session_manager()
app.state.session_manager.mode = SessionMode.EDIT
- app.state.session_manager.auth_token = AuthToken("") # no auth
+ # no auth
+ app.state.session_manager._token_manager.auth_token = AuthToken("")
app.state.config_manager = MarimoConfigManager(UserConfigManager())
# Mock out the server
uvicorn_server = uvicorn.Server(uvicorn.Config(app))
@@ -194,7 +195,8 @@ def no_auth_read_app() -> Starlette:
app = create_starlette_app(base_url="", enable_auth=False)
app.state.session_manager = get_mock_session_manager()
app.state.session_manager.mode = SessionMode.RUN
- app.state.session_manager.auth_token = AuthToken("") # no auth
+ # no auth
+ app.state.session_manager._token_manager.auth_token = AuthToken("")
app.state.config_manager = MarimoConfigManager(UserConfigManager())
# Mock out the server
uvicorn_server = uvicorn.Server(uvicorn.Config(app))
diff --git a/tests/_server/mocks.py b/tests/_server/mocks.py
index b883cf266a..f2e673902d 100644
--- a/tests/_server/mocks.py
+++ b/tests/_server/mocks.py
@@ -60,7 +60,7 @@ if __name__ == "__main__":
redirect_console_to_browser=False,
ttl_seconds=None,
)
- sm.skew_protection_token = SkewProtectionToken("skew-id-1")
+ sm._token_manager.skew_protection_token = SkewProtectionToken("skew-id-1")
return sm
diff --git a/tests/_server/sessions/__init__.py b/tests/_server/sessions/__init__.py
new file mode 100644
index 0000000000..be52407b50
--- /dev/null
+++ b/tests/_server/sessions/__init__.py
@@ -0,0 +1 @@
+# Copyright 2024 Marimo. All rights reserved.
diff --git a/tests/_server/sessions/test_file_change_handler.py b/tests/_server/sessions/test_file_change_handler.py
new file mode 100644
index 0000000000..7a6ad3c323
--- /dev/null
+++ b/tests/_server/sessions/test_file_change_handler.py
@@ -0,0 +1,405 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Tests for file_change_handler module."""
+
+from __future__ import annotations
+
+from textwrap import dedent
+from typing import TYPE_CHECKING
+from unittest.mock import MagicMock
+
+import pytest
+
+from marimo._ast.cell import CellConfig
+from marimo._config.manager import get_default_config_manager
+from marimo._messaging.ops import Reload, UpdateCellCodes, UpdateCellIdsRequest
+from marimo._runtime.requests import DeleteCellRequest, SyncGraphRequest
+from marimo._server.models.models import SaveNotebookRequest
+from marimo._server.notebook import AppFileManager
+from marimo._server.sessions.file_change_handler import (
+ EditModeReloadStrategy,
+ FileChangeCoordinator,
+ RunModeReloadStrategy,
+)
+from marimo._types.ids import CellId_t
+
+if TYPE_CHECKING:
+ from pathlib import Path
+
+
+def create_test_app_file_manager(
+ tmp_path: Path, content: str
+) -> AppFileManager:
+ """Create a test app file manager with the given content."""
+ test_file = tmp_path / "test.py"
+ test_file.write_text(content)
+ return AppFileManager(filename=str(test_file))
+
+
+def mock_session():
+ """Create a mock session for testing."""
+ session = MagicMock()
+ session.write_operation = MagicMock()
+ session.put_control_request = MagicMock()
+ return session
+
+
+def config_manager_lazy():
+ """Create a config manager with lazy watcher mode."""
+ return get_default_config_manager(current_path=None).with_overrides(
+ {"runtime": {"watcher_on_save": "lazy"}}
+ )
+
+
+def config_manager_autorun():
+ """Create a config manager with autorun watcher mode."""
+ return get_default_config_manager(current_path=None).with_overrides(
+ {"runtime": {"watcher_on_save": "autorun"}}
+ )
+
+
+def test_edit_mode_reload_strategy_lazy(
+ tmp_path: Path, mock_session: MagicMock, config_manager_lazy
+) -> None:
+ """Test edit mode reload strategy with lazy mode."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ app_file_manager = create_test_app_file_manager(tmp_path, content)
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = EditModeReloadStrategy(config_manager_lazy)
+ # Get actual cell IDs from the app
+ cell_ids = list(app_file_manager.app.cell_manager.cell_ids())
+ changed_cell_ids = set(cell_ids)
+
+ strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)
+
+ # Should send UpdateCellIdsRequest
+ assert mock_session.write_operation.call_count >= 1
+ update_ids_calls = [
+ call
+ for call in mock_session.write_operation.call_args_list
+ if isinstance(call[0][0], UpdateCellIdsRequest)
+ ]
+ assert len(update_ids_calls) == 1
+
+ # Should send UpdateCellCodes with code_is_stale=True
+ update_codes_calls = [
+ call
+ for call in mock_session.write_operation.call_args_list
+ if isinstance(call[0][0], UpdateCellCodes)
+ ]
+ assert len(update_codes_calls) == 1
+ assert update_codes_calls[0][0][0].code_is_stale is True
+
+ # Should not send execution requests
+ mock_session.put_control_request.assert_not_called()
+
+
+def test_edit_mode_reload_strategy_autorun(
+ tmp_path: Path, mock_session: MagicMock, config_manager_autorun
+) -> None:
+ """Test edit mode reload strategy with autorun mode."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ app_file_manager = create_test_app_file_manager(tmp_path, content)
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = EditModeReloadStrategy(config_manager_autorun)
+ changed_cell_ids = {CellId_t("cell1")}
+
+ strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)
+
+ # Should send UpdateCellIdsRequest
+ assert mock_session.write_operation.call_count >= 1
+ update_ids_calls = [
+ call
+ for call in mock_session.write_operation.call_args_list
+ if isinstance(call[0][0], UpdateCellIdsRequest)
+ ]
+ assert len(update_ids_calls) == 1
+
+ # Should send SyncGraphRequest for execution
+ assert mock_session.put_control_request.call_count >= 1
+ sync_calls = [
+ call
+ for call in mock_session.put_control_request.call_args_list
+ if isinstance(call[0][0], SyncGraphRequest)
+ ]
+ assert len(sync_calls) == 1
+
+
+def test_edit_mode_reload_strategy_with_deleted_cells(
+ tmp_path: Path, mock_session: MagicMock, config_manager_lazy
+) -> None:
+ """Test edit mode reload strategy with deleted cells."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ app_file_manager = create_test_app_file_manager(tmp_path, content)
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = EditModeReloadStrategy(config_manager_lazy)
+ # Get actual cell IDs from the app
+ cell_ids = list(app_file_manager.app.cell_manager.cell_ids())
+ # Indicate that cell2 was deleted (it's in changed but not in current cells)
+ changed_cell_ids = set(cell_ids) | {CellId_t("cell2")}
+
+ strategy.handle_reload(mock_session, changed_cell_ids=changed_cell_ids)
+
+ # Should send DeleteCellRequest for cell2
+ delete_calls = [
+ call
+ for call in mock_session.put_control_request.call_args_list
+ if isinstance(call[0][0], DeleteCellRequest)
+ ]
+ assert len(delete_calls) == 1
+ assert delete_calls[0][0][0].cell_id == CellId_t("cell2")
+
+
+def test_run_mode_reload_strategy(mock_session: MagicMock) -> None:
+ """Test run mode reload strategy sends Reload operation."""
+ strategy = RunModeReloadStrategy()
+ changed_cell_ids = {CellId_t("cell1")}
+
+ 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]
+ assert isinstance(operation, Reload)
+
+
+async def test_file_change_coordinator_handles_change(
+ tmp_path: Path, mock_session: MagicMock
+) -> None:
+ """Test file change coordinator handles file changes."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ test_file = tmp_path / "test.py"
+ test_file.write_text(content)
+
+ app_file_manager = AppFileManager(filename=str(test_file))
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = MagicMock()
+ coordinator = FileChangeCoordinator(strategy)
+
+ # Modify the file
+ test_file.write_text(
+ dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 2 # Changed
+ return x
+ """
+ )
+ )
+
+ result = await coordinator.handle_change(test_file, mock_session)
+
+ # Should handle the change
+ assert result.handled
+ assert result.error is None
+ strategy.handle_reload.assert_called_once()
+
+
+async def test_file_change_coordinator_skips_own_writes(
+ tmp_path: Path, mock_session: MagicMock
+) -> None:
+ """Test that file change coordinator skips reloading when it detects its own writes."""
+ # Create a temporary file with initial content
+ temp_file = tmp_path / "test_own_writes.py"
+ temp_file.write_text(
+ dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ )
+ )
+
+ # Set up app file manager
+ app_file_manager = AppFileManager(filename=str(temp_file))
+ mock_session.app_file_manager = app_file_manager
+
+ # Save the file to track the last saved content
+ cell_ids = list(app_file_manager.app.cell_manager.cell_ids())
+ codes = list(app_file_manager.app.cell_manager.codes())
+ app_file_manager.save(
+ SaveNotebookRequest(
+ cell_ids=cell_ids,
+ filename=str(temp_file),
+ codes=codes,
+ names=["cell1"],
+ configs=[CellConfig()],
+ persist=True,
+ )
+ )
+
+ # Create file change coordinator
+ strategy = MagicMock()
+ coordinator = FileChangeCoordinator(strategy)
+
+ # Call handle_change - should skip reload because content matches
+ result = await coordinator.handle_change(temp_file, mock_session)
+
+ # Verify reload was NOT called (early return triggered)
+ assert not result.handled
+ strategy.handle_reload.assert_not_called()
+
+ # Now externally modify the file
+ temp_file.write_text(
+ dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 2 # Changed by external editor
+ return x
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ )
+ )
+
+ # Call handle_change again - should now trigger reload
+ result = await coordinator.handle_change(temp_file, mock_session)
+
+ # Verify reload WAS called (content changed externally)
+ assert result.handled
+ strategy.handle_reload.assert_called_once()
+
+
+async def test_file_change_coordinator_handles_syntax_errors(
+ tmp_path: Path, mock_session: MagicMock
+) -> None:
+ """Test file change coordinator handles syntax errors gracefully."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ test_file = tmp_path / "test.py"
+ test_file.write_text(content)
+
+ app_file_manager = AppFileManager(filename=str(test_file))
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = MagicMock()
+ coordinator = FileChangeCoordinator(strategy)
+
+ # Write invalid syntax
+ test_file.write_text(
+ dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ # Invalid syntax below
+ def broken(
+ """
+ )
+ )
+
+ result = await coordinator.handle_change(test_file, session=mock_session)
+
+ # Should not handle due to error
+ assert not result.handled
+ assert result.error is not None
+
+
+async def test_file_change_coordinator_path_mismatch(
+ tmp_path: Path, mock_session: MagicMock
+) -> None:
+ """Test file change coordinator with path mismatch."""
+ content = dedent(
+ """\
+ import marimo
+ app = marimo.App()
+
+ @app.cell
+ def cell1():
+ x = 1
+ return x
+ """
+ )
+ test_file = tmp_path / "test.py"
+ test_file.write_text(content)
+
+ other_file = tmp_path / "other.py"
+ other_file.write_text(content)
+
+ app_file_manager = AppFileManager(filename=str(test_file))
+ mock_session.app_file_manager = app_file_manager
+
+ strategy = MagicMock()
+ coordinator = FileChangeCoordinator(strategy)
+
+ # Try to handle change for different file
+ result = await coordinator.handle_change(other_file, session=mock_session)
+
+ # Should not handle due to path mismatch
+ assert not result.handled
+ assert "mismatch" in result.error.lower()
diff --git a/tests/_server/sessions/test_file_watcher_integration.py b/tests/_server/sessions/test_file_watcher_integration.py
new file mode 100644
index 0000000000..7ca1370b9f
--- /dev/null
+++ b/tests/_server/sessions/test_file_watcher_integration.py
@@ -0,0 +1,323 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Tests for file_watcher_integration module."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from marimo._server.sessions.file_watcher_integration import (
+ FileWatcherAttachmentListener,
+ SessionFileWatcherLifecycle,
+)
+from marimo._server.sessions.session_repository import SessionRepository
+from marimo._types.ids import SessionId
+from marimo._utils.file_watcher import FileWatcherManager
+
+
+def create_mock_session(file_path: str | None):
+ """Create a mock session for testing."""
+ session = MagicMock()
+ session.app_file_manager = MagicMock()
+ session.app_file_manager.path = file_path
+ return session
+
+
+def watcher_manager():
+ """Create a mock file watcher manager."""
+ return MagicMock(spec=FileWatcherManager)
+
+
+def repository():
+ """Create a session repository."""
+ return SessionRepository()
+
+
+def file_change_callback():
+ """Create a mock file change callback."""
+ return AsyncMock()
+
+
+def lifecycle(watcher_manager, file_change_callback, repository):
+ """Create a SessionFileWatcherLifecycle instance."""
+ return SessionFileWatcherLifecycle(
+ watcher_manager, file_change_callback, repository
+ )
+
+
+def test_attach_session_with_path(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # Verify watcher was added
+ assert watcher_manager.add_callback.call_count == 1
+ call_args = watcher_manager.add_callback.call_args
+ assert call_args[0][0] == Path("/path/to/file.py")
+ # Second arg is the callback function
+ assert callable(call_args[0][1])
+
+
+def test_attach_session_without_path(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # Verify watcher was not added
+ watcher_manager.add_callback.assert_not_called()
+
+
+def test_detach_session_with_path(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # First attach
+ lifecycle.attach(session)
+ callback = watcher_manager.add_callback.call_args[0][1]
+
+ # Then detach
+ lifecycle.detach(session)
+
+ # Verify watcher was removed with the same callback
+ watcher_manager.remove_callback.assert_called_once_with(
+ Path("/path/to/file.py"), callback
+ )
+
+
+def test_detach_session_without_path(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ lifecycle.detach(session)
+
+ # Verify watcher was not removed
+ watcher_manager.remove_callback.assert_not_called()
+
+
+def test_detach_session_not_attached(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # Should not crash, just skip removal
+ watcher_manager.remove_callback.assert_not_called()
+
+
+def test_update_session_path(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # Attach to old path
+ lifecycle.attach(session)
+ old_callback = watcher_manager.add_callback.call_args[0][1]
+
+ # Update path
+ session.app_file_manager.path = "/path/to/new.py"
+ lifecycle.update(session, Path("/path/to/old.py"), Path("/path/to/new.py"))
+
+ # Verify old watcher was removed
+ watcher_manager.remove_callback.assert_called_once_with(
+ Path("/path/to/old.py"), old_callback
+ )
+
+ # Verify new watcher was added
+ assert watcher_manager.add_callback.call_count == 2
+ new_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()
+
+
+async def test_file_change_callback_invoked(
+ lifecycle: SessionFileWatcherLifecycle,
+ 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)
+
+ # 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 by calling the callback
+ await registered_callback(Path("/path/to/file.py"))
+
+ # Verify our file change callback was invoked
+ file_change_callback.assert_called_once_with(
+ Path("/path/to/file.py"), session
+ )
+
+
+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,
+ 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)
+
+ # Verify watcher was attached
+ watcher_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,
+ 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)
+
+ # Verify watcher was not detached
+ watcher_manager.remove_callback.assert_not_called()
diff --git a/tests/_server/sessions/test_resume_strategies.py b/tests/_server/sessions/test_resume_strategies.py
new file mode 100644
index 0000000000..dbf606085f
--- /dev/null
+++ b/tests/_server/sessions/test_resume_strategies.py
@@ -0,0 +1,182 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Tests for resume_strategies module."""
+
+from __future__ import annotations
+
+import os
+from unittest.mock import MagicMock
+
+import pytest
+
+from marimo._server.exceptions import InvalidSessionException
+from marimo._server.model import ConnectionState
+from marimo._server.sessions.resume_strategies import (
+ EditModeResumeStrategy,
+ RunModeResumeStrategy,
+)
+from marimo._server.sessions.session_repository import SessionRepository
+from marimo._types.ids import SessionId
+
+
+def create_mock_session(file_path: str, connection_state: ConnectionState):
+ """Create a mock session for testing."""
+ session = MagicMock()
+ session.app_file_manager = MagicMock()
+ session.app_file_manager.path = os.path.abspath(file_path)
+ session.connection_state.return_value = connection_state
+ return session
+
+
+def test_edit_mode_resume_no_existing_sessions() -> None:
+ """Test that edit mode returns None when no sessions exist."""
+ repository = SessionRepository()
+ strategy = EditModeResumeStrategy(repository)
+
+ result = strategy.try_resume(SessionId("new-session"), "test.py")
+ assert result is None
+
+
+def test_edit_mode_resume_orphaned_session() -> None:
+ """Test that edit mode resumes an orphaned session for the same file."""
+ repository = SessionRepository()
+ strategy = EditModeResumeStrategy(repository)
+
+ # Create an orphaned session
+ old_session_id = SessionId("old-session")
+ orphaned_session = create_mock_session("test.py", ConnectionState.ORPHANED)
+ repository.add_sync(old_session_id, orphaned_session)
+
+ # Try to resume
+ new_session_id = SessionId("new-session")
+ result = strategy.try_resume(new_session_id, "test.py")
+
+ # Should return the orphaned session
+ assert result is orphaned_session
+
+ # Session ID should be updated
+ assert repository.get_sync(new_session_id) is orphaned_session
+ assert repository.get_sync(old_session_id) is None
+
+
+def test_edit_mode_resume_open_session_not_resumed() -> None:
+ """Test that edit mode doesn't resume a non-orphaned session."""
+ repository = SessionRepository()
+ strategy = EditModeResumeStrategy(repository)
+
+ # Create an open session
+ old_session_id = SessionId("old-session")
+ open_session = create_mock_session("test.py", ConnectionState.OPEN)
+ repository.add_sync(old_session_id, open_session)
+
+ # Try to resume
+ new_session_id = SessionId("new-session")
+ result = strategy.try_resume(new_session_id, "test.py")
+
+ # Should return None because session is not orphaned
+ assert result is None
+
+ # Original session should remain unchanged
+ assert repository.get_sync(old_session_id) is open_session
+
+
+def test_edit_mode_resume_different_file() -> None:
+ """Test that edit mode doesn't resume session for different file."""
+ repository = SessionRepository()
+ strategy = EditModeResumeStrategy(repository)
+
+ # Create an orphaned session for a different file
+ old_session_id = SessionId("old-session")
+ orphaned_session = create_mock_session(
+ "other.py", ConnectionState.ORPHANED
+ )
+ repository.add_sync(old_session_id, orphaned_session)
+
+ # Try to resume for different file
+ new_session_id = SessionId("new-session")
+ result = strategy.try_resume(new_session_id, "test.py")
+
+ # Should return None because file doesn't match
+ assert result is None
+
+
+def test_edit_mode_resume_multiple_sessions_raises_error() -> None:
+ """Test that edit mode raises error if multiple sessions exist for same file."""
+ repository = SessionRepository()
+ strategy = EditModeResumeStrategy(repository)
+
+ # Create two sessions for the same file
+ session1 = create_mock_session("test.py", ConnectionState.ORPHANED)
+ session2 = create_mock_session("test.py", ConnectionState.ORPHANED)
+ repository.add_sync(SessionId("session1"), session1)
+ repository.add_sync(SessionId("session2"), session2)
+
+ # Should raise exception
+ with pytest.raises(
+ InvalidSessionException,
+ match="Only one session should exist while editing",
+ ):
+ strategy.try_resume(SessionId("new-session"), "test.py")
+
+
+def test_run_mode_resume_no_existing_session() -> None:
+ """Test that run mode returns None when session doesn't exist."""
+ repository = SessionRepository()
+ strategy = RunModeResumeStrategy(repository)
+
+ result = strategy.try_resume(SessionId("non-existent"), "test.py")
+ assert result is None
+
+
+def test_run_mode_resume_orphaned_session() -> None:
+ """Test that run mode resumes an orphaned session with matching ID."""
+ repository = SessionRepository()
+ strategy = RunModeResumeStrategy(repository)
+
+ # Create an orphaned session
+ session_id = SessionId("session-123")
+ orphaned_session = create_mock_session("test.py", ConnectionState.ORPHANED)
+ repository.add_sync(session_id, orphaned_session)
+
+ # Try to resume with same ID
+ result = strategy.try_resume(session_id, "test.py")
+
+ # Should return the orphaned session
+ assert result is orphaned_session
+
+
+def test_run_mode_resume_open_session_not_resumed() -> None:
+ """Test that run mode doesn't resume a non-orphaned session."""
+ repository = SessionRepository()
+ strategy = RunModeResumeStrategy(repository)
+
+ # Create an open session
+ session_id = SessionId("session-123")
+ open_session = create_mock_session("test.py", ConnectionState.OPEN)
+ repository.add_sync(session_id, open_session)
+
+ # Try to resume
+ result = strategy.try_resume(session_id, "test.py")
+
+ # Should return None because session is not orphaned
+ assert result is None
+
+
+def test_run_mode_allows_multiple_sessions_same_file() -> None:
+ """Test that run mode allows multiple sessions for the same file."""
+ repository = SessionRepository()
+ strategy = RunModeResumeStrategy(repository)
+
+ # Create multiple orphaned sessions for the same file
+ session1_id = SessionId("session-1")
+ session2_id = SessionId("session-2")
+ session1 = create_mock_session("test.py", ConnectionState.ORPHANED)
+ session2 = create_mock_session("test.py", ConnectionState.ORPHANED)
+ repository.add_sync(session1_id, session1)
+ repository.add_sync(session2_id, session2)
+
+ # Should be able to resume each by their ID
+ result1 = strategy.try_resume(session1_id, "test.py")
+ result2 = strategy.try_resume(session2_id, "test.py")
+
+ assert result1 is session1
+ assert result2 is session2
diff --git a/tests/_server/sessions/test_token_manager.py b/tests/_server/sessions/test_token_manager.py
new file mode 100644
index 0000000000..a5f5475a08
--- /dev/null
+++ b/tests/_server/sessions/test_token_manager.py
@@ -0,0 +1,97 @@
+# Copyright 2024 Marimo. All rights reserved.
+"""Tests for token_manager module."""
+
+from __future__ import annotations
+
+import pytest
+
+from marimo._server.model import SessionMode
+from marimo._server.sessions.token_manager import TokenManager
+from marimo._server.tokens import AuthToken, SkewProtectionToken
+
+
+def test_token_manager_edit_mode_random_tokens() -> None:
+ """Test that edit mode creates random tokens when no auth token provided."""
+ manager = TokenManager(mode=SessionMode.EDIT)
+
+ assert manager.mode == SessionMode.EDIT
+ assert manager.auth_token is not None
+ assert manager.skew_protection_token is not None
+
+ # Verify they are different from another instance
+ manager2 = TokenManager(mode=SessionMode.EDIT)
+ assert manager.auth_token != manager2.auth_token
+ assert manager.skew_protection_token != manager2.skew_protection_token
+
+
+def test_token_manager_edit_mode_with_provided_token() -> None:
+ """Test that edit mode uses provided auth token."""
+ provided_token = AuthToken.random()
+ manager = TokenManager(mode=SessionMode.EDIT, auth_token=provided_token)
+
+ assert manager.auth_token == provided_token
+ # Skew protection should still be random
+ assert manager.skew_protection_token is not None
+
+
+def test_token_manager_run_mode_code_based_tokens() -> None:
+ """Test that run mode creates code-based tokens."""
+ source_code = "print('hello world')"
+ manager = TokenManager(mode=SessionMode.RUN, source_code=source_code)
+
+ assert manager.mode == SessionMode.RUN
+ assert manager.auth_token is not None
+ assert manager.skew_protection_token is not None
+
+ # Verify they are consistent for the same code
+ manager2 = TokenManager(mode=SessionMode.RUN, source_code=source_code)
+ assert str(manager.auth_token) == str(manager2.auth_token)
+ assert str(manager.skew_protection_token) == str(
+ manager2.skew_protection_token
+ )
+
+
+def test_token_manager_run_mode_requires_source_code() -> None:
+ """Test that run mode raises error without source code."""
+ with pytest.raises(ValueError, match="source_code is required"):
+ TokenManager(mode=SessionMode.RUN)
+
+
+def test_token_manager_run_mode_with_provided_token() -> None:
+ """Test that run mode uses provided auth token."""
+ source_code = "print('test')"
+ provided_token = AuthToken.random()
+ manager = TokenManager(
+ mode=SessionMode.RUN,
+ auth_token=provided_token,
+ source_code=source_code,
+ )
+
+ assert str(manager.auth_token) == str(provided_token)
+ # Skew protection should still be code-based
+ expected_skew = SkewProtectionToken.from_code(source_code)
+ assert str(manager.skew_protection_token) == str(expected_skew)
+
+
+def test_token_manager_validate_auth() -> None:
+ """Test auth token validation."""
+ manager = TokenManager(mode=SessionMode.EDIT)
+
+ # Valid token should pass
+ assert manager.validate_auth(manager.auth_token)
+
+ # Invalid token should fail
+ invalid_token = AuthToken.random()
+ assert not manager.validate_auth(invalid_token)
+
+
+def test_token_manager_validate_skew() -> None:
+ """Test skew protection token validation."""
+ manager = TokenManager(mode=SessionMode.EDIT)
+
+ # Valid token should pass
+ assert manager.validate_skew(manager.skew_protection_token)
+
+ # Invalid token should fail
+ invalid_token = SkewProtectionToken.random()
+ assert not manager.validate_skew(invalid_token)
diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.py
index 49800365dd..cdeef70fc8 100644
--- a/tests/_server/test_session_manager.py
+++ b/tests/_server/test_session_manager.py
@@ -33,6 +33,8 @@ def mock_session_consumer():
@pytest.fixture
def mock_session():
session = Mock(spec=Session)
+ session.initialization_id = "test_init_id"
+ session.session_cache_manager = None
session.connection_state.return_value = ConnectionState.OPEN
session.kernel_manager = Mock(spec=KernelManager)
session.kernel_manager.kernel_task = None
@@ -56,6 +58,13 @@ def session_manager():
)
+def add_session(
+ manager: SessionManager, session_id: SessionId, session: Session
+) -> None:
+ """Add a session to the manager (for tests)."""
+ manager._repository.add_sync(session_id, session)
+
+
session_id = SessionId("test_session_id")
@@ -131,7 +140,7 @@ def test_maybe_resume_session_for_existing_file(
) -> None:
mock_session.connection_state.return_value = ConnectionState.ORPHANED
mock_session.app_file_manager = AppFileManager(filename=temp_marimo_file)
- session_manager.sessions[session_id] = mock_session
+ add_session(session_manager, session_id, mock_session)
# Resume the same session_id with the same file -> matches
resumed_session = session_manager.maybe_resume_session(
@@ -157,7 +166,7 @@ def test_close_session(
session_manager: SessionManager, mock_session: Session
) -> None:
mock_session.app_file_manager = AppFileManager(filename=None)
- session_manager.sessions[session_id] = mock_session
+ add_session(session_manager, session_id, mock_session)
assert session_manager.close_session(session_id)
assert session_id not in session_manager.sessions
mock_session.close.assert_called_once()
@@ -166,7 +175,7 @@ def test_close_session(
def test_any_clients_connected_new_file(
session_manager: SessionManager, mock_session: Session
) -> None:
- session_manager.sessions[session_id] = mock_session
+ add_session(session_manager, session_id, mock_session)
mock_session.app_file_manager = AppFileManager(filename=None)
assert (
session_manager.any_clients_connected(AppFileRouter.NEW_FILE) is False
@@ -179,7 +188,7 @@ def test_any_clients_connected_existing_file(
mock_session: Session,
temp_marimo_file: str,
) -> None:
- session_manager.sessions[session_id] = mock_session
+ add_session(session_manager, session_id, mock_session)
mock_session.app_file_manager = AppFileManager(filename=temp_marimo_file)
assert (
session_manager.any_clients_connected(AppFileRouter.NEW_FILE) is False
@@ -191,10 +200,8 @@ def test_any_clients_connected_existing_file(
def test_close_all_sessions(
session_manager: SessionManager, mock_session: Session
) -> None:
- session_manager.sessions = {
- "session1": mock_session,
- "session2": mock_session,
- }
+ add_session(session_manager, SessionId("session1"), mock_session)
+ add_session(session_manager, SessionId("session2"), mock_session)
session_manager.close_all_sessions()
assert len(session_manager.sessions) == 0
assert mock_session.close.call_count == 2
@@ -203,10 +210,8 @@ def test_close_all_sessions(
def test_shutdown(
session_manager: SessionManager, mock_session: Session
) -> None:
- session_manager.sessions = {
- "session1": mock_session,
- "session2": mock_session,
- }
+ add_session(session_manager, SessionId("session1"), mock_session)
+ add_session(session_manager, SessionId("session2"), mock_session)
session_manager.shutdown()
session_manager.lsp_server.stop.assert_called_once()
diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.py
index 100582ead7..4f569cdee0 100644
--- a/tests/_server/test_sessions.py
+++ b/tests/_server/test_sessions.py
@@ -499,6 +499,9 @@ def __():
file_key=file_key,
)
+ # Wait a loop to ensure the session is created
+ await asyncio.sleep(0.01)
+
tmp_file.write_text(
"""import marimo
app = marimo.App()
@@ -763,8 +766,9 @@ def __():
}
}
)
- session_manager._config_manager = config_reader_lazy
- session_manager.file_change_handler.config_manager = config_reader_lazy
+ session_manager._config_manager.get_config = (
+ lambda: config_reader_lazy.get_config()
+ )
# Reset the mock
session_consumer.put_control_request.reset_mock()
@@ -1078,130 +1082,3 @@ def test_session_with_script_config_overrides(
# Cleanup
session.close()
-
-
-@save_and_restore_main
-async def test_file_change_handler_skips_own_writes(tmp_path: Path) -> None:
- """Test that file change handler skips reloading when it detects its own writes."""
- from marimo._ast.cell import CellConfig
- from marimo._server.models.models import SaveNotebookRequest
- from marimo._types.ids import CellId_t
-
- # Create a temporary file with initial content
- temp_file = tmp_path / "test_own_writes.py"
- temp_file.write_text(
- dedent(
- """
- import marimo
- app = marimo.App()
-
- @app.cell
- def cell1():
- x = 1
- return x
-
- if __name__ == "__main__":
- app.run()
- """
- )
- )
-
- # Set up session with minimal dependencies
- session_consumer = MagicMock()
- session_consumer.connection_state.return_value = ConnectionState.OPEN
- app_file_manager = AppFileManager(filename=str(temp_file))
-
- session = SessionImpl.create(
- initialization_id="test_id",
- session_consumer=session_consumer,
- mode=SessionMode.EDIT,
- app_metadata=app_metadata,
- app_file_manager=app_file_manager,
- config_manager=get_default_config_manager(current_path=None),
- virtual_files_supported=True,
- redirect_console_to_browser=False,
- ttl_seconds=None,
- )
-
- # Save the file to track the last saved content
- session.app_file_manager.save(
- SaveNotebookRequest(
- cell_ids=[CellId_t("1")],
- filename=str(temp_file),
- codes=["x = 1"],
- names=["cell1"],
- configs=[CellConfig()],
- persist=True,
- )
- )
-
- # Create a minimal session manager for the file change handler
- file_router = AppFileRouter.from_filename(MarimoPath(str(temp_file)))
- session_manager = SessionManager(
- file_router=file_router,
- mode=SessionMode.EDIT,
- quiet=False,
- include_code=True,
- lsp_server=MagicMock(),
- config_manager=get_default_config_manager(current_path=None),
- watch=False,
- cli_args={},
- argv=None,
- auth_token=None,
- redirect_console_to_browser=False,
- ttl_seconds=None,
- )
-
- # Create file change handler
- file_change_handler = session_manager.file_change_handler
-
- # Mock the reload method to verify it's not called
- original_reload = session.app_file_manager.reload
- reload_called = False
-
- def mock_reload():
- nonlocal reload_called
- reload_called = True
- return original_reload()
-
- session.app_file_manager.reload = mock_reload
-
- # Call _handle_file_change - should skip reload because content matches
- file_change_handler._handle_file_change(str(temp_file), session)
-
- # Verify reload was NOT called (early return triggered)
- assert not reload_called, (
- "reload() should not have been called for own writes"
- )
-
- # Now externally modify the file
- temp_file.write_text(
- dedent(
- """
- import marimo
- app = marimo.App()
-
- @app.cell
- def cell1():
- x = 2 # Changed by external editor
- return x
-
- if __name__ == "__main__":
- app.run()
- """
- )
- )
-
- # Reset the flag
- reload_called = False
-
- # Call _handle_file_change again - should now trigger reload
- file_change_handler._handle_file_change(str(temp_file), session)
-
- # Verify reload WAS called (content changed externally)
- assert reload_called, (
- "reload() should have been called for external changes"
- )
-
- # Cleanup
- session.close()
diff --git a/tests/conftest.py b/tests/conftest.py
index 99b4eade3a..d4b6c34e98 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -489,7 +489,7 @@ def temp_md_marimo_file() -> Generator[str, None, None]:
f.write(content)
yield tmp_file
finally:
- tmp_dir.cleanup()
+ _cleanup_tmp_dir(tmp_dir)
@pytest.fixture