uira-orchestration 0.1.1

Agent definitions, SDK, tool registry, and hook implementations for Uira
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
pub mod types;

pub use types::*;

use std::collections::HashMap;
use std::process::Command;
use std::time::{Duration, SystemTime};

/// Standard verification checks used across workflows
pub fn standard_checks() -> HashMap<&'static str, VerificationCheck> {
    let mut checks = HashMap::new();

    checks.insert(
        "build",
        VerificationCheck {
            id: "build".to_string(),
            name: "Build Success".to_string(),
            description: "Code compiles without errors".to_string(),
            evidence_type: VerificationEvidenceType::BuildSuccess,
            required: true,
            command: Some("npm run build".to_string()),
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "test",
        VerificationCheck {
            id: "test".to_string(),
            name: "Tests Pass".to_string(),
            description: "All tests pass without errors".to_string(),
            evidence_type: VerificationEvidenceType::TestPass,
            required: true,
            command: Some("npm test".to_string()),
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "lint",
        VerificationCheck {
            id: "lint".to_string(),
            name: "Lint Clean".to_string(),
            description: "No linting errors".to_string(),
            evidence_type: VerificationEvidenceType::LintClean,
            required: true,
            command: Some("npm run lint".to_string()),
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "functionality",
        VerificationCheck {
            id: "functionality".to_string(),
            name: "Functionality Verified".to_string(),
            description: "All requested features work as described".to_string(),
            evidence_type: VerificationEvidenceType::FunctionalityVerified,
            required: true,
            command: None,
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "architect",
        VerificationCheck {
            id: "architect".to_string(),
            name: "Architect Approval".to_string(),
            description: "Architect has reviewed and approved the implementation".to_string(),
            evidence_type: VerificationEvidenceType::ArchitectApproval,
            required: true,
            command: None,
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "todo",
        VerificationCheck {
            id: "todo".to_string(),
            name: "TODO Complete".to_string(),
            description: "Zero pending or in_progress tasks".to_string(),
            evidence_type: VerificationEvidenceType::TodoComplete,
            required: true,
            command: None,
            completed: false,
            evidence: None,
        },
    );

    checks.insert(
        "error_free",
        VerificationCheck {
            id: "error_free".to_string(),
            name: "Error Free".to_string(),
            description: "Zero unaddressed errors".to_string(),
            evidence_type: VerificationEvidenceType::ErrorFree,
            required: true,
            command: None,
            completed: false,
            evidence: None,
        },
    );

    checks
}

/// Create a verification protocol
pub fn create_protocol(
    name: String,
    description: String,
    checks: Vec<VerificationCheck>,
    strict_mode: bool,
) -> VerificationProtocol {
    VerificationProtocol {
        name,
        description,
        checks,
        strict_mode,
        custom_validator: None,
    }
}

/// Create a verification checklist from a protocol
pub fn create_checklist(protocol: VerificationProtocol) -> VerificationChecklist {
    VerificationChecklist {
        checks: protocol.checks.clone(),
        protocol,
        started_at: SystemTime::now(),
        completed_at: None,
        status: VerificationStatus::Pending,
        summary: None,
    }
}

/// Run a single verification check
async fn run_single_check(
    check: &VerificationCheck,
    options: &VerificationOptions,
) -> VerificationEvidence {
    // If check has a command, run it
    // Note: timeout from options is not currently enforced (see run_command_without_timeout docs)
    if let Some(cmd) = &check.command {
        match run_command_without_timeout(cmd, options.cwd.as_deref()) {
            Ok((stdout, stderr)) => VerificationEvidence {
                evidence_type: check.evidence_type,
                passed: true,
                command: Some(cmd.clone()),
                output: Some(if !stdout.is_empty() { stdout } else { stderr }),
                error: None,
                timestamp: SystemTime::now(),
                metadata: None,
            },
            Err(err) => VerificationEvidence {
                evidence_type: check.evidence_type,
                passed: false,
                command: Some(cmd.clone()),
                output: None,
                error: Some(err),
                timestamp: SystemTime::now(),
                metadata: None,
            },
        }
    } else {
        // Manual verification checks (no command)
        let mut metadata = HashMap::new();
        metadata.insert(
            "requiresManualVerification".to_string(),
            serde_json::Value::Bool(true),
        );

        VerificationEvidence {
            evidence_type: check.evidence_type,
            passed: false,
            command: None,
            output: None,
            error: None,
            timestamp: SystemTime::now(),
            metadata: Some(metadata),
        }
    }
}

/// Run command with timeout
///
/// NOTE: The timeout parameter is currently not enforced. The function will block
/// until the command completes. For actual timeout support, use an async runtime
/// with `tokio::time::timeout` wrapping the command execution.
fn run_command_without_timeout(cmd: &str, cwd: Option<&str>) -> Result<(String, String), String> {
    let mut command = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.args(["/C", cmd]);
        c
    } else {
        let mut c = Command::new("sh");
        c.args(["-c", cmd]);
        c
    };

    if let Some(dir) = cwd {
        command.current_dir(dir);
    }

    // Note: Rust's std::process::Command doesn't have built-in timeout
    // For timeout support, wrap this in tokio::time::timeout when using async
    match command.output() {
        Ok(output) => {
            if output.status.success() {
                Ok((
                    String::from_utf8_lossy(&output.stdout).to_string(),
                    String::from_utf8_lossy(&output.stderr).to_string(),
                ))
            } else {
                Err(format!(
                    "Command failed with exit code: {:?}",
                    output.status.code()
                ))
            }
        }
        Err(e) => Err(format!("Failed to execute command: {}", e)),
    }
}

/// Execute all verification checks
pub async fn run_verification(
    mut checklist: VerificationChecklist,
    options: VerificationOptions,
) -> VerificationChecklist {
    checklist.status = VerificationStatus::InProgress;

    let check_ids_to_run: Vec<String> = if options.skip_optional {
        checklist
            .checks
            .iter()
            .filter(|c| c.required)
            .map(|c| c.id.clone())
            .collect()
    } else {
        checklist.checks.iter().map(|c| c.id.clone()).collect()
    };

    if options.parallel && !options.fail_fast {
        // Run all checks in parallel
        let mut handles = vec![];
        for check_id in &check_ids_to_run {
            if let Some(check) = checklist.checks.iter().find(|c| &c.id == check_id) {
                let check_clone = check.clone();
                let options_clone = options.clone();
                handles.push(tokio::spawn(async move {
                    (
                        check_clone.id.clone(),
                        run_single_check(&check_clone, &options_clone).await,
                    )
                }));
            }
        }

        // Collect results
        let results = futures::future::join_all(handles).await;

        // Update checklist with results
        for (check_id, evidence) in results.into_iter().flatten() {
            if let Some(check) = checklist.checks.iter_mut().find(|c| c.id == check_id) {
                check.evidence = Some(evidence);
                check.completed = true;
            }
        }
    } else {
        // Run checks sequentially
        for check_id in check_ids_to_run {
            if let Some(check_idx) = checklist.checks.iter().position(|c| c.id == check_id) {
                let check_clone = checklist.checks[check_idx].clone();
                let evidence = run_single_check(&check_clone, &options).await;

                checklist.checks[check_idx].evidence = Some(evidence.clone());
                checklist.checks[check_idx].completed = true;

                // Stop on first failure if failFast is enabled
                if options.fail_fast && !evidence.passed {
                    break;
                }
            }
        }
    }

    // Generate summary
    checklist.summary = Some(generate_summary(&checklist));
    checklist.completed_at = Some(SystemTime::now());
    checklist.status = if checklist.summary.as_ref().unwrap().all_required_passed {
        VerificationStatus::Complete
    } else {
        VerificationStatus::Failed
    };

    checklist
}

/// Validate evidence for a specific check
pub fn check_evidence(
    check: &VerificationCheck,
    evidence: &VerificationEvidence,
) -> ValidationResult {
    let mut issues = vec![];
    let mut recommendations = vec![];

    // Check evidence type matches
    if evidence.evidence_type != check.evidence_type {
        issues.push(format!(
            "Evidence type mismatch: expected {:?}, got {:?}",
            check.evidence_type, evidence.evidence_type
        ));
    }

    // Check if passed
    if !evidence.passed {
        issues.push(format!("Check failed: {}", check.name));
        if let Some(error) = &evidence.error {
            issues.push(format!("Error: {}", error));
        }
        if let Some(cmd) = &check.command {
            recommendations.push(format!("Review command output: {}", cmd));
        }
        recommendations.push("Fix the issue and re-run verification".to_string());
    }

    // Check for stale evidence (older than 5 minutes)
    let five_minutes_ago = SystemTime::now() - Duration::from_secs(5 * 60);
    if evidence.timestamp < five_minutes_ago {
        issues.push("Evidence is stale (older than 5 minutes)".to_string());
        recommendations.push("Re-run verification to get fresh evidence".to_string());
    }

    ValidationResult {
        valid: issues.is_empty(),
        message: if issues.is_empty() {
            format!("{} verified successfully", check.name)
        } else {
            format!("{} verification failed", check.name)
        },
        issues,
        recommendations,
    }
}

/// Generate summary of verification results
fn generate_summary(checklist: &VerificationChecklist) -> VerificationSummary {
    let total = checklist.checks.len();
    let passed = checklist
        .checks
        .iter()
        .filter(|c| c.evidence.as_ref().map(|e| e.passed).unwrap_or(false))
        .count();
    let failed = checklist
        .checks
        .iter()
        .filter(|c| c.completed && !c.evidence.as_ref().map(|e| e.passed).unwrap_or(false))
        .count();
    let skipped = checklist.checks.iter().filter(|c| !c.completed).count();

    let required_checks: Vec<_> = checklist.checks.iter().filter(|c| c.required).collect();
    let all_required_passed = required_checks
        .iter()
        .all(|c| c.evidence.as_ref().map(|e| e.passed).unwrap_or(false));

    let failed_checks: Vec<String> = checklist
        .checks
        .iter()
        .filter(|c| c.completed && !c.evidence.as_ref().map(|e| e.passed).unwrap_or(false))
        .map(|c| c.id.clone())
        .collect();

    let verdict = if skipped > 0 {
        Verdict::Incomplete
    } else if checklist.protocol.strict_mode && failed > 0 {
        Verdict::Rejected
    } else if all_required_passed {
        Verdict::Approved
    } else {
        Verdict::Rejected
    };

    VerificationSummary {
        total,
        passed,
        failed,
        skipped,
        all_required_passed,
        failed_checks,
        verdict,
    }
}

/// Format verification report
pub fn format_report(checklist: &VerificationChecklist, options: &ReportOptions) -> String {
    if options.format == ReportFormat::Json {
        return serde_json::to_string_pretty(checklist).unwrap_or_default();
    }

    let mut lines = vec![];

    // Header
    if options.format == ReportFormat::Markdown {
        lines.push(format!(
            "# Verification Report: {}",
            checklist.protocol.name
        ));
        lines.push(String::new());
        lines.push(format!("**Status:** {:?}", checklist.status));
        lines.push(format!("**Started:** {:?}", checklist.started_at));
        if let Some(completed) = checklist.completed_at {
            lines.push(format!("**Completed:** {:?}", completed));
        }
        lines.push(String::new());
    } else {
        lines.push(format!("Verification Report: {}", checklist.protocol.name));
        lines.push(format!("Status: {:?}", checklist.status));
        lines.push(format!("Started: {:?}", checklist.started_at));
        if let Some(completed) = checklist.completed_at {
            lines.push(format!("Completed: {:?}", completed));
        }
        lines.push(String::new());
    }

    // Summary
    if let Some(summary) = &checklist.summary {
        if options.format == ReportFormat::Markdown {
            lines.push("## Summary".to_string());
            lines.push(String::new());
            lines.push(format!("- **Total Checks:** {}", summary.total));
            lines.push(format!("- **Passed:** {}", summary.passed));
            lines.push(format!("- **Failed:** {}", summary.failed));
            lines.push(format!("- **Skipped:** {}", summary.skipped));
            lines.push(format!("- **Verdict:** {:?}", summary.verdict));
            lines.push(String::new());
        } else {
            lines.push("Summary:".to_string());
            lines.push(format!("  Total Checks: {}", summary.total));
            lines.push(format!("  Passed: {}", summary.passed));
            lines.push(format!("  Failed: {}", summary.failed));
            lines.push(format!("  Skipped: {}", summary.skipped));
            lines.push(format!("  Verdict: {:?}", summary.verdict));
            lines.push(String::new());
        }
    }

    // Checks
    if options.format == ReportFormat::Markdown {
        lines.push("## Checks".to_string());
        lines.push(String::new());
    } else {
        lines.push("Checks:".to_string());
    }

    for check in &checklist.checks {
        let status = if check.evidence.as_ref().map(|e| e.passed).unwrap_or(false) {
            "✓"
        } else if check.completed {
            "✗"
        } else {
            "â—‹"
        };
        let required = if check.required {
            "(required)"
        } else {
            "(optional)"
        };

        if options.format == ReportFormat::Markdown {
            lines.push(format!("### {} {} {}", status, check.name, required));
            lines.push(String::new());
            lines.push(check.description.clone());
            lines.push(String::new());
        } else {
            lines.push(format!("  {} {} {}", status, check.name, required));
            lines.push(format!("     {}", check.description));
        }

        if options.include_evidence {
            if let Some(evidence) = &check.evidence {
                if options.format == ReportFormat::Markdown {
                    lines.push("**Evidence:**".to_string());
                    lines.push(format!("- Passed: {}", evidence.passed));
                    lines.push(format!("- Timestamp: {:?}", evidence.timestamp));
                    if let Some(cmd) = &evidence.command {
                        lines.push(format!("- Command: `{}`", cmd));
                    }
                    if let Some(error) = &evidence.error {
                        lines.push(format!("- Error: {}", error));
                    }
                } else {
                    lines.push(format!(
                        "     Evidence: {}",
                        if evidence.passed { "PASSED" } else { "FAILED" }
                    ));
                    if let Some(error) = &evidence.error {
                        lines.push(format!("     Error: {}", error));
                    }
                }

                if options.include_output {
                    if let Some(output) = &evidence.output {
                        if options.format == ReportFormat::Markdown {
                            lines.push(String::new());
                            lines.push("**Output:**".to_string());
                            lines.push("```".to_string());
                            lines.push(output.trim().to_string());
                            lines.push("```".to_string());
                        } else {
                            let truncated = if output.len() > 100 {
                                format!("{}...", &output[..100])
                            } else {
                                output.clone()
                            };
                            lines.push(format!("     Output: {}", truncated));
                        }
                    }
                }

                lines.push(String::new());
            }
        }
    }

    lines.join("\n")
}

/// Validate entire checklist
pub async fn validate_checklist(checklist: &VerificationChecklist) -> ValidationResult {
    let mut issues = vec![];
    let mut recommendations = vec![];

    // Check if verification is complete
    if checklist.status != VerificationStatus::Complete
        && checklist.status != VerificationStatus::Failed
    {
        issues.push("Verification is not complete".to_string());
        recommendations.push("Run verification to completion before validating".to_string());
        return ValidationResult {
            valid: false,
            message: "Incomplete verification".to_string(),
            issues,
            recommendations,
        };
    }

    // Validate each check
    for check in &checklist.checks {
        if let Some(evidence) = &check.evidence {
            let validation = check_evidence(check, evidence);
            if !validation.valid && check.required {
                issues.extend(validation.issues);
                recommendations.extend(validation.recommendations);
            }
        } else if check.required {
            issues.push(format!(
                "Missing evidence for required check: {}",
                check.name
            ));
            recommendations.push(format!("Run verification check: {}", check.name));
        }
    }

    ValidationResult {
        valid: issues.is_empty(),
        message: if issues.is_empty() {
            "All verifications passed".to_string()
        } else {
            "Some verifications failed".to_string()
        },
        issues,
        recommendations,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_protocol() {
        let checks = standard_checks();
        let protocol = create_protocol(
            "test".to_string(),
            "Test protocol".to_string(),
            vec![checks["build"].clone()],
            true,
        );

        assert_eq!(protocol.name, "test");
        assert_eq!(protocol.checks.len(), 1);
        assert!(protocol.strict_mode);
    }

    #[test]
    fn test_create_checklist() {
        let checks = standard_checks();
        let protocol = create_protocol(
            "test".to_string(),
            "Test protocol".to_string(),
            vec![checks["build"].clone()],
            true,
        );

        let checklist = create_checklist(protocol);
        assert_eq!(checklist.status, VerificationStatus::Pending);
        assert_eq!(checklist.checks.len(), 1);
    }

    #[test]
    fn test_generate_summary() {
        let checks = standard_checks();
        let protocol = create_protocol(
            "test".to_string(),
            "Test protocol".to_string(),
            vec![checks["build"].clone()],
            true,
        );

        let checklist = create_checklist(protocol);
        let summary = generate_summary(&checklist);

        assert_eq!(summary.total, 1);
        assert_eq!(summary.passed, 0);
        assert_eq!(summary.verdict, Verdict::Incomplete);
    }
}