Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/subdirectory_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _add_path_candidate(self, raw_path: str, candidates: Set[Path]):
if parent == p:
break # filesystem root
p = parent
except (OSError, ValueError):
except (OSError, ValueError, RuntimeError):
pass

def _extract_paths_from_command(self, cmd: str, candidates: Set[Path]):
Expand Down Expand Up @@ -245,7 +245,7 @@ def _load_hints_for_directory(self, directory: Path) -> Optional[str]:
try:
rel_path = str(hint_path.relative_to(Path.home()))
rel_path = "~/" + rel_path
except ValueError:
except (ValueError, RuntimeError):
pass # keep absolute
found_hints.append((rel_path, content))
# First match wins per directory (like startup loading)
Expand Down
35 changes: 35 additions & 0 deletions tests/agent/test_subdirectory_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,38 @@ def test_is_valid_subdir_rejects_sibling_dir(self, tmp_path, project):
outside.mkdir(exist_ok=True)
tracker = SubdirectoryHintTracker(working_dir=str(project))
assert tracker._is_valid_subdir(outside) is False

def test_expanduser_runtime_error_when_home_unset(self, project):
"""_add_path_candidate should not crash when $HOME is unset (Python 3.11+).

Path.expanduser() raises RuntimeError when $HOME is unset on some
platforms. The except clause must catch it.
"""
tracker = SubdirectoryHintTracker(working_dir=str(project))
with patch("pathlib.Path.expanduser", side_effect=RuntimeError("Could not determine home directory")):
# Should not raise β€” gracefully skip the path
candidates = set()
tracker._add_path_candidate("~/some/path", candidates)
assert candidates == set()

def test_path_home_runtime_error_in_relative_display(self, project):
"""Relative path display should not crash when Path.home() raises RuntimeError.

When $HOME is unset, Path.home() raises RuntimeError on some platforms.
The relative-path fallback must catch it and keep the absolute path.
"""
tracker = SubdirectoryHintTracker(working_dir=str(project))
# Create a hint file in a subdirectory
subdir = project / "sub"
subdir.mkdir()
(subdir / "AGENTS.md").write_text("sub hints")
# Read a file in that subdirectory to trigger hint loading
(subdir / "main.py").write_text("print('hello')")
with patch("pathlib.Path.home", side_effect=RuntimeError("Could not determine home directory")):
# Should not raise β€” gracefully falls back to absolute path
result = tracker.check_tool_call(
"read_file", {"path": str(subdir / "main.py")}
)
# Result should still contain the hint content
assert result is not None
assert "sub hints" in result
Loading