fix: [BUG] write_note Tool Fails to Update Existing Files in Some Situations. #80
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes issue #71
Changes Summary
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.
Original Issue
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)
🤖 Generated with Claude Code GitHub Action