-- Knowledge Platform schema migration
-- Migration id: 0001_initial_schema
-- Dialect: tidb

CREATE TABLE IF NOT EXISTS `knowledge_spaces` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `slug` VARCHAR(160) NOT NULL, `name` TEXT NOT NULL, `description` TEXT, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL);
CREATE TABLE IF NOT EXISTS `knowledge_space_manifests` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `manifest_version` INT NOT NULL, `storage_provider` TEXT NOT NULL, `object_key_prefix` TEXT NOT NULL, `metadata_dialect` TEXT NOT NULL, `parser_policy_version` TEXT NOT NULL, `node_schema_version` INT NOT NULL, `projection_set_version` TEXT NOT NULL, `min_client_version` TEXT NOT NULL, `retention_policy` JSON NOT NULL, `quota_policy` JSON NOT NULL, `consistency_policy` JSON NOT NULL, `encryption_policy` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `sources` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `type` TEXT NOT NULL, `status` VARCHAR(16) NOT NULL, `name` TEXT NOT NULL, `uri` TEXT NOT NULL, `metadata` JSON NOT NULL, `permission_scope` JSON NOT NULL, `version` INT NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `resource_mounts` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `mount_path` VARCHAR(384) NOT NULL, `resource_type` VARCHAR(64) NOT NULL, `provider` TEXT NOT NULL, `mode` TEXT NOT NULL, `capabilities` JSON NOT NULL, `source_pointer` TEXT NOT NULL, `permission_scope` JSON NOT NULL, `permission_snapshot_version` INT NOT NULL, `freshness_policy` JSON NOT NULL, `cache_policy` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `last_synced_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `document_assets` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `source_id` CHAR(36), `filename` TEXT NOT NULL, `mime_type` TEXT NOT NULL, `object_key` TEXT NOT NULL, `sha256` TEXT NOT NULL, `size_bytes` INT NOT NULL, `version` INT NOT NULL, `parser_status` VARCHAR(16) NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `parse_artifacts` (`id` CHAR(36) PRIMARY KEY NOT NULL, `document_asset_id` CHAR(36) NOT NULL, `version` INT NOT NULL, `parser` TEXT NOT NULL, `content_type` TEXT NOT NULL, `artifact_hash` VARCHAR(64) NOT NULL, `elements` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`document_asset_id`) REFERENCES `document_assets` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `document_multimodal_manifests` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `document_asset_id` CHAR(36) NOT NULL, `parse_artifact_id` CHAR(36) NOT NULL, `version` INT NOT NULL, `artifact_hash` TEXT NOT NULL, `manifest_version` TEXT NOT NULL, `items` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`document_asset_id`) REFERENCES `document_assets` (`id`) ON DELETE CASCADE, FOREIGN KEY (`parse_artifact_id`) REFERENCES `parse_artifacts` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `artifact_segments` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `document_asset_id` CHAR(36) NOT NULL, `parse_artifact_id` CHAR(36) NOT NULL, `segment_index` INT NOT NULL, `segment_type` TEXT NOT NULL, `artifact_hash` TEXT NOT NULL, `checksum` VARCHAR(64) NOT NULL, `object_key` TEXT, `inline_text` TEXT, `content_encoding` TEXT NOT NULL, `size_bytes` INT, `start_offset` INT, `end_offset` INT, `source_location` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`document_asset_id`) REFERENCES `document_assets` (`id`) ON DELETE CASCADE, FOREIGN KEY (`parse_artifact_id`) REFERENCES `parse_artifacts` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `knowledge_space_staged_commits` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `operation_type` TEXT NOT NULL, `idempotency_key` VARCHAR(255) NOT NULL, `status` VARCHAR(32) NOT NULL, `raw_object_key` TEXT, `published_object_key` TEXT, `document_asset_id` CHAR(36), `parse_artifact_id` CHAR(36), `projection_fingerprint` TEXT, `checksum` TEXT, `size_bytes` INT, `error_code` TEXT, `error_message` TEXT, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, `expires_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`document_asset_id`) REFERENCES `document_assets` (`id`) ON DELETE SET NULL, FOREIGN KEY (`parse_artifact_id`) REFERENCES `parse_artifacts` (`id`) ON DELETE SET NULL);
CREATE TABLE IF NOT EXISTS `knowledge_fs_sessions` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `client_kind` TEXT NOT NULL, `client_version` TEXT NOT NULL, `subject` JSON NOT NULL, `permission_snapshot` JSON NOT NULL, `consistency_class` TEXT NOT NULL, `heartbeat_at` DATETIME(3) NOT NULL, `expires_at` DATETIME(3) NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `knowledge_fs_leases` (`id` CHAR(36) PRIMARY KEY NOT NULL, `tenant_id` VARCHAR(255) NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `session_id` CHAR(36) NOT NULL, `lease_type` TEXT NOT NULL, `target_type` TEXT NOT NULL, `target_id` TEXT NOT NULL, `target_version` INT, `virtual_path` VARCHAR(384) NOT NULL, `status` VARCHAR(16) NOT NULL, `heartbeat_at` DATETIME(3) NOT NULL, `expires_at` DATETIME(3) NOT NULL, `metadata` JSON NOT NULL, `acquired_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`session_id`) REFERENCES `knowledge_fs_sessions` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `knowledge_nodes` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `document_asset_id` CHAR(36) NOT NULL, `parse_artifact_id` CHAR(36) NOT NULL, `kind` VARCHAR(16) NOT NULL, `text` TEXT NOT NULL, `start_offset` INT NOT NULL, `end_offset` INT NOT NULL, `source_location` JSON NOT NULL, `permission_scope` JSON NOT NULL, `artifact_hash` TEXT NOT NULL, `metadata` JSON NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`document_asset_id`) REFERENCES `document_assets` (`id`) ON DELETE CASCADE, FOREIGN KEY (`parse_artifact_id`) REFERENCES `parse_artifacts` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `index_projections` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `node_id` CHAR(36) NOT NULL, `type` VARCHAR(32) NOT NULL, `status` VARCHAR(16) NOT NULL, `model` VARCHAR(255), `model_key` VARCHAR(255) GENERATED ALWAYS AS (COALESCE(`model`, '')) VIRTUAL, `projection_version` INT NOT NULL, `dense_vector` VECTOR, `visual_vector` VECTOR, `fts_document` TEXT, `metadata` JSON NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`node_id`) REFERENCES `knowledge_nodes` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `embedding_models` (`id` CHAR(36) PRIMARY KEY NOT NULL, `provider` VARCHAR(64) NOT NULL, `model_id` VARCHAR(255) NOT NULL, `version` VARCHAR(128) NOT NULL, `dimension` INT NOT NULL, `metric` TEXT NOT NULL, `tokenizer` TEXT NOT NULL, `max_tokens` INT NOT NULL, `status` VARCHAR(16) NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL);
CREATE TABLE IF NOT EXISTS `knowledge_paths` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `virtual_path` VARCHAR(384) NOT NULL, `resource_type` VARCHAR(64) NOT NULL, `target_id` VARCHAR(512) NOT NULL, `version` INT, `view_type` VARCHAR(16) NOT NULL, `view_name` VARCHAR(64) NOT NULL, `metadata` JSON NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `evidence_bundles` (`id` CHAR(36) PRIMARY KEY NOT NULL, `trace_id` CHAR(36), `query` TEXT NOT NULL, `state` VARCHAR(16) NOT NULL, `items` JSON NOT NULL, `missing_evidence` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3));
CREATE TABLE IF NOT EXISTS `golden_questions` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `question` TEXT NOT NULL, `expected_evidence_ids` JSON NOT NULL, `tags` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `answer_traces` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `evidence_bundle_id` CHAR(36), `query` TEXT NOT NULL, `mode` TEXT NOT NULL, `completed` BOOLEAN NOT NULL, `created_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`evidence_bundle_id`) REFERENCES `evidence_bundles` (`id`) ON DELETE SET NULL);
CREATE TABLE IF NOT EXISTS `answer_trace_steps` (`id` CHAR(36) PRIMARY KEY NOT NULL, `trace_id` CHAR(36) NOT NULL, `name` VARCHAR(64) NOT NULL, `status` VARCHAR(16) NOT NULL, `metadata` JSON NOT NULL, `started_at` DATETIME(3) NOT NULL, `ended_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`trace_id`) REFERENCES `answer_traces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `graph_entities` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `canonical_key` VARCHAR(512) NOT NULL, `type` VARCHAR(64) NOT NULL, `name` VARCHAR(255) NOT NULL, `aliases` JSON NOT NULL, `confidence` DOUBLE NOT NULL, `source_node_ids` JSON NOT NULL, `permission_scope` JSON NOT NULL, `metadata` JSON NOT NULL, `extraction_version` INT NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `graph_relations` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `subject_entity_id` CHAR(36) NOT NULL, `object_entity_id` CHAR(36) NOT NULL, `type` VARCHAR(64) NOT NULL, `confidence` DOUBLE NOT NULL, `source_node_ids` JSON NOT NULL, `permission_scope` JSON NOT NULL, `metadata` JSON NOT NULL, `extraction_version` INT NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE, FOREIGN KEY (`subject_entity_id`) REFERENCES `graph_entities` (`id`) ON DELETE CASCADE, FOREIGN KEY (`object_entity_id`) REFERENCES `graph_entities` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `failed_queries` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `answer_trace_id` CHAR(36), `query` TEXT NOT NULL, `mode` TEXT NOT NULL, `trigger` TEXT NOT NULL, `status` TEXT NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3) NOT NULL, FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS `document_outlines` (`id` CHAR(36) PRIMARY KEY NOT NULL, `knowledge_space_id` CHAR(36) NOT NULL, `document_asset_id` CHAR(36) NOT NULL, `parse_artifact_id` CHAR(36) NOT NULL, `artifact_hash` TEXT NOT NULL, `outline_version` TEXT NOT NULL, `version` INT NOT NULL, `nodes` JSON NOT NULL, `metadata` JSON NOT NULL, `created_at` DATETIME(3) NOT NULL, `updated_at` DATETIME(3), FOREIGN KEY (`knowledge_space_id`) REFERENCES `knowledge_spaces` (`id`) ON DELETE CASCADE);
CREATE UNIQUE INDEX IF NOT EXISTS `knowledge_spaces_tenant_slug_uq` ON `knowledge_spaces` (`tenant_id`, `slug`);
CREATE UNIQUE INDEX IF NOT EXISTS `knowledge_space_manifests_tenant_space_uq` ON `knowledge_space_manifests` (`tenant_id`, `knowledge_space_id`);
CREATE INDEX IF NOT EXISTS `knowledge_space_manifests_tenant_space_idx` ON `knowledge_space_manifests` (`tenant_id`, `knowledge_space_id`, `id`);
CREATE INDEX IF NOT EXISTS `sources_space_status_idx` ON `sources` (`knowledge_space_id`, `status`);
CREATE UNIQUE INDEX IF NOT EXISTS `resource_mounts_space_path_uq` ON `resource_mounts` (`knowledge_space_id`, `mount_path`);
CREATE INDEX IF NOT EXISTS `resource_mounts_space_type_path_idx` ON `resource_mounts` (`knowledge_space_id`, `resource_type`, `mount_path`, `id`);
CREATE INDEX IF NOT EXISTS `document_assets_space_source_version_idx` ON `document_assets` (`knowledge_space_id`, `source_id`, `version`, `id`);
CREATE INDEX IF NOT EXISTS `document_assets_space_status_created_idx` ON `document_assets` (`knowledge_space_id`, `parser_status`, `created_at`, `id`);
CREATE UNIQUE INDEX IF NOT EXISTS `parse_artifacts_asset_version_uq` ON `parse_artifacts` (`document_asset_id`, `version`);
CREATE INDEX IF NOT EXISTS `parse_artifacts_hash_idx` ON `parse_artifacts` (`artifact_hash`);
CREATE UNIQUE INDEX IF NOT EXISTS `document_multimodal_manifests_asset_version_uq` ON `document_multimodal_manifests` (`document_asset_id`, `version`);
CREATE INDEX IF NOT EXISTS `document_multimodal_manifests_space_asset_idx` ON `document_multimodal_manifests` (`knowledge_space_id`, `document_asset_id`, `id`);
CREATE UNIQUE INDEX IF NOT EXISTS `artifact_segments_artifact_index_uq` ON `artifact_segments` (`parse_artifact_id`, `segment_index`);
CREATE INDEX IF NOT EXISTS `artifact_segments_space_artifact_index_idx` ON `artifact_segments` (`knowledge_space_id`, `parse_artifact_id`, `segment_index`, `id`);
CREATE INDEX IF NOT EXISTS `artifact_segments_space_checksum_idx` ON `artifact_segments` (`knowledge_space_id`, `checksum`, `id`);
CREATE INDEX IF NOT EXISTS `artifact_segments_document_source_idx` ON `artifact_segments` (`document_asset_id`, `start_offset`, `id`);
CREATE UNIQUE INDEX IF NOT EXISTS `knowledge_space_staged_commits_idempotency_uq` ON `knowledge_space_staged_commits` (`tenant_id`, `knowledge_space_id`, `idempotency_key`);
CREATE INDEX IF NOT EXISTS `knowledge_space_staged_commits_status_updated_idx` ON `knowledge_space_staged_commits` (`tenant_id`, `knowledge_space_id`, `status`, `updated_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_space_staged_commits_expiry_idx` ON `knowledge_space_staged_commits` (`tenant_id`, `knowledge_space_id`, `expires_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_space_staged_commits_document_idx` ON `knowledge_space_staged_commits` (`document_asset_id`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_fs_sessions_space_expiry_idx` ON `knowledge_fs_sessions` (`tenant_id`, `knowledge_space_id`, `expires_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_fs_sessions_expiry_idx` ON `knowledge_fs_sessions` (`tenant_id`, `expires_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_fs_leases_active_path_idx` ON `knowledge_fs_leases` (`tenant_id`, `knowledge_space_id`, `status`, `virtual_path`, `expires_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_fs_leases_expiry_idx` ON `knowledge_fs_leases` (`tenant_id`, `expires_at`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_fs_leases_session_idx` ON `knowledge_fs_leases` (`tenant_id`, `session_id`, `status`, `id`);
CREATE INDEX IF NOT EXISTS `knowledge_nodes_space_asset_kind_idx` ON `knowledge_nodes` (`knowledge_space_id`, `document_asset_id`, `kind`);
CREATE INDEX IF NOT EXISTS `knowledge_nodes_artifact_offset_idx` ON `knowledge_nodes` (`parse_artifact_id`, `start_offset`, `id`);
CREATE INDEX IF NOT EXISTS `index_projections_space_type_status_idx` ON `index_projections` (`knowledge_space_id`, `type`, `status`, `node_id`, `id`);
CREATE INDEX IF NOT EXISTS `index_projections_node_type_version_idx` ON `index_projections` (`node_id`, `type`, `projection_version`);
CREATE UNIQUE INDEX IF NOT EXISTS `index_projections_node_type_version_model_uq` ON `index_projections` (`node_id`, `type`, `projection_version`, `model_key`);
CREATE UNIQUE INDEX IF NOT EXISTS `embedding_models_model_version_uq` ON `embedding_models` (`model_id`, `version`);
CREATE INDEX IF NOT EXISTS `embedding_models_status_provider_idx` ON `embedding_models` (`status`, `provider`, `model_id`, `id`);
CREATE INDEX IF NOT EXISTS `embedding_models_status_model_idx` ON `embedding_models` (`status`, `model_id`, `id`);
CREATE UNIQUE INDEX IF NOT EXISTS `knowledge_paths_space_path_uq` ON `knowledge_paths` (`knowledge_space_id`, `virtual_path`);
CREATE INDEX IF NOT EXISTS `knowledge_paths_target_idx` ON `knowledge_paths` (`resource_type`, `target_id`);
CREATE INDEX IF NOT EXISTS `knowledge_paths_space_view_path_idx` ON `knowledge_paths` (`knowledge_space_id`, `view_type`, `view_name`, `virtual_path`, `id`);
CREATE INDEX IF NOT EXISTS `evidence_bundles_trace_idx` ON `evidence_bundles` (`trace_id`);
CREATE INDEX IF NOT EXISTS `evidence_bundles_state_created_idx` ON `evidence_bundles` (`state`, `created_at`, `id`);
CREATE INDEX IF NOT EXISTS `golden_questions_space_id_idx` ON `golden_questions` (`knowledge_space_id`, `id`);
CREATE INDEX IF NOT EXISTS `golden_questions_space_created_idx` ON `golden_questions` (`knowledge_space_id`, `created_at`, `id`);
CREATE INDEX IF NOT EXISTS `answer_traces_space_created_idx` ON `answer_traces` (`knowledge_space_id`, `created_at`, `id`);
CREATE INDEX IF NOT EXISTS `answer_traces_bundle_idx` ON `answer_traces` (`evidence_bundle_id`);
CREATE INDEX IF NOT EXISTS `answer_trace_steps_trace_started_idx` ON `answer_trace_steps` (`trace_id`, `started_at`, `id`);
CREATE UNIQUE INDEX IF NOT EXISTS `graph_entities_space_key_uq` ON `graph_entities` (`knowledge_space_id`, `canonical_key`);
CREATE INDEX IF NOT EXISTS `graph_entities_space_type_name_idx` ON `graph_entities` (`knowledge_space_id`, `type`, `name`, `id`);
CREATE INDEX IF NOT EXISTS `graph_relations_subject_traversal_idx` ON `graph_relations` (`knowledge_space_id`, `subject_entity_id`, `type`, `object_entity_id`, `id`);
CREATE INDEX IF NOT EXISTS `graph_relations_object_traversal_idx` ON `graph_relations` (`knowledge_space_id`, `object_entity_id`, `type`, `subject_entity_id`, `id`);
