axon-lang 1.38.1

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
//! [`ReplayToken`] canonical shape + hash derivation.
//!
//! Hash input is built as:
//!
//! ```text
//!   effect_name  ∥ RS ∥
//!   canonical_json(inputs)  ∥ RS ∥
//!   canonical_json(outputs)  ∥ RS ∥
//!   model_version  ∥ RS ∥
//!   canonical_json(sampling)  ∥ RS ∥
//!   timestamp_rfc3339  ∥ RS ∥
//!   nonce_hex
//! ```
//!
//! where `RS = 0x1E` (ASCII Record Separator). The canonicaliser
//! matches the one already used by the §Fase 10.g audit chain
//! (recursive key sort, UTF-8 encoding, ASCII-safe escapes) so a
//! token can be re-hashed by the enterprise audit writer with zero
//! translation.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};

/// Sampling parameters captured at the point of a non-deterministic
/// effect (LLM inference, random sampling). Replayability depends on
/// the provider honouring `seed`; providers that ignore `seed` get
/// their effects marked `@non_replayable` in the tool descriptor and
/// the checker rejects their use in a `@sensitive` context.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SamplingParams {
    #[serde(default)]
    pub temperature: Option<f64>,
    #[serde(default)]
    pub top_p: Option<f64>,
    #[serde(default)]
    pub top_k: Option<i64>,
    #[serde(default)]
    pub seed: Option<i64>,
    #[serde(default)]
    pub max_tokens: Option<i64>,
    /// Extra provider-specific knobs (`frequency_penalty`, `stop`,
    /// tool choice strategy, …). The recorder copies whatever it
    /// was given so replay catches provider-specific defaults.
    #[serde(default, skip_serializing_if = "Value::is_null")]
    pub extras: Value,
}

impl Default for SamplingParams {
    fn default() -> Self {
        Self {
            temperature: None,
            top_p: None,
            top_k: None,
            seed: None,
            max_tokens: None,
            extras: Value::Null,
        }
    }
}

/// One replay receipt. Immutable once minted.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReplayToken {
    /// Canonical identifier of the effect invoked
    /// (`call_tool:send_slack`, `llm_infer:gpt_4o`, `db_read:customers`).
    pub effect_name: String,
    /// Inputs as given to the effect. Canonicalised then hashed into
    /// `inputs_hash_hex`; retained here in structured form for
    /// replay executors.
    pub inputs: Value,
    pub inputs_hash_hex: String,
    /// Outputs the effect produced. Same canonical treatment as
    /// inputs.
    pub outputs: Value,
    pub outputs_hash_hex: String,
    /// Model identifier (free string). For deterministic, non-LLM
    /// effects adopters use a stable version slug like
    /// `axon.builtin.db_read.v1`. For LLMs this is the provider's
    /// model id (`gpt-4o-2024-11-20`, `claude-opus-4-7`) at call
    /// time.
    pub model_version: String,
    pub sampling: SamplingParams,
    #[serde(with = "chrono::serde::ts_milliseconds")]
    pub timestamp: DateTime<Utc>,
    /// 128-bit random nonce; prevents collision between two
    /// semantically identical invocations.
    pub nonce_hex: String,
    /// Derived hex SHA-256 of the canonical hash input. Stable
    /// across Rust + Python implementations.
    pub token_hash_hex: String,
}

impl ReplayToken {
    /// Build a fresh token, computing every derived hash. Callers
    /// usually go through [`ReplayTokenBuilder`] for ergonomics but
    /// the direct constructor is public for explicit constructions
    /// in tests / adapters.
    pub fn mint(
        effect_name: impl Into<String>,
        inputs: Value,
        outputs: Value,
        model_version: impl Into<String>,
        sampling: SamplingParams,
        timestamp: DateTime<Utc>,
        nonce: [u8; 16],
    ) -> Self {
        let effect_name = effect_name.into();
        let model_version = model_version.into();
        let inputs_hash_hex = hex(&canonical_hash(&inputs));
        let outputs_hash_hex = hex(&canonical_hash(&outputs));
        let nonce_hex = hex(&nonce);
        let token_hash_hex = hex(&derive_token_hash(
            &effect_name,
            &inputs,
            &outputs,
            &model_version,
            &sampling,
            timestamp,
            &nonce,
        ));
        ReplayToken {
            effect_name,
            inputs,
            inputs_hash_hex,
            outputs,
            outputs_hash_hex,
            model_version,
            sampling,
            timestamp,
            nonce_hex,
            token_hash_hex,
        }
    }
}

/// Ergonomic builder; adopters populate fields and call `.mint()`.
#[derive(Debug, Default)]
pub struct ReplayTokenBuilder {
    effect_name: Option<String>,
    inputs: Option<Value>,
    outputs: Option<Value>,
    model_version: Option<String>,
    sampling: SamplingParams,
    timestamp: Option<DateTime<Utc>>,
    nonce: Option<[u8; 16]>,
}

impl ReplayTokenBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn effect_name(mut self, name: impl Into<String>) -> Self {
        self.effect_name = Some(name.into());
        self
    }
    pub fn inputs(mut self, v: Value) -> Self {
        self.inputs = Some(v);
        self
    }
    pub fn outputs(mut self, v: Value) -> Self {
        self.outputs = Some(v);
        self
    }
    pub fn model_version(mut self, s: impl Into<String>) -> Self {
        self.model_version = Some(s.into());
        self
    }
    pub fn sampling(mut self, s: SamplingParams) -> Self {
        self.sampling = s;
        self
    }
    pub fn timestamp(mut self, ts: DateTime<Utc>) -> Self {
        self.timestamp = Some(ts);
        self
    }
    pub fn nonce(mut self, bytes: [u8; 16]) -> Self {
        self.nonce = Some(bytes);
        self
    }

    pub fn mint(self) -> ReplayToken {
        let effect_name = self.effect_name.expect("effect_name required");
        let inputs = self.inputs.unwrap_or(Value::Null);
        let outputs = self.outputs.unwrap_or(Value::Null);
        let model_version = self.model_version.unwrap_or_else(|| "unset".into());
        let timestamp = self.timestamp.unwrap_or_else(Utc::now);
        let nonce = self.nonce.unwrap_or_else(generate_nonce);
        ReplayToken::mint(
            effect_name,
            inputs,
            outputs,
            model_version,
            self.sampling,
            timestamp,
            nonce,
        )
    }
}

// ── Canonical JSON hashing ──────────────────────────────────────────

/// Compute SHA-256 of `v` after canonical JSON encoding. The
/// canonical form sorts object keys recursively and omits optional
/// whitespace — identical to the §Fase 10.g audit-chain canonicaliser.
pub fn canonical_hash(v: &Value) -> [u8; 32] {
    let canonical = canonicalize(v);
    let mut h = Sha256::new();
    h.update(canonical.as_bytes());
    let out = h.finalize();
    let mut array = [0u8; 32];
    array.copy_from_slice(&out);
    array
}

/// RFC 8785-style canonical JSON — keys sorted, no whitespace,
/// ASCII-safe escapes. serde_json with `to_writer_pretty(false)`
/// already omits whitespace; key sorting we do here.
fn canonicalize(v: &Value) -> String {
    let sorted = sort_object_keys(v.clone());
    serde_json::to_string(&sorted).expect("canonical JSON encoding")
}

fn sort_object_keys(v: Value) -> Value {
    match v {
        Value::Object(map) => {
            // serde_json::Map preserves insertion order. Re-insert
            // in sorted order so the encoder emits sorted output.
            let mut entries: Vec<(String, Value)> = map.into_iter().collect();
            entries.sort_by(|a, b| a.0.cmp(&b.0));
            let mut sorted_map = serde_json::Map::new();
            for (k, inner) in entries {
                sorted_map.insert(k, sort_object_keys(inner));
            }
            Value::Object(sorted_map)
        }
        Value::Array(items) => {
            Value::Array(items.into_iter().map(sort_object_keys).collect())
        }
        other => other,
    }
}

fn derive_token_hash(
    effect_name: &str,
    inputs: &Value,
    outputs: &Value,
    model_version: &str,
    sampling: &SamplingParams,
    timestamp: DateTime<Utc>,
    nonce: &[u8; 16],
) -> [u8; 32] {
    const RS: u8 = 0x1E;
    let mut h = Sha256::new();
    h.update(effect_name.as_bytes());
    h.update([RS]);
    h.update(canonicalize(inputs).as_bytes());
    h.update([RS]);
    h.update(canonicalize(outputs).as_bytes());
    h.update([RS]);
    h.update(model_version.as_bytes());
    h.update([RS]);
    h.update(
        canonicalize(&serde_json::to_value(sampling).expect("sampling serialisable"))
            .as_bytes(),
    );
    h.update([RS]);
    h.update(timestamp.to_rfc3339().as_bytes());
    h.update([RS]);
    h.update(nonce);
    let out = h.finalize();
    let mut array = [0u8; 32];
    array.copy_from_slice(&out);
    array
}

fn generate_nonce() -> [u8; 16] {
    use rand::RngCore;
    let mut bytes = [0u8; 16];
    rand::rng().fill_bytes(&mut bytes);
    bytes
}

fn hex(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        out.push_str(&format!("{b:02x}"));
    }
    out
}

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

    fn fixed_nonce() -> [u8; 16] {
        [1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
    }

    fn fixed_timestamp() -> DateTime<Utc> {
        use chrono::TimeZone;
        Utc.with_ymd_and_hms(2026, 4, 22, 12, 0, 0).unwrap()
    }

    #[test]
    fn mint_sets_every_derived_hash() {
        let token = ReplayToken::mint(
            "call_tool:send_slack",
            json!({"channel": "#ops", "text": "hi"}),
            json!({"ok": true, "ts": "1700000000.000"}),
            "axon.builtin.slack.v1",
            SamplingParams::default(),
            fixed_timestamp(),
            fixed_nonce(),
        );
        assert_eq!(token.effect_name, "call_tool:send_slack");
        assert_eq!(token.inputs_hash_hex.len(), 64);
        assert_eq!(token.outputs_hash_hex.len(), 64);
        assert_eq!(token.token_hash_hex.len(), 64);
        assert_eq!(token.nonce_hex, "0102030405060708090a0b0c0d0e0f10");
    }

    #[test]
    fn canonical_hash_is_key_order_independent() {
        let a = json!({"a": 1, "b": 2, "c": 3});
        let b = json!({"c": 3, "a": 1, "b": 2});
        assert_eq!(canonical_hash(&a), canonical_hash(&b));
    }

    #[test]
    fn canonical_hash_propagates_to_nested_objects() {
        let a = json!({"outer": {"a": 1, "b": 2}});
        let b = json!({"outer": {"b": 2, "a": 1}});
        assert_eq!(canonical_hash(&a), canonical_hash(&b));
    }

    #[test]
    fn token_hash_is_deterministic_for_identical_inputs() {
        let nonce = fixed_nonce();
        let ts = fixed_timestamp();
        let inputs = json!({"prompt": "hi", "user_id": "u-1"});
        let outputs = json!({"text": "hello"});

        let t1 = ReplayToken::mint(
            "llm_infer",
            inputs.clone(),
            outputs.clone(),
            "claude-opus-4-7",
            SamplingParams {
                temperature: Some(0.7),
                top_p: Some(0.95),
                seed: Some(42),
                ..Default::default()
            },
            ts,
            nonce,
        );
        let t2 = ReplayToken::mint(
            "llm_infer",
            inputs,
            outputs,
            "claude-opus-4-7",
            SamplingParams {
                temperature: Some(0.7),
                top_p: Some(0.95),
                seed: Some(42),
                ..Default::default()
            },
            ts,
            nonce,
        );
        assert_eq!(t1.token_hash_hex, t2.token_hash_hex);
    }

    #[test]
    fn token_hash_differs_when_model_version_differs() {
        let t_old = ReplayToken::mint(
            "llm_infer",
            json!({"x": 1}),
            json!({"y": 2}),
            "claude-opus-4-7",
            SamplingParams::default(),
            fixed_timestamp(),
            fixed_nonce(),
        );
        let t_new = ReplayToken::mint(
            "llm_infer",
            json!({"x": 1}),
            json!({"y": 2}),
            "claude-opus-4-8",
            SamplingParams::default(),
            fixed_timestamp(),
            fixed_nonce(),
        );
        assert_ne!(t_old.token_hash_hex, t_new.token_hash_hex);
    }

    #[test]
    fn builder_works() {
        let t = ReplayTokenBuilder::new()
            .effect_name("db_read:customers")
            .inputs(json!({"where": {"id": 42}}))
            .outputs(json!({"name": "Acme"}))
            .model_version("axon.builtin.db_read.v1")
            .timestamp(fixed_timestamp())
            .nonce(fixed_nonce())
            .mint();
        assert_eq!(t.effect_name, "db_read:customers");
        assert_eq!(t.token_hash_hex.len(), 64);
    }

    #[test]
    fn random_nonce_differs_across_mints() {
        let t1 = ReplayTokenBuilder::new()
            .effect_name("x")
            .timestamp(fixed_timestamp())
            .mint();
        let t2 = ReplayTokenBuilder::new()
            .effect_name("x")
            .timestamp(fixed_timestamp())
            .mint();
        assert_ne!(t1.nonce_hex, t2.nonce_hex);
        assert_ne!(t1.token_hash_hex, t2.token_hash_hex);
    }
}