bnto-core 0.18.0

Core WASM engine library for Bnto — shared types, traits, and orchestration
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
// Definition JSON Schema — Validates `.bnto.json` files.
//
// Generates a JSON Schema (Draft 2020-12) describing the Definition format.
// Built by hand with `serde_json::json!()` — simpler than a codegen library
// since the Definition shape is stable. Makes the engine the single source
// of truth for both execution and validation.

use serde_json::Value;

/// Build the JSON Schema for a `Port` object (connection point on a node).
fn port_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "A connection point on a node (input or output).",
        "required": ["id", "name"],
        "properties": {
            "id": {
                "type": "string",
                "description": "Unique identifier for this port within its node."
            },
            "name": {
                "type": "string",
                "description": "Human-readable display name for the port."
            },
            "handle": {
                "type": "string",
                "description": "Optional handle identifier used by the visual editor for positioning."
            }
        },
        "additionalProperties": false
    })
}

/// Build the JSON Schema for an `Edge` (connection between two nodes).
fn edge_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "A connection between two nodes in the pipeline graph.",
        "required": ["id", "source", "target"],
        "properties": {
            "id": {
                "type": "string",
                "description": "Unique identifier for this edge."
            },
            "source": {
                "type": "string",
                "description": "The id of the source node (where data flows FROM)."
            },
            "target": {
                "type": "string",
                "description": "The id of the target node (where data flows TO)."
            },
            "sourceHandle": {
                "type": "string",
                "description": "Optional: which output port on the source node."
            },
            "targetHandle": {
                "type": "string",
                "description": "Optional: which input port on the target node."
            }
        },
        "additionalProperties": false
    })
}

/// Build the JSON Schema for the `metadata` object (description, timestamps, tags).
fn metadata_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "Descriptive metadata about the node (description, timestamps, tags).",
        "properties": {
            "description": {
                "type": "string",
                "description": "Human-readable description of what this node does."
            },
            "createdAt": {
                "type": "string",
                "description": "ISO 8601 timestamp of when this node was created."
            },
            "updatedAt": {
                "type": "string",
                "description": "ISO 8601 timestamp of the last modification."
            },
            "tags": {
                "type": "array",
                "description": "Tags for categorization and search.",
                "items": { "type": "string" }
            },
            "customData": {
                "type": "object",
                "description": "Open-ended key-value pairs for consumer-specific data.",
                "additionalProperties": { "type": "string" }
            }
        },
        "additionalProperties": false
    })
}

/// Build the JSON Schema for `PipelineSettings` (recipe-level configuration).
fn pipeline_settings_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "Recipe-level settings that control execution behavior.",
        "properties": {
            "iteration": {
                "type": "string",
                "description": "How the executor handles iteration over multiple input files. 'auto' wraps primitive sequences in implicit per-file loops; 'explicit' (default) executes exactly what's defined.",
                "enum": ["auto", "explicit"],
                "default": "explicit"
            },
            "timeout": {
                "type": "integer",
                "minimum": 1,
                "description": "The whole run's time limit in seconds. Once it passes, every step the run started is stopped and the run fails. No limit when unset; `bnto run --timeout` overrides it."
            }
        },
        "additionalProperties": false
    })
}

// --- Definition Schema ---

/// Build the JSON Schema for a canvas position (`{ x, y }`).
fn position_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "The node's position on the visual editor canvas.",
        "required": ["x", "y"],
        "properties": {
            "x": { "type": "number" },
            "y": { "type": "number" }
        },
        "additionalProperties": false
    })
}

/// Build the properties map for the Definition schema.
fn definition_properties() -> Value {
    serde_json::json!({
        "id":         { "type": "string", "description": "Unique identifier for this node within the recipe." },
        "type":       { "type": "string", "description": "The node type (e.g., 'image-compress', 'spreadsheet-clean', 'file-rename')." },
        "version":    { "type": "string", "description": "Format version (semver, e.g., '1.0.0')." },
        "parentId":   { "type": "string", "description": "Optional parent node id (for nested nodes)." },
        "name":       { "type": "string", "description": "Human-readable name for this node." },
        "position":   position_schema(),
        "metadata":   { "$ref": "#/$defs/Metadata" },
        "parameters": { "type": "object", "description": "Configuration parameters (key-value pairs).", "additionalProperties": true },
        "inputPorts": { "type": "array", "description": "Input connection ports.", "items": { "$ref": "#/$defs/Port" } },
        "outputPorts":{ "type": "array", "description": "Output connection ports.", "items": { "$ref": "#/$defs/Port" } },
        "settings":   { "$ref": "#/$defs/PipelineSettings" },
        "always":     { "type": "boolean", "default": false, "description": "Closing step: run even after an earlier node in the same chain failed, like `finally`. The run still fails with the first error." },
        "when":       { "$ref": "#/$defs/When" },
        "requires":   { "type": "array", "description": "External tools this node needs. At the root, needed on every OS; on a nested node, checked only on the OSes that node runs on (its platform guards).", "items": { "$ref": "#/$defs/Dependency" } },
        "nodes":      { "type": "array", "description": "Child nodes (recursive).", "items": { "$ref": "#/$defs/Definition" } },
        "edges":      { "type": "array", "description": "Connections between child nodes.", "items": { "$ref": "#/$defs/Edge" } }
    })
}

/// An external tool a node needs (`requires`).
fn dependency_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "An external program that must be on the PATH.",
        "required": ["binary"],
        "properties": {
            "binary":       { "type": "string", "description": "The program's name, as looked up on the PATH." },
            "version":      { "type": "string", "description": "A version constraint, e.g. >=6.0." },
            "installHint":  { "type": "string", "description": "How to install it, when no per-OS hint applies." },
            "installHints": {
                "type": "object",
                "description": "How to install it on each OS.",
                "properties": {
                    "macos":   { "type": "string" },
                    "linux":   { "type": "string" },
                    "windows": { "type": "string" }
                },
                "additionalProperties": false
            },
            "homepage":     { "type": "string", "description": "Where to read about it." }
        },
        "additionalProperties": false
    })
}

/// A node's `when` guard: run the step only when the comparison holds.
fn when_schema() -> Value {
    serde_json::json!({
        "type": "object",
        "description": "Run this step only when the comparison holds; otherwise skip it and pass its files through untouched. Templates work in value and expected, e.g. {{ctx.platform}} or {{fields.action}}.",
        "required": ["value", "op", "expected"],
        "properties": {
            "value":    { "type": "string", "description": "What to compare, usually a template such as {{ctx.platform}}." },
            "op":       { "type": "string", "enum": ["equals", "not-equals", "after", "before"], "description": "How value is compared to expected. after and before compare RFC 3339 times." },
            "expected": { "type": "string", "description": "What value is compared to." }
        },
        "additionalProperties": false
    })
}

/// Generate a JSON Schema (Draft 2020-12) for the `.bnto.json` Definition format.
///
/// The Definition is recursive — child `nodes` reference the same schema via `$ref`.
pub fn definition_json_schema() -> Value {
    serde_json::json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "title": "Bnto Definition",
        "description": "A .bnto.json recipe definition. Describes a pipeline of nodes that process data.",
        "$ref": "#/$defs/Definition",
        "$defs": {
            "Port": port_schema(),
            "Edge": edge_schema(),
            "Metadata": metadata_schema(),
            "PipelineSettings": pipeline_settings_schema(),
            "When": when_schema(),
            "Dependency": dependency_schema(),
            "Definition": {
                "type": "object",
                "description": "A single node in a .bnto.json recipe. Can contain child nodes (recursive).",
                "required": ["id", "type", "version", "name", "position", "metadata", "parameters", "inputPorts", "outputPorts"],
                "properties": definition_properties(),
                "additionalProperties": true
            }
        }
    })
}

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

    #[test]
    fn test_definition_schema_is_valid_json_object() {
        // The schema should be a valid JSON object (not null, not an array).
        let schema = definition_json_schema();
        assert!(schema.is_object(), "Schema should be a JSON object");
    }

    #[test]
    fn test_definition_schema_has_correct_meta_fields() {
        // Verify the schema metadata fields are present and correct.
        let schema = definition_json_schema();
        assert_eq!(
            schema["$schema"],
            "https://json-schema.org/draft/2020-12/schema"
        );
        assert_eq!(schema["title"], "Bnto Definition");
        assert!(schema["description"].is_string());
    }

    #[test]
    fn test_definition_schema_uses_ref_to_defs() {
        // The top-level schema should reference $defs/Definition.
        let schema = definition_json_schema();
        assert_eq!(schema["$ref"], "#/$defs/Definition");
    }

    #[test]
    fn test_definition_schema_has_all_defs() {
        // The $defs block should contain all 5 type definitions.
        let schema = definition_json_schema();
        let defs = schema["$defs"]
            .as_object()
            .expect("$defs should be an object");
        assert!(
            defs.contains_key("Definition"),
            "Missing Definition in $defs"
        );
        assert!(defs.contains_key("Port"), "Missing Port in $defs");
        assert!(defs.contains_key("Edge"), "Missing Edge in $defs");
        assert!(defs.contains_key("Metadata"), "Missing Metadata in $defs");
        assert!(
            defs.contains_key("PipelineSettings"),
            "Missing PipelineSettings in $defs"
        );
    }

    #[test]
    fn test_definition_has_required_properties() {
        // The Definition schema should list the correct required fields.
        let schema = definition_json_schema();
        let def = &schema["$defs"]["Definition"];
        let required = def["required"]
            .as_array()
            .expect("required should be an array");

        // Convert to strings for comparison.
        let required_strs: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();

        assert!(required_strs.contains(&"id"));
        assert!(required_strs.contains(&"type"));
        assert!(required_strs.contains(&"version"));
        assert!(required_strs.contains(&"name"));
        assert!(required_strs.contains(&"position"));
        assert!(required_strs.contains(&"metadata"));
        assert!(required_strs.contains(&"parameters"));
        assert!(required_strs.contains(&"inputPorts"));
        assert!(required_strs.contains(&"outputPorts"));
    }

    #[test]
    fn test_definition_has_recursive_nodes() {
        // The `nodes` field should reference $defs/Definition (recursive).
        let schema = definition_json_schema();
        let nodes_items = &schema["$defs"]["Definition"]["properties"]["nodes"]["items"];
        assert_eq!(
            nodes_items["$ref"], "#/$defs/Definition",
            "nodes items should reference Definition recursively"
        );
    }

    #[test]
    fn test_port_schema_has_required_fields() {
        // Port should require id and name.
        let schema = definition_json_schema();
        let port = &schema["$defs"]["Port"];
        let required = port["required"]
            .as_array()
            .expect("Port required should be an array");
        let required_strs: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(required_strs.contains(&"id"));
        assert!(required_strs.contains(&"name"));
    }

    #[test]
    fn test_edge_schema_has_required_fields() {
        // Edge should require id, source, and target.
        let schema = definition_json_schema();
        let edge = &schema["$defs"]["Edge"];
        let required = edge["required"]
            .as_array()
            .expect("Edge required should be an array");
        let required_strs: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(required_strs.contains(&"id"));
        assert!(required_strs.contains(&"source"));
        assert!(required_strs.contains(&"target"));
    }

    #[test]
    fn test_definition_schema_has_settings_property() {
        let schema = definition_json_schema();
        let props = schema["$defs"]["Definition"]["properties"]
            .as_object()
            .expect("Definition properties should be an object");
        assert!(
            props.contains_key("settings"),
            "Definition should have a settings property"
        );
        assert_eq!(
            props["settings"]["$ref"], "#/$defs/PipelineSettings",
            "settings should reference PipelineSettings"
        );
    }

    #[test]
    fn test_any_node_can_carry_requires() {
        let schema = definition_json_schema();
        let requires = &schema["$defs"]["Definition"]["properties"]["requires"];
        assert_eq!(requires["items"]["$ref"], "#/$defs/Dependency");
        let dep = &schema["$defs"]["Dependency"];
        assert_eq!(dep["required"], serde_json::json!(["binary"]));
        let hints = dep["properties"]["installHints"]["properties"]
            .as_object()
            .unwrap();
        assert!(hints.contains_key("macos") && hints.contains_key("windows"));
    }

    #[test]
    fn test_definition_schema_has_a_when_guard() {
        let schema = definition_json_schema();
        let when = &schema["$defs"]["Definition"]["properties"]["when"];
        assert_eq!(when["$ref"], "#/$defs/When");
        let guard = &schema["$defs"]["When"];
        assert_eq!(
            guard["required"],
            serde_json::json!(["value", "op", "expected"])
        );
        let ops = guard["properties"]["op"]["enum"].as_array().unwrap();
        assert!(ops.contains(&"not-equals".into()), "{ops:?}");
    }

    #[test]
    fn test_definition_schema_has_always_boolean() {
        let schema = definition_json_schema();
        let always = &schema["$defs"]["Definition"]["properties"]["always"];
        assert_eq!(always["type"], "boolean");
        assert_eq!(always["default"], false);
    }

    #[test]
    fn test_settings_schema_has_iteration_enum() {
        let schema = definition_json_schema();
        let settings = &schema["$defs"]["PipelineSettings"];
        let iteration = &settings["properties"]["iteration"];
        let enum_values = iteration["enum"]
            .as_array()
            .expect("iteration should have an enum");
        let values: Vec<&str> = enum_values.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(values.contains(&"auto"));
        assert!(values.contains(&"explicit"));
        assert_eq!(iteration["default"], "explicit");
    }

    #[test]
    fn test_settings_schema_has_timeout_in_whole_seconds() {
        let schema = definition_json_schema();
        let timeout = &schema["$defs"]["PipelineSettings"]["properties"]["timeout"];
        assert_eq!(timeout["type"], "integer");
        assert_eq!(timeout["minimum"], 1);
    }

    #[test]
    fn test_definition_schema_has_pipeline_settings_def() {
        let schema = definition_json_schema();
        let defs = schema["$defs"]
            .as_object()
            .expect("$defs should be an object");
        let settings = defs
            .get("PipelineSettings")
            .expect("PipelineSettings should exist in $defs");
        assert_eq!(settings["type"], "object");
        assert!(
            settings["properties"]["iteration"].is_object(),
            "PipelineSettings should have an iteration property"
        );
    }
}