lemma-engine 0.10.2

A pure, declarative language for business rules.
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
use serde_json::Value;

use crate::documentation::{GuideTopic, EVALUATE_GUIDE};
use crate::engine::{resolve_effective as resolve_effective_datetime, Engine};
use crate::evaluation::explanations::format_explanation;
use crate::evaluation::response::Response;
use crate::mcp::error::ToolError;
use crate::parse_evaluate_data_object;
use crate::parsing::ast::DateTimeValue;
use crate::parsing::source::SourceType;
use crate::resolve_evaluate_rules;
use crate::spec_set_id::parse_spec_set_id;

/// Evaluate a spec. Always explains (`Engine::evaluate(..., true)`). No `explain` arg.
/// Success text is ASCII explanation trees plus a Missing data block when unbound.
pub fn evaluate(engine: &Engine, args: &Value) -> Result<String, ToolError> {
    require_object(args)?;
    reject_explain_arg(args)?;
    if args.get("rule").is_some() {
        return Err(ToolError::invalid_arguments(
            "Unknown field 'rule'. Use 'rules' (string or string array).",
        ));
    }

    let spec_set_id = required_string(args, "spec")?;
    if spec_set_id.is_empty() {
        return Err(ToolError::invalid_arguments("Spec set id cannot be empty"));
    }
    let spec_name = parse_spec_set_id(spec_set_id).map_err(engine_error_to_diagnostics)?;
    let repository = optional_nonempty_string(args, "repository")?;
    let now = resolve_effective(args)?;
    let data_values = parse_evaluate_data_object(&args.get("data").cloned())
        .map_err(|e| ToolError::invalid_arguments(e.to_string()))?;
    let rule_names = resolve_evaluate_rules(&args.get("rules").cloned())
        .map_err(|e| ToolError::invalid_arguments(e.to_string()))?;
    let rules = rule_names.as_deref();

    let response = engine
        .evaluate(repository, &spec_name, Some(&now), data_values, rules, true)
        .map_err(engine_error_to_diagnostics)?;

    Ok(format_evaluate_text(&response))
}

/// Deprecated alias of [`evaluate`]. Same args and formatted trees.
pub fn run(engine: &Engine, args: &Value) -> Result<String, ToolError> {
    evaluate(engine, args)
}

fn format_evaluate_text(response: &Response) -> String {
    let mut output = String::new();
    let missing: Vec<&str> = response
        .results
        .values()
        .filter(|result| result.is_missing_data())
        .flat_map(|result| result.missing_data().iter().map(String::as_str))
        .collect();
    if !missing.is_empty() {
        output.push_str("Missing data\n");
        for key in &missing {
            output.push_str("  ");
            output.push_str(key);
            output.push('\n');
        }
        output.push('\n');
    }
    let mut first = true;
    for result in response.results.values() {
        if !first {
            output.push('\n');
        }
        first = false;
        let explanation = result
            .explanation
            .as_ref()
            .expect("BUG: MCP evaluate always explains");
        output.push_str(&format_explanation(explanation));
        output.push('\n');
    }
    output
}

pub fn specs(engine: &Engine, args: &Value) -> Result<String, ToolError> {
    require_object(args)?;
    let text = optional_bool(args, "text")?;
    let all = optional_bool(args, "all")?;
    let mut repositories = engine.specs();
    if text {
        return Ok(crate::format_repositories(&repositories, all));
    }
    if !all {
        repositories.retain(|repository| !repository.from_dependency);
    }
    Ok(serde_json::to_string_pretty(&repositories)
        .unwrap_or_else(|error| panic!("BUG: engine specs must serialize: {error}")))
}

pub fn show(engine: &Engine, args: &Value) -> Result<String, ToolError> {
    let repository = optional_nonempty_string(args, "repository")?;
    let text = optional_bool(args, "text")?;
    let spec_set_id = required_string(args, "spec")?;
    if spec_set_id.is_empty() {
        return Err(ToolError::invalid_arguments("Spec set id cannot be empty"));
    }
    let spec_name = parse_spec_set_id(spec_set_id).map_err(engine_error_to_diagnostics)?;
    let now = resolve_effective(args)?;
    let show = engine
        .show(repository, &spec_name, Some(&now))
        .map_err(engine_error_to_diagnostics)?;
    if text {
        return Ok(format!("{show}\n"));
    }
    Ok(
        serde_json::to_string_pretty(&crate::api::show_document(engine, &show))
            .unwrap_or_else(|error| panic!("BUG: show response must serialize: {error}")),
    )
}

pub fn source(engine: &Engine, args: &Value) -> Result<String, ToolError> {
    require_object(args)?;
    let repository = optional_nonempty_string(args, "repository")?;
    let spec = optional_nonempty_string(args, "spec")?;
    match (repository, spec) {
        (Some(repo), None) => engine
            .source(Some(repo), None, None)
            .map_err(engine_error_to_diagnostics),
        (repo, Some(spec_set_id)) => {
            let spec_name = parse_spec_set_id(spec_set_id).map_err(engine_error_to_diagnostics)?;
            let now = resolve_effective(args)?;
            engine
                .source(repo, Some(&spec_name), Some(&now))
                .map_err(engine_error_to_diagnostics)
        }
        (None, None) => Err(ToolError::invalid_arguments(
            "Missing 'spec' or 'repository' field",
        )),
    }
}

pub fn check(args: &Value) -> Result<String, ToolError> {
    let text = optional_bool(args, "text")?;
    let sources_value = args
        .get("sources")
        .ok_or_else(|| ToolError::invalid_arguments("Missing 'sources' array field"))?;
    let sources_arr = sources_value
        .as_array()
        .ok_or_else(|| ToolError::invalid_arguments("Missing 'sources' array field"))?;
    if sources_arr.is_empty() {
        return Err(ToolError::invalid_arguments(
            "'sources' must be a non-empty array of [label, code] pairs",
        ));
    }

    let mut sources: Vec<(SourceType, String)> = Vec::with_capacity(sources_arr.len());
    for (i, entry) in sources_arr.iter().enumerate() {
        let pair = entry.as_array().ok_or_else(|| {
            ToolError::invalid_arguments(format!("sources[{i}] must be a [label, code] array"))
        })?;
        if pair.len() != 2 {
            return Err(ToolError::invalid_arguments(format!(
                "sources[{i}] must have exactly 2 elements [label, code]"
            )));
        }
        let label = pair[0].as_str().ok_or_else(|| {
            ToolError::invalid_arguments(format!("sources[{i}][0] (label) must be a string"))
        })?;
        let code = pair[1].as_str().ok_or_else(|| {
            ToolError::invalid_arguments(format!("sources[{i}][1] (code) must be a string"))
        })?;
        let source_type =
            SourceType::from_binding_label(label).map_err(ToolError::invalid_arguments)?;
        sources.push((source_type, code.to_string()));
    }

    let mut engine = Engine::new();
    if let Err(load_err) = engine.load(sources) {
        return Err(ToolError::diagnostics(&load_err.errors));
    }

    let recommendations = engine.quality();
    if text {
        return Ok(format_recommendations_text(&recommendations));
    }
    Ok(serde_json::to_string_pretty(&recommendations)
        .unwrap_or_else(|error| panic!("BUG: quality recommendations must serialize: {error}")))
}

fn format_recommendations_text(recommendations: &[crate::Recommendation]) -> String {
    if recommendations.is_empty() {
        return "No recommendations.\n".to_string();
    }
    let mut output = String::new();
    for recommendation in recommendations {
        output.push_str(&recommendation.to_string());
        output.push('\n');
    }
    output
}

pub fn guide(args: &Value) -> Result<String, ToolError> {
    require_object(args)?;
    match args.get("topic") {
        None => Ok(EVALUATE_GUIDE.to_string()),
        Some(value) => {
            let topic_name = value
                .as_str()
                .ok_or_else(|| ToolError::invalid_arguments("topic must be a string"))?;
            let topic = GuideTopic::parse(topic_name).ok_or_else(|| {
                ToolError::invalid_arguments(format!(
                    "Unknown guide topic '{topic_name}'. Valid: {}",
                    GuideTopic::VALID_LIST
                ))
            })?;
            Ok(topic.section_text().to_string())
        }
    }
}

fn engine_error_to_diagnostics(error: crate::Error) -> ToolError {
    ToolError::diagnostics(std::slice::from_ref(&error))
}

fn reject_explain_arg(args: &Value) -> Result<(), ToolError> {
    if args.get("explain").is_some() {
        return Err(ToolError::invalid_arguments(
            "MCP run always includes explanations; do not pass 'explain'",
        ));
    }
    Ok(())
}

fn require_object(args: &Value) -> Result<(), ToolError> {
    if args.is_object() || args.is_null() {
        Ok(())
    } else {
        Err(ToolError::invalid_arguments("arguments must be an object"))
    }
}

fn required_string<'a>(args: &'a Value, field: &str) -> Result<&'a str, ToolError> {
    match args.get(field) {
        Some(Value::String(value)) => Ok(value.trim()),
        Some(_) => Err(ToolError::invalid_arguments(format!(
            "'{field}' must be a string"
        ))),
        None => Err(ToolError::invalid_arguments(format!(
            "Missing '{field}' field"
        ))),
    }
}

fn optional_nonempty_string<'a>(
    args: &'a Value,
    field: &str,
) -> Result<Option<&'a str>, ToolError> {
    match args.get(field) {
        None => Ok(None),
        Some(Value::Null) => Ok(None),
        Some(Value::String(value)) => {
            let trimmed = value.trim();
            if trimmed.is_empty() {
                Ok(None)
            } else {
                Ok(Some(trimmed))
            }
        }
        Some(_) => Err(ToolError::invalid_arguments(format!(
            "'{field}' must be a string"
        ))),
    }
}

fn optional_bool(args: &Value, field: &str) -> Result<bool, ToolError> {
    match args.get(field) {
        None | Some(Value::Null) => Ok(false),
        Some(Value::Bool(value)) => Ok(*value),
        Some(_) => Err(ToolError::invalid_arguments(format!(
            "'{field}' must be a boolean"
        ))),
    }
}

fn resolve_effective(args: &Value) -> Result<DateTimeValue, ToolError> {
    match args.get("effective") {
        None | Some(Value::Null) => {
            resolve_effective_datetime(None).map_err(engine_error_to_diagnostics)
        }
        Some(Value::String(raw)) => {
            resolve_effective_datetime(Some(raw)).map_err(engine_error_to_diagnostics)
        }
        Some(_) => Err(ToolError::invalid_arguments("'effective' must be a string")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::sync::Arc;

    fn load_pricing() -> Engine {
        let mut engine = Engine::new();
        engine
            .load([(
                SourceType::Path(Arc::new(PathBuf::from("pricing.lemma"))),
                "spec pricing\ndata quantity: number\nrule total: quantity * 10\n".to_string(),
            )])
            .expect("load");
        engine
    }

    #[test]
    fn evaluate_returns_formatted_explanation_tree() {
        let engine = load_pricing();
        let text = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "rules": "total",
                "data": { "quantity": "3" }
            }),
        )
        .expect("evaluate");
        assert!(
            text.contains("total: 30"),
            "expected formatted tree with total: 30, got: {text}"
        );
        assert!(
            text.contains("└─") || text.contains("quantity"),
            "expected tree connector or quantity in body, got: {text}"
        );
    }

    #[test]
    fn evaluate_rejects_json_number_data() {
        let engine = load_pricing();
        let err = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "data": { "quantity": 3 }
            }),
        )
        .expect_err("json number");
        match err {
            ToolError::InvalidArguments(message) => {
                assert!(message.contains("pass as string: \"3\""), "{message}");
            }
            other => panic!("expected InvalidArguments, got {other}"),
        }
    }

    #[test]
    fn evaluate_rejects_explain_arg() {
        let engine = load_pricing();
        let err = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "explain": false
            }),
        )
        .expect_err("explain forbidden");
        assert!(matches!(err, ToolError::InvalidArguments(_)));
    }

    #[test]
    fn evaluate_rejects_legacy_rule_field() {
        let engine = load_pricing();
        let err = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "rule": "total"
            }),
        )
        .expect_err("rule forbidden");
        assert!(matches!(err, ToolError::InvalidArguments(_)));
    }

    #[test]
    fn evaluate_rules_array() {
        let engine = load_pricing();
        let text = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "rules": ["total"],
                "data": { "quantity": "2" }
            }),
        )
        .expect("evaluate");
        assert!(
            text.contains("total: 20"),
            "expected formatted tree with total: 20, got: {text}"
        );
    }

    #[test]
    fn show_accepts_repository() {
        let engine = Engine::new();
        let text = show(
            &engine,
            &serde_json::json!({
                "repository": "lemma",
                "spec": "units"
            }),
        )
        .expect("show lemma units");
        let value: Value = serde_json::from_str(&text).expect("Show JSON");
        assert_eq!(value["title"], "units");
        assert_eq!(value["x-lemma-repository"], "lemma");
    }

    #[test]
    fn missing_spec_is_diagnostics() {
        let engine = Engine::new();
        let err =
            run(&engine, &serde_json::json!({ "spec": "nonexistent" })).expect_err("missing spec");
        match err {
            ToolError::Diagnostics(text) => {
                let value: Value = serde_json::from_str(&text).expect("EngineError JSON");
                assert!(value.is_array());
                assert!(!value.as_array().expect("array").is_empty());
            }
            other => panic!("expected Diagnostics, got {other}"),
        }
    }

    #[test]
    fn evaluate_aliases_run() {
        let engine = load_pricing();
        let a = run(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "data": { "quantity": "1" }
            }),
        )
        .expect("evaluate");
        let b = evaluate(
            &engine,
            &serde_json::json!({
                "spec": "pricing",
                "data": { "quantity": "1" }
            }),
        )
        .expect("evaluate");
        assert_eq!(a, b);
    }

    #[test]
    fn check_success_is_quality_json() {
        let text = check(&serde_json::json!({
            "sources": [["ok.lemma", "spec ok\nrule r: 1\n"]]
        }))
        .expect("check");
        let value: Value = serde_json::from_str(&text).expect("quality JSON");
        assert!(value.is_array());
    }

    fn assert_text_must_be_boolean(result: Result<String, ToolError>) {
        match result.expect_err("non-boolean text") {
            ToolError::InvalidArguments(message) => {
                assert_eq!(message, "'text' must be a boolean")
            }
            other => panic!("expected InvalidArguments, got {other}"),
        }
    }

    #[test]
    fn specs_text_is_the_cli_view() {
        let engine = load_pricing();
        let text = specs(&engine, &serde_json::json!({ "text": true })).expect("specs text");
        assert_eq!(text, crate::format_repositories(&engine.specs(), false));
        assert_eq!(text, "pricing\n");
        let all_text =
            specs(&engine, &serde_json::json!({ "text": true, "all": true })).expect("specs all");
        assert_eq!(all_text, crate::format_repositories(&engine.specs(), true));
        assert!(
            all_text.starts_with("pricing\n\nlemma\n  units\n"),
            "{all_text}"
        );
        let json = specs(&engine, &serde_json::json!({ "text": false })).expect("specs JSON");
        let value: Value = serde_json::from_str(&json).expect("specs JSON parses");
        assert!(value.is_array());
        assert!(
            !value
                .as_array()
                .expect("array")
                .iter()
                .any(|g| g["repository"] == "lemma"),
            "default specs JSON omits lemma: {json}"
        );
        assert_eq!(json, specs(&engine, &serde_json::json!({})).expect("specs"));
        let all_json = specs(&engine, &serde_json::json!({ "all": true })).expect("specs all JSON");
        let all_value: Value = serde_json::from_str(&all_json).expect("all JSON");
        assert!(
            all_value
                .as_array()
                .expect("array")
                .iter()
                .any(|g| g["repository"] == "lemma"),
            "all: true JSON includes lemma: {all_json}"
        );
        assert_text_must_be_boolean(specs(&engine, &serde_json::json!({ "text": "true" })));
        assert_text_must_be_boolean(specs(&engine, &serde_json::json!({ "text": 1 })));
    }

    #[test]
    fn show_text_is_the_cli_view() {
        let engine = load_pricing();
        let now = DateTimeValue::now();
        let expected = format!(
            "{}\n",
            engine.show(None, "pricing", Some(&now)).expect("show")
        );
        let text = show(
            &engine,
            &serde_json::json!({ "spec": "pricing", "text": true }),
        )
        .expect("show text");
        assert_eq!(text, expected);
        let json = show(
            &engine,
            &serde_json::json!({ "spec": "pricing", "text": null }),
        )
        .expect("show JSON");
        let value: Value = serde_json::from_str(&json).expect("Show JSON");
        assert_eq!(
            value["$schema"],
            "https://json-schema.org/draft/2020-12/schema"
        );
        assert_text_must_be_boolean(show(
            &engine,
            &serde_json::json!({ "spec": "pricing", "text": "yes" }),
        ));
    }

    #[test]
    fn check_text_lists_one_recommendation_per_line() {
        let source = "spec pricing 2026-01-01\n\"\"\"\nx\n\"\"\"\n\ndata qty: number\n  -> help \"Order quantity.\"\nrule total: qty\n";
        let mut engine = Engine::new();
        let source_type = SourceType::from_binding_label("pricing.lemma").expect("label");
        engine
            .load([(source_type, source.to_string())])
            .expect("load");
        let recommendations = engine.quality();
        assert!(!recommendations.is_empty());
        let expected: String = recommendations
            .iter()
            .map(|recommendation| format!("{recommendation}\n"))
            .collect();
        let text = check(&serde_json::json!({
            "sources": [["pricing.lemma", source]],
            "text": true
        }))
        .expect("check text");
        assert_eq!(text, expected);
        assert_text_must_be_boolean(check(&serde_json::json!({
            "sources": [["pricing.lemma", source]],
            "text": 0
        })));
    }

    #[test]
    fn check_text_without_recommendations_says_so() {
        let source = "spec pricing 2026-01-01\n\"\"\"\nBulk pricing.\n\"\"\"\n\ndata qty: number\n  -> minimum 0\n  -> maximum 1000000\n  -> help \"Order quantity.\"\n\nrule total: qty\n";
        let text = check(&serde_json::json!({
            "sources": [["pricing.lemma", source]],
            "text": true
        }))
        .expect("check text");
        assert_eq!(text, "No recommendations.\n");
    }
}