Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
PRD-10168 Fix project boot when a file opens from another drive (#7967)
Andrew Mikofalvye30b78006f62
diff --git a/.changeset/open-file-on-another-drive.md b/.changeset/open-file-on-another-drive.mdnew file mode 100644index 000000000..437114015--- /dev/null+++ b/.changeset/open-file-on-another-drive.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++On Windows, opening a file on a different drive no longer stops the project from starting with a "path should be a `path.relative()`d string" error.diff --git a/packages/server/src/content-filter-cross-volume.test.ts b/packages/server/src/content-filter-cross-volume.test.tsnew file mode 100644index 000000000..f9e0b48c9--- /dev/null+++ b/packages/server/src/content-filter-cross-volume.test.ts@@ -0,0 +1,92 @@+import { mkdirSync, writeFileSync } from 'node:fs';+import { mkdtemp, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';+import { createContentFilter, createContentFilterAsync } from './content-filter.ts';++const volumes = vi.hoisted(() => ({ roots: [] as string[] }));++vi.mock('node:path', async (importOriginal) => {+ const actual = await importOriginal<typeof import('node:path')>();+ const volumeOf = (p: string) =>+ volumes.roots.find((root) => p === root || p.startsWith(`${root}${actual.sep}`));+ const relative = (from: string, to: string): string => {+ const fromVolume = volumeOf(actual.resolve(from));+ const toVolume = volumeOf(actual.resolve(to));+ if (fromVolume && toVolume && fromVolume !== toVolume) return actual.resolve(to);+ return actual.relative(from, to);+ };+ return { ...actual, relative, default: { ...actual, relative } };+});++describe('content filter when the project and its content sit on different volumes', () => {+ let projectVolume: string;+ let contentVolume: string;+ let xdgDir: string;+ let prevXdg: string | undefined;++ beforeEach(async () => {+ projectVolume = await mkdtemp(join(tmpdir(), 'content-filter-volume-c-'));+ contentVolume = await mkdtemp(join(tmpdir(), 'content-filter-volume-e-'));+ xdgDir = await mkdtemp(join(tmpdir(), 'content-filter-xdg-'));+ volumes.roots = [projectVolume, contentVolume];+ prevXdg = process.env.XDG_CONFIG_HOME;+ process.env.XDG_CONFIG_HOME = xdgDir;+ });++ afterEach(async () => {+ volumes.roots = [];+ if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME;+ else process.env.XDG_CONFIG_HOME = prevXdg;+ await rm(projectVolume, { recursive: true, force: true });+ await rm(contentVolume, { recursive: true, force: true });+ await rm(xdgDir, { recursive: true, force: true });+ });++ function seedDownloads(): { projectDir: string; contentDir: string } {+ const projectDir = join(projectVolume, 'ok-ephemeral-abc123');+ const contentDir = join(contentVolume, 'Downloads');+ mkdirSync(projectDir, { recursive: true });+ mkdirSync(join(contentDir, 'hwi_828'), { recursive: true });+ mkdirSync(join(contentDir, '.obsidian'), { recursive: true });+ writeFileSync(join(contentDir, 'satellite_p1-10.md'), '# Satellite\n');+ writeFileSync(join(contentDir, 'hwi_828', 'note.md'), '# Note\n');+ return { projectDir, contentDir };+ }++ test.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+ ] as const)(+ '%s factory treats a folder on another volume as outside the project',+ async (_factory, create) => {+ const { projectDir, contentDir } = seedDownloads();++ const building = Promise.resolve().then(() => create({ projectDir, contentDir }));+ await expect(building).resolves.toBeDefined();+ const filter = await building;++ expect(filter.isExcluded('satellite_p1-10.md')).toBe(false);+ expect(filter.isExcluded('hwi_828/note.md')).toBe(false);+ expect(filter.isDirExcluded('hwi_828')).toBe(false);+ expect(filter.isPathIgnored('hwi_828/note.md')).toBe(false);+ },+ );++ test.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+ ] as const)('%s factory opens a single file on another volume', async (_factory, create) => {+ const { projectDir, contentDir } = seedDownloads();++ const building = Promise.resolve().then(() =>+ create({ projectDir, contentDir, singleDocRelPath: 'satellite_p1-10.md' }),+ );+ await expect(building).resolves.toBeDefined();+ const filter = await building;++ expect(filter.isExcluded('satellite_p1-10.md')).toBe(false);+ expect(filter.isExcluded('hwi_828/note.md')).toBe(true);+ });+});diff --git a/packages/server/src/content-filter.test.ts b/packages/server/src/content-filter.test.tsindex fc38ab6d8..70d0e9ac2 100644--- a/packages/server/src/content-filter.test.ts+++ b/packages/server/src/content-filter.test.ts@@ -1494,6 +1494,81 @@ describe('ContentFilter', () => {expect(filter.isDirExcluded('tutorials')).toBe(false);});+ test.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+ ] as const)(+ '%s factory applies project ignore files to a contentDir whose name starts with two dots',+ async (_name, create) => {+ const contentDir = join(projectDir, '..notes');+ mkdirSync(contentDir);+ writeFileSync(join(projectDir, '.gitignore'), 'secret.md\n');+ writeFileSync(join(projectDir, '.okignore'), 'hidden.md\n');+ writeFileSync(join(contentDir, 'secret.md'), '# Secret');+ writeFileSync(join(contentDir, 'hidden.md'), '# Hidden');+ writeFileSync(join(contentDir, 'open.md'), '# Open');+ mkdirSync(join(contentDir, 'sub'));+ writeFileSync(join(contentDir, 'sub', '.gitignore'), 'draft.md\n');+ writeFileSync(join(contentDir, 'sub', 'draft.md'), '# Draft');+ writeFileSync(join(contentDir, 'sub', 'kept.md'), '# Kept');++ const filter = await create({ projectDir, contentDir });++ expect(filter.isExcluded('secret.md')).toBe(true);+ expect(filter.isExcluded('hidden.md')).toBe(true);+ expect(filter.isExcluded('open.md')).toBe(false);+ expect(filter.isExcluded('sub/draft.md')).toBe(true);+ expect(filter.isExcluded('sub/kept.md')).toBe(false);+ },+ );++ test.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+ ] as const)(+ '%s factory builds when the project holds a directory named only with dots',+ async (_name, create) => {+ mkdirSync(join(projectDir, '...'));+ writeFileSync(join(projectDir, '...', '.gitignore'), 'draft.md\n');+ writeFileSync(join(projectDir, '...', 'note.md'), '# Note');+ writeFileSync(join(projectDir, 'open.md'), '# Open');++ const building = Promise.resolve().then(() =>+ create({ projectDir, contentDir: projectDir }),+ );+ await expect(building).resolves.toBeDefined();+ const filter = await building;++ expect(filter.isExcluded('open.md')).toBe(false);+ expect(filter.isPathIgnored('.../note.md')).toBe(false);+ const showAll = { bypassFilters: true, respectOkignore: true } as const;+ expect(filter.isDirExcluded('...', showAll)).toBe(false);+ expect(filter.isExcluded('.../note.md', showAll)).toBe(false);+ },+ );++ test.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+ ] as const)(+ '%s factory builds when the contentDir is named only with dots',+ async (_name, create) => {+ const contentDir = join(projectDir, '...');+ mkdirSync(join(contentDir, 'sub'), { recursive: true });+ writeFileSync(join(projectDir, '.okignore'), 'hidden.md\n');+ writeFileSync(join(contentDir, 'note.md'), '# Note');+ writeFileSync(join(contentDir, 'sub', 'nested.md'), '# Nested');++ const building = Promise.resolve().then(() => create({ projectDir, contentDir }));+ await expect(building).resolves.toBeDefined();+ const filter = await building;++ expect(filter.isExcluded('note.md')).toBe(false);+ expect(filter.isExcluded('sub/nested.md')).toBe(false);+ expect(filter.isDirExcluded('sub')).toBe(false);+ },+ );+test('handles contentDir completely outside projectDir (dotdot relative path)', async () => {const externalContentDir = await mkdtemp(join(tmpdir(), 'content-filter-external-'));try {diff --git a/packages/server/src/content-filter.ts b/packages/server/src/content-filter.tsindex c61ae7ca8..146b4ea0b 100644--- a/packages/server/src/content-filter.ts+++ b/packages/server/src/content-filter.ts@@ -21,7 +21,7 @@ import { isSupportedDocFile, stripDocExtension } from './doc-extensions.ts';import { isProjectRoot } from './fs/find-project-root.ts';import { getLogger } from './logger.ts';import { incrementInPlaceSkillRescanFailures } from './metrics.ts';-import { toPosix } from './path-utils.ts';+import { escapesRoot, toPosix } from './path-utils.ts';import { withSpan } from './telemetry.ts';const log = getLogger('content-filter');@@ -74,6 +74,16 @@ function isBuiltinSkipDirName(name: string): boolean {return BUILTIN_SKIP_DIRS.has(name) || ALWAYS_SKIP_DIRS.has(name.toLowerCase());}+const LEADING_DOTS_ONLY_SEGMENT = /^\.+(?:\/|$)/;++function hasDotsOnlyLeadingSegment(projectRelPath: string): boolean {+ return LEADING_DOTS_ONLY_SEGMENT.test(projectRelPath);+}++function isOutsideIgnoreScope(projectRelPath: string): boolean {+ return escapesRoot(projectRelPath) || hasDotsOnlyLeadingSegment(projectRelPath);+}+type AttachmentFolderSegment = (candidate: string) => boolean;type AttachmentFolderShape =@@ -696,7 +706,7 @@ export function createContentFilter(opts: ContentFilterOptions): ContentFilter {);const contentRelPrefix = toPosix(relative(projectDir, contentDir));- const contentOutsideProject = contentRelPrefix.startsWith('..');+ const contentOutsideProject = isOutsideIgnoreScope(contentRelPrefix);let ig: Ignore;let okignoreIg: Ignore;@@ -800,7 +810,7 @@ export function createContentFilter(opts: ContentFilterOptions): ContentFilter {syncScope?.pathBase === 'project' || !contentRelPrefix? relativePath: `${contentRelPrefix}/${relativePath}`;- if (projectRelPath.startsWith('..')) return false;+ if (hasDotsOnlyLeadingSegment(projectRelPath)) return false;return ig.ignores(projectRelPath);}@@ -816,6 +826,7 @@ export function createContentFilter(opts: ContentFilterOptions): ContentFilter {function isOkIgnored(relativePath: string): boolean {if (contentOutsideProject) return false;const projectRelPath = contentRelPrefix ? `${contentRelPrefix}/${relativePath}` : relativePath;+ if (hasDotsOnlyLeadingSegment(projectRelPath)) return false;return okignoreIg.ignores(projectRelPath);}@@ -1254,7 +1265,7 @@ function loadNestedIgnoreFiles(const dirPath = join(dir, entry.name);const relToProject = toPosix(relative(projectDir, dirPath));- if (relToProject.startsWith('..')) continue;+ if (isOutsideIgnoreScope(relToProject)) continue;if (ig.ignores(relToProject) || ig.ignores(`${relToProject}/`)) continue;@@ -1308,7 +1319,7 @@ async function initContentDirStateAsync(if (!contentOutsideProject) {const relToProject = toPosix(relative(projectDir, dirPath));- if (relToProject.startsWith('..')) continue;+ if (isOutsideIgnoreScope(relToProject)) continue;if (ig.ignores(relToProject) || ig.ignores(`${relToProject}/`)) continue;for (const name of IGNORE_FILE_NAMES) {@@ -1361,7 +1372,7 @@ export async function createContentFilterAsync(opts: ContentFilterOptions): Prom);const contentRelPrefix = toPosix(relative(projectDir, contentDir));- const contentOutsideProject = contentRelPrefix.startsWith('..');+ const contentOutsideProject = isOutsideIgnoreScope(contentRelPrefix);let ig = ignore();let okignoreIg = ignore();@@ -1384,7 +1395,7 @@ export async function createContentFilterAsync(opts: ContentFilterOptions): PromsyncScope?.pathBase === 'project' || !contentRelPrefix? relativePath: `${contentRelPrefix}/${relativePath}`;- if (projectRelPath.startsWith('..')) return false;+ if (hasDotsOnlyLeadingSegment(projectRelPath)) return false;return ig.ignores(projectRelPath);}@@ -1400,6 +1411,7 @@ export async function createContentFilterAsync(opts: ContentFilterOptions): Promfunction isOkIgnored(relativePath: string): boolean {if (contentOutsideProject) return false;const projectRelPath = contentRelPrefix ? `${contentRelPrefix}/${relativePath}` : relativePath;+ if (hasDotsOnlyLeadingSegment(projectRelPath)) return false;return okignoreIg.ignores(projectRelPath);}diff --git a/packages/server/src/mcp/tools/path-safety.test.ts b/packages/server/src/mcp/tools/path-safety.test.tsindex 1635f8b5f..18026a0a8 100644--- a/packages/server/src/mcp/tools/path-safety.test.ts+++ b/packages/server/src/mcp/tools/path-safety.test.ts@@ -1,6 +1,7 @@import { win32 } from 'node:path';import { describe, expect, test } from 'vitest';-import { escapesRoot, resolveWithinRoot } from './path-safety.ts';+import { escapesRoot } from '../../path-utils.ts';+import { resolveWithinRoot } from './path-safety.ts';describe('resolveWithinRoot — accepts inputs contained in root', () => {test('plain relative path', () => {@@ -124,7 +125,7 @@ describe('escapesRoot — win32 separators (PRD-8341)', () => {'refuses win32 absolute %s',(candidate) => {const rel = win32.relative(root, win32.resolve(root, candidate));- expect(escapesRoot(rel) || win32.isAbsolute(rel)).toBe(true);+ expect(escapesRoot(rel, win32)).toBe(true);},);diff --git a/packages/server/src/mcp/tools/path-safety.ts b/packages/server/src/mcp/tools/path-safety.tsindex 6c5be7424..67b0510a6 100644--- a/packages/server/src/mcp/tools/path-safety.ts+++ b/packages/server/src/mcp/tools/path-safety.ts@@ -1,4 +1,5 @@import { isAbsolute, relative, resolve, sep } from 'node:path';+import { escapesRoot } from '../../path-utils.ts';interface ContainmentOk {ok: true;@@ -13,11 +14,6 @@ interface ContainmentErr {type ContainmentResult = ContainmentOk | ContainmentErr;-export function escapesRoot(rel: string): boolean {- const forwardSlashed = rel.replace(/\\/g, '/');- return forwardSlashed === '..' || forwardSlashed.startsWith('../') || isAbsolute(rel);-}-export function resolveWithinRoot(root: string, candidate: string): ContainmentResult {if (typeof root !== 'string' || !isAbsolute(root)) {return { ok: false, reason: `root path is not absolute: ${String(root)}` };diff --git a/packages/server/src/path-utils.test.ts b/packages/server/src/path-utils.test.tsindex 437e9cde6..317a0946a 100644--- a/packages/server/src/path-utils.test.ts+++ b/packages/server/src/path-utils.test.ts@@ -1,5 +1,6 @@+import { posix, win32 } from 'node:path';import { describe, expect, test } from 'vitest';-import { isWithinDir, toPosix } from './path-utils.ts';+import { escapesRoot, isWithinDir, toPosix } from './path-utils.ts';describe('toPosix', () => {test('rewrites Windows backslashes to forward slashes', () => {@@ -44,3 +45,45 @@ describe('isWithinDir', () => {expect(isWithinDir('/Users/mike/kb/research/a.md', '/Users/mike/kb')).toBe(true);});});++describe('escapesRoot', () => {+ test('a path on another Windows drive escapes, because win32 relative returns it absolute', () => {+ const rel = win32.relative(+ 'C:\\Users\\mike\\AppData\\Local\\Temp\\ok-ephemeral-abc',+ 'E:\\Users\\mike\\Downloads',+ );+ expect(rel).toBe('E:\\Users\\mike\\Downloads');+ expect(escapesRoot(rel, win32)).toBe(true);+ expect(escapesRoot(toPosix(rel), win32)).toBe(true);+ });++ test('a child on the same Windows drive is contained', () => {+ const rel = win32.relative('C:\\Users\\mike\\kb', 'C:\\Users\\mike\\kb\\hwi_828');+ expect(escapesRoot(rel, win32)).toBe(false);+ expect(escapesRoot('', win32)).toBe(false);+ });++ test('a parent-relative path escapes on either platform', () => {+ expect(escapesRoot(win32.relative('C:\\Temp\\ok', 'C:\\Users\\mike\\Downloads'), win32)).toBe(+ true,+ );+ expect(escapesRoot(posix.relative('/tmp/ok', '/Users/mike/Downloads'), posix)).toBe(true);+ expect(escapesRoot('..', posix)).toBe(true);+ });++ test('a name that merely starts with two dots is contained', () => {+ expect(escapesRoot('..notes/a.md', posix)).toBe(false);+ expect(escapesRoot('..notes\\a.md', win32)).toBe(false);+ });++ test('a network share relative to a local drive escapes', () => {+ const rel = win32.relative('C:\\Users\\mike\\Temp\\ok', '\\\\server\\share\\Vault');+ expect(rel).toBe('\\\\server\\share\\Vault');+ expect(escapesRoot(toPosix(rel), win32)).toBe(true);+ });++ test('a drive-letter name is only absolute under win32 semantics', () => {+ expect(escapesRoot('E:/Users/mike', win32)).toBe(true);+ expect(escapesRoot('E:/Users/mike', posix)).toBe(false);+ });+});diff --git a/packages/server/src/path-utils.ts b/packages/server/src/path-utils.tsindex 2525f4110..a8af122a9 100644--- a/packages/server/src/path-utils.ts+++ b/packages/server/src/path-utils.ts@@ -1,3 +1,5 @@+import { isAbsolute, type PlatformPath } from 'node:path';+export function toPosix(p: string): string {return p.replace(/\\/g, '/');}@@ -7,3 +9,11 @@ export function isWithinDir(child: string, parent: string): boolean {const p = toPosix(parent);return c === p || c.startsWith(`${p}/`);}++export function escapesRoot(+ rel: string,+ pathApi: Pick<PlatformPath, 'isAbsolute'> = { isAbsolute },+): boolean {+ const forwardSlashed = toPosix(rel);+ return forwardSlashed === '..' || forwardSlashed.startsWith('../') || pathApi.isAbsolute(rel);+}