gadk-engine 0.4.3

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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// 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.

//! Port of Go workflow/review_cleanup_retry_test.go.

use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};

use gadk_agent::{AgentError, Context};
use gadk_session::{Event, NodeInfo};
use serde_json::{Value, json};

use crate::error::{WorkflowError, WorkflowErrorKind};
use crate::workflow::dynamic_scheduler::new_dynamic_sub_scheduler;
use crate::workflow::failure_inventory::FailureInventory;
use crate::workflow::persistence::{WORKFLOW_FAILED_CHILD_PATHS_KEY, workflow_node_outcome};
use crate::workflow::scheduler::test_support::*;
use crate::workflow::scheduler::{CompletionItem, EventItem, NodeRun, QueueItem, Scheduler};
use crate::workflow::state::{NodeState, NodeStatus};
use crate::workflow::{
    Emit, NodeConfig, RetryConfig, RunNodeOptions, dynamic_node, function_node, run_node,
};

#[rstest::rstest]
#[case::success_without_event(false)]
#[case::retryable_error_without_event(true)]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
// parity: adapted: Go loops 32 trials hoping map order schedules the valid node before the
// rejection and waits on a span exporter; Rust's resume iterates node names sorted, so the nodes
// are named "a_valid"/"b_invalid" (interrupt ids stay "valid"/"invalid") to make that order
// deterministic, and the test waits on the node's call counter instead of the exporter.
async fn test_resume_rejected_response_completion_does_not_dispatch(#[case] retry: bool) {
    use std::time::{Duration, Instant};

    use futures::StreamExt;

    use crate::workflow::RunState;

    let (valid_calls, successor_calls) = (Arc::new(AtomicI32::new(0)), Arc::new(AtomicI32::new(0)));
    let vc = Arc::clone(&valid_calls);
    let cfg = if retry {
        NodeConfig {
            retry_config: Some(RetryConfig {
                max_attempts: 2,
                ..RetryConfig::default()
            }),
            ..NodeConfig::default()
        }
    } else {
        NodeConfig::default()
    };
    let valid = dynamic_node::<Value, Value, _, _>(
        "a_valid",
        move |_c: Context, _i: Value, _e: Emit| {
            let n = vc.fetch_add(1, Ordering::SeqCst) + 1;
            async move {
                if n == 1 && retry {
                    Err(test_err("scripted parent failure"))
                } else {
                    Ok(Value::Null)
                }
            }
        },
        cfg,
    );
    let sc = Arc::clone(&successor_calls);
    let next = function_node::<Value, Value, _, _>(
        "next",
        move |_c: Context, _i: Value| {
            sc.fetch_add(1, Ordering::SeqCst);
            async { Ok(Value::Null) }
        },
        NodeConfig::default(),
    );
    let invalid = dynamic_node::<Value, Value, _, _>(
        "b_invalid",
        |_c: Context, _i: Value, _e: Emit| async { Ok(Value::Null) },
        NodeConfig::default(),
    );
    let wf = must_new(vec![
        edge(&start(), &valid),
        edge(&valid, &next),
        edge(&start(), &invalid),
    ]);
    let new_state = || {
        let mut state = RunState::default();
        let mut valid_inputs = serde_json::Map::new();
        valid_inputs.insert("valid".into(), json!("ok"));
        state.nodes.insert(
            "a_valid".into(),
            NodeState {
                status: NodeStatus::Pending,
                resumed_inputs: Some(valid_inputs),
                ..NodeState::default()
            },
        );
        let mut invalid_inputs = serde_json::Map::new();
        invalid_inputs.insert("invalid".into(), json!(1));
        let mut ns = NodeState {
            status: NodeStatus::Failed,
            resumed_inputs: Some(invalid_inputs),
            ..NodeState::default()
        };
        ns.interrupt_schemas
            .insert("invalid".into(), json!({"type": "integer"}));
        state.nodes.insert("b_invalid".into(), ns);
        state
    };
    let responses = |invalid: Value| {
        let mut m = serde_json::Map::new();
        m.insert("valid".into(), json!("ok"));
        m.insert("invalid".into(), invalid);
        m
    };

    let mut s = wf.resume(top_level_ctx(), new_state(), responses(json!("wrong")));
    let mut rejected = false;
    while let Some(item) = s.next().await {
        if let Err(e) = item
            && is_kind(&e, WorkflowErrorKind::InvalidResumeResponse)
        {
            rejected = true;
            // The valid node was scheduled before the rejection; let it run once.
            let deadline = Instant::now() + Duration::from_secs(2);
            while valid_calls.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
                tokio::time::sleep(Duration::from_millis(1)).await;
            }
            break;
        }
    }
    assert!(rejected, "fixture did not reject the invalid response");
    drop(s); // consumer exit
    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(
        valid_calls.load(Ordering::SeqCst),
        1,
        "cleanup dispatched a retry after consumer exit"
    );
    assert_eq!(
        successor_calls.load(Ordering::SeqCst),
        0,
        "cleanup dispatched a successor after consumer exit"
    );

    // Reload the last persisted state: cleanup events were not consumed.
    let (_, err) = collect(wf.resume(top_level_ctx(), new_state(), responses(json!(42)))).await;
    assert!(
        err.is_none(),
        "corrected response could not resume: {err:?}"
    );
    assert_eq!(
        valid_calls.load(Ordering::SeqCst),
        2,
        "corrected response repeated or skipped the valid node"
    );
    assert_eq!(
        successor_calls.load(Ordering::SeqCst),
        1,
        "corrected response repeated or skipped the successor"
    );
}

#[rstest::rstest]
#[case::queued_success(false, false)]
#[case::queued_retryable_failure(true, false)]
#[case::queued_event_and_completion(false, true)]
#[tokio::test]
async fn test_scheduler_initial_drain_does_not_dispatch(
    #[case] failure: bool,
    #[case] with_event: bool,
) {
    let successor_calls = Arc::new(AtomicI32::new(0));
    let sc = Arc::clone(&successor_calls);
    let n = dynamic_node::<Value, Value, _, _>(
        "valid",
        |_c: Context, _i: Value, _e: Emit| async { Ok(Value::Null) },
        NodeConfig {
            retry_config: Some(RetryConfig {
                max_attempts: 2,
                ..RetryConfig::default()
            }),
            ..NodeConfig::default()
        },
    );
    let next = function_node::<Value, Value, _, _>(
        "next",
        move |_c: Context, _i: Value| {
            sc.fetch_add(1, Ordering::SeqCst);
            async { Ok(Value::Null) }
        },
        NodeConfig::default(),
    );
    let wf = must_new(vec![edge(&start(), &n), edge(&n, &next)]);
    let ctx = top_level_ctx();
    let mut s = Scheduler::new(ctx.clone(), Arc::clone(&wf.graph), 0);
    s.state.nodes.insert(
        "valid".into(),
        NodeState {
            status: NodeStatus::Running,
            ..NodeState::default()
        },
    );
    s.runs_by_name
        .insert("valid".into(), NodeRun::with_path("valid@1"));
    let (ptx, mut prx) = tokio::sync::oneshot::channel();
    if with_event {
        let ev = Event::new(ctx.platform(), ctx.invocation_id());
        s.tx.send(QueueItem::Event(Box::new(EventItem {
            node_name: "valid".into(),
            ev,
            processed: Some(ptx),
        })))
        .await
        .unwrap_or_else(|_| panic!("queue"));
    }
    let err = failure.then(|| test_err("scripted parent failure"));
    s.tx.send(QueueItem::Completion(CompletionItem {
        node_name: "valid".into(),
        err,
        inventory: FailureInventory::default(),
    }))
    .await
    .unwrap_or_else(|_| panic!("queue"));
    s.cancel_all();
    // `drain` never yields (Go's yield counter stays 0).
    s.drain().await;
    assert!(
        successor_calls.load(Ordering::SeqCst) == 0
            && s.retry_timers.is_empty()
            && s.runs_by_name.is_empty(),
        "initial drain yielded or dispatched new work"
    );
    if with_event {
        assert!(
            prx.try_recv().is_ok(),
            "initial drain did not release the producer handshake"
        );
    }
}

#[tokio::test]
async fn test_scheduler_initial_drain_does_not_finalize() {
    let a = function_node::<Value, Value, _, _>(
        "a",
        |_c: Context, _i: Value| async { Ok(Value::Null) },
        NodeConfig::default(),
    );
    let b = function_node::<Value, Value, _, _>(
        "b",
        |_c: Context, _i: Value| async { Ok(Value::Null) },
        NodeConfig::default(),
    );
    let wf = must_new(vec![edge(&start(), &a), edge(&start(), &b)]);
    let mut s = Scheduler::new(top_level_ctx(), Arc::clone(&wf.graph), 0);
    for name in ["a", "b"] {
        s.state.nodes.insert(
            name.into(),
            NodeState {
                status: NodeStatus::Completed,
                output: Some(Value::String(name.into())),
                ..NodeState::default()
            },
        );
    }
    s.cancel_all();
    // `drain` never yields, so a finalize error (two terminal outputs) can never surface.
    s.drain().await;
    assert!(
        s.next().await.is_none(),
        "initial drain finalized or yielded"
    );
}

#[tokio::test]
async fn test_sub_scheduler_empty_failure_inventory_persists() {
    let ctx = top_level_ctx();
    let sub = new_dynamic_sub_scheduler(ctx.clone(), "parent".into(), noop_emit());
    let inventory = sub.child_failures();
    assert!(
        inventory.known && inventory.paths.is_empty(),
        "known empty inventory was lost"
    );
    let wf = must_new(vec![edge(&start(), &dummy_node("parent"))]);
    let mut sched = Scheduler::new(ctx, Arc::clone(&wf.graph), 0);
    sched.state.nodes.insert(
        "parent".into(),
        NodeState {
            status: NodeStatus::Failed,
            ..NodeState::default()
        },
    );
    let ev = sched
        .completion_event("parent", Some("parent"), &inventory)
        .expect("completion event");
    let data = serde_json::to_string(&ev).expect("failure record serialization failed");
    let persisted: Event =
        serde_json::from_str(&data).expect("failure record deserialization failed");
    assert_eq!(
        persisted
            .response
            .custom_metadata
            .get(WORKFLOW_FAILED_CHILD_PATHS_KEY),
        Some(&json!([])),
        "empty inventory was omitted or persisted as null"
    );
    let unknown = sched
        .completion_event("parent", Some("parent"), &FailureInventory::default())
        .expect("completion event");
    assert!(
        !unknown
            .response
            .custom_metadata
            .contains_key(WORKFLOW_FAILED_CHILD_PATHS_KEY),
        "unknown inventory was recorded as known"
    );
}

#[rstest::rstest]
#[case::recovered(None)]
#[case::known_empty(Some(vec![]))]
#[case::new_failure(Some(vec!["parent/mid@1/other@1"]))]
#[tokio::test]
async fn test_sub_scheduler_recovered_nested_failure_inventory(#[case] paths: Option<Vec<&str>>) {
    const MID: &str = "parent/mid@1";
    const LEAF: &str = "parent/mid@1/leaf@1";
    const SIBLING: &str = "parent/mid@10/leaf@1";
    let sub = new_dynamic_sub_scheduler(top_level_ctx(), "parent".into(), noop_emit());
    sub.state().failed_paths.insert(SIBLING.into());
    let failure = || WorkflowError::new(WorkflowErrorKind::Other, "scripted child failure");
    sub.finish_run(
        MID,
        Err(failure()),
        true,
        &FailureInventory {
            known: true,
            paths: vec![LEAF.into()],
        },
    );
    {
        let st = sub.state();
        assert!(
            st.failed_paths.contains(LEAF) && st.failed_paths.len() == 2,
            "nested inventory was widened or lost"
        );
    }
    let paths: Option<Vec<String>> = paths.map(|p| p.into_iter().map(String::from).collect());
    let (res, inventory) = match &paths {
        None => (Ok(json!("recovered")), FailureInventory::default()),
        Some(p) => (
            Err(failure()),
            FailureInventory {
                known: true,
                paths: p.clone(),
            },
        ),
    };
    sub.finish_run(MID, res, true, &inventory);
    let st = sub.state();
    let want_len = 1 + paths.as_ref().map_or(0, Vec::len);
    assert!(
        st.failed_paths.contains(SIBLING) && st.failed_paths.len() == want_len,
        "new child inventory retained stale failures or cleared another subtree: {:?}",
        st.failed_paths
    );
    for path in paths.iter().flatten() {
        assert!(
            st.failed_paths.contains(path),
            "new child inventory omitted a failed path"
        );
    }
}

#[rstest::rstest]
#[case::recorded_inventory(true)]
#[case::rejected_marker(false)]
#[tokio::test]
async fn test_sub_scheduler_unrecorded_child_failure_remains_conservative(#[case] recorded: bool) {
    const PARENT: &str = "parent";
    const MID_PATH: &str = "parent/mid@stable";
    const CHARGE_PATH: &str = "parent/mid@stable/charge@charge";
    let history: Arc<parking_lot::Mutex<Vec<Event>>> = Arc::default();
    let h = Arc::clone(&history);
    let emit: Emit = Arc::new(move |ev: Event| {
        let rejected = !workflow_node_outcome(&ev).is_empty()
            && ev.node_info.as_ref().is_some_and(|i| i.path == MID_PATH)
            && !recorded;
        if !rejected {
            h.lock().push(ev);
        }
        Box::pin(async move {
            if rejected {
                Err(AgentError::other("scripted marker rejection"))
            } else {
                Ok(())
            }
        })
    });
    let sub = new_dynamic_sub_scheduler(ctx_with_events(Vec::new()), PARENT.into(), emit);
    let charge = function_node::<Value, String, _, _>(
        "charge",
        |_c: Context, _i: Value| async { Ok("charged".to_string()) },
        NodeConfig::default(),
    );
    let mid = dynamic_node::<Value, Value, _, _>(
        "mid",
        move |ctx: Context, input: Value, emit: Emit| {
            let charge = Arc::clone(&charge);
            async move {
                run_node(
                    &ctx,
                    charge,
                    input,
                    RunNodeOptions {
                        run_id: Some("charge".into()),
                        ..RunNodeOptions::default()
                    },
                )
                .await?;
                let mut partial = output_event(&ctx, json!("partial"));
                partial.node_info = Some(NodeInfo {
                    path: ctx.path(),
                    ..NodeInfo::default()
                });
                emit(partial).await?;
                Err::<Value, _>(test_err("scripted nested body failure"))
            }
        },
        NodeConfig::default(),
    );
    let err = sub
        .run_child(
            mid,
            Value::Null,
            RunNodeOptions {
                run_id: Some("stable".into()),
                ..RunNodeOptions::default()
            },
        )
        .await
        .expect_err("fixture did not fail");
    assert_eq!(
        find_cause::<TestError>(&err),
        Some(&TestError("scripted nested body failure")),
        "fixture did not preserve the nested failure cause: {err}"
    );
    let wf = must_new(vec![edge(&start(), &dummy_node(PARENT))]);
    let mut sched = Scheduler::new(ctx_with_events(Vec::new()), Arc::clone(&wf.graph), 0);
    sched.state.nodes.insert(
        PARENT.into(),
        NodeState {
            status: NodeStatus::Failed,
            ..NodeState::default()
        },
    );
    let completion = sched
        .completion_event(PARENT, Some(PARENT), &sub.child_failures())
        .expect("completion event");
    history.lock().push(completion);
    let data = serde_json::to_string(&*history.lock()).expect("history serialization failed");
    let persisted: Vec<Event> =
        serde_json::from_str(&data).expect("history deserialization failed");
    let rebuilt = new_dynamic_sub_scheduler(ctx_with_events(persisted), PARENT.into(), noop_emit());
    let st = rebuilt.state();
    assert!(
        !st.result_by_path.contains_key(MID_PATH),
        "failed child's own partial output survived"
    );
    assert_eq!(
        st.result_by_path.contains_key(CHARGE_PATH),
        recorded,
        "unknown failure scope was trusted or known child completion was discarded"
    );
}