axon-lang 2.11.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
//! Tool registry — extensible tool dispatch for AXON execution.
//!
//! The `ToolRegistry` collects tool definitions from two sources:
//!   1. Built-in tools: Calculator, DateTimeTool (always available)
//!   2. Program-defined tools: declared via `tool Name { ... }` in .axon files
//!
//! When a `use_tool` step fires, the runner queries the registry:
//!   - Built-in tools execute natively (no LLM call)
//!   - Program-defined tools with known providers execute via provider adapters
//!   - Unknown tools fall through to LLM dispatch
//!
//! Provider adapters:
//!   - "native"  → built-in Calculator/DateTimeTool
//!   - "stub"    → returns a stub response (for testing/development)
//!   - "http"    → REST endpoint via reqwest (URL in runtime field)
//!   - "mcp"     → ℰMCP transducer (JSON-RPC 2.0 + blame + taint)
//!   - others    → fall through to LLM (future: gRPC, etc.)

use std::collections::HashMap;

use crate::emcp;
use crate::http_tool;
use crate::ir_nodes::IRToolSpec;
use crate::tool_executor::{self, ToolResult};

// ── Tool entry ─────────────────────────────────────────────────────────────

/// A registered tool with its metadata and dispatch configuration.
#[derive(Debug, Clone)]
pub struct ToolEntry {
    pub name: String,
    pub provider: String,
    pub timeout: String,
    pub runtime: String,
    pub sandbox: Option<bool>,
    pub max_results: Option<i64>,
    pub output_schema: String,
    pub effect_row: Vec<String>,
    /// §Fase 58.f.2 — the tool's typed INPUT SCHEMA (D1) as resolved
    /// `(param_name, type_name)` pairs, populated from
    /// `IRToolSpec.parameters` at [`ToolRegistry::register_from_ir`].
    /// The streaming dispatcher's `run_use_tool` reads this to coerce
    /// each `use Tool(k = v, …)` arg to its declared JSON type — the
    /// SAME `coerce_tool_arg_value` discipline the synchronous server
    /// path (§58.e/58.f) applies via `CompiledStep.tool_param_types`.
    /// Empty for a schema-less tool (D5) and for the built-ins.
    pub parameters: Vec<(String, String)>,
    pub source: ToolSource,
    /// §Fase 34.c (v1.29.0) — Whether this tool is a stream
    /// producer. Auto-derived at registration time from
    /// `effect_row` via [`derive_is_streaming`] when the tool comes
    /// from the IR (`register_from_ir`). Adopters programmatically
    /// registering tools via [`ToolRegistry::register`] set this
    /// flag explicitly (or use [`derive_is_streaming`] for the
    /// canonical rule).
    ///
    /// The dispatcher's `pure_shape::run_step` (Fase 34.d) reads
    /// this flag to decide whether to route through the streaming
    /// path (`tool.stream(args, ctx)`) or the synchronous path
    /// (`tool.execute(args, ctx)`). Built-in tools default to
    /// `false`; tools declaring `effects: <stream:<policy>>` in
    /// their AST get `true` automatically.
    pub is_streaming: bool,
}

/// §Fase 34.c (v1.29.0) — Canonical derivation rule for the
/// [`ToolEntry::is_streaming`] field.
///
/// A tool is a stream producer iff at least one entry in its
/// `effect_row` begins with the `stream:` slug prefix. This is the
/// AST-level structural signal the paper §3-§6 defines:
/// `effects: <stream:<policy>>` on a tool declaration means "this
/// tool is a stream producer with backpressure policy ⟨policy⟩".
///
/// The closed-catalog `<stream:<policy>>` payloads are
/// `{drop_oldest, degrade_quality, pause_upstream, fail}` per
/// Fase 33.e; new policies require a deliberate sub-fase. The
/// derivation rule itself is policy-agnostic — any `stream:` slug
/// flags the tool as a stream producer.
///
/// # Cross-stack contract (D10)
///
/// The Python mirror lives in `axon.runtime.tools.streaming`
/// (Fase 34.b). Both stacks check the same prefix predicate; the
/// drift gate `tests/test_fase34_c_registry_drift_cross_stack.py`
/// pins the 1-to-1 contract.
pub fn derive_is_streaming(effect_row: &[String]) -> bool {
    effect_row.iter().any(|e| e.starts_with("stream:"))
}

/// §Fase 58.g — resolve a tool's declared `runtime` into a concrete
/// dispatch URL against a per-tenant / per-server **base URL** (D7).
///
/// The resolution rule (config-driven provider→endpoint, never
/// hardcoded in the compiler):
///
/// - An ALREADY-ABSOLUTE `runtime` (`http://…` / `https://…`) is used
///   verbatim — the program pinned its own endpoint (D5 back-compat).
/// - Otherwise the declared `runtime` is treated as a **slug / path**
///   and joined onto `base_url`: `{base}/{slug}`. An empty `runtime`
///   falls back to the tool's name as the slug, so a `tool Crm {
///   provider: http }` with no `runtime:` resolves to `{base}/Crm`.
/// - An empty `base_url` is a no-op (returns `runtime` unchanged) — the
///   adopter hasn't wired a tool-server, so a relative runtime stays
///   relative and the dispatcher surfaces the actionable "no/invalid
///   endpoint URL" diagnostic.
///
/// Leading/trailing slashes are normalised so the join never produces a
/// `//` or a missing separator.
pub fn resolve_tool_endpoint(runtime: &str, tool_name: &str, base_url: &str) -> String {
    let rt = runtime.trim();
    if rt.starts_with("http://") || rt.starts_with("https://") {
        return runtime.to_string();
    }
    let base = base_url.trim().trim_end_matches('/');
    if base.is_empty() {
        return runtime.to_string();
    }
    let slug = if rt.is_empty() { tool_name } else { rt };
    let slug = slug.trim_start_matches('/');
    if slug.is_empty() {
        base.to_string()
    } else {
        format!("{base}/{slug}")
    }
}

/// Where the tool was defined.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolSource {
    /// Built-in tool (Calculator, DateTimeTool).
    Builtin,
    /// Defined in the AXON program via `tool Name { ... }`.
    Program,
}

// ── Tool registry ──────────────────────────────────────────────────────────

/// Central registry for all available tools during execution.
#[derive(Debug)]
pub struct ToolRegistry {
    tools: HashMap<String, ToolEntry>,
}

impl ToolRegistry {
    /// Create a new registry pre-loaded with built-in tools.
    pub fn new() -> Self {
        let mut registry = ToolRegistry {
            tools: HashMap::new(),
        };
        registry.register_builtins();
        registry
    }

    /// Register the built-in native tools.
    fn register_builtins(&mut self) {
        self.tools.insert(
            "Calculator".to_string(),
            ToolEntry {
                name: "Calculator".to_string(),
                provider: "native".to_string(),
                timeout: String::new(),
                runtime: String::new(),
                sandbox: None,
                max_results: None,
                output_schema: "number".to_string(),
                effect_row: vec!["compute".to_string()],
                // §Fase 58.f.2 — built-ins declare no typed input schema;
                // they accept the legacy positional `on <arg>` form.
                parameters: Vec::new(),
                source: ToolSource::Builtin,
                // §Fase 34.c — Calculator declares `compute` effect only.
                // No stream effect → is_streaming = false.
                is_streaming: false,
            },
        );
        self.tools.insert(
            "DateTimeTool".to_string(),
            ToolEntry {
                name: "DateTimeTool".to_string(),
                provider: "native".to_string(),
                timeout: String::new(),
                runtime: String::new(),
                sandbox: None,
                max_results: None,
                output_schema: String::new(),
                effect_row: vec!["read".to_string()],
                // §Fase 58.f.2 — see Calculator: no typed input schema.
                parameters: Vec::new(),
                source: ToolSource::Builtin,
                // §Fase 34.c — DateTimeTool declares `read` effect only.
                is_streaming: false,
            },
        );
    }

    /// Register tools from the IR program's tool definitions.
    ///
    /// §Fase 34.c (v1.29.0) — `is_streaming` is auto-derived from
    /// each spec's `effect_row` via [`derive_is_streaming`]. Tools
    /// declaring `effects: <stream:<policy>>` automatically register
    /// as stream producers; the dispatcher (Fase 34.d) routes them
    /// through the streaming path.
    pub fn register_from_ir(&mut self, tool_specs: &[IRToolSpec]) {
        for spec in tool_specs {
            let is_streaming = derive_is_streaming(&spec.effect_row);
            // §Fase 58.f.2 — resolve the typed input schema (D1) into
            // `(name, type_name)` pairs, matching the synchronous path's
            // `CompiledStep.tool_param_types` (runner.rs §58.e) so the
            // streaming `run_use_tool` coerces args identically.
            let parameters: Vec<(String, String)> = spec
                .parameters
                .iter()
                .map(|p| (p.name.clone(), p.type_name.clone()))
                .collect();
            self.tools.insert(
                spec.name.clone(),
                ToolEntry {
                    name: spec.name.clone(),
                    provider: spec.provider.clone(),
                    timeout: spec.timeout.clone(),
                    runtime: spec.runtime.clone(),
                    sandbox: spec.sandbox,
                    max_results: spec.max_results,
                    output_schema: spec.output_schema.clone(),
                    effect_row: spec.effect_row.clone(),
                    parameters,
                    source: ToolSource::Program,
                    is_streaming,
                },
            );
        }
    }

    /// Register a single tool entry directly.
    pub fn register(&mut self, entry: ToolEntry) {
        self.tools.insert(entry.name.clone(), entry);
    }

    /// §Fase 58.g — resolve every URL-dispatched **program** tool's
    /// relative `runtime` against `base_url` (D7, see
    /// [`resolve_tool_endpoint`]). Only `http` / `mcp` providers carry a
    /// dispatch URL, so only those are rewritten; `native` / `stub`
    /// builtins (and any tool whose `runtime` is already absolute) are
    /// left untouched. A blank `base_url` is a no-op.
    ///
    /// Called by the server entry points (`execute_server_flow` /
    /// `run_streaming_via_dispatcher`) when the caller supplies a
    /// per-tenant / per-server tool base URL — the request-scoped
    /// registry is rewritten before any dispatch, so resolution is
    /// per-request with zero cross-tenant leakage (§58 D10).
    pub fn resolve_relative_endpoints(&mut self, base_url: &str) {
        if base_url.trim().is_empty() {
            return;
        }
        for entry in self.tools.values_mut() {
            if entry.source != ToolSource::Program {
                continue;
            }
            if entry.provider != "http" && entry.provider != "mcp" {
                continue;
            }
            entry.runtime = resolve_tool_endpoint(&entry.runtime, &entry.name, base_url);
        }
    }

    /// Look up a tool by name.
    pub fn get(&self, name: &str) -> Option<&ToolEntry> {
        self.tools.get(name)
    }

    /// Check if a tool is registered.
    pub fn contains(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// Dispatch a tool call. Returns:
    ///   - `Some(ToolResult)` if the tool was handled locally
    ///   - `None` if the tool should fall through to LLM
    pub fn dispatch(&self, tool_name: &str, argument: &str) -> Option<ToolResult> {
        let entry = self.tools.get(tool_name)?;

        match entry.provider.as_str() {
            // Native built-in execution
            "native" => tool_executor::dispatch(tool_name, argument),

            // Stub provider: returns a synthetic response for testing
            "stub" => Some(ToolResult {
                success: true,
                output: format!("[stub] {}({})", tool_name, argument),
                tool_name: tool_name.to_string(),
            }),

            // HTTP provider: REST endpoint dispatch
            "http" => Some(http_tool::dispatch_http(entry, argument)),

            // ℰMCP provider: epistemic MCP transducer (JSON-RPC + blame + taint)
            "mcp" => Some(emcp::dispatch_mcp(entry, argument)),

            // Known providers that currently fall through to LLM
            // Future: "grpc" adapters
            _ => None,
        }
    }

    /// Number of registered tools.
    pub fn len(&self) -> usize {
        self.tools.len()
    }

    /// Check if registry is empty.
    pub fn is_empty(&self) -> bool {
        self.tools.is_empty()
    }

    /// List all registered tool names.
    pub fn tool_names(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self.tools.keys().map(|k| k.as_str()).collect();
        names.sort();
        names
    }

    /// List only built-in tool names.
    pub fn builtin_names(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self
            .tools
            .values()
            .filter(|e| e.source == ToolSource::Builtin)
            .map(|e| e.name.as_str())
            .collect();
        names.sort();
        names
    }

    /// List only program-defined tool names.
    pub fn program_names(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self
            .tools
            .values()
            .filter(|e| e.source == ToolSource::Program)
            .map(|e| e.name.as_str())
            .collect();
        names.sort();
        names
    }
}

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

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

    // §Fase 34.c — derive_is_streaming canonical rule pin.
    //
    // This lib unit test pins the derivation predicate semantics
    // at the language layer: a tool is a stream producer iff at
    // least one entry in its effect_row begins with `stream:`.
    // The drift gate `axon-rs/tests/fase34_c_registry_drift.rs`
    // extends this pin across a 30-tool synthetic corpus.
    #[test]
    fn fase34_c_derive_is_streaming_canonical_rule() {
        // Empty effect_row → not a stream producer.
        assert!(!derive_is_streaming(&[]));
        // Single non-stream effect → not a stream producer.
        assert!(!derive_is_streaming(&["compute".to_string()]));
        assert!(!derive_is_streaming(&["read".to_string()]));
        assert!(!derive_is_streaming(&["network".to_string()]));
        assert!(!derive_is_streaming(&["io".to_string()]));
        assert!(!derive_is_streaming(&["epistemic:speculate".to_string()]));
        // Multiple non-stream effects → not a stream producer.
        assert!(!derive_is_streaming(&[
            "compute".to_string(),
            "read".to_string(),
            "epistemic:speculate".to_string(),
        ]));
        // Any `stream:<policy>` prefix → stream producer.
        assert!(derive_is_streaming(&["stream:drop_oldest".to_string()]));
        assert!(derive_is_streaming(&["stream:degrade_quality".to_string()]));
        assert!(derive_is_streaming(&["stream:pause_upstream".to_string()]));
        assert!(derive_is_streaming(&["stream:fail".to_string()]));
        // Mixed: stream effect among other effects still flags streaming.
        assert!(derive_is_streaming(&[
            "compute".to_string(),
            "stream:drop_oldest".to_string(),
            "network".to_string(),
        ]));
        // `stream` substring NOT at prefix → not a stream effect
        // (the rule is `starts_with("stream:")`, not `contains`).
        assert!(!derive_is_streaming(&["downstream".to_string()]));
        assert!(!derive_is_streaming(&["upstream-flow".to_string()]));
        // `stream:` with empty policy — still detected as streaming
        // intent. The closed-catalog policy validation lives in the
        // resolver (Fase 33.e); the derive_is_streaming rule is the
        // CHEAPER predicate (used at registration time only).
        assert!(derive_is_streaming(&["stream:".to_string()]));
    }

    #[test]
    fn fase34_c_register_from_ir_auto_derives_is_streaming() {
        let mut reg = ToolRegistry::new();
        let specs = vec![
            IRToolSpec {
                node_type: "ToolDefinition",
                source_line: 1,
                source_column: 1,
                name: "ChatStreamer".to_string(),
                provider: "anthropic".to_string(),
                max_results: None,
                filter_expr: String::new(),
                timeout: String::new(),
                runtime: String::new(),
                sandbox: None,
                input_schema: Vec::new(),
                output_schema: String::new(),
                parameters: Vec::new(),
                output_type: None,
                effect_row: vec!["stream:drop_oldest".to_string()],
            },
            IRToolSpec {
                node_type: "ToolDefinition",
                source_line: 5,
                source_column: 1,
                name: "PlainScanner".to_string(),
                provider: "stub".to_string(),
                max_results: None,
                filter_expr: String::new(),
                timeout: String::new(),
                runtime: String::new(),
                sandbox: None,
                input_schema: Vec::new(),
                output_schema: String::new(),
                parameters: Vec::new(),
                output_type: None,
                effect_row: vec!["compute".to_string()],
            },
        ];
        reg.register_from_ir(&specs);
        let chat_entry = reg.get("ChatStreamer").unwrap();
        assert!(
            chat_entry.is_streaming,
            "34.c register_from_ir MUST auto-derive is_streaming=true \
             for tools declaring effects: <stream:<policy>>"
        );
        let plain_entry = reg.get("PlainScanner").unwrap();
        assert!(
            !plain_entry.is_streaming,
            "34.c register_from_ir MUST auto-derive is_streaming=false \
             for tools without `stream:` in effect_row"
        );
    }

    #[test]
    fn fase34_c_builtins_are_not_streaming() {
        let reg = ToolRegistry::new();
        // Built-in Calculator + DateTimeTool have no stream effect.
        assert!(!reg.get("Calculator").unwrap().is_streaming);
        assert!(!reg.get("DateTimeTool").unwrap().is_streaming);
    }

    #[test]
    fn new_registry_has_builtins() {
        let reg = ToolRegistry::new();
        assert!(reg.contains("Calculator"));
        assert!(reg.contains("DateTimeTool"));
        assert_eq!(reg.len(), 2);
        assert_eq!(reg.builtin_names(), vec!["Calculator", "DateTimeTool"]);
        assert!(reg.program_names().is_empty());
    }

    #[test]
    fn register_program_tool() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "WebSearch".to_string(),
            provider: "brave".to_string(),
            timeout: "10s".to_string(),
            runtime: String::new(),
            sandbox: None,
            max_results: Some(5),
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        assert!(reg.contains("WebSearch"));
        assert_eq!(reg.len(), 3);
        assert_eq!(reg.program_names(), vec!["WebSearch"]);

        let entry = reg.get("WebSearch").unwrap();
        assert_eq!(entry.provider, "brave");
        assert_eq!(entry.max_results, Some(5));
    }

    #[test]
    fn register_from_ir_specs() {
        let mut reg = ToolRegistry::new();
        let specs = vec![
            IRToolSpec {
                node_type: "ToolDefinition",
                source_line: 1,
                source_column: 1,
                name: "WebSearch".to_string(),
                provider: "brave".to_string(),
                max_results: Some(5),
                filter_expr: String::new(),
                timeout: "10s".to_string(),
                runtime: String::new(),
                sandbox: None,
                input_schema: Vec::new(),
                output_schema: String::new(),
                parameters: Vec::new(),
                output_type: None,
                effect_row: Vec::new(),
            },
            IRToolSpec {
                node_type: "ToolDefinition",
                source_line: 5,
                source_column: 1,
                name: "DataAnalyzer".to_string(),
                provider: "stub".to_string(),
                max_results: None,
                filter_expr: String::new(),
                timeout: String::new(),
                runtime: "python".to_string(),
                sandbox: Some(true),
                input_schema: Vec::new(),
                output_schema: String::new(),
                parameters: Vec::new(),
                output_type: None,
                effect_row: Vec::new(),
            },
        ];

        reg.register_from_ir(&specs);

        assert_eq!(reg.len(), 4); // 2 builtins + 2 program
        assert!(reg.contains("WebSearch"));
        assert!(reg.contains("DataAnalyzer"));
        assert_eq!(reg.program_names(), vec!["DataAnalyzer", "WebSearch"]);
    }

    #[test]
    fn dispatch_builtin_calculator() {
        let reg = ToolRegistry::new();
        let result = reg.dispatch("Calculator", "2 + 3").unwrap();
        assert!(result.success);
        assert_eq!(result.output, "5");
    }

    #[test]
    fn dispatch_builtin_datetime() {
        let reg = ToolRegistry::new();
        let result = reg.dispatch("DateTimeTool", "year").unwrap();
        assert!(result.success);
        let year: i32 = result.output.parse().unwrap();
        assert!(year >= 2024);
    }

    #[test]
    fn dispatch_stub_provider() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "TestTool".to_string(),
            provider: "stub".to_string(),
            timeout: String::new(),
            runtime: String::new(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        let result = reg.dispatch("TestTool", "hello world").unwrap();
        assert!(result.success);
        assert_eq!(result.output, "[stub] TestTool(hello world)");
    }

    #[test]
    fn dispatch_unknown_provider_falls_through() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "WebSearch".to_string(),
            provider: "brave".to_string(),
            timeout: "10s".to_string(),
            runtime: String::new(),
            sandbox: None,
            max_results: Some(5),
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        // brave provider not handled locally → falls through to LLM
        assert!(reg.dispatch("WebSearch", "query").is_none());
    }

    #[test]
    fn dispatch_unregistered_tool_returns_none() {
        let reg = ToolRegistry::new();
        assert!(reg.dispatch("NonExistent", "arg").is_none());
    }

    #[test]
    fn program_tool_overrides_builtin() {
        let mut reg = ToolRegistry::new();
        // Override Calculator with a stub provider
        reg.register(ToolEntry {
            name: "Calculator".to_string(),
            provider: "stub".to_string(),
            timeout: String::new(),
            runtime: String::new(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        let entry = reg.get("Calculator").unwrap();
        assert_eq!(entry.source, ToolSource::Program);
        assert_eq!(entry.provider, "stub");

        // Now dispatches via stub, not native
        let result = reg.dispatch("Calculator", "2+3").unwrap();
        assert_eq!(result.output, "[stub] Calculator(2+3)");
    }

    // §Fase 58.g — endpoint resolution (D7).

    #[test]
    fn resolve_tool_endpoint_absolute_passthrough() {
        // Already-absolute runtimes are pinned by the program (D5).
        assert_eq!(
            resolve_tool_endpoint("https://api.example.com/x", "T", "https://base"),
            "https://api.example.com/x"
        );
        assert_eq!(
            resolve_tool_endpoint("http://h/x", "T", "https://base"),
            "http://h/x"
        );
    }

    #[test]
    fn resolve_tool_endpoint_relative_joined_to_base() {
        assert_eq!(
            resolve_tool_endpoint("/crm/search", "CrmRadar", "https://tools.acme.io"),
            "https://tools.acme.io/crm/search"
        );
        // No leading slash on the slug works too.
        assert_eq!(
            resolve_tool_endpoint("crm/search", "CrmRadar", "https://tools.acme.io/"),
            "https://tools.acme.io/crm/search"
        );
    }

    #[test]
    fn resolve_tool_endpoint_empty_runtime_uses_tool_name() {
        assert_eq!(
            resolve_tool_endpoint("", "CrmRadar", "https://tools.acme.io"),
            "https://tools.acme.io/CrmRadar"
        );
    }

    #[test]
    fn resolve_tool_endpoint_empty_base_is_noop() {
        // No base wired → relative runtime stays relative (the
        // dispatcher then surfaces the actionable diagnostic).
        assert_eq!(resolve_tool_endpoint("/crm", "T", ""), "/crm");
        assert_eq!(resolve_tool_endpoint("", "T", "   "), "");
    }

    #[test]
    fn resolve_relative_endpoints_only_rewrites_http_mcp_program_tools() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "CrmRadar".to_string(),
            provider: "http".to_string(),
            timeout: String::new(),
            runtime: "/crm/search".to_string(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });
        reg.register(ToolEntry {
            name: "FhirMcp".to_string(),
            provider: "mcp".to_string(),
            timeout: String::new(),
            runtime: "fhir".to_string(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });
        reg.register(ToolEntry {
            name: "Pinned".to_string(),
            provider: "http".to_string(),
            timeout: String::new(),
            runtime: "https://pinned.example.com/api".to_string(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        reg.resolve_relative_endpoints("https://tenant-acme.tools.internal");

        assert_eq!(
            reg.get("CrmRadar").unwrap().runtime,
            "https://tenant-acme.tools.internal/crm/search"
        );
        assert_eq!(
            reg.get("FhirMcp").unwrap().runtime,
            "https://tenant-acme.tools.internal/fhir"
        );
        // Absolute runtime untouched (D5).
        assert_eq!(
            reg.get("Pinned").unwrap().runtime,
            "https://pinned.example.com/api"
        );
        // Built-ins (native) never carry a URL → untouched.
        assert_eq!(reg.get("Calculator").unwrap().runtime, "");
    }

    #[test]
    fn resolve_relative_endpoints_blank_base_is_noop() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "T".to_string(),
            provider: "http".to_string(),
            timeout: String::new(),
            runtime: "/x".to_string(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });
        reg.resolve_relative_endpoints("   ");
        assert_eq!(reg.get("T").unwrap().runtime, "/x");
    }

    #[test]
    fn tool_names_sorted() {
        let mut reg = ToolRegistry::new();
        reg.register(ToolEntry {
            name: "ZetaTool".to_string(),
            provider: "stub".to_string(),
            timeout: String::new(),
            runtime: String::new(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });
        reg.register(ToolEntry {
            name: "AlphaTool".to_string(),
            provider: "stub".to_string(),
            timeout: String::new(),
            runtime: String::new(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            parameters: Vec::new(),
            source: ToolSource::Program,
            is_streaming: false,
        });

        let names = reg.tool_names();
        assert_eq!(
            names,
            vec!["AlphaTool", "Calculator", "DateTimeTool", "ZetaTool"]
        );
    }
}