Skip to content

Implement Codebase Semantic Search for AI Agents #178

Closed

Description

@malibio

Overview

Implement semantic search over source code repositories for AI agents, enabling intelligent code navigation and reducing grep operations on large codebases.

Context

Purpose: Enable AI agents to semantically search codebases
Scope: Async background indexing of source files with incremental updates
Foundation: Builds on embedding service (#108) and semantic search (#107)
MCP Integration: Separate from #112 - focuses on indexing infrastructure

Use Cases

  1. AI Agent Code Navigation: "Find error handling functions" → Returns relevant code chunks
  2. Intelligent Grep Replacement: Semantic search reduces brute-force grep operations
  3. Cross-Repository Search: Search across multiple indexed codebases
  4. Documentation Discovery: Find relevant code examples and comments

Implementation Tasks

Data Model

  • Create code_embeddings table in Turso
  • Define code chunk schema (file, lines, language, context)
  • Add repository metadata (repo_id, path, git_hash)
  • Implement efficient chunk storage

Code Chunking Strategy

  • Language-aware chunking (AST-based for Rust/TS/Python)
  • Fallback line-based chunking (512 tokens, 50 token overlap)
  • Preserve context (function/class signatures)
  • Handle various file types (source code, configs, docs)

File Monitoring

  • Implement file watcher for incremental updates
  • Detect file changes (add/modify/delete)
  • Queue changed files for re-embedding
  • Handle git operations (branch switches, pulls)

Async Indexing

  • Background indexing queue
  • Batch embedding generation (100+ files)
  • Progress tracking and status updates
  • Error handling and retry logic

Search Implementation

  • Code-specific search API
  • Language/file-type filtering
  • Repository filtering
  • Context-aware result ranking

Acceptance Criteria

  • Repositories can be indexed in background
  • File changes trigger incremental re-indexing
  • Code chunks preserve meaningful context
  • Search returns relevant code snippets with file/line info
  • Performance: <100ms search over 100K code chunks
  • Indexing handles large repos (>10K files)
  • Multiple repositories supported simultaneously
  • Language-specific chunking works for Rust/TS/Python/Go

Technical Specifications

Database Schema:

CREATE TABLE code_embeddings (
    id TEXT PRIMARY KEY,
    repo_id TEXT NOT NULL,
    file_path TEXT NOT NULL,
    chunk_content TEXT NOT NULL,
    line_start INTEGER NOT NULL,
    line_end INTEGER NOT NULL,
    language TEXT,
    context TEXT,
    embedding_vector BLOB NOT NULL,
    indexed_at TEXT NOT NULL,
    git_hash TEXT,
    FOREIGN KEY (repo_id) REFERENCES repositories(id)
);

CREATE TABLE repositories (
    id TEXT PRIMARY KEY,
    path TEXT NOT NULL,
    name TEXT NOT NULL,
    indexed_at TEXT,
    file_count INTEGER,
    chunk_count INTEGER
);

Code Chunking:

pub struct CodeChunker {
    config: ChunkConfig,
}

impl CodeChunker {
    pub fn chunk_file(&self, file_path: &Path, content: &str) -> Result<Vec<CodeChunk>> {
        let language = detect_language(file_path);
        
        match language.as_str() {
            "rust" | "python" | "typescript" | "javascript" => {
                self.chunk_ast_based(content, &language)
            }
            _ => self.chunk_line_based(content, &language)
        }
    }
}

Performance Targets

  • Indexing speed: 1-5 files/second (async, not critical)
  • Search latency: <100ms for <100K chunks
  • Incremental update: <5 seconds for modified file
  • Memory usage: <1GB during indexing

Dependencies

Future Enhancements

  • AST-based chunking for more languages
  • Multi-repository search
  • Code similarity detection
  • Search with code context (call graphs)

Activity

  1. added
    enhancement
    on Oct 7, 2025
  2. malibio commented on May 17, 2026

    @malibio
    CollaboratorAuthor

    Closing — codebase semantic search for AI agents is handled by the PTY-based agent sessions (ADR-032). Coding agents (Claude Code, Codex, etc.) have direct filesystem access in the session temp folder, and @nodespace/agent-tools provides knowledge graph search. No separate implementation needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfoundation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions