Skip to content

fix(search): sort wanted list ascending so backlog isn't starved - #380

Open
lolimmlost wants to merge 1 commit into
ManiMatter:devfrom
lolimmlost:fix/search-missing-sort-direction
Open

fix(search): sort wanted list ascending so backlog isn't starved#380
lolimmlost wants to merge 1 commit into
ManiMatter:devfrom
lolimmlost:fix/search-missing-sort-direction

Conversation

@lolimmlost

Copy link
Copy Markdown
Collaborator

Summary

Fixes #376.

search_missing (and search_unmet_cutoff) could permanently re-search the same top MAX_CONCURRENT_SEARCHES items while items that had never been searched were never reached — logging a normal success line every cycle, so it looked healthy.

Root cause

src/utils/wanted_manager.py::_get_arr_records sent sortKey but no sortDirection:

params = {"page": "1", "pageSize": total_records_count, "sortKey": sort_key}

For lastSearchTime the *arr API defaults to descending, so the list comes back most-recently-searched first and never-searched items (lastSearchTime: null) last. search_handler.py::_filter_wanted_items then slices from the top (items[:max_concurrent_searches]), so it re-selects the items it just searched. With min_days_between_searches=0 the recent-search guard (_filter_recent_searches) never evicts anything either, turning this into total starvation of the backlog.

Confirmed against a live Sonarr in the issue thread: same endpoint and sortKey, only sortDirection varies — ascending yields the never-searched-first order the code intends.

Fix

Request the direction explicitly:

params = {
    "page": "1",
    "pageSize": total_records_count,
    "sortKey": sort_key,
    "sortDirection": "ascending",
}

Least-/never-searched items lead, which is what the job intends, and it is correct at any min_days_between_searches including 0.

Tests

Adds tests/utils/test_wanted_manager.py: a regression test asserting sortDirection=ascending in the request params, plus the empty-records early-return path. Full suite passes locally (pytest).

Credit to the original reporter for the diagnosis and live-Sonarr verification in #376.

🤖 Generated with Claude Code

_get_arr_records sent sortKey without sortDirection. The *arr API
defaults lastSearchTime to descending, returning most-recently-searched
items first and never-searched items (null lastSearchTime) last.
_filter_wanted_items slices from the top, so it re-searched the same top
N every cycle while never-searched items were never reached. With
min_days_between_searches=0 the recent-search guard never evicts
anything either, making it total starvation.

Request sortDirection=ascending so least-/never-searched items lead,
which is what the job intends. Correct at any min_days_between_searches,
including 0.

Fixes ManiMatter#376

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014qSXBcvcc8YbZKsM9RhcYo
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.

search_missing re-searches the same N items forever: wanted list is sorted DESCENDING by lastSearchTime (missing sortDirection)

1 participant