Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(md): make undo fast in large collaborative documents (#9013)
Wolf Mermelstein40442b35c64a
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.mdindex ba53ac0411..119f669f77 100644--- a/apps/web/AGENTS.md+++ b/apps/web/AGENTS.md@@ -41,6 +41,12 @@ beside a scrolling editor whose selection runs past its visible bottom(`clipToVisible`), have a backend-free browser regression:`bunx playwright test --config src/lib/core/component/LexicalMarkdown/directive/browser-test/playwright.config.ts`.+Undo in a large collaborative Markdown document (Loro undo, mirror, reconcile,+caret mapping) has a backend-free timing regression:+`bunx playwright test --config src/lib/core/component/LexicalMarkdown/collaboration/browser-test/playwright.config.ts`.+Keep reconcile and caret lookups linear in the document: no `getChildren()` or+container scan per node.+Event details placement has a backend-free browser regression: the desktoppopover stays beside its event when join requests arrive after it opens, andslides into a short window; the phone drawer shows late requests. The samediff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.browser.e2e.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.browser.e2e.tsnew file mode 100644index 0000000000..c209920f70--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.browser.e2e.ts@@ -0,0 +1,65 @@+import { expect, type Page, test } from '@playwright/test';++const fixture =+ '/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.html';++// About the length of Moby-Dick (1.2 MB of text).+const PARAGRAPHS = 4000;+// Undo used to reconcile the editor and look up the caret's Loro container in+// time quadratic in the document; at this size the document did not finish+// opening in two minutes. Now undoing typing takes about 300 ms, and restoring+// a thousand paragraphs about 900 ms, 400 ms of it in Loro's own undo.+const UNDO_TYPING_BUDGET_MS = 600;+const UNDO_DELETION_BUDGET_MS = 1500;++async function openLargeDocument(page: Page) {+ await page.goto(`${fixture}?paragraphs=${PARAGRAPHS}`);+ await page.waitForFunction(() => window.largeUndo?.ready(), undefined, {+ timeout: 120_000,+ });+ expect(await page.evaluate(() => window.largeUndo!.paragraphs())).toBe(+ PARAGRAPHS+ );+ return page.evaluate(() => window.largeUndo!.text());+}++async function timeUndo(page: Page, expected: string) {+ await page.evaluate((text) => window.largeUndo!.armUndoTimer(text), expected);+ await page.keyboard.press('ControlOrMeta+z');+ return page.evaluate(() => window.largeUndo!.undoTime());+}++test('undoing typing in a large document stays within the budget', async ({+ page,+}) => {+ const original = await openLargeDocument(page);++ await page.evaluate(() => window.largeUndo!.placeAtEnd(2000));+ await page.keyboard.type(' undo me');+ await expect+ .poll(() => page.evaluate(() => window.largeUndo!.text()))+ .toContain(' undo me');++ const ms = await timeUndo(page, original);+ console.log(`undo of typing took ${ms.toFixed(0)} ms`);+ expect(ms).toBeLessThan(UNDO_TYPING_BUDGET_MS);+});++test('undoing the deletion of a thousand paragraphs stays within the budget', async ({+ page,+}) => {+ const original = await openLargeDocument(page);++ await page.evaluate(() => window.largeUndo!.selectBlocks(1000, 1999));+ await page.keyboard.press('Backspace');+ await expect+ .poll(() => page.evaluate(() => window.largeUndo!.paragraphs()))+ .toBe(PARAGRAPHS - 999);++ const ms = await timeUndo(page, original);+ console.log(`undo of deletion took ${ms.toFixed(0)} ms`);+ expect(await page.evaluate(() => window.largeUndo!.paragraphs())).toBe(+ PARAGRAPHS+ );+ expect(ms).toBeLessThan(UNDO_DELETION_BUDGET_MS);+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.html b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.htmlnew file mode 100644index 0000000000..86b9d624a2--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.html@@ -0,0 +1,5 @@+<!doctype html>+<html lang="en">+ <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Large document undo fixture</title></head>+ <body><div id="root"></div><script type="module" src="./large-undo.tsx"></script></body>+</html>diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsxnew file mode 100644index 0000000000..fa82d2b77b--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx@@ -0,0 +1,268 @@+/**+ * The collaborative markdown editor (`CollabProvider`, sync engine, Loro undo+ * manager) over a large document and a sync source that accepts every update+ * locally, so undo can be timed without a backend.+ */++import '@app/index.css';+import { AnalyticsContextProvider } from '@app/lib/analytics/analytics-context';+import { PosthogProvider } from '@app/lib/analytics/posthog';+import { UserContextProvider } from '@core/context/user';+import { fireAndForget } from '@core/util/fireAndForget';+import { onElementConnect } from '@core/util/onElementConnect';+import { createLoroManager } from '@macro-inc/collaboration/collab/manager';+import {+ type LiveSyncSource,+ type SyncSourceEvent,+ SyncSourceStatus,+} from '@macro-inc/collaboration/collab/source';+import { createPeerIdValidator } from '@macro-inc/lexical-core';+import { MARKDOWN_LORO_SCHEMA } from '@macro-inc/lexical-core/markdown-loro-schema';+import { markdownToLoroSnapshot } from '@macro-inc/lexical-core/markdown-loro-snapshot';+import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';+import { DragDropProvider } from '@thisbeyond/solid-dnd';+import {+ $createRangeSelection,+ $getRoot,+ $isElementNode,+ $setSelection,+ type LexicalEditor,+} from 'lexical';+import { okAsync } from 'neverthrow';+import { createEffect, createSignal, onCleanup, Show } from 'solid-js';+import { render } from 'solid-js/web';+import type { MarkdownEditorErrors } from '../../constants';+import {+ createLexicalWrapper,+ LexicalWrapperContext,+} from '../../context/LexicalWrapperContext';+import { registerMarkdownEditing } from '../../editing/registerMarkdownEditing';+import { editorFocusSignal } from '../../utils';+import { CollabProvider } from '../CollabProvider';++declare global {+ interface Window {+ largeUndo?: {+ ready: () => boolean;+ paragraphs: () => number;+ text: () => string;+ /** Puts the caret at the end of the top-level block at `index`. */+ placeAtEnd: (index: number) => void;+ /** Selects the top-level blocks from `first` through `last`. */+ selectBlocks: (first: number, last: number) => void;+ /** Times the next keydown until the editor's text equals `expected`. */+ armUndoTimer: (expected: string) => void;+ /** The ms the armed timer measured. */+ undoTime: () => Promise<number>;+ };+ }+}++const PARAGRAPH =+ 'Call me Ishmael. Some years ago, never mind how long precisely, having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.';++function largeMarkdown(paragraphs: number): string {+ return Array.from(+ { length: paragraphs },+ (_, i) => `${i % 50 === 0 ? '## Chapter ' : ''}${i} ${PARAGRAPH}`+ ).join('\n\n');+}++function Fixture() {+ const paragraphs = Number(+ new URLSearchParams(location.search).get('paragraphs') ?? '2000'+ );+ const documentId = 'large-undo';+ const loroManager = createLoroManager(MARKDOWN_LORO_SCHEMA, { documentId });+ const source: LiveSyncSource = {+ documentId,+ status: () => SyncSourceStatus.Connected,+ listen: (_listener: (event: SyncSourceEvent) => void) => () => {},+ pushUpdate: async () => true,+ pushAwareness: () => {},+ registerPeerId: () => {},+ requestUpdatesSince: () => okAsync(new Uint8Array()),+ requestSnapshot: () => okAsync(new Uint8Array()),+ reconnect: () => {},+ cleanup: () => {},+ };+ const [syncSource, setSyncSource] = createSignal<LiveSyncSource>();+ const [ready, setReady] = createSignal(false);+ let editor: LexicalEditor | undefined;+ let undoTime: Promise<number> | undefined;++ const connect = async () => {+ const snapshot = await markdownToLoroSnapshot(largeMarkdown(paragraphs));+ if (!snapshot) throw new Error('no snapshot');+ await loroManager.ingest({ kind: 'dss', snapshot });+ setSyncSource(source);+ };+ fireAndForget(connect(), 'large-undo-fixture');++ window.largeUndo = {+ ready,+ paragraphs: () =>+ editor?.getEditorState().read(() => $getRoot().getChildrenSize()) ?? 0,+ text: () =>+ editor?.getEditorState().read(() => $getRoot().getTextContent()) ?? '',+ placeAtEnd: (index) => {+ editor?.update(+ () => {+ const block = $getRoot().getChildAtIndex(index);+ if ($isElementNode(block)) block.selectEnd();+ },+ { discrete: true }+ );+ editor?.focus();+ },+ selectBlocks: (first, last) => {+ editor?.update(+ () => {+ const root = $getRoot();+ const start = root.getChildAtIndex(first);+ const end = root.getChildAtIndex(last);+ if (!$isElementNode(start) || !$isElementNode(end)) return;+ const selection = $createRangeSelection();+ selection.anchor.set(start.getKey(), 0, 'element');+ selection.focus.set(end.getKey(), end.getChildrenSize(), 'element');+ $setSelection(selection);+ },+ { discrete: true }+ );+ editor?.focus();+ },+ armUndoTimer: (expected) => {+ undoTime = new Promise((resolve) => {+ const target = editor;+ if (!target) throw new Error('no editor');+ let start = 0;+ window.addEventListener(+ 'keydown',+ () => {+ start = performance.now();+ },+ { capture: true, once: true }+ );+ const unregister = target.registerUpdateListener(({ editorState }) => {+ if (start === 0) return;+ const text = editorState.read(() => $getRoot().getTextContent());+ if (text !== expected) return;+ unregister();+ // Let the follow-up updates queued by the undo run before stopping.+ setTimeout(() => resolve(performance.now() - start));+ });+ });+ },+ undoTime: () => undoTime ?? Promise.reject(new Error('timer not armed')),+ };++ return (+ <main class="p-4 text-ink bg-page">+ <Show when={syncSource()}>+ <Editor+ documentId={documentId}+ loroManager={loroManager}+ syncSource={syncSource}+ onEditor={(e) => {+ editor = e;+ }}+ onReady={() => setReady(true)}+ />+ </Show>+ </main>+ );+}++/** The editor `CollabMarkdownEditor` builds, minus menus and overlays. */+function Editor(props: {+ documentId: string;+ loroManager: ReturnType<+ typeof createLoroManager<typeof MARKDOWN_LORO_SCHEMA>+ >;+ syncSource: () => LiveSyncSource | undefined;+ onEditor: (editor: LexicalEditor) => void;+ onReady: () => void;+}) {+ const [editorReady, setEditorReady] = createSignal(false);+ const [editorError, setEditorError] =+ createSignal<MarkdownEditorErrors | null>(null);+ const isContentEditable = () => editorReady() && !editorError();+ const lexicalWrapper = createLexicalWrapper({+ type: 'markdown-sync',+ namespace: 'large-undo',+ isInteractable: isContentEditable,+ withIds: true,+ });+ const { editor, plugins, cleanup } = lexicalWrapper;+ onCleanup(cleanup);+ props.onEditor(editor);+ const [editorFocus, setEditorFocus] = createSignal(false);+ editorFocusSignal(editor, setEditorFocus);+ const peerId = () => props.loroManager.peerIdStr;+ plugins+ .richText()+ .list()+ .markdownShortcuts()+ .delete()+ .history(400, props.loroManager);+ registerMarkdownEditing({+ lexicalWrapper,+ isContentEditable,+ peerIdValidator: createPeerIdValidator(peerId, true),+ source: { id: props.documentId, trackMentions: false },+ onVersionError: setEditorError,+ peerId,+ });+ createEffect(() => editor.setEditable(isContentEditable()));+ createEffect(() => {+ if (editorReady()) props.onReady();+ });+ let container!: HTMLDivElement;+ return (+ <LexicalWrapperContext.Provider value={lexicalWrapper}>+ <div class="relative border border-edge-muted p-2" ref={container}>+ <div+ ref={(el) => onElementConnect(el, () => editor.setRootElement(el))}+ contentEditable={isContentEditable()}+ role="textbox"+ aria-multiline="true"+ class="w-full outline-none"+ />+ <CollabProvider+ editor={editor}+ pluginManager={plugins}+ editorContainerRef={container}+ highlightLayerRef={container}+ mappings={lexicalWrapper.mapping!}+ editorFocus={editorFocus}+ setEditorReady={setEditorReady}+ setEditorError={setEditorError}+ loroManager={props.loroManager}+ syncSource={props.syncSource}+ sourceReady={() => true}+ canEdit={() => true}+ canComment={() => false}+ editorError={editorError}+ />+ </div>+ </LexicalWrapperContext.Provider>+ );+}++const client = new QueryClient();+render(+ () => (+ <AnalyticsContextProvider>+ <PosthogProvider>+ <QueryClientProvider client={client}>+ <UserContextProvider>+ <DragDropProvider>+ <Fixture />+ </DragDropProvider>+ </UserContextProvider>+ </QueryClientProvider>+ </PosthogProvider>+ </AnalyticsContextProvider>+ ),+ document.getElementById('root')!+);diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/playwright.config.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/playwright.config.tsnew file mode 100644index 0000000000..6a005e3172--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/playwright.config.ts@@ -0,0 +1,29 @@+import { fileURLToPath } from 'node:url';+import { defineConfig } from '@playwright/test';++const webDirectory = fileURLToPath(+ new URL('../../../../../../../', import.meta.url)+);+const port = process.env.LARGE_UNDO_TEST_PORT ?? '3052';++export default defineConfig({+ testDir: fileURLToPath(new URL('.', import.meta.url)),+ testMatch: '*.browser.e2e.ts',+ workers: 1,+ timeout: 180_000,+ use: {+ baseURL: `http://localhost:${port}`,+ headless: true,+ viewport: { width: 1280, height: 900 },+ launchOptions: {+ executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,+ },+ },+ webServer: {+ command: `MACRO_DEV_HTTPS=false bunx vite --port ${port} --strictPort`,+ cwd: webDirectory,+ url: `http://localhost:${port}/app/`,+ reuseExistingServer: false,+ timeout: 120_000,+ },+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/mapping.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/mapping.tsindex 5c4655ec67..400617c04e 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/mapping.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/mapping.ts@@ -2,8 +2,8 @@ import { DEV_MODE_ENV } from '@core/constant/featureFlags';import type { LoroManager } from '@macro-inc/collaboration/collab/manager';import type { NodeIdMappings } from '@macro-inc/lexical-core';-import { $getNodeByKey, type LexicalNode } from 'lexical';-import { type ContainerID, LoroMap } from 'loro-crdt';+import { $getNodeByKey, $isRootNode, type LexicalNode } from 'lexical';+import { type ContainerID, LoroMap, LoroMovableList } from 'loro-crdt';const warn = (...args: any[]) => {if (DEV_MODE_ENV) console.warn('LoroNodeMappings:', ...args);@@ -78,7 +78,7 @@ export function $findLoroContainerForLexicalNode(return null;}- const containerId = smartSearchContainersForNode(loroManager, nodeId);+ const containerId = $searchContainersForNode(loroManager, node, nodeId);if (!containerId) {// %BOOKMARK - no container@@ -102,10 +102,51 @@ function getMapValueOrContainer(return {};}-function smartSearchContainersForNode(+function nodeIdOfMap(map: LoroMap): string | undefined {+ if (map.isDeleted()) return undefined;+ const innerValue = getMapValueOrContainer(map, '$');+ return 'id' in innerValue ? (innerValue.id as string) : undefined;+}++/** The container at the node's position in the Loro tree, if it holds the node. */+function $containerAtNodePath(+ loroManager: LoroManager,+ node: LexicalNode,+ nodeId: string+): ContainerID | undefined {+ const path: number[] = [];+ let current = node;+ while (!$isRootNode(current)) {+ const parent = current.getParent();+ if (!parent) return undefined;+ path.push(current.getIndexWithinParent());+ current = parent;+ }++ let map = loroManager.doc.getMap('root');+ for (const index of path.reverse()) {+ const children = map.get('children');+ if (!(children instanceof LoroMovableList)) return undefined;+ const child = children.get(index);+ if (!(child instanceof LoroMap)) return undefined;+ map = child;+ }++ return nodeIdOfMap(map) === nodeId ? map.id : undefined;+}++/**+ * Lexical and Loro trees line up except where local-only nodes (draft comment+ * marks) shift positions; only those lookups scan every container.+ */+function $searchContainersForNode(loroManager: LoroManager,+ node: LexicalNode,nodeId: string): ContainerID | undefined {+ const atPath = $containerAtNodePath(loroManager, node, nodeId);+ if (atPath) return atPath;+const res = loroManager.getAllContainerIds();if (res.isErr()) {@@ -113,9 +154,9 @@ function smartSearchContainersForNode(return undefined;}- const containerIds: ContainerID[] = res.value.reverse();-- for (const containerId of containerIds) {+ // Newest containers first, so a node re-created by undo or redo resolves to+ // its live container.+ for (const containerId of res.value.reverse()) {const maybeContainer = loroManager.getContainerById(containerId);if (maybeContainer.isErr()) {@@ -123,21 +164,9 @@ function smartSearchContainersForNode(return undefined;}- let container = maybeContainer.value;-- container = container?.getAttached();-- if (!container || !(container instanceof LoroMap)) continue;-- const innerValue = getMapValueOrContainer(container, '$');-- if (!('id' in innerValue)) {- continue;- }-- const innerNodeId = innerValue.id as string;+ const container = maybeContainer.value?.getAttached();- if (innerNodeId === nodeId) {+ if (container instanceof LoroMap && nodeIdOfMap(container) === nodeId) {return containerId;}}diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.test.tsnew file mode 100644index 0000000000..c72e97d3ac--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.test.ts@@ -0,0 +1,122 @@+import { loroSyncState } from '@core/component/LexicalMarkdown/utils';+import {+ $updateAllNodeIds,+ type NodeIdMappings,+} from '@macro-inc/lexical-core';+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ createEditor,+ type SerializedEditorState,+ type SerializedLexicalNode,+} from 'lexical';+import { describe, expect, it, vi } from 'vitest';+import { $reconcileLexicalState } from './reconcile';++// The editor utilities pull in the plugin barrel, whose leaves open the+// storage and connection-gateway sockets on import.+vi.mock('@service-storage/websocket', () => ({+ storageWS: { reconnectIfDisconnected: vi.fn() },+ createWebSocketJob: vi.fn(),+}));+vi.mock('@service-connection/websocket', () => ({+ ws: { addEventListener: vi.fn(), send: vi.fn() },+ state: () => 'closed',+ createConnectionBlockWebsocketEffect: vi.fn(),+ createConnectionWebsocketEffect: vi.fn(),+}));++type SerializedParagraph = SerializedLexicalNode & {+ children: (SerializedLexicalNode & { text: string })[];+};++describe('$reconcileLexicalState', () => {+ it('applies removals, reorders, and runs of new paragraphs in one pass', () => {+ const editor = createEditor({+ namespace: 'reconcile',+ onError: (error) => {+ throw error;+ },+ });+ const mappings: NodeIdMappings = {+ idToNodeKeyMap: new Map(),+ nodeKeyToIdMap: new Map(),+ };+ editor.update(+ () => {+ for (const text of ['a', 'b', 'c', 'd', 'e']) {+ $getRoot().append(+ $createParagraphNode().append($createTextNode(text))+ );+ }+ $updateAllNodeIds(mappings);+ },+ { discrete: true }+ );+ const local = structuredClone(+ loroSyncState(editor.getEditorState())+ ) as SerializedEditorState;+ const [a, , c, d, e] = local.root.children as SerializedParagraph[];++ // Peers' new paragraphs, as Loro holds them: ids of their own.+ const added = (text: string, id: string): SerializedParagraph => ({+ ...structuredClone(a),+ $: { id },+ children: [+ { ...structuredClone(a.children[0]), $: { id: `${id}-text` }, text },+ ],+ });+ e.children[0].text = 'e edited';+ const remote: SerializedEditorState = {+ ...local,+ root: {+ ...local.root,+ children: [+ added('x', 'x'),+ added('y', 'y'),+ d,+ a,+ added('z', 'z'),+ e,+ c,+ ],+ },+ };+ const keysBefore = editor.read(() =>+ $getRoot()+ .getChildren()+ .map((node) => node.getKey())+ );++ editor.update(+ () =>+ $reconcileLexicalState(+ editor.getEditorState().toJSON(),+ remote,+ mappings,+ () => '1'+ ),+ { discrete: true }+ );++ expect(+ editor.read(() =>+ $getRoot()+ .getChildren()+ .map((node) => node.getTextContent())+ )+ ).toEqual(['x', 'y', 'd', 'a', 'z', 'e edited', 'c']);+ // Surviving paragraphs keep their nodes; `b` is gone.+ const keysAfter = editor.read(() =>+ $getRoot()+ .getChildren()+ .map((node) => node.getKey())+ );+ expect(keysAfter[2]).toBe(keysBefore[3]);+ expect(keysAfter[3]).toBe(keysBefore[0]);+ expect(keysAfter[5]).toBe(keysBefore[4]);+ expect(keysAfter[6]).toBe(keysBefore[2]);+ expect(keysAfter).not.toContain(keysBefore[1]);+ });+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.tsindex a9a8c15fad..cf97ee2999 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts@@ -216,6 +216,8 @@ function $createNode(/*** Removes the children of `parent` that the new state no longer lists there.* Nodes with ids are found by id: one may already have moved to a new parent.+ * Nodes without ids are matched by position, walking the live children+ * alongside the old ones.*/function $removeDroppedChildren(reconcile: Reconcile,@@ -228,11 +230,15 @@ function $removeDroppedChildren(const id = getNodeId(child);if (id) newIds.add(id);}- for (let i = oldChildren.length - 1; i >= 0; i--) {- const id = getNodeId(oldChildren[i]);+ const dropped: LexicalNode[] = [];+ let child = parent.getFirstChild();+ for (const oldChild of oldChildren) {+ const live = child;+ child = child?.getNextSibling() ?? null;+ const id = getNodeId(oldChild);if (id && newIds.has(id)) continue;if (!id) {- if (i < parent.getChildrenSize()) parent.splice(i, 1, []);+ if (live) dropped.push(live);continue;}const node = $getNodeById(@@ -240,10 +246,9 @@ function $removeDroppedChildren(reconcile.mapping.idToNodeKeyMap,id);- if (node?.getParent()?.is(parent)) {- parent.splice(node.getIndexWithinParent(), 1, []);- }+ if (node?.getParent()?.is(parent)) dropped.push(node);}+ for (const node of dropped) node.remove(true);}/** Diff and apply changes between two sets of nodes@@ -274,24 +279,44 @@ function $applyChildren($removeDroppedChildren(reconcile, parent, oldChildren, newChildren);- for (let newIndex = 0; newIndex < newChildren.length; newIndex++) {- const newChild = newChildren[newIndex];+ // The first `index` children are in place and `previous` is the last of+ // them. New nodes collect in `inserts` and go in with one splice per run,+ // since each splice walks to its index.+ let index = 0;+ let previous: LexicalNode | null = null;+ let inserts: LexicalNode[] = [];+ const flushInserts = () => {+ if (inserts.length === 0) return;+ parent.splice(index, 0, inserts);+ index += inserts.length;+ previous = inserts[inserts.length - 1];+ inserts = [];+ };++ for (const newChild of newChildren) {const id = getNodeId(newChild);const node = id ? $getNodeById(editor, mapping.idToNodeKeyMap, id) : null;if (!node || !id) {- parent.splice(newIndex, 0, [$createNode(reconcile, newChild)]);+ inserts.push($createNode(reconcile, newChild));continue;}- if (!node.getParent()?.is(parent)) {- // Moved here from another parent: splice detaches it from that one.- parent.splice(newIndex, 0, [node]);- } else if (node.getIndexWithinParent() !== newIndex) {- parent.splice(node.getIndexWithinParent(), 1, []);- parent.splice(newIndex, 0, [node]);+ flushInserts();+ // Read after any creates: one may have moved the old next sibling into+ // a new parent.+ const expected = previous+ ? previous.getNextSibling()+ : parent.getFirstChild();+ if (!node.is(expected)) {+ // Moved here from another parent or from later in this one: splice+ // detaches it from where it was.+ parent.splice(index, 0, [node]);}+ index++;+ previous = node;$updateExistingNode(reconcile, id, newChild);}+ flushInserts();}diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.tsindex 3c0eca13ab..c8f251e8a3 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts@@ -5,6 +5,8 @@import { mergeRegister } from '@lexical/utils';import type { LoroManager } from '@macro-inc/collaboration/collab/manager';+import { COLLABORATION_TAG } from '@macro-inc/lexical-core';+import { Telemetry } from '@macro-inc/observability';import type { EditorState, LexicalEditor } from 'lexical';import {$isRangeSelection,@@ -362,6 +364,41 @@ export function registerLoroHistory(editor.dispatchCommand(CAN_REDO_COMMAND, undoManager.canRedo());};+ /**+ * Runs one undo or redo in a trace. `editor.history.loro` covers Loro's undo+ * manager and the mirror reading its result; `editor.history.apply` lasts+ * until the reconciled editor update commits.+ */+ const traceHistoryStep = (+ name: 'editor.undo' | 'editor.redo',+ step: () => boolean+ ) => {+ const span = Telemetry.span(name);+ span.setAttr('document.id', loroManager.documentId);+ span.setAttr('editor.node_count', editor.getEditorState()._nodeMap.size);+ const loroSpan = span.span('editor.history.loro');+ const applied = loroSpan.run(step);+ loroSpan.end();+ span.setAttr('history.applied', applied);+ if (!applied) {+ span.end();+ return;+ }+ const applySpan = span.span('editor.history.apply');+ const unregisterApplied = editor.registerUpdateListener(+ ({ tags, dirtyElements, dirtyLeaves }) => {+ if (!tags.has(COLLABORATION_TAG)) return;+ unregisterApplied();+ applySpan.setAttr(+ 'editor.dirty_node_count',+ dirtyElements.size + dirtyLeaves.size+ );+ applySpan.end();+ span.end();+ }+ );+ };+const unregister = mergeRegister(editor.registerCommand(UNDO_COMMAND,@@ -372,7 +409,7 @@ export function registerLoroHistory(isGroupActive = false;}- undoManager.undo();+ traceHistoryStep('editor.undo', () => undoManager.undo());editor.dispatchCommand(CAN_UNDO_COMMAND, undoManager.canUndo());editor.dispatchCommand(CAN_REDO_COMMAND, undoManager.canRedo());@@ -392,7 +429,7 @@ export function registerLoroHistory(isGroupActive = false;}- undoManager.redo();+ traceHistoryStep('editor.redo', () => undoManager.redo());editor.dispatchCommand(CAN_UNDO_COMMAND, undoManager.canUndo());editor.dispatchCommand(CAN_REDO_COMMAND, undoManager.canRedo());diff --git a/docs/agent-guide/documents.md b/docs/agent-guide/documents.mdindex 2cc889b4e6..94903c7dff 100644--- a/docs/agent-guide/documents.md+++ b/docs/agent-guide/documents.md@@ -3061,6 +3061,15 @@ In a disposable Markdown document, change several paragraphs in one edit, thenundo and redo. Verify the text, paragraph count, and a second peer agree aftereach operation; wait for saving to finish and reload to check persistence.+Undo timing has a backend-free browser regression over a 4,000-paragraph+document (about the length of Moby-Dick): undoing typing and undoing the+deletion of a thousand paragraphs each stay within a time budget.+`bunx playwright test --config src/lib/core/component/LexicalMarkdown/collaboration/browser-test/playwright.config.ts`+from `apps/web`. In traces, `editor.undo` / `editor.redo` carry+`document.id` and `editor.node_count`; the `editor.history.loro` child covers+Loro's undo and the mirror read, and `editor.history.apply` lasts until the+reconciled editor update commits.+### Task mention statusTask @mention pills and @mention menu rows display the same circulardiff --git a/packages/collaboration/src/collab/manager.ts b/packages/collaboration/src/collab/manager.tsindex 765b00f3b3..d71650d06b 100644--- a/packages/collaboration/src/collab/manager.ts+++ b/packages/collaboration/src/collab/manager.ts@@ -188,6 +188,9 @@ export class LoroManager<S extends GenericRootSchema = GenericRootSchema>get initialized(): boolean {return this._initialized;}+ get documentId(): string {+ return this.options.documentId;+ }get peerId(): bigint {return this._doc.peerId;}diff --git a/packages/loro-mirror/AGENTS.md b/packages/loro-mirror/AGENTS.mdindex 1453d1a205..9ee20904b8 100644--- a/packages/loro-mirror/AGENTS.md+++ b/packages/loro-mirror/AGENTS.md@@ -10,7 +10,9 @@ Tree nodes project as `{ data, children }`. Their schema supplies a globallyunique application ID per tree. Reparent/reorder surviving IDs through nativemoves; never replace their native IDs or data maps. Validate the entire proposalbefore writes, including when scalar validation is disabled. One setState call-commits once; one native batch notifies once. Atomic Value fields remain plain+commits once; one native batch notifies once. Subtrees with no tree schema+read as one native `toJSON`, and a native batch registers only containers the+registry has not seen, so undo of a large edit stays linear. Atomic Value fields remain plainmap registers. Dispose Mirror before the document owner frees its LoroDoc.## Checksdiff --git a/packages/loro-mirror/src/core/registry.ts b/packages/loro-mirror/src/core/registry.tsindex d72afd66d6..4395164d7e 100644--- a/packages/loro-mirror/src/core/registry.ts+++ b/packages/loro-mirror/src/core/registry.ts@@ -47,18 +47,39 @@ export class ContainerRegistry {return isContainerSchema(child) ? child : undefined;}+ /** Registers `id` and every container under it. */register(id: ContainerID, schema: ContainerSchemaType | undefined) {+ this.walk(id, schema, true);+ }++ /**+ * `walkKnown` false skips children already registered. Native events use it:+ * their new containers have new ids, and a known container's descendants+ * were registered with it or arrive as events of their own.+ */+ private walk(+ id: ContainerID,+ schema: ContainerSchemaType | undefined,+ walkKnown: boolean+ ) {const container = this.doc.getContainerById(id);if (!container) return;this.set(id, schema);+ const walkChild = (+ childId: ContainerID,+ childSchema: ContainerSchemaType | undefined+ ) => {+ if (walkKnown || !this.schemas.has(childId))+ this.walk(childId, childSchema, walkKnown);+ };if (container instanceof LoroTree && schema && isLoroTreeSchema(schema)) {for (const node of container.getNodes())- this.register(node.data.id, schema.nodeSchema);+ walkChild(node.data.id, schema.nodeSchema);} else if (container instanceof LoroMap) {for (const key of container.keys()) {const value = container.get(key);if (isContainer(value))- this.register(value.id, this.childContainer(id, key));+ walkChild(value.id, this.childContainer(id, key));}} else if (container instanceof LoroList ||@@ -71,7 +92,7 @@ export class ContainerRegistry {? schema.itemSchema: undefined;for (const value of container.toArray()) {- if (isContainer(value)) this.register(value.id, itemSchema);+ if (isContainer(value)) walkChild(value.id, itemSchema);}}}@@ -80,6 +101,6 @@ export class ContainerRegistry {// Parent registration discovers newly attached descendants regardless of// whether their data events precede or follow the parent's event in a batch.for (const event of batch.events)- this.register(event.target, this.get(event.target));+ this.walk(event.target, this.get(event.target), false);}}diff --git a/packages/loro-mirror/src/core/tree.ts b/packages/loro-mirror/src/core/tree.tsindex 6e989b971f..7138745f0c 100644--- a/packages/loro-mirror/src/core/tree.ts+++ b/packages/loro-mirror/src/core/tree.ts@@ -25,11 +25,37 @@ import { deepEqual, getRootContainerByType } from './utils';type TreeSchema = LoroTreeSchema<LoroMapSchema<Record<string, SchemaType>>>;-/** Read containers through their schemas: Loro's JSON tree includes native IDs. */+const SCHEMA_CONTAINS_TREE = new WeakMap<SchemaType, boolean>();++/** Whether a tree container can appear under `schema`; schemas may be recursive. */+function containsTreeSchema(schema: SchemaType): boolean {+ const known = SCHEMA_CONTAINS_TREE.get(schema);+ if (known !== undefined) return known;+ const visited = new Set<SchemaType>();+ const visit = (current: SchemaType): boolean => {+ if (visited.has(current)) return false;+ visited.add(current);+ if (isLoroTreeSchema(current)) return true;+ if (isListLikeSchema(current)) return visit(current.itemSchema);+ if (current.type === 'loro-map' || isRootSchemaType(current))+ return Object.values(current.definition).some(visit);+ return false;+ };+ const contains = visit(schema);+ SCHEMA_CONTAINS_TREE.set(schema, contains);+ return contains;+}++/**+ * Read containers through their schemas: Loro's JSON tree includes native IDs.+ * Subtrees without a tree read as one native `toJSON`, which is the same value+ * without a call into Loro per container.+ */export function readContainer(container: Container,schema: SchemaType | undefined): unknown {+ if (!schema || !containsTreeSchema(schema)) return container.toJSON();if (container instanceof LoroTree && schema && isLoroTreeSchema(schema)) {const visit = (nodes: ReturnType<LoroTree['roots']>): unknown[] =>nodes.map((node) => ({@@ -71,7 +97,11 @@ export function readDocument(doc: LoroDoc, schema: SchemaType | undefined) {const state = doc.toJSON();if (schema && isRootSchemaType(schema)) {for (const [key, field] of Object.entries(schema.definition)) {- if (isContainerSchema(field) && key in state) {+ if (+ isContainerSchema(field) &&+ key in state &&+ containsTreeSchema(field)+ ) {const container = getRootContainerByType(doc,key,