@@ -525,6 +525,67 @@ def mock_search(self, query):
525525 self .assertIn (0 , results [0 ].query_scores )
526526 self .assertIn (1 , results [0 ].query_scores )
527527
528+ def test_same_document_merges_tags_deterministically (self ):
529+ """Duplicate hits should merge tag sets in stable first-seen order."""
530+ backend = StubBackend ()
531+ storage = StubStorage ()
532+
533+ results_list = [
534+ [
535+ type ('HybridResult' , (), {
536+ 'filepath' : 'shared.md' ,
537+ 'display_path' : 'shared.md' ,
538+ 'title' : 'shared.md' ,
539+ 'context' : None ,
540+ 'hash' : 'h1' ,
541+ 'docid' : 'd1' ,
542+ 'collection' : 'test' ,
543+ 'modified_at' : '2026-01-01' ,
544+ 'body_length' : 100 ,
545+ 'body' : 'shared content' ,
546+ 'score' : 0.8 ,
547+ 'source' : 'hybrid' ,
548+ 'tags' : ['alpha' , 'shared' ],
549+ }),
550+ ],
551+ [
552+ type ('HybridResult' , (), {
553+ 'filepath' : 'shared.md' ,
554+ 'display_path' : 'shared.md' ,
555+ 'title' : 'shared.md' ,
556+ 'context' : None ,
557+ 'hash' : 'h1' ,
558+ 'docid' : 'd1' ,
559+ 'collection' : 'test' ,
560+ 'modified_at' : '2026-01-01' ,
561+ 'body_length' : 100 ,
562+ 'body' : 'shared content' ,
563+ 'score' : 0.9 ,
564+ 'source' : 'hybrid' ,
565+ 'tags' : ['shared' , 'beta' ],
566+ }),
567+ ],
568+ ]
569+
570+ call_idx = [0 ]
571+
572+ def mock_search (self , query ):
573+ idx = call_idx [0 ]
574+ call_idx [0 ] += 1
575+ return results_list [idx ]
576+
577+ with patch .object (HybridSearcher , '__init__' , lambda self , ** kwargs : None ):
578+ with patch .object (HybridSearcher , 'search' , mock_search ):
579+ results = search_batch (
580+ ["query one" , "query two" ],
581+ backend = backend ,
582+ storage = storage ,
583+ limit = 10 ,
584+ )
585+
586+ self .assertEqual (len (results ), 1 )
587+ self .assertEqual (results [0 ].tags , ["alpha" , "shared" , "beta" ])
588+
528589
529590if __name__ == "__main__" :
530- unittest .main ()
591+ unittest .main ()
0 commit comments