Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): Pattern-D pre-warm schema-instance identity (#1758)
Nick Gomez8aeec6ff5a52
diff --git a/.changeset/pattern-d-schema-identity.md b/.changeset/pattern-d-schema-identity.mdnew file mode 100644index 000000000..6ebfd9652--- /dev/null+++ b/.changeset/pattern-d-schema-identity.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Fix silent loss of unchanged content after a document editor opens. The editor's pre-warm fast path built its node cache against a throwaway internal schema instance; because the editor matches content by schema-instance identity, the first incremental collaborative or agent edit after opening could silently drop unchanged sibling paragraphs from the visible editor, and the next click or keystroke could republish that truncated copy over the shared document — erasing the content for every peer and on disk. The pre-warm walk now runs during editor construction against the editor's own schema, so cached nodes always match and unchanged content survives incremental updates.diff --git a/packages/app/src/editor/TiptapEditor.test.tsx b/packages/app/src/editor/TiptapEditor.test.tsxindex 278f5b40d..cd6f20025 100644--- a/packages/app/src/editor/TiptapEditor.test.tsx+++ b/packages/app/src/editor/TiptapEditor.test.tsx@@ -1,7 +1,25 @@-import { describe, expect, test } from 'bun:test';+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import type { HocuspocusProvider } from '@hocuspocus/provider';+import { Editor } from '@tiptap/core';+import type { Node as ProseMirrorNode } from '@tiptap/pm/model';import * as Y from 'yjs';import { buildPatternDConstructorOptions } from './TiptapEditor';+import {+ buildSeededPatternDProvider,+ fakeClipboard,+ installDomGlobals,+} from './walk-currency-test-harness';++let restoreDomGlobals: (() => void) | null = null;++beforeAll(() => {+ restoreDomGlobals = installDomGlobals();+});++afterAll(() => {+ restoreDomGlobals?.();+ restoreDomGlobals = null;+});describe('buildPatternDConstructorOptions', () => {function makeFakeProvider(): HocuspocusProvider {@@ -13,16 +31,7 @@ describe('buildPatternDConstructorOptions', () => {} as unknown as HocuspocusProvider;}- type ClipboardArg = Parameters<typeof buildPatternDConstructorOptions>[0]['clipboard'];- const fakeClipboard = {- mdManager: {},- text: () => '',- html: { serializer: {}, setView: () => {} },- paste: () => false,- drop: () => false,- } as unknown as ClipboardArg;-- test('always passes element: null explicitly (D12 1-way door regression guard)', () => {+ test('always passes element: null explicitly (1-way door regression guard)', () => {const opts = buildPatternDConstructorOptions({provider: makeFakeProvider(),clipboard: fakeClipboard,@@ -33,27 +42,70 @@ describe('buildPatternDConstructorOptions', () => {expect(opts.element).not.toBeUndefined();});- test('content is set from a pre-built PM doc walk (Q21 pre-warm)', () => {- const opts = buildPatternDConstructorOptions({- provider: makeFakeProvider(),+ /** Build options against a seeded one-paragraph fragment, exposing the+ * mapping Map the options handed to the Collaboration extension so the+ * pins can observe it across the `new Editor(...)` boundary. */+ function buildSeededOptions() {+ const { provider, cleanup } = buildSeededPatternDProvider('tiptap-editor-pins');+ const options = buildPatternDConstructorOptions({+ provider,clipboard: fakeClipboard,ctorStart: 0,});- expect(opts.content).toBeDefined();- expect(opts.content).toMatchObject({ type: 'doc' });+ const collaboration = options.extensions?.find((ext) => ext.name === 'collaboration') as+ | {+ options?: {+ ySyncOptions?: { mapping?: Map<unknown, ProseMirrorNode | ProseMirrorNode[]> };+ };+ }+ | undefined;+ const handedMapping = collaboration?.options?.ySyncOptions?.mapping;+ if (!(handedMapping instanceof Map)) {+ throw new Error('expected the options to hand a Map via ySyncOptions.mapping');+ }+ return { options, handedMapping, cleanup };+ }++ test('the construct-time walk injects the walked fragment content into the editor state (Q21 pre-warm)', () => {+ const { options, cleanup } = buildSeededOptions();+ let editor: Editor | null = null;+ try {+ editor = new Editor(options);+ expect(editor.state.doc.textContent).toContain('hello world');+ } finally {+ editor?.destroy();+ cleanup();+ }});- test('Collaboration extension carries ySyncOptions.mapping populated by the same walk', () => {- const opts = buildPatternDConstructorOptions({- provider: makeFakeProvider(),- clipboard: fakeClipboard,- ctorStart: 0,- });- const collaboration = opts.extensions?.find((ext) => ext.name === 'collaboration') as- | { options?: { ySyncOptions?: { mapping?: unknown } } }- | undefined;- expect(collaboration).toBeDefined();- expect(collaboration?.options?.ySyncOptions?.mapping).toBeDefined();- expect(collaboration?.options?.ySyncOptions?.mapping).toBeInstanceOf(Map);+ test('ySyncOptions.mapping is the options-handed Map instance, populated in place by construction', () => {+ const { options, handedMapping, cleanup } = buildSeededOptions();+ let editor: Editor | null = null;+ try {+ expect(handedMapping.size).toBe(0);+ editor = new Editor(options);+ expect(handedMapping.size).toBeGreaterThanOrEqual(1);+ } finally {+ editor?.destroy();+ cleanup();+ }+ });++ test('every mapping node belongs to the constructed editor schema instance (schema affinity)', () => {+ const { options, handedMapping, cleanup } = buildSeededOptions();+ let editor: Editor | null = null;+ try {+ editor = new Editor(options);+ const nodes = [...handedMapping.values()].flatMap((value) =>+ Array.isArray(value) ? value : [value],+ );+ expect(nodes.length).toBeGreaterThanOrEqual(1);+ for (const node of nodes) {+ expect(node.type.schema).toBe(editor.schema);+ }+ } finally {+ editor?.destroy();+ cleanup();+ }});});diff --git a/packages/app/src/editor/TiptapEditor.tsx b/packages/app/src/editor/TiptapEditor.tsxindex 8a57ce4a6..17487987d 100644--- a/packages/app/src/editor/TiptapEditor.tsx+++ b/packages/app/src/editor/TiptapEditor.tsx@@ -8,7 +8,7 @@ import {hasNewEntries,MarkdownManager,} from '@inkeep/open-knowledge-core';-import { type AnyExtension, Editor, type EditorOptions, Extension, getSchema } from '@tiptap/core';+import { type AnyExtension, Editor, type EditorOptions, Extension } from '@tiptap/core';import Collaboration from '@tiptap/extension-collaboration';import Placeholder from '@tiptap/extension-placeholder';import { EditorContent } from '@tiptap/react';@@ -255,20 +255,28 @@ export function buildPatternDConstructorOptions(args: BuildPatternDConstructorOptionsArgs,): PatternDConstructorOptions {const { provider, placeholder, clipboard, ctorStart, onWedged } = args;- const baseExtensions = buildExtensionList({ provider, placeholder, clipboard, ctorStart });- const schema = getSchema(baseExtensions);const fragment = provider.document.getXmlFragment('default');- const { doc: prebuiltDoc, mapping: prebuiltMapping } = initProseMirrorDoc(fragment, schema);+ const prebuiltMapping: ProsemirrorMapping = new Map();+ const baseOptions = buildEditorOptions({+ provider,+ placeholder,+ clipboard,+ ctorStart,+ prebuiltMapping,+ onWedged,+ });+ const baseOnBeforeCreate = baseOptions.onBeforeCreate;return {- ...buildEditorOptions({- provider,- placeholder,- clipboard,- ctorStart,- prebuiltMapping,- onWedged,- }),- content: prebuiltDoc.toJSON(),+ ...baseOptions,+ onBeforeCreate: (props) => {+ baseOnBeforeCreate?.(props);+ const { editor } = props;+ const { doc, mapping } = initProseMirrorDoc(fragment, editor.schema);+ mapping.forEach((node, key) => {+ prebuiltMapping.set(key, node);+ });+ editor.options.content = doc.toJSON();+ },element: null,};}diff --git a/packages/app/src/editor/mount-promise.test.ts b/packages/app/src/editor/mount-promise.test.tsindex 46f62e06e..de231d0db 100644--- a/packages/app/src/editor/mount-promise.test.ts+++ b/packages/app/src/editor/mount-promise.test.ts@@ -493,6 +493,7 @@ describe('mount-failure error path', () => {});test('construct() throws → promise rejects with the original error, no mount call', async () => {+ getCollector()?.reset();const constructError = new Error('synthetic construct failure');const promise = mountTiptapEditorPromise({docName: 'doc-construct-fail',@@ -503,6 +504,10 @@ describe('mount-failure error path', () => {});await expect(promise).rejects.toBe(constructError);expect(__getCacheSize('tiptap')).toBe(0);+ const marks = getCollector()?.marks.toArray() ?? [];+ const rejectMark = marks.find((m) => m.name === 'ok/mount/reject');+ expect(rejectMark?.properties?.reason).toBe('construct-failed');+ expect(rejectMark?.properties?.message).toContain('synthetic construct failure');});test('destroy() throws after mount() throws → promise still rejects with original mount error', async () => {diff --git a/packages/app/src/editor/mount-promise.ts b/packages/app/src/editor/mount-promise.tsindex 89d0d3357..94368aa37 100644--- a/packages/app/src/editor/mount-promise.ts+++ b/packages/app/src/editor/mount-promise.ts@@ -214,6 +214,7 @@ export function mountTiptapEditorPromise(docName,mountId: entry.mountId,reason: 'unhandled-body-throw',+ message: wrapped.message,});rejectFn(wrapped);finalizeColdMountSpan(entry.mountId);@@ -327,6 +328,7 @@ async function runMountBody(params: MountBodyParams): Promise<void> {docName,mountId: entry.mountId,reason: 'construct-failed',+ message: wrapped.message,});rejectFn(wrapped);finalizeColdMountSpan(entry.mountId);@@ -353,6 +355,7 @@ async function runMountBody(params: MountBodyParams): Promise<void> {docName,mountId: entry.mountId,reason: 'mount-failed',+ message: wrapped.message,});rejectFn(wrapped);finalizeColdMountSpan(entry.mountId);@@ -377,6 +380,7 @@ async function runMountBody(params: MountBodyParams): Promise<void> {docName,mountId: entry.mountId,reason: 'v2-register-failed',+ message: wrapped.message,});rejectFn(wrapped);finalizeColdMountSpan(entry.mountId);diff --git a/packages/app/src/editor/pattern-d-schema-identity.test.ts b/packages/app/src/editor/pattern-d-schema-identity.test.tsnew file mode 100644index 000000000..292a21a7b--- /dev/null+++ b/packages/app/src/editor/pattern-d-schema-identity.test.ts@@ -0,0 +1,176 @@+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { Editor } from '@tiptap/core';+import type * as Y from 'yjs';+import { buildPatternDConstructorOptions } from './TiptapEditor';+import {+ applyRemoteEdit,+ buildSeededPatternDProvider,+ dispatchSelectionOnly,+ fakeClipboard,+ flushMicrotasksAndTimers,+ insertParagraphAt,+ installDomGlobals,+ seedFragmentParagraph,+} from './walk-currency-test-harness';++let restoreDomGlobals: (() => void) | null = null;++beforeAll(() => {+ restoreDomGlobals = installDomGlobals();+});++afterAll(() => {+ restoreDomGlobals?.();+ restoreDomGlobals = null;+});++interface MountedPatternDHarness {+ editor: Editor;+ ydoc: Y.Doc;+ fragment: Y.XmlFragment;+ cleanup: () => void;+}++async function mountPatternDEditor(+ seed: (ydoc: Y.Doc) => void = (ydoc) => seedFragmentParagraph(ydoc, 'hello world'),+): Promise<MountedPatternDHarness> {+ const {+ ydoc,+ fragment,+ provider,+ cleanup: providerCleanup,+ } = buildSeededPatternDProvider('schema-identity', seed);++ const options = buildPatternDConstructorOptions({+ provider,+ clipboard: fakeClipboard,+ ctorStart: performance.now(),+ });+ const editor = new Editor(options);+ const host = document.createElement('div');+ document.body.appendChild(host);+ editor.mount(host);+ await flushMicrotasksAndTimers();++ const cleanup = () => {+ editor.destroy();+ host.remove();+ providerCleanup();+ };+ return { editor, ydoc, fragment, cleanup };+}++describe('Pattern D schema-instance identity (post-mount incremental updates)', () => {+ test('an unchanged sibling paragraph survives a post-mount remote paragraph insert', async () => {+ const harness = await mountPatternDEditor();+ try {+ applyRemoteEdit(harness.ydoc, (frag) => insertParagraphAt(frag, 1, 'second paragraph'));+ await flushMicrotasksAndTimers();++ const pmText = harness.editor.state.doc.textContent;+ expect(pmText).toContain('second paragraph');+ expect(pmText).toContain('hello world');++ const yXml = harness.fragment.toString();+ expect(yXml).toContain('second paragraph');+ expect(yXml).toContain('hello world');+ } finally {+ harness.cleanup();+ }+ });++ test('the first post-update user transaction does not erase the unchanged paragraph from the CRDT', async () => {+ const harness = await mountPatternDEditor();+ try {+ applyRemoteEdit(harness.ydoc, (frag) => insertParagraphAt(frag, 1, 'second paragraph'));+ await flushMicrotasksAndTimers();++ dispatchSelectionOnly(harness.editor);+ await flushMicrotasksAndTimers();++ const yXml = harness.fragment.toString();+ expect(yXml).toContain('hello world');+ expect(yXml).toContain('second paragraph');+ const pmText = harness.editor.state.doc.textContent;+ expect(pmText).toContain('hello world');+ expect(pmText).toContain('second paragraph');+ } finally {+ harness.cleanup();+ }+ });++ test('an unchanged paragraph survives a post-mount remote text edit inside a DIFFERENT walked paragraph', async () => {+ const harness = await mountPatternDEditor((ydoc) => {+ seedFragmentParagraph(ydoc, 'hello world');+ insertParagraphAt(ydoc.getXmlFragment('default'), 1, 'closing notes');+ });+ try {+ applyRemoteEdit(harness.ydoc, (frag) => {+ const second = frag.get(1) as Y.XmlElement;+ const text = second.get(0) as Y.XmlText;+ text.insert(text.length, ' EDITED');+ });+ await flushMicrotasksAndTimers();++ const pmText = harness.editor.state.doc.textContent;+ expect(pmText).toContain('closing notes EDITED');+ expect(pmText).toContain('hello world');++ dispatchSelectionOnly(harness.editor);+ await flushMicrotasksAndTimers();++ const yXml = harness.fragment.toString();+ expect(yXml).toContain('hello world');+ expect(yXml).toContain('closing notes EDITED');+ } finally {+ harness.cleanup();+ }+ });++ test('an unchanged sibling paragraph survives a post-mount remote prepend insert', async () => {+ const harness = await mountPatternDEditor();+ try {+ applyRemoteEdit(harness.ydoc, (frag) => insertParagraphAt(frag, 0, 'prepended paragraph'));+ await flushMicrotasksAndTimers();++ const pmText = harness.editor.state.doc.textContent;+ expect(pmText).toContain('prepended paragraph');+ expect(pmText).toContain('hello world');++ const yXml = harness.fragment.toString();+ expect(yXml).toContain('prepended paragraph');+ expect(yXml).toContain('hello world');+ } finally {+ harness.cleanup();+ }+ });++ test('repeated incremental remote inserts never drop the original paragraph', async () => {+ const harness = await mountPatternDEditor();+ try {+ for (let i = 1; i <= 3; i += 1) {+ applyRemoteEdit(harness.ydoc, (frag) =>+ insertParagraphAt(frag, frag.length, `update ${i}`),+ );+ await flushMicrotasksAndTimers();+ expect(harness.editor.state.doc.textContent).toContain('hello world');+ expect(harness.editor.state.doc.textContent).toContain(`update ${i}`);+ }++ const yXml = harness.fragment.toString();+ expect(yXml).toContain('hello world');+ for (let i = 1; i <= 3; i += 1) {+ expect(yXml).toContain(`update ${i}`);+ }+ dispatchSelectionOnly(harness.editor);+ await flushMicrotasksAndTimers();+ const postClickYXml = harness.fragment.toString();+ expect(postClickYXml).toContain('hello world');+ for (let i = 1; i <= 3; i += 1) {+ expect(postClickYXml).toContain(`update ${i}`);+ }+ } finally {+ harness.cleanup();+ }+ });+});diff --git a/packages/app/src/editor/pattern-d-walk-currency.test.ts b/packages/app/src/editor/pattern-d-walk-currency.test.tsindex 831d105d4..fc07aec25 100644--- a/packages/app/src/editor/pattern-d-walk-currency.test.ts+++ b/packages/app/src/editor/pattern-d-walk-currency.test.ts@@ -2,22 +2,22 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:testimport { randomUUID } from 'node:crypto';import type { HocuspocusProvider } from '@hocuspocus/provider';import { Editor } from '@tiptap/core';-import { TextSelection } from '@tiptap/pm/state';-import { Awareness } from 'y-protocols/awareness';-import * as Y from 'yjs';+import type { Awareness } from 'y-protocols/awareness';+import type * as Y from 'yjs';import { __resetCacheForTests, evictTiptapEditor } from './editor-cache';import { __resetMountPromiseCache, mountTiptapEditorPromise } from './mount-promise';import { buildPatternDConstructorOptions } from './TiptapEditor';import {appendToFirstParagraph,applyRemoteEdit,+ buildSeededPatternDProvider,createGapOrderingRecorder,+ dispatchSelectionOnly,fakeClipboard,flushMicrotasksAndTimers,type GapOrderingRecorder,insertParagraphAt,installDomGlobals,- seedFragmentParagraph,viewCreationSignalExtension,} from './walk-currency-test-harness';@@ -32,11 +32,6 @@ afterAll(() => {restoreDomGlobals = null;});-function dispatchSelectionOnly(editor: Editor): void {- const { state } = editor.view;- editor.view.dispatch(state.tr.setSelection(TextSelection.create(state.doc, 1)));-}-interface GapMountHarness {docName: string;ydoc: Y.Doc;@@ -53,16 +48,14 @@ interface GapMountHarness {}function createGapMountHarness(gapEdit: (fragment: Y.XmlFragment) => void): GapMountHarness {- const docName = `walk-currency-${randomUUID()}`;- const ydoc = new Y.Doc();- seedFragmentParagraph(ydoc, 'hello world');- const fragment = ydoc.getXmlFragment('default');- const awareness = new Awareness(ydoc);- const provider = {- document: ydoc,- configuration: { name: docName },+ const {+ docName,+ ydoc,+ fragment,awareness,- } as unknown as HocuspocusProvider;+ provider,+ cleanup: providerCleanup,+ } = buildSeededPatternDProvider('walk-currency');const ordering = createGapOrderingRecorder();@@ -100,8 +93,7 @@ function createGapMountHarness(gapEdit: (fragment: Y.XmlFragment) => void): GapMconst cleanup = () => {evictTiptapEditor(docName);- awareness.destroy();- ydoc.destroy();+ providerCleanup();};return { docName, ydoc, fragment, awareness, provider, mountWithGapEdit, ordering, cleanup };diff --git a/packages/app/src/editor/walk-currency-extension.test.ts b/packages/app/src/editor/walk-currency-extension.test.tsindex 79508198a..4e706de1f 100644--- a/packages/app/src/editor/walk-currency-extension.test.ts+++ b/packages/app/src/editor/walk-currency-extension.test.ts@@ -231,9 +231,9 @@ describe('non-stale fast path', () => {const collaboration = opts.extensions?.find((ext) => ext.name === 'collaboration') as| { options?: { ySyncOptions?: { mapping?: Map<unknown, unknown> } } }| undefined;+ const editor = new Editor(opts);prebuiltMapping = collaboration?.options?.ySyncOptions?.mapping ?? null;prebuiltParagraphNode = prebuiltMapping?.get(yParagraph) ?? null;- const editor = new Editor(opts);editor.on('transaction', ({ transaction }) => {const meta = transaction.getMeta(ySyncPluginKey) as| { isChangeOrigin?: boolean }diff --git a/packages/app/src/editor/walk-currency-test-harness.ts b/packages/app/src/editor/walk-currency-test-harness.tsindex 938b79c1c..70821145f 100644--- a/packages/app/src/editor/walk-currency-test-harness.ts+++ b/packages/app/src/editor/walk-currency-test-harness.ts@@ -1,6 +1,9 @@-import { Extension } from '@tiptap/core';-import { Plugin } from '@tiptap/pm/state';+import { randomUUID } from 'node:crypto';+import type { HocuspocusProvider } from '@hocuspocus/provider';+import { type Editor, Extension } from '@tiptap/core';+import { Plugin, TextSelection } from '@tiptap/pm/state';import { JSDOM } from 'jsdom';+import { Awareness } from 'y-protocols/awareness';import * as Y from 'yjs';import type { buildPatternDConstructorOptions } from './TiptapEditor';@@ -68,6 +71,41 @@ export function seedFragmentParagraph(ydoc: Y.Doc, text: string): void {fragment.insert(0, [paragraph]);}+export function dispatchSelectionOnly(editor: Editor): void {+ const { state } = editor.view;+ editor.view.dispatch(state.tr.setSelection(TextSelection.create(state.doc, 1)));+}++export interface SeededPatternDProvider {+ docName: string;+ ydoc: Y.Doc;+ fragment: Y.XmlFragment;+ awareness: Awareness;+ provider: HocuspocusProvider;+ cleanup: () => void;+}++export function buildSeededPatternDProvider(+ docNamePrefix: string,+ seed: (ydoc: Y.Doc) => void = (ydoc) => seedFragmentParagraph(ydoc, 'hello world'),+): SeededPatternDProvider {+ const docName = `${docNamePrefix}-${randomUUID()}`;+ const ydoc = new Y.Doc();+ seed(ydoc);+ const fragment = ydoc.getXmlFragment('default');+ const awareness = new Awareness(ydoc);+ const provider = {+ document: ydoc,+ configuration: { name: docName },+ awareness,+ } as unknown as HocuspocusProvider;+ const cleanup = () => {+ awareness.destroy();+ ydoc.destroy();+ };+ return { docName, ydoc, fragment, awareness, provider, cleanup };+}+/** Any Y transaction origin other than the binding's own — in production the* origin is the HocuspocusProvider instance. */const REMOTE_PROVIDER_ORIGIN = Object.freeze({ kind: 'remote-provider-stand-in' });