Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fix saving pages recreated after external moves (#6177)

Nick Gomeze99235ace0eb
diff --git a/.changeset/recreated-document-admission.md b/.changeset/recreated-document-admission.md
new file mode 100644
index 000000000..94b7f6c7e
--- /dev/null
+++ b/.changeset/recreated-document-admission.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Fix recreated documents showing old content or failing to save edits after the original file is moved externally.
diff --git a/packages/app/tests/integration/external-move-recreate-admission.test.ts b/packages/app/tests/integration/external-move-recreate-admission.test.ts
new file mode 100644
index 000000000..05e080f49
--- /dev/null
+++ b/packages/app/tests/integration/external-move-recreate-admission.test.ts
@@ -0,0 +1,197 @@
+import { randomUUID } from 'node:crypto';
+import { existsSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from 'node:fs';
+import { dirname, join } from 'node:path';
+import {
+ DocumentReadSuccessSchema,
+ DuplicatePathSuccessSchema,
+ MetricsReconciliationSuccessSchema,
+ MetricsWatcherRecentSuccessSchema,
+} from '@inkeep/open-knowledge-core';
+import { afterEach, beforeEach, expect, test } from 'vitest';
+import {
+ createTestClient,
+ createTestServer,
+ HARNESS_BOOT_TIMEOUT_MS,
+ pollUntil,
+ type TestServer,
+} from './test-harness';
+
+const ORIGINAL = '# Former occupant\n\nContent before the move.\n';
+const COPIED = '# Copied source\n\nContent from the source.\n';
+
+let server: TestServer;
+
+beforeEach(async () => {
+ server = await createTestServer();
+}, HARNESS_BOOT_TIMEOUT_MS);
+
+afterEach(async () => {
+ await server.cleanup();
+});
+
+async function watcherDispatchCount(kind: string, pathSuffix: string): Promise<number> {
+ const response = await fetch(`${server.baseUrl}/api/metrics/watcher-recent`);
+ expect(response.status).toBe(200);
+ const { decisions } = MetricsWatcherRecentSuccessSchema.parse(await response.json());
+ return decisions.filter(
+ (decision) =>
+ decision.decision === 'dispatched' &&
+ decision.kind === kind &&
+ decision['doc.name'].endsWith(`/${pathSuffix}`),
+ ).length;
+}
+
+async function watcherCreateCount(docName: string): Promise<number> {
+ return watcherDispatchCount('create', `${docName}.md`);
+}
+
+async function seedFile(docName: string, content: string): Promise<string> {
+ const path = join(server.contentDir, `${docName}.md`);
+ if (!existsSync(dirname(path))) {
+ const folder = dirname(docName);
+ mkdirSync(dirname(path));
+ await pollUntil(
+ async () => (await watcherDispatchCount('folder-create', folder)) > 0,
+ undefined,
+ undefined,
+ `the native watcher folder-create dispatch for seeded ${folder}`,
+ );
+ }
+ writeFileSync(path, content, { encoding: 'utf-8', flag: 'wx' });
+ await pollUntil(
+ async () => (await watcherCreateCount(docName)) > 0,
+ undefined,
+ undefined,
+ `the native watcher create dispatch for seeded ${docName}`,
+ );
+ return path;
+}
+
+async function moveLoadedDocument(docName: string): Promise<string> {
+ const originalPath = await seedFile(docName, ORIGINAL);
+ const destinationPath = join(server.contentDir, `moved-${randomUUID()}.md`);
+ const client = await createTestClient(server.port, docName, { resetOnCleanup: false });
+ try {
+ expect(client.ytext.toString()).toBe(ORIGINAL);
+ let closed = false;
+ client.provider.on('close', () => {
+ closed = true;
+ });
+ renameSync(originalPath, destinationPath);
+ await pollUntil(
+ () =>
+ closed &&
+ server.instance.hocuspocus.documents.get(docName)?.getMap('lifecycle').get('status') ===
+ 'renamed',
+ undefined,
+ undefined,
+ `the client close and renamed lifecycle after the external move of ${docName}`,
+ );
+ expect(existsSync(originalPath)).toBe(false);
+ expect(readFileSync(destinationPath, 'utf-8')).toBe(ORIGINAL);
+ } finally {
+ await client.cleanup();
+ }
+ return destinationPath;
+}
+
+async function assertAdmission(
+ docName: string,
+ content: string,
+ destinationPath: string,
+): Promise<void> {
+ expect(readFileSync(join(server.contentDir, `${docName}.md`), 'utf-8')).toBe(content);
+ const response = await fetch(
+ `${server.baseUrl}/api/document?docName=${encodeURIComponent(docName)}`,
+ );
+ expect(response.status).toBe(200);
+ const document = DocumentReadSuccessSchema.parse(await response.json());
+ expect({
+ docName: document.docName,
+ content: document.content,
+ lifecycle: document.lifecycle,
+ movedDisk: readFileSync(destinationPath, 'utf-8'),
+ }).toEqual({ docName, content, lifecycle: null, movedDisk: ORIGINAL });
+}
+
+test('empty page creation exposes the new content and active lifecycle at a moved path', async () => {
+ const docName = `page-${randomUUID()}`;
+ const destinationPath = await moveLoadedDocument(docName);
+ const response = await fetch(`${server.baseUrl}/api/create-page`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ path: `${docName}.md` }),
+ });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toMatchObject({ docName });
+ await assertAdmission(docName, '', destinationPath);
+});
+
+test('file duplication exposes copied content and active lifecycle at its reused path', async () => {
+ const source = `source-${randomUUID()}`;
+ await seedFile(source, COPIED);
+ const docName = `${source} copy`;
+ const destinationPath = await moveLoadedDocument(docName);
+ const response = await fetch(`${server.baseUrl}/api/duplicate-path`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ kind: 'file', path: `${source}.md` }),
+ });
+ expect(response.status).toBe(200);
+ const duplicate = DuplicatePathSuccessSchema.parse(await response.json());
+ expect(duplicate).toEqual({ kind: 'file', path: docName, duplicatedDocNames: [docName] });
+ await assertAdmission(duplicate.path, COPIED, destinationPath);
+});
+
+test('folder duplication exposes copied child content and active lifecycle at its reused path', async () => {
+ const folder = `folder-${randomUUID()}`;
+ await seedFile(`${folder}/entry`, COPIED);
+ const docName = `${folder} copy/entry`;
+ const destinationPath = await moveLoadedDocument(docName);
+ rmdirSync(join(server.contentDir, `${folder} copy`));
+ const response = await fetch(`${server.baseUrl}/api/duplicate-path`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ kind: 'folder', path: folder }),
+ });
+ expect(response.status).toBe(200);
+ const duplicate = DuplicatePathSuccessSchema.parse(await response.json());
+ expect(duplicate.kind).toBe('folder');
+ expect(duplicate.path).toBe(`${folder} copy`);
+ const child = `${duplicate.path}/entry`;
+ expect(duplicate.duplicatedDocNames).toContain(child);
+ expect(child).toBe(docName);
+ await assertAdmission(child, COPIED, destinationPath);
+});
+
+async function removedDocumentCount(): Promise<unknown> {
+ const response = await fetch(`${server.baseUrl}/api/metrics/reconciliation`);
+ expect(response.status).toBe(200);
+ const metrics = MetricsReconciliationSuccessSchema.parse(await response.json());
+ return metrics.recentlyRemovedDocsSize;
+}
+
+test('native recreation exposes its new content and active lifecycle after watcher admission', async () => {
+ const docName = `native-${randomUUID()}`;
+ const destinationPath = await moveLoadedDocument(docName);
+ expect(await removedDocumentCount()).toBe(1);
+ const seedCreateCount = await watcherCreateCount(docName);
+ const content = '# Recreated externally\n\nContent from the new file.\n';
+ writeFileSync(join(server.contentDir, `${docName}.md`), content, {
+ encoding: 'utf-8',
+ flag: 'wx',
+ });
+ await pollUntil(
+ async () => (await watcherCreateCount(docName)) > seedCreateCount,
+ undefined,
+ undefined,
+ `the native watcher create dispatch for recreated ${docName}`,
+ );
+ await pollUntil(
+ async () => (await removedDocumentCount()) === 0,
+ undefined,
+ undefined,
+ `the removed-document marker for ${docName} to clear`,
+ );
+ await assertAdmission(docName, content, destinationPath);
+});
diff --git a/packages/app/tests/integration/external-move-recreate-durability.test.ts b/packages/app/tests/integration/external-move-recreate-durability.test.ts
new file mode 100644
index 000000000..44b10ece3
--- /dev/null
+++ b/packages/app/tests/integration/external-move-recreate-durability.test.ts
@@ -0,0 +1,123 @@
+import { randomUUID } from 'node:crypto';
+import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import {
+ DocumentReadSuccessSchema,
+ MetricsWatcherRecentSuccessSchema,
+ PageHeadingsSuccessSchema,
+} from '@inkeep/open-knowledge-core';
+import { afterAll, beforeAll, expect, test } from 'vitest';
+import {
+ createTestClient,
+ createTestServer,
+ HARNESS_BOOT_TIMEOUT_MS,
+ pollUntil,
+ type TestServer,
+} from './test-harness';
+
+let server: TestServer;
+
+beforeAll(async () => {
+ server = await createTestServer();
+}, HARNESS_BOOT_TIMEOUT_MS);
+
+afterAll(async () => {
+ await server.cleanup();
+});
+
+test('persists an accepted replacement at a recreated path after an external move', async () => {
+ const originalName = `original-${randomUUID()}`;
+ const destinationName = `destination-${randomUUID()}`;
+ const originalPath = join(server.contentDir, `${originalName}.md`);
+ const destinationPath = join(server.contentDir, `${destinationName}.md`);
+ const original = '# Original first\n\nOriginal body.\n\n## Original second\n';
+ const replacement = '# Recreated first\n\nNew body.\n\n## Recreated second\n';
+
+ writeFileSync(originalPath, original, { encoding: 'utf-8', flag: 'wx' });
+ await pollUntil(
+ async () => {
+ const response = await fetch(`${server.baseUrl}/api/metrics/watcher-recent`);
+ expect(response.status).toBe(200);
+ const { decisions } = MetricsWatcherRecentSuccessSchema.parse(await response.json());
+ return decisions.some(
+ (decision) =>
+ decision.decision === 'dispatched' &&
+ decision.kind === 'create' &&
+ decision['doc.name'].endsWith(`/${originalName}.md`),
+ );
+ },
+ undefined,
+ undefined,
+ `the native watcher create dispatch for seeded ${originalName}`,
+ );
+
+ const client = await createTestClient(server.port, originalName, { resetOnCleanup: false });
+ try {
+ expect(client.ytext.toString()).toBe(original);
+ let closed = false;
+ client.provider.on('close', () => {
+ closed = true;
+ });
+
+ renameSync(originalPath, destinationPath);
+ await pollUntil(
+ () =>
+ closed &&
+ server.instance.hocuspocus.documents
+ .get(originalName)
+ ?.getMap('lifecycle')
+ .get('status') === 'renamed',
+ undefined,
+ undefined,
+ `the client close and renamed lifecycle after the external move of ${originalName}`,
+ );
+ expect(existsSync(originalPath)).toBe(false);
+ expect(readFileSync(destinationPath, 'utf-8')).toBe(original);
+ } finally {
+ await client.cleanup();
+ }
+
+ const created = await fetch(`${server.baseUrl}/api/create-page`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ path: `${originalName}.md` }),
+ });
+ expect(created.status).toBe(200);
+ expect(await created.json()).toMatchObject({ docName: originalName });
+ expect(readFileSync(originalPath, 'utf-8')).toBe('');
+
+ const written = await fetch(`${server.baseUrl}/api/agent-write-md`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ docName: originalName,
+ markdown: replacement,
+ position: 'replace',
+ agentId: `writer-${randomUUID()}`,
+ }),
+ });
+ expect(written.status).toBe(200);
+
+ const recreatedDisk = readFileSync(originalPath, 'utf-8');
+ const destinationDisk = readFileSync(destinationPath, 'utf-8');
+ const liveResponse = await fetch(`${server.baseUrl}/api/document?docName=${originalName}`);
+ expect(liveResponse.status).toBe(200);
+ const live = DocumentReadSuccessSchema.parse(await liveResponse.json());
+ const headingsResponse = await fetch(
+ `${server.baseUrl}/api/page-headings?docName=${originalName}`,
+ );
+ expect(headingsResponse.status).toBe(200);
+ const { headings } = PageHeadingsSuccessSchema.parse(await headingsResponse.json());
+
+ expect({
+ live: live.content,
+ recreatedDisk,
+ destinationDisk,
+ headings: headings.map(({ text }) => text),
+ }).toEqual({
+ live: replacement,
+ recreatedDisk: replacement,
+ destinationDisk: original,
+ headings: ['Recreated first', 'Recreated second'],
+ });
+});
diff --git a/packages/server/src/server-factory.ts b/packages/server/src/server-factory.ts
index 38e4cee32..ed738d6b1 100644
--- a/packages/server/src/server-factory.ts
+++ b/packages/server/src/server-factory.ts
@@ -74,6 +74,7 @@ import {
CC1Broadcaster,
isConfigDoc,
isManagedArtifactDoc,
+ isPersistenceExcludedDoc,
isReservedForUserTree,
SYSTEM_DOC_NAME,
} from './cc1-broadcast.ts';
@@ -2327,6 +2328,9 @@ export function createServer(options: ServerOptions): ServerInstance {
}
break;
case 'create':
+ restoreRenamedDocumentOnCreate(event.docName, event.content);
+ scheduleIndexRegeneration(event.docName);
+ break;
case 'conflict':
scheduleIndexRegeneration(event.docName);
break;
@@ -2501,6 +2505,22 @@ export function createServer(options: ServerOptions): ServerInstance {
return serializeYDocSource(document);
}
+ function restoreRenamedDocumentOnCreate(docName: string, content: string): void {
+ if (isPersistenceExcludedDoc(docName)) return;
+ const document = hocuspocus.documents.get(docName);
+ if (!document) return;
+ const lifecycle = document.getMap('lifecycle');
+ if (lifecycle.get('status') !== 'renamed') return;
+
+ document.transact(() => {
+ applyDiskContentToDoc(document, content, resolveEmbed, docName, resolveSize);
+ lifecycle.delete('status');
+ lifecycle.delete('newPath');
+ }, FILE_WATCHER_ORIGIN);
+ setReconciledBase(docName, content);
+ log.info({ docName }, `[reconcile] create: cleared stale renamed lifecycle on ${docName}`);
+ }
+
const applyToDoc = (docName: string, content: string): void =>
applyExternalChange(
durabilityState,
@@ -2781,6 +2801,7 @@ export function createServer(options: ServerOptions): ServerInstance {
switch (event.kind) {
case 'create': {
log.info({ docName: event.docName }, `[reconcile] create: ${event.docName}`);
+ restoreRenamedDocumentOnCreate(event.docName, event.content);
await derivedDocumentIndex.recordDiskUpsert(event.docName, event.content);
signalChannel('files');
scheduleIndexRegeneration(event.docName);