Skip to content

fix: prevent imported types from being duplicated during code replacement#1222

Merged
misrasaurabh1 merged 2 commits intomainfrom
fix-duplicate-type-definitions-in-replacer
Jan 31, 2026
Merged

fix: prevent imported types from being duplicated during code replacement#1222
misrasaurabh1 merged 2 commits intomainfrom
fix-duplicate-type-definitions-in-replacer

Conversation

@misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Jan 31, 2026

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 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 (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_context as read_only_context_code (dependency_code) instead of prepending it to the source code:

  • source_code = only the function to optimize
  • dependency_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:

  • If a type is already imported, don't add it as a new declaration

Verification

Before fix:

read_writable_code.markdown: contains "interface TreeNode" (BAD!)
read_only_context_code: "" (empty)

After fix:

read_writable_code.markdown: contains only the function (GOOD!)
read_only_context_code: contains "interface TreeNode" (GOOD!)

Test plan

  • Added test test_imported_interface_not_added_as_declaration - verifies imported types are not duplicated in code replacer
  • Added test test_multiple_imported_types_not_duplicated - verifies multiple imported types are not duplicated
  • All 54 JavaScript support tests pass
  • All 81 code context extractor tests pass
  • All 61 code replacer tests pass

Related

Fixes the bug in codeflash-ai/appsmith#20 where TreeNode interface was duplicated.

🤖 Generated with Claude Code

misrasaurabh1 and others added 2 commits January 31, 2026 07:50
…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>
@misrasaurabh1 misrasaurabh1 merged commit 51264a9 into main Jan 31, 2026
24 of 27 checks passed
@misrasaurabh1 misrasaurabh1 deleted the fix-duplicate-type-definitions-in-replacer branch January 31, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant