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
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, error, info, warn};
use crate::error::SdkResult;
use crate::types::file_change::{ChangeType, FileChange};
use crate::types::message::{
Envelope, MessageKind, MessageTarget, PlanSubmissionPayload, ShutdownRejectedPayload,
TaskCompletePayload, TaskFailedPayload, TeammateIdlePayload,
};
use crate::types::task::{Task, TaskResult};
use crate::tools::command_tools::RunCommandTool;
use crate::tools::context_tools::{GetTaskContextTool, ListCompletedTasksTool};
use crate::tools::fs_tools::{ListDirectoryTool, ReadFileTool, WriteFileTool};
use crate::tools::memory_tools::{ListMemoryTool, ReadMemoryTool, WriteMemoryTool};
use crate::tools::registry::ToolRegistry;
use crate::tools::search_tools::SearchFilesTool;
use crate::tools::web_tools::WebSearchTool;
use super::agent_loop::AgentLoop;
use super::context::AgentContext;
use super::events::AgentEvent;
use super::handle::AgentResult;
use super::hooks::{HookEvent, HookResult};
pub struct Teammate {
ctx: AgentContext,
}
impl Teammate {
pub fn new(ctx: AgentContext) -> Self {
Self { ctx }
}
/// Build the default tool registry. Override via `PromptBuilder::customize_tools`.
fn build_tool_registry(&self) -> ToolRegistry {
let mut registry = ToolRegistry::new();
registry.register(Arc::new(ReadFileTool {
source_root: self.ctx.source_root.clone(),
work_dir: self.ctx.work_dir.clone(),
}));
registry.register(Arc::new(WriteFileTool {
work_dir: self.ctx.work_dir.clone(),
}));
registry.register(Arc::new(ListDirectoryTool {
source_root: self.ctx.source_root.clone(),
work_dir: self.ctx.work_dir.clone(),
}));
registry.register(Arc::new(SearchFilesTool {
source_root: self.ctx.source_root.clone(),
}));
registry.register(Arc::new(WebSearchTool));
registry.register(Arc::new(RunCommandTool::with_defaults(
self.ctx.work_dir.clone(),
)));
registry.register(Arc::new(ReadMemoryTool {
memory_store: self.ctx.memory_store.clone(),
}));
registry.register(Arc::new(WriteMemoryTool {
memory_store: self.ctx.memory_store.clone(),
agent_id: self.ctx.agent_id,
}));
registry.register(Arc::new(ListMemoryTool {
memory_store: self.ctx.memory_store.clone(),
}));
registry.register(Arc::new(GetTaskContextTool {
task_store: self.ctx.task_store.clone(),
}));
registry.register(Arc::new(ListCompletedTasksTool {
task_store: self.ctx.task_store.clone(),
}));
registry
}
pub async fn run(self) -> AgentResult {
let agent_id = self.ctx.agent_id;
let name = self.ctx.name.clone();
info!(agent_id = %agent_id, name = %name, "Teammate started");
let mut tasks_completed = 0;
let mut tasks_failed = 0;
let mut total_tokens = 0u64;
let mut idle_cycles = 0u32;
let max_idle_cycles = self.ctx.max_idle_cycles;
let mut mailbox = match self.ctx.broker.agent_mailbox(agent_id) {
Ok(m) => m,
Err(e) => {
error!(agent_id = %agent_id, error = %e, "Failed to open mailbox");
return AgentResult {
agent_id,
name,
tasks_completed,
tasks_failed,
total_tokens_used: total_tokens,
};
}
};
loop {
// --- Check mailbox for messages ---
if let Ok(messages) = mailbox.recv() {
for msg in &messages {
match msg.kind {
MessageKind::Shutdown => {
info!(agent_id = %agent_id, "Received shutdown (immediate)");
self.emit(AgentEvent::AgentShutdown { agent_id, name: name.clone() });
return AgentResult {
agent_id,
name,
tasks_completed,
tasks_failed,
total_tokens_used: total_tokens,
};
}
MessageKind::ShutdownRequest => {
// Shutdown negotiation: accept if idle, reject if working
let has_work = self.ctx.task_store
.completed_task_ids()
.map(|ids| {
self.ctx
.task_store
.try_claim_next(agent_id, &self.ctx.name, &ids)
.ok()
.flatten()
.is_some()
})
.unwrap_or(false);
if has_work {
info!(agent_id = %agent_id, "Rejecting shutdown: still have work");
let reply = Envelope::new(
agent_id,
MessageTarget::TeamLead,
MessageKind::ShutdownRejected,
)
.with_payload(
serde_json::to_value(ShutdownRejectedPayload {
reason: "Still processing tasks".to_string(),
})
.unwrap_or_default(),
)
.in_reply_to(msg.id);
let _ = self.ctx.broker.route(&reply);
self.emit(AgentEvent::ShutdownRejected {
agent_id,
name: name.clone(),
reason: "Still processing tasks".to_string(),
});
} else {
info!(agent_id = %agent_id, "Accepting shutdown");
let reply = Envelope::new(
agent_id,
MessageTarget::TeamLead,
MessageKind::ShutdownAccepted,
)
.in_reply_to(msg.id);
let _ = self.ctx.broker.route(&reply);
self.emit(AgentEvent::ShutdownAccepted { agent_id, name: name.clone() });
self.emit(AgentEvent::AgentShutdown { agent_id, name: name.clone() });
return AgentResult {
agent_id,
name,
tasks_completed,
tasks_failed,
total_tokens_used: total_tokens,
};
}
}
MessageKind::PlanApproved => {
debug!(agent_id = %agent_id, "Plan approved by lead");
// Plan approval is handled inside process_task
}
MessageKind::PlanRejected => {
debug!(agent_id = %agent_id, "Plan rejected by lead");
}
MessageKind::TeammateMessage => {
// Teammate-to-teammate messages can be read from context
debug!(agent_id = %agent_id, from = %msg.from, "Received teammate message");
}
_ => {}
}
}
}
// --- Try to claim and process a task ---
let completed_ids = match self.ctx.task_store.completed_task_ids() {
Ok(ids) => ids,
Err(e) => {
warn!(error = %e, "Failed to get completed task IDs");
Vec::new()
}
};
match self
.ctx
.task_store
.try_claim_next(agent_id, &self.ctx.name, &completed_ids)
{
Ok(Some(task)) => {
idle_cycles = 0;
let task_id = task.id;
info!(agent_id = %agent_id, task_id = %task_id, "Processing task: {}", task.title);
self.emit(AgentEvent::TaskStarted {
agent_id,
name: name.clone(),
task_id,
title: task.title.clone(),
});
if let Err(e) = self.ctx.task_store.mark_in_progress(task_id, agent_id) {
warn!(error = %e, "Failed to mark task in-progress");
}
match self.process_task(&task).await {
Ok((result, tokens)) => {
total_tokens += tokens;
let tool_calls = result.tool_calls_count;
let iterations = result.conversation_log.len() / 2;
// Run TaskCompleted hook
let hook_result = self.ctx.hooks.evaluate(&HookEvent::TaskCompleted {
task: task.clone(),
agent_id,
});
if let HookResult::Reject { feedback } = hook_result {
warn!(task_id = %task_id, "TaskCompleted hook rejected: {}", feedback);
self.emit(AgentEvent::HookRejected {
event_name: "TaskCompleted".to_string(),
feedback: feedback.clone(),
});
// Hook rejected completion — mark as failed so it retries
if let Err(e) = self.ctx.task_store.fail_task(
task_id,
agent_id,
format!("Hook rejected: {}", feedback),
) {
error!(error = %e, "Failed to mark task as failed after hook rejection");
}
tasks_failed += 1;
continue;
}
if let Err(e) =
self.ctx
.task_store
.complete_task(task_id, agent_id, result)
{
error!(error = %e, "Failed to complete task");
tasks_failed += 1;
} else {
tasks_completed += 1;
self.emit(AgentEvent::TaskCompleted {
agent_id,
name: name.clone(),
task_id,
tokens_used: tokens,
iterations,
tool_calls,
});
let envelope = Envelope::new(
agent_id,
MessageTarget::TeamLead,
MessageKind::TaskComplete,
)
.with_payload(
serde_json::to_value(TaskCompletePayload {
task_id,
tokens_used: tokens,
})
.unwrap_or_default(),
);
if let Err(e) = self.ctx.broker.route(&envelope) {
warn!(error = %e, "Failed to send completion message");
}
}
}
Err(e) => {
error!(task_id = %task_id, error = %e, "Task processing failed");
self.emit(AgentEvent::TaskFailed {
agent_id,
name: name.clone(),
task_id,
error: e.to_string(),
});
let error_msg = e.to_string();
if let Err(e2) = self.ctx.task_store.fail_task(
task_id,
agent_id,
error_msg.clone(),
) {
error!(error = %e2, "Failed to mark task as failed");
}
tasks_failed += 1;
let envelope = Envelope::new(
agent_id,
MessageTarget::TeamLead,
MessageKind::TaskFailed,
)
.with_payload(
serde_json::to_value(TaskFailedPayload {
task_id,
error: error_msg,
retryable: true,
})
.unwrap_or_default(),
);
if let Err(e) = self.ctx.broker.route(&envelope) {
warn!(error = %e, "Failed to send failure message");
}
}
}
}
Ok(None) => {
idle_cycles += 1;
// Notify lead when going idle (first time)
if idle_cycles == 1 {
// Run TeammateIdle hook
let hook_result = self.ctx.hooks.evaluate(&HookEvent::TeammateIdle {
agent_id,
name: self.ctx.name.clone(),
tasks_completed,
});
if let HookResult::Reject { feedback } = hook_result {
debug!(agent_id = %agent_id, "TeammateIdle hook: keep working - {}", feedback);
self.emit(AgentEvent::HookRejected {
event_name: "TeammateIdle".to_string(),
feedback,
});
idle_cycles = 0; // reset to keep trying
continue;
}
self.emit(AgentEvent::TeammateIdle {
agent_id,
name: name.clone(),
tasks_completed,
});
let envelope = Envelope::new(
agent_id,
MessageTarget::TeamLead,
MessageKind::TeammateIdle,
)
.with_payload(
serde_json::to_value(TeammateIdlePayload { tasks_completed })
.unwrap_or_default(),
);
let _ = self.ctx.broker.route(&envelope);
}
if idle_cycles >= max_idle_cycles {
debug!(agent_id = %agent_id, "No more tasks, exiting");
break;
}
}
Err(e) => {
warn!(error = %e, "Error claiming task");
idle_cycles += 1;
}
}
tokio::time::sleep(Duration::from_millis(self.ctx.poll_interval_ms)).await;
}
self.emit(AgentEvent::AgentShutdown { agent_id, name: name.clone() });
AgentResult {
agent_id,
name,
tasks_completed,
tasks_failed,
total_tokens_used: total_tokens,
}
}
async fn process_task(&self, task: &Task) -> SdkResult<(TaskResult, u64)> {
let base_tools = self.build_tool_registry();
let tools = self.ctx.prompt_builder.customize_tools(task, base_tools);
let mut system_prompt = self.ctx.prompt_builder.build_system_prompt(
task,
&self.ctx.source_root,
&self.ctx.work_dir,
);
if !self.ctx.role_prompt.trim().is_empty() {
system_prompt.push_str(&crate::prompts::teammate_role_suffix(&self.ctx.role_prompt));
}
let user_message = self.ctx.prompt_builder.build_user_message(task);
// --- Plan mode: if required, first generate a plan, wait for approval ---
if self.ctx.require_plan_approval {
let plan = self.generate_plan(task, &system_prompt).await?;
self.emit(AgentEvent::PlanSubmitted {
agent_id: self.ctx.agent_id,
name: self.ctx.name.clone(),
task_id: task.id,
plan_preview: truncate(&plan, 200),
});
// Send plan to lead for approval
let envelope = Envelope::new(
self.ctx.agent_id,
MessageTarget::TeamLead,
MessageKind::PlanSubmission,
)
.with_payload(
serde_json::to_value(PlanSubmissionPayload {
task_id: task.id,
plan: plan.clone(),
})
.unwrap_or_default(),
);
self.ctx.broker.route(&envelope)?;
// Wait for approval or rejection
self.wait_for_plan_decision(task).await?;
}
// --- Execute the task ---
let agent_loop = AgentLoop::new(
self.ctx.agent_id,
self.ctx.llm_client.clone(),
tools,
system_prompt,
self.ctx.max_loop_iterations,
)
.with_max_context_tokens(self.ctx.max_context_tokens)
.with_agent_name(&self.ctx.name);
let mut agent_loop = agent_loop;
if let Some(ref tx) = self.ctx.event_tx {
agent_loop.set_event_sink(tx.clone());
}
let loop_result = agent_loop.run(user_message).await?;
let output_files = self.collect_written_files(&loop_result.messages);
let result = TaskResult {
file_changes: output_files,
notes: loop_result.final_content.clone(),
llm_tokens_used: loop_result.total_tokens,
conversation_log: loop_result.messages,
tool_calls_count: loop_result.tool_calls_count,
extra: None,
};
Ok((result, loop_result.total_tokens))
}
/// Generate a read-only plan for a task (plan mode).
async fn generate_plan(&self, task: &Task, system_prompt: &str) -> SdkResult<String> {
let plan_prompt = crate::prompts::plan_mode_prompt(system_prompt, task);
let (plan, _tokens) = self.ctx.llm_client.ask(&plan_prompt, &task.description).await?;
Ok(plan)
}
/// Wait for the lead to approve or reject the plan.
async fn wait_for_plan_decision(&self, task: &Task) -> SdkResult<()> {
let agent_id = self.ctx.agent_id;
let mut mailbox = self.ctx.broker.agent_mailbox(agent_id)?;
let max_wait = self.ctx.plan_approval_timeout_secs;
for _ in 0..max_wait {
if let Ok(messages) = mailbox.recv() {
for msg in messages {
match msg.kind {
MessageKind::PlanApproved => {
info!(agent_id = %agent_id, task_id = %task.id, "Plan approved");
self.emit(AgentEvent::PlanApproved {
agent_id,
name: self.ctx.name.clone(),
task_id: task.id,
});
return Ok(());
}
MessageKind::PlanRejected => {
let feedback = msg.payload["feedback"]
.as_str()
.unwrap_or("No feedback")
.to_string();
info!(agent_id = %agent_id, task_id = %task.id, "Plan rejected: {}", feedback);
self.emit(AgentEvent::PlanRejected {
agent_id,
name: self.ctx.name.clone(),
task_id: task.id,
feedback,
});
// On rejection, generate a new plan (recursive retry)
// For now, just proceed with implementation
return Ok(());
}
MessageKind::Shutdown | MessageKind::ShutdownRequest => {
return Err(crate::error::SdkError::AgentCrashed {
agent_id,
reason: "Shutdown while waiting for plan approval".to_string(),
});
}
_ => {}
}
}
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
// Timeout: proceed anyway
warn!(agent_id = %agent_id, "Plan approval timed out, proceeding");
Ok(())
}
fn collect_written_files(
&self,
messages: &[crate::types::chat::ChatMessage],
) -> Vec<FileChange> {
let mut changes = Vec::new();
for msg in messages {
if let crate::types::chat::ChatMessage::Assistant { tool_calls, .. } = msg {
for tc in tool_calls {
if tc.function.name == "write_file" {
if let Ok(args) =
serde_json::from_str::<serde_json::Value>(&tc.function.arguments)
{
if let (Some(path), Some(content)) =
(args["path"].as_str(), args["content"].as_str())
{
changes.push(FileChange {
path: std::path::PathBuf::from(path),
change_type: ChangeType::Created,
original_content: None,
new_content: content.to_string(),
hunks: Vec::new(),
});
}
}
}
}
}
}
changes
}
fn emit(&self, event: AgentEvent) {
if let Some(ref tx) = self.ctx.event_tx {
let _ = tx.send(event);
}
}
}
fn truncate(s: &str, max: usize) -> String {
if s.len() <= max {
s.to_string()
} else {
format!("{}...", &s[..max])
}
}