cymule-core 0.1.3

Small trusted semantic kernel for Cymule
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
use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{CoreError, Result, content_id};

/// Frozen canonical IR version.
pub const IR_VERSION: &str = "cymule.ir/2";

/// An unsealed, language-neutral semantic plan.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PlanCandidate {
    /// IR version.
    pub ir_version: String,
    /// Human-readable plan name.
    pub name: String,
    /// Entry definition ID.
    pub entry: String,
    /// Abstract component contracts.
    #[serde(default)]
    pub components: Vec<ComponentContract>,
    /// Abstract effect contracts.
    #[serde(default)]
    pub effects: Vec<EffectContract>,
    /// Structured definitions.
    pub definitions: Vec<Definition>,
    /// Non-semantic author metadata. Keys and values are still content-addressed.
    #[serde(default)]
    pub metadata: BTreeMap<String, String>,
}

/// An immutable sealed plan.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SealedPlan {
    /// Content-addressed plan identity.
    pub plan_id: String,
    /// Validated canonical candidate.
    pub candidate: PlanCandidate,
}

/// Abstract executable component contract.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComponentContract {
    /// Stable operation ID.
    pub id: String,
    /// Input JSON Schema.
    pub input_schema: Value,
    /// Output JSON Schema.
    pub output_schema: Value,
    /// Provider-neutral required properties.
    #[serde(default)]
    pub requirements: BTreeMap<String, String>,
}

/// Abstract world-effect contract.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EffectContract {
    /// Stable operation ID.
    pub id: String,
    /// Input JSON Schema.
    pub input_schema: Value,
    /// Output JSON Schema.
    pub output_schema: Value,
    /// Effect safety and recovery properties.
    pub profile: EffectProfile,
    /// Provider-neutral required properties.
    #[serde(default)]
    pub requirements: BTreeMap<String, String>,
}

/// Orthogonal effect properties used by admission and binding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EffectProfile {
    /// Whether the operation can change the external world.
    pub mutation: MutationKind,
    /// When dispatch may be released.
    pub dispatch: DispatchPolicy,
    /// How an ambiguous result can be reconciled.
    pub reconciliation: ReconciliationMode,
    /// Whether the provider accepts a stable idempotency key.
    #[serde(default)]
    pub keyed_idempotency: bool,
    /// Whether the operation is irreversible after application.
    #[serde(default)]
    pub irreversible: bool,
}

/// Effect mutation class.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MutationKind {
    /// Read-only or observational nondeterminism.
    Observational,
    /// External mutation.
    Mutating,
}

/// Effect release policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DispatchPolicy {
    /// The observation may execute immediately.
    Eager,
    /// Release only after scope commit.
    OnScopeCommit,
    /// Release requires a separate explicit command.
    Explicit,
}

/// Reconciliation capability.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconciliationMode {
    /// Adapter can query or otherwise prove the outcome.
    Queryable,
    /// An external attestation can prove the outcome.
    ExternallyAttested,
    /// Human governance is required.
    Human,
    /// No authoritative reconciliation is available.
    Impossible,
}

/// A named Flow definition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Definition {
    /// Stable logical definition ID.
    pub id: String,
    /// Input JSON Schema.
    pub input_schema: Value,
    /// Output JSON Schema.
    pub output_schema: Value,
    /// Structured body.
    pub body: Region,
}

/// A structured sequence with an explicit result expression.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Region {
    /// Ordered semantic operations.
    #[serde(default)]
    pub steps: Vec<Step>,
    /// Region result.
    pub result: Expression,
}

/// A stable operation site.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Step {
    /// Stable source-assigned site ID.
    pub id: String,
    /// Operation at the site.
    #[serde(flatten)]
    pub operation: Operation,
}

/// Frozen structured operations. Complex frontend syntax lowers to these five
/// semantic boundaries.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Operation {
    /// Call an abstract component.
    Call {
        /// Component contract ID.
        component: String,
        /// Typed input expression.
        input: Expression,
        /// Optional result binding.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        bind: Option<String>,
    },
    /// Invoke another definition in the same immutable Plan.
    Invoke {
        /// Referenced definition ID.
        definition: String,
        /// Typed invocation input expression.
        input: Expression,
        /// Optional result binding.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        bind: Option<String>,
    },
    /// Durably suspend.
    Wait {
        /// Wait description.
        wait: WaitSpec,
    },
    /// Propose an abstract external effect.
    Effect {
        /// Effect contract ID.
        effect: String,
        /// Typed argument expression.
        input: Expression,
        /// Intentional occurrence key. Retries reuse it.
        occurrence: String,
        /// Optional result binding.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        bind: Option<String>,
    },
    /// Execute a nested transactional scope.
    Scope {
        /// Scope behavior profile.
        mode: ScopeMode,
        /// Nested body.
        body: Box<Region>,
        /// Optional result binding.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        bind: Option<String>,
    },
}

/// Scope behavior profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScopeMode {
    /// Normal nested state/evidence transaction.
    Transactional,
    /// Speculative branch; mutating effects remain staged until commit.
    Speculative,
}

/// Durable wait descriptions.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum WaitSpec {
    /// Wait for an external signal key.
    Signal {
        /// Stable signal key.
        key: String,
        /// Whether at most one waiter may consume it.
        #[serde(default)]
        consume_once: bool,
    },
    /// Wait for a logical timer identifier.
    Timer {
        /// Stable timer ID. Wall-clock observation belongs in a receipt.
        timer_id: String,
    },
    /// Wait for typed external input.
    Input {
        /// Correlation key.
        correlation: String,
        /// Input schema.
        schema: Value,
    },
}

/// Pure expressions over input, prior bindings, and literals.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Expression {
    /// Definition input.
    Input,
    /// Literal canonical JSON.
    Literal {
        /// Literal value.
        value: Value,
    },
    /// A prior binding.
    Binding {
        /// Binding name.
        name: String,
    },
    /// An object constructed from expressions.
    Object {
        /// Deterministically ordered fields.
        fields: BTreeMap<String, Expression>,
    },
    /// An array constructed from expressions.
    Array {
        /// Array items.
        items: Vec<Expression>,
    },
}

impl PlanCandidate {
    /// Validate and content-address a Plan Candidate.
    pub fn seal(self) -> Result<SealedPlan> {
        self.validate()?;
        let plan_id = content_id("cymule.plan/1", &self)?;
        Ok(SealedPlan {
            plan_id,
            candidate: self,
        })
    }

    /// Validate semantic structure before canonical identity is computed.
    pub fn validate(&self) -> Result<()> {
        if self.ir_version != IR_VERSION {
            return Err(CoreError::Validation(format!(
                "unsupported IR version {:?}; expected {IR_VERSION}",
                self.ir_version
            )));
        }
        validate_id("plan name", &self.name)?;

        let component_ids = unique_contract_ids(
            "component",
            self.components.iter().map(|contract| contract.id.as_str()),
        )?;
        let effect_ids = unique_contract_ids(
            "effect",
            self.effects.iter().map(|contract| contract.id.as_str()),
        )?;
        for contract in &self.components {
            validate_schema("component input", &contract.input_schema)?;
            validate_schema("component output", &contract.output_schema)?;
        }
        for contract in &self.effects {
            validate_schema("effect input", &contract.input_schema)?;
            validate_schema("effect output", &contract.output_schema)?;
            if contract.profile.mutation == MutationKind::Mutating
                && contract.profile.dispatch == DispatchPolicy::Eager
            {
                return Err(CoreError::Validation(format!(
                    "mutating effect {} cannot use eager dispatch",
                    contract.id
                )));
            }
        }

        let definition_ids = unique_contract_ids(
            "definition",
            self.definitions
                .iter()
                .map(|definition| definition.id.as_str()),
        )?;
        if !definition_ids.contains(&self.entry) {
            return Err(CoreError::Validation(format!(
                "entry definition {:?} does not exist",
                self.entry
            )));
        }

        let mut sites = BTreeSet::new();
        for definition in &self.definitions {
            validate_schema("definition input", &definition.input_schema)?;
            validate_schema("definition output", &definition.output_schema)?;
            validate_region(
                &definition.body,
                &component_ids,
                &effect_ids,
                &definition_ids,
                &mut sites,
                &BTreeSet::new(),
            )?;
        }
        Ok(())
    }
}

impl SealedPlan {
    /// Verify that the embedded candidate still matches its Plan ID.
    pub fn verify(&self) -> Result<()> {
        self.candidate.validate()?;
        let expected = content_id("cymule.plan/1", &self.candidate)?;
        if expected != self.plan_id {
            return Err(CoreError::IdentityMismatch(format!(
                "plan ID {} does not match {expected}",
                self.plan_id
            )));
        }
        Ok(())
    }
}

fn unique_contract_ids<'a>(
    kind: &str,
    ids: impl Iterator<Item = &'a str>,
) -> Result<BTreeSet<String>> {
    let mut unique = BTreeSet::new();
    for id in ids {
        validate_id(kind, id)?;
        if !unique.insert(id.to_owned()) {
            return Err(CoreError::Validation(format!("duplicate {kind} ID {id:?}")));
        }
    }
    Ok(unique)
}

fn validate_region(
    region: &Region,
    components: &BTreeSet<String>,
    effects: &BTreeSet<String>,
    definitions: &BTreeSet<String>,
    sites: &mut BTreeSet<String>,
    inherited: &BTreeSet<String>,
) -> Result<()> {
    let mut bindings = inherited.clone();
    for step in &region.steps {
        validate_id("step", &step.id)?;
        if !sites.insert(step.id.clone()) {
            return Err(CoreError::Validation(format!(
                "duplicate stable step site {:?}",
                step.id
            )));
        }

        let result_binding: Option<&String> = match &step.operation {
            Operation::Call {
                component,
                input,
                bind,
            } => {
                if !components.contains(component) {
                    return Err(CoreError::Validation(format!(
                        "step {} references unknown component {component:?}",
                        step.id
                    )));
                }
                validate_expression(input, &bindings)?;
                bind.as_ref()
            }
            Operation::Invoke {
                definition,
                input,
                bind,
            } => {
                if !definitions.contains(definition) {
                    return Err(CoreError::Validation(format!(
                        "step {} references unknown definition {definition:?}",
                        step.id
                    )));
                }
                validate_expression(input, &bindings)?;
                bind.as_ref()
            }
            Operation::Wait { wait } => {
                validate_wait(wait)?;
                None
            }
            Operation::Effect {
                effect,
                input,
                occurrence,
                bind,
            } => {
                if !effects.contains(effect) {
                    return Err(CoreError::Validation(format!(
                        "step {} references unknown effect {effect:?}",
                        step.id
                    )));
                }
                validate_id("occurrence", occurrence)?;
                validate_expression(input, &bindings)?;
                bind.as_ref()
            }
            Operation::Scope { body, bind, .. } => {
                validate_region(body, components, effects, definitions, sites, &bindings)?;
                bind.as_ref()
            }
        };

        if let Some(binding) = result_binding {
            validate_id("binding", binding)?;
            if !bindings.insert(binding.clone()) {
                return Err(CoreError::Validation(format!(
                    "binding {binding:?} is assigned more than once"
                )));
            }
        }
    }
    validate_expression(&region.result, &bindings)
}

fn validate_wait(wait: &WaitSpec) -> Result<()> {
    match wait {
        WaitSpec::Signal { key, .. } => validate_id("signal key", key),
        WaitSpec::Timer { timer_id } => validate_id("timer", timer_id),
        WaitSpec::Input {
            correlation,
            schema,
        } => {
            validate_id("input correlation", correlation)?;
            validate_schema("wait input", schema)
        }
    }
}

fn validate_expression(expression: &Expression, bindings: &BTreeSet<String>) -> Result<()> {
    match expression {
        Expression::Input | Expression::Literal { .. } => Ok(()),
        Expression::Binding { name } => {
            if bindings.contains(name) {
                Ok(())
            } else {
                Err(CoreError::Validation(format!(
                    "expression references undefined binding {name:?}"
                )))
            }
        }
        Expression::Object { fields } => {
            for expression in fields.values() {
                validate_expression(expression, bindings)?;
            }
            Ok(())
        }
        Expression::Array { items } => {
            for expression in items {
                validate_expression(expression, bindings)?;
            }
            Ok(())
        }
    }
}

fn validate_schema(kind: &str, schema: &Value) -> Result<()> {
    if !schema.is_object() && !schema.is_boolean() {
        return Err(CoreError::Validation(format!(
            "{kind} schema must be a JSON object or boolean"
        )));
    }
    Ok(())
}

fn validate_id(kind: &str, id: &str) -> Result<()> {
    let valid = !id.is_empty()
        && id.len() <= 160
        && id
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"._-/:".contains(&byte));
    if valid {
        Ok(())
    } else {
        Err(CoreError::Validation(format!(
            "{kind} ID {id:?} must be 1..=160 ASCII identifier characters"
        )))
    }
}