Skip to content

Commit 63ee4cd

Browse files
fix(wiki): wiki_get and wiki compile miss nested source files (#96022)
* fix(wiki): discover nested source files in QUERY_DIRS Two functions in the memory-wiki extension — listWikiMarkdownFiles (wiki_get runtime lookup) and collectMarkdownFiles (wiki compile indexing) — used fs.readdir without { recursive: true }. Nested source files (e.g. sources/audi/car.md) were silently invisible to both wiki_get and wiki compile. Add recursive: true and adjust path construction using entry.parentPath so nested .md files in all QUERY_DIRS are discovered while preserving the index.md exclusion and backward compatibility with flat vaults. * fix(wiki): remove entry.path fallback, only parentPath is typed on Dirent * fix(wiki): add recursive scan to status.ts and add nested-file regression tests * fix(wiki): use toSorted instead of sort to pass lint * style(memory-wiki): format recursive discovery fix --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 599294b commit 63ee4cd

6 files changed

Lines changed: 122 additions & 8 deletions

File tree

‎extensions/memory-wiki/src/compile.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ describe("compileMemoryWikiVault", () => {
121121
).resolves.toContain('"text":"Alpha is the canonical source page."');
122122
});
123123

124+
it("discovers pages in nested subdirectories during compile", async () => {
125+
const { rootDir, config } = await createVault({
126+
rootDir: nextCaseRoot(),
127+
initialize: true,
128+
});
129+
130+
await fs.mkdir(path.join(rootDir, "sources", "sub"), { recursive: true });
131+
await fs.writeFile(
132+
path.join(rootDir, "sources", "top.md"),
133+
renderWikiMarkdown({
134+
frontmatter: { pageType: "source", id: "source.top", title: "Top Source" },
135+
body: "# Top Source\n",
136+
}),
137+
"utf8",
138+
);
139+
await fs.writeFile(
140+
path.join(rootDir, "sources", "sub", "nested.md"),
141+
renderWikiMarkdown({
142+
frontmatter: { pageType: "source", id: "source.nested", title: "Nested Source" },
143+
body: "# Nested Source\n",
144+
}),
145+
"utf8",
146+
);
147+
148+
const result = await compileMemoryWikiVault(config);
149+
150+
expect(result.pageCounts.source).toBe(2);
151+
// Root index should link to both
152+
await expect(fs.readFile(path.join(rootDir, "index.md"), "utf8")).resolves.toContain(
153+
"[Top Source](sources/top.md)",
154+
);
155+
await expect(fs.readFile(path.join(rootDir, "index.md"), "utf8")).resolves.toContain(
156+
"[Nested Source](sources/sub/nested.md)",
157+
);
158+
// Sources index should link to nested file
159+
await expect(fs.readFile(path.join(rootDir, "sources", "index.md"), "utf8")).resolves.toContain(
160+
"[Nested Source](sub/nested.md)",
161+
);
162+
});
163+
124164
it("renders native directory index links relative to each generated index", async () => {
125165
const { rootDir, config } = await createVault({
126166
rootDir: nextCaseRoot(),

‎extensions/memory-wiki/src/compile.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,15 @@ export type RefreshMemoryWikiIndexesResult = {
364364

365365
async function collectMarkdownFiles(rootDir: string, relativeDir: string): Promise<string[]> {
366366
const dirPath = path.join(rootDir, relativeDir);
367-
const entries = await fs.readdir(dirPath, { withFileTypes: true }).catch(() => []);
367+
const entries = await fs
368+
.readdir(dirPath, { withFileTypes: true, recursive: true })
369+
.catch(() => []);
368370
return entries
369371
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
370-
.map((entry) => path.join(relativeDir, entry.name))
372+
.map((entry) => {
373+
const absPath = path.join(entry.parentPath ?? dirPath, entry.name);
374+
return path.relative(rootDir, absPath).split(path.sep).join("/");
375+
})
371376
.filter((relativePath) => path.basename(relativePath) !== "index.md")
372377
.toSorted((left, right) => left.localeCompare(right));
373378
}

‎extensions/memory-wiki/src/query.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,35 @@ describe("searchMemoryWiki", () => {
10671067
]);
10681068
});
10691069

1070+
it("discovers pages in nested subdirectories", async () => {
1071+
const { rootDir, config } = await createQueryVault({
1072+
initialize: true,
1073+
});
1074+
await fs.mkdir(path.join(rootDir, "sources", "sub"), { recursive: true });
1075+
await fs.writeFile(
1076+
path.join(rootDir, "sources", "top.md"),
1077+
renderWikiMarkdown({
1078+
frontmatter: { pageType: "source", id: "source.top", title: "Top Source" },
1079+
body: "# Top Source\n",
1080+
}),
1081+
"utf8",
1082+
);
1083+
await fs.writeFile(
1084+
path.join(rootDir, "sources", "sub", "nested.md"),
1085+
renderWikiMarkdown({
1086+
frontmatter: { pageType: "source", id: "source.nested", title: "Nested Source" },
1087+
body: "# Nested Source\n",
1088+
}),
1089+
"utf8",
1090+
);
1091+
1092+
const results = await searchMemoryWiki({ config, query: "Source" });
1093+
1094+
expect(results).toHaveLength(2);
1095+
const paths = results.map((r) => r.path).toSorted();
1096+
expect(paths).toEqual(["sources/sub/nested.md", "sources/top.md"]);
1097+
});
1098+
10701099
it("drops gateway-style owner-qualified session hits that collide with the scoped store", async () => {
10711100
const { config } = await createQueryVault({
10721101
initialize: true,

‎extensions/memory-wiki/src/query.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,17 @@ async function listWikiMarkdownFiles(rootDir: string): Promise<string[]> {
245245
await Promise.all(
246246
QUERY_DIRS.map(async (relativeDir) => {
247247
const dirPath = path.join(rootDir, relativeDir);
248-
const entries = await fs.readdir(dirPath, { withFileTypes: true }).catch(() => []);
248+
const entries = await fs
249+
.readdir(dirPath, { withFileTypes: true, recursive: true })
250+
.catch(() => []);
249251
return entries
250252
.filter(
251253
(entry) => entry.isFile() && entry.name.endsWith(".md") && entry.name !== "index.md",
252254
)
253-
.map((entry) => path.join(relativeDir, entry.name));
255+
.map((entry) => {
256+
const absPath = path.join(entry.parentPath ?? dirPath, entry.name);
257+
return path.relative(rootDir, absPath).split(path.sep).join("/");
258+
});
254259
}),
255260
)
256261
).flat();

‎extensions/memory-wiki/src/status.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ describe("resolveMemoryWikiStatus", () => {
9292
expect(status.warnings.map((warning) => warning.code)).toContain("bridge-artifacts-missing");
9393
});
9494

95+
it("discovers pages in nested subdirectories", async () => {
96+
const { rootDir, config } = await createVault({
97+
prefix: "memory-wiki-nested-",
98+
initialize: true,
99+
});
100+
101+
await fs.mkdir(path.join(rootDir, "sources", "sub"), { recursive: true });
102+
await fs.writeFile(
103+
path.join(rootDir, "sources", "top.md"),
104+
renderWikiMarkdown({
105+
frontmatter: { pageType: "source", id: "source.top", title: "Top Source" },
106+
body: "# Top Source\n",
107+
}),
108+
"utf8",
109+
);
110+
await fs.writeFile(
111+
path.join(rootDir, "sources", "sub", "nested.md"),
112+
renderWikiMarkdown({
113+
frontmatter: { pageType: "source", id: "source.nested", title: "Nested Source" },
114+
body: "# Nested Source\n",
115+
}),
116+
"utf8",
117+
);
118+
119+
const status = await resolveMemoryWikiStatus(config, {
120+
pathExists: async () => true,
121+
resolveCommand: async () => null,
122+
});
123+
124+
expect(status.pageCounts.source).toBe(2);
125+
expect(status.sourceCounts.native).toBe(2);
126+
});
127+
95128
it("counts source provenance from the vault", async () => {
96129
const { rootDir, config } = await createVault({
97130
prefix: "memory-wiki-status-",

‎extensions/memory-wiki/src/status.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,28 @@ async function collectVaultCounts(vaultPath: string): Promise<{
8787
};
8888
const dirs = ["entities", "concepts", "sources", "syntheses", "reports"] as const;
8989
for (const dir of dirs) {
90+
const dirPath = path.join(vaultPath, dir);
9091
const entries = await fs
91-
.readdir(path.join(vaultPath, dir), { withFileTypes: true })
92+
.readdir(dirPath, { withFileTypes: true, recursive: true })
9293
.catch(() => []);
9394
for (const entry of entries) {
9495
if (!entry.isFile() || !entry.name.endsWith(".md") || entry.name === "index.md") {
9596
continue;
9697
}
97-
const kind = inferWikiPageKind(path.join(dir, entry.name));
98+
const absolutePath = path.join(entry.parentPath ?? dirPath, entry.name);
99+
const relativeToVault = path.relative(vaultPath, absolutePath).split(path.sep).join("/");
100+
const kind = inferWikiPageKind(relativeToVault);
98101
if (kind) {
99102
pageCounts[kind] += 1;
100103
}
101104
if (dir === "sources") {
102-
const absolutePath = path.join(vaultPath, dir, entry.name);
103105
const raw = await fs.readFile(absolutePath, "utf8").catch(() => null);
104106
if (!raw) {
105107
continue;
106108
}
107109
const page = toWikiPageSummary({
108110
absolutePath,
109-
relativePath: path.join(dir, entry.name),
111+
relativePath: relativeToVault,
110112
raw,
111113
});
112114
if (!page) {

0 commit comments

Comments
 (0)