diff --git a/.github/workflows/claude-code-review.yml b/.github/workflows/claude-code-review.yml new file mode 100644 index 000000000..a4c8f6a89 --- /dev/null +++ b/.github/workflows/claude-code-review.yml @@ -0,0 +1,57 @@ +name: Claude Code Review + +on: + pull_request: + types: [opened, synchronize] + # Optional: Only run on specific file changes + # paths: + # - "src/**/*.ts" + # - "src/**/*.tsx" + # - "src/**/*.js" + # - "src/**/*.jsx" + +jobs: + claude-review: + # Optional: Filter by PR author + # if: | + # github.event.pull_request.user.login == 'external-contributor' || + # github.event.pull_request.user.login == 'new-developer' || + # github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' + + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + issues: read + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run Claude Code Review + id: claude-review + uses: anthropics/claude-code-action@v1 + with: + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + prompt: | + REPO: ${{ github.repository }} + PR NUMBER: ${{ github.event.pull_request.number }} + + Please review this pull request and provide feedback on: + - Code quality and best practices + - Potential bugs or issues + - Performance considerations + - Security concerns + - Test coverage + + Use the repository's CLAUDE.md for guidance on style and conventions. Be constructive and helpful in your feedback. + + Use `gh pr comment` with your Bash tool to leave your review as a comment on the PR. + + # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md + # or https://code.claude.com/docs/en/cli-reference for available options + claude_args: '--allowed-tools "Bash(gh issue view:*),Bash(gh search:*),Bash(gh issue list:*),Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr list:*)"' + diff --git a/.github/workflows/claude.yml b/.github/workflows/claude.yml new file mode 100644 index 000000000..79fe05647 --- /dev/null +++ b/.github/workflows/claude.yml @@ -0,0 +1,50 @@ +name: Claude Code + +on: + issue_comment: + types: [created] + pull_request_review_comment: + types: [created] + issues: + types: [opened, assigned] + pull_request_review: + types: [submitted] + +jobs: + claude: + if: | + (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) || + (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) || + (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) || + (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude'))) + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + issues: read + id-token: write + actions: read # Required for Claude to read CI results on PRs + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run Claude Code + id: claude + uses: anthropics/claude-code-action@v1 + with: + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + + # This is an optional setting that allows Claude to read CI results on PRs + additional_permissions: | + actions: read + + # Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it. + # prompt: 'Update the pull request description to include a summary of changes.' + + # Optional: Add claude_args to customize behavior and configuration + # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md + # or https://code.claude.com/docs/en/cli-reference for available options + # claude_args: '--allowed-tools Bash(gh pr:*)' + diff --git a/codeflash/verification/pytest_plugin.py b/codeflash/verification/pytest_plugin.py index 82025aac7..81cf640d4 100644 --- a/codeflash/verification/pytest_plugin.py +++ b/codeflash/verification/pytest_plugin.py @@ -12,7 +12,6 @@ # System Imports from pathlib import Path -from statistics import mean, median from typing import TYPE_CHECKING, Any, Callable, Optional from unittest import TestCase @@ -297,20 +296,28 @@ def should_stop( return False recent = runtimes[-window:] - m = median(recent) + + # Use sorted array for faster median and min/max operations + recent_sorted = sorted(recent) + mid = window // 2 + m = recent_sorted[mid] if window % 2 else (recent_sorted[mid - 1] + recent_sorted[mid]) / 2 # 1) All recent points close to the median - centered = all(abs(r - m) / m <= center_rel_tol for r in recent) + centered = True + for r in recent: + if abs(r - m) / m > center_rel_tol: + centered = False + break # 2) Window spread is small - r_min, r_max = min(recent), max(recent) + r_min, r_max = recent_sorted[0], recent_sorted[-1] spread_ok = (r_max - r_min) / r_min <= spread_rel_tol # 3) No strong downward trend (still improving) # Compare first half vs second half half = window // 2 - first = mean(recent[:half]) - second = mean(recent[half:]) + first = sum(recent[:half]) / half + second = sum(recent[half:]) / (window - half) slope_ok = (first - second) / first <= slope_rel_tol return centered and spread_ok and slope_ok diff --git a/docs/images/priority-order.png b/docs/images/priority-order.png new file mode 100644 index 000000000..8ee6eb300 Binary files /dev/null and b/docs/images/priority-order.png differ diff --git a/docs/optimizing-with-codeflash/trace-and-optimize.mdx b/docs/optimizing-with-codeflash/trace-and-optimize.mdx index d57ec319c..9d8385287 100644 --- a/docs/optimizing-with-codeflash/trace-and-optimize.mdx +++ b/docs/optimizing-with-codeflash/trace-and-optimize.mdx @@ -6,9 +6,11 @@ sidebarTitle: "Optimize E2E Workflows" keywords: ["tracing", "workflow optimization", "replay tests", "end-to-end", "script optimization", "context manager"] --- -Codeflash supports optimizing an entire Python script end-to-end by tracing the script's execution and generating Replay Tests. +Codeflash can optimize an entire Python script end-to-end by tracing the script's execution and generating Replay Tests. Tracing follows the execution of a script, profiles it and captures inputs to all functions it called, allowing them to be replayed during optimization. -Codeflash uses these Replay Tests to optimize all functions called in the script, starting from the most important ones. +Codeflash uses these Replay Tests to optimize the most important functions called in the script, delivering the best performance for your workflow. + +![Function Optimization](/images/priority-order.png) To optimize a script, `python myscript.py`, simply replace `python` with `codeflash optimize` and run the following command: