Skip to content

introduce a new integrated "codeflash optimize" command #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Jun 26, 2025

PR Type

Enhancement


Description

  • Integrate trace-and-optimize pipeline in CLI

  • Record and use replay_test_file_path in tracer

  • Add --trace-only flag and default trace file

  • Fix method separators and remove debug print


Changes walkthrough 📝

Relevant files
Enhancement
tracer.py
Integrate replay test optimization pipeline                           

codeflash/tracer.py

  • Record replay_test_file_path during tracing
  • Integrate replay test and optimization flow in main
  • Add --trace-only flag and default trace file
  • Fix method name separators and stats caption
  • +45/-8   
    cli.py
    Add optimize subcommand to CLI                                                     

    codeflash/cli_cmds/cli.py

  • Add optimize subcommand for trace-and-optimize
  • Use parse_known_args and adjust sys.argv
  • +8/-1     
    Miscellaneous
    pickle_patcher.py
    Eliminate placeholder debug output                                             

    codeflash/picklepatch/pickle_patcher.py

    • Remove debug print in placeholder creation
    +0/-2     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @misrasaurabh1 misrasaurabh1 marked this pull request as draft June 26, 2025 03:50
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Missing CLI argument

    The optimize pipeline invokes a --replay-test flag when chaining into the optimizer, but parse_args does not define or handle --replay-test. This will cause parsing failures or ignore the flag during the optimize command.

    trace_optimize = subparsers.add_parser("optimize", help="Trace and optimize a Python project.")
    from codeflash.tracer import main as tracer_main
    
    trace_optimize.set_defaults(func=tracer_main)
    sys.argv mutation

    Overwriting sys.argv after parse_known_args may drop required arguments for downstream parsing and alter the invocation context unexpectedly. Verify this manipulation does not break other subcommands or nested CLI parsing.

    args, unknown_args = parser.parse_known_args()
    sys.argv[:] = [sys.argv[0], *unknown_args]

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Include optimize subcommand in invocation

    Include the "optimize" subcommand in the sys.argv array so the top-level CLI parser
    recognizes it and invokes the optimization phase correctly.

    codeflash/tracer.py [873]

    -sys.argv = ["codeflash", "--replay-test", str(replay_test_path)]
    +sys.argv = ["codeflash", "optimize", "--replay-test", str(replay_test_path)]
    Suggestion importance[1-10]: 8

    __

    Why: The CLI invocation omits the "optimize" subcommand causing the top-level parser to misinterpret "--replay-test", so including it is critical for correct workflow.

    Medium
    General
    Limit sys.argv mutation to optimize

    Restrict the mutation of sys.argv to only when the "optimize" command is active, so
    other subcommands receive their intended arguments and avoid unintended stripping.

    codeflash/cli_cmds/cli.py [73-74]

     args, unknown_args = parser.parse_known_args()
    -sys.argv[:] = [sys.argv[0], *unknown_args]
    +if args.command == "optimize":
    +    sys.argv[:] = [sys.argv[0], *unknown_args]
    Suggestion importance[1-10]: 6

    __

    Why: Restricting the global sys.argv rewrite to the optimize command prevents other subcommands from losing their arguments and improves correctness.

    Low
    Hoist tracer_main import

    Move the import of tracer_main to the module level to avoid repeated imports each
    time parse_args is called and clarify dependencies.

    codeflash/cli_cmds/cli.py [26-27]

    -trace_optimize = subparsers.add_parser("optimize", help="Trace and optimize a Python project.")
     from codeflash.tracer import main as tracer_main
     
    +trace_optimize = subparsers.add_parser("optimize", help="Trace and optimize a Python project.")
    +
    Suggestion importance[1-10]: 3

    __

    Why: Moving the import to module scope reduces repeated imports in parse_args, but the performance gain is minimal and context clarity is modest.

    Low

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants