Commit graph

50 commits

Author SHA1 Message Date
Kevin Turcios
7b33e8b7f6 refactor: smarter placement of global assignments based on dependencies
Assignments that don't reference module-level definitions are now placed
right after imports. Only assignments that reference classes/functions
are placed after those definitions to prevent NameError.
2026-01-24 06:29:39 -05:00
Kevin Turcios
50fba096f7 fix: insert global statements after function definitions to prevent NameError
When LLM-generated optimizations include module-level function calls like
`_register(MessageKind.ASK, ...)`, they were being inserted right after
imports, BEFORE the function definition they reference, causing NameError
at module load time.

Changes:
- Add GlobalStatementTransformer to append global statements at module end
- Reorder transformations: functions → assignments → statements
- Remove unused ImportInserter class
- Update test expectations to reflect new placement behavior
2026-01-24 02:09:38 -05:00
Kevin Turcios
abfa640578 fix: insert global assignments after class definitions to prevent NameError
When LLM-generated optimizations include module-level code like
`_REIFIERS = {MessageKind.XXX: ...}`, the global assignment was being
inserted right after imports, BEFORE the class definition it referenced,
causing NameError at module load time.

Changes:
- GlobalAssignmentTransformer now inserts assignments after all
  class/function definitions instead of right after imports
- GlobalStatementCollector now skips AnnAssign (annotated assignments)
  so they are handled by GlobalAssignmentCollector instead
2026-01-24 01:37:15 -05:00
Kevin Turcios
6b3b10e7fa fix: include dependency classes in read-writable optimization context
Classes used as dependencies (enums, dataclasses, types) were being
excluded from the optimization context even when marked as used by
the target function. This caused NameError when the LLM used these
types in generated optimizations.
2026-01-24 01:19:22 -05:00
Kevin Turcios
dbc88ad105 feat: extract __init__ from external library base classes for test context
Add get_external_base_class_inits to extract __init__ methods from external
library base classes (e.g., collections.UserDict) when project classes inherit
from them. This helps the LLM understand constructor signatures for mocking.
2026-01-23 20:46:51 -05:00
Kevin Turcios
6009b83f20 fix: handle module-level function definitions in add_global_assignments
Add GlobalFunctionCollector and GlobalFunctionTransformer to collect and
insert module-level function definitions introduced by LLM optimizations.
This fixes NameError when optimized code introduces new helper functions
like @lru_cache decorated functions that are used by the optimized method.
2026-01-23 18:59:00 -05:00
Kevin Turcios
9f929c2151 fix: handle annotated assignments in GlobalAssignmentCollector
GlobalAssignmentCollector only handled cst.Assign but not cst.AnnAssign
(annotated assignments like `X: int = 1`). When the LLM generated
optimizations with annotated module-level variables, these weren't
copied to the target file, causing NameError at runtime.

- Add visit_AnnAssign to GlobalAssignmentCollector
- Add leave_AnnAssign to GlobalAssignmentTransformer
- Update type hints to include cst.AnnAssign
- Add test for annotated assignment handling
2026-01-23 18:46:49 -05:00
Kevin Turcios
ebf77033ba test: add tests for base class extraction in imported dataclasses
- Update test_get_imported_class_definitions_includes_dataclass_decorators
  to expect both base class and derived class to be extracted
- Add test_get_imported_class_definitions_extracts_multilevel_inheritance
  to verify multi-level inheritance chains are fully extracted
2026-01-23 07:26:03 -05:00
Kevin Turcios
722d05345d decorators & annotations 2026-01-23 06:52:45 -05:00
Kevin Turcios
0312c37631 feat: extract imported class definitions for testgen context
When generating tests, the LLM now receives class definitions for
types imported from project modules. This helps the LLM understand:
- Constructor signatures (avoiding incorrect argument guessing)
- Base classes (e.g., abstract classes that can't be instantiated)
- Class structure for creating proper test instances

Previously, the LLM only saw import statements like:
  from mypackage.elements import Element

Now it also sees the actual class definition with constructor details.

Changes:
- Add get_imported_class_definitions() to extract class definitions
  from project modules referenced in import statements
- Integrate into get_code_optimization_context() to include extracted
  classes in testgen context
- Gracefully handle token limits by dropping class definitions if needed
- Add 4 unit tests covering extraction, deduplication, and filtering
2026-01-07 16:09:29 -05:00
Kevin Turcios
3048ece4da fix: track class __init__ as helper when class is instantiated
Ensures LLM sees constructor signatures for proper test generation.
2026-01-01 02:25:10 -05:00
ali
b464c4d869
typo 2025-11-21 20:53:35 +02:00
ali
15d2027bb0
keep the refrenced global definitions 2025-11-21 20:10:26 +02:00
Kevin Turcios
f978a406bb Merge branch 'main' into part-1-windows-fixes 2025-09-29 14:46:25 -07:00
Kevin Turcios
c9cfaacaaa macos symlink shenanigans 2025-09-27 23:21:15 -07:00
Kevin Turcios
09e9d12813 update context extractor 2025-09-28 04:47:46 +00:00
Kevin Turcios
a826252277 Update test_code_context_extractor.py 2025-09-28 04:38:25 +00:00
Kevin Turcios
c6c9d9559f few missing things 2025-09-26 16:25:28 -07:00
Kevin Turcios
4325416827 resolve paths
in my case, i've started using symlinks a bit more often, and the current impl causes issues, we need to resolve the symlinked path too.
2025-09-26 14:49:29 -07:00
mohammed
a7ff701309
eleminate the use of flat code for parsing 2025-08-06 22:48:03 +03:00
mohammed
989b1f30a2
unit tests fixing 2025-08-06 03:33:46 +03:00
mohammed
654a6ec251
refactoring 2025-07-26 08:49:23 +03:00
mohammed
f48c77df38
flat method rename 2025-07-25 17:50:13 +03:00
mohammed
99cd9dc706
fix tests for context extractor 2025-07-25 15:13:10 +03:00
mohammed
216eb7e794
start using markdown representation for read writable context 2025-07-16 18:48:31 +03:00
mohammed
2e394f6b8f tests and fix global assignments imports 2025-06-29 00:23:36 +03:00
mohammed
2acda6a411 initial test files 2025-06-28 21:14:26 +03:00
Saurabh Misra
7167d2b75d edge case for python 39
Some checks are pending
CodeFlash / Optimize new Python code (pull_request) Waiting to run
PR Labeler / label-workflow-changes (pull_request) Waiting to run
Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
/ Run pr agent on every pull request, respond to user comments (pull_request) Waiting to run
Lint / Run pre-commit hooks (pull_request) Waiting to run
unit-tests / unit-tests (3.10) (pull_request) Waiting to run
unit-tests / unit-tests (3.11) (pull_request) Waiting to run
unit-tests / unit-tests (3.12) (pull_request) Waiting to run
unit-tests / unit-tests (3.9) (pull_request) Waiting to run
2025-06-09 00:14:45 -07:00
Saurabh Misra
51c936f421 normalize code before hashing 2025-06-09 00:02:59 -07:00
Saurabh Misra
be1ef9b7d9 fix for test 2025-06-08 01:14:53 -07:00
Saurabh Misra
6ed93871b4 add more tests 2025-06-08 01:13:17 -07:00
Saurabh Misra
2c1314dc91 fix more tests 2025-06-08 00:41:25 -07:00
Saurabh Misra
9e14cfe7a0 fix bugs with docstring removal 2025-06-08 00:30:47 -07:00
Alvin Ryanputra
0d77ea4a0b reworked tests 2025-04-18 20:29:38 -04:00
Alvin Ryanputra
7cda6aafa7 tests for code context extractor 2025-04-17 18:44:15 -04:00
Alvin Ryanputra
9b4ede56a3 initial implementation 2025-04-16 14:14:05 -04:00
Alvin Ryanputra
99189a7cb2 prevent nested classes/ functions from showing up in code context . helper_functions
Some checks are pending
CodeFlash / Optimize new Python code (pull_request) Waiting to run
end-to-end-test / bubble-sort-optimization-pytest-no-git (pull_request) Waiting to run
end-to-end-test / bubble-sort-optimization-unittest (pull_request) Waiting to run
Coverage E2E / end-to-end-test-coverage (pull_request) Waiting to run
end-to-end-test / futurehouse-structure (pull_request) Waiting to run
end-to-end-test / init-optimization (pull_request) Waiting to run
end-to-end-test / tracer-replay (pull_request) Waiting to run
end-to-end-test / topological-sort-optimization (pull_request) Waiting to run
Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
/ Run pr agent on every pull request, respond to user comments (pull_request) Waiting to run
unit-tests / unit-tests (3.10.13) (pull_request) Waiting to run
unit-tests / unit-tests (3.11.6) (pull_request) Waiting to run
unit-tests / unit-tests (3.12.1) (pull_request) Waiting to run
unit-tests / unit-tests (3.13.0) (pull_request) Waiting to run
unit-tests / unit-tests (3.9.18) (pull_request) Waiting to run
2025-04-01 15:26:40 -07:00
Alvin Ryanputra
26e5923b50 fixed a bug where helper functions called in the __init__ was not being found as a helper of helper
Some checks are pending
/ Run pr agent on every pull request, respond to user comments (pull_request) Waiting to run
unit-tests / unit-tests (3.13.0) (pull_request) Waiting to run
unit-tests / unit-tests (3.9.18) (pull_request) Waiting to run
CodeFlash / Optimize new code in this PR (pull_request) Waiting to run
end-to-end-test / bubble-sort-optimization-pytest-no-git (pull_request) Waiting to run
end-to-end-test / bubble-sort-optimization-unittest (pull_request) Waiting to run
Coverage E2E / end-to-end-test-coverage (pull_request) Waiting to run
end-to-end-test / futurehouse-structure (pull_request) Waiting to run
end-to-end-test / init-optimization (pull_request) Waiting to run
end-to-end-test / tracer-replay (pull_request) Waiting to run
end-to-end-test / topological-sort-optimization (pull_request) Waiting to run
Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
unit-tests / unit-tests (3.10.13) (pull_request) Waiting to run
unit-tests / unit-tests (3.11.6) (pull_request) Waiting to run
unit-tests / unit-tests (3.12.1) (pull_request) Waiting to run
2025-03-13 11:25:16 -07:00
Alvin Ryanputra
7d33b948b0 Merge branch 'refs/heads/main' into testgen-context-improvement
# Conflicts:
#	codeflash/context/code_context_extractor.py
#	tests/test_get_read_writable_code.py
2025-03-05 16:50:10 -08:00
Alvin Ryanputra
17a42a218c Implemented testgen context retrieval. Context retrieved is the union of read-writable code and read-only code. Did some refactors to remove code_to_optimize_with_helpers, and updated tests. 2025-03-05 16:40:23 -08:00
Kevin Turcios
d28031f174 ruff check . 2025-02-28 19:21:22 -08:00
Alvin Ryanputra
f5f136ba9b changes after code review, made logic more robust, added tests_root arg to codeflash capture, added recursive tests, renamed some files / functions 2025-02-05 16:17:41 +08:00
Alvin Ryanputra
17c354a466 takes care of docstring of new init function 2025-01-14 10:54:43 -08:00
Alvin Ryanputra
8de9cebe90 draft PR for init caching. no instrumentation checks implemented yet 2025-01-13 17:01:52 -08:00
Alvin Ryanputra
c41f710f5f Modified code replacer to work file by file. now compatible with new code context extractor. 2025-01-08 14:56:53 -08:00
Alvin Ryanputra
57d1d979da changed file paths in read only context to be relative to project root 2025-01-02 12:05:41 -08:00
Alvin Ryanputra
a10b399dbe added helpers of helpers into readonly context, and refactored code slightly 2024-12-31 17:19:09 -08:00
Alvin Ryanputra
0204ef2fcb handle token limits 2024-12-26 14:29:32 -08:00
Alvin Ryanputra
e10f13a83a added docstring removal 2024-12-26 14:06:05 -08:00
Alvin Ryanputra
693c150262 some refactoring 2024-12-23 12:27:31 -08:00
Renamed from tests/test_retriever.py (Browse further)