Repository navigation
Description
Human Summary
write_note kept failing to edit existing files, so I had Claude troubleshoot. Something I've done has caused some path discrepancies that I might expect the sync command to fix, but it's not. Calude came up with a two step workaround to use write_note. Details below.
Bug Description
The write_note tool fails to update existing files in Basic Memory with an error message indicating the file already exists, despite the tool supposedly supporting updates to existing notes. The synchronization process that should reconcile path differences appears to be failing. write_note behaves differently depending on the exact path given.
Steps To Reproduce
Install Basic Memory with persistence enabled
Create a note using write_note tool with a specific folder and title
Attempt to update the same note using write_note with identical folder and title
Receive error: Error executing tool write_note: file for entity [path] already exists
Expected Behavior
When using write_note on an existing note, the function should detect the existing file and update its content, returning a message like: "Updated [path] (checksum)" as shown in the test cases.
Actual Behavior
Instead of updating the note, the system throws an error:
CopyError executing tool write_note: file for entity The First Kings - v2\Project_Narrative\Characters/Seren Thorne already exists: The First Kings - v2\Project_Narrative\Characters\Seren Thorne.md
The error occurs even though the test suite includes tests explicitly verifying that write_note can update existing files. The automatic sync process that should reconcile path differences isn't working as expected.
Environment
OS: Windows 10
Python version: 3.11
Basic Memory version: 0.10.0
Claude Desktop version: 0.9.0
Additional Context
The issue appears to be related to file path handling:
Existing files have permalinks like: first-kings-project/narrative/characters/seren-thorne
We're attempting to write to: The First Kings - v2\Project_Narrative\Characters
When using the exact folder structure of the existing file (First Kings Project/Narrative/Characters), the update works properly, indicating a file path normalization or persistence issue.
Test files confirm that file updates should work:
pythonCopy@pytest.mark.asyncio
async def test_write_note_update_existing(app):
# Creates and then updates a note successfully
# Verifies with "# Updated test/Test Note.md (a8eb4d44)"
Possible Solution
Several potential fixes:
Path Normalization: Ensure consistent normalization of paths between creation and update operations
Permalink-Based Updates: Add a mode to the write_note function that accepts permalinks directly
Synchronization Fix: Debug why the sync process isn't reconciling path differences
Two-Step Process: Implement a workflow that first reads an entity to get its exact path structure before updating
For immediate use, the workaround is to match the exact folder structure of existing files when attempting to update them.
(Written by Claude)
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
phernandez commentedon Mar 31, 2025
Thanks for reporting this issue. This certainly shouldn't be happening. The
write_notetool should either create or a update a file (if it already exists).If you can, could you send a copy of your log? It will be in your <HOME_DIR>/.basic-memory/basis-memory.log
If it's still not working, you can also delete the db for the project and resync. You can run
The db will be recreated from the state of the files in your project.
Be sure to also upgrade to the latest release. If you are using uv:
bdf1992 commentedon Mar 31, 2025
Attached.
I'll troubleshoot with a reset and an update. I'll do a backup before reset and let you know when I try.
basic-memory.log
bdf1992 commentedon Apr 2, 2025
I thought I was able to fix this after troubleshooting, but it's still occurring after a bunch of torubleshooting like.
This is being experienced on 0.11 and after all this troubleshooting.
Exmaple Error
bdf1992 commentedon Apr 2, 2025
OK, if anyone runs into this if I delete the permalinks for all MD files, then delete the database, THEN run a sync I get new permalinks that seem to work.
Ideally the write_note could check for the actual file path in addition to permalinks, before deciding if it's updating or creating.
Also the sync should likely handle the existing permalinks more robustly to handle situations like mine where permalinks are becoming stale and not actually syncing.
phernandez commentedon Apr 5, 2025
claude: what do you make of this?
github-actions commentedon Apr 5, 2025
Claude Code Analysis
Based on my analysis of Issue #71, I've found the root cause and can suggest potential fixes for the write_note tool failing to update existing files.
Root Cause Analysis
The issue occurs because of path normalization discrepancies between entity creation and updates:
Path Normalization Disconnect:
File Existence Check:
EntityService.create_entity, there's an explicit check:Link Resolution Limitations:
link_resolver.resolve_link(schema.permalink)Windows Path Handling:
Potential Fixes
Improve Link Resolution:
LinkResolver.resolve_link()insrc/basic_memory/services/link_resolver.pyto include file path similarity matchingEnhance File Existence Check:
EntityService.create_entity()insrc/basic_memory/services/entity_service.pyto:Add Path Reconciliation:
EntityService.create_or_update_entity()to add a fallback mechanism:Improve Error Handling:
write_note.pyto detect the specific "file already exists" errorCode Snippets for Potential Solutions
Files to Modify
src/basic_memory/services/link_resolver.py- Add path-based entity resolutionsrc/basic_memory/services/entity_service.py- Improve file existence handlingsrc/basic_memory/mcp/tools/write_note.py- Add better error handlingsrc/basic_memory/utils.py- Enhance path normalization for cross-platform compatibilityThis issue highlights a disconnect between permalink normalization and file path handling, especially when users provide different formatting of the same logical path, causing the system to fail to recognize existing entities during updates.
🤖 Generated with Claude Code GitHub Action
phernandez commentedon Apr 6, 2025
claude-fix: lets do option 3 and implement the filename fallback in EntityService
Fix issue #71: [BUG] write_note Tool Fails to Update Existing Files i…
github-actions commentedon Apr 6, 2025
I've analyzed this issue and created a fix in PR: #80
Here's a summary of the changes:
Fixed a bug in the EntityParser class related to handling absolute file paths:
Modified the
parse_filemethod insrc/basic_memory/markdown/entity_parser.pyto correctly handle both absolute and relative paths.Added a new test case in
tests/markdown/test_entity_parser.pyto verify that files with absolute paths can be parsed correctly:test_parse_file_with_absolute_pathThis fix ensures that when users provide absolute paths to files, they will be processed correctly rather than incorrectly joined with the base path, which would result in an invalid path.
Please review the PR and test the changes to verify they resolve the issue.
🤖 Generated with Claude Code GitHub Action
Fix issue #71: [BUG] write_note Tool Fails to Update Existing Files i…
phernandez commentedon Apr 7, 2025
@bdf1992 FYI this should be fixed in the v0.12.0 release