fix: prevent imported types from being duplicated during code replacement#1222
Merged
misrasaurabh1 merged 2 commits intomainfrom Jan 31, 2026
Merged
Conversation
…ment When the optimized code contains type definitions (interfaces, types) that are already imported in the original file, the code replacer was incorrectly adding them as new declarations because it only checked for existing declarations, not imports. Fix: In `_add_global_declarations_for_language`, also check for imported names (default imports, named imports, namespace imports) and exclude them from being added as new declarations. This fixes the bug where imported interfaces like `TreeNode` were being duplicated in the output even though they were already imported. See: codeflash-ai/appsmith#20 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
… to source code The root cause of the duplicate type definitions bug was that code_context.read_only_context (containing type definitions) was being prepended to target_file_code, making it part of the read-writable code sent to the AI. When the AI receives: - source_code = code with type definitions prepended - dependency_code = "" (empty) The AI treats the type definitions as code to optimize and naturally includes them in its output. Fix: Pass read_only_context as read_only_context_code (dependency_code) instead of prepending it to the source code. This way: - source_code = only the function to optimize - dependency_code = type definitions (AI sees as context, won't include in output) Combined with the previous code_replacer fix (checking for imported names), this provides defense-in-depth against duplicate type definitions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix bug where type definitions (interfaces, types) were being duplicated in the optimized output.
Root Cause Analysis
The real issue: In
get_code_optimization_context_for_language,code_context.read_only_context(containing type definitions) was being prepended totarget_file_code, making it part of the read-writable code sent to the AI.When the AI receives:
source_code= code with type definitions prepended (AI thinks this is code to optimize)dependency_code=""(empty - no read-only context!)The AI naturally includes the type definitions in its output because they're part of the input code.
Fixes (2 commits)
1. Context Extractor Fix (root cause)
Pass
read_only_contextasread_only_context_code(dependency_code) instead of prepending it to the source code:source_code= only the function to optimizedependency_code= type definitions (AI sees as context, won't include in output)2. Code Replacer Fix (defense-in-depth)
Also check for imported names when adding new declarations from optimized code:
Verification
Before fix:
After fix:
Test plan
test_imported_interface_not_added_as_declaration- verifies imported types are not duplicated in code replacertest_multiple_imported_types_not_duplicated- verifies multiple imported types are not duplicatedRelated
Fixes the bug in codeflash-ai/appsmith#20 where
TreeNodeinterface was duplicated.🤖 Generated with Claude Code