Skip to main content

Database Schema

Complete reference for the KOSMOS PostgreSQL database schema.

Auto-Generated

This documentation is automatically extracted from SQL migration files.

Entity Relationship Diagram​

Database Schemas​

KOSMOS uses a multi-schema PostgreSQL design for logical separation:

SchemaPurpose
agentsAgent registry, state, and metrics
auditImmutable audit logging
coreUsers, tenants, conversations, messages
governancePentarchy voting, proposals, cost tracking
knowledgeRAG documents, embeddings, knowledge graph
metricsPerformance and operational metrics
routingApplication data

agents Schema​

registry​

AGENTS SCHEMA - Agent State and Configuration

ColumnTypeNullableDefaultReferences
id PKVARCHAR(50)YES--
nameVARCHAR(100)NO--
domainVARCHAR(100)NO--
descriptionTEXTYES--
toolsJSONBYES'[]'-
mcp_serversJSONBYES'[]'-
configJSONBYES'{}'-
statusVARCHAR(20)YES'active'-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

state​

Agent state (for persistence)

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
agent_idVARCHAR(50)NO-agents.registry.id
conversation_idUUIDYES-core.conversations.id
stateJSONBNO--
versionINTEGERNO1-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

metrics​

Agent metrics

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
agent_idVARCHAR(50)NO-agents.registry.id
metric_nameVARCHAR(100)NO--
metric_valueDOUBLE PRECISIONNO--
labelsJSONBYES'{}'-
recorded_atTIMESTAMPTZYESNOW()-

audit Schema​

events​

AUDIT SCHEMA - Immutable Audit Log

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO--
event_typeVARCHAR(100)NO--
actor_idVARCHAR(100)NO--
resource_typeVARCHAR(100)YES--
resource_idVARCHAR(100)YES--
actionVARCHAR(50)NO--
detailsJSONBYES'{}'-
ip_addressINETYES--
user_agentTEXTYES--
created_atTIMESTAMPTZYESNOW()-

core Schema​

tenants​

CORE SCHEMA - Tenants, Users, Conversations

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
nameVARCHAR(255)NO--
slugVARCHAR(100)NO--
settingsJSONBYES'{}'-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

users​

Users table

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
external_idVARCHAR(255),YES--
emailVARCHAR(255)NO--
nameVARCHAR(255)YES--
roleVARCHAR(50)YES'user'-
preferencesJSONBYES'{}'-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

conversations​

Conversations table

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
user_idUUIDNO-core.users.id
titleVARCHAR(500)YES--
metadataJSONBYES'{}'-
started_atTIMESTAMPTZYESNOW()-
ended_atTIMESTAMPTZYES--
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

messages​

Messages table

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
conversation_idUUIDNO-core.conversations.id
tenant_idUUIDNO-core.tenants.id
contentTEXTNO--
agent_idVARCHAR(50)YES--
tool_callsJSONBYES--
tokens_inputINTEGERYES--
tokens_outputINTEGERYES--
cost_usdDECIMAL(10, 6)YES--
latency_msINTEGERYES--
created_atTIMESTAMPTZYESNOW()-

governance Schema​

proposals​

GOVERNANCE SCHEMA - Pentarchy Voting and Approvals

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
conversation_idUUIDYES-core.conversations.id
actionVARCHAR(200)NO--
descriptionTEXTYES--
estimated_costDECIMAL(10, 2)YES--
contextJSONBYES'{}'-
outcomeVARCHAR(20)YES--
decided_byVARCHAR(50)YES--
created_atTIMESTAMPTZYESNOW()-
resolved_atTIMESTAMPTZYES--

votes​

Votes

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
proposal_idUUIDNO-governance.proposals.id
voter_agentVARCHAR(50)NO--
reasoningTEXTYES--
created_atTIMESTAMPTZYESNOW()-

cost_tracking​

Cost tracking

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
proposal_idUUIDYES-governance.proposals.id
tool_nameVARCHAR(100)NO--
agent_idVARCHAR(50)YES--
estimated_costDECIMAL(12, 4)NO--
actual_costDECIMAL(12, 4)YES--
variance_pctDECIMAL(5, 2)YES--
governance_decisionVARCHAR(20)YES--
approved_byVARCHAR(50)YES--
created_atTIMESTAMPTZYESNOW()-
completed_atTIMESTAMPTZYES--

knowledge Schema​

documents​

KNOWLEDGE SCHEMA - RAG and Documents

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
source_urlTEXTYES--
titleVARCHAR(500)YES--
contentTEXTNO--
content_typeVARCHAR(50)YES'text/plain'-
metadataJSONBYES'{}'-
statusVARCHAR(20)YES'active'-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

chunks​

Document chunks with embeddings

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
document_idUUIDNO-knowledge.documents.id
tenant_idUUIDNO-core.tenants.id
contentTEXTNO--
embeddingvector(768),YES--
chunk_indexINTEGERNO--
token_countINTEGERYES--
metadataJSONBYES'{}'-
created_atTIMESTAMPTZYESNOW()-

entities​

Full-text search column

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
user_idUUIDYES-core.users.id
nameVARCHAR(255)NO--
entity_typeVARCHAR(100)NO--
observationsJSONBYES'[]'-
embeddingvector(1536),YES--
importance_scoreFLOATYES0.5-
decay_rateFLOATYES0.01-
created_atTIMESTAMPTZYESNOW()-
accessed_atTIMESTAMPTZYESNOW()-

relations​

Memory relations

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO-core.tenants.id
from_entity_idUUIDNO-knowledge.entities.id
to_entity_idUUIDNO-knowledge.entities.id
relation_typeVARCHAR(100)NO--
propertiesJSONBYES'{}'-
created_atTIMESTAMPTZYESNOW()-

routing Schema​

intent_categories​

INTENT TAXONOMY - Hierarchical Intent Classification

ColumnTypeNullableDefaultReferences
id PKVARCHAR(50)YES--
nameVARCHAR(100)NO--
descriptionTEXTYES--
parent_category_idVARCHAR(50)YES-routing.intent_categories.id
priority_weightDECIMAL(3, 2)YES1.0-
created_atTIMESTAMPTZYESNOW()-

intents​

Intent definitions with embeddings

ColumnTypeNullableDefaultReferences
id PKVARCHAR(100)YES--
category_idVARCHAR(50)NO-routing.intent_categories.id
nameVARCHAR(200)NO--
descriptionTEXTYES--
example_utterancesTEXT[]NO--
embeddingvector(768),YES--
target_agentVARCHAR(50)NO--
secondary_agentsVARCHAR(50)[]YES'{}'-
required_capabilitiesVARCHAR(100)[]YES'{}'-
confidence_thresholdDECIMAL(3, 2)YES0.75-
priorityINTEGERYES5-
metadataJSONBYES'{}'-
is_activeBOOLEANYESTRUE-
created_atTIMESTAMPTZYESNOW()-
updated_atTIMESTAMPTZYESNOW()-

intent_utterance_embeddings​

Intent embeddings for all example utterances

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
intent_idVARCHAR(100)NO-routing.intents.id
utteranceTEXTNO--
embeddingvector(768)NO--
created_atTIMESTAMPTZYESNOW()-

keyword_rules​

ROUTING RULES - Fallback and Override Logic

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
patternTEXTNO--
target_agentVARCHAR(50)NO--
priorityINTEGERYES5-
confidence_boostDECIMAL(3, 2)YES0.0-
is_activeBOOLEANYESTRUE-
created_atTIMESTAMPTZYESNOW()-

context_overrides​

Context-aware routing overrides

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
context_conditionJSONBNO--
target_agentVARCHAR(50)NO--
priorityINTEGERYES10-
valid_duration_minutesINTEGERYES60-
is_activeBOOLEANYESTRUE-
created_atTIMESTAMPTZYESNOW()-

decisions​

ROUTING HISTORY - Analytics and Learning

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
tenant_idUUIDNO--
conversation_idUUIDYES--
message_idUUIDYES--
input_textTEXTNO--
input_embeddingvector(768)YES--
matched_intent_idVARCHAR(100)YES-routing.intents.id
semantic_confidenceDECIMAL(5, 4)YES--
keyword_matchBOOLEANYESFALSE-
context_overrideBOOLEANYESFALSE-
selected_agentVARCHAR(50)NO--
alternative_agentsJSONBYES'[]'-
routing_latency_msINTEGERYES--
created_atTIMESTAMPTZYESNOW()-

performance_metrics​

Routing performance aggregates (materialized for dashboards)

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
metric_dateDATENO--
intent_idVARCHAR(100)YES--
agent_idVARCHAR(50)YES--
total_routesINTEGERYES0-
avg_confidenceDECIMAL(5, 4)YES--
avg_latency_msDECIMAL(10, 2)YES--
positive_feedbackINTEGERYES0-
negative_feedbackINTEGERYES0-
misroutesINTEGERYES0-
created_atTIMESTAMPTZYESNOW()-

experiments​

A/B TESTING - Routing Strategy Experiments

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
nameVARCHAR(200)NO--
descriptionTEXTYES--
strategy_aJSONBNO--
strategy_bJSONBNO--
traffic_splitDECIMAL(3, 2)YES0.5-
started_atTIMESTAMPTZYES--
ended_atTIMESTAMPTZYES--
winnerVARCHAR(10),YES--
created_atTIMESTAMPTZYESNOW()-

experiment_assignments​

'a', 'b', or null

ColumnTypeNullableDefaultReferences
id PKUUIDYESuuid_generate_v4()-
experiment_idUUIDNO-routing.experiments.id
user_idUUIDNO--
created_atTIMESTAMPTZYESNOW()-

Indexes​

Performance-optimized indexes:

IndexTableColumns
idx_users_tenantcore.userstenant_id
idx_users_emailcore.usersemail
idx_conversations_tenantcore.conversationstenant_id
idx_conversations_usercore.conversationsuser_id
idx_messages_conversationcore.messagesconversation_id
idx_messages_createdcore.messagescreated_at
idx_agent_state_agentagents.stateagent_id
idx_agent_state_conversationagents.stateconversation_id
idx_agent_metrics_agentagents.metricsagent_id
idx_agent_metrics_recordedagents.metricsrecorded_at
idx_proposals_tenantgovernance.proposalstenant_id
idx_proposals_statusgovernance.proposalsstatus
idx_votes_proposalgovernance.votesproposal_id
idx_cost_tracking_tenantgovernance.cost_trackingtenant_id
idx_documents_tenantknowledge.documentstenant_id
idx_chunks_documentknowledge.chunksdocument_id
idx_chunks_tenantknowledge.chunkstenant_id
idx_entities_tenantknowledge.entitiestenant_id
idx_entities_userknowledge.entitiesuser_id
idx_entities_typeknowledge.entitiesentity_type
idx_relations_fromknowledge.relationsfrom_entity_id
idx_relations_toknowledge.relationsto_entity_id
idx_audit_tenantaudit.eventstenant_id
idx_audit_createdaudit.eventscreated_at
idx_audit_actoraudit.eventsactor_type, actor_id
idx_audit_resourceaudit.eventsresource_type, resource_id
idx_intents_categoryrouting.intentscategory_id
idx_intents_agentrouting.intentstarget_agent
idx_intents_activerouting.intentsis_active
idx_utterances_intentrouting.intent_utterance_embeddingsintent_id
idx_decisions_tenantrouting.decisionstenant_id
idx_decisions_createdrouting.decisionscreated_at
idx_decisions_agentrouting.decisionsselected_agent
idx_decisions_intentrouting.decisionsmatched_intent_id
idx_performance_daterouting.performance_metricsmetric_date
idx_keyword_rules_activerouting.keyword_rulesis_active
idx_context_overrides_activerouting.context_overridesis_active

Row Level Security​

KOSMOS implements multi-tenancy using PostgreSQL Row Level Security (RLS):

-- Example: Tenant isolation policy
CREATE POLICY tenant_isolation_users ON core.users
USING (tenant_id = current_setting('app.current_tenant', true)::uuid);

All tenant-scoped tables have RLS enabled to ensure data isolation.

Extensions​

Required PostgreSQL extensions:

ExtensionPurpose
uuid-osspUUID generation
pgvectorVector embeddings for RAG
pg_trgmFuzzy text search