Skip to content

Core Concepts

The Code Graph

kc-graph represents your codebase as a directed graph where:

  • Nodes are code entities (files, functions, classes, variables, types, docs)
  • Edges are relationships between them (contains, calls, imports, extends, etc.)

This structure lets AI agents navigate your code by following relationships instead of reading entire files.

Node Types

TypeDescriptionExample
fileA source filesrc/auth.ts
moduleA module/namespace@app/auth
classA class definitionclass UserService
functionA function or methodfunction login()
variableA variable/constantconst MAX_RETRIES = 3
typeA type or interfaceinterface User
docDocumentationREADME section, JSDoc block
snippetA code snippetInline code for RAG

Every node has:

  • id — unique identifier (typically filePath#symbolName)
  • name — human-readable name
  • content — the actual source code or text
  • signature — function/type signature
  • location — file path and line numbers
  • embedding — optional Float32Array for semantic search

Edge Types

TypeMeaningExample
containsParent contains childFile contains function
callsA calls Blogin() calls validate()
importsA imports from BFile imports from module
extendsA extends BAdminUser extends User
implementsA implements BAuthService implements IAuth
referencesA references BFunction uses variable
exportsA exports BFile exports function
depends_onA depends on BModule dependency
documentsA documents BDoc describes function
tagged_withA tagged with BEntity has category

Identifiers

kc-graph uses qualified names as node IDs:

src/auth.ts                    → file node
src/auth.ts#login              → function node
src/auth.ts#AuthService        → class node
src/auth.ts#AuthService.verify → method node

The graph.resolve() method accepts any of: full ID, qualified name, or plain symbol name.

Indexes

The graph maintains several indexes for fast lookups:

  • By typegraph.findByType('function') → O(1)
  • By filegraph.findByFile('src/auth.ts') → O(1)
  • By namegraph.findByName('login') → O(1), case-insensitive
  • Adjacencygraph.getSuccessors(id) / graph.getPredecessors(id) → O(edges)

V8 Optimizations

kc-graph is designed for maximum V8 engine performance:

  1. Monomorphic hidden classes — All nodes use the same interface shape. V8 assigns one hidden class to all CodeNode objects, keeping property lookups fast via inline caching.

  2. Map over ObjectMap<string, T> is 2-5x faster than plain objects for frequent insertions and deletions of dynamic keys.

  3. Float32Array — Embeddings use typed arrays: 4x less memory than number[], and eligible for SIMD optimization by TurboFan.

  4. Integer timestampsDate.now() returns a number. We never create Date objects (avoids heap allocation).

  5. Generator-based traversal — BFS/DFS use generators for lazy evaluation. If you only need the first 10 results, only 10 nodes are visited.

  6. Array-backed BFS queue — Uses a head pointer instead of Array.shift(), avoiding O(n) moves on each dequeue.

Released under the MIT License.