gadk-engine 0.5.9

The gadk runtime: LLM agent and flow, workflow agents and graphs, and the runner
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
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! `ParallelWorker`: runs a wrapped node once per item of a list input.

use std::any::Any;
use std::sync::Arc;

use futures::StreamExt;
use gadk_agent::fiber::{self, AttemptSite, PARALLEL_WORKER_CREATOR};
use gadk_agent::{AgentError, CancelCause, Context, ContextDelta, EventStream};
use gadk_platform::json_kind_article;
use gadk_session::Event;
use parking_lot::Mutex;
use serde_json::Value;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tokio::task::JoinSet;
use tracing::Instrument;

use crate::error::{WorkflowError, WorkflowErrorKind};
use crate::llm::flow::context_err;
use crate::workflow::branch::derive_sub_branch;
use crate::workflow::nodes::AgentNode;
use crate::workflow::scheduler::{record_node_error, start_node_span};
use crate::workflow::{BaseNode, Node, NodeConfig, RetryConfig, calculate_delay, should_retry};

/// Runs a wrapped node once per item of a list input.
pub struct ParallelWorker {
    pub(crate) base: BaseNode,
    pub(crate) wrapped: Arc<dyn Node>,
    pub(crate) max_concurrency: usize,
    pub(crate) retry_cfg: Option<RetryConfig>,
}

impl ParallelWorker {
    /// Wraps `wrapped`. The wrapped node may not carry its own RetryConfig; the worker's own
    /// RetryConfig is taken out of `cfg` (hidden from the scheduler) and applied per item.
    pub fn new(
        name: &str,
        wrapped: Arc<dyn Node>,
        max_concurrency: usize,
        mut cfg: NodeConfig,
    ) -> Result<Arc<ParallelWorker>, WorkflowError> {
        if wrapped.config().retry_config.is_some() {
            return Err(WorkflowError::new(
                WorkflowErrorKind::Other,
                format!(
                    "ParallelWorker {name}: wrapped node {} cannot have RetryConfig",
                    wrapped.name()
                ),
            ));
        }
        let retry_cfg = cfg.retry_config.take();
        Ok(Arc::new(ParallelWorker {
            base: BaseNode::new(name, "", cfg),
            wrapped,
            max_concurrency,
            retry_cfg,
        }))
    }
}

impl Node for ParallelWorker {
    fn name(&self) -> &str {
        self.base.name()
    }
    fn description(&self) -> &str {
        self.base.description()
    }
    fn config(&self) -> &NodeConfig {
        self.base.config()
    }
    fn input_schema(&self) -> Option<&Value> {
        self.base.input_schema()
    }
    fn output_schema(&self) -> Option<&Value> {
        self.base.output_schema()
    }
    fn validate_input(&self, v: Value) -> Result<Value, WorkflowError> {
        self.base.validate_input(v)
    }
    fn validate_output(&self, v: Value) -> Result<Value, WorkflowError> {
        self.base.validate_output(v)
    }
    /// Runs the wrapped node once per list item (at most
    /// `max_concurrency` at a time; 0 = unlimited), each item under its own sub-branch
    /// `<wrapped>@<i+1>` and retried independently under the worker's RetryConfig. The first
    /// non-retryable failure cancels the in-flight siblings and is the node's error; otherwise one
    /// event carries the per-index outputs. Intermediate wrapped events are suppressed. Each item
    /// task runs on a fresh fiber spawned with [`PARALLEL_WORKER_CREATOR`] (its retries stay on
    /// it). Dropping the stream drops the `JoinSet`, aborting the workers; the engine never drops
    /// it mid-await (the scheduler drains its node tasks), so only a direct user of the stream
    /// sees the abort.
    fn run(&self, ctx: Context, input: Value) -> EventStream {
        let name = self.base.name().to_string();
        let wrapped = Arc::clone(&self.wrapped);
        let max_c = self.max_concurrency;
        let retry = self.retry_cfg.clone();
        Box::pin(async_stream::stream! {
            let (worker_ctx, guard) = ctx.with_agent_cancel();
            let items = match input {
                Value::Array(items) => items,
                other => {
                    yield Err(AgentError::other(format!(
                        "parallel worker {name} expects an array input, found {}",
                        json_kind_article(&other)
                    )));
                    return;
                }
            };
            if items.is_empty() {
                let mut ev = Event::new(ctx.platform(), ctx.invocation_id());
                ev.output = Some(Value::Array(Vec::new()));
                yield Ok(ev);
                return;
            }
            let n = items.len();
            let sem = (max_c > 0).then(|| Arc::new(Semaphore::new(max_c)));
            let first_err: Arc<Mutex<Option<AgentError>>> = Arc::default();
            let token = worker_ctx.cancellation_token().clone();
            // Branch isolation: items 0..N-1 run under sub-branches <wrapped>@1..<wrapped>@N.
            let parent_branch = worker_ctx.branch();
            let mut set: JoinSet<(usize, Option<Value>)> = JoinSet::new();
            for (i, item) in items.into_iter().enumerate() {
                let permit = match &sem {
                    Some(sem) => {
                        let acquired = tokio::select! {
                            biased;
                            () = token.cancelled() => None,
                            p = Arc::clone(sem).acquire_owned() => p.ok(),
                        };
                        // Re-check after acquiring: cancellation may have raced the slot.
                        match acquired {
                            Some(p) if !token.is_cancelled() => Some(p),
                            _ => continue,
                        }
                    }
                    None => {
                        if token.is_cancelled() {
                            continue;
                        }
                        None
                    }
                };
                let item_branch =
                    derive_sub_branch(&parent_branch, &format!("{}@{}", wrapped.name(), i + 1));
                let item_ctx = worker_ctx.with_delta(ContextDelta {
                    branch: Some(item_branch),
                    ..ContextDelta::default()
                });
                let job = Worker {
                    wrapped: Arc::clone(&wrapped),
                    ctx: item_ctx,
                    retry: retry.clone(),
                    first_err: Arc::clone(&first_err),
                };
                // A fiber started by the one running the node.
                let worker_fiber = fiber::spawn_child(PARALLEL_WORKER_CREATOR);
                set.spawn(worker_fiber.scope(job.run(i, item, permit)).in_current_span());
            }
            let mut outputs = vec![Value::Null; n];
            while let Some(res) = set.join_next().await {
                match res {
                    Ok((i, Some(out))) => {
                        if let Some(slot) = outputs.get_mut(i) {
                            *slot = out;
                        }
                    }
                    Ok((_, None)) => {}
                    // A panicking worker re-raises into the node's task, where the scheduler turns
                    // it into the node's failure.
                    Err(e) if e.is_panic() => std::panic::resume_unwind(e.into_panic()),
                    Err(_) => {}
                }
            }
            drop(guard);
            let mut err = first_err.lock().take();
            // Items skipped at dispatch, and workers that return without an error once ctx is
            // done, report no failure. Without this check the node would yield a partial or
            // post-cancellation result as a success.
            if err.is_none() && ctx.cancellation_token().is_cancelled() {
                err = Some(context_err(&ctx));
            }
            if let Some(e) = err {
                yield Err(e);
                return;
            }
            let mut ev = Event::new(ctx.platform(), ctx.invocation_id());
            ev.output = Some(Value::Array(outputs));
            yield Ok(ev);
        })
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The event's output (a `Value::Null` output counts as none).
fn extract_output(ev: &Event) -> Option<Value> {
    ev.output.clone().filter(|v| !v.is_null())
}

/// The output value of an item's worker event: none, the single output, or the list of outputs.
fn make_worker_output(mut outputs: Vec<Value>) -> Option<Value> {
    match outputs.len() {
        0 => None,
        1 => outputs.pop(),
        _ => Some(Value::Array(outputs)),
    }
}

/// The first error wins and cancels the siblings plainly through the worker context's cancel
/// scope, which every item context shares.
fn report_failure(first: &Mutex<Option<AgentError>>, ctx: &Context, err: AgentError) {
    let mut g = first.lock();
    if g.is_none() {
        *g = Some(err);
        ctx.cancel_with_cause(CancelCause::Canceled);
    }
}

/// One attempt under its own `invoke_node` span, so each item and each retry attempt is a
/// distinct span. An `AgentNode`'s attempt is marked as a worker attempt on the item's fiber
/// (`with_attempt(Worker, ..)`), which the trace manager reads to tell worker attempts apart.
async fn run_wrapped_once(
    wrapped: &Arc<dyn Node>,
    ctx: &Context,
    item: Value,
) -> Result<Vec<Value>, AgentError> {
    if wrapped.as_any().is::<AgentNode>() {
        fiber::with_attempt(AttemptSite::Worker, run_wrapped_attempt(wrapped, ctx, item)).await
    } else {
        run_wrapped_attempt(wrapped, ctx, item).await
    }
}

/// One attempt of [`run_wrapped_once`].
async fn run_wrapped_attempt(
    wrapped: &Arc<dyn Node>,
    ctx: &Context,
    item: Value,
) -> Result<Vec<Value>, AgentError> {
    let span = start_node_span(ctx, wrapped);
    let attempt = async {
        let mut outputs = Vec::new();
        let mut s = wrapped.run(ctx.clone(), item);
        while let Some(r) = s.next().await {
            match r {
                Err(e) => return Err(e),
                Ok(ev) => outputs.extend(extract_output(&ev)),
            }
        }
        Ok(outputs)
    };
    let res = match &span {
        Some(sp) => attempt.instrument(sp.clone()).await,
        None => attempt.await,
    };
    record_node_error(span.as_ref(), res.as_ref().err(), None);
    res
}

/// One item's worker and the state it captures.
struct Worker {
    wrapped: Arc<dyn Node>,
    ctx: Context,
    retry: Option<RetryConfig>,
    first_err: Arc<Mutex<Option<AgentError>>>,
}

impl Worker {
    /// Runs the item, retrying under the worker's RetryConfig. Failures are reported before the
    /// semaphore permit is released (the permit drops when this returns).
    async fn run(
        self,
        idx: usize,
        item: Value,
        _permit: Option<OwnedSemaphorePermit>,
    ) -> (usize, Option<Value>) {
        let mut failed_attempts = 0;
        loop {
            match run_wrapped_once(&self.wrapped, &self.ctx, item.clone()).await {
                Ok(outputs) => return (idx, make_worker_output(outputs)),
                Err(run_err) => {
                    failed_attempts += 1;
                    if let Some(cfg) = self
                        .retry
                        .as_ref()
                        .filter(|c| should_retry(c, &run_err, failed_attempts))
                    {
                        let delay = calculate_delay(cfg, failed_attempts, self.ctx.platform());
                        tokio::select! {
                            () = tokio::time::sleep(delay) => continue,
                            () = self.ctx.cancellation_token().cancelled() => {
                                // Cancellation while parked in the retry delay is a real failure.
                                report_failure(&self.first_err, &self.ctx, context_err(&self.ctx));
                                return (idx, None);
                            }
                        }
                    }
                    // Cannot retry or exhausted attempts.
                    report_failure(&self.first_err, &self.ctx, run_err);
                    return (idx, None);
                }
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    use futures::StreamExt;
    use gadk_agent::fiber::{self, AttemptSite, Fiber, PARALLEL_WORKER_CREATOR};
    use gadk_agent::{
        AgentConfig, AgentError, BeforeAgentCallback, CancelCause, Context, InvocationContext,
    };
    use gadk_session::Event;
    use parking_lot::Mutex;
    use serde_json::{Value, json};

    use super::ParallelWorker;
    use crate::workflow::nodes::AgentNode;
    use crate::workflow::{Node, NodeConfig, RetryConfig, default_retry_config, function_node};

    fn ctx() -> Context {
        InvocationContext::builder().build()
    }

    fn retry(max_attempts: i32, delay: Duration) -> NodeConfig {
        NodeConfig {
            retry_config: Some(RetryConfig {
                max_attempts,
                initial_delay: delay,
                max_delay: delay,
                jitter: 0.0,
                ..default_retry_config()
            }),
            ..NodeConfig::default()
        }
    }

    async fn run(
        node: &Arc<ParallelWorker>,
        ctx: Context,
        input: Value,
    ) -> Vec<Result<Event, AgentError>> {
        node.run(ctx, input).collect().await
    }

    /// R2/R3: each item of a wrapped `AgentNode` runs on its own `PARALLEL_WORKER_CREATOR` fiber
    /// started by the fiber running the worker, and every attempt (retries included, on the same
    /// fiber) is claimed by the agent as a `Worker` attempt.
    #[tokio::test]
    async fn agent_node_attempts_are_worker_attempts_on_the_item_fiber() {
        type Seen = Arc<Mutex<Vec<(Fiber, Option<AttemptSite>)>>>;
        let seen: Seen = Arc::default();
        let seen_cb = seen.clone();
        let before: BeforeAgentCallback = Arc::new(move |_ctx: Context| {
            seen_cb
                .lock()
                .push((fiber::current(), fiber::current_attempt()));
            Box::pin(async { Ok(None) })
        });
        let runs = Arc::new(AtomicUsize::new(0));
        let runs_agent = runs.clone();
        let agent = gadk_agent::new_agent(AgentConfig {
            name: "flaky".into(),
            before_agent_callbacks: vec![before],
            run: Some(Arc::new(move |_ctx: InvocationContext| {
                let n = runs_agent.fetch_add(1, Ordering::SeqCst);
                Box::pin(async_stream::stream! {
                    if n == 0 {
                        yield Err(AgentError::other("flaky"));
                    }
                })
            })),
            ..Default::default()
        })
        .unwrap();
        let wrapped: Arc<dyn Node> = AgentNode::new(agent, NodeConfig::default()).unwrap();
        let worker = ParallelWorker::new("pw", wrapped, 0, retry(3, Duration::ZERO)).unwrap();
        let outer = Fiber::root();
        let outer_id = outer.id;
        let items = outer.scope(run(&worker, ctx(), json!(["a"]))).await;
        assert_eq!(items.len(), 1);
        assert!(items[0].is_ok(), "{:?}", items[0]);
        let seen = seen.lock().clone();
        assert_eq!(seen.len(), 2, "one record per attempt");
        for (f, attempt) in &seen {
            assert_eq!(*attempt, Some(AttemptSite::Worker));
            assert_eq!(f.origin.creator, PARALLEL_WORKER_CREATOR);
            assert_eq!(f.origin.parent, outer_id);
            assert_ne!(f.id, outer_id);
        }
        assert_eq!(
            seen[0].0.id, seen[1].0.id,
            "a retry stays on the item's fiber"
        );
    }

    /// R7: the first failure cancels the in-flight siblings plainly (Go `cancelFunc()`).
    #[tokio::test]
    async fn the_first_failure_cancels_the_siblings_plainly() {
        let causes: Arc<Mutex<Vec<Option<CancelCause>>>> = Arc::default();
        let causes_node = causes.clone();
        let wrapped = function_node::<String, String, _, _>(
            "item",
            move |c: Context, input: String| {
                let causes = causes_node.clone();
                async move {
                    if input == "fail" {
                        return Err(AgentError::other("boom"));
                    }
                    c.cancellation_token().cancelled().await;
                    causes.lock().push(c.cause());
                    Ok(input)
                }
            },
            NodeConfig::default(),
        );
        let worker = ParallelWorker::new("pw", wrapped, 0, NodeConfig::default()).unwrap();
        let items = run(&worker, ctx(), json!(["wait", "fail"])).await;
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].as_ref().unwrap_err().to_string(), "boom");
        let causes = causes.lock();
        assert_eq!(causes.len(), 1);
        assert!(
            matches!(causes[0], Some(CancelCause::Canceled)),
            "{causes:?}"
        );
    }

    /// Go `reportFailure(ctx.Err())`: a worker parked in its retry delay when the enclosing
    /// node's timeout fires fails with `context deadline exceeded`, not `context canceled`.
    #[tokio::test(start_paused = true)]
    async fn a_deadline_during_the_retry_delay_is_the_workers_error() {
        let wrapped = function_node::<String, String, _, _>(
            "item",
            |_c: Context, _input: String| async move { Err(AgentError::other("boom")) },
            NodeConfig::default(),
        );
        let worker =
            ParallelWorker::new("pw", wrapped, 0, retry(5, Duration::from_secs(1))).unwrap();
        let (tctx, _guard) = ctx().with_agent_timeout(Duration::from_millis(10));
        let items = run(&worker, tctx, json!(["a"])).await;
        assert_eq!(items.len(), 1);
        assert!(
            matches!(items[0], Err(AgentError::DeadlineExceeded)),
            "{:?}",
            items[0]
        );
    }
}