One MediaItem row per file path globally — a second library serving the same file never gets its own row #606

Open
opened 2026-07-25 15:15:20 +02:00 by timothy · 0 comments
Owner

Carved out of #496 (and its independent review) as the actual remaining cause of cross-library trashing. This is a whole-app property, not a music-video one — movies, TV, other-video, songs and images all behave this way.

Problem

MediaItemRepository.MediaFileAlreadyExists (ErsatzTV.Infrastructure/Data/Repositories/MediaItemRepository.cs:162) resolves an incoming file by MediaFile.PathHash with no library predicate, and MediaFile.PathHash is globally unique. So one file path is one MediaItem row, process-wide, owned by whichever library scanned it first.

Consequence for two libraries with genuinely overlapping local paths (library A and library B both serve /shared/song.mkv):

  • B's add is refused with MediaFileAlreadyExists; B never gets a row of its own.
  • When A stops reporting the file while B still serves it, A's sweep flags the single shared row FileNotFound — and B has no library-scoped row to keep it available. The item is trashed for B too.

What #496 already fixed (and what it deliberately did not)

#496 gave music videos a per-library server identity (JellyfinMusicVideo.ItemId/Etag) and replaced the hard delete with reversible soft FileNotFound trash. That removed the unrecoverable failure — the shared row is no longer destroyed, and collection membership / playout references survive — and brought music videos to parity with the MediaServer{Movie,Television,OtherVideo} scanners.

It did not make a shared path resolvable to two rows, because that is this issue. #496's Done-when #2 was amended to the parity wording and closed on that basis; the independent review confirmed the diagnosis.

Fix direction (unvalidated — needs design work first)

Make library ownership independent of the single shared MediaItem, e.g. one of:

  • relax MediaFile.PathHash global uniqueness to a (LibraryPathId, PathHash) uniqueness, and make MediaFileAlreadyExists library-scoped; or
  • keep one MediaItem per file but model per-library membership/state separately, so a sweep flags a membership, not the item.

Both touch every media kind and need a migration plus a hard look at what currently depends on the global dedup (it exists to stop the same file being scheduled twice from two libraries — enumerate that before changing it, per process.enumerate-workaround-behaviors-before-deleting).

Notes

Low priority: it needs genuinely overlapping local paths across two libraries, which is an unusual configuration, and the data-loss teeth were pulled by #496.

Done-when

  • The behavior MediaFileAlreadyExists's global dedup provides is enumerated before it is changed
  • Two libraries with overlapping local paths each resolve their own row (or their own membership) for a shared file
  • One library's sweep cannot flag/trash a file another library still serves
  • Behavior verified for at least movies and music videos, not just the kind that motivated it
  • Adversarial review passed
Carved out of #496 (and its independent review) as the *actual* remaining cause of cross-library trashing. This is a whole-app property, **not** a music-video one — movies, TV, other-video, songs and images all behave this way. ## Problem `MediaItemRepository.MediaFileAlreadyExists` (`ErsatzTV.Infrastructure/Data/Repositories/MediaItemRepository.cs:162`) resolves an incoming file by `MediaFile.PathHash` with **no library predicate**, and `MediaFile.PathHash` is globally unique. So one file path is one `MediaItem` row, process-wide, owned by whichever library scanned it first. Consequence for two libraries with genuinely overlapping local paths (library A and library B both serve `/shared/song.mkv`): - B's add is refused with `MediaFileAlreadyExists`; B never gets a row of its own. - When A stops reporting the file while B still serves it, A's sweep flags the single shared row `FileNotFound` — and B has no library-scoped row to keep it available. The item is trashed for B too. ## What #496 already fixed (and what it deliberately did not) #496 gave music videos a per-library server identity (`JellyfinMusicVideo.ItemId/Etag`) and replaced the hard delete with reversible soft `FileNotFound` trash. That removed the **unrecoverable** failure — the shared row is no longer destroyed, and collection membership / playout references survive — and brought music videos to **parity** with the `MediaServer{Movie,Television,OtherVideo}` scanners. It did **not** make a shared path resolvable to two rows, because that is this issue. #496's Done-when #2 was amended to the parity wording and closed on that basis; the independent review confirmed the diagnosis. ## Fix direction (unvalidated — needs design work first) Make library ownership independent of the single shared `MediaItem`, e.g. one of: - relax `MediaFile.PathHash` global uniqueness to a `(LibraryPathId, PathHash)` uniqueness, and make `MediaFileAlreadyExists` library-scoped; or - keep one `MediaItem` per file but model per-library membership/state separately, so a sweep flags a *membership*, not the item. Both touch every media kind and need a migration plus a hard look at what currently *depends* on the global dedup (it exists to stop the same file being scheduled twice from two libraries — enumerate that before changing it, per `process.enumerate-workaround-behaviors-before-deleting`). ## Notes Low priority: it needs genuinely overlapping local paths across two libraries, which is an unusual configuration, and the data-loss teeth were pulled by #496. ## Done-when - [ ] The behavior `MediaFileAlreadyExists`'s global dedup provides is enumerated before it is changed - [ ] Two libraries with overlapping local paths each resolve their own row (or their own membership) for a shared file - [ ] One library's sweep cannot flag/trash a file another library still serves - [ ] Behavior verified for at least movies and music videos, not just the kind that motivated it - [ ] Adversarial review passed
timothy added the bugpriority: low labels 2026-07-25 15:15:30 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: timothy/ersatztv#606