from __future__ import annotations import subprocess from pathlib import Path from control_plane.projects.models import Decision, Task class WorkerContextBuilder: def build_for_task(self, task: Task, worktree_path: Path | None = None) -> dict[str, object]: decisions = Decision.objects.filter(project=task.project).order_by("-created_at")[:10] files: dict[str, str] = {} tests: dict[str, str] = {} current_diff = "" if worktree_path is not None: safe_root = worktree_path.resolve() for path in sorted(safe_root.rglob("*.py"))[:40]: if ".git" in path.parts or "__pycache__" in path.parts or "migrations" in path.parts: continue relative = path.relative_to(safe_root).as_posix() content = path.read_text(encoding="utf-8") if "SECRET" in relative.upper() or ".env" in relative: continue if relative.startswith("tests") or "test" in path.name: tests[relative] = content[:4000] else: files[relative] = content[:4000] current_diff = subprocess.run( ["git", "diff", "--", "."], cwd=safe_root, check=False, capture_output=True, text=True, ).stdout[:12000] context = { "task": { "id": str(task.id), "goal": task.goal, "acceptance_criteria": task.acceptance_criteria, }, "architecture_summary": task.project.architecture_summary, "decisions": [decision.decision for decision in decisions], "files": files, "tests": tests, "current_diff": current_diff, } return context