Review a generated context builder

from AI coding agents
Python 3.14 intermediate 9 min 4 issues to find

Review this generated context builder before an agent uses it.

Assemble requested UTF-8 Python files for a coding agent, keep every read inside the workspace, reject a negative character budget, stop at the budget without reading unnecessary data, and report only files included in full.

Python
from dataclasses import dataclass
from pathlib import Path

@dataclass(frozen=True)
class ContextBundle:
    text: str
    files: tuple[str, ...]

def build_context(
    workspace: Path, requested: list[str], character_budget: int
) -> ContextBundle:
    sections: list[str] = []
    included: list[str] = []

    for name in requested:
        candidate = workspace / name
        if candidate.suffix != ".py":
            continue
        source = candidate.read_text(encoding="utf-8")
        sections.append(f"### {name}\n{source}")
        included.append(name)

    context = "\n\n".join(sections)
    context = context[:character_budget]
    return ContextBundle(context, tuple(included))

generated code is illustrative, not from any one model

Open in playground
Report an error