Commit graph

82 commits

Author SHA1 Message Date
Kevin Turcios
dd5e347bbb fix: skip attrs classes in __init__ instrumentation; add attrs support to code_context_extractor
- instrument_codeflash_capture: detect @attrs.define / @attr.s / etc. in the
  'no explicit __init__' branch and return early, same as dataclass/NamedTuple.
  Prevents a TypeError caused by attrs(slots=True) creating a new class whose
  __class__ cell no longer matches the injected super().__init__ wrapper.

- code_context_extractor: add _get_attrs_config() helper; update
  _collect_synthetic_constructor_type_names, _build_synthetic_init_stub, and
  _extract_synthetic_init_parameters to handle attrs field conventions
  (factory= keyword, init=False, kw_only).

- tests: add 3 exact-output tests for instrumentation skip behaviour and
  3 exact-output tests for attrs stub generation.

Co-Authored-By: Oz <oz-agent@warp.dev>
2026-03-18 01:33:40 -06:00
Kevin Turcios
282f2ba713 Improve testgen constructor context extraction 2026-03-16 00:47:17 -06:00
Mohamed Ashraf
fa9d32f1c4 Merge branch 'main' into omni-java
Resolve 7 merge conflicts from main's modular refactoring + JS improvements:

- aiservice.py: combine multi-language metadata (omni-java) with main's structure
- cmd_init.py: adopt main's modular split (init_config, init_auth, github_workflow) + add Java import
- code_replacer.py: main's clean early-return style + omni-java's non-Python single-block fallback
- version.py, test_support_dispatch.py, test_javascript_test_runner.py: take main's versions
- uv.lock: regenerated

Port Java into main's modular structure:
- Fix init_java.py lazy imports to point to new modules (init_config, init_auth, github_workflow)
- Add Java workflow support to github_workflow.py (detection, template, customization)
- Fix broken Java imports (function_optimizer, line_profiler) after main's module moves

Add safety tests for merge-critical functions:
- test_add_language_metadata.py: 10 tests covering per-language payload correctness
- test_code_replacer_matching.py: 8 tests covering fallback chain

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 00:15:19 +00:00
Kevin Turcios
6330005999 test: update expected markdown ordering to match target-file-first change 2026-03-05 07:04:03 -05:00
Kevin Turcios
eceac13fc3 Merge remote-tracking branch 'origin/main' into omni-java
# Conflicts:
#	.claude/rules/architecture.md
#	.claude/rules/code-style.md
#	.github/workflows/claude.yml
#	.github/workflows/duplicate-code-detector.yml
#	codeflash/api/aiservice.py
#	codeflash/cli_cmds/console.py
#	codeflash/cli_cmds/logging_config.py
#	codeflash/code_utils/deduplicate_code.py
#	codeflash/discovery/discover_unit_tests.py
#	codeflash/languages/base.py
#	codeflash/languages/code_replacer.py
#	codeflash/languages/javascript/mocha_runner.py
#	codeflash/languages/javascript/support.py
#	codeflash/languages/python/support.py
#	codeflash/optimization/function_optimizer.py
#	codeflash/verification/parse_test_output.py
#	codeflash/verification/verification_utils.py
#	codeflash/verification/verifier.py
#	packages/codeflash/package-lock.json
#	packages/codeflash/package.json
#	tests/languages/javascript/test_support_dispatch.py
#	tests/test_codeflash_capture.py
#	tests/test_languages/test_javascript_test_runner.py
#	tests/test_multi_file_code_replacement.py
2026-03-04 01:52:32 -05:00
Kevin Turcios
19bd6e4bad test: sync test files from main (safe, main-only changes)
34 test files updated with main's refactored tests for new language
support protocol, JS/TS improvements, and code context extraction.
2026-03-02 15:25:50 -05:00
Kevin Turcios
5cee1b5b48 feat: improve test generation context for external library types
Extend extract_parameter_type_constructors to scan function bodies for
isinstance/type() patterns and collect base class names from enclosing
classes. Add one-level transitive stub extraction so the LLM also sees
constructor signatures for types referenced in __init__ parameters.

In enrich_testgen_context, branch on source: project classes get full
definitions, third-party (site-packages) classes get compact __init__
stubs to avoid blowing token limits.
2026-02-26 09:40:04 -05:00
Kevin Turcios
6c4378db51 fix: preserve comment position by passing CST module directly to import adder
parse_code_and_prune_cst now returns cst.Module instead of str.
add_needed_imports_from_module accepts cst.Module | str, skipping re-parse
when a Module is passed. This eliminates the string round-trip that caused
comments to migrate from statement leading_lines to Module.header,
resulting in comments appearing above imports instead of at their
original position.
2026-02-23 01:08:39 -05:00
Kevin Turcios
c6fbdfa535 chore: merge main into fixes-for-core-unstructured-experimental 2026-02-21 00:57:33 -05:00
Kevin Turcios
c1703a2d71 Revert "commit"
This reverts commit 2966e15775.
2026-02-21 00:50:31 -05:00
Kevin Turcios
2966e15775 commit
feat: extend testgen type context to include function body references

Extract types referenced in the function body (constructor calls, attribute
access, isinstance/issubclass args) in addition to parameter annotations.
Use full class extraction instead of init-stub-only, with instance resolution
fallback and project/site-packages filtering.
2026-02-21 00:50:04 -05:00
Kevin Turcios
ef99747697 refactor: move code_extractor, code_replacer to languages/python/static_analysis/ 2026-02-19 03:21:34 -05:00
Kevin Turcios
2652e71617 Merge remote-tracking branch 'origin/main' into call-graphee
# Conflicts:
#	.codex/skills/.gitignore
#	.gemini/skills/.gitignore
#	codeflash/languages/python/context/code_context_extractor.py
2026-02-19 01:05:42 -05:00
Kevin Turcios
6a5c9c1b40 test: update expected context values for statement-type helpers 2026-02-18 21:07:03 -05:00
Kevin Turcios
b269212edd context extraction imporvements 2026-02-18 09:09:36 -05:00
claude[bot]
7f5e163e38 fix: resolve mypy attr-defined errors in new test functions 2026-02-18 13:46:54 +00:00
Kevin Turcios
bfcfa44d15 fix: correct pre-existing test failures in test_code_context_extractor
Fix 10 failing tests: remove wrong assertions expecting import statements
inside extracted class code, use substring matching for UserDict class
signature, and rewrite click-dependent tests as project-local equivalents.
Add tests for resolve_instance_class_name, enhanced extract_init_stub_from_class,
and enrich_testgen_context instance resolution.
2026-02-18 08:43:42 -05:00
Kevin Turcios
d480061064 temp 2026-02-18 08:26:37 -05:00
Kevin Turcios
2367b4c02c feat: extract parameter type constructor signatures into testgen context
Add enrichment step that parses FTO parameter type annotations, resolves
types via jedi (following re-exports), and extracts full __init__ source
to give the LLM constructor context for typed parameters.
2026-02-18 07:51:53 -05:00
Kevin Turcios
a2238168a3 refactor: remove 13 unused functions from code_context_extractor
Remove safe_relative_to, resolve_classes_from_modules,
extract_classes_from_type_hint, resolve_transitive_type_deps,
extract_init_stub, _is_project_module_cached, is_project_path,
_is_project_module, extract_imports_for_class,
collect_names_from_annotation, is_dunder_method, _qualified_name,
and _validate_classdef. Inline trivial helpers into prune_cst and
clean up enrich_testgen_context and get_function_sources_from_jedi.
Remove corresponding tests.
2026-02-18 05:03:54 -05:00
Kevin Turcios
0bcc483a95 Merge branch 'main' into call-graphee 2026-02-16 22:50:24 -05:00
Kevin Turcios
bba3e0aa4d Merge branch 'main' into call-graphee 2026-02-16 22:43:03 -05:00
Kevin Turcios
547c02e8bc refactor: move context extraction modules to languages/python/context/
Move code_context_extractor.py and unused_definition_remover.py from
codeflash/context/ to codeflash/languages/python/context/ and update
all import sites.
2026-02-16 14:49:04 -05:00
Kevin Turcios
fa00422fea refactor: simplify and deduplicate code_context_extractor
Consolidate three enricher functions (get_imported_class_definitions,
get_external_base_class_inits, get_external_class_inits) into a single
enrich_testgen_context that parses code context once. Extract shared
helpers, unify prune_cst variants, deduplicate loop bodies, and remove
dead UsedNameCollector class.
2026-02-16 13:34:07 -05:00
Kevin Turcios
e837ad9d17 feat: resolve transitive type dependencies in get_external_class_inits
Add BFS-based transitive resolution so that classes referenced in __init__
type annotations of imported external classes are also extracted. This gives
the LLM the constructor signatures it needs to instantiate parameter types.
2026-02-13 09:35:30 -05:00
claude[bot]
8eb1c86245 fix: resolve mypy union-attr error in test_get_external_class_inits
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 14:09:28 +00:00
Kevin Turcios
f4c0208f49 test: add unit tests for get_external_class_inits
Tests cover: extracting __init__ from site-packages classes (click.Option),
skipping project classes, non-classes, already-defined classes, builtins,
classes with trivial object.__init__, and empty import scenarios.
2026-02-13 09:03:09 -05:00
Kevin Turcios
fc42548f9f test: update token limit tests for 64K default 2026-02-12 01:03:34 -05:00
Kevin Turcios
9e904483d8 fix: use explicit token limits in tests to decouple from global constant 2026-02-12 01:02:21 -05:00
misrasaurabh1
198487bf81 format and lint all 2026-01-29 01:39:48 -08:00
Kevin Turcios
65ff392d20 add tests 2026-01-24 10:14:54 -05:00
Kevin Turcios
571b2ba694 skip for 3.9 2026-01-24 07:05:27 -05:00
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