agent_hub_runtime/core/automation/
runtime_audit.rs1use super::{AutomationDraft, Provider, TaskOrigin};
3use serde::Serialize;
4use std::path::Path;
5
6#[derive(Debug, Serialize)]
7#[serde(rename_all = "snake_case")]
8pub enum RuntimeMode {
9 SharedAgent,
10 UnverifiedAgent,
11 Script,
12 NativeHermes,
13 External,
14}
15
16#[derive(Debug, Serialize)]
17pub struct Finding {
18 pub path: String,
19 pub code: &'static str,
20}
21
22#[derive(Debug, Serialize)]
23pub struct TaskAudit {
24 pub id: String,
25 pub name: String,
26 pub mode: RuntimeMode,
27 pub findings: Vec<Finding>,
28}
29
30#[derive(Debug, Default, Serialize)]
31pub struct RuntimeAudit {
32 pub tasks: Vec<TaskAudit>,
33 pub discovery_issues: Vec<String>,
34}
35
36impl RuntimeAudit {
37 pub fn is_clean(&self) -> bool {
38 self.discovery_issues.is_empty() && self.tasks.iter().all(|task| task.findings.is_empty())
39 }
40}
41
42fn inspect(id: String, draft: &AutomationDraft) -> TaskAudit {
43 let agent = draft
44 .files
45 .iter()
46 .any(|file| file.path == "config/agent.json" || file.path.ends_with("/config/agent.json"));
47 let mut shared = false;
48 let mut findings = Vec::new();
49 for file in &draft.files {
50 if !file.path.ends_with(".py") {
51 continue;
52 }
53 let body = &file.content;
54 shared |=
55 body.trim() == crate::default_content!("automations/_shared/agent-entry.py").trim();
56 let code = if body.contains("def previous_agent(") && body.contains("def wait_for_result(")
57 {
58 Some("embedded_agent_runtime")
59 } else if body.contains("class TaskState:") && body.contains("def extension(") {
60 Some("embedded_state_store")
61 } else {
62 None
63 };
64 if let Some(code) = code {
65 findings.push(Finding {
66 path: file.path.clone(),
67 code,
68 });
69 }
70 }
71 if agent && !shared {
72 findings.push(Finding {
73 path: "scripts/".into(),
74 code: "agent_adapter_needs_review",
75 });
76 }
77 if draft.provider == Provider::System {
78 if let Some(entry) = draft
79 .files
80 .iter()
81 .find(|file| file.path == "scripts/run.sh")
82 {
83 if !entry.content.contains("dirname") && entry.content.contains("$AGENT_HUB_SCRIPT_DIR")
84 {
85 findings.push(Finding {
86 path: entry.path.clone(),
87 code: "entry_requires_inherited_directory",
88 });
89 }
90 }
91 }
92 let mode = if draft.provider == Provider::Hermes {
93 RuntimeMode::NativeHermes
94 } else if agent && shared {
95 RuntimeMode::SharedAgent
96 } else if agent {
97 RuntimeMode::UnverifiedAgent
98 } else {
99 RuntimeMode::Script
100 };
101 TaskAudit {
102 id,
103 name: draft.name.clone(),
104 mode,
105 findings,
106 }
107}
108
109pub fn repository(root: &Path) -> Result<RuntimeAudit, String> {
111 let catalog = super::package::catalog::scan_portable(root)?;
112 if catalog.packages.is_empty() && catalog.issues.is_empty() {
113 return Err("No task packages found in the selected directory".into());
114 }
115 Ok(RuntimeAudit {
116 tasks: catalog
117 .packages
118 .into_iter()
119 .map(|entry| inspect(entry.path, &entry.package.draft))
120 .collect(),
121 discovery_issues: catalog
122 .issues
123 .into_iter()
124 .map(|issue| format!("{}: {}", issue.path, issue.message))
125 .collect(),
126 })
127}
128
129pub fn installed() -> RuntimeAudit {
131 let inventory = super::overview(None);
132 let tasks = inventory
133 .tasks
134 .into_iter()
135 .filter(|task| !task.is_builtin)
136 .map(|task| {
137 if task.origin == TaskOrigin::AgentHub || task.source == "hermes" {
138 if let Some(draft) = task.definition {
139 return inspect(task.id, &draft);
140 }
141 return TaskAudit {
142 id: task.id,
143 name: task.name,
144 mode: RuntimeMode::External,
145 findings: vec![Finding {
146 path: "config/task.json".into(),
147 code: "task_definition_unavailable",
148 }],
149 };
150 }
151 TaskAudit {
152 id: task.id,
153 name: task.name,
154 mode: RuntimeMode::External,
155 findings: Vec::new(),
156 }
157 })
158 .collect();
159 RuntimeAudit {
160 tasks,
161 discovery_issues: inventory
162 .issues
163 .into_iter()
164 .map(|issue| format!("{}: {}", issue.source, issue.message))
165 .collect(),
166 }
167}
168
169#[cfg(test)]
170mod tests;