adk-graph 3.0.0

Graph-based workflow orchestration for ADK-Rust agents
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
//! Action node executor for adk-graph.
//!
//! Wraps any `ActionNodeConfig` and implements the `Node` trait, applying
//! `StandardProperties` (error handling, timeout, skip condition) before
//! dispatching to node-specific execution logic.

pub mod code;
pub mod file;
pub mod loop_node;
pub mod merge;
pub mod set;
pub mod switch;
pub mod transform;
pub mod trigger;
#[cfg(feature = "action-trigger")]
pub mod trigger_runtime;
pub mod wait;

#[cfg(feature = "action-http")]
pub mod http;

#[cfg(feature = "action-db")]
pub mod database;

#[cfg(feature = "action-email")]
pub mod email;

#[cfg(feature = "action-http")]
pub mod notification;

#[cfg(feature = "action-rss")]
pub mod rss;

use std::collections::HashMap;

use adk_action::{ActionNodeConfig, ErrorMode, interpolate_variables};
use async_trait::async_trait;
use serde_json::Value;

use crate::error::{GraphError, Result};
use crate::node::{Node, NodeContext, NodeOutput};

/// Executor that wraps an `ActionNodeConfig` and implements the `Node` trait.
///
/// Applies `StandardProperties` uniformly:
/// 1. Check skip condition
/// 2. Apply timeout from `ExecutionControl`
/// 3. Dispatch to the appropriate node executor
/// 4. Apply error handling (stop/continue/retry/fallback)
pub struct ActionNodeExecutor {
    config: ActionNodeConfig,
    /// Directories a file node may touch; empty means the working directory.
    file_roots: Vec<std::path::PathBuf>,
    /// URLs the HTTP, notification, and RSS nodes may request.
    #[cfg(feature = "action-http")]
    http_policy: http::HttpActionPolicy,
}

impl ActionNodeExecutor {
    /// Create a new executor wrapping the given action node config.
    ///
    /// A file node is confined to the current working directory, and the HTTP,
    /// notification, and RSS nodes to `https`/`http` URLs; see
    /// [`Self::with_file_roots`] and `with_http_policy`.
    pub fn new(config: ActionNodeConfig) -> Self {
        Self {
            config,
            file_roots: Vec::new(),
            #[cfg(feature = "action-http")]
            http_policy: http::HttpActionPolicy::default(),
        }
    }

    /// Confines a file node to these directories instead of the working directory.
    ///
    /// A relative path resolves against the first root. A path that resolves
    /// outside every root, through `..` or a symbolic link, fails the node; see
    /// [`mod@file`] for the full rules.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use adk_graph::action::ActionNodeExecutor;
    ///
    /// let executor = ActionNodeExecutor::new(config).with_file_roots(["/srv/workflow-data"]);
    /// ```
    #[must_use]
    pub fn with_file_roots<I, P>(mut self, roots: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<std::path::PathBuf>,
    {
        self.file_roots = roots.into_iter().map(Into::into).collect();
        self
    }

    /// Sets which URLs the HTTP, notification, and RSS nodes may request, and how
    /// redirects are followed.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use adk_graph::action::ActionNodeExecutor;
    /// use adk_graph::action::http::HttpActionPolicy;
    ///
    /// let executor = ActionNodeExecutor::new(config)
    ///     .with_http_policy(HttpActionPolicy::new().allow_hosts(["api.example.com"]));
    /// ```
    #[cfg(feature = "action-http")]
    #[must_use]
    pub fn with_http_policy(mut self, policy: http::HttpActionPolicy) -> Self {
        self.http_policy = policy;
        self
    }

    /// Returns a reference to the inner config.
    pub fn config(&self) -> &ActionNodeConfig {
        &self.config
    }

    /// Why this node cannot execute, if it cannot.
    ///
    /// Some action variants accept and validate a configuration while their backend is
    /// still a placeholder, and some require a feature that is not compiled in. Both
    /// used to surface only when the node ran, which is operationally different from a
    /// configuration rejected before the workflow starts.
    pub fn unavailable_reason(&self) -> Option<String> {
        match &self.config {
            ActionNodeConfig::Database(config) => Some(format!(
                "database node '{}' cannot execute: database action nodes are not \
                 implemented in any feature configuration",
                config.standard.id
            )),
            ActionNodeConfig::Email(config) => Some(format!(
                "email node '{}' cannot execute: email action nodes are not implemented \
                 in any feature configuration",
                config.standard.id
            )),
            ActionNodeConfig::Code(config)
                if matches!(
                    config.language,
                    adk_action::CodeLanguage::Javascript | adk_action::CodeLanguage::Typescript
                ) =>
            {
                Some(format!(
                    "code node '{}' cannot execute: JavaScript and TypeScript code nodes are \
                     not implemented in any feature configuration; use language 'rust'",
                    config.standard.id
                ))
            }
            #[cfg(not(feature = "action-http"))]
            ActionNodeConfig::Http(config) => Some(format!(
                "http node '{}' cannot execute: the 'action-http' feature is not enabled",
                config.standard.id
            )),
            _ => None,
        }
    }

    /// Build a state map from the `NodeContext` for interpolation.
    fn state_map(ctx: &NodeContext) -> HashMap<String, Value> {
        ctx.state.clone()
    }

    /// Check the skip condition. Returns `true` if the node should be skipped.
    fn should_skip(&self, ctx: &NodeContext) -> bool {
        let std = self.config.standard();
        if let Some(condition) = &std.execution.condition {
            let state = Self::state_map(ctx);
            let resolved = interpolate_variables(condition, &state);
            let trimmed = resolved.trim().to_lowercase();
            trimmed.is_empty() || trimmed == "false"
        } else {
            false
        }
    }

    /// Dispatch to the appropriate node executor based on the config variant.
    async fn dispatch(&self, ctx: &NodeContext) -> Result<NodeOutput> {
        match &self.config {
            ActionNodeConfig::Trigger(c) => trigger::execute_trigger(c, ctx).await,
            ActionNodeConfig::Set(c) => set::execute_set(c, ctx).await,
            ActionNodeConfig::Transform(c) => transform::execute_transform(c, ctx).await,
            ActionNodeConfig::Switch(c) => switch::execute_switch(c, ctx).await,
            ActionNodeConfig::Loop(c) => loop_node::execute_loop(c, ctx).await,
            ActionNodeConfig::Merge(c) => merge::execute_merge(c, ctx).await,
            ActionNodeConfig::Wait(c) => wait::execute_wait(c, ctx).await,
            ActionNodeConfig::File(c) => file::execute_file_in(c, ctx, &self.file_roots).await,
            ActionNodeConfig::Code(c) => code::execute_code(c, ctx).await,

            #[cfg(feature = "action-http")]
            ActionNodeConfig::Http(c) => {
                http::execute_http_with_policy(c, ctx, &self.http_policy).await
            }
            #[cfg(not(feature = "action-http"))]
            ActionNodeConfig::Http(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: "HTTP node requires the 'action-http' feature".into(),
            }),

            #[cfg(feature = "action-db")]
            ActionNodeConfig::Database(c) => database::execute_database(c, ctx).await,
            // Not "enable the feature": the feature compiles a placeholder that
            // fails the same way, so naming it would send the caller nowhere.
            #[cfg(not(feature = "action-db"))]
            ActionNodeConfig::Database(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: "database action nodes are not implemented; no database driver is \
                          integrated"
                    .into(),
            }),

            #[cfg(feature = "action-email")]
            ActionNodeConfig::Email(c) => email::execute_email(c, ctx).await,
            #[cfg(not(feature = "action-email"))]
            ActionNodeConfig::Email(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: "email action nodes are not implemented; neither IMAP monitoring nor \
                          SMTP sending is integrated"
                    .into(),
            }),

            #[cfg(feature = "action-http")]
            ActionNodeConfig::Notification(c) => {
                notification::execute_notification_with_policy(c, ctx, &self.http_policy).await
            }
            #[cfg(not(feature = "action-http"))]
            ActionNodeConfig::Notification(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: "Notification node requires the 'action-http' feature".into(),
            }),

            #[cfg(feature = "action-rss")]
            ActionNodeConfig::Rss(c) => {
                rss::execute_rss_with_policy(c, ctx, &self.http_policy).await
            }
            #[cfg(not(feature = "action-rss"))]
            ActionNodeConfig::Rss(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: "RSS node requires the 'action-rss' feature".into(),
            }),
        }
    }

    /// Applies the skip condition, the timeout and the error handling around `run`.
    ///
    /// A governed node's call runs once (spec 3.3 §12): its error handling neither retries
    /// it nor swallows or replaces its failure, so the action ledger records what happened,
    /// and its timeout is a [`GraphError::NodeTimedOut`], an outcome the bracket keeps
    /// unknown because the call may have taken effect.
    async fn run_guarded<F, Fut>(&self, ctx: &NodeContext, run: F) -> Result<NodeOutput>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = Result<NodeOutput>>,
    {
        // 1. Check skip condition
        if self.should_skip(ctx) {
            tracing::debug!(node = %self.config.standard().id, "skipping action node (condition false)");
            return Ok(NodeOutput::new());
        }

        // 2. Apply timeout
        let timeout_ms = self.config.standard().execution.timeout;
        let timeout_duration = std::time::Duration::from_millis(timeout_ms);

        if ctx.governed_call() {
            return match tokio::time::timeout(timeout_duration, run()).await {
                Ok(result) => result,
                Err(_) => Err(GraphError::NodeTimedOut {
                    node: self.config.standard().id.clone(),
                    elapsed: timeout_duration,
                }),
            };
        }

        match tokio::time::timeout(timeout_duration, self.execute_with_error_handling(run)).await {
            Ok(result) => result,
            Err(_) => Err(GraphError::NodeExecutionFailed {
                node: self.config.standard().id.clone(),
                message: format!("action node timed out after {timeout_ms}ms"),
            }),
        }
    }

    /// Apply error handling around `run`, the dispatch or an approved call.
    ///
    /// A governed refusal is never swallowed or retried: the call was not sent.
    async fn execute_with_error_handling<F, Fut>(&self, run: F) -> Result<NodeOutput>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = Result<NodeOutput>>,
    {
        let std = self.config.standard();
        let error_handling = &std.error_handling;
        let governed = |e: &GraphError| crate::governed::GovernedError::code_of(e).is_some();

        match error_handling.mode {
            ErrorMode::Stop => run().await,

            ErrorMode::Continue => match run().await {
                Ok(output) => Ok(output),
                Err(e) if governed(&e) => Err(e),
                Err(e) => {
                    tracing::warn!(
                        node = %std.id,
                        error = %e,
                        "action node error swallowed (continue mode)"
                    );
                    Ok(NodeOutput::new())
                }
            },

            ErrorMode::Retry => {
                let max_attempts = error_handling.retry_count.unwrap_or(1) + 1;
                let delay_ms = error_handling.retry_delay.unwrap_or(0);
                let mut last_err = None;

                for attempt in 0..max_attempts {
                    match run().await {
                        Ok(output) => return Ok(output),
                        Err(e) if governed(&e) => return Err(e),
                        Err(e) => {
                            tracing::warn!(
                                node = %std.id,
                                attempt = attempt + 1,
                                max_attempts,
                                error = %e,
                                "action node retry"
                            );
                            last_err = Some(e);
                            if attempt + 1 < max_attempts && delay_ms > 0 {
                                tokio::time::sleep(std::time::Duration::from_millis(delay_ms))
                                    .await;
                            }
                        }
                    }
                }

                Err(last_err.unwrap_or_else(|| GraphError::NodeExecutionFailed {
                    node: std.id.clone(),
                    message: "retry exhausted with no error captured".into(),
                }))
            }

            ErrorMode::Fallback => match run().await {
                Ok(output) => Ok(output),
                Err(e) if governed(&e) => Err(e),
                Err(e) => {
                    tracing::warn!(
                        node = %std.id,
                        error = %e,
                        "action node error caught, using fallback value"
                    );
                    let fallback = error_handling.fallback_value.clone().unwrap_or(Value::Null);
                    Ok(NodeOutput::new().with_update(&std.mapping.output_key, fallback))
                }
            },
        }
    }
}

#[async_trait]
impl Node for ActionNodeExecutor {
    fn validate(&self) -> Result<()> {
        match self.unavailable_reason() {
            Some(message) => Err(GraphError::InvalidGraph(message)),
            None => Ok(()),
        }
    }

    #[allow(clippy::misnamed_getters)]
    fn name(&self) -> &str {
        // Node::name() is used as a unique identifier, which is the `id` field
        &self.config.standard().id
    }

    fn kind(&self) -> crate::shape::NodeKind {
        use crate::shape::NodeKind;
        match &self.config {
            ActionNodeConfig::Trigger(_) => NodeKind::Trigger,
            ActionNodeConfig::Http(_) => NodeKind::HttpCall,
            ActionNodeConfig::Set(_) => NodeKind::Set,
            ActionNodeConfig::Transform(_) => NodeKind::Transform,
            ActionNodeConfig::Switch(_) => NodeKind::Switch,
            ActionNodeConfig::Loop(_) => NodeKind::Loop,
            ActionNodeConfig::Merge(_) => NodeKind::Merge,
            ActionNodeConfig::Wait(_) => NodeKind::Wait,
            ActionNodeConfig::Code(_) => NodeKind::Code,
            ActionNodeConfig::Database(_) => NodeKind::Database,
            ActionNodeConfig::Email(_) => NodeKind::Email,
            ActionNodeConfig::Notification(_) => NodeKind::Notification,
            ActionNodeConfig::Rss(_) => NodeKind::Rss,
            ActionNodeConfig::File(_) => NodeKind::File,
        }
    }

    /// `false` when the node's condition is false, so a governed node asks for no decision;
    /// an error when an HTTP node's request cannot be built (spec 3.3 §12).
    fn will_act(&self, ctx: &NodeContext) -> Result<bool> {
        if self.should_skip(ctx) {
            return Ok(false);
        }
        match (&self.config, ctx) {
            #[cfg(feature = "action-http")]
            (ActionNodeConfig::Http(config), ctx) => {
                http::buildable(config, &ctx.state, &self.http_policy).map(|()| true)
            }
            _ => Ok(true),
        }
    }

    /// The HTTP node's call with its credentials as digests (spec 3.3 §12); `None` for
    /// other action nodes, whose call is bound to the state they read.
    fn call_args(&self, ctx: &NodeContext) -> Option<Value> {
        match (&self.config, ctx) {
            #[cfg(feature = "action-http")]
            (ActionNodeConfig::Http(config), ctx) => {
                http::call_args(config, &ctx.state, &self.http_policy)
            }
            _ => None,
        }
    }

    async fn execute(&self, ctx: &NodeContext) -> Result<NodeOutput> {
        self.run_guarded(ctx, || self.dispatch(ctx)).await
    }

    /// Sends exactly the approved HTTP call, with each credential digest replaced by the
    /// current credential (spec 3.3 §7.1). Other action nodes cannot run an edited call.
    async fn run_call(&self, ctx: &NodeContext, args: Value) -> Result<NodeOutput> {
        match (&self.config, ctx, args) {
            #[cfg(feature = "action-http")]
            (ActionNodeConfig::Http(config), ctx, args) => {
                self.run_guarded(ctx, || {
                    http::run_call(
                        config,
                        &ctx.state,
                        &self.http_policy,
                        &args,
                        ctx.governed_call(),
                    )
                })
                .await
            }
            _ => Err(crate::governed::GovernedError::EditUnsupported {
                node: self.config.standard().id.clone(),
            }
            .into()),
        }
    }
}