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:
BaseOptimizerContextinoptimizer_context.pyhandles prompt management and code extractionextract_code_and_explanation_from_llm_res()parses LLM markdown response into code blocksparse_and_generate_candidate_schema()converts extracted code toOptimizeResponseItemSchema- Model distribution:
claude_calls = (total - 1) // 2,gpt_calls = total - claude_calls - LLM calls go through
aiservice/llm.py, prompts are.mdfiles rendered with Jinja2
Task
-
Create a postprocessor at
core/languages/js_ts/optimizer/postprocess.pythat:- 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
libcstfor any Python source transforms if needed (notastmodule for transforms) - Uses
astonly for read-only analysis operations - Follows the
OptimizeResponseItemSchemaoutput format
-
Create a prompt template at
core/languages/js_ts/optimizer/postprocess_prompt.mdfor any LLM-assisted dedup decisions, using Jinja2 template syntax -
Ensure the postprocessor integrates with the model distribution formula:
claude_calls = (total - 1) // 2gpt_calls = total - claude_callsMAX_OPTIMIZER_CALLS = 6
-
Create tests at
tests/optimizer/test_js_ts_postprocess.py:- Test deduplication of identical-AST candidates
- Test no-op detection
- Use
@pytest.mark.asynciofor async tests - Use test factories where applicable
Expected Outputs
core/languages/js_ts/optimizer/postprocess.py-- dedup and validation logiccore/languages/js_ts/optimizer/postprocess_prompt.md-- Jinja2 prompt templatetests/optimizer/test_js_ts_postprocess.py-- async tests for dedup and no-op detection