Skip to main content

Knowledge Graph

The Knowledge Graph is a graph database layer for storing typed entities (nodes), relationships (edges), and Cypher-like queryable views. It is built on Kuzu DB, an embedded graph database, and follows a schema-first approach: define templates, create instances, then populate data.

Knowledge Graph

Storage Layout

All data lives under the project directory:

PathPurpose
{projectPath}/.codebolt/knowledgegraph/kuzu/Kuzu embedded database files
{projectPath}/.codebolt/knowledgegraph/instance-templates/{id}.jsonInstance template definitions
{projectPath}/.codebolt/knowledgegraph/instances/{id}.jsonInstance metadata
{projectPath}/.codebolt/knowledgegraph/view-templates/{id}.jsonView template definitions
{projectPath}/.codebolt/knowledgegraph/views/{id}.jsonView bindings

Kuzu Tables

Node table

ColumnTypeDescription
idSTRINGUnique node identifier
nameSTRINGHuman-readable name
kindSTRINGRecord kind (maps to template definition)
memory_instance_idSTRINGOwning instance
attributesSTRINGJSON-encoded attribute map
valid_fromSTRINGValidity start timestamp
valid_toSTRINGValidity end timestamp
created_atSTRINGCreation timestamp

Edge table

ColumnTypeDescription
idSTRINGUnique edge identifier
kindSTRINGRelationship type
memory_instance_idSTRINGOwning instance
created_atSTRINGCreation timestamp
Schema Version

The current schema version is 2. All timestamp columns use STRING rather than native Kuzu timestamp types to avoid a known crash in the Kuzu engine.

Core Concepts

Instance Template

An instance template is the schema definition for a graph. It declares what node types and edge types are allowed.

  • record_kinds -- A map of node type names to their field definitions.
  • edge_types -- A map of relationship names with from/to constraints that reference record_kinds.

Each field in a record kind has:

PropertyDescription
typeA Kuzu field type (see below)
requiredWhether the field must be present
descriptionHuman-readable explanation
itemTypeElement type when type is LIST

Supported Kuzu field types: STRING, INT64, INT32, INT16, INT8, UINT64, UINT32, UINT16, UINT8, DOUBLE, FLOAT, BOOL, DATE, TIMESTAMP, INTERVAL, UUID, LIST, MAP, STRUCT, BLOB.

Instance

An instance is a live graph created from a template.

  • Linked to a template by templateId.
  • Tracks nodeCount and edgeCount (refreshed from Kuzu on read).
  • Instance ID is derived from a sanitized version of its name.

Memory Record (Node)

A memory record represents a single node in the graph.

FieldDescription
idUnique identifier
nameDisplay name
kindMust match a record_kinds entry in the template
attributesRecord<string, any> -- stored as JSON in Kuzu
valid_fromOptional validity start
valid_toOptional validity end

Attributes are validated against the template schema on every insert and update.

Edge

An edge represents a directed relationship between two nodes.

FieldDescription
idUnique identifier
kindMust match an edge_types entry in the template
from_node_idSource node
to_node_idTarget node

The from and to nodes must match the record kinds declared in the template edge type definition.

View Template

A view template is a reusable query definition written in a Cypher-like DSL.

ClauseRequiredDescription
matchYesStarting node pattern
patternsNoEdge traversal patterns
whereNoFilter conditions
withNoComputed expressions with aliases
orderByNoSort specification
skipNoOffset
limitNoMaximum results
returnYesField selection with aliases
  • applicable_template_ids restricts which instance templates a view template can query.

View

A view is an active binding of a view template to a specific instance. Executing a view runs the translated Cypher query against that instance's data.

Query Translation

The translateViewToCypher() function converts the DSL into executable Cypher:

  1. MATCH clause -- MATCH (alias:Node {kind: 'recordKind', memory_instance_id: instanceId})
  2. Pattern clauses -- Edge traversals respecting direction (outgoing, incoming, both).
  3. WHERE clause -- Condition translation with supported operators:
    • Comparison: =, !=, <>, >, <, >=, <=
    • String: CONTAINS, STARTS WITH, ENDS WITH
    • Collection: IN, NOT IN
    • Null checks: IS NULL, IS NOT NULL
  4. WITH clause -- Computed expressions with aliases.
  5. ORDER BY, SKIP, LIMIT -- Standard pagination and sorting.
  6. RETURN clause -- Field selection with optional aliases.

Schema Validation

Three validation functions enforce data integrity:

  • validateRecordAgainstSchema() -- Checks node attributes against the template's record kind fields. Validates required fields and Kuzu type compatibility.
  • validateEdgeAgainstSchema() -- Confirms that from and to nodes match the template's edge type definitions.
  • validateKuzuFieldType() -- Performs type validation for all supported Kuzu field types.

Subgraph and Graph Visualization

Two endpoints support interactive graph exploration:

getSubgraph(instanceId, limit, seedNodeId)

Retrieves a bounded subgraph starting from an optional seed node. Returns:

  • nodes -- Each node includes inDegree and outDegree counts.
  • edges -- Relationships within the subgraph.
  • totalCounts -- Total nodes and edges in the instance.
  • hasMore -- Whether additional data exists beyond the limit.

expandNode(instanceId, nodeId, direction, limit, excludeNodeIds)

Expands a single node's neighborhood in a given direction. Returns the same structure as getSubgraph.

Corruption Recovery

Kuzu DB corruption is handled automatically:

  1. isCorruptionError() inspects errors for known Kuzu corruption indicators.
  2. deleteCorruptedDatabase() removes the entire Kuzu directory.
  3. initializeDatabase() detects corruption on startup, deletes the corrupt database, and recreates it from scratch.

WebSocket Events

The Knowledge Graph emits 20 event types for real-time UI updates:

CategoryEvents
Instance templatecreated, updated, deleted
Instancecreated, updated, deleted
Memory recordadded, records-added, updated, deleted
Edgeadded, edges-added, deleted
View templatecreated, updated, deleted
Viewcreated, deleted

REST API

Instance Templates

MethodEndpointDescription
POST/kg/instance-templatesCreate a template
GET/kg/instance-templatesList all templates
PUT/kg/instance-templatesUpdate a template
DELETE/kg/instance-templatesDelete a template

Instances

MethodEndpointDescription
POST/kg/instancesCreate an instance
GET/kg/instancesList all instances
DELETE/kg/instancesDelete an instance

Records (Nodes)

MethodEndpointDescription
POST/kg/instances/:id/recordsAdd a record
GET/kg/instances/:id/recordsList records
GET/kg/instances/:id/records/:recordIdGet a single record
PUT/kg/instances/:id/records/:recordIdUpdate a record
DELETE/kg/instances/:id/records/:recordIdDelete a record

Edges

MethodEndpointDescription
POST/kg/instances/:id/edgesAdd an edge
GET/kg/instances/:id/edgesList edges
DELETE/kg/instances/:id/edges/:edgeIdDelete an edge

View Templates

MethodEndpointDescription
POST/kg/view-templatesCreate a view template
GET/kg/view-templatesList view templates
PUT/kg/view-templatesUpdate a view template
DELETE/kg/view-templatesDelete a view template

Views

MethodEndpointDescription
POST/kg/viewsCreate a view
GET/kg/viewsList views
DELETE/kg/viewsDelete a view
GET/kg/views/:id/executeExecute a view query

Graph Exploration

MethodEndpointDescription
GET/kg/instances/:id/subgraphRetrieve a bounded subgraph
POST/kg/instances/:id/expand-nodeExpand a node's neighborhood

SDK Reference

Create an Instance

import codebolt from '@codebolt/codeboltjs';

// Create the template first
const template = await codebolt.knowledgeGraph.createInstanceTemplate({
name: 'codebase-map',
record_kinds: {
Module: {
fields: {
path: { type: 'STRING', required: true, description: 'File path' },
language: { type: 'STRING', required: false, description: 'Programming language' }
}
},
Function: {
fields: {
name: { type: 'STRING', required: true, description: 'Function name' },
lineNumber: { type: 'INT64', required: false, description: 'Start line' }
}
}
},
edge_types: {
CONTAINS: { from: 'Module', to: 'Function' },
CALLS: { from: 'Function', to: 'Function' }
}
});

// Create an instance from the template
const instance = await codebolt.knowledgeGraph.createInstance({
name: 'my-project-map',
templateId: template.id
});

Add Records and Edges

// Add nodes
const moduleNode = await codebolt.knowledgeGraph.addMemoryRecord(instance.id, {
name: 'auth-module',
kind: 'Module',
attributes: { path: 'src/auth/index.ts', language: 'typescript' }
});

const funcNode = await codebolt.knowledgeGraph.addMemoryRecord(instance.id, {
name: 'validateToken',
kind: 'Function',
attributes: { name: 'validateToken', lineNumber: 42 }
});

// Add an edge
await codebolt.knowledgeGraph.addEdge(instance.id, {
kind: 'CONTAINS',
from_node_id: moduleNode.id,
to_node_id: funcNode.id
});

Create and Execute a View

// Define a reusable query
const viewTemplate = await codebolt.knowledgeGraph.createViewTemplate({
name: 'all-functions-in-module',
applicable_template_ids: [template.id],
match: { alias: 'm', recordKind: 'Module' },
patterns: [
{ from: 'm', edge: 'CONTAINS', to: { alias: 'f', recordKind: 'Function' }, direction: 'outgoing' }
],
return: [
{ expression: 'm.name', alias: 'module' },
{ expression: 'f.name', alias: 'function' }
]
});

// Bind it to an instance
const view = await codebolt.knowledgeGraph.createView({
viewTemplateId: viewTemplate.id,
instanceId: instance.id
});

// Execute the query
const results = await codebolt.knowledgeGraph.executeView(view.id);

Delete a Record

await codebolt.knowledgeGraph.deleteMemoryRecord(instance.id, recordId);