Files
silo-server/internal/scanner/root_observation.go
75b476d124 fix(naming,metadata): anchor identity on provider IDs, drop bare-numeric IDs, back off match queue retries (#112)
* fix(naming,metadata): anchor identity on provider IDs, drop bare-numeric IDs, back off match queue retries

Three scanner/matching fixes validated against dev data:

- Group identity: explicit structured provider tags ({tmdb-...},
  [tvdbid-...]) now anchor a group's identity, so folder/file title
  conflicts (renamed releases in Radarr-tagged folders) no longer mark
  groups ambiguous and silently exclude them from matching. 3,049 of
  3,121 ambiguous groups on dev carried explicit tags.

- ParseFolderIDs: remove bare trailing numeric ID parsing entirely,
  mirroring Jellyfin's path-attribute model (bracketed key tags plus
  unambiguous tt-prefixed IMDb ids only). Titles ending in numbers
  ("District 9", "Beverly Hills 90210", "Season 01") were misparsed as
  trusted IDs, which suppresses title search and silently mismatches.
  The folderType parameter existed only to type bare numerics, so it
  is gone too.

- Match queues: replace the constant 15s/30s retry delay with shared
  exponential backoff capped at 24h. Terminal failures ("no metadata
  found from any provider") had rows at 15k+ attempts hot-looping
  every 15s on dev.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(naming): merge trailing bare IMDb id with structured folder tags

ParseFolderIDs returned early on any structured tag, so a folder like
"Show [tvdbid-81189] tt1375666" lost the trailing IMDb id. Parse both and
merge, with an explicit structured imdb tag still taking precedence over a
trailing bare id. Matches Jellyfin, which resolves each provider key
independently.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-09 16:38:20 -04:00

102 lines
3.0 KiB
Go

package scanner
import (
"path/filepath"
"github.com/Silo-Server/silo-server/internal/models"
"github.com/Silo-Server/silo-server/internal/naming"
)
const (
RootObservationReasonMatchable = "matchable"
RootObservationReasonMissingFolderIDs = "missing_folder_ids"
)
// RootObservation summarizes one scanned content root and whether it is
// eligible for scanner-driven matching.
type RootObservation struct {
RootPath string
SampleFilePath string
FileCount int
HasFolderIDs bool
Reason string
}
type fileRootAssignment = naming.RootAssignment
type rootInferenceResult struct {
Observations []RootObservation
Snapshots []models.ScannedMediaRoot
Assignments map[string]fileRootAssignment
}
// ObserveRoot derives the logical content root for a media file path.
func ObserveRoot(filePath string, libraryType string) (RootObservation, bool) {
result := inferRootAssignments([]string{filePath}, libraryType, 0, nil)
assignment, ok := result.Assignments[filepath.Clean(filePath)]
if !ok {
return RootObservation{}, false
}
return observationFromAssignment(assignment), true
}
func collectRootObservations(filePaths []string, libraryType string) []RootObservation {
return inferRootAssignments(filePaths, libraryType, 0, nil).Observations
}
func collectScannedRoots(
filePaths []string,
libraryType string,
folderID int,
overrides map[string]models.MediaRootOverride,
) []models.ScannedMediaRoot {
return inferRootAssignments(filePaths, libraryType, folderID, overrides).Snapshots
}
func inferRootAssignments(
filePaths []string,
libraryType string,
folderID int,
overrides map[string]models.MediaRootOverride,
) rootInferenceResult {
snapshots, assignments := naming.InferRootAssignments(filePaths, libraryType, folderID, overrides)
observations := make([]RootObservation, 0, len(snapshots))
for _, snapshot := range snapshots {
observations = append(observations, observationFromSnapshot(snapshot))
}
return rootInferenceResult{
Observations: observations,
Snapshots: snapshots,
Assignments: assignments,
}
}
func observationFromAssignment(assignment fileRootAssignment) RootObservation {
observation := RootObservation{
RootPath: assignment.RootPath,
SampleFilePath: assignment.FilePath,
FileCount: 1,
HasFolderIDs: assignment.HasFolderIDs,
Reason: RootObservationReasonMissingFolderIDs,
}
if assignment.HasFolderIDs {
observation.Reason = RootObservationReasonMatchable
}
return observation
}
func observationFromSnapshot(snapshot models.ScannedMediaRoot) RootObservation {
hasFolderIDs := naming.ParseFolderIDs(filepath.Base(snapshot.RootPath)) != nil
observation := RootObservation{
RootPath: snapshot.RootPath,
SampleFilePath: snapshot.SampleFilePath,
FileCount: snapshot.ObservedFileCount,
HasFolderIDs: hasFolderIDs,
Reason: RootObservationReasonMissingFolderIDs,
}
if hasFolderIDs {
observation.Reason = RootObservationReasonMatchable
}
return observation
}