Skip to content

Add language guards for Python-only endpoints#1997

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/python-only-endpoints-language-guards
Open

Add language guards for Python-only endpoints#1997
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/python-only-endpoints-language-guards

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Issue #9: Python-Only Endpoints Called Without Language Guards

Problem

The process_review() method calls get_new_explanation() and get_optimization_review() without checking the language. These are Python-only endpoints that would fail or return incorrect results for JavaScript/TypeScript.

Root Cause

File: /opt/codeflash/codeflash/languages/function_optimizer.py

Line 2575: get_new_explanation() called without language check
Line 2638: get_optimization_review() called without language check

This is the same pattern as Issue #8 (adaptive_optimize) which was already fixed in PR #1995.

Impact

  • Latent bug (not yet triggered - will manifest when JS/TS optimizations start succeeding)
  • Severity: MEDIUM (not blocking current work, but will block future success)
  • All current JS/TS optimizations fail at baseline (Docker/timeouts), so these endpoints are never reached
  • Once baseline issues are resolved, JS/TS optimizations would fail when trying to generate explanations or reviews

Fix

Line 2575 - get_new_explanation:

# Before (broken):
new_explanation_raw_str = self.aiservice_client.get_new_explanation(...)

# After (fixed):
new_explanation_raw_str = None
if self.function_to_optimize.language == "python":
    new_explanation_raw_str = self.aiservice_client.get_new_explanation(...)

Line 2638 - get_optimization_review:

# Before (broken):
try:
    opt_review_result = self.aiservice_client.get_optimization_review(...)
except Exception as e:
    logger.debug(...)

# After (fixed):
if self.function_to_optimize.language == "python":
    try:
        opt_review_result = self.aiservice_client.get_optimization_review(...)
    except Exception as e:
        logger.debug(...)

For non-Python languages:

  • get_new_explanation returns None → uses original explanation from AI service
  • get_optimization_review returns empty review → PR proceeds without review rating

Testing

Existing function_optimizer tests pass (5 tests)
No linting/type errors (uv run prek)
No regressions in optimization flow

Related Issues

Category

Latent bug (will reproduce when JS/TS optimizations succeed)

Files Changed

  • codeflash/languages/function_optimizer.py - Added language guards

Issue #9: process_review() called get_new_explanation() and
get_optimization_review() without checking language. These are Python-only
endpoints that would fail or return incorrect results for JavaScript/TypeScript.

Root Cause:
- process_review() (line 2459) calls two Python-only endpoints:
  - get_new_explanation() (line 2575) - no language check
  - get_optimization_review() (line 2638) - no language check
- Same pattern as Issue #8 (adaptive_optimize) which was already fixed
- Latent bug - only manifests when JS/TS optimization succeeds

Fix:
- Added language guard before get_new_explanation: if language == "python"
- Added language guard before get_optimization_review: if language == "python"
- For non-Python languages, use original explanation from AI service
- For non-Python languages, skip optimization review (sets empty review)

Impact:
- Latent bug (not yet triggered because all JS/TS optimizations fail at baseline)
- Would block JS/TS optimization success once baseline issues are resolved
- Severity: MEDIUM (latent, not blocking current work)

Testing:
- Existing function_optimizer tests pass
- No linting/type errors

Category: Latent bug (will reproduce when JS/TS optimizations succeed)
Type: Missing language guard in CLI

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 4, 2026

Claude finished @mohammedahmed18's task in 3m 22s —— View job


PR Review Summary

  • Triage PR size — SMALL (1 file, +39/-30)
  • Run lint/typecheck (prek) — ✅ Pass
  • Resolve stale review threads — none found
  • Review diff
  • Duplicate detection
  • Post summary

Prek Checks

✅ All ruff/format checks pass. No issues.


Code Review

Correctness: The guards are logically correct. get_new_explanation() and get_optimization_review() are Python-specific AI service endpoints and should not be called for JS/TS/Java. The FunctionToOptimize.language field stores str(Language.X) values (e.g. "javascript", "python"), so == "python" comparisons work correctly.

⚠️ Overlap with open PR #1995

The change at function_optimizer.py:1268 (adding and self.function_to_optimize.language == "python" to the adaptive_optimize guard) is identical to the change in PR #1995, which is currently open. Both PRs modify the same line to the same content. Whichever merges second will have a trivial merge conflict on that line. Coordinate with or supersede PR #1995 to avoid churn.

⚠️ Language guard pattern goes against migration direction

Per .claude/rules/language-patterns.md:

Prefer LanguageSupport protocol dispatch over is_python()/is_javascript() guards — remaining guards are being migrated to protocol methods.

These new == "python" string comparisons add three more sites that will eventually need to be migrated. For the two new guards (get_new_explanation, get_optimization_review), consider whether the AI service client itself should handle this (e.g., a supports_explanation_generation() protocol method), rather than adding more inline language checks. This isn't a blocker given the stated latent-bug urgency, but worth tracking.

⚠️ No tests for the new guards

CLAUDE.md: "Everything that can be tested should have tests." PR #1995 added 195 lines of tests for the adaptive_optimize guard (same pattern, same file). This PR adds no tests for the get_new_explanation and get_optimization_review guards. Tests should be added to cover the non-Python path returning None/empty-review correctly. Fix this →


Duplicate Detection

No duplicates detected. The guards follow the same pattern already used in this file.


Last updated: 2026-04-04

mohammedahmed18 added a commit that referenced this pull request Apr 4, 2026
**Problem:**
The `maybe_repair_optimization()` method called `/ai/code_repair` (a Python-only
endpoint) without checking if the language is Python first. This would cause
errors when JavaScript/TypeScript optimizations reach the repair stage.

**Root Cause:**
File: codeflash/languages/function_optimizer.py (line 2957)
- `repair_optimization()` calls `/ai/code_repair` endpoint
- `/ai/code_repair` uses Python-specific tools (libcst for AST manipulation)
- No language check before calling repair

**Impact:**
- Latent bug (not yet triggered in production)
- Would block JS/TS optimization success once candidates reach repair stage
- Severity: MEDIUM

**Fix:**
Added language guard at line 2948:
- Check `self.function_to_optimize.language != "python"`
- Skip repair for JavaScript/TypeScript/Java
- Log debug message explaining why repair was skipped

**Testing:**
- Added tests/test_languages/test_code_repair_language_guard.py
- Documents expected behavior for language checks
- All existing tests pass
- No linting/type errors

**Trace IDs:**
N/A (latent bug, not yet triggered)

**Related Issues:**
- Similar pattern to Issue #8 (adaptive_optimize) - PR #1995
- Similar pattern to Issue #9 (get_new_explanation, get_optimization_review) - PR #1997

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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