Skip to content

fix: prevent type definitions from being duplicated in optimized code#1221

Closed
misrasaurabh1 wants to merge 1 commit intomainfrom
fix-duplicate-type-definitions
Closed

fix: prevent type definitions from being duplicated in optimized code#1221
misrasaurabh1 wants to merge 1 commit intomainfrom
fix-duplicate-type-definitions

Conversation

@misrasaurabh1
Copy link
Contributor

Summary

  • Fix bug where type definitions (interfaces, types) were being duplicated in the optimized output
  • Type definitions that are imported in the original file were incorrectly being included in the code sent to the AI for optimization
  • When the AI returned the optimized code, it included these type definitions, causing duplicates

Root Cause

Type definitions extracted during context extraction were being combined with read_only_context, which gets prepended to the target code and becomes part of the "read-writable" code to be replaced. This caused the AI to see the type definitions as part of the code it should return, duplicating imported types.

Fix

  • Add type_definitions_context field to CodeContext to keep type definitions separate from global variables
  • Pass type definitions as read_only_context_code to the optimizer (true read-only context that is NOT part of the code to be replaced)
  • Global variables (which may need to be modified) remain in read_only_context and are prepended to target code

Test plan

  • Added unit tests that verify type definitions are in type_definitions_context, not read_only_context
  • Added integration test that verifies type definitions are passed as read_only_context_code to the optimizer
  • All 56 JavaScript support tests pass
  • All 81 code context extractor tests pass

Related

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

🤖 Generated with Claude Code

Type definitions (interfaces, types) that are found during context extraction
were being combined with read_only_context, which gets prepended to the target
code and becomes part of the code to be replaced. This caused the AI to include
these type definitions in the optimized output, duplicating imported types.

Fix:
- Add `type_definitions_context` field to CodeContext to keep type definitions
  separate from global variables
- Pass type definitions as `read_only_context_code` to the optimizer (true
  read-only context, not part of code to be replaced)
- Add tests to verify type definitions are properly separated

This fixes the bug where imported interfaces like `TreeNode` were being
duplicated in the optimization output even though they were already imported.

See: codeflash-ai/appsmith#20

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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