Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Keep a failed delete conflict resolution retryable (#4850)

Nick Gomez588663df5cff
diff --git a/.changeset/stale-external-write-delete-retry.md b/.changeset/stale-external-write-delete-retry.md
new file mode 100644
index 000000000..73c99e7b7
--- /dev/null
+++ b/.changeset/stale-external-write-delete-retry.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge-server": patch
+---
+
+A conflict resolution that deletes the file now stays retryable when saving its state fails. The file watcher no longer misreads the server's own removal as an external delete, so it no longer clears the conflict that resolution is still working on.
diff --git a/docs/content/reference/mcp.mdx b/docs/content/reference/mcp.mdx
index 7af512ce0..48cb1bdb3 100644
--- a/docs/content/reference/mcp.mdx
+++ b/docs/content/reference/mcp.mdx
@@ -98,7 +98,7 @@ A whole-document replace attempted while another writer changed the same documen
1. `conflicts({ kind: "list" })` returns every tracked conflict as `{ file, detectedAt, conflict, reason?, conflictKind?, docName }`. Call it to detect conflicts without waiting for a 409.
2. `conflicts({ kind: "content", file })` returns `{ content: { file, base, ours, theirs, shape, conflict, reason?, conflictKind?, resolutionOptions } }`. `ours` is the live Y.Text when the doc is loaded and marker-free; otherwise it falls back to the stage pinned at detection. For a stale external write, `ours` is the protected content and `theirs` is the rejected older save.
-3. `resolve_conflict({ file, strategy, content? })` writes the chosen bytes. Send a strategy from that conflict's `resolutionOptions`, not the full set. A `reconcile` conflict whose `theirs` side is raw marker text does not offer `theirs`, and sending it anyway returns a 422 with `refusal: "strategy-not-offered"`. The same 422 shape carries `refusal: "markers-in-content"` when the bytes you sent still contain markers, and that field is what tells the two apart. A 404 (`urn:ok:error:no-conflict-tracked`) means nothing is tracked for that path. Re-read the list; it does not mean your resolution was saved.
+3. `resolve_conflict({ file, strategy, content? })` writes the chosen bytes. Send a strategy from that conflict's `resolutionOptions`, not the full set. A `reconcile` conflict whose `theirs` side is raw marker text does not offer `theirs`, and sending it anyway returns a 422 with `refusal: "strategy-not-offered"`. The same 422 shape carries `refusal: "markers-in-content"` when the bytes you sent still contain markers, and that field is what tells the two apart. A 404 (`urn:ok:error:no-conflict-tracked`) means nothing is tracked for that path. Re-read the list; it does not mean your resolution was saved. A 500 (`urn:ok:error:internal-server-error`) means the resolution was not recorded: the conflict is still tracked and the call is retryable. Both 500s surface as an error whose text carries the problem title: `Failed to resolve conflict.` from the resolver itself, or `Failed to read request body.` when the request never got that far. The structured content is `{ ok: false, file }`, where `file` is the argument you sent rather than a value from the response. Your `strategy` does not come back either way, so resubmit the `file` and `strategy` you already hold. On `delete` the file may already be gone from disk even though the resolution did not complete; resubmitting `delete` finishes the teardown rather than failing on the missing file.
What `mine` and `theirs` mean depends on the kind. On `merge-native` they are `git checkout --ours` and `git checkout --theirs` (the committed stages 2 and 3), and `delete` is `git rm`. On `working-tree`, `mine` keeps your overlay verbatim and `theirs` restores the pinned origin blob. On `reconcile`, `mine` uses the latest marker-free live Y.Text, falling back to the captured editor stage when the document is unloaded or contains markers; `theirs` uses the disk bytes captured at detection. `content` always writes the bytes you supply, and `delete` always removes the file.
diff --git a/packages/app/tests/integration/stale-external-write-rollback.test.ts b/packages/app/tests/integration/stale-external-write-rollback.test.ts
index 4bd7cec37..a0c2cb348 100644
--- a/packages/app/tests/integration/stale-external-write-rollback.test.ts
+++ b/packages/app/tests/integration/stale-external-write-rollback.test.ts
@@ -13,10 +13,15 @@ import {
writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
-import { afterEach, describe, expect, test } from 'vitest';
+import { basename, join } from 'node:path';
+import { afterEach, describe, expect, test, vi } from 'vitest';
import { DocumentDurabilityStateError } from '../../../server/src/document-durability-state';
-import { writeTracker } from '../../../server/src/file-watcher';
+import {
+ getWatcherDecisionRingSnapshot,
+ isSelfRemoval,
+ removalTracker,
+ writeTracker,
+} from '../../../server/src/file-watcher';
import { contentHash } from '../../../server/src/version-hash';
import {
agentWriteMd,
@@ -110,6 +115,46 @@ function lifecycleOf(
return target.instance.conflicts.lifecycleOf(document, docName) ?? {};
}
+function observeLifecycleStatuses(target: TestServer, docName: string): unknown[] {
+ const document = target.instance.hocuspocus.documents.get(docName);
+ if (!document) throw new Error(`document ${docName} not loaded`);
+ const lifecycle = document.getMap('lifecycle');
+ const seen: unknown[] = [];
+ lifecycle.observe(() => seen.push(lifecycle.get('status')));
+ return seen;
+}
+
+function lifecycleStatusOf(target: TestServer, docName: string): unknown {
+ const document = target.instance.hocuspocus.documents.get(docName);
+ if (!document) return 'document-unloaded';
+ return target.instance.conflicts.lifecycleOf(document, docName)?.status;
+}
+
+async function awaitWatcherObservationOfDoc(docName: string): Promise<void> {
+ await pollUntil(
+ () =>
+ getWatcherDecisionRingSnapshot().some((record) => basename(record.path) === `${docName}.md`),
+ 15_000,
+ 25,
+ `the file watcher to observe the creation of ${docName}.md, without which @parcel/watcher can coalesce that creation with the unlink the resolution issues next into one batch entry that its inotify backend drops outright`,
+ );
+}
+
+async function awaitWatcherDecisionForDoc(docName: string, ringOffset: number): Promise<void> {
+ await pollUntil(
+ () =>
+ getWatcherDecisionRingSnapshot()
+ .slice(ringOffset)
+ .some(
+ (record) =>
+ basename(record.path) === `${docName}.md` && record.decision === 'self-removal-skip',
+ ),
+ 15_000,
+ 25,
+ `the file watcher to classify the disk event this resolution produced for ${docName}.md as the server's own removal, without which the assertions after it race that event`,
+ );
+}
+
describe('stale external write does not roll back an acknowledged agent write', () => {
test.each([false, true])(
'resolves nonempty stale sides to an explicit empty file (unloaded=%s)',
@@ -204,12 +249,13 @@ describe('stale external write does not roll back an acknowledged agent write',
const docName = `resolve-${randomUUID()}`;
const { stale } = await acknowledgeWrite(server, docName);
await restoreStale(server, docName, stale);
+ const lifecycleStatuses = observeLifecycleStatuses(server, docName);
expect((await resolveConflict(server, `${docName}.md`, strategy)).status).toBe(200);
expect(server.instance.durabilityState.listStaleExternalWrites()).toEqual([]);
expect(server.instance.durabilityState.isDisplacedVersion(docName, stale)).toBe(false);
if (strategy === 'delete') {
expect(existsSync(join(server.contentDir, `${docName}.md`))).toBe(false);
- expect(lifecycleOf(server, docName).status).toBe('deleted-upstream');
+ expect(lifecycleStatuses).toContain('deleted-upstream');
const tags = await (await fetch(`${server.baseUrl}/api/tags/stale-write-test`)).json();
expect(tags.docs.map((entry: { docName: string }) => entry.docName)).not.toContain(docName);
} else {
@@ -220,6 +266,21 @@ describe('stale external write does not roll back an acknowledged agent write',
},
);
+ test('a successful delete resolution leaves no resident document, so suppressing the self-delete must move that unload onto the resolution path', async () => {
+ server = await createTestServer({ debounce: 50, maxDebounce: 200 });
+ const docName = `unload-${randomUUID()}`;
+ const { stale } = await acknowledgeWrite(server, docName);
+ await restoreStale(server, docName, stale);
+ expect(server.instance.hocuspocus.documents.has(docName)).toBe(true);
+ expect((await resolveConflict(server, `${docName}.md`, 'delete')).status).toBe(200);
+ await pollUntil(
+ () => server?.instance.hocuspocus.documents.has(docName) === false,
+ 15_000,
+ 25,
+ `${docName} to be unloaded from hocuspocus.documents after its successful delete resolution`,
+ );
+ });
+
test('an unloaded stale write is listed, and delete then restore starts fresh', async () => {
server = await createTestServer({ debounce: 50, maxDebounce: 200 });
const docName = `unloaded-${randomUUID()}`;
@@ -309,6 +370,24 @@ describe('stale external write does not roll back an acknowledged agent write',
expect(state.listStaleExternalWrites()).toEqual([]);
});
+ test('a delete resolution removes an in-tree symlink at the alias and the watcher attributes it there, not at its target', async () => {
+ server = await createTestServer({ debounce: 50, maxDebounce: 200 });
+ const docName = `alias-delete-${randomUUID()}`;
+ const target = join(server.contentDir, `${docName}-target.md`);
+ const alias = join(server.contentDir, `${docName}.md`);
+ writeFileSync(target, 'stale\n');
+ symlinkSync(target, alias);
+ await awaitWatcherObservationOfDoc(docName);
+ const state = server.instance.durabilityState;
+ state.setReconciledBase(docName, 'acknowledged\n');
+ state.recordStaleExternalWrite(docName, 'stale\n');
+ const ringOffset = getWatcherDecisionRingSnapshot().length;
+ expect((await resolveConflict(server, `${docName}.md`, 'delete')).status).toBe(200);
+ expect(existsSync(alias)).toBe(false);
+ expect(readFileSync(target, 'utf-8')).toBe('stale\n');
+ await awaitWatcherDecisionForDoc(docName, ringOffset);
+ });
+
test('a failed disk write leaves the acknowledged document and conflict unchanged', async () => {
server = await createTestServer({ debounce: 50, maxDebounce: 200 });
const docName = `directory-${randomUUID()}`;
@@ -327,6 +406,32 @@ describe('stale external write does not roll back an acknowledged agent write',
expect(readTestDoc(server.contentDir, docName)).toBe('acknowledged\n');
});
+ test('a delete resolution whose unlink fails leaves no removal declared', async () => {
+ server = await createTestServer({ debounce: 50, maxDebounce: 200 });
+ const docName = `unlink-fail-${randomUUID()}`;
+ const file = join(server.contentDir, `${docName}.md`);
+ mkdirSync(file);
+ const state = server.instance.durabilityState;
+ state.setReconciledBase(docName, 'acknowledged\n');
+ state.recordStaleExternalWrite(docName, 'stale\n');
+
+ const declared = vi.spyOn(removalTracker, 'set');
+ try {
+ const failed = await resolveConflict(server, `${docName}.md`, 'delete');
+ expect(failed.status).toBe(500);
+ expect(lstatSync(file).isDirectory()).toBe(true);
+ expect(declared).toHaveBeenCalledWith(
+ join(realpathSync(server.contentDir), `${docName}.md`),
+ expect.any(Number),
+ );
+ } finally {
+ declared.mockRestore();
+ }
+ expect(isSelfRemoval(join(realpathSync(server.contentDir), `${docName}.md`))).toBe(false);
+
+ rmSync(file, { recursive: true });
+ });
+
test.each(['mine', 'delete'])(
'a durability filesystem failure during %s keeps the conflict retryable',
async (strategy) => {
@@ -337,18 +442,23 @@ describe('stale external write does not roll back an acknowledged agent write',
const statePath = join(server.instance.lockDir, 'stale-external-writes.json');
renameSync(statePath, `${statePath}.backup`);
mkdirSync(statePath);
+ const ringOffset = getWatcherDecisionRingSnapshot().length;
const failed = await resolveConflict(server, `${docName}.md`, strategy);
expect(failed.status).toBe(500);
expect((await failed.json()).detail).toBeTruthy();
- expect(server.instance.durabilityState.getStaleExternalWrite(docName)).toBeDefined();
- expect(lifecycleOf(server, docName).status).toBe('conflict');
- if (strategy === 'mine') expect(readTestDoc(server.contentDir, docName)).toBe(acknowledged);
- else expect(existsSync(join(server.contentDir, `${docName}.md`))).toBe(false);
+ if (strategy === 'delete') await awaitWatcherDecisionForDoc(docName, ringOffset);
+ expect.soft(server.instance.durabilityState.getStaleExternalWrite(docName)).toBeDefined();
+ expect.soft(lifecycleStatusOf(server, docName)).toBe('conflict');
+ if (strategy === 'mine')
+ expect.soft(readTestDoc(server.contentDir, docName)).toBe(acknowledged);
+ else expect.soft(existsSync(join(server.contentDir, `${docName}.md`))).toBe(false);
rmSync(statePath, { recursive: true });
renameSync(`${statePath}.backup`, statePath);
- expect((await resolveConflict(server, `${docName}.md`, 'mine')).status).toBe(200);
+ const retry = await resolveConflict(server, `${docName}.md`, 'mine');
+ expect(retry.status).toBe(200);
+ expect(await retry.json()).toEqual({});
expect(server.instance.durabilityState.getStaleExternalWrite(docName)).toBeUndefined();
- expect(lifecycleOf(server, docName).status).toBeUndefined();
+ expect(lifecycleStatusOf(server, docName)).toBeUndefined();
},
);
diff --git a/packages/server/src/conflict-authority.test-helper.ts b/packages/server/src/conflict-authority.test-helper.ts
index dd1574f01..49aa88c5b 100644
--- a/packages/server/src/conflict-authority.test-helper.ts
+++ b/packages/server/src/conflict-authority.test-helper.ts
@@ -22,7 +22,8 @@ export function createTestConflictAuthority(
io: {
gitRaw: async () => '',
writeProjectFileUntracked: () => {},
- unlinkProjectFile: () => {},
+ unlinkProjectFileUndeclared: () => {},
+ deleteResolvedContent: () => {},
applyResolvedContent: async () => {},
},
});
diff --git a/packages/server/src/conflict-authority.test.ts b/packages/server/src/conflict-authority.test.ts
index 95dfa3aea..78e66898e 100644
--- a/packages/server/src/conflict-authority.test.ts
+++ b/packages/server/src/conflict-authority.test.ts
@@ -46,6 +46,7 @@ interface FakeIo extends ConflictIo {
gitCalls: string[][];
writes: Array<{ absPath: string; bytes: string }>;
unlinks: string[];
+ declaredDeletes: string[];
applied: Array<{ docName: string; bytes: string }>;
}
@@ -53,11 +54,13 @@ function makeIo(overrides: Partial<ConflictIo> = {}): FakeIo {
const gitCalls: string[][] = [];
const writes: Array<{ absPath: string; bytes: string }> = [];
const unlinks: string[] = [];
+ const declaredDeletes: string[] = [];
const applied: Array<{ docName: string; bytes: string }> = [];
return {
gitCalls,
writes,
unlinks,
+ declaredDeletes,
applied,
gitRaw: async (args) => {
gitCalls.push(args);
@@ -67,7 +70,12 @@ function makeIo(overrides: Partial<ConflictIo> = {}): FakeIo {
writes.push({ absPath, bytes });
writeFileSync(absPath, bytes, 'utf-8');
},
- unlinkProjectFile: (absPath) => {
+ unlinkProjectFileUndeclared: (absPath) => {
+ unlinks.push(absPath);
+ rmSync(absPath, { force: true });
+ },
+ deleteResolvedContent: (_docName, absPath) => {
+ declaredDeletes.push(absPath);
unlinks.push(absPath);
rmSync(absPath, { force: true });
},
@@ -901,7 +909,7 @@ describe('ConflictAuthority resolve', () => {
expect(rig.io.gitCalls).toEqual([]);
});
- test("a reconcile 'delete' unlinks the file and clears the entry", async () => {
+ test("a reconcile 'delete' declares the unlink and clears the entry", async () => {
const rig = makeAuthority();
writeFileSync(join(projectDir, 'a.md'), 'markers\n', 'utf-8');
rig.authority.raise({
@@ -913,6 +921,7 @@ describe('ConflictAuthority resolve', () => {
await rig.authority.resolve('a.md', 'delete');
expect(existsSync(join(projectDir, 'a.md'))).toBe(false);
+ expect(rig.io.declaredDeletes).toEqual([join(projectDir, 'a.md')]);
expect(rig.authority.count()).toBe(0);
});
diff --git a/packages/server/src/conflict-authority.ts b/packages/server/src/conflict-authority.ts
index 03870ccbe..afade0f68 100644
--- a/packages/server/src/conflict-authority.ts
+++ b/packages/server/src/conflict-authority.ts
@@ -60,7 +60,8 @@ export type ConflictChange =
export interface ConflictIo {
gitRaw(args: string[]): Promise<string>;
writeProjectFileUntracked(absPath: string, bytes: string): void;
- unlinkProjectFile(absPath: string): void;
+ unlinkProjectFileUndeclared(absPath: string): void;
+ deleteResolvedContent(docName: string, absPath: string): void;
applyResolvedContent(docName: string, absPath: string, bytes: string): Promise<void>;
readLiveContent?(docName: string): string | null;
finalizeReconcileResolution?(
diff --git a/packages/server/src/conflict-resolution.test.ts b/packages/server/src/conflict-resolution.test.ts
index d034ba594..f903b24db 100644
--- a/packages/server/src/conflict-resolution.test.ts
+++ b/packages/server/src/conflict-resolution.test.ts
@@ -39,22 +39,30 @@ interface RecordingIo extends ConflictIo {
gitCalls: string[][];
applied: Array<{ docName: string; bytes: string }>;
unlinks: string[];
+ declaredDeletes: string[];
}
function makeIo(overrides: Partial<ConflictIo> = {}): RecordingIo {
const gitCalls: string[][] = [];
const applied: Array<{ docName: string; bytes: string }> = [];
const unlinks: string[] = [];
+ const declaredDeletes: string[] = [];
return {
gitCalls,
applied,
unlinks,
+ declaredDeletes,
gitRaw: async (args) => {
gitCalls.push(args);
return '';
},
writeProjectFileUntracked: (absPath, bytes) => writeFileSync(absPath, bytes, 'utf-8'),
- unlinkProjectFile: (absPath) => {
+ unlinkProjectFileUndeclared: (absPath) => {
+ unlinks.push(absPath);
+ rmSync(absPath, { force: true });
+ },
+ deleteResolvedContent: (_docName, absPath) => {
+ declaredDeletes.push(absPath);
unlinks.push(absPath);
rmSync(absPath, { force: true });
},
@@ -186,11 +194,13 @@ describe('resolveWorkingTree', () => {
expect(readFileSync(replacementTarget, 'utf-8')).toBe('REPLACEMENT\n');
});
- test("'delete' removes the file", async () => {
+ test("'delete' removes the file without declaring the removal", async () => {
writeFileSync(join(projectDir, 'a.md'), 'LOCAL\n', 'utf-8');
const io = makeIo();
await resolveWorkingTree(workingTree('a.md', 'sha'), 'delete', undefined, io, projectDir);
expect(existsSync(join(projectDir, 'a.md'))).toBe(false);
+ expect(io.unlinks).toEqual([join(projectDir, 'a.md')]);
+ expect(io.declaredDeletes).toEqual([]);
});
});
@@ -217,12 +227,13 @@ describe('resolveReconcile', () => {
expect(io.gitCalls).toEqual([]);
});
- test("'delete' unlinks the file and never touches git", async () => {
+ test("'delete' unlinks the file through the declared-removal member and never touches git", async () => {
const abs = join(projectDir, 'a.md');
writeFileSync(abs, 'markers\n', 'utf-8');
const io = makeIo();
await resolveReconcile('delete', undefined, 'a', abs, io);
expect(io.unlinks).toEqual([abs]);
+ expect(io.declaredDeletes).toEqual([abs]);
expect(io.gitCalls).toEqual([]);
});
diff --git a/packages/server/src/conflict-resolution.ts b/packages/server/src/conflict-resolution.ts
index d6f958cd3..17468ad85 100644
--- a/packages/server/src/conflict-resolution.ts
+++ b/packages/server/src/conflict-resolution.ts
@@ -40,7 +40,7 @@ export async function resolveMergeNative(
entry: Extract<Conflict, { kind: 'merge-native' }>,
strategy: ResolveStrategy,
content: string | undefined,
- io: ConflictIo,
+ io: Pick<ConflictIo, 'gitRaw' | 'writeProjectFileUntracked'>,
projectDir: string,
): Promise<void> {
switch (strategy) {
@@ -76,7 +76,7 @@ export async function resolveWorkingTree(
entry: Extract<Conflict, { kind: 'working-tree' }>,
strategy: ResolveStrategy,
content: string | undefined,
- io: ConflictIo,
+ io: Pick<ConflictIo, 'gitRaw' | 'writeProjectFileUntracked' | 'unlinkProjectFileUndeclared'>,
projectDir: string,
): Promise<void> {
const requestedPath = resolve(projectDir, entry.file);
@@ -99,8 +99,10 @@ export async function resolveWorkingTree(
return;
}
+ /* WARN: this unlink is undeclared because nothing fallible runs between it and
+ `unindex`; inserting one requires `deleteResolvedContent` and the teardown with it. */
case 'delete':
- if (existsSync(requestedPath)) io.unlinkProjectFile(requestedPath);
+ if (existsSync(requestedPath)) io.unlinkProjectFileUndeclared(requestedPath);
return;
default: {
@@ -115,7 +117,7 @@ export async function resolveReconcile(
resolvedBytes: string | undefined,
docName: string,
absPath: string,
- io: ConflictIo,
+ io: Pick<ConflictIo, 'applyResolvedContent' | 'deleteResolvedContent'>,
): Promise<void> {
switch (strategy) {
case 'mine':
@@ -126,7 +128,7 @@ export async function resolveReconcile(
}
case 'delete':
- if (existsSync(absPath)) io.unlinkProjectFile(absPath);
+ if (existsSync(absPath)) io.deleteResolvedContent(docName, absPath);
return;
default: {
@@ -141,7 +143,9 @@ export type CommitMergeResult =
| { ok: false; unmerged: string[]; cause: unknown }
| { ok: false; unmerged: null; probeError: unknown; cause: unknown };
-export async function commitMergeIfEmpty(io: ConflictIo): Promise<CommitMergeResult> {
+export async function commitMergeIfEmpty(
+ io: Pick<ConflictIo, 'gitRaw'>,
+): Promise<CommitMergeResult> {
try {
await io.gitRaw(['commit', '--no-edit']);
return { ok: true };
diff --git a/packages/server/src/document-durability-state.ts b/packages/server/src/document-durability-state.ts
index 6b15326dd..0e794de1a 100644
--- a/packages/server/src/document-durability-state.ts
+++ b/packages/server/src/document-durability-state.ts
@@ -470,6 +470,16 @@ export class DocumentDurabilityState {
this.mutateDocument(docName, () => conflicts?.delete(docName));
}
+ restoreStaleExternalWrite(conflict: StaleExternalWriteConflict, branch: string): void {
+ let conflicts = this.staleExternalWritesByBranch.get(branch);
+ if (!conflicts) {
+ conflicts = new Map();
+ this.staleExternalWritesByBranch.set(branch, conflicts);
+ }
+ if (conflicts.has(conflict.docName)) return;
+ this.mutateDocument(conflict.docName, () => conflicts.set(conflict.docName, conflict));
+ }
+
private mutateDocument<T>(docName: string, mutate: () => T, persist = true): T {
const bases = this.reconciledBaseByBranch.get(this.activeBranch) ?? new Map<string, string>();
const versions =
diff --git a/packages/server/src/file-watcher-removal-syscall.test.ts b/packages/server/src/file-watcher-removal-syscall.test.ts
new file mode 100644
index 000000000..5160c05b4
--- /dev/null
+++ b/packages/server/src/file-watcher-removal-syscall.test.ts
@@ -0,0 +1,39 @@
+import { realpathSync } from 'node:fs';
+import { mkdtemp, rm } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { resolve } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import { isSelfRemoval, registerRemoval, removalTracker } from './file-watcher.ts';
+
+vi.mock('node:fs', async (importOriginal) => {
+ const fs = await importOriginal<typeof import('node:fs')>();
+ return { ...fs, realpathSync: vi.fn(fs.realpathSync) };
+});
+
+const mockedRealpathSync = vi.mocked(realpathSync);
+
+let removalDir: string;
+
+beforeEach(async () => {
+ removalTracker.clear();
+ removalDir = await mkdtemp(resolve(tmpdir(), 'ok-removal-syscall-'));
+ mockedRealpathSync.mockClear();
+});
+
+afterEach(async () => {
+ removalTracker.clear();
+ await rm(removalDir, { recursive: true, force: true });
+});
+
+describe('isSelfRemoval path resolution', () => {
+ test('resolves no path when nothing is declared, and one once a declaration is live', () => {
+ expect(isSelfRemoval(resolve(removalDir, 'undeclared.md'))).toBe(false);
+ expect(mockedRealpathSync).not.toHaveBeenCalled();
+
+ registerRemoval(resolve(removalDir, 'declared.md'));
+ mockedRealpathSync.mockClear();
+
+ expect(isSelfRemoval(resolve(removalDir, 'undeclared.md'))).toBe(false);
+ expect(mockedRealpathSync).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/packages/server/src/file-watcher.test.ts b/packages/server/src/file-watcher.test.ts
index 15c206a66..620f52377 100644
--- a/packages/server/src/file-watcher.test.ts
+++ b/packages/server/src/file-watcher.test.ts
@@ -1,8 +1,8 @@
-import { mkdirSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
+import { mkdirSync, realpathSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { resolve } from 'node:path';
-import { afterEach, beforeEach, describe, expect, test } from 'vitest';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { createContentFilter } from './content-filter.ts';
import type { DiskEvent } from './file-watcher';
import {
@@ -10,17 +10,22 @@ import {
contentHash,
evictStaleTrackerEntries,
handleRawEvents,
+ isSelfRemoval,
isSelfWrite,
lastKnownHash,
pathToDocName,
reconcileFileIndexAfterFilterRebuild,
+ registerRemoval,
registerWrite,
+ removalTracker,
+ retractRemoval,
startWatcher,
toParcelIgnorePaths,
updateFileIndex,
updateLastKnownHash,
writeTracker,
} from './file-watcher';
+import { getLogger } from './logger.ts';
describe('writeTracker', () => {
beforeEach(() => {
@@ -185,6 +190,254 @@ describe('isSelfWrite', () => {
});
});
+describe('isSelfRemoval', () => {
+ let removalDir: string;
+
+ beforeEach(async () => {
+ removalTracker.clear();
+ removalDir = await mkdtemp(resolve(tmpdir(), 'ok-removal-test-'));
+ });
+
+ afterEach(async () => {
+ await rm(removalDir, { recursive: true, force: true });
+ });
+
+ test('returns true and consumes the entry for a declared removal', () => {
+ const filePath = resolve(realpathSync(removalDir), 'declared.md');
+ writeFileSync(filePath, 'body\n', 'utf-8');
+ registerRemoval(filePath);
+
+ expect(isSelfRemoval(filePath)).toBe(true);
+ expect(isSelfRemoval(filePath)).toBe(false);
+ expect(removalTracker.has(filePath)).toBe(false);
+ });
+
+ test('returns false for a path that was never declared', () => {
+ expect(isSelfRemoval(resolve(realpathSync(removalDir), 'undeclared.md'))).toBe(false);
+ });
+
+ test('keys an aliased doc at the alias the watcher reports, not at its target', () => {
+ const canonicalDir = realpathSync(removalDir);
+ const target = resolve(canonicalDir, 'alias-target.md');
+ const alias = resolve(canonicalDir, 'alias.md');
+ writeFileSync(target, 'body\n', 'utf-8');
+ symlinkSync(target, alias);
+ registerRemoval(alias);
+
+ expect(isSelfRemoval(target)).toBe(false);
+ expect(isSelfRemoval(alias)).toBe(true);
+ });
+
+ test('canonicalises the declared directory so an uncanonical parent still matches', () => {
+ const filePath = resolve(removalDir, 'uncanonical.md');
+ writeFileSync(filePath, 'body\n', 'utf-8');
+ registerRemoval(filePath);
+
+ expect(isSelfRemoval(resolve(realpathSync(removalDir), 'uncanonical.md'))).toBe(true);
+ });
+
+ test('returns false once the declaration has aged past the TTL', () => {
+ const filePath = resolve(realpathSync(removalDir), 'stale.md');
+ removalTracker.set(filePath, Date.now() - 31_000);
+
+ expect(isSelfRemoval(filePath)).toBe(false);
+ expect(removalTracker.has(filePath)).toBe(false);
+ });
+
+ test('evicts a declaration older than the TTL and keeps a fresh one', () => {
+ removalTracker.set('/content/stale-removal.md', Date.now() - 31_000);
+ removalTracker.set('/content/fresh-removal.md', Date.now() - 2_000);
+
+ evictStaleTrackerEntries();
+ expect(removalTracker.has('/content/stale-removal.md')).toBe(false);
+ expect(removalTracker.has('/content/fresh-removal.md')).toBe(true);
+ });
+
+ test('a declaration still suppresses at an age the watcher is known to deliver at', () => {
+ const filePath = resolve(realpathSync(removalDir), 'late-delivery.md');
+ removalTracker.set(filePath, Date.now() - 12_000);
+
+ expect(isSelfRemoval(filePath)).toBe(true);
+ });
+
+ test('the eviction sweep keeps a removal declaration the write sweep would drop', () => {
+ removalTracker.set('/content/late-removal.md', Date.now() - 12_000);
+ writeTracker.set('/content/late-write.md', [{ hash: 'h', timestamp: Date.now() - 12_000 }]);
+
+ evictStaleTrackerEntries();
+ expect(removalTracker.has('/content/late-removal.md')).toBe(true);
+ expect(writeTracker.has('/content/late-write.md')).toBe(false);
+ });
+
+ test('a retracted declaration no longer consumes a deletion at that path', () => {
+ const filePath = resolve(realpathSync(removalDir), 'retracted.md');
+ writeFileSync(filePath, 'body\n', 'utf-8');
+ const declaration = registerRemoval(filePath);
+
+ retractRemoval(declaration);
+
+ expect(isSelfRemoval(filePath)).toBe(false);
+ });
+
+ test('retracting an uncanonical declaration removes the key the watcher would hit', () => {
+ const filePath = resolve(removalDir, 'retracted-uncanonical.md');
+ writeFileSync(filePath, 'body\n', 'utf-8');
+
+ retractRemoval(registerRemoval(filePath));
+
+ expect(isSelfRemoval(resolve(realpathSync(removalDir), 'retracted-uncanonical.md'))).toBe(
+ false,
+ );
+ });
+
+ test('an unresolvable removal key is surfaced with its errno instead of being swallowed', () => {
+ const canonicalDir = realpathSync(removalDir);
+ const loopA = resolve(canonicalDir, 'loop-a');
+ const loopB = resolve(canonicalDir, 'loop-b');
+ symlinkSync(loopB, loopA);
+ symlinkSync(loopA, loopB);
+ const underLoop = resolve(loopA, 'note.md');
+ const warnSpy = vi.spyOn(getLogger('file-watcher'), 'warn');
+ warnSpy.mockClear();
+
+ try {
+ registerRemoval(resolve(canonicalDir, 'unrelated.md'));
+
+ expect(isSelfRemoval(underLoop)).toBe(false);
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.objectContaining({ path: underLoop, code: 'ELOOP' }),
+ expect.stringContaining('ELOOP'),
+ );
+ } finally {
+ warnSpy.mockRestore();
+ }
+ });
+
+ test('a declaration keyed by the raw fallback is not matched once the directory resolves again', () => {
+ const canonicalDir = realpathSync(removalDir);
+ const loopA = resolve(canonicalDir, 'loop-a');
+ const loopB = resolve(canonicalDir, 'loop-b');
+ symlinkSync(loopB, loopA);
+ symlinkSync(loopA, loopB);
+ const underLoop = resolve(loopA, 'note.md');
+
+ registerRemoval(underLoop);
+ expect(removalTracker.has(underLoop)).toBe(true);
+
+ const realTarget = resolve(canonicalDir, 'real-target');
+ mkdirSync(realTarget);
+ unlinkSync(loopA);
+ symlinkSync(realTarget, loopA);
+
+ expect(isSelfRemoval(underLoop)).toBe(false);
+ });
+
+ test('a declared removal is recognised when the watcher reports it through an uncanonical parent', async () => {
+ const root = realpathSync(removalDir);
+ const realContent = resolve(root, 'content');
+ mkdirSync(realContent);
+ const aliasContent = resolve(root, 'alias');
+ symlinkSync(realContent, aliasContent);
+ const filePath = resolve(realContent, 'note.md');
+ writeFileSync(filePath, '# Note\n', 'utf-8');
+ registerRemoval(filePath);
+ await rm(filePath);
+
+ const dispatched: DiskEvent[] = [];
+ await handleRawEvents(
+ [{ type: 'delete', path: resolve(aliasContent, 'note.md') }],
+ aliasContent,
+ undefined,
+ new Map(),
+ new Map(),
+ async (event) => {
+ dispatched.push(event);
+ },
+ );
+
+ expect(dispatched).toEqual([]);
+ });
+
+ test('a create batch already in flight cannot void a declaration made after it arrived', async () => {
+ const contentDir = realpathSync(removalDir);
+ const filePath = resolve(contentDir, 'in-flight.md');
+ writeFileSync(filePath, '# In flight\n', 'utf-8');
+
+ const inFlight = handleRawEvents(
+ [{ type: 'create', path: filePath }],
+ contentDir,
+ undefined,
+ new Map(),
+ new Map(),
+ async () => {},
+ );
+ registerRemoval(filePath);
+ await inFlight;
+ unlinkSync(filePath);
+
+ const afterOwnRemoval: DiskEvent[] = [];
+ await handleRawEvents(
+ [{ type: 'delete', path: filePath }],
+ contentDir,
+ undefined,
+ new Map(),
+ new Map(),
+ async (event) => {
+ afterOwnRemoval.push(event);
+ },
+ );
+
+ expect(afterOwnRemoval).toEqual([]);
+ });
+
+ test('a declaration whose delete is paired into a rename cannot be spent on a later deletion', async () => {
+ const contentDir = realpathSync(removalDir);
+ const source = resolve(contentDir, 'paired-source.md');
+ const copy = resolve(contentDir, 'paired-copy.md');
+ const body = '# Paired\n\nBody the copy also carries.\n';
+ lastKnownHash.clear();
+ writeFileSync(source, body, 'utf-8');
+ updateLastKnownHash(source, contentHash(body));
+
+ registerRemoval(source);
+ unlinkSync(source);
+ writeFileSync(copy, body, 'utf-8');
+
+ const paired: DiskEvent[] = [];
+ await handleRawEvents(
+ [
+ { type: 'delete', path: source },
+ { type: 'create', path: copy },
+ ],
+ contentDir,
+ undefined,
+ new Map(),
+ new Map(),
+ async (event) => {
+ paired.push(event);
+ },
+ );
+ expect(paired.map((event) => event.kind)).toEqual(['rename']);
+
+ writeFileSync(source, '# Paired\n\nBody the user typed back.\n', 'utf-8');
+ unlinkSync(source);
+
+ const later: DiskEvent[] = [];
+ await handleRawEvents(
+ [{ type: 'delete', path: source }],
+ contentDir,
+ undefined,
+ new Map(),
+ new Map(),
+ async (event) => {
+ later.push(event);
+ },
+ );
+
+ expect(later.map((event) => event.kind)).toEqual(['delete']);
+ });
+});
+
describe('classifyEvents', () => {
let tmpDir: string;
let contentDir: string;
diff --git a/packages/server/src/file-watcher.ts b/packages/server/src/file-watcher.ts
index d3a9c6999..0b131a21c 100644
--- a/packages/server/src/file-watcher.ts
+++ b/packages/server/src/file-watcher.ts
@@ -8,7 +8,7 @@ import {
statSync,
} from 'node:fs';
import { lstat, readdir, readFile, realpath, stat } from 'node:fs/promises';
-import { dirname, extname, join, relative } from 'node:path';
+import { basename, dirname, extname, join, relative } from 'node:path';
import { LINKABLE_ASSET_EXTENSIONS } from '@inkeep/open-knowledge-core';
import { isConfigDoc, isReservedForUserTree, isSystemDoc } from './cc1-broadcast.ts';
import { type ContentFilter, WATCHER_STRUCTURAL_IGNORE_DIRS } from './content-filter.ts';
@@ -165,6 +165,7 @@ export interface WatcherHandle {
export const writeTracker = new Map<string, Array<{ hash: string; timestamp: number }>>();
const WRITE_TRACKER_TTL_MS = 10_000;
+const REMOVAL_TRACKER_TTL_MS = 30_000;
export function registerWrite(filePath: string, hash: string): void {
const queue = writeTracker.get(filePath) ?? [];
@@ -172,6 +173,57 @@ export function registerWrite(filePath: string, hash: string): void {
writeTracker.set(filePath, queue);
}
+export const removalTracker = new Map<string, number>();
+
+function removalKey(filePath: string): string {
+ try {
+ return join(realpathSync(dirname(filePath)), basename(filePath));
+ } catch (e) {
+ const code = errnoCode(e);
+ if (code !== 'ENOENT') {
+ log.warn(
+ { path: filePath, code },
+ `realpathSync failed for the removal key of ${filePath} (${code})`,
+ );
+ }
+ return filePath;
+ }
+}
+
+export type RemovalDeclaration = string & { readonly __brand: 'RemovalDeclaration' };
+
+type Assert<T extends true> = T;
+type _RawStringIsNotARemovalDeclaration = Assert<string extends RemovalDeclaration ? false : true>;
+
+/* STOP: call this before the unlink, retract it if the unlink throws, and never resolve the
+ leaf; the watcher reports a removed alias at the alias, not at the target it pointed to. */
+export function registerRemoval(filePath: string): RemovalDeclaration {
+ const key = removalKey(filePath);
+ removalTracker.set(key, Date.now());
+ return key as RemovalDeclaration;
+}
+
+export function retractRemoval(declaration: RemovalDeclaration): void {
+ removalTracker.delete(declaration);
+}
+
+function voidRemoval(filePath: string, declaredBeforeBatch: ReadonlySet<string>): void {
+ if (declaredBeforeBatch.size === 0) return;
+ const key = removalKey(filePath);
+ if (!declaredBeforeBatch.has(key)) return;
+ removalTracker.delete(key);
+}
+
+/* STOP: return false on every ambiguous input; a true discards a real disk deletion. */
+export function isSelfRemoval(filePath: string): boolean {
+ if (removalTracker.size === 0) return false;
+ const key = removalKey(filePath);
+ const timestamp = removalTracker.get(key);
+ if (timestamp === undefined) return false;
+ removalTracker.delete(key);
+ return Date.now() - timestamp <= REMOVAL_TRACKER_TTL_MS;
+}
+
export function evictStaleTrackerEntries(): void {
const now = Date.now();
for (const [path, queue] of writeTracker) {
@@ -182,6 +234,9 @@ export function evictStaleTrackerEntries(): void {
writeTracker.set(path, fresh);
}
}
+ for (const [path, timestamp] of removalTracker) {
+ if (now - timestamp > REMOVAL_TRACKER_TTL_MS) removalTracker.delete(path);
+ }
}
type WatcherDropReason =
@@ -191,7 +246,11 @@ type WatcherDropReason =
| 'reserved-doc'
| 'stat-failed';
-type WatcherDecision = 'dispatched' | 'self-write-skip' | `drop-${WatcherDropReason}`;
+type WatcherDecision =
+ | 'dispatched'
+ | 'self-write-skip'
+ | 'self-removal-skip'
+ | `drop-${WatcherDropReason}`;
export interface WatcherDecisionRecord {
ts: number;
@@ -207,6 +266,7 @@ const watcherDecisionRing: WatcherDecisionRecord[] = [];
const watcherDropCounts = new Map<WatcherDropReason, number>();
let watcherDispatchedCount = 0;
let watcherSelfWriteSkipCount = 0;
+let watcherSelfRemovalSkipCount = 0;
let watcherDropsSinceLastSummary = 0;
function recordWatcherDecision(decision: WatcherDecision, kind: string, rawPath: string): void {
@@ -228,6 +288,10 @@ function recordWatcherDecision(decision: WatcherDecision, kind: string, rawPath:
watcherSelfWriteSkipCount++;
return;
}
+ if (decision === 'self-removal-skip') {
+ watcherSelfRemovalSkipCount++;
+ return;
+ }
const reason = decision.slice('drop-'.length) as WatcherDropReason;
watcherDropCounts.set(reason, (watcherDropCounts.get(reason) ?? 0) + 1);
watcherDropsSinceLastSummary++;
@@ -254,6 +318,7 @@ export function logWatcherDropSummary(): void {
dropTotals,
dispatched: watcherDispatchedCount,
selfWriteSkips: watcherSelfWriteSkipCount,
+ selfRemovalSkips: watcherSelfRemovalSkipCount,
},
`[file-watcher] drop summary: ${droppedSinceLastSummary} event(s) dropped since last summary`,
);
@@ -264,6 +329,7 @@ export function resetWatcherDecisionDiagnostics(): void {
watcherDropCounts.clear();
watcherDispatchedCount = 0;
watcherSelfWriteSkipCount = 0;
+ watcherSelfRemovalSkipCount = 0;
watcherDropsSinceLastSummary = 0;
}
@@ -1055,6 +1121,7 @@ export async function handleRawEvents(
onDiskEvent: (event: DiskEvent) => Promise<void>,
aliasMap?: Map<string, string>,
): Promise<void> {
+ const declaredBeforeBatch: ReadonlySet<string> = new Set(removalTracker.keys());
const safeEvents = rawEvents.filter((e) => {
if (!eventEscapesContentDir(e.path, contentDir)) return true;
recordWatcherDecision('drop-symlink-escape', e.type, e.path);
@@ -1157,6 +1224,7 @@ export async function handleRawEvents(
}
}
isSelf = isSelfWrite(checkPath, hash);
+ voidRemoval(event.path, declaredBeforeBatch);
} else if (event.kind === 'rename') {
const hash = contentHash(event.content);
let checkPath = event.newPath;
@@ -1172,6 +1240,10 @@ export async function handleRawEvents(
}
}
isSelf = isSelfWrite(checkPath, hash);
+ voidRemoval(event.oldPath, declaredBeforeBatch);
+ voidRemoval(event.newPath, declaredBeforeBatch);
+ } else {
+ isSelf = isSelfRemoval(event.path);
}
const dispatchedEvent =
@@ -1202,17 +1274,18 @@ export async function handleRawEvents(
}
if (isSelf) {
+ const selfKind = event.kind === 'delete' ? 'self-removal' : 'self-write';
log.debug(
{
kind: event.kind,
path: event.kind === 'rename' ? event.newPath : event.path,
self: true,
},
- `[file-watcher] Skipped self-write: ${event.kind}`,
+ `[file-watcher] Skipped ${selfKind}: ${event.kind}`,
);
_fileWatcherEventsCounter().add(1, { 'disk.kind': event.kind, self: true });
recordWatcherDecision(
- 'self-write-skip',
+ `${selfKind}-skip`,
event.kind,
event.kind === 'rename' ? event.newPath : event.path,
);
@@ -1640,6 +1713,7 @@ export async function startWatcher(
clearInterval(evictionInterval);
clearInterval(dropSummaryInterval);
writeTracker.clear();
+ removalTracker.clear();
lastKnownHash.clear();
resetWatcherDecisionDiagnostics();
return originalUnsubscribe();
diff --git a/packages/server/src/git-pathspec-name-invariance.test.ts b/packages/server/src/git-pathspec-name-invariance.test.ts
index 8ccce6288..b1b9e01ef 100644
--- a/packages/server/src/git-pathspec-name-invariance.test.ts
+++ b/packages/server/src/git-pathspec-name-invariance.test.ts
@@ -34,7 +34,8 @@ function conflictAuthorityFor(projectDir: string): ConflictAuthority {
io: {
gitRaw: (args) => simpleGit(projectDir).raw(args),
writeProjectFileUntracked: (absPath, bytes) => writeFileSync(absPath, bytes, 'utf-8'),
- unlinkProjectFile: (absPath) => rmSync(absPath, { force: true }),
+ unlinkProjectFileUndeclared: (absPath) => rmSync(absPath, { force: true }),
+ deleteResolvedContent: (_docName, absPath) => rmSync(absPath, { force: true }),
applyResolvedContent: async (_docName, absPath, bytes) => {
writeFileSync(absPath, bytes, 'utf-8');
},
diff --git a/packages/server/src/http/error-response.test.ts b/packages/server/src/http/error-response.test.ts
index b7f60bd02..b858d41f1 100644
--- a/packages/server/src/http/error-response.test.ts
+++ b/packages/server/src/http/error-response.test.ts
@@ -279,6 +279,56 @@ describe('errorResponse — defense-in-depth branches', () => {
expect(body.colliding).toEqual([{ existing: 'a.md', incoming: 'A.md', to: 'A.md' }]);
});
+ test('extension members reach the structured log line under one key they cannot escape', () => {
+ const log = loggerFactory.getLogger('http');
+ const errorSpy = vi.spyOn(log, 'error');
+ errorSpy.mockClear();
+
+ const { res } = makeMockRes();
+ errorResponse(res, 500, 'urn:ok:error:internal-server-error', 'Failed to resolve conflict.', {
+ handler: 'sync-resolve-conflict',
+ detail: 'EISDIR',
+ extensions: {
+ file: 'notes/a.md',
+ strategy: 'delete',
+ handler: 'hostile-handler',
+ event: 'forged.event',
+ requestId: 'forged-request',
+ err: 'forged-err',
+ level: 'debug',
+ time: 0,
+ name: 'forged-logger',
+ trace_id: 'FORGED-TRACE',
+ span_id: 'FORGED-SPAN',
+ msg: 'forged message',
+ pid: -1,
+ hostname: 'forged-host',
+ },
+ });
+
+ const line = errorSpy.mock.calls.find(
+ ([arg]) => isObjLike(arg) && arg.event === 'api.error' && arg.status === 500,
+ )?.[0] as Record<string, unknown>;
+ expect(line).toBeDefined();
+ expect(line.extensions).toMatchObject({ file: 'notes/a.md', strategy: 'delete' });
+ expect(line.handler).toBe('sync-resolve-conflict');
+ expect(line.event).toBe('api.error');
+ expect(line.detail).toBe('EISDIR');
+ expect(Object.keys(line).sort()).toEqual([
+ 'detail',
+ 'err',
+ 'event',
+ 'extensions',
+ 'handler',
+ 'instance',
+ 'requestId',
+ 'status',
+ 'type',
+ ]);
+
+ errorSpy.mockRestore();
+ });
+
test('the :(literal) pathspec marker is scrubbed from title and detail', () => {
const { res, endCalls } = makeMockRes();
errorResponse(
diff --git a/packages/server/src/http/error-response.ts b/packages/server/src/http/error-response.ts
index d5403c2b8..d0da1a17c 100644
--- a/packages/server/src/http/error-response.ts
+++ b/packages/server/src/http/error-response.ts
@@ -150,6 +150,7 @@ export function errorResponse(
handler: options.handler,
detail: options.detail,
err: options.cause,
+ ...(options.extensions ? { extensions: options.extensions } : {}),
},
title,
);
diff --git a/packages/server/src/http/sync-routes.ts b/packages/server/src/http/sync-routes.ts
index 466858317..2384345fe 100644
--- a/packages/server/src/http/sync-routes.ts
+++ b/packages/server/src/http/sync-routes.ts
@@ -355,6 +355,7 @@ export function createSyncRoutes(deps: SyncRouteDeps): ApiRouteGroup {
handler: 'sync-resolve-conflict',
cause: e,
detail,
+ extensions: { file, strategy },
},
);
} finally {
diff --git a/packages/server/src/server-factory.test.ts b/packages/server/src/server-factory.test.ts
index 60367ca3d..ff0e52cc6 100644
--- a/packages/server/src/server-factory.test.ts
+++ b/packages/server/src/server-factory.test.ts
@@ -5450,6 +5450,18 @@ function createReconcileServer(rig: ReconcileRig): ServerInstance {
});
}
+function persistedStaleExternalWrite(
+ projectDir: string,
+ docName: string,
+): Record<string, unknown> | undefined {
+ const snapshot = join(projectDir, '.ok', LOCAL_DIR, 'stale-external-writes.json');
+ if (!existsSync(snapshot)) return undefined;
+ const parsed = JSON.parse(readFileSync(snapshot, 'utf-8')) as {
+ branches: Record<string, { docName: string; staleExternalWrite?: Record<string, unknown> }[]>;
+ };
+ return parsed.branches.main?.find((entry) => entry.docName === docName)?.staleExternalWrite;
+}
+
async function expectAbsentDuring(
read: () => Promise<string[]>,
absent: string,
@@ -5577,6 +5589,461 @@ describe('createServer() — disk-event reconcile with an absent reconciled base
}
}, 30_000);
+ test('a delete resolution rescues unflushed live edits now that the suppressed self-delete no longer routes that teardown through the watcher', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-rescue-');
+ const docName = 'resolve-delete-rescue-target';
+ const diskContent = '# Resolve delete rescue target\n\nOn-disk body.\n';
+ writeFileSync(join(rig.tmpDir, `${docName}.md`), diskContent, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(diskContent),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ await conn.transact((doc) => {
+ const xmlFragment = doc.getXmlFragment('default');
+ const paragraph = new Y.XmlElement('paragraph');
+ paragraph.insert(0, [new Y.XmlText('Unflushed paragraph only in the editor doc.')]);
+ xmlFragment.insert(0, [paragraph]);
+ });
+ await vi.waitFor(
+ () =>
+ expect(server.hocuspocus.documents.get(docName)?.getText('source').toString()).toContain(
+ 'Unflushed paragraph only in the editor doc.',
+ ),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: `${docName}.md`,
+ reason: 'stale-external-write',
+ stages: { base: diskContent, ours: diskContent, theirs: diskContent },
+ });
+ await server.conflicts.resolve(`${docName}.md`, 'delete');
+
+ expect(existsSync(join(rig.tmpDir, `${docName}.md`))).toBe(false);
+ let rescueSha = '';
+ await vi.waitFor(
+ async () => {
+ const rescues = await listRescueCheckpoints(rig.shadow, 'main');
+ const rescue = rescues.find((r) => r.docName === docName);
+ expect(rescue).toBeDefined();
+ rescueSha = rescue?.sha ?? '';
+ },
+ { timeout: 10_000, interval: 50 },
+ );
+ const resolveRescueBody = (
+ await shadowGit(rig.shadow).raw('log', '-1', '--format=%B', rescueSha)
+ ).trim();
+ expect(parseCheckpoint(resolveRescueBody)?.kind).toBe('external-change-rescue');
+ const rescuedContents = await shadowGit(rig.shadow).raw('show', `${rescueSha}:${docName}`);
+ expect(rescuedContents).toContain('Unflushed paragraph only in the editor doc.');
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution that fails after its rescue decision leaves the base the retry re-reads', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-retry-base-');
+ const docName = 'resolve-delete-retry-base';
+ const diskContent = '# Resolve delete retry base\n\nOn-disk body.\n';
+ writeFileSync(join(rig.tmpDir, `${docName}.md`), diskContent, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(diskContent),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ throw new Error('injected unload failure');
+ };
+
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: `${docName}.md`,
+ reason: 'stale-external-write',
+ stages: { base: diskContent, ours: diskContent, theirs: diskContent },
+ });
+ await expect(server.conflicts.resolve(`${docName}.md`, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+
+ expect(server.durabilityState.getReconciledBase(docName)).toBe(diskContent);
+
+ await server.conflicts.resolve(`${docName}.md`, 'delete');
+ await expectAbsentDuring(
+ async () => (await listRescueCheckpoints(rig.shadow, 'main')).map((r) => r.docName),
+ docName,
+ );
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution whose file is already gone leaves the directory count its remaining markdown justifies', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-dircount-');
+ const notesDir = join(rig.tmpDir, 'notes');
+ mkdirSync(notesDir);
+ writeFileSync(join(notesDir, 'keep.md'), '# Keep\n', 'utf-8');
+ writeFileSync(join(notesDir, 'sibling.png'), 'png-bytes', 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: 'notes/gone.md',
+ reason: 'stale-external-write',
+ stages: { base: '', ours: '', theirs: '' },
+ });
+ await server.conflicts.resolve('notes/gone.md', 'delete');
+
+ expect(existsSync(join(notesDir, 'keep.md'))).toBe(true);
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution that unlinks then fails, retried with mine, leaves the directory count matching the file it put back', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-then-mine-dircount-');
+ const notesDir = join(rig.tmpDir, 'notes');
+ mkdirSync(notesDir);
+ const docName = 'notes/delete-then-mine';
+ const diskContent = '# Delete then mine\n\nOn-disk body.\n';
+ writeFileSync(join(notesDir, 'delete-then-mine.md'), diskContent, 'utf-8');
+ writeFileSync(join(notesDir, 'sibling.png'), 'png-bytes', 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(diskContent),
+ { timeout: 5_000, interval: 25 },
+ );
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ throw new Error('injected unload failure');
+ };
+
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: `${docName}.md`,
+ reason: 'stale-external-write',
+ stages: { base: diskContent, ours: diskContent, theirs: diskContent },
+ });
+ await expect(server.conflicts.resolve(`${docName}.md`, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+ expect(existsSync(join(notesDir, 'delete-then-mine.md'))).toBe(false);
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(true);
+
+ await server.conflicts.resolve(`${docName}.md`, 'mine');
+
+ expect(existsSync(join(notesDir, 'delete-then-mine.md'))).toBe(true);
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution that fails at unload leaves the lifecycle it found, so a later resolution clears the document', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-lifecycle-restore-');
+ const docName = 'resolve-delete-lifecycle-restore';
+ const diskContent = '# Resolve delete lifecycle restore\n\nOn-disk body.\n';
+ writeFileSync(join(rig.tmpDir, `${docName}.md`), diskContent, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(diskContent),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ throw new Error('injected unload failure');
+ };
+
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: `${docName}.md`,
+ reason: 'stale-external-write',
+ stages: { base: diskContent, ours: diskContent, theirs: diskContent },
+ });
+ await expect(server.conflicts.resolve(`${docName}.md`, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+
+ const afterFailure = server.hocuspocus.documents.get(docName);
+ expect(afterFailure).toBeDefined();
+ if (!afterFailure) throw new Error('expected the document to stay resident');
+ expect(server.conflicts.lifecycleOf(afterFailure, docName)).toEqual({
+ status: 'conflict',
+ kind: 'reconcile',
+ reason: 'stale-external-write',
+ });
+
+ await server.conflicts.resolve(`${docName}.md`, 'mine');
+
+ const afterRetry = server.hocuspocus.documents.get(docName);
+ expect(afterRetry).toBeDefined();
+ if (!afterRetry) throw new Error('expected the document to stay resident');
+ expect(server.conflicts.lifecycleOf(afterRetry, docName)).toBeNull();
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution that fails at unload leaves the stale-external-write record its rehydration re-reads', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-durable-restore-');
+ const docName = 'resolve-delete-durable-restore';
+ const file = `${docName}.md`;
+ const retained = '# Resolve delete durable restore\n\nAcknowledged body.\n';
+ const diskContent = '# Resolve delete durable restore\n\nRejected older save.\n';
+ writeFileSync(join(rig.tmpDir, file), retained, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(retained),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ const raised = server.durabilityState.recordStaleExternalWrite(
+ docName,
+ diskContent,
+ retained,
+ );
+ expect(server.durabilityState.listStaleExternalWrites().map((c) => c.docName)).toContain(
+ docName,
+ );
+ expect(server.conflicts.findByFile(file)).toMatchObject({
+ kind: 'reconcile',
+ reason: 'stale-external-write',
+ });
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ throw new Error('injected unload failure');
+ };
+
+ await expect(server.conflicts.resolve(file, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+
+ expect(server.durabilityState.getStaleExternalWrite(docName)).toMatchObject({
+ docName,
+ diskContent,
+ retainedContent: retained,
+ detectedAt: raised.detectedAt,
+ });
+ expect(server.durabilityState.listStaleExternalWrites().map((c) => c.docName)).toContain(
+ docName,
+ );
+ expect(persistedStaleExternalWrite(rig.tmpDir, docName)).toMatchObject({
+ docName,
+ diskContent,
+ detectedAt: raised.detectedAt,
+ });
+
+ await server.conflicts.resolve(file, 'mine');
+
+ expect(server.durabilityState.listStaleExternalWrites().map((c) => c.docName)).not.toContain(
+ docName,
+ );
+ expect(persistedStaleExternalWrite(rig.tmpDir, docName)).toBeUndefined();
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution whose teardown observed a newer external write rolls back to that newer record', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-durable-newer-');
+ const docName = 'resolve-delete-durable-newer';
+ const file = `${docName}.md`;
+ const retained = '# Resolve delete durable newer\n\nAcknowledged body.\n';
+ const olderDisk = '# Resolve delete durable newer\n\nFirst rejected save.\n';
+ const newerDisk = '# Resolve delete durable newer\n\nSecond rejected save.\n';
+ writeFileSync(join(rig.tmpDir, file), retained, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(retained),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ server.durabilityState.recordStaleExternalWrite(docName, olderDisk, retained);
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ server.durabilityState.recordStaleExternalWrite(docName, newerDisk, retained);
+ throw new Error('injected unload failure');
+ };
+
+ await expect(server.conflicts.resolve(file, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+
+ expect(server.durabilityState.getStaleExternalWrite(docName)).toMatchObject({
+ docName,
+ diskContent: newerDisk,
+ });
+ expect(persistedStaleExternalWrite(rig.tmpDir, docName)).toMatchObject({
+ docName,
+ diskContent: newerDisk,
+ });
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a delete resolution that fails at unload after a checkout restores the record to the branch it was cleared from', async () => {
+ rig = await setupReconcileRig('ok-resolve-delete-branch-moved-');
+ const docName = 'resolve-delete-branch-moved';
+ const file = `${docName}.md`;
+ const retained = '# Resolve delete branch moved\n\nAcknowledged body.\n';
+ const diskContent = '# Resolve delete branch moved\n\nRejected older save.\n';
+ writeFileSync(join(rig.tmpDir, file), retained, 'utf-8');
+
+ const server = createReconcileServer(rig);
+ try {
+ await server.ready;
+ const conn = await server.hocuspocus.openDirectConnection(docName);
+ await vi.waitFor(
+ () => expect(server.durabilityState.getReconciledBase(docName)).toBe(retained),
+ { timeout: 5_000, interval: 25 },
+ );
+
+ const raised = server.durabilityState.recordStaleExternalWrite(
+ docName,
+ diskContent,
+ retained,
+ );
+ expect(server.conflicts.findByFile(file)).toMatchObject({
+ kind: 'reconcile',
+ reason: 'stale-external-write',
+ });
+
+ const unloadDocument = server.hocuspocus.unloadDocument.bind(server.hocuspocus);
+ let failNextUnload = true;
+ server.hocuspocus.unloadDocument = async (document) => {
+ if (!failNextUnload) return unloadDocument(document);
+ failNextUnload = false;
+ server.durabilityState.switchReconciledBaseScope('feature');
+ server.conflicts.setBranch('feature');
+ throw new Error('injected unload failure');
+ };
+
+ await expect(server.conflicts.resolve(file, 'delete')).rejects.toThrow(
+ 'injected unload failure',
+ );
+
+ expect(server.durabilityState.getActiveBranch()).toBe('feature');
+ expect(server.durabilityState.getStaleExternalWrite(docName)).toBeUndefined();
+ expect(server.durabilityState.listStaleExternalWrites()).toEqual([]);
+ expect(server.conflicts.findByFile(file)).toBeUndefined();
+
+ server.durabilityState.switchReconciledBaseScope('main');
+ server.conflicts.setBranch('main');
+
+ expect(server.durabilityState.getStaleExternalWrite(docName)).toMatchObject({
+ docName,
+ diskContent,
+ retainedContent: retained,
+ detectedAt: raised.detectedAt,
+ });
+ expect(persistedStaleExternalWrite(rig.tmpDir, docName)).toMatchObject({
+ docName,
+ diskContent,
+ detectedAt: raised.detectedAt,
+ });
+ conn.disconnect();
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
+ test('a resolution that rewrites a file already on disk leaves the directory count where it found it', async () => {
+ rig = await setupReconcileRig('ok-resolve-mine-dircount-');
+ const notesDir = join(rig.tmpDir, 'notes');
+ mkdirSync(notesDir);
+ const docName = 'notes/rewritten-in-place';
+ const diskContent = '# Rewritten in place\n\nOn-disk body.\n';
+ writeFileSync(join(notesDir, 'rewritten-in-place.md'), diskContent, 'utf-8');
+ writeFileSync(join(notesDir, 'sibling.png'), 'png-bytes', 'utf-8');
+
+ const server = createReconcileServer(rig);
+ const raiseFor = (): void =>
+ server.conflicts.raise({
+ kind: 'reconcile',
+ file: `${docName}.md`,
+ reason: 'stale-external-write',
+ stages: { base: diskContent, ours: diskContent, theirs: diskContent },
+ });
+ try {
+ await server.ready;
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+
+ raiseFor();
+ await server.conflicts.resolve(`${docName}.md`, 'mine');
+
+ expect(existsSync(join(notesDir, 'rewritten-in-place.md'))).toBe(true);
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(false);
+
+ raiseFor();
+ await server.conflicts.resolve(`${docName}.md`, 'delete');
+
+ expect(existsSync(join(notesDir, 'rewritten-in-place.md'))).toBe(false);
+ expect(server.contentFilter.isExcluded('notes/sibling.png')).toBe(true);
+ } finally {
+ await server.destroy();
+ }
+ }, 30_000);
+
test('rename-path teardown rescues a dirty doc under the same checkpoint kind as the other teardown sites', async () => {
rig = await setupReconcileRig('ok-reconcile-rename-rescue-');
const docName = 'rename-rescue-source';
diff --git a/packages/server/src/server-factory.ts b/packages/server/src/server-factory.ts
index b80586525..0898354d1 100644
--- a/packages/server/src/server-factory.ts
+++ b/packages/server/src/server-factory.ts
@@ -118,7 +118,10 @@ import {
isRegisteredMarkdownDocName,
} from './doc-extensions.ts';
import { runDocLineageGuard } from './doc-lineage-guard.ts';
-import { DocumentDurabilityState } from './document-durability-state.ts';
+import {
+ DocumentDurabilityState,
+ type StaleExternalWriteConflict,
+} from './document-durability-state.ts';
import {
type Embedder,
type EmbeddingsKeyStore,
@@ -144,7 +147,9 @@ import {
type DiskEvent,
pathToDocName,
reconcileFileIndexAfterFilterRebuild,
+ registerRemoval,
registerWrite,
+ retractRemoval,
startWatcher,
type WatcherHandle,
} from './file-watcher.ts';
@@ -532,17 +537,36 @@ export function createServer(options: ServerOptions): ServerInstance {
gitRaw: async (args) =>
createGitInstance(projectDir, { credentialConfig: [], timeoutMs: 30_000 }).git.raw(args),
writeProjectFileUntracked: (absPath, bytes) => tracedWriteFileSync(absPath, bytes, 'utf-8'),
- unlinkProjectFile: (absPath) => tracedUnlinkSync(absPath),
+ unlinkProjectFileUndeclared: (absPath) => tracedUnlinkSync(absPath),
+ deleteResolvedContent: (docName, absPath) => {
+ const declaration = registerRemoval(absPath);
+ try {
+ tracedUnlinkSync(absPath);
+ } catch (cause) {
+ retractRemoval(declaration);
+ throw cause;
+ }
+ contentFilter.decrementMdDir(dirname(docName));
+ },
applyResolvedContent: async (docName, absPath, bytes) => {
+ const absent = !existsSync(absPath);
registerWrite(absPath, contentHash(bytes));
await atomicWriteFile(absPath, bytes, { fs: tracedAtomicFs });
+ if (absent) contentFilter.incrementMdDir(dirname(docName));
if (hocuspocus.documents.get(docName)) applyToDoc(docName, bytes);
setReconciledBase(docName, bytes);
await derivedDocumentIndex.recordDiskUpsert(docName, bytes);
},
readLiveContent: (docName) => serializeDoc(docName),
finalizeReconcileResolution: async (entry, strategy, docName) => {
+ let priorStaleExternalWrite:
+ | { conflict: StaleExternalWriteConflict; branch: string }
+ | undefined;
if (entry.reason === 'stale-external-write') {
+ const conflict = durabilityState.getStaleExternalWrite(docName);
+ if (conflict) {
+ priorStaleExternalWrite = { conflict, branch: durabilityState.getActiveBranch() };
+ }
if (strategy === 'mine' || strategy === 'content') {
durabilityState.recordDisplacedVersion(docName, entry.stages.theirs);
} else {
@@ -553,14 +577,67 @@ export function createServer(options: ServerOptions): ServerInstance {
const document = hocuspocus.documents.get(docName);
const lifecycle = document?.getMap('lifecycle');
if (strategy === 'delete') {
- deleteReconciledBase(docName);
+ const isDirty = document
+ ? rescueUnflushedEditsBeforeTeardown(
+ docName,
+ headWatcher?.getLastKnownBranch() ?? 'main',
+ 'delete',
+ true,
+ )
+ : false;
await derivedDocumentIndex.recordDiskDelete(docName);
+ log.info(
+ { docName, isDirty, source: 'resolve-delete' },
+ `[reconcile] delete: ${docName} (dirty=${isDirty})`,
+ );
+ const priorLifecycle = lifecycle
+ ? {
+ status: lifecycle.get('status'),
+ reason: lifecycle.get('reason'),
+ detectedAt: lifecycle.get('detectedAt'),
+ }
+ : undefined;
lifecycle?.set('status', 'deleted-upstream');
lifecycle?.delete('reason');
lifecycle?.delete('detectedAt');
- if (document) hocuspocus.closeConnections(docName);
+ if (document) {
+ try {
+ hocuspocus.closeConnections(docName);
+ await forceUnloadDocument(document);
+ } catch (cause) {
+ if (lifecycle && priorLifecycle) {
+ for (const [key, value] of Object.entries(priorLifecycle)) {
+ if (value === undefined) lifecycle.delete(key);
+ else lifecycle.set(key, value);
+ }
+ }
+ if (priorStaleExternalWrite) {
+ try {
+ durabilityState.restoreStaleExternalWrite(
+ priorStaleExternalWrite.conflict,
+ priorStaleExternalWrite.branch,
+ );
+ } catch (rollbackFailure) {
+ log.error(
+ { err: rollbackFailure, docName },
+ `[reconcile] stale-external-write rollback failed for ${docName}; the retry may not find the conflict`,
+ );
+ }
+ }
+ throw cause;
+ }
+ }
+ deleteReconciledBase(docName);
onUpstreamDelete(docName);
scheduleIndexRegenerationAfterRemoval(docName);
+ console.info(
+ JSON.stringify({
+ event: 'recently-removed-docs-populate',
+ docName,
+ kind: 'deleted',
+ source: 'resolve-delete',
+ }),
+ );
} else if (lifecycle?.get('reason') === entry.reason) {
lifecycle.delete('status');
lifecycle.delete('reason');
@@ -2655,7 +2732,10 @@ export function createServer(options: ServerOptions): ServerInstance {
deleteReconciledBase(docName);
await derivedDocumentIndex.recordDiskDelete(docName);
- log.info({ docName, isDirty }, `[reconcile] delete: ${docName} (dirty=${isDirty})`);
+ log.info(
+ { docName, isDirty, source: 'watcher-delete' },
+ `[reconcile] delete: ${docName} (dirty=${isDirty})`,
+ );
hocuspocus.closeConnections(docName);
await forceUnloadDocument(document);
diff --git a/packages/server/src/stale-external-write-gate.test.ts b/packages/server/src/stale-external-write-gate.test.ts
index c7dd5bdeb..383f16b6f 100644
--- a/packages/server/src/stale-external-write-gate.test.ts
+++ b/packages/server/src/stale-external-write-gate.test.ts
@@ -140,7 +140,8 @@ describe('reconcileDiskBeforeAgentWrite — stale external write gate', () => {
io: {
gitRaw: async () => '',
writeProjectFileUntracked: () => undefined,
- unlinkProjectFile: () => undefined,
+ unlinkProjectFileUndeclared: () => undefined,
+ deleteResolvedContent: () => undefined,
applyResolvedContent: async () => undefined,
},
});
@@ -491,7 +492,8 @@ describe('reconcileDiskBeforeAgentWrite — stale external write gate', () => {
io: {
gitRaw: async () => '',
writeProjectFileUntracked: () => undefined,
- unlinkProjectFile: () => undefined,
+ unlinkProjectFileUndeclared: () => undefined,
+ deleteResolvedContent: () => undefined,
applyResolvedContent: async () => undefined,
},
});
diff --git a/packages/server/src/sync-engine.test.ts b/packages/server/src/sync-engine.test.ts
index d27a61512..d2d5527c9 100644
--- a/packages/server/src/sync-engine.test.ts
+++ b/packages/server/src/sync-engine.test.ts
@@ -90,7 +90,8 @@ function newAuthority(): ConflictAuthority {
io: {
gitRaw: (args) => simpleGit(projectDir).raw(args),
writeProjectFileUntracked: (absPath, bytes) => writeFileSync(absPath, bytes, 'utf-8'),
- unlinkProjectFile: (absPath) => rmSync(absPath, { force: true }),
+ unlinkProjectFileUndeclared: (absPath) => rmSync(absPath, { force: true }),
+ deleteResolvedContent: (_docName, absPath) => rmSync(absPath, { force: true }),
applyResolvedContent: async (_docName, absPath, bytes) => {
writeFileSync(absPath, bytes, 'utf-8');
},