sim-lib-topology 0.1.2

Data-driven topology engine.
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
//! Public topology graph model: graphs, nodes, edges, ports, cells, budgets.
//!
//! These are the authoring value types that topology data compiles from.

use sim_kernel::{Expr, Symbol};

/// Canonical topology data API tag used by serialized graphs.
pub const TOPOLOGY_API: &str = "sim.topology.v3";

/// Default graph version for newly constructed in-memory graphs.
pub const DEFAULT_GRAPH_VERSION: &str = "0.1.0";

/// Default maximum scheduler steps for a graph run.
pub const DEFAULT_MAX_STEPS: u32 = 256;

/// Default maximum visits to any node during a graph run.
pub const DEFAULT_MAX_NODE_VISITS: u32 = 64;

/// Default maximum traversals of any edge during a graph run.
pub const DEFAULT_MAX_EDGE_VISITS: u32 = 64;

/// Stable node identifier inside a topology graph.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeId(pub Symbol);

impl NodeId {
    /// Creates a node id from an unqualified symbol name.
    pub fn new(name: impl Into<String>) -> Self {
        Self(Symbol::new(name.into()))
    }

    /// Returns the underlying symbol.
    pub fn as_symbol(&self) -> &Symbol {
        &self.0
    }
}

impl From<Symbol> for NodeId {
    fn from(value: Symbol) -> Self {
        Self(value)
    }
}

impl From<&str> for NodeId {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for NodeId {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

/// Stable edge identifier inside a compiled or parsed topology graph.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EdgeId(pub u32);

impl EdgeId {
    /// Creates an edge id from a stable zero-based index.
    pub const fn new(index: u32) -> Self {
        Self(index)
    }
}

impl From<u32> for EdgeId {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

/// Reference to a named port on a node.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PortRef {
    /// The node that owns the referenced port.
    pub node: NodeId,
    /// The referenced port name.
    pub port: Symbol,
}

impl PortRef {
    /// Creates a port reference from an explicit node and port symbol.
    pub fn new(node: impl Into<NodeId>, port: Symbol) -> Self {
        Self {
            node: node.into(),
            port,
        }
    }

    /// Creates a port reference from an unqualified port name.
    pub fn named(node: impl Into<NodeId>, port: impl Into<String>) -> Self {
        Self::new(node, Symbol::new(port.into()))
    }

    /// Creates a destination reference using the default `in` port.
    pub fn input(node: impl Into<NodeId>) -> Self {
        Self::named(node, "in")
    }

    /// Creates a source reference using the default `out` port.
    pub fn output(node: impl Into<NodeId>) -> Self {
        Self::named(node, "out")
    }
}

/// Canonical in-memory representation of topology graph data.
#[derive(Clone, Debug)]
pub struct Graph {
    /// Graph name.
    pub name: Symbol,
    /// Graph artifact version.
    pub version: String,
    /// Canonical topology data API tag.
    pub api: String,
    /// Optional public input shape expression.
    pub input: Option<Expr>,
    /// Optional public output shape expression.
    pub output: Option<Expr>,
    /// Graph nodes in deterministic declaration order.
    pub nodes: Vec<Node>,
    /// Graph edges in deterministic declaration order.
    pub edges: Vec<Edge>,
    /// Graph state cells.
    pub cells: Vec<Cell>,
    /// Run scheduling policy.
    pub scheduler: Scheduler,
    /// Run budget limits.
    pub budget: Budget,
    /// Capabilities required by this graph.
    pub capabilities: Vec<Symbol>,
    /// Extra graph metadata.
    pub metadata: Vec<(Symbol, Expr)>,
    /// Embedded graph tests.
    pub tests: Vec<GraphTest>,
}

impl Graph {
    /// Creates an empty graph with deterministic defaults.
    pub fn new(name: Symbol) -> Self {
        Self {
            name,
            version: DEFAULT_GRAPH_VERSION.to_owned(),
            api: TOPOLOGY_API.to_owned(),
            input: None,
            output: None,
            nodes: Vec::new(),
            edges: Vec::new(),
            cells: Vec::new(),
            scheduler: Scheduler::default(),
            budget: Budget::default(),
            capabilities: Vec::new(),
            metadata: Vec::new(),
            tests: Vec::new(),
        }
    }

    /// Creates a minimal graph from an unqualified symbol name.
    pub fn minimal(name: impl Into<String>) -> Self {
        Self::new(Symbol::new(name.into()))
    }

    /// Compatibility constructor for earlier scaffold tests.
    pub fn placeholder() -> Self {
        Self::minimal("topology")
    }
}

impl Default for Graph {
    fn default() -> Self {
        Self::minimal("topology")
    }
}

/// A topology graph step with named input and output ports.
#[derive(Clone, Debug)]
pub struct Node {
    /// Node id unique within the graph.
    pub id: NodeId,
    /// Generic node verb, such as `in`, `out`, `call`, or `wire`.
    pub verb: Symbol,
    /// Canonical `in` ports.
    pub inputs: Vec<Port>,
    /// Canonical `out` ports.
    pub outputs: Vec<Port>,
    /// Optional runtime target value expression.
    pub target: Option<Expr>,
    /// Optional role tag used by frame-aware targets.
    pub role: Option<Symbol>,
    /// Optional node input shape expression.
    pub input: Option<Expr>,
    /// Optional node output shape expression.
    pub output: Option<Expr>,
    /// Node options preserved from graph data.
    pub options: Vec<(Symbol, Expr)>,
}

impl Node {
    /// Creates a node with default ports for the provided verb.
    pub fn new(id: impl Into<NodeId>, verb: Symbol) -> Self {
        let (inputs, outputs) = default_ports_for_verb(&verb);
        Self {
            id: id.into(),
            verb,
            inputs,
            outputs,
            target: None,
            role: None,
            input: None,
            output: None,
            options: Vec::new(),
        }
    }

    /// Creates a node from an unqualified verb name.
    pub fn named(id: impl Into<NodeId>, verb: impl Into<String>) -> Self {
        Self::new(id, Symbol::new(verb.into()))
    }

    /// Creates a node with explicitly declared ports.
    pub fn with_ports(
        id: impl Into<NodeId>,
        verb: Symbol,
        inputs: Vec<Port>,
        outputs: Vec<Port>,
    ) -> Self {
        Self {
            id: id.into(),
            verb,
            inputs,
            outputs,
            target: None,
            role: None,
            input: None,
            output: None,
            options: Vec::new(),
        }
    }
}

/// A named input or output port on a topology node.
#[derive(Clone, Debug)]
pub struct Port {
    /// Port name.
    pub name: Symbol,
    /// Optional port shape expression.
    pub shape: Option<Expr>,
    /// Whether packets are values or streams.
    pub mode: PortMode,
    /// Whether this port must be connected or provided.
    pub required: bool,
}

impl Port {
    /// Creates a port from an explicit symbol.
    pub fn new(name: Symbol, mode: PortMode, required: bool) -> Self {
        Self {
            name,
            shape: None,
            mode,
            required,
        }
    }

    /// Creates a value port from an unqualified symbol name.
    pub fn value(name: impl Into<String>, required: bool) -> Self {
        Self::new(Symbol::new(name.into()), PortMode::Value, required)
    }

    /// Creates a stream port from an unqualified symbol name.
    pub fn stream(name: impl Into<String>, required: bool) -> Self {
        Self::new(Symbol::new(name.into()), PortMode::Stream, required)
    }
}

/// Port packet mode.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PortMode {
    /// The port carries one value packet at a time.
    #[default]
    Value,
    /// The port carries stream packets or stream handles.
    Stream,
}

/// A directed route from one node port to another node port.
#[derive(Clone, Debug)]
pub struct Edge {
    /// Stable edge id.
    pub id: EdgeId,
    /// Source node port. Omitted source ports normalize to `out`.
    pub from: PortRef,
    /// Destination node port. Omitted destination ports normalize to `in`.
    pub to: PortRef,
    /// Optional edge predicate expression.
    pub when: Option<Expr>,
    /// Optional transform expression applied while routing.
    pub transform: Option<Expr>,
    /// Optional wrapper key for the routed value.
    pub as_name: Option<Symbol>,
    /// Deterministic routing priority.
    pub priority: i64,
    /// Optional per-edge visit cap.
    pub max_visits: Option<u32>,
    /// Optional buffer policy expression.
    pub buffer: Option<Expr>,
    /// Extra edge metadata.
    pub metadata: Vec<(Symbol, Expr)>,
}

impl Edge {
    /// Creates an edge with deterministic defaults.
    pub fn new(id: impl Into<EdgeId>, from: PortRef, to: PortRef) -> Self {
        Self {
            id: id.into(),
            from,
            to,
            when: None,
            transform: None,
            as_name: None,
            priority: 0,
            max_visits: None,
            buffer: None,
            metadata: Vec::new(),
        }
    }
}

/// State cell available to nodes during a topology run.
#[derive(Clone, Debug)]
pub struct Cell {
    /// Cell name.
    pub name: Symbol,
    /// Optional cell shape expression.
    pub shape: Option<Expr>,
    /// Initial cell value expression.
    pub initial: Expr,
    /// Optional merge strategy symbol.
    pub merge: Option<Symbol>,
    /// Whether reflection should redact this cell by default.
    pub private: bool,
}

impl Cell {
    /// Creates a public cell with no shape or merge strategy.
    pub fn new(name: Symbol, initial: Expr) -> Self {
        Self {
            name,
            shape: None,
            initial,
            merge: None,
            private: false,
        }
    }
}

/// Deterministic scheduler settings for a topology graph.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Scheduler {
    /// Scheduler mode.
    pub mode: SchedulerMode,
    /// Optional deterministic seed.
    pub seed: Option<u64>,
    /// Maximum number of concurrent node runs.
    pub max_concurrency: u32,
    /// Whether scheduling must preserve deterministic replay.
    pub deterministic: bool,
}

impl Default for Scheduler {
    fn default() -> Self {
        Self {
            mode: SchedulerMode::Sequential,
            seed: None,
            max_concurrency: 1,
            deterministic: true,
        }
    }
}

/// Scheduler strategy for graph execution.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SchedulerMode {
    /// Runs nodes in deterministic sequential order.
    #[default]
    Sequential,
}

/// Bounded execution budget for one topology run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Budget {
    /// Maximum scheduler steps.
    pub max_steps: u32,
    /// Maximum visits to any node.
    pub max_node_visits: u32,
    /// Maximum traversals of any edge.
    pub max_edge_visits: u32,
    /// Maximum emitted outputs.
    pub max_outputs: u32,
    /// Maximum nested child topology runs.
    pub max_child_runs: u32,
    /// Optional wall-clock deadline in milliseconds.
    pub deadline_ms: Option<u64>,
    /// Exhaustion policy.
    pub on_exhausted: BudgetExhausted,
}

impl Default for Budget {
    fn default() -> Self {
        Self {
            max_steps: DEFAULT_MAX_STEPS,
            max_node_visits: DEFAULT_MAX_NODE_VISITS,
            max_edge_visits: DEFAULT_MAX_EDGE_VISITS,
            max_outputs: 64,
            max_child_runs: 16,
            deadline_ms: None,
            on_exhausted: BudgetExhausted::Fail,
        }
    }
}

/// Budget exhaustion behavior.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BudgetExhausted {
    /// Fails the graph run when a budget is exhausted.
    #[default]
    Fail,
    /// Returns the outputs produced before exhaustion.
    Partial,
}

/// Embedded graph-level test case.
#[derive(Clone, Debug)]
pub struct GraphTest {
    /// Test case name.
    pub name: Symbol,
    /// Input expression for the graph.
    pub input: Expr,
    /// Expected output expression or shape expression.
    pub expect: Expr,
    /// Fixture values available while running the test.
    pub fixtures: Vec<(Symbol, Expr)>,
}

impl GraphTest {
    /// Creates a test case without fixtures.
    pub fn new(name: Symbol, input: Expr, expect: Expr) -> Self {
        Self {
            name,
            input,
            expect,
            fixtures: Vec::new(),
        }
    }
}

fn default_ports_for_verb(verb: &Symbol) -> (Vec<Port>, Vec<Port>) {
    match verb.name.as_ref() {
        "in" => (Vec::new(), vec![Port::value("out", true)]),
        "out" => (vec![Port::value("in", true)], Vec::new()),
        "call" => (
            vec![Port::value("in", true)],
            vec![Port::value("out", true), Port::value("error", false)],
        ),
        "branch" => (
            vec![Port::value("in", true)],
            vec![
                Port::value("true", false),
                Port::value("false", false),
                Port::value("else", false),
            ],
        ),
        "merge" => (
            vec![Port::value("in", true)],
            vec![Port::value("out", true)],
        ),
        "tee" => (
            vec![Port::value("in", true)],
            vec![Port::value("out", true)],
        ),
        _ => (
            vec![Port::value("in", true)],
            vec![Port::value("out", true)],
        ),
    }
}