From e84bfba74b2c116fd4a8da8860de6139b3f21d5e Mon Sep 17 00:00:00 2001 From: Jordan Boxer Date: Wed, 25 Mar 2026 08:18:37 -0400 Subject: [PATCH] Remove duplicate cells on document change via jupyter-ydoc public API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Call `remove_duplicate_cells()` (added in jupyter-ydoc) from `_on_document_change` when cells change, preventing duplicate cells from accumulating when multiple clients sync notebook content concurrently. Uses `getattr` to duck-type the check, so `rooms.py` remains unaware of notebook-specific document structure — all cell-level knowledge stays encapsulated in `jupyter_ydoc.YNotebook`. Requires jupyter-ydoc with `YNotebook.remove_duplicate_cells()`. PY-1190 Co-Authored-By: Claude Opus 4.6 (1M context) --- projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py b/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py index 9cc45435..82f1fc73 100644 --- a/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py +++ b/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py @@ -247,6 +247,10 @@ def _on_document_change(self, target: str, event: Any) -> None: document. This tasks are debounced (60 seconds by default) so we need to cancel previous tasks before creating a new one. """ + remove_duplicate_cells = getattr(self._document, "remove_duplicate_cells", None) + if target == "cells" and remove_duplicate_cells is not None: + asyncio.get_running_loop().call_soon(remove_duplicate_cells) + # Collect autosave values from all clients autosave_states = [ state.get("autosave", True)