axon-lang 1.38.5

AXON v1.5.1 — first crates.io publication of the AXON language full-stack runtime. Lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the native Rust runtime: typed channels (TypedEventBus with QoS×5, π-calculus mobility, capability extrusion via shield D8 — Fase 13.f.2), Free Monad CPS handlers (Fase 2), lease kernel + reconcile loop (Fase 3+5), Epistemic Security Kernel (ESK Fase 6), Trust Types + ReplayLog (Fase 11.a+11.c), Stateful PEM over WebSocket (Fase 11.d), Ontological Tool Synthesis (Fase 11.e), Mobile Typed Channels (Fase 13). Crate publishes as `axon-lang` to mirror the Python PyPI package; library import remains `use axon::*` so existing call sites keep working unchanged.
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
//! Execution plan export — JSONB-compatible structured output.
//!
//! Produces a self-describing JSON representation of the execution plan
//! before execution begins. Designed for external consumption by:
//!   - PostgreSQL JSONB columns (stable schema, predictable paths)
//!   - CI/CD pipelines (plan review before execution)
//!   - Dashboards and visualization tools
//!   - External orchestrators
//!
//! Every exported JSON document includes a `_schema` header with:
//!   - `type`: document type identifier (e.g., "axon.plan", "axon.report")
//!   - `version`: schema version for backwards compatibility
//!   - `axon_version`: AXON compiler version that produced it
//!
//! JSONB path conventions:
//!   $.units[*].flow_name          — all flow names
//!   $.units[*].steps[*].name      — all step names
//!   $.dependencies.parallel_groups — parallelizable step groups
//!   $.tools.registered[*].name    — all registered tool names

use serde::Serialize;

// ── Schema metadata ────────────────────────────────────────────────────────

/// Schema metadata header — included in every exported JSON document.
#[derive(Debug, Clone, Serialize)]
pub struct SchemaHeader {
    /// Document type identifier.
    #[serde(rename = "type")]
    pub doc_type: String,
    /// Schema version (semver).
    pub version: String,
    /// AXON version that produced this document.
    pub axon_version: String,
}

impl SchemaHeader {
    pub fn new(doc_type: &str) -> Self {
        SchemaHeader {
            doc_type: doc_type.to_string(),
            version: "1.0.0".to_string(),
            axon_version: crate::runner::AXON_VERSION.to_string(),
        }
    }
}

// ── Plan export structures ─────────────────────────────────────────────────

/// Exported execution plan — the full pre-execution view.
#[derive(Debug, Clone, Serialize)]
pub struct PlanExport {
    pub _schema: SchemaHeader,
    pub source_file: String,
    pub backend: String,
    pub units: Vec<PlanUnit>,
    pub tools: PlanTools,
    pub dependencies: PlanDependencies,
    pub summary: PlanSummary,
}

/// A unit in the exported plan.
#[derive(Debug, Clone, Serialize)]
pub struct PlanUnit {
    pub flow_name: String,
    pub persona_name: String,
    pub context_name: String,
    pub effort: String,
    pub anchor_count: usize,
    pub anchors: Vec<String>,
    pub steps: Vec<PlanStep>,
}

/// A step in the exported plan.
#[derive(Debug, Clone, Serialize)]
pub struct PlanStep {
    pub name: String,
    pub step_type: String,
    pub prompt_preview: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_argument: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory_expression: Option<String>,
    pub depends_on: Vec<String>,
    pub is_root: bool,
}

/// Tool registry summary in the exported plan.
#[derive(Debug, Clone, Serialize)]
pub struct PlanTools {
    pub total: usize,
    pub builtin: Vec<String>,
    pub program: Vec<String>,
    pub registered: Vec<PlanToolEntry>,
}

/// A tool entry in the exported plan.
#[derive(Debug, Clone, Serialize)]
pub struct PlanToolEntry {
    pub name: String,
    pub provider: String,
    pub source: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub output_schema: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub effect_row: Vec<String>,
}

/// Dependency analysis summary in the exported plan.
#[derive(Debug, Clone, Serialize)]
pub struct PlanDependencies {
    pub max_depth: usize,
    pub parallel_groups: Vec<Vec<String>>,
    pub unresolved_refs: Vec<UnresolvedRef>,
}

/// An unresolved variable reference.
#[derive(Debug, Clone, Serialize)]
pub struct UnresolvedRef {
    pub step: String,
    pub variable: String,
}

/// Plan-level summary.
#[derive(Debug, Clone, Serialize)]
pub struct PlanSummary {
    pub total_units: usize,
    pub total_steps: usize,
    pub total_anchors: usize,
    pub total_tools: usize,
    pub has_parallel_steps: bool,
    pub has_unresolved_refs: bool,
}

// ── Plan builder ───────────────────────────────────────────────────────────

/// Build a plan export from execution components.
pub struct PlanBuilder;

impl PlanBuilder {
    /// Build the plan export from components.
    pub fn build(
        source_file: &str,
        backend: &str,
        units: &[PlanUnit],
        tools: PlanTools,
        deps: PlanDependencies,
    ) -> PlanExport {
        let total_steps: usize = units.iter().map(|u| u.steps.len()).sum();
        let total_anchors: usize = units.iter().map(|u| u.anchor_count).sum();

        PlanExport {
            _schema: SchemaHeader::new("axon.plan"),
            source_file: source_file.to_string(),
            backend: backend.to_string(),
            units: units.to_vec(),
            tools: tools.clone(),
            dependencies: deps.clone(),
            summary: PlanSummary {
                total_units: units.len(),
                total_steps,
                total_anchors,
                total_tools: tools.total,
                has_parallel_steps: !deps.parallel_groups.is_empty(),
                has_unresolved_refs: !deps.unresolved_refs.is_empty(),
            },
        }
    }

    /// Serialize to JSON string.
    pub fn to_json(plan: &PlanExport) -> String {
        serde_json::to_string_pretty(plan).unwrap_or_else(|e| {
            format!("{{\"error\": \"serialization failed: {e}\"}}")
        })
    }
}

// ── JSONB path query ───────────────────────────────────────────────────────

/// Simple JSONB-style path query on a serde_json::Value.
///
/// Supports a subset of JSONPath:
///   $.field           — object field access
///   $.field[N]        — array index access
///   $.field[*]        — array wildcard (returns all elements)
///   $.a.b.c           — nested field access
///
/// Returns a Vec of matched values.
pub fn jsonb_query(value: &serde_json::Value, path: &str) -> Vec<serde_json::Value> {
    let path = path.strip_prefix("$.").unwrap_or(path.strip_prefix('$').unwrap_or(path));
    if path.is_empty() {
        return vec![value.clone()];
    }

    let segments = parse_path(path);
    let mut current = vec![value.clone()];

    for seg in &segments {
        let mut next = Vec::new();
        for val in &current {
            match seg {
                PathSegment::Field(name) => {
                    if let Some(v) = val.get(name.as_str()) {
                        next.push(v.clone());
                    }
                }
                PathSegment::Index(idx) => {
                    if let Some(v) = val.get(*idx) {
                        next.push(v.clone());
                    }
                }
                PathSegment::Wildcard => {
                    if let Some(arr) = val.as_array() {
                        next.extend(arr.iter().cloned());
                    }
                }
            }
        }
        current = next;
    }

    current
}

#[derive(Debug)]
enum PathSegment {
    Field(String),
    Index(usize),
    Wildcard,
}

fn parse_path(path: &str) -> Vec<PathSegment> {
    let mut segments = Vec::new();
    let mut remaining = path;

    while !remaining.is_empty() {
        // Strip leading dot
        remaining = remaining.strip_prefix('.').unwrap_or(remaining);
        if remaining.is_empty() {
            break;
        }

        // Check for bracket notation
        if let Some(bracket_start) = remaining.find('[') {
            // Field before bracket
            let field = &remaining[..bracket_start];
            if !field.is_empty() {
                segments.push(PathSegment::Field(field.to_string()));
            }

            // Parse bracket content
            if let Some(bracket_end) = remaining[bracket_start..].find(']') {
                let inner = &remaining[bracket_start + 1..bracket_start + bracket_end];
                if inner == "*" {
                    segments.push(PathSegment::Wildcard);
                } else if let Ok(idx) = inner.parse::<usize>() {
                    segments.push(PathSegment::Index(idx));
                }
                remaining = &remaining[bracket_start + bracket_end + 1..];
            } else {
                break;
            }
        } else {
            // Find next dot or end
            let end = remaining.find('.').unwrap_or(remaining.len());
            let field = &remaining[..end];
            if !field.is_empty() {
                segments.push(PathSegment::Field(field.to_string()));
            }
            remaining = &remaining[end..];
        }
    }

    segments
}

// ── Tests ──────────────────────────────────────────────────────────────────

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

    #[test]
    fn schema_header_defaults() {
        let h = SchemaHeader::new("axon.plan");
        assert_eq!(h.doc_type, "axon.plan");
        assert_eq!(h.version, "1.0.0");
        assert!(!h.axon_version.is_empty());
    }

    #[test]
    fn plan_builder_empty() {
        let plan = PlanBuilder::build(
            "test.axon",
            "anthropic",
            &[],
            PlanTools {
                total: 2,
                builtin: vec!["Calculator".into(), "DateTimeTool".into()],
                program: vec![],
                registered: vec![],
            },
            PlanDependencies {
                max_depth: 0,
                parallel_groups: vec![],
                unresolved_refs: vec![],
            },
        );

        assert_eq!(plan._schema.doc_type, "axon.plan");
        assert_eq!(plan.source_file, "test.axon");
        assert_eq!(plan.summary.total_units, 0);
        assert_eq!(plan.summary.total_steps, 0);
        assert!(!plan.summary.has_parallel_steps);
    }

    #[test]
    fn plan_builder_with_units() {
        let units = vec![PlanUnit {
            flow_name: "Analyze".into(),
            persona_name: "Expert".into(),
            context_name: "Review".into(),
            effort: "high".into(),
            anchor_count: 1,
            anchors: vec!["NoHallucination".into()],
            steps: vec![
                PlanStep {
                    name: "Extract".into(),
                    step_type: "step".into(),
                    prompt_preview: "Extract entities".into(),
                    tool_argument: None,
                    memory_expression: None,
                    depends_on: vec![],
                    is_root: true,
                },
                PlanStep {
                    name: "Assess".into(),
                    step_type: "step".into(),
                    prompt_preview: "Assess ${Extract}".into(),
                    tool_argument: None,
                    memory_expression: None,
                    depends_on: vec!["Extract".into()],
                    is_root: false,
                },
            ],
        }];

        let plan = PlanBuilder::build(
            "contract.axon",
            "anthropic",
            &units,
            PlanTools {
                total: 2,
                builtin: vec!["Calculator".into()],
                program: vec![],
                registered: vec![],
            },
            PlanDependencies {
                max_depth: 1,
                parallel_groups: vec![],
                unresolved_refs: vec![],
            },
        );

        assert_eq!(plan.summary.total_units, 1);
        assert_eq!(plan.summary.total_steps, 2);
        assert_eq!(plan.summary.total_anchors, 1);
    }

    #[test]
    fn plan_serializes_to_json() {
        let plan = PlanBuilder::build(
            "test.axon",
            "anthropic",
            &[],
            PlanTools { total: 0, builtin: vec![], program: vec![], registered: vec![] },
            PlanDependencies { max_depth: 0, parallel_groups: vec![], unresolved_refs: vec![] },
        );
        let json = PlanBuilder::to_json(&plan);
        assert!(json.contains("\"_schema\""));
        assert!(json.contains("\"axon.plan\""));
        assert!(json.contains("\"version\""));
    }

    #[test]
    fn plan_json_has_schema_header() {
        let plan = PlanBuilder::build(
            "test.axon",
            "anthropic",
            &[],
            PlanTools { total: 0, builtin: vec![], program: vec![], registered: vec![] },
            PlanDependencies { max_depth: 0, parallel_groups: vec![], unresolved_refs: vec![] },
        );
        let json = PlanBuilder::to_json(&plan);
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed["_schema"]["type"], "axon.plan");
        assert_eq!(parsed["_schema"]["version"], "1.0.0");
        assert!(parsed["_schema"]["axon_version"].is_string());
    }

    // ── JSONB path query tests ─────────────────────────────────────

    #[test]
    fn jsonb_query_simple_field() {
        let val: serde_json::Value = serde_json::json!({"name": "test", "version": 1});
        let results = jsonb_query(&val, "$.name");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], "test");
    }

    #[test]
    fn jsonb_query_nested_field() {
        let val: serde_json::Value = serde_json::json!({"a": {"b": {"c": 42}}});
        let results = jsonb_query(&val, "$.a.b.c");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], 42);
    }

    #[test]
    fn jsonb_query_array_index() {
        let val: serde_json::Value = serde_json::json!({"items": [10, 20, 30]});
        let results = jsonb_query(&val, "$.items[1]");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], 20);
    }

    #[test]
    fn jsonb_query_wildcard() {
        let val: serde_json::Value = serde_json::json!({"units": [
            {"flow_name": "A"},
            {"flow_name": "B"},
        ]});
        let results = jsonb_query(&val, "$.units[*].flow_name");
        assert_eq!(results.len(), 2);
        assert_eq!(results[0], "A");
        assert_eq!(results[1], "B");
    }

    #[test]
    fn jsonb_query_missing_field() {
        let val: serde_json::Value = serde_json::json!({"name": "test"});
        let results = jsonb_query(&val, "$.nonexistent");
        assert!(results.is_empty());
    }

    #[test]
    fn jsonb_query_root() {
        let val: serde_json::Value = serde_json::json!(42);
        let results = jsonb_query(&val, "$");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], 42);
    }

    #[test]
    fn jsonb_query_nested_wildcard() {
        let val: serde_json::Value = serde_json::json!({
            "units": [
                {"steps": [{"name": "A"}, {"name": "B"}]},
                {"steps": [{"name": "C"}]},
            ]
        });
        let results = jsonb_query(&val, "$.units[*].steps[*].name");
        assert_eq!(results.len(), 3);
        assert_eq!(results[0], "A");
        assert_eq!(results[1], "B");
        assert_eq!(results[2], "C");
    }
}