reddb-io-server 1.2.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! `AuditRecordBuilder` — pure builder for `red_ask_audit` rows.
//!
//! Issue #402 (PRD #391): every ASK call writes one row to the
//! `red_ask_audit` system collection. This module owns the *shape* of
//! that row: which fields exist, how `answer_hash` is computed, what
//! `include_answer` toggles, what the keys look like on the wire.
//!
//! Deep module: no I/O, no clock, no collection access. Inputs are
//! plain data (the call state assembled by `execute_ask`); the output
//! is a [`BTreeMap`] ready to be passed to the insert path. Keeping
//! the builder pure means the audit schema is pinned by unit tests
//! and can't drift behind the spec.
//!
//! ## Field policy
//!
//! Always present (PRD §audit, ADR-319 `red_*` convention):
//!
//! - `ts` — caller-injected wall time in epoch nanoseconds. The
//!   builder takes it as input rather than reading the clock so that
//!   tests are deterministic; production callers feed
//!   `SystemTime::now()`.
//! - `tenant`, `user`, `role` — identity columns. Empty strings are
//!   allowed (e.g. embedded usage with no auth context) so the audit
//!   row still lands.
//! - `question` — verbatim user question.
//! - `sources_urns` — JSON array of stable source URNs (post-fusion,
//!   pre-redaction). Order is preserved from the input.
//! - `provider`, `model` — provider token and model id sent to the
//!   LLM. Both are recorded as the caller actually used them, not
//!   what the user requested, so the determinism contract (#400) and
//!   the capability fallback (#396) are auditable.
//! - `prompt_tokens`, `completion_tokens` — usage counters reported
//!   by the provider. Stored as `i64` (some providers can return 0).
//! - `cost_usd` — cost accrued for this call, `f64`. Always recorded
//!   even on cache hits (zero in that case).
//! - `answer_hash` — lowercase hex SHA-256 of the answer string.
//!   Deterministic; recorded regardless of `include_answer`.
//! - `citations` — JSON array of 1-indexed citation markers found in
//!   the answer (from `CitationParser`).
//! - `cache_hit` — boolean; `true` when answered from the answer
//!   cache (#403) without calling the LLM.
//! - `mode` — `"strict"` or `"lenient"`, the mode *effectively* used
//!   after any provider-capability fallback (#396).
//! - `temperature`, `seed` — determinism knobs actually sent to the
//!   provider. `null` means the selected provider does not support that
//!   knob, not that a default was forgotten.
//! - `validation_ok` — boolean; `true` iff the citation validator
//!   (#395) returned `Decision::Ok` for the final answer.
//! - `retry_count` — `0` or `1`. The strict-mode retry budget is
//!   pinned at one (#395), but we record the count so a future budget
//!   change doesn't silently corrupt the schema.
//! - `errors` — JSON array of `{kind, detail}` objects. Empty when
//!   the call succeeded.
//!
//! Conditional:
//!
//! - `answer` — full answer string. Only present when
//!   `Settings::include_answer == true`. Default is `false`; the
//!   `ask.audit.include_answer` setting flips it on per deployment.
//!   The shape change is explicit (key absent vs key present) so
//!   downstream consumers can detect it without sentinels.
//!
//! ## Why a deep module
//!
//! The schema is a contract: operators write dashboards against it,
//! retention purges read `ts` directly, replication forwarders
//! serialize it. Concentrating the build logic in one place — with
//! every field pinned by a test — keeps the contract honest. The
//! `execute_ask` glue can then call `AuditRecordBuilder::build(...)`
//! and treat the result as opaque key/value rows.

use std::collections::BTreeMap;

use sha2::{Digest, Sha256};

use crate::json;
use crate::runtime::ai::strict_validator::{Mode, ValidationError, ValidationErrorKind};
use crate::serde_json::Value;

/// Deployment-level audit settings.
///
/// Surfaced via the `ask.audit.*` settings tree; threaded into the
/// builder so tests can pin both shapes without touching global
/// config.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Settings {
    /// When `true`, store the full answer string under the `answer`
    /// key in addition to `answer_hash`. Default `false`.
    pub include_answer: bool,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            include_answer: false,
        }
    }
}

/// All call state needed to render one audit row. Plain data —
/// borrowed where cheap, owned only for the fields the caller almost
/// always owns at insert time.
#[derive(Debug, Clone)]
pub struct CallState<'a> {
    /// Epoch nanoseconds. Injected by the caller (do not read the
    /// clock inside the builder).
    pub ts_nanos: i64,
    pub tenant: &'a str,
    pub user: &'a str,
    pub role: &'a str,
    pub question: &'a str,
    pub sources_urns: &'a [String],
    pub provider: &'a str,
    pub model: &'a str,
    pub prompt_tokens: i64,
    pub completion_tokens: i64,
    pub cost_usd: f64,
    /// The full LLM answer text. Always hashed; only stored when
    /// `Settings::include_answer == true`.
    pub answer: &'a str,
    /// 1-indexed citation markers parsed from the answer.
    pub citations: &'a [u32],
    pub cache_hit: bool,
    /// The mode effectively used — after provider capability fallback
    /// (#396), not the mode the user asked for.
    pub effective_mode: Mode,
    pub temperature: Option<f32>,
    pub seed: Option<u64>,
    pub validation_ok: bool,
    pub retry_count: u32,
    pub errors: &'a [ValidationError],
}

/// Produce one audit row, ready to insert into `red_ask_audit`.
///
/// The keys are stable (pinned by tests) and the value types match
/// what the storage layer expects for the collection's columns. The
/// `BTreeMap` ordering is alphabetical; downstream consumers MUST NOT
/// rely on insertion order, but operators reading raw rows benefit
/// from the predictable layout.
pub fn build(state: &CallState<'_>, settings: Settings) -> BTreeMap<&'static str, Value> {
    let mut row: BTreeMap<&'static str, Value> = BTreeMap::new();

    row.insert("ts", json!(state.ts_nanos));
    row.insert("tenant", json!(state.tenant));
    row.insert("user", json!(state.user));
    row.insert("role", json!(state.role));
    row.insert("question", json!(state.question));
    row.insert("sources_urns", json!(state.sources_urns));
    row.insert("provider", json!(state.provider));
    row.insert("model", json!(state.model));
    row.insert("prompt_tokens", json!(state.prompt_tokens));
    row.insert("completion_tokens", json!(state.completion_tokens));
    row.insert("cost_usd", json!(state.cost_usd));
    row.insert("answer_hash", json!(answer_hash(state.answer)));
    row.insert("citations", json!(state.citations));
    row.insert("cache_hit", json!(state.cache_hit));
    row.insert("mode", json!(mode_str(state.effective_mode)));
    row.insert(
        "temperature",
        state
            .temperature
            .map(|value| json!(value))
            .unwrap_or(Value::Null),
    );
    row.insert(
        "seed",
        state.seed.map(|value| json!(value)).unwrap_or(Value::Null),
    );
    row.insert("validation_ok", json!(state.validation_ok));
    row.insert("retry_count", json!(state.retry_count));
    row.insert(
        "errors",
        Value::Array(state.errors.iter().map(error_json).collect()),
    );

    if settings.include_answer {
        row.insert("answer", json!(state.answer));
    }

    row
}

/// Lowercase hex SHA-256 of the answer text. Deterministic; the
/// audit row is identical for byte-equal answers regardless of when
/// or where the call ran.
pub fn answer_hash(answer: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(answer.as_bytes());
    let bytes = hasher.finalize();
    let mut out = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        out.push_str(&format!("{b:02x}"));
    }
    out
}

fn mode_str(mode: Mode) -> &'static str {
    match mode {
        Mode::Strict => "strict",
        Mode::Lenient => "lenient",
    }
}

fn error_kind_str(kind: ValidationErrorKind) -> &'static str {
    match kind {
        ValidationErrorKind::Malformed => "malformed",
        ValidationErrorKind::OutOfRange => "out_of_range",
    }
}

fn error_json(err: &ValidationError) -> Value {
    json!({
        "kind": error_kind_str(err.kind),
        "detail": err.detail,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn base_state<'a>(
        question: &'a str,
        urns: &'a [String],
        answer: &'a str,
        citations: &'a [u32],
        errors: &'a [ValidationError],
    ) -> CallState<'a> {
        CallState {
            ts_nanos: 1_700_000_000_000_000_000,
            tenant: "acme",
            user: "alice",
            role: "analyst",
            question,
            sources_urns: urns,
            provider: "openai",
            model: "gpt-4o-mini",
            prompt_tokens: 123,
            completion_tokens: 45,
            cost_usd: 0.0012,
            answer,
            citations,
            cache_hit: false,
            effective_mode: Mode::Strict,
            temperature: Some(0.0),
            seed: Some(42),
            validation_ok: true,
            retry_count: 0,
            errors,
        }
    }

    // ---- answer_hash ----------------------------------------------------

    #[test]
    fn answer_hash_is_deterministic_sha256() {
        // Known SHA-256 of empty string.
        assert_eq!(
            answer_hash(""),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
    }

    #[test]
    fn answer_hash_known_value_for_short_string() {
        // sha256("hello") — pinned so a regression in hashing is loud.
        assert_eq!(
            answer_hash("hello"),
            "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
        );
    }

    #[test]
    fn answer_hash_repeated_calls_byte_equal() {
        let a = answer_hash("the cat sat on the mat");
        let b = answer_hash("the cat sat on the mat");
        assert_eq!(a, b);
    }

    #[test]
    fn answer_hash_differs_for_differing_input() {
        assert_ne!(answer_hash("a"), answer_hash("b"));
    }

    // ---- core schema ---------------------------------------------------

    #[test]
    fn build_emits_every_required_field() {
        let urns = vec!["urn:a".to_string(), "urn:b".to_string()];
        let citations = vec![1u32, 2];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q?", &urns, "answer text", &citations, &errors);

        let row = build(&state, Settings::default());

        for key in [
            "ts",
            "tenant",
            "user",
            "role",
            "question",
            "sources_urns",
            "provider",
            "model",
            "prompt_tokens",
            "completion_tokens",
            "cost_usd",
            "answer_hash",
            "citations",
            "cache_hit",
            "mode",
            "temperature",
            "seed",
            "validation_ok",
            "retry_count",
            "errors",
        ] {
            assert!(row.contains_key(key), "row missing required field `{key}`");
        }
    }

    #[test]
    fn build_field_values_match_state() {
        let urns = vec!["urn:x".to_string()];
        let citations = vec![3u32];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("why?", &urns, "because", &citations, &errors);

        let row = build(&state, Settings::default());

        assert_eq!(row["ts"], json!(1_700_000_000_000_000_000_i64));
        assert_eq!(row["tenant"], json!("acme"));
        assert_eq!(row["user"], json!("alice"));
        assert_eq!(row["role"], json!("analyst"));
        assert_eq!(row["question"], json!("why?"));
        assert_eq!(row["sources_urns"], json!(["urn:x"]));
        assert_eq!(row["provider"], json!("openai"));
        assert_eq!(row["model"], json!("gpt-4o-mini"));
        assert_eq!(row["prompt_tokens"], json!(123));
        assert_eq!(row["completion_tokens"], json!(45));
        assert_eq!(row["cost_usd"], json!(0.0012));
        assert_eq!(row["answer_hash"], json!(answer_hash("because")));
        assert_eq!(row["citations"], json!([3]));
        assert_eq!(row["cache_hit"], json!(false));
        assert_eq!(row["mode"], json!("strict"));
        assert_eq!(row["temperature"], json!(0.0));
        assert_eq!(row["seed"], json!(42u64));
        assert_eq!(row["validation_ok"], json!(true));
        assert_eq!(row["retry_count"], json!(0));
        assert_eq!(row["errors"], json!([]));
    }

    #[test]
    fn unsupported_determinism_knobs_are_recorded_as_null() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let mut state = base_state("q", &urns, "a", &citations, &errors);
        state.temperature = None;
        state.seed = None;

        let row = build(&state, Settings::default());

        assert_eq!(row["temperature"], Value::Null);
        assert_eq!(row["seed"], Value::Null);
    }

    // ---- include_answer toggle -----------------------------------------

    #[test]
    fn answer_field_absent_by_default() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q", &urns, "secret answer", &citations, &errors);

        let row = build(&state, Settings::default());

        assert!(!row.contains_key("answer"));
        // Hash is still recorded — operators can compare hashes
        // without the answer itself.
        assert_eq!(row["answer_hash"], json!(answer_hash("secret answer")));
    }

    #[test]
    fn answer_field_present_when_include_answer_set() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q", &urns, "full text", &citations, &errors);

        let row = build(
            &state,
            Settings {
                include_answer: true,
            },
        );

        assert_eq!(row["answer"], json!("full text"));
        // Hash is still present — toggling the flag must not silently
        // remove other fields.
        assert_eq!(row["answer_hash"], json!(answer_hash("full text")));
    }

    // ---- mode + validation --------------------------------------------

    #[test]
    fn lenient_mode_serializes_as_lenient_string() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let mut state = base_state("q", &urns, "a", &citations, &errors);
        state.effective_mode = Mode::Lenient;

        let row = build(&state, Settings::default());

        assert_eq!(row["mode"], json!("lenient"));
    }

    #[test]
    fn errors_round_trip_with_kind_and_detail() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors = vec![
            ValidationError {
                kind: ValidationErrorKind::Malformed,
                detail: "empty marker body".to_string(),
            },
            ValidationError {
                kind: ValidationErrorKind::OutOfRange,
                detail: "marker [^9] references source #9".to_string(),
            },
        ];
        let mut state = base_state("q", &urns, "a", &citations, &errors);
        state.validation_ok = false;
        state.retry_count = 1;

        let row = build(&state, Settings::default());

        assert_eq!(row["validation_ok"], json!(false));
        assert_eq!(row["retry_count"], json!(1));
        assert_eq!(
            row["errors"],
            json!([
                json!({"kind": "malformed", "detail": "empty marker body"}),
                json!({"kind": "out_of_range", "detail": "marker [^9] references source #9"}),
            ])
        );
    }

    // ---- cache hit ------------------------------------------------------

    #[test]
    fn cache_hit_recorded() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let mut state = base_state("q", &urns, "cached", &citations, &errors);
        state.cache_hit = true;
        state.prompt_tokens = 0;
        state.completion_tokens = 0;
        state.cost_usd = 0.0;

        let row = build(&state, Settings::default());

        assert_eq!(row["cache_hit"], json!(true));
        // Cost is recorded even when zero — downstream sum() must not
        // see a missing-field surprise.
        assert_eq!(row["cost_usd"], json!(0.0));
        assert_eq!(row["prompt_tokens"], json!(0));
    }

    // ---- edge cases ----------------------------------------------------

    #[test]
    fn empty_identity_fields_allowed() {
        // Embedded use with no auth context — we still want the row
        // to land. Empty strings serialize as empty strings, not null.
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let mut state = base_state("q", &urns, "a", &citations, &errors);
        state.tenant = "";
        state.user = "";
        state.role = "";

        let row = build(&state, Settings::default());

        assert_eq!(row["tenant"], json!(""));
        assert_eq!(row["user"], json!(""));
        assert_eq!(row["role"], json!(""));
    }

    #[test]
    fn empty_sources_serializes_as_empty_array() {
        let urns: Vec<String> = vec![];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q", &urns, "a", &citations, &errors);

        let row = build(&state, Settings::default());

        assert_eq!(row["sources_urns"], json!([]));
        assert_eq!(row["citations"], json!([]));
        assert_eq!(row["errors"], json!([]));
    }

    #[test]
    fn sources_order_preserved() {
        // RRF (#398) emits a *ranked* list — the audit row MUST keep
        // that ranking so post-hoc analysis of "what did the model
        // see, in what order" stays honest.
        let urns = vec![
            "urn:c".to_string(),
            "urn:a".to_string(),
            "urn:b".to_string(),
        ];
        let citations: Vec<u32> = vec![];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q", &urns, "a", &citations, &errors);

        let row = build(&state, Settings::default());

        assert_eq!(row["sources_urns"], json!(["urn:c", "urn:a", "urn:b"]));
    }

    #[test]
    fn build_is_deterministic_across_calls() {
        // Same inputs → byte-equal rows. Required by the ASK
        // determinism contract (#400): the audit trail must not
        // depend on map-randomization or any clock side-effect.
        let urns = vec!["urn:a".to_string()];
        let citations = vec![1u32];
        let errors: Vec<ValidationError> = vec![];
        let state = base_state("q", &urns, "a", &citations, &errors);

        let a = build(&state, Settings::default());
        let b = build(&state, Settings::default());
        assert_eq!(a, b);
    }
}