haematite-cli 0.21.8

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use std::collections::BTreeMap;
use std::error::Error;
use std::num::NonZeroU64;

use haematite_query::page::{Allowance, Origin, Resume};
use serde_json::value::RawValue;
use serde_json::{Value, json};

use super::{Binding, Continuation, Engine, Options, Rows, Seal};
use crate::serve::contracts::Caller;
use crate::serve::protocol::Version;

type TestResult = Result<(), Box<dyn Error>>;

fn raw(value: &str) -> Result<Box<RawValue>, serde_json::Error> {
    RawValue::from_string(value.to_owned())
}

#[derive(Clone)]
struct Request {
    query: String,
    parameters: BTreeMap<String, Box<RawValue>>,
    engine: Engine,
    caller: Caller,
    allowance: Allowance,
}

impl Request {
    fn new() -> Result<Self, Box<dyn Error>> {
        Ok(Self {
            query: ".private_original_question".to_owned(),
            parameters: BTreeMap::from([("threshold".to_owned(), raw(r#"{"a":1,"b":[2,3]}"#)?)]),
            engine: Engine {
                protocol: 1,
                worker: "0.1.0".to_owned(),
                engine: "jaq".to_owned(),
                jaq_core: "3.1.0".to_owned(),
                jaq_std: "3.0.1".to_owned(),
                jaq_json: "2.0.2".to_owned(),
            },
            caller: Caller::new(
                "local",
                "private-original-person",
                Some("responsible-person".to_owned()),
            )?,
            allowance: Allowance {
                rows: NonZeroU64::new(10).ok_or("zero rows")?,
                bytes: NonZeroU64::new(1000).ok_or("zero bytes")?,
                scan_records: 100,
            },
        })
    }

    fn binding(&self) -> Result<Binding, crate::serve::storage::Failure> {
        Binding::new(
            &self.query,
            &self.parameters,
            &self.engine,
            &self.caller,
            &self.allowance,
            Rows::Values,
        )
    }
}

fn continuation(request: &Request) -> Result<Continuation, Box<dyn Error>> {
    Ok(Continuation::new(
        "main".to_owned(),
        Version {
            generation: 7,
            sequence: 11,
        },
        Resume {
            records_read: 3,
            pending: Origin {
                collection: "charts".to_owned(),
                id: "chart-3".to_owned(),
                version: raw(r#"{"generation":"7","sequence":"11"}"#)?,
                score: Some(0.75),
            },
            discard_outputs: 2,
            passed: false,
            passed_counts: Vec::new(),
        },
        raw("1700000000.125")?,
        request.binding()?,
    )?)
}

#[test]
fn each_bound_kind_changed_alone_is_named_without_private_values() -> TestResult {
    let original = Request::new()?;
    let token = continuation(&original)?;
    for kind in ["query", "parameters", "engine", "caller", "allowances"] {
        let mut changed = original.clone();
        match kind {
            "query" => changed.query = ".other_question".to_owned(),
            "parameters" => {
                changed.parameters.insert("threshold".to_owned(), raw("4")?);
            }
            "engine" => changed.engine.engine = "different-engine".to_owned(),
            "caller" => changed.caller = Caller::new("local", "other-person", None)?,
            "allowances" => changed.allowance.rows = NonZeroU64::new(11).ok_or("zero rows")?,
            other => return Err(format!("unknown test kind {other}").into()),
        }
        let error = token
            .check_binding(&changed.binding()?)
            .err()
            .ok_or("changed binding accepted")?;
        assert_eq!(error.code, format!("query_continuation_{kind}"));
        assert!(error.message.contains(kind));
        assert!(!error.message.contains("private-original-person"));
        assert!(!error.message.contains("private_original_question"));
    }
    Ok(())
}

#[test]
fn each_engine_version_caller_member_and_allowance_is_bound() -> TestResult {
    let original = Request::new()?;
    let token = continuation(&original)?;
    for member in [
        "protocol",
        "worker",
        "jaq_core",
        "jaq_std",
        "jaq_json",
        "issuer",
        "subject",
        "responsible",
        "bytes",
        "scan",
    ] {
        let mut changed = original.clone();
        let kind = match member {
            "protocol" => {
                changed.engine.protocol += 1;
                "engine"
            }
            "worker" => {
                changed.engine.worker.push('x');
                "engine"
            }
            "jaq_core" => {
                changed.engine.jaq_core.push('x');
                "engine"
            }
            "jaq_std" => {
                changed.engine.jaq_std.push('x');
                "engine"
            }
            "jaq_json" => {
                changed.engine.jaq_json.push('x');
                "engine"
            }
            "issuer" => {
                changed.caller = Caller::new(
                    "other-issuer",
                    original.caller.subject(),
                    original.caller.responsible().map(str::to_owned),
                )?;
                "caller"
            }
            "subject" => {
                changed.caller = Caller::new(
                    original.caller.issuer(),
                    "other-subject",
                    original.caller.responsible().map(str::to_owned),
                )?;
                "caller"
            }
            "responsible" => {
                changed.caller =
                    Caller::new(original.caller.issuer(), original.caller.subject(), None)?;
                "caller"
            }
            "bytes" => {
                changed.allowance.bytes = NonZeroU64::new(1001).ok_or("zero bytes")?;
                "allowances"
            }
            "scan" => {
                changed.allowance.scan_records += 1;
                "allowances"
            }
            other => return Err(format!("unknown test member {other}").into()),
        };
        let error = token
            .check_binding(&changed.binding()?)
            .err()
            .ok_or("unbound member")?;
        assert_eq!(error.code, format!("query_continuation_{kind}"));
    }
    Ok(())
}

#[test]
fn the_row_form_is_bound_with_the_allowances() -> TestResult {
    let request = Request::new()?;
    let origins = Binding::new(
        &request.query,
        &request.parameters,
        &request.engine,
        &request.caller,
        &request.allowance,
        Rows::Origins,
    )?;
    let error = continuation(&request)?
        .check_binding(&origins)
        .err()
        .ok_or("a values token was taken for origins")?;
    assert_eq!(error.code, "query_continuation_allowances");
    Ok(())
}

#[test]
fn parameter_object_order_and_whitespace_do_not_change_the_binding() -> TestResult {
    let original = Request::new()?;
    let mut reordered = original.clone();
    reordered
        .parameters
        .insert("threshold".to_owned(), raw(r#"{ "b": [2, 3], "a": 1 }"#)?);
    continuation(&original)?.check_binding(&reordered.binding()?)?;
    Ok(())
}

#[test]
fn continuation_decode_and_encode_are_byte_identical_and_keep_replay_state() -> TestResult {
    let request = Request::new()?;
    let seal = Seal::new()?;
    let encoded = continuation(&request)?.encode(&seal)?;
    let decoded = Continuation::decode(&encoded, &seal)?;
    decoded.check_binding(&request.binding()?)?;
    assert_eq!(decoded.plain()?, continuation(&request)?.plain()?);
    assert_eq!(decoded.branch, "main");
    assert_eq!(
        decoded.version,
        Version {
            generation: 7,
            sequence: 11
        }
    );
    assert_eq!(decoded.resume.records_read, 3);
    assert_eq!(decoded.resume.discard_outputs, 2);
    assert_eq!(decoded.resume.pending.id, "chart-3");
    assert_eq!(decoded.now.get(), "1700000000.125");
    Ok(())
}

#[test]
fn a_later_request_checks_branch_and_only_an_explicit_version() -> TestResult {
    let token = continuation(&Request::new()?)?;
    token.check_moment("main", None)?;
    token.check_moment(
        "main",
        Some(Version {
            generation: 7,
            sequence: 11,
        }),
    )?;
    for (branch, version) in [
        ("other-branch", None),
        (
            "main",
            Some(Version {
                generation: 8,
                sequence: 11,
            }),
        ),
        (
            "main",
            Some(Version {
                generation: 7,
                sequence: 12,
            }),
        ),
    ] {
        let error = token
            .check_moment(branch, version)
            .err()
            .ok_or("different moment accepted")?;
        assert_eq!(error.code, "query_continuation_moment");
        assert!(!error.message.contains("other-branch"));
    }
    assert_eq!(token.now.get(), "1700000000.125");
    Ok(())
}

#[test]
fn truncated_padded_and_noncanonical_tokens_are_refused() -> TestResult {
    let seal = Seal::new()?;
    let encoded = continuation(&Request::new()?)?.encode(&seal)?;
    let bytes = seal.open(&encoded)?;
    let mut padded = bytes.clone();
    padded.push(b' ');
    let mut trailing = bytes;
    trailing.extend_from_slice(b"{}");
    for invalid in [
        encoded
            .get(..encoded.len() - 2)
            .ok_or("encoded token too short")?
            .to_owned(),
        format!(" {encoded}"),
        encoded.to_uppercase(),
        seal.seal(&padded)?,
        seal.seal(&trailing)?,
    ] {
        assert!(Continuation::decode(&invalid, &seal).is_err());
    }
    Ok(())
}

/// `encoded` opened, one field replaced, and sealed again by the same key.
fn replace_token(
    seal: &Seal,
    encoded: &str,
    from: &str,
    to: &str,
) -> Result<String, Box<dyn Error>> {
    let bytes = String::from_utf8(seal.open(encoded)?)?;
    if bytes.matches(from).count() != 1 {
        return Err("token replacement did not name exactly one field".into());
    }
    Ok(seal.seal(bytes.replacen(from, to, 1).as_bytes())?)
}

#[test]
fn unknown_format_zero_scan_count_and_overflowing_score_are_refused() -> TestResult {
    let seal = Seal::new()?;
    let encoded = continuation(&Request::new()?)?.encode(&seal)?;
    let format = replace_token(&seal, &encoded, "\"format\":1", "\"format\":2")?;
    let error = Continuation::decode(&format, &seal)
        .err()
        .ok_or("unknown format accepted")?;
    assert_eq!(error.code, "query_continuation_format");
    let zero = replace_token(&seal, &encoded, "\"records_read\":3", "\"records_read\":0")?;
    assert!(Continuation::decode(&zero, &seal).is_err());
    let overflow = replace_token(&seal, &encoded, "\"score\":0.75", "\"score\":1e400")?;
    assert!(Continuation::decode(&overflow, &seal).is_err());
    let mut invalid = continuation(&Request::new()?)?;
    invalid.resume.pending.score = Some(f64::NAN);
    assert!(invalid.encode(&seal).is_err());
    Ok(())
}

/// The fixture's continuation, its pending record moved to patients/p2.
fn pending_p2(request: &Request) -> Result<Continuation, Box<dyn Error>> {
    let mut token = continuation(request)?;
    token.resume.pending.collection = "patients".to_owned();
    token.resume.pending.id = "p2".to_owned();
    Ok(token)
}

#[test]
fn a_sealed_token_names_no_collection_id_or_field_of_its_record() -> TestResult {
    let token = pending_p2(&Request::new()?)?;
    let encoded = token.encode(&Seal::new()?)?;
    let plain = token.plain()?;
    for hidden in [
        &b"patients"[..],
        &b"\"p2\""[..],
        &b"\"id\":\"p2\""[..],
        &b"\"pending\""[..],
        &plain[..],
    ] {
        let written = super::codec::encode(hidden);
        assert!(
            !encoded.contains(&written),
            "{written} is readable in {encoded}"
        );
    }
    Ok(())
}

#[test]
fn one_changed_hex_digit_anywhere_is_refused_as_invalid() -> TestResult {
    let seal = Seal::new()?;
    let encoded = pending_p2(&Request::new()?)?.encode(&seal)?;
    for (index, digit) in encoded.char_indices() {
        let other = if digit == '0' { "1" } else { "0" };
        let mut changed = encoded.clone();
        changed.replace_range(index..=index, other);
        let error = Continuation::decode(&changed, &seal)
            .err()
            .ok_or_else(|| format!("a change at hex digit {index} was accepted"))?;
        assert_eq!(error.code, "query_continuation_invalid");
    }
    Ok(())
}

#[test]
fn a_token_sealed_by_another_key_is_refused_as_invalid() -> TestResult {
    let encoded = pending_p2(&Request::new()?)?.encode(&Seal::new()?)?;
    let error = Continuation::decode(&encoded, &Seal::new()?)
        .err()
        .ok_or("another key opened the token")?;
    assert_eq!(error.code, "query_continuation_invalid");
    assert!(error.message.contains("restarted"), "{}", error.message);
    Ok(())
}

#[test]
fn two_tokens_for_one_position_differ_and_open_alike() -> TestResult {
    let seal = Seal::new()?;
    let token = pending_p2(&Request::new()?)?;
    let first = token.encode(&seal)?;
    let second = token.encode(&seal)?;
    assert_ne!(first, second);
    let first = Continuation::decode(&first, &seal)?.plain()?;
    let second = Continuation::decode(&second, &seal)?.plain()?;
    assert_eq!(first, second);
    Ok(())
}

#[test]
fn large_parameter_numbers_and_array_order_remain_distinct() -> TestResult {
    for (first, second) in [
        ("18446744073709551617", "18446744073709551618"),
        ("[2,3]", "[3,2]"),
    ] {
        let mut original = Request::new()?;
        original
            .parameters
            .insert("threshold".to_owned(), raw(first)?);
        let mut changed = original.clone();
        changed
            .parameters
            .insert("threshold".to_owned(), raw(second)?);
        let error = continuation(&original)?
            .check_binding(&changed.binding()?)
            .err()
            .ok_or("different parameter values collided")?;
        assert_eq!(error.code, "query_continuation_parameters");
    }
    Ok(())
}

#[test]
fn a_mismatched_aggregate_digest_and_unknown_fields_are_refused() -> TestResult {
    let seal = Seal::new()?;
    let encoded = continuation(&Request::new()?)?.encode(&seal)?;
    let value: Value = serde_json::from_slice(&seal.open(&encoded)?)?;
    let aggregate = value["binding"]["aggregate"]
        .as_str()
        .ok_or("aggregate missing")?;
    let replacement = if aggregate.starts_with('0') {
        "1".repeat(64)
    } else {
        "0".repeat(64)
    };
    let invalid = replace_token(
        &seal,
        &encoded,
        &format!("\"aggregate\":\"{aggregate}\""),
        &format!("\"aggregate\":\"{replacement}\""),
    )?;
    assert!(Continuation::decode(&invalid, &seal).is_err());
    let unknown = replace_token(
        &seal,
        &encoded,
        "\"format\":1",
        "\"format\":1,\"unexpected\":true",
    )?;
    assert!(Continuation::decode(&unknown, &seal).is_err());
    Ok(())
}

fn settings_value() -> Value {
    json!({
        "query_time_ms": 1000,
        "query_memory_bytes": 64_000_000,
        "query_outputs_per_record": 32,
        "query_scan_records": 100,
        "query_max_running": 2,
        "query_worker": "/worker/haematite-jq"
    })
}

#[test]
fn query_settings_have_all_present_and_all_absent_states() -> TestResult {
    let absent: Options = serde_json::from_value(json!({}))?;
    assert!(absent.resolve()?.is_none());
    let present: Options = serde_json::from_value(settings_value())?;
    let settings = present.resolve()?.ok_or("settings lost")?;
    assert_eq!(settings.time_ms.get(), 1000);
    assert_eq!(settings.memory_bytes.get(), 64_000_000);
    assert_eq!(settings.outputs_per_record.get(), 32);
    assert_eq!(settings.scan_records.get(), 100);
    assert_eq!(settings.max_running.get(), 2);
    assert_eq!(settings.worker.as_os_str(), "/worker/haematite-jq");
    Ok(())
}

#[test]
fn each_missing_query_setting_refuses_and_names_the_missing_one() -> TestResult {
    for name in super::settings::NAMES {
        let mut value = settings_value();
        value
            .as_object_mut()
            .ok_or("settings not object")?
            .remove(name);
        let options: Options = serde_json::from_value(value)?;
        let error = options.resolve().err().ok_or("mixed settings accepted")?;
        assert!(error.contains(name), "missing {name}: {error}");
    }
    let options: Options = serde_json::from_value(json!({"query_time_ms": 1000}))?;
    let error = options.resolve().err().ok_or("single setting accepted")?;
    for name in &super::settings::NAMES[1..] {
        assert!(error.contains(*name));
    }
    Ok(())
}

#[test]
fn a_records_read_limit_below_two_is_refused_by_name() -> TestResult {
    let mut one = settings_value();
    one["query_scan_records"] = json!(1);
    let options: Options = serde_json::from_value(one)?;
    let error = options.resolve().err().ok_or("a limit of one accepted")?;
    assert!(error.contains("query_scan_records"), "{error}");
    assert!(error.contains("at least 2"), "{error}");
    let mut two = settings_value();
    two["query_scan_records"] = json!(2);
    let options: Options = serde_json::from_value(two)?;
    let settings = options.resolve()?.ok_or("no settings")?;
    assert_eq!(settings.scan_records.get(), 2);
    Ok(())
}

/// A page that read only records the caller may not open stops at a place
/// having given none: its token carries no records read and still opens.
#[test]
fn a_passed_place_with_no_records_read_seals_and_opens() -> TestResult {
    let seal = Seal::new()?;
    let mut token = continuation(&Request::new()?)?;
    token.resume.records_read = 0;
    token.resume.discard_outputs = 0;
    token.resume.passed = true;
    let opened = Continuation::decode(&token.encode(&seal)?, &seal)?;
    assert!(opened.resume.passed);
    assert_eq!(opened.resume.records_read, 0);
    token.resume.passed = false;
    assert!(
        token.encode(&seal).is_err(),
        "a pending record read by no one"
    );
    Ok(())
}

#[test]
fn zero_query_settings_relative_paths_and_explicit_null_are_refused() -> TestResult {
    for name in &super::settings::NAMES[..5] {
        let mut value = settings_value();
        value[*name] = json!(0);
        let options: Options = serde_json::from_value(value)?;
        assert!(
            options
                .resolve()
                .err()
                .ok_or("zero accepted")?
                .contains(*name)
        );
    }
    let mut relative = settings_value();
    relative["query_worker"] = json!("haematite-jq");
    let options: Options = serde_json::from_value(relative)?;
    assert!(
        options
            .resolve()
            .err()
            .ok_or("relative path accepted")?
            .contains("query_worker")
    );
    let error = serde_json::from_value::<Options>(json!({"query_time_ms": null}))
        .err()
        .ok_or("explicit null accepted")?;
    assert!(error.to_string().contains("query_time_ms"));
    Ok(())
}

#[cfg(unix)]
#[test]
fn mixed_settings_refuse_service_start_before_other_start_checks() -> TestResult {
    let directory = tempfile::tempdir()?;
    let socket = directory.path().join("service.sock");
    let path = directory.path().join("serve.json");
    // A later path refusal prevents an absent settings check from running a listener.
    let value = json!({
        "data_dir": "relative-database", "socket": socket,
        "branch_cache_bytes_per_shard": 1024,
        "max_frame": 8192, "max_clients": 2, "max_page": 10, "timeout_ms": 1000,
        "query_time_ms": 1000,
        "create": {"shards": 1, "cache_bytes_total": 1024, "executor_threads": 1}
    });
    std::fs::write(&path, serde_json::to_vec(&value)?)?;
    let args = [
        "--config".to_owned(),
        path.to_string_lossy().into_owned(),
        "--create".to_owned(),
    ];
    let error = crate::serve::cli::run(args.into_iter())
        .err()
        .ok_or("mixed settings started service")?;
    for name in &super::settings::NAMES[1..] {
        assert!(error.contains(*name));
    }
    assert!(!socket.exists());
    Ok(())
}