Skip to content

Remove duplicate cells on document change#555

Closed
jordanhboxer wants to merge 1 commit into
jupyterlab:mainfrom
jordanhboxer:jupyter-server-ydoc-2.2.1+hrt1
Closed

Remove duplicate cells on document change#555
jordanhboxer wants to merge 1 commit into
jupyterlab:mainfrom
jordanhboxer:jupyter-server-ydoc-2.2.1+hrt1

Conversation

@jordanhboxer
Copy link
Copy Markdown
Contributor

@jordanhboxer jordanhboxer commented Mar 24, 2026

Summary

  • Calls remove_duplicate_cells() (added in jupyter-ydoc#397) from _on_document_change when cells change, preventing duplicate cells from accumulating when multiple clients sync notebook content concurrently
  • Uses getattr so rooms.py remains unaware of notebook-specific document structure — all cell-level knowledge stays encapsulated in jupyter_ydoc.YNotebook

Dependencies

Test plan

  • Existing tests pass
  • Manual verification with concurrent notebook editing

Related

@github-actions
Copy link
Copy Markdown
Contributor

Binder 👈 Launch a Binder on branch jordanhboxer/jupyter-collaboration/jupyter-server-ydoc-2.2.1%2Bhrt1

@jordanhboxer
Copy link
Copy Markdown
Contributor Author

@davidbrochart @krassowski another couple of changes related to cell duplication (also #556). unfortunately this has been a bit of a thorn in our sides but with the amazing help of @pablogsal we've been able to reproduce a situation where concurrent out-of-band updates cause things to blow up so these two commit will guard against that

@krassowski krassowski added the bug Something isn't working label Mar 25, 2026
Copy link
Copy Markdown
Collaborator

@davidbrochart davidbrochart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this change be in jupyter_ydoc? I don't think jupyter-server-ydoc should be aware of the types of shared documents.

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) <noreply@anthropic.com>
@jordanhboxer jordanhboxer force-pushed the jupyter-server-ydoc-2.2.1+hrt1 branch from 194810b to e84bfba Compare March 25, 2026 12:24
@jordanhboxer
Copy link
Copy Markdown
Contributor Author

good call! i created a public method on YNotebook which handles the logic and now all we need to do here is call it if its exists. let me know if there's a better approach you have in mind. I tried going down the path of adding an observer to YNotebook but that cause significant performance degradation so this seems like a better approach. jupyter-server/jupyter_ydoc#397

Comment on lines +250 to +253
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I still see that as an abstraction leak.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair point. adding an observe cells callback to handle in jupyter-server/jupyter_ydoc#397

@jordanhboxer
Copy link
Copy Markdown
Contributor Author

abandoning in favor of jupyter-server/jupyter_ydoc#397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants