@@ -397,6 +397,128 @@ def test_version_diff_preserves_numeric_meaning(old, new):
397397 assert "1 passages changed" in built .notice
398398
399399
400+ def _compare_text_versions (old : str , new : str , window_size : int | None = None ):
401+ entries = [{"id" : name , "metadata" : {}, "chunks" : 1 } for name in ("old.txt" , "new.txt" )]
402+ chunks = [
403+ _windows (text , window_size ) if window_size else [{"text" : text , "metadata" : {}}]
404+ for text in (old , new )
405+ ]
406+ return docmode .compare_context (entries , chunks , fake_embedding ("q" ), budget = 24_000 )
407+
408+
409+ @pytest .mark .parametrize (
410+ "old,new" ,
411+ [
412+ ("Rate: 10%" , "Rate: 20%" ),
413+ ("100" , "200" ),
414+ ("100" , "1100" ),
415+ ("100" , "-100" ),
416+ ("+100" , "-100" ),
417+ ("1.5" , "15" ),
418+ ("15%" , "15" ),
419+ ("01.02.2026" , "02.02.2026" ),
420+ ("Да" , "Нет" ),
421+ ("On" , "Off" ),
422+ ],
423+ )
424+ @pytest .mark .parametrize ("window_size" , [None , 160 , 240 ])
425+ def test_version_diff_reports_short_value_changes (old , new , window_size ):
426+ before = "\n " .join (SHARED [:3 ]) + "\n "
427+ after = "\n " + "\n " .join (SHARED [3 :])
428+ built = _compare_text_versions (before + old + after , before + new + after , window_size )
429+ blocks = built .context [1 :]
430+ assert "share most of their text" in built .context [0 ]
431+ assert any (f"old.txt: { old } " in b or f"[only in old.txt] { old } " in b for b in blocks )
432+ assert any (f"new.txt: { new } " in b or f"[only in new.txt] { new } " in b for b in blocks )
433+ assert "No differences were found" not in built .context [0 ]
434+ assert not any ("Unchanged provision" in b for b in blocks )
435+
436+
437+ @pytest .mark .parametrize ("value" , ["100" , "Да" , "Rate: 10%" ])
438+ @pytest .mark .parametrize ("added" , [False , True ])
439+ def test_version_diff_reports_short_line_additions_and_removals (value , added ):
440+ unchanged = "\n " .join (SHARED )
441+ changed = unchanged + "\n " + value
442+ old , new = (unchanged , changed ) if added else (changed , unchanged )
443+ built = _compare_text_versions (old , new )
444+ name = "new.txt" if added else "old.txt"
445+ assert built .context [1 :] == [f"[only in { name } ] { value } " ]
446+
447+
448+ @pytest .mark .parametrize (
449+ "old,new" ,
450+ [
451+ ("Rate: 10%" , " Rate : 10 % " ),
452+ ("Net: -100" , "Net: − 100" ),
453+ ("Да" , " ДА! " ),
454+ ("Café" , "Cafe\u0301 " ),
455+ ("office" , "of\u00ad fice" ),
456+ ("Rate: 10%" , "Rate:\n 10%" ),
457+ ("Rate:\n 10%" , "Rate: 10%" ),
458+ ("The status is Active" , "The status is\n Active" ),
459+ ("The status is\n Active" , "The status is Active" ),
460+ ("" , "\n ---\n ...\n " ),
461+ ],
462+ )
463+ @pytest .mark .parametrize ("window_size" , [None , 160 , 240 ])
464+ def test_version_diff_ignores_formatting_changes_around_short_lines (old , new , window_size ):
465+ before = "\n " .join (SHARED [:3 ]) + "\n "
466+ after = "\n " + "\n " .join (SHARED [3 :])
467+ built = _compare_text_versions (before + old + after , before + new + after , window_size )
468+ assert "share most of their text" in built .context [0 ]
469+ assert built .context [1 :] == []
470+ assert "0 passages changed, 0 only in new.txt, 0 only in old.txt" in built .notice
471+
472+
473+ def test_short_common_values_do_not_make_unrelated_documents_versions ():
474+ common = "\n " .join (f"Code { i } " for i in range (30 ))
475+ old = common + "\n " + "\n " .join (SHARED )
476+ new = common + "\n " + "\n " .join (
477+ f"Chapter { i } . Instructions for preparing a vegetable soup." for i in range (6 )
478+ )
479+ built = _compare_text_versions (old , new )
480+ assert built .system_prompt == docmode .EXCERPTS_SYSTEM_PROMPT
481+ assert "share little or no identical text" in built .context [0 ]
482+
483+
484+ @pytest .mark .parametrize (
485+ "old,new,other" ,
486+ [("100" , "200" , "1100" ), ("On" , "Off" , "authentication." )],
487+ )
488+ def test_short_chunks_are_not_confused_with_other_value_suffixes (old , new , other ):
489+ entries = [{"id" : name , "metadata" : {}, "chunks" : 8 } for name in ("old.txt" , "new.txt" )]
490+ common = [{"text" : line , "metadata" : {}} for line in SHARED ]
491+ common .append ({"text" : "A separate unchanged field contains " + other , "metadata" : {}})
492+ chunks = [[* common , {"text" : value , "metadata" : {}}] for value in (old , new )]
493+ built = docmode .compare_context (entries , chunks , fake_embedding ("q" ), budget = 24_000 )
494+ assert f"[only in old.txt] { old } " in built .context [1 :]
495+ assert f"[only in new.txt] { new } " in built .context [1 :]
496+
497+
498+ def test_rag_compares_short_values_from_named_documents (tmp_path , mock_server_embedder ):
499+ path = tmp_path / "versions.duckdb"
500+ write_duckdb_rows (path , [
501+ {
502+ "id" : name ,
503+ "text" : "\n " .join ([* SHARED , rate ]),
504+ "metadata" : {"path" : name },
505+ "embedding" : fake_embedding ("rate" ),
506+ }
507+ for name , rate in (("old.txt" , "Rate: 10%" ), ("new.txt" , "Rate: 20%" ))
508+ ])
509+ llm = _ScriptedLLM ()
510+ body = _ask (path , mock_server_embedder , llm , "Compare old.txt and new.txt" )
511+ assert body ["mode" ] == "compare"
512+ assert "1 passages changed" in body ["notice" ]
513+ assert any (
514+ "Rate: 10%" in block and "Rate: 20%" in block
515+ for block in llm .calls [0 ]["context" ][1 :]
516+ )
517+ assert {source ["metadata" ]["path" ] for source in body ["sources" ]} == {
518+ "old.txt" , "new.txt"
519+ }
520+
521+
400522@pytest .mark .parametrize ("old,new" , [
401523 ("Balance is -100 dollars." , "Balance is − 100 dollars." ),
402524 ("Balance is -100 dollars." , "Balance is –100 dollars." ),
0 commit comments