|
8 | 8 |
|
9 | 9 | from kaievolve.config import PromptConfig |
10 | 10 | from kaievolve.prompt.templates import TemplateManager |
| 11 | +from kaievolve.utils.code_utils import parse_evolve_blocks |
11 | 12 | from kaievolve.utils.format_utils import format_metrics_safe |
12 | 13 | from kaievolve.utils.metrics_utils import safe_numeric_average |
13 | 14 |
|
@@ -258,6 +259,41 @@ def _format_program_summary(self, program: Dict[str, Any]) -> str: |
258 | 259 | # Trailing newline so the next template line (code fence, etc.) starts cleanly. |
259 | 260 | return "\n".join(lines) + "\n" |
260 | 261 |
|
| 262 | + def _reference_snippet(self, code: str) -> str: |
| 263 | + """Render a reference program's code for the prompt history (issue #6). |
| 264 | +
|
| 265 | + The parent being evolved is shown in full elsewhere; the top/diverse/ |
| 266 | + inspiration programs are reference *examples*. Under the EVOLVE-BLOCK |
| 267 | + contract only the marked block(s) ever differ between programs -- the |
| 268 | + surrounding scaffold is identical to the parent and already in the |
| 269 | + prompt. So when ``reference_evolve_blocks_only`` is set we show only the |
| 270 | + evolve block(s), eliding the unchanged scaffold with a short marker. |
| 271 | +
|
| 272 | + This drops the redundant boilerplate (lower token cost, room for more |
| 273 | + diverse examples) without ever trimming the part that actually changed, |
| 274 | + and is bounded by block size rather than file size -- so it stays safe |
| 275 | + for large / multi-file programs. Programs with no markers (full-rewrite |
| 276 | + tasks) are returned unchanged. |
| 277 | + """ |
| 278 | + if not getattr(self.config, "reference_evolve_blocks_only", True): |
| 279 | + return code |
| 280 | + blocks = parse_evolve_blocks(code) |
| 281 | + if not blocks: |
| 282 | + return code # no markers (e.g. full-rewrite task) -> show in full |
| 283 | + lines = code.split("\n") |
| 284 | + out: List[str] = [] |
| 285 | + cursor = 0 |
| 286 | + for start_line, end_line, _content in blocks: |
| 287 | + if start_line > cursor: |
| 288 | + out.append("# ... (unchanged scaffold omitted) ...") |
| 289 | + # Keep the block including its START/END markers so the evolved |
| 290 | + # region is unambiguous. |
| 291 | + out.extend(lines[start_line : end_line + 1]) |
| 292 | + cursor = end_line + 1 |
| 293 | + if cursor < len(lines): |
| 294 | + out.append("# ... (unchanged scaffold omitted) ...") |
| 295 | + return "\n".join(out) |
| 296 | + |
261 | 297 | def _format_evolution_history( |
262 | 298 | self, |
263 | 299 | previous_programs: List[Dict[str, Any]], |
@@ -340,8 +376,9 @@ def _format_evolution_history( |
340 | 376 | selected_top = top_programs[: min(self.config.num_top_programs, len(top_programs))] |
341 | 377 |
|
342 | 378 | for i, program in enumerate(selected_top): |
343 | | - # Use the full program code |
344 | | - program_code = program.get("code", "") |
| 379 | + # Reference example: show only the evolved block(s); the unchanged |
| 380 | + # scaffold is already in the (full) parent program above. |
| 381 | + program_code = self._reference_snippet(program.get("code", "")) |
345 | 382 |
|
346 | 383 | # Calculate a composite score using safe numeric average |
347 | 384 | score = safe_numeric_average(program.get("metrics", {})) |
@@ -391,8 +428,9 @@ def _format_evolution_history( |
391 | 428 | diverse_programs_str += "\n\n## Diverse Programs\n\n" |
392 | 429 |
|
393 | 430 | for i, program in enumerate(diverse_programs): |
394 | | - # Use the full program code |
395 | | - program_code = program.get("code", "") |
| 431 | + # Reference example: show only the evolved block(s); the |
| 432 | + # unchanged scaffold is already in the (full) parent above. |
| 433 | + program_code = self._reference_snippet(program.get("code", "")) |
396 | 434 |
|
397 | 435 | # Calculate a composite score using safe numeric average |
398 | 436 | score = safe_numeric_average(program.get("metrics", {})) |
@@ -457,8 +495,9 @@ def _format_inspirations_section( |
457 | 495 | inspiration_programs_str = "" |
458 | 496 |
|
459 | 497 | for i, program in enumerate(inspirations): |
460 | | - # Use the full program code |
461 | | - program_code = program.get("code", "") |
| 498 | + # Reference example: show only the evolved block(s); the unchanged |
| 499 | + # scaffold is already in the (full) parent program above. |
| 500 | + program_code = self._reference_snippet(program.get("code", "")) |
462 | 501 |
|
463 | 502 | # Calculate a composite score using safe numeric average |
464 | 503 | score = safe_numeric_average(program.get("metrics", {})) |
|
0 commit comments