Fix prompt template path listing when CWD differs from project root#470
Fix prompt template path listing when CWD differs from project root#470simonrosenberg wants to merge 2 commits intomainfrom
Conversation
`p.relative_to(Path.cwd())` raises ValueError when the current working directory is not an ancestor of the prompt directory. Use `str(p)` with the already-resolved absolute path instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
all-hands-bot
left a comment
There was a problem hiding this comment.
🟡 Acceptable - Fixes a real bug but there's a cleaner solution.
VERDICT: ✅ Worth merging - Core logic is sound, improvement suggested
KEY INSIGHT: You're solving the right problem (CWD fragility) but using absolute paths in help text degrades UX when a simpler solution exists.
benchmarks/swebench/run_infer.py
Outdated
| def main() -> None: | ||
| prompt_dir = (Path(__file__).parent / "prompts").resolve() | ||
| choices = [str(p.relative_to(Path.cwd())) for p in prompt_dir.glob("*.j2")] | ||
| choices = [str(p) for p in prompt_dir.glob("*.j2")] |
There was a problem hiding this comment.
🟢 Code Smell: This identical change in 6 files suggests you could extract get_prompt_choices(prompt_dir) to a shared utility in benchmarks/utils/. Not critical, but reduces duplication and makes future fixes easier.
all-hands-bot
left a comment
There was a problem hiding this comment.
Additional observation on code structure:
| def main() -> None: | ||
| prompt_dir = (Path(__file__).parent / "prompts").resolve() | ||
| choices = [str(p.relative_to(Path.cwd())) for p in prompt_dir.glob("*.j2")] | ||
| choices = [str(p) for p in prompt_dir.glob("*.j2")] |
There was a problem hiding this comment.
🟢 Nit - Code Duplication: This exact pattern (prompt directory setup + choices list + argparse) appears identically in all 6 benchmark files. Consider extracting to benchmarks/utils/prompts.py:
def get_prompt_arg_parser(script_file: Path) -> tuple[list[str], Path]:
"""Get prompt choices and default path for argparse."""
prompt_dir = (script_file.parent / "prompts").resolve()
choices = [p.name for p in prompt_dir.glob("*.j2")]
default = prompt_dir / "default.j2"
assert default.exists(), f"Default prompt {default} not found"
return choices, defaultNot critical for this PR, but would make the next fix easier (you'd update one place instead of six).
Co-authored-by: OpenHands Bot <contact@all-hands.dev>
Summary
ValueErrorin prompt template choice listing when the current working directory is not the project rootp.relative_to(Path.cwd())→str(p)in all 6run_infer.pyfilesstr(p)gives a reliable absolute path regardless of CWDFiles changed
benchmarks/commit0/run_infer.pyp.relative_to(Path.cwd())→str(p)benchmarks/multiswebench/run_infer.pyp.relative_to(Path.cwd())→str(p)benchmarks/swebench/run_infer.pyp.relative_to(Path.cwd())→str(p)benchmarks/swebenchmultimodal/run_infer.pyp.relative_to(Path.cwd())→str(p)benchmarks/swefficiency/run_infer.pyp.relative_to(Path.cwd())→str(p)benchmarks/swtbench/run_infer.pyp.relative_to(Path.cwd())→str(p)Context
Extracted from #455. The old code used
p.relative_to(Path.cwd())to build the--prompt-templateargparse choices list. This raisesValueErrorwhen CWD is not an ancestor of the prompt directory (e.g., when running benchmarks as an installed package or from a different directory).Validation
All benchmarks pass with this change — see CI results on #455:
Test plan
--prompt-templateargument still works with the listed choices🤖 Generated with Claude Code