@@ -198,6 +198,7 @@ def build_diff_context(
198198 full : bool = False ,
199199) -> dict [str , Any ]:
200200 _validate_inputs (root_dir , alpha , tau , budget_tokens )
201+ root_dir = root_dir .resolve ()
201202
202203 hunks = parse_diff (root_dir , diff_range )
203204 if not hunks :
@@ -265,6 +266,9 @@ def _validate_inputs(root_dir: Path, alpha: float, tau: float, budget_tokens: in
265266 raise ValueError (f"budget_tokens must be > 0, got { budget_tokens } " )
266267
267268
269+ _CONTAINER_FRAGMENT_KINDS = frozenset ({"class" , "interface" , "struct" })
270+
271+
268272def _identify_core_fragments (hunks : list [DiffHunk ], all_fragments : list [Fragment ]) -> set [FragmentId ]:
269273 frags_by_path : dict [Path , list [Fragment ]] = defaultdict (list )
270274 for frag in all_fragments :
@@ -276,9 +280,25 @@ def _identify_core_fragments(hunks: list[DiffHunk], all_fragments: list[Fragment
276280 if frags :
277281 h_start , h_end = h .core_selection_range
278282 core_ids .update (_find_core_for_hunk (frags , h_start , h_end ))
283+
284+ _add_container_headers (core_ids , frags_by_path )
279285 return core_ids
280286
281287
288+ def _add_container_headers (core_ids : set [FragmentId ], frags_by_path : dict [Path , list [Fragment ]]) -> None :
289+ core_paths = {fid .path for fid in core_ids }
290+ headers_to_add : list [FragmentId ] = []
291+ for path in core_paths :
292+ for frag in frags_by_path .get (path , []):
293+ if frag .kind not in _CONTAINER_FRAGMENT_KINDS or frag .id in core_ids :
294+ continue
295+ for core_id in core_ids :
296+ if core_id .path == path and core_id .start_line > frag .end_line :
297+ headers_to_add .append (frag .id )
298+ break
299+ core_ids .update (headers_to_add )
300+
301+
282302def _log_full_mode (selected : list [Fragment ]) -> None :
283303 try :
284304 used = sum (f .token_count for f in selected )
@@ -409,21 +429,6 @@ def _collect_expansion_files(
409429 return list (expansion_files )
410430
411431
412- def _expand_universe_by_rare_identifiers (
413- root_dir : Path ,
414- concepts : frozenset [str ],
415- already_included : list [Path ],
416- combined_spec : pathspec .PathSpec ,
417- ) -> list [Path ]:
418- if not concepts :
419- return []
420-
421- included_set = set (already_included )
422- files = _collect_candidate_files (root_dir , included_set , combined_spec )
423- inverted_index = _build_ident_index (files , concepts )
424- return _collect_expansion_files (inverted_index , concepts , included_set )
425-
426-
427432def _filter_ignored (
428433 files : list [Path ],
429434 root_dir : Path ,
@@ -436,10 +441,25 @@ def _filter_ignored(
436441 if not should_ignore (rel_path , combined_spec ):
437442 result .append (file_path )
438443 except ValueError :
439- result . append ( file_path )
444+ pass
440445 return result
441446
442447
448+ def _expand_universe_by_rare_identifiers (
449+ root_dir : Path ,
450+ concepts : frozenset [str ],
451+ already_included : list [Path ],
452+ combined_spec : pathspec .PathSpec ,
453+ ) -> list [Path ]:
454+ if not concepts :
455+ return []
456+
457+ included_set = set (already_included )
458+ files = _collect_candidate_files (root_dir , included_set , combined_spec )
459+ inverted_index = _build_ident_index (files , concepts )
460+ return _collect_expansion_files (inverted_index , concepts , included_set )
461+
462+
443463def _empty_tree (root_dir : Path ) -> dict [str , Any ]:
444464 return {
445465 "name" : root_dir .name ,
0 commit comments