Skip to content

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Jan 19, 2026

No description provided.

KRRT7 and others added 15 commits January 18, 2026 21:19
When LLM-generated optimizations use module-level code that depends on
functions defined later in the original file (e.g., `_TABLE = func(...)`),
the assignments were being inserted after imports but before the function
definitions, causing NameError at import time.

This fix:
- Adds NameCollector visitor to extract names from assignment values
- Tracks positions of function/class definitions in the module
- Inserts each assignment after all its dependencies are defined
- Assignments without dependencies still go after imports

Fixes optimization failures for functions like `standardize_quotes` that
use helper functions like `unicode_to_char` defined later in the file.
…ization results

Adds a new `codeflash create-pr` subcommand that creates PRs from
previously applied optimizations stored in a JSON results file.
This enables a two-phase workflow where optimizations are applied
locally first, then PRs can be created separately.
…n add_global_assignments

Previously, GlobalStatementCollector only collected SimpleStatementLine nodes,
causing module-level for-loops (and other compound statements) to be dropped.
This caused NameError for loop variables like 'uval', 'unicode_val', 'ch' when
LLM-generated optimizations used for-loops to build translation tables.

- Add compound statement collection to GlobalStatementCollector
- Update ImportInserter to handle both simple and compound statements
- Add tests for all reported NameError cases
For-loops and other compound statements at module level may call functions
defined later in the file. Previously, they were inserted after imports,
causing NameError when the called function was defined after the for-loop.

Now:
- Simple statements (assignments) are inserted after imports
- Compound statements (for/while/with/try) are inserted at the END of the module

This fixes NameError cases like 'unicode_to_char is not defined' when
LLM-generated optimizations use module-level for-loops to build translation
tables that call helper functions.
When LLM-generated optimizations introduce new helper functions that are
called at module level, these functions must be transferred to the original
code. Added FunctionDefCollector, FunctionNameCollector, and FunctionDefInserter
to detect and transfer new function definitions before assignments that depend
on them.
Capture test report summary and loop count during phase1 optimization
so the create-pr CLI can generate PR comments without re-running tests.
Previously, ImportInserter inserted new global statements right after
imports, which caused NameError when those statements referenced other
globals defined later in the file (e.g., `_CACHE: dict = {None: tbl}`
being inserted before `tbl` was defined).

Now analyzes dependencies in each new statement and inserts it after
the last definition of any name it references.
…cking

- Add _extract_names_from_target() to handle tuple/list unpacking targets
- Replace get_statement_defined_name() with get_statement_defined_names()
  returning a set to support multiple names per statement
- Add _sort_statements_by_dependencies() for topological sorting of new
  statements before insertion
- Fix GlobalStatementCollector to include tuple/chained assignments
- Fix GlobalAssignmentTransformer to recognize tuple assignments in
  definition_positions
- Remove type annotations from dependency tracking (they don't cause
  runtime NameErrors)
- Add tests for tuple unpacking, chained assignments, multiple statements,
  and annotated assignments
…lar dependency warnings

- Add match statement (Python 3.10+) handling in get_statement_defined_names()
- Fix comprehension variables leaking as external dependencies in NameCollector
- Fix lambda parameters leaking as external dependencies
- Add warning when circular dependencies are detected in _sort_statements_by_dependencies()
- Add Match handling to GlobalAssignmentCollector, GlobalAssignmentTransformer, and GlobalStatementCollector
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.

2 participants