Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): show doubled-extension files in the file tree (PRD-7746) (#3136)

Andrew Mikofalvye609c6aa0667
diff --git a/.changeset/file-tree-doubled-extension-row.md b/.changeset/file-tree-doubled-extension-row.md
new file mode 100644
index 000000000..a513d29e0
--- /dev/null
+++ b/.changeset/file-tree-doubled-extension-row.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Files whose name carries a doubled markdown extension now get their own row in the Files sidebar. A `notes.md.md` sitting next to a real `notes.md` resolved to the same sidebar path as its neighbour, and the sidebar kept only the first of the two, so the other file had no row, no badge, and no warning even though it was still on disk and still reachable everywhere else. Each file now appears under its real name, so a stray doubled extension is visible and can be renamed or deleted. Same-name `.md` and `.mdx` pairs are unchanged, since those already resolved to separate rows.
diff --git a/packages/app/src/components/file-tree-adapter.test.ts b/packages/app/src/components/file-tree-adapter.test.ts
index b91158dc7..505027ad6 100644
--- a/packages/app/src/components/file-tree-adapter.test.ts
+++ b/packages/app/src/components/file-tree-adapter.test.ts
@@ -80,6 +80,172 @@ describe('file-tree-adapter', () => {
expect(documentsToTreePaths([stale, fresh])).toEqual(['note.mdx']);
});
+ test('a doubled markdown extension keeps its own row next to the base file', () => {
+ // `name.md.md` strips to docName `name.md`, which already ends in `.md` and
+ // so maps to tree path `name.md` verbatim — the same path the real `name.md`
+ // claims. Both files exist on disk and both must be reachable; collapsing
+ // them to one row hides a real file with no badge, warning, or error.
+ const base: FileEntry = {
+ kind: 'document',
+ docName: 'name',
+ docExt: '.md',
+ size: 10,
+ modified: '2026-01-01T00:00:00Z',
+ };
+ const doubled: FileEntry = {
+ kind: 'document',
+ docName: 'name.md',
+ docExt: '.md',
+ size: 20,
+ modified: '2026-01-01T00:00:01Z',
+ };
+ expect(documentsToTreePaths([base, doubled])).toEqual(['name.md', 'name.md.md']);
+ // Order-independent: directory listing order is not guaranteed, so the
+ // collision must resolve the same way when the doubled file arrives first.
+ expect(documentsToTreePaths([doubled, base])).toEqual(['name.md.md', 'name.md']);
+ });
+
+ test('the relocated doubled-extension row still resolves to its real docName', () => {
+ // A visible row that opens the wrong doc is not actionable. `name.md.md`
+ // must round-trip to docName `name.md` (what the CRDT layer addresses it
+ // by), and the base row must stay on `name`.
+ const base: FileEntry = {
+ kind: 'document',
+ docName: 'name',
+ docExt: '.md',
+ size: 10,
+ modified: '2026-01-01T00:00:00Z',
+ };
+ const doubled: FileEntry = {
+ kind: 'document',
+ docName: 'name.md',
+ docExt: '.md',
+ size: 20,
+ modified: '2026-01-01T00:00:01Z',
+ };
+ expect(treeFilePathToDocumentDocName('name.md.md', [base, doubled])).toBe('name.md');
+ expect(treeFilePathToDocumentDocName('name.md', [base, doubled])).toBe('name');
+ // Order-independent for the same reason the forward direction is: both
+ // entries produce the raw tree path `name.md`, so a listing that returns
+ // the doubled file first would otherwise make the `name.md` row open,
+ // rename, and DELETE `name.md.md` while the real `name.md` goes unreachable.
+ expect(treeFilePathToDocumentDocName('name.md.md', [doubled, base])).toBe('name.md');
+ expect(treeFilePathToDocumentDocName('name.md', [doubled, base])).toBe('name');
+ });
+
+ test('server-qualified same-stem siblings keep their own paths and fabricate nothing', () => {
+ // When `note.md` and `note.mdx` share a stem, the Show All walk qualifies
+ // BOTH docNames with their extension. Those are already distinct tree
+ // paths. A third, extension-less entry for the same file (an index-backed
+ // listing merged into the same array) is the SAME file under a second
+ // spelling — it must dedupe, not grow a phantom `note.mdx.mdx` row.
+ const qualifiedMd: FileEntry = {
+ kind: 'document',
+ docName: 'note.md',
+ docExt: '.md',
+ size: 10,
+ modified: '2026-01-01T00:00:00Z',
+ };
+ const qualifiedMdx: FileEntry = {
+ kind: 'document',
+ docName: 'note.mdx',
+ docExt: '.mdx',
+ size: 20,
+ modified: '2026-01-01T00:00:00Z',
+ };
+ const unqualified: FileEntry = {
+ kind: 'document',
+ docName: 'note',
+ docExt: '.mdx',
+ size: 20,
+ modified: '2026-01-01T00:00:00Z',
+ };
+ expect(documentsToTreePaths([qualifiedMd, qualifiedMdx, unqualified])).toEqual([
+ 'note.md',
+ 'note.mdx',
+ ]);
+ });
+
+ test('server-qualified case-variant siblings keep their verbatim filenames', () => {
+ // `name.md` and `name.MD` are two files on a case-sensitive filesystem, and
+ // the server strips extensions case-insensitively — so the Show All walk
+ // qualifies BOTH docNames and each already IS its filename. Counting the
+ // stem's extensions case-insensitively would see one extension, read both
+ // as doubled, and point each row at a file that does not exist.
+ const lower: FileEntry = {
+ kind: 'document',
+ docName: 'name.md',
+ docExt: '.md',
+ size: 1,
+ modified: '',
+ };
+ const upper: FileEntry = {
+ kind: 'document',
+ docName: 'name.MD',
+ docExt: '.MD',
+ size: 2,
+ modified: '',
+ };
+ expect(documentsToTreePaths([lower, upper])).toEqual(['name.md', 'name.MD']);
+ });
+
+ test('a lone doubled extension with no sibling still gets its real filename', () => {
+ // The common shape of the bug: a stray `notes.md.md` in a directory that
+ // has no `notes.md` at all. Nothing collides, so nothing forces the issue —
+ // but the row must still point at the file that exists on disk.
+ expect(
+ documentsToTreePaths([
+ { kind: 'document', docName: 'notes.md', docExt: '.md', size: 1, modified: '' },
+ ]),
+ ).toEqual(['notes.md.md']);
+ // Same in a subdirectory — the stem is taken from the full slashed docName.
+ expect(
+ documentsToTreePaths([
+ { kind: 'document', docName: 'docs/guide.md', docExt: '.md', size: 1, modified: '' },
+ ]),
+ ).toEqual(['docs/guide.md.md']);
+ });
+
+ test('every returned tree path is unique for adversarial collision input', () => {
+ // `@pierre/trees` `PathStoreBuilder` hard-throws `Duplicate path`, which
+ // takes the whole editor down through the top-level error boundary. The
+ // uniqueness of this list is the only thing standing between a doubled
+ // extension on disk and that crash, so pin it directly.
+ const entries: FileEntry[] = [
+ { kind: 'document', docName: 'a', docExt: '.md', size: 1, modified: '' },
+ { kind: 'document', docName: 'a.md', docExt: '.md', size: 1, modified: '' },
+ { kind: 'document', docName: 'a.md.md', docExt: '.md', size: 1, modified: '' },
+ { kind: 'document', docName: 'a.md', docExt: '.md', size: 2, modified: '' },
+ { kind: 'document', docName: 'a', docExt: '.md', size: 3, modified: '' },
+ { kind: 'document', docName: 'b.mdx', docExt: '.mdx', size: 1, modified: '' },
+ { kind: 'document', docName: 'b', docExt: '.mdx', size: 1, modified: '' },
+ ];
+ const paths = documentsToTreePaths(entries);
+ expect(new Set(paths).size).toBe(paths.length);
+ // Five genuinely distinct files on disk — the `a` family (`a.md`,
+ // `a.md.md`, `a.md.md.md`) and the `b` pair (`b.mdx.mdx`, `b.mdx`) — each
+ // keep a row; the repeated entries dedupe.
+ expect(paths).toEqual(['a.md', 'a.md.md', 'a.md.md.md', 'b.mdx.mdx', 'b.mdx']);
+ });
+
+ test('a doubled extension sharing a stem with an .md/.mdx pair stays collapsed but never duplicates', () => {
+ // Known residual, pinned for the crash invariant rather than the row.
+ // With `a.md`, `a.mdx` AND `a.md.md` in one directory, the Show All walk
+ // qualifies the same-stem pair — so the real `a.md` arrives as docName
+ // `a.md`, which is byte-identical to what `a.md.md` strips to. The two
+ // files are indistinguishable on the wire, so `a.md.md` keeps collapsing.
+ // What must hold is that the feed stays unique: a repeat here would throw
+ // `Duplicate path` and take the window down.
+ const entries: FileEntry[] = [
+ { kind: 'document', docName: 'a.md', docExt: '.md', size: 1, modified: '' },
+ { kind: 'document', docName: 'a.mdx', docExt: '.mdx', size: 2, modified: '' },
+ { kind: 'document', docName: 'a.md', docExt: '.md', size: 3, modified: '' },
+ ];
+ const paths = documentsToTreePaths(entries);
+ expect(new Set(paths).size).toBe(paths.length);
+ expect(paths).toEqual(['a.md', 'a.mdx']);
+ });
+
test('converts Trees file and directory paths back to app paths', () => {
expect(treeFilePathToDocName('docs/guide.md')).toBe('docs/guide');
expect(treeFilePathToDocName('docs/guide')).toBe('docs/guide');
diff --git a/packages/app/src/components/file-tree-adapter.ts b/packages/app/src/components/file-tree-adapter.ts
index d4b9fd1ad..733422a50 100644
--- a/packages/app/src/components/file-tree-adapter.ts
+++ b/packages/app/src/components/file-tree-adapter.ts
@@ -51,10 +51,22 @@ export function treeFilePathToDocumentDocName(
documents: readonly FileEntry[],
): string {
const normalized = stripTrailingSlash(treePath);
- const exact = documents.find(
- (entry): entry is DocumentEntry =>
- isDocumentEntry(entry) && fileEntryToTreePath(entry) === normalized,
- );
+ // Resolve against the same collection-aware mapping the rows were built
+ // from, or a visible row can act on the wrong file: a doubled extension and
+ // the real base file both map RAW to `name.md`, so a flip in listing order
+ // would silently point the `name.md` row's open / rename / delete at
+ // `name.md.md`. The raw pass stays as a fallback for tree paths this feed
+ // never produced — Pierre can move a node out from under the document list.
+ const stemExtensions = markdownStemExtensions(documents);
+ const exact =
+ documents.find(
+ (entry): entry is DocumentEntry =>
+ isDocumentEntry(entry) && collectionTreePath(entry, stemExtensions) === normalized,
+ ) ??
+ documents.find(
+ (entry): entry is DocumentEntry =>
+ isDocumentEntry(entry) && fileEntryToTreePath(entry) === normalized,
+ );
if (exact) return exact.docName;
const extensionless = treeFilePathToDocName(normalized);
const collidingEntry = documents.find((entry) => {
@@ -68,6 +80,25 @@ export function treeFilePathToDocumentDocName(
return collidingEntry && TREE_EXTENSION_PATTERN.test(normalized) ? normalized : extensionless;
}
+/**
+ * The entry a tree path belongs to, resolved through the same collection-aware
+ * mapping the rows are built from. Two documents can share a RAW tree path — a
+ * doubled extension and its base file both map to `name.md` — so a raw match
+ * hands back whichever the server happened to list first and points that row's
+ * action at the other file. Raw matching stays as a fallback for tree paths
+ * this feed never produced.
+ */
+export function findEntryByTreePath(
+ treePath: string,
+ documents: readonly FileEntry[],
+): FileEntry | undefined {
+ const stemExtensions = markdownStemExtensions(documents);
+ return (
+ documents.find((entry) => collectionTreePath(entry, stemExtensions) === treePath) ??
+ documents.find((entry) => fileEntryToTreePath(entry) === treePath)
+ );
+}
+
export function fileEntryToTreePath(entry: FileEntry): string {
if (isFolderEntry(entry)) return folderPathToTreeDirectoryPath(entry.path);
return isAssetEntry(entry) ? entry.path : docNameToTreePath(entry.docName, entry.docExt);
@@ -98,19 +129,85 @@ export function treePathToAppPath(treePath: string): string {
: treeFilePathToDocName(treePath);
}
+/**
+ * Map every extension-suffixed document docName to the set of extensions seen
+ * for its stem — the collection evidence for whether the server
+ * extension-QUALIFIED a docName. When `note.md` and `note.mdx` share a
+ * directory, the Show All walk emits BOTH docNames with their extension so
+ * each file stays independently addressable, and those docNames already ARE
+ * their filenames. A doubled extension on disk produces a docName of the same
+ * shape (`name.md.md` strips to `name.md`) but means the opposite, and one
+ * entry alone can't tell them apart.
+ *
+ * Counting extensions per stem approximates that decision; it is exact only
+ * while at most one file per stem carries a doubled extension. Two doubled
+ * files sharing a stem (`a.md.md` plus `a.mdx.mdx`) present as a qualified
+ * pair and stay collapsed onto their stripped names. Extensions keep their
+ * on-disk casing here because the server qualifies case-variant siblings too
+ * (`name.md` beside `name.MD` on a case-sensitive filesystem) — folding case
+ * would count that pair once and read both as doubled.
+ */
+function markdownStemExtensions(documents: readonly FileEntry[]): Map<string, ReadonlySet<string>> {
+ const byStem = new Map<string, Set<string>>();
+ for (const entry of documents) {
+ if (!isDocumentEntry(entry)) continue;
+ const match = entry.docName.match(TREE_EXTENSION_PATTERN);
+ if (!match) continue;
+ const stem = entry.docName.slice(0, -match[0].length);
+ const extensions = byStem.get(stem);
+ if (extensions) extensions.add(match[0]);
+ else byStem.set(stem, new Set([match[0]]));
+ }
+ return byStem;
+}
+
+/**
+ * The tree path for an entry, resolved with collection context.
+ *
+ * `docNameToTreePath` returns any docName already ending in `.md`/`.mdx`
+ * verbatim. That is right for a server-qualified docName and wrong for a
+ * doubled extension: `name.md.md` arrives as docName `name.md`, maps to
+ * `name.md`, and lands on the exact path the real `name.md` occupies. Sending
+ * both to the model would throw `Duplicate path`; dropping one hides a real
+ * file. Resolving the doubled entry to its true filename does neither.
+ *
+ * Only markdown docNames take this branch — mermaid and editable-text
+ * docNames carry extensions that `TREE_EXTENSION_PATTERN` never matches, so
+ * they keep `docNameToTreePath`'s verbatim handling.
+ */
+function collectionTreePath(
+ entry: FileEntry,
+ stemExtensions: ReadonlyMap<string, ReadonlySet<string>>,
+): string {
+ const treePath = fileEntryToTreePath(entry);
+ if (!isDocumentEntry(entry)) return treePath;
+ const match = entry.docName.match(TREE_EXTENSION_PATTERN);
+ if (!match) return treePath;
+ const stem = entry.docName.slice(0, -match[0].length);
+ // Two extensions on one stem — the server qualified these docNames, so the
+ // docName is already the filename.
+ if ((stemExtensions.get(stem)?.size ?? 0) > 1) return treePath;
+ return `${entry.docName}${entry.docExt ?? DEFAULT_TREE_EXTENSION}`;
+}
+
export function documentsToTreePaths(documents: readonly FileEntry[]): string[] {
- // De-dupe by tree path. Two entries with the same tree path are the same tree
- // node — the model can only hold one, and `@pierre/trees` `resetPaths` hard-
- // throws `Duplicate path` (crashing the whole editor) on a collision. A brief
- // double-entry for one doc is reachable when two refreshes race (e.g. a stale
- // size=0 fetch and the fresh size=40 fetch landing in `documents` together),
- // so unique-ify here at the single feed every `resetPaths` call routes through
- // rather than trusting every async `setDocuments` producer to dedupe. First
- // occurrence wins (stable order); the next refresh reconciles the metadata.
+ // Every `resetPaths` call routes through here, and `@pierre/trees`
+ // `PathStoreBuilder` hard-throws `Duplicate path` on a repeat — which takes
+ // the whole editor down through the top-level error boundary. So the list
+ // this returns must be unique no matter what the server sent.
+ //
+ // Uniqueness alone is not enough: skipping a repeat silently deletes a row.
+ // That is correct only when the repeat is the SAME file twice (two refreshes
+ // racing a stale size=0 and a fresh size=40 into `documents` together), and
+ // wrong when two real files merely resolve to one path — then a file that
+ // exists on disk gets no row, no badge, and no warning. `collectionTreePath`
+ // separates the two by giving a doubled-extension file back its real
+ // filename, leaving only genuine repeats for the skip below.
+ const stemExtensions = markdownStemExtensions(documents);
const seen = new Set<string>();
const paths: string[] = [];
for (const entry of documents) {
- const path = fileEntryToTreePath(entry);
+ const path = collectionTreePath(entry, stemExtensions);
if (seen.has(path)) continue;
seen.add(path);
paths.push(path);
diff --git a/packages/app/src/components/file-tree-selection.test.ts b/packages/app/src/components/file-tree-selection.test.ts
index 7c8841130..33d9f807a 100644
--- a/packages/app/src/components/file-tree-selection.test.ts
+++ b/packages/app/src/components/file-tree-selection.test.ts
@@ -164,6 +164,45 @@ describe('resolveFileTreeSelectionAction', () => {
});
});
+ test('opens the base file from its own row when a doubled extension is listed first', () => {
+ // The `name.md` row and the `name.md.md` row are both visible now, and both
+ // entries map RAW to `name.md`. Picking whichever entry the server happened
+ // to list first would open `name.md.md` from the `name.md` row, which is
+ // the wrong file and invisible to the user. Directory order is not
+ // guaranteed, so assert the order that used to resolve incorrectly.
+ const doubled: FileEntry = {
+ kind: 'document',
+ docName: 'name.md',
+ docExt: '.md',
+ size: 20,
+ modified: '',
+ };
+ const base: FileEntry = {
+ kind: 'document',
+ docName: 'name',
+ docExt: '.md',
+ size: 10,
+ modified: '',
+ };
+ expect(resolveFileTreeSelectionAction('name.md', [doubled, base])).toEqual({
+ kind: 'document',
+ path: 'name',
+ });
+ expect(resolveFileTreeSelectionAction('name.md.md', [doubled, base])).toEqual({
+ kind: 'document',
+ path: 'name.md',
+ });
+ // Same answers with the entries the other way round.
+ expect(resolveFileTreeSelectionAction('name.md', [base, doubled])).toEqual({
+ kind: 'document',
+ path: 'name',
+ });
+ expect(resolveFileTreeSelectionAction('name.md.md', [base, doubled])).toEqual({
+ kind: 'document',
+ path: 'name.md',
+ });
+ });
+
test('routes extension-qualified document rows to their exact document identity', () => {
expect(
resolveFileTreeSelectionAction('docs/guide.mdx', [
diff --git a/packages/app/src/components/file-tree-selection.ts b/packages/app/src/components/file-tree-selection.ts
index b0afc1d85..2bbbf336e 100644
--- a/packages/app/src/components/file-tree-selection.ts
+++ b/packages/app/src/components/file-tree-selection.ts
@@ -6,7 +6,7 @@ import {
} from '@inkeep/open-knowledge-core';
import { hashFromAssetPath } from '@/lib/doc-hash';
import {
- fileEntryToTreePath,
+ findEntryByTreePath,
treeFilePathToDocumentDocName,
treePathToAppPath,
} from './file-tree-adapter';
@@ -94,7 +94,7 @@ export function resolveFileTreeSelectionAction(
): FileTreeSelectionAction {
if (!selectedPath) return { kind: 'none' };
- const entry = entries.find((item) => fileEntryToTreePath(item) === selectedPath);
+ const entry = findEntryByTreePath(selectedPath, entries);
const appPath = treePathToAppPath(selectedPath);
const documentDocName = selectedPath.endsWith('/')
? appPath