Repository navigation
Description
name: Bug report
about: Create a report to help us improve Basic Memory
title: '[BUG] build_context does not support underscore format in memory:// URLs'
labels: bug
assignees: ''
Bug Description
When using Basic Memory's build_context function with memory:// URLs, it fails to recognize paths that contain underscores in the relation type segment. The function only works with hyphenated format (e.g., part-of) but not with underscore format (e.g., part_of), even when both formats exist in the actual markdown files.
Steps To Reproduce
- Create markdown files with relation links using both formats:
part-of [[target1]](hyphenated)part_of [[target2]](underscore)
- Use
build_contextwith memory:// URL pattern matching:memory://path/to/file/part-of/*→ Returns results ✅memory://path/to/file/part_of/*→ Returns empty results ❌
Expected Behavior
Both underscore and hyphenated formats should be supported and return matching results, as both are valid markdown link formats that users might naturally use.
Actual Behavior
memory://workflow/specs/workflow-manager-agent/part-of/*returns 2 resultsmemory://workflow/specs/workflow-manager-agent/part_of/*returns 0 results
The underscore format fails silently with empty results, even when matching content exists.
Environment
- OS: macOS 14.5 (Darwin 24.5.0, ARM64)
- Python version: 3.13.5
- Basic Memory version: Latest (via MCP)
- Installation method: MCP server
- Claude Desktop version: Latest
Additional Context
This inconsistency affects user experience as both underscore and hyphenated formats are commonly used in markdown. Users may naturally use either format and expect both to work.
Example of actual file content showing both formats:
- part-of [[Sequence Diagram Operations Guide]] - task parsing syntax, generation standards, participant validation mechanism
- part_of [[Knowledge Deposition Strategy Specification]]Tested URLs that demonstrate the issue:
- Working:
memory://workflow/specs/workflow-manager-agent/part-of/* - Broken:
memory://workflow/specs/workflow-manager-agent/part_of/*
Possible Solution
The URL parsing logic should normalize both underscore and hyphenated formats, or support pattern matching that works with both formats.
Activity
phernandez commentedon Sep 26, 2025
@claude can you research this issue?
claude commentedon Sep 26, 2025
Claude finished @phernandez's task —— View job
Research Complete ✅
I've investigated the issue with
build_contextnot supporting underscore format in memory:// URLs. Here are my findings:Root Cause
The issue occurs because of how permalinks are generated and stored in the search index. The
generate_permalink()function insrc/basic_memory/utils.pyalways converts underscores to hyphens (lines 102, 123):This conversion happens at multiple stages:
generate_permalink()which convertspart_oftopart-ofWhy This Matters
- part_of [[target]]source/part-of/target(underscore converted)memory://source/part_of/*, it finds nothing because the index containspart-ofmemory://source/part-of/*, it works because it matches the indexed formatEvidence from Code
Relation permalink generation (
src/basic_memory/models/knowledge.py:208):Pattern matching in search (
src/basic_memory/services/context_service.py:109):GLOB search uses direct pattern (
src/basic_memory/repository/search_repository.py:411):Tasks Completed
Suggested Solution
Two possible approaches:
Normalize memory:// URL paths before searching (Recommended)
generate_permalink()transformation to the search pathcontext_service.pyStore multiple permalink variants in the index
part-ofandpart_ofversionsThe first approach would be simpler and maintain backward compatibility while fixing the issue.
Job Run · Branch
phernandez commentedon Sep 26, 2025
@claude make a PR with the fix
part_of_to-We should be generous with what we accept as input, and convert it to the format we need internally.
Make sure we have an integration test for this case
claude commentedon Sep 26, 2025
Claude encountered an error —— View job •
claude/issue-304-20250926-1840• Create PR ➔✅ Fix Implemented
I've successfully implemented the fix to support underscores in memory:// URLs for the
build_contextfunction.Changes Made
Modified
context_service.py:generate_permalink()before searching*) and exact path lookupsCreated comprehensive integration tests (
test_build_context_underscore.py):part_ofvspart-of)How it Works
The fix implements the principle of being "generous with what we accept as input" by:
Next Steps
Job Run · Branch
fix: normalize underscores in memory:// URLs for build_context
fix: normalize underscores in memory:// URLs for build_context
fix: normalize underscores in memory:// URLs for build_context