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
241 changes: 241 additions & 0 deletions docs/character-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# Character Handling and Conflict Resolution

Basic Memory handles various character encoding scenarios and file naming conventions to provide consistent permalink generation and conflict resolution. This document explains how the system works and how to resolve common character-related issues.

## Overview

Basic Memory uses a sophisticated system to generate permalinks from file paths while maintaining consistency across different operating systems and character encodings. The system normalizes file paths and generates unique permalinks to prevent conflicts.

## Character Normalization Rules

### 1. Permalink Generation

When Basic Memory processes a file path, it applies these normalization rules:

```
Original: "Finance/My Investment Strategy.md"
Permalink: "finance/my-investment-strategy"
```

**Transformation process:**
1. Remove file extension (`.md`)
2. Convert to lowercase (case-insensitive)
3. Replace spaces with hyphens
4. Replace underscores with hyphens
5. Handle international characters (transliteration for Latin, preservation for non-Latin)
6. Convert camelCase to kebab-case

### 2. International Character Support

**Latin characters with diacritics** are transliterated:
- `ø` → `o` (Søren → soren)
- `ü` → `u` (Müller → muller)
- `é` → `e` (Café → cafe)
- `ñ` → `n` (Niño → nino)

**Non-Latin characters** are preserved:
- Chinese: `中文/测试文档.md` → `中文/测试文档`
- Japanese: `日本語/文書.md` → `日本語/文書`

## Common Conflict Scenarios

### 1. Hyphen vs Space Conflicts

**Problem:** Files with existing hyphens conflict with generated permalinks from spaces.

**Example:**
```
File 1: "basic memory bug.md" → permalink: "basic-memory-bug"
File 2: "basic-memory-bug.md" → permalink: "basic-memory-bug" (CONFLICT!)
```

**Resolution:** The system automatically resolves this by adding suffixes:
```
File 1: "basic memory bug.md" → permalink: "basic-memory-bug"
File 2: "basic-memory-bug.md" → permalink: "basic-memory-bug-1"
```

**Best Practice:** Choose consistent naming conventions within your project.

### 2. Case Sensitivity Conflicts

**Problem:** Different case variations that normalize to the same permalink.

**Example on macOS:**
```
Directory: Finance/investment.md
Directory: finance/investment.md (different on filesystem, same permalink)
```

**Resolution:** Basic Memory detects case conflicts and prevents them during sync operations with helpful error messages.

**Best Practice:** Use consistent casing for directory and file names.

### 3. Character Encoding Conflicts

**Problem:** Different Unicode normalizations of the same logical character.

**Example:**
```
File 1: "café.md" (é as single character)
File 2: "café.md" (e + combining accent)
```

**Resolution:** Basic Memory normalizes Unicode characters using NFD normalization to detect these conflicts.

### 4. Forward Slash Conflicts

**Problem:** Forward slashes in frontmatter or file names interpreted as path separators.

**Example:**
```yaml
---
permalink: finance/investment/strategy
---
```

**Resolution:** Basic Memory validates frontmatter permalinks and warns about path separator conflicts.

## Error Messages and Troubleshooting

### "UNIQUE constraint failed: entity.file_path, entity.project_id"

**Cause:** Two entities trying to use the same file path within a project.

**Common scenarios:**
1. File move operation where destination is already occupied
2. Case sensitivity differences on macOS
3. Character encoding conflicts
4. Concurrent file operations

**Resolution steps:**
1. Check for duplicate file names with different cases
2. Look for files with similar names but different character encodings
3. Rename conflicting files to have unique names
4. Run sync again after resolving conflicts

### "File path conflict detected during move"

**Cause:** Enhanced conflict detection preventing potential database integrity violations.

**What this means:** The system detected that moving a file would create a conflict before attempting the database operation.

**Resolution:** Follow the specific guidance in the error message, which will indicate the type of conflict detected.

## Best Practices

### 1. File Naming Conventions

**Recommended patterns:**
- Use consistent casing (prefer lowercase)
- Use hyphens instead of spaces for multi-word files
- Avoid special characters that could conflict with path separators
- Be consistent with directory structure casing

**Examples:**
```
✅ Good:
- finance/investment-strategy.md
- projects/basic-memory-features.md
- docs/api-reference.md

❌ Problematic:
- Finance/Investment Strategy.md (mixed case, spaces)
- finance/Investment Strategy.md (inconsistent case)
- docs/API/Reference.md (mixed case directories)
```

### 2. Permalink Management

**Custom permalinks in frontmatter:**
```yaml
---
type: knowledge
permalink: custom-permalink-name
---
```

**Guidelines:**
- Use lowercase permalinks
- Use hyphens for word separation
- Avoid path separators unless creating sub-paths
- Ensure uniqueness within your project

### 3. Directory Structure

**Consistent casing:**
```
✅ Good:
finance/
investment-strategies.md
portfolio-management.md

❌ Problematic:
Finance/ (capital F)
investment-strategies.md
finance/ (lowercase f)
portfolio-management.md
```

## Migration and Cleanup

### Identifying Conflicts

Use Basic Memory's built-in conflict detection:

```bash
# Sync will report conflicts
basic-memory sync

# Check sync status for warnings
basic-memory status
```

### Resolving Existing Conflicts

1. **Identify conflicting files** from sync error messages
2. **Choose consistent naming convention** for your project
3. **Rename files** to follow the convention
4. **Re-run sync** to verify resolution

### Bulk Renaming Strategy

For projects with many conflicts:

1. **Backup your project** before making changes
2. **Standardize on lowercase** file and directory names
3. **Replace spaces with hyphens** in file names
4. **Use consistent character encoding** (UTF-8)
5. **Test sync after each batch** of changes

## System Enhancements

### Recent Improvements (v0.13+)

1. **Enhanced conflict detection** before database operations
2. **Improved error messages** with specific resolution guidance
3. **Character normalization utilities** for consistent handling
4. **File swap detection** for complex move scenarios
5. **Proactive conflict warnings** during permalink resolution

### Monitoring and Logging

The system now provides detailed logging for conflict resolution:

```
DEBUG: Detected potential file path conflicts for 'Finance/Investment.md': ['finance/investment.md']
WARNING: File path conflict detected during move: entity_id=123 trying to move from 'old.md' to 'new.md'
```

These logs help identify and resolve conflicts before they cause sync failures.

## Support and Resources

If you encounter character-related conflicts not covered in this guide:

1. **Check the logs** for specific conflict details
2. **Review error messages** for resolution guidance
3. **Report issues** with examples of the conflicting files
4. **Consider the file naming best practices** outlined above

The Basic Memory system is designed to handle most character conflicts automatically while providing clear guidance for manual resolution when needed.
52 changes: 49 additions & 3 deletions src/basic_memory/services/entity_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from basic_memory.markdown.utils import entity_model_from_markdown, schema_to_markdown
from basic_memory.models import Entity as EntityModel
from basic_memory.models import Observation, Relation
from basic_memory.models.knowledge import Entity
from basic_memory.repository import ObservationRepository, RelationRepository
from basic_memory.repository.entity_repository import EntityRepository
from basic_memory.schemas import Entity as EntitySchema
Expand Down Expand Up @@ -44,6 +45,39 @@ def __init__(
self.file_service = file_service
self.link_resolver = link_resolver

async def detect_file_path_conflicts(self, file_path: str) -> List[Entity]:
"""Detect potential file path conflicts for a given file path.

This checks for entities with similar file paths that might cause conflicts:
- Case sensitivity differences (Finance/file.md vs finance/file.md)
- Character encoding differences
- Hyphen vs space differences
- Unicode normalization differences

Args:
file_path: The file path to check for conflicts

Returns:
List of entities that might conflict with the given file path
"""
from basic_memory.utils import detect_potential_file_conflicts

conflicts = []

# Get all existing file paths
all_entities = await self.repository.find_all()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how performant this will be for large numbers of entities

existing_paths = [entity.file_path for entity in all_entities]

# Use the enhanced conflict detection utility
conflicting_paths = detect_potential_file_conflicts(file_path, existing_paths)

# Find the entities corresponding to conflicting paths
for entity in all_entities:
if entity.file_path in conflicting_paths:
conflicts.append(entity)

return conflicts

async def resolve_permalink(
self, file_path: Permalink | Path, markdown: Optional[EntityMarkdown] = None
) -> str:
Expand All @@ -54,18 +88,30 @@ async def resolve_permalink(
2. If markdown has permalink but it's used by another file -> make unique
3. For existing files, keep current permalink from db
4. Generate new unique permalink from file path

Enhanced to detect and handle character-related conflicts.
"""
file_path_str = str(file_path)

# Check for potential file path conflicts before resolving permalink
conflicts = await self.detect_file_path_conflicts(file_path_str)
if conflicts:
logger.warning(
f"Detected potential file path conflicts for '{file_path_str}': "
f"{[entity.file_path for entity in conflicts]}"
)

# If markdown has explicit permalink, try to validate it
if markdown and markdown.frontmatter.permalink:
desired_permalink = markdown.frontmatter.permalink
existing = await self.repository.get_by_permalink(desired_permalink)

# If no conflict or it's our own file, use as is
if not existing or existing.file_path == str(file_path):
if not existing or existing.file_path == file_path_str:
return desired_permalink

# For existing files, try to find current permalink
existing = await self.repository.get_by_file_path(str(file_path))
existing = await self.repository.get_by_file_path(file_path_str)
if existing:
return existing.permalink

Expand All @@ -75,7 +121,7 @@ async def resolve_permalink(
else:
desired_permalink = generate_permalink(file_path)

# Make unique if needed
# Make unique if needed - enhanced to handle character conflicts
permalink = desired_permalink
suffix = 1
while await self.repository.get_by_permalink(permalink):
Expand Down
51 changes: 50 additions & 1 deletion src/basic_memory/sync/sync_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,36 @@ async def handle_move(self, old_path, new_path):

entity = await self.entity_repository.get_by_file_path(old_path)
if entity:
# Check if destination path is already occupied by another entity
existing_at_destination = await self.entity_repository.get_by_file_path(new_path)
if existing_at_destination and existing_at_destination.id != entity.id:
# Handle the conflict - this could be a file swap or replacement scenario
logger.warning(
f"File path conflict detected during move: "
f"entity_id={entity.id} trying to move from '{old_path}' to '{new_path}', "
f"but entity_id={existing_at_destination.id} already occupies '{new_path}'"
)

# Check if this is a file swap (the destination entity is being moved to our old path)
# This would indicate a simultaneous move operation
old_path_after_swap = await self.entity_repository.get_by_file_path(old_path)
if old_path_after_swap and old_path_after_swap.id == existing_at_destination.id:
logger.info(f"Detected file swap between '{old_path}' and '{new_path}'")
# This is a swap scenario - both moves should succeed
# We'll allow this to proceed since the other file has moved out
else:
# This is a conflict where the destination is occupied
raise ValueError(
f"Cannot move entity from '{old_path}' to '{new_path}': "
f"destination path is already occupied by another file. "
f"This may be caused by: "
f"1. Conflicting file names with different character encodings, "
f"2. Case sensitivity differences (e.g., 'Finance/' vs 'finance/'), "
f"3. Character conflicts between hyphens in filenames and generated permalinks, "
f"4. Files with similar names containing special characters. "
f"Try renaming one of the conflicting files to resolve this issue."
)

# Update file_path in all cases
updates = {"file_path": new_path}

Expand All @@ -477,7 +507,26 @@ async def handle_move(self, old_path, new_path):
f"new_checksum={new_checksum}"
)

updated = await self.entity_repository.update(entity.id, updates)
try:
updated = await self.entity_repository.update(entity.id, updates)
except Exception as e:
# Catch any database integrity errors and provide helpful context
if "UNIQUE constraint failed" in str(e):
logger.error(
f"Database constraint violation during move: "
f"entity_id={entity.id}, old_path='{old_path}', new_path='{new_path}'"
)
raise ValueError(
f"Cannot complete move from '{old_path}' to '{new_path}': "
f"a database constraint was violated. This usually indicates "
f"a file path or permalink conflict. Please check for: "
f"1. Duplicate file names, "
f"2. Case sensitivity issues (e.g., 'File.md' vs 'file.md'), "
f"3. Character encoding conflicts in file names."
) from e
else:
# Re-raise other exceptions as-is
raise

if updated is None: # pragma: no cover
logger.error(
Expand Down
Loading