Refactor Code Tool Bug Fix Request
Background
I've recently completed testing of our new refactor code tool for TypeScript projects. This tool uses AST-based analysis to perform safe refactoring operations including:
- Rename - Rename symbols while properly updating all references
- Move - Relocate code between files with automatic import/export management
- Remove - Safely remove code elements with reference handling
- Batch operations - Execute multiple refactorings as a single transaction
The tool is implemented in:
src/core/tools/refactorCodeTool.ts - Main implementation
src/core/prompts/tools/refactor-code.ts - Prompt definition
src/core/tools/refactor-code/ - Supporting modules (engine, parser, schema, etc.)
Testing Completed
I've run the test plan from refactor-tool-test-plan.md which covered:
- Setting up a test TypeScript project with cross-referenced files
- Testing each operation type individually (rename, move, remove)
- Testing batch operations with multiple changes
The test plan included sample TypeScript files specifically structured to test various refactoring scenarios, and bug reports were filed in examples/bugreport.md
Fix Requirements
Please analyze the reported issues and implement fixes that:
- Address the root cause of each bug, not just the symptoms
- Maintain or improve code quality and test coverage
- Preserve the existing API and user experience where possible
- Add appropriate error handling for edge cases
- Include additional validation where needed
For each fix, please:
- Explain your reasoning and approach
- Update or add unit tests to verify the fix
- Consider performance implications
- Update documentation if the behavior changes
Technical Context
Our refactoring tool uses:
- ts-morph for TypeScript AST manipulation
- Zod for schema validation
- A transaction-based approach where all operations in a batch succeed or fail together
The current implementation follows a pipeline:
- Parse the JSON operations from the LLM response
- Validate operations against the schema
- Create snapshots of files before modification
- Execute operations in sequence
- Either commit or roll back the transaction based on success
Edge Cases to Consider
When fixing these issues, please pay special attention to:
- Symbol resolution - Handling overloaded functions, method overrides, and nested symbols
- Import management - Circular dependencies, relative path resolution, and import style consistency
- Batch operation atomicity - Ensuring proper rollback if any operation fails
- File system interactions - Race conditions, file locking, and error handling
- Parser robustness - Handling various LLM response formats and malformed JSON
Success Criteria
Your fixes will be considered successful if:
- All reported issues are resolved
- No regressions are introduced
- The tool passes the original test plan
- Code quality is maintained or improved
- Edge cases are properly handled
Thank you for your help improving our TypeScript refactoring tool!
Remember, don't add any special hacks in order to get the implementation to work with any specific test implementation. And the goal is to have a generic working implementation of the refactor tool that will work without adding other robust code paths or any kind of hacks. We want to be very generic and work in as many cases as possible. For any bugs you fix, try to add new unit tests so those bugs do not regress in the