| from __future__ import annotations | |
| from pathlib import Path | |
| from .types import TaskSpec | |
| def discover_tasks(base_tasks: Path, *, task_glob: str, task_ids: set[str] | None) -> list[TaskSpec]: | |
| tasks_root = base_tasks / "tasks" | |
| if not tasks_root.is_dir(): | |
| raise FileNotFoundError(f"tasks directory not found: {tasks_root}") | |
| script_root = find_script_root(base_tasks) | |
| specs: list[TaskSpec] = [] | |
| for task_dir in sorted(path for path in tasks_root.glob(task_glob) if path.is_dir()): | |
| task_id = task_dir.name | |
| if task_ids is not None and task_id not in task_ids: | |
| continue | |
| env_builder_path = task_dir / "env_builder.py" | |
| if not env_builder_path.is_file(): | |
| continue | |
| prompt_path = find_prompt_path(tasks_root, task_dir, task_id) | |
| verifier_path = find_verifier_path(script_root, task_id) if script_root is not None else None | |
| specs.append( | |
| TaskSpec( | |
| task_id=task_id, | |
| task_dir=task_dir, | |
| prompt_path=prompt_path, | |
| env_builder_path=env_builder_path, | |
| verifier_path=verifier_path, | |
| ) | |
| ) | |
| if task_ids is not None: | |
| found_ids = {spec.task_id for spec in specs} | |
| missing_ids = sorted(task_ids - found_ids) | |
| if missing_ids: | |
| raise FileNotFoundError(f"requested task ids not found or missing env_builder.py: {missing_ids}") | |
| if not specs: | |
| raise FileNotFoundError(f"no task directories matched {task_glob!r} under {tasks_root}") | |
| return specs | |
| def find_script_root(base_tasks: Path) -> Path | None: | |
| for dirname in ("scrips", "scripts"): | |
| candidate = base_tasks / dirname | |
| if candidate.is_dir(): | |
| return candidate | |
| return None | |
| def find_prompt_path(tasks_root: Path, task_dir: Path, task_id: str) -> Path: | |
| candidates = ( | |
| tasks_root / "prompts" / f"{task_id}.md", | |
| task_dir / "prompt.md", | |
| task_dir / "task.md", | |
| ) | |
| for candidate in candidates: | |
| if candidate.is_file(): | |
| return candidate | |
| raise FileNotFoundError( | |
| f"prompt file not found for {task_id}; tried: " | |
| + ", ".join(str(path) for path in candidates) | |
| ) | |
| def find_verifier_path(script_root: Path, task_id: str) -> Path | None: | |
| candidates = ( | |
| script_root / task_id / "verify_workplace.py", | |
| script_root / f"{task_id}.py", | |
| script_root / "verify_workplace.py", | |
| ) | |
| for candidate in candidates: | |
| if candidate.is_file(): | |
| return candidate | |
| return None | |