Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): preserve agent bytes in same-block WYSIWYG edits (#2625)

Nick Gomez96563d1ea9b5
diff --git a/.changeset/writer-attributed-splice.md b/.changeset/writer-attributed-splice.md
new file mode 100644
index 000000000..5523155a3
--- /dev/null
+++ b/.changeset/writer-attributed-splice.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Preserve agent-authored byte-forms through WYSIWYG edits in the same block. Blockquote lazy continuations now round-trip via a `'lazy'` marker-spacing capture, tight ATX heading-paragraph adjacency round-trips via the existing contiguity attribute, and Observer A's map-driven splice narrows into an edited blockquote, list, or list item so untouched sibling children keep their source bytes (including blank-line runs inside list items).
diff --git a/packages/app/tests/integration/writer-splice-same-block-preservation.test.ts b/packages/app/tests/integration/writer-splice-same-block-preservation.test.ts
new file mode 100644
index 000000000..3c63af19f
--- /dev/null
+++ b/packages/app/tests/integration/writer-splice-same-block-preservation.test.ts
@@ -0,0 +1,188 @@
+/**
+ * Same-block byte preservation through Observer A's map-driven splice.
+ *
+ * Contract (per-write-path fidelity): the WYSIWYG serialize path is
+ * construct-canonical — it may canonicalize the construct the human actually
+ * edited, but agent-authored bytes of constructs the human did NOT touch must
+ * survive, including constructs inside the SAME top-level block as the edit
+ * and constructs whose byte-form does not round-trip through parse+serialize
+ * (blockquote lazy continuation, blank-line runs inside list items, tight
+ * ATX-heading adjacency).
+ *
+ * Multi-client topology per the observer-bridge coverage rule: the edit is
+ * authored on client A; assertions run on BOTH clients after convergence,
+ * so remote-peer divergence is covered, not just local echo.
+ */
+
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { setTimeout as wait } from 'node:timers/promises';
+import * as Y from 'yjs';
+import { HARNESS_BOOT_TIMEOUT_MS } from './harness-boot-timeout';
+import {
+ agentWriteMd,
+ assertBridgeInvariant,
+ createTestClients,
+ createTestServer,
+ pollUntil,
+ type TestClient,
+ type TestServer,
+} from './test-harness';
+
+let server: TestServer;
+
+beforeAll(async () => {
+ server = await createTestServer();
+}, HARNESS_BOOT_TIMEOUT_MS);
+
+afterAll(async () => {
+ await server.cleanup();
+});
+
+function findXmlTextContaining(
+ node: Y.XmlFragment | Y.XmlElement,
+ marker: string,
+): Y.XmlText | null {
+ const len = node.length;
+ for (let i = 0; i < len; i++) {
+ const child = node.get(i);
+ if (child instanceof Y.XmlText) {
+ if (child.toString().includes(marker)) return child;
+ } else if (child instanceof Y.XmlElement) {
+ const found = findXmlTextContaining(child, marker);
+ if (found) return found;
+ }
+ }
+ return null;
+}
+
+interface SpliceCase {
+ name: string;
+ seed: string;
+ /** text content of the span the simulated human edits (gets " EDITWORD" appended) */
+ editMarker: string;
+ /** full expected Y.Text bytes after the edit settles */
+ expected: string;
+ covers: string;
+}
+
+/**
+ * Seed via the agent path (bytes land verbatim by construction), append
+ * " EDITWORD" to the edit-target span through client A's XmlFragment (the
+ * WYSIWYG write surface), await convergence, and return both clients' Y.Text.
+ */
+async function runSpliceCase(c: SpliceCase): Promise<{ texts: string[]; clients: TestClient[] }> {
+ const docName = `splice-${crypto.randomUUID()}`;
+ const clients = await createTestClients(server.port, {
+ count: 2,
+ docName,
+ perClientOptions: { skipInvariantWatcher: true },
+ });
+ try {
+ await agentWriteMd(server.port, c.seed, { docName, position: 'replace' });
+ for (const client of clients) {
+ await pollUntil(() => client.ytext.toString().includes(c.editMarker), 5000);
+ }
+ await wait(400);
+
+ const target = findXmlTextContaining(clients[0].fragment, c.editMarker);
+ if (!target) throw new Error(`edit marker not found in fragment: ${c.editMarker}`);
+ target.insert(target.length, ' EDITWORD');
+
+ for (const client of clients) {
+ await pollUntil(() => client.ytext.toString().includes('EDITWORD'), 5000);
+ }
+ await wait(600);
+
+ const texts = clients.map((cl) => cl.ytext.toString());
+ for (const client of clients) {
+ assertBridgeInvariant(client.ytext, client.fragment);
+ }
+ return { texts, clients };
+ } finally {
+ for (const cl of clients) await cl.cleanup();
+ }
+}
+
+const CASES: SpliceCase[] = [
+ {
+ name: 'lazy continuation survives an edit to a sibling paragraph in the SAME blockquote',
+ seed: '> lazy first line\nlazy continuation stays\n>\n> editable second para\n',
+ editMarker: 'editable second para',
+ expected: '> lazy first line\nlazy continuation stays\n>\n> editable second para EDITWORD\n',
+ covers: 'blockquote',
+ },
+ {
+ name: 'lazy continuation survives an edit to a DIFFERENT top-level block',
+ seed: '> lazy ctrl line\nlazy ctrl continuation\n\nSeparate ctrl paragraph.\n',
+ editMarker: 'Separate ctrl paragraph.',
+ expected: '> lazy ctrl line\nlazy ctrl continuation\n\nSeparate ctrl paragraph. EDITWORD\n',
+ covers: 'blockquote',
+ },
+ {
+ name: 'multi-blank run inside a list item survives an edit to a sibling item in the SAME list',
+ seed: '- item one\n\n para in item\n\n\n wide gap para\n- item two editable\n',
+ editMarker: 'item two editable',
+ expected: '- item one\n\n para in item\n\n\n wide gap para\n- item two editable EDITWORD\n',
+ covers: 'list-bullet-dash, list-item',
+ },
+ {
+ name: 'tight ATX heading adjacency survives an edit to the adjacent paragraph',
+ seed: '## TightHead\nTight paragraph editable.\n',
+ editMarker: 'Tight paragraph editable.',
+ expected: '## TightHead\nTight paragraph editable. EDITWORD\n',
+ covers: 'heading-atx-2',
+ },
+];
+
+describe('same-block byte preservation through Observer A', () => {
+ for (const c of CASES) {
+ test(c.name, async () => {
+ const { texts } = await runSpliceCase(c);
+ expect(texts[0]).toBe(c.expected);
+ expect(texts[1]).toBe(texts[0]);
+ }, 25_000);
+ }
+
+ /**
+ * Control pins: attr-covered cosmetic forms already survive same-block
+ * edits via source-form capture/replay. These pin the behavior the fix
+ * must not regress.
+ *
+ */
+ test('attr-covered cosmetic forms survive same-block edits (control pins)', async () => {
+ const cases: Array<Omit<SpliceCase, 'expected' | 'covers'> & { untouched: string[] }> = [
+ {
+ name: 'star bullets',
+ seed: '* alpha item\n* beta item\n',
+ editMarker: 'alpha item',
+ untouched: ['* beta item'],
+ },
+ {
+ name: 'paren ordered',
+ seed: '1) first thing\n2) second thing\n',
+ editMarker: 'first thing',
+ untouched: ['2) second thing'],
+ },
+ {
+ name: 'underscore emphasis sibling span',
+ seed: 'Lead sentence here. Tail with _underscore emphasis_ kept.\n',
+ editMarker: 'kept.',
+ untouched: ['_underscore emphasis_'],
+ },
+ {
+ name: 'padded table cells',
+ seed: '| Name | Value |\n| ------- | ------- |\n| rowone | 111 |\n| rowtwo | 222 |\n',
+ editMarker: 'rowone',
+ untouched: ['| rowtwo | 222 |'],
+ },
+ ];
+ for (const c of cases) {
+ const { texts } = await runSpliceCase({ ...c, expected: '', covers: '' });
+ for (const bytes of c.untouched) {
+ expect(texts[0]).toContain(bytes);
+ }
+ expect(texts[0]).toContain('EDITWORD');
+ expect(texts[1]).toBe(texts[0]);
+ }
+ }, 60_000);
+});
diff --git a/packages/core/src/extensions/heading-fidelity.ts b/packages/core/src/extensions/heading-fidelity.ts
index 1c5577024..d10f1d01c 100644
--- a/packages/core/src/extensions/heading-fidelity.ts
+++ b/packages/core/src/extensions/heading-fidelity.ts
@@ -14,10 +14,11 @@
* content length per CommonMark §4.3 (1+ chars).
* `null` means no captured length (synthesized
* PM tree, ATX form, or legacy input).
- * - sourceContiguousNext: setext-only — the next sibling at parse time
- * was a paragraph with NO blank line between
- * this heading's underline and the paragraph's
- * first line. Drives positionAwareBlankLineJoin
+ * - sourceContiguousNext: the next sibling at parse time was a
+ * paragraph with NO blank line between this
+ * heading's last line (setext underline or the
+ * ATX heading line) and the paragraph's first
+ * line. Drives positionAwareBlankLineJoin
* so the no-blank-line shape survives PM
* round-trip (where source `position` info is
* destroyed). Default `false`.
diff --git a/packages/core/src/markdown/index.ts b/packages/core/src/markdown/index.ts
index 58be35302..48f665bc0 100644
--- a/packages/core/src/markdown/index.ts
+++ b/packages/core/src/markdown/index.ts
@@ -1078,7 +1078,7 @@ function buildPmToMdastHandlers(
if (n.blockquote) {
nodeHandlers.blockquote = fromPmNode('blockquote', (pmNode: PmNode) => {
const spacings = pmNode.attrs.sourceMarkerSpacings;
- const data: { sourceMarkerSpacings?: Array<number | 'single' | 'none'> } = {};
+ const data: { sourceMarkerSpacings?: Array<number | 'single' | 'none' | 'lazy'> } = {};
if (Array.isArray(spacings) && spacings.length > 0) {
data.sourceMarkerSpacings = spacings;
}
diff --git a/packages/core/src/markdown/mdast-augmentation.ts b/packages/core/src/markdown/mdast-augmentation.ts
index e04f30337..ad309fe06 100644
--- a/packages/core/src/markdown/mdast-augmentation.ts
+++ b/packages/core/src/markdown/mdast-augmentation.ts
@@ -177,7 +177,7 @@ declare module 'mdast' {
sourceDelimiter?: '~' | '~~';
}
interface BlockquoteData {
- sourceMarkerSpacings?: Array<number | 'single' | 'none'>;
+ sourceMarkerSpacings?: Array<number | 'single' | 'none' | 'lazy'>;
}
interface TableData {
sourceDashCounts?: number[];
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index 35d74b85e..bfd7e3109 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -39,12 +39,7 @@ export function mergedPostParseWalkerPlugin() {
parent as { type?: string } | undefined,
);
- if (
- node.type === 'heading' &&
- parent !== undefined &&
- typeof index === 'number' &&
- (node as Nodes & { data?: { sourceStyle?: string } }).data?.sourceStyle === 'setext'
- ) {
+ if (node.type === 'heading' && parent !== undefined && typeof index === 'number') {
const headingEnd = node.position?.end?.line;
const next = (parent as Parent).children[index + 1] as Nodes | undefined;
const nextStart = next?.position?.start?.line;
diff --git a/packages/core/src/markdown/position-aware-join.ts b/packages/core/src/markdown/position-aware-join.ts
index 248685cfe..5a5e4c42a 100644
--- a/packages/core/src/markdown/position-aware-join.ts
+++ b/packages/core/src/markdown/position-aware-join.ts
@@ -8,14 +8,14 @@ type MaybeTyped = {
};
type FlowNode = Parameters<Join>[0];
-function isContiguousSetextWithParagraph(left: FlowNode, right: FlowNode): boolean {
+function isContiguousHeadingWithParagraph(left: FlowNode, right: FlowNode): boolean {
const lt = left as MaybeTyped;
const rt = right as MaybeTyped;
return lt.type === 'heading' && lt.data?.sourceContiguousNext === true && rt.type === 'paragraph';
}
export const positionAwareBlankLineJoin: Join = (left, right) => {
- if (isContiguousSetextWithParagraph(left, right)) return 0;
+ if (isContiguousHeadingWithParagraph(left, right)) return 0;
const dataGap = (right as MaybeTyped).data?.sourcePrecedingBlankLines;
if (typeof dataGap === 'number' && dataGap >= 2) return dataGap;
const lp = (left as MaybePositioned).position;
diff --git a/packages/core/src/markdown/position-slice.ts b/packages/core/src/markdown/position-slice.ts
index 50a193a16..51f600876 100644
--- a/packages/core/src/markdown/position-slice.ts
+++ b/packages/core/src/markdown/position-slice.ts
@@ -160,13 +160,16 @@ export function applyPositionSliceToNode(
const slice = source.slice(startOff, endOff);
const lines = slice.split('\n');
- const markerSpacings: number[] = [];
+ const markerSpacings: Array<number | 'lazy'> = [];
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
const line = lines[lineIdx];
- if (line.length === 0) continue;
+ if (line.trim().length === 0) continue;
if (isNested && lineIdx > 0) break;
const stripped = line.replace(/^ {1,3}/, '');
- if (stripped[0] !== '>') continue;
+ if (stripped[0] !== '>') {
+ if (lineIdx > 0 && line.trimStart()[0] !== '>') markerSpacings.push('lazy');
+ continue;
+ }
const restAfterMarker = stripped.slice(1).trimEnd();
if (restAfterMarker === '') continue;
if (stripped[1] === '\t') {
diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.ts
index cba68fb3a..935a1641d 100644
--- a/packages/core/src/markdown/to-markdown-handlers.ts
+++ b/packages/core/src/markdown/to-markdown-handlers.ts
@@ -45,6 +45,9 @@ const upstreamMdxJsxFlowHandler: Handle = (() => {
);
})();
+const LAZY_UNSAFE_LINE_RE =
+ /^[ \t]*(?:$|#{1,6}(?:[ \t]|[ \t]*$)|>|(?:[-+*]|\d{1,9}[.)])[ \t]|`{3,}|~{3,}|=+[ \t]*$|-+[ \t]*$|(?:\*[ \t]*){3,}$|(?:_[ \t]*){3,}$)/;
+
export const toMarkdownHandlers = {
text(node, _parent, state, info) {
if (typeof node.data?.sourceRaw === 'string') {
@@ -705,7 +708,9 @@ export const toMarkdownHandlers = {
return state.indentLines(inner, (line, _lineNumber, blank) => {
if (blank) return '>';
- const spacing = spacings?.[nonBlankIdx++];
+ const lineIdx = nonBlankIdx++;
+ const spacing = spacings?.[lineIdx];
+ if (spacing === 'lazy' && lineIdx > 0 && !LAZY_UNSAFE_LINE_RE.test(line)) return line;
if (spacing === 'none' || spacing === 0) return `>${line}`;
if (typeof spacing === 'number' && spacing >= 2 && !line.startsWith(' ')) {
return `>${' '.repeat(spacing)}${line}`;
diff --git a/packages/server/src/map-driven-splice.ts b/packages/server/src/map-driven-splice.ts
index 431ff65bb..054915ed3 100644
--- a/packages/server/src/map-driven-splice.ts
+++ b/packages/server/src/map-driven-splice.ts
@@ -24,12 +24,17 @@
* the settle path (it replaces incremental-diff work of similar order),
* but a future large-doc latency report should start here.
*
+ * When the changed region is a single container block on both sides
+ * (blockquote / list / listItem) whose own fields are untouched, the walk
+ * recurses into the container's children and splices at child granularity —
+ * sibling children the edit never touched keep their source bytes even for
+ * byte-forms mdast cannot represent (blank-line runs inside a list item).
+ *
* Returns null on parse failure or when any top-level block lacks a
* position offset (caller falls back to whole-body diff path). The
* non-contiguous-changes case (paragraph 1 + paragraph 3 edited,
- * paragraph 2 unchanged) collapses to one over-wide splice covering all
- * three — block-granular degradation in that narrow case, called out
- * as expected pending sub-block work.
+ * paragraph 2 unchanged) still collapses to one over-wide splice covering
+ * all three — block-granular degradation in that narrow case.
*/
import type { MarkdownManager } from '@inkeep/open-knowledge-core';
import type { JSONContent } from '@tiptap/core';
@@ -69,6 +74,70 @@ export function computeMapDrivenBodySplice(
return null;
}
+ try {
+ return computeChildrenSplice(
+ oldChildren,
+ newChildren,
+ {
+ start: oldChildren.length > 0 ? blockStartOffset(oldChildren[0]) : 0,
+ end: oldBody.length,
+ },
+ {
+ start: newChildren.length > 0 ? blockStartOffset(newChildren[0]) : 0,
+ end: newBody.length,
+ },
+ newBody,
+ );
+ } catch (err) {
+ // The position guards above (and inside tryNarrowIntoContainer) make
+ // offset throws unreachable under current parser behavior, but the
+ // caller's contract is MapDrivenSplice | null — an exception here would
+ // bypass the fallback telemetry and surface inside the observer drain.
+ onFallback?.('missing-position', err);
+ return null;
+ }
+}
+
+interface ByteRegion {
+ readonly start: number;
+ readonly end: number;
+}
+
+/**
+ * Node types whose byte-form contains independently-positioned children the
+ * splice can narrow into. Leaf blocks (paragraph, heading, code) and table
+ * internals (row/cell slices don't re-anchor as standalone source) stay at
+ * their own granularity.
+ *
+ * Admission criteria for a new type: (1) every child carries its own mdast
+ * position offsets into the same source string; (2) the container's own
+ * byte contribution (markers, indentation) is confined to line prefixes so
+ * splicing between child boundaries yields well-formed source; (3) a
+ * child-range slice of the canonical serialization stays valid when
+ * spliced between preserved old-byte siblings. Types that serialize their
+ * children through a bespoke walker (table, commentBlock, JSX containers)
+ * fail (2)/(3) and must stay out.
+ */
+const NARROWABLE_CONTAINER_TYPES = new Set(['blockquote', 'list', 'listItem']);
+
+/**
+ * Prefix/suffix walk over one sibling level, emitting the splice for the
+ * changed middle region. When the changed region is exactly one node on each
+ * side, both of the same narrowable container type, and the containers
+ * differ ONLY in their children (own fields incl. `data` equal — a
+ * container-level source-form edit like a bullet-marker or marker-spacing
+ * change must rewrite the whole container, never be silently dropped),
+ * recurse into the children so untouched siblings INSIDE the container keep
+ * their source bytes (blank-line runs and other byte-forms mdast cannot
+ * represent).
+ */
+function computeChildrenSplice(
+ oldChildren: readonly RootContent[],
+ newChildren: readonly RootContent[],
+ oldRegion: ByteRegion,
+ newRegion: ByteRegion,
+ newBody: string,
+): MapDrivenSplice {
let prefixLen = 0;
while (
prefixLen < oldChildren.length &&
@@ -90,23 +159,24 @@ export function computeMapDrivenBodySplice(
suffixLen++;
}
- const spliceStart =
- prefixLen > 0
- ? blockEndOffset(oldChildren[prefixLen - 1])
- : oldChildren.length > 0
- ? blockStartOffset(oldChildren[0])
- : 0;
+ if (
+ oldChildren.length - prefixLen - suffixLen === 1 &&
+ newChildren.length - prefixLen - suffixLen === 1
+ ) {
+ const oldChanged = oldChildren[prefixLen];
+ const newChanged = newChildren[prefixLen];
+ const narrowed = tryNarrowIntoContainer(oldChanged, newChanged, newBody);
+ if (narrowed) return narrowed;
+ }
+
+ const spliceStart = prefixLen > 0 ? blockEndOffset(oldChildren[prefixLen - 1]) : oldRegion.start;
const spliceEnd =
- suffixLen > 0 ? blockStartOffset(oldChildren[oldChildren.length - suffixLen]) : oldBody.length;
+ suffixLen > 0 ? blockStartOffset(oldChildren[oldChildren.length - suffixLen]) : oldRegion.end;
const newSliceStart =
- prefixLen > 0
- ? blockEndOffset(newChildren[prefixLen - 1])
- : newChildren.length > 0
- ? blockStartOffset(newChildren[0])
- : 0;
+ prefixLen > 0 ? blockEndOffset(newChildren[prefixLen - 1]) : newRegion.start;
const newSliceEnd =
- suffixLen > 0 ? blockStartOffset(newChildren[newChildren.length - suffixLen]) : newBody.length;
+ suffixLen > 0 ? blockStartOffset(newChildren[newChildren.length - suffixLen]) : newRegion.end;
return {
spliceStart,
@@ -115,6 +185,34 @@ export function computeMapDrivenBodySplice(
};
}
+function tryNarrowIntoContainer(
+ oldNode: RootContent,
+ newNode: RootContent,
+ newBody: string,
+): MapDrivenSplice | null {
+ if (oldNode.type !== newNode.type || !NARROWABLE_CONTAINER_TYPES.has(oldNode.type)) return null;
+ if (!('children' in oldNode) || !('children' in newNode)) return null;
+ const oldKids = oldNode.children as readonly RootContent[];
+ const newKids = newNode.children as readonly RootContent[];
+ if (oldKids.length === 0 || newKids.length === 0) return null;
+ if (!allBlocksCarryPositions(oldKids) || !allBlocksCarryPositions(newKids)) return null;
+ // Recursing is only sound when the difference is confined to the
+ // children: compare the containers with children stripped.
+ if (
+ stringifyIgnorePosition({ ...oldNode, children: [] }) !==
+ stringifyIgnorePosition({ ...newNode, children: [] })
+ ) {
+ return null;
+ }
+ return computeChildrenSplice(
+ oldKids,
+ newKids,
+ { start: blockStartOffset(oldNode), end: blockEndOffset(oldNode) },
+ { start: blockStartOffset(newNode), end: blockEndOffset(newNode) },
+ newBody,
+ );
+}
+
function allBlocksCarryPositions(children: readonly RootContent[]): boolean {
for (const child of children) {
const start = child.position?.start?.offset;
diff --git a/packages/server/src/map-driven-splice.unchanged-detector.test.ts b/packages/server/src/map-driven-splice.unchanged-detector.test.ts
new file mode 100644
index 000000000..c359ea153
--- /dev/null
+++ b/packages/server/src/map-driven-splice.unchanged-detector.test.ts
@@ -0,0 +1,119 @@
+/**
+ * Unchanged-block detection must be writer-blind but round-trip-artifact
+ * tolerant: a block the fragment did NOT edit must stay outside the splice
+ * even when its parse captures data the serializer cannot replay (blockquote
+ * lazy continuation), and a same-container edit must not rewrite sibling
+ * children whose bytes the editor never touched.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
+import type { JSONContent } from '@tiptap/core';
+import { computeMapDrivenBodySplice } from './map-driven-splice.ts';
+
+const mdManager = new MarkdownManager({ extensions: sharedExtensions });
+
+function applySplice(
+ oldBody: string,
+ splice: { spliceStart: number; spliceEnd: number; newSlice: string },
+): string {
+ return oldBody.slice(0, splice.spliceStart) + splice.newSlice + oldBody.slice(splice.spliceEnd);
+}
+
+/** Append ` EDITWORD` to the first PM text node containing `marker`. */
+function editTextNode(pm: JSONContent, marker: string): JSONContent {
+ const clone = JSON.parse(JSON.stringify(pm)) as JSONContent;
+ let done = false;
+ const walk = (node: JSONContent): void => {
+ if (done) return;
+ if (node.type === 'text' && typeof node.text === 'string' && node.text.includes(marker)) {
+ node.text = `${node.text} EDITWORD`;
+ done = true;
+ return;
+ }
+ for (const child of node.content ?? []) walk(child);
+ };
+ walk(clone);
+ if (!done) throw new Error(`marker not found in PM doc: ${marker}`);
+ return clone;
+}
+
+describe('computeMapDrivenBodySplice unchanged-block detection', () => {
+ test('lazy-continuation blockquote untouched by the edit stays outside the splice', () => {
+ const oldBody = '> lazy first line\nlazy continuation stays\n\nSeparate paragraph.\n';
+ const pm = editTextNode(mdManager.parse(oldBody), 'Separate paragraph.');
+
+ const splice = computeMapDrivenBodySplice(oldBody, pm, mdManager);
+ expect(splice).not.toBeNull();
+ if (!splice) return;
+
+ const result = applySplice(oldBody, splice);
+ expect(result).toBe(
+ '> lazy first line\nlazy continuation stays\n\nSeparate paragraph. EDITWORD\n',
+ );
+ });
+
+ test('same-list edit preserves a sibling item containing a multi-blank run', () => {
+ const oldBody = '- item one\n\n para in item\n\n\n wide gap para\n- item two editable\n';
+ const pm = editTextNode(mdManager.parse(oldBody), 'item two editable');
+
+ const splice = computeMapDrivenBodySplice(oldBody, pm, mdManager);
+ expect(splice).not.toBeNull();
+ if (!splice) return;
+
+ const result = applySplice(oldBody, splice);
+ expect(result).toBe(
+ '- item one\n\n para in item\n\n\n wide gap para\n- item two editable EDITWORD\n',
+ );
+ });
+
+ test('same-blockquote edit preserves a sibling lazy-continuation paragraph', () => {
+ const oldBody = '> lazy first line\nlazy continuation stays\n>\n> editable second para\n';
+ const pm = editTextNode(mdManager.parse(oldBody), 'editable second para');
+
+ const splice = computeMapDrivenBodySplice(oldBody, pm, mdManager);
+ expect(splice).not.toBeNull();
+ if (!splice) return;
+
+ const result = applySplice(oldBody, splice);
+ expect(result).toBe(
+ '> lazy first line\nlazy continuation stays\n>\n> editable second para EDITWORD\n',
+ );
+ });
+
+ test('container-data-only change blocks narrowing: bullet-marker flip rewrites the whole list', () => {
+ // Drives tryNarrowIntoContainer into its own-fields-inequality bail: the
+ // list's data.bulletMarker differs while every item's text is identical,
+ // so the splice must cover the entire container — narrowing here would
+ // silently drop the marker change.
+ const oldBody = '* item one\n* item two\n';
+ const pm = JSON.parse(JSON.stringify(mdManager.parse(oldBody))) as JSONContent;
+ const list = pm.content?.find((n) => n.type === 'list');
+ if (!list?.attrs) throw new Error('list node with attrs not found');
+ expect(list.attrs.bulletMarker).toBe('*');
+ list.attrs.bulletMarker = '-';
+
+ const splice = computeMapDrivenBodySplice(oldBody, pm, mdManager);
+ expect(splice).not.toBeNull();
+ if (!splice) return;
+
+ const result = applySplice(oldBody, splice);
+ expect(result).toBe('- item one\n- item two\n');
+ });
+
+ test('a REAL source-form change on an otherwise text-identical block is still detected (dash-count tripwire twin)', () => {
+ // The fragment carries a delimiter-row padding change (source-form data
+ // difference with identical cell text). The splice must treat the table
+ // as changed — skipping it would silently drop the concurrent edit.
+ const oldBody = '| A | B |\n| - | - |\n| x | y |\n';
+ const newBody = '| A | B |\n| --- | --- |\n| x | y |\n';
+ const pm = mdManager.parse(newBody);
+
+ const splice = computeMapDrivenBodySplice(oldBody, pm, mdManager);
+ expect(splice).not.toBeNull();
+ if (!splice) return;
+
+ const result = applySplice(oldBody, splice);
+ expect(result).toBe(newBody);
+ });
+});
diff --git a/packages/server/src/server-observers.lazy-continuation.test.ts b/packages/server/src/server-observers.lazy-continuation.test.ts
index f11563ed1..09f6b2b4e 100644
--- a/packages/server/src/server-observers.lazy-continuation.test.ts
+++ b/packages/server/src/server-observers.lazy-continuation.test.ts
@@ -118,10 +118,14 @@ const FIXTURES = [
preservedSlice: '- Third bullet\n**Trigger to revisit:**',
},
{
- label: 'blockquote lazy continuation',
- raw: '---\ntitle: Blockquote continuation fixture\n---\n\n# Hello\n\n> Lazy quote\nstays lazy.\n\nBody text stays.\n',
- // Canonical form gains the `> ` prefix on the continuation line.
- preservedSlice: '> Lazy quote\nstays lazy.',
+ label: 'nested blockquote lazy continuation',
+ // A TOP-LEVEL blockquote lazy continuation is byte-faithful now (the
+ // `'lazy'` marker-spacing capture replays it), so it left this family;
+ // the NESTED form still canonicalizes because the spacings capture
+ // bails after the first line on nested blockquotes.
+ raw: '---\ntitle: Blockquote continuation fixture\n---\n\n# Hello\n\n> > Nested quote\nlazy tail.\n\nBody text stays.\n',
+ // Canonical form gains the `> > ` prefix on the continuation line.
+ preservedSlice: '> > Nested quote\nlazy tail.',
},
] as const;
diff --git a/packages/server/src/server-observers.test.ts b/packages/server/src/server-observers.test.ts
index 5ef288eaa..3d6d1c014 100644
--- a/packages/server/src/server-observers.test.ts
+++ b/packages/server/src/server-observers.test.ts
@@ -1105,11 +1105,11 @@ describe('Server Observer B — error recovery paths', () => {
__resetBridgeWatchdogForTests();
resetMetrics();
- // Beyond-tolerance fixture (a lazy blockquote continuation canonicalizes
- // to a `> `-prefixed line), so a coherent witness pair WOULD qualify for
- // the in-sync residual merge.
+ // Beyond-tolerance fixture (a literal NBSP canonicalizes to a plain
+ // space), so a coherent witness pair WOULD qualify for the in-sync
+ // residual merge.
const ngRaw =
- '---\ntitle: NG recovery fixture\n---\n\n# Hello\n\n> Lazy quote\nstays lazy.\n\nBody text stays.\n';
+ '---\ntitle: NG recovery fixture\n---\n\n# Hello\n\nKeeps a\u00A0nbsp byte.\n\nBody text stays.\n';
const doc = new Y.Doc();
const xmlFragment = doc.getXmlFragment('default');
const ytext = doc.getText('source');
@@ -1638,13 +1638,15 @@ describe('Observer A routing — Path B fires iff Y.Text holds unabsorbed change
cleanup();
});
- // Beyond-tolerance fixture: the lazy blockquote continuation
- // canonicalizes to a `> `-prefixed continuation line — a byte divergence
- // normalizeBridge does NOT tolerate. Storage never sanitizes: these bytes must survive in-sync
- // fragment settlements. (The original inline-math `$a + b$` fixture was
- // retired when the engine grew byte-faithful for single-`$` math.)
+ // Beyond-tolerance fixture: a literal NBSP (U+00A0) canonicalizes to a
+ // plain space (the patched remark-prosemirror reserves NBSP as its
+ // whitespace sentinel) — a byte divergence normalizeBridge does NOT
+ // tolerate. Storage never sanitizes: these bytes must survive in-sync
+ // fragment settlements. (Prior fixtures — inline-math `$a + b$`, then the
+ // lazy blockquote continuation — were each retired when the engine grew
+ // byte-faithful for them.)
const NG_RAW =
- '---\ntitle: NG routing fixture\n---\n\n# Hello\n\n> Lazy quote\nstays lazy.\n\nBody text stays.\n';
+ '---\ntitle: NG routing fixture\n---\n\n# Hello\n\nKeeps a\u00A0nbsp byte.\n\nBody text stays.\n';
test('in-sync doc with beyond-tolerance residual: fragment change preserves NG bytes without a Path B fire', () => {
__resetBridgeWatchdogForTests();
@@ -1679,8 +1681,8 @@ describe('Observer A routing — Path B fires iff Y.Text holds unabsorbed change
// raw byte form (no wholesale canonical rewrite of Y.Text).
const finalText = ytext.toString();
expect(finalText).toContain('User WYSIWYG edit.');
- expect(finalText).toContain('> Lazy quote\nstays lazy.');
- expect(finalText).not.toContain('> stays lazy.');
+ expect(finalText).toContain('Keeps a\u00A0nbsp byte.');
+ expect(finalText).not.toContain('Keeps a nbsp byte.');
expect(finalText).toContain('Body text stays.');
cleanup();
@@ -1745,8 +1747,8 @@ describe('Observer A routing — Path B fires iff Y.Text holds unabsorbed change
});
expect(events1).toHaveLength(0);
expect(getMetrics().observerAResidualMergeRuns).toBe(1);
- expect(ytext.toString()).toContain('> Lazy quote\nstays lazy.');
- expect(ytext.toString()).not.toContain('> stays lazy.');
+ expect(ytext.toString()).toContain('Keeps a\u00A0nbsp byte.');
+ expect(ytext.toString()).not.toContain('Keeps a nbsp byte.');
// Second in-sync fragment edit: the post-merge settlement kept the
// witnesses coherent and beyond tolerance, so this must dispatch the
@@ -1761,8 +1763,8 @@ describe('Observer A routing — Path B fires iff Y.Text holds unabsorbed change
const finalText = ytext.toString();
expect(finalText).toContain('First edit.');
expect(finalText).toContain('Second edit.');
- expect(finalText).toContain('> Lazy quote\nstays lazy.');
- expect(finalText).not.toContain('> stays lazy.');
+ expect(finalText).toContain('Keeps a\u00A0nbsp byte.');
+ expect(finalText).not.toContain('Keeps a nbsp byte.');
cleanup();
});
@@ -1788,7 +1790,7 @@ describe('Observer A routing — Path B fires iff Y.Text holds unabsorbed change
// a no-op updateYFragment would never trigger the short-circuit. Mirrors
// an agent write under a paired origin.
const pairedRaw =
- '---\ntitle: NG routing fixture\n---\n\n# Hello\n\n> Lazy quote\nstays lazy.\n\nPaired body.\n';
+ '---\ntitle: NG routing fixture\n---\n\n# Hello\n\nKeeps a\u00A0nbsp byte.\n\nPaired body.\n';
// Fixture precondition: pairedRaw is also beyond-tolerance, so the
// coherence flag (not residualInTolerance) is what gates the residual
// merge after the paired write.