Buckets:

download
raw
39.7 kB
"""Repository indexing functionality for RAG.""" from __future__ import annotations import logging import os from collections import deque from dataclasses import dataclass, field from datetime import UTC, datetime from pathlib import Path from typing import TYPE_CHECKING from ..embeddings.base import BaseEmbeddingProvider from ..utils import get_agent_ignore_patterns from ..vector_store.base import BaseVectorStore from .semantic_chunker import SemanticChunker logger = logging.getLogger(__name__) if TYPE_CHECKING: from ..config import RAGConfig @dataclass class IndexingMetrics: """Track indexing performance and quality metrics.""" start_time: datetime = field(default_factory=lambda: datetime.now(UTC)) end_time: datetime | None = None files_processed: int = 0 files_skipped: int = 0 chunks_created: int = 0 chunks_failed: int = 0 total_bytes: int = 0 skip_reasons: dict[str, int] = field(default_factory=dict) def add_skip_reason(self, reason: str) -> None: """Track why a file was skipped.""" self.skip_reasons[reason] = self.skip_reasons.get(reason, 0) + 1 @property def duration_seconds(self) -> float: """Calculate indexing duration.""" if self.end_time: return (self.end_time - self.start_time).total_seconds() return 0.0 @property def chunks_per_second(self) -> float: """Calculate throughput.""" duration = self.duration_seconds return self.chunks_created / duration if duration > 0 else 0.0 def finalize(self) -> None: """Mark indexing as complete.""" self.end_time = datetime.now(UTC) def report(self) -> str: """Generate human-readable report.""" report_lines = [ "", "═" * 60, "INDEXING METRICS REPORT".center(60), "═" * 60, f"Duration: {self.duration_seconds:.2f}s", f"Files Processed: {self.files_processed}", f"Files Skipped: {self.files_skipped}", f"Chunks Created: {self.chunks_created}", f"Chunks Failed: {self.chunks_failed}", f"Total Bytes: {self.total_bytes / 1024:.2f} KB", f"Throughput: {self.chunks_per_second:.2f} chunks/s", ] if self.skip_reasons: report_lines.append("") report_lines.append("Skip Reasons:") for reason, count in sorted(self.skip_reasons.items(), key=lambda x: x[1], reverse=True): report_lines.append(f" - {reason}: {count}") report_lines.append("═" * 60) return "\n".join(report_lines) def read_file_content(path: Path, max_size: int = 1024 * 1024) -> str | None: """Read file content with size limit.""" try: if path.stat().st_size > max_size: return None return path.read_text(encoding="utf-8", errors="ignore") except Exception: return None def chunk_text( text: str, chunk_size: int = 1000, overlap: int = 100, separator: str = "\n\n", max_chunk_size: int = 4000, # Nomic V1 handles this easily min_chunk_size: int = 50, ) -> list[str]: """Split text into overlapping chunks with recursive splitting for oversized sections. Uses a queue to process sections so oversized sections are split and re-queued without mutating the iterated sequence (avoids unbounded growth and undefined behavior). """ if not text: return [] # Queue of sections to process (avoids mutating list while iterating) sections_queue: deque[str] = deque(text.split(separator)) chunks = [] current_chunk = "" while sections_queue: section = sections_queue.popleft() # Split oversized section and re-queue remainder instead of mutating during iteration if len(section) > max_chunk_size: sub_sections = [section[i : i + max_chunk_size] for i in range(0, len(section), max_chunk_size - overlap)] for sub in reversed(sub_sections[1:]): sections_queue.appendleft(sub) section = sub_sections[0] if len(current_chunk) + len(section) + len(separator) > chunk_size and current_chunk: chunks.append(current_chunk.strip()) words = current_chunk.split() overlap_words = words[-overlap:] if len(words) > overlap else words current_chunk = " ".join(overlap_words) + separator + section else: if current_chunk: current_chunk += separator + section else: current_chunk = section if current_chunk: chunks.append(current_chunk.strip()) # Final hard limit check (Safety split) final_chunks = [] for chunk in chunks: if len(chunk) > max_chunk_size: sub = [chunk[i : i + max_chunk_size] for i in range(0, len(chunk), max_chunk_size - overlap)] final_chunks.extend(sub) else: final_chunks.append(chunk) return [c for c in final_chunks if len(c) >= min_chunk_size] def is_text_file(path: Path, text_extensions: set[str]) -> bool: """Check if file is likely a text file.""" # Check extension if path.suffix.lower() in text_extensions: return True # Check for common text files without extension return True # Check for shebang try: # Security: This function should only be called with validated paths # from the indexing process which already restricts to repo_path with open(path, encoding="utf-8", errors="ignore") as f: first_line = f.readline(100) return first_line.startswith("#!") except Exception: return False def index_repository( repo_path: str, store_path: str | None = None, chunk_size: int = 3000, overlap: int = 300, rebuild: bool = False, embedding_provider: BaseEmbeddingProvider | None = None, exclude_dirs: list[str] | set[str] | None = None, include_patterns: list[str] | None = None, vector_store: BaseVectorStore | None = None, files: list[str] | None = None, quality_threshold: float = 0.0, quiet: bool = False, ) -> BaseVectorStore: """Index a repository into a vector store. Args: repo_path: Path to the repository to index store_path: Path to save/load the vector store (deprecated, use vector_store) chunk_size: Maximum size of each chunk in characters overlap: Number of characters to overlap between chunks rebuild: Whether to rebuild the index even if store exists embedding_provider: Provider for generating embeddings (must return dense vectors) exclude_dirs: Directory names to exclude include_patterns: File patterns to include (glob patterns) vector_store: Vector store instance to use (if None, creates ChromaDB store) Returns: VectorStore with indexed documents """ from ..embeddings.simple import SimpleEmbedding from ..vector_store.chromadb_store import ChromaDBVectorStore repo = Path(repo_path) if not repo.exists(): raise ValueError(f"Repository path does not exist: {repo_path}") try: repo_resolved = repo.resolve() except Exception: repo_resolved = repo # Default exclude directories exclude_dirs_set: set[str] if exclude_dirs is None: exclude_dirs_set = { ".git", ".svn", ".hg", "__pycache__", ".pytest_cache", ".mypy_cache", "node_modules", ".venv", "venv", ".env", ".idea", ".vscode", "dist", "build", ".tox", ".rag_db", ".rag_logs", "research_snapshots", "archival", "ui_backup", "ui_node_modules_orphan", "Hogwarts", "visualizations", "Arena", "datakit", "legacy_src", "artifacts", "analysis_report", "logs", "media", "assets", "temp", "rust", "interfaces", "acoustics", "awareness", "evolution", "motion", } else: exclude_dirs_set = set(exclude_dirs) if isinstance(exclude_dirs, list) else exclude_dirs # Load patterns from .agentignore agent_ignore = get_agent_ignore_patterns( str(repo_resolved if "repo_resolved" in locals() else repo_path if "repo_path" in locals() else ".") ) if agent_ignore: exclude_dirs_set.update(agent_ignore) # Default text file extensions text_extensions = { ".py", ".js", ".ts", ".jsx", ".tsx", ".md", ".txt", ".rst", ".yml", ".yaml", ".json", ".xml", ".html", ".css", ".sql", ".sh", ".bat", ".ps1", ".cfg", ".ini", ".toml", ".lock", ".java", ".c", ".cpp", ".h", ".hpp", ".cs", ".go", ".rs", ".php", ".rb", ".swift", ".kt", ".scala", ".r", ".m", ".pl", ".lua", ".vim", ".el", ".lisp", ".hs", ".ml", } # Files to explicitly exclude by name exclude_files = { "artifact.json", "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "poetry.lock", "Cargo.lock", "Gemfile.lock", "composer.lock", } # Initialize vector store if vector_store is None: vector_store = ChromaDBVectorStore() # Check if store has documents and rebuild is False if not rebuild and vector_store.count() > 0: logger.info("Using existing vector store with %d documents", vector_store.count()) if not rebuild: return vector_store if rebuild: reset_fn = getattr(vector_store, "reset", None) if callable(reset_fn): reset_fn() logger.info("Rebuilding index...") logger.info("Indexing repository: %s", repo_path) # Initialize metrics metrics = IndexingMetrics() # Walk the repository or use provided file list chunk_ids = [] chunk_texts = [] chunk_metadatas = [] if files: logger.info("Indexing %d specific files from manifest...", len(files)) file_iterator = [] seen: set[str] = set() for f in files: # Normalize separators to reduce Windows '\\' vs '/' duplication. f_norm = str(f).replace("\\\\", "/") p = Path(f_norm) if not p.is_absolute(): p = repo / p try: p_resolved = p.resolve() except Exception: p_resolved = p key = os.path.normcase(os.path.normpath(str(p_resolved))) if key in seen: continue seen.add(key) file_iterator.append(str(p_resolved)) else: file_iterator = [] for root, dirs, f_list in os.walk(repo): # Skip excluded directories dirs[:] = [d for d in dirs if d not in exclude_dirs_set] for f in f_list: file_iterator.append(str(Path(root) / f)) for file_str in file_iterator: file_path = Path(file_str) logger.debug("Processing %s", file_path) if not file_path.exists(): logger.debug("File not found: %s", file_path) continue # Check if file matches include patterns if include_patterns: if not any(file_path.match(pattern) for pattern in include_patterns): continue # Check if it's a text file and not too large if file_path.name in exclude_files: continue if not is_text_file(file_path, text_extensions): logger.debug("Not a text file: %s, suffix: %s", file_path.name, file_path.suffix) continue # Skip files larger than 1MB to avoid indexing huge artifacts try: if file_path.stat().st_size > 1024 * 1024: logger.debug("Skipping large file: %s", file_path) continue except Exception: # noqa: S112 intentional skip on error continue # Read file content logger.debug("Reading content for %s", file_path) content = read_file_content(file_path) if content is None: logger.debug("Content is None for %s", file_path) metrics.files_skipped += 1 metrics.add_skip_reason("Failed to read") continue if not content.strip(): logger.debug("Content is empty for %s", file_path) metrics.files_skipped += 1 metrics.add_skip_reason("Empty file") continue # Quality scoring (if threshold > 0) if quality_threshold > 0.0: from ..quality import should_index_file should_index, quality = should_index_file(file_path, quality_threshold, content) if not should_index: logger.debug( "Skipping %s: quality=%.2f, reasons=%s", file_path, quality.score, quality.reasons, ) metrics.files_skipped += 1 metrics.add_skip_reason(f"Low quality ({quality.score:.2f})") continue # Track file processing metrics.files_processed += 1 metrics.total_bytes += len(content) # Create relative path try: rel_path = file_path.resolve().relative_to(repo_resolved) except Exception: try: rel_path = file_path.relative_to(repo) except Exception: rel_path = Path(file_path.name) rel_path_str = rel_path.as_posix() # Chunk the document semantically chunker = SemanticChunker(min_chunk_size=50, max_chunk_size=chunk_size, chunk_overlap=overlap) semantic_chunks = chunker.chunk_file(content, rel_path_str) for i, sc in enumerate(semantic_chunks): chunk_id = f"{rel_path_str}#{i}" chunk_ids.append(chunk_id) chunk_texts.append(sc.content) # Merge chunker metadata with standard metadata metadata = {"path": rel_path_str, "chunk_index": i, "type": "chunk", "file_size": len(content)} metadata.update(sc.metadata) chunk_metadatas.append(metadata) metrics.chunks_created += 1 total_chunks = len(chunk_texts) if total_chunks > 50000: logger.warning( "HIGH CHUNK COUNT: %d chunks detected (recommended < 20,000). " "Consider increasing chunk_size (current: %d), using curate flag, " "or adding more exclude directories. Continuing with warning.", total_chunks, chunk_size, ) logger.info("Created %d chunks from repository", total_chunks) # Initialize progress tracker from ..progress import IndexProgress progress = IndexProgress() progress.start(total_chunks) # Generate embeddings if embedding_provider is None: if not quiet: logger.info("No embedding provider provided, using simple fallback") embedding_provider = SimpleEmbedding() if not quiet: logger.info("Generating embeddings using %s...", embedding_provider.__class__.__name__) # Filter out chunks that are too long (will be handled by embedding provider, but warn) limit = 4000 long_count = sum(1 for text in chunk_texts if len(text) > limit) if long_count: logger.warning( "%d chunks exceed recommended length (%d chars) and will be truncated", long_count, limit ) # Generate embeddings with batch support for performance embeddings_list = [] valid_indices = [] # Check if provider supports batch embeddings has_batch = hasattr(embedding_provider, "embed_batch") batch_size = 32 if has_batch else 1 if has_batch and total_chunks > 1: # Use batch embedding for better performance if not quiet: logger.info("Using batch embedding (batch_size=%d) for %d chunks...", batch_size, total_chunks) for batch_start in range(0, total_chunks, batch_size): batch_end = min(batch_start + batch_size, total_chunks) batch_texts = chunk_texts[batch_start:batch_end] try: batch_embs = embedding_provider.embed_batch(batch_texts) for j, emb in enumerate(batch_embs): idx = batch_start + j if isinstance(emb, list): embeddings_list.append(emb) else: embeddings_list.append(emb.tolist() if hasattr(emb, "tolist") else list(emb)) valid_indices.append(idx) except Exception as e: error_str = str(e).lower() if "context length" in error_str or "exceeds" in error_str: # Fall back to sequential for this batch logger.warning( "Batch embedding failed, falling back to sequential for batch %d-%d", batch_start, batch_end, ) for j, text in enumerate(batch_texts): idx = batch_start + j try: emb = embedding_provider.embed(text) if isinstance(emb, list): embeddings_list.append(emb) else: embeddings_list.append(emb.tolist() if hasattr(emb, "tolist") else list(emb)) valid_indices.append(idx) except Exception as inner_e: inner_error_str = str(inner_e).lower() if "context length" in inner_error_str or "exceeds" in inner_error_str: logger.warning("Skipping chunk %d (length: %d chars) - too long", idx, len(text)) metrics.chunks_failed += 1 metrics.chunks_created -= 1 continue else: raise else: raise if (batch_end // batch_size) % 5 == 0: progress.tick(batch_end - progress.completed) if not quiet and progress.should_report(): logger.info("Embedded %s", progress.format()) else: # Sequential embedding (fallback) if not quiet: logger.info("Using sequential embedding for %d chunks...", total_chunks) for i, text in enumerate(chunk_texts): try: emb = embedding_provider.embed(text) if isinstance(emb, list): embeddings_list.append(emb) else: # Convert numpy array to list embeddings_list.append(emb.tolist() if hasattr(emb, "tolist") else list(emb)) valid_indices.append(i) progress.tick() if not quiet and progress.should_report(): logger.info("Embedded %s", progress.format()) except Exception as e: error_str = str(e).lower() if "context length" in error_str or "exceeds" in error_str: logger.warning("Skipping chunk %d (length: %d chars) - too long even after truncation", i, len(text)) # Skip this chunk - don't add to valid_indices metrics.chunks_failed += 1 metrics.chunks_created -= 1 # Decrement since we counted it earlier continue else: raise # Re-raise other errors # Filter lists to only include valid chunks if not quiet: logger.info("Total chunks: %d, Valid: %d", len(chunk_texts), len(valid_indices)) if len(valid_indices) < len(chunk_texts): logger.info("Processed %d/%d chunks successfully", len(valid_indices), len(chunk_texts)) chunk_ids = [chunk_ids[i] for i in valid_indices] chunk_texts = [chunk_texts[i] for i in valid_indices] chunk_metadatas = [chunk_metadatas[i] for i in valid_indices] # Add to vector store in batches; trim in-memory lists after each batch to reduce peak memory store_batch_size = 100 batch_num = 0 while chunk_ids: batch_ids = chunk_ids[:store_batch_size] batch_texts = chunk_texts[:store_batch_size] batch_embeddings = embeddings_list[:store_batch_size] batch_metadatas = chunk_metadatas[:store_batch_size] batch_num += 1 if not quiet: logger.info("Adding batch %d (%d items) to store...", batch_num, len(batch_ids)) vector_store.add(ids=batch_ids, documents=batch_texts, embeddings=batch_embeddings, metadatas=batch_metadatas) if not quiet: logger.info("Current store count: %d", vector_store.count()) if batch_num % 10 == 0 and not quiet: logger.info("Indexed %d chunks...", batch_num * store_batch_size) # Drop batch from lists so we don't hold full corpus in memory until the end chunk_ids = chunk_ids[store_batch_size:] chunk_texts = chunk_texts[store_batch_size:] embeddings_list = embeddings_list[store_batch_size:] chunk_metadatas = chunk_metadatas[store_batch_size:] if not quiet: logger.info("Indexing completed. Total documents: %d", vector_store.count()) # Finalize and display metrics metrics.finalize() if not quiet: logger.info(metrics.report()) return vector_store def update_index( repo_path: str, vector_store: BaseVectorStore, embedding_provider: BaseEmbeddingProvider | None = None, exclude_dirs: list[str] | set[str] | None = None, include_patterns: list[str] | None = None, config: RAGConfig | None = None, quiet: bool = False, ) -> BaseVectorStore: """Update an existing index with new/modified files (true incremental). Uses FileTracker to detect changes and only re-index modified files. Deletes chunks for removed files. Args: repo_path: Path to the repository vector_store: Existing vector store to update embedding_provider: Provider for generating embeddings exclude_dirs: Directory names to exclude include_patterns: File patterns to include Returns: Updated VectorStore """ from ..config import RAGConfig from ..embeddings.simple import SimpleEmbedding from .file_tracker import FileTracker, compute_file_hash repo = Path(repo_path) if not repo.exists(): raise ValueError(f"Repository path does not exist: {repo_path}") repo_resolved = repo.resolve() if config is None: config = RAGConfig.from_env() is_databricks_store = vector_store.__class__.__name__ == "DatabricksVectorStore" # Guard: embedding dimension mismatch can hard-fail incremental updates (e.g. Chroma collection) # If the existing store has a dimension and it differs from the current embedding model, # reset the store so indexing can proceed. if not is_databricks_store and embedding_provider is not None: existing_dim = 0 try: existing_dim = int(getattr(vector_store, "get_dimension", lambda: 0)()) except Exception: existing_dim = 0 current_dim = 0 try: current_dim = int(getattr(embedding_provider, "dimension", 0) or 0) except Exception: current_dim = 0 if current_dim <= 0: try: current_dim = len(embedding_provider.embed("dimension probe")) except Exception: current_dim = 0 if existing_dim > 0 and current_dim > 0 and existing_dim != current_dim: logger.warning( "Embedding dimension mismatch for incremental update: store=%dD, model=%dD. " "Resetting vector store collection to avoid corruption...", existing_dim, current_dim, ) reset_fn = getattr(vector_store, "reset", None) if callable(reset_fn): reset_fn() else: raise RuntimeError( f"Vector store dimension mismatch (store={existing_dim}D, model={current_dim}D) and store has no reset(). " "Run with --rebuild or delete the existing index." ) # Tracker selection: # - Databricks store: use Delta manifest table (cross-machine, persistent) # - Other stores: use local FileTracker JSON persist_dir = getattr(vector_store, "persist_directory", ".rag_db") tracker = None dbx_tracker = None if is_databricks_store: from ..databricks_manifest import DatabricksManifestTracker dbx_tracker = DatabricksManifestTracker( schema=getattr(config, "databricks_schema", "default"), table=getattr(config, "databricks_manifest_table", "file_manifest"), repo=str(Path(repo_path).resolve()), ) else: tracker = FileTracker(persist_dir=persist_dir) # Default exclude directories exclude_dirs_set: set[str] if exclude_dirs is None: exclude_dirs_set = { ".git", ".svn", ".hg", "__pycache__", ".pytest_cache", ".mypy_cache", "node_modules", ".venv", "venv", ".env", ".idea", ".vscode", "dist", "build", ".tox", ".rag_db", ".rag_logs", "research_snapshots", "archival", "ui_backup", "ui_node_modules_orphan", "Hogwarts", "visualizations", "Arena", "datakit", "legacy_src", "artifacts", "analysis_report", "logs", "media", "assets", "temp", "rust", "interfaces", "acoustics", "awareness", "evolution", "motion", } else: exclude_dirs_set = set(exclude_dirs) if isinstance(exclude_dirs, list) else exclude_dirs # Load patterns from .agentignore agent_ignore = get_agent_ignore_patterns( str(repo_resolved if "repo_resolved" in locals() else repo_path if "repo_path" in locals() else ".") ) if agent_ignore: exclude_dirs_set.update(agent_ignore) # Text file extensions text_extensions = { ".py", ".js", ".ts", ".jsx", ".tsx", ".md", ".txt", ".rst", ".yml", ".yaml", ".json", ".xml", ".html", ".css", ".sql", ".sh", ".bat", ".ps1", ".cfg", ".ini", ".toml", ".java", ".c", ".cpp", ".h", ".hpp", ".cs", ".go", ".rs", ".php", ".rb", ".swift", ".kt", ".scala", ".r", ".m", ".pl", ".lua", } exclude_files = { "artifact.json", "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "poetry.lock", "Cargo.lock", "Gemfile.lock", "composer.lock", } # Collect current files current_files = [] for root, dirs, f_list in os.walk(repo): dirs[:] = [d for d in dirs if d not in exclude_dirs_set] for f in f_list: file_path = Path(root) / f if file_path.name in exclude_files: continue if not is_text_file(file_path, text_extensions): continue try: if file_path.stat().st_size > 1024 * 1024: continue except Exception: # noqa: S112 intentional skip on error continue current_files.append(file_path) # Deterministic ordering helps stable progress and reproducibility current_files.sort(key=lambda p: str(p).lower()) # Databricks manifest path: use remote manifest for deleted/changed detection if dbx_tracker is not None: from ..databricks_manifest import DatabricksFileState manifest = dbx_tracker.fetch_all() current_rel_paths = set() for p in current_files: rel_path_posix = str(p.relative_to(repo)) current_rel_paths.add(Path(rel_path_posix).as_posix()) deleted_paths = sorted([p for p in manifest.keys() if p not in current_rel_paths]) for rel_path_str in deleted_paths: if not quiet: logger.info("Removing chunks for deleted file: %s", rel_path_str) try: vector_store.delete(where={"path": rel_path_str}) vector_store.delete(where={"path": Path(rel_path_str).as_posix()}) except Exception as e: if not quiet: logger.warning("Could not delete chunks for %s: %s", rel_path_str, e) if deleted_paths: dbx_tracker.delete_paths(deleted_paths) # Detect changed/new files (fast path using size+mtime, hash only on mismatch) changed_files = [] for p in current_files: rel_path = str(p.relative_to(repo)) rel_path_str = Path(rel_path).as_posix() try: st = p.stat() size = int(st.st_size) mtime_ms = int(st.st_mtime * 1000) except Exception: # noqa: S112 intentional skip on error continue prev = manifest.get(rel_path_str) if prev is not None and prev.file_size == size and prev.mtime_ms == mtime_ms: continue # Hash only when necessary file_hash = compute_file_hash(p) if prev is not None and prev.file_hash == file_hash: continue changed_files.append(p) if not changed_files: if not quiet: logger.info("No changes detected. Index is up to date.") return vector_store if not quiet: logger.info("Found %d changed/new files to index", len(changed_files)) if embedding_provider is None: if not quiet: logger.warning("No embedding provider provided, using simple fallback") embedding_provider = SimpleEmbedding() updated_states = [] for file_path in changed_files: rel_path = str(file_path.relative_to(repo)) rel_path_str = Path(rel_path).as_posix() try: vector_store.delete(where={"path": rel_path}) vector_store.delete(where={"path": rel_path_str}) except Exception: # noqa: S110 intentional silent handling pass content = read_file_content(file_path) if content is None or not content.strip(): continue chunk_size_val = getattr(config, "chunk_size", 3000) if config else 3000 chunk_overlap_val = getattr(config, "chunk_overlap", 300) if config else 300 chunker = SemanticChunker(min_chunk_size=50, max_chunk_size=chunk_size_val, chunk_overlap=chunk_overlap_val) semantic_chunks = chunker.chunk_file(content, rel_path_str) if not semantic_chunks: continue file_hash = compute_file_hash(file_path) try: st = file_path.stat() size = int(st.st_size) mtime_ms = int(st.st_mtime * 1000) except Exception: size = len(content) mtime_ms = 0 chunk_ids = [] chunk_texts = [] chunk_metadatas = [] embeddings_list = [] for i, sc in enumerate(semantic_chunks): try: if embedding_provider is None: raise RuntimeError("Embedding provider is required") emb = embedding_provider.embed(sc.content) if isinstance(emb, list): embeddings_list.append(emb) else: embeddings_list.append(emb.tolist() if hasattr(emb, "tolist") else list(emb)) chunk_id = f"{rel_path_str}#{i}" chunk_ids.append(chunk_id) chunk_texts.append(sc.content) metadata = { "path": rel_path_str, "chunk_index": i, "type": "chunk", "file_size": len(content), "file_hash": file_hash, } metadata.update(sc.metadata) chunk_metadatas.append(metadata) except Exception as e: error_str = str(e).lower() if "context length" in error_str or "exceeds" in error_str: logger.warning("Skipping chunk %d in %s - too long", i, rel_path_str) continue raise if chunk_ids: vector_store.add( ids=chunk_ids, documents=chunk_texts, embeddings=embeddings_list, metadatas=chunk_metadatas ) updated_states.append( DatabricksFileState( repo=str(Path(repo_path).resolve()), path=rel_path_str, file_hash=file_hash, file_size=size, mtime_ms=mtime_ms, chunk_count=len(chunk_ids), ) ) if not quiet: logger.info("Indexed %d chunks from %s", len(chunk_ids), rel_path_str) if updated_states and dbx_tracker is not None: dbx_tracker.upsert_states(updated_states) if not quiet: logger.info("Incremental update completed. Total documents: %d", vector_store.count()) return vector_store # Ensure tracker is available for non-Databricks path if tracker is None: raise RuntimeError("File tracker is required for non-Databricks vector stores") # Detect deleted files and remove their chunks deleted_files = tracker.get_deleted_files(repo, current_files) for rel_path in deleted_files: if not quiet: logger.info("Removing chunks for deleted file: %s", rel_path) try: # Backward/forward compatibility: older indexes may store Windows-style paths # while curated/normalized indexes store POSIX paths. vector_store.delete(where={"path": rel_path}) vector_store.delete(where={"path": Path(rel_path).as_posix()}) except Exception as e: if not quiet: logger.warning("Could not delete chunks for %s: %s", rel_path, e) tracker.remove_file(rel_path) # Detect changed files changed_files = tracker.get_changed_files(repo, current_files) if not changed_files: if not quiet: logger.info("No changes detected. Index is up to date.") tracker.save() return vector_store if not quiet: logger.info("Found %d changed/new files to index", len(changed_files)) # Initialize embedding provider if embedding_provider is None: if not quiet: logger.warning("No embedding provider provided, using simple fallback") embedding_provider = SimpleEmbedding() # Process changed files for file_path in changed_files: rel_path = str(file_path.relative_to(repo)) rel_path_str = Path(rel_path).as_posix() # Delete existing chunks for this file try: vector_store.delete(where={"path": rel_path}) vector_store.delete(where={"path": rel_path_str}) except Exception: # noqa: S110 intentional silent handling pass # May not exist # Read and chunk file content = read_file_content(file_path) if content is None or not content.strip(): tracker.remove_file(rel_path) continue chunker = SemanticChunker(min_chunk_size=50, max_chunk_size=3000, chunk_overlap=300) semantic_chunks = chunker.chunk_file(content, rel_path_str) if not semantic_chunks: tracker.remove_file(rel_path) continue # Compute hash file_hash = compute_file_hash(file_path) # Generate embeddings and add chunks chunk_ids = [] chunk_texts = [] chunk_metadatas = [] embeddings_list = [] for i, sc in enumerate(semantic_chunks): try: if embedding_provider is None: raise RuntimeError("Embedding provider is required") emb = embedding_provider.embed(sc.content) if isinstance(emb, list): embeddings_list.append(emb) else: embeddings_list.append(emb.tolist() if hasattr(emb, "tolist") else list(emb)) chunk_id = f"{rel_path_str}#{i}" chunk_ids.append(chunk_id) chunk_texts.append(sc.content) metadata_base = { "source": rel_path_str, "chunk": i, "total_chunks": len(semantic_chunks), } full_metadata = { "path": rel_path_str, "chunk_index": i, "type": "chunk", "file_size": len(content), "file_hash": file_hash, "indexed_at": (tracker.state.last_updated if tracker else "") or "", **metadata_base, } full_metadata.update(sc.metadata) chunk_metadatas.append(full_metadata) except Exception as e: error_str = str(e).lower() if "context length" in error_str or "exceeds" in error_str: logger.warning("Skipping chunk %d in %s - too long", i, rel_path) continue raise if chunk_ids: vector_store.add( ids=chunk_ids, documents=chunk_texts, embeddings=embeddings_list, metadatas=chunk_metadatas ) tracker.update_file(rel_path, file_hash, len(content), len(chunk_ids)) logger.info("Indexed %d chunks from %s", len(chunk_ids), rel_path) # Save tracker state after successful indexing tracker.save() logger.info("Incremental update completed. Total documents: %d", vector_store.count()) return vector_store
"""Repository indexing functionality for RAG."""
from __future__ import annotations
import logging
import os
from collections import deque
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path
from typing import TYPE_CHECKING
from ..embeddings.base import BaseEmbeddingProvider
from ..utils import get_agent_ignore_patterns
from ..vector_store.base import BaseVectorStore
from .semantic_chunker import SemanticChunker
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from ..config import RAGConfig
@dataclass
class IndexingMetrics:
"""Track indexing performance and quality metrics."""
start_time: datetime = field(default_factory=lambda: datetime.now(UTC))
end_time: datetime | None = None
files_processed: int = 0
files_skipped: int = 0
chunks_created: int = 0
chunks_failed: int = 0
total_bytes: int = 0
skip_reasons: dict[str, int] = field(default_factory=dict)
def add_skip_reason(self, reason: str) -> None:
"""Track why a file was skipped."""
self.skip_reasons[reason] = self.skip_reasons.get(reason, 0) + 1
@property
def duration_seconds(self) -> float:
"""Calculate indexing duration."""
if self.end_time:
return (self.end_time - self.start_time).total_seconds()
return 0.0
@property
def chunks_per_second(self) -> float:
"""Calculate throughput."""
duration = self.duration_seconds
return self.chunks_created / duration if duration > 0 else 0.0
def finalize(self) -> None:
"""Mark indexing as complete."""
self.end_time = datetime.now(UTC)
def report(self) -> str:
"""Generate human-readable report."""
report_lines = [
"",
"═" * 60,
"INDEXING METRICS REPORT".center(60),
"═" * 60,
f"Duration: {self.duration_seconds:.2f}s",
f"Files Processed: {self.files_processed}",
f"Files Skipped: {self.files_skipped}",
f"Chunks Created: {self.chunks_created}",
f"Chunks Failed: {self.chunks_failed}",
f"Total Bytes: {self.total_bytes / 1024:.2f} KB",
f"Throughput: {self.chunks_per_second:.2f} chunks/s",
]
if self.skip_reasons:
report_lines.append("")
report_lines.append("Skip Reasons:")
for reason, count in sorted(self.skip_reasons.items(), key=lambda x: x[1], reverse=True):
report_lines.append(f" - {reason}: {count}")
report_lines.append("═" * 60)
return "\n".join(report_lines)
def read_file_content(path: Path, max_size: int = 1024 * 1024) -> str | None:
"""Read file content with size limit."""
try:
if path.stat().st_size > max_size:
return None
return path.read_text(encoding="utf-8", errors="ignore")
except Exception:
return None
def chunk_text(
text: str,
chunk_size: int = 1000,
overlap: int = 100,
separator: str = "\n\n",
max_chunk_size: int = 4000, # Nomic V1 handles this easily
min_chunk_size: int = 50,

Xet Storage Details

Size:
39.7 kB
·
Xet hash:
f14d462096bb1a291dd962d52312b2ff273ab0cd7e2b6ec4efb744789889143f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.