-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathdelegate.rs
More file actions
540 lines (469 loc) · 16.2 KB
/
delegate.rs
File metadata and controls
540 lines (469 loc) · 16.2 KB
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
use std::io::{
Write,
stdin,
stdout,
};
use std::path::PathBuf;
use chrono::Utc;
use crossterm::style::{
Color,
Print,
SetForegroundColor,
};
use crossterm::{
execute,
queue,
style,
};
use eyre::{
Result,
bail,
};
use schemars::JsonSchema;
use serde::{
Deserialize,
Serialize,
};
use strum::{
Display,
EnumString,
};
use crate::cli::agent::Agents;
use crate::cli::chat::tools::{
InvokeOutput,
OutputKind,
};
use crate::cli::experiment::experiment_manager::{
ExperimentManager,
ExperimentName,
};
use crate::cli::{
Agent,
DEFAULT_AGENT_NAME,
};
use crate::os::Os;
/// Launch and manage async agent processes. Delegate tasks to agents that run independently in
/// background.
///
/// Operations:
/// - launch: Start task with agent (requires task, agent optional - defaults to 'default_agent')
/// - status: Check agent status (agent optional - defaults to 'all')
/// - list: Show available agents
///
/// Only one task per agent. Files stored in ~/.aws/amazonq/.subagents/
///
/// Examples:
/// - Launch: {"operation": "launch", "agent": "rust-agent", "task": "Create snake game"}
/// - Status: {"operation": "status", "agent": "rust-agent"}
/// - List all: {"operation": "status"}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Delegate {
/// Operation to perform: launch, status, or list
pub operation: Operation,
/// Agent name to use (optional - uses "q_cli_default" if not specified)
#[serde(default)]
pub agent: Option<String>,
/// Task description (required for launch operation)
#[serde(default)]
pub task: Option<String>,
}
#[derive(Serialize, Clone, Deserialize, Debug, Display, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Operation {
/// Launch a new agent with a specified task
Launch,
/// Check the status of a specific agent or all agents if None is provided
Status,
/// List all available agents
List,
}
impl Delegate {
pub fn is_enabled(os: &Os) -> bool {
ExperimentManager::is_enabled(os, ExperimentName::Delegate)
}
pub async fn invoke(&self, os: &Os, _output: &mut impl Write, agents: &Agents) -> Result<InvokeOutput> {
if !Self::is_enabled(os) {
return Ok(InvokeOutput {
output: OutputKind::Text(
"Delegate tool is experimental and not enabled. Use /experiment to enable it.".to_string(),
),
});
}
let result = match &self.operation {
Operation::Launch => {
let task = self
.task
.as_ref()
.ok_or(eyre::eyre!("Task description is required for launch operation"))?;
let agent_name = self.agent.as_deref().unwrap_or(DEFAULT_AGENT_NAME);
launch_agent(os, agent_name, agents, task).await?
},
Operation::Status => match &self.agent {
Some(agent_name) => status_agent(os, agent_name).await?,
None => match status_all_agents(os).await {
Ok(execution) => execution,
Err(msg) => msg.to_string(),
},
},
Operation::List => agents.agents.keys().cloned().fold(
format!("Available agents: \n- {DEFAULT_AGENT_NAME}\n"),
|mut acc, name| {
acc.push_str(&format!("- {name}\n"));
acc
},
),
};
Ok(InvokeOutput {
output: OutputKind::Text(result),
})
}
pub fn queue_description(&self, output: &mut impl Write) -> Result<()> {
match self.operation {
Operation::Launch => queue!(output, style::Print("Delegating task to agent\n"))?,
Operation::Status => queue!(output, style::Print("Checking agent status\n"))?,
Operation::List => queue!(output, style::Print("Listing available agents\n"))?,
}
Ok(())
}
}
pub async fn launch_agent(os: &Os, agent: &str, agents: &Agents, task: &str) -> Result<String> {
validate_agent_availability(os, agent).await?;
// Check if agent is already running
if let Some((execution, _)) = load_agent_execution(os, agent).await? {
if execution.status == AgentStatus::Running {
return Err(eyre::eyre!(
"Agent '{}' is already running. Use status operation to check progress or wait for completion.",
agent
));
}
}
if agent == DEFAULT_AGENT_NAME {
// Show warning for default agent but no approval needed
display_default_agent_warning()?;
} else {
// Show agent info and require approval for specific agents
request_user_approval(agent, agents, task).await?;
}
spawn_agent_process(os, agent, task).await?;
Ok(format_launch_success(agent, task))
}
fn format_launch_success(agent: &str, task: &str) -> String {
format!(
"✓ Agent '{}' launched successfully.\nTask: {}\n\nUse 'status' operation to check progress.",
agent, task
)
}
pub fn display_agent_info(agent: &str, task: &str, config: &AgentConfig) -> Result<()> {
let short_desc = truncate_description(config.description.as_deref().unwrap_or("No description"));
execute!(
stdout(),
Print(format!("Agent: {}\n", agent)),
Print(format!("Description: {}\n", short_desc)),
Print(format!("Task: {}\n", task)),
)?;
if !config.allowed_tools.is_empty() {
let tools: Vec<&str> = config.allowed_tools.iter().map(|s| s.as_str()).collect();
execute!(stdout(), Print(format!("Tools: {}\n", tools.join(", "))))?;
}
// Add appropriate security warning based on agent permissions
execute!(
stdout(),
Print("\n"),
SetForegroundColor(Color::Yellow),
Print("! This task will run with the agent's specific tool permissions.\n\n"),
SetForegroundColor(Color::Reset),
)?;
Ok(())
}
pub fn truncate_description(desc: &str) -> &str {
if let Some(pos) = desc.find('.') {
&desc[..pos + 1]
} else if desc.len() > 60 {
&desc[..57]
} else {
desc
}
}
pub fn display_default_agent_warning() -> Result<()> {
execute!(
stdout(),
Print("\n"),
SetForegroundColor(Color::Yellow),
Print(
"! This task will run with trust-all permissions and can execute commands or consume system/cloud resources.\n\n"
),
SetForegroundColor(Color::Reset),
)?;
Ok(())
}
pub fn get_user_confirmation() -> Result<bool> {
execute!(
stdout(),
SetForegroundColor(Color::Yellow),
Print("Continue? [y/N]: "),
SetForegroundColor(Color::Reset),
)?;
let mut input = String::new();
stdin().read_line(&mut input)?;
let input = input.trim().to_lowercase();
if input == "y" || input == "yes" {
println!();
Ok(true)
} else {
Ok(false)
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Display, EnumString)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum AgentStatus {
Running,
Completed,
Failed,
}
impl Default for AgentStatus {
fn default() -> Self {
Self::Running
}
}
impl AgentStatus {
// No methods currently needed - all functionality is in format_status
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct AgentExecution {
#[serde(default)]
pub agent: String,
#[serde(default)]
pub task: String,
#[serde(default)]
pub status: AgentStatus,
#[serde(default, with = "chrono::serde::ts_seconds")]
pub launched_at: chrono::DateTime<chrono::Utc>,
#[serde(default, with = "chrono::serde::ts_seconds_option")]
pub completed_at: Option<chrono::DateTime<chrono::Utc>>,
#[serde(default)]
pub pid: u32,
#[serde(default)]
pub exit_code: Option<i32>,
#[serde(default)]
pub output: String,
}
impl AgentExecution {
pub fn format_status(&self) -> String {
match self.status {
AgentStatus::Running => {
format!("Agent '{}' is still running. Please wait...", self.agent)
},
AgentStatus::Completed => {
format!(
"Agent '{}' completed successfully.\n\nOutput:\n{}",
self.agent, self.output
)
},
AgentStatus::Failed => {
format!(
"Agent '{}' failed.\nExit code: {}\n\nError:\n{}",
self.agent,
self.exit_code.unwrap_or(-1),
self.output
)
},
}
}
}
#[derive(Debug, Deserialize)]
pub struct AgentConfig {
pub description: Option<String>,
#[serde(rename = "allowedTools")]
pub allowed_tools: Vec<String>,
}
impl From<&Agent> for AgentConfig {
fn from(value: &Agent) -> Self {
Self {
description: value.description.clone(),
allowed_tools: value.allowed_tools.iter().cloned().collect::<Vec<String>>(),
}
}
}
pub async fn spawn_agent_process(os: &Os, agent: &str, task: &str) -> Result<AgentExecution> {
let now = Utc::now();
// Run Q chat with specific agent in background, non-interactive
let mut cmd = tokio::process::Command::new("q");
cmd.args(["chat", "--non-interactive"]);
if agent == DEFAULT_AGENT_NAME {
cmd.arg("--trust-all-tools");
}
cmd.args(["--agent", agent, task]);
// Redirect to capture output (runs silently)
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
cmd.stdin(std::process::Stdio::null()); // No user input
cmd.envs(std::env::vars());
#[cfg(not(windows))]
cmd.process_group(0);
let child = cmd.spawn()?;
let pid = child.id().ok_or(eyre::eyre!("Process spawned had already exited"))?;
let execution = AgentExecution {
agent: agent.to_string(),
task: task.to_string(),
status: AgentStatus::Running,
launched_at: now,
completed_at: None,
pid,
exit_code: None,
output: String::new(),
};
save_agent_execution(os, &execution).await?;
// Start monitoring with the actual child process
tokio::spawn(monitor_child_process(child, execution.clone(), os.clone()));
Ok(execution)
}
async fn monitor_child_process(child: tokio::process::Child, mut execution: AgentExecution, os: Os) {
match child.wait_with_output().await {
Ok(output) => {
execution.status = if output.status.success() {
AgentStatus::Completed
} else {
AgentStatus::Failed
};
execution.completed_at = Some(Utc::now());
execution.exit_code = output.status.code();
// Combine stdout and stderr into the output field
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
execution.output = if stderr.is_empty() {
stdout.to_string()
} else {
format!("STDOUT:\n{}\n\nSTDERR:\n{}", stdout, stderr)
};
// Save to ~/.aws/amazonq/.subagents/{agent}.json
if let Err(e) = save_agent_execution(&os, &execution).await {
eprintln!("Failed to save agent execution: {}", e);
}
},
Err(e) => {
execution.status = AgentStatus::Failed;
execution.completed_at = Some(Utc::now());
execution.exit_code = Some(-1);
execution.output = format!("Failed to wait for process: {}", e);
// Save to ~/.aws/amazonq/.subagents/{agent}.json
if let Err(e) = save_agent_execution(&os, &execution).await {
eprintln!("Failed to save agent execution: {}", e);
}
},
}
}
pub async fn status_agent(os: &Os, agent: &str) -> Result<String> {
match load_agent_execution(os, agent).await? {
Some((mut execution, path)) => {
// If status is running, check if PID is still alive
if execution.status == AgentStatus::Running && execution.pid != 0 && !is_process_alive(execution.pid) {
// Process died, mark as failed
execution.status = AgentStatus::Failed;
execution.completed_at = Some(chrono::Utc::now());
execution.exit_code = Some(-1);
execution.output = "Process terminated unexpectedly (PID not found)".to_string();
// Save the updated status
save_agent_execution(os, &execution).await?;
}
if execution.status == AgentStatus::Completed {
let _ = os.fs.remove_file(path).await;
}
Ok(execution.format_status())
},
None => Ok(format!("No execution found for agent '{}'", agent)),
}
}
pub async fn status_all_agents(os: &Os) -> Result<String> {
// Because we would delete completed execution that has been read, everything that remains is
// assumed to not be stale
let mut dir_walker = os.fs.read_dir(subagents_dir(os).await?).await?;
let mut status = String::new();
while let Ok(Some(file)) = dir_walker.next_entry().await {
let file_name = file.file_name();
let bytes = os.fs.read(file.path()).await?;
let execution = serde_json::from_slice::<AgentExecution>(&bytes)?;
if execution.status != AgentStatus::Running {
let file_name = file_name
.as_os_str()
.to_str()
.ok_or(eyre::eyre!("Error obtaining execution file name"))?;
if !status.is_empty() {
status.push_str(", ");
}
status.push_str(file_name);
}
}
if status.is_empty() {
bail!("No new completed delegate task".to_string())
} else {
Ok(format!("The following delegate tasks are ready: {status}"))
}
}
#[allow(unused_variables)]
fn is_process_alive(pid: u32) -> bool {
#[cfg(unix)]
{
// Use `kill -0` to check if process exists without actually killing it
std::process::Command::new("kill")
.args(["-0", &pid.to_string()])
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
#[cfg(not(unix))]
{
// For non-Unix systems, assume process is alive (fallback)
true
}
}
pub async fn validate_agent_availability(_os: &Os, _agent: &str) -> Result<()> {
// For now, accept any agent name (no need to print here, will show in approval)
Ok(())
}
pub async fn request_user_approval(agent: &str, agents: &Agents, task: &str) -> Result<()> {
let config = agents
.agents
.get(agent)
.ok_or(eyre::eyre!("No agent by the name {agent} found"))?
.into();
display_agent_info(agent, task, &config)?;
get_user_confirmation()?;
Ok(())
}
pub async fn load_agent_execution(os: &Os, agent: &str) -> Result<Option<(AgentExecution, PathBuf)>> {
let file_path = agent_file_path(os, agent).await?;
if file_path.exists() {
let content = os.fs.read_to_string(&file_path).await?;
let execution: AgentExecution = serde_json::from_str(&content)?;
Ok(Some((execution, file_path)))
} else {
Ok(None)
}
}
pub async fn save_agent_execution(os: &Os, execution: &AgentExecution) -> Result<()> {
let file_path = agent_file_path(os, &execution.agent).await?;
let content = serde_json::to_string_pretty(execution)?;
os.fs.write(&file_path, content).await?;
Ok(())
}
pub async fn agent_file_path(os: &Os, agent: &str) -> Result<PathBuf> {
let subagents_dir = subagents_dir(os).await?;
Ok(subagents_dir.join(format!("{}.json", agent)))
}
pub async fn subagents_dir(os: &Os) -> Result<PathBuf> {
let subagents_dir = os.env.current_dir()?.join(".amazonq").join(".subagents");
if !subagents_dir.exists() {
os.fs.create_dir_all(&subagents_dir).await?;
}
Ok(subagents_dir)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_schema() {
let schema = schemars::schema_for!(Delegate);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
}