fix: Terminate sync immediately when project is deleted by phernandez · Pull Request #366 · basicmachines-co/basic-memory · GitHub
Skip to content

fix: Terminate sync immediately when project is deleted - #366

Merged
phernandez merged 1 commit into
mainfrom
fix/sync-fatal-error-project-deleted
Oct 16, 2025
Merged

fix: Terminate sync immediately when project is deleted#366
phernandez merged 1 commit into
mainfrom
fix/sync-fatal-error-project-deleted

Conversation

@phernandez

Copy link
Copy Markdown
Member

Summary

Fixes #188 where sync fails with FOREIGN KEY constraint violations when a project is deleted during sync operations.

Problem

When a project is deleted mid-sync, every file fails with a FOREIGN KEY constraint error. The circuit breaker treats these as file-level errors and retries each file 3 times, wasting resources before eventually skipping them all.

Solution

Introduced SyncFatalError exception to distinguish between:

  • Fatal errors (project deleted, database corrupt) → terminate sync immediately
  • Recoverable errors (bad markdown, parse errors) → circuit breaker handles normally

Changes

  • Added SyncFatalError exception in services/exceptions.py
  • Modified entity_repository.upsert_entity() to detect FOREIGN KEY constraint failures and raise SyncFatalError with clear message
  • Updated sync_service.sync_file() to check exception chain for SyncFatalError and re-raise immediately
  • Added repository-level test in test_entity_repository_upsert.py
  • Added integration test in test_sync_service.py to verify sync terminates immediately

Behavior

Before:

  • Project deleted during sync → every file fails with FOREIGN KEY error
  • Circuit breaker tries 3 times per file → wastes time and resources
  • Eventually skips all files after repeated failures

After:

  • Project deleted during sync → first file fails with FOREIGN KEY error
  • SyncFatalError is raised and propagates through exception chain
  • Sync terminates immediately with clear error message
  • No wasted retries on remaining files

Test Plan

  • ✅ Repository test verifies SyncFatalError is raised with correct message
  • ✅ Integration test verifies sync terminates immediately without circuit breaker retry
  • ✅ All existing tests pass

🤖 Generated with Claude Code

Fixes #188 where sync fails with FOREIGN KEY constraint violations when a
project is deleted during sync operations.

Changes:
- Add SyncFatalError exception to distinguish fatal from recoverable errors
- Detect FOREIGN KEY failures in entity_repository.upsert_entity()
- Check exception chain in sync_service.sync_file() and re-raise fatal errors
- Add repository-level test for invalid project_id handling
- Add integration test to verify fatal errors terminate sync immediately

Before: Circuit breaker retries each file 3 times, wastes resources
After: First FOREIGN KEY error terminates sync immediately with clear message

Fatal errors (project deleted, database corrupt) now bypass the circuit
breaker and terminate sync immediately, while recoverable file-level errors
(bad markdown, parse errors) continue to use circuit breaker as intended.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: phernandez <paul@basicmachines.co>
@github-actions

github-actions Bot commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

@phernandez
phernandez merged commit 729a5a3 into main Oct 16, 2025
16 checks passed
@phernandez
phernandez deleted the fix/sync-fatal-error-project-deleted branch October 16, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant