refactor(aggregate): consolidate build_markdown and remove redundant file building

This commit is contained in:
2026-05-10 10:13:56 -04:00
parent 6e62b4867f
commit 76f4e1be4f
2 changed files with 5 additions and 58 deletions
+2 -56
View File
@@ -77,34 +77,6 @@ def build_discussion_section(history: list[Any]) -> str:
sections.append(f"### Discussion Excerpt {i}\n\n{text}") sections.append(f"### Discussion Excerpt {i}\n\n{text}")
return "\n\n---\n\n".join(sections) return "\n\n---\n\n".join(sections)
def build_files_section(base_dir: Path, files: list[str | dict[str, Any]]) -> str:
"""
[C: tests/test_tiered_context.py:test_build_files_section_with_dicts]
"""
sections = []
for entry_raw in files:
if isinstance(entry_raw, dict):
entry = cast(str, entry_raw.get("path", ""))
else:
entry = entry_raw
if not entry or not isinstance(entry, str):
continue
paths = resolve_paths(base_dir, entry)
if not paths:
sections.append(f"### `{entry}`\n\n```text\nERROR: no files matched: {entry}\n```")
continue
for path in paths:
suffix = path.suffix.lstrip(".")
lang = suffix if suffix else "text"
try:
content = path.read_text(encoding="utf-8")
except FileNotFoundError:
content = f"ERROR: file not found: {path}"
except Exception as e:
content = f"ERROR: {e}"
original = entry if "*" not in entry else str(path)
sections.append(f"### `{original}`\n\n```{lang}\n{content}\n```")
return "\n\n---\n\n".join(sections)
def build_screenshots_section(base_dir: Path, screenshots: list[str]) -> str: def build_screenshots_section(base_dir: Path, screenshots: list[str]) -> str:
sections = [] sections = []
@@ -181,16 +153,6 @@ def build_file_items(base_dir: Path, files: list[str | dict[str, Any]]) -> list[
items.append({"path": path, "entry": entry, "content": content, "error": error, "mtime": mtime, "tier": tier, "auto_aggregate": auto_aggregate, "force_full": force_full}) items.append({"path": path, "entry": entry, "content": content, "error": error, "mtime": mtime, "tier": tier, "auto_aggregate": auto_aggregate, "force_full": force_full})
return items return items
def build_summary_section(base_dir: Path, files: list[str | dict[str, Any]]) -> str:
"""
Build a compact summary section using summarize.py — one short block per file.
Used as the initial <context> block instead of full file contents.
"""
with get_monitor().scope("build_summary_section"):
items = build_file_items(base_dir, files)
return summarize.build_summary_markdown(items)
def _build_files_section_from_items(file_items: list[dict[str, Any]]) -> str: def _build_files_section_from_items(file_items: list[dict[str, Any]]) -> str:
""" """
@@ -332,24 +294,8 @@ def build_tier3_context(file_items: list[dict[str, Any]], screenshot_base_dir: P
return "\n\n---\n\n".join(parts) return "\n\n---\n\n".join(parts)
def build_markdown(base_dir: Path, files: list[str | dict[str, Any]], screenshot_base_dir: Path, screenshots: list[str], history: list[str], summary_only: bool = False, execution_mode: str = "standard") -> str: def build_markdown(base_dir: Path, files: list[str | dict[str, Any]], screenshot_base_dir: Path, screenshots: list[str], history: list[str], summary_only: bool = False, execution_mode: str = "standard") -> str:
with get_monitor().scope("build_markdown"): file_items = build_file_items(base_dir, files)
parts = [] return build_markdown_from_items(file_items, screenshot_base_dir, screenshots, history, summary_only=summary_only, aggregation_strategy='auto', execution_mode=execution_mode, base_dir=base_dir)
# STATIC PREFIX: Files and Screenshots must go first to maximize Cache Hits
if files:
if summary_only:
parts.append("## Files (Summary)\n\n" + build_summary_section(base_dir, files))
else:
parts.append("## Files\n\n" + build_files_section(base_dir, files))
if screenshots:
parts.append("## Screenshots\n\n" + build_screenshots_section(screenshot_base_dir, screenshots))
if execution_mode == "beads":
beads_md = build_beads_section(base_dir)
if beads_md:
parts.append(beads_md)
# DYNAMIC SUFFIX: History changes every turn, must go last
if history:
parts.append("## Discussion History\n\n" + build_discussion_section(history))
return "\n\n---\n\n".join(parts)
def run(config: dict[str, Any], aggregation_strategy: str = "auto") -> tuple[str, Path, list[dict[str, Any]]]: def run(config: dict[str, Any], aggregation_strategy: str = "auto") -> tuple[str, Path, list[dict[str, Any]]]:
""" """
+3 -2
View File
@@ -59,13 +59,14 @@ def test_build_file_items_with_tiers(tmp_path: Any) -> None:
assert item2["tier"] == 3 assert item2["tier"] == 3
def test_build_files_section_with_dicts(tmp_path: Any) -> None: def test_build_files_section_with_dicts(tmp_path: Any) -> None:
from src.aggregate import build_files_section from src.aggregate import _build_files_section_from_items, build_file_items
file1 = tmp_path / "file1.txt" file1 = tmp_path / "file1.txt"
file1.write_text("content1") file1.write_text("content1")
files_config = [ files_config = [
{"path": str(file1)} {"path": str(file1)}
] ]
result = build_files_section(tmp_path, files_config) items = build_file_items(tmp_path, files_config)
result = _build_files_section_from_items(items)
assert "content1" in result assert "content1" in result
assert "file1.txt" in result assert "file1.txt" in result