@@ -660,6 +660,19 @@ def _add_global_declarations_for_language(
660660 # Get names of existing declarations
661661 existing_names = {decl .name for decl in original_declarations }
662662
663+ # Also exclude names that are already imported (to avoid duplicating imported types)
664+ original_imports = analyzer .find_imports (original_source )
665+ for imp in original_imports :
666+ # Add default import name
667+ if imp .default_import :
668+ existing_names .add (imp .default_import )
669+ # Add named imports (use alias if present, otherwise use original name)
670+ for name , alias in imp .named_imports :
671+ existing_names .add (alias if alias else name )
672+ # Add namespace import
673+ if imp .namespace_import :
674+ existing_names .add (imp .namespace_import )
675+
663676 # Find new declarations (names that don't exist in original)
664677 new_declarations = []
665678 seen_sources = set () # Track to avoid duplicates from destructuring
@@ -725,7 +738,8 @@ def _find_insertion_line_after_imports_js(lines: list[str], analyzer: TreeSitter
725738
726739def get_optimized_code_for_module (relative_path : Path , optimized_code : CodeStringsMarkdown ) -> str :
727740 file_to_code_context = optimized_code .file_to_path ()
728- module_optimized_code = file_to_code_context .get (str (relative_path ))
741+ relative_path_str = str (relative_path )
742+ module_optimized_code = file_to_code_context .get (relative_path_str )
729743 if module_optimized_code is None :
730744 # Fallback: if there's only one code block with None file path,
731745 # use it regardless of the expected path (the AI server doesn't always include file paths)
@@ -738,10 +752,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
738752 # the full path like "src/main/java/com/example/Algorithms.java")
739753 target_filename = relative_path .name
740754 for file_path_str , code in file_to_code_context .items ():
741- if file_path_str and Path (file_path_str ).name == target_filename :
742- module_optimized_code = code
743- logger .debug (f"Matched { file_path_str } to { relative_path } by filename" )
744- break
755+ if file_path_str :
756+ # Extract filename without creating Path object repeatedly
757+ if file_path_str .endswith (target_filename ) and (len (file_path_str ) == len (target_filename ) or file_path_str [- len (target_filename )- 1 ] in ('/' , '\\ ' )):
758+ module_optimized_code = code
759+ logger .debug (f"Matched { file_path_str } to { relative_path } by filename" )
760+ break
761+
745762
746763 if module_optimized_code is None :
747764 # Also try matching if there's only one code file
@@ -750,11 +767,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
750767 module_optimized_code = file_to_code_context [only_key ]
751768 logger .debug (f"Using only code block { only_key } for { relative_path } " )
752769 else :
753- logger .warning (
754- f"Optimized code not found for { relative_path } In the context\n -------\n { optimized_code } \n -------\n "
755- "re-check your 'markdown code structure'"
756- f"existing files are { file_to_code_context .keys ()} "
757- )
770+ # Delay expensive string formatting until actually logging
771+ if logger .isEnabledFor (logger .level ):
772+ logger .warning (
773+ f"Optimized code not found for { relative_path } In the context\n -------\n { optimized_code } \n -------\n "
774+ "re-check your 'markdown code structure'"
775+ f"existing files are { file_to_code_context .keys ()} "
776+ )
758777 module_optimized_code = ""
759778 return module_optimized_code
760779
0 commit comments