sim-lib-bridge 0.1.4

Checked BRIDGE packet runtime for SIM model exchanges.
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
use std::collections::BTreeSet;
use std::sync::Arc;

use sim_codec_bridge::{
    BridgeBook, BridgePacket, BridgePart, BridgePartSpec, content_id_string, decode_bridge_text,
    expr_to_packet, packet_content_id,
};
use sim_kernel::{CapabilitySet, Cx, Diagnostic, Error, Expr, Result, Symbol, Value};
use sim_lib_agent_runner_core::{ModelResponse, effective_ceiling, terminal_model_content};
use sim_shape::{
    AnyShape, ExactExprShape, ExprKind, ExprKindShape, FieldShape, FieldSpec, MatchScore,
    OneOfShape, Shape, ShapeDoc, ShapeMatch, check_value_report, shape_value,
};
use sim_value::access::field;

use crate::loom_validate::validate_packet_weaves;
use crate::parent::parents_contain_cid;
use crate::report::{BridgeObligation, BridgeReport};
use crate::warrant::verify_warrant;

/// Resolves a packet's declared capability ceiling against the current context.
pub fn effective_caps(cx: &Cx, packet: &BridgePacket) -> Result<CapabilitySet> {
    effective_ceiling(cx.capabilities(), &packet.header.ceiling)
}

/// Checks a BRIDGE packet against the local book and optional parent packet.
///
/// A packet checked as a reply must cite the parent content id, send from one
/// of the parent's recipients, and address exactly the parent's sender. The
/// same check also validates move legality, part shapes, warrant freshness,
/// LOOM payloads, and the parent `Return` contract.
pub fn rx_check(
    cx: &mut Cx,
    book: &BridgeBook,
    packet: &BridgePacket,
    reply_to: Option<&BridgePacket>,
) -> Result<BridgeReport> {
    let mut report = BridgeReport::new(packet_report_cid(packet));
    check_header_linkage(&mut report, packet, reply_to);
    check_move(book, &mut report, packet, reply_to);
    check_parts(cx, book, &mut report, packet)?;
    for obligation in verify_warrant(cx, book, packet)? {
        report.obligate(obligation);
    }
    validate_packet_weaves(cx, packet, &mut report)?;
    if let Some(parent) = reply_to {
        check_parent_return(cx, &mut report, packet, parent)?;
    }
    Ok(report)
}

/// Decodes and checks the last content item of a model response.
pub fn bridge_rx_response(
    cx: &mut Cx,
    book: &BridgeBook,
    response: &ModelResponse,
    reply_to: Option<&BridgePacket>,
) -> Result<(BridgePacket, BridgeReport)> {
    let text = terminal_bridge_text(response)?;
    let packet = decode_bridge_text(text, book)?;
    let report = rx_check(cx, book, &packet, reply_to)?;
    if report.accepted() {
        Ok((packet, report))
    } else {
        Err(Error::Eval(format!(
            "bridge rx check failed: {:?}",
            report.obligations
        )))
    }
}

/// Decodes and checks a model response expression.
pub fn bridge_rx(
    cx: &mut Cx,
    book: &BridgeBook,
    response: Expr,
    reply_to: Option<&BridgePacket>,
) -> Result<(BridgePacket, BridgeReport)> {
    let response = ModelResponse::try_from(response)?;
    bridge_rx_response(cx, book, &response, reply_to)
}

pub(crate) fn terminal_bridge_text(response: &ModelResponse) -> Result<&str> {
    match terminal_model_content(response)? {
        Expr::String(text) => Ok(text),
        Expr::Map(_) => match field(terminal_model_content(response)?, "text") {
            Some(Expr::String(text)) => Ok(text),
            _ => Err(Error::Eval(
                "terminal BRIDGE response content must be a string or text map".to_owned(),
            )),
        },
        _ => Err(Error::Eval(
            "terminal BRIDGE response content must be text".to_owned(),
        )),
    }
}

fn packet_report_cid(packet: &BridgePacket) -> String {
    packet.header.cid.clone().unwrap_or_else(|| {
        packet_content_id(packet)
            .map(|id| content_id_string(&id))
            .unwrap_or_else(|_| "unhashable".to_owned())
    })
}

fn check_header_linkage(
    report: &mut BridgeReport,
    packet: &BridgePacket,
    reply_to: Option<&BridgePacket>,
) {
    let mut seen = BTreeSet::new();
    for part in &packet.body {
        if !seen.insert(part.id.clone()) {
            report.reject(&part.id);
            report.obligate(BridgeObligation::repair_packet(
                format!("body/{}", part.id.as_qualified_str()),
                "duplicate part id",
                "unique part id",
                part.id.as_qualified_str(),
            ));
        }
    }

    require_part_id(report, packet, &packet.header.task, "header/task");
    require_part_id(report, packet, &packet.header.output, "header/output");
    for context in &packet.header.context {
        require_part_id(report, packet, context, "header/context");
    }

    if let Some(parent) = reply_to {
        let Some(parent_cid) = &parent.header.cid else {
            report.obligate(BridgeObligation::repair_packet(
                "header/parents",
                "parent packet is missing a cid",
                "stamped parent cid",
                "none",
            ));
            return;
        };
        if !parents_contain_cid(&packet.header.parents, parent_cid) {
            report.obligate(BridgeObligation::repair_packet(
                "header/parents",
                "reply does not cite parent packet",
                parent_cid,
                format!("{:?}", packet.header.parents),
            ));
        }
        check_reply_actor_linkage(report, packet, parent);
    }
}

fn check_reply_actor_linkage(
    report: &mut BridgeReport,
    packet: &BridgePacket,
    parent: &BridgePacket,
) {
    if !parent
        .header
        .to
        .iter()
        .any(|actor| actor == &packet.header.from)
    {
        let expected = if parent.header.to.is_empty() {
            "one parent header/to recipient".to_owned()
        } else {
            format!("{:?}", parent.header.to)
        };
        report.obligate(BridgeObligation::repair_packet(
            "header/from",
            "reply sender is not a parent recipient",
            expected,
            packet.header.from.clone(),
        ));
    }

    let expected_to = vec![parent.header.from.clone()];
    if packet.header.to != expected_to {
        report.obligate(BridgeObligation::repair_packet(
            "header/to",
            "reply recipient does not mirror parent sender",
            format!("{expected_to:?}"),
            format!("{:?}", packet.header.to),
        ));
    }
}

fn require_part_id(report: &mut BridgeReport, packet: &BridgePacket, id: &Symbol, path: &str) {
    if packet.body.iter().any(|part| &part.id == id) {
        return;
    }
    report.obligate(BridgeObligation::repair_packet(
        path,
        "header references a missing part",
        id.as_qualified_str(),
        "missing",
    ));
}

fn check_move(
    book: &BridgeBook,
    report: &mut BridgeReport,
    packet: &BridgePacket,
    reply_to: Option<&BridgePacket>,
) {
    let parent_moves = reply_to
        .map(|parent| vec![parent.header.move_kind.clone()])
        .unwrap_or_default();
    let part_kinds = packet
        .body
        .iter()
        .map(|part| part.kind.clone())
        .collect::<Vec<_>>();
    if let Err(err) = book
        .moves
        .check_move(&packet.header.move_kind, &parent_moves, &part_kinds)
    {
        report.obligate(BridgeObligation::repair_packet(
            "header/move",
            "illegal BRIDGE move",
            "move book accepts intent, parent, and required parts",
            err.to_string(),
        ));
    }
}

fn check_parts(
    cx: &mut Cx,
    book: &BridgeBook,
    report: &mut BridgeReport,
    packet: &BridgePacket,
) -> Result<()> {
    for part in &packet.body {
        match book.parts.spec(&part.kind) {
            Some(spec) => {
                if check_part_shape(cx, spec, part, report)? {
                    report.accept(&part.id);
                } else {
                    report.reject(&part.id);
                }
            }
            None => {
                report.reject(&part.id);
                report.obligate(BridgeObligation::repair_packet(
                    format!("body/{}", part.id.as_qualified_str()),
                    "unknown BRIDGE part kind",
                    "registered part kind",
                    part.kind.as_qualified_str(),
                ));
            }
        }
    }
    Ok(())
}

fn check_part_shape(
    cx: &mut Cx,
    spec: &BridgePartSpec,
    part: &BridgePart,
    report: &mut BridgeReport,
) -> Result<bool> {
    let expected = expected_part_payload_shape(spec);
    check_payload_shape(
        cx,
        &format!("body/{}/payload", part.id.as_qualified_str()),
        &format!("{} payload", spec.kind.as_qualified_str()),
        expected,
        &part.payload,
        report,
    )
}

fn expected_part_payload_shape(spec: &BridgePartSpec) -> Arc<dyn Shape> {
    if spec.kind == Symbol::qualified("bridge", "Extension")
        || spec.kind == Symbol::qualified("bridge", "Return")
    {
        Arc::new(AnyShape)
    } else {
        Arc::new(ExprKindShape::new(ExprKind::Map))
    }
}

fn check_parent_return(
    cx: &mut Cx,
    report: &mut BridgeReport,
    packet: &BridgePacket,
    parent: &BridgePacket,
) -> Result<()> {
    let Some(contract) = part_by_id(parent, &parent.header.output) else {
        report.obligate(BridgeObligation::repair_packet(
            "reply-to/header/output",
            "parent output part is missing",
            parent.header.output.as_qualified_str(),
            "missing",
        ));
        return Ok(());
    };
    if contract.kind != Symbol::qualified("bridge", "Return") {
        report.obligate(BridgeObligation::repair_packet(
            "reply-to/header/output",
            "parent output part is not a Return contract",
            "bridge/Return",
            contract.kind.as_qualified_str(),
        ));
        return Ok(());
    }

    let Some(shape_expr) = field(&contract.payload, "shape") else {
        return Ok(());
    };
    let Some(reply_output) = part_by_id(packet, &packet.header.output) else {
        report.obligate(BridgeObligation::repair_packet(
            "header/output",
            "reply output part is missing",
            packet.header.output.as_qualified_str(),
            "missing",
        ));
        return Ok(());
    };
    let Some(shape) = shape_from_contract_expr(shape_expr) else {
        report.obligate(BridgeObligation::repair_packet(
            "reply-to/return/shape",
            "unsupported Return shape expression",
            "core primitives, bridge/Answer, bridge/Refusal, or shape/OneOf",
            format!("{shape_expr:?}"),
        ));
        return Ok(());
    };
    check_payload_shape(
        cx,
        &format!("body/{}/payload", reply_output.id.as_qualified_str()),
        "parent Return contract",
        shape,
        &reply_output.payload,
        report,
    )?;
    Ok(())
}

fn part_by_id<'a>(packet: &'a BridgePacket, id: &Symbol) -> Option<&'a BridgePart> {
    packet.body.iter().find(|part| &part.id == id)
}

pub(crate) fn shape_from_contract_expr(expr: &Expr) -> Option<Arc<dyn Shape>> {
    match expr {
        Expr::Symbol(symbol) => symbol_shape(symbol),
        Expr::Map(_) => shape_descriptor(expr),
        _ => None,
    }
}

fn symbol_shape(symbol: &Symbol) -> Option<Arc<dyn Shape>> {
    let shape: Arc<dyn Shape> = match (symbol.namespace.as_deref(), symbol.name.as_ref()) {
        (Some("core"), "Any") => Arc::new(AnyShape),
        (Some("core"), "String") | (Some("bridge"), "Answer") => {
            Arc::new(ExprKindShape::new(ExprKind::String))
        }
        (Some("core"), "Bool") => Arc::new(ExprKindShape::new(ExprKind::Bool)),
        (Some("core"), "Number") => Arc::new(ExprKindShape::new(ExprKind::Number)),
        (Some("core"), "Symbol") => Arc::new(ExprKindShape::new(ExprKind::Symbol)),
        (Some("core"), "List") => Arc::new(ExprKindShape::new(ExprKind::List)),
        (Some("core"), "Map") => Arc::new(ExprKindShape::new(ExprKind::Map)),
        (Some("bridge"), "Packet") => Arc::new(BridgePacketExprShape),
        (Some("bridge"), "Refusal") => refusal_shape(),
        _ => return None,
    };
    Some(shape)
}

struct BridgePacketExprShape;

impl Shape for BridgePacketExprShape {
    fn symbol(&self) -> Option<Symbol> {
        Some(Symbol::qualified("bridge", "Packet"))
    }

    fn check_value(&self, cx: &mut Cx, value: Value) -> Result<ShapeMatch> {
        let expr = value.object().as_expr(cx)?;
        self.check_expr(cx, &expr)
    }

    fn check_expr(&self, _cx: &mut Cx, expr: &Expr) -> Result<ShapeMatch> {
        match expr_to_packet(expr) {
            Ok(_) => Ok(ShapeMatch::accept(MatchScore::exact(10))),
            Err(err) => Ok(ShapeMatch::reject_with_diagnostic(Diagnostic::error(
                format!("expected bridge/Packet expression: {err}"),
            ))),
        }
    }

    fn describe(&self, _cx: &mut Cx) -> Result<ShapeDoc> {
        Ok(ShapeDoc::new("bridge/Packet"))
    }
}

fn shape_descriptor(expr: &Expr) -> Option<Arc<dyn Shape>> {
    let Some(Expr::Symbol(kind)) = field(expr, "shape") else {
        return None;
    };
    if kind != &Symbol::qualified("shape", "OneOf") {
        return None;
    }
    let choices = match field(expr, "choices")? {
        Expr::Vector(items) | Expr::List(items) => items,
        _ => return None,
    };
    let choices = choices
        .iter()
        .map(shape_from_contract_expr)
        .collect::<Option<Vec<_>>>()?;
    Some(Arc::new(OneOfShape::new(choices)))
}

fn refusal_shape() -> Arc<dyn Shape> {
    Arc::new(FieldShape::anonymous(vec![
        FieldSpec::required(
            Symbol::new("kind"),
            Arc::new(ExactExprShape::new(Expr::Symbol(Symbol::qualified(
                "bridge", "Refusal",
            )))),
        ),
        FieldSpec::required(
            Symbol::new("reason"),
            Arc::new(ExprKindShape::new(ExprKind::String)),
        ),
    ]))
}

fn check_payload_shape(
    cx: &mut Cx,
    path: &str,
    expected: &str,
    shape: Arc<dyn Shape>,
    payload: &Expr,
    report: &mut BridgeReport,
) -> Result<bool> {
    let shape_ref = shape_value(Symbol::qualified("bridge", expected), shape);
    let value = cx.factory().expr(payload.clone())?;
    let matched = check_value_report(cx, &shape_ref, value)?;
    if matched.accepted {
        return Ok(true);
    }
    report.obligate(BridgeObligation::repair_packet(
        path,
        "payload failed Shape check",
        expected,
        if matched.diagnostics.is_empty() {
            format!("{payload:?}")
        } else {
            matched
                .diagnostics
                .iter()
                .map(|diagnostic| diagnostic.message.clone())
                .collect::<Vec<_>>()
                .join("; ")
        },
    ));
    Ok(false)
}