codeflash-internal/tiles/codeflash-internal-rules/evals/scenario-5/task.md
2026-02-14 22:25:30 -05:00

2.5 KiB

Scenario 5: Add a JavaScript/TypeScript Postprocessor for Optimization Deduplication

Context

The codeflash-internal optimizer postprocesses optimization candidates to remove duplicates and no-ops. Currently, the Python postprocessor in core/languages/python/optimizer/postprocess.py deduplicates by normalizing code with ast.parse() + ast.dump() and comparing AST dumps. It uses libcst for any code transformations that modify source.

The team needs a JavaScript/TypeScript equivalent postprocessor that follows the same patterns adapted for JS/TS, using the appropriate tools for that language while following the same architectural conventions.

The relevant existing patterns:

  • BaseOptimizerContext in optimizer_context.py handles prompt management and code extraction
  • extract_code_and_explanation_from_llm_res() parses LLM markdown response into code blocks
  • parse_and_generate_candidate_schema() converts extracted code to OptimizeResponseItemSchema
  • Model distribution: claude_calls = (total - 1) // 2, gpt_calls = total - claude_calls
  • LLM calls go through aiservice/llm.py, prompts are .md files rendered with Jinja2

Task

  1. Create a postprocessor at core/languages/js_ts/optimizer/postprocess.py that:

    • Deduplicates optimization candidates for JavaScript/TypeScript code
    • Uses a JS/TS AST normalization approach (can shell out to a Node.js script or use a Python JS parser)
    • Checks for no-op equality (optimized code identical to original)
    • Uses libcst for any Python source transforms if needed (not ast module for transforms)
    • Uses ast only for read-only analysis operations
    • Follows the OptimizeResponseItemSchema output format
  2. Create a prompt template at core/languages/js_ts/optimizer/postprocess_prompt.md for any LLM-assisted dedup decisions, using Jinja2 template syntax

  3. Ensure the postprocessor integrates with the model distribution formula:

    • claude_calls = (total - 1) // 2
    • gpt_calls = total - claude_calls
    • MAX_OPTIMIZER_CALLS = 6
  4. Create tests at tests/optimizer/test_js_ts_postprocess.py:

    • Test deduplication of identical-AST candidates
    • Test no-op detection
    • Use @pytest.mark.asyncio for async tests
    • Use test factories where applicable

Expected Outputs

  • core/languages/js_ts/optimizer/postprocess.py -- dedup and validation logic
  • core/languages/js_ts/optimizer/postprocess_prompt.md -- Jinja2 prompt template
  • tests/optimizer/test_js_ts_postprocess.py -- async tests for dedup and no-op detection