1818
1919from marimo import _loggers
2020from marimo ._cli .print import red
21+ from marimo ._messaging .notebook .changes import TransactionSource
2122from marimo ._messaging .notebook .document import NotebookCell
2223from marimo ._messaging .notification import (
2324 AlertNotification ,
@@ -260,7 +261,7 @@ def _on_kernel_message(self, session: Session, msg: KernelMessage) -> None:
260261 consider a middleware chain instead of inline dispatch.
261262 """
262263 notif : KernelMessage | NotificationMessage = msg
263- kernel_transaction_applied = False
264+ applied_source : TransactionSource | None = None
264265
265266 name = try_deserialize_kernel_notification_name (msg )
266267 if name == NotebookDocumentTransactionNotification .name :
@@ -273,24 +274,31 @@ def _on_kernel_message(self, session: Session, msg: KernelMessage) -> None:
273274 notif = NotebookDocumentTransactionNotification (
274275 transaction = applied
275276 )
276- kernel_transaction_applied = applied .source == "kernel"
277+ applied_source = applied .source
277278 except Exception :
278279 LOGGER .warning (
279280 "Failed to decode/apply kernel document transaction"
280281 )
281282
282283 session .notify (notif , from_consumer_id = None )
283284
284- if kernel_transaction_applied :
285- self ._maybe_autosave (session )
285+ if applied_source is not None :
286+ self ._maybe_autosave (session , applied_source )
286287
287- def _maybe_autosave (self , session : Session ) -> None :
288- """Best-effort persistence of kernel-driven mutations to disk.
288+ def _maybe_autosave (
289+ self , session : Session , source : TransactionSource
290+ ) -> None :
291+ """Best-effort persistence of code-mode mutations to disk.
289292
293+ Only ``source="code-mode"`` transactions persist; ``"kernel"``
294+ bookkeeping (e.g. instantiation cell-order broadcasts) is skipped
295+ so opening or running a notebook never rewrites it on disk.
290296 Skipped in run mode and for unnamed notebooks. Failures surface as
291297 an ``AlertNotification`` toast; they never raise out of the
292298 interceptor.
293299 """
300+ if source != "code-mode" :
301+ return
294302 if self .kernel_manager .mode != SessionMode .EDIT :
295303 return
296304
0 commit comments