axon-lang 4.2.0

AXON — the formal cognitive language: a deterministic, proof-carrying AI runtime. Native Rust lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the runtime: typed channels (π-calculus mobility, capability extrusion), algebraic effects via Free Monad CPS handlers, lease kernel + reconcile loop, the Epistemic Security Kernel, Trust Types, Proof-Carrying Code (independently verifiable proof objects), and the closed-catalog extension mechanism. Crate publishes as `axon-lang`; library import is `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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! v2.40.0 — the result-memoization cache core.
//!
//! This is the production-hardened runtime behind the `cache` primitive. The
//! type checker (v2.40.0) already proved WHAT is safe to cache (a `pure` tool by
//! construction; a widened one only with a finite TTL); this module implements
//! HOW, with the properties a naïve cache omits and that cause real outages:
//!
//! - **Content-addressed, deploy-safe, tenant-isolated keys:** the key
//!   is a hash of `(tenant ‖ cache ‖ tool ‖ tool-declaration-fingerprint ‖
//!   output_type ‖ selected params)`. A redeploy that changes a tool changes
//!   its fingerprint → a new key → no stale cross-deploy hit; the tenant is a
//!   key component → no cross-tenant leak even if a backend mis-namespaces.
//! - **Single-flight:** concurrent misses for one key compute ONCE;
//!   the rest wait for that result (no thundering herd).
//! - **Provable-forever, never non-deterministic-forever:** enforced at
//!   compile time; the runtime simply honours the (optional) TTL.
//! - **Production hygiene:** errors are never cached; oversized values
//!   are not cached (never truncated into a wrong value); TTL expiry is
//!   *jittered* (deterministically, per key) so entries don't expire in a herd.
//!
//! The `CacheBackend` trait lets the enterprise inject a Redis (multi-replica)
//! tier; with none injected, the in-process tier is fully functional
//! single-replica.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use sha2::{Digest, Sha256};

use axon_frontend::ir_nodes::{IRCache, IRProgram, IRToolSpec};

/// Default cap on the in-process tier (entries), mirroring `IdempotencyStore`.
pub const DEFAULT_CAPACITY: usize = 10_000;
/// Default per-value size ceiling (bytes). An oversized result is simply not
/// cached — never truncated into a wrong value.
pub const DEFAULT_MAX_VALUE_BYTES: usize = 512 * 1024;

// ── Duration parsing (mirrors the lexer's `<n><unit>` Duration token) ────────

/// Parse a duration literal (`"10s"`, `"500ms"`, `"5m"`, `"2h"`, `"1d"`) to a
/// `Duration`. `None` for a malformed string (the lexer already guarantees the
/// shape for a `ttl:` field, so this is defence in depth).
pub fn parse_duration(s: &str) -> Option<Duration> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    let (num, unit): (&str, &str) = if let Some(p) = s.strip_suffix("ms") {
        (p, "ms")
    } else if let Some(p) = s.strip_suffix('s') {
        (p, "s")
    } else if let Some(p) = s.strip_suffix('m') {
        (p, "m")
    } else if let Some(p) = s.strip_suffix('h') {
        (p, "h")
    } else if let Some(p) = s.strip_suffix('d') {
        (p, "d")
    } else {
        return None;
    };
    let n: u64 = num.parse().ok()?;
    Some(match unit {
        "ms" => Duration::from_millis(n),
        "s" => Duration::from_secs(n),
        "m" => Duration::from_secs(n * 60),
        "h" => Duration::from_secs(n * 3600),
        "d" => Duration::from_secs(n * 86400),
        _ => return None,
    })
}

// ── Content-addressed key derivation ─────────────────────────────────

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

/// A length-prefixed hash component — length-prefixing makes element boundaries
/// forgery-proof (no value can fake a boundary, the v2.39.0 argv-hash discipline
/// strengthened with explicit lengths).
fn update_part(h: &mut Sha256, part: &str) {
    h.update((part.len() as u64).to_le_bytes());
    h.update(part.as_bytes());
}

/// The stable fingerprint of a tool's DECLARATION — a hash of its IR spec. A
/// redeploy that changes the tool's provider, effects, output type, or
/// parameters changes this, so a behaviour change can never serve a result
/// cached under the old behaviour.
pub fn tool_fingerprint(tool: &IRToolSpec) -> String {
    match serde_json::to_vec(tool) {
        Ok(bytes) => {
            let mut h = Sha256::new();
            h.update(&bytes);
            hex(&h.finalize())[..16].to_string()
        }
        Err(_) => "unfingerprintable".to_string(),
    }
}

/// Derive the content-addressed cache key. `key_args` are the selected
/// `(param_name, value)` pairs (the full bound set, or the `key:` subset).
pub fn derive_key(
    tenant: &str,
    cache_name: &str,
    tool_name: &str,
    tool_fingerprint: &str,
    output_type: &str,
    key_args: &[(String, String)],
) -> String {
    let mut h = Sha256::new();
    for part in [tenant, cache_name, tool_name, tool_fingerprint, output_type] {
        update_part(&mut h, part);
    }
    // Sort so argument order never changes the key.
    let mut sorted: Vec<&(String, String)> = key_args.iter().collect();
    sorted.sort();
    update_part(&mut h, &format!("__argc={}", sorted.len()));
    for (k, v) in sorted {
        update_part(&mut h, k);
        update_part(&mut h, v);
    }
    hex(&h.finalize())
}

// ── The backend trait + in-process tier ──────────────────────────────────────

/// A pluggable cache tier. `namespace` is the cache declaration's name so
/// `invalidate` can flush exactly one cache's entries. The enterprise injects a
/// Redis impl of this; the OSS default is [`InProcessCache`].
pub trait CacheBackend: Send + Sync {
    fn get(&self, namespace: &str, key: &str) -> Option<Vec<u8>>;
    fn put(&self, namespace: &str, key: &str, value: Vec<u8>, ttl: Option<Duration>);
    /// Flush every entry belonging to `namespace` (an `emit` on an
    /// `invalidate_on:` channel triggers this).
    fn invalidate(&self, namespace: &str);
}

struct Entry {
    value: Vec<u8>,
    expires_at: Option<Instant>,
    last_access: Instant,
}

struct State {
    entries: HashMap<(String, String), Entry>,
    capacity: usize,
    max_value_bytes: usize,
}

/// The OSS default single-replica tier: a bounded map with per-entry TTL
/// (jittered), LRU eviction, a value-size bound, and single-flight miss
/// coalescing via per-key locks.
pub struct InProcessCache {
    state: Mutex<State>,
    /// Per-key locks that serialise concurrent computers for the same key
    /// (single-flight, the design decision). Held only during a compute; opportunistically
    /// reclaimed when no computer references it.
    keylocks: Mutex<HashMap<(String, String), Arc<Mutex<()>>>>,
}

impl Default for InProcessCache {
    fn default() -> Self {
        Self::new(DEFAULT_CAPACITY, DEFAULT_MAX_VALUE_BYTES)
    }
}

impl InProcessCache {
    pub fn new(capacity: usize, max_value_bytes: usize) -> Self {
        InProcessCache {
            state: Mutex::new(State {
                entries: HashMap::new(),
                capacity: capacity.max(1),
                max_value_bytes,
            }),
            keylocks: Mutex::new(HashMap::new()),
        }
    }

    fn now() -> Instant {
        Instant::now()
    }

    /// Deterministic per-key jitter (0..=ttl/10) so entries sharing a TTL do
    /// NOT expire in a synchronised herd. Deterministic (derived from
    /// the key) — no RNG, reproducible, and still spreads expiries across keys.
    fn jitter(key: &str, ttl: Duration) -> Duration {
        let span = ttl.as_millis() as u64 / 10;
        if span == 0 {
            return Duration::ZERO;
        }
        let mut h = Sha256::new();
        h.update(key.as_bytes());
        let digest = h.finalize();
        let seed = u64::from_le_bytes(digest[..8].try_into().unwrap_or([0; 8]));
        Duration::from_millis(seed % (span + 1))
    }

    /// Single-flight compute-through: return a cached value, or compute it
    /// exactly once even under concurrent misses for the same key. A computed
    /// ERROR is propagated but NEVER cached.
    pub fn get_or_compute<F, E>(
        &self,
        namespace: &str,
        key: &str,
        ttl: Option<Duration>,
        compute: F,
    ) -> Result<Vec<u8>, E>
    where
        F: FnOnce() -> Result<Vec<u8>, E>,
    {
        if let Some(v) = self.get(namespace, key) {
            return Ok(v);
        }
        // Acquire (or create) the per-key lock and serialise computers for it.
        let keylock = {
            let mut locks = self.keylocks.lock().unwrap();
            locks
                .entry((namespace.to_string(), key.to_string()))
                .or_insert_with(|| Arc::new(Mutex::new(())))
                .clone()
        };
        let _flight = keylock.lock().unwrap();
        // Re-check under the flight lock: a peer may have filled it.
        if let Some(v) = self.get(namespace, key) {
            self.reclaim_keylock(namespace, key, &keylock);
            return Ok(v);
        }
        let result = compute();
        if let Ok(ref value) = result {
            self.put(namespace, key, value.clone(), ttl);
        }
        drop(_flight);
        self.reclaim_keylock(namespace, key, &keylock);
        result
    }

    /// Drop the per-key lock from the map once no other computer references it
    /// (strong_count == 2: the map's + our local clone).
    fn reclaim_keylock(&self, namespace: &str, key: &str, held: &Arc<Mutex<()>>) {
        let mut locks = self.keylocks.lock().unwrap();
        if Arc::strong_count(held) <= 2 {
            locks.remove(&(namespace.to_string(), key.to_string()));
        }
    }

    /// Current entry count (test/introspection).
    pub fn len(&self) -> usize {
        self.state.lock().unwrap().entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl CacheBackend for InProcessCache {
    fn get(&self, namespace: &str, key: &str) -> Option<Vec<u8>> {
        let mut st = self.state.lock().unwrap();
        let k = (namespace.to_string(), key.to_string());
        let expired = match st.entries.get(&k) {
            Some(e) => e.expires_at.map(|t| Self::now() >= t).unwrap_or(false),
            None => return None,
        };
        if expired {
            st.entries.remove(&k);
            return None;
        }
        let now = Self::now();
        let e = st.entries.get_mut(&k)?;
        e.last_access = now;
        Some(e.value.clone())
    }

    fn put(&self, namespace: &str, key: &str, value: Vec<u8>, ttl: Option<Duration>) {
        let mut st = self.state.lock().unwrap();
        // the design decision — an oversized value is simply not cached.
        if value.len() > st.max_value_bytes {
            return;
        }
        // LRU eviction when at capacity (and not overwriting an existing key).
        let k = (namespace.to_string(), key.to_string());
        if st.entries.len() >= st.capacity && !st.entries.contains_key(&k) {
            if let Some(oldest) = st
                .entries
                .iter()
                .min_by_key(|(_, e)| e.last_access)
                .map(|(k, _)| k.clone())
            {
                st.entries.remove(&oldest);
            }
        }
        let expires_at = ttl.map(|d| Self::now() + d + Self::jitter(key, d));
        st.entries.insert(
            k,
            Entry {
                value,
                expires_at,
                last_access: Self::now(),
            },
        );
    }

    fn invalidate(&self, namespace: &str) {
        let mut st = self.state.lock().unwrap();
        st.entries.retain(|(ns, _), _| ns != namespace);
    }
}

// ── Policy resolution (which cache governs a tool) ───────────────────────────

/// Resolve which `cache` (if any) governs a tool's memoization, given the whole
/// program IR. Precedence: an explicit `cache: none` opts out; an
/// explicit `cache: <Name>` selects that cache; otherwise the single
/// `default: true` cache applies IFF the tool is eligible (provably `pure`, or
/// its effects are a subset of the default's `apply_to_effects`). Returns
/// `None` when nothing caches the tool.
pub fn resolve_tool_cache<'a>(ir: &'a IRProgram, tool: &IRToolSpec) -> Option<&'a IRCache> {
    // Explicit opt-out.
    if tool.cache == "none" {
        return None;
    }
    // Explicit reference.
    if !tool.cache.is_empty() {
        return ir.caches.iter().find(|c| c.name == tool.cache);
    }
    // Module default (if exactly one and the tool is eligible).
    let default = ir.caches.iter().find(|c| c.default_policy)?;
    let apply: Vec<String> = if default.apply_to_effects.is_empty() {
        vec!["pure".to_string()]
    } else {
        default
            .apply_to_effects
            .iter()
            .map(|e| e.split_once(':').map(|(b, _)| b.to_string()).unwrap_or_else(|| e.clone()))
            .collect()
    };
    // The tool's effect row (IR lowers effects with an optional `epistemic:`
    // suffix; compare on the base).
    let eligible = !tool.effect_row.is_empty()
        && tool.effect_row.iter().all(|e| {
            let base = e.split_once(':').map(|(b, _)| b).unwrap_or(e.as_str());
            apply.iter().any(|a| a == base)
        });
    if eligible {
        Some(default)
    } else {
        None
    }
}

// ── v2.89.0 — resolution, hoisted to plan-build time ────────────────────

/// Everything the dispatch path needs to key ONE memoised call, resolved from
/// the `IRProgram` once, when the plan is built.
///
/// # Why this type exists
///
/// [`CacheRuntime::dispatch`] took `&IRProgram` because [`resolve_tool_cache`]
/// needs the whole module to answer "which cache governs this tool?" — the
/// default-policy rule is a property of the module, not of the tool. But
/// `DispatchCtx` carries no `IRProgram`, deliberately: it carries narrow,
/// pre-resolved catalogs (`credentials`, `anchors`, the v2.69.0 shield policies)
/// so the hot path looks nothing up that could have been looked up once.
///
/// Threading the IR through the runtime to satisfy one call would have inverted
/// that, and for no gain: the answer cannot change between the deploy and the
/// call. So resolution moves to where the IR already is, and the runtime
/// receives the answer.
///
/// This is the v2.69.0 `collect_shield_policies` shape, and it is also the v2.87.0
/// discipline — [`resolve_tool_cache`] stays the ONE place that decides, and
/// gains a second caller rather than a second copy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedCachePolicy {
    /// The governing cache's name — also the backend NAMESPACE, so `invalidate`
    /// flushes exactly this cache's entries.
    pub cache_name: String,
    /// Raw TTL literal (`"5m"`); `None` ⇒ cache-forever, sound only because
    /// `axon-T865` proved the memoised thing deterministic.
    pub ttl: Option<String>,
    /// The `key:` subset; empty ⇒ every bound argument keys the entry.
    pub key_params: Vec<String>,
    /// The declaration fingerprint — a redeploy that changes what is
    /// memoised changes this, so a behaviour change can never be served a
    /// result cached under the old behaviour.
    pub fingerprint: String,
    /// Part of the key so two tools with identical arguments but different
    /// result types never collide.
    pub output_type: String,
}

impl ResolvedCachePolicy {
    fn from_parts(cache: &IRCache, fingerprint: String, output_type: String) -> Self {
        ResolvedCachePolicy {
            cache_name: cache.name.clone(),
            ttl: cache.ttl.clone(),
            key_params: cache.key_params.clone(),
            fingerprint,
            output_type,
        }
    }

    /// v2.89.0 — the policy governing a `retrieve … cache: <Name>`.
    ///
    /// A retrieve names its cache directly, so there is no eligibility question
    /// to resolve — but it still needs a fingerprint, and the honest one is the
    /// STORE it reads. A redeploy that changes the store's shape must not serve
    /// rows cached against the old one, exactly as a changed tool declaration
    /// must not. `axon-T865` already forces a finite `ttl:` here,
    /// because a store read is never `pure`.
    pub fn for_retrieve(cache: &IRCache, store_name: &str) -> Self {
        let mut h = Sha256::new();
        update_part(&mut h, "retrieve");
        update_part(&mut h, store_name);
        Self::from_parts(
            cache,
            hex(&h.finalize())[..16].to_string(),
            String::new(),
        )
    }
}

/// Resolve, for every tool in the program, which cache (if any) memoises it —
/// keyed by TOOL NAME, which is how the dispatch path knows a tool.
///
/// Empty for a program with no `cache` declaration, which is the overwhelming
/// majority: an absent entry is the same "not memoised" answer
/// [`resolve_tool_cache`] gives, so a cache-less program pays one failed hash
/// lookup per tool call and behaves byte-identically to pre-v2.89.0.
pub fn resolve_tool_cache_policies(ir: &IRProgram) -> HashMap<String, ResolvedCachePolicy> {
    let mut out = HashMap::new();
    if ir.caches.is_empty() {
        return out;
    }
    for tool in &ir.tools {
        if let Some(cache) = resolve_tool_cache(ir, tool) {
            out.insert(
                tool.name.clone(),
                ResolvedCachePolicy::from_parts(
                    cache,
                    tool_fingerprint(tool),
                    tool.output_type.clone().unwrap_or_default(),
                ),
            );
        }
    }
    out
}

/// v2.89.0 — **everything a deployment memoises**, resolved once from the
/// `IRProgram`.
///
/// # Why one struct and not three parameters
///
/// The three maps are not independent knobs; they are one answer to "what does
/// this program memoise?". A caller that supplied two of them would get a
/// runtime that caches but never invalidates, or one that flushes a namespace
/// nothing writes to — silent wrong answers, both, and the shape that makes
/// them reachable is a builder with three setters.
///
/// Bundled, the only way to half-wire the cache is not to wire it at all, which
/// is the honest `None` default. This is the v2.87.0 lesson expressed as an API:
/// make the second door impossible rather than remembering to walk through it.
///
/// Travels the same route as `scopes`, `observables` and `credentials` — built
/// where the IR is, passed down as a resolved catalog, so the hot path looks
/// nothing up that could have been looked up once.
#[derive(Debug, Clone, Default)]
pub struct CachePlan {
    /// Tool name → the policy memoising it.
    pub tool_policies: HashMap<String, ResolvedCachePolicy>,
    /// Cache name → declaration, for `retrieve … cache:` and namespace flushes.
    pub caches: HashMap<String, IRCache>,
    /// Channel name → the namespaces an `emit` on it flushes.
    pub invalidation_channels: HashMap<String, Vec<String>>,
}

impl CachePlan {
    /// Resolve the whole plan from a compiled program.
    pub fn from_ir(ir: &IRProgram) -> Self {
        CachePlan {
            tool_policies: resolve_tool_cache_policies(ir),
            caches: ir
                .caches
                .iter()
                .map(|c| (c.name.clone(), c.clone()))
                .collect(),
            invalidation_channels: resolve_invalidation_channels(ir),
        }
    }

    /// `true` for a program that declares no `cache` at all — the overwhelming
    /// majority, and the case where attaching a runtime buys nothing.
    pub fn is_empty(&self) -> bool {
        self.caches.is_empty()
    }
}

/// Channel name → the cache namespaces an `emit` on it flushes (`invalidate_on:`).
///
/// Inverted at plan-build time so `run_emit` answers "does this emit invalidate
/// anything?" with one hash lookup instead of scanning every cache declaration
/// on every emit. Empty ⇒ no cache in this program declares `invalidate_on:`,
/// and the emit path is byte-identical to pre-v2.89.0.
pub fn resolve_invalidation_channels(ir: &IRProgram) -> HashMap<String, Vec<String>> {
    let mut out: HashMap<String, Vec<String>> = HashMap::new();
    for cache in &ir.caches {
        for channel in &cache.invalidate_on {
            out.entry(channel.clone())
                .or_default()
                .push(cache.name.clone());
        }
    }
    out
}

// ── Integration layer (the one seam the runner calls) ───────────────────────

/// Ties policy resolution + content-addressed key derivation + single-flight
/// compute-through into one call the dispatch path makes per tool. The
/// enterprise injects a Redis `backend` + the real `tenant`; the OSS default is
/// an in-process backend under a `"local"` tenant. This is the whole runtime
/// contract for v2.40.0 — a hit returns before `compute` runs (so a budget gate
/// placed after the lookup never sees it, the design decision).
pub struct CacheRuntime {
    backend: Arc<dyn CacheBackend>,
    tenant: String,
}

/// The outcome of a cache-mediated dispatch — lets the caller emit the right
/// `cache:hit` / `cache:miss` audit signal without re-deriving it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheOutcome {
    Hit(Vec<u8>),
    Miss(Vec<u8>),
    /// The tool is not cache-eligible; carries the freshly computed value so
    /// the caller uses it exactly as it would a `Miss`, minus the audit signal.
    Uncached(Vec<u8>),
}

impl CacheOutcome {
    /// The result value, regardless of hit/miss/uncached.
    pub fn value(&self) -> &[u8] {
        match self {
            CacheOutcome::Hit(v) | CacheOutcome::Miss(v) | CacheOutcome::Uncached(v) => v,
        }
    }
}

/// v2.89.0 — a reserved place to put a computed value, handed out by
/// [`CacheRuntime::probe`] on a miss and consumed by [`CacheRuntime::store`].
///
/// It carries the derived key rather than the inputs, so the value is stored
/// under the key the lookup missed on — a caller cannot accidentally store
/// against a key derived from arguments that changed in between.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheSlot {
    namespace: String,
    key: String,
    ttl: Option<Duration>,
}

/// v2.89.0 — what a pre-dispatch probe found.
///
/// The three arms are the three different things a caller must do, which is why
/// this is not an `Option<Vec<u8>>`: "nothing memoises this call" and
/// "memoised, but absent" look identical to an `Option` and are not the same
/// fact — the first stores nothing afterwards, the second must.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheProbe {
    /// No policy governs this call. Dispatch normally; store nothing.
    NotCached,
    /// A memoised value. **Do not dispatch, and do not charge a budget** — that
    /// ordering is the design decision, and it is the caller's to honour.
    Hit(Vec<u8>),
    /// Memoised but absent. Dispatch, then hand the value to
    /// [`CacheRuntime::store`] with this slot.
    Miss(CacheSlot),
}

impl CacheRuntime {
    pub fn new(backend: Arc<dyn CacheBackend>, tenant: impl Into<String>) -> Self {
        CacheRuntime {
            backend,
            tenant: tenant.into(),
        }
    }

    /// In-process, single-tenant default (OSS runtime with no injected tier).
    pub fn in_process() -> Self {
        Self::new(Arc::new(InProcessCache::default()), "local")
    }

    /// v2.89.0 — the OSS runtime a production flow run gets: the
    /// **process-wide** in-process tier, keyed to **this run's tenant**.
    ///
    /// # Why the split, and what each half prevents
    ///
    /// This looks like a detail and is the difference between a cache and a
    /// decoration.
    ///
    /// Build the whole `CacheRuntime` per flow run and the backend is empty
    /// every time: a tool called once per run — which is most tools — never
    /// hits anything, and v2.89.0 would ship a memoiser that memoises within a
    /// single run and forgets between them. Wired, tested, and worthless.
    ///
    /// Share the whole `CacheRuntime` across runs and it is worse than
    /// worthless. `tenant` is a field of the runtime, not a parameter of the
    /// call, so one shared instance would key every tenant's results under
    /// whichever tenant built it first — and the design decision puts the tenant IN the key
    /// precisely so that a mis-namespacing backend still cannot leak. A shared
    /// runtime with a fixed tenant defeats that from above the backend, where
    /// the key is derived.
    ///
    /// So the BACKEND is process-wide (entries survive between runs, which is
    /// what makes it a cache) and the TENANT comes from the run (which is what
    /// keeps them apart). Constructing this is two `Arc` clones.
    ///
    /// The enterprise v2.40.0 Redis tier replaces the backend here and inherits
    /// the same discipline unchanged — it is a different `CacheBackend`, not a
    /// different call site.
    pub fn process_local(tenant: impl Into<String>) -> Self {
        static TIER: std::sync::OnceLock<Arc<InProcessCache>> = std::sync::OnceLock::new();
        let backend = TIER.get_or_init(|| Arc::new(InProcessCache::default()));
        Self::new(backend.clone(), tenant)
    }

    /// Look up (or compute-and-store) a tool result. `args` is the full bound
    /// `(name, value)` set; the `key:` subset (if any) is applied here.
    /// `compute` runs ONLY on a miss and its error is never cached.
    pub fn dispatch<F, E>(
        &self,
        ir: &IRProgram,
        tool: &IRToolSpec,
        args: &[(String, String)],
        compute: F,
    ) -> Result<CacheOutcome, E>
    where
        F: FnOnce() -> Result<Vec<u8>, E>,
    {
        // v2.89.0 — resolution and execution split, so the dispatch path can
        // supply a policy resolved once at plan-build time (see
        // [`ResolvedCachePolicy`]). This entry point keeps its `&IRProgram`
        // signature and resolves on the spot; both routes run the SAME body
        // below, so there is one memoisation law and two ways to reach it —
        // never two laws.
        let policy = resolve_tool_cache(ir, tool).map(|cache| {
            ResolvedCachePolicy::from_parts(
                cache,
                tool_fingerprint(tool),
                tool.output_type.clone().unwrap_or_default(),
            )
        });
        self.dispatch_resolved(policy.as_ref(), &tool.name, args, compute)
    }

    /// v2.89.0 — the memoisation body, against an already-resolved policy.
    ///
    /// `subject` is what the entry is keyed to — a tool's name, or a store's
    /// name for a `retrieve`. `policy: None` means "nothing memoises this":
    /// `compute` runs and the value comes back as [`CacheOutcome::Uncached`],
    /// which the caller uses exactly as a `Miss` minus the audit signal.
    ///
    /// # The ordering that the design decision rests on
    ///
    /// A hit returns BEFORE `compute` is called. That is not an optimisation,
    /// it is the guarantee: the caller places its budget charge inside
    /// `compute`'s caller, so a hit cannot decrement a `budget { rate: … }`
    /// quota. v2.40.0's plan calls this *"structurally guaranteed by ordering the
    /// cache lookup before the budget gate"* — the structure is right here.
    pub fn dispatch_resolved<F, E>(
        &self,
        policy: Option<&ResolvedCachePolicy>,
        subject: &str,
        args: &[(String, String)],
        compute: F,
    ) -> Result<CacheOutcome, E>
    where
        F: FnOnce() -> Result<Vec<u8>, E>,
    {
        match self.probe(policy, subject, args) {
            CacheProbe::NotCached => compute().map(CacheOutcome::Uncached),
            CacheProbe::Hit(v) => Ok(CacheOutcome::Hit(v)),
            CacheProbe::Miss(slot) => {
                let value = compute()?;
                self.store(&slot, value.clone());
                Ok(CacheOutcome::Miss(value))
            }
        }
    }

    /// v2.89.0 — **look, without computing.**
    ///
    /// # Why the seam had to split
    ///
    /// [`dispatch_resolved`](Self::dispatch_resolved) takes a `compute` closure,
    /// which is the right shape when the work is synchronous and owns nothing.
    /// The real tool-call path is neither: the work between the lookup and the
    /// value is `async`, it borrows `&mut DispatchCtx`, and it is not one call
    /// but four in sequence — the budget charge, the lease charge, the
    /// concurrency permit, then the vendor dispatch. None of that fits inside
    /// an `FnOnce() -> Result<Vec<u8>, E>`, and contorting it to fit would have
    /// meant either blocking the executor or duplicating the memoisation law at
    /// the call site.
    ///
    /// So the law splits into the two moments an async caller actually has:
    /// probe before the work, store after it. `dispatch_resolved` is now
    /// implemented in terms of these, so the synchronous seam v2.40.0 designed and
    /// the asynchronous one v2.89.0 needed run the SAME key derivation, the same
    /// TTL parse and the same namespace — one law, two ways in.
    ///
    /// **The ordering the design decision rests on is the caller's to keep**: a
    /// [`CacheProbe::Hit`] means the call must not be dispatched AND no budget
    /// charged. Returning early on a hit is what makes "a hit never consumes a
    /// quota" structural rather than hopeful, and it is asserted from a real
    /// deploy in `cache_hits.rs`.
    pub fn probe(
        &self,
        policy: Option<&ResolvedCachePolicy>,
        subject: &str,
        args: &[(String, String)],
    ) -> CacheProbe {
        let Some(policy) = policy else {
            return CacheProbe::NotCached;
        };
        // Apply the `key:` subset (empty ⇒ all args).
        let key_args: Vec<(String, String)> = if policy.key_params.is_empty() {
            args.to_vec()
        } else {
            args.iter()
                .filter(|(k, _)| policy.key_params.contains(k))
                .cloned()
                .collect()
        };
        let key = derive_key(
            &self.tenant,
            &policy.cache_name,
            subject,
            &policy.fingerprint,
            &policy.output_type,
            &key_args,
        );
        if let Some(v) = self.backend.get(&policy.cache_name, &key) {
            return CacheProbe::Hit(v);
        }
        CacheProbe::Miss(CacheSlot {
            namespace: policy.cache_name.clone(),
            key,
            ttl: policy.ttl.as_deref().and_then(parse_duration),
        })
    }

    /// v2.89.0 — fill the slot a [`CacheProbe::Miss`] reserved.
    ///
    /// Errors are never stored — that is the caller's decision, and it
    /// is expressed by simply not calling this. Oversized values are dropped by
    /// the backend, never truncated into a wrong value.
    pub fn store(&self, slot: &CacheSlot, value: Vec<u8>) {
        self.backend.put(&slot.namespace, &slot.key, value, slot.ttl);
    }

    /// Flush a cache namespace (called when an `emit` fires on one of its
    /// `invalidate_on:` channels).
    pub fn invalidate(&self, cache_name: &str) {
        self.backend.invalidate(cache_name);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc as StdArc;

    fn ir_from(src: &str) -> IRProgram {
        let toks = axon_frontend::lexer::Lexer::new(src, "<cache-test>")
            .tokenize()
            .unwrap();
        let prog = axon_frontend::parser::Parser::new(toks).parse().unwrap();
        axon_frontend::ir_generator::IRGenerator::new().generate(&prog)
    }

    const CACHE_PROG: &str = concat!(
        "flow F() -> Unit { step S { ask: \"hi\" } }\n",
        "tool Enrich { provider: http effects: <pure> output_type: Report parameters: { id: String } }\n",
        "cache DefaultPure { default: true }\n",
    );

    #[test]
    fn end_to_end_pure_tool_second_call_is_a_hit() {
        let ir = ir_from(CACHE_PROG);
        let tool = ir.tools.iter().find(|t| t.name == "Enrich").unwrap();
        let rt = CacheRuntime::in_process();
        let computes = StdArc::new(AtomicUsize::new(0));
        let args = vec![("id".to_string(), "42".to_string())];

        let call = || {
            let computes = computes.clone();
            rt.dispatch::<_, ()>(&ir, tool, &args, || {
                computes.fetch_add(1, Ordering::SeqCst);
                Ok(b"enriched".to_vec())
            })
        };
        // First call → miss (computes once).
        assert_eq!(call().unwrap(), CacheOutcome::Miss(b"enriched".to_vec()));
        // Second call, same args → hit (no recompute).
        assert_eq!(call().unwrap(), CacheOutcome::Hit(b"enriched".to_vec()));
        assert_eq!(computes.load(Ordering::SeqCst), 1, "pure tool computed once");

        // A different arg value → a fresh miss (distinct content-addressed key).
        let args2 = vec![("id".to_string(), "99".to_string())];
        let out = rt
            .dispatch::<_, ()>(&ir, tool, &args2, || Ok(b"other".to_vec()))
            .unwrap();
        assert_eq!(out, CacheOutcome::Miss(b"other".to_vec()));
    }

    #[test]
    fn ineligible_tool_is_uncached() {
        // A network tool with no cache reference and no covering default.
        let ir = ir_from(concat!(
            "flow F() -> Unit { step S { ask: \"hi\" } }\n",
            "tool Fetch { provider: http effects: <network> parameters: { url: String } }\n",
        ));
        let tool = ir.tools.iter().find(|t| t.name == "Fetch").unwrap();
        let rt = CacheRuntime::in_process();
        let out = rt
            .dispatch::<_, ()>(&ir, tool, &[], || Ok(b"x".to_vec()))
            .unwrap();
        assert_eq!(out, CacheOutcome::Uncached(b"x".to_vec()));
    }

    #[test]
    fn invalidate_forces_recompute() {
        let ir = ir_from(CACHE_PROG);
        let tool = ir.tools.iter().find(|t| t.name == "Enrich").unwrap();
        let rt = CacheRuntime::in_process();
        let args = vec![("id".to_string(), "1".to_string())];
        rt.dispatch::<_, ()>(&ir, tool, &args, || Ok(b"v1".to_vec())).unwrap();
        rt.invalidate("DefaultPure");
        let out = rt
            .dispatch::<_, ()>(&ir, tool, &args, || Ok(b"v2".to_vec()))
            .unwrap();
        assert_eq!(out, CacheOutcome::Miss(b"v2".to_vec()), "invalidated → recompute");
    }

    #[test]
    fn duration_parsing() {
        assert_eq!(parse_duration("10s"), Some(Duration::from_secs(10)));
        assert_eq!(parse_duration("500ms"), Some(Duration::from_millis(500)));
        assert_eq!(parse_duration("5m"), Some(Duration::from_secs(300)));
        assert_eq!(parse_duration("2h"), Some(Duration::from_secs(7200)));
        assert_eq!(parse_duration("1d"), Some(Duration::from_secs(86400)));
        assert_eq!(parse_duration("bogus"), None);
    }

    #[test]
    fn key_is_content_addressed_and_deploy_safe() {
        let args = vec![("city".to_string(), "London".to_string())];
        let base = derive_key("t1", "C", "Weather", "fp1", "Out", &args);
        // Same everything → same key.
        assert_eq!(base, derive_key("t1", "C", "Weather", "fp1", "Out", &args));
        // Different tenant → different key (the design decision isolation in the key).
        assert_ne!(base, derive_key("t2", "C", "Weather", "fp1", "Out", &args));
        // Different tool fingerprint (a redeploy) → different key.
        assert_ne!(base, derive_key("t1", "C", "Weather", "fp2", "Out", &args));
        // Different arg value → different key.
        let args2 = vec![("city".to_string(), "Paris".to_string())];
        assert_ne!(base, derive_key("t1", "C", "Weather", "fp1", "Out", &args2));
    }

    #[test]
    fn arg_order_does_not_change_key() {
        let a = vec![("a".to_string(), "1".to_string()), ("b".to_string(), "2".to_string())];
        let b = vec![("b".to_string(), "2".to_string()), ("a".to_string(), "1".to_string())];
        assert_eq!(
            derive_key("t", "C", "T", "fp", "O", &a),
            derive_key("t", "C", "T", "fp", "O", &b)
        );
    }

    #[test]
    fn arg_boundaries_are_forgery_proof() {
        // ("ab","c") vs ("a","bc") must NOT collide (length-prefixing).
        let a = vec![("ab".to_string(), "c".to_string())];
        let b = vec![("a".to_string(), "bc".to_string())];
        assert_ne!(
            derive_key("t", "C", "T", "fp", "O", &a),
            derive_key("t", "C", "T", "fp", "O", &b)
        );
    }

    #[test]
    fn hit_returns_stored_value() {
        let c = InProcessCache::default();
        c.put("C", "k", b"value".to_vec(), None);
        assert_eq!(c.get("C", "k"), Some(b"value".to_vec()));
        assert_eq!(c.get("C", "missing"), None);
    }

    #[test]
    fn ttl_expiry_evicts() {
        let c = InProcessCache::default();
        c.put("C", "k", b"v".to_vec(), Some(Duration::from_millis(1)));
        std::thread::sleep(Duration::from_millis(30));
        assert_eq!(c.get("C", "k"), None, "expired entry must be gone");
    }

    #[test]
    fn invalidate_flushes_only_its_namespace() {
        let c = InProcessCache::default();
        c.put("A", "k", b"1".to_vec(), None);
        c.put("B", "k", b"2".to_vec(), None);
        c.invalidate("A");
        assert_eq!(c.get("A", "k"), None);
        assert_eq!(c.get("B", "k"), Some(b"2".to_vec()), "other cache untouched");
    }

    #[test]
    fn oversized_value_is_not_cached() {
        let c = InProcessCache::new(10, 4);
        c.put("C", "k", vec![0u8; 100], None);
        assert_eq!(c.get("C", "k"), None, "oversized value must not be cached");
    }

    #[test]
    fn capacity_evicts_lru() {
        let c = InProcessCache::new(2, DEFAULT_MAX_VALUE_BYTES);
        c.put("C", "a", b"1".to_vec(), None);
        c.put("C", "b", b"2".to_vec(), None);
        let _ = c.get("C", "a"); // touch a → b is now LRU
        c.put("C", "c", b"3".to_vec(), None); // evicts b
        assert_eq!(c.get("C", "a"), Some(b"1".to_vec()));
        assert_eq!(c.get("C", "b"), None, "LRU entry evicted");
        assert_eq!(c.get("C", "c"), Some(b"3".to_vec()));
    }

    #[test]
    fn errors_are_never_cached() {
        let c = InProcessCache::default();
        let r: Result<Vec<u8>, &str> =
            c.get_or_compute("C", "k", None, || Err("boom"));
        assert!(r.is_err());
        assert_eq!(c.get("C", "k"), None, "a computed error must not be cached");
    }

    #[test]
    fn single_flight_coalesces_concurrent_misses() {
        let c = StdArc::new(InProcessCache::default());
        let computes = StdArc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();
        for _ in 0..16 {
            let c = c.clone();
            let computes = computes.clone();
            handles.push(std::thread::spawn(move || {
                c.get_or_compute::<_, ()>("C", "hot", None, || {
                    computes.fetch_add(1, Ordering::SeqCst);
                    std::thread::sleep(Duration::from_millis(20));
                    Ok(b"result".to_vec())
                })
                .unwrap()
            }));
        }
        for h in handles {
            assert_eq!(h.join().unwrap(), b"result".to_vec());
        }
        assert_eq!(
            computes.load(Ordering::SeqCst),
            1,
            "single-flight: concurrent misses for one key compute exactly once"
        );
    }
}