reddb-io-server 1.12.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Parser hardening test suite for the ASK / AI-extension surface
//! (issue #101).
//!
//! Reuses the `tests/support/parser_hardening` harness from #87 to
//! cover the `ASK '<question>' [USING …] [MODEL …] [DEPTH n]
//! [LIMIT n] [COLLECTION col]` and the `SEARCH CONTEXT '<query>' …`
//! shapes. Both reach the production parser through the standard
//! `reddb_server::storage::query::parser::parse` entry point so
//! `ParserLimits` cascade automatically.
//!
//! Phase A — tests-only. Bugs uncovered here are pinned with
//! `FIXME(#101): …` in the failing assertion; follow-up issues fix
//! the parser source without touching this file.

mod support {
    pub mod parser_hardening;
}

use proptest::prelude::*;
use reddb_server::storage::query::ast::{QueryExpr, SearchCommand};
use reddb_server::storage::query::parser::{self, ParseError, ParserLimits};
use support::parser_hardening::{
    self as harness, ask_grammar, assert_no_panic_on, corpus::ask_adversarial_inputs,
    HardenedParser,
};

/// `HardenedParser` shim around the ASK / SEARCH CONTEXT surface.
/// Both shapes share the top-level entry point with the rest of the
/// SQL grammar, so the shim simply funnels into `parser::parse` —
/// the property + snapshot suites below are what concentrate the
/// coverage on the AI extension subset.
pub struct AskParser;

impl HardenedParser for AskParser {
    type Error = ParseError;

    fn parse(input: &str) -> Result<(), Self::Error> {
        parser::parse(input).map(|_| ())
    }

    fn parse_with_limits(input: &str, limits: ParserLimits) -> Result<(), Self::Error> {
        let mut p = parser::Parser::with_limits(input, limits)?;
        p.parse().map(|_| ())
    }
}

// ---- panic-safety on adversarial corpus -------------------------

#[test]
fn ask_parser_does_not_panic_on_adversarial_corpus() {
    let handle = std::thread::Builder::new()
        .stack_size(8 * 1024 * 1024)
        .spawn(|| {
            for (name, input) in ask_adversarial_inputs() {
                let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    assert_no_panic_on::<AskParser>(&input);
                }));
                if result.is_err() {
                    panic!("ask adversarial corpus entry {} panicked", name);
                }
            }
        })
        .expect("spawn corpus thread");
    handle.join().expect("corpus thread panic");
}

// ---- property tests ---------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig {
        cases: 256,
        max_shrink_iters: 64,
        ..ProptestConfig::default()
    })]

    /// Strategy 1: full ASK shape with all clauses optional
    /// (USING excluded — see strategy 2 + FIXME(#101)). Generated
    /// input must parse cleanly.
    #[test]
    fn proptest_ask_full_shape_roundtrips(s in ask_grammar::ask_stmt()) {
        harness::roundtrip_property::<AskParser>(&s);
        prop_assert!(
            AskParser::parse(&s).is_ok(),
            "ask full shape did not parse: {}", s
        );
    }

    /// Strategy 2: ASK with the USING-provider clause concentrated.
    /// Now that #101 / #108 fixed `parse_ask_query` to match `USING`
    /// via `Token::Using`, this property asserts the generated input
    /// parses cleanly — same shape as the other strategies.
    #[test]
    fn proptest_ask_using_provider_roundtrips(
        s in ask_grammar::ask_using_provider_stmt(),
    ) {
        harness::roundtrip_property::<AskParser>(&s);
        prop_assert!(
            AskParser::parse(&s).is_ok(),
            "ask USING <provider> did not parse: {}", s
        );
    }

    /// Strategy 3: ASK with the MODEL string-literal clause
    /// concentrated. Pins the `MODEL '<name>'` slot.
    #[test]
    fn proptest_ask_model_ident_roundtrips(s in ask_grammar::ask_model_ident_stmt()) {
        harness::roundtrip_property::<AskParser>(&s);
        prop_assert!(
            AskParser::parse(&s).is_ok(),
            "ask MODEL '<name>' did not parse: {}", s
        );
    }

    /// Strategy 4: SEARCH CONTEXT with optional FIELD / COLLECTION /
    /// LIMIT / DEPTH clauses.
    #[test]
    fn proptest_search_context_roundtrips(s in ask_grammar::search_context_stmt()) {
        harness::roundtrip_property::<AskParser>(&s);
        prop_assert!(
            AskParser::parse(&s).is_ok(),
            "search context did not parse: {}", s
        );
    }

    /// Strategy 5: ASK with depth + scope (LIMIT) numeric ranges.
    #[test]
    fn proptest_ask_depth_scope_roundtrips(s in ask_grammar::ask_depth_scope_stmt()) {
        harness::roundtrip_property::<AskParser>(&s);
        prop_assert!(
            AskParser::parse(&s).is_ok(),
            "ask DEPTH/LIMIT did not parse: {}", s
        );
    }

    /// Arbitrary-bytes suffix on each AI keyword: never panic.
    #[test]
    fn proptest_ask_arbitrary_suffix_no_panic(
        prefix in prop_oneof![
            Just("ASK ".to_string()),
            Just("ASK 'q' ".to_string()),
            Just("SEARCH CONTEXT ".to_string()),
            Just("SEARCH CONTEXT 'q' ".to_string()),
        ],
        suffix in ".{0,512}",
    ) {
        let s = format!("{}{}", prefix, suffix);
        harness::roundtrip_property::<AskParser>(&s);
    }

    /// Tighter input-size limit refuses oversized ASK questions. The
    /// 64-byte ceiling is well below the expanded input length so the
    /// parser must reject before the lexer gets to the question body.
    #[test]
    fn proptest_ask_input_size_limit_enforced(len in 200usize..2000) {
        let limits = ParserLimits {
            max_input_bytes: 64,
            ..ParserLimits::default()
        };
        let body = "x".repeat(len);
        let input = format!("ASK '{}'", body);
        let r = AskParser::parse_with_limits(&input, limits);
        prop_assert!(r.is_err(), "oversized ASK question must error");
    }
}

// ---- happy-path regression tests --------------------------------
//
// These pin the documented ASK / SEARCH CONTEXT shapes against
// future grammar drift. The `parse_query` helper unwraps the
// `QueryWithCte` envelope so each test asserts directly against the
// AST node.

fn parse_query(input: &str) -> QueryExpr {
    parser::parse(input)
        .unwrap_or_else(|e| panic!("expected ok for {input:?}, got error: {e}"))
        .query
}

#[test]
fn ask_minimal_question_parses() {
    let q = parse_query("ASK 'why is the sky blue?'");
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.question, "why is the sky blue?");
            assert_eq!(ask.provider, None);
            assert_eq!(ask.model, None);
            assert_eq!(ask.depth, None);
            assert_eq!(ask.limit, None);
            assert_eq!(ask.collection, None);
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_with_model_only_parses() {
    let q = parse_query("ASK 'q' MODEL 'gpt-4o-mini'");
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.question, "q");
            assert_eq!(ask.model.as_deref(), Some("gpt-4o-mini"));
            assert_eq!(ask.provider, None);
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_with_depth_limit_collection_parses() {
    let q = parse_query("ASK 'q' DEPTH 3 LIMIT 25 MIN_SCORE 0.7 COLLECTION docs");
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.depth, Some(3));
            assert_eq!(ask.limit, Some(25));
            assert_eq!(ask.min_score, Some(0.7));
            assert_eq!(ask.collection.as_deref(), Some("docs"));
            assert_eq!(ask.provider, None);
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_full_chain_without_using_parses() {
    // `USING <provider>` is omitted here; the dedicated
    // `ask_using_provider_parses` test pins the USING clause shape
    // separately. Every other documented clause is exercised here.
    let q = parse_query(
        "ASK 'what happened?' MODEL 'claude-3-5-sonnet' \
         DEPTH 2 LIMIT 50 MIN_SCORE 0.7 COLLECTION events",
    );
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.question, "what happened?");
            assert_eq!(ask.model.as_deref(), Some("claude-3-5-sonnet"));
            assert_eq!(ask.depth, Some(2));
            assert_eq!(ask.limit, Some(50));
            assert_eq!(ask.min_score, Some(0.7));
            assert_eq!(ask.collection.as_deref(), Some("events"));
            assert_eq!(ask.provider, None);
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

/// Regression guard for #101 / #108: `parse_ask_query` now matches
/// `USING` via `Token::Using` (the typed-keyword consumer), so the
/// optional `USING <provider>` clause on `ASK '…'` parses
/// end-to-end. Mirrors the #92 fix that flipped `DEPENDS ON` /
/// `FOR TENANT` / `APPLY MIGRATION` to typed consumers.
#[test]
fn ask_using_provider_parses() {
    let q = parse_query("ASK 'who?' USING openai");
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.question, "who?");
            assert_eq!(ask.provider.as_deref(), Some("openai"));
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn search_context_minimal_parses_with_defaults() {
    let q = parse_query("SEARCH CONTEXT 'find this'");
    match q {
        QueryExpr::SearchCommand(SearchCommand::Context {
            query,
            field,
            collection,
            limit,
            depth,
            ..
        }) => {
            assert_eq!(query, "find this");
            assert_eq!(field, None);
            assert_eq!(collection, None);
            // Default limit and depth are documented in
            // `parse_search_context` (limit=25, depth=1).
            assert_eq!(limit, 25);
            assert_eq!(depth, 1);
        }
        other => panic!("expected SearchCommand::Context, got {other:?}"),
    }
}

#[test]
fn search_context_full_clause_chain_parses() {
    let q = parse_query(
        "SEARCH CONTEXT '000.000.000-00' FIELD cpf COLLECTION customers LIMIT 50 DEPTH 2",
    );
    match q {
        QueryExpr::SearchCommand(SearchCommand::Context {
            query,
            field,
            collection,
            limit,
            depth,
            ..
        }) => {
            assert_eq!(query, "000.000.000-00");
            assert_eq!(field.as_deref(), Some("cpf"));
            assert_eq!(collection.as_deref(), Some("customers"));
            assert_eq!(limit, 50);
            assert_eq!(depth, 2);
        }
        other => panic!("expected SearchCommand::Context, got {other:?}"),
    }
}

#[test]
fn ask_as_rql_clause_parses() {
    let q = parse_query("ASK 'who owns passport FDD-12313?' AS RQL");
    match q {
        QueryExpr::Ask(ask) => {
            assert!(ask.as_rql, "AS RQL should set as_rql");
            assert!(!ask.execute, "AS RQL without EXECUTE must not set execute");
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_as_rql_execute_clause_parses() {
    // `EXECUTE` is the opt-in that auto-runs a read-only candidate; it
    // can appear with `AS RQL` in any order.
    let q = parse_query("ASK 'who owns passport FDD-12313?' AS RQL EXECUTE");
    match q {
        QueryExpr::Ask(ask) => {
            assert!(ask.as_rql);
            assert!(ask.execute, "EXECUTE should set execute");
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_execute_before_as_rql_parses() {
    let q = parse_query("ASK 'q' EXECUTE AS RQL");
    match q {
        QueryExpr::Ask(ask) => {
            assert!(ask.as_rql);
            assert!(ask.execute);
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}

#[test]
fn ask_execute_specified_twice_errors() {
    let err = parser::parse("ASK 'q' EXECUTE EXECUTE").expect_err("double EXECUTE must error");
    assert!(
        err.to_string().contains("EXECUTE specified more than once"),
        "got: {err}"
    );
}

// ---- inference seam with a mock model (#1273) --------------------
//
// There is no stubbable LLM transport on the SQL ASK path, so the
// generate-provider seam is exercised here through a mock `RqlModel`
// closure that stands in for the configured text2text provider.

mod ask_rql_inference {
    use reddb_server::runtime::ai::ask_rql_planner::{infer, CandidateDisposition};
    use reddb_server::runtime::ask_pipeline::CandidateCollections;

    fn candidates() -> CandidateCollections {
        CandidateCollections {
            collections: vec!["travelers".to_string()],
            columns_by_collection: std::collections::HashMap::from([(
                "travelers".to_string(),
                vec!["passport".to_string(), "name".to_string()],
            )]),
        }
    }

    #[test]
    fn invalid_candidate_is_rejected_by_parser() {
        let model = |_p: &str| Ok("not valid rql at all".to_string());
        let err = infer("q", &candidates(), Some("travelers"), false, &model).unwrap_err();
        assert!(err.to_string().contains("invalid RQL candidate"), "{err}");
    }

    #[test]
    fn default_returns_validated_candidate_without_executing() {
        let model =
            |_p: &str| Ok("SELECT * FROM travelers WHERE passport = 'FDD-12313'".to_string());
        let out = infer("q", &candidates(), Some("travelers"), false, &model).unwrap();
        assert!(!out.execute);
        assert_eq!(out.candidate.disposition, CandidateDisposition::ReadOnly);
    }

    #[test]
    fn execute_runs_read_only_candidate() {
        let model =
            |_p: &str| Ok("SELECT * FROM travelers WHERE passport = 'FDD-12313'".to_string());
        let out = infer("q", &candidates(), Some("travelers"), true, &model).unwrap();
        assert!(out.execute, "read-only candidate with EXECUTE must run");
    }

    #[test]
    fn mutating_candidate_refused_for_execute() {
        let model = |_p: &str| Ok("DELETE FROM travelers WHERE passport = 'FDD-12313'".to_string());
        let err = infer("q", &candidates(), Some("travelers"), true, &model).unwrap_err();
        assert!(err.to_string().contains("refused"), "{err}");
    }
}

#[test]
fn ask_lowercase_keyword_parses() {
    // The dispatch path uses `eq_ignore_ascii_case("ASK")`, so the
    // lowercase form must round-trip. Pinning prevents a future
    // tightening of the dispatcher from breaking case-insensitive
    // RQL clients.
    let q = parse_query("ask 'q' depth 3");
    match q {
        QueryExpr::Ask(ask) => {
            assert_eq!(ask.question, "q");
            assert_eq!(ask.depth, Some(3));
        }
        other => panic!("expected Ask, got {other:?}"),
    }
}