Skip to main content

reddb_server/runtime/
statement_frame.rs

1use std::cell::RefCell;
2use std::collections::HashSet;
3use std::rc::Rc;
4use std::sync::Arc;
5
6use super::execution_context::capture_current_snapshot;
7use super::impl_core::{
8    collections_referenced, current_auth_identity, current_connection_id, current_tenant,
9    has_with_prefix, intent_lock_modes_for, peek_top_level_as_of_with_table,
10    query_has_volatile_builtin, query_is_ask_statement, ConfigSnapshotGuard, CurrentSnapshotGuard,
11    KvStoreGuard, SecretStoreGuard, SnapshotContext, TxLocalTenantGuard,
12};
13use super::{RedDBRuntime, RuntimeQueryResult, RuntimeResultCacheEntry};
14use crate::api::{RedDBError, RedDBResult};
15use crate::auth::Role;
16use crate::storage::transaction::snapshot::{Snapshot, Xid};
17use reddb_rql::ast::QueryExpr;
18use reddb_rql::modes::{detect_mode, parse_multi, QueryMode};
19
20/// Coarse privilege classification for a statement, computed once at
21/// frame-build time from the SQL text. Mirrors the three-role auth
22/// model (`Role::Read < Role::Write < Role::Admin`) so the frame can
23/// answer "can this identity run this statement?" without re-walking
24/// the parsed `QueryExpr` at every call site.
25///
26/// `None` means the statement does not touch the privilege gate at
27/// all (transaction control, SET, SHOW). Such statements must remain
28/// runnable under any authenticated identity.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub(crate) enum Privilege {
31    /// Read-only data access (SELECT, EXPLAIN, SHOW). Satisfied by
32    /// any role from `Role::Read` upward.
33    Read,
34    /// Mutation of user data or schema author DDL (INSERT, UPDATE,
35    /// DELETE, CREATE/ALTER/DROP TABLE, CREATE MIGRATION). Requires
36    /// at least `Role::Write`.
37    Write,
38    /// Authority statements — GRANT, REVOKE, ALTER USER, APPLY /
39    /// ROLLBACK MIGRATION, IAM policy mutation. Requires `Role::Admin`.
40    Admin,
41    /// Statement does not consult the privilege gate (BEGIN, COMMIT,
42    /// ROLLBACK, SET, SHOW with no data exposure). Always permitted
43    /// for any authenticated identity.
44    None,
45}
46
47impl Privilege {
48    /// `true` iff `role` is sufficient to execute a statement carrying
49    /// this required privilege. Encodes the standard `Read ⊆ Write ⊆
50    /// Admin` containment used by the auth fallback path.
51    pub(crate) fn is_satisfied_by(self, role: Role) -> bool {
52        match self {
53            Self::None => true,
54            Self::Read => role.can_read(),
55            Self::Write => role.can_write(),
56            Self::Admin => role.can_admin(),
57        }
58    }
59}
60
61/// Coarse lock intent for a statement, computed once at frame-build
62/// time. Maps onto the storage-layer's `LockMode` matrix downstream
63/// but stays decoupled here so the runtime can answer "does this
64/// statement need the lock manager at all?" without a `use storage::`
65/// at every call site.
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub(crate) enum LockIntent {
68    /// No collection-level lock needed (transaction control, SET,
69    /// SHOW, EXPLAIN). The lock-acquisition path can short-circuit.
70    None,
71    /// Reader-style intent: SELECT, joins, graph / queue / search
72    /// reads. Maps to `(IS, IS)` at the storage layer.
73    Shared,
74    /// Writer- or DDL-style intent: INSERT/UPDATE/DELETE (`(IX, IX)`)
75    /// and CREATE/ALTER/DROP (`(IX, X)`). Both are surfaced as
76    /// `Exclusive` at this granularity — call sites that need the
77    /// finer distinction still consult `intent_lock_modes_for`.
78    Exclusive,
79}
80
81/// Small, stable Interface that *represents* a read statement's
82/// execution context. Every read caller that needs to know "under
83/// what scope / identity / snapshot am I running, and is there an
84/// AS OF floor in effect?" consults this trait — never the
85/// underlying thread-locals or runtime fields directly.
86///
87/// The deletion test: removing this trait would force the four
88/// concerns it exposes back into ad-hoc lookups at every read
89/// callsite (`current_tenant()`, `current_auth_identity()`,
90/// `capture_current_snapshot()`, AS OF re-parsing). The trait
91/// concentrates them in one place so future changes (per-statement
92/// logging, audit, scope policy) have a single seam to extend.
93pub(crate) trait ReadFrame {
94    /// Effective tenant scope for the statement after WITHIN /
95    /// SET LOCAL TENANT / SET TENANT resolution. `None` means
96    /// "no tenant bound" (RLS deny-default applies).
97    fn effective_scope(&self) -> Option<&str>;
98
99    /// Authenticated identity observed at frame-build time, if any.
100    /// Returns `(username, role)` so callers can render audit lines
101    /// or feed RLS policy lookups without re-reading thread-locals.
102    fn identity(&self) -> Option<(&str, Role)>;
103
104    /// MVCC snapshot the statement reads against. For autocommit
105    /// this is a fresh snapshot; inside an active transaction it
106    /// is the txn's snapshot; under AS OF it is the resolved
107    /// historical xid.
108    fn snapshot(&self) -> &Snapshot;
109
110    /// Complete visibility context, including the reader's savepoint writes.
111    fn snapshot_context(&self, runtime: &RedDBRuntime) -> SnapshotContext {
112        SnapshotContext {
113            snapshot: self.snapshot().clone(),
114            manager: runtime.inner.transaction_state.snapshot_manager(),
115            own_xids: HashSet::new(),
116            requires_index_fallback: true,
117            serializable_reader: None,
118        }
119    }
120
121    /// AS OF xid floor when AS OF was applied for this statement,
122    /// `None` for live reads. Useful for downstream callers that
123    /// want to gate behaviour on historical-read mode without
124    /// re-parsing the query.
125    fn as_of_floor(&self) -> Option<Xid>;
126
127    /// Stable result-cache key for the statement (already mixes
128    /// effective tenant + identity).
129    fn cache_key(&self) -> &str;
130
131    /// Whether the statement is safe to serve from / populate the
132    /// result cache. Combines two underlying signals:
133    ///
134    ///   * the query does not call a volatile builtin (e.g. `NOW()`,
135    ///     `RANDOM()`, `UUID()`), which would change between calls,
136    ///   * the connection is not inside an active transaction with
137    ///     uncommitted writes that other readers shouldn't observe.
138    ///
139    /// SELECT cache callsites (read + write) consult this method
140    /// instead of re-deriving safety from globals or poking the
141    /// frame's private fields. Removing it would force every cache
142    /// callsite to re-run `query_has_volatile_builtin` plus
143    /// `result_cache_safe(conn_id)` inline.
144    fn should_cache_result(&self) -> bool;
145
146    /// Coarse privilege class the statement requires, computed once
147    /// at frame-build time from the SQL prefix. Read/write dispatch
148    /// sites consult this instead of re-classifying the parsed
149    /// `QueryExpr` inline at every callsite.
150    ///
151    /// Removing this method would force every privilege gate to
152    /// recompute the (action, resource) classification from the
153    /// parsed expression and re-check the role hierarchy inline.
154    fn required_privilege(&self) -> Privilege;
155
156    /// Coarse collection-level lock intent the statement implies.
157    /// `None` lets the lock-acquisition path short-circuit without
158    /// touching the lock manager.
159    ///
160    /// Removing this method would force the lock-acquisition path
161    /// to always invoke `intent_lock_modes_for` (which itself walks
162    /// the parsed expression) even for transaction-control / SET /
163    /// SHOW statements that need no collection lock at all.
164    fn lock_intent(&self) -> LockIntent;
165
166    /// Set of collection ids the calling identity is allowed to
167    /// observe under the active `(tenant, role)` scope. Computed once
168    /// at frame-build time via the `AuthStore` visible-collections
169    /// cache (see `auth::scope_cache`) and used by `AuthorizedSearch`
170    /// to pre-filter SEARCH SIMILAR / SEARCH CONTEXT candidate sets
171    /// before any similarity score is computed (issue #119).
172    ///
173    /// `None` means the frame was built without an auth store wired —
174    /// embedded / single-tenant tests run that way. AI search call
175    /// sites refuse to proceed with `None`, which is the deny-default
176    /// the issue requires; pure SELECT paths fall back to the existing
177    /// per-row RLS gate.
178    fn visible_collections(&self) -> Option<&std::collections::HashSet<String>>;
179}
180
181/// Cheap first-word classification of a SQL statement, used at
182/// frame-build time to derive `Privilege` + `LockIntent` without
183/// re-parsing the query. Matches the keywords that the legacy
184/// inline checks in `RedDBRuntime::check_query_privilege` and
185/// `intent_lock_modes_for` already key on.
186/// Whether [`statement_kind`] reads `query` as a mutating statement.
187/// The Request seam uses it to keep its pre-write commit-policy check off
188/// read paths.
189pub(crate) fn statement_is_write(query: &str) -> bool {
190    statement_kind(query) == "write"
191}
192
193fn statement_kind(query: &str) -> &'static str {
194    let trimmed = query.trim_start();
195    // Skip a leading line / block comment so the classifier doesn't
196    // misread `/* ... */ SELECT ...` as an unknown statement.
197    let trimmed = if let Some(rest) = trimmed.strip_prefix("--") {
198        rest.split_once('\n')
199            .map(|(_, r)| r)
200            .unwrap_or("")
201            .trim_start()
202    } else {
203        trimmed
204    };
205    let mut tokens = trimmed.split(|c: char| c.is_whitespace() || c == '(' || c == ';');
206    let first = tokens.next().unwrap_or("");
207    let second = tokens.next().unwrap_or("");
208    if first.eq_ignore_ascii_case("KV") {
209        if second.eq_ignore_ascii_case("GET")
210            || second.eq_ignore_ascii_case("LIST")
211            || second.eq_ignore_ascii_case("WATCH")
212        {
213            return "read";
214        }
215        return "write";
216    }
217    if first.eq_ignore_ascii_case("VAULT") {
218        if second.eq_ignore_ascii_case("GET")
219            || second.eq_ignore_ascii_case("REVEAL")
220            || second.eq_ignore_ascii_case("UNSEAL")
221            || second.eq_ignore_ascii_case("LIST")
222            || second.eq_ignore_ascii_case("WATCH")
223            || second.eq_ignore_ascii_case("HISTORY")
224        {
225            return "read";
226        }
227        return "write";
228    }
229    // ASCII-uppercase compare without allocating: SQL keywords are ASCII.
230    let mut buf = [0u8; 16];
231    let bytes = first.as_bytes();
232    let n = bytes.len().min(buf.len());
233    for i in 0..n {
234        buf[i] = bytes[i].to_ascii_uppercase();
235    }
236    match &buf[..n] {
237        b"SELECT" | b"WITH" | b"SHOW" | b"EXPLAIN" | b"DESCRIBE" | b"DESC" | b"RANK"
238        | b"APPROX" | b"APPROXIMATE" | b"ZRANK" | b"ZRANGE" | b"LIST" | b"WATCH" | b"GET"
239        | b"HISTORY" | b"REVEAL" | b"UNSEAL" => "read",
240        b"INSERT" | b"UPDATE" | b"DELETE" | b"UPSERT" | b"MERGE" | b"COPY" | b"TRUNCATE" => "write",
241        b"CREATE" | b"ALTER" | b"DROP" | b"PROMOTE" | b"REINDEX" | b"VACUUM" | b"ANALYZE" => "ddl",
242        b"GRANT" | b"REVOKE" => "admin",
243        b"BEGIN" | b"START" | b"COMMIT" | b"ROLLBACK" | b"SAVEPOINT" | b"RELEASE" | b"END"
244        | b"SET" | b"RESET" | b"PREPARE" | b"EXECUTE" | b"DEALLOCATE" | b"USE" => "control",
245        b"PUT" | b"INCR" | b"DECR" | b"ADD" | b"ROTATE" | b"PURGE" | b"INVALIDATE" => "write",
246        _ => "unknown",
247    }
248}
249
250/// Refuse a pre-parsed expression that carries a top-level `AS OF`.
251///
252/// `AS OF` is resolved from the statement text (`AsOfSpec` -> commit /
253/// branch / tag / timestamp -> xid) and `TableQuery::as_of` is consumed
254/// nowhere in the runtime or scan path. An expression-only frame
255/// therefore cannot honour it, and installing a live snapshot for a
256/// historical read would answer with current rows presented as history.
257/// Fail loudly instead; callers that have the text (`execute_query`,
258/// `execute_prepared_query`, `execute_query_with_params`) resolve it
259/// correctly. Mirrors `peek_top_level_as_of_with_table`, which likewise
260/// only recognises a top-level `AS OF`.
261fn reject_unresolvable_as_of(expr: &QueryExpr) -> RedDBResult<()> {
262    let QueryExpr::Table(table) = expr else {
263        return Ok(());
264    };
265    if table.as_of.is_none() {
266        return Ok(());
267    }
268    Err(RedDBError::InvalidOperation(
269        "AS OF cannot be resolved from a pre-parsed expression — \
270         execute the statement text (execute_query / execute_prepared_query / \
271         execute_query_with_params) so the historical snapshot is resolved"
272            .to_string(),
273    ))
274}
275
276fn classify_privilege(query: &str) -> Privilege {
277    match statement_kind(query) {
278        "read" => Privilege::Read,
279        "write" => Privilege::Write,
280        // DDL is gated at `Role::Write` in the legacy fallback (see
281        // `RedDBRuntime::check_query_privilege` for CreateTable et al.),
282        // so it classifies as Write here. APPLY / ROLLBACK MIGRATION and
283        // GRANT / REVOKE upgrade to Admin via finer checks at the call
284        // site — the frame surfaces only the coarse class.
285        "ddl" => Privilege::Write,
286        "admin" => Privilege::Admin,
287        _ => Privilege::None,
288    }
289}
290
291fn classify_lock_intent(query: &str) -> LockIntent {
292    match statement_kind(query) {
293        "read" => LockIntent::Shared,
294        "write" | "ddl" => LockIntent::Exclusive,
295        _ => LockIntent::None,
296    }
297}
298
299pub(super) struct StatementExecutionFrame {
300    tx_local_tenant: Option<Option<String>>,
301    snapshot: Snapshot,
302    own_xids: HashSet<Xid>,
303    serializable_reader: Option<Xid>,
304    cache_key: String,
305    is_volatile_query: bool,
306    cache_safe: bool,
307    /// Effective tenant captured at frame-build time after WITHIN /
308    /// SET LOCAL TENANT / SET TENANT resolution. Stored on the frame
309    /// so the `ReadFrame` Interface can return a borrow without
310    /// re-touching the thread-local stack.
311    effective_scope: Option<String>,
312    /// Auth identity captured at frame-build time. `None` for
313    /// embedded / anonymous callers.
314    identity: Option<(String, Role)>,
315    /// `Some(xid)` when AS OF resolved to a historical xid; `None`
316    /// for live reads.
317    as_of_floor: Option<Xid>,
318    /// True when the statement snapshot can require tuple versions that
319    /// current secondary indexes no longer contain.
320    requires_index_fallback: bool,
321    /// Privilege class required by the statement, derived from the
322    /// SQL text at frame-build time. Read/write dispatch sites
323    /// consult this instead of re-classifying the parsed expression.
324    required_privilege: Privilege,
325    /// Collection-level lock intent the statement implies. The
326    /// lock-acquisition path short-circuits when this is `None`.
327    lock_intent: LockIntent,
328    /// Set of collection ids the active `(tenant, role)` scope is
329    /// allowed to observe. Computed at frame-build time via the
330    /// `AuthStore` visibility cache and consumed by `AuthorizedSearch`
331    /// to gate SEARCH SIMILAR / SEARCH CONTEXT candidate sets before
332    /// scoring (issue #119). `None` when no auth store is wired
333    /// (embedded test mode) — AI search refuses on `None`.
334    visible_collections: Option<HashSet<String>>,
335    /// Per-owner buffer arena for query-result row chunks (#885). Owned
336    /// by the frame because the frame already owns the query lifecycle;
337    /// lent to the row-streaming path (`execute_runtime_table_query_in`)
338    /// so chunk buffers are reused across the statement's chunk-fetches
339    /// instead of allocated fresh per chunk. Reclaimed when the frame
340    /// drops at statement end — no `thread_local!` scratch, which would
341    /// be unsound under tokio's work-stealing runtime.
342    row_arena: Rc<RefCell<super::query_exec::RowBufferArena>>,
343}
344
345pub(super) struct StatementFrameGuards {
346    _tx_local_guard: TxLocalTenantGuard,
347    _config_snapshot_guard: ConfigSnapshotGuard,
348    _secret_store_guard: SecretStoreGuard,
349    _kv_store_guard: KvStoreGuard,
350    _snapshot_guard: CurrentSnapshotGuard,
351}
352
353pub(super) struct PreparedStatement {
354    pub(super) expr: QueryExpr,
355    pub(super) mode: QueryMode,
356}
357
358/// What a `StatementExecutionFrame` is built from.
359///
360/// The frame derives the statement snapshot (including any `AS OF`
361/// floor), the coarse privilege class, the lock intent, the volatility
362/// flag, and the result-cache key from its input. Feeding it anything
363/// other than the statement itself makes every one of those describe a
364/// different statement than the one that runs, so the input is named
365/// explicitly here rather than being "some string".
366#[derive(Clone, Copy)]
367pub(super) enum StatementIdentity<'a> {
368    /// The statement's SQL text. All five derivations are exact.
369    Text(&'a str),
370    /// A pre-parsed expression with no accompanying text — the nested
371    /// in-statement callers (CTE prelude, materialized-view refresh,
372    /// inline graph TVF) hold an expression the outer statement already
373    /// parsed.
374    ///
375    /// Text-derived facts are not guessed from a kind label: there is no
376    /// stable cache key (a label would collide across every statement of
377    /// the same kind), and the coarse privilege / lock short-circuits are
378    /// left at `None` so the deep grant engine stays the gate. `AS OF` is
379    /// rejected up front by `build` because it cannot be resolved without
380    /// the text.
381    Expr(&'a QueryExpr),
382}
383
384impl<'a> StatementIdentity<'a> {
385    /// SQL text when the frame was built from text, `""` otherwise. Used
386    /// for the text-derived classifications, all of which degrade to the
387    /// deliberately inert `unknown` / `None` classes on the expr path.
388    fn text(&self) -> &'a str {
389        match self {
390            Self::Text(query) => query,
391            Self::Expr(_) => "",
392        }
393    }
394}
395
396impl StatementExecutionFrame {
397    pub(super) fn build(
398        runtime: &RedDBRuntime,
399        identity_source: StatementIdentity<'_>,
400    ) -> RedDBResult<Self> {
401        let query = identity_source.text();
402        if let StatementIdentity::Expr(expr) = identity_source {
403            reject_unresolvable_as_of(expr)?;
404        }
405        let conn_id = current_connection_id();
406        let tx_local_tenant = runtime.inner.transaction_state.local_tenant(conn_id);
407        let tx_context = runtime.inner.transaction_state.context(conn_id);
408        let own_xids = own_transaction_xids(tx_context.as_ref());
409        let serializable_reader = tx_context
410            .as_ref()
411            .filter(|ctx| {
412                ctx.isolation == crate::storage::transaction::IsolationLevel::Serializable
413            })
414            .map(|ctx| ctx.xid);
415        let (snapshot, as_of_floor) = runtime.statement_snapshot(query, identity_source)?;
416        let requires_index_fallback = as_of_floor.is_some() || tx_context.is_some();
417        let (cache_key, is_volatile_query) = match identity_source {
418            StatementIdentity::Text(_) => (
419                result_cache_key(query),
420                query_has_volatile_builtin(query) || query_is_ask_statement(query),
421            ),
422            // No text means no stable key, so the frame opts out of the
423            // result cache on both sides: an empty key is never read from
424            // or written to, and `is_volatile_query` is set so
425            // `should_cache_result` refuses regardless of the key.
426            StatementIdentity::Expr(_) => (String::new(), true),
427        };
428        let cache_safe = runtime.result_cache_safe(conn_id);
429        // Capture identity + effective scope under the same
430        // thread-local view that the cache key was built from, so
431        // the Interface and the cache key agree on what "this
432        // statement" means.
433        let effective_scope = current_tenant();
434        let identity = current_auth_identity();
435
436        // Coarse classification of the statement, computed once from
437        // the SQL prefix so downstream callers don't re-derive it
438        // from the parsed `QueryExpr` at every privilege / lock site.
439        // On the expr path `query` is empty, so both classify as the
440        // inert `None` class: the coarse gate can only deny, never
441        // allow (see `check_query_privilege`), so declining to
442        // short-circuit leaves the deep grant engine as the gate.
443        let required_privilege = classify_privilege(query);
444        let lock_intent = classify_lock_intent(query);
445
446        // Issue #119: resolve the visible-collections set for the
447        // active (tenant, role) scope. Only meaningful when an auth
448        // store is wired *and* an identity was captured — embedded
449        // anonymous callers fall back to `None`, and AI search call
450        // sites refuse on `None`.
451        let visible_collections = match (runtime.inner.auth_store.read().clone(), identity.as_ref())
452        {
453            (Some(store), Some((principal, role))) => {
454                let collections = runtime.inner.db.store().list_collections();
455                Some(store.visible_collections_for_scope(
456                    effective_scope.as_deref(),
457                    *role,
458                    principal,
459                    &collections,
460                ))
461            }
462            _ => None,
463        };
464
465        Ok(Self {
466            tx_local_tenant,
467            snapshot,
468            own_xids,
469            serializable_reader,
470            cache_key,
471            is_volatile_query,
472            cache_safe,
473            effective_scope,
474            identity,
475            as_of_floor,
476            requires_index_fallback,
477            required_privilege,
478            lock_intent,
479            visible_collections,
480            row_arena: Rc::new(RefCell::new(super::query_exec::RowBufferArena::new())),
481        })
482    }
483
484    /// Lend the frame's per-owner row-buffer arena (#885) to the
485    /// row-streaming path. Returns a cloned `Rc` handle; the frame remains
486    /// the owner and the arena is reclaimed when the frame drops at
487    /// statement end.
488    pub(super) fn row_arena(&self) -> Rc<RefCell<super::query_exec::RowBufferArena>> {
489        Rc::clone(&self.row_arena)
490    }
491
492    pub(super) fn install(&self, runtime: &RedDBRuntime) -> StatementFrameGuards {
493        StatementFrameGuards {
494            _tx_local_guard: TxLocalTenantGuard::install(self.tx_local_tenant.clone()),
495            _config_snapshot_guard: ConfigSnapshotGuard::install(
496                Arc::clone(&runtime.inner.db),
497                runtime.inner.auth_store.read().clone(),
498            ),
499            _secret_store_guard: SecretStoreGuard::install(runtime),
500            _kv_store_guard: KvStoreGuard::install(runtime.inner.auth_store.read().clone()),
501            _snapshot_guard: CurrentSnapshotGuard::install(SnapshotContext {
502                snapshot: self.snapshot.clone(),
503                manager: runtime.inner.transaction_state.snapshot_manager(),
504                own_xids: self.own_xids.clone(),
505                requires_index_fallback: self.requires_index_fallback,
506                serializable_reader: self.serializable_reader,
507            }),
508        }
509    }
510
511    pub(super) fn cache_key(&self) -> &str {
512        &self.cache_key
513    }
514
515    pub(super) fn can_read_result_cache(&self) -> bool {
516        // Delegates to the `ReadFrame` Interface so the volatile +
517        // active-tx safety decision lives in exactly one place.
518        <Self as ReadFrame>::should_cache_result(self)
519    }
520
521    pub(super) fn should_write_result_cache(&self, result: &RuntimeQueryResult) -> bool {
522        // Cache-safety (volatile builtin, active-tx writes) comes from
523        // the Interface; the rest are write-side payload heuristics
524        // (statement shape, result size) that aren't part of the
525        // safety contract.
526        <Self as ReadFrame>::should_cache_result(self)
527            && result.statement_type == "select"
528            && result.engine != "vault"
529            && result.engine != "runtime-rank"
530            // `QUEUE READ` is a stateful read: a delayed message
531            // (issue #722) becomes deliverable over time without a
532            // producer push to invalidate the cache, so a cached empty
533            // result would hide it. Skip caching entirely.
534            && result.statement != "queue_group_read"
535            // SCRUB is a stateful read too: the background form advances
536            // a pacing cursor per tick (a cached tick would replay the
537            // summary and never advance), and even a full pass must
538            // re-read the file to see new corruption. Never cache.
539            && result.engine != "runtime-scrub"
540            && result.result.pre_serialized_json.is_none()
541            // Graph-analytics TVF output (issue #802) is deterministic and
542            // expensive to recompute, so it is cached at any row count. The
543            // ≤5-row heuristic only bounds payload size for ordinary SELECTs.
544            && (is_graph_tvf_engine(result.engine) || result.result.records.len() <= 5)
545    }
546
547    pub(super) fn read_result_cache(&self, runtime: &RedDBRuntime) -> Option<RuntimeQueryResult> {
548        if self.can_read_result_cache() {
549            runtime
550                .get_result_cache_entry(self.cache_key())
551                .map(|mut result| {
552                    if let Some(vector) = result.result.stats.vector.as_mut() {
553                        vector.cache_hit = true;
554                    }
555                    result
556                })
557        } else {
558            None
559        }
560    }
561
562    pub(super) fn write_result_cache(
563        &self,
564        runtime: &RedDBRuntime,
565        result: &RuntimeQueryResult,
566        scopes: HashSet<String>,
567    ) {
568        if self.should_write_result_cache(result) {
569            runtime.put_result_cache_entry(
570                self.cache_key(),
571                RuntimeResultCacheEntry {
572                    result: result.clone(),
573                    cached_at: std::time::Instant::now(),
574                    scopes,
575                },
576            );
577        }
578    }
579
580    pub(super) fn prepare_cte(&self, query: &str) -> RedDBResult<Option<QueryExpr>> {
581        // Detected via cheap prefix check so non-CTE queries skip the
582        // full parse here. CTE-bearing queries bypass the plan cache
583        // and result cache (rare workload — perf optimization is a
584        // follow-up). Inlining substitutes every CTE reference with
585        // its body as a subquery in FROM, after which the existing
586        // subquery-in-FROM machinery handles execution. Recursive
587        // CTEs are rejected explicitly until fixpoint execution wires
588        // through the runtime.
589        if !has_with_prefix(query) {
590            return Ok(None);
591        }
592        let parsed =
593            reddb_rql::parser::parse(query).map_err(|err| RedDBError::Query(err.to_string()))?;
594        if parsed.with_clause.is_some() {
595            let rewritten = crate::storage::query::executors::inline_ctes(parsed)
596                .map_err(|err| RedDBError::Query(err.to_string()))?;
597            return Ok(Some(rewritten));
598        }
599        // No WITH after parse (the prefix matched something else like
600        // `WITHIN` that already routed elsewhere) — fall through to
601        // the normal path with the original query.
602        Ok(None)
603    }
604
605    pub(super) fn prepare_statement(
606        &self,
607        runtime: &RedDBRuntime,
608        query: &str,
609    ) -> RedDBResult<PreparedStatement> {
610        let mode = detect_mode(query);
611        if matches!(mode, QueryMode::Unknown) {
612            return Err(RedDBError::Query("unable to detect query mode".to_string()));
613        }
614
615        // ── Plan cache: reuse only exact-query ASTs ──
616        //
617        // DML statements (INSERT/UPDATE/DELETE) almost always have unique literal
618        // values, so caching them burns CPU on eviction bookkeeping (Vec::remove(0)
619        // shifts the entire LRU list) with zero hit rate. Skip the cache entirely
620        // Plan cache applies to statements whose shape can be
621        // normalised + rebound (`UPDATE t SET x=? WHERE _entity_id=?`
622        // reuses the same plan across thousands of varying literals).
623        // INSERT is still bypassed — its shape changes per column set
624        // and bulk paths don't go through here anyway.
625        let first_word = query
626            .trim()
627            .split_ascii_whitespace()
628            .next()
629            .unwrap_or("")
630            .to_ascii_uppercase();
631        let is_insert = first_word == "INSERT";
632        // #1370 — volatile queries ($config / $secret resolve mutable runtime
633        // state at execution time) must bypass the plan cache too. A cached
634        // optimized plan drops the live `$config` resolution, so a later
635        // `SET CONFIG` would be ignored and the query would serve a stale value.
636        // Re-parse fresh every time so the resolver runs against current state.
637        let bypass_plan_cache = is_insert || self.is_volatile_query;
638
639        // Fused normalize+extract: one byte-scan produces both the
640        // cache_key AND the literal bindings. Saves a second Lexer
641        // pass over the query text on every cache hit — dominant
642        // cost on tight UPDATE loops that hit the same shape
643        // thousands of times with varying literals.
644        let (cache_key, prescan_binds) = if bypass_plan_cache {
645            (String::new(), Vec::new())
646        } else {
647            crate::storage::query::planner::cache_key::normalize_and_extract(query)
648        };
649
650        let expr = if bypass_plan_cache {
651            // Bypass plan cache for INSERT — shape varies per query.
652            parse_multi(query).map_err(|err| RedDBError::Query(err.to_string()))?
653        } else {
654            // ── Hot path: read lock only (no writer serialization on cache hits) ──
655            //
656            // peek() is a non-mutating probe: no LRU promotion, no touch().
657            // This lets concurrent readers proceed without blocking each other.
658            // On hit we bind literals if needed and return immediately.
659            // Only on miss do we drop to a write lock to parse + insert.
660            let hit = {
661                let plan_cache = runtime.inner.query_cache.read();
662                plan_cache.peek(&cache_key).map(|cached| {
663                    let parameter_count = cached.parameter_count;
664                    let optimized = cached.plan.optimized.clone();
665                    let exact_query = cached.exact_query.clone();
666                    (parameter_count, optimized, exact_query)
667                })
668            };
669
670            if let Some((parameter_count, optimized, exact_query)) = hit {
671                if parameter_count > 0 {
672                    // Shape hit: use the binds extracted during normalise.
673                    let shape_binds = prescan_binds.clone();
674                    if let Some(bound) =
675                        crate::storage::query::planner::shape::bind_parameterized_query(
676                            &optimized,
677                            &shape_binds,
678                            parameter_count,
679                        )
680                    {
681                        bound
682                    } else {
683                        // A failed bind leaves placeholders in the cached AST,
684                        // even for the same exact SQL. Never execute it unbound.
685                        parse_multi(query).map_err(|err| RedDBError::Query(err.to_string()))?
686                    }
687                } else {
688                    // No parameters means either there truly are no literals,
689                    // or this statement type does not participate in shape
690                    // parameterization (for example graph/queue commands).
691                    // Reusing a normalized-cache hit across a different exact
692                    // query can therefore leak stale literals into execution.
693                    if exact_query.as_deref() == Some(query) {
694                        optimized
695                    } else {
696                        parse_multi(query).map_err(|err| RedDBError::Query(err.to_string()))?
697                    }
698                }
699            } else {
700                // Cache miss — parse, parameterize, store.
701                let parsed =
702                    parse_multi(query).map_err(|err| RedDBError::Query(err.to_string()))?;
703                let (cached_expr, parameter_count) = if let Some(prepared) =
704                    crate::storage::query::planner::shape::parameterize_query_expr(&parsed)
705                {
706                    (prepared.shape, prepared.parameter_count)
707                } else {
708                    (parsed.clone(), 0)
709                };
710                {
711                    let mut pc = runtime.inner.query_cache.write();
712                    let plan = crate::storage::query::planner::QueryPlan::new(
713                        parsed.clone(),
714                        cached_expr,
715                        Default::default(),
716                    );
717                    pc.insert(
718                        cache_key.clone(),
719                        crate::storage::query::planner::CachedPlan::new(plan)
720                            .with_shape_key(cache_key.clone())
721                            .with_exact_query(query.to_string())
722                            .with_parameter_count(parameter_count),
723                    );
724                }
725                parsed
726            }
727        };
728
729        // Phase 5 PG parity: substitute any registered view name that
730        // appears in the expression with its stored body. Runs after
731        // parse and before dispatch so the SQL entrypoint gets the
732        // same view resolution `execute_query_expr` already does.
733        let expr = runtime.rewrite_view_refs(expr);
734
735        Ok(PreparedStatement { expr, mode })
736    }
737
738    pub(super) fn check_query_privilege(
739        &self,
740        runtime: &RedDBRuntime,
741        expr: &QueryExpr,
742    ) -> RedDBResult<()> {
743        // Frame-level coarse gate. We consult `required_privilege()`
744        // (computed once at frame-build) against the captured identity
745        // before the deep grant engine walks the parsed expression.
746        // The coarse gate cannot ALLOW anything the grant engine would
747        // deny — it only short-circuits the obvious "Role::Read tries
748        // INSERT" case so a downstream caller never has to redo this
749        // check inline. `Privilege::None` (transaction control / SET /
750        // SHOW) flows through unchanged; the grant engine treats those
751        // as bypass too.
752        if let Some((username, role)) = <Self as ReadFrame>::identity(self) {
753            let needed = <Self as ReadFrame>::required_privilege(self);
754            if !needed.is_satisfied_by(role) {
755                // Issue #205 — when the deep grant engine *also*
756                // denies, we treat this as an ordinary permission
757                // failure. But when an Admin-only statement reaches
758                // this gate without an auth_store wired (so the deep
759                // engine can't double-check), the coarse rejection is
760                // the only line of defence — emit an OperatorEvent so
761                // the operator notices an Admin-class statement was
762                // attempted with insufficient role.
763                if matches!(needed, Privilege::Admin) && runtime.inner.auth_store.read().is_none() {
764                    crate::telemetry::operator_event::OperatorEvent::AuthBypass {
765                        principal: username.to_string(),
766                        resource: format!("statement requiring {needed:?}"),
767                        detail: format!(
768                            "auth_store not wired; coarse gate is sole defence (role={role:?})"
769                        ),
770                    }
771                    .emit_global();
772                }
773                return Err(RedDBError::Query(format!(
774                    "permission denied: principal=`{username}` role=`{role:?}` lacks {needed:?} privilege"
775                )));
776            }
777        }
778        runtime
779            .check_query_privilege(expr)
780            .map_err(|err| RedDBError::Query(format!("permission denied: {err}")))
781    }
782
783    pub(super) fn prepare_dispatch(
784        &self,
785        runtime: &RedDBRuntime,
786        expr: &QueryExpr,
787    ) -> RedDBResult<Option<crate::runtime::locking::LockerGuard>> {
788        runtime.validate_model_operations_before_auth(expr)?;
789        self.check_query_privilege(runtime, expr)?;
790        Ok(self.acquire_intent_locks(runtime, expr))
791    }
792
793    pub(super) fn acquire_intent_locks(
794        &self,
795        runtime: &RedDBRuntime,
796        expr: &QueryExpr,
797    ) -> Option<crate::runtime::locking::LockerGuard> {
798        if !runtime.config_bool("concurrency.locking.enabled", true) {
799            return None;
800        }
801        // Frame-level short-circuit: if the statement carries no lock
802        // intent (transaction control, SET, SHOW), skip the lock
803        // manager entirely instead of letting `intent_lock_modes_for`
804        // walk the parsed expression to reach the same conclusion.
805        if matches!(<Self as ReadFrame>::lock_intent(self), LockIntent::None) {
806            return None;
807        }
808        intent_lock_modes_for(expr).map(|(global_mode, coll_mode)| {
809            let mut guard =
810                crate::runtime::locking::LockerGuard::new(runtime.inner.lock_manager.clone());
811            let _ = guard.acquire(crate::runtime::locking::Resource::Global, global_mode);
812            for collection in collections_referenced(expr) {
813                let _ = guard.acquire(
814                    crate::runtime::locking::Resource::Collection(collection),
815                    coll_mode,
816                );
817            }
818            guard
819        })
820    }
821}
822
823impl ReadFrame for StatementExecutionFrame {
824    fn snapshot_context(&self, runtime: &RedDBRuntime) -> SnapshotContext {
825        SnapshotContext {
826            snapshot: self.snapshot.clone(),
827            manager: runtime.inner.transaction_state.snapshot_manager(),
828            own_xids: self.own_xids.clone(),
829            requires_index_fallback: self.requires_index_fallback,
830            serializable_reader: self.serializable_reader,
831        }
832    }
833
834    fn effective_scope(&self) -> Option<&str> {
835        self.effective_scope.as_deref()
836    }
837
838    fn identity(&self) -> Option<(&str, Role)> {
839        self.identity.as_ref().map(|(u, r)| (u.as_str(), *r))
840    }
841
842    fn snapshot(&self) -> &Snapshot {
843        &self.snapshot
844    }
845
846    fn as_of_floor(&self) -> Option<Xid> {
847        self.as_of_floor
848    }
849
850    fn cache_key(&self) -> &str {
851        &self.cache_key
852    }
853
854    fn should_cache_result(&self) -> bool {
855        !self.is_volatile_query && self.cache_safe
856    }
857
858    fn required_privilege(&self) -> Privilege {
859        self.required_privilege
860    }
861
862    fn lock_intent(&self) -> LockIntent {
863        self.lock_intent
864    }
865
866    fn visible_collections(&self) -> Option<&HashSet<String>> {
867        self.visible_collections.as_ref()
868    }
869}
870
871/// Lightweight `ReadFrame` carrier used by AI command entry points
872/// (`SEARCH SIMILAR`, `SEARCH CONTEXT`, `ASK`).
873///
874/// Issue #119 calls this struct `EffectiveScope`. It bundles the
875/// `(tenant, identity, role, visible_collections, snapshot)` tuple so
876/// every AI runtime entry can pass *one* value to `AuthorizedSearch`
877/// instead of re-reading thread-locals at every call site.
878///
879/// Built via `RedDBRuntime::ai_scope()` which sources tenant + identity
880/// from the per-statement thread-locals (identical to how
881/// `StatementExecutionFrame::build` derives them) and resolves
882/// `visible_collections` via the `AuthStore` cache.
883pub struct EffectiveScope {
884    pub(crate) captured_snapshot: Option<SnapshotContext>,
885    pub(crate) tenant: Option<String>,
886    pub(crate) identity: Option<(String, Role)>,
887    pub(crate) snapshot: Snapshot,
888    pub(crate) visible_collections: Option<HashSet<String>>,
889}
890
891impl EffectiveScope {
892    /// Capability check used by the AI runtime (`runtime/ai/ner.rs`)
893    /// to gate LLM-backed NER calls behind `ai:ner:read`.
894    ///
895    /// Placeholder for now: always returns `false`. The auth engine's
896    /// capability matrix is future work; until it lands, every routed
897    /// LLM-NER call denies at the gate and `extract_tokens_routed`'s
898    /// heuristic fallback fires (see `ask_pipeline::extract_tokens_routed`).
899    /// Documented in code so the wire-up is a one-line change once
900    /// the auth engine learns capabilities.
901    pub fn has_capability(&self, _capability: &str) -> bool {
902        false
903    }
904}
905
906impl ReadFrame for EffectiveScope {
907    fn snapshot_context(&self, runtime: &RedDBRuntime) -> SnapshotContext {
908        self.captured_snapshot
909            .clone()
910            .unwrap_or_else(|| SnapshotContext {
911                snapshot: self.snapshot.clone(),
912                manager: runtime.inner.transaction_state.snapshot_manager(),
913                own_xids: HashSet::new(),
914                requires_index_fallback: true,
915                serializable_reader: None,
916            })
917    }
918
919    fn effective_scope(&self) -> Option<&str> {
920        self.tenant.as_deref()
921    }
922    fn identity(&self) -> Option<(&str, Role)> {
923        self.identity.as_ref().map(|(u, r)| (u.as_str(), *r))
924    }
925    fn snapshot(&self) -> &Snapshot {
926        &self.snapshot
927    }
928    fn as_of_floor(&self) -> Option<Xid> {
929        None
930    }
931    fn cache_key(&self) -> &str {
932        ""
933    }
934    fn should_cache_result(&self) -> bool {
935        false
936    }
937    fn required_privilege(&self) -> Privilege {
938        Privilege::Read
939    }
940    fn lock_intent(&self) -> LockIntent {
941        LockIntent::Shared
942    }
943    fn visible_collections(&self) -> Option<&HashSet<String>> {
944        self.visible_collections.as_ref()
945    }
946}
947
948fn own_transaction_xids(
949    ctx: Option<&crate::storage::transaction::snapshot::TxnContext>,
950) -> HashSet<Xid> {
951    let mut set = HashSet::new();
952    if let Some(ctx) = ctx {
953        set.insert(ctx.xid);
954        for (_, sub) in &ctx.savepoints {
955            set.insert(*sub);
956        }
957        for sub in &ctx.released_sub_xids {
958            set.insert(*sub);
959        }
960    }
961    set
962}
963
964impl RedDBRuntime {
965    /// Build the AI command `EffectiveScope` from the current
966    /// statement thread-locals + auth store.
967    ///
968    /// Returns `None` for embedded callers (no auth store, no
969    /// identity) — `AuthorizedSearch` treats `None` as deny-default.
970    pub(crate) fn ai_scope(&self) -> EffectiveScope {
971        let tenant = super::impl_core::current_tenant();
972        let identity = super::impl_core::current_auth_identity();
973        let captured_snapshot = super::execution_context::capture_current_snapshot();
974        let snapshot = captured_snapshot.as_ref().map_or_else(
975            || self.current_snapshot(),
976            |context| context.snapshot.clone(),
977        );
978        let visible_collections = match (self.inner.auth_store.read().clone(), identity.as_ref()) {
979            (Some(store), Some((principal, role))) => {
980                let collections = self.inner.db.store().list_collections();
981                Some(store.visible_collections_for_scope(
982                    tenant.as_deref(),
983                    *role,
984                    principal,
985                    &collections,
986                ))
987            }
988            _ => None,
989        };
990        EffectiveScope {
991            captured_snapshot,
992            tenant,
993            identity,
994            snapshot,
995            visible_collections,
996        }
997    }
998}
999
1000/// Test fixtures for callers that need to drive `ReadFrame` without
1001/// booting a runtime. Lives behind `cfg(test)` and `pub(crate)` so it
1002/// only leaks across module boundaries inside the crate.
1003#[cfg(test)]
1004pub(crate) mod test_support {
1005    use super::{LockIntent, Privilege, ReadFrame};
1006    use crate::auth::Role;
1007    use crate::storage::transaction::snapshot::{Snapshot, Xid};
1008    use std::collections::HashSet;
1009
1010    /// A `ReadFrame` impl with hand-set fields. Used by
1011    /// `authorized_search` tests to assert the deny-default and
1012    /// scope-trim behaviour without going through frame construction.
1013    pub(crate) struct FakeReadFrame {
1014        pub tenant: Option<String>,
1015        pub identity: Option<(String, Role)>,
1016        pub snapshot: Snapshot,
1017        pub visible: Option<HashSet<String>>,
1018    }
1019
1020    impl FakeReadFrame {
1021        pub(crate) fn without_scope() -> Self {
1022            Self {
1023                tenant: None,
1024                identity: None,
1025                snapshot: Snapshot {
1026                    xid: 0,
1027                    in_progress: HashSet::new(),
1028                },
1029                visible: None,
1030            }
1031        }
1032
1033        pub(crate) fn with_visible(visible: HashSet<String>) -> Self {
1034            Self {
1035                tenant: Some("acme".to_string()),
1036                identity: Some(("alice".to_string(), Role::Read)),
1037                snapshot: Snapshot {
1038                    xid: 0,
1039                    in_progress: HashSet::new(),
1040                },
1041                visible: Some(visible),
1042            }
1043        }
1044    }
1045
1046    impl ReadFrame for FakeReadFrame {
1047        fn effective_scope(&self) -> Option<&str> {
1048            self.tenant.as_deref()
1049        }
1050        fn identity(&self) -> Option<(&str, Role)> {
1051            self.identity.as_ref().map(|(u, r)| (u.as_str(), *r))
1052        }
1053        fn snapshot(&self) -> &Snapshot {
1054            &self.snapshot
1055        }
1056        fn as_of_floor(&self) -> Option<Xid> {
1057            None
1058        }
1059        fn cache_key(&self) -> &str {
1060            ""
1061        }
1062        fn should_cache_result(&self) -> bool {
1063            false
1064        }
1065        fn required_privilege(&self) -> Privilege {
1066            Privilege::Read
1067        }
1068        fn lock_intent(&self) -> LockIntent {
1069            LockIntent::Shared
1070        }
1071        fn visible_collections(&self) -> Option<&HashSet<String>> {
1072            self.visible.as_ref()
1073        }
1074    }
1075}
1076
1077impl RedDBRuntime {
1078    /// Resolve the snapshot for the current statement, returning
1079    /// the snapshot itself and (when AS OF is in effect) the
1080    /// resolved xid floor. The floor is the same xid carried inside
1081    /// `Snapshot.xid` for AS OF reads — exposing it separately lets
1082    /// the `ReadFrame` Interface tell "live read" from "historical
1083    /// read" without inferring from `in_progress.is_empty()`.
1084    ///
1085    /// A live read nested inside an already-installed frame inherits that
1086    /// frame's snapshot instead of minting a fresh one — but only when it
1087    /// is genuinely *part of* that statement, which is exactly what
1088    /// [`StatementIdentity::Expr`] means. A CTE prelude, a
1089    /// materialized-view refresh, or an inline graph TVF is one statement
1090    /// and must observe one snapshot; re-minting under autocommit / Read
1091    /// Committed takes a new high-water mark, so the nodes and edges of a
1092    /// single graph TVF could otherwise be materialized under two
1093    /// different snapshots.
1094    ///
1095    /// A nested `Text` statement is a different thing entirely: a driver
1096    /// loop issuing statement after statement through `execute_query`.
1097    /// Those are sequential autocommit statements and each must see its
1098    /// predecessors' commits. Inheriting there froze the loop's view —
1099    /// `apply_batched` (`impl_migrations.rs`) drives `APPLY MIGRATION …
1100    /// BATCH N ROWS` by re-running the body with `LIMIT N` until fewer
1101    /// than `N` rows are affected, so a frozen snapshot re-selected the
1102    /// same rows forever and the migration never converged. Twenty-five
1103    /// rows at `BATCH 7` should finish in four iterations; it hung
1104    /// unbounded instead.
1105    ///
1106    /// `AS OF` still wins over both — it names its own snapshot explicitly.
1107    fn statement_snapshot(
1108        &self,
1109        query: &str,
1110        identity: StatementIdentity<'_>,
1111    ) -> RedDBResult<(Snapshot, Option<Xid>)> {
1112        match peek_top_level_as_of_with_table(query) {
1113            Some((spec, Some(table))) => {
1114                if !table.starts_with("red_") && !self.vcs_is_versioned(&table)? {
1115                    return Err(RedDBError::InvalidConfig(format!(
1116                        "AS OF requires a versioned collection — \
1117                         `{table}` has not opted in. \
1118                         Call vcs.set_versioned(\"{table}\", true) first."
1119                    )));
1120                }
1121                let xid = self.vcs_resolve_as_of(spec)?;
1122                Ok((
1123                    Snapshot {
1124                        xid,
1125                        in_progress: HashSet::new(),
1126                    },
1127                    Some(xid),
1128                ))
1129            }
1130            Some((spec, None)) => {
1131                let xid = self.vcs_resolve_as_of(spec)?;
1132                Ok((
1133                    Snapshot {
1134                        xid,
1135                        in_progress: HashSet::new(),
1136                    },
1137                    Some(xid),
1138                ))
1139            }
1140            None => match (identity, capture_current_snapshot()) {
1141                (StatementIdentity::Expr(_), Some(outer)) => Ok((outer.snapshot, None)),
1142                _ => Ok((self.current_snapshot(), None)),
1143            },
1144        }
1145    }
1146
1147    fn result_cache_safe(&self, conn_id: u64) -> bool {
1148        let has_active_xids = self
1149            .inner
1150            .transaction_state
1151            .snapshot_manager()
1152            .oldest_active_xid()
1153            .is_some();
1154        let in_own_tx = self.inner.transaction_state.in_transaction(conn_id);
1155        !has_active_xids && !in_own_tx
1156    }
1157}
1158
1159/// Whether a result's `engine` tag is one of the graph-analytics TVF
1160/// executors (issue #802). Graph-collection (`louvain(g)`) and inline
1161/// (`louvain(nodes => …, edges => …)`) forms both produce deterministic
1162/// algorithm output that is cached regardless of row count.
1163fn is_graph_tvf_engine(engine: &str) -> bool {
1164    matches!(engine, "runtime-graph-tvf" | "runtime-graph-tvf-inline")
1165}
1166
1167fn result_cache_key(query: &str) -> String {
1168    let tenant = current_tenant().unwrap_or_default();
1169    let auth = current_auth_identity()
1170        .map(|(user, role)| format!("{}|{:?}", user, role))
1171        .unwrap_or_default();
1172    if tenant.is_empty() && auth.is_empty() {
1173        query.to_string()
1174    } else {
1175        format!("{query}\u{001e}{tenant}\u{001e}{auth}")
1176    }
1177}
1178
1179#[cfg(test)]
1180mod tests {
1181    use super::*;
1182    use crate::api::RedDBOptions;
1183    use crate::runtime::impl_core::{
1184        clear_current_auth_identity, clear_current_tenant, set_current_auth_identity,
1185        set_current_tenant,
1186    };
1187    use crate::runtime::RedDBRuntime;
1188
1189    fn fresh_runtime() -> RedDBRuntime {
1190        RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("in-memory runtime")
1191    }
1192
1193    /// Ensure thread-local state from a prior test can't leak into
1194    /// the next one — tests in the same binary share the thread.
1195    fn reset_thread_locals() {
1196        clear_current_tenant();
1197        clear_current_auth_identity();
1198    }
1199
1200    #[test]
1201    fn autocommit_select_takes_live_snapshot() {
1202        reset_thread_locals();
1203        let rt = fresh_runtime();
1204        let frame = StatementExecutionFrame::build(&rt, StatementIdentity::Text("SELECT 1"))
1205            .expect("frame builds for SELECT 1");
1206
1207        // The inclusive bound excludes the next unallocated writer xid.
1208        // A pristine manager therefore has a valid empty snapshot at zero.
1209        let f: &dyn ReadFrame = &frame;
1210        assert!(f.as_of_floor().is_none(), "live read has no AS OF floor");
1211        assert_eq!(f.snapshot().xid, rt.snapshot_manager().peek_next_xid() - 1);
1212        assert!(!f.snapshot().sees(rt.snapshot_manager().peek_next_xid(), 0));
1213    }
1214
1215    #[test]
1216    fn frame_captures_identity_and_scope() {
1217        reset_thread_locals();
1218        set_current_tenant("acme".to_string());
1219        set_current_auth_identity("alice".to_string(), Role::Write);
1220
1221        let rt = fresh_runtime();
1222        let frame = StatementExecutionFrame::build(&rt, StatementIdentity::Text("SELECT 1"))
1223            .expect("frame builds");
1224        let f: &dyn ReadFrame = &frame;
1225
1226        assert_eq!(f.effective_scope(), Some("acme"));
1227        let id = f.identity().expect("identity captured");
1228        assert_eq!(id.0, "alice");
1229        assert!(matches!(id.1, Role::Write));
1230
1231        // Cache key mixes scope + identity so two callers under
1232        // different tenants never share a cache slot.
1233        assert!(
1234            f.cache_key().contains("acme") && f.cache_key().contains("alice"),
1235            "cache key folds in scope + identity, got {:?}",
1236            f.cache_key()
1237        );
1238
1239        reset_thread_locals();
1240    }
1241
1242    #[test]
1243    fn as_of_rejects_non_versioned_user_collection() {
1244        reset_thread_locals();
1245        let rt = fresh_runtime();
1246
1247        // `not_versioned` is a plain user collection — the frame
1248        // builder must reject AS OF until the caller opts in via
1249        // `vcs.set_versioned`.
1250        let err = match StatementExecutionFrame::build(
1251            &rt,
1252            StatementIdentity::Text("SELECT * FROM not_versioned AS OF COMMIT 'deadbeef'"),
1253        ) {
1254            Err(e) => e,
1255            Ok(_) => panic!("AS OF on non-versioned user collection rejected"),
1256        };
1257
1258        let msg = format!("{err}");
1259        assert!(
1260            msg.contains("AS OF requires a versioned collection"),
1261            "expected AS OF rejection, got: {msg}"
1262        );
1263    }
1264
1265    /// End-to-end proof that the SELECT path consumes a `ReadFrame`.
1266    ///
1267    /// Sets a tenant + identity via the public thread-local API the
1268    /// runtime uses for ambient scope, drives a real `SELECT` through
1269    /// `execute_query`, then inspects the result cache that the SELECT
1270    /// path populates via `frame.cache_key()`. The key only carries
1271    /// the tenant + identity *because* it was built through the frame —
1272    /// reverting the wiring to inline `current_tenant()` /
1273    /// `current_auth_identity()` reads would still pass this test, but
1274    /// dropping the frame entirely (so the SELECT path stopped touching
1275    /// `cache_key`) would break it.
1276    #[test]
1277    fn select_path_routes_through_frame_cache_key() {
1278        reset_thread_locals();
1279        set_current_tenant("acme".to_string());
1280        set_current_auth_identity("alice".to_string(), Role::Read);
1281
1282        let rt = fresh_runtime();
1283        let result = rt
1284            .execute_query("SELECT 1")
1285            .expect("SELECT 1 executes under tenant=acme/identity=alice");
1286        assert_eq!(result.statement_type, "select");
1287
1288        // The textual SELECT path builds a frame and
1289        // writes its result through `frame.cache_key()`. That key folds
1290        // tenant + identity in via `result_cache_key`, so finding "acme"
1291        // and "alice" inside any cached key proves the frame was the
1292        // seam used.
1293        let cache = rt.inner.result_cache.read();
1294        let any_keyed_with_scope = cache
1295            .0
1296            .keys()
1297            .any(|k| k.contains("acme") && k.contains("alice"));
1298        assert!(
1299            any_keyed_with_scope,
1300            "expected at least one result-cache key carrying tenant+identity, \
1301             got keys: {:?}",
1302            cache.0.keys().collect::<Vec<_>>()
1303        );
1304
1305        reset_thread_locals();
1306    }
1307
1308    /// A SELECT that calls a volatile builtin (here:
1309    /// `pg_advisory_unlock`, the volatile token the runtime currently
1310    /// recognises in `query_has_volatile_builtin`) must NOT populate
1311    /// the result cache. Any caller hitting the cache after this would
1312    /// see a stale answer for an inherently-volatile query, so the
1313    /// SELECT path gates writes through `frame.should_cache_result()`.
1314    ///
1315    /// Deletion test: removing `ReadFrame::should_cache_result`, or
1316    /// reverting the SELECT path to skip its safety gate, would let
1317    /// the result cache silently absorb this statement and break the
1318    /// assertion below.
1319    #[test]
1320    fn volatile_select_does_not_populate_result_cache() {
1321        reset_thread_locals();
1322        let rt = fresh_runtime();
1323
1324        // Frame-level invariant: the volatile-builtin signal collapses
1325        // `should_cache_result` to false even for an autocommit /
1326        // out-of-tx connection.
1327        let frame = StatementExecutionFrame::build(
1328            &rt,
1329            StatementIdentity::Text("SELECT pg_advisory_unlock(1)"),
1330        )
1331        .expect("frame");
1332        let f: &dyn ReadFrame = &frame;
1333        assert!(
1334            !f.should_cache_result(),
1335            "volatile builtin must disable result-cache safety"
1336        );
1337
1338        // End-to-end: drive the volatile SELECT through `execute_query`
1339        // and confirm no entry was stamped under its cache key. Other
1340        // entries from prior tests sharing the binary may exist, so we
1341        // assert specifically on this query's key.
1342        let _ = rt
1343            .execute_query("SELECT pg_advisory_unlock(1)")
1344            .expect("volatile SELECT executes");
1345        let cache = rt.inner.result_cache.read();
1346        let key = result_cache_key("SELECT pg_advisory_unlock(1)");
1347        assert!(
1348            !cache.0.contains_key(&key),
1349            "volatile SELECT must not populate result cache, found key {key:?} in {:?}",
1350            cache.0.keys().collect::<Vec<_>>()
1351        );
1352
1353        reset_thread_locals();
1354    }
1355
1356    #[test]
1357    fn blob_cache_backend_populates_blob_path_without_legacy_write() {
1358        reset_thread_locals();
1359        let rt = fresh_runtime();
1360        rt.inner
1361            .db
1362            .store()
1363            .set_config_tree("runtime.result_cache.backend", &crate::json!("blob_cache"));
1364
1365        let result = rt.execute_query("SELECT 1").expect("SELECT 1 executes");
1366        assert_eq!(result.statement_type, "select");
1367
1368        let key = result_cache_key("SELECT 1");
1369        assert!(
1370            rt.inner
1371                .result_blob_cache
1372                .get("runtime.result_cache", &key)
1373                .is_some(),
1374            "blob backend should stamp the Blob Cache path"
1375        );
1376        assert!(rt.inner.result_blob_entries.read().0.contains_key(&key));
1377        assert!(
1378            !rt.inner.result_cache.read().0.contains_key(&key),
1379            "blob backend should not write the legacy map"
1380        );
1381    }
1382
1383    #[test]
1384    fn blob_cache_backend_keeps_volatile_select_out_of_blob_path() {
1385        reset_thread_locals();
1386        let rt = fresh_runtime();
1387        rt.inner
1388            .db
1389            .store()
1390            .set_config_tree("runtime.result_cache.backend", &crate::json!("blob_cache"));
1391
1392        let _ = rt
1393            .execute_query("SELECT pg_advisory_unlock(1)")
1394            .expect("volatile SELECT executes");
1395        let key = result_cache_key("SELECT pg_advisory_unlock(1)");
1396        assert!(
1397            rt.inner
1398                .result_blob_cache
1399                .get("runtime.result_cache", &key)
1400                .is_none(),
1401            "volatile SELECT must not populate blob result cache"
1402        );
1403        assert!(!rt.inner.result_blob_entries.read().0.contains_key(&key));
1404    }
1405
1406    #[test]
1407    fn shadow_backend_dual_writes_and_reports_no_divergence_on_equal_results() {
1408        reset_thread_locals();
1409        let rt = fresh_runtime();
1410        rt.inner
1411            .db
1412            .store()
1413            .set_config_tree("runtime.result_cache.backend", &crate::json!("shadow"));
1414
1415        let first = rt.execute_query("SELECT 1").expect("first SELECT");
1416        let second = rt.execute_query("SELECT 1").expect("cached SELECT");
1417        assert_eq!(first.result.len(), second.result.len());
1418
1419        let key = result_cache_key("SELECT 1");
1420        assert!(rt.inner.result_cache.read().0.contains_key(&key));
1421        assert!(rt.inner.result_blob_entries.read().0.contains_key(&key));
1422        assert_eq!(rt.result_cache_shadow_divergences(), 0);
1423        assert_eq!(
1424            crate::runtime::METRIC_CACHE_SHADOW_DIVERGENCE_TOTAL,
1425            "cache_shadow_divergence_total"
1426        );
1427    }
1428
1429    #[test]
1430    fn as_of_on_red_collection_records_floor() {
1431        reset_thread_locals();
1432        let rt = fresh_runtime();
1433
1434        // `red_*` collections always allow AS OF. The frame should
1435        // resolve to a concrete xid and surface it via the Interface.
1436        let frame = StatementExecutionFrame::build(
1437            &rt,
1438            StatementIdentity::Text("SELECT * FROM red_commits AS OF SNAPSHOT 1"),
1439        )
1440        .expect("AS OF SNAPSHOT 1 on red_commits resolves");
1441
1442        let f: &dyn ReadFrame = &frame;
1443        assert_eq!(
1444            f.as_of_floor(),
1445            Some(1),
1446            "AS OF SNAPSHOT 1 records xid=1 as the floor"
1447        );
1448        assert_eq!(f.snapshot().xid, 1);
1449        assert!(
1450            f.snapshot().in_progress.is_empty(),
1451            "historical reads have no in-progress set"
1452        );
1453    }
1454
1455    /// The frame classifies common SQL prefixes into the coarse
1456    /// `Privilege` / `LockIntent` buckets at build time. This test
1457    /// pins the mapping so a regression that silently re-routes
1458    /// (e.g. INSERT classified as Read) surfaces here, not at a
1459    /// downstream privilege gate.
1460    #[test]
1461    fn frame_classifies_privilege_and_lock_intent_from_prefix() {
1462        reset_thread_locals();
1463        let rt = fresh_runtime();
1464
1465        let cases = [
1466            ("SELECT 1", Privilege::Read, LockIntent::Shared),
1467            ("LIST KV settings", Privilege::Read, LockIntent::Shared),
1468            (
1469                "KV GET settings.feature",
1470                Privilege::Read,
1471                LockIntent::Shared,
1472            ),
1473            ("VAULT LIST secrets", Privilege::Read, LockIntent::Shared),
1474            (
1475                "INSERT INTO t (id) VALUES (1)",
1476                Privilege::Write,
1477                LockIntent::Exclusive,
1478            ),
1479            (
1480                "KV PUT settings.feature = 'on'",
1481                Privilege::Write,
1482                LockIntent::Exclusive,
1483            ),
1484            (
1485                "VAULT PUT secrets.api = 'x'",
1486                Privilege::Write,
1487                LockIntent::Exclusive,
1488            ),
1489            (
1490                "UPDATE t SET x = 1 WHERE id = 1",
1491                Privilege::Write,
1492                LockIntent::Exclusive,
1493            ),
1494            (
1495                "DELETE FROM t WHERE id = 1",
1496                Privilege::Write,
1497                LockIntent::Exclusive,
1498            ),
1499            (
1500                "CREATE TABLE foo (id INT)",
1501                Privilege::Write,
1502                LockIntent::Exclusive,
1503            ),
1504            ("BEGIN", Privilege::None, LockIntent::None),
1505            ("COMMIT", Privilege::None, LockIntent::None),
1506            ("SET timezone = 'UTC'", Privilege::None, LockIntent::None),
1507        ];
1508
1509        for (q, want_priv, want_lock) in cases {
1510            let frame = StatementExecutionFrame::build(&rt, StatementIdentity::Text(q))
1511                .unwrap_or_else(|e| panic!("frame builds for {q:?}: {e}"));
1512            let f: &dyn ReadFrame = &frame;
1513            assert_eq!(f.required_privilege(), want_priv, "privilege for {q:?}");
1514            assert_eq!(f.lock_intent(), want_lock, "lock intent for {q:?}");
1515        }
1516    }
1517
1518    /// Deletion-test for `ReadFrame::required_privilege`: a SELECT
1519    /// driven through `execute_query` under an identity whose role
1520    /// doesn't satisfy the frame's coarse `Read` privilege gets
1521    /// denied with the frame's signal.
1522    ///
1523    /// We test the gate by classifying an INSERT (which the frame
1524    /// reports as `Privilege::Write`) under `Role::Read` — the only
1525    /// pair the legacy fallback would also reject, but here the
1526    /// rejection comes through `frame.check_query_privilege` BEFORE
1527    /// the parsed-expression walker runs. Removing
1528    /// `required_privilege` (or the `is_satisfied_by` consult inside
1529    /// `check_query_privilege`) would force the deny path back to the
1530    /// inline `RedDBRuntime::check_query_privilege` walker — but the
1531    /// auth_store gate up there is bypassed when no auth_store is
1532    /// wired (embedded test mode), so this test would FLIP from
1533    /// denied to permitted and break the assertion below.
1534    #[test]
1535    fn insert_under_read_role_denied_via_frame_privilege() {
1536        reset_thread_locals();
1537        set_current_auth_identity("alice".to_string(), Role::Read);
1538
1539        let rt = fresh_runtime();
1540        // Bypass parser by reaching into the frame directly: the
1541        // frame derives privilege from the SQL prefix without
1542        // needing an auth_store wired up. Driving end-to-end via
1543        // `execute_query` would also reject (no table `t`), but for
1544        // a different reason — we want to pin the privilege seam.
1545        let frame = StatementExecutionFrame::build(
1546            &rt,
1547            StatementIdentity::Text("INSERT INTO t (id) VALUES (1)"),
1548        )
1549        .expect("frame builds for INSERT");
1550        let f: &dyn ReadFrame = &frame;
1551        assert_eq!(
1552            f.required_privilege(),
1553            Privilege::Write,
1554            "INSERT classified as Write"
1555        );
1556        let id = f.identity().expect("identity captured");
1557        assert!(
1558            !f.required_privilege().is_satisfied_by(id.1),
1559            "Role::Read does not satisfy Privilege::Write — frame must deny"
1560        );
1561
1562        // End-to-end: the frame's `check_query_privilege` sees the
1563        // (Read role, Write privilege) mismatch and denies before
1564        // dispatch. We drive a synthetic `QueryExpr::Table` because
1565        // the SELECT/INSERT parser would happen to also fail, and we
1566        // want the failure to come from the privilege seam.
1567        use reddb_rql::ast::{QueryExpr, TableQuery};
1568        let expr = QueryExpr::Table(TableQuery::new("t"));
1569        let err = frame
1570            .check_query_privilege(&rt, &expr)
1571            .expect_err("denied via frame's coarse privilege gate");
1572        let msg = format!("{err}");
1573        assert!(
1574            msg.contains("permission denied") && msg.contains("Write"),
1575            "expected frame-level Write deny, got: {msg}"
1576        );
1577
1578        reset_thread_locals();
1579    }
1580
1581    /// End-to-end proof that the frame-owned row-buffer arena (#885) is
1582    /// wired into the SELECT path and produces observable results
1583    /// byte-identical to the per-request-allocation baseline.
1584    ///
1585    /// A table with more rows than the streaming high-water mark
1586    /// (`DEFAULT_HIGH_WATER_MARK`) forces the `execute_runtime_table_query_in`
1587    /// path to assemble many chunks, each leasing/recycling the frame
1588    /// arena's single chunk buffer. Driving it through `execute_query`
1589    /// (which builds a `StatementExecutionFrame` and lends its arena)
1590    /// must return every inserted row, in order — exactly what the
1591    /// allocate-per-chunk path returned. A bug in the arena wiring
1592    /// (dropped rows, bled rows, mis-ordering) would surface here.
1593    #[test]
1594    fn large_select_through_frame_arena_returns_all_rows_in_order() {
1595        reset_thread_locals();
1596        let rt = fresh_runtime();
1597        rt.execute_query("CREATE TABLE big (id INT)")
1598            .expect("create table");
1599
1600        // > DEFAULT_HIGH_WATER_MARK (1024) rows so the streaming channel
1601        // spans multiple chunks and the arena buffer is reused.
1602        const N: usize = 2_500;
1603        for start in (0..N).step_by(250) {
1604            let end = (start + 250).min(N);
1605            let values = (start..end)
1606                .map(|i| format!("({i})"))
1607                .collect::<Vec<_>>()
1608                .join(", ");
1609            rt.execute_query(&format!("INSERT INTO big (id) VALUES {values}"))
1610                .unwrap_or_else(|err| panic!("insert rows {start}..{end}: {err:?}"));
1611        }
1612
1613        let result = rt
1614            .execute_query("SELECT id FROM big ORDER BY id")
1615            .expect("large SELECT executes through the frame arena path");
1616        assert_eq!(result.statement_type, "select");
1617        assert_eq!(
1618            result.result.records.len(),
1619            N,
1620            "every inserted row streams back through the arena-backed channel"
1621        );
1622        for (i, record) in result.result.records.iter().enumerate() {
1623            assert_eq!(
1624                record.get("id"),
1625                Some(&reddb_types::Value::Integer(i as i64)),
1626                "row {i} is byte-identical to the per-request-allocation baseline"
1627            );
1628        }
1629
1630        reset_thread_locals();
1631    }
1632
1633    /// Transport adapters may decode their wire-specific parameter value
1634    /// shapes, but SQL parsing/binding must stay behind the runtime's
1635    /// statement entrypoint. This pins the deeper seam introduced for
1636    /// parameterized query execution. HTTP, JSON-RPC, RedWire, and gRPC hand
1637    /// a `QueryRequest` to the Request module, as does PG wire through the
1638    /// shared prepared registry. All paths install a real
1639    /// `StatementExecutionFrame` before dispatch.
1640    ///
1641    /// The Request transports reach the runtime entrypoint one layer deeper:
1642    /// `QueryRequestExecutor` calls `execute_query_with_params` on their
1643    /// behalf. Their delegation target is the Request seam, so that is what
1644    /// is pinned here — the "no binding in the adapter" half is unchanged
1645    /// for all.
1646    #[test]
1647    fn parameterized_transport_adapters_delegate_binding_to_runtime() {
1648        let manifest_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
1649        let adapters = [
1650            ("src/server/handlers_query.rs", "QueryRequestExecutor"),
1651            ("src/rpc_stdio.rs", "QueryRequestExecutor"),
1652            ("src/wire/redwire/session.rs", "QueryRequestExecutor"),
1653            ("src/wire/postgres/server.rs", "QueryRequestExecutor"),
1654            ("src/grpc.rs", "QueryRequestExecutor"),
1655        ];
1656
1657        for (relative, delegation) in adapters {
1658            let path = manifest_dir.join(relative);
1659            let text = std::fs::read_to_string(&path)
1660                .unwrap_or_else(|err| panic!("read {}: {err}", path.display()));
1661            assert!(
1662                text.contains(delegation),
1663                "{relative} should delegate parameterized query execution through {delegation}"
1664            );
1665            assert!(
1666                !text.contains("user_params::bind"),
1667                "{relative} must not bind SQL params in the transport adapter"
1668            );
1669        }
1670
1671        // The Request module is the only place RedWire's delegation may
1672        // bottom out: it must still reach the runtime entrypoint itself.
1673        let seam = std::fs::read_to_string(manifest_dir.join("src/runtime/query_request.rs"))
1674            .expect("read src/runtime/query_request.rs");
1675        assert!(
1676            seam.contains("execute_query_with_params"),
1677            "the Request module must execute text SQL through the runtime entrypoint"
1678        );
1679    }
1680
1681    /// Deletion-test for `ReadFrame::lock_intent`: a transaction
1682    /// control statement carries `LockIntent::None` and the
1683    /// `acquire_intent_locks` path returns `None` without consulting
1684    /// `intent_lock_modes_for`. Removing the method (or its consult
1685    /// site in `acquire_intent_locks`) would force the lock-mode
1686    /// helper to walk a fabricated parsed expression to reach the
1687    /// same conclusion — but the assertion that no guard is allocated
1688    /// for a `BEGIN` frame would still hold, so we additionally pin
1689    /// the classifier mapping above to make the deletion observable.
1690    #[test]
1691    fn control_statement_skips_intent_locks_via_frame() {
1692        reset_thread_locals();
1693        let rt = fresh_runtime();
1694
1695        let frame = StatementExecutionFrame::build(&rt, StatementIdentity::Text("BEGIN"))
1696            .expect("frame builds for BEGIN");
1697        let f: &dyn ReadFrame = &frame;
1698        assert_eq!(f.lock_intent(), LockIntent::None);
1699
1700        // Drive `acquire_intent_locks` against a fabricated SELECT
1701        // expression that WOULD normally yield `(IS, IS)`; the frame's
1702        // `lock_intent() == None` short-circuit must still suppress
1703        // the guard.
1704        use reddb_rql::ast::{QueryExpr, TableQuery};
1705        let expr = QueryExpr::Table(TableQuery::new("t"));
1706        let guard = frame.acquire_intent_locks(&rt, &expr);
1707        assert!(
1708            guard.is_none(),
1709            "BEGIN frame's lock_intent=None must short-circuit lock acquisition"
1710        );
1711    }
1712
1713    /// Nested sub-execution inside one statement (CTE prelude,
1714    /// materialized-view refresh, inline graph TVF) must read the
1715    /// enclosing statement's snapshot. Autocommit is the case that used
1716    /// to break: `current_snapshot()` mints a fresh high-water mark per
1717    /// call, so an inline graph TVF could materialize its nodes and its
1718    /// edges under two different snapshots within one statement.
1719    #[test]
1720    fn nested_frame_inherits_outer_statement_snapshot() {
1721        reset_thread_locals();
1722        let rt = fresh_runtime();
1723        rt.execute_query("CREATE TABLE nested_snap (id INT)")
1724            .expect("create table");
1725
1726        let outer = StatementExecutionFrame::build(
1727            &rt,
1728            StatementIdentity::Text("SELECT * FROM nested_snap"),
1729        )
1730        .expect("outer frame builds");
1731        let _outer_guards = outer.install(&rt);
1732
1733        // A write between the two builds moves the high-water mark, so a
1734        // re-minting nested frame would land on a different xid.
1735        rt.execute_query("INSERT INTO nested_snap (id) VALUES (1)")
1736            .expect("concurrent insert");
1737
1738        let expr = QueryExpr::Table(reddb_rql::ast::TableQuery::new("nested_snap"));
1739        let nested = StatementExecutionFrame::build(&rt, StatementIdentity::Expr(&expr))
1740            .expect("nested frame builds");
1741
1742        assert_eq!(
1743            ReadFrame::snapshot(&nested).xid,
1744            ReadFrame::snapshot(&outer).xid,
1745            "nested execution must inherit the outer statement's snapshot"
1746        );
1747    }
1748
1749    /// The other side of the same rule, and the one that matters more:
1750    /// a nested statement built from *text* is a statement in its own
1751    /// right, not part of the enclosing one, so it must mint a fresh
1752    /// snapshot and observe everything committed before it.
1753    ///
1754    /// `apply_batched` (`impl_migrations.rs`) is the load-bearing caller:
1755    /// it drives `APPLY MIGRATION … BATCH N ROWS` by re-running the body
1756    /// with `LIMIT N` through `execute_query` until fewer than `N` rows
1757    /// are affected. Inheriting the outer `APPLY MIGRATION` statement's
1758    /// snapshot froze that loop's view, so every iteration re-selected
1759    /// the same rows and the migration never converged — 25 rows at
1760    /// `BATCH 7` hung instead of finishing in four iterations.
1761    #[test]
1762    fn nested_text_statement_sees_commits_made_since_the_outer_frame() {
1763        reset_thread_locals();
1764        let rt = fresh_runtime();
1765        rt.execute_query("CREATE TABLE nested_text_snap (id INT)")
1766            .expect("create table");
1767
1768        let outer = StatementExecutionFrame::build(
1769            &rt,
1770            StatementIdentity::Text("SELECT * FROM nested_text_snap"),
1771        )
1772        .expect("outer frame builds");
1773        let _outer_guards = outer.install(&rt);
1774
1775        rt.execute_query("INSERT INTO nested_text_snap (id) VALUES (1)")
1776            .expect("concurrent insert");
1777
1778        let nested = StatementExecutionFrame::build(
1779            &rt,
1780            StatementIdentity::Text("SELECT * FROM nested_text_snap"),
1781        )
1782        .expect("nested frame builds");
1783
1784        assert_ne!(
1785            ReadFrame::snapshot(&nested).xid,
1786            ReadFrame::snapshot(&outer).xid,
1787            "a nested text statement must mint a fresh snapshot, not inherit \
1788             the enclosing statement's frozen view"
1789        );
1790    }
1791
1792    /// A frame with no statement text must not silently answer an
1793    /// `AS OF` read with live data. `TableQuery::as_of` is consumed
1794    /// nowhere in the runtime or scan path, so the only correct
1795    /// behaviours are "resolve it" (text path) or "refuse".
1796    #[test]
1797    fn expr_frame_rejects_as_of_instead_of_reading_live() {
1798        reset_thread_locals();
1799        let rt = fresh_runtime();
1800
1801        let mut table = reddb_rql::ast::TableQuery::new("red_commits");
1802        table.as_of = Some(reddb_rql::ast::AsOfClause::Snapshot(1));
1803        let expr = QueryExpr::Table(table);
1804
1805        let err = match StatementExecutionFrame::build(&rt, StatementIdentity::Expr(&expr)) {
1806            Err(err) => err,
1807            Ok(_) => panic!("AS OF without statement text must be refused, not read live"),
1808        };
1809        let msg = format!("{err}");
1810        assert!(
1811            msg.contains("AS OF cannot be resolved from a pre-parsed expression"),
1812            "expected a loud AS OF refusal, got: {msg}"
1813        );
1814    }
1815
1816    /// The expression path has no stable text key, so it must opt out of
1817    /// the result cache entirely. A kind label (`"table"`, `"insert"`)
1818    /// would key every statement of the same kind to one cache slot.
1819    #[test]
1820    fn expr_frame_opts_out_of_the_result_cache() {
1821        reset_thread_locals();
1822        let rt = fresh_runtime();
1823
1824        let expr = QueryExpr::Table(reddb_rql::ast::TableQuery::new("t"));
1825        let frame = StatementExecutionFrame::build(&rt, StatementIdentity::Expr(&expr))
1826            .expect("expr frame builds");
1827
1828        assert!(frame.cache_key().is_empty(), "expr frame has no cache key");
1829        assert!(
1830            !frame.can_read_result_cache(),
1831            "expr frame must never serve from the result cache"
1832        );
1833    }
1834}