nmbrs-runtime 0.4.0

Workload execution runtime for nmbrs
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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! End-to-end validation tests for the recall@k relevancy pipeline
//! (SRD 33). Drives `ValidatingDispenser` with mock adapter results
//! shaped like a CQL `SELECT key FROM ... ANN OF ...` response and
//! verifies that non-zero recall is produced when the returned keys
//! match a ground-truth binding.
//!
//! All ground-truth values flow through the canonical SRD 32 path:
//! a per-test [`ScopeFixture`] is opened against a synthetic
//! `PolydatProgram` that declares `ground_truth` as an input slot;
//! [`ValidatingDispenser::wrap`] registers a `PullHandle` for it;
//! at execute time, the handle reads from a `ResolvedPulls`
//! materialized from the program's state. There is no fields-based
//! fallback — the validation wrapper has exactly one read path.

use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;

use nmbrs_metrics::labels::Labels;
use nmbrs_runtime::adapter::{
    ExecCtx, ExecutionError, OpDispenser, OpResult, ResolvedFields, ResultBody,
};
use nmbrs_runtime::fixture::{PullPlan, ResolvedPulls, ScopeFixture};
use nmbrs_runtime::validation::ValidatingDispenser;
use nmbrs_runtime::wires::CycleWires;
use polydat::ast::Value;
use polydat::dsl::compile::compile_polydat_interpreter;
use polydat::kernel::PolydatProgram;

/// Result body shaped like `CqlResultBody.to_json()` — a JSON array of
/// row objects, each with a `"key"` column holding the decimal-text
/// representation of the row index (matching what `format_u64(row, 10)`
/// produces in the workload's rampup phase).
#[derive(Debug)]
struct RowsBody {
    rows: Vec<HashMap<String, serde_json::Value>>,
}

impl ResultBody for RowsBody {
    fn to_json(&self) -> serde_json::Value {
        serde_json::Value::Array(
            self.rows
                .iter()
                .map(|row| {
                    serde_json::Value::Object(
                        row.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
                    )
                })
                .collect(),
        )
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn element_count(&self) -> u64 {
        self.rows.len() as u64
    }
}

/// Build a result body with `keys` as string-valued `key` columns.
fn rows_body(keys: &[i64]) -> Box<dyn ResultBody> {
    let rows = keys
        .iter()
        .map(|k| {
            let mut row = HashMap::new();
            row.insert("key".into(), serde_json::Value::String(k.to_string()));
            row
        })
        .collect();
    Box::new(RowsBody { rows })
}

/// Fake inner dispenser: returns a fixed, pre-built body.
struct FixedBodyDispenser {
    keys: Vec<i64>,
}

impl OpDispenser for FixedBodyDispenser {
    fn execute<'a>(
        &'a self,
        _cycle: u64,
        _ctx: &'a ExecCtx<'a>,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<OpResult, ExecutionError>> + Send + 'a>,
    > {
        let body = rows_body(&self.keys);
        Box::pin(async move {
            Ok(OpResult {
                body: Some(body),
                skipped: false,
            })
        })
    }
}

/// Build a synthetic PolydatProgram via the DSL that declares `cycle`
/// as a coordinate and `ground_truth` as an extern Str input slot.
/// The fixture's `register_pull("ground_truth")` resolves to that
/// slot; tests inject the ground-truth value via `set_input` per
/// cycle.
fn make_gt_program() -> Arc<PolydatProgram> {
    let kernel = compile_polydat_interpreter(
        "input cycle: u64\n\
         extern ground_truth: Str = \"\"\n",
    )
    .expect("compile_polydat_interpreter extern declaration");
    kernel.into_program()
}

/// Build a ParsedOp that carries a `relevancy:` block.
fn make_template_with_relevancy(k: u64) -> nmbrs_workload::model::ParsedOp {
    let mut template = nmbrs_workload::model::ParsedOp::simple(
        "select_ann",
        "SELECT key FROM t ORDER BY value ANN OF :v LIMIT :k",
    );
    template.params.insert(
        "relevancy".into(),
        serde_json::json!({
            "actual": "key",
            "expected": "{ground_truth}",
            "k": k,
            "functions": ["recall"],
        }),
    );
    template
}

/// Snapshot the mean of a named relevancy stat.
fn snapshot_mean(
    vm: &Arc<nmbrs_runtime::validation::ValidationMetrics>,
    name: &str,
) -> (usize, f64) {
    let stats = vm
        .relevancy_stats
        .get(name)
        .unwrap_or_else(|| panic!("missing relevancy stat '{name}'"));
    let snap = stats.snapshot();
    (snap.len(), snap.mean())
}

/// Build a wrapped `ValidatingDispenser` against the supplied inner
/// dispenser, returning the wrapper, its metrics handle, and the
/// sealed PullPlan. The plan must be resolved per-cycle against a
/// `PolydatState` that has had `ground_truth` injected via `set_input`.
fn wrap_with_relevancy(
    inner: Arc<dyn OpDispenser>,
    program: &Arc<PolydatProgram>,
    template: &nmbrs_workload::model::ParsedOp,
) -> (
    Arc<dyn OpDispenser>,
    Arc<nmbrs_runtime::validation::ValidationMetrics>,
    PullPlan,
) {
    let mut fx = ScopeFixture::new(program.clone());
    let labels = Labels::of("session", "test");
    let (validated, vm) =
        ValidatingDispenser::wrap(inner, template, &labels, Some(program.as_ref()), &mut fx)
            .expect("wrap should succeed");
    let plan = fx.seal();
    let vm = vm.expect("relevancy declared — metrics should be created");
    (validated, vm, plan)
}

/// Inject a string-form ground-truth value into the program's
/// `ground_truth` input slot, then resolve the plan to produce
/// `ResolvedPulls` for the cycle.
fn pulls_with_gt_string(
    program: &Arc<PolydatProgram>,
    plan: &PullPlan,
    gt_csv: &str,
) -> ResolvedPulls {
    let mut kernel = polydat::KernelProgram::create_kernel(program.clone());
    let idx = program
        .find_input("ground_truth")
        .expect("ground_truth input");
    kernel
        .set_input_at(idx, Value::Str(gt_csv.into()))
        .expect("ground_truth takes a string");
    plan.resolve(kernel.as_mut())
}

/// Build a real `PolydatKernel` with the `ground_truth` input populated.
/// Used to bind a `CycleWires` for `ExecCtx::with_wires` so the
/// validation wrapper's `ctx.wires.get("ground_truth")` read
/// resolves to the test fixture value. Returns the kernel by value;
/// the caller wraps `CycleWires` around it for the cycle's duration.
fn kernel_with_gt_string(gt_csv: &str) -> polydat::kernel::PolydatKernel {
    let mut k = compile_polydat_interpreter(
        "input cycle: u64\n\
         extern ground_truth: Str = \"\"\n",
    )
    .expect("compile_polydat_interpreter extern declaration");
    k.set_inputs(&[0]);
    let idx = k
        .program()
        .find_input("ground_truth")
        .expect("ground_truth input");
    k.state().set_input(idx, Value::Str(gt_csv.into()));
    k
}

#[tokio::test]
async fn perfect_recall_when_keys_match_ground_truth() {
    let ground_truth = [4_i64, 17, 42, 99, 123, 256, 512, 777, 1001, 2048];
    let returned_keys = ground_truth.to_vec();

    let program = make_gt_program();
    let template = make_template_with_relevancy(10);
    let inner: Arc<dyn OpDispenser> = Arc::new(FixedBodyDispenser {
        keys: returned_keys,
    });
    let (validated, vm, plan) = wrap_with_relevancy(inner, &program, &template);

    let gt_string = format!(
        "[{}]",
        ground_truth
            .iter()
            .map(i64::to_string)
            .collect::<Vec<_>>()
            .join(",")
    );
    let pulls = pulls_with_gt_string(&program, &plan, &gt_string);
    let fields = ResolvedFields::new(Vec::new(), Vec::new());
    let mut kernel = kernel_with_gt_string(&gt_string);
    let cw = CycleWires::new(&mut kernel);
    let ctx = ExecCtx::with_wires(&fields, &pulls, &cw);

    validated
        .execute(0, &ctx)
        .await
        .expect("validation should not error");

    let (count, mean) = snapshot_mean(&vm, "recall");
    assert_eq!(count, 1, "one recall score should be recorded");
    assert!(
        (mean - 1.0).abs() < 1e-9,
        "perfect-recall scenario should produce recall=1.0, got {mean}"
    );
}

#[tokio::test]
async fn zero_recall_when_returned_keys_do_not_match() {
    let ground_truth = [4_i64, 17, 42, 99, 123, 256, 512, 777, 1001, 2048];
    let returned_keys = vec![1_i64, 2, 3, 5, 6, 7, 8, 9, 10, 11];

    let program = make_gt_program();
    let template = make_template_with_relevancy(10);
    let inner: Arc<dyn OpDispenser> = Arc::new(FixedBodyDispenser {
        keys: returned_keys,
    });
    let (validated, vm, plan) = wrap_with_relevancy(inner, &program, &template);

    let gt_string = format!(
        "[{}]",
        ground_truth
            .iter()
            .map(i64::to_string)
            .collect::<Vec<_>>()
            .join(",")
    );
    let pulls = pulls_with_gt_string(&program, &plan, &gt_string);
    let fields = ResolvedFields::new(Vec::new(), Vec::new());
    let mut kernel = kernel_with_gt_string(&gt_string);
    let cw = CycleWires::new(&mut kernel);
    let ctx = ExecCtx::with_wires(&fields, &pulls, &cw);

    validated
        .execute(0, &ctx)
        .await
        .expect("validation should not error");

    let (count, mean) = snapshot_mean(&vm, "recall");
    assert_eq!(count, 1);
    assert_eq!(mean, 0.0, "no overlap → recall must be 0.0");
}

/// Returns a different body per cycle based on the `cycle` index.
struct MultiBodyDispenser {
    per_cycle_keys: Vec<Vec<i64>>,
}

impl OpDispenser for MultiBodyDispenser {
    fn execute<'a>(
        &'a self,
        cycle: u64,
        _ctx: &'a ExecCtx<'a>,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<OpResult, ExecutionError>> + Send + 'a>,
    > {
        let keys = self.per_cycle_keys[cycle as usize].clone();
        Box::pin(async move {
            Ok(OpResult {
                body: Some(rows_body(&keys)),
                skipped: false,
            })
        })
    }
}

#[tokio::test]
async fn averaged_recall_across_three_cycles() {
    // Cycle 0: 10/10 match → recall=1.0
    // Cycle 1: 5/10 match  → recall=0.5
    // Cycle 2: 0/10 match  → recall=0.0
    // Mean across the three = 0.5
    let gt = [4_i64, 17, 42, 99, 123, 256, 512, 777, 1001, 2048];
    let half = vec![4_i64, 17, 42, 99, 123, 0, 0, 0, 0, 0]; // 5 of GT
    let none = vec![1_i64, 2, 3, 5, 6, 7, 8, 9, 10, 11];

    let program = make_gt_program();
    let template = make_template_with_relevancy(10);
    let multi: Arc<dyn OpDispenser> = Arc::new(MultiBodyDispenser {
        per_cycle_keys: vec![gt.to_vec(), half, none],
    });
    let (validated, vm, plan) = wrap_with_relevancy(multi, &program, &template);

    let gt_string = format!(
        "[{}]",
        gt.iter().map(i64::to_string).collect::<Vec<_>>().join(",")
    );
    for cycle in 0u64..3 {
        let pulls = pulls_with_gt_string(&program, &plan, &gt_string);
        let fields = ResolvedFields::new(Vec::new(), Vec::new());
        let mut kernel = kernel_with_gt_string(&gt_string);
        let cw = CycleWires::new(&mut kernel);
        let ctx = ExecCtx::with_wires(&fields, &pulls, &cw);
        validated.execute(cycle, &ctx).await.expect("ok");
    }

    let (count, mean) = snapshot_mean(&vm, "recall");
    assert_eq!(count, 3, "three recall scores should be recorded");
    assert!(
        (mean - 0.5).abs() < 1e-9,
        "average of (1.0, 0.5, 0.0) should be 0.5, got {mean}"
    );
}

#[tokio::test]
async fn zero_recall_when_column_name_does_not_match() {
    // Regression guard for a failure mode observed in the CQL workload:
    // the adapter's `CqlResultBody::from_cass_result` falls back to `"?"`
    // when `column_name(idx)` errors, so rows come back as `{"?": "42"}`
    // instead of `{"key": "42"}`. The relevancy config asks for `actual:
    // key`, so no indices extract, intersection is empty, recall=0 on
    // every cycle — matching the pathological output.md pattern.
    #[derive(Debug)]
    struct WrongColumnBody {
        values: Vec<i64>,
    }
    impl ResultBody for WrongColumnBody {
        fn to_json(&self) -> serde_json::Value {
            serde_json::Value::Array(
                self.values
                    .iter()
                    .map(|v| {
                        let mut row = serde_json::Map::new();
                        row.insert("?".into(), serde_json::Value::String(v.to_string()));
                        serde_json::Value::Object(row)
                    })
                    .collect(),
            )
        }
        fn as_any(&self) -> &dyn Any {
            self
        }
        fn element_count(&self) -> u64 {
            self.values.len() as u64
        }
    }
    struct WrongColumnDispenser;
    impl OpDispenser for WrongColumnDispenser {
        fn execute<'a>(
            &'a self,
            _cycle: u64,
            _ctx: &'a ExecCtx<'a>,
        ) -> std::pin::Pin<
            Box<dyn std::future::Future<Output = Result<OpResult, ExecutionError>> + Send + 'a>,
        > {
            Box::pin(async move {
                Ok(OpResult {
                    body: Some(Box::new(WrongColumnBody {
                        values: vec![1, 2, 3, 4],
                    })),
                    skipped: false,
                })
            })
        }
    }

    let program = make_gt_program();
    let template = make_template_with_relevancy(4);
    let inner: Arc<dyn OpDispenser> = Arc::new(WrongColumnDispenser);
    let (validated, vm, plan) = wrap_with_relevancy(inner, &program, &template);

    let pulls = pulls_with_gt_string(&program, &plan, "[1,2,3,4]");
    let fields = ResolvedFields::new(Vec::new(), Vec::new());
    let mut kernel = kernel_with_gt_string("[1,2,3,4]");
    let cw = CycleWires::new(&mut kernel);
    let ctx = ExecCtx::with_wires(&fields, &pulls, &cw);

    validated.execute(0, &ctx).await.expect("ok");

    let (_, mean) = snapshot_mean(&vm, "recall");
    assert_eq!(
        mean, 0.0,
        "mismatched column name must produce recall=0 (reproduces the bug)"
    );
}

#[tokio::test]
async fn zero_recall_when_body_is_empty() {
    // A query that returns no rows (body=None): recall must be 0.0
    // and the pipeline should not crash or emit misleading scores.
    struct NoBodyDispenser;
    impl OpDispenser for NoBodyDispenser {
        fn execute<'a>(
            &'a self,
            _cycle: u64,
            _ctx: &'a ExecCtx<'a>,
        ) -> std::pin::Pin<
            Box<dyn std::future::Future<Output = Result<OpResult, ExecutionError>> + Send + 'a>,
        > {
            Box::pin(async move {
                Ok(OpResult {
                    body: None,
                    skipped: false,
                })
            })
        }
    }

    let program = make_gt_program();
    let template = make_template_with_relevancy(10);
    let inner: Arc<dyn OpDispenser> = Arc::new(NoBodyDispenser);
    let (validated, vm, plan) = wrap_with_relevancy(inner, &program, &template);

    let pulls = pulls_with_gt_string(&program, &plan, "[1,2,3]");
    let fields = ResolvedFields::new(Vec::new(), Vec::new());
    let mut kernel = kernel_with_gt_string("[1,2,3]");
    let cw = CycleWires::new(&mut kernel);
    let ctx = ExecCtx::with_wires(&fields, &pulls, &cw);

    validated.execute(0, &ctx).await.expect("ok");

    let (count, mean) = snapshot_mean(&vm, "recall");
    assert_eq!(count, 1, "empty-body should still record a score");
    assert_eq!(mean, 0.0, "empty body → recall=0.0");
}