Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): keep a deleted document deleted when a store is in flight (#4519)

Nick Gomez566a344e0937
diff --git a/.changeset/removed-document-stays-deleted.md b/.changeset/removed-document-stays-deleted.md
new file mode 100644
index 000000000..3d231d558
--- /dev/null
+++ b/.changeset/removed-document-stays-deleted.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge': patch
+---
+
+A save for a document whose file is gone is now refused instead of recreating the removed file with text you never saved.
diff --git a/docs/content/reference/mcp.mdx b/docs/content/reference/mcp.mdx
index 822967fab..e6aa9cf12 100644
--- a/docs/content/reference/mcp.mdx
+++ b/docs/content/reference/mcp.mdx
@@ -79,6 +79,10 @@ Git-index resolution stages the file and creates a merge commit after the final
A write that detects a stale file during its final disk flush returns the distinct `urn:ok:error:stale-external-write` (409): the edit reached collaborative state and the recovery snapshot, but the disk write was skipped. Resolve and re-read before retrying to avoid duplicating an already-applied append.
+A write to a document whose file has been deleted returns `urn:ok:error:doc-removed` (409): the edit reached collaborative state, but the disk write was refused rather than recreating the removed file. This is not retryable — the document is gone. Re-create it with `write` if you still want the content.
+
+A write whose document path cannot be resolved inside the content directory returns `urn:ok:error:path-escape` (400): the edit reached collaborative state, but the disk write was refused. This covers a symlink cycle at the document's own path and a link resolving outside the content directory. It is not retryable — repair the path on disk before writing this content again.
+
To detect conflict state proactively without waiting for a 409, call `conflicts({ kind: "list" })`; each entry is `{ file, detectedAt, conflictKind, … }`. `conflicts({ kind: "content" })` adds the doc's `lifecycleStatus` alongside its versions.
## Preview links
diff --git a/packages/app/tests/integration/doc-removed-http.test.ts b/packages/app/tests/integration/doc-removed-http.test.ts
new file mode 100644
index 000000000..05d4b9c73
--- /dev/null
+++ b/packages/app/tests/integration/doc-removed-http.test.ts
@@ -0,0 +1,139 @@
+import { randomUUID } from 'node:crypto';
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ realpathSync,
+ rmSync,
+ symlinkSync,
+ writeFileSync,
+} from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { AgentWriteBatchSuccessSchema, ProblemDetailsSchema } from '@inkeep/open-knowledge-core';
+import { afterEach, describe, expect, test } from 'vitest';
+import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
+
+const DISK_FAULT_CLAIM = 'will be lost if the server restarts';
+const DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS = 2000;
+const MAX_DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS = 8000;
+
+let server: TestServer | undefined;
+let projectDir: string | undefined;
+
+afterEach(async () => {
+ await server?.cleanup();
+ server = undefined;
+ if (projectDir) rmSync(projectDir, { recursive: true, force: true });
+ projectDir = undefined;
+});
+
+async function prepareRemovedDoc() {
+ projectDir = realpathSync(mkdtempSync(join(tmpdir(), 'ok-doc-removed-http-')));
+ const contentDir = join(projectDir, 'notes');
+ mkdirSync(contentDir);
+ const docName = `removed-${randomUUID()}`;
+ const path = join(contentDir, `${docName}.md`);
+ writeFileSync(path, '# Original\n\noriginal-body\n');
+ const target = await createTestServer({
+ contentDir,
+ projectDir,
+ debounce: DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS,
+ maxDebounce: MAX_DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS,
+ });
+ server = target;
+ await agentWriteMd(target.port, 'acknowledged-body\n', { docName, position: 'append' });
+ rmSync(path);
+ return { target, docName, path };
+}
+
+function expectRemovedDocProblem(problem: unknown): void {
+ expect(problem).toMatchObject({
+ type: 'urn:ok:error:doc-removed',
+ title: 'Edit applied in memory; disk write refused because the document is no longer on disk.',
+ detail: expect.stringContaining('Retrying will not help'),
+ });
+ expect(JSON.stringify(problem)).not.toContain(DISK_FAULT_CLAIM);
+}
+
+describe('a write to a document whose file was deleted', () => {
+ test('single write is refused as a 409 doc-removed, not a 500 disk fault', async () => {
+ const removed = await prepareRemovedDoc();
+ const response = await fetch(`${removed.target.baseUrl}/api/agent-write-md`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ docName: removed.docName,
+ markdown: 'after-delete\n',
+ position: 'append',
+ }),
+ });
+ expect(response.status).toBe(409);
+ expect(response.headers.get('content-type')).toBe('application/problem+json');
+ const raw = await response.text();
+ expect(raw).not.toContain(DISK_FAULT_CLAIM);
+ const body: unknown = JSON.parse(raw);
+ expect(ProblemDetailsSchema.safeParse(body).success).toBe(true);
+ expectRemovedDocProblem(body);
+ expect(existsSync(removed.path)).toBe(false);
+ });
+
+ test('batch entry carries the same refusal while a sibling write still lands', async () => {
+ const removed = await prepareRemovedDoc();
+ const otherDoc = `other-${randomUUID()}`;
+ const response = await fetch(`${removed.target.baseUrl}/api/agent-write-batch`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ docs: [
+ { docName: removed.docName, markdown: 'after-delete\n', position: 'append' },
+ { docName: otherDoc, markdown: '# Independent\n', position: 'replace' },
+ ],
+ }),
+ });
+ expect(response.status).toBe(200);
+ const body = AgentWriteBatchSuccessSchema.parse(await response.json());
+ expect(body.written).toBe(1);
+ expect(body.failed).toBe(1);
+ const failed = body.results[0];
+ expect(failed.status).toBe('error');
+ if (failed.status !== 'error') throw new Error('Expected the removed document to be refused');
+ expectRemovedDocProblem(failed.error);
+ expect(body.results[1]).toMatchObject({ status: 'written', docName: otherDoc });
+ expect(existsSync(removed.path)).toBe(false);
+ });
+});
+
+describe('a write to a document whose path cannot be resolved', () => {
+ test('a symlink cycle at the document path is refused as a 400 path-escape', async () => {
+ projectDir = realpathSync(mkdtempSync(join(tmpdir(), 'ok-path-fault-http-')));
+ const contentDir = join(projectDir, 'notes');
+ mkdirSync(contentDir);
+ const docName = `cycle-${randomUUID()}`;
+ const path = join(contentDir, `${docName}.md`);
+ const other = join(contentDir, `${docName}-loop.md`);
+ symlinkSync(other, path);
+ symlinkSync(path, other);
+ const target = await createTestServer({
+ contentDir,
+ projectDir,
+ debounce: DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS,
+ maxDebounce: MAX_DEBOUNCE_OUTLASTS_ROUTE_FLUSH_MS,
+ });
+ server = target;
+
+ const response = await fetch(`${target.baseUrl}/api/agent-write-md`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ docName, markdown: 'body\n', position: 'append' }),
+ });
+
+ expect(response.status).toBe(400);
+ const raw = await response.text();
+ expect(raw).not.toContain(DISK_FAULT_CLAIM);
+ expect(raw).not.toContain(contentDir);
+ const body: unknown = JSON.parse(raw);
+ expect(ProblemDetailsSchema.safeParse(body).success).toBe(true);
+ expect(body).toMatchObject({ type: 'urn:ok:error:path-escape' });
+ });
+});
diff --git a/packages/core/src/handoff/urn-ipc-registry.ts b/packages/core/src/handoff/urn-ipc-registry.ts
index f8a2dcfa3..8fcc0c79a 100644
--- a/packages/core/src/handoff/urn-ipc-registry.ts
+++ b/packages/core/src/handoff/urn-ipc-registry.ts
@@ -56,6 +56,7 @@ export const URN_HTTP_ONLY: ReadonlySet<ProblemType> = new Set<ProblemType>([
'urn:ok:error:too-many-agent-sessions',
'urn:ok:error:disk-divergence',
'urn:ok:error:stale-external-write',
+ 'urn:ok:error:doc-removed',
'urn:ok:error:doc-in-conflict',
'urn:ok:error:no-conflict-tracked',
'urn:ok:error:unresolved-conflict-markers',
diff --git a/packages/core/src/schemas/api/_envelope.ts b/packages/core/src/schemas/api/_envelope.ts
index 5f8b17373..0a419781e 100644
--- a/packages/core/src/schemas/api/_envelope.ts
+++ b/packages/core/src/schemas/api/_envelope.ts
@@ -79,6 +79,7 @@ export const ProblemTypeSchema = z.enum([
'urn:ok:error:too-many-agent-sessions',
'urn:ok:error:disk-divergence',
'urn:ok:error:stale-external-write',
+ 'urn:ok:error:doc-removed',
'urn:ok:error:doc-not-found',
'urn:ok:error:doc-already-exists',
'urn:ok:error:doc-not-open',
diff --git a/packages/server/src/api-extension.ts b/packages/server/src/api-extension.ts
index 6813cc6be..b6e7ed8f1 100644
--- a/packages/server/src/api-extension.ts
+++ b/packages/server/src/api-extension.ts
@@ -222,7 +222,12 @@ import {
SUPPORTED_DOC_EXTENSIONS,
stripDocExtension,
} from './doc-extensions.ts';
-import type { DocumentDurabilityState, StoreFailure } from './document-durability-state.ts';
+import {
+ type DocumentDurabilityState,
+ OK_DOC_REMOVED,
+ OK_PATH_UNRESOLVABLE,
+ type StoreFailure,
+} from './document-durability-state.ts';
import {
type ReconcileBeforeWriteResult,
reconcileDiskBeforeAgentWrite,
@@ -1739,11 +1744,39 @@ export function createApiExtension(
return null;
}
+ function removedDocProblem(failure: StoreFailure) {
+ return {
+ type: 'urn:ok:error:doc-removed' as const,
+ title:
+ 'Edit applied in memory; disk write refused because the document is no longer on disk.',
+ detail: `${failure.message}. Retrying will not help: re-create the document before writing this content again.`,
+ } satisfies BatchEntryError;
+ }
+
+ function pathFaultProblem(failure: StoreFailure) {
+ return {
+ type: 'urn:ok:error:path-escape' as const,
+ title:
+ 'Edit applied in memory; disk write refused because the document path could not be resolved.',
+ detail: `${failure.message}. Retrying will not help: the path has to be repaired on disk before this content can be written.`,
+ } satisfies BatchEntryError;
+ }
+
function respondPersistenceFailure(
res: ServerResponse,
failure: StoreFailure,
handler: string,
): void {
+ if (failure.code === OK_DOC_REMOVED) {
+ const { type, title, detail } = removedDocProblem(failure);
+ errorResponse(res, 409, type, title, { handler, detail });
+ return;
+ }
+ if (failure.code === OK_PATH_UNRESOLVABLE) {
+ const { type, title, detail } = pathFaultProblem(failure);
+ errorResponse(res, 400, type, title, { handler, detail });
+ return;
+ }
const reason = classifyUploadErrno({ code: failure.code } as NodeJS.ErrnoException);
errorResponse(
res,
@@ -3626,14 +3659,20 @@ export function createApiExtension(
if (flushErrors.has(p.docName)) continue;
const flushOutcome = await flushDiskAndDetectOutcome(p.docName);
if (flushOutcome?.kind === 'failure') {
- const reason = classifyUploadErrno({
- code: flushOutcome.failure.code,
- } as NodeJS.ErrnoException);
- flushErrors.set(p.docName, {
- type: reason,
- title: 'Write applied in memory but failed to persist to disk.',
- detail: `${flushOutcome.failure.code ?? 'unknown error'}: ${flushOutcome.failure.message}. The content was NOT saved and will be lost if the server restarts.`,
- });
+ if (flushOutcome.failure.code === OK_DOC_REMOVED) {
+ flushErrors.set(p.docName, removedDocProblem(flushOutcome.failure));
+ } else if (flushOutcome.failure.code === OK_PATH_UNRESOLVABLE) {
+ flushErrors.set(p.docName, pathFaultProblem(flushOutcome.failure));
+ } else {
+ const reason = classifyUploadErrno({
+ code: flushOutcome.failure.code,
+ } as NodeJS.ErrnoException);
+ flushErrors.set(p.docName, {
+ type: reason,
+ title: 'Write applied in memory but failed to persist to disk.',
+ detail: `${flushOutcome.failure.code ?? 'unknown error'}: ${flushOutcome.failure.message}. The content was NOT saved and will be lost if the server restarts.`,
+ });
+ }
} else if (flushOutcome?.kind === 'divergence') {
flushErrors.set(p.docName, {
type: 'urn:ok:error:disk-divergence',
@@ -5366,6 +5405,8 @@ export function createApiExtension(
shippedBundleSkillMd,
flushDiskAndDetectOutcome,
respondStaleExternalWrite,
+ respondPersistenceFailure,
+ respondDiskDivergence,
skillInstallOps,
skillPlacementOps,
signalChannel,
diff --git a/packages/server/src/document-durability-state.ts b/packages/server/src/document-durability-state.ts
index 46e732b74..047c2f950 100644
--- a/packages/server/src/document-durability-state.ts
+++ b/packages/server/src/document-durability-state.ts
@@ -16,6 +16,9 @@ export interface StoreFailure {
message: string;
}
+export const OK_DOC_REMOVED = 'OK_DOC_REMOVED';
+export const OK_PATH_UNRESOLVABLE = 'OK_PATH_UNRESOLVABLE';
+
const IN_FLIGHT_FLUSH_TTL_MS = 60_000;
export const DISPLACED_VERSION_TTL_MS = 30 * 60_000;
diff --git a/packages/server/src/http/skills-install-routes.test.ts b/packages/server/src/http/skills-install-routes.test.ts
index 32f4324e5..3affb415b 100644
--- a/packages/server/src/http/skills-install-routes.test.ts
+++ b/packages/server/src/http/skills-install-routes.test.ts
@@ -1,12 +1,21 @@
-import { describe, expect, test } from 'vitest';
+import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
+import type { IncomingMessage } from 'node:http';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { Readable } from 'node:stream';
+import { afterEach, describe, expect, test } from 'vitest';
+import { makeCaptureRes } from '../composition-rig.test-helper.ts';
+import { OK_DOC_REMOVED } from '../document-durability-state.ts';
import { loggerFactory } from '../logger.ts';
-import { createSkillsInstallRoutes } from './skills-install-routes.ts';
+import { createSkillsInstallRoutes, type SkillsInstallRouteDeps } from './skills-install-routes.ts';
+
+const INSTALL_PATH = '/api/skill/install';
function unexpectedCall(): never {
throw new Error('Route table construction must not invoke skill services.');
}
-function buildGroup() {
+function buildGroup(overrides: Partial<SkillsInstallRouteDeps> = {}) {
return createSkillsInstallRoutes({
resolveSkillsRoot: () => '/nonexistent-skills',
validateSkillName: () => true,
@@ -17,6 +26,8 @@ function buildGroup() {
shippedBundleSkillMd: () => null,
flushDiskAndDetectOutcome: () => Promise.resolve(null),
respondStaleExternalWrite: unexpectedCall,
+ respondPersistenceFailure: unexpectedCall,
+ respondDiskDivergence: unexpectedCall,
skillInstallOps: {
resolveFork: unexpectedCall,
applyAddRemove: unexpectedCall,
@@ -35,18 +46,105 @@ function buildGroup() {
scheduleDeferredIgnoreRebuild: () => {},
effectiveInstallMode: () => 'copy',
log: loggerFactory.getLogger('test'),
+ ...overrides,
});
}
describe('createSkillsInstallRoutes table', () => {
test('registers exactly the one skill-install path', () => {
- expect([...buildGroup().paths].sort()).toEqual(['/api/skill/install'].sort());
+ expect([...buildGroup().paths].sort()).toEqual([INSTALL_PATH].sort());
});
test('every skill-install path is mutating (whole-family legacy MUTATING_ROUTES membership)', () => {
const { table } = buildGroup();
- for (const path of ['/api/skill/install']) {
+ for (const path of [INSTALL_PATH]) {
expect(table.isMutating(path), path).toBe(true);
}
});
});
+
+function makeReq(body: unknown): IncomingMessage {
+ const req = Readable.from(Buffer.from(JSON.stringify(body))) as unknown as IncomingMessage;
+ req.method = 'POST';
+ req.url = INSTALL_PATH;
+ req.headers = { host: 'localhost', 'content-type': 'application/json' };
+ return req;
+}
+
+describe('skill-install surfaces every flush outcome instead of installing over it', () => {
+ const SKILL_NAME = 'probe-skill';
+ let roots: string[] = [];
+
+ afterEach(() => {
+ for (const root of roots) rmSync(root, { recursive: true, force: true });
+ roots = [];
+ });
+
+ function seedSourcelessSkill() {
+ const skillsRoot = mkdtempSync(join(tmpdir(), 'ok-skill-install-root-'));
+ const skillsHome = mkdtempSync(join(tmpdir(), 'ok-skill-install-home-'));
+ roots.push(skillsRoot, skillsHome);
+ mkdirSync(join(skillsRoot, SKILL_NAME), { recursive: true });
+ return { skillsRoot, skillsHome };
+ }
+
+ async function dispatchInstall(overrides: Partial<SkillsInstallRouteDeps>) {
+ const { skillsRoot, skillsHome } = seedSourcelessSkill();
+ const group = buildGroup({
+ resolveSkillsRoot: () => skillsRoot,
+ skillsHome,
+ skillInstallBase: () => skillsHome,
+ ...overrides,
+ });
+ const route = group.table.resolve(INSTALL_PATH);
+ if (!route?.dispatch) throw new Error(`${INSTALL_PATH} did not resolve to a dispatch handler`);
+ const { res, captured } = makeCaptureRes();
+ await route.dispatch(makeReq({ name: SKILL_NAME, scope: 'global' }), res);
+ return captured;
+ }
+
+ test('a refused disk write answers with the failure rather than validating and installing', async () => {
+ const seen: unknown[] = [];
+ const captured = await dispatchInstall({
+ flushDiskAndDetectOutcome: () =>
+ Promise.resolve({
+ kind: 'failure',
+ failure: { code: OK_DOC_REMOVED, message: 'the skill document is no longer on disk' },
+ }),
+ respondPersistenceFailure: (res, failure, handler) => {
+ seen.push({ failure, handler });
+ res.writeHead(409, { 'Content-Type': 'application/problem+json' });
+ res.end(JSON.stringify({ type: 'urn:ok:error:doc-removed' }));
+ },
+ });
+ expect(seen).toEqual([
+ {
+ failure: { code: OK_DOC_REMOVED, message: 'the skill document is no longer on disk' },
+ handler: 'skill-install',
+ },
+ ]);
+ expect(captured.status).toBe(409);
+ expect(captured.body).not.toContain('INVALID_SKILL_SOURCE');
+ });
+
+ test('a diverged disk write answers with the divergence rather than validating and installing', async () => {
+ const seen: string[] = [];
+ const captured = await dispatchInstall({
+ flushDiskAndDetectOutcome: () => Promise.resolve({ kind: 'divergence' }),
+ respondDiskDivergence: (res, handler) => {
+ seen.push(handler);
+ res.writeHead(409, { 'Content-Type': 'application/problem+json' });
+ res.end(JSON.stringify({ type: 'urn:ok:error:disk-divergence' }));
+ },
+ });
+ expect(seen).toEqual(['skill-install']);
+ expect(captured.status).toBe(409);
+ expect(captured.body).not.toContain('INVALID_SKILL_SOURCE');
+ });
+
+ test('a clean flush falls through to validation, which rejects a SKILL.md-less source', async () => {
+ const captured = await dispatchInstall({});
+ expect(captured.status).toBe(400);
+ expect(captured.body).toContain('INVALID_SKILL_SOURCE');
+ });
+});
diff --git a/packages/server/src/http/skills-install-routes.ts b/packages/server/src/http/skills-install-routes.ts
index 72f94f384..de079f5e8 100644
--- a/packages/server/src/http/skills-install-routes.ts
+++ b/packages/server/src/http/skills-install-routes.ts
@@ -48,6 +48,8 @@ export interface SkillsInstallRouteDeps {
skillsHome: string;
shippedBundleSkillMd: (name: string, scope?: 'project' | 'global') => string | null;
respondStaleExternalWrite: (res: ServerResponse, handler: string, docName: string) => void;
+ respondPersistenceFailure: (res: ServerResponse, failure: StoreFailure, handler: string) => void;
+ respondDiskDivergence: (res: ServerResponse, handler: string) => void;
flushDiskAndDetectOutcome: (
docName: string,
) => Promise<
@@ -77,6 +79,8 @@ export function createSkillsInstallRoutes(deps: SkillsInstallRouteDeps): ApiRout
shippedBundleSkillMd,
flushDiskAndDetectOutcome,
respondStaleExternalWrite,
+ respondPersistenceFailure,
+ respondDiskDivergence,
skillInstallOps,
skillPlacementOps,
signalChannel,
@@ -133,6 +137,14 @@ export function createSkillsInstallRoutes(deps: SkillsInstallRouteDeps): ApiRout
? `${relative(inPlaceScanBase, skillDir).split(sep).join('/')}/SKILL`
: skillLiveDocName(body.scope, body.name);
const liveSkillFlush = await flushDiskAndDetectOutcome(liveSkillDoc);
+ if (liveSkillFlush?.kind === 'failure') {
+ respondPersistenceFailure(res, liveSkillFlush.failure, 'skill-install');
+ return;
+ }
+ if (liveSkillFlush?.kind === 'divergence') {
+ respondDiskDivergence(res, 'skill-install');
+ return;
+ }
if (liveSkillFlush?.kind === 'stale-external-write') {
respondStaleExternalWrite(res, 'skill-install', liveSkillDoc);
return;
diff --git a/packages/server/src/persistence-removed-doc-publish.test.ts b/packages/server/src/persistence-removed-doc-publish.test.ts
new file mode 100644
index 000000000..6a554035c
--- /dev/null
+++ b/packages/server/src/persistence-removed-doc-publish.test.ts
@@ -0,0 +1,428 @@
+import {
+ existsSync,
+ lstatSync,
+ mkdirSync,
+ mkdtempSync,
+ readdirSync,
+ readFileSync,
+ realpathSync,
+ renameSync,
+ rmSync,
+ symlinkSync,
+ writeFileSync,
+} from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import * as Y from 'yjs';
+import { DocumentDurabilityState } from './document-durability-state.ts';
+import * as fsTraced from './fs-traced.ts';
+import { getMetrics } from './metrics.ts';
+import { createPersistenceExtension } from './persistence.ts';
+
+const BROWSER_ORIGIN = {
+ source: 'connection',
+ connection: { context: { principalId: 'principal-test' } },
+};
+
+const SEEDED = 'alpha\n\nbeta\n';
+
+function appendUnflushedEdit(document: Y.Doc): void {
+ const fragment = document.getXmlFragment('default');
+ const ytext = document.getText('source');
+ const paragraph = new Y.XmlElement('paragraph');
+ paragraph.insert(0, [new Y.XmlText('pending edit')]);
+ fragment.insert(fragment.length, [paragraph]);
+ ytext.insert(ytext.length, '\npending edit\n');
+}
+
+function refusalEvents(warn: ReturnType<typeof vi.spyOn>): Array<Record<string, unknown>> {
+ return warn.mock.calls
+ .map(([arg]) => (typeof arg === 'string' ? arg : ''))
+ .filter((arg) => arg.includes('"event":"persistence-store-removed-doc"'))
+ .map((arg) => JSON.parse(arg) as Record<string, unknown>);
+}
+
+describe('persistence publish guard — a removed document is never republished', () => {
+ let tmpDir: string;
+ let docName: string;
+ let docPath: string;
+ let document: Y.Doc;
+ let durabilityState: DocumentDurabilityState;
+
+ function persistenceWith(isRecentlyRemoved?: (name: string) => boolean) {
+ return createPersistenceExtension({
+ contentDir: tmpDir,
+ projectDir: tmpDir,
+ gitEnabled: false,
+ durabilityState,
+ ...(isRecentlyRemoved ? { isRecentlyRemoved } : {}),
+ });
+ }
+
+ async function loadFromDisk(persistence: ReturnType<typeof persistenceWith>): Promise<void> {
+ await persistence.extension.onLoadDocument?.({
+ document,
+ documentName: docName,
+ context: {},
+ } as never);
+ expect(durabilityState.getReconciledBase(docName)).toBe(SEEDED);
+ }
+
+ async function store(persistence: ReturnType<typeof persistenceWith>): Promise<void> {
+ await persistence.extension.onStoreDocument?.({
+ document,
+ documentName: docName,
+ lastTransactionOrigin: BROWSER_ORIGIN,
+ lastContext: {},
+ } as never);
+ }
+
+ beforeEach(() => {
+ tmpDir = realpathSync(mkdtempSync(join(tmpdir(), 'ok-removed-publish-')));
+ docName = 'removed-doc';
+ docPath = join(tmpDir, `${docName}.md`);
+ document = new Y.Doc();
+ durabilityState = new DocumentDurabilityState();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ document.destroy();
+ rmSync(tmpDir, { recursive: true, force: true });
+ });
+
+ test('does not recreate a file deleted before the store began', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ rmSync(docPath);
+
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ });
+
+ test('does not recreate a file deleted while the store was already in flight', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+
+ queueMicrotask(() => rmSync(docPath));
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ expect(refusalEvents(warn).at(-1)?.reason).toBe('file-absent');
+ });
+
+ test('names the watcher record as the reason, and the bytes it refused', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith(() => true);
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ const before = getMetrics().persistenceStoreRemovedDocCount;
+ rmSync(docPath);
+
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ expect(getMetrics().persistenceStoreRemovedDocCount).toBe(before + 1);
+ const event = refusalEvents(warn).at(-1);
+ expect(event?.['doc.name']).toBe(docName);
+ expect(event?.reason).toBe('recently-removed');
+ expect(event?.baseBytes).toBe(SEEDED.length);
+ expect(event?.candidateBytes).toBeGreaterThan(SEEDED.length);
+ });
+
+ test('still publishes when the watcher record is stale and the file is on disk', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith(() => true);
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+
+ await store(persistence);
+
+ expect(readFileSync(docPath, 'utf-8')).toContain('pending edit');
+ });
+
+ test('reports the refusal to a caller awaiting the flush', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
+ rmSync(docPath);
+ durabilityState.markAgentWriteStore(docName);
+
+ await store(persistence);
+
+ expect(durabilityState.takeStoreFailure(docName)?.code).toBe('OK_DOC_REMOVED');
+ });
+
+ test('records no flush outcome when nobody was awaiting the refused store', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
+ rmSync(docPath);
+
+ await store(persistence);
+
+ expect(durabilityState.takeStoreFailure(docName)).toBeNull();
+ });
+
+ test('does not recreate the folder of a document removed with its directory', async () => {
+ docName = 'folder/removed-doc';
+ docPath = join(tmpDir, `${docName}.md`);
+ mkdirSync(dirname(docPath), { recursive: true });
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ rmSync(dirname(docPath), { recursive: true });
+
+ await store(persistence);
+
+ expect(existsSync(dirname(docPath))).toBe(false);
+ });
+
+ test('refuses, and leaves the link intact, when the symlink target has gone', async () => {
+ const targetPath = join(tmpDir, 'target.md');
+ writeFileSync(targetPath, SEEDED, 'utf-8');
+ symlinkSync(targetPath, docPath);
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ rmSync(targetPath);
+
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ expect(lstatSync(docPath).isSymbolicLink()).toBe(true);
+ expect(refusalEvents(warn).at(-1)?.reason).toBe('file-absent');
+ });
+
+ test('refuses after a branch switch has dropped the reconciled base', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
+ durabilityState.switchReconciledBaseScope('feature');
+ expect(durabilityState.getReconciledBase(docName)).toBeUndefined();
+ rmSync(docPath);
+
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ });
+
+ test('does not recreate a file deleted between the temp write and the rename', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ const realWriteFile = fsTraced.tracedWriteFile;
+ vi.spyOn(fsTraced, 'tracedWriteFile').mockImplementation(async (path, data, options) => {
+ await realWriteFile(path, data, options);
+ if (typeof path === 'string' && path.startsWith(`${docPath}.tmp.`)) rmSync(docPath);
+ });
+
+ await store(persistence);
+
+ expect(existsSync(docPath)).toBe(false);
+ expect(readdirSync(tmpDir).filter((name) => name.includes('.tmp.'))).toEqual([]);
+ expect(refusalEvents(warn).at(-1)?.reason).toBe('file-absent');
+ });
+
+ test('refuses when the document folder is reoccupied by a file', async () => {
+ docName = 'folder/removed-doc';
+ docPath = join(tmpDir, `${docName}.md`);
+ mkdirSync(dirname(docPath), { recursive: true });
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ rmSync(dirname(docPath), { recursive: true });
+ writeFileSync(dirname(docPath), 'reoccupied\n', 'utf-8');
+ durabilityState.markAgentWriteStore(docName);
+
+ await store(persistence);
+
+ expect(readFileSync(dirname(docPath), 'utf-8')).toBe('reoccupied\n');
+ expect(refusalEvents(warn).at(-1)?.reason).toBe('file-absent');
+ expect(durabilityState.takeStoreFailure(docName)?.code).toBe('OK_DOC_REMOVED');
+ });
+
+ test('records a probe error the caller cannot read as a clean flush', async () => {
+ docName = 'a/doc';
+ docPath = join(tmpDir, 'a', 'doc.md');
+ mkdirSync(dirname(docPath), { recursive: true });
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ rmSync(join(tmpDir, 'a'), { recursive: true });
+ symlinkSync(join(tmpDir, 'b'), join(tmpDir, 'a'));
+ symlinkSync(join(tmpDir, 'a'), join(tmpDir, 'b'));
+ durabilityState.markAgentWriteStore(docName);
+
+ await expect(store(persistence)).rejects.toThrow(/ELOOP/);
+
+ expect(durabilityState.takeStoreFailure(docName)?.code).toBe('OK_PATH_UNRESOLVABLE');
+ });
+
+ test('records a probe error when the document path itself is a symlink cycle', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ rmSync(docPath);
+ symlinkSync(join(tmpDir, 'cycle.md'), docPath);
+ symlinkSync(docPath, join(tmpDir, 'cycle.md'));
+ durabilityState.markAgentWriteStore(docName);
+
+ await expect(store(persistence)).rejects.toThrow(/ELOOP/);
+
+ expect(durabilityState.takeStoreFailure(docName)?.code).toBe('OK_PATH_UNRESOLVABLE');
+ expect(lstatSync(docPath).isSymbolicLink()).toBe(true);
+ });
+
+ test('refuses, and leaves the link intact, when the symlink resolves through a file', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ rmSync(docPath);
+ const regularPath = join(tmpDir, 'regular.md');
+ writeFileSync(regularPath, 'x', 'utf-8');
+ symlinkSync(join(regularPath, 'sub.md'), docPath);
+ durabilityState.markAgentWriteStore(docName);
+
+ await store(persistence);
+
+ expect(refusalEvents(warn).at(-1)?.reason).toBe('file-absent');
+ expect(durabilityState.takeStoreFailure(docName)?.code).toBe('OK_DOC_REMOVED');
+ expect(lstatSync(docPath).isSymbolicLink()).toBe(true);
+ expect(readFileSync(regularPath, 'utf-8')).toBe('x');
+ });
+
+ test('records one path-fault classification when a cycle blocks a first write', async () => {
+ symlinkSync(join(tmpDir, 'cycle.md'), docPath);
+ symlinkSync(docPath, join(tmpDir, 'cycle.md'));
+ const persistence = persistenceWith();
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ expect(durabilityState.getReconciledBase(docName)).toBeUndefined();
+ durabilityState.markAgentWriteStore(docName);
+
+ await expect(store(persistence)).rejects.toThrow(/Symlink cycle detected/);
+
+ const failure = durabilityState.takeStoreFailure(docName);
+ expect(failure?.code).toBe('OK_PATH_UNRESOLVABLE');
+ expect(failure?.message).not.toContain(tmpDir);
+ });
+
+ test('records no path fault for a first write nobody was awaiting', async () => {
+ symlinkSync(join(tmpDir, 'cycle.md'), docPath);
+ symlinkSync(docPath, join(tmpDir, 'cycle.md'));
+ const persistence = persistenceWith();
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+
+ await expect(store(persistence)).rejects.toThrow(/Symlink cycle detected/);
+
+ expect(durabilityState.takeStoreFailure(docName)).toBeNull();
+ });
+
+ test('keeps the path-fault record when the fault appears between the temp write and the rename', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
+ durabilityState.markAgentWriteStore(docName);
+ const realWriteFile = fsTraced.tracedWriteFile;
+ vi.spyOn(fsTraced, 'tracedWriteFile').mockImplementation(async (path, data, options) => {
+ await realWriteFile(path, data, options);
+ if (typeof path === 'string' && path.startsWith(`${docPath}.tmp.`)) {
+ rmSync(docPath);
+ symlinkSync(join(tmpDir, 'cycle.md'), docPath);
+ symlinkSync(docPath, join(tmpDir, 'cycle.md'));
+ }
+ });
+
+ await expect(store(persistence)).rejects.toThrow();
+
+ const failure = durabilityState.takeStoreFailure(docName);
+ expect(failure?.code).toBe('OK_PATH_UNRESOLVABLE');
+ expect(failure?.message).not.toContain(tmpDir);
+ });
+
+ test('still creates the file for a document that never had one', async () => {
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ expect(durabilityState.getReconciledBase(docName)).toBeUndefined();
+
+ await store(persistenceWith());
+
+ expect(readFileSync(docPath, 'utf-8')).toContain('pending edit');
+ });
+
+ test('recreates a document that was removed after it had been loaded from disk', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ rmSync(docPath);
+ durabilityState.deleteReconciledBase(docName);
+ await persistence.extension.onLoadDocument?.({
+ document,
+ documentName: docName,
+ context: {},
+ } as never);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ await store(persistence);
+
+ expect(readFileSync(docPath, 'utf-8')).toContain('pending edit');
+ });
+
+ test('recreates a document whose file was renamed away while it stayed resident', async () => {
+ writeFileSync(docPath, SEEDED, 'utf-8');
+ const persistence = persistenceWith();
+ await loadFromDisk(persistence);
+
+ renameSync(docPath, join(tmpDir, 'renamed-doc.md'));
+ durabilityState.deleteReconciledBase(docName);
+ persistence.forgetObservedFile(docName);
+
+ document.transact(() => appendUnflushedEdit(document), BROWSER_ORIGIN);
+ await store(persistence);
+
+ expect(readFileSync(docPath, 'utf-8')).toContain('pending edit');
+ });
+});
diff --git a/packages/server/src/persistence.ts b/packages/server/src/persistence.ts
index 4f629ebfd..6ae74bd00 100644
--- a/packages/server/src/persistence.ts
+++ b/packages/server/src/persistence.ts
@@ -54,7 +54,12 @@ import {
} from './contributor-tracker.ts';
import type { DerivedDocumentIndexPersistencePort } from './derived-document-index.ts';
import { applyDiskContentToDoc, FILE_WATCHER_ORIGIN } from './disk-content-intake.ts';
-import { DocumentDurabilityState, type StoreFailure } from './document-durability-state.ts';
+import {
+ DocumentDurabilityState,
+ OK_DOC_REMOVED,
+ OK_PATH_UNRESOLVABLE,
+ type StoreFailure,
+} from './document-durability-state.ts';
import { refuseStaleExternalWrite } from './external-change.ts';
import { contentHash, registerWrite } from './file-watcher.ts';
import { tracedMkdir, tracedRename, tracedUnlinkSync, tracedWriteFile } from './fs-traced.ts';
@@ -302,6 +307,34 @@ function connectionCount(document: Y.Doc): number {
}
}
+const PATH_CANNOT_EXIST_CODES = new Set(['ENOENT', 'ENOTDIR']);
+
+type DocumentFileProbe = { readonly onDisk: boolean } | { readonly error: unknown };
+
+function classifyProbeError(
+ err: unknown,
+): { readonly onDisk: false } | { readonly error: unknown } {
+ const code = errnoCode(err);
+ if (code !== undefined && PATH_CANNOT_EXIST_CODES.has(code)) return { onDisk: false };
+ return { error: err };
+}
+
+function probeDocumentFileOnDisk(path: string): DocumentFileProbe {
+ try {
+ const entry = lstatSync(path, { throwIfNoEntry: false });
+ if (entry === undefined) return { onDisk: false };
+ if (!entry.isSymbolicLink()) return { onDisk: true };
+ } catch (err) {
+ return classifyProbeError(err);
+ }
+ try {
+ realpathSync(path);
+ return { onDisk: true };
+ } catch (err) {
+ return classifyProbeError(err);
+ }
+}
+
function toStoreFailure(err: unknown): StoreFailure {
let code: string | undefined;
try {
@@ -329,6 +362,7 @@ export interface PersistenceHandle {
waitForPendingCommits: () => Promise<void>;
getQueueDepths: () => PersistenceQueueDepths;
forceStore: (document: Y.Doc, documentName: string) => Promise<void>;
+ forgetObservedFile: (docName: string) => void;
readonly configPersistenceCtx: ConfigPersistenceCtx;
readonly managedArtifactCtx: ManagedArtifactCtx;
}
@@ -384,6 +418,7 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
const tripwireResetFailedDocs = new Set<string>();
const docsWithSettledWrite = new Set<string>();
+ const docsWithFileObservedOnDisk = new Set<string>();
const applyDiskContent = options?.applyDiskContentToDoc ?? applyDiskContentToDoc;
let pendingDeferredStoreFlushMode: 'within-branch' | 'discard-stale' | null = null;
@@ -1106,6 +1141,82 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
});
}
+ const abandonmentsRecorded = new WeakSet<object>();
+
+ function recordPathFault(
+ documentName: string,
+ err: unknown,
+ callerAwaitingFlush: boolean,
+ containmentEscape = false,
+ ): void {
+ persistenceDeferCounts.delete(documentName);
+ if (typeof err === 'object' && err !== null) abandonmentsRecorded.add(err);
+ if (!callerAwaitingFlush) return;
+ const errno = errnoCode(err);
+ const pathIsUnrepairable = containmentEscape || errno === 'ELOOP';
+ durabilityState.recordStoreFailure(
+ documentName,
+ pathIsUnrepairable
+ ? {
+ code: OK_PATH_UNRESOLVABLE,
+ message: `the path for ${documentName} could not be resolved inside the content directory${
+ errno === undefined ? '' : ` (${errno})`
+ }`,
+ }
+ : toStoreFailure(err),
+ );
+ }
+
+ function refuseRemovedDocPublish(
+ documentName: string,
+ base: string | undefined,
+ candidate: string,
+ callerAwaitingFlush: boolean,
+ ): void {
+ incrementPersistenceStoreRemovedDoc();
+ console.warn(
+ JSON.stringify({
+ event: 'persistence-store-removed-doc',
+ 'doc.name': documentName,
+ reason: options?.isRecentlyRemoved?.(documentName) ? 'recently-removed' : 'file-absent',
+ baseBytes: base?.length ?? null,
+ candidateBytes: candidate.length,
+ }),
+ );
+ if (callerAwaitingFlush) {
+ durabilityState.recordStoreFailure(documentName, {
+ code: OK_DOC_REMOVED,
+ message: `${documentName} is no longer on disk, so the store was refused rather than recreating the removed file`,
+ });
+ }
+ persistenceDeferCounts.delete(documentName);
+ }
+
+ function documentHadFileOnDisk(documentName: string, base: string | undefined): boolean {
+ return base !== undefined || docsWithFileObservedOnDisk.has(documentName);
+ }
+
+ function resolveStorePathPresence(
+ documentName: string,
+ path: string,
+ base: string | undefined,
+ candidate: string,
+ callerAwaitingFlush: boolean,
+ ): 'on-disk' | 'refused' {
+ const probe = probeDocumentFileOnDisk(path);
+ if ('error' in probe) {
+ recordPathFault(documentName, probe.error, callerAwaitingFlush);
+ log.error(
+ { err: probe.error, documentName, path },
+ `[persistence] Could not determine whether ${path} still exists; store abandoned`,
+ );
+ throw probe.error;
+ }
+ if (probe.onDisk) return 'on-disk';
+ refuseRemovedDocPublish(documentName, base, candidate, callerAwaitingFlush);
+ return 'refused';
+ }
+
async function storeDocumentNow({
document,
documentName,
@@ -1391,6 +1502,18 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
durabilityState.beginInFlightFlush(documentName, inFlightFlushValue);
const requestedPath = safeContentPath(documentName, contentDir);
+ if (
+ documentHadFileOnDisk(documentName, currentBase) &&
+ resolveStorePathPresence(
+ documentName,
+ requestedPath,
+ currentBase,
+ markdown,
+ agentTriggeredStore,
+ ) === 'refused'
+ ) {
+ return;
+ }
await tracedMkdir(dirname(requestedPath), { recursive: true });
let canonicalPath: string;
@@ -1418,12 +1541,18 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
}
canonicalPath = requestedPath;
} else if (code === 'ELOOP') {
+ recordPathFault(documentName, e, agentTriggeredStore);
log.error(
- { path: requestedPath, err: e },
+ { docName: documentName, path: requestedPath, err: e },
`[persistence] Symlink cycle at ${requestedPath}`,
);
throw new Error(`Symlink cycle detected at ${requestedPath}`);
} else {
+ recordPathFault(documentName, e, agentTriggeredStore);
+ log.error(
+ { docName: documentName, path: requestedPath, err: e },
+ `[persistence] Could not canonicalize ${requestedPath}; store abandoned`,
+ );
throw e;
}
}
@@ -1439,7 +1568,9 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
},
`[persistence] ${msg}`,
);
- throw new Error(msg);
+ const escapeErr = new Error(msg);
+ recordPathFault(documentName, escapeErr, agentTriggeredStore, true);
+ throw escapeErr;
}
if (
@@ -1531,14 +1662,17 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
}
}
- if (options?.isRecentlyRemoved?.(documentName)) {
- incrementPersistenceStoreRemovedDoc();
- console.warn(
- JSON.stringify({
- event: 'persistence-store-removed-doc',
- 'doc.name': documentName,
- }),
- );
+ if (
+ documentHadFileOnDisk(documentName, currentBase) &&
+ resolveStorePathPresence(
+ documentName,
+ canonicalPath,
+ currentBase,
+ markdown,
+ agentTriggeredStore,
+ ) === 'refused'
+ ) {
+ return;
}
const tmpPath = `${canonicalPath}.tmp.${crypto.randomUUID()}`;
@@ -1551,6 +1685,28 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
throw faultErr;
}
await tracedWriteFile(tmpPath, markdown, 'utf-8');
+ if (
+ documentHadFileOnDisk(documentName, currentBase) &&
+ resolveStorePathPresence(
+ documentName,
+ canonicalPath,
+ currentBase,
+ markdown,
+ agentTriggeredStore,
+ ) === 'refused'
+ ) {
+ try {
+ tracedUnlinkSync(tmpPath);
+ } catch (cleanupErr) {
+ if (errnoCode(cleanupErr) !== 'ENOENT') {
+ log.warn(
+ { err: cleanupErr, docName: documentName, path: tmpPath },
+ '[persistence] could not remove the temp file after refusing the publish',
+ );
+ }
+ }
+ return;
+ }
await tracedRename(tmpPath, canonicalPath);
registerWrite(canonicalPath, contentHash(markdown));
durabilityState.clearStoreFailure(documentName);
@@ -1568,7 +1724,9 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
tracedUnlinkSync(tmpPath);
} catch {}
persistenceDeferCounts.delete(documentName);
- durabilityState.recordStoreFailure(documentName, toStoreFailure(e));
+ if (!(typeof e === 'object' && e !== null && abandonmentsRecorded.has(e))) {
+ durabilityState.recordStoreFailure(documentName, toStoreFailure(e));
+ }
log.error({ err: e, documentName }, `[persistence] Failed to save ${documentName}`);
throw e;
}
@@ -1585,6 +1743,7 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
: undefined,
);
docsWithSettledWrite.add(documentName);
+ docsWithFileObservedOnDisk.add(documentName);
tripwireResetFailedDocs.delete(documentName);
persistenceDeferCounts.delete(documentName);
@@ -1710,6 +1869,7 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
const extension: Extension = {
async onLoadDocument({ document, documentName, context: _context }) {
docsWithSettledWrite.delete(documentName);
+ docsWithFileObservedOnDisk.delete(documentName);
if (isSystemDoc(documentName)) return;
if (isConfigDoc(documentName)) {
loadConfigDoc(document, documentName, configPersistenceCtx);
@@ -1739,6 +1899,7 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
);
const filePath = safeContentPath(documentName, contentDir);
if (!existsSync(filePath)) return;
+ docsWithFileObservedOnDisk.add(documentName);
let canonical = filePath;
try {
@@ -1958,6 +2119,10 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
return storeDocumentNow({ document, documentName, lastTransactionOrigin: null });
}
+ function forgetObservedFile(docName: string): void {
+ docsWithFileObservedOnDisk.delete(docName);
+ }
+
return {
extension,
durabilityState,
@@ -1967,6 +2132,7 @@ export function createPersistenceExtension(options?: PersistenceOptions): Persis
waitForPendingCommits,
getQueueDepths,
forceStore,
+ forgetObservedFile,
configPersistenceCtx,
managedArtifactCtx,
};
diff --git a/packages/server/src/server-factory.ts b/packages/server/src/server-factory.ts
index 87595bae1..cba799ed5 100644
--- a/packages/server/src/server-factory.ts
+++ b/packages/server/src/server-factory.ts
@@ -2496,6 +2496,7 @@ export function createServer(options: ServerOptions): ServerInstance {
if (loadedBeforeIndex) freezeAsRenamed(loadedBeforeIndex);
deleteReconciledBase(oldDocName);
+ persistence.forgetObservedFile(oldDocName);
setReconciledBase(newDocName, content);
log.info(