When multiple ANN profiles have the same symbol distance, the profile
selection would pick the first one encountered rather than the newest.
This caused issues when a newly trained ANN (version 1) existed alongside
the initial profile (version 0) - the scanner would select version 0
which had no actual ANN data.
Fix by adding a secondary selection criterion: when distances are equal,
prefer the profile with the higher version number.
local dist = lua_util.distance_sorted(elt.symbols, my_symbols)
-- Check distance
if dist < #my_symbols * .3 then
- if dist < min_diff then
+ -- Prefer profiles with smaller distance, or higher version when distance is equal
+ if dist < min_diff or (dist == min_diff and sel_elt and elt.version > sel_elt.version) then
min_diff = dist
sel_elt = elt
end