Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
PRD-9707 Keep merged Markdown byte-faithful and check the output (#7060)
mike-inkeepb915eb4f03ba
diff --git a/.changeset/merge-output-byte-faithful.md b/.changeset/merge-output-byte-faithful.mdnew file mode 100644index 000000000..2e74c1d42--- /dev/null+++ b/.changeset/merge-output-byte-faithful.md@@ -0,0 +1,5 @@+---+'@inkeep/open-knowledge': patch+---++When an outside edit is merged with your own, text neither of you touched keeps its exact bytes (line endings, blank lines, indentation), a file whose line endings were converted keeps one convention, and a merge that cannot be verified is no longer applied.diff --git a/docs/content/reference/mcp.mdx b/docs/content/reference/mcp.mdxindex 42d1b6b3d..1c6941c80 100644--- a/docs/content/reference/mcp.mdx+++ b/docs/content/reference/mcp.mdx@@ -120,7 +120,7 @@ A write that first detects the stale file during its final disk flush returns a`conflict.kind` says where the conflict came from, and the kind decides what a resolve does. `merge-native` is a git merge that left unmerged stages in the index. Resolving one stages your choice with git and commits the merge once no `merge-native` conflict is left. `working-tree` is a pull-only overlay collision, pinned to the origin blob it hit. `reconcile` is OpenKnowledge's own three-way merge of the editor against disk, including protected stale saves; no git object holds those stages, so OpenKnowledge snapshots them when it detects the conflict. Resolving a `working-tree` or `reconcile` conflict commits nothing.-A `reconcile` conflict also carries a `reason`: `merged-with-markers`, `refused-conflict-markers`, `refused-no-base`, `refused-too-large`, `disk-markers`, or `stale-external-write`. On that last reason the compatibility field `conflictKind` is `stale-external-write`. Every other row uses `conflictKind: "git"`.+A `reconcile` conflict also carries a `reason`: `merged-with-markers`, `refused-conflict-markers`, `refused-no-base`, `refused-too-large`, `disk-markers`, or `stale-external-write`. On that last reason the compatibility field `conflictKind` is `stale-external-write`. Every other row uses `conflictKind: "git"`. `refused-too-large` means the document is past the merge size cap or, rarely, that the merged output failed OpenKnowledge's fidelity check; the server log names the failed check.A write to a document whose file was deleted, renamed or moved on disk returns `urn:ok:error:doc-removed` (409): the edit reached collaborative state, but the disk write was refused. Retrying the same write does not help, and the `detail` says which case applies. If the file was deleted, re-create the document with `write` if you still want the content. If it was renamed or moved outside the app, the `detail` names its new path: re-read the document there and re-apply your change. If a different file now sits at the old path and has not been loaded into the document, only a reload of that document from disk clears the refusal, so tell the user instead of retrying.diff --git a/packages/server/assets/skills/project/references/conflict-resolution.md b/packages/server/assets/skills/project/references/conflict-resolution.mdindex ef774abe2..786ed3bff 100644--- a/packages/server/assets/skills/project/references/conflict-resolution.md+++ b/packages/server/assets/skills/project/references/conflict-resolution.md@@ -28,7 +28,7 @@ A write that first detects a stale file during its final disk flush returns `urn| `working-tree` | a pull-only overlay collision, pinned to the origin blob it collided with | writes the chosen bytes to disk; nothing is committed || `reconcile` | OK's own three-way merge of the editor against disk, including stale external saves. No git object holds the stages, so OK snapshots them at detection | writes the chosen bytes to disk and into the loaded document; no git command runs |-A `reconcile` conflict carries a `reason`: `merged-with-markers` (the three-way merge produced markers), `refused-conflict-markers` (the disk bytes already carried markers), `refused-too-large` (the document is past the merge size cap), `disk-markers` (markers landed on disk under an open doc), or `stale-external-write` (an older displaced version returned on disk). The compatibility `conflictKind` field is `stale-external-write` for that last reason and `git` otherwise.+A `reconcile` conflict carries a `reason`: `merged-with-markers` (the three-way merge produced markers), `refused-conflict-markers` (the disk bytes already carried markers), `refused-too-large` (the document is past the merge size cap or, rarely, the merged output failed OpenKnowledge's fidelity check; the server log names the failed check), `disk-markers` (markers landed on disk under an open doc), or `stale-external-write` (an older displaced version returned on disk). The compatibility `conflictKind` field is `stale-external-write` for that last reason and `git` otherwise.## What the user choosesdiff --git a/packages/server/src/external-change.test.ts b/packages/server/src/external-change.test.tsindex 6b7b35a27..b38f7cba2 100644--- a/packages/server/src/external-change.test.ts+++ b/packages/server/src/external-change.test.ts@@ -10,8 +10,14 @@ import {import { beforeEach, describe, expect, test, vi } from 'vitest';import type * as Y from 'yjs';import { DocumentDurabilityState } from './document-durability-state.ts';-import { applyExternalChange, createExternalChangeHandler } from './external-change.ts';+import {+ applyExternalChange,+ createExternalChangeHandler,+ reportReconcileCheckFailed,+} from './external-change.ts';import { getLogger } from './logger.ts';+import { getMetrics, resetMetrics } from './metrics.ts';+import { reconcile } from './reconciliation.ts';type Conn = Awaited<ReturnType<Hocuspocus['openDirectConnection']>>;@@ -441,3 +447,38 @@ describe('createExternalChangeHandler — error-swallowing factory', () => {}});});++describe('reportReconcileCheckFailed', () => {+ test('counts and logs a check-failed refusal with its failure detail, and ignores every other outcome', () => {+ resetMetrics();+ const warnSpy = vi.spyOn(getLogger('reconcile'), 'warn');+ try {+ const refused = reconcile({+ docName: 'check-failed-doc',+ base: 'A\n\nB\n\nC\n',+ ours: 'A\n\nX\n\nB edited\n\nC\n',+ theirs: 'A\n\nX\n\nB\n\nC\n',+ });+ const merged = reconcile({+ docName: 'merged-doc',+ base: 'A\n\nB\n',+ ours: 'A ours\n\nB\n',+ theirs: 'A\n\nB theirs\n',+ });++ reportReconcileCheckFailed('merged-doc', merged, 'watcher');+ reportReconcileCheckFailed('check-failed-doc', refused, 'branch-switch');++ expect(getMetrics().reconcileCheckFailed).toBe(1);+ expect(warnSpy).toHaveBeenCalledTimes(1);+ expect(warnSpy.mock.calls[0]?.[0]).toEqual({+ docName: 'check-failed-doc',+ site: 'branch-switch',+ failure: { check: 'single-occurrence', outputBlockIndex: 2 },+ });+ } finally {+ warnSpy.mockRestore();+ resetMetrics();+ }+ });+});diff --git a/packages/server/src/external-change.ts b/packages/server/src/external-change.tsindex 4a1021e4b..9d7b783de 100644--- a/packages/server/src/external-change.ts+++ b/packages/server/src/external-change.ts@@ -37,11 +37,12 @@ import { canonicalContentPathIsRefused } from './fs-safety.ts';import { getLogger } from './logger.ts';import {incrementExternalChangeHandlerErrors,+ incrementReconcileCheckFailed,incrementReconcileInFlightFallthroughs,incrementReconcileInsertDedupSkipped,incrementReconcileOwnFlushSkips,} from './metrics.ts';-import { type ReconcileRefusalReason, reconcile } from './reconciliation.ts';+import { type ReconcileOutcome, type ReconcileRefusalReason, reconcile } from './reconciliation.ts';import { FILE_SYSTEM_WRITER } from './shadow-repo.ts';export { FILE_WATCHER_ORIGIN } from './disk-content-intake.ts';@@ -241,6 +242,21 @@ function clearStaleExternalWriteConflict(lifecycleMap.delete('detectedAt');}+type ReconcileCheckSite = 'watcher' | 'agent-write' | 'branch-switch';++export function reportReconcileCheckFailed(+ docName: string,+ outcome: ReconcileOutcome,+ site: ReconcileCheckSite,+): void {+ if (outcome.kind !== 'refused' || outcome.reason !== 'check-failed') return;+ incrementReconcileCheckFailed();+ getLogger('reconcile').warn(+ { docName, site, failure: outcome.failure },+ `[reconcile] ${site} ${docName} merge output failed the fidelity check (${outcome.failure.check})`,+ );+}+export function wireReasonForRefusal(reason: ReconcileRefusalReason): ReconcileReason {switch (reason) {case 'conflict-markers':@@ -248,6 +264,7 @@ export function wireReasonForRefusal(reason: ReconcileRefusalReason): ReconcileRcase 'no-base':return 'refused-no-base';case 'too-large':+ case 'check-failed':return 'refused-too-large';default: {const unhandled: never = reason;@@ -438,6 +455,7 @@ export function reconcileDiskBeforeAgentWrite(case 'conflicts':case 'refused': {+ reportReconcileCheckFailed(docName, outcome, 'agent-write');conflicts.raise({kind: 'reconcile',file: conflicts.fileOf(docName),diff --git a/packages/server/src/metrics.ts b/packages/server/src/metrics.tsindex 014a5c903..04a92aa88 100644--- a/packages/server/src/metrics.ts+++ b/packages/server/src/metrics.ts@@ -23,6 +23,7 @@ export interface ReconciliationMetrics {reconcileCount: number;conflictCount: number;reconcileInsertDedupSkipped: number;+ reconcileCheckFailed: number;staleExternalWriteRefused: number;concurrentOverwriteRefused: number;batchCount: number;@@ -143,6 +144,7 @@ const counters: ReconciliationMetrics = {reconcileCount: 0,conflictCount: 0,reconcileInsertDedupSkipped: 0,+ reconcileCheckFailed: 0,staleExternalWriteRefused: 0,concurrentOverwriteRefused: 0,batchCount: 0,@@ -259,6 +261,10 @@ export function incrementReconcileInsertDedupSkipped(): void {counters.reconcileInsertDedupSkipped++;}+export function incrementReconcileCheckFailed(): void {+ counters.reconcileCheckFailed++;+}+export function incrementStaleExternalWriteRefused(): void {counters.staleExternalWriteRefused++;}@@ -691,6 +697,7 @@ export function resetMetrics(): void {counters.reconcileCount = 0;counters.conflictCount = 0;counters.reconcileInsertDedupSkipped = 0;+ counters.reconcileCheckFailed = 0;counters.staleExternalWriteRefused = 0;counters.concurrentOverwriteRefused = 0;counters.batchCount = 0;diff --git a/packages/server/src/reconciliation.fidelity.test.ts b/packages/server/src/reconciliation.fidelity.test.tsnew file mode 100644index 000000000..405b0bb57--- /dev/null+++ b/packages/server/src/reconciliation.fidelity.test.ts@@ -0,0 +1,488 @@+import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';+import { describe, expect, test } from 'vitest';+import {+ checkMergeOutput,+ MAX_CONSTRUCT_CHECK_BYTES,+ MAX_LCS_CELLS,+ reconcile,+} from './reconciliation';++const docName = 'fidelity-doc';+const cleanMerge = { conflicts: [], dedupSkipped: false } as const;+const parser = new MarkdownManager({ extensions: sharedExtensions });+const rootTypes = (md: string): string[] => parser.parseToMdast(md).children.map((c) => c.type);++interface SpikeCase {+ readonly name: string;+ readonly base: string;+ readonly ours: string;+ readonly theirs: string;+ readonly expected:+ | { readonly kind: 'noop' }+ | { readonly kind: 'clean' | 'merged'; readonly newContent: string };+}++const SPIKE_CASES: readonly SpikeCase[] = [+ {+ name: 'CONTROL-1 all-LF canonical document, both sides edit different blocks',+ base: '# Title\n\nPara one.\n\nPara two.\n\nPara three.\n',+ ours: '# Title\n\nPara one EDITED by ours.\n\nPara two.\n\nPara three.\n',+ theirs: '# Title\n\nPara one.\n\nPara two.\n\nPara three EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\n\nPara one EDITED by ours.\n\nPara two.\n\nPara three EDITED by theirs.\n',+ },+ },+ {+ name: 'CONTROL-2 ours, theirs and base identical',+ base: '# Title\n\nPara one.\n\nPara two.\n\nPara three.\n',+ ours: '# Title\n\nPara one.\n\nPara two.\n\nPara three.\n',+ theirs: '# Title\n\nPara one.\n\nPara two.\n\nPara three.\n',+ expected: { kind: 'noop' },+ },+ {+ name: 'CONTROL-3 ours equals base, so theirs lands verbatim',+ base: '# Title\r\n\r\n code line 1\r\n code line 2\r\n\r\nAfter.\r\n',+ ours: '# Title\r\n\r\n code line 1\r\n code line 2\r\n\r\nAfter.\r\n',+ theirs: '# Title\r\n\r\n code line 1\r\n code line 2\r\n\r\nAfter EDITED by theirs.\r\n',+ expected: {+ kind: 'clean',+ newContent:+ '# Title\r\n\r\n code line 1\r\n code line 2\r\n\r\nAfter EDITED by theirs.\r\n',+ },+ },+ {+ name: 'Q1.a CRLF base and ours with one-line blocks, LF theirs edits another block',+ base: '# Title\r\n\r\nPara one.\r\n\r\nPara two.\r\n',+ ours: '# Title\r\n\r\nPara one EDITED by ours.\r\n\r\nPara two.\r\n',+ theirs: '# Title\n\nPara one.\n\nPara two EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent: '# Title\n\nPara one EDITED by ours.\n\nPara two EDITED by theirs.\n',+ },+ },+ {+ name: 'Q1.b CRLF ours edit makes a two-line block, LF theirs edits another block',+ base: '# Title\r\n\r\nPara one.\r\n\r\nPara two.\r\n',+ ours: '# Title\r\n\r\nPara one EDITED by ours.\r\nSecond line by ours.\r\n\r\nPara two.\r\n',+ theirs: '# Title\n\nPara one.\n\nPara two EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\n\nPara one EDITED by ours.\nSecond line by ours.\n\nPara two EDITED by theirs.\n',+ },+ },+ {+ name: 'Q1.c CRLF untouched two-line block, LF theirs edits another block',+ base: '# Title\r\n\r\nMulti one.\r\nMulti two.\r\n\r\nPara A.\r\n\r\nPara B.\r\n',+ ours: '# Title\r\n\r\nMulti one.\r\nMulti two.\r\n\r\nPara A EDITED by ours.\r\n\r\nPara B.\r\n',+ theirs: '# Title\n\nMulti one.\nMulti two.\n\nPara A.\n\nPara B EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\n\nMulti one.\nMulti two.\n\nPara A EDITED by ours.\n\nPara B EDITED by theirs.\n',+ },+ },+ {+ name: 'Q1.d CRLF ours edits a two-line block whose LF theirs copy differs only in endings',+ base: '# Title\r\n\r\nMulti one.\r\nMulti two.\r\n\r\nPara B.\r\n',+ ours: '# Title\r\n\r\nMulti one EDITED by ours.\r\nMulti two.\r\n\r\nPara B.\r\n',+ theirs: '# Title\n\nMulti one.\nMulti two.\n\nPara B EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent: '# Title\n\nMulti one EDITED by ours.\nMulti two.\n\nPara B EDITED by theirs.\n',+ },+ },+ {+ name: 'Q1.e LF base and ours, CRLF theirs edits a two-line block',+ base: '# Title\n\nPara A.\n\nPara B.\nPara B continued.\n',+ ours: '# Title\n\nPara A EDITED by ours.\n\nPara B.\nPara B continued.\n',+ theirs: '# Title\r\n\r\nPara A.\r\n\r\nPara B EDITED by theirs.\r\nPara B continued.\r\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\r\n\r\nPara A EDITED by ours.\r\n\r\nPara B EDITED by theirs.\r\nPara B continued.\r\n',+ },+ },+ {+ name: 'Q1.f all-CRLF triple with one-line blocks',+ base: '# Title\r\n\r\nPara one.\r\n\r\nPara two.\r\n',+ ours: '# Title\r\n\r\nPara one EDITED by ours.\r\n\r\nPara two.\r\n',+ theirs: '# Title\r\n\r\nPara one.\r\n\r\nPara two EDITED by theirs.\r\n',+ expected: {+ kind: 'merged',+ newContent: '# Title\r\n\r\nPara one EDITED by ours.\r\n\r\nPara two EDITED by theirs.\r\n',+ },+ },+ {+ name: 'Q1.g all-CRLF triple with two-line blocks',+ base: '# Title\r\n\r\nA1.\r\nA2.\r\n\r\nB1.\r\nB2.\r\n\r\nC1.\r\nC2.\r\n',+ ours: '# Title\r\n\r\nA1 EDITED by ours.\r\nA2.\r\n\r\nB1.\r\nB2.\r\n\r\nC1.\r\nC2.\r\n',+ theirs: '# Title\r\n\r\nA1.\r\nA2.\r\n\r\nB1.\r\nB2.\r\n\r\nC1 EDITED by theirs.\r\nC2.\r\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\r\n\r\nA1 EDITED by ours.\r\nA2.\r\n\r\nB1.\r\nB2.\r\n\r\nC1 EDITED by theirs.\r\nC2.\r\n',+ },+ },+ {+ name: 'Q2.a four-space indented code block at a block boundary',+ base: '# Title\n\nIntro.\n\n code line 1\n code line 2\n\nAfter.\n',+ ours: '# Title\n\nIntro EDITED by ours.\n\n code line 1\n code line 2\n\nAfter.\n',+ theirs: '# Title\n\nIntro.\n\n code line 1\n code line 2\n\nAfter EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ '# Title\n\nIntro EDITED by ours.\n\n code line 1\n code line 2\n\nAfter EDITED by theirs.\n',+ },+ },+ {+ name: 'Q2.b four-space indented list continuation paragraph',+ base: 'Intro.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter.\n',+ ours: 'Intro EDITED by ours.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter.\n',+ theirs:+ 'Intro.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ 'Intro EDITED by ours.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter EDITED by theirs.\n',+ },+ },+ {+ name: 'Q2.c two-space indented list continuation paragraph',+ base: 'Intro.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter.\n',+ ours: 'Intro EDITED by ours.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter.\n',+ theirs:+ 'Intro.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter EDITED by theirs.\n',+ expected: {+ kind: 'merged',+ newContent:+ 'Intro EDITED by ours.\n\n- item one\n\n continuation of item one\n\n- item two\n\nAfter EDITED by theirs.\n',+ },+ },+];++describe('the thirteen CRLF and indentation spike cases merge byte-exact', () => {+ test.each(SPIKE_CASES)('$name', ({ base, ours, theirs, expected }) => {+ const outcome = reconcile({ docName, base, ours, theirs });+ expect(outcome).toMatchObject(expected);+ });++ test.each(SPIKE_CASES.filter((c) => c.name.startsWith('Q2')))(+ 'the core parser reads the same root constructs in base and output: $name',+ ({ base, ours, theirs }) => {+ const outcome = reconcile({ docName, base, ours, theirs });+ if (outcome.kind !== 'merged') throw new Error(`expected merged, got ${outcome.kind}`);+ expect(rootTypes(outcome.newContent)).toEqual(rootTypes(base));+ },+ );+});++describe('assembly keeps every separator, leading and trailing byte', () => {+ test('two blank lines stay two and a missing final newline stays missing', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\n\nB\n\nC',+ ours: 'A ours\n\n\nB\n\nC',+ theirs: 'A\n\n\nB\n\nC theirs',+ });+ expect(outcome).toEqual({+ kind: 'merged',+ newContent: 'A ours\n\n\nB\n\nC theirs',+ mergedBlocks: 3,+ });+ });++ test('a separator one side changed before an untouched block keeps that change', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nB\n\nC\n',+ ours: 'A\n\n\n\nB\n\nC\n',+ theirs: 'A\n\nB\n\nC theirs\n',+ });+ expect(outcome).toMatchObject({ kind: 'merged', newContent: 'A\n\n\n\nB\n\nC theirs\n' });+ });++ test("a side's modified block and its inserted block carry that side's separators", () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nB\n\nC\n',+ ours: 'A ours\n\nB\n\nC\n',+ theirs: 'A\n\n\n\nB theirs\n\n\nX theirs\n\nC\n',+ });+ expect(outcome).toMatchObject({+ kind: 'merged',+ newContent: 'A ours\n\n\n\nB theirs\n\n\nX theirs\n\nC\n',+ });+ });++ test('deleting the first block leaves no leading blank lines', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nB\n',+ ours: 'B\n',+ theirs: 'A\n\nB\n\nC\n',+ });+ expect(outcome).toMatchObject({ kind: 'merged', newContent: 'B\n\nC\n' });+ });++ test('trailing bytes come from theirs when theirs changed them, otherwise from ours', () => {+ expect(+ reconcile({ docName, base: 'A\n\nB', ours: 'A ours\n\nB', theirs: 'A\n\nB\n' }),+ ).toMatchObject({ kind: 'merged', newContent: 'A ours\n\nB\n' });+ expect(+ reconcile({ docName, base: 'A\n\nB\n', ours: 'A ours\n\nB', theirs: 'A\n\nB theirs\n' }),+ ).toMatchObject({ kind: 'merged', newContent: 'A ours\n\nB theirs' });+ });+});++describe('line endings', () => {+ test('when ours converted the whole file and theirs did not, the output takes ours convention', () => {+ const outcome = reconcile({+ docName,+ base: '# T\r\n\r\nA.\r\nA2.\r\n\r\nB.\r\n',+ ours: '# T\n\nA ours.\nA2.\n\nB.\n',+ theirs: '# T\r\n\r\nA.\r\nA2.\r\n\r\nB theirs.\r\n',+ });+ expect(outcome).toMatchObject({+ kind: 'merged',+ newContent: '# T\n\nA ours.\nA2.\n\nB theirs.\n',+ });+ });++ test('when both sides converted a mixed file to different conventions, theirs wins', () => {+ const outcome = reconcile({+ docName,+ base: '# T\r\n\r\nA.\n\nB.\r\n',+ ours: '# T\n\nA ours.\n\nB.\n',+ theirs: '# T\r\n\r\nA.\r\n\r\nB theirs.\r\n',+ });+ expect(outcome).toMatchObject({+ kind: 'merged',+ newContent: '# T\r\n\r\nA ours.\r\n\r\nB theirs.\r\n',+ });+ });++ test('without a whole-file conversion each block keeps its own endings', () => {+ const outcome = reconcile({+ docName,+ base: '# T\r\n\r\nA.\r\n\r\nB.\r\nB2.\r\n',+ ours: '# T\r\n\r\nA ours.\r\n\r\nB.\r\nB2.\r\n',+ theirs: '# T\r\n\r\nA.\r\n\r\nB theirs.\nB2.\r\n',+ });+ expect(outcome).toMatchObject({+ kind: 'merged',+ newContent: '# T\r\n\r\nA ours.\r\n\r\nB theirs.\nB2.\r\n',+ });+ });+});++describe('classification under the raw-byte definition of changed', () => {+ test('both sides making the identical edit land it once', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nB\n\nC\n',+ ours: 'A\n\nB edited\n\nC\n',+ theirs: 'A\n\nB edited\n\nC\n',+ });+ expect(outcome).toEqual({+ kind: 'merged',+ newContent: 'A\n\nB edited\n\nC\n',+ mergedBlocks: 3,+ });+ });++ test('a whitespace-only outside change inside a paragraph ours edited is a conflict', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nPara text here.\n\nC\n',+ ours: 'A\n\nPara text EDITED.\n\nC\n',+ theirs: 'A\n\nPara text here.\n\nC theirs\n',+ });+ expect(outcome).toEqual({+ kind: 'conflicts',+ newContent: 'A\n\nPara text EDITED.\n\nC theirs\n',+ conflicts: [+ {+ blockIndex: 1,+ base: 'Para text here.',+ ours: 'Para text EDITED.',+ theirs: 'Para text here.',+ },+ ],+ });+ });++ test('an insert the engine reads as an edit, beside the same insert from theirs, is refused instead of landing twice', () => {+ const outcome = reconcile({+ docName,+ base: 'A\n\nB\n\nC\n',+ ours: 'A\n\nX\n\nB edited\n\nC\n',+ theirs: 'A\n\nX\n\nB\n\nC\n',+ });+ expect(outcome).toEqual({+ kind: 'refused',+ reason: 'check-failed',+ failure: { check: 'single-occurrence', outputBlockIndex: 2 },+ });+ });++ test('the size cap still refuses as too-large before any merge or check runs', () => {+ const perSide = Math.ceil(Math.sqrt(MAX_LCS_CELLS)) + 10;+ const blocks = (prefix: string): string =>+ `${Array.from({ length: perSide }, (_, i) => `${prefix} ${i}.`).join('\r\n\r\n')}\r\n`;+ const outcome = reconcile({+ docName,+ base: blocks('base'),+ ours: blocks('ours'),+ theirs: blocks('theirs'),+ });+ expect(outcome).toEqual({ kind: 'refused', reason: 'too-large' });+ });+});++describe('checkMergeOutput', () => {+ const base = 'Intro.\n\n code\n\nAfter.\n';+ const ours = 'Intro.\n\n code\n\nAfter.\n\n- new item\n';+ const theirs = 'Intro.\n\n code\n\nAfter EDITED.\n';++ test('accepts the merge reconcile produces', () => {+ const outcome = reconcile({ docName, base, ours, theirs });+ expect(outcome).toMatchObject({+ kind: 'merged',+ newContent: 'Intro.\n\n code\n\nAfter EDITED.\n\n- new item\n',+ });+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base,+ ours,+ theirs,+ newContent: 'Intro.\n\n code\n\nAfter EDITED.\n\n- new item\n',+ }),+ ).toBeNull();+ });++ test('refuses a duplicated block', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base,+ ours,+ theirs,+ newContent: 'Intro.\n\n code\n\nAfter EDITED.\n\n- new item\n\n- new item\n',+ }),+ ).toEqual({ check: 'single-occurrence', outputBlockIndex: 4 });+ });++ test('refuses a respelled untouched block', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base,+ ours,+ theirs,+ newContent: 'Intro.\n\ncode\n\nAfter EDITED.\n\n- new item\n',+ }),+ ).toEqual({ check: 'untouched-verbatim', baseBlockIndex: 1 });+ });++ test('refuses an untouched block whose line endings were respelled without a whole-file conversion', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base: 'A.\r\nA2.\r\n\r\nB.\r\n',+ ours: 'A.\r\nA2.\r\n\r\nB ours.\r\n',+ theirs: 'A.\r\nA2.\r\n\r\nB ours.\r\n\r\nC.\r\n',+ newContent: 'A.\nA2.\r\n\r\nB ours.\r\n\r\nC.\r\n',+ }),+ ).toEqual({ check: 'untouched-verbatim', baseBlockIndex: 0 });+ });++ test('refuses a side block that lost bytes', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base: 'A.\n\nB.\n',+ ours: 'A.\n\nLine one \nLine two\n',+ theirs: 'A theirs.\n\nB.\n',+ newContent: 'A theirs.\n\nLine one\nLine two\n',+ }),+ ).toEqual({ check: 'side-bytes', side: 'ours', sideBlockIndex: 1 });+ });++ test('refuses a side block that is missing', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base,+ ours,+ theirs,+ newContent: 'Intro.\n\n code\n\nAfter EDITED.\n',+ }),+ ).toEqual({ check: 'side-bytes', side: 'ours', sideBlockIndex: 3 });+ });++ test('refuses an output whose blocks are all faithful but one now parses as a different construct', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base,+ ours,+ theirs,+ newContent: 'Intro.\n\n- new item\n\n code\n\nAfter EDITED.\n',+ }),+ ).toEqual({ check: 'construct', outputBlockIndex: 2 });+ });+});++describe('documents the strict parse rejects', () => {+ test('an unparseable block nobody touched does not stop an ordinary merge', () => {+ const outcome = reconcile({+ docName,+ base: '# T\n\nA.\n\n<Foo>broken</Bar>\n\nB.\n',+ ours: '# T\n\nA ours.\n\n<Foo>broken</Bar>\n\nB.\n',+ theirs: '# T\n\nA.\n\n<Foo>broken</Bar>\n\nB theirs.\n',+ });+ expect(outcome).toEqual({+ kind: 'merged',+ newContent: '# T\n\nA ours.\n\n<Foo>broken</Bar>\n\nB theirs.\n',+ mergedBlocks: 4,+ });+ });++ test('an output that fails to parse when every source parses is refused as a parse failure', () => {+ expect(+ checkMergeOutput({+ ...cleanMerge,+ base: 'A\n\nB\n',+ ours: 'A\n\n<Foo>\n\nB\n\n</Foo>\n',+ theirs: 'A\n\nB theirs\n',+ newContent: 'A\n\n</Foo>\n\nB theirs\n\n<Foo>\n',+ }),+ ).toEqual({ check: 'parse' });+ });+});++describe('the construct check size bound', () => {+ const filler = (bytes: number): string => 'Filler words. '.repeat(Math.ceil(bytes / 14));+ const shape = (padding: string) => ({+ ...cleanMerge,+ base: `${padding}\n\nIntro.\n\n code\n\nAfter.\n`,+ ours: `${padding}\n\nIntro.\n\n code\n\nAfter.\n\n- new item\n`,+ theirs: `${padding}\n\nIntro.\n\n code\n\nAfter EDITED.\n`,+ newContent: `${padding}\n\nIntro.\n\n- new item\n\n code\n\nAfter EDITED.\n`,+ });++ test('a construct-changing output under the bound is refused', () => {+ expect(checkMergeOutput(shape(filler(1024)))).toEqual({+ check: 'construct',+ outputBlockIndex: 3,+ });+ });++ test('the same output over the bound is not refused, since only the byte checks run', () => {+ expect(checkMergeOutput(shape(filler(MAX_CONSTRUCT_CHECK_BYTES + 1024)))).toBeNull();+ });+});diff --git a/packages/server/src/reconciliation.test.ts b/packages/server/src/reconciliation.test.tsindex 38cc453cc..49a4d667e 100644--- a/packages/server/src/reconciliation.test.ts+++ b/packages/server/src/reconciliation.test.ts@@ -5,6 +5,7 @@ import {MAX_LCS_CELLS,reconcile,splitMarkdownBlocks,+ splitMarkdownDocument,} from './reconciliation';describe('splitMarkdownBlocks', () => {@@ -32,6 +33,37 @@ describe('splitMarkdownBlocks', () => {expect(splitMarkdownBlocks('A\n\n \n\nB')).toEqual(['A', 'B']);expect(splitMarkdownBlocks('\n\n\na')).toEqual(['a']);});++ test('keeps first-line indentation, trailing spaces and interior CRLF endings', () => {+ expect(+ splitMarkdownBlocks('Intro.\r\n\r\n code 1\r\n code 2 \r\n\r\nAfter.\r\n'),+ ).toEqual(['Intro.', ' code 1\r\n code 2 ', 'After.']);+ });++ test('carries the separator before each block and the bytes after the last one', () => {+ expect(splitMarkdownDocument('\n\nA\r\n\r\n\r\nB\n \nC \n\n')).toEqual({+ blocks: [+ { separator: '\n\n', text: 'A' },+ { separator: '\r\n\r\n\r\n', text: 'B' },+ { separator: '\n \n', text: 'C ' },+ ],+ trailing: '\n\n',+ });+ });++ test.each([+ ['canonical LF', '# T\n\nA.\n\nB.\n'],+ ['CRLF with no final newline', '# T\r\n\r\nA.\r\nA2.\r\n\r\nB.'],+ ['mixed endings and leading blank lines', '\n\r\n# T\r\n\nA.\n\r\n\r\nB.\r\n'],+ ['whitespace-only lines between blocks', 'A.\n \n\t\nB.\n '],+ ['a fence with interior blank lines', '```js\nx\n\n\ny\n```\n\nAfter.\n'],+ ['an unclosed fence running to the end', 'A.\n\n~~~\ncode\n\n\n'],+ ['whitespace only', ' \n\n\t\n'],+ ['empty', ''],+ ])('joining the split back together reproduces the document byte for byte: %s', (_, md) => {+ const { blocks, trailing } = splitMarkdownDocument(md);+ expect(blocks.map((block) => block.separator + block.text).join('') + trailing).toBe(md);+ });});describe('containsConflictMarkers', () => {@@ -77,14 +109,14 @@ describe('reconcile', () => {expect(result.kind).toBe('noop');});- test('merged: an extra blank line on the disk side does not manufacture a conflict', () => {+ test('merged: an extra blank line on the disk side does not manufacture a conflict and keeps its bytes', () => {const result = reconcile({docName,base: 'A\n\nB\n',ours: 'A\n\nC\n',theirs: 'A\n\n\n\nC\n',});- expect(result).toEqual({ kind: 'merged', newContent: 'A\n\nC\n', mergedBlocks: 2 });+ expect(result).toEqual({ kind: 'merged', newContent: 'A\n\n\n\nC\n', mergedBlocks: 2 });});test('clean: ours equals base (Y.Doc unchanged)', () => {diff --git a/packages/server/src/reconciliation.ts b/packages/server/src/reconciliation.tsindex a4a67c10d..201d1f846 100644--- a/packages/server/src/reconciliation.ts+++ b/packages/server/src/reconciliation.ts@@ -1,3 +1,4 @@+import { type LineEnding, spellLineEndings } from '@inkeep/open-knowledge-core';import {isConfigDoc,isEditableTextDoc,@@ -5,6 +6,8 @@ import {isMermaidDoc,isSystemDoc,} from './cc1-broadcast.ts';+import { getLogger } from './logger.ts';+import { mdManager } from './md-manager.ts';export interface ReconcileInput {docName: string;@@ -20,17 +23,29 @@ export interface BlockConflict {theirs: string;}-export type ReconcileRefusalReason = 'conflict-markers' | 'no-base' | 'too-large';+export type ReconcileRefusalReason = 'conflict-markers' | 'no-base' | 'too-large' | 'check-failed';++type MergeSide = 'ours' | 'theirs';++type MergeCheckFailure =+ | { readonly check: 'untouched-verbatim'; readonly baseBlockIndex: number }+ | { readonly check: 'side-bytes'; readonly side: MergeSide; readonly sideBlockIndex: number }+ | { readonly check: 'single-occurrence'; readonly outputBlockIndex: number }+ | { readonly check: 'construct'; readonly outputBlockIndex: number }+ | { readonly check: 'parse' };export type ReconcileOutcome =| { kind: 'clean'; newContent: string }| { kind: 'merged'; newContent: string; mergedBlocks: number; dedupSkipped?: true }| { kind: 'conflicts'; newContent: string; conflicts: BlockConflict[]; dedupSkipped?: true }- | { kind: 'refused'; reason: ReconcileRefusalReason }+ | { kind: 'refused'; reason: Exclude<ReconcileRefusalReason, 'check-failed'> }+ | { kind: 'refused'; reason: 'check-failed'; failure: MergeCheckFailure }| { kind: 'noop' };export const MAX_LCS_CELLS = 4_000_000;+export const MAX_CONSTRUCT_CHECK_BYTES = 256 * 1024;+export const CONFLICT_MARKER_RE = /^(<{7} |={7}$|>{7} |\|{7} )/m;export function containsConflictMarkers(content: string): boolean {@@ -43,37 +58,120 @@ export function containsUnresolvedConflictBlock(content: string): boolean {return /^>{7} /m.test(content.slice(start.index + start[0].length));}-export function splitMarkdownBlocks(md: string): string[] {- const normalized = md.replace(/\n+$/, '');- if (!normalized) return [];- const lines = normalized.split('\n');- const blocks: string[] = [];- let current: string[] = [];- let fenceChar: string | null = null;+interface MarkdownBlock {+ readonly separator: string;+ readonly text: string;+}++interface MarkdownBlocks {+ readonly blocks: readonly MarkdownBlock[];+ readonly trailing: string;+}++const FENCE_OPEN_RE = /^(`{3,}|~{3,})/;+const BLANK_LINE_RE = /^[ \t]*$/;- for (const line of lines) {- const fenceMatch = line.match(/^(`{3,}|~{3,})/);+export function splitMarkdownDocument(md: string): MarkdownBlocks {+ const blocks: MarkdownBlock[] = [];+ let fenceChar: string | null = null;+ let separatorStart = 0;+ let blockStart = -1;+ let blockEnd = -1;+ let lineStart = 0;++ while (lineStart < md.length) {+ const newline = md.indexOf('\n', lineStart);+ const lineEnd = newline === -1 ? md.length : newline + 1;+ const contentEnd =+ newline === -1+ ? md.length+ : newline > lineStart && md[newline - 1] === '\r'+ ? newline - 1+ : newline;+ const content = md.slice(lineStart, contentEnd);++ const fenceMatch = FENCE_OPEN_RE.exec(content);if (fenceMatch) {const char = fenceMatch[1][0];if (!fenceChar) fenceChar = char;else if (char === fenceChar) fenceChar = null;}- const inFence = fenceChar !== null;- if (!inFence && line.trim() === '') {- if (current.length > 0) {- const block = current.join('\n').trim();- if (block) blocks.push(block);- current = [];+ const blank = BLANK_LINE_RE.test(content);++ if (!fenceMatch && fenceChar === null && blank) {+ if (blockStart !== -1) {+ blocks.push({+ separator: md.slice(separatorStart, blockStart),+ text: md.slice(blockStart, blockEnd),+ });+ separatorStart = blockEnd;+ blockStart = -1;}} else {- current.push(line);+ if (blockStart === -1) blockStart = lineStart;+ if (!blank) blockEnd = contentEnd;}+ lineStart = lineEnd;}- if (current.length > 0) {- const block = current.join('\n').trim();- if (block) blocks.push(block);++ if (blockStart !== -1) {+ blocks.push({+ separator: md.slice(separatorStart, blockStart),+ text: md.slice(blockStart, blockEnd),+ });+ separatorStart = blockEnd;}- return blocks;+ return { blocks, trailing: md.slice(separatorStart) };+}++export function splitMarkdownBlocks(md: string): string[] {+ return splitMarkdownDocument(md).blocks.map((block) => block.text);+}++interface BlockDocument extends MarkdownBlocks {+ readonly keys: readonly string[];+}++function blockIdentity(bytes: string): string {+ return spellLineEndings(bytes, '\n');+}++function readBlockDocument(md: string): BlockDocument {+ const split = splitMarkdownDocument(md);+ return { ...split, keys: split.blocks.map((block) => blockIdentity(block.text)) };+}++type LineEndingConvention = 'crlf' | 'lf' | 'mixed' | 'none';++function lineEndingConvention(text: string): LineEndingConvention {+ let crlf = 0;+ let lf = 0;+ for (let i = text.indexOf('\n'); i !== -1; i = text.indexOf('\n', i + 1)) {+ if (i > 0 && text[i - 1] === '\r') crlf++;+ else lf++;+ }+ if (crlf === 0 && lf === 0) return 'none';+ if (lf === 0) return 'crlf';+ if (crlf === 0) return 'lf';+ return 'mixed';+}++function uniformLineEnding(convention: LineEndingConvention): LineEnding | null {+ if (convention === 'crlf') return '\r\n';+ if (convention === 'lf') return '\n';+ return null;+}++function mergeOutputLineEnding(base: string, ours: string, theirs: string): LineEnding | null {+ const baseConvention = lineEndingConvention(base);+ const theirConvention = lineEndingConvention(theirs);+ if (theirConvention !== baseConvention) {+ const converted = uniformLineEnding(theirConvention);+ if (converted) return converted;+ }+ const ourConvention = lineEndingConvention(ours);+ if (ourConvention !== baseConvention) return uniformLineEnding(ourConvention);+ return null;}export function reconcile(input: ReconcileInput): ReconcileOutcome {@@ -99,196 +197,285 @@ export function reconcile(input: ReconcileInput): ReconcileOutcome {return { kind: 'clean', newContent: theirs };}- const baseBlocks = splitMarkdownBlocks(base);- if (baseBlocks.every((block) => block.trim() === '')) {+ const baseDoc = readBlockDocument(base);+ if (baseDoc.blocks.every((block) => block.text.trim() === '')) {return { kind: 'refused', reason: 'no-base' };}- const ourBlocks = splitMarkdownBlocks(ours);- const theirBlocks = splitMarkdownBlocks(theirs);+ const ourDoc = readBlockDocument(ours);+ const theirDoc = readBlockDocument(theirs);if (- (baseBlocks.length + 1) * (ourBlocks.length + 1) > MAX_LCS_CELLS ||- (baseBlocks.length + 1) * (theirBlocks.length + 1) > MAX_LCS_CELLS+ (baseDoc.blocks.length + 1) * (ourDoc.blocks.length + 1) > MAX_LCS_CELLS ||+ (baseDoc.blocks.length + 1) * (theirDoc.blocks.length + 1) > MAX_LCS_CELLS) {return { kind: 'refused', reason: 'too-large' };}- return mergeBlocks(baseBlocks, ourBlocks, theirBlocks);+ const merge = mergeBlocks(baseDoc, ourDoc, theirDoc, mergeOutputLineEnding(base, ours, theirs));++ const failure = checkMergeOutput({+ base,+ ours,+ theirs,+ newContent: merge.newContent,+ conflicts: merge.conflicts,+ dedupSkipped: merge.dedupSkipped,+ });+ if (failure) return { kind: 'refused', reason: 'check-failed', failure };++ if (merge.conflicts.length > 0) {+ return merge.dedupSkipped+ ? {+ kind: 'conflicts',+ newContent: merge.newContent,+ conflicts: merge.conflicts,+ dedupSkipped: true,+ }+ : { kind: 'conflicts', newContent: merge.newContent, conflicts: merge.conflicts };+ }++ return merge.dedupSkipped+ ? {+ kind: 'merged',+ newContent: merge.newContent,+ mergedBlocks: merge.mergedBlocks,+ dedupSkipped: true,+ }+ : { kind: 'merged', newContent: merge.newContent, mergedBlocks: merge.mergedBlocks };+}++interface EmittedBlock {+ readonly text: string;+ readonly separator: string | null;}-function mergeBlocks(- baseBlocks: string[],- ourBlocks: string[],- theirBlocks: string[],-): ReconcileOutcome {- const ourOps = computeEditOps(baseBlocks, ourBlocks);- const theirOps = computeEditOps(baseBlocks, theirBlocks);-- const merged: string[] = [];- const conflicts: BlockConflict[] = [];- let dedupSkipped = false;+interface BlockMerge {+ readonly newContent: string;+ readonly mergedBlocks: number;+ readonly conflicts: BlockConflict[];+ readonly dedupSkipped: boolean;+}++function interBlockSeparator(doc: BlockDocument, index: number): string | null {+ return index === 0 ? null : doc.blocks[index].separator;+}- for (let i = 0; i < baseBlocks.length; i++) {- const baseBlock = baseBlocks[i];- const ourOp = ourOps.get(i);- const theirOp = theirOps.get(i);+function sameModuloLineEndings(a: string | null, b: string | null): boolean {+ if (a === null || b === null) return a === b;+ return blockIdentity(a) === blockIdentity(b);+}- const ourInserts = ourOp?.insertsBefore ?? [];- const theirInserts = theirOp?.insertsBefore ?? [];- if (pushInsertGroupDeduped(merged, ourInserts, theirInserts)) dedupSkipped = true;+function pickThreeWay<T extends string | null>(+ base: T,+ ours: T | undefined,+ theirs: T | undefined,+): T {+ if (theirs !== undefined && !sameModuloLineEndings(theirs, base)) return theirs;+ if (ours !== undefined) return ours;+ return theirs ?? base;+}- const ourAction = ourOp?.action ?? 'keep';- const theirAction = theirOp?.action ?? 'keep';+function sideSeparator(doc: BlockDocument, op: EditOp): string | null | undefined {+ return op.editIndex === undefined ? undefined : interBlockSeparator(doc, op.editIndex);+}- if (ourAction === 'keep' && theirAction === 'keep') {- merged.push(baseBlock);- } else if (ourAction === 'keep' && theirAction !== 'keep') {- if (theirAction === 'modify' && theirOp?.newContent !== undefined) {- merged.push(theirOp.newContent);- }- } else if (ourAction !== 'keep' && theirAction === 'keep') {- if (ourAction === 'modify' && ourOp?.newContent !== undefined) {- merged.push(ourOp.newContent);- }- } else {- const ourContent = ourAction === 'modify' ? ourOp?.newContent : null;- const theirContent = theirAction === 'modify' ? theirOp?.newContent : null;+function firstInterBlockSeparator(docs: readonly BlockDocument[], fallback: string): string {+ for (const doc of docs) {+ if (doc.blocks.length > 1) return doc.blocks[1].separator;+ }+ return fallback;+}- if (ourContent === theirContent) {- if (ourContent !== null && ourContent !== undefined) merged.push(ourContent);- } else {+function mergeBlocks(+ base: BlockDocument,+ ours: BlockDocument,+ theirs: BlockDocument,+ outputLineEnding: LineEnding | null,+): BlockMerge {+ const ourEdits = computeEditOps(base.keys, ours.keys);+ const theirEdits = computeEditOps(base.keys, theirs.keys);++ const emitted: EmittedBlock[] = [];+ const conflicts: BlockConflict[] = [];+ let dedupSkipped = false;++ for (let i = 0; i < base.blocks.length; i++) {+ const ourOp = ourEdits.blockOps[i];+ const theirOp = theirEdits.blockOps[i];++ if (pushInsertGroupDeduped(emitted, ours, ourOp.insertsBefore, theirs, theirOp.insertsBefore))+ dedupSkipped = true;++ const anchored = (text: string): void => {+ emitted.push({+ text,+ separator: pickThreeWay(+ interBlockSeparator(base, i),+ sideSeparator(ours, ourOp),+ sideSeparator(theirs, theirOp),+ ),+ });+ };+ const ourText = ourOp.action === 'modify' ? ours.blocks[ourOp.editIndex].text : null;+ const theirText = theirOp.action === 'modify' ? theirs.blocks[theirOp.editIndex].text : null;++ if (ourOp.action === 'keep' && theirOp.action === 'keep') {+ anchored(base.blocks[i].text);+ } else if (ourOp.action === 'keep') {+ if (theirText !== null) anchored(theirText);+ } else if (theirOp.action === 'keep') {+ if (ourText !== null) anchored(ourText);+ } else {+ const ourKey = ourText === null ? null : blockIdentity(ourText);+ const theirKey = theirText === null ? null : blockIdentity(theirText);+ if (ourKey !== theirKey) {conflicts.push({blockIndex: i,- base: baseBlock,- ours: ourContent ?? '',- theirs: theirContent ?? '',+ base: base.blocks[i].text,+ ours: ourText ?? '',+ theirs: theirText ?? '',});- if (ourContent !== null && ourContent !== undefined) merged.push(ourContent);}+ if (ourText !== null) anchored(ourText);}}- const lastOurOp = ourOps.get(baseBlocks.length);- const lastTheirOp = theirOps.get(baseBlocks.length);if (- pushInsertGroupDeduped(merged, lastOurOp?.insertsBefore ?? [], lastTheirOp?.insertsBefore ?? [])+ pushInsertGroupDeduped(+ emitted,+ ours,+ ourEdits.trailingInserts,+ theirs,+ theirEdits.trailingInserts,+ ))dedupSkipped = true;- const newContent = merged.length > 0 ? `${merged.join('\n\n')}\n` : '';-- if (conflicts.length > 0) {- return dedupSkipped- ? { kind: 'conflicts', newContent, conflicts, dedupSkipped: true }- : { kind: 'conflicts', newContent, conflicts };+ const leading = pickThreeWay(+ base.blocks[0]?.separator ?? '',+ ours.blocks[0]?.separator ?? '',+ theirs.blocks[0]?.separator ?? '',+ );+ const trailing = pickThreeWay(base.trailing, ours.trailing, theirs.trailing);+ const fallbackSeparator = firstInterBlockSeparator(+ [base, ours, theirs],+ base.trailing.startsWith('\r\n') ? '\r\n\r\n' : '\n\n',+ );++ let assembled = '';+ if (emitted.length > 0) {+ emitted.forEach((block, index) => {+ assembled += index === 0 ? leading : (block.separator ?? fallbackSeparator);+ assembled += block.text;+ });+ assembled += trailing;}- return dedupSkipped- ? { kind: 'merged', newContent, mergedBlocks: merged.length, dedupSkipped: true }- : { kind: 'merged', newContent, mergedBlocks: merged.length };+ return {+ newContent: outputLineEnding ? spellLineEndings(assembled, outputLineEnding) : assembled,+ mergedBlocks: emitted.length,+ conflicts,+ dedupSkipped,+ };}function pushInsertGroupDeduped(- target: string[],- ourInserts: string[],- theirInserts: string[],+ target: EmittedBlock[],+ ours: BlockDocument,+ ourInserts: readonly number[],+ theirs: BlockDocument,+ theirInserts: readonly number[],): boolean {+ const pushOurs = (index: number): void => {+ target.push({ text: ours.blocks[index].text, separator: interBlockSeparator(ours, index) });+ };+ const pushTheirs = (index: number): void => {+ target.push({ text: theirs.blocks[index].text, separator: interBlockSeparator(theirs, index) });+ };if ((ourInserts.length + 1) * (theirInserts.length + 1) > MAX_LCS_CELLS) {- target.push(...ourInserts, ...theirInserts);+ for (const index of ourInserts) pushOurs(index);+ for (const index of theirInserts) pushTheirs(index);return true;}- const spine = longestCommonSubsequence(ourInserts, theirInserts);+ const spine = longestCommonSubsequence(+ ourInserts.map((index) => ours.keys[index]),+ theirInserts.map((index) => theirs.keys[index]),+ );let ourCursor = 0;let theirCursor = 0;for (const [ourMatch, theirMatch] of spine) {- while (ourCursor < ourMatch) target.push(ourInserts[ourCursor++]);- while (theirCursor < theirMatch) target.push(theirInserts[theirCursor++]);- target.push(ourInserts[ourMatch]);+ while (ourCursor < ourMatch) pushOurs(ourInserts[ourCursor++]);+ while (theirCursor < theirMatch) pushTheirs(theirInserts[theirCursor++]);+ pushOurs(ourInserts[ourMatch]);ourCursor = ourMatch + 1;theirCursor = theirMatch + 1;}- while (ourCursor < ourInserts.length) target.push(ourInserts[ourCursor++]);- while (theirCursor < theirInserts.length) target.push(theirInserts[theirCursor++]);+ while (ourCursor < ourInserts.length) pushOurs(ourInserts[ourCursor++]);+ while (theirCursor < theirInserts.length) pushTheirs(theirInserts[theirCursor++]);return false;}-interface EditOp {- action: 'keep' | 'modify' | 'delete';- newContent?: string;- insertsBefore: string[];-}--function computeEditOps(baseBlocks: string[], editedBlocks: string[]): Map<number, EditOp> {- const ops = new Map<number, EditOp>();- const lcs = longestCommonSubsequence(baseBlocks, editedBlocks);+type EditOp =+ | { action: 'keep'; editIndex: number; insertsBefore: number[] }+ | { action: 'modify'; editIndex: number; insertsBefore: number[] }+ | { action: 'delete'; editIndex?: undefined; insertsBefore: number[] };- for (let i = 0; i <= baseBlocks.length; i++) {- ops.set(i, { action: 'keep', insertsBefore: [] });- }+interface EditScript {+ readonly blockOps: readonly EditOp[];+ readonly trailingInserts: readonly number[];+}- const matchedBase = new Set<number>();- const matchedEdit = new Set<number>();+function computeEditOps(baseKeys: readonly string[], editedKeys: readonly string[]): EditScript {+ const lcs = longestCommonSubsequence(baseKeys, editedKeys);+ const editOfBase = new Array<number>(baseKeys.length).fill(-1);+ const matchedEdit = new Array<boolean>(editedKeys.length).fill(false);for (const [bi, ei] of lcs) {- matchedBase.add(bi);- matchedEdit.add(ei);+ editOfBase[bi] = ei;+ matchedEdit[ei] = true;+ }+ const nextAnchorEdit = new Array<number>(baseKeys.length + 1).fill(editedKeys.length);+ for (let bi = baseKeys.length - 1; bi >= 0; bi--) {+ nextAnchorEdit[bi] = editOfBase[bi] === -1 ? nextAnchorEdit[bi + 1] : editOfBase[bi];}+ const blockOps: EditOp[] = [];let prevEditAnchor = -1;- for (let bi = 0; bi < baseBlocks.length; bi++) {- if (matchedBase.has(bi)) {- const editIdx = lcs.find((p) => p[0] === bi)?.[1] ?? -1;-- const inserts: string[] = [];+ for (let bi = 0; bi < baseKeys.length; bi++) {+ const editIdx = editOfBase[bi];+ if (editIdx !== -1) {+ const inserts: number[] = [];for (let ei = prevEditAnchor + 1; ei < editIdx; ei++) {- if (!matchedEdit.has(ei)) {- inserts.push(editedBlocks[ei]);- }+ if (!matchedEdit[ei]) inserts.push(ei);}- const op = ops.get(bi);- if (op) op.insertsBefore = inserts;-+ blockOps.push({ action: 'keep', editIndex: editIdx, insertsBefore: inserts });prevEditAnchor = editIdx;- } else {- const nextBaseAnchor = lcs.find((p) => p[0] > bi);- const nextEditAnchor = nextBaseAnchor ? nextBaseAnchor[1] : editedBlocks.length;-- const candidateEdits: number[] = [];- for (let ei = prevEditAnchor + 1; ei < nextEditAnchor; ei++) {- if (!matchedEdit.has(ei)) {- candidateEdits.push(ei);- }- }-- if (candidateEdits.length > 0) {- const editIdx = candidateEdits[0];- matchedEdit.add(editIdx);- const op = ops.get(bi);- if (op) {- op.action = 'modify';- op.newContent = editedBlocks[editIdx];- }- } else {- const op = ops.get(bi);- if (op) op.action = 'delete';+ continue;+ }+ let candidate = -1;+ for (let ei = prevEditAnchor + 1; ei < nextAnchorEdit[bi + 1]; ei++) {+ if (!matchedEdit[ei]) {+ candidate = ei;+ break;}}- }-- const trailingInserts: string[] = [];- for (let ei = prevEditAnchor + 1; ei < editedBlocks.length; ei++) {- if (!matchedEdit.has(ei)) {- trailingInserts.push(editedBlocks[ei]);+ if (candidate === -1) {+ blockOps.push({ action: 'delete', insertsBefore: [] });+ } else {+ matchedEdit[candidate] = true;+ blockOps.push({ action: 'modify', editIndex: candidate, insertsBefore: [] });}}- const trailingOp = ops.get(baseBlocks.length);- if (trailingOp) trailingOp.insertsBefore = trailingInserts;- return ops;+ const trailingInserts: number[] = [];+ for (let ei = prevEditAnchor + 1; ei < editedKeys.length; ei++) {+ if (!matchedEdit[ei]) trailingInserts.push(ei);+ }+ return { blockOps, trailingInserts };}-function longestCommonSubsequence(a: string[], b: string[]): [number, number][] {+function longestCommonSubsequence(a: readonly string[], b: readonly string[]): [number, number][] {const m = a.length;const n = b.length;const stride = n + 1;@@ -325,3 +512,342 @@ function longestCommonSubsequence(a: string[], b: string[]): [number, number][]return pairs.reverse();}++interface MergeCheckInput {+ readonly base: string;+ readonly ours: string;+ readonly theirs: string;+ readonly newContent: string;+ readonly conflicts: readonly BlockConflict[];+ readonly dedupSkipped: boolean;+}++type CheckedDocument = 'base' | MergeSide | 'output';++interface BlockSource {+ readonly document: CheckedDocument;+ readonly index: number;+}++interface SideAlignment {+ readonly sideOfBase: readonly number[];+ readonly gapStart: readonly number[];+ readonly gapEnd: readonly number[];+ readonly unmatched: readonly number[];+}++interface Occurrence {+ readonly key: string;+ readonly expected: readonly string[];+ readonly sources: readonly BlockSource[];+ readonly side: MergeSide;+ readonly sideBlockIndex: number;+ required: boolean;+ consumed: boolean;+}++function alignToBase(baseKeys: readonly string[], sideKeys: readonly string[]): SideAlignment {+ const sideOfBase = new Array<number>(baseKeys.length).fill(-1);+ const baseOfSide = new Array<number>(sideKeys.length).fill(-1);+ for (const [bi, si] of longestCommonSubsequence(baseKeys, sideKeys)) {+ sideOfBase[bi] = si;+ baseOfSide[si] = bi;+ }+ const gapStart = new Array<number>(sideKeys.length).fill(-1);+ const gapEnd = new Array<number>(sideKeys.length).fill(baseKeys.length);+ const unmatched: number[] = [];+ let previous = -1;+ for (let si = 0; si < sideKeys.length; si++) {+ if (baseOfSide[si] === -1) {+ gapStart[si] = previous;+ unmatched.push(si);+ } else previous = baseOfSide[si];+ }+ let next = baseKeys.length;+ for (let si = sideKeys.length - 1; si >= 0; si--) {+ if (baseOfSide[si] === -1) gapEnd[si] = next;+ else next = baseOfSide[si];+ }+ return { sideOfBase, gapStart, gapEnd, unmatched };+}++function gapsOverlap(a: SideAlignment, ai: number, b: SideAlignment, bi: number): boolean {+ return Math.max(a.gapStart[ai], b.gapStart[bi]) < Math.min(a.gapEnd[ai], b.gapEnd[bi]);+}++function indexByKey(keys: readonly string[], indices: readonly number[]): Map<string, number[]> {+ const byKey = new Map<string, number[]>();+ for (const index of indices) {+ const bucket = byKey.get(keys[index]);+ if (bucket) bucket.push(index);+ else byKey.set(keys[index], [index]);+ }+ return byKey;+}++function collectOccurrences(+ ours: BlockDocument,+ ourAlignment: SideAlignment,+ theirs: BlockDocument,+ theirAlignment: SideAlignment,+ pairIdentical: boolean,+ expectBytes: (raw: string) => string,+): Map<string, Occurrence[]> {+ const occurrences = new Map<string, Occurrence[]>();+ const add = (occurrence: Occurrence): void => {+ const bucket = occurrences.get(occurrence.key);+ if (bucket) bucket.push(occurrence);+ else occurrences.set(occurrence.key, [occurrence]);+ };+ const theirsByKey = indexByKey(theirs.keys, theirAlignment.unmatched);+ const pairedTheirs = new Set<number>();+ for (const oi of ourAlignment.unmatched) {+ const key = ours.keys[oi];+ const ourBytes = expectBytes(ours.blocks[oi].text);+ const twin = pairIdentical+ ? theirsByKey+ .get(key)+ ?.find((ti) => !pairedTheirs.has(ti) && gapsOverlap(ourAlignment, oi, theirAlignment, ti))+ : undefined;+ if (twin !== undefined) pairedTheirs.add(twin);+ add({+ key,+ expected: twin === undefined ? [ourBytes] : [ourBytes, expectBytes(theirs.blocks[twin].text)],+ sources:+ twin === undefined+ ? [{ document: 'ours', index: oi }]+ : [+ { document: 'ours', index: oi },+ { document: 'theirs', index: twin },+ ],+ side: 'ours',+ sideBlockIndex: oi,+ required: true,+ consumed: false,+ });+ }+ for (const ti of theirAlignment.unmatched) {+ if (pairedTheirs.has(ti)) continue;+ add({+ key: theirs.keys[ti],+ expected: [expectBytes(theirs.blocks[ti].text)],+ sources: [{ document: 'theirs', index: ti }],+ side: 'theirs',+ sideBlockIndex: ti,+ required: true,+ consumed: false,+ });+ }+ return occurrences;+}++function releaseConflictingTheirs(+ occurrences: ReadonlyMap<string, readonly Occurrence[]>,+ conflicts: readonly BlockConflict[],+): void {+ for (const conflict of conflicts) {+ if (conflict.theirs === '') continue;+ const occurrence = occurrences+ .get(blockIdentity(conflict.theirs))+ ?.find((candidate) => candidate.required && candidate.side === 'theirs');+ if (occurrence) occurrence.required = false;+ }+}++export function checkMergeOutput(input: MergeCheckInput): MergeCheckFailure | null {+ const outputLineEnding = mergeOutputLineEnding(input.base, input.ours, input.theirs);+ const expectBytes = (raw: string): string =>+ outputLineEnding ? spellLineEndings(raw, outputLineEnding) : raw;+ const base = readBlockDocument(input.base);+ const ours = readBlockDocument(input.ours);+ const theirs = readBlockDocument(input.theirs);+ const output = readBlockDocument(input.newContent);+ const ourAlignment = alignToBase(base.keys, ours.keys);+ const theirAlignment = alignToBase(base.keys, theirs.keys);++ const untouched: number[] = [];+ for (let bi = 0; bi < base.keys.length; bi++) {+ if (ourAlignment.sideOfBase[bi] !== -1 && theirAlignment.sideOfBase[bi] !== -1)+ untouched.push(bi);+ }++ const sources = new Array<readonly BlockSource[]>(output.blocks.length).fill([]);+ const rest: number[] = [];+ let nextUntouched = 0;+ for (let k = 0; k < output.blocks.length; k++) {+ const bi = untouched[nextUntouched];+ if (bi === undefined || output.keys[k] !== base.keys[bi]) {+ rest.push(k);+ continue;+ }+ if (output.blocks[k].text !== expectBytes(base.blocks[bi].text)) {+ return { check: 'untouched-verbatim', baseBlockIndex: bi };+ }+ sources[k] = [+ { document: 'base', index: bi },+ { document: 'ours', index: ourAlignment.sideOfBase[bi] },+ { document: 'theirs', index: theirAlignment.sideOfBase[bi] },+ ];+ nextUntouched++;+ }+ if (nextUntouched < untouched.length) {+ return { check: 'untouched-verbatim', baseBlockIndex: untouched[nextUntouched] };+ }++ const occurrences = collectOccurrences(+ ours,+ ourAlignment,+ theirs,+ theirAlignment,+ !input.dedupSkipped,+ expectBytes,+ );+ releaseConflictingTheirs(occurrences, input.conflicts);++ const unclaimed: number[] = [];+ for (const k of rest) {+ const candidates = (occurrences.get(output.keys[k]) ?? []).filter(+ (occurrence) => !occurrence.consumed,+ );+ if (candidates.length === 0) {+ unclaimed.push(k);+ continue;+ }+ const bytes = output.blocks[k].text;+ const exact =+ candidates.find((c) => c.required && c.expected.includes(bytes)) ??+ candidates.find((c) => c.expected.includes(bytes));+ if (!exact) {+ return {+ check: 'side-bytes',+ side: candidates[0].side,+ sideBlockIndex: candidates[0].sideBlockIndex,+ };+ }+ exact.consumed = true;+ sources[k] = exact.sources;+ }++ for (const bucket of occurrences.values()) {+ const missing = bucket.find((occurrence) => occurrence.required && !occurrence.consumed);+ if (missing) {+ return { check: 'side-bytes', side: missing.side, sideBlockIndex: missing.sideBlockIndex };+ }+ }+ if (unclaimed.length > 0) {+ return { check: 'single-occurrence', outputBlockIndex: unclaimed[0] };+ }++ return checkConstructs(+ { base: input.base, ours: input.ours, theirs: input.theirs, output: input.newContent },+ sources,+ );+}++interface RootSpan {+ readonly type: string;+ readonly startLine: number;+ readonly endLine: number;+}++interface ParsedDocument {+ readonly spans: readonly RootSpan[];+ readonly blockLines: readonly (readonly [number, number])[];+}++function parseDocument(md: string): ParsedDocument | null {+ let root: ReturnType<typeof mdManager.parseToMdast>;+ try {+ root = mdManager.parseToMdast(md);+ } catch {+ return null;+ }+ const spans: RootSpan[] = [];+ for (const child of root.children) {+ if (!child.position) continue;+ spans.push({+ type: child.type,+ startLine: child.position.start.line,+ endLine: child.position.end.line,+ });+ }+ return { spans, blockLines: blockLineRanges(md) };+}++const LINE_ENDING_RE = /\r\n|\r|\n/g;++function blockLineRanges(md: string): (readonly [number, number])[] {+ const lineStarts = [0];+ for (const match of md.matchAll(LINE_ENDING_RE)) lineStarts.push(match.index + match[0].length);+ const lineOf = (offset: number): number => {+ let lo = 0;+ let hi = lineStarts.length - 1;+ while (lo < hi) {+ const mid = (lo + hi + 1) >>> 1;+ if (lineStarts[mid] <= offset) lo = mid;+ else hi = mid - 1;+ }+ return lo + 1;+ };+ const ranges: (readonly [number, number])[] = [];+ let offset = 0;+ for (const block of splitMarkdownDocument(md).blocks) {+ offset += block.separator.length;+ ranges.push([lineOf(offset), lineOf(offset + block.text.length - 1)]);+ offset += block.text.length;+ }+ return ranges;+}++function coveringTypes(+ spans: readonly RootSpan[],+ [startLine, endLine]: readonly [number, number],+): string {+ let lo = 0;+ let hi = spans.length;+ while (lo < hi) {+ const mid = (lo + hi) >>> 1;+ if (spans[mid].endLine < startLine) lo = mid + 1;+ else hi = mid;+ }+ const types: string[] = [];+ for (let i = lo; i < spans.length && spans[i].startLine <= endLine; i++)+ types.push(spans[i].type);+ return types.join(' ');+}++function checkConstructs(+ texts: Readonly<Record<CheckedDocument, string>>,+ sources: readonly (readonly BlockSource[])[],+): MergeCheckFailure | null {+ const largestBytes = Math.max(...Object.values(texts).map((text) => Buffer.byteLength(text)));+ if (largestBytes > MAX_CONSTRUCT_CHECK_BYTES) {+ getLogger('reconcile').debug(+ { largestBytes, limitBytes: MAX_CONSTRUCT_CHECK_BYTES },+ '[reconcile] construct check skipped: a document is over the size bound',+ );+ return null;+ }+ const base = parseDocument(texts.base);+ const ours = parseDocument(texts.ours);+ const theirs = parseDocument(texts.theirs);+ if (!base || !ours || !theirs) return null;+ const output = parseDocument(texts.output);+ if (!output) return { check: 'parse' };+ const parsed: Readonly<Record<CheckedDocument, ParsedDocument>> = { base, ours, theirs, output };++ const constructAt = (source: BlockSource): string | null => {+ const document = parsed[source.document];+ const lines = document.blockLines[source.index];+ return lines ? coveringTypes(document.spans, lines) : null;+ };++ for (let k = 0; k < sources.length; k++) {+ const merged = constructAt({ document: 'output', index: k });+ const stable =+ merged !== null &&+ sources[k].some((source) => source.index !== -1 && constructAt(source) === merged);+ if (!stable) return { check: 'construct', outputBlockIndex: k };+ }+ return null;+}diff --git a/packages/server/src/server-factory.ts b/packages/server/src/server-factory.tsindex 0c1ffd0ed..854420cc3 100644--- a/packages/server/src/server-factory.ts+++ b/packages/server/src/server-factory.ts@@ -152,6 +152,7 @@ import {type FrozenResidentRevival,redactedErrorSummary,refuseStaleExternalWrite,+ reportReconcileCheckFailed,reviveFrozenResidentDoc,serializeYDocSource,wireReasonForRefusal,@@ -3232,6 +3233,7 @@ export function createServer(options: ServerOptions): ServerInstance {await applyDiskAuthoritativeIngest(docName, theirs, false);break;}+ reportReconcileCheckFailed(docName, result, 'watcher');incrementConflict();conflicts.raise({kind: 'reconcile',@@ -4995,6 +4997,7 @@ export function createServer(options: ServerOptions): ServerInstance {});reportInsertDedupSkipped(docName, outcome, parked.markdown, currentDisk);+ reportReconcileCheckFailed(docName, outcome, 'branch-switch');switch (outcome.kind) {case 'merged':@@ -5022,18 +5025,20 @@ export function createServer(options: ServerOptions): ServerInstance {}case 'noop':break;- case 'refused':- switch (outcome.reason) {+ case 'refused': {+ const refusal = outcome.reason;+ switch (refusal) {case 'conflict-markers':case 'no-base':case 'too-large':+ case 'check-failed':log.warn(- { docName, reason: outcome.reason, branch: newBranch },- `[branch-switch] parked WIP not restored for ${docName} (reconcile refused: ${outcome.reason})`,+ { docName, reason: refusal, branch: newBranch },+ `[branch-switch] parked WIP not restored for ${docName} (reconcile refused: ${refusal})`,);break;default: {- const unhandled: never = outcome.reason;+ const unhandled: never = refusal;log.warn({ docName, reason: String(unhandled), branch: newBranch },`[branch-switch] parked WIP not restored for ${docName} (reconcile refused: ${String(unhandled)})`,@@ -5042,6 +5047,7 @@ export function createServer(options: ServerOptions): ServerInstance {}}break;+ }}} catch (e) {log.error(