-
Notifications
You must be signed in to change notification settings - Fork 22
Formatting should only target changes suggested by optimizer, not entire files (CF-637) #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
PR Reviewer Guide 🔍(Review updated until commit 85fd3c0)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to 85fd3c0
Previous suggestionsSuggestions up to commit 3cbd6b7
|
… sections of the file... This is to test the new formatting changes
To test that the new formatting logic correctly handles indentation
|
I have tested this code using two optimization targets:
Both of these are bubble sort implementations but they contain valid, poorly formatted Python code. The new formatting logic in the For python -m codeflash.main \
--file code_to_optimize/bubble_sort_preserve_bad_formatting_for_nonoptimized_code.py \
--module-root ./ \
--function sorter \
--test-framework pytest \
--tests-root code_to_optimize/tests/pytest \
--no-prFor python -m codeflash.main \
--file code_to_optimize/bubble_sort_method_preserve_bad_formatting_for_nonoptimized_code.py \
--module-root ./ \
--function BubbleSorter.sorter \
--test-framework pytest \
--tests-root code_to_optimize/tests/pytest \
--no-prAfter running either/both of these commands, run |
|
Persistent review updated to latest commit 85fd3c0 |
| tmp_file.write(candidate.source_code) | ||
| temp_code_file_path = Path(tmp_file.name) | ||
|
|
||
| formatted_candidate_code = format_code( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting code is depenedent on the cwd of the code, imports are grouped according to what module they belong to, for the project's own module they are grouped together. This determination of what is the module they belong to is determined by the cwd.
So we should not format code in a temp directory, the results may not be the same. This is btw why your unit tests were failing today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's true, as this is working.
This is only formatting the function snippet and the helper snippets -- I don't think this has to do with why those unit tests are failing.
|
Decided to use a gentler approach using LibCST. Will open separate PR. |
User description
Closes CF-637
This PR implements targeted formatting for AI-generated code snippets within the
FunctionOptimizer. The goal is to format only the specific function being optimized (and any helpers modified by the AI) rather than entire files with tools like Black/Ruff. Import sorting (isort) is still applied at a file level for correctness after the formatted snippet is integrated.The existing
codeflash/code_utils/formatter.pyremains unchanged. All new temporary file management for string formatting is handled withinFunctionOptimizer.Key Changes Implemented:
FunctionOptimizer.__init__Modifications:tempfile(shutil and Path were already present).self.optimizer_temp_dir = Path(tempfile.mkdtemp(prefix="codeflash_opt_fmt_")). This directory is not cleaned up byFunctionOptimizeritself, allowing it to persist for potential inspection or higher-level cleanup.FunctionOptimizer.determine_best_candidateMethod Modifications:candidate.source_code(a string) needs formatting (i.e.,self.args.formatter_cmdsis configured):self.optimizer_temp_dir.candidate.source_codeis written to this temp file.format_code()is called with the temp file's path. (format_code()handles the "disabled" formatter case internally).finallyblock.candidate.source_codeis updated with the formatted snippet (output of primary formatter like Black/Ruff).FunctionOptimizer.optimize_functionMethod Modifications:reformat_code_and_helpersmethod and its call site are removed.replace_function_and_helpers_with_optimized_code(which splices the formatted snippet into files):not self.args.disable_imports_sorting,sort_imports()is applied to the full content of the main function's file.sort_imports()is applied to the full content of any helper files (fromcode_context.helper_functions) that could have been modified.new_code(for the main file) andnew_helper_code(for helper files, used in PR generation) are populated by reading the respective file contents after the above snippet replacement and potential file-level import sorting.Note on Error Handling:
If
format_code()encounters an issue while formatting the temporary file (e.g., formatter command not found, or the formatter errors out), it currently logs an error and returns the content of the temporary file (which might be unchanged or malformed).determine_best_candidateproceeds with this returned content. For this PR, we accept this behavior. Future improvements could involve more explicit error signaling or fallback mechanisms if desired.PR Type
Enhancement, Tests
Description
Implement targeted formatting for optimized snippets
Apply import sorting after optimization
Support disabling telemetry via environment variable
Add bubble sort sample files preserving bad formatting
Changes walkthrough 📝
bubble_sort_method_preserve_bad_formatting_for_nonoptimized_code.py
Add bubble sort class samplecode_to_optimize/bubble_sort_method_preserve_bad_formatting_for_nonoptimized_code.py
lolfunction andBubbleSorter.sorterbubble_sort_preserve_bad_formatting_for_nonoptimized_code.py
Add bubble sort function samplecode_to_optimize/bubble_sort_preserve_bad_formatting_for_nonoptimized_code.py
loland standalonesorterfunctionmain.py
Support disabling telemetry by env varcodeflash/main.py
osfor environment variable accessCODEFLASH_DISABLE_TELEMETRYin CLI flowfunction_optimizer.py
Implement targeted formatting and import sortingcodeflash/optimization/function_optimizer.py
optimizer_temp_dirfor formatting temp filesreformat_code_and_helpersmethod