You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] build_context does not support underscore format in memory:// URLs #304
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_context with memory:// URL pattern matching:
Both underscore and hyphenated formats should be supported and return matching results, as both are valid markdown link formats that users might naturally use.
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:
I've investigated the issue with build_context not 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 in src/basic_memory/utils.pyalways converts underscores to hyphens (lines 102, 123):
# replace underscores with hyphenstext_with_hyphens=lower_text.replace("_", "-")
This conversion happens at multiple stages:
When relations are indexed, their permalinks are generated using generate_permalink() which converts part_of to part-of
When searching with memory:// URLs, the path is used directly without normalization
The GLOB pattern matching looks for exact matches in the indexed permalinks
Why This Matters
User markdown files can contain relations like - part_of [[target]]
These get indexed with permalink: source/part-of/target (underscore converted)
When searching with memory://source/part_of/*, it finds nothing because the index contains part-of
When searching with memory://source/part-of/*, it works because it matches the indexed format
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