Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Preserve exact filesystem identities across file consumers (#6965)

Nick Gomez73b03821ae28
diff --git a/.changeset/exact-linked-file-identities.md b/.changeset/exact-linked-file-identities.md
new file mode 100644
index 000000000..bd99eec7f
--- /dev/null
+++ b/.changeset/exact-linked-file-identities.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Keep linked documents and files visible with correct metadata, recognize replaced bundles, and apply the matching page policy when filesystem identifiers are large.
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
index 0889bf4af..81d0250b8 100644
--- a/THIRD_PARTY_NOTICES.md
+++ b/THIRD_PARTY_NOTICES.md
@@ -6718,6 +6718,7 @@ The following MIT-licensed packages are patched in this repository via pnpm's `p
diff --git a/packages/app/tests/integration/older-own-write-echo.test.ts b/packages/app/tests/integration/older-own-write-echo.test.ts
index 0ba5395cc..3c1dfe863 100644
--- a/packages/app/tests/integration/older-own-write-echo.test.ts
+++ b/packages/app/tests/integration/older-own-write-echo.test.ts
@@ -457,7 +457,7 @@ describe('older own-write observations', () => {
size: 0,
modified: new Date(0).toISOString(),
canonicalPath: path(name),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
},
diff --git a/packages/cli/src/mcp/bundle-identity.test.ts b/packages/cli/src/mcp/bundle-identity.test.ts
index 7048efc0a..00bb129f8 100644
--- a/packages/cli/src/mcp/bundle-identity.test.ts
+++ b/packages/cli/src/mcp/bundle-identity.test.ts
@@ -5,6 +5,8 @@ import {
type BundleIdentityWatcherDeps,
captureBootIdentity,
detectBundleIdentity,
+ formatBootIdentityLine,
+ formatBundleReplacedLine,
startBundleIdentityWatcher,
} from './bundle-identity.ts';
@@ -15,10 +17,10 @@ const REAL_ANCHOR_PATH = '/Applications/OpenKnowledge.app/Contents/MacOS/OpenKno
function input(overrides: Partial<BundleIdentityCheckInput> = {}): BundleIdentityCheckInput {
return {
bundleAnchorPath: ANCHOR_PATH,
- currentInode: 299_520_753,
+ currentInode: '0:299520753',
platform: DARWIN,
realpath: () => REAL_ANCHOR_PATH,
- statInode: () => 299_520_753,
+ statInode: () => '0:299520753',
...overrides,
};
}
@@ -32,14 +34,14 @@ describe('detectBundleIdentity', () => {
test('returns `replaced` when realpath inode differs from process-start inode', () => {
const state = detectBundleIdentity(
input({
- currentInode: 299_520_753,
- statInode: () => 299_520_789,
+ currentInode: '0:299520753',
+ statInode: () => '0:299520789',
}),
);
expect(state.kind).toBe('replaced');
if (state.kind === 'replaced') {
- expect(state.currentInode).toBe(299_520_753);
- expect(state.onDiskInode).toBe(299_520_789);
+ expect(state.currentInode).toBe('0:299520753');
+ expect(state.onDiskInode).toBe('0:299520789');
}
});
@@ -102,8 +104,8 @@ describe('detectBundleIdentity', () => {
const linuxState = detectBundleIdentity(
input({
platform: 'linux',
- currentInode: 100,
- statInode: () => 200,
+ currentInode: '0:100',
+ statInode: () => '0:200',
realpath: () => {
throw new Error('realpath should not be called on non-darwin');
},
@@ -114,8 +116,8 @@ describe('detectBundleIdentity', () => {
const winState = detectBundleIdentity(
input({
platform: 'win32',
- currentInode: 100,
- statInode: () => 200,
+ currentInode: '0:100',
+ statInode: () => '0:200',
realpath: () => {
throw new Error('realpath should not be called on non-darwin');
},
@@ -128,8 +130,8 @@ describe('detectBundleIdentity', () => {
const state = detectBundleIdentity(
input({
realpath: () => '/Applications/OK.app/Contents/MacOS/OK',
- currentInode: 299_520_753,
- statInode: () => 299_520_753,
+ currentInode: '0:299520753',
+ statInode: () => '0:299520753',
}),
);
expect(state.kind).toBe('unchanged');
@@ -240,8 +242,8 @@ describe('startBundleIdentityWatcher', () => {
const fx = makeWatcherFixtures();
const replaced: BundleIdentityState = {
kind: 'replaced',
- currentInode: 100,
- onDiskInode: 200,
+ currentInode: '0:100',
+ onDiskInode: '0:200',
};
const onReplacedCalls: BundleIdentityState[] = [];
startBundleIdentityWatcher({
@@ -350,8 +352,8 @@ describe('startBundleIdentityWatcher', () => {
const onReplacedCalls: BundleIdentityState[] = [];
const replaced: BundleIdentityState = {
kind: 'replaced',
- currentInode: 100,
- onDiskInode: 200,
+ currentInode: '0:100',
+ onDiskInode: '0:200',
};
let kind: BundleIdentityState['kind'] = 'unreadable';
startBundleIdentityWatcher({
@@ -445,13 +447,13 @@ describe('captureBootIdentity', () => {
'/Applications/OpenKnowledge.app/Contents/MacOS/OpenKnowledge',
{
realpathSync: () => '/Applications/OpenKnowledge.app/Contents/MacOS/OpenKnowledge',
- statInoSync: () => 299_520_753,
+ statInoSync: () => '0:299520753',
log: (m) => logs.push(m),
},
);
expect(result).toEqual({
resolvedPath: '/Applications/OpenKnowledge.app/Contents/MacOS/OpenKnowledge',
- inode: 299_520_753,
+ inode: '0:299520753',
});
expect(logs).toEqual([]);
});
@@ -498,10 +500,33 @@ describe('captureBootIdentity', () => {
realpathSync: () => {
throw 'plain-string-error';
},
- statInoSync: () => 0,
+ statInoSync: () => '0:0',
log: (m) => logs.push(m),
});
expect(result).toBeUndefined();
expect(logs.join(' ')).toMatch(/plain-string-error/);
});
});
+
+describe('bundle identity log lines', () => {
+ test('the boot line names the device and inode it captured', () => {
+ expect(
+ formatBootIdentityLine(
+ { resolvedPath: REAL_ANCHOR_PATH, inode: '16777232:299520753' },
+ '1.2.3',
+ ),
+ ).toBe(
+ `[mcp] bundle identity anchor=${REAL_ANCHOR_PATH} dev=16777232 inode=299520753 version=1.2.3`,
+ );
+ });
+
+ test('a replacement on another device with the same inode prints both devices', () => {
+ const state = detectBundleIdentity(
+ input({ currentInode: '16777232:299520753', statInode: () => '16777240:299520753' }),
+ );
+ if (state.kind !== 'replaced') throw new Error(`expected replaced, got ${state.kind}`);
+ expect(formatBundleReplacedLine(REAL_ANCHOR_PATH, state, '1.2.3')).toBe(
+ `[mcp] bundle replaced anchor=${REAL_ANCHOR_PATH} bootDev=16777232 bootInode=299520753 onDiskDev=16777240 onDiskInode=299520753 version=1.2.3 — exiting for host respawn`,
+ );
+ });
+});
diff --git a/packages/cli/src/mcp/bundle-identity.ts b/packages/cli/src/mcp/bundle-identity.ts
index 3eb42e1fd..e6b17d317 100644
--- a/packages/cli/src/mcp/bundle-identity.ts
+++ b/packages/cli/src/mcp/bundle-identity.ts
@@ -1,14 +1,14 @@
export interface BundleIdentityCheckInput {
bundleAnchorPath: string;
- currentInode: number;
+ currentInode: string;
platform: NodeJS.Platform;
realpath: (path: string) => string;
- statInode: (path: string) => number;
+ statInode: (path: string) => string;
}
export type BundleIdentityState =
| { kind: 'unchanged' }
- | { kind: 'replaced'; currentInode: number; onDiskInode: number }
+ | { kind: 'replaced'; currentInode: string; onDiskInode: string }
| { kind: 'unreadable'; reason?: string };
export function detectBundleIdentity(input: BundleIdentityCheckInput): BundleIdentityState {
@@ -21,7 +21,7 @@ export function detectBundleIdentity(input: BundleIdentityCheckInput): BundleIde
return { kind: 'unreadable', reason: err instanceof Error ? err.message : String(err) };
}
- let onDiskInode: number;
+ let onDiskInode: string;
try {
onDiskInode = input.statInode(resolvedPath);
} catch (err) {
@@ -104,13 +104,13 @@ export function startBundleIdentityWatcher(
interface CaptureBootIdentityDeps {
realpathSync: (p: string) => string;
- statInoSync: (p: string) => number;
+ statInoSync: (p: string) => string;
log: (msg: string) => void;
}
interface BootIdentity {
resolvedPath: string;
- inode: number;
+ inode: string;
}
export function captureBootIdentity(
@@ -126,7 +126,7 @@ export function captureBootIdentity(
);
return undefined;
}
- let inode: number;
+ let inode: string;
try {
inode = deps.statInoSync(resolvedPath);
} catch (err) {
@@ -137,3 +137,23 @@ export function captureBootIdentity(
}
return { resolvedPath, inode };
}
+
+function identityParts(identity: string): { dev: string; inode: string } {
+ const separator = identity.indexOf(':');
+ return { dev: identity.slice(0, separator), inode: identity.slice(separator + 1) };
+}
+
+export function formatBootIdentityLine(boot: BootIdentity, version: string): string {
+ const { dev, inode } = identityParts(boot.inode);
+ return `[mcp] bundle identity anchor=${boot.resolvedPath} dev=${dev} inode=${inode} version=${version}`;
+}
+
+export function formatBundleReplacedLine(
+ anchorPath: string,
+ state: BundleIdentityState & { kind: 'replaced' },
+ version: string,
+): string {
+ const boot = identityParts(state.currentInode);
+ const onDisk = identityParts(state.onDiskInode);
+ return `[mcp] bundle replaced anchor=${anchorPath} bootDev=${boot.dev} bootInode=${boot.inode} onDiskDev=${onDisk.dev} onDiskInode=${onDisk.inode} version=${version} — exiting for host respawn`;
+}
diff --git a/packages/cli/src/mcp/server-bundle-identity.test.ts b/packages/cli/src/mcp/server-bundle-identity.test.ts
new file mode 100644
index 000000000..a9f548a4d
--- /dev/null
+++ b/packages/cli/src/mcp/server-bundle-identity.test.ts
@@ -0,0 +1,101 @@
+import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
+import { join, resolve } from 'node:path';
+import { ConfigSchema } from '@inkeep/open-knowledge-server';
+import { afterEach, expect, test, vi } from 'vitest';
+import type { BundleIdentityState, BundleIdentityWatcherDeps } from './bundle-identity.ts';
+import { startGlobalMcpServer } from './server.ts';
+
+const observation = vi.hoisted(() => ({
+ exact: 1152921500312607309n,
+ numeric: 1152921500312607400,
+ sampled: 0,
+ detect: undefined as (() => BundleIdentityState) | undefined,
+}));
+
+vi.mock('node:fs', async (importOriginal) => {
+ const original = await importOriginal<typeof import('node:fs')>();
+ return {
+ ...original,
+ statSync: (...args: Parameters<typeof original.statSync>) => {
+ const stats = original.statSync(...args);
+ if (String(args[0]).replaceAll('\\', '/').endsWith('/mcp/server.ts')) {
+ observation.sampled++;
+ Object.defineProperty(stats, 'ino', {
+ value: typeof stats.ino === 'bigint' ? observation.exact : observation.numeric,
+ });
+ }
+ return stats;
+ },
+ };
+});
+
+vi.mock('./bundle-identity.ts', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('./bundle-identity.ts')>()),
+ startBundleIdentityWatcher: (deps: BundleIdentityWatcherDeps) => {
+ observation.detect = deps.detect;
+ return { stop() {} };
+ },
+}));
+vi.mock('./host-liveness.ts', () => ({ startHostLivenessWatch: () => ({ stop() {} }) }));
+vi.mock('./lifecycle-logging.ts', () => ({ attachLifecycleLogging() {} }));
+vi.mock('./stderr-mirror.ts', () => ({ createMcpStderrMirror: () => ({ write() {} }) }));
+vi.mock('@modelcontextprotocol/sdk/server/stdio.js', () => ({
+ StdioServerTransport: class {
+ async start() {}
+ async send() {}
+ async close() {}
+ },
+}));
+
+afterEach(() => vi.restoreAllMocks());
+
+test.for([false, true])(
+ 'detects bundle replacement through MCP startup with inode collision=%s',
+ async (collision) => {
+ const scratch = resolve(import.meta.dirname, '../../../../tmp');
+ mkdirSync(scratch, { recursive: true });
+ const root = mkdtempSync(join(scratch, 'mcp-bundle-identity-'));
+ const platform = Object.getOwnPropertyDescriptor(process, 'platform');
+ const signals = new Map(
+ ['SIGINT', 'SIGTERM'].map((name) => [name, new Set(process.listeners(name))]),
+ );
+ const stdinEvents = new Map(
+ ['end', 'error'].map((name) => [name, new Set(process.stdin.listeners(name))]),
+ );
+ observation.exact = collision ? 1152921500312607309n : 11n;
+ observation.numeric = collision ? 1152921500312607400 : 11;
+ observation.sampled = 0;
+ observation.detect = undefined;
+ Object.defineProperty(process, 'platform', { value: 'darwin' });
+ const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
+ let handle: Awaited<ReturnType<typeof startGlobalMcpServer>> | undefined;
+ try {
+ handle = await startGlobalMcpServer({
+ startupCwd: root,
+ startupConfig: ConfigSchema.parse({}),
+ });
+ const detect = observation.detect;
+ if (detect === undefined) throw new Error('Bundle watcher was not registered');
+ expect(detect().kind).toBe('unchanged');
+ observation.exact = collision ? 1152921500312607343n : 13n;
+ observation.numeric = collision ? 1152921500312607400 : 13;
+ expect(detect().kind).toBe('replaced');
+ expect(observation.sampled).toBeGreaterThanOrEqual(3);
+ expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join('')).toContain(
+ ` inode=${collision ? 1152921500312607309n : 11n} `,
+ );
+ } finally {
+ await handle?.close();
+ for (const [name, prior] of signals) {
+ for (const listener of process.listeners(name))
+ if (!prior.has(listener)) process.removeListener(name, listener);
+ }
+ for (const [name, prior] of stdinEvents) {
+ for (const listener of process.stdin.listeners(name))
+ if (!prior.has(listener)) process.stdin.removeListener(name, listener);
+ }
+ if (platform) Object.defineProperty(process, 'platform', platform);
+ rmSync(root, { recursive: true, force: true });
+ }
+ },
+);
diff --git a/packages/cli/src/mcp/server.ts b/packages/cli/src/mcp/server.ts
index 3013e5349..c26b1e2cb 100644
--- a/packages/cli/src/mcp/server.ts
+++ b/packages/cli/src/mcp/server.ts
@@ -6,6 +6,7 @@ import { fileURLToPath } from 'node:url';
import { promisify } from 'node:util';
import { OK_PROJECT_MARKER, resolveIsHostedAgent } from '@inkeep/open-knowledge-core';
import { type KeepaliveHandle, startKeepalive } from '@inkeep/open-knowledge-core/keepalive';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import {
type AgentIdentity,
type Config,
@@ -27,6 +28,8 @@ import {
type BundleIdentityWatcherHandle,
captureBootIdentity,
detectBundleIdentity,
+ formatBootIdentityLine,
+ formatBundleReplacedLine,
startBundleIdentityWatcher,
} from './bundle-identity.ts';
import { type HostLivenessWatchHandle, startHostLivenessWatch } from './host-liveness.ts';
@@ -345,13 +348,11 @@ export async function startGlobalMcpServer(
if (process.platform === 'darwin') {
const bootIdentity = captureBootIdentity(BUNDLE_IDENTITY_ANCHOR, {
realpathSync,
- statInoSync: (p) => statSync(p).ino,
+ statInoSync: (p) => filesystemIdentity(statSync(p, { bigint: true })),
log: (m) => stderr.write(`${m}\n`),
});
if (bootIdentity !== undefined) {
- stderr.write(
- `[mcp] bundle identity anchor=${bootIdentity.resolvedPath} inode=${bootIdentity.inode} version=${RUNTIME_VERSION}\n`,
- );
+ stderr.write(`${formatBootIdentityLine(bootIdentity, RUNTIME_VERSION)}\n`);
const { resolvedPath: capturedAnchorPath, inode: capturedInode } = bootIdentity;
bundleWatcher = startBundleIdentityWatcher({
detect: () =>
@@ -360,12 +361,10 @@ export async function startGlobalMcpServer(
currentInode: capturedInode,
platform: process.platform,
realpath: realpathSync,
- statInode: (p) => statSync(p).ino,
+ statInode: (p) => filesystemIdentity(statSync(p, { bigint: true })),
}),
onReplaced: (state) => {
- stderr.write(
- `[mcp] bundle replaced anchor=${capturedAnchorPath} bootInode=${state.currentInode} onDiskInode=${state.onDiskInode} version=${RUNTIME_VERSION} — exiting for host respawn\n`,
- );
+ stderr.write(`${formatBundleReplacedLine(capturedAnchorPath, state, RUNTIME_VERSION)}\n`);
shutdown();
},
log: (msg) => stderr.write(`[mcp] ${msg}\n`),
diff --git a/packages/core/src/app-shell-csp.test.ts b/packages/core/src/app-shell-csp.test.ts
index 2e3621c64..14ebf2c66 100644
--- a/packages/core/src/app-shell-csp.test.ts
+++ b/packages/core/src/app-shell-csp.test.ts
@@ -62,7 +62,7 @@ describe('createAppShellPolicies', () => {
function serve(policyFor: ReturnType<typeof createAppShellPolicies>, file: string) {
const path = join(dist, file);
- return policyFor(statSync(path), path);
+ return policyFor({ file: path, stats: statSync(path) });
}
test('each served document gets its own policy, and other files get none', () => {
diff --git a/packages/core/src/app-shell-csp.ts b/packages/core/src/app-shell-csp.ts
index d176e849b..7052fbdcc 100644
--- a/packages/core/src/app-shell-csp.ts
+++ b/packages/core/src/app-shell-csp.ts
@@ -1,6 +1,7 @@
import { createHash } from 'node:crypto';
-import { readdirSync, readFileSync, type Stats, statSync } from 'node:fs';
+import { type BigIntStats, readdirSync, readFileSync, type Stats, statSync } from 'node:fs';
import { extname, join, resolve } from 'node:path';
+import { filesystemIdentity, statMtimeMs } from './filesystem-identity.ts';
// WARN: packages/app ships this page as public/assets/preview-frame.html and loads it from preview-iframe-header.ts; keep the paths in step.
const PREVIEW_FRAME_FILE = 'assets/preview-frame.html';
@@ -67,13 +68,22 @@ function folderChanged([folder, mtimeMs]: [string, number]): boolean {
}
}
-function fileIdentity(stats: Stats): string {
- return [stats.dev, stats.ino, stats.size, stats.mtimeMs].join(':');
+interface ServedDocument {
+ file: string;
+ stats: Pick<Stats, 'size' | 'mtimeMs'>;
+}
+
+function fileIdentity(
+ stats: Pick<BigIntStats, 'dev' | 'ino'>,
+ metadata: Pick<Stats, 'size' | 'mtimeMs'>,
+): string {
+ return `${filesystemIdentity(stats)}:${metadata.size}:${metadata.mtimeMs}`;
}
function currentIdentity(file: string): string | undefined {
try {
- return fileIdentity(statSync(file));
+ const stats = statSync(file, { bigint: true });
+ return fileIdentity(stats, { size: Number(stats.size), mtimeMs: statMtimeMs(stats) });
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
throw err;
@@ -83,13 +93,13 @@ function currentIdentity(file: string): string | undefined {
export interface AppShellPolicyFailure {
err: unknown;
root: string;
- file?: string;
+ file: string;
}
export function createAppShellPolicies(
bundleDir: string,
onFailure: (failure: AppShellPolicyFailure, message: string) => void,
-): (served: Stats, servedPath?: string) => string | undefined {
+): (served: ServedDocument) => string | undefined {
const root = resolve(bundleDir);
const previewFrame = resolve(root, PREVIEW_FRAME_FILE);
let listing: BundleListing | undefined;
@@ -102,8 +112,8 @@ export function createAppShellPolicies(
return listing;
}
- function servedDocument(served: Stats): string | undefined {
- const identity = fileIdentity(served);
+ function servedDocument(served: ServedDocument): string | undefined {
+ const identity = fileIdentity(statSync(served.file, { bigint: true }), served.stats);
const matches = currentListing().documents.filter((file) => currentIdentity(file) === identity);
if (matches.length > 1) {
throw new Error(`the served file matches more than one document: ${matches.join(', ')}`);
@@ -127,18 +137,18 @@ export function createAppShellPolicies(
}
}
- return (served, servedPath) => {
+ return (served) => {
let file: string | undefined;
try {
file = servedDocument(served);
} catch (err) {
onFailure(
- { err, root, file: servedPath },
+ { err, root, file: served.file },
'could not identify the app shell file being served; sending a policy that allows no inline script',
);
return NO_INLINE_SCRIPT_POLICY;
}
if (file === undefined) return undefined;
- return file === previewFrame ? PREVIEW_FRAME_CSP : documentPolicy(file, served.mtimeMs);
+ return file === previewFrame ? PREVIEW_FRAME_CSP : documentPolicy(file, served.stats.mtimeMs);
};
}
diff --git a/packages/core/src/filesystem-identity.test.ts b/packages/core/src/filesystem-identity.test.ts
new file mode 100644
index 000000000..da2ff75f8
--- /dev/null
+++ b/packages/core/src/filesystem-identity.test.ts
@@ -0,0 +1,16 @@
+import { expect, test } from 'vitest';
+import { statMtime, statMtimeMs } from './filesystem-identity.ts';
+
+test('preserves fractional milliseconds from a native nanosecond timestamp', () => {
+ expect(statMtimeMs({ mtimeNs: 1700000000123749971n })).toBe(1700000000123.75);
+});
+
+test('preserves numeric Stats date rounding', () => {
+ expect(statMtime({ mtimeNs: 1700000000123749971n }).toISOString()).toBe(
+ '2023-11-14T22:13:20.124Z',
+ );
+});
+
+test('preserves numeric Stats rounding before the epoch', () => {
+ expect(statMtimeMs({ mtimeNs: -1n })).toBe(-9.999999974752427e-7);
+});
diff --git a/packages/core/src/filesystem-identity.ts b/packages/core/src/filesystem-identity.ts
new file mode 100644
index 000000000..7a17ccf3d
--- /dev/null
+++ b/packages/core/src/filesystem-identity.ts
@@ -0,0 +1,15 @@
+import type { BigIntStats } from 'node:fs';
+
+export function filesystemIdentity(stats: Pick<BigIntStats, 'dev' | 'ino'>): `${bigint}:${bigint}` {
+ return `${stats.dev}:${stats.ino}`;
+}
+
+export function statMtimeMs(stats: Pick<BigIntStats, 'mtimeNs'>): number {
+ const nanoseconds = ((stats.mtimeNs % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n;
+ const seconds = (stats.mtimeNs - nanoseconds) / 1_000_000_000n;
+ return Number(seconds) * 1_000 + Number(nanoseconds) / 1_000_000;
+}
+
+export function statMtime(stats: Pick<BigIntStats, 'mtimeNs'>): Date {
+ return new Date(Math.round(statMtimeMs(stats)));
+}
diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts
index f3bebd7a9..619a08a4c 100644
--- a/packages/core/src/server.ts
+++ b/packages/core/src/server.ts
@@ -27,6 +27,7 @@ export {
writeConfigPatch,
} from './config/write-config-patch.ts';
export { okUserHomeDir } from './constants/ok-user-home.ts';
+export { filesystemIdentity, statMtime, statMtimeMs } from './filesystem-identity.ts';
export {
LOCALE_OVERRIDE_ENV_VAR,
type LocaleEnvironment,
diff --git a/packages/desktop/src/main/renderer-file-protocol.ts b/packages/desktop/src/main/renderer-file-protocol.ts
index 49b8bb059..cb6a597c3 100644
--- a/packages/desktop/src/main/renderer-file-protocol.ts
+++ b/packages/desktop/src/main/renderer-file-protocol.ts
@@ -26,7 +26,7 @@ export function installRendererFileProtocol(protocol: Protocol, rendererRoot: st
}
const policyFor = createAppShellPolicies(root, ({ err, file }, message) =>
- warnOnce([message, file ?? '', causeOf(err)].join('\u0000'), { err, file }, message),
+ warnOnce([message, file, causeOf(err)].join('\u0000'), { err, file }, message),
);
function refuse(status: number, target: string, reason: string, err?: unknown): Response {
@@ -61,7 +61,7 @@ export function installRendererFileProtocol(protocol: Protocol, rendererRoot: st
} catch (err) {
return refuse(500, file, 'could not load the file', err);
}
- const policy = response.ok ? policyFor(served, file) : undefined;
+ const policy = response.ok ? policyFor({ file, stats: served }) : undefined;
if (policy === undefined) return response;
const headers = new Headers(response.headers);
headers.set('Content-Security-Policy', policy);
diff --git a/packages/server/src/api-agent-write-file-index.test.ts b/packages/server/src/api-agent-write-file-index.test.ts
index 6cc580951..dde0f6010 100644
--- a/packages/server/src/api-agent-write-file-index.test.ts
+++ b/packages/server/src/api-agent-write-file-index.test.ts
@@ -100,7 +100,7 @@ describe('agent write self-registers the file index (PRD-7201)', () => {
size: 1,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
},
diff --git a/packages/server/src/api-backlinks.test.ts b/packages/server/src/api-backlinks.test.ts
index 724b6a224..d6b4c8c33 100644
--- a/packages/server/src/api-backlinks.test.ts
+++ b/packages/server/src/api-backlinks.test.ts
@@ -99,7 +99,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -109,7 +109,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -119,7 +119,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -293,7 +293,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -303,7 +303,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -313,7 +313,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -323,7 +323,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -424,7 +424,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -469,7 +469,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
]),
@@ -637,7 +637,7 @@ describe('graph endpoints', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -840,7 +840,7 @@ describe('POST /api/test-rescan-backlinks', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: '',
- inode: 0,
+ inode: '',
aliases: [],
},
],
@@ -850,7 +850,7 @@ describe('POST /api/test-rescan-backlinks', () => {
size: 10,
modified: new Date(0).toISOString(),
canonicalPath: join(skillDir, 'SKILL.md'),
- inode: 0,
+ inode: '',
aliases: [],
},
],
diff --git a/packages/server/src/api-document-list-ready-gate.test.ts b/packages/server/src/api-document-list-ready-gate.test.ts
index 79e7f3e04..ced541e16 100644
--- a/packages/server/src/api-document-list-ready-gate.test.ts
+++ b/packages/server/src/api-document-list-ready-gate.test.ts
@@ -80,7 +80,7 @@ describe('handleDocumentList ready gating', () => {
size: 7,
modified: new Date().toISOString(),
canonicalPath: join(tmp, 'log.md'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
});
@@ -88,7 +88,7 @@ describe('handleDocumentList ready gating', () => {
size: 9,
modified: new Date().toISOString(),
canonicalPath: join(tmp, 'notes.md'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
});
@@ -122,7 +122,7 @@ describe('handleDocumentList ready gating', () => {
size: 9,
modified: now,
canonicalPath: join(tmp, 'notes/page.md'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
},
@@ -133,7 +133,7 @@ describe('handleDocumentList ready gating', () => {
size: 64,
modified: now,
canonicalPath: join(tmp, 'data/example.csv'),
- inode: 1,
+ inode: '0:1',
aliases: [],
kind: 'file',
},
@@ -204,7 +204,7 @@ describe('handleDocumentList ready gating', () => {
size: 4,
modified: new Date().toISOString(),
canonicalPath: join(tmp, 'only.md'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
},
diff --git a/packages/server/src/api-extension.ts b/packages/server/src/api-extension.ts
index 25bcdb661..c526bff2b 100644
--- a/packages/server/src/api-extension.ts
+++ b/packages/server/src/api-extension.ts
@@ -1,3 +1,4 @@
+import { filesystemIdentity, statMtimeMs } from '@inkeep/open-knowledge-core/server';
import { createAgentIntegrationsRoutes } from './http/agent-integrations-routes.ts';
import { createAgentWriteRoutes } from './http/agent-write-routes.ts';
import { createTestRoutes } from './http/test-routes.ts';
@@ -1173,9 +1174,9 @@ function isCaseOnlySelfCollision(sourcePath: string, destinationPath: string): b
if (!existsSync(sourcePath) || !existsSync(destinationPath)) return false;
try {
- const sourceStat = statSync(sourcePath);
- const destinationStat = statSync(destinationPath);
- return sourceStat.dev === destinationStat.dev && sourceStat.ino === destinationStat.ino;
+ const sourceStat = statSync(sourcePath, { bigint: true });
+ const destinationStat = statSync(destinationPath, { bigint: true });
+ return filesystemIdentity(sourceStat) === filesystemIdentity(destinationStat);
} catch {
return false;
}
@@ -1668,7 +1669,7 @@ export function createApiExtension(options: ApiExtensionOptions): Extension & {
const index = getMutableFolderIndex();
if (!index) return;
try {
- const folderStat = statSync(fullPath);
+ const folderStat = statSync(fullPath, { bigint: true });
upsertFolderIndexEntryInIndex(index, contentDir, fullPath, folderStat, fullPath);
} catch (err) {
log.warn({ path: fullPath, err }, `folder index stat failed for ${fullPath}`);
@@ -2855,14 +2856,14 @@ export function createApiExtension(options: ApiExtensionOptions): Extension & {
forgetDocExtension(sourceDocName);
mutateFileIndex?.({ kind: 'delete', path: sourcePath, docName: sourceDocName });
- const destinationStat = statSync(destinationPath);
+ const destinationStat = statSync(destinationPath, { bigint: true });
mutateFileIndex?.({
kind: 'file-create',
path: destinationPath,
relativePath: toPath,
- size: destinationStat.size,
- modifiedTs: destinationStat.mtimeMs,
- inode: destinationStat.ino,
+ size: Number(destinationStat.size),
+ modifiedTs: statMtimeMs(destinationStat),
+ inode: filesystemIdentity(destinationStat),
});
const rewriteResult = await applyPendingAssetReferenceRewrites(
diff --git a/packages/server/src/api-file-ops.test.ts b/packages/server/src/api-file-ops.test.ts
index 57bfd7256..59ec30f86 100644
--- a/packages/server/src/api-file-ops.test.ts
+++ b/packages/server/src/api-file-ops.test.ts
@@ -14,6 +14,7 @@ import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { Readable } from 'node:stream';
import { Hocuspocus } from '@hocuspocus/server';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import simpleGit from 'simple-git';
import { afterEach, describe, expect, test, vi } from 'vitest';
import type * as Y from 'yjs';
@@ -70,12 +71,14 @@ function buildFileIndex(contentDir: string): ReadonlyMap<string, FileIndexEntry>
if (!entry.isFile() || !entry.name.endsWith('.md')) continue;
const stat = statSync(fullPath);
+
+ const statIdentity = filesystemIdentity(statSync(fullPath, { bigint: true }));
const docName = fullPath.slice(contentDir.length + 1).replace(/\.md$/, '');
index.set(docName, {
size: stat.size,
modified: stat.mtime.toISOString(),
canonicalPath: fullPath,
- inode: stat.ino,
+ inode: statIdentity,
aliases: [],
});
}
@@ -93,12 +96,13 @@ function buildFolderIndex(contentDir: string): ReadonlyMap<string, FolderIndexEn
if (!entry.isDirectory()) continue;
const fullPath = resolve(dir, entry.name);
const stat = statSync(fullPath);
+ const statIdentity = filesystemIdentity(statSync(fullPath, { bigint: true }));
const folderPath = fullPath.slice(contentDir.length + 1);
index.set(folderPath, {
size: 0,
modified: stat.mtime.toISOString(),
canonicalPath: fullPath,
- inode: stat.ino,
+ inode: statIdentity,
});
walk(fullPath);
}
@@ -2603,7 +2607,7 @@ describe('file operation API routes', () => {
size: event.content.length,
modified: new Date(0).toISOString(),
canonicalPath: event.path,
- inode: 0,
+ inode: '',
aliases: [],
});
} else if (event.kind === 'delete') {
@@ -3018,7 +3022,7 @@ describe('file operation API routes', () => {
size: 0,
modified: '2026-01-01T00:00:00.000Z',
canonicalPath: join(dir, 'empty'),
- inode: 1,
+ inode: '0:1',
},
],
]);
diff --git a/packages/server/src/api-folder-rename-disk-enum.test.ts b/packages/server/src/api-folder-rename-disk-enum.test.ts
index 8338fd9f3..a55af5b2c 100644
--- a/packages/server/src/api-folder-rename-disk-enum.test.ts
+++ b/packages/server/src/api-folder-rename-disk-enum.test.ts
@@ -286,7 +286,7 @@ describe('managed rename corpus discovery', () => {
size: 18,
modified: new Date(0).toISOString(),
canonicalPath: join(contentDir, 'late.md'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'markdown',
},
diff --git a/packages/server/src/api-pages.test.ts b/packages/server/src/api-pages.test.ts
index 17ada0b16..91b99a375 100644
--- a/packages/server/src/api-pages.test.ts
+++ b/packages/server/src/api-pages.test.ts
@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Readable } from 'node:stream';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { describe, expect, test } from 'vitest';
import {
createApiExtension,
@@ -222,11 +223,12 @@ function buildFileIndex(dir: string, base = ''): ReadonlyMap<string, FileIndexEn
}
} else if (entry.isFile() && entry.name.endsWith('.md')) {
const st = statSync(join(dir, entry.name));
+ const stIdentity = filesystemIdentity(statSync(join(dir, entry.name), { bigint: true }));
index.set(rel.slice(0, -3), {
size: st.size,
modified: st.mtime.toISOString(),
canonicalPath: join(dir, entry.name),
- inode: st.ino,
+ inode: stIdentity,
aliases: [],
});
}
@@ -321,7 +323,7 @@ describe('GET /api/pages', () => {
size: 123,
modified: new Date(0).toISOString(),
canonicalPath: join(dir, 'ghost.md'),
- inode: 1,
+ inode: '0:1',
aliases: [],
kind: 'markdown',
title: 'Cached Title',
@@ -353,7 +355,7 @@ describe('GET /api/pages', () => {
size: 0,
modified: new Date(0).toISOString(),
canonicalPath: join(dir, 'orphan.md'),
- inode: 1,
+ inode: '0:1',
aliases: [],
kind: 'markdown',
},
diff --git a/packages/server/src/api-search-incremental.test.ts b/packages/server/src/api-search-incremental.test.ts
index 12f556b6c..ff066d802 100644
--- a/packages/server/src/api-search-incremental.test.ts
+++ b/packages/server/src/api-search-incremental.test.ts
@@ -88,7 +88,7 @@ function createHarness(contentDir: string, options: { withGeneration: boolean })
size: Buffer.byteLength(content),
modified,
canonicalPath: abs,
- inode: ++stampSeq,
+ inode: `0:${++stampSeq}`,
aliases: [],
kind: 'markdown',
});
@@ -101,7 +101,7 @@ function createHarness(contentDir: string, options: { withGeneration: boolean })
size: 0,
modified: nextStamp(),
canonicalPath: join(contentDir, docName),
- inode: ++stampSeq,
+ inode: `0:${++stampSeq}`,
aliases: [],
kind: 'markdown',
...entry,
diff --git a/packages/server/src/api-search-semantic.test.ts b/packages/server/src/api-search-semantic.test.ts
index 0caead0dd..9ecd4a953 100644
--- a/packages/server/src/api-search-semantic.test.ts
+++ b/packages/server/src/api-search-semantic.test.ts
@@ -17,6 +17,7 @@ import {
type SemanticProviderErrorReason,
type SemanticQueryOutcome,
} from '@inkeep/open-knowledge-core';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { describe, expect, test } from 'vitest';
import { createApiExtension } from './api-extension.test-helper.ts';
import { createConceptEmbedder, type Embedder, SemanticSearchService } from './embeddings/index.ts';
@@ -83,11 +84,12 @@ function buildFileIndex(dir: string, base = ''): ReadonlyMap<string, FileIndexEn
for (const [k, v] of buildFileIndex(join(dir, entry.name), rel)) index.set(k, v);
} else if (entry.isFile() && entry.name.endsWith('.md')) {
const stat = statSync(join(dir, entry.name));
+ const statIdentity = filesystemIdentity(statSync(join(dir, entry.name), { bigint: true }));
index.set(rel.slice(0, -3), {
size: stat.size,
modified: stat.mtime.toISOString(),
canonicalPath: join(dir, entry.name),
- inode: stat.ino,
+ inode: statIdentity,
aliases: [],
});
}
diff --git a/packages/server/src/api-search.test.ts b/packages/server/src/api-search.test.ts
index 45a6940f5..421e51104 100644
--- a/packages/server/src/api-search.test.ts
+++ b/packages/server/src/api-search.test.ts
@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Readable } from 'node:stream';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { describe, expect, test } from 'vitest';
import {
createApiExtension,
@@ -49,11 +50,12 @@ function buildFileIndex(dir: string, base = ''): ReadonlyMap<string, FileIndexEn
}
} else if (entry.isFile() && entry.name.endsWith('.md')) {
const stat = statSync(join(dir, entry.name));
+ const statIdentity = filesystemIdentity(statSync(join(dir, entry.name), { bigint: true }));
index.set(rel.slice(0, -3), {
size: stat.size,
modified: stat.mtime.toISOString(),
canonicalPath: join(dir, entry.name),
- inode: stat.ino,
+ inode: statIdentity,
aliases: [],
kind: 'markdown',
});
@@ -272,11 +274,12 @@ describe('GET /api/search', () => {
function indexEntry(canonicalPath: string, kind: FileIndexEntry['kind']): FileIndexEntry {
const stat = statSync(canonicalPath);
+ const statIdentity = filesystemIdentity(statSync(canonicalPath, { bigint: true }));
return {
size: stat.size,
modified: stat.mtime.toISOString(),
canonicalPath,
- inode: stat.ino,
+ inode: statIdentity,
aliases: [],
kind,
};
@@ -732,7 +735,7 @@ describe('GET /api/search — operational budget (D15)', () => {
size: 1,
modified: new Date(0).toISOString(),
canonicalPath: join(dir, 'does-not-exist.csv'),
- inode: 1,
+ inode: '0:1',
aliases: [],
kind: 'file',
},
@@ -758,7 +761,7 @@ describe('GET /api/search — operational budget (D15)', () => {
size: 1,
modified: new Date(0).toISOString(),
canonicalPath: join(dir, `f${i}`),
- inode: i + 1,
+ inode: `0:${i + 1}`,
aliases: [],
kind: 'file',
});
@@ -1003,7 +1006,7 @@ describe('GET /api/search — incremental corpus (per-page document cache)', ()
size: 0,
modified: new Date(0).toISOString(),
canonicalPath: alphaPath,
- inode: 424242,
+ inode: '0:424242',
aliases: [],
kind: 'markdown',
};
diff --git a/packages/server/src/api-suggest-links.test.ts b/packages/server/src/api-suggest-links.test.ts
index a79ad783c..aee6072bb 100644
--- a/packages/server/src/api-suggest-links.test.ts
+++ b/packages/server/src/api-suggest-links.test.ts
@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Readable } from 'node:stream';
import type { Hocuspocus } from '@hocuspocus/server';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { describe, expect, test } from 'vitest';
import { createApiExtension } from './api-extension.test-helper.ts';
import type { FileIndexEntry } from './file-watcher.ts';
@@ -44,11 +45,12 @@ function buildFileIndex(
for (const docName of docNames) {
const filePath = join(contentDir, `${docName}.md`);
const stats = statSync(filePath);
+ const statsIdentity = filesystemIdentity(statSync(filePath, { bigint: true }));
index.set(docName, {
size: stats.size,
modified: stats.mtime.toISOString(),
canonicalPath: filePath,
- inode: stats.ino,
+ inode: statsIdentity,
aliases: [],
});
}
diff --git a/packages/server/src/api-trash-cleanup.test.ts b/packages/server/src/api-trash-cleanup.test.ts
index e6e8bbb97..098d21c14 100644
--- a/packages/server/src/api-trash-cleanup.test.ts
+++ b/packages/server/src/api-trash-cleanup.test.ts
@@ -12,6 +12,7 @@ import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { Readable } from 'node:stream';
import type { Principal } from '@inkeep/open-knowledge-core';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { afterEach, describe, expect, test } from 'vitest';
import { createApiExtension } from './api-extension.test-helper.ts';
import { BacklinkIndex } from './backlink-index.ts';
@@ -58,12 +59,13 @@ function buildFileIndex(contentDir: string): Map<string, FileIndexEntry> {
}
if (!entry.isFile() || !entry.name.endsWith('.md')) continue;
const stat = statSync(fullPath);
+ const statIdentity = filesystemIdentity(statSync(fullPath, { bigint: true }));
const docName = fullPath.slice(contentDir.length + 1).replace(/\.md$/, '');
index.set(docName, {
size: stat.size,
modified: stat.mtime.toISOString(),
canonicalPath: fullPath,
- inode: stat.ino,
+ inode: statIdentity,
aliases: [],
});
}
@@ -79,12 +81,13 @@ function buildFolderIndex(contentDir: string): Map<string, FolderIndexEntry> {
if (!entry.isDirectory()) continue;
const fullPath = resolve(dir, entry.name);
const stat = statSync(fullPath);
+ const statIdentity = filesystemIdentity(statSync(fullPath, { bigint: true }));
const folderPath = fullPath.slice(contentDir.length + 1);
index.set(folderPath, {
size: 0,
modified: stat.mtime.toISOString(),
canonicalPath: fullPath,
- inode: stat.ino,
+ inode: statIdentity,
});
walk(fullPath);
}
diff --git a/packages/server/src/api-write-link-policy.test.ts b/packages/server/src/api-write-link-policy.test.ts
index 1b5aafdff..9a6be7c3b 100644
--- a/packages/server/src/api-write-link-policy.test.ts
+++ b/packages/server/src/api-write-link-policy.test.ts
@@ -314,7 +314,7 @@ describe('write-time advisories for links to folders that hold no document', ()
new Map(
folders.map((folder) => [
folder,
- { size: 0 as const, modified: '', canonicalPath: join(contentDir, folder), inode: 0 },
+ { size: 0 as const, modified: '', canonicalPath: join(contentDir, folder), inode: '' },
]),
),
});
@@ -382,7 +382,7 @@ describe('write-time advisories for slash-free wiki asset embeds', () => {
size: 3,
modified: '',
canonicalPath: join(contentDir, 'media', 'photo.png'),
- inode: 0,
+ inode: '',
aliases: [],
kind: 'file' as const,
},
diff --git a/packages/server/src/asset-references.test.ts b/packages/server/src/asset-references.test.ts
index ec593bffe..e71cba683 100644
--- a/packages/server/src/asset-references.test.ts
+++ b/packages/server/src/asset-references.test.ts
@@ -391,7 +391,7 @@ describe('asset reference extraction', () => {
size: 1,
modified: now,
canonicalPath: join(dir, 'docs/guide.md'),
- inode: 1,
+ inode: '0:1',
aliases: [],
},
],
@@ -401,7 +401,7 @@ describe('asset reference extraction', () => {
size: 1,
modified: now,
canonicalPath: join(dir, 'docs/second.md'),
- inode: 2,
+ inode: '0:2',
aliases: [],
},
],
@@ -477,7 +477,7 @@ describe('asset reference extraction', () => {
size: 1,
modified: now,
canonicalPath: join(dir, 'fishing-log/log.md'),
- inode: 1,
+ inode: '0:1',
aliases: [],
},
],
diff --git a/packages/server/src/asset-walk.ts b/packages/server/src/asset-walk.ts
index 75406e580..eac44be37 100644
--- a/packages/server/src/asset-walk.ts
+++ b/packages/server/src/asset-walk.ts
@@ -1,4 +1,4 @@
-import type { Dirent, Stats } from 'node:fs';
+import type { BigIntStats, Dirent } from 'node:fs';
import { readdirSync } from 'node:fs';
import { lstat, readdir, realpath, stat } from 'node:fs/promises';
import { join, relative } from 'node:path';
@@ -7,6 +7,7 @@ import {
type BasenameIndex,
LINKABLE_ASSET_EXTENSIONS,
} from '@inkeep/open-knowledge-core';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import type { ContentFilter } from './content-filter.ts';
import { resolveDirectoryRoot } from './directory-root.ts';
import { isSupportedAssetFile } from './doc-extensions.ts';
@@ -53,7 +54,7 @@ export function seedSingleDirBasenameIndex(opts: {
export async function seedBasenameIndex(opts: SeedOptions): Promise<void> {
const root = resolveDirectoryRoot(opts.contentDir, { root: 'content', component: 'asset-walk' });
- const visited = new Set<number>();
+ const visited = new Set<string>();
async function walk(dir: string): Promise<void> {
let entries: Dirent[];
@@ -70,9 +71,9 @@ export async function seedBasenameIndex(opts: SeedOptions): Promise<void> {
if (rel.startsWith('..')) continue;
if (opts.contentFilter?.isDirExcluded(rel) && entry.isDirectory()) continue;
- let entryStat: Stats;
+ let entryStat: BigIntStats;
try {
- entryStat = await lstat(full);
+ entryStat = await lstat(full, { bigint: true });
} catch (err) {
const code = errnoCode(err);
if (code !== 'ENOENT') opts.onSkip?.('lstat-failed', code, full);
@@ -92,16 +93,16 @@ export async function seedBasenameIndex(opts: SeedOptions): Promise<void> {
opts.onSkip?.('symlink-escape', undefined, full);
continue;
}
- let realStat: Stats;
+ let realStat: BigIntStats;
try {
- realStat = await stat(canonical);
+ realStat = await stat(canonical, { bigint: true });
} catch (err) {
const code = errnoCode(err);
if (code !== 'ENOENT') opts.onSkip?.('symlink-stat-failed', code, canonical);
continue;
}
- if (visited.has(realStat.ino)) continue;
- visited.add(realStat.ino);
+ if (visited.has(filesystemIdentity(realStat))) continue;
+ visited.add(filesystemIdentity(realStat));
if (realStat.isDirectory()) await walk(canonical);
else if (
realStat.isFile() &&
@@ -114,8 +115,8 @@ export async function seedBasenameIndex(opts: SeedOptions): Promise<void> {
}
if (entryStat.isDirectory()) {
- if (visited.has(entryStat.ino)) continue;
- visited.add(entryStat.ino);
+ if (visited.has(filesystemIdentity(entryStat))) continue;
+ visited.add(filesystemIdentity(entryStat));
await walk(full);
continue;
}
diff --git a/packages/server/src/document-list.test.ts b/packages/server/src/document-list.test.ts
index 6b9f7726b..1b60074a5 100644
--- a/packages/server/src/document-list.test.ts
+++ b/packages/server/src/document-list.test.ts
@@ -19,19 +19,25 @@ describe('document listing from file index', () => {
const sampleIndex = new Map<string, FileIndexEntry>([
[
'README',
- { size: 50, modified: now, canonicalPath: '/test/README.md', inode: 0, aliases: [] },
+ { size: 50, modified: now, canonicalPath: '/test/README.md', inode: '', aliases: [] },
],
[
'docs/guide',
- { size: 200, modified: now, canonicalPath: '/test/docs/guide.md', inode: 0, aliases: [] },
+ { size: 200, modified: now, canonicalPath: '/test/docs/guide.md', inode: '', aliases: [] },
],
[
'docs/setup',
- { size: 150, modified: now, canonicalPath: '/test/docs/setup.md', inode: 0, aliases: [] },
+ { size: 150, modified: now, canonicalPath: '/test/docs/setup.md', inode: '', aliases: [] },
],
[
'articles/intro',
- { size: 100, modified: now, canonicalPath: '/test/articles/intro.md', inode: 0, aliases: [] },
+ {
+ size: 100,
+ modified: now,
+ canonicalPath: '/test/articles/intro.md',
+ inode: '',
+ aliases: [],
+ },
],
[
'articles/nested/deep',
@@ -39,7 +45,7 @@ describe('document listing from file index', () => {
size: 80,
modified: now,
canonicalPath: '/test/articles/nested/deep.md',
- inode: 0,
+ inode: '',
aliases: [],
},
],
diff --git a/packages/server/src/file-watcher-settle.test.ts b/packages/server/src/file-watcher-settle.test.ts
index aa87b2e28..4dc69318d 100644
--- a/packages/server/src/file-watcher-settle.test.ts
+++ b/packages/server/src/file-watcher-settle.test.ts
@@ -348,7 +348,7 @@ test('with no settle pending, a batch and a dropped-events recovery are handled
relativePath: 'notes.txt',
size: 6,
modifiedTs: expect.any(Number),
- inode: expect.any(Number),
+ inode: expect.any(String),
},
{ kind: 'asset-delete', path: notes, relativePath: 'notes.txt' },
{ kind: 'file-delete', path: notes, relativePath: 'notes.txt' },
diff --git a/packages/server/src/file-watcher.test.ts b/packages/server/src/file-watcher.test.ts
index aebd8c412..e64aee48b 100644
--- a/packages/server/src/file-watcher.test.ts
+++ b/packages/server/src/file-watcher.test.ts
@@ -1715,7 +1715,7 @@ describe('startWatcher symlink handling', () => {
const entry = index.get('target');
expect(entry).toBeTruthy();
expect(entry?.canonicalPath).toBe(targetPath);
- expect(entry?.inode).toBeGreaterThan(0);
+ expect(entry?.inode).toMatch(/^\d+:[1-9]\d*$/);
expect(entry?.aliases).toContain('link');
} finally {
await handle.unsubscribe();
@@ -1988,7 +1988,7 @@ describe('startWatcher symlink handling', () => {
const entry = handle.getFileIndex().get('regular');
expect(entry).toBeTruthy();
expect(entry?.canonicalPath).toBe(resolve(contentDir, 'regular.md'));
- expect(entry?.inode).toBeGreaterThan(0);
+ expect(entry?.inode).toMatch(/^\d+:[1-9]\d*$/);
expect(entry?.aliases).toEqual([]);
} finally {
await handle.unsubscribe();
diff --git a/packages/server/src/file-watcher.ts b/packages/server/src/file-watcher.ts
index e26b6cc70..e65963ba3 100644
--- a/packages/server/src/file-watcher.ts
+++ b/packages/server/src/file-watcher.ts
@@ -1,5 +1,6 @@
import { randomUUID } from 'node:crypto';
import {
+ type BigIntStats,
type Dirent,
existsSync,
lstatSync,
@@ -13,6 +14,7 @@ import {
import { lstat, readdir, readFile, realpath, stat } from 'node:fs/promises';
import { basename, dirname, extname, isAbsolute, join, relative, sep } from 'node:path';
import { LINKABLE_ASSET_EXTENSIONS } from '@inkeep/open-knowledge-core';
+import { filesystemIdentity, statMtime } from '@inkeep/open-knowledge-core/server';
import { isConfigDoc, isReservedForUserTree, isSystemDoc } from './cc1-broadcast.ts';
import { type ContentFilter, WATCHER_STRUCTURAL_IGNORE_DIRS } from './content-filter.ts';
import { isWithinContentDir } from './content-path.ts';
@@ -98,7 +100,7 @@ type FileDiskEvent =
relativePath: string;
size: number;
modifiedTs: number;
- inode: number;
+ inode: string;
}
| {
kind: 'file-update';
@@ -106,7 +108,7 @@ type FileDiskEvent =
relativePath: string;
size: number;
modifiedTs: number;
- inode: number;
+ inode: string;
}
| { kind: 'file-delete'; path: string; relativePath: string };
@@ -127,7 +129,7 @@ export interface FileIndexEntry {
size: number;
modified: string;
canonicalPath: string;
- inode: number;
+ inode: string;
aliases: string[];
kind: 'markdown' | 'file';
fileMembers?: GeneralFileMembers;
@@ -198,7 +200,7 @@ export interface FolderIndexEntry {
size: 0;
modified: string;
canonicalPath: string;
- inode: number;
+ inode: string;
}
class WatcherFolderIndex extends Map<string, FolderIndexEntry> {
@@ -583,16 +585,16 @@ export function upsertFolderIndexEntry(
folderIndex: Map<string, FolderIndexEntry>,
contentDir: string,
folderPath: string,
- stat: { mtime: Date; ino: number | bigint },
+ stat: Pick<BigIntStats, 'dev' | 'ino' | 'mtimeNs'>,
canonicalPath = folderPath,
): string | null {
const relativePath = contentRelativePath(contentDir, folderPath);
if (!relativePath) return null;
folderIndex.set(relativePath, {
size: 0,
- modified: stat.mtime.toISOString(),
+ modified: statMtime(stat).toISOString(),
canonicalPath,
- inode: Number(stat.ino),
+ inode: filesystemIdentity(stat),
});
return relativePath;
}
@@ -1050,7 +1052,7 @@ interface DiskObservation {
complete: boolean;
}
-type GeneralIdentity = { device: number; inode: number };
+type GeneralIdentity = ReturnType<typeof filesystemIdentity>;
type KnownGeneralEntry = FileIndexEntry & { kind: 'file'; fileMembers: GeneralFileMembers };
type GeneralFileState =
| { kind: 'known'; entry: KnownGeneralEntry; identity: GeneralIdentity; sequence: number }
@@ -1068,11 +1070,7 @@ function knownGeneralState(
}
function sameGeneralIdentity(left: GeneralIdentity, right: GeneralIdentity): boolean {
- return left.device === right.device && left.inode === right.inode;
-}
-
-function generalIdentityKey(identity: GeneralIdentity): string {
- return `${identity.device}:${identity.inode}`;
+ return left === right;
}
function addGeneralOwner(owners: Map<string, Set<string>>, key: string, name: string): void {
@@ -1146,7 +1144,7 @@ class GeneralFileIndex implements ReadonlyMap<string, FileIndexEntry> {
}
ownersOfIdentity(identity: GeneralIdentity): ReadonlySet<string> | undefined {
- return this.#identities.get(generalIdentityKey(identity));
+ return this.#identities.get(identity);
}
set(name: string, entry: FileIndexEntry): void {
@@ -1175,7 +1173,7 @@ class GeneralFileIndex implements ReadonlyMap<string, FileIndexEntry> {
);
for (const member of members) addGeneralOwner(this.#members, member, name);
const state = knownGeneralState(entry);
- const identity = state ? generalIdentityKey(state.identity) : null;
+ const identity = state ? state.identity : null;
if (identity !== null) addGeneralOwner(this.#identities, identity, name);
this.#linked.set(name, { members, identity });
}
@@ -1247,7 +1245,7 @@ function storeKnownGeneralGroup(
const entry: KnownGeneralEntry = {
kind: 'file',
canonicalPath: join(contentDir, representative),
- inode: identity.inode,
+ inode: identity,
size: metadata.size,
modified: metadata.modified,
aliases: relations.map((relation) => relation.path),
@@ -1267,7 +1265,7 @@ function storeUnresolvedGeneralEntry(
const entry: FileIndexEntry = {
kind: 'file',
canonicalPath: event.path,
- inode: event.inode || prior?.inode || 0,
+ inode: event.inode || prior?.inode || '',
size: event.size,
modified: new Date(event.modifiedTs).toISOString(),
aliases:
@@ -1386,16 +1384,15 @@ function registerKnownGeneralPath(
changes.set(relation.path, null);
continue;
}
- const targetStat = targetPath === canonicalName ? null : statSync(actualTarget);
+ const targetStat =
+ targetPath === canonicalName ? null : statSync(actualTarget, { bigint: true });
if (targetStat && !targetStat.isFile()) {
changes.set(relation.path, null);
continue;
}
- const targetIdentity = targetStat
- ? { device: targetStat.dev, inode: targetStat.ino }
- : identity;
+ const targetIdentity = targetStat ? filesystemIdentity(targetStat) : identity;
const targetMetadata = targetStat
- ? { size: targetStat.size, modified: targetStat.mtime.toISOString() }
+ ? { size: Number(targetStat.size), modified: statMtime(targetStat).toISOString() }
: metadata;
const existingTarget = selectedFact(targetPath);
if (
@@ -1462,7 +1459,7 @@ function registerKnownGeneralPath(
if (!entry) continue;
addGeneralEntryFacts(previousFacts, name, entry);
const state = knownGeneralState(entry);
- const key = state && generalIdentityKey(state.identity);
+ const key = state?.identity;
if (key && !representatives.has(key)) representatives.set(key, name);
}
const selected = new Map(previousFacts);
@@ -1471,7 +1468,7 @@ function registerKnownGeneralPath(
else selected.delete(path);
}
const metadataEffects = new Map<string, GeneralMetadataEffect>([
- [generalIdentityKey(identity), { ...metadata, sequence }],
+ [identity, { ...metadata, sequence }],
]);
for (const name of affected) index.delete(name);
storeGeneralFacts(index, contentDir, selected, representatives, metadataEffects);
@@ -1590,7 +1587,7 @@ function generalRepresentativeNames(
for (const [name, entry] of index) {
const state = knownGeneralState(entry);
if (!state) continue;
- const key = generalIdentityKey(state.identity);
+ const key = state.identity;
if (!names.has(key)) names.set(key, name);
}
return names;
@@ -1626,7 +1623,7 @@ function storeGeneralFacts(
>();
for (const fact of facts.values()) {
if (fact.kind !== 'regular') continue;
- const key = generalIdentityKey(fact.identity);
+ const key = fact.identity;
const group = grouped.get(key);
if (group) {
group.regularPaths.push(fact.path);
@@ -1648,7 +1645,7 @@ function storeGeneralFacts(
if (fact.kind !== 'symlink') continue;
const target = facts.get(fact.targetPath);
if (target?.kind !== 'regular') continue;
- grouped.get(generalIdentityKey(target.identity))?.symlinks.push({
+ grouped.get(target.identity)?.symlinks.push({
path: fact.path,
targetPath: fact.targetPath,
});
@@ -1686,7 +1683,7 @@ function materializeScanGeneralIndex(
const representatives = generalRepresentativeNames(observation.generalFileIndex);
for (const fact of scan.facts.values()) {
if (fact.kind !== 'regular') continue;
- const key = generalIdentityKey(fact.identity);
+ const key = fact.identity;
if (!representatives.has(key)) representatives.set(key, fact.path);
}
const next = new GeneralFileIndex(contentDir);
@@ -1758,7 +1755,7 @@ function recordScannedGeneralPath(
sequence,
});
}
- scan.metadataEffects.set(generalIdentityKey(identity), { ...metadata, sequence });
+ scan.metadataEffects.set(identity, { ...metadata, sequence });
return 'applied';
}
@@ -1783,7 +1780,7 @@ function recordGeneralPublicEffect(
if (event.kind === 'file-delete') return;
const fact = facts.get(canonicalName) ?? facts.get(event.relativePath);
if (fact?.kind !== 'regular' && fact?.kind !== 'symlink') return;
- record.generalMetadata.set(generalIdentityKey(fact.identity), {
+ record.generalMetadata.set(fact.identity, {
size: event.size,
modified: new Date(event.modifiedTs).toISOString(),
sequence: fact.sequence,
@@ -1814,18 +1811,18 @@ async function scanDisk(
contentFilter: ContentFilter | undefined,
observation: DiskObservation,
generalScan: GeneralScanAccumulator,
- visitedInodes?: Set<number>,
- generalVisitedInodes?: Set<number>,
+ visitedInodes?: Set<string>,
+ generalVisitedInodes?: Set<string>,
): Promise<void> {
- const visited = visitedInodes ?? new Set<number>();
- const generalVisited = generalVisitedInodes ?? new Set<number>();
+ const visited = visitedInodes ?? new Set<string>();
+ const generalVisited = generalVisitedInodes ?? new Set<string>();
try {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
- let lst: Stats;
+ let lst: BigIntStats;
try {
- lst = await lstat(fullPath);
+ lst = await lstat(fullPath, { bigint: true });
} catch (e) {
const code = errnoCode(e);
if (code !== 'ENOENT') {
@@ -1834,6 +1831,7 @@ async function scanDisk(
}
continue;
}
+ const lstIdentity = filesystemIdentity(lst);
if (lst.isSymbolicLink()) {
let canonical: string;
@@ -1862,7 +1860,8 @@ async function scanDisk(
}
try {
- const canonStat = await stat(canonical);
+ const canonStat = await stat(canonical, { bigint: true });
+ const canonIdentity = filesystemIdentity(canonStat);
if (canonStat.isFile() && !isSupportedDocFile(entry.name)) {
const canonicalName = toPosix(relative(contentDir, canonical));
const lexicalName = toPosix(relative(contentDir, fullPath));
@@ -1875,9 +1874,9 @@ async function scanDisk(
isConfigDoc(lexicalName)
)
continue;
- if (visited.has(canonStat.ino) && !generalVisited.has(canonStat.ino)) continue;
- visited.add(canonStat.ino);
- generalVisited.add(canonStat.ino);
+ if (visited.has(canonIdentity) && !generalVisited.has(canonIdentity)) continue;
+ visited.add(canonIdentity);
+ generalVisited.add(canonIdentity);
const registered = recordScannedGeneralPath(
observation,
generalScan,
@@ -1885,15 +1884,15 @@ async function scanDisk(
canonicalName,
lexicalName,
true,
- { device: canonStat.dev, inode: canonStat.ino },
- { size: canonStat.size, modified: canonStat.mtime.toISOString() },
+ canonIdentity,
+ { size: Number(canonStat.size), modified: statMtime(canonStat).toISOString() },
contentFilter,
);
if (registered === 'unobserved') observation.complete = false;
observation.paths.add(fullPath);
continue;
}
- if (visited.has(canonStat.ino)) {
+ if (visited.has(canonIdentity)) {
if (canonStat.isFile() && isSupportedDocFile(entry.name)) {
const aliasDocName = pathToDocName(fullPath, contentDir);
const canonicalDocName = pathToDocName(canonical, contentDir);
@@ -1916,7 +1915,7 @@ async function scanDisk(
}
continue;
}
- visited.add(canonStat.ino);
+ visited.add(canonIdentity);
if (canonStat.isDirectory()) {
const relPath = contentRelativePath(contentDir, fullPath);
@@ -1956,10 +1955,10 @@ async function scanDisk(
const ext = extractDocExtension(canonical);
if (ext && !recordObservedExtension(observation, canonicalDocName, ext)) continue;
observation.fileIndex.set(canonicalDocName, {
- size: canonStat.size,
- modified: canonStat.mtime.toISOString(),
+ size: Number(canonStat.size),
+ modified: statMtime(canonStat).toISOString(),
canonicalPath: canonical,
- inode: canonStat.ino,
+ inode: canonIdentity,
aliases: [aliasDocName],
kind: 'markdown',
...derivePageMeta(content, canonicalDocName),
@@ -2000,8 +1999,8 @@ async function scanDisk(
generalVisited,
);
} else if (lst.isFile() && isSupportedDocFile(entry.name)) {
- if (visited.has(lst.ino)) continue;
- visited.add(lst.ino);
+ if (visited.has(lstIdentity)) continue;
+ visited.add(lstIdentity);
if (contentFilter) {
const relPath = toPosix(relative(contentDir, fullPath));
@@ -2016,10 +2015,10 @@ async function scanDisk(
const ext = extractDocExtension(fullPath);
if (ext && !recordObservedExtension(observation, docName, ext)) continue;
observation.fileIndex.set(docName, {
- size: lst.size,
- modified: lst.mtime.toISOString(),
+ size: Number(lst.size),
+ modified: statMtime(lst).toISOString(),
canonicalPath: fullPath,
- inode: lst.ino,
+ inode: lstIdentity,
aliases: [],
kind: 'markdown',
...derivePageMeta(content, docName),
@@ -2044,9 +2043,9 @@ async function scanDisk(
}
const relativePath = toPosix(relative(contentDir, fullPath));
if (isSystemDoc(relativePath) || isConfigDoc(relativePath)) continue;
- if (visited.has(lst.ino) && !generalVisited.has(lst.ino)) continue;
- visited.add(lst.ino);
- generalVisited.add(lst.ino);
+ if (visited.has(lstIdentity) && !generalVisited.has(lstIdentity)) continue;
+ visited.add(lstIdentity);
+ generalVisited.add(lstIdentity);
const registered = recordScannedGeneralPath(
observation,
generalScan,
@@ -2054,8 +2053,8 @@ async function scanDisk(
relativePath,
relativePath,
false,
- { device: lst.dev, inode: lst.ino },
- { size: lst.size, modified: lst.mtime.toISOString() },
+ lstIdentity,
+ { size: Number(lst.size), modified: statMtime(lst).toISOString() },
contentFilter,
);
if (registered === 'unobserved') observation.complete = false;
@@ -2239,7 +2238,7 @@ function updateGeneralFileIndex(
size: event.size,
modified: new Date(event.modifiedTs).toISOString(),
canonicalPath,
- inode: event.inode || (prior?.kind === 'file' ? prior.inode : 0),
+ inode: event.inode || (prior?.kind === 'file' ? prior.inode : ''),
aliases: [...aliases],
kind: 'file',
});
@@ -2274,7 +2273,7 @@ export function updateFileIndex(event: DiskEvent, fileIndex: Map<string, FileInd
size: event.size,
modified: new Date(event.modifiedTs).toISOString(),
canonicalPath: existing?.canonicalPath ?? event.path,
- inode: event.inode || existing?.inode || 0,
+ inode: event.inode || existing?.inode || '',
aliases: existing?.aliases ?? [],
kind: 'file',
});
@@ -2294,7 +2293,7 @@ export function updateFileIndex(event: DiskEvent, fileIndex: Map<string, FileInd
size: Buffer.byteLength(event.content, 'utf-8'),
modified: new Date().toISOString(),
canonicalPath: existing?.canonicalPath ?? event.path,
- inode: existing?.inode ?? 0,
+ inode: existing?.inode ?? '',
aliases: existing?.aliases ?? [],
kind: 'markdown',
...derivePageMeta(event.content, docName),
@@ -2326,7 +2325,7 @@ export function updateFileIndex(event: DiskEvent, fileIndex: Map<string, FileInd
size: Buffer.byteLength(event.content, 'utf-8'),
modified: new Date().toISOString(),
canonicalPath: event.newPath,
- inode: existing?.inode ?? 0,
+ inode: existing?.inode ?? '',
aliases: existing?.aliases ?? [],
kind: 'markdown',
...derivePageMeta(event.content, event.newDocName),
@@ -2358,9 +2357,9 @@ function updateFolderIndexFromRawEvents(
continue;
}
- let lst: ReturnType<typeof lstatSync>;
+ let lst: BigIntStats;
try {
- lst = lstatSync(raw.path);
+ lst = lstatSync(raw.path, { bigint: true });
} catch (err) {
const code = errnoCode(err);
if (code !== 'ENOENT') {
@@ -2369,7 +2368,7 @@ function updateFolderIndexFromRawEvents(
continue;
}
- let folderStat: ReturnType<typeof statSync> | null = null;
+ let folderStat: BigIntStats | null = null;
let canonicalPath = raw.path;
if (lst.isDirectory()) {
folderStat = lst;
@@ -2377,7 +2376,7 @@ function updateFolderIndexFromRawEvents(
try {
canonicalPath = resolveNativePath(raw.path);
if (!isWithinContentDir(canonicalPath, contentDir)) continue;
- const stat = statSync(canonicalPath);
+ const stat = statSync(canonicalPath, { bigint: true });
if (stat.isDirectory()) folderStat = stat;
} catch (err) {
const code = errnoCode(err);
@@ -2447,9 +2446,9 @@ function scanForUntrackedSubfolders(
if (!relPath) continue;
if (contentFilter?.isDirExcluded(relPath)) continue;
- let stat: ReturnType<typeof lstatSync>;
+ let stat: BigIntStats;
try {
- stat = lstatSync(fullPath);
+ stat = lstatSync(fullPath, { bigint: true });
} catch (err) {
const code = errnoCode(err);
if (code !== 'ENOENT') {
@@ -2849,16 +2848,16 @@ async function handleRawEventsInternal(
continue;
}
- let st: ReturnType<typeof lstatSync>;
+ let st: BigIntStats;
let leafSymlink = false;
let canonicalPath: string;
let canonicalContentDir: string;
try {
canonicalContentDir = resolveNativePath(contentDir);
- st = lstatSync(raw.path);
+ st = lstatSync(raw.path, { bigint: true });
leafSymlink = st.isSymbolicLink();
canonicalPath = resolveNativePath(raw.path);
- if (leafSymlink) st = statSync(canonicalPath);
+ if (leafSymlink) st = statSync(canonicalPath, { bigint: true });
} catch (e) {
const code = errnoCode(e);
recordWatcherDecision('drop-stat-failed', raw.type, raw.path);
@@ -2889,17 +2888,17 @@ async function handleRawEventsInternal(
kind: 'file-create',
path: raw.path,
relativePath,
- size: st.size,
- modifiedTs: st.mtime.getTime(),
- inode: Number(st.ino),
+ size: Number(st.size),
+ modifiedTs: statMtime(st).getTime(),
+ inode: filesystemIdentity(st),
}
: {
kind: 'file-update',
path: raw.path,
relativePath,
- size: st.size,
- modifiedTs: st.mtime.getTime(),
- inode: Number(st.ino),
+ size: Number(st.size),
+ modifiedTs: statMtime(st).getTime(),
+ inode: filesystemIdentity(st),
};
if (admitEvent && !admitEvent(event)) continue;
let formerOwner: [string, FileIndexEntry] | undefined;
@@ -2952,7 +2951,7 @@ async function handleRawEventsInternal(
canonicalName,
relativePath,
leafSymlink,
- { device: st.dev, inode: st.ino },
+ filesystemIdentity(st),
{ size: event.size, modified: new Date(event.modifiedTs).toISOString() },
contentFilter,
confirmedAbsentFormerPath,
@@ -3320,10 +3319,10 @@ async function recoverDiskChanges(
folderAliasIndex.delete(relativePath);
continue;
}
- let folderStat: ReturnType<typeof statSync>;
+ let folderStat: BigIntStats;
let canonicalPath = path;
try {
- const lst = lstatSync(path);
+ const lst = lstatSync(path, { bigint: true });
if (lst.isSymbolicLink()) {
canonicalPath = resolveNativePath(path);
if (!isWithinContentDir(canonicalPath, contentDir)) {
@@ -3331,7 +3330,7 @@ async function recoverDiskChanges(
folderAliasIndex.delete(relativePath);
continue;
}
- folderStat = statSync(canonicalPath);
+ folderStat = statSync(canonicalPath, { bigint: true });
} else {
folderStat = lst;
}
@@ -3420,7 +3419,7 @@ async function recoverDiskChanges(
relativePath: candidate.targetPath,
size: target.metadata.size,
modifiedTs: Date.parse(target.metadata.modified),
- inode: target.identity.inode,
+ inode: target.identity,
};
}
recordWatcherDecision('dispatched', event.kind, event.path);
@@ -3996,20 +3995,22 @@ export async function startWatcher(
} else {
try {
const lexicalPath = join(contentDir, event.relativePath);
- const lexicalStat = lstatSync(lexicalPath);
+ const lexicalStat = lstatSync(lexicalPath, { bigint: true });
const leafSymlink = lexicalStat.isSymbolicLink();
const canonicalPath = resolveNativePath(lexicalPath);
if (!isWithinContentDir(canonicalPath, contentDir)) {
throw new Error('General file path resolves outside content directory');
}
- const physicalStat = leafSymlink ? statSync(canonicalPath) : lexicalStat;
+ const physicalStat = leafSymlink
+ ? statSync(canonicalPath, { bigint: true })
+ : lexicalStat;
const registered = registerKnownGeneralPath(
generalFileIndex,
contentDir,
toPosix(relative(contentDir, canonicalPath)),
event.relativePath,
leafSymlink,
- { device: physicalStat.dev, inode: physicalStat.ino },
+ filesystemIdentity(physicalStat),
{ size: event.size, modified: new Date(event.modifiedTs).toISOString() },
contentFilter,
);
diff --git a/packages/server/src/filesystem-identity.test.ts b/packages/server/src/filesystem-identity.test.ts
new file mode 100644
index 000000000..b661a49c0
--- /dev/null
+++ b/packages/server/src/filesystem-identity.test.ts
@@ -0,0 +1,177 @@
+import {
+ mkdirSync,
+ mkdtempSync,
+ rmSync,
+ statSync,
+ symlinkSync,
+ utimesSync,
+ writeFileSync,
+} from 'node:fs';
+import { join, resolve } from 'node:path';
+import { createBasenameIndex } from '@inkeep/open-knowledge-core';
+import { expect, onTestFinished, test, vi } from 'vitest';
+import { seedBasenameIndex } from './asset-walk.ts';
+import { bootCompositionRig } from './composition-rig.test-helper.ts';
+
+const identities = vi.hoisted(() => ({
+ values: new Map<string, { dev: bigint; ino: bigint; numericDev: number; numericIno: number }>(),
+ observed: new Set<string>(),
+}));
+
+vi.mock('node:fs/promises', async (importOriginal) => {
+ const original = await importOriginal<typeof import('node:fs/promises')>();
+ const substitute = <T extends { dev: number | bigint; ino: number | bigint }>(
+ path: string,
+ result: T,
+ ): T => {
+ const identity = identities.values.get(path);
+ if (identity) {
+ identities.observed.add(path);
+ Object.defineProperties(result, {
+ dev: { value: typeof result.dev === 'bigint' ? identity.dev : identity.numericDev },
+ ino: { value: typeof result.ino === 'bigint' ? identity.ino : identity.numericIno },
+ });
+ }
+ return result;
+ };
+ return {
+ ...original,
+ lstat: async (...args: Parameters<typeof original.lstat>) =>
+ substitute(String(args[0]), await original.lstat(...args)),
+ stat: async (...args: Parameters<typeof original.stat>) =>
+ substitute(String(args[0]), await original.stat(...args)),
+ };
+});
+
+const cases = [
+ {
+ name: 'distinct numeric inodes',
+ first: { dev: 7n, ino: 11n, numericDev: 7, numericIno: 11 },
+ second: { dev: 7n, ino: 13n, numericDev: 7, numericIno: 13 },
+ },
+ {
+ name: 'rounded numeric inodes',
+ first: { dev: 7n, ino: 9851624185071827n, numericDev: 7, numericIno: 9851624185071828 },
+ second: { dev: 7n, ino: 9851624185071829n, numericDev: 7, numericIno: 9851624185071828 },
+ },
+ {
+ name: 'equal inodes on different devices',
+ first: { dev: 7n, ino: 11n, numericDev: 7, numericIno: 11 },
+ second: { dev: 9n, ino: 11n, numericDev: 9, numericIno: 11 },
+ },
+];
+
+const scratch = resolve(import.meta.dirname, '../tmp');
+mkdirSync(scratch, { recursive: true });
+
+function boot(root: string): ReturnType<typeof bootCompositionRig> {
+ const server = bootCompositionRig(root);
+ onTestFinished(async () => {
+ const [result] = await Promise.allSettled([server]);
+ if (result.status === 'fulfilled') await result.value.destroy();
+ });
+ return server;
+}
+
+function fixture(): string {
+ onTestFinished(() => {
+ identities.values.clear();
+ identities.observed.clear();
+ });
+ const root = mkdtempSync(join(scratch, 'filesystem-identity-'));
+ onTestFinished(() => rmSync(root, { recursive: true, force: true }));
+ return root;
+}
+
+test.for(cases)('lists linked-folder documents with $name', async ({ first, second }) => {
+ const root = fixture();
+ const content = join(root, 'content');
+ mkdirSync(join(content, 'real', 'sub'), { recursive: true });
+ writeFileSync(join(content, 'real', 'note.md'), '# Note\n');
+ utimesSync(join(content, 'real', 'note.md'), 1_700_000_000.12375, 1_700_000_000.12375);
+ const expectedMetadata = statSync(join(content, 'real', 'note.md'));
+ symlinkSync(join(content, 'real'), join(content, 'alias'), 'junction');
+ symlinkSync(content, join(root, 'content-link'), 'junction');
+ identities.values.set(join(content, 'real'), first);
+ identities.values.set(join(content, 'real', 'note.md'), second);
+ const server = await boot(join(root, 'content-link'));
+ await server.ready;
+ const response = await fetch(`http://127.0.0.1:${server.port}/api/documents`);
+ const body = await response.json();
+ expect(identities.observed).toEqual(new Set(identities.values.keys()));
+ expect(response.status).toBe(200);
+ expect(body.documents).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ kind: 'document',
+ docName: 'real/note',
+ size: expectedMetadata.size,
+ modified: expectedMetadata.mtime.toISOString(),
+ }),
+ expect.objectContaining({
+ kind: 'document',
+ docName: 'alias/note',
+ targetPath: 'real/note.md',
+ }),
+ ]),
+ );
+});
+
+test.for(cases)('lists general files and their links with $name', async ({ first, second }) => {
+ const content = fixture();
+ mkdirSync(join(content, 'real', 'sub'), { recursive: true });
+ writeFileSync(join(content, 'real', 'note.txt'), 'Note\n');
+ symlinkSync(join(content, 'real'), join(content, 'alias'), 'junction');
+ symlinkSync(join(content, 'real', 'note.txt'), join(content, 'linked.txt'), 'file');
+ identities.values.set(join(content, 'real'), first);
+ identities.values.set(join(content, 'real', 'note.txt'), second);
+ const server = await boot(content);
+ await server.ready;
+ const response = await fetch(`http://127.0.0.1:${server.port}/api/documents`);
+ const body = await response.json();
+ expect(identities.observed).toEqual(new Set(identities.values.keys()));
+ expect(response.status).toBe(200);
+ expect(body.documents).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ kind: 'file', path: 'real/note.txt' }),
+ expect.objectContaining({ kind: 'file', path: 'linked.txt', targetPath: 'real/note.txt' }),
+ ]),
+ );
+});
+
+test.for(cases)('seeds assets from distinct directories with $name', async ({ first, second }) => {
+ const content = fixture();
+ mkdirSync(join(content, 'a'));
+ mkdirSync(join(content, 'b'));
+ writeFileSync(join(content, 'a', 'first.png'), 'first');
+ writeFileSync(join(content, 'b', 'second.png'), 'second');
+ symlinkSync(join(content, 'a'), join(content, 'alias-a'), 'junction');
+ symlinkSync(join(content, 'b'), join(content, 'alias-b'), 'junction');
+ identities.values.set(join(content, 'a'), first);
+ identities.values.set(join(content, 'b'), second);
+ const index = createBasenameIndex();
+ await seedBasenameIndex({ contentDir: content, basenameIndex: index });
+ expect(identities.observed).toEqual(new Set(identities.values.keys()));
+ expect(index.resolveEmbed('first.png', 'note.md')).toBe('a/first.png');
+ expect(index.resolveEmbed('second.png', 'note.md')).toBe('b/second.png');
+});
+
+test.for(cases)('keeps distinct general-file sizes with $name', async ({ first, second }) => {
+ const content = fixture();
+ writeFileSync(join(content, 'first.txt'), 'a');
+ writeFileSync(join(content, 'second.txt'), 'bb');
+ identities.values.set(join(content, 'first.txt'), first);
+ identities.values.set(join(content, 'second.txt'), second);
+ const server = await boot(content);
+ await server.ready;
+ const response = await fetch(`http://127.0.0.1:${server.port}/api/documents`);
+ const body = await response.json();
+ expect(identities.observed).toEqual(new Set(identities.values.keys()));
+ expect(response.status).toBe(200);
+ expect(body.documents).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ kind: 'file', path: 'first.txt', size: 1 }),
+ expect.objectContaining({ kind: 'file', path: 'second.txt', size: 2 }),
+ ]),
+ );
+});
diff --git a/packages/server/src/http/document-routes.test.ts b/packages/server/src/http/document-routes.test.ts
index dc387244d..56b4367d1 100644
--- a/packages/server/src/http/document-routes.test.ts
+++ b/packages/server/src/http/document-routes.test.ts
@@ -16,6 +16,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Hocuspocus } from '@hocuspocus/server';
import { LOCAL_DIR } from '@inkeep/open-knowledge-core';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { streamShowAllEntries, walkContentDirForShowAll } from '../api-extension.ts';
import { makeCaptureRes, makeSyntheticReq } from '../composition-rig.test-helper.ts';
@@ -280,7 +281,7 @@ test.each(['repeatable', 'single-pass'] as const)(
const rich = {
kind: 'file' as const,
canonicalPath: join(projectDir, 'outside/atlas.csv'),
- inode: 41,
+ inode: '0:41',
size: 5,
modified: '2026-01-01T00:00:00.000Z',
aliases: ['linked-atlas.csv', 'linked-beacon.csv'],
@@ -305,7 +306,7 @@ test.each(['repeatable', 'single-pass'] as const)(
size: 0,
modified: '2026-01-01T00:00:00.000Z',
canonicalPath: join(projectDir, 'canonical'),
- inode: 42,
+ inode: '0:42',
},
],
]);
@@ -385,11 +386,13 @@ test('a folder alias lists a contained legacy file alias', async () => {
const mirror = join(projectDir, 'mirror');
symlinkSync(canonicalDir, mirror);
const targetStat = statSync(target);
+ const targetStatIdentity = filesystemIdentity(statSync(target, { bigint: true }));
const folderStat = statSync(canonicalDir);
+ const folderStatIdentity = filesystemIdentity(statSync(canonicalDir, { bigint: true }));
const entry: FileIndexEntry = {
kind: 'file',
canonicalPath: target,
- inode: targetStat.ino,
+ inode: targetStatIdentity,
size: targetStat.size,
modified: targetStat.mtime.toISOString(),
aliases: ['canonical/y.csv'],
@@ -402,7 +405,7 @@ test('a folder alias lists a contained legacy file alias', async () => {
size: 0,
modified: folderStat.mtime.toISOString(),
canonicalPath: canonicalDir,
- inode: folderStat.ino,
+ inode: folderStatIdentity,
},
],
]);
@@ -501,13 +504,14 @@ test('a public recovery creation preserves its literal folder alias and one list
symlinkSync(real, linked);
writeFileSync(join(linked, 'created.csv'), 'public,bytes\n');
const stat = statSync(created);
+ const statIdentity = filesystemIdentity(statSync(created, { bigint: true }));
watcher.mutateFileIndex({
kind: 'file-create',
path: created,
relativePath: 'linked.mdx/created.csv',
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
creations++;
expect(
diff --git a/packages/server/src/http/shell-csp.ts b/packages/server/src/http/shell-csp.ts
index 1a5870a9e..d38ba0de9 100644
--- a/packages/server/src/http/shell-csp.ts
+++ b/packages/server/src/http/shell-csp.ts
@@ -7,8 +7,8 @@ export function createShellHeaderSetter(distDir: string) {
const policyFor = createAppShellPolicies(distDir, ({ err, root, file }, message) =>
getLogger('shell-csp').warn({ err, root, file }, message),
);
- return (res: ServerResponse, _pathname: string, stats: Stats): void => {
- const policy = policyFor(stats);
+ return (res: ServerResponse, _pathname: string, stats: Stats, file: string): void => {
+ const policy = policyFor({ file, stats });
if (policy !== undefined) res.setHeader('Content-Security-Policy', policy);
};
}
diff --git a/packages/server/src/local-target-inventory.test.ts b/packages/server/src/local-target-inventory.test.ts
index 8462f6421..d4c833ef8 100644
--- a/packages/server/src/local-target-inventory.test.ts
+++ b/packages/server/src/local-target-inventory.test.ts
@@ -19,7 +19,7 @@ function entry(
kind,
canonicalPath,
aliases,
- inode: 1,
+ inode: '0:1',
modified: '2026-01-01T00:00:00.000Z',
size: 1,
};
@@ -48,7 +48,7 @@ describe('localTargetInventoryFromIndexes', () => {
size: 0,
modified: '2026-01-01T00:00:00.000Z',
canonicalPath: join(contentDir, 'canonical'),
- inode: 1,
+ inode: '0:1',
},
],
]);
diff --git a/packages/server/src/parcel-drop-notice.test.ts b/packages/server/src/parcel-drop-notice.test.ts
index fd63039bb..e957de48e 100644
--- a/packages/server/src/parcel-drop-notice.test.ts
+++ b/packages/server/src/parcel-drop-notice.test.ts
@@ -18,6 +18,7 @@ import {
} from 'node:fs';
import { tmpdir } from 'node:os';
import { basename, dirname, join } from 'node:path';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import type { Event } from '@parcel/watcher';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { configureTestGitRepository } from '../../../test-support/configure-git-fixture.test-helper.ts';
@@ -221,8 +222,8 @@ vi.mock('node:fs', async (importOriginal) => {
const deniedDirectoryRead = vi.hoisted(() => ({ path: null as string | null }));
const deviceContrast = vi.hoisted(() => ({
path: null as string | null,
- device: null as number | null,
- inode: null as number | null,
+ device: null as bigint | null,
+ inode: null as bigint | null,
observed: false,
}));
const memberStatSchedule = vi.hoisted(() => ({
@@ -2073,6 +2074,7 @@ test('notice recovery tracks general-file changes and asset notifications outsid
writeFileSync(asset, 'fake-png-bytes');
writeFileSync(changed, 'new,more,bytes\n');
const changedStat = statSync(changed);
+ const changedStatIdentity = filesystemIdentity(statSync(changed, { bigint: true }));
unlinkSync(removed);
unlinkSync(removedAsset);
@@ -2090,7 +2092,7 @@ test('notice recovery tracks general-file changes and asset notifications outsid
relativePath: 'changed.csv',
size: Buffer.byteLength('new,more,bytes\n'),
modifiedTs: changedStat.mtime.getTime(),
- inode: Number(changedStat.ino),
+ inode: changedStatIdentity,
}),
);
expect
@@ -2316,6 +2318,7 @@ test.each(['markdown', 'general'] as const)(
const path = writeDoc(name, '# Fresh\n');
const key = kind === 'markdown' ? 'fresh' : name;
const stat = statSync(path);
+ const statIdentity = filesystemIdentity(statSync(path, { bigint: true }));
watcher.mutateFileIndex(
kind === 'markdown'
? { kind: 'create', path, docName: key, content: '# Fresh\n' }
@@ -2325,7 +2328,7 @@ test.each(['markdown', 'general'] as const)(
relativePath: key,
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: stat.ino,
+ inode: statIdentity,
},
);
expect(Boolean(indexedRow(watcher, key, kind === 'markdown' ? 'markdown' : 'file'))).toBe(
@@ -2460,6 +2463,7 @@ test.each(['markdown', 'general'] as const)(
if (event.kind !== 'delete' || event.docName !== 'first') return;
writeFileSync(later, '# Recreated\n');
const stat = statSync(later);
+ const statIdentity = filesystemIdentity(statSync(later, { bigint: true }));
watched.watcher.mutateFileIndex(
kind === 'markdown'
? { kind: 'create', path: later, docName: key, content: '# Recreated\n' }
@@ -2469,7 +2473,7 @@ test.each(['markdown', 'general'] as const)(
relativePath: key,
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: stat.ino,
+ inode: statIdentity,
},
);
});
@@ -2633,7 +2637,7 @@ test('an ordinary canonical file update retains its aliases beside a same-stem d
name: 'real.csv',
aliases: ['alias.csv', 'sibling.csv'],
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
modified: statSync(real).mtime.toISOString(),
},
);
@@ -2682,7 +2686,7 @@ test('an ordinary alias retarget retains aliases already owned by its new target
{
aliases: ['old-sibling.csv'],
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
},
);
expect(
@@ -2691,7 +2695,7 @@ test('an ordinary alias retarget retains aliases already owned by its new target
name: 'other.csv',
aliases: expect.arrayContaining(['alias.csv', 'new-sibling.csv']),
size: statSync(other).size,
- inode: Number(statSync(other).ino),
+ inode: filesystemIdentity(statSync(other, { bigint: true })),
});
expect(
rows.find((entry) => entry.kind === 'file' && entry.canonicalPath === other)?.aliases,
@@ -3905,6 +3909,7 @@ test.each(['realpath', 'stat'] as const)(
scanSchedule.orderedPath = contentDir;
scanSchedule.firstEntry = 'alias.csv';
const priorStat = statSync(prior);
+ const priorStatIdentity = filesystemIdentity(statSync(prior, { bigint: true }));
const events: DiskEvent[] = [];
const completed: string[] = [];
const logger = getLogger('file-watcher');
@@ -3953,7 +3958,7 @@ test.each(['realpath', 'stat'] as const)(
expect(priorEntries[0]).toMatchObject({
size: priorStat.size,
modified: priorStat.mtime.toISOString(),
- inode: Number(priorStat.ino),
+ inode: priorStatIdentity,
});
const diagnostic = JSON.stringify([...warnings.mock.calls, ...errors.mock.calls]);
expect(diagnostic).toContain('EACCES');
@@ -5040,7 +5045,7 @@ test.each([
.toMatchObject({
name: 'real.csv',
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
aliases: withAlias ? expect.arrayContaining(['alias.csv', 'sibling.csv']) : [],
});
if (withAlias) {
@@ -5073,7 +5078,7 @@ test.each([
name: 'real.csv',
aliases: ['sibling.csv'],
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
});
expect
.soft(afterRetarget.find((entry) => entry.kind === 'file' && entry.canonicalPath === other))
@@ -5081,7 +5086,7 @@ test.each([
name: 'other.csv',
aliases: ['alias.csv'],
size: statSync(other).size,
- inode: Number(statSync(other).ino),
+ inode: filesystemIdentity(statSync(other, { bigint: true })),
});
events.length = 0;
}
@@ -5393,13 +5398,14 @@ test.each(['file', 'markdown'] as const)(
if (publicDomain === 'file') {
writeFileSync(real, 'public,file,updated\n');
const stat = statSync(real);
+ const statIdentity = filesystemIdentity(statSync(real, { bigint: true }));
watcher?.mutateFileIndex({
kind: 'file-update',
path: real,
relativePath: 'real.csv',
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
} else {
writeFileSync(document, '# Public document update\n');
@@ -5611,7 +5617,7 @@ test.each(['add', 'remove', 'retarget'] as const)(
name: 'real.csv',
aliases: action === 'add' ? ['alias.csv'] : [],
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
});
expect
.soft(
@@ -5706,7 +5712,7 @@ test('ordinary same-stem alias removal drops only that alias before notice recov
(entry) => entry.kind === 'file' && entry.canonicalPath === real,
);
expect(originalFile?.size).toBe(statSync(real).size);
- expect(originalFile?.inode).toBe(Number(statSync(real).ino));
+ expect(originalFile?.inode).toBe(filesystemIdentity(statSync(real, { bigint: true })));
unlinkSync(alias);
await deliver(contentDir, [{ type: 'delete', path: alias }]);
@@ -5726,7 +5732,7 @@ test('ordinary same-stem alias removal drops only that alias before notice recov
.soft(immediate.find((entry) => entry.kind === 'file' && entry.canonicalPath === real))
.toMatchObject({
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
});
expect
.soft(
@@ -5773,13 +5779,14 @@ test('a public general-file recreation during recovery retains its restored pare
mkdirSync(folder);
writeFileSync(later, 'recreated,general,bytes\n');
const stat = statSync(later);
+ const statIdentity = filesystemIdentity(statSync(later, { bigint: true }));
watched.watcher.mutateFileIndex({
kind: 'file-create',
path: later,
relativePath: 'nested/later.csv',
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
recreations += 1;
});
@@ -5808,7 +5815,7 @@ test('a public general-file recreation during recovery retains its restored pare
expect.objectContaining({
name: 'nested/later.csv',
size: statSync(later).size,
- inode: Number(statSync(later).ino),
+ inode: filesystemIdentity(statSync(later, { bigint: true })),
}),
]);
expect
@@ -5913,7 +5920,7 @@ test.each(['general', 'markdown'] as const)(
.soft(restored.find((entry) => entry.kind === 'file' && entry.canonicalPath === real))
.toMatchObject({
size: statSync(real).size,
- inode: Number(statSync(real).ino),
+ inode: filesystemIdentity(statSync(real, { bigint: true })),
aliases: ['alias.csv'],
});
expect(watcher.getFileIndex().get('real.csv')?.canonicalPath).toBe(document);
@@ -5969,13 +5976,14 @@ test.each(['file', 'markdown'] as const)(
if (publicDomain === 'file') {
writeFileSync(real, 'public,file,updated\n');
const stat = statSync(real);
+ const statIdentity = filesystemIdentity(statSync(real, { bigint: true }));
watcher.mutateFileIndex({
kind: 'file-update',
path: real,
relativePath: 'real.csv',
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
} else {
writeFileSync(document, '# Public document update\n');
@@ -6044,7 +6052,7 @@ test.each(['a.csv', 'b.csv'] as const)(
expect(rows[0]).toMatchObject({
name: 'a.csv',
canonicalPath: first,
- inode: Number(statSync(first).ino),
+ inode: filesystemIdentity(statSync(first, { bigint: true })),
});
expect(readFileSync(alias, 'utf8')).toBe('same,inode,content\n');
expect.soft(rows[0]?.aliases).toContain('z-alias.csv');
@@ -6084,21 +6092,21 @@ test.each([
scanSchedule.orderedEntries = [first, second, 'z-alias.csv'];
const disk = () => ({
a: {
- inode: Number(statSync(a).ino),
+ inode: filesystemIdentity(statSync(a, { bigint: true })),
links: statSync(a).nlink,
regular: lstatSync(a).isFile(),
canonicalPath: realpathSync(a),
bytes: readFileSync(a, 'utf8'),
},
b: {
- inode: Number(statSync(b).ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
links: statSync(b).nlink,
regular: lstatSync(b).isFile(),
canonicalPath: realpathSync(b),
bytes: readFileSync(b, 'utf8'),
},
alias: {
- inode: Number(statSync(alias).ino),
+ inode: filesystemIdentity(statSync(alias, { bigint: true })),
symlink: lstatSync(alias).isSymbolicLink(),
canonicalPath: realpathSync(alias),
bytes: readFileSync(alias, 'utf8'),
@@ -6146,6 +6154,7 @@ test.each([
} else if (action === 'public') {
const path = join(contentDir, target);
const stat = statSync(path);
+ const statIdentity = filesystemIdentity(statSync(path, { bigint: true }));
const generation = active.getFileIndexGeneration();
active.mutateFileIndex({
kind: 'file-update',
@@ -6153,7 +6162,7 @@ test.each([
relativePath: target,
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
expect(active.getFileIndexGeneration()).toBeGreaterThan(generation);
} else {
@@ -6228,14 +6237,14 @@ test.each(['direct b update', 'order a to b'] as const)(
scanSchedule.orderedEntries = ['a.csv', 'b.csv', 'y-alias.csv', 'z-alias.csv', 'source.md'];
const physical = () => ({
a: {
- inode: Number(statSync(a).ino),
+ inode: filesystemIdentity(statSync(a, { bigint: true })),
links: statSync(a).nlink,
regular: lstatSync(a).isFile(),
canonicalPath: realpathSync(a),
bytes: readFileSync(a, 'utf8'),
},
b: {
- inode: Number(statSync(b).ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
links: statSync(b).nlink,
regular: lstatSync(b).isFile(),
canonicalPath: realpathSync(b),
@@ -6447,6 +6456,7 @@ test.each(['rescued-folder-create', 'public-file-create', 'alias-before-member']
} else if (input === 'public-file-create') {
expect(bName.localeCompare('a.csv')).toBeLessThan(0);
const st = statSync(b);
+ const stIdentity = filesystemIdentity(statSync(b, { bigint: true }));
const generation = watcher.getFileIndexGeneration();
watcher.mutateFileIndex({
kind: 'file-create',
@@ -6454,7 +6464,7 @@ test.each(['rescued-folder-create', 'public-file-create', 'alias-before-member']
relativePath: bName,
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
});
expect(watcher.getFileIndexGeneration()).toBeGreaterThan(generation);
expect.soft(indexedGeneralRows(watcher)).toHaveLength(1);
@@ -6555,6 +6565,7 @@ test('a public canonical path with a lexical symlink name joins the existing har
expect(realpathSync(z)).toBe(b);
expect([statSync(a).dev, statSync(a).ino]).toEqual([statSync(b).dev, statSync(b).ino]);
const st = statSync(b);
+ const stIdentity = filesystemIdentity(statSync(b, { bigint: true }));
const generation = watcher.getFileIndexGeneration();
watcher.mutateFileIndex({
kind: 'file-update',
@@ -6562,7 +6573,7 @@ test('a public canonical path with a lexical symlink name joins the existing har
relativePath: 'z-alias.csv',
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
});
expect(watcher.getFileIndexGeneration()).toBeGreaterThan(generation);
expect.soft(indexedGeneralRows(watcher)).toHaveLength(1);
@@ -6578,12 +6589,12 @@ test('a public canonical path with a lexical symlink name joins the existing har
test('different devices do not merge equal reported inode numbers during external observation', async () => {
const a = writeDoc('atlas.csv', 'atlas\n');
const b = writeDoc('beacon.csv', 'beacon\n');
- const aPhysical = statSync(a);
- const bPhysical = statSync(b);
+ const aPhysical = statSync(a, { bigint: true });
+ const bPhysical = statSync(b, { bigint: true });
expect(aPhysical.ino).not.toBe(bPhysical.ino);
expect(aPhysical.dev).toBe(bPhysical.dev);
deviceContrast.path = b;
- deviceContrast.device = aPhysical.dev + 1;
+ deviceContrast.device = aPhysical.dev + 1n;
deviceContrast.inode = aPhysical.ino;
const { watcher } = await watchContent();
expect(deviceContrast.observed).toBe(true);
@@ -7252,7 +7263,7 @@ test('a denied actual former target preserves its hardlink group and later recor
canonicalPath: a,
size: statSync(a).size,
modified: statSync(a).mtime.toISOString(),
- inode: Number(statSync(a).ino),
+ inode: filesystemIdentity(statSync(a, { bigint: true })),
aliases: expect.arrayContaining(['y-alias.csv', 'z-alias.csv']),
});
@@ -7308,7 +7319,9 @@ test('filter rebuild prunes individual hardlink paths and aliases before positiv
scanSchedule.orderedEntries = ['a.csv', 'b.csv', 'y-alias.csv', 'z-alias.csv', 'a.csv.md'];
const { watcher, events } = await watchContent(filter);
const memberRows = () =>
- indexedGeneralRows(watcher).filter((row) => row.inode === statSync(a).ino);
+ indexedGeneralRows(watcher).filter(
+ (row) => row.inode === filesystemIdentity(statSync(a, { bigint: true })),
+ );
const memberTargets = () => indexedFileTargets(watcher).filter((name) => name !== '.okignore');
expect(filter.isPathIgnored('a.csv')).toBe(true);
expect(filter.isPathIgnored('b.csv')).toBe(false);
@@ -7479,7 +7492,7 @@ test('a public update to one member before synthesis does not suppress another m
relativePath: 'b.csv',
size: publicStat.size,
modifiedTs: publicStat.mtimeMs,
- inode: Number(publicStat.ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
});
} finally {
resume.release();
@@ -7519,13 +7532,14 @@ test('a public symlink transfer during recovery keeps its dependent without prot
unlinkSync(b);
symlinkSync(c, b);
const st = statSync(b);
+ const stIdentity = filesystemIdentity(statSync(b, { bigint: true }));
watcher?.mutateFileIndex({
kind: 'file-update',
path: b,
relativePath: 'b.csv',
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
});
expect(realpathSync(y)).toBe(c);
const active = watcher;
@@ -7586,6 +7600,7 @@ test('a public update after synthesis retains an unrelated member replacement',
callbackCount++;
writeFileSync(b, 'newer,old,group\n');
const st = statSync(b);
+ const stIdentity = filesystemIdentity(statSync(b, { bigint: true }));
publicBModifiedTs = st.mtimeMs;
watcher?.mutateFileIndex({
kind: 'file-update',
@@ -7593,7 +7608,7 @@ test('a public update after synthesis retains an unrelated member replacement',
relativePath: 'b.csv',
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
});
},
undefined,
@@ -7623,13 +7638,13 @@ test('a public update after synthesis retains an unrelated member replacement',
expect.soft(indexedFileTargets(watcher)).toEqual(['a.csv', 'b.csv', 'y-alias.csv']);
expect.soft(indexedGeneralRows(watcher).find((row) => row.name === 'a.csv')).toMatchObject({
canonicalPath: a,
- inode: Number(statSync(a).ino),
+ inode: filesystemIdentity(statSync(a, { bigint: true })),
size: statSync(a).size,
modified: statSync(a).mtime.toISOString(),
});
expect.soft(indexedGeneralRows(watcher).find((row) => row.name === 'b.csv')).toMatchObject({
canonicalPath: b,
- inode: Number(statSync(b).ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
size: statSync(b).size,
modified: new Date(publicBModifiedTs).toISOString(),
});
@@ -7658,6 +7673,7 @@ test('the latest public member tombstone survives an older scan without rolling
callbackCount++;
writeFileSync(b, 'newer,shared,content\n');
const sharedStat = statSync(b);
+ const sharedStatIdentity = filesystemIdentity(statSync(b, { bigint: true }));
sharedPublicModifiedTs = sharedStat.mtimeMs;
watcher?.mutateFileIndex({
kind: 'file-update',
@@ -7665,18 +7681,19 @@ test('the latest public member tombstone survives an older scan without rolling
relativePath: 'b.csv',
size: sharedStat.size,
modifiedTs: sharedStat.mtimeMs,
- inode: Number(sharedStat.ino),
+ inode: sharedStatIdentity,
});
const temporary = writeDoc('replacement-temporary.csv', 'replacement\n');
renameSync(temporary, b);
const replacementStat = statSync(b);
+ const replacementStatIdentity = filesystemIdentity(statSync(b, { bigint: true }));
watcher?.mutateFileIndex({
kind: 'file-update',
path: b,
relativePath: 'b.csv',
size: replacementStat.size,
modifiedTs: replacementStat.mtimeMs,
- inode: Number(replacementStat.ino),
+ inode: replacementStatIdentity,
});
unlinkSync(b);
watcher?.mutateFileIndex({ kind: 'file-delete', path: b, relativePath: 'b.csv' });
@@ -7731,7 +7748,7 @@ test('index-only reseed merges an observed sibling with a newer public hardlink
relativePath: 'b.csv',
size: publicStat.size,
modifiedTs: publicStat.mtimeMs,
- inode: Number(publicStat.ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
});
} finally {
resume.release();
@@ -7773,6 +7790,7 @@ test.each(['known-member', 'new-member'] as const)(
writeFileSync(b, expectedBytes);
}
const st = statSync(b);
+ const stIdentity = filesystemIdentity(statSync(b, { bigint: true }));
if (input === 'known-member') expect(st.size).not.toBe(priorIndexedSize);
denyAdmission('lstat', b);
expect(() =>
@@ -7782,7 +7800,7 @@ test.each(['known-member', 'new-member'] as const)(
relativePath: 'b.csv',
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
}),
).not.toThrow();
expect.soft(admissionFault.observed).toBe(true);
@@ -7810,6 +7828,7 @@ test('a stale public file-create still registers without a new read failure and
const { watcher } = await watchContent();
const stale = writeDoc('stale.csv', 'gone\n');
const st = statSync(stale);
+ const stIdentity = filesystemIdentity(statSync(stale, { bigint: true }));
unlinkSync(stale);
expect(existsSync(stale)).toBe(false);
expect(() =>
@@ -7819,7 +7838,7 @@ test('a stale public file-create still registers without a new read failure and
relativePath: 'stale.csv',
size: st.size,
modifiedTs: st.mtimeMs,
- inode: Number(st.ino),
+ inode: stIdentity,
}),
).not.toThrow();
expect.soft(indexedFileTargets(watcher)).toContain('stale.csv');
@@ -7867,11 +7886,11 @@ test('legacy mixed-Map helpers expose general mutation before their public callb
relativePath: 'legacy.csv',
size: 3,
modifiedTs: 0,
- inode: 7,
+ inode: '0:7',
},
index,
);
- expect(index.get('legacy.csv')).toMatchObject({ kind: 'file', size: 3, inode: 7 });
+ expect(index.get('legacy.csv')).toMatchObject({ kind: 'file', size: 3, inode: '0:7' });
updateFileIndex({ kind: 'file-delete', path: file, relativePath: 'legacy.csv' }, index);
expect(index.has('legacy.csv')).toBe(false);
});
@@ -7957,13 +7976,14 @@ test('a public recreation during the first alias deletion cancels the pending se
await entered.promise;
symlinkSync(a, y);
const stat = statSync(y);
+ const statIdentity = filesystemIdentity(statSync(y, { bigint: true }));
watcher.mutateFileIndex({
kind: 'file-create',
path: y,
relativePath: 'y.csv',
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
expect(lstatSync(y).isSymbolicLink()).toBe(true);
expect(realpathSync(y)).toBe(a);
@@ -8798,17 +8818,18 @@ test.each(['notice recovery', 'index-only rescan'] as const)(
relativePath: 'b.csv',
size: publicStat.size,
modifiedTs: publicStat.mtimeMs,
- inode: Number(publicStat.ino),
+ inode: filesystemIdentity(statSync(b, { bigint: true })),
});
writeFileSync(c, 'c,unrelated,public\n');
const cStat = statSync(c);
+ const cStatIdentity = filesystemIdentity(statSync(c, { bigint: true }));
watcher.mutateFileIndex({
kind: 'file-create',
path: c,
relativePath: 'c.csv',
size: cStat.size,
modifiedTs: cStat.mtimeMs,
- inode: Number(cStat.ino),
+ inode: cStatIdentity,
});
expect(sortedTargetInventory(watcher).fileTargets).toEqual([
'.okignore',
@@ -9066,6 +9087,7 @@ test('the final symlink target stat supplies shared metadata after earlier hardl
memberStatSchedule.resume = resume.promise;
const started = watchContent();
let lastStat: ReturnType<typeof statSync> | undefined;
+ let lastStatIdentity = '';
try {
const paused = await Promise.race([
entered.promise.then(() => true),
@@ -9074,6 +9096,7 @@ test('the final symlink target stat supplies shared metadata after earlier hardl
expect(paused).toBe(true);
writeFileSync(b, 'new,shared,metadata\n');
lastStat = statSync(b);
+ lastStatIdentity = filesystemIdentity(statSync(b, { bigint: true }));
expect(readFileSync(a, 'utf8')).toBe('new,shared,metadata\n');
expect(readFileSync(z, 'utf8')).toBe('new,shared,metadata\n');
} finally {
@@ -9095,7 +9118,7 @@ test('the final symlink target stat supplies shared metadata after earlier hardl
aliases: ['z.csv'],
size: lastStat?.size,
modified: lastStat?.mtime.toISOString(),
- inode: Number(lastStat?.ino),
+ inode: lastStatIdentity,
}),
]);
});
@@ -9594,6 +9617,7 @@ test('a pending alias update carries newer shared metadata from another accepted
);
writeFileSync(pendingPeer, 'new,longer,shared,metadata\n');
const stat = statSync(pendingPeer);
+ const statIdentity = filesystemIdentity(statSync(pendingPeer, { bigint: true }));
expectedSize = stat.size;
expectedModifiedTs = new Date(stat.mtimeMs).getTime();
watched.watcher.mutateFileIndex({
@@ -9602,7 +9626,7 @@ test('a pending alias update carries newer shared metadata from another accepted
relativePath: basename(pendingPeer),
size: stat.size,
modifiedTs: stat.mtimeMs,
- inode: Number(stat.ino),
+ inode: statIdentity,
});
callbackBytes = readFileSync(pendingTarget, 'utf8');
callbackGroupSize = [...watched.watcher.getAllFilesIndex()].find(
diff --git a/packages/server/src/recovered-file-target.test.ts b/packages/server/src/recovered-file-target.test.ts
index 464bac686..6363c027c 100644
--- a/packages/server/src/recovered-file-target.test.ts
+++ b/packages/server/src/recovered-file-target.test.ts
@@ -1,6 +1,7 @@
import { mkdirSync, mkdtempSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { describe, expect, test, vi } from 'vitest';
import type { DiskEvent, WatcherHandle } from './file-watcher.ts';
import { getLogger } from './logger.ts';
@@ -64,6 +65,7 @@ describe('reconcileRecoveredFileTarget', () => {
writeFileSync(targetPath, 'report');
const canonicalPath = realpathSync(targetPath);
const stats = statSync(canonicalPath);
+ const statsIdentity = filesystemIdentity(statSync(canonicalPath, { bigint: true }));
const mutations: DiskEvent[] = [];
const invalidateReferencedAssetsCache = vi.fn();
@@ -84,7 +86,7 @@ describe('reconcileRecoveredFileTarget', () => {
relativePath,
size: stats.size,
modifiedTs: stats.mtimeMs,
- inode: stats.ino,
+ inode: statsIdentity,
},
]);
expect(invalidateReferencedAssetsCache).toHaveBeenCalledOnce();
diff --git a/packages/server/src/recovered-file-target.ts b/packages/server/src/recovered-file-target.ts
index fc3b10019..9d3847db8 100644
--- a/packages/server/src/recovered-file-target.ts
+++ b/packages/server/src/recovered-file-target.ts
@@ -1,5 +1,6 @@
import { realpathSync, statSync } from 'node:fs';
import { join } from 'node:path';
+import { filesystemIdentity, statMtimeMs } from '@inkeep/open-knowledge-core/server';
import type { WatcherHandle } from './file-watcher.ts';
import { getLogger } from './logger.ts';
@@ -29,14 +30,14 @@ export function reconcileRecoveredFileTarget({
try {
const canonicalPath = realpathSync(absolutePath);
- const stats = statSync(canonicalPath);
+ const stats = statSync(canonicalPath, { bigint: true });
watcher.mutateFileIndex({
kind: 'file-create',
path: canonicalPath,
relativePath,
- size: stats.size,
- modifiedTs: stats.mtimeMs,
- inode: stats.ino,
+ size: Number(stats.size),
+ modifiedTs: statMtimeMs(stats),
+ inode: filesystemIdentity(stats),
});
} catch (error) {
const code =
diff --git a/packages/server/src/rename-identity.test.ts b/packages/server/src/rename-identity.test.ts
new file mode 100644
index 000000000..c99d8a486
--- /dev/null
+++ b/packages/server/src/rename-identity.test.ts
@@ -0,0 +1,98 @@
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { join, resolve } from 'node:path';
+import { aborted } from 'node:util';
+import { expect, type TestContext, test, vi } from 'vitest';
+import { bootCompositionRig } from './composition-rig.test-helper.ts';
+
+const identities = vi.hoisted(() => ({
+ values: new Map<string, { exact: bigint; numeric: number }>(),
+ observed: new Set<string>(),
+}));
+
+vi.mock('node:fs', async (importOriginal) => {
+ const original = await importOriginal<typeof import('node:fs')>();
+ return {
+ ...original,
+ statSync: (...args: Parameters<typeof original.statSync>) => {
+ const result = original.statSync(...args);
+ const identity = identities.values.get(String(args[0]));
+ if (result !== undefined && identity !== undefined) {
+ identities.observed.add(String(args[0]));
+ Object.defineProperty(result, 'ino', {
+ value: typeof result.ino === 'bigint' ? identity.exact : identity.numeric,
+ });
+ }
+ return result;
+ },
+ };
+});
+
+async function renameCaseOnlyWithIdentities(
+ { onTestFinished, signal }: TestContext,
+ sourceIdentity: { exact: bigint; numeric: number },
+ destinationIdentity: { exact: bigint; numeric: number },
+): Promise<number> {
+ onTestFinished(() => {
+ identities.values.clear();
+ identities.observed.clear();
+ });
+ const scratch = resolve(import.meta.dirname, '../tmp');
+ mkdirSync(scratch, { recursive: true });
+ const root = mkdtempSync(join(scratch, 'rename-identity-'));
+ onTestFinished(() => rmSync(root, { recursive: true, force: true }));
+ const source = join(root, 'note.md');
+ const destination = join(root, 'NOTE.md');
+ writeFileSync(source, '# Source\n');
+ writeFileSync(destination, '# Target\n');
+ const starting = bootCompositionRig(root);
+ let pending: Promise<number> | undefined;
+ onTestFinished(async () => {
+ try {
+ await (await starting).destroy();
+ } finally {
+ await Promise.allSettled([pending]);
+ }
+ });
+ pending = (async () => {
+ const server = await starting;
+ signal.throwIfAborted();
+ await Promise.race([server.ready, aborted(signal, server)]);
+ signal.throwIfAborted();
+ identities.values.set(source, sourceIdentity);
+ identities.values.set(destination, destinationIdentity);
+ const response = await fetch(`http://127.0.0.1:${server.port}/api/rename-path`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ kind: 'file', fromPath: 'note', toPath: 'NOTE' }),
+ signal,
+ });
+ await response.text();
+ expect(identities.observed).toEqual(new Set([source, destination]));
+ return response.status;
+ })();
+ return await pending;
+}
+
+test.for([false, true])(
+ 'refuses distinct case-only targets with inode collision=%s',
+ async (collision, context) => {
+ const status = await renameCaseOnlyWithIdentities(
+ context,
+ {
+ exact: collision ? 9851624185071827n : 11n,
+ numeric: collision ? 9851624185071828 : 11,
+ },
+ {
+ exact: collision ? 9851624185071829n : 13n,
+ numeric: collision ? 9851624185071828 : 13,
+ },
+ );
+ expect(status).toBe(409);
+ },
+);
+
+test('admits a case-only rename when both spellings have the same exact identity', async (context) => {
+ const shared = { exact: 9851624185071827n, numeric: 9851624185071828 };
+ const status = await renameCaseOnlyWithIdentities(context, shared, shared);
+ expect(status).toBe(200);
+});
diff --git a/packages/server/src/shell-identity.test.ts b/packages/server/src/shell-identity.test.ts
new file mode 100644
index 000000000..f33841d78
--- /dev/null
+++ b/packages/server/src/shell-identity.test.ts
@@ -0,0 +1,89 @@
+import { createHash } from 'node:crypto';
+import { once } from 'node:events';
+import fs from 'node:fs';
+import { createServer } from 'node:http';
+import { syncBuiltinESMExports } from 'node:module';
+import { join, resolve } from 'node:path';
+import sirv from 'sirv';
+import { expect, test, vi } from 'vitest';
+import { createShellHeaderSetter } from './http/shell-csp.ts';
+import { closeTestHttpServer } from './http-server.test-helper.ts';
+
+test.for([false, true])(
+ 'selects the served HTML policy with inode collision=%s',
+ async (collision, { onTestFinished, signal }) => {
+ const scratch = resolve(import.meta.dirname, '../tmp');
+ fs.mkdirSync(scratch, { recursive: true });
+ const root = fs.mkdtempSync(join(scratch, 'shell-identity-'));
+ onTestFinished(() => fs.rmSync(root, { recursive: true, force: true }));
+ const first = join(root, 'index.html');
+ const second = join(root, 'other.html');
+ const script = 'window.first = 1;';
+ const html = `<script>${script}</script>`;
+ fs.writeFileSync(first, html);
+ fs.writeFileSync(second, '<script>window.other = 2;</script>');
+ const stamp = new Date('2020-01-01T00:00:00Z');
+ fs.utimesSync(first, stamp, stamp);
+ fs.utimesSync(second, stamp, stamp);
+ const original = fs.statSync;
+ const statOverride = vi.spyOn(fs, 'statSync');
+ onTestFinished(() => {
+ statOverride.mockRestore();
+ syncBuiltinESMExports();
+ });
+ const observed = new Set<string>();
+ statOverride.mockImplementation((...args: Parameters<typeof fs.statSync>) => {
+ const result = original(...args);
+ const path = String(args[0]);
+ if (result !== undefined && (path === first || path === second)) {
+ observed.add(path);
+ const exact = collision
+ ? path === first
+ ? 9851624185071827n
+ : 9851624185071829n
+ : path === first
+ ? 11n
+ : 13n;
+ const numeric = collision ? 9851624185071828 : path === first ? 11 : 13;
+ Object.defineProperty(result, 'ino', {
+ value: typeof result.ino === 'bigint' ? exact : numeric,
+ });
+ }
+ return result;
+ });
+ syncBuiltinESMExports();
+ const serve = sirv(root, {
+ dev: true,
+ single: true,
+ setHeaders: createShellHeaderSetter(root),
+ });
+ const server = createServer((req, res) => serve(req, res));
+ let pending: Promise<void> | undefined;
+ onTestFinished(async () => {
+ try {
+ await closeTestHttpServer(server);
+ } finally {
+ await Promise.allSettled([pending]);
+ }
+ });
+ pending = (async () => {
+ const listening = once(server, 'listening', { signal });
+ server.listen({ port: 0, host: '127.0.0.1' });
+ await listening;
+ const address = server.address();
+ if (address === null || typeof address === 'string') throw new Error('No HTTP address');
+ for (const pathname of ['/', '/nested/fallback']) {
+ const response = await fetch(`http://127.0.0.1:${address.port}${pathname}`, { signal });
+ expect(await response.text()).toBe(html);
+ expect(response.status).toBe(200);
+ expect(response.headers.get('content-length')).toBe(String(Buffer.byteLength(html)));
+ expect(response.headers.get('last-modified')).toBe(stamp.toUTCString());
+ expect(observed).toEqual(new Set([first, second]));
+ expect(response.headers.get('content-security-policy')).toContain(
+ `'sha256-${createHash('sha256').update(script).digest('base64')}'`,
+ );
+ }
+ })();
+ await pending;
+ },
+);
diff --git a/packages/server/src/suggest-links.test.ts b/packages/server/src/suggest-links.test.ts
index 63595da4c..623c18e59 100644
--- a/packages/server/src/suggest-links.test.ts
+++ b/packages/server/src/suggest-links.test.ts
@@ -2,6 +2,7 @@ import { mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { Hocuspocus } from '@hocuspocus/server';
+import { filesystemIdentity } from '@inkeep/open-knowledge-core/server';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
import { AGENT_WRITE_ORIGIN, applyAgentMarkdownWrite } from './agent-sessions.ts';
@@ -16,11 +17,12 @@ function buildFileIndex(dir: string, docNames: string[]): ReadonlyMap<string, Fi
for (const docName of docNames) {
const filePath = join(dir, `${docName}.md`);
const stats = statSync(filePath);
+ const statsIdentity = filesystemIdentity(statSync(filePath, { bigint: true }));
index.set(docName, {
size: stats.size,
modified: stats.mtime.toISOString(),
canonicalPath: filePath,
- inode: stats.ino,
+ inode: statsIdentity,
aliases: [],
});
}
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 000000000..da9e57361
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,52 @@
+diff --git a/build.js b/build.js
+index 3734120d67745ff83b2df07fa5d0a40dcb92a69b..0a5ab7af0d72440436638bc39203cd8203c68e2d 100644
+--- a/build.js
++++ b/build.js
+@@ -191,7 +191,7 @@ module.exports = function (dir, opts={}) {
+ res.setHeader('Vary', 'Accept-Encoding');
+ }
+
+- setHeaders(res, pathname, data.stats);
++ setHeaders(res, pathname, data.stats, data.abs);
+ send(req, res, data.abs, data.stats, data.headers);
+ };
+ }
+diff --git a/build.mjs b/build.mjs
+index 2f866d5216c951ec125f2044af070fa6b530e375..823066030292622030a89c2914780d8239c70c2c 100644
+--- a/build.mjs
++++ b/build.mjs
+@@ -191,7 +191,7 @@ export default function (dir, opts={}) {
+ res.setHeader('Vary', 'Accept-Encoding');
+ }
+
+- setHeaders(res, pathname, data.stats);
++ setHeaders(res, pathname, data.stats, data.abs);
+ send(req, res, data.abs, data.stats, data.headers);
+ };
+ }
+diff --git a/index.d.mts b/index.d.mts
+index 8bfe364f1db2d1382c56a9b75a014579083cfa70..d4b2f87b5cb05a37694a20dedd93de906c31b73f 100644
+--- a/index.d.mts
++++ b/index.d.mts
+@@ -23,7 +23,7 @@ export interface Options {
+ brotli?: boolean;
+ gzip?: boolean;
+ onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void;
+- setHeaders?: (res: ServerResponse, pathname: string, stats: Stats) => void;
++ setHeaders?: (res: ServerResponse, pathname: string, stats: Stats, filePath: string) => void;
+ }
+
+ export default function (dir?: string, opts?: Options): RequestHandler;
+diff --git a/index.d.ts b/index.d.ts
+index 96cc63e7ee1ccdd7266e9193252b799068ef2e3c..7275f8ce2e76687bd433c0d74f3185bc0241331c 100644
+--- a/index.d.ts
++++ b/index.d.ts
+@@ -24,7 +24,7 @@ declare namespace sirv {
+ brotli?: boolean;
+ gzip?: boolean;
+ onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void;
+- setHeaders?: (res: ServerResponse, pathname: string, stats: Stats) => void;
++ setHeaders?: (res: ServerResponse, pathname: string, stats: Stats, filePath: string) => void;
+ }
+ }
+
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2306dbb37..6830a06c5 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -157,6 +157,7 @@ patchedDependencies:
[email protected]: b06bc76a3813cc8142d7f75f85020a8ebff65a0ee84fac816da25986da6aa2a1
[email protected]: d59ed459a6ae570e843d0829f844dde24c0697b883351b198034f47606766517
[email protected]: bd17d922d19720e50c1805aeb59f56599bf2b15eefbb6e93fc84f7a548793f77
+ [email protected]: c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842
[email protected]: bc95fab9af70f704280673eafabcb5a40cc180fd5122d62e6c44cac2f7076245
[email protected]: b77fc38bdf246f08f73b6bc5d12ec24d29408b663f0a4224758d3f776ef124e7
@@ -795,7 +796,7 @@ importers:
version: 11.0.0([email protected])
sirv:
specifier: ^3.0.2
- version: 3.0.2
+ version: 3.0.2(patch_hash=c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842)
size-limit:
specifier: ^12.1.0
version: 12.1.0([email protected])
@@ -1372,7 +1373,7 @@ importers:
version: 3.36.0([email protected])
sirv:
specifier: ^3.0.1
- version: 3.0.2
+ version: 3.0.2(patch_hash=c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842)
ws:
specifier: ^8.20.0
version: 8.21.0
@@ -17283,7 +17284,7 @@ snapshots:
'@vitest/utils': 5.0.3
magic-string: 1.4.2
pngjs: 7.0.0
- sirv: 3.0.2
+ sirv: 3.0.2(patch_hash=c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842)
tinyrainbow: 3.2.0
ws: 8.22.0
@@ -17314,7 +17315,7 @@ snapshots:
fflate: 0.8.3
flatted: 3.4.4
pathe: 2.0.3
- sirv: 3.0.2
+ sirv: 3.0.2(patch_hash=c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842)
tinyrainbow: 3.2.0
@@ -23071,7 +23072,7 @@ snapshots:
dependencies:
semver: 7.8.5
+ [email protected](patch_hash=c18b28f0bc922fd0abed32545bae498c471d8f715f8d5fac40e84840d4e23842):
dependencies:
'@polka/url': 1.0.0-next.29
mrmime: 2.0.1
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index dcc9e5002..71392ca2c 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -303,5 +303,6 @@ patchedDependencies: