fix: add pre-flight module import check to exit early before API calls#1745
fix: add pre-flight module import check to exit early before API calls#1745aseembits93 wants to merge 10 commits intomainfrom
Conversation
When module-root is misconfigured or dependencies are missing, the optimization pipeline previously made expensive API calls (test generation + optimization) before discovering the failure at baseline test execution. This adds a subprocess-based import check in can_be_optimized() that runs before any API calls. If the import fails, it attempts to auto-correct module-root by inferring it from the __init__.py chain and updating pyproject.toml. If auto-correction isn't possible (e.g. missing dependency), it fails early with a clear error message. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @aseembits93's task in 5m 18s —— View job PR Review Summary
Prek ChecksAll ruff and format checks pass after fixing the unsorted-import error in Code ReviewBugs / Issues found:
Looks good:
Duplicate DetectionNo duplicates detected. The Test CoverageAll 21 tests in Last updated: 2026-03-04 | | Branch |
|
a full subprocess call is far more expensive than how we check if something is installed in the comparator: you can use that for this |
The function should return the topmost directory with __init__.py (the top-level package), not the first directory without it. This matches the convention where module-root = package directory and project_root = its parent (the PYTHONPATH entry). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
this pr is still in progress |
The pre-flight check only tried to correct module-root when the import failed, but imports can succeed even with a misconfigured module-root, causing downstream test result parsing failures after expensive API calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cover validate_module_import, infer_module_root_from_file, and FunctionOptimizer.try_correct_module_root with 21 tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Tests were patching functions at their definition modules (e.g. importlib.util.find_spec) instead of where they are imported and used (e.g. codeflash.code_utils.code_utils.find_spec). With `from X import Y`, the local binding is unaffected by patching X.Y. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@claude is the windows unittest failure related to this pull request? |
|
Claude finished @aseembits93's task in 48s —— View job Windows unittest failure analysis
No, the Windows unittest failure is not related to this PR. The failing test is: The error is a Windows path separator issue in the Jest reporter module path: The path Evidence that this PR is not the cause:
The Windows Jest reporter path bug is a separate pre-existing issue that would need to be fixed in the JavaScript test runner code. |
|
@claude fix the failing |
|
Claude finished @aseembits93's task in 1m 12s —— View job Fix: Windows path separator issue in Jest reporter test
Root CauseIn # Before (broken on Windows):
process.env.JEST_JUNIT_OUTPUT_FILE = '{output_file}'; # D:\a\... → D:a... (backslash escapes mangled)
const Reporter = require('{reporter_path}');Fix (commit
|
…plate Windows Path backslashes become JS escape sequences when interpolated directly into the script string, mangling the path. Co-authored-by: Aseem Saxena <aseembits93@users.noreply.github.com>
| f"Cannot optimize without tests when --no-gen-tests is set." | ||
| ) | ||
|
|
||
| # Pre-flight: verify module-root consistency and importability before expensive API calls |
There was a problem hiding this comment.
instead of implementing it in the function optimizer here, can you find a good spot for it in https://github.com/codeflash-ai/codeflash/blob/main/codeflash/languages/python/function_optimizer.py?
There was a problem hiding this comment.
if pre-flight doesn't exist in the language base add it
…ythonFunctionOptimizer The try_correct_module_root() method and the Python import validation guard in can_be_optimized() used Python-only concepts (__init__.py chain walking, pyproject.toml updates). Moving them to PythonFunctionOptimizer keeps the base FunctionOptimizer language-agnostic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…rdering Co-authored-by: Aseem Saxena <aseembits93@users.noreply.github.com>
Summary
can_be_optimized()that runs before any expensive API calls (test generation + optimization)module-root, automatically infers the correct value from the__init__.pychain, updatespyproject.toml, and retriesTest plan
module-rootto an incorrect value inpyproject.tomland runcodeflash --file <file> --function <func>— verify it auto-corrects and updatespyproject.tomlmodule-rootand verify no regression — optimization proceeds normally🤖 Generated with Claude Code