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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! Tool execution (invocation, guards, truncation)

use super::super::{ReactAgent, TOOL_FINAL_ANSWER};
use super::context::HookMessageBatches;
use crate::error::{ReactError, Result, ToolError};
use crate::guard::GuardDirection;
use crate::tools::{ToolParameters, is_read_tool, is_write_tool};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{debug, info, warn};

#[allow(dead_code)]
pub(crate) struct ToolExecutionOutcome {
    pub output: String,
    pub tool_result: Option<crate::tools::ToolResult>,
    pub hook_messages: HookMessageBatches,
}

pub(crate) struct ToolExecutionFailure {
    pub error: ReactError,
    pub hook_messages: HookMessageBatches,
}

impl ReactAgent {
    #[tracing::instrument(skip(self, input), fields(agent = %self.config.agent_name, tool.name = %tool_name))]
    pub(crate) async fn execute_tool_feedback_raw(
        &self,
        tool_name: &str,
        input: &Value,
        soften_errors: bool,
    ) -> std::result::Result<ToolExecutionOutcome, ToolExecutionFailure> {
        // If a custom pipeline is configured, delegate to it
        if let Some(ref pipeline) = self.tool_execution_pipeline {
            return self
                .execute_with_pipeline(tool_name, input, soften_errors, pipeline)
                .await;
        }

        // ── Inline implementation (fallback when no pipeline configured) ──
        let agent = self.config.agent_name.clone();
        let callbacks = self.config.callbacks.clone();
        let params: ToolParameters = if let Value::Object(map) = input {
            map.clone().into_iter().collect()
        } else {
            HashMap::new()
        };
        let mut hook_messages = HookMessageBatches::default();

        for cb in &callbacks {
            cb.on_tool_start(&agent, tool_name, input).await;
        }

        info!(agent = %agent, tool = %tool_name, "🔧 Starting tool execution");
        debug!(agent = %agent, tool = %tool_name, params = %input, "Tool parameter details");

        // ── PreToolUse hooks (execute before approval, allow hook to intercept or modify params) ──
        let mut effective_params = params;
        let mut hook_modified_input = input.clone();
        let has_hooks = {
            let hook_reg = self.tools.hook_registry.read().await;
            !hook_reg.is_empty()
        };
        if has_hooks {
            // Clone registry to release lock BEFORE awaiting hooks.
            // Prevents deadlock when hook triggers nested tool calls
            // that re-enter execute_tool and try to acquire the same RwLock.
            let hook_reg = {
                let guard = self.tools.hook_registry.read().await;
                guard.clone()
            };
            let hook_result = hook_reg
                .run_pre_tool_use(tool_name, input, self.config.get_session_id().unwrap_or(""))
                .await;
            hook_messages.pre = hook_result.messages.clone();

            if hook_result.block {
                let reason = hook_result
                    .block_reason
                    .unwrap_or_else(|| "blocked by skill hook".into());
                info!(agent = %agent, tool = %tool_name, reason = %reason, "Hook blocked tool");
                return Ok(ToolExecutionOutcome {
                    tool_result: None,
                    output: format!("Tool {} blocked by hook: {}", tool_name, reason),
                    hook_messages,
                });
            }

            if let Some(updated) = hook_result.updated_input {
                hook_modified_input = updated.clone();
                if let Value::Object(map) = &updated {
                    effective_params = map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                }
            }
        }

        // ── Unified approval check ──
        // PermissionService → PermissionPolicy
        // Returns the user-modified parameters during approval (if any)
        let approval_modified_args = self
            .check_tool_approval(tool_name, &hook_modified_input)
            .await
            .map_err(|error| ToolExecutionFailure {
                error,
                hook_messages: hook_messages.clone(),
            })?;

        // If the user modified parameters during approval, override actual execution parameters
        if let Some(modified) = approval_modified_args
            && let Value::Object(map) = &modified
        {
            effective_params = map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
        }

        // ── Safety notice for destructive tools ──
        if is_write_tool(tool_name) || tool_name == "shell" || tool_name == "delete_file" {
            let risk = match tool_name {
                "shell" => "Command execution",
                "delete_file" => "File deletion",
                _ => "File modification",
            };
            let path = extract_path_param(tool_name, &effective_params)
                .unwrap_or_else(|| "unknown".into());
            info!(
                agent = %agent,
                tool = %tool_name,
                path = %path,
                risk = %risk,
                "Safety: {tool_name} will {risk} at {path}"
            );
            self.record_trace_event(crate::trace::RunEvent::PermissionDecision {
                tool: tool_name.to_string(),
                decision: "allow".into(),
                reason: format!("{risk} at {path}"),
            })
            .await;
        }

        // ── Read-before-edit enforcement ──
        if self.config.force_read_before_edit
            && is_write_tool(tool_name)
            && let Some(path) = extract_path_param(tool_name, &effective_params)
        {
            let canonical = std::fs::canonicalize(&path)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|_| path.clone());
            if !self.was_file_read(&canonical) {
                let msg = format!(
                    "Read-before-edit is enabled. File '{}' has not been read in this conversation turn. \
                         Use read_file to read it first, then retry this operation.",
                    path
                );
                warn!(agent = %agent, tool = %tool_name, path = %path, "Read-before-edit rejected");
                return Ok(ToolExecutionOutcome {
                    tool_result: None,
                    output: msg,
                    hook_messages,
                });
            }
        }

        let execution_start = std::time::Instant::now();

        let call_id = format!("call_{}", uuid::Uuid::new_v4());
        // Record ToolCall trace event (redaction handled by new_tool_call)
        self.record_trace_event(crate::trace::RunEvent::new_tool_call(
            call_id.clone(),
            tool_name.to_string(),
            Some(input.clone()),
            None,
            0,
        ))
        .await;

        let result = match self
            .tools
            .tool_manager
            .execute_tool(tool_name, effective_params.clone())
            .await
        {
            Ok(result) => result,
            Err(error) => {
                // Apply softening logic for tool execution errors (connection failures etc.)
                // so transient MCP/network errors don't terminate the agent stream.
                let error_msg = error.to_string();
                warn!(agent = %agent, tool = %tool_name, error = %error_msg, "💥 Tool execution failed");

                // Record ToolError trace event
                self.record_trace_event(crate::trace::RunEvent::ToolError {
                    call_id: call_id.clone(),
                    name: tool_name.to_string(),
                    message: error_msg.clone(),
                })
                .await;

                for cb in &callbacks {
                    cb.on_tool_error(&agent, tool_name, &error).await;
                }
                self.log_tool_call_audit(tool_name, input, &error_msg, false, 0)
                    .await;

                // ── PostToolUseFailure hook ──
                self.run_post_failure_hook(
                    &agent,
                    tool_name,
                    input,
                    &error_msg,
                    &mut hook_messages,
                )
                .await?;

                if soften_errors && tool_name != TOOL_FINAL_ANSWER {
                    warn!(
                        agent = %agent,
                        tool = %tool_name,
                        error = %error,
                        "⚠️ Tool error converted to observation and sent back to LLM"
                    );
                    return Ok(ToolExecutionOutcome {
                        tool_result: None,
                        output: format!(
                            "[Tool execution failed] {error}\nTip: adjust parameters based on the error and retry, or try other tools."
                        ),
                        hook_messages,
                    });
                } else {
                    return Err(ToolExecutionFailure {
                        error,
                        hook_messages,
                    });
                }
            }
        };
        let duration_ms = execution_start.elapsed().as_millis() as u64;

        // Record ToolResult trace event
        self.record_trace_event(crate::trace::RunEvent::ToolResult {
            call_id: call_id.clone(),
            name: tool_name.to_string(),
            success: true,
            output_preview: Some(result.output.chars().take(200).collect()),
            output_truncated: false,
            duration_ms,
        })
        .await;

        // ── Record file reads for read-before-edit enforcement ──
        if result.success
            && is_read_tool(tool_name)
            && let Some(path) = extract_path_param(tool_name, &effective_params)
        {
            let canonical = std::fs::canonicalize(&path)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|_| path.clone());
            self.record_file_read(&canonical);
        }

        // Record FileEdit trace event for write tools
        if result.success
            && is_write_tool(tool_name)
            && let Some(path) = extract_path_param(tool_name, &effective_params)
        {
            self.record_trace_event(crate::trace::RunEvent::FileEdit {
                tool: tool_name.to_string(),
                path,
            })
            .await;
        }

        // ── PostToolUse hooks ──
        let is_hook_post = {
            let hook_reg = self.tools.hook_registry.read().await;
            !hook_reg.is_empty()
        };
        if is_hook_post {
            // Clone registry to release lock BEFORE awaiting hooks (prevent deadlock).
            let hook_reg = {
                let guard = self.tools.hook_registry.read().await;
                guard.clone()
            };
            let post_result = hook_reg
                .run_post_tool_use(
                    tool_name,
                    input,
                    &result.output,
                    self.config.get_session_id().unwrap_or(""),
                )
                .await;
            hook_messages.post = post_result.messages;
            if post_result.block {
                info!(agent = %agent, tool = %tool_name, reason = ?post_result.block_reason, "PostToolUse hook blocked tool output");
                let blocked_output = post_result
                    .block_reason
                    .unwrap_or_else(|| format!("Tool {} output blocked by hook", tool_name));
                return Ok(ToolExecutionOutcome {
                    tool_result: None,
                    output: blocked_output,
                    hook_messages,
                });
            }
        }

        if result.success {
            info!(agent = %agent, tool = %tool_name, "📤 Tool executed successfully");
            debug!(agent = %agent, tool = %tool_name, output = %result.output, "Tool output details");

            // Run output guard checks to prevent malicious content injection
            if let Some(guard_output) = self.check_tool_output_guard(&result.output).await {
                debug!(agent = %agent, tool = %tool_name, "🛡️ Tool output filtered by guard");
                for cb in callbacks.iter() {
                    cb.on_tool_end(&agent, tool_name, &guard_output).await;
                }
                self.log_tool_call_audit(tool_name, input, &guard_output, true, duration_ms)
                    .await;
                return Ok(ToolExecutionOutcome {
                    tool_result: None,
                    output: guard_output,
                    hook_messages,
                });
            }

            for cb in callbacks.iter() {
                cb.on_tool_end(&agent, tool_name, &result.output).await;
            }
            self.log_tool_call_audit(tool_name, input, &result.output, true, duration_ms)
                .await;
            Ok(ToolExecutionOutcome {
                tool_result: None,
                output: result.output,
                hook_messages,
            })
        } else {
            let error_msg = result
                .error
                .clone()
                .unwrap_or_else(|| result.output.clone());
            warn!(agent = %agent, tool = %tool_name, error = %error_msg, "💥 Tool execution failed");
            let err = ReactError::from(ToolError::ExecutionFailed {
                tool: tool_name.to_string(),
                message: error_msg.clone(),
            });
            for cb in &callbacks {
                cb.on_tool_error(&agent, tool_name, &err).await;
            }
            self.log_tool_call_audit(tool_name, input, &error_msg, false, duration_ms)
                .await;

            // ── PostToolUseFailure hook ──
            self.run_post_failure_hook(&agent, tool_name, input, &error_msg, &mut hook_messages)
                .await?;

            if soften_errors && tool_name != TOOL_FINAL_ANSWER {
                warn!(
                    agent = %agent,
                    tool = %tool_name,
                    error = %err,
                    "⚠️ Tool error converted to observation and sent back to LLM"
                );
                Ok(ToolExecutionOutcome {
                    tool_result: None,
                    output: format!(
                        "[Tool execution failed] {err}\nTip: adjust parameters based on the error and retry, or try other tools."
                    ),
                    hook_messages,
                })
            } else {
                Err(ToolExecutionFailure {
                    error: err,
                    hook_messages,
                })
            }
        }
    }

    /// Run the PostToolUseFailure hook and check for block.
    ///
    /// Returns `Err(ToolExecutionFailure)` if a hook blocks the error output.
    async fn run_post_failure_hook(
        &self,
        agent: &str,
        tool_name: &str,
        input: &Value,
        error_msg: &str,
        hook_messages: &mut HookMessageBatches,
    ) -> std::result::Result<(), ToolExecutionFailure> {
        let hook_reg = {
            let guard = self.tools.hook_registry.read().await;
            guard.clone()
        };
        let failure_result = hook_reg
            .run_post_tool_use_failure(
                tool_name,
                input,
                error_msg,
                self.config.get_session_id().unwrap_or(""),
            )
            .await;
        hook_messages.post = failure_result.messages;
        if failure_result.block {
            info!(agent = %agent, tool = %tool_name, reason = ?failure_result.block_reason, "PostToolUseFailure hook blocked error output");
            let blocked_msg = failure_result
                .block_reason
                .unwrap_or_else(|| format!("Tool {} error output blocked by hook", tool_name));
            return Err(ToolExecutionFailure {
                error: ReactError::Other(blocked_msg),
                hook_messages: hook_messages.clone(),
            });
        }
        Ok(())
    }

    /// Execute tool, preserving the real error information returned by the tool
    #[allow(dead_code)]
    pub(crate) async fn execute_tool(&self, tool_name: &str, input: &Value) -> Result<String> {
        match self
            .execute_tool_feedback_raw(tool_name, input, false)
            .await
        {
            Ok(outcome) => {
                self.apply_hook_messages(tool_name, &outcome.hook_messages)
                    .await;
                Ok(outcome.output)
            }
            Err(failure) => {
                self.apply_hook_messages(tool_name, &failure.hook_messages)
                    .await;
                Err(failure.error)
            }
        }
    }

    /// Truncate tool output based on token budget.
    ///
    /// When `max_tool_output_tokens` is configured and the estimated output tokens
    /// exceed the limit, the output is truncated to a **head + tail** view:
    /// the first ~70% of the budget is taken from the beginning, the remaining
    /// ~30% from the end.  Cut points are aligned to newline boundaries so that
    /// code blocks, JSON structures, and log lines stay intact.
    pub(crate) async fn truncate_tool_output(&self, output: String) -> String {
        // Only apply truncation/summary when a max token limit is configured
        let Some(max_tokens) = self.config.max_tool_output_tokens else {
            return output;
        };

        let ctx = self.memory.context.lock().await;
        let tokenizer = ctx.tokenizer();
        let token_count = tokenizer.count_tokens(&output);
        if token_count <= max_tokens {
            drop(ctx);
            return output;
        }
        drop(ctx);

        // Event-level summary: for long tool outputs (>2000 chars), generate a structured summary
        const SUMMARY_THRESHOLD: usize = 2000;
        if output.len() > SUMMARY_THRESHOLD {
            let line_count = output.lines().count();
            let char_count = output.len();
            let first_line = output.lines().next().unwrap_or("");
            let summary = format!(
                "[Summary: {char_count} chars, ~{line_count} lines. First line: {first_line}]\n\n"
            );
            let head: String = output.chars().skip(first_line.len()).take(1400).collect();
            let tail: String = output
                .chars()
                .rev()
                .take(400)
                .collect::<String>()
                .chars()
                .rev()
                .collect();
            return format!("{summary}{head}\n...\n{tail}");
        }

        // Large output spill-to-disk: if output exceeds 1MB, write to temp file
        const SPILL_THRESHOLD: usize = 1_048_576; // 1MB
        if output.len() > SPILL_THRESHOLD {
            let tmp_dir = std::env::temp_dir().join("echo_agent_spill");
            let _ = std::fs::create_dir_all(&tmp_dir);
            match tempfile::NamedTempFile::new_in(&tmp_dir) {
                Ok(mut tmp) => {
                    use std::io::Write;
                    if tmp.write_all(output.as_bytes()).is_ok() {
                        // Persist the temp file so it can be read later
                        match tmp.keep() {
                            Ok((_, path)) => {
                                let preview: String = output.chars().take(500).collect();
                                return format!(
                                    "{preview}\n\n[Output spilled to disk: {} ({:.1}MB). Use read_file to read the full output.]",
                                    path.display(),
                                    output.len() as f64 / 1_048_576.0
                                );
                            }
                            Err(_) => { /* keep failed, fall through to truncation */ }
                        }
                    }
                }
                Err(_) => { /* temp file creation failed, fall through to truncation */ }
            }
        }

        let Some(max_tokens) = self.config.max_tool_output_tokens else {
            return output;
        };
        let ctx = self.memory.context.lock().await;
        let tokenizer = ctx.tokenizer();
        let token_count = tokenizer.count_tokens(&output);
        if token_count <= max_tokens {
            drop(ctx);
            return output;
        }

        let notice = format!(
            "\n\n[... output truncated: {} tokens total → {} tokens shown ...]\n\n",
            token_count, max_tokens,
        );
        let notice_tokens = tokenizer.count_tokens(&notice);
        let available = max_tokens.saturating_sub(notice_tokens);

        // If the budget is too tight for a meaningful split, fall back to
        // prefix-only with a short suffix.
        if available < 4 {
            drop(ctx);
            let truncated: String = output.chars().take(max_tokens * 4).collect();
            return format!("{truncated}\n[Output truncated, total {token_count} tokens]");
        }

        let head_budget = (available as f64 * 0.7) as usize;
        let tail_budget = available.saturating_sub(head_budget);

        let chars: Vec<char> = output.chars().collect();
        let char_per_token = chars.len() as f64 / token_count as f64;

        // ── head ──────────────────────────────────────────────────
        let head_char_end = {
            let est = (head_budget as f64 * char_per_token * 1.05) as usize;
            newline_boundary_fwd(&chars, est.min(chars.len()))
        };
        let head: String = chars[..head_char_end].iter().collect();
        let actual_head = tokenizer.count_tokens(&head);
        let head = if actual_head > head_budget {
            let scale = head_budget as f64 / actual_head as f64;
            let adj = newline_boundary_fwd(&chars, (head_char_end as f64 * scale) as usize);
            chars[..adj].iter().collect::<String>()
        } else {
            head
        };

        // ── tail ──────────────────────────────────────────────────
        let tail_char_start = {
            let est = chars
                .len()
                .saturating_sub((tail_budget as f64 * char_per_token * 1.05) as usize);
            newline_boundary_rev(&chars, est)
        };
        let tail: String = chars[tail_char_start..].iter().collect();
        let actual_tail = tokenizer.count_tokens(&tail);
        let tail = if actual_tail > tail_budget {
            let scale = tail_budget as f64 / actual_tail as f64;
            let keep = (tail.len() as f64 * scale) as usize;
            let adj =
                newline_boundary_rev(&chars, tail_char_start + tail.len().saturating_sub(keep));
            chars[adj..].iter().collect::<String>()
        } else {
            tail
        };

        drop(ctx);
        format!("{head}{notice}{tail}")
    }

    /// Perform guard check on tool output to prevent malicious content injection
    ///
    /// If a guard manager is configured, output is checked for safety.
    /// Returns `Some(filtered_output)` if output was filtered/modified,
    /// returns `None` if output is fine and needs no modification.
    pub(crate) async fn check_tool_output_guard(&self, output: &str) -> Option<String> {
        // Secret scan: redact secrets from tool output before guard check
        if crate::security::contains_secrets(output) {
            let redacted = crate::security::redact_secrets(output);
            warn!(agent = %self.config.agent_name, "Secret detected in tool output; redacted");
            return Some(redacted);
        }
        let gm = self.guard.guard_manager.as_ref()?;
        let result = gm.check_all(output, GuardDirection::Output).await.ok()?;
        if let crate::guard::GuardResult::Block { reason } = &result {
            info!(agent = %self.config.agent_name, reason = %reason, "🛡️ Tool output blocked by guard");
            if let Some(al) = &self.guard.audit_logger {
                let event = crate::audit::AuditEvent::now(
                    self.config.session_id.clone(),
                    self.config.agent_name.clone(),
                    crate::audit::AuditEventType::GuardBlock {
                        guard: "guard_manager".to_string(),
                        direction: GuardDirection::Output,
                        reason: reason.clone(),
                    },
                );
                let _ = al.log(event).await;
            }
            Some(format!("Output content filtered by safety guard: {reason}"))
        } else {
            None
        }
    }

    /// Execute a tool through the configured pipeline.
    async fn execute_with_pipeline(
        &self,
        tool_name: &str,
        input: &Value,
        soften_errors: bool,
        pipeline: &Arc<crate::agent::react::run::pipeline::ToolExecutionPipeline>,
    ) -> std::result::Result<ToolExecutionOutcome, ToolExecutionFailure> {
        let _agent_name = self.config.agent_name.clone();
        let params: ToolParameters = if let Value::Object(map) = input {
            map.clone().into_iter().collect()
        } else {
            HashMap::new()
        };

        let mut ctx = crate::agent::react::run::pipeline::ToolExecutionContext {
            call_id: format!("call_{}", uuid::Uuid::new_v4()),
            tool_name: tool_name.to_string(),
            params,
            input: input.clone(),
            hook_messages: HookMessageBatches::default(),
            result: None,
            output: None,
            blocked: false,
            block_reason: None,
            duration_ms: 0,
            plan_mode: self.config.plan_mode,
        };

        match pipeline.run(&mut ctx, self).await {
            Ok(()) => {
                if ctx.blocked {
                    return Ok(ToolExecutionOutcome {
                        tool_result: None,
                        output: ctx
                            .block_reason
                            .unwrap_or_else(|| format!("Tool {} blocked", tool_name)),
                        hook_messages: ctx.hook_messages,
                    });
                }

                if let Some(result) = ctx.result {
                    if result.success {
                        let output = ctx.output.unwrap_or_else(|| result.output.clone());
                        Ok(ToolExecutionOutcome {
                            tool_result: Some(result),
                            output,
                            hook_messages: ctx.hook_messages,
                        })
                    } else {
                        let error_msg = result
                            .error
                            .clone()
                            .unwrap_or_else(|| result.output.clone());
                        let err = ReactError::from(ToolError::ExecutionFailed {
                            tool: tool_name.to_string(),
                            message: error_msg.clone(),
                        });
                        if soften_errors && tool_name != TOOL_FINAL_ANSWER {
                            Ok(ToolExecutionOutcome {
                                tool_result: Some(result),
                                output: format!(
                                    "[Tool execution failed] {err}\nTip: adjust parameters based on the error and retry, or try other tools."
                                ),
                                hook_messages: ctx.hook_messages,
                            })
                        } else {
                            Err(ToolExecutionFailure {
                                error: err,
                                hook_messages: ctx.hook_messages,
                            })
                        }
                    }
                } else {
                    Err(ToolExecutionFailure {
                        error: ReactError::Other("Pipeline completed without result".into()),
                        hook_messages: ctx.hook_messages,
                    })
                }
            }
            Err(error) => Err(ToolExecutionFailure {
                error,
                hook_messages: ctx.hook_messages,
            }),
        }
    }

    /// Execute tool, deciding failure behavior based on `tool_error_feedback` config:
    /// - `true` (default): convert error info to a tool observation sent back to LLM so the model can self-correct
    /// - `false`: propagate `Err` upwards directly, matching legacy behavior
    ///
    /// The `final_answer` tool always preserves original error semantics and is never softened.
    /// Tool output goes through `truncate_tool_output` for token budget truncation.
    pub(crate) async fn execute_tool_feedback(
        &self,
        tool_name: &str,
        input: &Value,
    ) -> Result<String> {
        match self
            .execute_tool_feedback_raw(tool_name, input, self.config.tool_error_feedback)
            .await
        {
            Ok(outcome) => {
                self.apply_hook_messages(tool_name, &outcome.hook_messages)
                    .await;
                Ok(self.truncate_tool_output(outcome.output).await)
            }
            Err(failure) => {
                self.apply_hook_messages(tool_name, &failure.hook_messages)
                    .await;
                Err(failure.error)
            }
        }
    }
}

// ── truncation helpers ──────────────────────────────────────────────

/// Find the nearest newline at or before `target`, so truncation lands on
/// a natural boundary (line / code-block / JSON key end).
fn newline_boundary_fwd(chars: &[char], target: usize) -> usize {
    let t = target.min(chars.len());
    for i in (0..t).rev() {
        if chars[i] == '\n' {
            return i + 1; // include the newline in the kept portion
        }
    }
    t
}

/// Find the nearest newline at or after `target`, so the tail starts at a
/// clean line boundary.
fn newline_boundary_rev(chars: &[char], target: usize) -> usize {
    let t = target.min(chars.len());
    for i in t..chars.len() {
        if chars[i] == '\n' {
            return i + 1; // start after the newline
        }
    }
    t
}

// ── Read-before-edit helpers ─────────────────────────────────────────────────

/// Extract the target file path from the tool parameters.
/// Looks for `path` or `file_path` keys common across file tools.
fn extract_path_param(_tool_name: &str, params: &ToolParameters) -> Option<String> {
    params
        .get("path")
        .or_else(|| params.get("file_path"))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
}