diff --git a/agent/context_references.py b/agent/context_references.py index 7ecb90c49..50a33a1d7 100644 --- a/agent/context_references.py +++ b/agent/context_references.py @@ -483,9 +483,7 @@ def _rg_files(path: Path, cwd: Path, limit: int) -> list[Path] | None: text=True, timeout=10, ) - except FileNotFoundError: - return None - except subprocess.TimeoutExpired: + except (FileNotFoundError, OSError, subprocess.TimeoutExpired): return None if result.returncode != 0: return None diff --git a/tests/agent/test_context_references.py b/tests/agent/test_context_references.py index ea5579c56..02456d064 100644 --- a/tests/agent/test_context_references.py +++ b/tests/agent/test_context_references.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio import subprocess from pathlib import Path +from unittest.mock import patch import pytest @@ -124,6 +125,31 @@ def test_expand_file_range_and_folder_listing(sample_repo: Path): assert not result.warnings +def test_folder_listing_falls_back_when_rg_is_blocked(sample_repo: Path): + from agent.context_references import preprocess_context_references + + real_run = subprocess.run + + def blocked_rg(*args, **kwargs): + cmd = args[0] if args else kwargs.get("args") + if isinstance(cmd, list) and cmd and cmd[0] == "rg": + raise PermissionError("rg blocked by policy") + return real_run(*args, **kwargs) + + with patch("agent.context_references.subprocess.run", side_effect=blocked_rg): + result = preprocess_context_references( + "Review @folder:src/", + cwd=sample_repo, + context_length=100_000, + ) + + assert result.expanded + assert "src/" in result.message + assert "main.py" in result.message + assert "helper.py" in result.message + assert not result.warnings + + def test_expand_quoted_file_reference_with_spaces(tmp_path: Path): from agent.context_references import preprocess_context_references