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

CREATE TABLE IF NOT EXISTS "knowledge_spaces" ("id" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "slug" TEXT NOT NULL, "name" TEXT NOT NULL, "description" TEXT, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL);
CREATE TABLE IF NOT EXISTS "knowledge_space_manifests" ("id" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "knowledge_space_id" UUID NOT NULL, "manifest_version" INTEGER 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" INTEGER NOT NULL, "projection_set_version" TEXT NOT NULL, "min_client_version" TEXT NOT NULL, "retention_policy" JSONB NOT NULL, "quota_policy" JSONB NOT NULL, "consistency_policy" JSONB NOT NULL, "encryption_policy" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "sources" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "type" TEXT NOT NULL, "status" TEXT NOT NULL, "name" TEXT NOT NULL, "uri" TEXT NOT NULL, "metadata" JSONB NOT NULL, "permission_scope" JSONB NOT NULL, "version" INTEGER NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "resource_mounts" ("id" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "knowledge_space_id" UUID NOT NULL, "mount_path" TEXT NOT NULL, "resource_type" TEXT NOT NULL, "provider" TEXT NOT NULL, "mode" TEXT NOT NULL, "capabilities" JSONB NOT NULL, "source_pointer" TEXT NOT NULL, "permission_scope" JSONB NOT NULL, "permission_snapshot_version" INTEGER NOT NULL, "freshness_policy" JSONB NOT NULL, "cache_policy" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "last_synced_at" TIMESTAMPTZ, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "document_assets" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "source_id" UUID, "filename" TEXT NOT NULL, "mime_type" TEXT NOT NULL, "object_key" TEXT NOT NULL, "sha256" TEXT NOT NULL, "size_bytes" INTEGER NOT NULL, "version" INTEGER NOT NULL, "parser_status" TEXT NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "parse_artifacts" ("id" UUID PRIMARY KEY NOT NULL, "document_asset_id" UUID NOT NULL, "version" INTEGER NOT NULL, "parser" TEXT NOT NULL, "content_type" TEXT NOT NULL, "artifact_hash" TEXT NOT NULL, "elements" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, FOREIGN KEY ("document_asset_id") REFERENCES "document_assets" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "document_multimodal_manifests" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "document_asset_id" UUID NOT NULL, "parse_artifact_id" UUID NOT NULL, "version" INTEGER NOT NULL, "artifact_hash" TEXT NOT NULL, "manifest_version" TEXT NOT NULL, "items" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, 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" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "document_asset_id" UUID NOT NULL, "parse_artifact_id" UUID NOT NULL, "segment_index" INTEGER NOT NULL, "segment_type" TEXT NOT NULL, "artifact_hash" TEXT NOT NULL, "checksum" TEXT NOT NULL, "object_key" TEXT, "inline_text" TEXT, "content_encoding" TEXT NOT NULL, "size_bytes" INTEGER, "start_offset" INTEGER, "end_offset" INTEGER, "source_location" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, 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" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "knowledge_space_id" UUID NOT NULL, "operation_type" TEXT NOT NULL, "idempotency_key" TEXT NOT NULL, "status" TEXT NOT NULL, "raw_object_key" TEXT, "published_object_key" TEXT, "document_asset_id" UUID, "parse_artifact_id" UUID, "projection_fingerprint" TEXT, "checksum" TEXT, "size_bytes" INTEGER, "error_code" TEXT, "error_message" TEXT, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, "expires_at" TIMESTAMPTZ, 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" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "knowledge_space_id" UUID NOT NULL, "client_kind" TEXT NOT NULL, "client_version" TEXT NOT NULL, "subject" JSONB NOT NULL, "permission_snapshot" JSONB NOT NULL, "consistency_class" TEXT NOT NULL, "heartbeat_at" TIMESTAMPTZ NOT NULL, "expires_at" TIMESTAMPTZ NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "knowledge_fs_leases" ("id" UUID PRIMARY KEY NOT NULL, "tenant_id" TEXT NOT NULL, "knowledge_space_id" UUID NOT NULL, "session_id" UUID NOT NULL, "lease_type" TEXT NOT NULL, "target_type" TEXT NOT NULL, "target_id" TEXT NOT NULL, "target_version" INTEGER, "virtual_path" TEXT NOT NULL, "status" TEXT NOT NULL, "heartbeat_at" TIMESTAMPTZ NOT NULL, "expires_at" TIMESTAMPTZ NOT NULL, "metadata" JSONB NOT NULL, "acquired_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ 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" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "document_asset_id" UUID NOT NULL, "parse_artifact_id" UUID NOT NULL, "kind" TEXT NOT NULL, "text" TEXT NOT NULL, "start_offset" INTEGER NOT NULL, "end_offset" INTEGER NOT NULL, "source_location" JSONB NOT NULL, "permission_scope" JSONB NOT NULL, "artifact_hash" TEXT NOT NULL, "metadata" JSONB NOT NULL, "updated_at" TIMESTAMPTZ, 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" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "node_id" UUID NOT NULL, "type" TEXT NOT NULL, "status" TEXT NOT NULL, "model" TEXT, "projection_version" INTEGER NOT NULL, "dense_vector" vector(1536), "visual_vector" vector, "fts_document" tsvector, "metadata" JSONB NOT NULL, "updated_at" TIMESTAMPTZ, 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" UUID PRIMARY KEY NOT NULL, "provider" TEXT NOT NULL, "model_id" TEXT NOT NULL, "version" TEXT NOT NULL, "dimension" INTEGER NOT NULL, "metric" TEXT NOT NULL, "tokenizer" TEXT NOT NULL, "max_tokens" INTEGER NOT NULL, "status" TEXT NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL);
CREATE TABLE IF NOT EXISTS "knowledge_paths" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "virtual_path" TEXT NOT NULL, "resource_type" TEXT NOT NULL, "target_id" TEXT NOT NULL, "version" INTEGER, "view_type" TEXT NOT NULL, "view_name" TEXT NOT NULL, "metadata" JSONB NOT NULL, "updated_at" TIMESTAMPTZ, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "evidence_bundles" ("id" UUID PRIMARY KEY NOT NULL, "trace_id" UUID, "query" TEXT NOT NULL, "state" TEXT NOT NULL, "items" JSONB NOT NULL, "missing_evidence" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ);
CREATE TABLE IF NOT EXISTS "golden_questions" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "question" TEXT NOT NULL, "expected_evidence_ids" JSONB NOT NULL, "tags" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "answer_traces" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "evidence_bundle_id" UUID, "query" TEXT NOT NULL, "mode" TEXT NOT NULL, "completed" BOOLEAN NOT NULL, "created_at" TIMESTAMPTZ 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" UUID PRIMARY KEY NOT NULL, "trace_id" UUID NOT NULL, "name" TEXT NOT NULL, "status" TEXT NOT NULL, "metadata" JSONB NOT NULL, "started_at" TIMESTAMPTZ NOT NULL, "ended_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, FOREIGN KEY ("trace_id") REFERENCES "answer_traces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "graph_entities" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "canonical_key" TEXT NOT NULL, "type" TEXT NOT NULL, "name" TEXT NOT NULL, "aliases" JSONB NOT NULL, "confidence" DOUBLE PRECISION NOT NULL, "source_node_ids" JSONB NOT NULL, "permission_scope" JSONB NOT NULL, "metadata" JSONB NOT NULL, "extraction_version" INTEGER NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "graph_relations" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "subject_entity_id" UUID NOT NULL, "object_entity_id" UUID NOT NULL, "type" TEXT NOT NULL, "confidence" DOUBLE PRECISION NOT NULL, "source_node_ids" JSONB NOT NULL, "permission_scope" JSONB NOT NULL, "metadata" JSONB NOT NULL, "extraction_version" INTEGER NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ 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" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "answer_trace_id" UUID, "query" TEXT NOT NULL, "mode" TEXT NOT NULL, "trigger" TEXT NOT NULL, "status" TEXT NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ NOT NULL, FOREIGN KEY ("knowledge_space_id") REFERENCES "knowledge_spaces" ("id") ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS "document_outlines" ("id" UUID PRIMARY KEY NOT NULL, "knowledge_space_id" UUID NOT NULL, "document_asset_id" UUID NOT NULL, "parse_artifact_id" UUID NOT NULL, "artifact_hash" TEXT NOT NULL, "outline_version" TEXT NOT NULL, "version" INTEGER NOT NULL, "nodes" JSONB NOT NULL, "metadata" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL, "updated_at" TIMESTAMPTZ, 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 "resource_mounts_permission_scope_idx" ON "resource_mounts" USING GIN ("permission_scope");
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 "knowledge_nodes_permission_scope_idx" ON "knowledge_nodes" USING GIN ("permission_scope");
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", (COALESCE("model", '')));
CREATE INDEX IF NOT EXISTS "index_projections_dense_vector_hnsw_idx" ON "index_projections" USING hnsw ("dense_vector" vector_cosine_ops);
CREATE INDEX IF NOT EXISTS "index_projections_fts_document_idx" ON "index_projections" USING GIN ("fts_document");
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_entities_permission_scope_idx" ON "graph_entities" USING GIN ("permission_scope");
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");
CREATE INDEX IF NOT EXISTS "graph_relations_permission_scope_idx" ON "graph_relations" USING GIN ("permission_scope");
