|
2 | 2 |
|
3 | 3 | import ast |
4 | 4 | import concurrent.futures |
| 5 | +import logging |
5 | 6 | import os |
6 | 7 | import queue |
7 | 8 | import random |
|
15 | 16 | from rich.console import Group |
16 | 17 | from rich.panel import Panel |
17 | 18 | from rich.syntax import Syntax |
| 19 | +from rich.text import Text |
18 | 20 | from rich.tree import Tree |
19 | 21 |
|
20 | 22 | from codeflash.api.aiservice import AiServiceClient, AIServiceRefinerRequest, LocalAiServiceClient |
|
34 | 36 | create_rank_dictionary_compact, |
35 | 37 | create_score_dictionary_from_metrics, |
36 | 38 | diff_length, |
| 39 | + encoded_tokens_len, |
37 | 40 | extract_unique_errors, |
38 | 41 | file_name_from_test_module_name, |
39 | 42 | get_run_tmp_file, |
|
46 | 49 | COVERAGE_THRESHOLD, |
47 | 50 | INDIVIDUAL_TESTCASE_TIMEOUT, |
48 | 51 | MIN_CORRECT_CANDIDATES, |
| 52 | + OPTIMIZATION_CONTEXT_TOKEN_LIMIT, |
49 | 53 | REFINED_CANDIDATE_RANKING_WEIGHTS, |
50 | 54 | REPEAT_OPTIMIZATION_PROBABILITY, |
51 | 55 | TOTAL_LOOPING_TIME_EFFECTIVE, |
|
128 | 132 | from codeflash.verification.verification_utils import TestConfig |
129 | 133 |
|
130 | 134 |
|
| 135 | +def log_optimization_context(function_name: str, code_context: CodeOptimizationContext) -> None: |
| 136 | + """Log optimization context details when in verbose mode using Rich formatting.""" |
| 137 | + if logger.getEffectiveLevel() > logging.DEBUG: |
| 138 | + return |
| 139 | + |
| 140 | + console.rule() |
| 141 | + read_writable_tokens = encoded_tokens_len(code_context.read_writable_code.markdown) |
| 142 | + read_only_tokens = ( |
| 143 | + encoded_tokens_len(code_context.read_only_context_code) if code_context.read_only_context_code else 0 |
| 144 | + ) |
| 145 | + total_tokens = read_writable_tokens + read_only_tokens |
| 146 | + token_pct = min(total_tokens / OPTIMIZATION_CONTEXT_TOKEN_LIMIT, 1.0) |
| 147 | + |
| 148 | + # Token bar color based on usage |
| 149 | + bar_color = "green" if token_pct < 0.7 else "yellow" if token_pct < 0.9 else "red" |
| 150 | + |
| 151 | + # Build compact info line |
| 152 | + helper_names = [hf.qualified_name for hf in code_context.helper_functions] |
| 153 | + helpers_str = f"[magenta]{', '.join(helper_names)}[/]" if helper_names else "[dim]none[/]" |
| 154 | + read_writable_files = [str(cs.file_path) for cs in code_context.read_writable_code.code_strings] |
| 155 | + |
| 156 | + # Create a tree view for the context |
| 157 | + tree = Tree(f"[bold cyan]Context for {function_name}[/]") |
| 158 | + tree.add( |
| 159 | + Text.assemble( |
| 160 | + ("Tokens: ", "dim"), |
| 161 | + (f"{total_tokens:,}", "bold " + bar_color), |
| 162 | + (f"/{OPTIMIZATION_CONTEXT_TOKEN_LIMIT:,} ", "dim"), |
| 163 | + (f"({token_pct:.0%})", bar_color), |
| 164 | + (" [", "dim"), |
| 165 | + (f"{read_writable_tokens:,}", "green"), |
| 166 | + (" rw", "dim green"), |
| 167 | + (" + ", "dim"), |
| 168 | + (f"{read_only_tokens:,}", "yellow"), |
| 169 | + (" ro", "dim yellow"), |
| 170 | + ("]", "dim"), |
| 171 | + ) |
| 172 | + ) |
| 173 | + tree.add(f"[dim]Helpers:[/] {helpers_str}") |
| 174 | + files_branch = tree.add("[dim]Files:[/]") |
| 175 | + for f in read_writable_files: |
| 176 | + files_branch.add(f"[blue]{f}[/]") |
| 177 | + |
| 178 | + console.print(tree) |
| 179 | + |
| 180 | + console.print( |
| 181 | + Panel( |
| 182 | + Syntax(code_context.read_writable_code.markdown, "markdown", theme="monokai", word_wrap=True), |
| 183 | + title="[green]Read-Writable Code[/]", |
| 184 | + border_style="green", |
| 185 | + ) |
| 186 | + ) |
| 187 | + |
| 188 | + if code_context.read_only_context_code: |
| 189 | + console.print( |
| 190 | + Panel( |
| 191 | + Syntax(code_context.read_only_context_code, "markdown", theme="monokai", word_wrap=True), |
| 192 | + title="[yellow]Read-Only Dependencies[/]", |
| 193 | + border_style="yellow", |
| 194 | + ) |
| 195 | + ) |
| 196 | + |
| 197 | + |
131 | 198 | class CandidateNode: |
132 | 199 | __slots__ = ("candidate", "children", "parent") |
133 | 200 |
|
@@ -409,6 +476,7 @@ def can_be_optimized(self) -> Result[tuple[bool, CodeOptimizationContext, dict[P |
409 | 476 | if not is_successful(ctx_result): |
410 | 477 | return Failure(ctx_result.failure()) |
411 | 478 | code_context: CodeOptimizationContext = ctx_result.unwrap() |
| 479 | + log_optimization_context(self.function_to_optimize.function_name, code_context) |
412 | 480 | original_helper_code: dict[Path, str] = {} |
413 | 481 | helper_function_paths = {hf.file_path for hf in code_context.helper_functions} |
414 | 482 | for helper_function_path in helper_function_paths: |
|
0 commit comments