periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Comprehensive Error Handling Tests
//!
//! Tests for retry logic, exponential backoff, and fallback agents

use periplon_sdk::dsl::executor::DSLExecutor;
use periplon_sdk::dsl::schema::{DSLWorkflow, ErrorHandlingSpec, TaskSpec};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::Instant;

/// Create a test workflow directory
fn setup_test_workspace(test_name: &str) -> String {
    let workspace_dir = format!("test_results/{}", test_name);
    let _ = fs::remove_dir_all(&workspace_dir);
    fs::create_dir_all(&workspace_dir).unwrap();
    workspace_dir
}

/// Cleanup test workspace
fn cleanup_test_workspace(workspace_dir: &str) {
    let _ = fs::remove_dir_all(workspace_dir);
}

#[tokio::test]
async fn test_basic_retry_mechanism() {
    let workspace_dir = setup_test_workspace("basic_retry");

    // Create a script that fails twice, then succeeds
    let script_path = format!("{}/retry_script.sh", workspace_dir);
    let counter_file = format!("{}/counter.txt", workspace_dir);

    fs::write(
        &script_path,
        format!(
            r#"#!/bin/bash
if [ ! -f "{counter_file}" ]; then
    echo "0" > "{counter_file}"
fi
count=$(cat "{counter_file}")
count=$((count + 1))
echo "$count" > "{counter_file}"
if [ "$count" -lt 3 ]; then
    echo "Attempt $count - failing"
    exit 1
else
    echo "Attempt $count - success"
    exit 0
fi
"#,
            counter_file = counter_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "Basic Retry Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    // Add task with retry configuration
    let task = TaskSpec {
        description: "Task that retries".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![script_path.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 3,
            fallback_agent: None,
            retry_delay_secs: 1,
            exponential_backoff: false,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("retry_task".to_string(), task);

    // Execute workflow
    let mut executor = DSLExecutor::new(workflow).expect("Failed to create executor");
    executor
        .initialize()
        .await
        .expect("Failed to initialize executor");
    let result = executor.execute().await;

    // Should succeed after retries
    assert!(result.is_ok(), "Workflow should succeed after retries");

    // Check that it took 3 attempts
    let counter_content = fs::read_to_string(&counter_file).unwrap();
    let count: u32 = counter_content.trim().parse().unwrap();
    assert_eq!(count, 3, "Should have taken 3 attempts");

    cleanup_test_workspace(&workspace_dir);
}

#[tokio::test]
async fn test_exponential_backoff() {
    let workspace_dir = setup_test_workspace("exponential_backoff");

    // Create a script that always fails (to test backoff timing)
    let script_path = format!("{}/failing_script.sh", workspace_dir);
    let timestamp_file = format!("{}/timestamps.txt", workspace_dir);

    fs::write(
        &script_path,
        format!(
            r#"#!/bin/bash
date +%s >> "{timestamp_file}"
exit 1
"#,
            timestamp_file = timestamp_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "Exponential Backoff Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    // Add task with exponential backoff
    let task = TaskSpec {
        description: "Task with exponential backoff".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![script_path.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 3,
            fallback_agent: None,
            retry_delay_secs: 1,
            exponential_backoff: true,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("backoff_task".to_string(), task);

    // Execute workflow (will fail, but we're testing timing)
    let start_time = Instant::now();
    let mut executor = DSLExecutor::new(workflow).expect("Failed to create executor");
    executor
        .initialize()
        .await
        .expect("Failed to initialize executor");
    let _ = executor.execute().await;
    let elapsed = start_time.elapsed();

    // With exponential backoff: 1s, 2s, 4s = 7s total minimum
    // Allow some tolerance for execution time
    assert!(
        elapsed.as_secs() >= 7,
        "Exponential backoff should take at least 7 seconds, took {}s",
        elapsed.as_secs()
    );

    // Verify timestamps show increasing delays
    if Path::new(&timestamp_file).exists() {
        let timestamps_content = fs::read_to_string(&timestamp_file).unwrap();
        let timestamps: Vec<i64> = timestamps_content
            .lines()
            .filter_map(|line| line.trim().parse().ok())
            .collect();

        if timestamps.len() >= 3 {
            let delay1 = timestamps[1] - timestamps[0];
            let delay2 = timestamps[2] - timestamps[1];

            // Second delay should be roughly 2x first delay (exponential)
            // Allow tolerance for execution overhead
            assert!(
                delay2 >= delay1,
                "Delays should be increasing: {}s, {}s",
                delay1,
                delay2
            );
        }
    }

    cleanup_test_workspace(&workspace_dir);
}

#[tokio::test]
async fn test_fallback_agent_success() {
    let workspace_dir = setup_test_workspace("fallback_agent");

    // Create scripts: primary fails, fallback succeeds
    let primary_script = format!("{}/primary.sh", workspace_dir);
    let fallback_script = format!("{}/fallback.sh", workspace_dir);
    let output_file = format!("{}/output.txt", workspace_dir);

    fs::write(
        &primary_script,
        r#"#!/bin/bash
echo "Primary agent failed"
exit 1
"#,
    )
    .unwrap();

    fs::write(
        &fallback_script,
        format!(
            r#"#!/bin/bash
echo "Fallback agent succeeded" > "{output_file}"
exit 0
"#,
            output_file = output_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        for script in &[&primary_script, &fallback_script] {
            let mut perms = fs::metadata(script).unwrap().permissions();
            perms.set_mode(0o755);
            fs::set_permissions(script, perms).unwrap();
        }
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "Fallback Agent Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    // Note: Fallback agents are designed for agent-based tasks, not command tasks
    // This test demonstrates the configuration structure
    // In a real scenario, you would use actual agents with the SDK

    let task = TaskSpec {
        description: "Task with fallback".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![primary_script.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 0, // No retries, go straight to fallback
            fallback_agent: Some("fallback_agent".to_string()),
            retry_delay_secs: 1,
            exponential_backoff: false,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("fallback_task".to_string(), task);

    // The fallback mechanism is implemented for agent-based tasks
    // For this test, we verify the configuration structure is correct
    assert!(workflow.tasks.contains_key("fallback_task"));
    let task = workflow.tasks.get("fallback_task").unwrap();
    assert!(task.on_error.is_some());
    let error_handling = task.on_error.as_ref().unwrap();
    assert_eq!(
        error_handling.fallback_agent,
        Some("fallback_agent".to_string())
    );

    cleanup_test_workspace(&workspace_dir);
}

#[tokio::test]
async fn test_retry_with_delay() {
    let workspace_dir = setup_test_workspace("retry_with_delay");

    let script_path = format!("{}/delay_script.sh", workspace_dir);
    let timestamp_file = format!("{}/timestamps.txt", workspace_dir);

    fs::write(
        &script_path,
        format!(
            r#"#!/bin/bash
date +%s >> "{timestamp_file}"
exit 1
"#,
            timestamp_file = timestamp_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "Retry Delay Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    let task = TaskSpec {
        description: "Task with retry delay".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![script_path.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 2,
            fallback_agent: None,
            retry_delay_secs: 2,
            exponential_backoff: false,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("delay_task".to_string(), task);

    let start_time = Instant::now();
    let mut executor = DSLExecutor::new(workflow).expect("Failed to create executor");
    executor
        .initialize()
        .await
        .expect("Failed to initialize executor");
    let _ = executor.execute().await;
    let elapsed = start_time.elapsed();

    // With 2 retries and 2s delay: 2s + 2s = 4s minimum
    assert!(
        elapsed.as_secs() >= 4,
        "Retry delay should be at least 4 seconds, was {}s",
        elapsed.as_secs()
    );

    cleanup_test_workspace(&workspace_dir);
}

#[tokio::test]
async fn test_error_handling_config_validation() {
    // Test that ErrorHandlingSpec is correctly configured
    let error_spec = ErrorHandlingSpec {
        retry: 3,
        fallback_agent: Some("backup_agent".to_string()),
        retry_delay_secs: 5,
        exponential_backoff: true,
    };

    assert_eq!(error_spec.retry, 3);
    assert_eq!(error_spec.fallback_agent, Some("backup_agent".to_string()));
    assert_eq!(error_spec.retry_delay_secs, 5);
    assert!(error_spec.exponential_backoff);
}

#[tokio::test]
async fn test_retry_exhaustion() {
    let workspace_dir = setup_test_workspace("retry_exhaustion");

    // Create a script that always fails
    let script_path = format!("{}/always_fail.sh", workspace_dir);
    let counter_file = format!("{}/attempts.txt", workspace_dir);

    fs::write(
        &script_path,
        format!(
            r#"#!/bin/bash
if [ ! -f "{counter_file}" ]; then
    echo "0" > "{counter_file}"
fi
count=$(cat "{counter_file}")
count=$((count + 1))
echo "$count" > "{counter_file}"
echo "Attempt $count - failing"
exit 1
"#,
            counter_file = counter_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "Retry Exhaustion Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    let task = TaskSpec {
        description: "Task that exhausts retries".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![script_path.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 3,
            fallback_agent: None,
            retry_delay_secs: 1,
            exponential_backoff: false,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("exhaust_task".to_string(), task);

    let mut executor = DSLExecutor::new(workflow).expect("Failed to create executor");
    executor
        .initialize()
        .await
        .expect("Failed to initialize executor");
    let result = executor.execute().await;

    // Should fail after all retries
    assert!(
        result.is_err(),
        "Workflow should fail after exhausting retries"
    );

    // Verify it tried the correct number of times (initial + 3 retries = 4 total)
    let counter_content = fs::read_to_string(&counter_file).unwrap();
    let count: u32 = counter_content.trim().parse().unwrap();
    assert_eq!(
        count, 4,
        "Should have 4 total attempts (1 initial + 3 retries)"
    );

    cleanup_test_workspace(&workspace_dir);
}

#[tokio::test]
async fn test_no_retry_on_success() {
    let workspace_dir = setup_test_workspace("no_retry_success");

    // Create a script that succeeds immediately
    let script_path = format!("{}/success_script.sh", workspace_dir);
    let counter_file = format!("{}/success_counter.txt", workspace_dir);

    fs::write(
        &script_path,
        format!(
            r#"#!/bin/bash
if [ ! -f "{counter_file}" ]; then
    echo "0" > "{counter_file}"
fi
count=$(cat "{counter_file}")
count=$((count + 1))
echo "$count" > "{counter_file}"
echo "Success on attempt $count"
exit 0
"#,
            counter_file = counter_file
        ),
    )
    .unwrap();

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let mut workflow = DSLWorkflow {
        provider: Default::default(),
        model: None,
        name: "No Retry on Success Test".to_string(),
        version: "1.0.0".to_string(),
        dsl_version: "1.0.0".to_string(),
        cwd: Some(workspace_dir.clone()),
        create_cwd: Some(true),
        agents: HashMap::new(),
        tasks: HashMap::new(),
        workflows: HashMap::new(),
        tools: None,
        communication: None,
        mcp_servers: HashMap::new(),
        subflows: HashMap::new(),
        imports: HashMap::new(),
        notifications: None,
        secrets: HashMap::new(),
        inputs: HashMap::new(),
        outputs: HashMap::new(),
        limits: None,
    };

    let task = TaskSpec {
        description: "Successful task with retry config".to_string(),
        command: Some(periplon_sdk::dsl::schema::CommandSpec {
            executable: "bash".to_string(),
            args: vec![script_path.clone()],
            working_dir: None,
            env: HashMap::new(),
            timeout_secs: None,
            capture_stdout: true,
            capture_stderr: true,
        }),
        on_error: Some(ErrorHandlingSpec {
            retry: 5,
            fallback_agent: None,
            retry_delay_secs: 1,
            exponential_backoff: false,
        }),
        ..Default::default()
    };

    workflow.tasks.insert("success_task".to_string(), task);

    let mut executor = DSLExecutor::new(workflow).expect("Failed to create executor");
    executor
        .initialize()
        .await
        .expect("Failed to initialize executor");
    let result = executor.execute().await;

    assert!(result.is_ok(), "Workflow should succeed");

    // Should only execute once (no retries on success)
    let counter_content = fs::read_to_string(&counter_file).unwrap();
    let count: u32 = counter_content.trim().parse().unwrap();
    assert_eq!(count, 1, "Should only execute once on success");

    cleanup_test_workspace(&workspace_dir);
}