MemoryRetriever.ts indexes only top-level KNOWLEDGE files — subdirectories invisible
MemoryRetriever.ts discoverNotes() calls fs.readdirSync(domainDir).filter(...) at line 123, which lists only direct children of each domain dir. Files in subdirectories (e.g., MEMORY/KNOWLEDGE/Research/2026-02/foo.md) are invisible to BM25 retrieval.
Users who organize their KNOWLEDGE/ with date prefixes, topic subdirs, or per-source hierarchies (a common pattern when migrating from older PAI versions or external archives) lose visibility on everything below the top level. The "Searched N notes" footer silently under-reports the corpus size, so the bug isn't obvious until you grep manually and notice the gap.
Steps to Reproduce
- Create a subdirectory under a KNOWLEDGE/ domain and place a markdown file inside:
mkdir -p ~/.claude/PAI/MEMORY/KNOWLEDGE/Research/2026-02 echo "# Test\n\nThis file mentions unicorns." > ~/.claude/PAI/MEMORY/KNOWLEDGE/Research/2026-02/test.md - Place a parallel file at top level:
echo "# Top\n\nThis file mentions dragons." > ~/.claude/PAI/MEMORY/KNOWLEDGE/Research/top.md - Run retrieval for each term:
bun ~/.claude/PAI/TOOLS/MemoryRetriever.ts "dragons" bun ~/.claude/PAI/TOOLS/MemoryRetriever.ts "unicorns"
Expected Behavior
Both queries return a hit. Footer reports "Searched 2 notes" (or however many KNOWLEDGE files exist total).
Actual Behavior
"dragons" returns the top-level file. "unicorns" returns No matching memories found with Searched 1 notes across People, Companies, Ideas, Research. — the file in the subdirectory is invisible.
Suggested Fix (drop-in-patch)
Add a small recursive walker above discoverNotes(). Patch is +25 / -8 lines, isolated to one helper + one function:
function walkMarkdown(root: string): string[] {
const out: string[] = [];
if (!fs.existsSync(root)) return out;
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(root, { withFileTypes: true });
} catch {
return out;
}
for (const entry of entries) {
if (entry.name.startsWith("_") || entry.name.startsWith(".")) continue;
const full = path.join(root, entry.name);
if (entry.isDirectory()) {
out.push(...walkMarkdown(full));
} else if (entry.isFile() && entry.name.endsWith(".md")) {
out.push(full);
}
}
return out;
}
function discoverNotes(): KnowledgeNote[] {
const notes: KnowledgeNote[] = [];
for (const domain of DOMAINS) {
const domainDir = path.join(KNOWLEDGE_DIR, domain);
const filePaths = walkMarkdown(domainDir);
for (const filePath of filePaths) {
try {
const content = fs.readFileSync(filePath, "utf-8");
const { frontmatter, body } = parseFrontmatter(content);
const wordCount = body.split(/\s+/).filter(Boolean).length;
notes.push({ filePath, frontmatter, body, wordCount });
} catch {
// Skip unreadable files
}
}
}
return notes;
}
Preserves the existing _-prefix exclusion (used for _harvest-queue/ and similar), adds .-prefix exclusion (so VCS metadata like .git/ never gets indexed), preserves the existing error tolerance.
Verification
After applying the patch locally (2026-05-13), a corpus of 40 KNOWLEDGE/Research files (5 top-level + 35 in date-organized and topic-grouped subdirs like 2026-01/, 2026-02/, topic-foo/) became fully searchable. Retrieval probes:
- A query for a term that lives ONLY in a subdir file → 2 hits (was 0)
- A query for a term in a top-level file → 3 hits (no regression)
- A second top-level query → 3 hits (no regression)
- Footer:
"Searched 40 notes"(was 5) - Hidden-sentinel test: planted
.hidden-test/sentinel.mdwith a unique marker → 0 hits (confirms.-dir exclusion) - BM25 weights, CLI flags (
--top,--raw,--budget,--help), frontmatter parsing all unchanged
Impact
- Severity: High — silent under-indexing degrades retrieval quality without any error signal. Users may not notice for months.
- Scope: Anyone using subdirectories under
MEMORY/KNOWLEDGE/{People,Companies,Ideas,Research}/. Date-organized or topic-organized layouts are the obvious failure case; any user migrating from another PAI install or an external archive likely hits this. - Failure mode: Silent. Footer reports the under-count as if it were correct.
File:line citation
PAI/TOOLS/MemoryRetriever.ts:116-141 (the discoverNotes() function).
Related
Same pattern as a sibling bug filed alongside this one: SessionHarvester.ts has a similar hardcoded-path-prevents-reuse problem where PROJECTS_DIR cannot be overridden to point at archive directories. Two-of-a-kind worth fixing together.
Local-fix status: The patch above is applied on my install (with backup at MemoryRetriever.ts.pre-subdir-fix-<ts>); happy to clarify or attach a more detailed diff if helpful.
Hey — Kai here, going through the queue.
Thanks for the careful patch. Your walkMarkdown recursive helper is now in the upstream tree (the source the next release builds from). The change preserves the _-prefix exclusion (so _harvest-queue/ stays out) and adds the .-prefix exclusion you suggested (so .git/ and friends can't accidentally get indexed). discoverNotes() now calls walkMarkdown(domainDir) instead of the flat readdirSync, so any KNOWLEDGE/
Verified with bun ~/.claude/PAI/TOOLS/MemoryRetriever.ts --help (parse clean) and a full bun build (1 module, 11.1 KB, no errors).
The retrieval-quality compound you flagged — silent under-indexing because the footer reports the under-count as if it were correct — was the part that made this worth jumping on. Date-organized or topic-organized layouts under MEMORY/KNOWLEDGE/Research/ will now actually surface.
Ships with the next release. Closing as fixed.
— Kai