Skip to content

Comments

refactor: inline async decorators to remove codeflash import dependency#1518

Merged
KRRT7 merged 6 commits intomainfrom
proper-async
Feb 18, 2026
Merged

refactor: inline async decorators to remove codeflash import dependency#1518
KRRT7 merged 6 commits intomainfrom
proper-async

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 18, 2026

Summary

  • Replace AsyncDecoratorImportAdder CST transformer with standalone helper-file approach: decorator implementations are written to a codeflash_async_wrapper.py file in the project root, and source files import from that module instead of from codeflash.code_utils.codeflash_wrap_decorator import ...
  • Removes the hard dependency on the codeflash package being importable at runtime in the target environment, matching the pattern already used for sync instrumentation
  • Add inline code generators for all three async modes (get_behavior_async_inline_code, get_performance_async_inline_code, get_concurrency_async_inline_code)
  • Add project_root parameter to add_async_decorator_to_function so the helper file is written to a directory on sys.path; all call sites in function_optimizer.py updated
  • Fix async_e2e/main.py test fixture: replace blocking time.sleep with await asyncio.sleep
  • Add test case for duplicate-detection when an inline decorator definition is already present

Test plan

  • uv run pytest tests/test_instrument_async_tests.py -x -v — 14 passed
  • uv run pytest tests/test_async_run_and_parse_tests.py -x -v — 8 passed
  • uv run pytest tests/test_async_wrapper_sqlite_validation.py tests/test_async_concurrency_decorator.py -x -v — 15 passed (unchanged)
  • Verified instrumented files contain no from codeflash imports
  • ruff check clean on changed files

KRRT7 and others added 2 commits February 18, 2026 06:30
Instead of injecting `from codeflash.code_utils.codeflash_wrap_decorator import ...`
into instrumented source files, inject the decorator function definitions directly.
This removes the hard dependency on the codeflash package being importable at runtime
in the target environment, matching the pattern already used for sync instrumentation.
Replace inline code injection with a helper file approach that writes
decorator implementations to a separate codeflash_async_wrapper.py file.
This removes the codeflash package import dependency from instrumented
source files while keeping line numbers stable (only 1 import + 1
decorator line added, same as before).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codeflash-ai codeflash-ai deleted a comment from claude bot Feb 18, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Feb 18, 2026
@codeflash-ai codeflash-ai deleted a comment from codeflash-ai bot Feb 18, 2026
KRRT7 and others added 3 commits February 18, 2026 03:46
…e file

Write all three async decorator implementations into one helper file to
avoid overwrite issues when switching modes. Clean up the helper file in
revert_code_and_helpers and early-exit paths so it doesn't persist in the
user's project root after optimization.
…rget

The e2e test expects codeflash to detect and fix the intentional use of
blocking time.sleep() in an async function. Using asyncio.sleep() removes
the optimization opportunity and causes the CI job to fail.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The optimized code removes `import time`, shifting all function lines
up by 1. Update expected_lines from [10-20] to [9-19] to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 18, 2026

PR Review Summary

Prek Checks

All checks passed (ruff check, ruff format). No issues found.

Mypy

No new type errors introduced by this PR. All mypy errors in the changed files are pre-existing.

Code Review

No critical issues found. The refactoring cleanly replaces the AsyncDecoratorImportAdder CST transformer with a standalone helper-file approach:

  • Helper file approach: Decorator implementations are written to codeflash_async_wrapper.py at the project root, and source files import from that module instead of requiring codeflash to be importable at runtime
  • Cleanup: cleanup_async_helper_file() is called in all error/revert paths in FunctionOptimizer. Note: the worktree early-return path (line 2263) skips cleanup, but this appears intentional since worktrees are cleaned up separately
  • Tests updated: All tests properly updated to verify the new import style (from codeflash_async_wrapper import ...) and check for helper file existence
  • New duplicate-detection test: Added Case 2 in test_async_decorator_no_duplicate_application for inline-style decorators

Test Coverage

File Main PR Change
codeflash/code_utils/instrument_existing_tests.py 91% (423 stmts) 92% (417 stmts) +1%
codeflash/optimization/function_optimizer.py 18% (1155 stmts) 18% (1162 stmts) 0%
Overall ~78% ~78% No change
  • No coverage regressions
  • instrument_existing_tests.py coverage improved slightly (+1%) despite code changes
  • function_optimizer.py coverage is low (18%) but this is pre-existing and unchanged by this PR
  • All 8 test failures are pre-existing in test_tracer.py (unrelated to this PR)

Last updated: 2026-02-18

@KRRT7 KRRT7 merged commit 9d23a0e into main Feb 18, 2026
27 of 28 checks passed
@KRRT7 KRRT7 deleted the proper-async branch February 18, 2026 10:11
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.

1 participant