Skip to content

Code Intelligence

scarecr0w12 edited this page Jun 19, 2026 · 2 revisions

Code Intelligence (Codegraph)

CortexPrism's code intelligence system provides deep structural understanding of codebases using tree-sitter WASM parsing with FTS5-backed full-text search over a graph database of code symbols and their relationships.

Architecture

┌──────────────────────────────────────────────────────────────┐
│                    Code Intelligence                          │
│                                                               │
│  tree-sitter WASM (14+ languages)                             │
│          │                                                    │
│          ▼                                                    │
│  ┌──────────────────────────────────────┐                    │
│  │          AST Extraction               │                    │
│  │  - Functions, classes, interfaces     │                    │
│  │  - Imports, exports, calls            │                    │
│  │  - Decorators, HTTP calls, async      │                    │
│  │  - Complexity estimation              │                    │
│  └──────────────────────────────────────┘                    │
│          │                                                    │
│          ▼                                                    │
│  ┌──────────────────────────────────────┐                    │
│  │        Code Graph (memory.db)         │                    │
│  │  - 14 node labels (CodeFunction,      │                    │
│  │    CodeClass, CodeInterface, etc.)     │                    │
│  │  - 18 edge types (CALLS, IMPORTS,     │                    │
│  │    DEFINES, IMPLEMENTS, INHERITS)      │                    │
│  │  - FTS5 full-text search              │                    │
│  └──────────────────────────────────────┘                    │
│          │                                                    │
│          ▼                                                    │
│  ┌──────────────────────────────────────┐                    │
│  │        Codegraph Resolver             │                    │
│  │  - Exact symbol match                 │                    │
│  │  - Method on class resolution         │                    │
│  │  - Wildcard/relative import           │                    │
│  │  - Type inference                     │                    │
│  │  - Fallback search                    │                    │
│  └──────────────────────────────────────┘                    │
│          │                                                    │
│          ▼                                                    │
│  ┌──────────────────────────────────────┐                    │
│  │           6 Agent Tools               │                    │
│  │  code_index    code_search_symbol     │                    │
│  │  code_trace_path  code_get_architecture│                   │
│  │  code_analyze_impact  code_list_projects│                  │
│  └──────────────────────────────────────┘                    │
│                                                               │
│  Web UI: D3.js force-directed graph                           │
│  ┌──────────────────────────────────────┐                    │
│  │  - Interactive dependency graph       │                    │
│  │  - Symbol search with FTS5            │                    │
│  │  - Impact analysis (blast radius)     │                    │
│  │  - Path tracer (caller ↔ callee)      │                    │
│  └──────────────────────────────────────┘                    │
└──────────────────────────────────────────────────────────────┘

Supported Languages

Tree-sitter WASM parsers with lazy-loading from CDN:

Language File Extensions Parser
TypeScript .ts, .tsx tree-sitter-typescript
JavaScript .js, .jsx tree-sitter-javascript
Python .py tree-sitter-python
Go .go tree-sitter-go
Rust .rs tree-sitter-rust
Java .java tree-sitter-java
Kotlin .kt tree-sitter-kotlin
C .c, .h tree-sitter-c
C++ .cpp, .hpp tree-sitter-cpp
Ruby .rb tree-sitter-ruby
PHP .php tree-sitter-php
Swift .swift tree-sitter-swift
Lua .lua tree-sitter-lua
Bash .sh, .bash tree-sitter-bash

Node Labels (14)

CodeProject, CodeFile, CodeFunction, CodeClass, CodeInterface, CodeEnum, CodeType, CodeVariable, CodeConstant, CodeModule, CodeDecorator, CodeEndpoint, CodeDatabaseQuery, CodeTest

Edge Types (18)

CALLS, IMPORTS, DEFINES, IMPLEMENTS, INHERITS, HTTP_CALLS, ASYNC_CALLS, DECORATES, EXPORTS, REFERENCES, RETURNS, PARAMETER_OF, THROWS, TESTS, MOCKS, CONFIGURES, MIGRATES, DEPENDS_ON

Ingestion Pipeline

Indexing (code_index)

code_index(projectPath)
  1. Directory walk (max 200K files, depth 100)
  2. File hash comparison (skip unchanged)
  3. Language detection by extension
  4. WASM parser validation (integrity check)
  5. AST extraction per file
  6. Chunked bulk insert (BFS-batched: 2 queries/level instead of N+1)
  7. FTS5 index refresh

Resolver (6 strategies)

  1. Exact symbol match — direct name lookup
  2. Method on class — resolve ClassName.methodName
  3. Wildcard import — expand import * as X references
  4. Relative import path — resolve ./path/to/module
  5. Type inference — follow type declarations to methods
  6. Fallback search — FTS5 full-text lookup

Agent Tools

code_index

Full repository indexing with incremental sync.

Parameter Type Description
projectPath string Root directory to index
projectName string Optional project identifier
force boolean Re-index all files (skip hash check)

code_search_symbol

FTS5-backed symbol search across projects.

Parameter Type Description
query string Symbol name to search
project string Optional project filter
kind string Optional node label filter
limit number Max results (default 20)

code_trace_path

Bidirectional call graph traversal.

Parameter Type Description
symbolId string Starting symbol node ID
direction string "inbound", "outbound", or "both"
maxDepth number Maximum traversal depth (default 5)

code_get_architecture

System architecture diagram extraction.

Parameter Type Description
project string Project name

Returns layers, modules, and dependency structure.

code_analyze_impact

Blast radius analysis for a symbol.

Parameter Type Description
symbolId string Target symbol node ID

Returns direct/indirect callers, callees, dead code detection, and complexity metrics.

code_list_projects

Project registry with language statistics.

Returns per-project node counts, edge counts, language breakdowns, and last-indexed timestamps.

Web UI (Codegraph Page)

  • D3.js force-directed graph — interactive zoomable dependency visualization
  • Symbol search — FTS5-powered search with type filters
  • Impact analysis — click a node to see blast radius (callers + callees)
  • Path tracer — trace calls between any two symbols
  • Hotspot detection — identify high-complexity nodes

API Endpoints

Method Path Description
GET /api/codegraph/projects List indexed projects
POST /api/codegraph/index Start code indexing
GET /api/codegraph/search?q=&project= Search symbols
POST /api/codegraph/impact Impact analysis
GET /api/codegraph/architecture?project= Architecture extraction
POST /api/codegraph/trace Path tracing

See Also

Cross-Repository Search (#74)

Search across all indexed projects simultaneously:

  • GET /api/codegraph/search-all?q=&language= returns ranked cross-repo results
  • Project name labels distinguish results from different repositories
  • Codegraph web UI: 🌐 All repos button in the sidebar search panel

Language Filtering (#84)

  • ftsSearchNodes() accepts optional language parameter
  • getLanguages() returns distinct languages per project
  • Language dropdown in the Codegraph sidebar filters search results

Code Ownership (#81)

  • GET /api/codegraph/ownership?file= returns git blame author attribution
  • Owners ranked by lines authored (name and email)

Codebase Archeologist (#229)

  • GET /api/codegraph/history?file= returns recent commits via git log
  • Hash, message context for each commit

Live Codebase Q&A (#239)

  • GET /api/codegraph/qa?q=&project= returns symbol citations with provenance
  • Citations include file, line, signature, and language context

Architecture Fitness (#238)

  • GET /api/codegraph/fitness?project= runs naming conventions, circular dependency, and layer isolation checks

Codebase Pilot Config (#295)

  • GET /api/codegraph/pilot-config returns token budget and pruning mode
  • PUT /api/codegraph/pilot-config updates pilot settings

Alcove Private Documentation (#294)

  • GET /api/alcove/search?q= performs semantic search over .cortex/data/docs/
  • Returns snippets with surrounding context lines from markdown/text files

Clone this wiki locally