echo_agent 0.2.0

Production-grade AI Agent framework for Rust — ReAct engine, multi-agent, memory, streaming, MCP, IM channels, workflows
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
//! Tool execution approval (human-in-the-loop)

use super::super::ReactAgent;
use crate::error::{ReactError, Result};
use serde_json::Value;
use tracing::{info, warn};

impl ReactAgent {
    #[cfg(feature = "human-loop")]
    /// Determine whether a tool requires human approval (for process_steps to decide serial/parallel execution)
    ///
    /// Prioritize PermissionService (passing real tool params instead of empty JSON), fall back to legacy two-phase check.
    pub(crate) async fn tool_needs_approval(&self, tool_name: &str) -> bool {
        // 1. PermissionService unified pipeline (fast path: no handler triggers)
        if let Some(service) = &self.approval.permission_service {
            self.flush_pending_permission_rules(service).await;
            let mode = service.mode().await;
            // BypassPermissions / Auto / DontAsk modes do not need serial approval wait
            if matches!(
                mode,
                crate::tools::permission::PermissionMode::BypassPermissions
                    | crate::tools::permission::PermissionMode::DontAsk
            ) {
                return false;
            }
            // Plan mode directly denies write operations (no serial approval needed)
            if mode == crate::tools::permission::PermissionMode::Plan {
                return false;
            }

            // Get actual tool permissions to pass to service.check_with_permissions
            let tool_perms = self
                .tools
                .tool_manager
                .get_tool(tool_name)
                .map(|t| t.permissions())
                .unwrap_or_default();

            // Use check_with_permissions with real perms instead of empty JSON
            let decision = service
                .check_with_permissions(tool_name, &serde_json::json!({}), &tool_perms)
                .await
                .unwrap_or(crate::tools::permission::PermissionDecision::RequireApproval);

            return decision.requires_approval();
        }

        // 2. PermissionPolicy fallback
        if let Some(policy) = &self.guard.permission_policy {
            let tool_perms = self
                .tools
                .tool_manager
                .get_tool(tool_name)
                .map(|t| t.permissions())
                .unwrap_or_default();

            if !tool_perms.is_empty() {
                let decision = policy.check(tool_name, &tool_perms).await;
                if matches!(
                    decision,
                    crate::tools::permission::PermissionDecision::RequireApproval
                        | crate::tools::permission::PermissionDecision::Ask { .. }
                ) {
                    return true;
                }
            }
        }

        false
    }

    #[cfg(not(feature = "human-loop"))]
    pub(crate) async fn tool_needs_approval(&self, _tool_name: &str) -> bool {
        false
    }

    /// Unified approval check entry point
    ///
    /// Prioritizes PermissionService (unified pipeline: mode → rules → cache → denial → classifier/handler),
    /// then PermissionPolicy check; if approval is required, requests human intervention.
    ///
    /// Returns `Ok(Some(modified_args))` if the user modified parameters during approval (caller should use modified params).
    /// Returns `Ok(None)` if approved, use original params.
    #[cfg(feature = "human-loop")]
    pub(crate) async fn check_tool_approval(
        &self,
        tool_name: &str,
        input: &Value,
    ) -> Result<Option<Value>> {
        let agent = &self.config.agent_name;

        // ── Phase -1: PermissionRequest hook ──
        {
            let hook_ctx = crate::skills::hooks::HookContext::for_permission_request(
                tool_name,
                input,
                self.config.session_id.as_deref().unwrap_or(""),
                &self.config.agent_name,
            );
            let registry = self.tools.hook_registry.read().await.clone();
            let perm_result = registry.run_lifecycle_hooks(&hook_ctx).await;
            // Check block first: a hook may signal block via exit code 2 or
            // {"decision": "block"} without setting a PermissionDecision.
            if perm_result.block {
                let reason = perm_result
                    .block_reason
                    .unwrap_or_else(|| "blocked by hook".to_string());
                let hook_result = self.log_permission_denied(tool_name, &[], &reason).await;
                let retry_hint = if hook_result.retry {
                    " (retry allowed by hook)"
                } else {
                    ""
                };
                return Err(ReactError::Other(format!(
                    "Hook blocked permission for {tool_name}: {reason}{retry_hint}"
                )));
            }
            if let Some(decision) = perm_result.permission_decision {
                match decision {
                    crate::tools::permission::PermissionDecision::Allow => {
                        info!(agent = %agent, tool = %tool_name, "🔓 PermissionRequest hook auto-approved");
                        return Ok(None);
                    }
                    crate::tools::permission::PermissionDecision::Deny { reason } => {
                        let hook_result = self.log_permission_denied(tool_name, &[], &reason).await;
                        let retry_hint = if hook_result.retry {
                            " (retry allowed by hook)"
                        } else {
                            ""
                        };
                        return Err(ReactError::Other(format!(
                            "Hook denied permission for {tool_name}: {reason}{retry_hint}"
                        )));
                    }
                    _ => {} // Ask/RequireApproval: fall through to normal approval flow
                }
            }
        }

        // ── Phase 0: PermissionService unified pipeline ──
        if let Some(service) = &self.approval.permission_service {
            self.flush_pending_permission_rules(service).await;
            let tool_perms = self
                .tools
                .tool_manager
                .get_tool(tool_name)
                .map(|t| t.permissions())
                .unwrap_or_default();

            let decision = service
                .check_with_permissions(tool_name, input, &tool_perms)
                .await
                .map_err(|e| ReactError::Other(format!("PermissionService error: {e}")))?;

            match decision {
                crate::tools::permission::PermissionDecision::Allow => {
                    let modified = service.take_modified_args().await;
                    return Ok(modified);
                }
                crate::tools::permission::PermissionDecision::Deny { reason } => {
                    let hook_result = self
                        .log_permission_denied(tool_name, &tool_perms, &reason)
                        .await;
                    let retry_hint = if hook_result.retry {
                        " (retry allowed by hook)"
                    } else {
                        ""
                    };
                    return Err(ReactError::Other(format!(
                        "Tool {tool_name} has insufficient permissions: {reason}{retry_hint}"
                    )));
                }
                crate::tools::permission::PermissionDecision::RequireApproval => {
                    info!(agent = %agent, tool = %tool_name, "🔐 PermissionService requires human approval");
                    return self.request_human_approval(tool_name, input).await;
                }
                crate::tools::permission::PermissionDecision::Ask { suggestions } => {
                    return self.handle_ask_decision(tool_name, &suggestions).await;
                }
            }
        }

        // ── Phase 1 (fallback): PermissionPolicy check ──
        if let Some(policy) = &self.guard.permission_policy {
            let tool_perms = self
                .tools
                .tool_manager
                .get_tool(tool_name)
                .map(|t| t.permissions())
                .unwrap_or_default();

            if !tool_perms.is_empty() {
                let decision = policy.check(tool_name, &tool_perms).await;
                match decision {
                    crate::tools::permission::PermissionDecision::Allow => {}
                    crate::tools::permission::PermissionDecision::Deny { reason } => {
                        let hook_result = self
                            .log_permission_denied(tool_name, &tool_perms, &reason)
                            .await;
                        let retry_hint = if hook_result.retry {
                            " (retry allowed by hook)"
                        } else {
                            ""
                        };
                        return Err(ReactError::Other(format!(
                            "Tool {tool_name} has insufficient permissions: {reason}{retry_hint}"
                        )));
                    }
                    crate::tools::permission::PermissionDecision::RequireApproval => {
                        info!(agent = %agent, tool = %tool_name, "🔐 PermissionPolicy requires human approval");
                        return self.request_human_approval(tool_name, input).await;
                    }
                    crate::tools::permission::PermissionDecision::Ask { suggestions } => {
                        return self.handle_ask_decision(tool_name, &suggestions).await;
                    }
                }
            }
        }

        Ok(None)
    }

    #[cfg(not(feature = "human-loop"))]
    pub(crate) async fn check_tool_approval(
        &self,
        tool_name: &str,
        _input: &Value,
    ) -> Result<Option<Value>> {
        // Record PermissionDecision trace event (allowed — no human-loop)
        self.record_trace_event(crate::trace::RunEvent::PermissionDecision {
            tool: tool_name.to_string(),
            decision: "allow".into(),
            reason: "Permission check passed (no human-loop)".into(),
        })
        .await;
        Ok(None)
    }

    /// Log permission denied audit event and fire PermissionDenied hook
    #[cfg(feature = "human-loop")]
    async fn log_permission_denied(
        &self,
        tool_name: &str,
        tool_perms: &[crate::tools::permission::ToolPermission],
        reason: &str,
    ) -> crate::skills::hooks::HookResult {
        let agent = &self.config.agent_name;
        let session_id = self.config.session_id.clone().unwrap_or_default();
        warn!(agent = %agent, tool = %tool_name, reason = %reason, "🔒 Permission denied");
        if let Some(al) = &self.guard.audit_logger {
            let event = crate::audit::AuditEvent::now(
                self.config.session_id.clone(),
                agent.to_string(),
                crate::audit::AuditEventType::PermissionDenied {
                    tool: tool_name.to_string(),
                    required: tool_perms.to_vec(),
                    reason: reason.to_string(),
                },
            );
            let _ = al.log(event).await;
        }

        // Fire PermissionDenied hook
        let hook_ctx = crate::skills::hooks::HookContext::for_permission_denied(
            tool_name,
            &serde_json::json!({}),
            reason,
            true, // retry_allowed: model can retry with different params
            &session_id,
            agent,
        );
        let registry = self.tools.hook_registry.read().await.clone();
        registry.run_lifecycle_hooks(&hook_ctx).await
    }

    /// Handle Ask decision — confirm tool execution with the user
    #[cfg(feature = "human-loop")]
    async fn handle_ask_decision(
        &self,
        tool_name: &str,
        suggestions: &[String],
    ) -> Result<Option<Value>> {
        let agent = &self.config.agent_name;
        info!(agent = %agent, tool = %tool_name, "❓ Permission requires user confirmation");
        let prompt = format!(
            "Tool '{}' requires confirmation. Suggested options: {}",
            tool_name,
            suggestions.join(", ")
        );
        let req = crate::human_loop::HumanLoopRequest::input(prompt);
        match self.approval.approval_provider.request(req).await? {
            crate::human_loop::HumanLoopResponse::Text(response) => {
                if response.to_lowercase().contains("reject")
                    || response.to_lowercase().contains("deny")
                {
                    return Err(ReactError::Other(format!(
                        "Tool {tool_name} user chose to reject: {response}"
                    )));
                }
                info!(agent = %agent, tool = %tool_name, "✅ User confirmed execution");
            }
            crate::human_loop::HumanLoopResponse::Approved => {
                info!(agent = %agent, tool = %tool_name, "✅ User confirmed execution");
            }
            crate::human_loop::HumanLoopResponse::Rejected { reason } => {
                return Err(ReactError::Other(format!(
                    "Tool {tool_name} user rejected{}",
                    reason.map(|r| format!(", reason: {r}")).unwrap_or_default()
                )));
            }
            _ => {
                return Err(ReactError::Other(format!(
                    "Tool {tool_name} user confirmation timed out"
                )));
            }
        }
        Ok(None)
    }

    /// Request human approval and record audit log
    ///
    /// Handles all `HumanLoopResponse` variants, consistently recording approval request/completion audit events.
    /// Risk level is dynamically computed based on tool permissions rather than hardcoded.
    ///
    /// Returns `Ok(Some(modified_args))` if the user modified parameters and approved.
    /// Returns `Ok(None)` if the user directly approved.
    #[cfg(feature = "human-loop")]
    async fn request_human_approval(
        &self,
        tool_name: &str,
        input: &Value,
    ) -> Result<Option<Value>> {
        let agent = &self.config.agent_name;
        let approval_start = std::time::Instant::now();

        // Fire Notification hook (permission_prompt) before requesting human approval
        {
            let hook_ctx = crate::skills::hooks::HookContext::for_notification(
                "permission_prompt",
                self.config.session_id.as_deref().unwrap_or(""),
                &self.config.agent_name,
            );
            let registry = self.tools.hook_registry.read().await.clone();
            let notif_result = registry.run_lifecycle_hooks(&hook_ctx).await;
            // If a hook provides a permission decision, short-circuit the approval flow
            if let Some(decision) = notif_result.permission_decision {
                match decision {
                    crate::tools::permission::PermissionDecision::Allow => {
                        info!(agent = %agent, tool = %tool_name, "🔓 Notification hook auto-approved");
                        return Ok(None);
                    }
                    crate::tools::permission::PermissionDecision::Deny { reason } => {
                        return Err(ReactError::Other(format!(
                            "Hook auto-denied {tool_name}: {reason}"
                        )));
                    }
                    _ => {} // Ask/RequireApproval: continue to human approval
                }
            }
        }

        // Dynamically compute risk level based on tool permissions
        let tool_perms = self
            .tools
            .tool_manager
            .get_tool(tool_name)
            .map(|t| t.permissions())
            .unwrap_or_default();
        let risk_level = crate::human_loop::RiskLevel::from_permissions(&tool_perms);
        let risk_level_str = format!("{:?}", risk_level).to_lowercase();

        // Audit: approval request
        if let Some(al) = &self.guard.audit_logger {
            let args_hash = {
                use std::hash::{Hash, Hasher};
                let mut hasher = std::collections::hash_map::DefaultHasher::new();
                format!("{input}").hash(&mut hasher);
                format!("{:016x}", hasher.finish())
            };
            let event = crate::audit::AuditEvent::now(
                self.config.session_id.clone(),
                agent.clone(),
                crate::audit::AuditEventType::ApprovalRequested {
                    tool: tool_name.to_string(),
                    args_hash,
                    risk_level: risk_level_str,
                },
            );
            let _ = al.log(event).await;
        }

        let req = crate::human_loop::HumanLoopRequest::approval(tool_name, input.clone());
        let response = self.approval.approval_provider.request(req).await?;

        // Audit: approval completed
        if let Some(al) = &self.guard.audit_logger {
            let duration_ms = approval_start.elapsed().as_millis() as u64;
            let (decision, scope, reason) = match &response {
                crate::human_loop::HumanLoopResponse::Approved => {
                    ("approved".into(), "once".into(), None)
                }
                crate::human_loop::HumanLoopResponse::ApprovedWithScope { scope: s } => {
                    ("approved".into(), format!("{s:?}"), None)
                }
                crate::human_loop::HumanLoopResponse::ModifiedArgs { args: _, scope: s } => {
                    ("modified".into(), format!("{s:?}"), None)
                }
                crate::human_loop::HumanLoopResponse::Rejected { reason: r } => {
                    ("rejected".into(), "once".into(), r.clone())
                }
                crate::human_loop::HumanLoopResponse::Timeout => {
                    ("timeout".into(), "once".into(), None)
                }
                crate::human_loop::HumanLoopResponse::Deferred => {
                    ("deferred".into(), "once".into(), None)
                }
                crate::human_loop::HumanLoopResponse::Text(_) => {
                    ("unexpected".into(), "once".into(), None)
                }
            };
            let event = crate::audit::AuditEvent::now(
                self.config.session_id.clone(),
                agent.clone(),
                crate::audit::AuditEventType::ApprovalCompleted {
                    tool: tool_name.to_string(),
                    decision,
                    scope,
                    reason,
                    duration_ms,
                },
            );
            let _ = al.log(event).await;
        }

        match response {
            crate::human_loop::HumanLoopResponse::Approved => {
                info!(agent = %agent, tool = %tool_name, "✅ User approved tool execution");
                Ok(None)
            }
            crate::human_loop::HumanLoopResponse::ApprovedWithScope { scope: _ } => {
                info!(agent = %agent, tool = %tool_name, "✅ User approved tool execution with scope");
                Ok(None)
            }
            crate::human_loop::HumanLoopResponse::ModifiedArgs { args, scope: _ } => {
                info!(agent = %agent, tool = %tool_name, "✅ User modified parameters and approved tool execution");
                Ok(Some(args))
            }
            crate::human_loop::HumanLoopResponse::Rejected { reason } => {
                warn!(agent = %agent, tool = %tool_name, "❌ User rejected tool execution");
                Err(ReactError::Other(format!(
                    "User rejected tool execution {}{}",
                    tool_name,
                    reason.map(|r| format!(", reason: {r}")).unwrap_or_default()
                )))
            }
            crate::human_loop::HumanLoopResponse::Timeout => {
                warn!(agent = %agent, tool = %tool_name, "⏰ Approval timed out");
                Err(ReactError::Other(format!(
                    "Tool {tool_name} approval timed out, execution skipped"
                )))
            }
            crate::human_loop::HumanLoopResponse::Deferred => {
                warn!(agent = %agent, tool = %tool_name, "⏸️ User deferred approval");
                Err(ReactError::Other(format!(
                    "Tool {tool_name} approval deferred, execution skipped"
                )))
            }
            crate::human_loop::HumanLoopResponse::Text(_) => {
                warn!(agent = %agent, tool = %tool_name, "⚠️ Approval request received unexpected Text response");
                Err(ReactError::Other(format!(
                    "Tool {tool_name} approval abnormal, execution skipped"
                )))
            }
        }
    }
}