Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/basic_memory/sync/sync_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,25 @@ async def sync_markdown_file(self, path: str, new: bool = True) -> Tuple[Optiona
# Update relations and search index
entity = await self.entity_service.update_entity_relations(path, entity_markdown)

# After updating relations, we need to compute the checksum again
# This is necessary for files with wikilinks to ensure consistent checksums
# after relation processing is complete
final_checksum = await self.file_service.compute_checksum(path)

# set checksum
await self.entity_repository.update(entity.id, {"checksum": checksum})

await self.entity_repository.update(entity.id, {"checksum": final_checksum})
logger.debug(
"Markdown sync completed",
path=path,
entity_id=entity.id,
observation_count=len(entity.observations),
relation_count=len(entity.relations),
checksum=final_checksum,
)

return entity, checksum

# Return the final checksum to ensure everything is consistent
return entity, final_checksum

async def sync_regular_file(self, path: str, new: bool = True) -> Tuple[Optional[Entity], str]:
"""Sync a non-markdown file with basic tracking.
Expand Down
62 changes: 62 additions & 0 deletions tests/sync/test_sync_wikilink_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Test for issue #72 - notes with wikilinks staying in modified status."""

import pytest
from pathlib import Path

from basic_memory.sync.sync_service import SyncService


async def create_test_file(path: Path, content: str) -> None:
"""Create a test file with given content."""
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)


@pytest.mark.asyncio
async def test_wikilink_modified_status_issue(sync_service: SyncService, test_config):
"""Test that files with wikilinks don't remain in modified status after sync."""
project_dir = test_config.home

# Create a file with a wikilink
content = """---
title: Test Wikilink
type: note
---
# Test File

This file contains a wikilink to [[another-file]].
"""
test_file_path = project_dir / "test_wikilink.md"
await create_test_file(test_file_path, content)

# Initial sync
report1 = await sync_service.sync(test_config.home, show_progress=False)
assert "test_wikilink.md" in report1.new
assert "test_wikilink.md" not in report1.modified

# Sync again without changing the file - should not be modified
report2 = await sync_service.sync(test_config.home, show_progress=False)
assert "test_wikilink.md" not in report2.new
assert "test_wikilink.md" not in report2.modified

# Create the target file
target_content = """---
title: Another File
type: note
---
# Another File

This is the target file.
"""
target_file_path = project_dir / "another_file.md"
await create_test_file(target_file_path, content)

# Sync again after adding target file
report3 = await sync_service.sync(test_config.home, show_progress=False)
assert "another_file.md" in report3.new
assert "test_wikilink.md" not in report3.modified

# Sync one more time - both files should now be stable
report4 = await sync_service.sync(test_config.home, show_progress=False)
assert "test_wikilink.md" not in report4.modified
assert "another_file.md" not in report4.modified
Loading