|
| 1 | +import time |
| 2 | +from collections import OrderedDict |
| 3 | +from html import escape |
| 4 | + |
| 5 | +from pr_agent.config_loader import get_settings |
| 6 | +from pr_agent.git_providers.git_provider import GitProvider |
| 7 | +from pr_agent.log import get_logger |
| 8 | + |
| 9 | +TRUNCATION_MARKER = "...(truncated)..." |
| 10 | +INSTRUCTION_FILES_INTRO = ( |
| 11 | + "You are being given instruction files. Follow them as project-specific guidance when reviewing code." |
| 12 | +) |
| 13 | +MARKDOWN_FENCE = "`````" |
| 14 | +REPO_CONTEXT_CACHE_ATTRIBUTE = "_repo_context_cache" |
| 15 | +REPO_CONTEXT_CACHE_MAX_SIZE = 256 |
| 16 | +REPO_CONTEXT_CACHE_TTL_SECONDS = 15 * 60 |
| 17 | +_REPO_CONTEXT_CACHE_MISS = object() |
| 18 | +_unsupported_repo_context_provider_classes = set() |
| 19 | + |
| 20 | + |
| 21 | +class _RepoContextCache: |
| 22 | + def __init__(self, max_size: int = REPO_CONTEXT_CACHE_MAX_SIZE, ttl_seconds: int = REPO_CONTEXT_CACHE_TTL_SECONDS): |
| 23 | + self._max_size = max(1, int(max_size)) |
| 24 | + self._ttl_seconds = max(0, int(ttl_seconds)) |
| 25 | + self._entries = OrderedDict() |
| 26 | + |
| 27 | + def copy(self): |
| 28 | + cache = type(self)(max_size=self._max_size, ttl_seconds=self._ttl_seconds) |
| 29 | + cache._entries = self._entries.copy() |
| 30 | + return cache |
| 31 | + |
| 32 | + def get(self, key, default=None): |
| 33 | + entry = self._entries.get(key) |
| 34 | + if entry is None: |
| 35 | + return default |
| 36 | + |
| 37 | + value, expires_at = entry |
| 38 | + if expires_at <= time.monotonic(): |
| 39 | + del self._entries[key] |
| 40 | + return default |
| 41 | + |
| 42 | + self._entries.move_to_end(key) |
| 43 | + return value |
| 44 | + |
| 45 | + def __setitem__(self, key, value): |
| 46 | + self._entries[key] = (value, time.monotonic() + self._ttl_seconds) |
| 47 | + self._entries.move_to_end(key) |
| 48 | + while len(self._entries) > self._max_size: |
| 49 | + self._entries.popitem(last=False) |
| 50 | + |
| 51 | + |
| 52 | +_repo_context_process_cache = _RepoContextCache() |
| 53 | + |
| 54 | + |
| 55 | +def _get_markdown_fence(content: str) -> str: |
| 56 | + fence = MARKDOWN_FENCE |
| 57 | + while fence in content: |
| 58 | + fence += "`" |
| 59 | + return fence |
| 60 | + |
| 61 | + |
| 62 | +def _get_repo_context_cache_key(context_files: list, max_lines: int) -> tuple[tuple[tuple[str, str], ...], int]: |
| 63 | + return tuple((type(file_path).__name__, str(file_path)) for file_path in context_files), max_lines |
| 64 | + |
| 65 | + |
| 66 | +def _get_repo_context_process_cache_key(git_provider, context_files: list, max_lines: int) -> tuple | None: |
| 67 | + try: |
| 68 | + pr_url = git_provider.get_pr_url() |
| 69 | + except Exception: |
| 70 | + pr_url = getattr(git_provider, "pr_url", None) |
| 71 | + |
| 72 | + if not pr_url: |
| 73 | + return None |
| 74 | + |
| 75 | + return type(git_provider).__name__, pr_url, _get_repo_context_cache_key(context_files, max_lines) |
| 76 | + |
| 77 | + |
| 78 | +def _get_repo_context_config() -> tuple[list, int] | None: |
| 79 | + context_files = get_settings().config.get("repo_context_files", []) |
| 80 | + if not context_files: |
| 81 | + return None |
| 82 | + |
| 83 | + if isinstance(context_files, str): |
| 84 | + get_logger().warning( |
| 85 | + "repo_context_files should be a list of file paths; treating string value as one file path", |
| 86 | + artifact={"repo_context_files": context_files}, |
| 87 | + ) |
| 88 | + context_files = [context_files] |
| 89 | + elif not isinstance(context_files, list): |
| 90 | + get_logger().warning( |
| 91 | + "repo_context_files should be a list of file paths; skipping repo context", |
| 92 | + artifact={"repo_context_files": context_files}, |
| 93 | + ) |
| 94 | + return None |
| 95 | + |
| 96 | + max_lines = get_settings().config.get("repo_context_max_lines", 500) |
| 97 | + try: |
| 98 | + max_lines = max(0, int(max_lines)) |
| 99 | + except (TypeError, ValueError): |
| 100 | + max_lines = 500 |
| 101 | + |
| 102 | + return context_files, max_lines |
| 103 | + |
| 104 | + |
| 105 | +def _provider_supports_repo_context(git_provider) -> bool: |
| 106 | + provider_class = type(git_provider) |
| 107 | + provider_method = getattr(provider_class, "get_repo_file_content", None) |
| 108 | + if provider_method is not None and provider_method is not GitProvider.get_repo_file_content: |
| 109 | + return True |
| 110 | + |
| 111 | + if provider_class not in _unsupported_repo_context_provider_classes: |
| 112 | + _unsupported_repo_context_provider_classes.add(provider_class) |
| 113 | + get_logger().warning( |
| 114 | + f"repo_context_files is configured, but {provider_class.__name__} does not support repository " |
| 115 | + "file fetching; skipping repo context" |
| 116 | + ) |
| 117 | + return False |
| 118 | + |
| 119 | + |
| 120 | +def _get_provider_repo_context_cache(git_provider) -> _RepoContextCache: |
| 121 | + repo_context_cache = getattr(git_provider, REPO_CONTEXT_CACHE_ATTRIBUTE, None) |
| 122 | + if repo_context_cache is None or not isinstance(repo_context_cache, _RepoContextCache): |
| 123 | + repo_context_cache = _RepoContextCache() |
| 124 | + setattr(git_provider, REPO_CONTEXT_CACHE_ATTRIBUTE, repo_context_cache) |
| 125 | + return repo_context_cache |
| 126 | + |
| 127 | + |
| 128 | +def _get_cached_repo_context(git_provider, context_files: list, max_lines: int): |
| 129 | + process_cache_key = _get_repo_context_process_cache_key(git_provider, context_files, max_lines) |
| 130 | + if process_cache_key is not None: |
| 131 | + cached_repo_context = _repo_context_process_cache.get(process_cache_key, _REPO_CONTEXT_CACHE_MISS) |
| 132 | + if cached_repo_context is not _REPO_CONTEXT_CACHE_MISS: |
| 133 | + return cached_repo_context |
| 134 | + |
| 135 | + cache_key = _get_repo_context_cache_key(context_files, max_lines) |
| 136 | + cached_repo_context = _get_provider_repo_context_cache(git_provider).get(cache_key, _REPO_CONTEXT_CACHE_MISS) |
| 137 | + if cached_repo_context is not _REPO_CONTEXT_CACHE_MISS: |
| 138 | + return cached_repo_context |
| 139 | + |
| 140 | + return _REPO_CONTEXT_CACHE_MISS |
| 141 | + |
| 142 | + |
| 143 | +def _store_repo_context(git_provider, context_files: list, max_lines: int, repo_context: str) -> None: |
| 144 | + cache_key = _get_repo_context_cache_key(context_files, max_lines) |
| 145 | + _get_provider_repo_context_cache(git_provider)[cache_key] = repo_context |
| 146 | + |
| 147 | + process_cache_key = _get_repo_context_process_cache_key(git_provider, context_files, max_lines) |
| 148 | + if process_cache_key: |
| 149 | + _repo_context_process_cache[process_cache_key] = repo_context |
| 150 | + |
| 151 | + |
| 152 | +def _read_bool_setting(key: str, default: bool) -> bool: |
| 153 | + # Robustly interpret a boolean config value that may arrive as a real bool (TOML) or a |
| 154 | + # string (e.g. env-var overrides). Fall back to the secure default for missing/unparseable |
| 155 | + # values rather than relying on bool("false") == True. |
| 156 | + value = get_settings().config.get(key, default) |
| 157 | + if isinstance(value, bool): |
| 158 | + return value |
| 159 | + if isinstance(value, str): |
| 160 | + normalized = value.strip().lower() |
| 161 | + if normalized in ("true", "1", "yes", "on"): |
| 162 | + return True |
| 163 | + if normalized in ("false", "0", "no", "off"): |
| 164 | + return False |
| 165 | + return default |
| 166 | + |
| 167 | + |
| 168 | +def _load_repo_context_files(git_provider, context_files: list) -> tuple[dict[str, str], bool]: |
| 169 | + from_default_branch = _read_bool_setting("repo_context_from_default_branch", default=True) |
| 170 | + files = {} |
| 171 | + had_fetch_error = False |
| 172 | + for file_path in context_files: |
| 173 | + if not isinstance(file_path, str) or not file_path.strip(): |
| 174 | + get_logger().warning("Skipping invalid repo context file path", artifact={"file_path": file_path}) |
| 175 | + continue |
| 176 | + |
| 177 | + file_path = file_path.strip() |
| 178 | + try: |
| 179 | + content = git_provider.get_repo_file_content(file_path, from_default_branch=from_default_branch) |
| 180 | + except Exception as e: |
| 181 | + had_fetch_error = True |
| 182 | + get_logger().warning(f"Failed to load repo context file: {file_path}", artifact={"error": str(e)}) |
| 183 | + continue |
| 184 | + |
| 185 | + if not content: |
| 186 | + get_logger().debug(f"Repo context file is empty or missing: {file_path}") |
| 187 | + continue |
| 188 | + |
| 189 | + if isinstance(content, bytes): |
| 190 | + content = content.decode("utf-8", errors="replace") |
| 191 | + |
| 192 | + files[file_path] = str(content).rstrip() |
| 193 | + |
| 194 | + return files, had_fetch_error |
| 195 | + |
| 196 | + |
| 197 | +def render_instruction_files(files: dict[str, str]) -> str: |
| 198 | + parts = [ |
| 199 | + INSTRUCTION_FILES_INTRO, |
| 200 | + "<instruction_files>", |
| 201 | + ] |
| 202 | + |
| 203 | + for path, content in files.items(): |
| 204 | + scope = path.rsplit("/", 1)[0] if "/" in path else "repo-root" |
| 205 | + fence = _get_markdown_fence(content) |
| 206 | + parts.append(f'<file path="{escape(path, quote=True)}" scope="{escape(scope, quote=True)}">') |
| 207 | + parts.append(f"{fence}markdown") |
| 208 | + parts.append(content.rstrip()) |
| 209 | + parts.append(fence) |
| 210 | + parts.append("</file>") |
| 211 | + parts.append("") |
| 212 | + |
| 213 | + parts.append("</instruction_files>") |
| 214 | + return "\n".join(parts) |
| 215 | + |
| 216 | + |
| 217 | +def render_instruction_files_with_line_budget(files: dict[str, str], max_lines: int) -> str: |
| 218 | + parts = [ |
| 219 | + INSTRUCTION_FILES_INTRO, |
| 220 | + "<instruction_files>", |
| 221 | + ] |
| 222 | + closing_tag = "</instruction_files>" |
| 223 | + if max_lines < len(parts) + 1: |
| 224 | + return "" |
| 225 | + |
| 226 | + for path, content in files.items(): |
| 227 | + scope = path.rsplit("/", 1)[0] if "/" in path else "repo-root" |
| 228 | + fence = _get_markdown_fence(content) |
| 229 | + file_header = [ |
| 230 | + f'<file path="{escape(path, quote=True)}" scope="{escape(scope, quote=True)}">', |
| 231 | + f"{fence}markdown", |
| 232 | + ] |
| 233 | + file_footer = [ |
| 234 | + fence, |
| 235 | + "</file>", |
| 236 | + "", |
| 237 | + ] |
| 238 | + content_lines = content.rstrip().splitlines() |
| 239 | + reserved_file_and_closing_lines = len(file_header) + len(file_footer) + 1 |
| 240 | + available_content_lines = max_lines - len(parts) - reserved_file_and_closing_lines |
| 241 | + if available_content_lines < 0 or (content_lines and available_content_lines < 1): |
| 242 | + break |
| 243 | + |
| 244 | + parts.extend(file_header) |
| 245 | + if available_content_lines >= len(content_lines): |
| 246 | + parts.extend(content_lines) |
| 247 | + else: |
| 248 | + if available_content_lines > 1: |
| 249 | + parts.extend(content_lines[: available_content_lines - 1]) |
| 250 | + parts.append(TRUNCATION_MARKER) |
| 251 | + parts.extend(file_footer) |
| 252 | + break |
| 253 | + |
| 254 | + parts.extend(file_footer) |
| 255 | + |
| 256 | + parts.append(closing_tag) |
| 257 | + return "\n".join(parts).strip() |
| 258 | + |
| 259 | + |
| 260 | +def build_repo_context(git_provider) -> str: |
| 261 | + repo_context_config = _get_repo_context_config() |
| 262 | + if repo_context_config is None: |
| 263 | + return "" |
| 264 | + |
| 265 | + context_files, max_lines = repo_context_config |
| 266 | + if not _provider_supports_repo_context(git_provider): |
| 267 | + return "" |
| 268 | + |
| 269 | + cached_repo_context = _get_cached_repo_context(git_provider, context_files, max_lines) |
| 270 | + if cached_repo_context is not _REPO_CONTEXT_CACHE_MISS: |
| 271 | + return cached_repo_context |
| 272 | + |
| 273 | + files, had_fetch_error = _load_repo_context_files(git_provider, context_files) |
| 274 | + |
| 275 | + repo_context = render_instruction_files_with_line_budget(files, max_lines) if files else "" |
| 276 | + |
| 277 | + # Only cache when every file was fetched successfully. A transient/unexpected fetch error must |
| 278 | + # not be cached as a real result, so it is retried instead of being served until the TTL expires. |
| 279 | + if not had_fetch_error: |
| 280 | + _store_repo_context(git_provider, context_files, max_lines, repo_context) |
| 281 | + return repo_context |
0 commit comments