Skip to main content

reddb_server/runtime/
impl_lifecycle.rs

1//! Runtime bootstrap / constructor / handle accessors.
2//!
3//! Extracted verbatim from `impl_core.rs` (impl_core slice 8/10, issue #1629).
4//! Houses the in-memory / options constructors, the ~800-line `with_pool`
5//! constructor (moved verbatim — restructuring it is future work), snapshot
6//! rehydration and materialized-view backing lifecycle, system keyed-collection
7//! bootstrap, and the db / index-store / schema-vocabulary / auth-store /
8//! oauth / browser-token / registry handle accessors.
9//!
10//! - **Free helpers** — `view_records_to_entities`,
11//!   `system_keyed_collection_contract`, `table_row_index_fields`.
12use super::impl_config_secret::seed_storage_deploy_config;
13use super::*;
14
15/// Convert the rows produced by a materialized-view body into
16/// `UnifiedEntity` table rows targeting the backing collection.
17/// Issue #595 slice 9c — feeds `UnifiedStore::refresh_collection`.
18///
19/// Graph fragments and vector hits are ignored: a materialized view
20/// is a relational result set (SELECT-shaped); slices 11+ may extend
21/// this once we have a richer view body shape. Each row materialises
22/// the union of its schema-bound columns + overflow.
23pub(crate) fn view_records_to_entities(
24    table: &str,
25    records: &[crate::storage::query::unified::UnifiedRecord],
26) -> Vec<crate::storage::UnifiedEntity> {
27    use std::collections::HashMap;
28    let table_arc: std::sync::Arc<str> = std::sync::Arc::from(table);
29    let mut out = Vec::with_capacity(records.len());
30    for record in records {
31        let mut named: HashMap<String, reddb_types::Value> = HashMap::new();
32        for (name, value) in record.iter_fields() {
33            named.insert(name.to_string(), value.clone());
34        }
35        let entity = crate::storage::UnifiedEntity::new(
36            crate::storage::EntityId::new(0),
37            crate::storage::EntityKind::TableRow {
38                table: std::sync::Arc::clone(&table_arc),
39                row_id: 0,
40            },
41            crate::storage::EntityData::Row(crate::storage::RowData {
42                columns: Vec::new(),
43                named: Some(named),
44                schema: None,
45            }),
46        );
47        out.push(entity);
48    }
49    out
50}
51
52fn system_keyed_collection_contract(
53    name: &str,
54    model: crate::catalog::CollectionModel,
55) -> crate::physical::CollectionContract {
56    let now = crate::utils::now_unix_millis() as u128;
57    crate::physical::CollectionContract {
58        name: name.to_string(),
59        declared_model: model,
60        schema_mode: crate::catalog::SchemaMode::Dynamic,
61        origin: crate::physical::ContractOrigin::Implicit,
62        version: 1,
63        created_at_unix_ms: now,
64        updated_at_unix_ms: now,
65        default_ttl_ms: None,
66        vector_dimension: None,
67        vector_metric: None,
68        context_index_fields: Vec::new(),
69        declared_columns: Vec::new(),
70        table_def: None,
71        timestamps_enabled: false,
72        context_index_enabled: false,
73        metrics_raw_retention_ms: None,
74        metrics_rollup_policies: Vec::new(),
75        metrics_tenant_identity: None,
76        metrics_namespace: None,
77        append_only: false,
78        subscriptions: Vec::new(),
79        analytics_config: Vec::new(),
80        session_key: None,
81        session_gap_ms: None,
82        retention_duration_ms: None,
83        analytical_storage: None,
84
85        ai_policy: None,
86    }
87}
88
89pub(crate) fn table_row_index_fields(
90    entity: &crate::storage::unified::entity::UnifiedEntity,
91) -> Vec<(String, reddb_types::Value)> {
92    let crate::storage::EntityData::Row(row) = &entity.data else {
93        return Vec::new();
94    };
95    if let Some(named) = &row.named {
96        return named
97            .iter()
98            .map(|(name, value)| (name.clone(), value.clone()))
99            .collect();
100    }
101    if let Some(schema) = &row.schema {
102        return schema
103            .iter()
104            .zip(row.columns.iter())
105            .map(|(name, value)| (name.clone(), value.clone()))
106            .collect();
107    }
108    Vec::new()
109}
110
111impl RedDBRuntime {
112    pub fn in_memory() -> RedDBResult<Self> {
113        Self::with_options(RedDBOptions::in_memory())
114    }
115
116    pub fn flush(&self) -> RedDBResult<()> {
117        self.inner
118            .db
119            .flush()
120            .map_err(|err| RedDBError::Internal(err.to_string()))
121    }
122
123    /// Handle to the intent-lock manager for tests + introspection.
124    /// Production code acquires via `LockerGuard::new(rt.lock_manager())`
125    /// rather than touching the manager directly.
126    pub fn lock_manager(&self) -> std::sync::Arc<crate::runtime::lock_manager::LockManager> {
127        self.inner.lock_manager.clone()
128    }
129
130    /// Process-local governance registry for managed policy/config guardrails.
131    pub fn config_registry(&self) -> std::sync::Arc<crate::auth::registry::ConfigRegistry> {
132        self.inner.config_registry.clone()
133    }
134
135    pub fn query_audit(&self) -> std::sync::Arc<crate::runtime::query_audit::QueryAuditStream> {
136        self.inner.query_audit.clone()
137    }
138
139    pub fn control_events_require_persistence(&self) -> bool {
140        self.inner.control_event_config.require_persistence()
141    }
142
143    pub fn control_event_config(&self) -> crate::runtime::control_events::ControlEventConfig {
144        self.inner.control_event_config
145    }
146
147    pub fn control_event_ledger(
148        &self,
149    ) -> Arc<dyn crate::runtime::control_events::ControlEventLedger> {
150        self.inner.control_event_ledger.read().clone()
151    }
152
153    #[doc(hidden)]
154    pub fn replace_control_event_ledger_for_tests(
155        &self,
156        ledger: Arc<dyn crate::runtime::control_events::ControlEventLedger>,
157    ) {
158        *self.inner.control_event_ledger.write() = ledger;
159    }
160
161    #[inline(never)]
162    pub fn with_options(options: RedDBOptions) -> RedDBResult<Self> {
163        Self::with_pool(options, ConnectionPoolConfig::default())
164    }
165
166    /// The memory budget resolved at boot (ADR 0073 §1). Immutable for the
167    /// process lifetime; echoed by the boot log and the `red.stats` budget
168    /// section.
169    pub fn memory_budget(&self) -> crate::storage::memory_budget::MemoryBudget {
170        self.inner.memory_budget
171    }
172
173    /// The one shared accounting pool (ADR 0073 §2). Per-pool shares are fixed
174    /// at boot; live usage is refreshed by
175    /// [`RedDBRuntime::refresh_memory_accounting`].
176    pub fn memory_accounting(&self) -> &crate::storage::memory_pools::MemoryAccounting {
177        &self.inner.memory_accounting
178    }
179
180    pub fn with_pool(
181        options: RedDBOptions,
182        pool_config: ConnectionPoolConfig,
183    ) -> RedDBResult<Self> {
184        Self::with_pool_and_limits(
185            options,
186            pool_config,
187            super::resource_limits::ResourceLimits::from_env(),
188        )
189    }
190
191    pub(super) fn with_pool_and_limits(
192        options: RedDBOptions,
193        pool_config: ConnectionPoolConfig,
194        resource_limits: super::resource_limits::ResourceLimits,
195    ) -> RedDBResult<Self> {
196        options
197            .replication
198            .validate_failover_profile()
199            .map_err(RedDBError::InvalidConfig)?;
200        // PLAN.md Phase 9.1 — capture wall-clock before storage
201        // open so the cold-start phase markers can be backfilled
202        // once Lifecycle is constructed below. Storage open
203        // encapsulates auto-restore + WAL replay; we treat the
204        // whole window as one combined "restore" + "wal_replay"
205        // phase split at the same boundary because the storage
206        // layer doesn't yet emit a finer signal.
207        let boot_open_start_ms = std::time::SystemTime::now()
208            .duration_since(std::time::UNIX_EPOCH)
209            .map(|d| d.as_millis() as u64)
210            .unwrap_or(0);
211        // ADR 0073 §1 — resolve the one memory budget before anything is
212        // opened, so a nonsensical operator value fails the boot before the
213        // process touches disk. The log line fires once per process.
214        let memory_budget = crate::storage::memory_budget::resolve_for_boot(
215            options.storage_profile.deploy_profile,
216            options.memory_budget_bytes,
217        )
218        .map_err(|err| RedDBError::InvalidConfig(err.to_string()))?;
219        crate::storage::memory_budget::log_resolved_once(&memory_budget);
220        // ADR 0073 §2 — divide the budget among the pools before any of them
221        // is constructed. `BudgetShares::resolve` asserts Σ(shares) ≤ budget;
222        // one boot log line per pool names its share.
223        let memory_shares = crate::storage::memory_pools::BudgetShares::resolve(
224            memory_budget,
225            options.storage_profile.deploy_profile,
226        );
227        memory_shares.log_once();
228        let embedded_single_file = options.storage_profile.deploy_profile
229            == crate::storage::DeployProfile::Embedded
230            && options.storage_profile.packaging == crate::storage::StoragePackaging::SingleFile;
231        let db = Arc::new(RedDB::open_with_options(&options).map_err(|err| {
232            match err.downcast::<crate::storage::StoreError>() {
233                Ok(store_err) => RedDBError::from(*store_err),
234                Err(err) => RedDBError::Internal(err.to_string()),
235            }
236        })?);
237        // The RAM tier is sized from its share. L2's disk extent is not memory
238        // and keeps its own ceiling; only L1 and L2's resident metadata are
239        // accounted against `blob_cache_l1`.
240        let result_blob_cache_config = if embedded_single_file {
241            crate::storage::cache::BlobCacheConfig::default()
242        } else {
243            crate::storage::cache::BlobCacheConfig::default().with_l2_path(
244                reddb_file::layout::result_cache_l2_path(
245                    &options.resolved_path(reddb_file::default_database_path()),
246                ),
247            )
248        }
249        .with_l1_bytes_max(memory_shares.blob_cache_l1_bytes());
250        let result_blob_cache =
251            crate::storage::cache::BlobCache::open_with_l2(result_blob_cache_config).map_err(
252                |err| RedDBError::Internal(format!("open result Blob Cache L2 failed: {err:?}")),
253            )?;
254        let storage_ready_ms = std::time::SystemTime::now()
255            .duration_since(std::time::UNIX_EPOCH)
256            .map(|d| d.as_millis() as u64)
257            .unwrap_or(0);
258
259        let no_auth_boot = crate::service_cli::no_auth_active(&options);
260        let runtime = Self {
261            background_tasks: Some(Arc::new(background_tasks::RuntimeBackgroundTasks::default())),
262            inner: Arc::new(RuntimeInner {
263                functions: parking_lot::RwLock::new(
264                    super::function_catalog::FunctionCatalog::load(&db)?,
265                ),
266                db: db.clone(),
267                layout: PhysicalLayout::from_options(&options),
268                embedded_single_file,
269                memory_budget,
270                memory_accounting: Arc::new(
271                    crate::storage::memory_pools::MemoryAccounting::from_shares(
272                        memory_budget,
273                        memory_shares,
274                    ),
275                ),
276                memory_reservations: parking_lot::Mutex::new(
277                    super::memory_admission::MemoryReservations::default(),
278                ),
279                indices: IndexCatalog::register_default_vector_graph(
280                    options.has_capability(crate::api::Capability::Table),
281                    options.has_capability(crate::api::Capability::Graph),
282                ),
283                pool_config,
284                pool: Mutex::new(PoolState::default()),
285                started_at_unix_ms: SystemTime::now()
286                    .duration_since(UNIX_EPOCH)
287                    .unwrap_or_default()
288                    .as_millis(),
289                probabilistic: super::probabilistic_store::ProbabilisticStore::new(),
290                index_store: super::index_store::IndexStore::new(),
291                cdc: crate::replication::cdc::CdcBuffer::new(100_000),
292                checkpoint_projection_stats: super::CheckpointProjectionStats::default(),
293                scrub_state: parking_lot::Mutex::new(super::ScrubRuntimeState::default()),
294                checkpoint_columnar_emission_budget_chunks: options
295                    .checkpoint_columnar_emission_budget_chunks,
296                columnar_projection_size_floor_rows: options.columnar_projection_size_floor_rows,
297                backup_scheduler: crate::replication::scheduler::BackupScheduler::new(3600),
298                query_cache: parking_lot::RwLock::new(
299                    crate::storage::query::planner::cache::PlanCache::new(1000),
300                ),
301                result_cache: parking_lot::RwLock::new((
302                    HashMap::new(),
303                    std::collections::VecDeque::new(),
304                )),
305                result_blob_cache,
306                result_blob_entries: parking_lot::RwLock::new((
307                    HashMap::new(),
308                    std::collections::VecDeque::new(),
309                )),
310                ask_answer_cache_entries: parking_lot::RwLock::new((
311                    HashSet::new(),
312                    std::collections::VecDeque::new(),
313                )),
314                result_cache_shadow_divergences: std::sync::atomic::AtomicU64::new(0),
315                result_cache_hits: std::sync::atomic::AtomicU64::new(0),
316                result_cache_misses: std::sync::atomic::AtomicU64::new(0),
317                result_cache_evictions: std::sync::atomic::AtomicU64::new(0),
318                ask_daily_spend: parking_lot::RwLock::new(HashMap::new()),
319                queue_message_locks: parking_lot::RwLock::new(HashMap::new()),
320                rmw_locks: RmwLockTable::new(),
321                planner_dirty_tables: parking_lot::RwLock::new(HashSet::new()),
322                ec_registry: Arc::new(crate::ec::config::EcRegistry::new()),
323                config_registry: Arc::new(crate::auth::registry::ConfigRegistry::new()),
324                ec_worker: crate::ec::worker::EcWorker::new(),
325                auth_store: parking_lot::RwLock::new(None),
326                oauth_validator: parking_lot::RwLock::new(None),
327                browser_token_authority: parking_lot::RwLock::new(None),
328                views: parking_lot::RwLock::new(HashMap::new()),
329                materialized_views: parking_lot::RwLock::new(
330                    crate::storage::cache::result::MaterializedViewCache::new(),
331                ),
332                retention_sweeper: parking_lot::RwLock::new(
333                    crate::runtime::retention_sweeper::RetentionSweeperState::new(),
334                ),
335                transaction_state: crate::runtime::transaction_state::TransactionState::new(),
336                env_config_overrides: crate::runtime::config_overlay::collect_env_overrides(),
337                lock_manager: Arc::new({
338                    // Sourced from the matrix: Tier B key
339                    // `concurrency.locking.deadlock_timeout_ms`
340                    // (default 5000). Env var wins at boot so
341                    // operators can tune without touching red_config.
342                    let env = crate::runtime::config_overlay::collect_env_overrides();
343                    let timeout_ms = env
344                        .get("concurrency.locking.deadlock_timeout_ms")
345                        .and_then(|raw| raw.parse::<u64>().ok())
346                        .unwrap_or_else(|| {
347                            match crate::runtime::config_matrix::default_for(
348                                "concurrency.locking.deadlock_timeout_ms",
349                            ) {
350                                Some(crate::serde_json::Value::Number(n)) => n as u64,
351                                _ => 5000,
352                            }
353                        });
354                    let cfg = crate::runtime::lock_manager::LockConfig {
355                        default_timeout: std::time::Duration::from_millis(timeout_ms),
356                        ..Default::default()
357                    };
358                    crate::runtime::lock_manager::LockManager::new(cfg)
359                }),
360                rls_policies: parking_lot::RwLock::new(HashMap::new()),
361                rls_enabled_tables: parking_lot::RwLock::new(HashSet::new()),
362                foreign_tables: Arc::new(crate::storage::fdw::ForeignTableRegistry::with_builtins()),
363                pending_tombstones: parking_lot::RwLock::new(HashMap::new()),
364                pending_versioned_updates: parking_lot::RwLock::new(HashMap::new()),
365                pending_queue_dedup: parking_lot::RwLock::new(HashMap::new()),
366                pending_kv_watch_events: parking_lot::RwLock::new(HashMap::new()),
367                pending_vault_writes: parking_lot::RwLock::new(HashMap::new()),
368                pending_store_wal_actions: parking_lot::RwLock::new(HashMap::new()),
369                pending_claim_locks: parking_lot::RwLock::new(HashMap::new()),
370                queue_wait_registry: std::sync::Arc::new(
371                    crate::runtime::queue_wait_registry::QueueWaitRegistry::new(),
372                ),
373                pending_queue_wakes: parking_lot::RwLock::new(HashMap::new()),
374                tenant_tables: parking_lot::RwLock::new(HashMap::new()),
375                ddl_epoch: std::sync::atomic::AtomicU64::new(0),
376                write_gate: Arc::new(crate::runtime::write_gate::WriteGate::from_options(
377                    &options,
378                )),
379                lifecycle: crate::runtime::lifecycle::Lifecycle::new(),
380                resource_limits,
381                query_budget_slots: AtomicU64::new(0),
382                audit_log: {
383                    // Default audit-log path for the in-memory case
384                    // sits in the system temp dir; persistent runs
385                    // place it next to the resolved data file.
386                    //
387                    // gh-471 iter 2: route through the resolved
388                    // `LogDestination`. Performance/Max tiers emit a
389                    // file-backed log destination under the file-owned
390                    // support-directory logs tier;
391                    // lower tiers / ephemeral runs report `Stderr`
392                    // and we keep the legacy file-next-to-data sink.
393                    // #1375 — single-file embedded mode keeps the data
394                    // directory to exactly the `.rdb` artifact, so the audit
395                    // log must NOT land as a sibling. Route it to a
396                    // process-unique temp location even when a data path is
397                    // set; only the non-embedded case uses the data dir.
398                    let data_path = if embedded_single_file {
399                        std::env::temp_dir()
400                            .join("reddb-embedded-runtime")
401                            .join(format!("audit-{}", std::process::id()))
402                    } else {
403                        options
404                            .data_path
405                            .clone()
406                            .unwrap_or_else(|| std::env::temp_dir().join("reddb"))
407                    };
408                    let (audit_dest, _) = crate::api::tier_wiring::current_log_destinations();
409                    if !matches!(audit_dest, crate::storage::layout::LogDestination::File(_))
410                        && (embedded_single_file
411                            || options
412                                .metadata
413                                .contains_key(crate::api::EPHEMERAL_RUNTIME_METADATA_KEY))
414                    {
415                        // The Stderr/Syslog lower-tier sink resolves to a
416                        // `for_data_path` sibling that collides across concurrent
417                        // temp-dir runtimes — nextest's process-per-test model
418                        // truncates one shared file, flaking audit assertions.
419                        // Pin a unique sibling for these short-lived ephemeral /
420                        // single-file embedded runtimes. The file-owned support-
421                        // dir tier (`File`) is already per-data unique, so leave
422                        // it to `for_destination` (#1375: the embedded audit then
423                        // still never lands a sibling next to the `.rdb`).
424                        let audit_path = reddb_file::layout::sibling_path(
425                            &data_path,
426                            &reddb_file::layout::sidecar_file_name(&data_path, "audit.log"),
427                        );
428                        Arc::new(crate::runtime::audit_log::AuditLogger::with_path(
429                            audit_path,
430                        ))
431                    } else {
432                        Arc::new(crate::runtime::audit_log::AuditLogger::for_destination(
433                            &audit_dest,
434                            &data_path,
435                        ))
436                    }
437                },
438                control_event_ledger: parking_lot::RwLock::new(Arc::new(
439                    crate::runtime::control_events::RuntimeLedger::new(db.store()),
440                )),
441                control_event_config: options.control_events,
442                query_audit: Arc::new(crate::runtime::query_audit::QueryAuditStream::new(
443                    db.store(),
444                    options.query_audit.clone(),
445                )),
446                lease_lifecycle: std::sync::OnceLock::new(),
447                replica_apply_metrics: std::sync::Arc::new(
448                    crate::replication::logical::ReplicaApplyMetrics::default(),
449                ),
450                replica_link_metrics: std::sync::Arc::new(
451                    crate::replication::reconnect::ReplicaLinkMetrics::default(),
452                ),
453                quota_bucket: crate::runtime::quota_bucket::QuotaBucket::from_env(),
454                schema_vocabulary: parking_lot::RwLock::new(
455                    crate::runtime::schema_vocabulary::SchemaVocabulary::new(),
456                ),
457                slow_query_logger: {
458                    // Issue #205 — slow-query sink lives in the same
459                    // directory the audit log uses, so backup/restore
460                    // ships them together. Threshold + sample-pct
461                    // default conservatively (1 s, 100% sampling) so
462                    // emitted lines are rare and complete. Operators
463                    // tune via env / config matrix in a follow-up.
464                    //
465                    // gh-471 iter 2: same routing as the audit log —
466                    // `LogDestination::File(...)` for Performance/Max
467                    // lands under the file-owned support-directory logs tier;
468                    // lower tiers fall back to `red-slow.log` in the
469                    // data directory.
470                    // #1375 — see the audit-log note above: single-file mode
471                    // never writes the slow-query log as a sibling of the
472                    // `.rdb`. Route to a process-unique temp dir when embedded,
473                    // regardless of the data path.
474                    let fallback_dir = if embedded_single_file {
475                        std::env::temp_dir()
476                            .join("reddb-embedded-runtime")
477                            .join(format!("slow-{}", std::process::id()))
478                    } else {
479                        options
480                            .data_path
481                            .as_ref()
482                            .and_then(|p| p.parent().map(std::path::PathBuf::from))
483                            .unwrap_or_else(|| std::env::temp_dir().join("reddb"))
484                    };
485                    let threshold_ms = std::env::var("RED_SLOW_QUERY_THRESHOLD_MS")
486                        .ok()
487                        .and_then(|s| s.parse::<u64>().ok())
488                        .unwrap_or(1000);
489                    let sample_pct = std::env::var("RED_SLOW_QUERY_SAMPLE_PCT")
490                        .ok()
491                        .and_then(|s| s.parse::<u8>().ok())
492                        .unwrap_or(100);
493                    let (_, slow_dest) = crate::api::tier_wiring::current_log_destinations();
494                    crate::telemetry::slow_query_logger::SlowQueryLogger::for_destination(
495                        &slow_dest,
496                        &fallback_dir,
497                        threshold_ms,
498                        sample_pct,
499                    )
500                },
501                slow_query_store: crate::telemetry::slow_query_store::SlowQueryStore::new(
502                    crate::telemetry::slow_query_store::DEFAULT_CAP,
503                ),
504                kv_stats: crate::runtime::KvStatsCounters::default(),
505                metrics_ingest_stats: crate::runtime::MetricsIngestCounters::default(),
506                metrics_tenant_activity_stats:
507                    crate::runtime::MetricsTenantActivityCounters::default(),
508                claim_telemetry: Arc::new(
509                    crate::runtime::claim_telemetry::ClaimTelemetryCounters::default(),
510                ),
511                queue_telemetry: Arc::new(
512                    crate::runtime::queue_telemetry::QueueTelemetryCounters::default(),
513                ),
514                query_latency_telemetry: Arc::new(
515                    crate::runtime::query_latency_telemetry::QueryLatencyTelemetry::default(),
516                ),
517                occupancy_sampler: Arc::new(
518                    crate::runtime::occupancy_sampler::OccupancySampler::new(),
519                ),
520                node_load_telemetry: Arc::new(
521                    crate::runtime::node_load_telemetry::NodeLoadTelemetry::default(),
522                ),
523                queue_presence: Arc::new(
524                    crate::storage::queue::presence::ConsumerPresenceRegistry::new(),
525                ),
526                vector_introspection: Arc::new(
527                    crate::storage::vector::introspection::VectorIntrospectionRegistry::new(),
528                ),
529                kv_tag_index: crate::runtime::KvTagIndex::default(),
530                chain_tip_cache: parking_lot::Mutex::new(HashMap::new()),
531                chain_integrity_broken: parking_lot::Mutex::new(HashMap::new()),
532                integrity_tombstones: parking_lot::Mutex::new(Vec::new()),
533                integrity_tombstones_state: std::sync::atomic::AtomicU8::new(0),
534            }),
535        };
536
537        // Issue #205 — install the process-wide OperatorEvent sink so
538        // emit sites buried in storage / replication / signal handlers
539        // can record without threading an `&AuditLogger` through every
540        // call stack. First registration wins; subsequent in-memory
541        // runtimes (test harnesses) fall through to tracing+eprintln.
542        crate::telemetry::operator_event::install_global_audit_sink(Arc::clone(
543            &runtime.inner.audit_log,
544        ));
545
546        // Issue #1238 — wire the slow-query telemetry substrate (ADR 0060).
547        // The logger dual-writes: file sink (existing) + ring store (new).
548        runtime
549            .inner
550            .slow_query_logger
551            .attach_store(Arc::clone(&runtime.inner.slow_query_store));
552
553        // PLAN.md Phase 9.1 — backfill cold-start phase markers
554        // from the wall-clock captured before storage open. The
555        // entire `RedDB::open_with_options` call covers both
556        // auto-restore (when configured) and WAL replay. We
557        // record both phases against the same boundary today;
558        // a follow-up will split them once the storage layer
559        // surfaces a finer-grained event.
560        runtime
561            .inner
562            .lifecycle
563            .set_restore_started_at_ms(boot_open_start_ms);
564        runtime
565            .inner
566            .lifecycle
567            .set_restore_ready_at_ms(storage_ready_ms);
568        runtime
569            .inner
570            .lifecycle
571            .set_wal_replay_started_at_ms(boot_open_start_ms);
572        runtime
573            .inner
574            .lifecycle
575            .set_wal_replay_ready_at_ms(storage_ready_ms);
576
577        let restored_cdc_lsn = runtime
578            .inner
579            .db
580            .replication
581            .as_ref()
582            .map(|repl| {
583                repl.logical_wal_spool
584                    .as_ref()
585                    .map(|spool| spool.current_lsn())
586                    .unwrap_or(0)
587            })
588            .unwrap_or(0)
589            .max(runtime.config_u64("red.config.timeline.last_archived_lsn", 0));
590        runtime.inner.cdc.set_current_lsn(restored_cdc_lsn);
591        runtime.rehydrate_snapshot_xid_floor();
592        assert!(
593            runtime
594                .inner
595                .db
596                .store()
597                .snapshot_manager
598                .set(runtime.snapshot_manager())
599                .is_ok(),
600            "a new runtime owns the store transaction outcomes"
601        );
602        runtime
603            .bootstrap_system_keyed_collections()
604            .map_err(|err| RedDBError::Internal(format!("bootstrap system collections: {err}")))?;
605        runtime.rehydrate_declared_column_schemas();
606        runtime.rehydrate_runtime_index_registry()?;
607        runtime.rehydrate_kv_tags()?;
608        runtime
609            .load_probabilistic_state()
610            .map_err(|err| RedDBError::Internal(format!("load probabilistic state: {err}")))?;
611
612        // Phase 2.5.4: replay `tenant_tables.{table}.column` markers so
613        // tables declared via `TENANT BY (col)` survive restart. Each
614        // entry re-registers the auto-policy and flips RLS on again.
615        runtime.rehydrate_tenant_tables();
616        // Issue #593 slice 9a — replay persisted materialized-view
617        // descriptors so `CREATE MATERIALIZED VIEW v AS …` survives a
618        // restart. Runs after the system-keyed collections bootstrap
619        // and before the API opens.
620        runtime.rehydrate_materialized_view_descriptors();
621        if let Some(repl) = &runtime.inner.db.replication {
622            repl.wal_buffer.set_current_lsn(restored_cdc_lsn);
623        }
624
625        // Save system info to red_config on boot
626        {
627            let sys = SystemInfo::collect();
628            runtime.inner.db.store().set_config_tree(
629                "red.system",
630                &crate::serde_json::json!({
631                    "pid": sys.pid,
632                    "cpu_cores": sys.cpu_cores,
633                    "total_memory_bytes": sys.total_memory_bytes,
634                    "available_memory_bytes": sys.available_memory_bytes,
635                    "os": sys.os,
636                    "arch": sys.arch,
637                    "hostname": sys.hostname,
638                    "started_at": SystemTime::now()
639                        .duration_since(UNIX_EPOCH)
640                        .unwrap_or_default()
641                        .as_millis() as u64
642                }),
643            );
644
645            // Seed defaults on first boot (only if red_config is empty or missing defaults)
646            let store = runtime.inner.db.store();
647            if store
648                .get_collection("red_config")
649                .map(|m| m.query_all(|_| true).len())
650                .unwrap_or(0)
651                <= 10
652            {
653                store.set_config_tree("red.ai", &crate::json!({
654                    "default": crate::json!({
655                        "provider": "openai",
656                        "model": crate::ai::DEFAULT_OPENAI_PROMPT_MODEL
657                    }),
658                    "max_embedding_inputs": 256,
659                    "max_prompt_batch": 256,
660                    "timeout": crate::json!({ "connect_secs": 10, "read_secs": 90, "write_secs": 30 })
661                }));
662                store.set_config_tree(
663                    "red.server",
664                    &crate::json!({
665                        "max_scan_limit": 1000,
666                        "max_body_size": 1048576,
667                        "read_timeout_ms": 5000,
668                        "write_timeout_ms": 5000
669                    }),
670                );
671                store.set_config_tree(
672                    "red.storage",
673                    &crate::json!({
674                        "page_size": 4096,
675                        "page_cache_capacity": 100000,
676                        "auto_checkpoint_pages": 1000,
677                        "snapshot_retention": 16,
678                        "verify_checksums": true,
679                        "segment": crate::json!({
680                            "max_entities": 100000,
681                            "max_bytes": 268435456_u64,
682                            "compression_level": 6
683                        }),
684                        "hnsw": crate::json!({ "m": 16, "ef_construction": 100, "ef_search": 50 }),
685                        "ivf": crate::json!({ "n_lists": 100, "n_probes": 10 }),
686                        "bm25": crate::json!({ "k1": 1.2, "b": 0.75 })
687                    }),
688                );
689                store.set_config_tree(
690                    "red.search",
691                    &crate::json!({
692                        "rag": crate::json!({
693                            "max_chunks_per_source": 10,
694                            "max_total_chunks": 25,
695                            "similarity_threshold": 0.8,
696                            "graph_depth": 2,
697                            "min_relevance": 0.3
698                        }),
699                        "fusion": crate::json!({
700                            "vector_weight": 0.5,
701                            "graph_weight": 0.3,
702                            "table_weight": 0.2,
703                            "dedup_threshold": 0.85
704                        })
705                    }),
706                );
707                store.set_config_tree(
708                    "red.auth",
709                    &crate::json!({
710                        "enabled": false,
711                        "session_ttl_secs": 3600,
712                        "require_auth": false
713                    }),
714                );
715                store.set_config_tree(
716                    "red.query",
717                    &crate::json!({
718                        "connection_pool": crate::json!({ "max_connections": 64, "max_idle": 16 }),
719                        "max_recursion_depth": 1000
720                    }),
721                );
722                store.set_config_tree(
723                    "red.indexes",
724                    &crate::json!({
725                        "auto_select": true,
726                        "bloom_filter": crate::json!({
727                            "enabled": true,
728                            "false_positive_rate": 0.01,
729                            "prune_on_scan": true
730                        }),
731                        "hash": crate::json!({ "enabled": true }),
732                        "bitmap": crate::json!({ "enabled": true, "max_cardinality": 1000 }),
733                        "spatial": crate::json!({ "enabled": true })
734                    }),
735                );
736                store.set_config_tree(
737                    "red.probabilistic",
738                    &crate::json!({
739                        "hll_registers": 16384,
740                        "sketch_default_width": 1000,
741                        "sketch_default_depth": 5,
742                        "filter_default_capacity": 100000
743                    }),
744                );
745                store.set_config_tree(
746                    "red.timeseries",
747                    &crate::json!({
748                        "default_chunk_size": 1024,
749                        "compression": crate::json!({
750                            "timestamps": "delta_of_delta",
751                            "values": "gorilla_xor"
752                        }),
753                        "default_retention_days": 0
754                    }),
755                );
756                store.set_config_tree(
757                    "red.queue",
758                    &crate::json!({
759                        "default_max_size": 0,
760                        "default_max_attempts": 3,
761                        "visibility_timeout_ms": 30000,
762                        "consumer_idle_timeout_ms": 60000
763                    }),
764                );
765                store.set_config_tree(
766                    "red.backup",
767                    &crate::json!({
768                        "enabled": false,
769                        "interval_secs": 3600,
770                        "retention_count": 24,
771                        "upload": false,
772                        "backend": "local"
773                    }),
774                );
775                store.set_config_tree(
776                    "red.wal",
777                    &crate::json!({
778                        "archive": crate::json!({
779                            "enabled": false,
780                            "retention_hours": 168,
781                            "prefix": reddb_file::backup_wal_prefix("")
782                        })
783                    }),
784                );
785                store.set_config_tree(
786                    "red.cdc",
787                    &crate::json!({
788                        "enabled": true,
789                        "buffer_size": 100000
790                    }),
791                );
792                store.set_config_tree(
793                    "red.config.secret",
794                    &crate::json!({
795                        "auto_encrypt": true,
796                        "auto_decrypt": true
797                    }),
798                );
799            }
800
801            // Perf-parity config matrix: heal the Tier A (critical)
802            // keys unconditionally on every boot. Idempotent — only
803            // writes the default when the key is missing. Keeps
804            // `SHOW CONFIG` showing every guarantee the operator has
805            // (durability.mode, concurrency.locking.enabled, …) even
806            // on long-running datadirs that predate the matrix.
807            crate::runtime::config_matrix::heal_critical_keys(store.as_ref());
808            seed_storage_deploy_config(store.as_ref(), options.storage_profile);
809
810            // Phase 5 — Lehman-Yao runtime flag. Read the Tier A
811            // `storage.btree.lehman_yao` value from the matrix (env
812            // > file > red_config > default) and publish it to the
813            // storage layer's atomic so the B-tree read / split
814            // paths can branch without re-reading the config on
815            // every hot-path call.
816            let lehman_yao = runtime.config_bool("storage.btree.lehman_yao", true);
817            crate::storage::engine::btree::lehman_yao::set_enabled(lehman_yao);
818            if lehman_yao {
819                tracing::info!(
820                    "storage.btree.lehman_yao=true — lock-free concurrent descent enabled"
821                );
822            }
823
824            // Config file overlay — mounted `/etc/reddb/config.json`
825            // (override path via REDDB_CONFIG_FILE). Writes keys with
826            // write-if-absent semantics so a later user `SET CONFIG`
827            // always wins. Missing file = silent no-op.
828            let overlay_path = crate::runtime::config_overlay::config_file_path();
829            let _ =
830                crate::runtime::config_overlay::apply_config_file(store.as_ref(), &overlay_path);
831        }
832
833        // VCS ("Git for Data") — create the `red_*` metadata
834        // collections on first boot. Idempotent: `get_or_create_collection`
835        // is a no-op if the collection already exists.
836        {
837            let store = runtime.inner.db.store();
838            for name in crate::application::vcs_collections::ALL {
839                let _ = store.get_or_create_collection(*name);
840            }
841            // Seed VCS config namespace with sensible defaults on first
842            // boot, matching the pattern used by red.ai / red.storage.
843            store.set_config_tree(
844                crate::application::vcs_collections::CONFIG_NAMESPACE,
845                &crate::json!({
846                    "default_branch": "main",
847                    "author": crate::json!({
848                        "name": "reddb",
849                        "email": "reddb@localhost"
850                    }),
851                    "protected_branches": crate::json!(["main"]),
852                    "closure": crate::json!({
853                        "enabled": true,
854                        "lazy": true
855                    }),
856                    "merge": crate::json!({
857                        "default_strategy": "auto",
858                        "fast_forward": true
859                    })
860                }),
861            );
862        }
863
864        // Migrations — create the `red_migrations` / `red_migration_deps`
865        // system collections on first boot. Idempotent.
866        {
867            let store = runtime.inner.db.store();
868            for name in crate::application::migration_collections::ALL {
869                let _ = store.get_or_create_collection(*name);
870            }
871        }
872
873        // Topology graph (#803) — ensure the built-in `red.topology.cluster`
874        // graph collection (declared WITH ANALYTICS) and its metadata sidecar
875        // exist. Idempotent and survives restarts via the WAL-backed contract.
876        let _ = crate::application::topology_collections::ensure(&runtime);
877
878        // #1369 — reserve a fixed internal-id floor so the first user-inserted
879        // entity always receives a stable, documented `rid` (FIRST_USER_ENTITY_ID),
880        // independent of how many internal collection-descriptor / config-default
881        // entities the boot sequence seeded above. `register_entity_id` only ever
882        // raises the allocator, so a database that already holds user data
883        // (counter past the floor) is untouched; a freshly-seeded database jumps
884        // straight to the floor.
885        runtime
886            .inner
887            .db
888            .store()
889            .register_entity_id(crate::storage::EntityId::new(
890                crate::storage::FIRST_USER_ENTITY_ID - 1,
891            ));
892
893        // Start background maintenance thread (context index refresh +
894        // session purge). Held by a WEAK reference to `RuntimeInner`
895        // to avoid an ownership cycle. The last foreground owner unparks
896        // and joins the worker before releasing its inner reference, so
897        // an active weak upgrade cannot retain the pager lock after close.
898        // The 60-second work cadence is tracked via `last_work`.
899        //
900        // The previous version captured `rt = runtime.clone()` by
901        // strong reference and ran an unterminated `loop`, which held
902        // Arc<RuntimeInner> forever — reopening a persistent database
903        // in the same process failed with "Database is locked" because
904        // the pager could never drop. See the regression test
905        // `finding_1_select_after_bulk_insert_persistent_reopen`.
906        {
907            let weak = Arc::downgrade(&runtime.inner);
908            runtime
909                .background_tasks
910                .as_ref()
911                .expect("foreground runtime")
912                .spawn(
913                    std::thread::Builder::new().name("reddb-maintenance".into()),
914                    move |stopped| {
915                        let tick = std::time::Duration::from_millis(200);
916                        let work_interval = std::time::Duration::from_secs(60);
917                        let mut last_work = std::time::Instant::now();
918                        loop {
919                            std::thread::park_timeout(tick);
920                            if stopped.load(AtomicOrdering::Acquire) {
921                                break;
922                            }
923                            let Some(inner) = weak.upgrade() else {
924                                // All strong references dropped — the
925                                // runtime is gone, exit cleanly.
926                                break;
927                            };
928                            if last_work.elapsed() >= work_interval {
929                                let _stats = inner.db.store().context_index().stats();
930                                last_work = std::time::Instant::now();
931                            }
932                        }
933                    },
934                );
935        }
936
937        // Start backup scheduler if enabled via red_config
938        {
939            let store = runtime.inner.db.store();
940            let mut backup_enabled = false;
941            let mut backup_interval = 3600u64;
942
943            if let Some(manager) = store.get_collection("red_config") {
944                manager.for_each_entity(|entity| {
945                    if let Some(row) = entity.data.as_row() {
946                        let key = row.get_field("key").and_then(|v| match v {
947                            reddb_types::Value::Text(s) => Some(s.as_ref()),
948                            _ => None,
949                        });
950                        let val = row.get_field("value");
951                        if key == Some("red.config.backup.enabled") {
952                            backup_enabled = match val {
953                                Some(reddb_types::Value::Boolean(true)) => true,
954                                Some(reddb_types::Value::Text(s)) => &**s == "true",
955                                _ => false,
956                            };
957                        } else if key == Some("red.config.backup.interval_secs") {
958                            if let Some(reddb_types::Value::Integer(n)) = val {
959                                backup_interval = *n as u64;
960                            }
961                        }
962                    }
963                    true
964                });
965            }
966
967            if backup_enabled {
968                runtime.inner.backup_scheduler.set_interval(backup_interval);
969                let rt = runtime.clone();
970                runtime
971                    .inner
972                    .backup_scheduler
973                    .start(move || rt.trigger_backup().map_err(|e| format!("{}", e)));
974            }
975        }
976
977        // Load EC registry from red_config and start worker
978        {
979            runtime
980                .inner
981                .ec_registry
982                .load_from_config_store(runtime.inner.db.store().as_ref());
983            if !runtime.inner.ec_registry.async_configs().is_empty() {
984                runtime.inner.ec_worker.start(
985                    Arc::clone(&runtime.inner.ec_registry),
986                    Arc::clone(&runtime.inner.db.store()),
987                );
988            }
989        }
990
991        if let (true, crate::replication::ReplicationRole::Replica { primary_addr }) = (
992            runtime.inner.db.options().replica_loop_enabled,
993            runtime.inner.db.options().replication.role.clone(),
994        ) {
995            let rt = runtime.clone();
996            std::thread::Builder::new()
997                .name("reddb-replica".into())
998                .spawn(move || rt.run_replica_loop(primary_addr))
999                .ok();
1000        }
1001
1002        // PLAN.md Phase 1 — Lifecycle Contract. Mark Ready once every
1003        // boot stage above has completed (WAL replay, restore-from-
1004        // remote, replica-loop spawn). Health probes flip from 503 to
1005        // 200 here; shutdown begins from this state.
1006        runtime.inner.lifecycle.mark_ready();
1007
1008        // Issue #583 slice 10 — ContinuousMaterializedView scheduler.
1009        // Low-priority background ticker that drains the cache's
1010        // `claim_due_at` set every ~50ms. Holds only a Weak<RuntimeInner>
1011        // to avoid an ownership cycle; foreground close unparks and drains
1012        // an active refresh before releasing storage. Materialized views without
1013        // a `REFRESH EVERY` clause stay on the manual-refresh path
1014        // and are skipped by `claim_due_at`, so the loop is a no-op
1015        // when no scheduled views exist.
1016        {
1017            let weak_inner = Arc::downgrade(&runtime.inner);
1018            runtime
1019                .background_tasks
1020                .as_ref()
1021                .expect("foreground runtime")
1022                .spawn(
1023                    std::thread::Builder::new()
1024                        .name("reddb-mv-scheduler".into())
1025                        // Rust's default for spawned threads is 2 MiB, which is too
1026                        // small for the query-execution path that refresh_due_materialized_views
1027                        // runs (StatementExecutionFrame + guards per view). Match the
1028                        // 8 MiB that the OS gives the main thread.
1029                        .stack_size(8 * 1024 * 1024),
1030                    move |stopped| loop {
1031                        std::thread::park_timeout(std::time::Duration::from_millis(50));
1032                        if stopped.load(AtomicOrdering::Acquire) {
1033                            break;
1034                        }
1035                        let Some(inner) = weak_inner.upgrade() else {
1036                            break;
1037                        };
1038                        let rt = RedDBRuntime {
1039                            background_tasks: None,
1040                            inner,
1041                        };
1042                        rt.refresh_due_materialized_views();
1043                    },
1044                );
1045        }
1046
1047        // Issue #584 slice 12 — DeclarativeRetention background sweeper.
1048        // Low-priority ticker that physically reclaims rows whose
1049        // timestamp has fallen beyond the retention window. Holds a
1050        // `Weak<RuntimeInner>`; foreground close drains an active tick
1051        // before releasing storage (shutdown leaves storage consistent
1052        // because each tick goes through the standard DELETE path —
1053        // there is no half-finished mutation state to clean up). The
1054        // tick interval is intentionally longer than the MV scheduler
1055        // (500ms) because retention is order-of-seconds at minimum.
1056        if !runtime.write_gate().is_read_only() {
1057            let weak_inner = Arc::downgrade(&runtime.inner);
1058            runtime
1059                .background_tasks
1060                .as_ref()
1061                .expect("foreground runtime")
1062                .spawn(
1063                    std::thread::Builder::new().name("reddb-retention-sweeper".into()),
1064                    move |stopped| loop {
1065                        std::thread::park_timeout(std::time::Duration::from_millis(500));
1066                        if stopped.load(AtomicOrdering::Acquire) {
1067                            break;
1068                        }
1069                        let Some(inner) = weak_inner.upgrade() else {
1070                            break;
1071                        };
1072                        let rt = RedDBRuntime {
1073                            background_tasks: None,
1074                            inner,
1075                        };
1076                        rt.sweep_retention_tick(
1077                            crate::runtime::retention_sweeper::DEFAULT_SWEEPER_BATCH,
1078                        );
1079                    },
1080                );
1081        }
1082
1083        // Anonymous access is the most consequential posture an operator can
1084        // choose. It was only ever announced on stderr; leave it in the
1085        // chained ledger too, so an audit of the store itself shows it.
1086        if no_auth_boot {
1087            use crate::runtime::control_events::{
1088                ActorRef, ControlEvent, ControlEventCtx, EventKind, Outcome,
1089            };
1090            let ledger = std::sync::Arc::clone(&runtime.inner.control_event_ledger.read());
1091            let ctx = ControlEventCtx {
1092                actor: ActorRef::System("boot"),
1093                scope: None,
1094                request_id: None,
1095                trace_id: None,
1096            };
1097            let event = ControlEvent {
1098                kind: EventKind::AuthDisabled,
1099                outcome: Outcome::Allowed,
1100                action: std::borrow::Cow::Borrowed("auth.disabled"),
1101                resource: Some("server".to_string()),
1102                reason: Some("--no-auth / --dev".to_string()),
1103                matched_policy_id: None,
1104                fields: std::collections::HashMap::new(),
1105            };
1106            if let Err(err) = ledger.emit(&ctx, event) {
1107                tracing::warn!(
1108                    target: "reddb::security",
1109                    error = %err,
1110                    "could not record the auth-disabled boot in the control-event ledger"
1111                );
1112            }
1113        }
1114
1115        Ok(runtime)
1116    }
1117
1118    fn rehydrate_snapshot_xid_floor(&self) {
1119        let store = self.inner.db.store();
1120        for collection in store.list_collections() {
1121            let Some(manager) = store.get_collection(&collection) else {
1122                continue;
1123            };
1124            for entity in manager.query_all(|_| true) {
1125                let snapshot_manager = self.inner.transaction_state.snapshot_manager();
1126                snapshot_manager.observe_committed_xid(entity.xmin);
1127                snapshot_manager.observe_committed_xid(entity.xmax);
1128            }
1129        }
1130    }
1131
1132    /// Provision an empty Table-shaped collection that backs a
1133    /// `CREATE MATERIALIZED VIEW v` (issue #594 slice 9b of #575).
1134    /// `SELECT FROM v` reads this collection directly; the rewriter is
1135    /// configured to skip materialized views so the body is no longer
1136    /// substituted. REFRESH still writes to the cache slot — wiring it
1137    /// into this backing collection is the job of slice 9c.
1138    ///
1139    /// Idempotent: re-running for the same name leaves the existing
1140    /// collection in place (mirrors `CREATE TABLE IF NOT EXISTS`
1141    /// semantics). This keeps `CREATE OR REPLACE MATERIALIZED VIEW v`
1142    /// cheap — the body change does not invalidate already-buffered
1143    /// rows. Until 9c lands the backing is always empty anyway.
1144    pub(crate) fn ensure_materialized_view_backing(&self, name: &str) -> RedDBResult<()> {
1145        let store = self.inner.db.store();
1146        let mut changed = false;
1147        if store.get_collection(name).is_none() {
1148            store.get_or_create_collection(name);
1149            changed = true;
1150        }
1151        if self.inner.db.collection_contract(name).is_none() {
1152            self.inner
1153                .db
1154                .save_collection_contract(system_keyed_collection_contract(
1155                    name,
1156                    crate::catalog::CollectionModel::Table,
1157                ))
1158                .map_err(|err| RedDBError::Internal(err.to_string()))?;
1159            changed = true;
1160        }
1161        if changed {
1162            self.inner
1163                .db
1164                .persist_metadata()
1165                .map_err(|err| RedDBError::Internal(err.to_string()))?;
1166        }
1167        Ok(())
1168    }
1169
1170    /// Inverse of [`ensure_materialized_view_backing`] — drops the
1171    /// backing collection on `DROP MATERIALIZED VIEW v`. No-op when
1172    /// the collection was never created (e.g. a `DROP MATERIALIZED
1173    /// VIEW IF EXISTS v` against an unknown name).
1174    pub(crate) fn drop_materialized_view_backing(&self, name: &str) -> RedDBResult<()> {
1175        let store = self.inner.db.store();
1176        if store.get_collection(name).is_none() {
1177            return Ok(());
1178        }
1179        store
1180            .drop_collection(name)
1181            .map_err(|err| RedDBError::Internal(err.to_string()))?;
1182        // The contract may have been dropped already (DROP TABLE path)
1183        // — ignore "not found" errors by checking presence first.
1184        if self.inner.db.collection_contract(name).is_some() {
1185            self.inner
1186                .db
1187                .remove_collection_contract(name)
1188                .map_err(|err| RedDBError::Internal(err.to_string()))?;
1189        }
1190        self.invalidate_result_cache();
1191        self.inner
1192            .db
1193            .persist_metadata()
1194            .map_err(|err| RedDBError::Internal(err.to_string()))?;
1195        Ok(())
1196    }
1197
1198    fn bootstrap_system_keyed_collections(&self) -> RedDBResult<()> {
1199        let mut changed = false;
1200        for (name, model) in [
1201            ("red.config", crate::catalog::CollectionModel::Config),
1202            ("red.vault", crate::catalog::CollectionModel::Vault),
1203            // Issue #593 — materialized-view catalog. One row per
1204            // `CREATE MATERIALIZED VIEW`; rehydrated at boot before
1205            // the API opens.
1206            (
1207                crate::runtime::continuous_materialized_view::CATALOG_COLLECTION,
1208                crate::catalog::CollectionModel::Config,
1209            ),
1210        ] {
1211            if self.inner.db.store().get_collection(name).is_none() {
1212                self.inner.db.store().get_or_create_collection(name);
1213                changed = true;
1214            }
1215            if self.inner.db.collection_contract(name).is_none() {
1216                self.inner
1217                    .db
1218                    .save_collection_contract(system_keyed_collection_contract(name, model))
1219                    .map_err(|err| RedDBError::Internal(err.to_string()))?;
1220                changed = true;
1221            }
1222        }
1223        if changed {
1224            self.inner
1225                .db
1226                .persist_metadata()
1227                .map_err(|err| RedDBError::Internal(err.to_string()))?;
1228        }
1229        Ok(())
1230    }
1231
1232    pub fn db(&self) -> Arc<RedDB> {
1233        Arc::clone(&self.inner.db)
1234    }
1235
1236    /// Direct access to the runtime's secondary-index store.
1237    /// Used by bulk-insert entry points (gRPC binary bulk, HTTP bulk,
1238    /// wire bulk) that need to push new rows through the per-index
1239    /// maintenance hook after `store.bulk_insert` returns.
1240    pub fn index_store_ref(&self) -> &super::index_store::IndexStore {
1241        &self.inner.index_store
1242    }
1243
1244    /// Apply a DDL event to the schema-vocabulary reverse index
1245    /// (issue #120). Called by DDL execution paths after the catalog
1246    /// mutation has succeeded so the index never holds entries for
1247    /// half-applied DDL.
1248    pub(crate) fn schema_vocabulary_apply(
1249        &self,
1250        event: crate::runtime::schema_vocabulary::DdlEvent,
1251    ) {
1252        self.inner.schema_vocabulary.write().on_ddl(event);
1253    }
1254
1255    /// Lookup `token` in the schema-vocabulary reverse index. Returns
1256    /// an owned `Vec<VocabHit>` because the underlying read lock
1257    /// cannot be borrowed across the call boundary; the slice from
1258    /// `SchemaVocabulary::lookup` is cloned per hit.
1259    pub fn schema_vocabulary_lookup(
1260        &self,
1261        token: &str,
1262    ) -> Vec<crate::runtime::schema_vocabulary::VocabHit> {
1263        self.inner.schema_vocabulary.read().lookup(token).to_vec()
1264    }
1265
1266    /// Inject an AuthStore into the runtime. Called by server boot
1267    /// after the vault has been bootstrapped, so that `Value::Secret`
1268    /// auto-encrypt/decrypt can reach the vault AES key.
1269    pub fn set_auth_store(&self, store: Arc<crate::auth::store::AuthStore>) {
1270        *self.inner.auth_store.write() = Some(store);
1271    }
1272
1273    /// Snapshot the current AuthStore (if any). Used by the wire listener
1274    /// to validate bearer tokens issued via HTTP `/auth/login`.
1275    pub fn auth_store(&self) -> Option<Arc<crate::auth::store::AuthStore>> {
1276        self.inner.auth_store.read().clone()
1277    }
1278
1279    /// Inject an `OAuthValidator` into the runtime. When set, HTTP and
1280    /// wire transports try OAuth JWT validation before falling back to
1281    /// the local AuthStore lookup. Pass `None` to disable.
1282    pub fn set_oauth_validator(&self, validator: Option<Arc<crate::auth::oauth::OAuthValidator>>) {
1283        *self.inner.oauth_validator.write() = validator;
1284    }
1285
1286    /// Returns a clone of the configured `OAuthValidator` Arc, if any.
1287    /// Hot path: called per HTTP request when an Authorization header
1288    /// is present, so we hand back a cheap Arc clone.
1289    pub fn oauth_validator(&self) -> Option<Arc<crate::auth::oauth::OAuthValidator>> {
1290        self.inner.oauth_validator.read().clone()
1291    }
1292
1293    /// Inject the browser-token authority (issue #936). When set, the
1294    /// RedWire WS handshake accepts the short-lived access JWT it mints
1295    /// (alongside, and tried before, the federated OAuth validator), and
1296    /// the `/auth/browser/*` HTTP endpoints can issue/rotate the pair.
1297    /// `None` leaves the browser credential flow inert.
1298    pub fn set_browser_token_authority(
1299        &self,
1300        authority: Option<Arc<crate::auth::browser_token::BrowserTokenAuthority>>,
1301    ) {
1302        *self.inner.browser_token_authority.write() = authority;
1303    }
1304
1305    /// Snapshot the browser-token authority, if wired. Read on the WS
1306    /// handshake path and by the `/auth/browser/*` handlers; a cheap Arc
1307    /// clone keeps the lock hold short.
1308    pub fn browser_token_authority(
1309        &self,
1310    ) -> Option<Arc<crate::auth::browser_token::BrowserTokenAuthority>> {
1311        self.inner.browser_token_authority.read().clone()
1312    }
1313}