Skip to main content

CODITECT Database Schema Reference

Governance: ADR-118, ADR-119


Overview

CODITECT uses a four-tier database architecture (ADR-118) with multi-tenant support (ADR-119):

┌─────────────────────────────────────────────────────────────────┐
│ FOUR-TIER ARCHITECTURE │
├─────────────────────────────────────────────────────────────────┤
│ TIER 1: platform.db (Read-Only, Regenerable) │
│ └── Component registry: agents, skills, commands, scripts │
│ └── Location: ~/.coditect/framework-data/ │
├─────────────────────────────────────────────────────────────────┤
│ TIER 2: org.db (CRITICAL - Daily Backup Required) │
│ └── IRREPLACEABLE: skill_learnings, decisions, error_solutions │
│ └── Location: ~/PROJECTS/.coditect-data/context-storage/ │
├─────────────────────────────────────────────────────────────────┤
│ TIER 3: sessions.db (Regenerable from JSONL) │
│ └── Messages, tool_analytics, token_economics, code_patterns │
│ └── Location: ~/PROJECTS/.coditect-data/context-storage/ │
├─────────────────────────────────────────────────────────────────┤
│ TIER 4: project.db (Project-Specific) │
│ └── Project-local data, work items, sprint tracking │
│ └── Location: <project>/.coditect-data/ │
└─────────────────────────────────────────────────────────────────┘

Quick Reference

TierDatabaseBackupSizeTablesPrimary Use
1platform.dbNOT REQUIRED101 MB15Component metadata
2org.dbDAILY416 MB28Critical knowledge + knowledge graph
3sessions.dbOptional38 GB96Session data + context graphs
4projects.dbWith code9.6 MB12Project-specific
-messaging.dbOptional1.2 MB9Inter-session coordination (ADR-160)
-call_graph.dbNOT REQUIRED0 B0Code call graph (MCP server)

Database Locations

# Tier 1: Platform (framework installation)
~/.coditect/framework-data/platform.db

# Tier 2 & 3: User data
~/PROJECTS/.coditect-data/context-storage/org.db # CRITICAL
~/PROJECTS/.coditect-data/context-storage/sessions.db # Regenerable

# Tier 4: Project-specific
<project>/.coditect-data/project.db

# Inter-session coordination (ADR-160)
~/PROJECTS/.coditect-data/context-storage/messaging.db # Session bus

# Code analysis (MCP server)
~/PROJECTS/.coditect-data/context-storage/call_graph.db # Call graph index

# DEPRECATED (do not use in new code)
~/PROJECTS/.coditect-data/context-storage/context.db # → migrate to org.db + sessions.db

Tier 2: org.db (CRITICAL)

⚠️ BACKUP DAILY - Contains irreplaceable organizational knowledge

Tables

TableRowsDescriptionStatus
skill_learnings758K+AI learning patternsIRREPLACEABLE
decisions1.8K+Architectural decisionsIRREPLACEABLE
error_solutions475+Error-solution pairsIRREPLACEABLE
kg_nodes16.9K+Knowledge graph nodes (ADR-151)IRREPLACEABLE
kg_edges7.1K+Knowledge graph edges (ADR-151)IRREPLACEABLE
kg_nodes_fts-FTS5 index for kg_nodesAuto-synced
cloud_sync_state-Cloud sync trackingOperational
local_identity-Tenant/user identityOperational

Key Schemas

-- skill_learnings: Accumulated AI patterns
CREATE TABLE skill_learnings (
id INTEGER PRIMARY KEY,
session_id TEXT NOT NULL,
skill_name TEXT NOT NULL,
outcome TEXT,
effectiveness_score INTEGER,
errors TEXT,
analyzed_at TEXT NOT NULL,
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT,
cloud_id TEXT, synced_at TEXT, sync_status TEXT DEFAULT 'pending',
UNIQUE(session_id, skill_name)
);

-- decisions: Architectural decisions
CREATE TABLE decisions (
id INTEGER PRIMARY KEY,
message_id INTEGER,
project_path TEXT,
decision_type TEXT NOT NULL,
decision TEXT NOT NULL,
rationale TEXT,
alternatives_considered TEXT,
confidence REAL DEFAULT 0.5,
tags TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT,
cloud_id TEXT, synced_at TEXT, sync_status TEXT DEFAULT 'pending'
);

-- error_solutions: Known fixes
CREATE TABLE error_solutions (
id INTEGER PRIMARY KEY,
error_hash TEXT UNIQUE,
error_type TEXT NOT NULL,
error_signature TEXT NOT NULL,
solution TEXT NOT NULL,
success_count INTEGER DEFAULT 1,
failure_count INTEGER DEFAULT 0,
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT,
cloud_id TEXT, synced_at TEXT, sync_status TEXT DEFAULT 'pending'
);

-- kg_nodes: Knowledge graph entities (ADR-151)
CREATE TABLE kg_nodes (
id TEXT PRIMARY KEY, -- "{type}:{uuid}" e.g., "decision:42"
node_type TEXT NOT NULL, -- component, session, decision, error_solution,
-- skill_learning, file, function, track, adr
subtype TEXT, -- component: agent/skill/command/script/hook
name TEXT NOT NULL, -- Display name
properties TEXT, -- JSON properties bag (type-specific)
embedding BLOB, -- 1536-dim float32 vector
tenant_id TEXT, project_id TEXT,
created_at TEXT NOT NULL, updated_at TEXT NOT NULL,
source_table TEXT, source_id TEXT -- Provenance tracking
);
-- Indexes: node_type, subtype, tenant, project, source, updated_at
-- FTS5: kg_nodes_fts(name, properties)

-- kg_edges: Knowledge graph relationships (ADR-151)
CREATE TABLE kg_edges (
id TEXT PRIMARY KEY,
edge_type TEXT NOT NULL, -- INVOKES, PRODUCES, SOLVES, BELONGS_TO,
-- DEFINES, SIMILAR_TO, REFERENCES, CALLS,
-- USES, GOVERNED_BY, CREATED_BY
from_node TEXT NOT NULL, -- Source kg_nodes.id
to_node TEXT NOT NULL, -- Target kg_nodes.id
properties TEXT, -- JSON: weight, context, metadata
tenant_id TEXT,
created_at TEXT NOT NULL,
UNIQUE(from_node, to_node, edge_type),
FOREIGN KEY (from_node) REFERENCES kg_nodes(id) ON DELETE CASCADE,
FOREIGN KEY (to_node) REFERENCES kg_nodes(id) ON DELETE CASCADE
);
-- Indexes: edge_type, from_node, to_node, tenant, (from_node, to_node)

Knowledge Graph Statistics (ADR-151)

Node types (16,932 total):

TypeCountSource
function7,066Call graph analysis
component5,411Component indexer
decision1,856/cx extraction
skill_learning1,029Session retrospectives
file577Project indexer
error_solution475/cx extraction
session449Session tracking
track37PILOT tracks A-AK
adr32ADR documents

Edge types (7,118 total):

TypeCountMeaning
INVOKES3,363Component invokes another
CALLS2,565Function calls function
USES493Entity uses another
SOLVES475Error solution resolves error
BELONGS_TO220Membership relationship
DEFINES1Definition relationship
REFERENCES1Reference relationship

Tier 3: sessions.db (Regenerable)

Regenerable from unified_messages.jsonl - backup optional

Tables

TableRowsDescription
messages251K+Session messages
tool_analytics8.2M+Tool usage analytics
token_economics17.8M+Token usage and costs
session_insights5.7K+Session-level insights
code_patterns10K+Extracted code patterns
embeddings143K+Vector embeddings
context_graphs4+Stored context graph projections (ADR-151)
context_graph_nodes56+Nodes included in context graphs
context_graph_usage-Graph usage tracking and feedback

Key Schemas

-- messages: Session content
CREATE TABLE messages (
id INTEGER PRIMARY KEY,
hash TEXT UNIQUE NOT NULL, -- SHA256 for dedup
content TEXT NOT NULL,
role TEXT NOT NULL, -- user, assistant, system
session_id TEXT,
timestamp TEXT,
has_code BOOLEAN,
tool_use TEXT, -- JSON tool calls
tool_result TEXT, -- JSON tool results
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT,
cloud_id TEXT, synced_at TEXT, sync_status TEXT DEFAULT 'pending'
);

-- session_log_entries: Indexed session log entries (J.32)
CREATE TABLE session_log_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT UNIQUE NOT NULL, -- SHA256(source_file:entry_line_number)
source_file TEXT NOT NULL, -- Full path to SESSION-LOG-YYYY-MM-DD.md
source_file_mtime REAL, -- File mtime for change detection
project_id TEXT, -- From directory path (e.g., "PILOT")
machine_uuid TEXT, -- From directory path
log_date TEXT NOT NULL, -- YYYY-MM-DD from filename
entry_timestamp TEXT, -- ISO 8601 from heading (nullable)
task_ids TEXT, -- Comma-separated (e.g., "H.13.9,H.0")
author TEXT, -- e.g., "Claude (Opus 4.6)"
heading TEXT NOT NULL, -- Entry heading text
content TEXT NOT NULL, -- Full markdown content
content_length INTEGER DEFAULT 0,
files_modified TEXT, -- JSON array of files mentioned
has_tables INTEGER DEFAULT 0,
has_code INTEGER DEFAULT 0,
session_uuid TEXT, -- From YAML frontmatter sessions[].id
extracted_at TEXT NOT NULL
);

CREATE INDEX idx_sle_project ON session_log_entries(project_id);
CREATE INDEX idx_sle_date ON session_log_entries(log_date);
CREATE INDEX idx_sle_task ON session_log_entries(task_ids);
CREATE INDEX idx_sle_source ON session_log_entries(source_file);

-- tool_analytics: Tool execution tracking
CREATE TABLE tool_analytics (
id INTEGER PRIMARY KEY,
session_id TEXT NOT NULL,
tool_name TEXT NOT NULL,
status TEXT NOT NULL, -- success, failed, timeout
execution_time_ms INTEGER,
error_message TEXT,
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT
);

-- token_economics: Cost tracking
CREATE TABLE token_economics (
id INTEGER PRIMARY KEY,
session_id TEXT NOT NULL,
model_name TEXT NOT NULL, -- opus, sonnet, haiku
token_input INTEGER DEFAULT 0,
token_output INTEGER DEFAULT 0,
cost_total_usd REAL,
-- Multi-tenant (ADR-119)
tenant_id TEXT, user_id TEXT, team_id TEXT, project_id TEXT
);

-- context_graphs: Stored graph projections (ADR-151)
CREATE TABLE context_graphs (
id TEXT PRIMARY KEY, -- "cg:{timestamp}:{hash}"
name TEXT, -- Human-readable name
task_description TEXT, -- Task/query that prompted this graph
seed_nodes TEXT, -- JSON array of seed node IDs
seed_strategy TEXT DEFAULT 'anchor', -- 'anchor', 'semantic', 'policy_first'
token_budget INTEGER DEFAULT 4000,
max_depth INTEGER DEFAULT 3,
max_nodes INTEGER DEFAULT 128,
relevance_threshold REAL DEFAULT 0.5,
node_count INTEGER DEFAULT 0,
edge_count INTEGER DEFAULT 0,
tokens_estimated INTEGER DEFAULT 0,
tenant_id TEXT, project_id TEXT, session_id TEXT,
created_at TEXT NOT NULL, expires_at TEXT,
build_time_ms INTEGER, builder_version TEXT DEFAULT '1.0.0',
policies_applied TEXT, -- J.25.4.4: JSON array of governance policies applied
phi_node_count INTEGER DEFAULT 0 -- J.25.4.3: Count of nodes with potential PHI indicators
);

-- context_graph_nodes: Nodes included in a context graph
CREATE TABLE context_graph_nodes (
context_graph_id TEXT NOT NULL, -- References context_graphs.id
node_id TEXT NOT NULL, -- References kg_nodes.id (in org.db)
relevance_score REAL DEFAULT 1.0, -- 0.0-1.0 relevance to task
depth INTEGER DEFAULT 0, -- BFS depth from seed
is_seed INTEGER DEFAULT 0,
include_properties INTEGER DEFAULT 1,
token_estimate INTEGER DEFAULT 0,
PRIMARY KEY (context_graph_id, node_id),
FOREIGN KEY (context_graph_id) REFERENCES context_graphs(id) ON DELETE CASCADE
);

-- context_graph_usage: Usage tracking and feedback loop
CREATE TABLE context_graph_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
context_graph_id TEXT NOT NULL,
session_id TEXT, message_id TEXT, agent_id TEXT,
tokens_used INTEGER, retrieval_time_ms INTEGER,
was_helpful INTEGER, -- 1=helpful, 0=not, NULL=unknown
feedback_text TEXT,
used_at TEXT NOT NULL,
FOREIGN KEY (context_graph_id) REFERENCES context_graphs(id) ON DELETE CASCADE
);

Tier 1: platform.db (Read-Only)

Component registry - regenerated by component-indexer.py

Tables

TableDescription
componentsFramework components (agents, skills, commands)
capabilitiesComponent capabilities
component_metadataExtended metadata
component_frontmatterYAML frontmatter

Multi-Tenant Columns (ADR-119)

All user data tables include:

-- Identity columns
tenant_id TEXT NOT NULL, -- Organization UUID
user_id TEXT NOT NULL, -- User UUID
team_id TEXT, -- Team scope (optional)
project_id TEXT, -- Project scope (optional)

-- Cloud sync columns
cloud_id TEXT UNIQUE, -- Cloud record UUID
synced_at TEXT, -- Last sync (RFC3339)
sync_status TEXT DEFAULT 'pending' -- pending | synced | conflict

Standard Indexes

CREATE INDEX idx_{table}_tenant ON {table}(tenant_id);
CREATE INDEX idx_{table}_user ON {table}(tenant_id, user_id);
CREATE INDEX idx_{table}_project ON {table}(tenant_id, project_id);
CREATE INDEX idx_{table}_sync ON {table}(sync_status, synced_at);
CREATE INDEX idx_{table}_cloud_id ON {table}(cloud_id);

Full-Text Search (FTS5)

FTS TableBase TableDatabaseIndexed Columns
messages_ftsmessagessessions.dbcontent, role
decisions_ftsdecisionsorg.dbdecision, rationale
errors_ftserror_solutionsorg.dberror_signature, solution
session_log_ftssession_log_entriessessions.dbheading, content, task_ids
kg_nodes_ftskg_nodesorg.dbname, properties
doc_searchdoc_indexsessions.dbtitle, summary, content_preview
component_searchcomponentsplatform.dbname, description

Query Examples

-- Search messages (Tier 3)
SELECT * FROM messages
WHERE id IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH 'authentication');

-- Search decisions (Tier 2)
SELECT * FROM decisions
WHERE id IN (SELECT rowid FROM decisions_fts WHERE decisions_fts MATCH 'architecture');

Python API

from scripts.core.paths import get_org_db_path, get_sessions_db_path

# Tier 2: Critical data
ORG_DB = get_org_db_path() # ~/PROJECTS/.coditect-data/context-storage/org.db

# Tier 3: Session data
SESSIONS_DB = get_sessions_db_path() # ~/PROJECTS/.coditect-data/context-storage/sessions.db

# Connect
import sqlite3
conn = sqlite3.connect(ORG_DB)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM decisions")

CLI Access

# Tier 2: Decisions
sqlite3 ~/PROJECTS/.coditect-data/context-storage/org.db "SELECT COUNT(*) FROM decisions"

# Tier 3: Messages
sqlite3 ~/PROJECTS/.coditect-data/context-storage/sessions.db "SELECT COUNT(*) FROM messages"

# Via /cxq command (auto-routes to correct DB)
/cxq --decisions --limit 10
/cxq --recent 50
/cxq --stats

Migration

From context.db to Four-Tier

# 1. Dry run
python3 scripts/migrate-to-four-tier-db.py --dry-run

# 2. Execute
python3 scripts/migrate-to-four-tier-db.py

# 3. Verify
python3 scripts/migrate-to-four-tier-db.py --verify

Add Multi-Tenant Columns

python3 scripts/migrate-multi-tenant-schema.py --dry-run
python3 scripts/migrate-multi-tenant-schema.py

Backup

# Backup critical org.db to GCS (REQUIRED - daily)
~/.coditect/scripts/backup-context-db.sh

# Restore
~/.coditect/scripts/backup-context-db.sh --restore latest

Bucket: gs://coditect-cloud-infra-context-backups


DocumentDescription
four-tier-database-schema.mdDetailed table definitions
ADR-118Four-tier architecture decision
ADR-119Multi-tenant schema decision
ADR-151Knowledge graph architecture
CONTEXT-GRAPH-GUIDE.mdContext graph usage guide
MEMORY-MANAGEMENT-GUIDE.md/cx, /cxq usage

Statistics (as of 2026-02-19)

DatabaseTableRowsStatus
org.dbskill_learnings758,358IRREPLACEABLE
org.dbdecisions1,856IRREPLACEABLE
org.dberror_solutions475IRREPLACEABLE
org.dbkg_nodes18,622IRREPLACEABLE
org.dbkg_edges12,023IRREPLACEABLE
org.dbfile_integrity_audit388,274Operational
sessions.dbmessages718,721Regenerable
sessions.dbtool_analytics53,394,316Regenerable
sessions.dbtoken_economics73,733,714Regenerable
sessions.dbembeddings650,425Regenerable
sessions.dbsession_log_entries3,765Regenerable
sessions.dbcontext_graphs4Regenerable
sessions.dbcontext_graph_nodes56Regenerable
platform.dbcomponents10,405Regenerable
platform.dbcapabilities242,741Regenerable
platform.dbtriggers82,587Regenerable
projects.dbprojects18Project-specific
projects.dbcontent_hashes23,411Regenerable
messaging.dbsession_registry48Operational
messaging.dbsession_messages423Operational
messaging.dbtask_claims2Operational

Last Updated: 2026-02-19 Version: 2.3.0