Translate only an actual active-SHA uniqueness conflict into a safe 409 response. Preserve both records and their bytes so restoring the earlier entry can be retried after deleting the replacement; unrelated database failures still propagate.
AI-assisted: implemented and validated with OpenAI Codex; isolated API and real SQLite constraints, no user workspace writes.
* feat: Persistent File Library with API router (issue #1437)
FileLibraryStore (deeptutor/services/storage/file_library.py):
- add_file: stores bytes to disk, deduplicates by SHA-256
- get_file: returns entry by id
- delete_file: soft-delete, idempotent
- hard_delete_file: permanent delete (requires prior soft-delete)
- restore_file: clears is_deleted, idempotent
- list_files: active entries, newest-first, paginated
- search_files: case-insensitive filename LIKE search
- resolve_path: returns absolute Path or None
API router (deeptutor/api/routers/file_library.py):
- GET /files/library/ — list files
- POST /files/library/ — multipart upload with SHA-256 deduplication
- GET /files/library/search — filename search
- GET /files/library/{id} — get entry
- GET /files/library/{id}/download — download file bytes
- DELETE /files/library/{id} — soft-delete
- POST /files/library/{id}/restore — restore soft-deleted
25 unit tests + 20 API integration tests, all passing.
* fix: ruff import-sort/unused-import fixes for file library
* fix: scope file library singleton per user, close upload dedup race
- get_file_library_store() was caching the FileLibraryStore under a
hardcoded key ("default") instead of the resolved per-user root, so
in multi-user deployments every user after the first shared one
library (list/get/download/delete/restore all leaked across
accounts). Now resolves get_path_service().get_user_root() on every
call and keys the cache by str(user_root), mirroring
get_attachment_store().
- _add_file_sync's SHA-256 dedup check was a plain SELECT followed by
an INSERT with no synchronization; concurrent uploads of identical
content (double-click, duplicate tabs, client retries) could both
miss the dedup lookup and each create a duplicate row/file. Added a
per-instance lock around the check-then-write-then-insert sequence,
plus a partial unique index on sha256 (active rows only) with
IntegrityError handling as a defense-in-depth backstop for
multi-process deployments.
- Fixed the test fixture in test_file_library_router.py: it patched
file_library._fl.get_file_library_store then reverted the patch
before any request ran, so the mock never actually intercepted
anything; removed the dead code and added tokens/auth_enabled
support for multi-user testing.
- Added regression tests: test_upload_and_list_are_isolated_between_users
and test_add_file_concurrent_uploads_of_same_content_do_not_duplicate,
both verified to fail against the pre-fix code.
- ruff format on the three touched files (pre-existing line-length
debt in code this change touches).