Spaces:
Runtime error
Runtime error
File size: 64,326 Bytes
60b97da 24f9940 60b97da 538d769 60b97da 0815510 f1089a9 60b97da f1089a9 0815510 60b97da 24f9940 60b97da 24f9940 538d769 24f9940 538d769 24f9940 538d769 60b97da 24f9940 60b97da 24f9940 60b97da 24f9940 538d769 24f9940 538d769 24f9940 538d769 24f9940 538d769 24f9940 538d769 60b97da 0815510 60b97da f1089a9 60b97da f1089a9 60b97da f1089a9 60b97da f1089a9 60b97da f1089a9 0815510 f1089a9 60b97da f1089a9 60b97da f1089a9 60b97da f1089a9 60b97da 0815510 60b97da 0815510 60b97da 0815510 60b97da 0815510 f1089a9 0815510 60b97da f1089a9 60b97da 0815510 f1089a9 60b97da 0815510 f1089a9 0815510 60b97da 0815510 60b97da 0815510 60b97da 0815510 60b97da f1089a9 60b97da 0815510 60b97da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 | import os
import re
from datetime import datetime, timedelta
from typing import Dict, List, Optional
from openai import OpenAI
from src.code_parser import CodeParser
from src.bedrock_claude import create_bedrock_runtime_client, generate_bedrock_claude_text
from src.database import Repository, get_db_session, init_db, resolve_database_url
from src.embeddings import EmbeddingGenerator
from src.hybrid_search import HybridSearchEngine
from src.repo_fetcher import RepoFetcher
from src.vector_store import QdrantVectorStore
class SessionCancelledError(RuntimeError):
pass
class CodebaseRAGSystem:
def __init__(
self,
database_url: str = None,
repo_dir: str = None,
index_path: str = None,
):
self.database_url = database_url or os.getenv(
"DATABASE_URL", "sqlite:///./codebase_rag.db"
)
self.database_url = resolve_database_url(self.database_url)
init_db(self.database_url)
print(f"[database] Using database_url={self.database_url}", flush=True)
self.repo_fetcher = RepoFetcher(base_dir=repo_dir)
self.parser = CodeParser()
self.embedder = EmbeddingGenerator()
self.vector_store = QdrantVectorStore(
embedding_dim=self.embedder.get_embedding_dim(),
index_path=index_path or "./data/faiss/codebase_index",
persist=False,
)
self.hybrid_search = HybridSearchEngine(
reranker_model=os.getenv(
"RERANKER_MODEL", "cross-encoder/ms-marco-MiniLM-L-6-v2"
)
)
self.app_env = os.getenv("APP_ENV", os.getenv("ENVIRONMENT", "local")).lower()
self.llm_provider = os.getenv("LLM_PROVIDER", "bedrock").lower()
self.llm_client = None
self.llm_model = ""
self._configure_llm()
self.session_ttl_minutes = int(os.getenv("SESSION_TTL_MINUTES", "120"))
self.indexing_progress: Dict[int, dict] = {}
self.repo_chunks: Dict[int, List[dict]] = {}
self.cancelled_repo_ids = set()
self.rebuild_indexes()
def rebuild_indexes(self):
session = get_db_session(self.database_url)
try:
self.vector_store.clear()
self.repo_chunks.clear()
self.indexing_progress.clear()
self.cancelled_repo_ids.clear()
repos = session.query(Repository).all()
self._delete_repositories(session, repos, track_cancellation=False)
self.cancelled_repo_ids.clear()
session.commit()
finally:
session.close()
def create_or_reset_repository(self, github_url: str, session_key: str) -> Repository:
info = self.repo_fetcher.parse_github_url(github_url)
registry_key = self._build_registry_key(session_key, github_url)
session = get_db_session(self.database_url)
try:
self._cleanup_expired_sessions(session)
repo = session.query(Repository).filter_by(github_url=registry_key).first()
if repo is None:
repo = Repository(
github_url=registry_key,
source_url=github_url,
session_key=session_key,
session_expires_at=self._session_expiry(),
owner=info["owner"],
name=info["repo"],
branch=info["branch"],
status="queued",
)
session.add(repo)
session.flush()
self.cancelled_repo_ids.discard(repo.id)
else:
repo.source_url = github_url
repo.session_key = session_key
repo.session_expires_at = self._session_expiry()
repo.owner = info["owner"]
repo.name = info["repo"]
repo.branch = info["branch"]
repo.status = "queued"
repo.error_message = None
repo.file_count = 0
repo.chunk_count = 0
repo.indexed_at = None
self.cancelled_repo_ids.discard(repo.id)
self.hybrid_search.remove_repository(repo.id)
self.vector_store.remove_repository(repo.id)
self.repo_chunks.pop(repo.id, None)
session.commit()
session.refresh(repo)
return repo
finally:
session.close()
def index_repository(self, repo_id: int):
session = get_db_session(self.database_url)
try:
self._cleanup_expired_sessions(session)
repo = session.query(Repository).filter_by(id=repo_id).first()
if repo is None:
raise ValueError("Repository not found")
self._ensure_repo_not_cancelled(repo.id)
print(f"[indexing] Starting repository index repo_id={repo.id}", flush=True)
repo.status = "indexing"
repo.error_message = None
repo.session_expires_at = self._session_expiry()
session.commit()
self._set_progress(repo.id, phase="cloning", message="Cloning repository")
clone_info = self.repo_fetcher.clone_repository(repo.source_url or repo.github_url)
self._ensure_repo_not_cancelled(repo.id)
repo.local_path = None
repo.branch = clone_info["branch"]
print(
f"[indexing] Repository cloned repo_id={repo.id} branch={repo.branch} "
f"path={clone_info['local_path']}",
flush=True,
)
source_files = list(self.repo_fetcher.iter_source_files(clone_info["local_path"]))
total_files = len(source_files)
print(
f"[indexing] Found {total_files} source files for repo_id={repo.id}",
flush=True,
)
self._set_progress(
repo.id,
phase="parsing",
message=f"Scanning {total_files} source files",
total_files=total_files,
processed_files=0,
discovered_chunks=0,
)
chunk_payloads = []
file_count = 0
for index, file_path in enumerate(source_files, start=1):
file_chunks = self.parser.chunk_file(str(file_path), clone_info["local_path"])
if not file_chunks:
self._set_progress(
repo.id,
phase="parsing",
message=f"Parsed {index}/{total_files} files",
total_files=total_files,
processed_files=index,
discovered_chunks=len(chunk_payloads),
)
continue
file_count += 1
chunk_payloads.extend(file_chunks)
self._set_progress(
repo.id,
phase="parsing",
message=f"Parsed {index}/{total_files} files",
total_files=total_files,
processed_files=index,
discovered_chunks=len(chunk_payloads),
)
searchable_texts = [chunk["searchable_text"] for chunk in chunk_payloads]
print(
f"[indexing] Parsed repo_id={repo.id} files={file_count} chunks={len(searchable_texts)}",
flush=True,
)
self._set_progress(
repo.id,
phase="embedding",
message=f"Embedding {len(searchable_texts)} chunks",
total_files=total_files,
processed_files=total_files,
discovered_chunks=len(chunk_payloads),
total_chunks=len(chunk_payloads),
embedded_chunks=0,
)
embeddings = self.embedder.embed_batch(
searchable_texts,
progress_callback=lambda completed, total: self._set_progress(
repo.id,
phase="embedding",
message=f"Embedding chunks ({completed}/{total})",
total_files=total_files,
processed_files=total_files,
discovered_chunks=len(chunk_payloads),
total_chunks=total,
embedded_chunks=completed,
),
)
self._ensure_repo_not_cancelled(repo.id)
vector_metadata = []
for chunk in chunk_payloads:
vector_metadata.append(
{
"repository_id": repo.id,
"file_path": chunk["file_path"],
"language": chunk["language"],
"symbol_name": chunk["symbol_name"],
"symbol_type": chunk["symbol_type"],
"line_start": chunk["line_start"],
"line_end": chunk["line_end"],
"signature": chunk["signature"],
"content": chunk["content"],
}
)
embedding_ids = self.vector_store.add_embeddings(embeddings, vector_metadata)
print(
f"[indexing] Uploaded {len(embedding_ids)} embeddings to vector store for repo_id={repo.id}",
flush=True,
)
self._set_progress(
repo.id,
phase="saving",
message="Saving chunks and search indexes",
total_files=total_files,
processed_files=total_files,
discovered_chunks=len(chunk_payloads),
)
created_rows = []
for chunk, embedding_id in zip(chunk_payloads, embedding_ids):
row = {
**chunk,
"id": embedding_id,
"repository_id": repo.id,
"embedding_id": embedding_id,
}
created_rows.append(row)
repo.status = "indexed"
repo.file_count = file_count
repo.chunk_count = len(created_rows)
repo.indexed_at = datetime.utcnow()
repo.session_expires_at = self._session_expiry()
self._ensure_repo_still_exists(session, repo.id)
self._ensure_repo_not_cancelled(repo.id)
session.commit()
serialized = [self._serialize_chunk(chunk) for chunk in created_rows]
self.repo_chunks[repo.id] = serialized
self.vector_store.save()
self.indexing_progress.pop(repo.id, None)
self.cancelled_repo_ids.discard(repo.id)
self.repo_fetcher.cleanup_repository(clone_info["local_path"])
print(f"[indexing] Repository index complete repo_id={repo.id}", flush=True)
except Exception as exc:
print(f"[indexing] Repository index failed repo_id={repo_id} error={exc}", flush=True)
session.rollback()
self.vector_store.remove_repository(repo_id)
self.repo_chunks.pop(repo_id, None)
self.hybrid_search.remove_repository(repo_id)
repo = session.query(Repository).filter_by(id=repo_id).first()
if repo:
if repo_id in self.cancelled_repo_ids:
session.delete(repo)
else:
repo.status = "failed"
repo.error_message = str(exc)
session.commit()
try:
if "clone_info" in locals():
self.repo_fetcher.cleanup_repository(clone_info["local_path"])
except Exception:
pass
self.indexing_progress.pop(repo_id, None)
if isinstance(exc, SessionCancelledError):
return
raise
finally:
session.close()
def list_repositories(self) -> List[dict]:
raise NotImplementedError
def list_repositories_for_session(self, session_key: str) -> List[dict]:
session = get_db_session(self.database_url)
try:
self._cleanup_expired_sessions(session)
repos = (
session.query(Repository)
.filter_by(session_key=session_key)
.order_by(Repository.updated_at.desc())
.all()
)
self._touch_session(session, session_key)
return [self._serialize_repo(repo) for repo in repos]
finally:
session.close()
def get_repository(self, repo_id: int) -> Optional[dict]:
raise NotImplementedError
def get_repository_for_session(self, repo_id: int, session_key: str) -> Optional[dict]:
session = get_db_session(self.database_url)
try:
self._cleanup_expired_sessions(session)
repo = (
session.query(Repository)
.filter_by(id=repo_id, session_key=session_key)
.first()
)
self._touch_session(session, session_key)
return self._serialize_repo(repo) if repo else None
finally:
session.close()
def answer_question(
self,
repo_id: int,
session_key: str,
question: str,
top_k: int = 8,
history: Optional[List[object]] = None,
) -> dict:
session = get_db_session(self.database_url)
try:
self._cleanup_expired_sessions(session)
repo = (
session.query(Repository)
.filter_by(id=repo_id, session_key=session_key)
.first()
)
if repo is None:
raise ValueError("Repository not found")
if repo.status != "indexed":
raise ValueError("Repository is not ready for questions yet")
if repo_id not in self.repo_chunks:
raise ValueError("Session cache expired. Re-index the repository and try again.")
self._touch_session(session, session_key)
normalized_history = self._normalize_history(history or [])
question_intent = self._question_intent(question)
deep_search_intents = {
"api",
"implementation",
"cross_file",
"error_handling",
"setup",
"tests",
}
deep_multiplier = int(os.getenv("RAG_DEEP_SEARCH_MULTIPLIER", "8"))
shallow_multiplier = int(os.getenv("RAG_SEARCH_MULTIPLIER", "4"))
search_depth = (
top_k * deep_multiplier
if question_intent in deep_search_intents
else top_k * shallow_multiplier
)
search_depth = max(top_k, min(search_depth, 120))
retrieval_query = self._build_retrieval_query(question, normalized_history)
query_embedding = self.embedder.embed_text(retrieval_query)
semantic_hits = []
for score, meta in self.vector_store.search(query_embedding, k=search_depth, repo_filter=repo_id):
serialized = dict(meta)
serialized["semantic_score"] = score
semantic_hits.append(serialized)
lexical_hits = self.hybrid_search.bm25_search(
self.repo_chunks[repo_id],
retrieval_query,
top_k=search_depth,
)
semantic_hits = self.hybrid_search.normalize_semantic_results(semantic_hits)
fused = self.hybrid_search.reciprocal_rank_fusion(lexical_hits, semantic_hits, top_k=search_depth)
path_hits = self._path_intent_search(
self.repo_chunks[repo_id],
question,
retrieval_query,
top_k=search_depth,
)
fused = self._merge_ranked_candidates(fused, path_hits, top_k=search_depth)
rerank_query = retrieval_query if question_intent in deep_search_intents else question
reranked = self.hybrid_search.rerank(rerank_query, fused, top_k=search_depth)
reranked = self._prioritize_results(question, retrieval_query, reranked, top_k=top_k)
reranked = self._select_answer_sources(question, reranked, top_k=top_k)
answer = self._generate_answer(repo, question, reranked, normalized_history)
return answer
finally:
session.close()
def end_session(self, session_key: str):
session = get_db_session(self.database_url)
try:
repos = session.query(Repository).filter_by(session_key=session_key).all()
self._delete_repositories(session, repos)
session.commit()
finally:
session.close()
def _generate_answer(
self,
repo: Repository,
question: str,
sources: List[dict],
history: Optional[List[dict]] = None,
) -> dict:
if not sources:
return {
"answer": "I could not find enough grounded evidence in the indexed codebase to answer that confidently.",
"confidence": "low",
"sources": [],
"repo": self._serialize_repo(repo),
}
context_blocks = []
slim_sources = []
for index, source in enumerate(sources, start=1):
context_blocks.append(
"\n".join(
[
f"[Source {index}]",
f"File: {source['file_path']}",
f"Symbol: {source['symbol_name']}",
f"Lines: {source['line_start']}-{source['line_end']}",
source["content"][:2500],
]
)
)
slim_sources.append(
{
"file_path": source["file_path"],
"language": source["language"],
"symbol_name": source["symbol_name"],
"symbol_type": source["symbol_type"],
"line_start": source["line_start"],
"line_end": source["line_end"],
"signature": source["signature"],
"snippet": source["content"],
"semantic_score": round(float(source.get("semantic_score", 0.0)), 4),
"bm25_score": round(float(source.get("bm25_score", 0.0)), 4),
"rrf_score": round(float(source.get("rrf_score", 0.0)), 4),
"rerank_score": round(float(source.get("rerank_score", 0.0)), 4),
}
)
wants_repo_overview = self._is_repo_overview_question(question)
question_intent = self._question_intent(question)
system_prompt = """
You are answering questions as a knowledgeable teammate who has carefully read this repository.
Rules:
1. Use only the supplied repository context.
2. Answer conversationally and directly, as if the repo is explaining itself to the user.
3. Do not say "Based on the provided context", "The repository is about", or similar throat-clearing phrases.
4. Be concrete about files, functions, and behavior.
5. If evidence is partial, clearly separate what is certain from what is inferred.
6. Respond in Markdown, not JSON.
7. Keep the answer complete. Do not stop mid-sentence.
8. Use short sections or bullets only when they genuinely help readability.
9. Do not leave unfinished headings, dangling bullets, or trailing markdown markers like #, ##, or ###.
10. Do not include inline citation markers like [Source 1] in the prose. The UI already shows sources separately.
11. Do not make claims that are not directly supported by the supplied sources.
12. Prefer the most canonical source files for API and implementation questions, such as package exports, core modules, and session/query code, over tutorial prose when they disagree in specificity.
13. Keep the answer tight. Lead with the direct answer, then add only the most important supporting detail.
"""
if wants_repo_overview:
system_prompt += """
14. For repository overview questions, lead with a direct one or two sentence summary of what the repo does.
15. Prioritize README and top-level documentation when they are present, then use code to support the explanation.
16. Mention the main workflow, core stack, and any important product constraints the user would care about.
17. Keep the answer polished and self-contained, like the overview a real user expects when they ask what a repo is about.
"""
elif question_intent in {"api", "implementation", "cross_file", "error_handling", "setup"}:
system_prompt += """
14. For API, implementation, setup, and cross-file questions, prefer the smallest correct answer that is directly supported by code.
15. If a detail comes only from docs or examples and not from the canonical implementation, say that clearly instead of presenting it as core behavior.
16. When describing exports or code paths, name the file first and keep the explanation precise.
17. Default to one short paragraph plus at most 3 short bullets. Avoid long explanatory walkthroughs unless the question explicitly asks for depth.
"""
joined_context = "\n\n".join(context_blocks)
user_prompt = f"""
Repository: {repo.owner}/{repo.name}
Question: {question}
Recent conversation:
{self._format_history(history or [])}
Context:
{joined_context}
"""
answer_text, finish_reason = self._generate_markdown_response(system_prompt, user_prompt)
if self._looks_incomplete(answer_text, finish_reason):
repair_prompt = f"""
The draft answer below appears to be cut off or incomplete.
Rewrite it into a complete final answer using the same repository context and rules.
Draft answer:
{answer_text}
"""
answer_text, finish_reason = self._generate_markdown_response(
system_prompt,
f"{user_prompt.strip()}\n\n{repair_prompt.strip()}",
)
if self._looks_incomplete(answer_text, finish_reason):
short_prompt = f"""
Answer the question again, but keep it concise and complete.
Use 2 short paragraphs or 4-6 bullets max.
Do not leave the answer unfinished.
"""
answer_text, _ = self._generate_markdown_response(
system_prompt,
f"{user_prompt.strip()}\n\n{short_prompt.strip()}",
)
answer_text = self._finalize_answer(answer_text)
confidence = self._estimate_confidence(sources)
summary = " ".join(answer_text.split())[:160] if answer_text else ""
citations = [
{
"source": index,
"reason": f"Relevant context from {source['file_path']}",
}
for index, source in enumerate(sources[: min(len(sources), 4)], start=1)
]
return {
"answer": answer_text,
"confidence": confidence,
"summary": summary,
"citations": citations,
"sources": slim_sources,
"repo": self._serialize_repo(repo),
}
def _configure_llm(self):
if self.llm_provider == "bedrock":
self.llm_client = create_bedrock_runtime_client()
self.llm_model = os.getenv(
"BEDROCK_LLM_MODEL",
"anthropic.claude-sonnet-4-20250514-v1:0",
)
return
if self.llm_provider == "groq":
self.llm_client = OpenAI(
api_key=os.getenv("GROQ_API_KEY"),
base_url=os.getenv("GROQ_BASE_URL", "https://api.groq.com/openai/v1"),
)
self.llm_model = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")
return
if self.llm_provider == "vertex_ai":
project = os.getenv("GOOGLE_CLOUD_PROJECT")
location = os.getenv("GOOGLE_CLOUD_LOCATION", "global")
if not project:
raise RuntimeError(
"GOOGLE_CLOUD_PROJECT must be set when using Vertex AI LLMs."
)
self.llm_model = os.getenv("VERTEX_LLM_MODEL", "claude-sonnet-4@20250514")
if self.llm_model.startswith("claude-"):
try:
from anthropic import AnthropicVertex
except ImportError as exc:
raise RuntimeError(
"Vertex AI Claude support requires the `anthropic[vertex]` package."
) from exc
self.llm_client = AnthropicVertex(project_id=project, region=location)
return
try:
from google import genai
except ImportError as exc:
raise RuntimeError(
"Vertex AI Gemini support requires the `google-genai` package."
) from exc
self.llm_client = genai.Client(
vertexai=True,
project=project,
location=location,
)
return
raise RuntimeError(f"Unsupported LLM provider: {self.llm_provider}")
def _generate_markdown_response(self, system_prompt: str, user_prompt: str) -> tuple[str, str]:
if self.llm_provider == "bedrock":
text, stop_reason = generate_bedrock_claude_text(
self.llm_client,
self.llm_model,
system_prompt,
user_prompt,
max_tokens=2200,
temperature=0.1,
)
return self._normalize_markdown_answer(text), stop_reason
if self.llm_provider == "groq":
response = self.llm_client.chat.completions.create(
model=self.llm_model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.1,
max_tokens=1600,
)
content = response.choices[0].message.content
finish_reason = getattr(response.choices[0], "finish_reason", "") or ""
return self._normalize_markdown_answer(content), str(finish_reason)
if self.llm_provider == "vertex_ai" and self.llm_model.startswith("claude-"):
message = self.llm_client.messages.create(
model=self.llm_model,
system=system_prompt.strip(),
max_tokens=2200,
temperature=0.1,
messages=[
{
"role": "user",
"content": user_prompt.strip(),
}
],
)
content_blocks = getattr(message, "content", None) or []
text = "".join(
getattr(block, "text", "") for block in content_blocks if getattr(block, "text", "")
)
if not text.strip():
raise RuntimeError("Vertex AI Claude returned an empty response.")
stop_reason = getattr(message, "stop_reason", "") or ""
return self._normalize_markdown_answer(text), str(stop_reason)
response = self.llm_client.models.generate_content(
model=self.llm_model,
contents=f"{system_prompt.strip()}\n\n{user_prompt.strip()}",
config={
"temperature": 0.1,
"max_output_tokens": 2200,
},
)
if not getattr(response, "text", None):
raise RuntimeError("Vertex AI Gemini returned an empty response.")
finish_reason = ""
candidates = getattr(response, "candidates", None) or []
if candidates:
finish_reason = str(getattr(candidates[0], "finish_reason", "") or "")
return self._normalize_markdown_answer(response.text), finish_reason
@staticmethod
def _normalize_markdown_answer(raw_text: str) -> str:
cleaned = (raw_text or "").strip()
cleaned = re.sub(r"^```(?:markdown|md)?\s*|\s*```$", "", cleaned, flags=re.IGNORECASE)
cleaned = re.sub(r"\s*\[(?:Source\s+\d+(?:\s*,\s*Source\s+\d+)*)\]", "", cleaned, flags=re.IGNORECASE)
cleaned = re.sub(
r"^(?:based on the provided context[,:\s-]*|from the provided context[,:\s-]*)",
"",
cleaned,
flags=re.IGNORECASE,
).strip()
cleaned = re.sub(
r"\n(?:#{1,6}|[-*])\s*$",
"",
cleaned,
flags=re.MULTILINE,
).strip()
cleaned = re.sub(r"(?:\n\s*){3,}", "\n\n", cleaned)
cleaned = cleaned.strip()
if not cleaned:
return "I found relevant code context, but the model returned an empty response."
return cleaned
@staticmethod
def _finalize_answer(answer_text: str) -> str:
cleaned = (answer_text or "").strip()
if not cleaned:
return "I found relevant code context, but the model returned an empty response."
# If the tail still looks truncated, trim back to the last complete sentence or list item
if CodebaseRAGSystem._looks_incomplete(cleaned):
sentence_match = re.search(r"(?s)^.*[.!?](?:['\"\)`\]]+)?", cleaned)
if sentence_match:
trimmed = sentence_match.group(0).strip()
if len(trimmed.split()) >= 12:
return trimmed
lines = cleaned.splitlines()
while lines and CodebaseRAGSystem._looks_incomplete(lines[-1]):
lines.pop()
candidate = "\n".join(line for line in lines if line.strip()).strip()
if candidate:
return candidate
return cleaned
@staticmethod
def _looks_incomplete(answer_text: str, finish_reason: str = "") -> bool:
cleaned = (answer_text or "").strip()
if not cleaned:
return True
finish_reason = (finish_reason or "").strip().lower()
if finish_reason and finish_reason not in {"stop", "stopsequence", "finish_reason_unspecified"}:
return True
if cleaned.endswith(("#", "-", "*", ":", "(", "[", "/", "`")):
return True
if cleaned.endswith(("[source", "[source 1", "[source 2", "[source 3", "[source 4")):
return True
if cleaned.count("```") % 2 != 0:
return True
if cleaned.count("(") > cleaned.count(")"):
return True
if cleaned.count("[") > cleaned.count("]"):
return True
tokens = re.findall(r"\b[\w'-]+\b", cleaned.lower())
if not tokens:
return True
if tokens[-1] in {"a", "an", "the", "to", "for", "with", "of", "in", "on", "from", "about"}:
return True
if len(tokens) >= 20 and cleaned[-1] not in {".", "!", "?", "\"", "'", "`"}:
return True
return False
@staticmethod
def _estimate_confidence(sources: List[dict]) -> str:
if not sources:
return "low"
top = sources[0]
rerank = float(top.get("rerank_score", 0.0))
semantic = float(top.get("semantic_score", 0.0))
if len(sources) >= 3 and (rerank >= 0.2 or semantic >= 0.75):
return "high"
if rerank >= 0.05 or semantic >= 0.45:
return "medium"
return "low"
def _serialize_repo(self, repo: Repository) -> dict:
payload = {
"id": repo.id,
"github_url": repo.source_url or repo.github_url,
"owner": repo.owner,
"name": repo.name,
"branch": repo.branch,
"local_path": repo.local_path,
"status": repo.status,
"error_message": repo.error_message,
"file_count": repo.file_count,
"chunk_count": repo.chunk_count,
"indexed_at": repo.indexed_at.isoformat() if repo.indexed_at else None,
"created_at": repo.created_at.isoformat() if repo.created_at else None,
"updated_at": repo.updated_at.isoformat() if repo.updated_at else None,
}
progress = self.indexing_progress.get(repo.id)
if progress:
payload["progress"] = progress
return payload
def _set_progress(self, repo_id: int, **progress):
self.indexing_progress[repo_id] = {
**self.indexing_progress.get(repo_id, {}),
**progress,
"updated_at": datetime.utcnow().isoformat(),
}
def _touch_session(self, session, session_key: str):
expiry = self._session_expiry()
repos = session.query(Repository).filter_by(session_key=session_key).all()
for repo in repos:
repo.session_expires_at = expiry
session.commit()
def _cleanup_expired_sessions(self, session):
now = datetime.utcnow()
expired = (
session.query(Repository)
.filter(Repository.session_expires_at.is_not(None))
.filter(Repository.session_expires_at < now)
.all()
)
if not expired:
return
self._delete_repositories(session, expired)
session.commit()
def _delete_repositories(
self,
session,
repos: List[Repository],
track_cancellation: bool = True,
):
repo_ids = [repo.id for repo in repos]
for repo_id in repo_ids:
if track_cancellation:
self.cancelled_repo_ids.add(repo_id)
self.hybrid_search.remove_repository(repo_id)
self.vector_store.remove_repository(repo_id)
self.repo_chunks.pop(repo_id, None)
self.indexing_progress.pop(repo_id, None)
for repo in repos:
session.delete(repo)
def _ensure_repo_not_cancelled(self, repo_id: int):
if repo_id in self.cancelled_repo_ids:
raise SessionCancelledError("Session ended before indexing completed.")
def _build_retrieval_query(self, question: str, history: List[dict]) -> str:
normalized = " ".join(question.strip().split())
if self._is_repo_overview_question(normalized):
return "\n".join(
[
normalized,
"repository overview purpose main workflow architecture README features stack",
]
)
if not history:
return self._expand_query_for_intent(normalized)
recent_user = [
turn["content"].strip()
for turn in reversed(history)
if turn.get("role") == "user" and turn.get("content", "").strip()
]
recent_assistant = [
turn["content"].strip()
for turn in reversed(history)
if turn.get("role") == "assistant" and turn.get("content", "").strip()
and self._is_substantive_assistant_message(turn.get("content", ""))
]
is_follow_up = (
len(normalized.split()) <= 6
or bool(re.fullmatch(r"(give|show|where|which|how|what)(?:\s+.+)?", normalized.lower()))
or any(token in normalized.lower() for token in {"code", "snippet", "implementation"})
)
if not is_follow_up or not recent_user:
return self._expand_query_for_intent(normalized)
parts = [self._expand_query_for_intent(normalized)]
if recent_user:
parts.append(f"Follow-up to: {recent_user[0]}")
if recent_assistant:
parts.append(f"Previous answer: {recent_assistant[0][:300]}")
return "\n".join(parts)
def _merge_ranked_candidates(
self,
ranked_results: List[dict],
path_results: List[dict],
top_k: int,
) -> List[dict]:
merged = {}
for rank, item in enumerate(ranked_results, start=1):
enriched = dict(item)
enriched.setdefault("rrf_score", 0.0)
enriched["candidate_rank"] = rank
merged[enriched["id"]] = enriched
for rank, item in enumerate(path_results, start=1):
existing = merged.get(item["id"])
path_bonus = 1.0 / (20 + rank)
if existing is None:
enriched = dict(item)
enriched["rrf_score"] = float(enriched.get("rrf_score", 0.0)) + path_bonus
enriched["path_rank"] = rank
merged[enriched["id"]] = enriched
continue
existing.update({key: value for key, value in item.items() if key not in existing})
existing["rrf_score"] = float(existing.get("rrf_score", 0.0)) + path_bonus
existing["path_rank"] = rank
return sorted(
merged.values(),
key=lambda item: (
float(item.get("rrf_score", 0.0)),
float(item.get("path_score", 0.0)),
float(item.get("semantic_score", 0.0)),
),
reverse=True,
)[:top_k]
def _path_intent_search(
self,
chunks: List[dict],
question: str,
retrieval_query: str,
top_k: int,
) -> List[dict]:
if not chunks:
return []
combined_query = f"{question}\n{retrieval_query}"
path_hints = self._domain_path_hints(combined_query)
code_terms = self._query_code_terms(combined_query)
if not path_hints and not code_terms:
return []
scored = []
path_fragments = self._query_path_fragments(combined_query)
for item in chunks:
score = 0
file_path = (item.get("file_path") or "").lower()
text = " ".join(
[
file_path,
str(item.get("symbol_name") or "").lower(),
str(item.get("signature") or "").lower(),
str(item.get("content") or "")[:500].lower(),
]
)
for fragment in path_fragments:
if file_path == fragment or file_path.endswith(f"/{fragment}") or fragment in file_path:
score += 12
for hint in path_hints:
normalized_hint = hint.rstrip("/").lower()
if file_path == normalized_hint or file_path.startswith(normalized_hint + "/"):
score += 10
elif normalized_hint in file_path:
score += 6
if code_terms:
score += min(sum(1 for term in code_terms if term in text), 8)
if score <= 0:
continue
score += max(self._canonical_path_priority(item, combined_query), 0)
enriched = dict(item)
enriched["path_score"] = float(score)
scored.append(enriched)
scored.sort(
key=lambda item: (
float(item.get("path_score", 0.0)),
float(item.get("bm25_score", 0.0)),
float(item.get("semantic_score", 0.0)),
),
reverse=True,
)
return scored[:top_k]
def _prioritize_results(
self,
question: str,
retrieval_query: str,
results: List[dict],
top_k: int,
) -> List[dict]:
combined_query = f"{question} {retrieval_query}".lower()
wants_code = any(
token in combined_query
for token in {"code", "snippet", "implementation", "function", "class", "import"}
)
question_intent = self._question_intent(question)
wants_docs = self._is_documentation_query(combined_query) and question_intent in {
"docs",
"overview",
}
wants_repo_overview = self._is_repo_overview_question(question) or self._is_repo_overview_question(
retrieval_query
)
def sort_key(item: dict):
is_doc = self._is_doc_source(item)
return (
self._canonical_path_priority(item, combined_query),
float(item.get("path_score", 0.0)),
self._doc_priority(item),
1 if wants_repo_overview and is_doc else 0,
1 if (wants_docs and is_doc) or (not wants_docs and not is_doc) else 0,
1 if wants_code and not is_doc else 0,
1 if question_intent in {"api", "implementation", "cross_file", "error_handling", "setup"} and not is_doc else 0,
float(item.get("rerank_score", 0.0)),
float(item.get("semantic_score", 0.0)),
float(item.get("bm25_score", 0.0)),
)
ranked = sorted(results, key=sort_key, reverse=True)
if wants_docs or wants_repo_overview:
return ranked[:top_k]
selected = []
doc_items = []
for item in ranked:
if self._is_doc_source(item):
doc_items.append(item)
continue
selected.append(item)
if len(selected) == top_k:
return selected
selected.extend(doc_items[: max(1, top_k - len(selected))])
return selected[:top_k]
def _select_answer_sources(
self,
question: str,
results: List[dict],
top_k: int,
) -> List[dict]:
if not results:
return []
intent = self._question_intent(question)
max_per_file = 2 if intent in {"overview", "docs"} else 1
selected = []
file_counts = {}
for item in results:
file_path = item.get("file_path", "")
count = file_counts.get(file_path, 0)
if count >= max_per_file:
continue
selected.append(item)
file_counts[file_path] = count + 1
if len(selected) == top_k:
break
return selected
@staticmethod
def _is_documentation_query(query: str) -> bool:
return any(
token in query
for token in {
"readme",
"docs",
"documentation",
"setup",
"install",
"installation",
"usage",
"overview",
"what is this repo",
"what is the repository about",
"what is the repo about",
"what does the repo do",
"what does this repo do",
"repo summary",
"repository summary",
"project summary",
"feature",
"features",
"architecture",
}
)
@staticmethod
def _question_intent(question: str) -> str:
normalized = " ".join((question or "").lower().split())
if not normalized:
return "general"
def has_any(terms: set[str]) -> bool:
return any(
re.search(rf"(?<![a-z0-9]){re.escape(term)}(?![a-z0-9])", normalized)
for term in terms
)
if CodebaseRAGSystem._is_repo_overview_question(normalized):
return "overview"
if has_any({"test", "tests", "pytest", "spec"}):
return "tests"
if has_any({"error", "invalid", "conflict", "raises", "guard against"}):
return "error_handling"
if has_any(
{
"api",
"api v1",
"api v2",
"endpoint",
"frontend",
"backend",
"openapi",
"public api",
"route",
"router",
"trpc",
}
):
return "api"
if has_any(
{
"build",
"configure",
"configured",
"configuration",
"create",
"database",
"env",
"environment",
"install",
"local development",
"metadata",
"orchestration",
"self-host",
"self hosting",
"setup",
"table",
"workspace",
}
):
return "setup"
if has_any({"flow", "across", "across files", "connect", "code path"}):
return "cross_file"
if has_any(
{
"behavior",
"class",
"function",
"implementation",
"implemented",
"job",
"jobs",
"lifecycle",
"lives",
"method",
"represented",
"signing",
"webhook",
"webhooks",
"what is special",
"where does",
"where is",
"where should",
"where would",
}
):
return "implementation"
if CodebaseRAGSystem._is_documentation_query(normalized):
return "docs"
return "general"
def _expand_query_for_intent(self, question: str) -> str:
normalized = " ".join((question or "").split())
lowered = normalized.lower()
hints = []
code_terms = self._query_code_terms(normalized)
if any(token in lowered for token in {"export", "expose", "import"}):
hints.extend(["package exports", "__init__.py", "index", "public api", "re-export"])
if any(token in lowered for token in {"public api", "exposed", "exported"}):
hints.extend(["public api", "__init__.py", "index"])
if "session.exec" in lowered or ("session" in lowered and "exec" in lowered):
hints.extend(["session exec", "session.py", "execute", "scalars"])
if "async" in lowered:
hints.extend(["async", "await", "asyncio"])
if any(token in lowered for token in {"relationship", "field", "function", "method", "class"}):
hints.extend(["class", "function", "method", "metadata"])
if any(token in lowered for token in {"under the hood", "implementation", "code path", "conversion"}):
hints.extend(["implementation", "source", "call path", "class", "function"])
if any(token in lowered for token in {"error", "invalid", "conflict", "raise", "raises", "guard"}):
hints.extend(["raise", "raises", "exception", "validation", "guard"])
if any(token in lowered for token in {"test", "tests", "pytest", "spec"}):
hints.extend(["test", "tests", "pytest", "spec"])
if any(token in lowered for token in {"create", "setup", "install", "configuration", "metadata", "table"}):
hints.extend(["create", "setup", "configure", "initialize", "schema", "README.md", "docs"])
if "__init__" in lowered or "exports" in lowered:
hints.extend(["__init__.py", "package exports", "public api"])
hints.extend(self._domain_path_hints(normalized))
for term in sorted(code_terms):
parts = [part for part in re.split(r"[._/-]+", term) if len(part) > 2]
if len(parts) > 1:
hints.append(" ".join(parts))
if not hints:
return normalized
return "\n".join([normalized, " ".join(hints)])
@staticmethod
def _is_repo_overview_question(question: str) -> bool:
normalized = " ".join((question or "").lower().split())
return any(
phrase in normalized
for phrase in {
"what is the repo about",
"what is this repo about",
"what does the repo do",
"what does this repo do",
"what is the repository about",
"what does the repository do",
"what is this project about",
"what does this project do",
"repo summary",
"repository summary",
"project summary",
"summarize the repo",
"summarize this repo",
"repo overview",
"repository overview",
"project overview",
}
)
@staticmethod
def _is_doc_source(item: dict) -> bool:
file_path = (item.get("file_path") or "").lower()
language = (item.get("language") or "").lower()
return language == "text" or file_path.endswith(".md") or "/readme" in file_path
@staticmethod
def _doc_priority(item: dict) -> int:
file_path = (item.get("file_path") or "").lower()
if file_path in {"readme.md", "readme"}:
return 3
if file_path.startswith("docs/") or "/docs/" in file_path:
return 2
if file_path.endswith(".md"):
return 1
return 0
@staticmethod
def _domain_path_hints(query: str) -> List[str]:
normalized = " ".join((query or "").lower().split())
hints = []
def has_any(terms: set[str]) -> bool:
matched = False
for term in terms:
if term.startswith("/"):
matched = matched or term in normalized
continue
matched = matched or bool(
re.search(rf"(?<![a-z0-9]){re.escape(term)}(?![a-z0-9])", normalized)
)
return matched
rules = [
(
{"email", "emails", "mailer", "notification", "template"},
[
"packages/email",
"packages/lib/server-only/email",
"packages/lib/jobs/definitions/emails",
],
),
(
{"signing", "certificate", "gcloud", "hsm", "p12", "pdf signing"},
[
"packages/signing",
"packages/signing/transports",
"packages/signing/helpers",
"packages/lib/jobs/definitions/internal/seal-document",
],
),
(
{"job", "jobs", "background", "inngest", "bullmq", "queue"},
[
"packages/lib/jobs",
"packages/lib/jobs/client",
"packages/lib/jobs/definitions",
"apps/remix/server/router.ts",
],
),
(
{"webhook", "webhooks", "ssrf"},
[
"packages/lib/server-only/webhooks",
"packages/lib/jobs/definitions/internal/execute-webhook",
],
),
(
{"recipient", "recipients"},
[
"packages/lib/server-only/recipient",
"packages/trpc/server/recipient-router",
],
),
(
{"field", "fields"},
[
"packages/lib/server-only/field",
"packages/trpc/server/field-router",
"packages/lib/universal/field-renderer",
],
),
(
{"template", "templates"},
[
"packages/lib/server-only/template",
"packages/trpc/server/template-router",
],
),
(
{"envelope", "envelopes"},
[
"packages/lib/server-only/envelope",
"packages/lib/server-only/envelope-item",
"packages/trpc/server/envelope-router",
],
),
(
{"document", "documents"},
[
"packages/lib/server-only/document",
"packages/lib/server-only/document-data",
"packages/trpc/server/document-router",
],
),
(
{"pdf", "storage", "upload", "s3"},
[
"packages/lib/server-only/pdf",
"packages/lib/server-only/document-data",
"packages/lib/universal/upload",
"apps/remix/server/api/files",
],
),
(
{"api v1", "/api/v1", "ts-rest", "ts rest"},
[
"packages/api",
"packages/api/v1",
"packages/api/hono.ts",
],
),
(
{"api v2", "/api/v2", "openapi", "trpc-to-openapi"},
[
"packages/trpc/server",
"packages/trpc/server/open-api.ts",
"apps/remix/server/router.ts",
"apps/remix/server/trpc",
],
),
(
{"trpc", "frontend", "backend", "/api/trpc", "internal api"},
[
"packages/trpc",
"packages/trpc/react",
"packages/trpc/client",
"packages/trpc/server/context.ts",
"apps/remix/server/trpc",
"apps/remix/server/router.ts",
],
),
(
{"auth", "authentication", "session", "api token", "authorization", "bearer"},
[
"packages/auth",
"packages/lib/server-only/auth",
"packages/lib/server-only/public-api",
"packages/trpc/server/context.ts",
"packages/trpc/server/trpc.ts",
"packages/api/v1/middleware/authenticated.ts",
"apps/remix/server/context.ts",
],
),
(
{"database", "postgres", "postgresql", "prisma", "kysely", "migration"},
[
"packages/prisma",
"packages/prisma/schema.prisma",
"packages/prisma/migrations",
".env.example",
],
),
(
{"remix", "hono", "react router", "route", "routes", "user interface"},
[
"apps/remix/server",
"apps/remix/app/routes",
"apps/remix/app/root.tsx",
"apps/remix/app/routes.ts",
],
),
(
{"test", "tests", "e2e", "playwright", "spec", "vitest"},
[
"packages/app-tests",
"packages/lib/vitest.config.ts",
"packages/lib/package.json",
],
),
(
{
"config",
"configuration",
"env",
"environment",
"local development",
"self-host",
"self hosting",
"workspace",
"workspaces",
"turborepo",
"turbo",
},
[
".env.example",
"README.md",
"package.json",
"turbo.json",
"apps/docs/content/docs/developers/local-development",
"apps/docs/content/docs/self-hosting/configuration",
],
),
]
for terms, paths in rules:
if has_any(terms):
hints.extend(paths)
return list(dict.fromkeys(hints))
def _canonical_path_priority(self, item: dict, question: str) -> int:
file_path = (item.get("file_path") or "").lower()
source_text = " ".join(
[
file_path,
str(item.get("symbol_name") or "").lower(),
str(item.get("signature") or "").lower(),
]
)
basename = file_path.rsplit("/", 1)[-1]
stem = basename.rsplit(".", 1)[0]
symbol_name = str(item.get("symbol_name") or "").lower()
signature = str(item.get("signature") or "").lower()
intent = self._question_intent(question)
code_terms = self._query_code_terms(question)
path_fragments = self._query_path_fragments(question)
path_hints = self._domain_path_hints(question)
score = 0
for fragment in path_fragments:
if file_path == fragment or file_path.endswith(f"/{fragment}") or fragment in file_path:
score += 8
for path_hint in path_hints:
normalized_hint = path_hint.rstrip("/").lower()
if file_path == normalized_hint or file_path.startswith(normalized_hint + "/"):
score += 8
elif normalized_hint in file_path:
score += 4
matched_terms = {term for term in code_terms if term in source_text}
score += min(len(matched_terms), 6)
for term in code_terms:
if term == basename or term == stem:
score += 4
elif term in basename:
score += 3
if term and term in symbol_name:
score += 3
if term and term in file_path:
score += 2
if term and term in signature:
score += 1
if intent == "api":
if basename == "__init__.py" or stem in {"index", "public", "api"}:
score += 4
if any(token in file_path for token in {"api", "route", "router", "controller"}):
score += 2
if intent in {"implementation", "cross_file"}:
if not self._is_doc_source(item):
score += 2
if item.get("symbol_type") != "fallback_chunk":
score += 1
if intent == "tests":
if (
file_path.startswith("tests/")
or "/tests/" in file_path
or basename.startswith("test_")
or basename.endswith("_test.py")
or basename.endswith(".test.js")
or basename.endswith(".spec.js")
or basename.endswith(".test.ts")
or basename.endswith(".spec.ts")
):
score += 5
if intent == "error_handling":
if any(token in source_text for token in {"raise", "except", "error", "invalid", "exception"}):
score += 3
if "test" in file_path:
score += 1
if intent == "setup":
setup_files = {
"readme.md",
"package.json",
"pyproject.toml",
"requirements.txt",
"dockerfile",
"docker-compose.yml",
"compose.yml",
}
if basename in setup_files or any(token in file_path for token in {"config", "settings", "setup"}):
score += 3
if any(token in source_text for token in {"create", "configure", "initialize", "metadata", "schema"}):
score += 1
if intent in {"docs", "overview"} and self._is_doc_source(item):
score += self._doc_priority(item) + 1
if self._is_doc_source(item) and intent in {
"api",
"implementation",
"cross_file",
"error_handling",
"tests",
}:
score -= 3
if file_path.startswith(".agents/") or file_path.startswith(".opencode/"):
score -= 8
return score
@staticmethod
def _query_code_terms(text: str) -> set:
stopwords = {
"about",
"against",
"also",
"and",
"are",
"between",
"code",
"does",
"file",
"for",
"from",
"happen",
"happens",
"how",
"into",
"main",
"me",
"model",
"models",
"path",
"project",
"that",
"the",
"this",
"through",
"under",
"using",
"what",
"when",
"where",
"which",
"with",
}
raw_terms = re.findall(
r"[A-Za-z_][A-Za-z0-9_]*(?:[./-][A-Za-z_][A-Za-z0-9_]*)*",
text or "",
)
terms = set()
for raw_term in raw_terms:
expanded = {raw_term}
expanded.update(re.split(r"[._/-]+", raw_term))
expanded.update(re.sub(r"(?<=[a-z0-9])(?=[A-Z])", " ", raw_term).split())
for term in expanded:
normalized = term.strip("_./-").lower()
if len(normalized) < 3 or normalized in stopwords:
continue
terms.add(normalized)
return terms
@staticmethod
def _query_path_fragments(text: str) -> set:
fragments = set()
for fragment in re.findall(r"[A-Za-z0-9_./-]+(?:\.[A-Za-z0-9_./-]+|/[A-Za-z0-9_./-]+)", text or ""):
normalized = fragment.strip().strip("./").lower()
if "/" in normalized or "." in normalized:
fragments.add(normalized)
return fragments
@staticmethod
def _is_substantive_assistant_message(content: str) -> bool:
normalized = " ".join((content or "").strip().lower().split())
if len(normalized) < 24:
return False
if normalized in {
"hey, what question do you have for me today?",
"ask a question",
}:
return False
return True
@staticmethod
def _normalize_history(history: List[object]) -> List[dict]:
normalized = []
for turn in history:
if isinstance(turn, dict):
role = turn.get("role")
content = turn.get("content")
else:
role = getattr(turn, "role", None)
content = getattr(turn, "content", None)
if not role or not content:
continue
normalized.append(
{
"role": str(role),
"content": str(content),
}
)
return normalized
@staticmethod
def _format_history(history: List[dict]) -> str:
if not history:
return "None"
lines = []
for turn in history[-4:]:
role = turn.get("role", "user").capitalize()
content = " ".join(turn.get("content", "").split())
if content:
lines.append(f"{role}: {content[:400]}")
return "\n".join(lines) if lines else "None"
@staticmethod
def _ensure_repo_still_exists(session, repo_id: int):
if session.query(Repository.id).filter_by(id=repo_id).first() is None:
raise RuntimeError("Repository was removed before indexing completed.")
def _session_expiry(self) -> datetime:
return datetime.utcnow() + timedelta(minutes=self.session_ttl_minutes)
@staticmethod
def _build_registry_key(session_key: str, github_url: str) -> str:
return f"{session_key}::{github_url}"
@staticmethod
def _serialize_chunk(chunk: dict) -> dict:
return {
"id": chunk["id"],
"file_path": chunk["file_path"],
"language": chunk["language"],
"symbol_name": chunk["symbol_name"],
"symbol_type": chunk["symbol_type"],
"line_start": chunk["line_start"],
"line_end": chunk["line_end"],
"signature": chunk["signature"],
"content": chunk["content"],
"searchable_text": chunk["searchable_text"],
"metadata_json": chunk.get("metadata_json") or {},
}
|