Skip to main content

gadk_engine/workflow/
config.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! `NodeConfig` and `RetryConfig`.
17
18use std::sync::Arc;
19use std::time::Duration;
20
21use gadk_agent::AgentError;
22
23use crate::error::{WorkflowErrorKind, workflow_error};
24
25/// The configuration for a node.
26///
27/// All fields are optional. The `Option<bool>` fields (`rerun_on_resume`, `wait_for_output`) are
28/// tri-state so node implementations can distinguish "user explicitly opted out" (`Some(false)`)
29/// from "user did not configure" (`None`).
30#[derive(Clone, Default)]
31pub struct NodeConfig {
32    /// When true, runs the node concurrently for each item of a list-typed input. The engine
33    /// collects per-item outputs and emits a single aggregate output event.
34    pub parallel_worker: bool,
35
36    /// Controls human-in-the-loop resume behavior: `Some(true)` re-runs the interrupted node from
37    /// scratch on resume (re-entry mode), `Some(false)` routes the resume payload to the node's
38    /// successor as input (handoff mode), and `None` defers to the engine. The engine currently
39    /// treats `None` as handoff.
40    pub rerun_on_resume: Option<bool>,
41
42    /// When true, parks the parent (NodeInterrupted) to re-run instead of completing it when a
43    /// child dispatched via RunNode finishes without setting an event output — letting a
44    /// multi-turn child be retried rather than falsely completed with the zero value. Only honored
45    /// on the RunNode (dynamic sub-scheduler) path today. `None` means "use the engine default" —
46    /// false for most node kinds.
47    pub wait_for_output: Option<bool>,
48
49    /// When set, makes the scheduler retry this node on failure per the policy. `None` means
50    /// "no retries".
51    pub retry_config: Option<RetryConfig>,
52
53    /// When set and non-zero, bounds a single activation of the node. `None` (and
54    /// `Some(Duration::ZERO)`) means the node is bounded only by the parent invocation's deadline.
55    pub timeout: Option<Duration>,
56
57    /// When true, tells the scheduler not to wrap the node in an "invoke_node" telemetry span
58    /// because the node body already starts its own span (e.g. an LlmAgent node whose wrapped
59    /// agent emits "invoke_agent").
60    pub emits_own_span: bool,
61}
62
63/// The parameters for retrying a failed node.
64///
65/// Recommended construction is via [`default_retry_config`] and overriding the fields you want to
66/// customize. `RetryConfig::default()` is the zero value: a valid "no retry, no backoff, no
67/// jitter" policy.
68#[derive(Clone, Default)]
69pub struct RetryConfig {
70    /// Maximum number of attempts, including the original request. 0 or 1 means no retries.
71    pub max_attempts: i32,
72
73    /// Initial delay before the first retry.
74    pub initial_delay: Duration,
75
76    /// Maximum delay between retries.
77    pub max_delay: Duration,
78
79    /// The per-attempt multiplier applied to the delay. 1.0 means a constant `initial_delay`
80    /// between retries; 2.0 means classic exponential backoff. Values < 1.0 shrink the delay each
81    /// attempt (rare but permitted).
82    pub backoff_factor: f64,
83
84    /// A randomness factor in [0.0, 1.0]. The actual delay is sampled from
85    /// `delay * (1 ± jitter)`. Zero means deterministic delays.
86    pub jitter: f64,
87
88    /// Predicate that defines when to retry (true means retry). `None` retries every error except
89    /// input-validation failures.
90    #[allow(clippy::type_complexity)] // the pinned C4 field type
91    pub should_retry: Option<Arc<dyn Fn(&AgentError) -> bool + Send + Sync>>,
92}
93
94/// Retries everything except input-validation failures.
95pub(crate) fn default_should_retry(err: &AgentError) -> bool {
96    !workflow_error(err).is_some_and(|w| w.is(WorkflowErrorKind::InputValidation))
97}
98
99/// The default retry policy: 5 attempts, 1s initial delay, 60s cap, 2x backoff, full jitter. Every
100/// error is retried except input-validation failures, which a retry cannot fix because the input is
101/// deterministic.
102pub fn default_retry_config() -> RetryConfig {
103    RetryConfig {
104        max_attempts: 5,
105        initial_delay: Duration::from_secs(1),
106        max_delay: Duration::from_secs(60),
107        backoff_factor: 2.0,
108        jitter: 1.0,
109        should_retry: Some(Arc::new(default_should_retry)),
110    }
111}