File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed
Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -42,8 +42,9 @@ def get_preview(
4242 return self .chunk_content [:max_chars ]
4343
4444 # Sort by start_offset to maintain document order
45- top_sentences .sort (
46- key = lambda s : s .start_offset if s .start_offset is not None else - 1
45+ top_sentences = sorted (
46+ top_sentences ,
47+ key = lambda s : s .start_offset if s .start_offset is not None else - 1 ,
4748 )
4849
4950 preview_parts = []
Original file line number Diff line number Diff line change @@ -177,6 +177,47 @@ def test_get_preview_orders_sentences_by_offset(self):
177177 # Should be in document order despite rank order
178178 assert "First sentence. [...] Third sentence." == preview
179179
180+ def test_get_preview_orders_sentences_stress_test (self ):
181+ """Stress test to detect race conditions in sentence ordering."""
182+ import gc
183+
184+ for _ in range (100 ):
185+ doc = Document (uri = "test.txt" , content = "test content" )
186+
187+ sentences = [
188+ SentenceResult (
189+ chunk_id = 1 ,
190+ id = 3 ,
191+ content = "Third sentence." ,
192+ rank = 1 ,
193+ distance = 0.1 ,
194+ start_offset = 66 ,
195+ end_offset = 82 ,
196+ ),
197+ SentenceResult (
198+ chunk_id = 1 ,
199+ id = 1 ,
200+ content = "First sentence." ,
201+ rank = 2 ,
202+ distance = 0.2 ,
203+ start_offset = 0 ,
204+ end_offset = 15 ,
205+ ),
206+ ]
207+
208+ result = DocumentResult (
209+ document = doc ,
210+ chunk_id = 1 ,
211+ combined_rank = 1.0 ,
212+ sentences = sentences ,
213+ )
214+
215+ preview = result .get_preview (max_chars = 400 )
216+ assert "First sentence. [...] Third sentence." == preview
217+
218+ # Force garbage collection to expose potential memory issues
219+ gc .collect ()
220+
180221 def test_get_preview_limits_to_top_k_sentences (self ):
181222 """Test get_preview respects top_k_sentences parameter."""
182223 doc = Document (uri = "test.txt" , content = "test content" )
You can’t perform that action at this time.
0 commit comments