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.

fix: RTC duplication bug (#4112)

Myles Scolnickbaebe58d7682
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index e244800047..ec61d5cba6 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -190,6 +190,7 @@ const CellEditorInternal = ({
cellId,
userConfig.keymap,
userConfig.completion,
+ userConfig.language_servers,
aiEnabled,
theme,
showPlaceholder,
@@ -206,11 +207,15 @@ const CellEditorInternal = ({
const handleInitializeEditor = useEvent(() => {
// If rtc is enabled, use collaborative editing
if (getFeatureFlag("rtc_v2")) {
- const rtc = realTimeCollaboration(cellId, (code) => {
- // It's not really a formatting change,
- // but this means it won't be marked as stale
- cellActions.updateCellCode({ cellId, code, formattingChange: true });
- });
+ const rtc = realTimeCollaboration(
+ cellId,
+ (code) => {
+ // It's not really a formatting change,
+ // but this means it won't be marked as stale
+ cellActions.updateCellCode({ cellId, code, formattingChange: true });
+ },
+ code,
+ );
extensions.push(rtc.extension);
code = rtc.code;
}
diff --git a/frontend/src/core/codemirror/rtc/__tests__/cell-manager.test.ts b/frontend/src/core/codemirror/rtc/__tests__/cell-manager.test.ts
index 4ec3165fc3..fdcf745264 100644
--- a/frontend/src/core/codemirror/rtc/__tests__/cell-manager.test.ts
+++ b/frontend/src/core/codemirror/rtc/__tests__/cell-manager.test.ts
@@ -80,7 +80,7 @@ describe("CellProviderManager", () => {
},
);
expect(provider).toBe(mockProvider);
- expect(ytext.toJSON()).toBe("initial code");
+ expect(ytext.toJSON()).toBe("");
});
it("should return existing provider if one exists", async () => {
diff --git a/frontend/src/core/codemirror/rtc/cell-manager.ts b/frontend/src/core/codemirror/rtc/cell-manager.ts
index cb5b76b689..79fcc6ed45 100644
--- a/frontend/src/core/codemirror/rtc/cell-manager.ts
+++ b/frontend/src/core/codemirror/rtc/cell-manager.ts
@@ -43,14 +43,6 @@ export class CellProviderManager {
const ydoc = new Y.Doc();
const ytext = ydoc.getText(DOC_KEY);
const ylanguage = ydoc.getText(LANGUAGE_KEY);
- // Handle initial code. This is only used if a cell is brought back
- // from being deleted.
- if (initialCode) {
- ytext.doc?.transact(() => {
- ytext.delete(0, ytext.length);
- ytext.insert(0, initialCode);
- });
- }
const params: Record<string, string> = {
session_id: getSessionId(),
diff --git a/frontend/src/core/codemirror/rtc/extension.ts b/frontend/src/core/codemirror/rtc/extension.ts
index fefef43a1b..7048ff0e4c 100644
--- a/frontend/src/core/codemirror/rtc/extension.ts
+++ b/frontend/src/core/codemirror/rtc/extension.ts
@@ -35,7 +35,7 @@ export function realTimeCollaboration(
// Code sync plugin
const codeSync = ViewPlugin.define((view) => {
const handleSync = (isSynced: boolean) => {
- Logger.debug(`RTC: sync=${isSynced}, ytext.length=${ytext.toJSON()}`);
+ Logger.debug(`RTC sync=${isSynced}, ytext.length=${ytext.toJSON()}`);
// If it's not synced, update the editor code
if (!isSynced) {
@@ -52,6 +52,23 @@ export function realTimeCollaboration(
// If it's synced, update the cell code
if (isSynced) {
updateCellCode(ytext.toJSON());
+
+ // If the cell is new, insert the initial code
+ // Only do this once when we first connect and the document is empty
+ if (initialCode && ytext.length === 0) {
+ // Set a flag to prevent multiple insertions
+ const hasInserted = ytext.doc
+ ?.getMap("_metadata")
+ .get("initialCodeInserted");
+ if (!hasInserted) {
+ ytext.doc?.transact(() => {
+ ytext.delete(0, ytext.length);
+ ytext.insert(0, initialCode);
+ // Mark that we've inserted the initial code
+ ytext.doc?.getMap("_metadata").set("initialCodeInserted", true);
+ });
+ }
+ }
}
};
diff --git a/marimo/_server/api/endpoints/ws.py b/marimo/_server/api/endpoints/ws.py
index 92fb2968e5..8071881bb5 100644
--- a/marimo/_server/api/endpoints/ws.py
+++ b/marimo/_server/api/endpoints/ws.py
@@ -232,18 +232,10 @@ async def ycell_provider(
ycell.cleaner.cancel()
ycell.cleaner = None
else:
- file_manager = session.app_file_manager
- mgr = file_manager.app.cell_manager
- if mgr.has_cell(cell_id):
- code = mgr.cell_data_at(cell_id).code
- LOGGER.debug(
- f"RTC: Creating new ydoc for existing cell {cell_id} with code length {len(code)}"
- )
- else:
- code = ""
- LOGGER.debug(
- f"RTC: Creating new ydoc for new cell {cell_id} with empty code"
- )
+ code = ""
+ LOGGER.debug(
+ f"RTC: Creating new ydoc for new cell {cell_id} with empty code"
+ )
ydoc = Doc[Text]()
ytext = ydoc.get("code", type=Text)
ylang = ydoc.get("language", type=Text)