noetl-tools 3.17.0

NoETL Tool Library - Shared tool implementations for workflow execution
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
//! Tool registry and dispatch.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use crate::context::ExecutionContext;
use crate::error::ToolError;
use crate::result::ToolResult;

/// Configuration for tool execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
    /// Tool kind/type (e.g., "shell", "http", "rhai").
    pub kind: String,

    /// Tool-specific configuration.
    #[serde(flatten)]
    pub config: serde_json::Value,

    /// Timeout in seconds (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout: Option<u64>,

    /// Retry configuration (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry: Option<RetryConfig>,

    /// Authentication configuration (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth: Option<AuthConfig>,
}

/// Retry configuration for tool execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Maximum number of retries.
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,

    /// Initial delay between retries in milliseconds.
    #[serde(default = "default_initial_delay_ms")]
    pub initial_delay_ms: u64,

    /// Maximum delay between retries in milliseconds.
    #[serde(default = "default_max_delay_ms")]
    pub max_delay_ms: u64,

    /// Exponential backoff multiplier.
    #[serde(default = "default_backoff_multiplier")]
    pub backoff_multiplier: f64,
}

fn default_max_retries() -> u32 {
    3
}

fn default_initial_delay_ms() -> u64 {
    500
}

fn default_max_delay_ms() -> u64 {
    10000
}

fn default_backoff_multiplier() -> f64 {
    2.0
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: default_max_retries(),
            initial_delay_ms: default_initial_delay_ms(),
            max_delay_ms: default_max_delay_ms(),
            backoff_multiplier: default_backoff_multiplier(),
        }
    }
}

/// Authentication configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Authentication type.
    #[serde(rename = "type")]
    pub auth_type: AuthType,

    /// Credential name (for credential lookup).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub credential: Option<String>,

    /// Token (for direct token auth).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,

    /// Username (for basic auth).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,

    /// Password (for basic auth).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,

    /// API key header name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub header: Option<String>,

    /// GCP scopes (for ADC auth).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scopes: Option<Vec<String>>,
}

/// Authentication type.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum AuthType {
    /// Bearer token authentication.
    Bearer,
    /// Basic authentication (username/password).
    Basic,
    /// API key authentication.
    ApiKey,
    /// GCP Application Default Credentials.
    GcpAdc,
    /// No authentication.
    #[default]
    None,
}

/// Classify whether a tool **kind** has external side effects
/// ([noetl/ai-meta#104](https://github.com/noetl/ai-meta/issues/104) Phase E —
/// the side-effect durability barrier; OQ4).
///
/// "Side-effecting" means a single execution of the cycle mutates state outside
/// NoETL that a replay must not repeat: an HTTP `POST`, a database write, a
/// payment, an email. "Pure / re-runnable" means re-execution is free of
/// externally-observable consequences: a condition evaluation, a sandboxed
/// transform, a no-op.
///
/// The barrier uses this to decide **where to block on resume**: before
/// re-dispatching a side-effecting cycle whose durable result already exists,
/// the worker skips re-execution and adopts the recorded result (so the side
/// effect fires exactly once across a crash-resume / re-drive). Non-side-effecting
/// cycles are never blocked — idempotent recompute is fine.
///
/// ## Static classification (OQ4 disposition)
///
/// This is a **static, per-kind** attribute — the RFC's baseline. The
/// classification is **conservative: the default is `true`**. Over-classifying a
/// pure tool as side-effecting is *safe* because the barrier only ever ADOPTS an
/// already-durable result — it never skips a cycle whose result is absent, and
/// adopting an idempotent recompute's prior result is byte-equivalent to
/// recomputing it. Under-classifying a genuinely side-effecting tool is the only
/// unsafe direction (it would re-fire the side effect on resume), so the safe
/// default is `true` and only kinds with no externally-observable effect are
/// `false`.
///
/// Pure / re-runnable kinds (`false`):
/// - `noop` — does nothing.
/// - `rhai` — a sandboxed expression/transform evaluator with no host I/O.
///
/// Every other registered kind is `true`. A *per-invocation* refinement (e.g. an
/// `http` `GET` is pure while a `POST` is not) is deliberately **not** done here:
/// because over-classification is safe, the static flag is sufficient for
/// correctness, and a per-invocation predicate is a future optimization (OQ4)
/// rather than a correctness requirement.
pub fn kind_is_side_effecting(kind: &str) -> bool {
    !matches!(kind, "noop" | "rhai")
}

/// Tool trait for implementing executable tools.
#[async_trait]
pub trait Tool: Send + Sync {
    /// Returns the tool's unique name/kind.
    fn name(&self) -> &'static str;

    /// Whether this tool has external side effects (see [`kind_is_side_effecting`]
    /// for the full contract — [noetl/ai-meta#104](https://github.com/noetl/ai-meta/issues/104)
    /// Phase E). The default delegates to the per-kind classifier keyed on
    /// [`Tool::name`], so the classification has a single source of truth; a tool
    /// only overrides this when its kind string can't carry the distinction.
    fn side_effecting(&self) -> bool {
        kind_is_side_effecting(self.name())
    }

    /// Execute the tool with the given configuration and context.
    async fn execute(
        &self,
        config: &ToolConfig,
        ctx: &ExecutionContext,
    ) -> Result<ToolResult, ToolError>;
}

/// Registry of available tools.
pub struct ToolRegistry {
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl ToolRegistry {
    /// Create a new empty tool registry.
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    /// Register a tool.
    pub fn register<T: Tool + 'static>(&mut self, tool: T) {
        let name = tool.name().to_string();
        self.tools.insert(name, Arc::new(tool));
    }

    /// Get a tool by name.
    pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
        self.tools.get(name).cloned()
    }

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

    /// List all registered tool names.
    pub fn list(&self) -> Vec<&str> {
        self.tools.keys().map(|s| s.as_str()).collect()
    }

    /// Whether the registered tool `name` has external side effects
    /// ([noetl/ai-meta#104](https://github.com/noetl/ai-meta/issues/104) Phase E).
    /// `None` if no tool by that name is registered. Callers that only have a
    /// kind string (e.g. the worker's WASM dispatch path, which holds
    /// `command.tool_kind` but no `Tool` instance) should use the free
    /// [`kind_is_side_effecting`] instead — it is the same classification without
    /// the registry lookup.
    pub fn is_side_effecting(&self, name: &str) -> Option<bool> {
        self.get(name).map(|tool| tool.side_effecting())
    }

    /// Execute a tool by name.
    pub async fn execute(
        &self,
        name: &str,
        config: &ToolConfig,
        ctx: &ExecutionContext,
    ) -> Result<ToolResult, ToolError> {
        let tool = self
            .get(name)
            .ok_or_else(|| ToolError::NotFound(name.to_string()))?;
        tool.execute(config, ctx).await
    }

    /// Execute a tool from config (uses config.kind as tool name).
    pub async fn execute_from_config(
        &self,
        config: &ToolConfig,
        ctx: &ExecutionContext,
    ) -> Result<ToolResult, ToolError> {
        self.execute(&config.kind, config, ctx).await
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ToolRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolRegistry")
            .field("tools", &self.tools.keys().collect::<Vec<_>>())
            .finish()
    }
}

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

    struct MockTool;

    #[async_trait]
    impl Tool for MockTool {
        fn name(&self) -> &'static str {
            "mock"
        }

        async fn execute(
            &self,
            _config: &ToolConfig,
            _ctx: &ExecutionContext,
        ) -> Result<ToolResult, ToolError> {
            Ok(ToolResult::success(serde_json::json!({"mock": true})))
        }
    }

    #[test]
    fn test_registry_new() {
        let registry = ToolRegistry::new();
        assert!(registry.list().is_empty());
    }

    #[test]
    fn test_registry_register() {
        let mut registry = ToolRegistry::new();
        registry.register(MockTool);

        assert!(registry.has("mock"));
        assert!(!registry.has("unknown"));
        assert_eq!(registry.list(), vec!["mock"]);
    }

    #[tokio::test]
    async fn test_registry_execute() {
        let mut registry = ToolRegistry::new();
        registry.register(MockTool);

        let config = ToolConfig {
            kind: "mock".to_string(),
            config: serde_json::json!({}),
            timeout: None,
            retry: None,
            auth: None,
        };

        let ctx = ExecutionContext::default();
        let result = registry.execute("mock", &config, &ctx).await.unwrap();
        assert!(result.is_success());
    }

    #[tokio::test]
    async fn test_registry_execute_not_found() {
        let registry = ToolRegistry::new();
        let config = ToolConfig {
            kind: "unknown".to_string(),
            config: serde_json::json!({}),
            timeout: None,
            retry: None,
            auth: None,
        };

        let ctx = ExecutionContext::default();
        let result = registry.execute("unknown", &config, &ctx).await;
        assert!(matches!(result, Err(ToolError::NotFound(_))));
    }

    #[test]
    fn test_retry_config_default() {
        let config = RetryConfig::default();
        assert_eq!(config.max_retries, 3);
        assert_eq!(config.initial_delay_ms, 500);
        assert_eq!(config.max_delay_ms, 10000);
        assert_eq!(config.backoff_multiplier, 2.0);
    }

    // ---- noetl/ai-meta#104 Phase E: side_effecting classification ----

    /// A pure mock tool that overrides the default classification to `false`.
    struct PureMockTool;

    #[async_trait]
    impl Tool for PureMockTool {
        fn name(&self) -> &'static str {
            "pure_mock"
        }
        fn side_effecting(&self) -> bool {
            false
        }
        async fn execute(
            &self,
            _config: &ToolConfig,
            _ctx: &ExecutionContext,
        ) -> Result<ToolResult, ToolError> {
            Ok(ToolResult::success(serde_json::json!({"pure": true})))
        }
    }

    #[test]
    fn kind_is_side_effecting_classifies_pure_kinds_false() {
        // The only two pure / re-runnable kinds.
        assert!(!kind_is_side_effecting("noop"));
        assert!(!kind_is_side_effecting("rhai"));
    }

    #[test]
    fn kind_is_side_effecting_defaults_true_for_everything_else() {
        // Conservative default: an over-classification is safe (adopt-only barrier).
        for kind in [
            "http",
            "postgres",
            "snowflake",
            "duckdb",
            "ducklake",
            "python",
            "shell",
            "script",
            "transfer",
            "container",
            "nats",
            "mcp",
            "subscription",
            "artifact",
            "result_fetch",
            "playbook",
            "task_sequence",
            // an unknown kind is treated as side-effecting (safe default).
            "some_future_kind",
        ] {
            assert!(
                kind_is_side_effecting(kind),
                "kind {kind:?} should classify as side-effecting"
            );
        }
    }

    #[test]
    fn tool_side_effecting_default_delegates_to_kind() {
        // MockTool's name "mock" is not in the pure set → default trait method true.
        assert!(MockTool.side_effecting());
        // An override wins over the default.
        assert!(!PureMockTool.side_effecting());
    }

    #[test]
    fn registry_is_side_effecting_lookup() {
        let mut registry = ToolRegistry::new();
        registry.register(MockTool);
        registry.register(PureMockTool);
        assert_eq!(registry.is_side_effecting("mock"), Some(true));
        assert_eq!(registry.is_side_effecting("pure_mock"), Some(false));
        // An unregistered name yields None (caller falls back to the free fn).
        assert_eq!(registry.is_side_effecting("nope"), None);
    }

    #[test]
    fn test_auth_config_serialization() {
        let config = AuthConfig {
            auth_type: AuthType::Bearer,
            credential: Some("my-cred".to_string()),
            token: None,
            username: None,
            password: None,
            header: None,
            scopes: None,
        };

        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("\"type\":\"bearer\""));
        assert!(json.contains("\"credential\":\"my-cred\""));
    }
}