ncp-runtime 0.3.4

NCP reference runtime — composable, auditable WASM agent graphs
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use std::collections::HashMap;

use anyhow::{bail, Context, Result};

/// Maximum definite length for CBOR arrays/maps before we reject.
/// Prevents huge allocation from a malicious/buggy brick before max_output_bytes kicks in.
const MAX_COLLECTION_LEN: u64 = 1_000_000;

/// Decoded brick result after boundary validation.
#[derive(Debug, Clone)]
pub enum BrickResult {
    Success {
        output: CborValue,
    },
    LowConfidence {
        output: CborValue,
        error: ErrorObject,
    },
    Failure {
        error: ErrorObject,
    },
}

/// Decoded error object from a brick result.
#[derive(Debug, Clone)]
pub struct ErrorObject {
    pub error_class: String,
    pub message: String,
    #[allow(dead_code)]
    pub retry_advice: Option<String>,
    #[allow(dead_code)]
    pub severity: Option<String>,
}

/// Minimal CBOR value representation for runtime use.
#[derive(Debug, Clone)]
pub enum CborValue {
    Null,
    Bool(bool),
    Integer(i64),
    Float(f64),
    Text(String),
    Bytes(Vec<u8>),
    Array(Vec<CborValue>),
    Map(Vec<(CborValue, CborValue)>),
}

impl BrickResult {
    /// Get the result type as a string.
    pub fn result_type(&self) -> &str {
        match self {
            Self::Success { .. } => "Success",
            Self::LowConfidence { .. } => "LowConfidence",
            Self::Failure { .. } => "Failure",
        }
    }

    /// Get the output value, if present.
    pub fn output(&self) -> Option<&CborValue> {
        match self {
            Self::Success { output } | Self::LowConfidence { output, .. } => Some(output),
            Self::Failure { .. } => None,
        }
    }

    /// Get the error object, if present.
    pub fn error(&self) -> Option<&ErrorObject> {
        match self {
            Self::LowConfidence { error, .. } | Self::Failure { error } => Some(error),
            Self::Success { .. } => None,
        }
    }
}

/// Decode raw CBOR result bytes and validate §9.2 structural boundaries.
pub fn decode_result(cbor_bytes: &[u8]) -> Result<BrickResult> {
    let mut decoder = minicbor::Decoder::new(cbor_bytes);

    // Top-level must be a definite-length map
    let map_len = match decoder.map() {
        Ok(Some(len)) => len,
        Ok(None) => bail!("result is an indefinite-length map (must be definite)"),
        Err(e) => bail!("result is not a valid CBOR map: {e}"),
    };

    if map_len > MAX_COLLECTION_LEN {
        bail!("result map has {map_len} entries (max {MAX_COLLECTION_LEN})");
    }

    // Collect all top-level key-value pairs, rejecting duplicates
    let mut fields: HashMap<String, CborValue> = HashMap::new();
    for _ in 0..map_len {
        let key = decode_text(&mut decoder).context("result map key must be a text string")?;
        let value = decode_value(&mut decoder).context("decoding result map value")?;
        if fields.insert(key.clone(), value).is_some() {
            bail!("duplicate top-level key in result map: '{key}'");
        }
    }

    // Extract discriminant
    let type_val = fields
        .get("type")
        .ok_or_else(|| anyhow::anyhow!("result missing 'type' discriminant field"))?;
    let type_str = match type_val {
        CborValue::Text(s) => s.as_str(),
        _ => bail!("result 'type' field must be a text string"),
    };

    match type_str {
        "Success" => validate_success(&fields),
        "LowConfidence" => validate_low_confidence(&fields),
        "Failure" => validate_failure(&fields),
        other => {
            bail!("unknown result type '{other}' (expected Success, LowConfidence, or Failure)")
        }
    }
}

fn validate_success(fields: &HashMap<String, CborValue>) -> Result<BrickResult> {
    // MUST have output
    let output = fields
        .get("output")
        .ok_or_else(|| anyhow::anyhow!("Success result missing 'output' field"))?
        .clone();

    // MUST NOT have error
    if fields.contains_key("error") {
        bail!("Success result MUST NOT have 'error' field");
    }

    // MUST NOT have carry_state_side_effects
    if fields.contains_key("carry_state_side_effects") {
        bail!("Success result MUST NOT have 'carry_state_side_effects' field");
    }

    // Phase 2: carry_state_next must be null or absent (carry_state_class=none)
    if let Some(v) = fields.get("carry_state_next") {
        if !matches!(v, CborValue::Null) {
            bail!("carry_state_next must be null/absent in Phase 2 (carry_state_class=none)");
        }
    }

    Ok(BrickResult::Success { output })
}

fn validate_low_confidence(fields: &HashMap<String, CborValue>) -> Result<BrickResult> {
    // MUST have output
    let output = fields
        .get("output")
        .ok_or_else(|| anyhow::anyhow!("LowConfidence result missing 'output' field"))?
        .clone();

    // MUST have error
    let error_val = fields
        .get("error")
        .ok_or_else(|| anyhow::anyhow!("LowConfidence result missing 'error' field"))?;
    let error = parse_error_object(error_val).context("parsing LowConfidence error object")?;

    // error.error_class MUST be LOW_CONFIDENCE
    if error.error_class != "LOW_CONFIDENCE" {
        bail!(
            "LowConfidence result error.error_class must be 'LOW_CONFIDENCE', got '{}'",
            error.error_class
        );
    }

    // MUST NOT have carry_state_side_effects
    if fields.contains_key("carry_state_side_effects") {
        bail!("LowConfidence result MUST NOT have 'carry_state_side_effects' field");
    }

    // Phase 2: carry_state_next must be null or absent (carry_state_class=none)
    if let Some(v) = fields.get("carry_state_next") {
        if !matches!(v, CborValue::Null) {
            bail!("carry_state_next must be null/absent in Phase 2 (carry_state_class=none)");
        }
    }

    Ok(BrickResult::LowConfidence { output, error })
}

fn validate_failure(fields: &HashMap<String, CborValue>) -> Result<BrickResult> {
    // MUST have error
    let error_val = fields
        .get("error")
        .ok_or_else(|| anyhow::anyhow!("Failure result missing 'error' field"))?;
    let error = parse_error_object(error_val).context("parsing Failure error object")?;

    // error.error_class MUST NOT be LOW_CONFIDENCE
    if error.error_class == "LOW_CONFIDENCE" {
        bail!("Failure result error.error_class MUST NOT be 'LOW_CONFIDENCE'");
    }

    // MUST NOT have output
    if fields.contains_key("output") {
        bail!("Failure result MUST NOT have 'output' field");
    }

    // MUST NOT have carry_state_next
    if fields.contains_key("carry_state_next") {
        bail!("Failure result MUST NOT have 'carry_state_next' field");
    }

    // carry_state_side_effects: manifest-dependent, enforced in orchestration layer.

    Ok(BrickResult::Failure { error })
}

/// Parse an error object from a CborValue (must be a map with error_class + message).
fn parse_error_object(val: &CborValue) -> Result<ErrorObject> {
    let map = match val {
        CborValue::Map(pairs) => pairs,
        _ => bail!("error field must be a CBOR map"),
    };

    let mut error_class: Option<String> = None;
    let mut message: Option<String> = None;
    let mut retry_advice: Option<String> = None;
    let mut severity: Option<String> = None;

    for (k, v) in map {
        let key = match k {
            CborValue::Text(s) => s.as_str(),
            _ => bail!("error map key must be a text string"),
        };
        match key {
            "error_class" => {
                if error_class.is_some() {
                    bail!("duplicate key 'error_class' in error object");
                }
                error_class = Some(extract_text(v).context("error.error_class must be text")?);
            }
            "message" => {
                if message.is_some() {
                    bail!("duplicate key 'message' in error object");
                }
                message = Some(extract_text(v).context("error.message must be text")?);
            }
            "retry_advice" => {
                if retry_advice.is_some() {
                    bail!("duplicate key 'retry_advice' in error object");
                }
                retry_advice = Some(extract_text(v).context("error.retry_advice must be text")?);
            }
            "severity" => {
                if severity.is_some() {
                    bail!("duplicate key 'severity' in error object");
                }
                severity = Some(extract_text(v).context("error.severity must be text")?);
            }
            _ => {} // ignore unknown fields for forward compatibility
        }
    }

    let error_class =
        error_class.ok_or_else(|| anyhow::anyhow!("error object missing 'error_class' field"))?;
    let message = message.ok_or_else(|| anyhow::anyhow!("error object missing 'message' field"))?;

    Ok(ErrorObject {
        error_class,
        message,
        retry_advice,
        severity,
    })
}

fn extract_text(val: &CborValue) -> Result<String> {
    match val {
        CborValue::Text(s) => Ok(s.clone()),
        _ => bail!("expected text string"),
    }
}

// ── CBOR Decoding ───────────────────────────────────────────────────

fn decode_text(d: &mut minicbor::Decoder<'_>) -> Result<String> {
    d.str()
        .map(|s| s.to_string())
        .map_err(|e| anyhow::anyhow!("expected CBOR text string: {e}"))
}

fn decode_value(d: &mut minicbor::Decoder<'_>) -> Result<CborValue> {
    use minicbor::data::Type;

    match d
        .datatype()
        .map_err(|e| anyhow::anyhow!("cannot peek CBOR type: {e}"))?
    {
        Type::Null => {
            d.null()
                .map_err(|e| anyhow::anyhow!("decoding null: {e}"))?;
            Ok(CborValue::Null)
        }
        Type::Undefined => {
            d.undefined()
                .map_err(|e| anyhow::anyhow!("consuming undefined: {e}"))?;
            bail!("CBOR undefined is not allowed in NCP results");
        }
        Type::Bool => {
            let b = d
                .bool()
                .map_err(|e| anyhow::anyhow!("decoding bool: {e}"))?;
            Ok(CborValue::Bool(b))
        }
        Type::U8 | Type::U16 | Type::U32 | Type::U64 => {
            let n = d.u64().map_err(|e| anyhow::anyhow!("decoding uint: {e}"))?;
            if n > i64::MAX as u64 {
                bail!("CBOR uint too large for i64: {n}");
            }
            Ok(CborValue::Integer(n as i64))
        }
        Type::I8 | Type::I16 | Type::I32 | Type::I64 => {
            let n = d.i64().map_err(|e| anyhow::anyhow!("decoding int: {e}"))?;
            Ok(CborValue::Integer(n))
        }
        Type::F16 | Type::F32 | Type::F64 => {
            let f = d
                .f64()
                .map_err(|e| anyhow::anyhow!("decoding float: {e}"))?;
            Ok(CborValue::Float(f))
        }
        Type::String => {
            let s = decode_text(d)?;
            Ok(CborValue::Text(s))
        }
        Type::Bytes => {
            let b = d
                .bytes()
                .map_err(|e| anyhow::anyhow!("decoding bytes: {e}"))?
                .to_vec();
            Ok(CborValue::Bytes(b))
        }
        Type::Array => {
            let len = d
                .array()
                .map_err(|e| anyhow::anyhow!("decoding array: {e}"))?
                .ok_or_else(|| anyhow::anyhow!("indefinite-length arrays not supported"))?;
            if len > MAX_COLLECTION_LEN {
                bail!("CBOR array has {len} elements (max {MAX_COLLECTION_LEN})");
            }
            let mut items = Vec::with_capacity(len as usize);
            for _ in 0..len {
                items.push(decode_value(d)?);
            }
            Ok(CborValue::Array(items))
        }
        Type::Map => {
            let len = d
                .map()
                .map_err(|e| anyhow::anyhow!("decoding map: {e}"))?
                .ok_or_else(|| anyhow::anyhow!("indefinite-length maps not supported"))?;
            if len > MAX_COLLECTION_LEN {
                bail!("CBOR map has {len} entries (max {MAX_COLLECTION_LEN})");
            }
            let mut pairs = Vec::with_capacity(len as usize);
            for _ in 0..len {
                let k = decode_value(d)?;
                let v = decode_value(d)?;
                pairs.push((k, v));
            }
            Ok(CborValue::Map(pairs))
        }
        Type::Tag => {
            let tag = d.tag().map_err(|e| anyhow::anyhow!("decoding tag: {e}"))?;
            bail!("CBOR tags are not supported in Phase 2 results (tag={tag:?})");
        }
        other => bail!("unsupported CBOR type: {other:?}"),
    }
}

// ── Trap Detection ──────────────────────────────────────────────────

/// Create a Failure result for a WASM trap (invoke error).
pub fn trap_failure(error_class: &str, message: String) -> BrickResult {
    BrickResult::Failure {
        error: ErrorObject {
            error_class: error_class.to_string(),
            message,
            retry_advice: None,
            severity: None,
        },
    }
}

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

    /// Helper: encode a result map to CBOR bytes.
    fn encode_result(fields: &[(&str, EncodableValue)]) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);
        enc.map(fields.len() as u64).unwrap();
        for (key, val) in fields {
            enc.str(key).unwrap();
            encode_test_value(&mut enc, val);
        }
        buf
    }

    #[allow(dead_code)]
    enum EncodableValue {
        Text(String),
        Int(i64),
        Float(f64),
        Null,
        Map(Vec<(String, EncodableValue)>),
    }

    fn encode_test_value(enc: &mut Encoder<&mut Vec<u8>>, val: &EncodableValue) {
        match val {
            EncodableValue::Text(s) => {
                enc.str(s).unwrap();
            }
            EncodableValue::Int(n) => {
                enc.i64(*n).unwrap();
            }
            EncodableValue::Float(f) => {
                enc.f64(*f).unwrap();
            }
            EncodableValue::Null => {
                enc.null().unwrap();
            }
            EncodableValue::Map(pairs) => {
                enc.map(pairs.len() as u64).unwrap();
                for (k, v) in pairs {
                    enc.str(k).unwrap();
                    encode_test_value(enc, v);
                }
            }
        }
    }

    fn text(s: &str) -> EncodableValue {
        EncodableValue::Text(s.to_string())
    }
    fn output_map() -> EncodableValue {
        EncodableValue::Map(vec![
            ("label".into(), text("positive")),
            ("confidence".into(), EncodableValue::Float(0.95)),
        ])
    }
    fn error_obj(class: &str) -> EncodableValue {
        EncodableValue::Map(vec![
            ("error_class".into(), text(class)),
            ("message".into(), text("something went wrong")),
        ])
    }

    // ── Valid variants ──────────────────────────────────────────────

    #[test]
    fn valid_success() {
        let bytes = encode_result(&[("type", text("Success")), ("output", output_map())]);
        let result = decode_result(&bytes).unwrap();
        assert_eq!(result.result_type(), "Success");
        assert!(result.output().is_some());
        assert!(result.error().is_none());
    }

    #[test]
    fn valid_low_confidence() {
        let bytes = encode_result(&[
            ("type", text("LowConfidence")),
            ("output", output_map()),
            ("error", error_obj("LOW_CONFIDENCE")),
        ]);
        let result = decode_result(&bytes).unwrap();
        assert_eq!(result.result_type(), "LowConfidence");
        assert!(result.output().is_some());
        assert_eq!(result.error().unwrap().error_class, "LOW_CONFIDENCE");
    }

    #[test]
    fn valid_failure() {
        let bytes = encode_result(&[
            ("type", text("Failure")),
            ("error", error_obj("COMPUTATION_ERROR")),
        ]);
        let result = decode_result(&bytes).unwrap();
        assert_eq!(result.result_type(), "Failure");
        assert!(result.output().is_none());
        assert_eq!(result.error().unwrap().error_class, "COMPUTATION_ERROR");
    }

    #[test]
    fn valid_success_with_null_carry_state_next() {
        let bytes = encode_result(&[
            ("type", text("Success")),
            ("output", output_map()),
            ("carry_state_next", EncodableValue::Null),
        ]);
        let result = decode_result(&bytes).unwrap();
        assert_eq!(result.result_type(), "Success");
    }

    // ── Invalid variants ────────────────────────────────────────────

    #[test]
    fn invalid_success_with_error() {
        let bytes = encode_result(&[
            ("type", text("Success")),
            ("output", output_map()),
            ("error", error_obj("LOW_CONFIDENCE")),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("MUST NOT have 'error'"));
    }

    #[test]
    fn invalid_failure_with_output() {
        let bytes = encode_result(&[
            ("type", text("Failure")),
            ("error", error_obj("COMPUTATION_ERROR")),
            ("output", output_map()),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("MUST NOT have 'output'"));
    }

    #[test]
    fn invalid_low_confidence_without_error() {
        let bytes = encode_result(&[("type", text("LowConfidence")), ("output", output_map())]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("missing 'error'"));
    }

    #[test]
    fn invalid_low_confidence_wrong_error_class() {
        let bytes = encode_result(&[
            ("type", text("LowConfidence")),
            ("output", output_map()),
            ("error", error_obj("COMPUTATION_ERROR")),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("must be 'LOW_CONFIDENCE'"));
    }

    #[test]
    fn invalid_missing_type() {
        let bytes = encode_result(&[("output", output_map())]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("missing 'type'"));
    }

    #[test]
    fn invalid_unknown_type() {
        let bytes = encode_result(&[("type", text("Unknown")), ("output", output_map())]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("unknown result type"));
    }

    #[test]
    fn invalid_error_missing_message() {
        let error_no_msg =
            EncodableValue::Map(vec![("error_class".into(), text("COMPUTATION_ERROR"))]);
        let bytes = encode_result(&[("type", text("Failure")), ("error", error_no_msg)]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(
            err.chain()
                .any(|c| c.to_string().contains("missing 'message'")),
            "expected cause not found in error chain: {err:?}"
        );
    }

    #[test]
    fn invalid_failure_with_low_confidence_class() {
        let bytes = encode_result(&[
            ("type", text("Failure")),
            ("error", error_obj("LOW_CONFIDENCE")),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("MUST NOT be 'LOW_CONFIDENCE'"));
    }

    #[test]
    fn invalid_duplicate_top_level_key() {
        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);
        enc.map(3).unwrap();
        enc.str("type").unwrap();
        enc.str("Success").unwrap();
        enc.str("output").unwrap();
        enc.str("hello").unwrap();
        enc.str("type").unwrap();
        enc.str("Failure").unwrap();
        let err = decode_result(&buf).unwrap_err();
        assert!(err.to_string().contains("duplicate top-level key"));
    }

    #[test]
    fn invalid_failure_with_carry_state_next() {
        let bytes = encode_result(&[
            ("type", text("Failure")),
            ("error", error_obj("COMPUTATION_ERROR")),
            ("carry_state_next", EncodableValue::Null),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("MUST NOT have 'carry_state_next'"));
    }

    #[test]
    fn invalid_success_with_non_null_carry_state_next() {
        let bytes = encode_result(&[
            ("type", text("Success")),
            ("output", output_map()),
            ("carry_state_next", text("some_state")),
        ]);
        let err = decode_result(&bytes).unwrap_err();
        assert!(err.to_string().contains("carry_state_next must be null"));
    }

    #[test]
    fn invalid_error_duplicate_key() {
        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);
        enc.map(2).unwrap();
        enc.str("type").unwrap();
        enc.str("Failure").unwrap();
        enc.str("error").unwrap();
        enc.map(3).unwrap();
        enc.str("error_class").unwrap();
        enc.str("COMPUTATION_ERROR").unwrap();
        enc.str("error_class").unwrap();
        enc.str("LOW_CONFIDENCE").unwrap();
        enc.str("message").unwrap();
        enc.str("oops").unwrap();
        let err = decode_result(&buf).unwrap_err();
        assert!(
            err.chain()
                .any(|c| c.to_string().contains("duplicate key 'error_class'")),
            "expected cause not found in error chain: {err:?}"
        );
    }
}