Fix: break autocomplete frequency ties alphabetically for stable output

Equal-frequency words were non-deterministically ordered; sort key is
now (-count, word) so ties resolve alphabetically.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Trenton HandClaude Sonnet 4.6 committed 2026-03-30 14:13:10 -07:00
1 parent 897f7d2199
commit 8107b7d209
1 file changed
+3 -3
+3 -3
View File
@@ -538,11 +538,11 @@ class TantivyBackend:
if "autocomplete_word" in doc_dict:
word_counts.update(doc_dict["autocomplete_word"])
# Filter to prefix matches, then sort by document frequency descending
# so the most-used matching word comes first.
# Filter to prefix matches, sort by document frequency descending;
# break ties alphabetically for stable, deterministic output.
matches = sorted(
(w for w in word_counts if w.startswith(normalized_term)),
key=lambda w: -word_counts[w],
key=lambda w: (-word_counts[w], w),
)
return matches[:limit]